Home | History | Annotate | Line # | Download | only in ic
uha.c revision 1.24.2.4
      1  1.24.2.4   nathanw /*	$NetBSD: uha.c,v 1.24.2.4 2001/11/14 19:14:40 nathanw Exp $	*/
      2       1.1   mycroft 
      3       1.8   thorpej /*-
      4      1.15   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5       1.8   thorpej  * All rights reserved.
      6       1.8   thorpej  *
      7       1.8   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.17   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9      1.17   mycroft  * Simulation Facility, NASA Ames Research Center.
     10       1.8   thorpej  *
     11       1.8   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.8   thorpej  * modification, are permitted provided that the following conditions
     13       1.8   thorpej  * are met:
     14       1.8   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.8   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.8   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.8   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.8   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.8   thorpej  * 3. All advertising materials mentioning features or use of this software
     20       1.8   thorpej  *    must display the following acknowledgement:
     21       1.8   thorpej  *	This product includes software developed by the NetBSD
     22       1.8   thorpej  *	Foundation, Inc. and its contributors.
     23       1.8   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.8   thorpej  *    contributors may be used to endorse or promote products derived
     25       1.8   thorpej  *    from this software without specific prior written permission.
     26       1.8   thorpej  *
     27       1.8   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.8   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.8   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.8   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.8   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.8   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.8   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.8   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.8   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.8   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.8   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1   mycroft  */
     39       1.1   mycroft 
     40       1.1   mycroft /*
     41       1.1   mycroft  * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
     42       1.1   mycroft  * Slight fixes to timeouts to run with the 34F
     43       1.1   mycroft  * Thanks to Julian Elischer for advice and help with this port.
     44       1.1   mycroft  *
     45       1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     46       1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     47       1.1   mycroft  *
     48       1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     49       1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     50       1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     51       1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     52       1.1   mycroft  * organisations permission to use or modify this software.
     53       1.1   mycroft  *
     54       1.1   mycroft  * TFS supplies this software to be publicly redistributed
     55       1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     56       1.1   mycroft  * functioning of this software in any circumstances.
     57       1.1   mycroft  *
     58       1.1   mycroft  * commenced: Sun Sep 27 18:14:01 PDT 1992
     59       1.1   mycroft  * slight mod to make work with 34F as well: Wed Jun  2 18:05:48 WST 1993
     60       1.1   mycroft  */
     61  1.24.2.4   nathanw 
     62  1.24.2.4   nathanw #include <sys/cdefs.h>
     63  1.24.2.4   nathanw __KERNEL_RCSID(0, "$NetBSD: uha.c,v 1.24.2.4 2001/11/14 19:14:40 nathanw Exp $");
     64  1.24.2.4   nathanw 
     65  1.24.2.4   nathanw #undef UHADEBUG
     66  1.24.2.4   nathanw #ifdef DDB
     67  1.24.2.4   nathanw #define	integrate
     68  1.24.2.4   nathanw #else
     69  1.24.2.4   nathanw #define	integrate	static inline
     70  1.24.2.4   nathanw #endif
     71       1.1   mycroft 
     72       1.1   mycroft #include <sys/types.h>
     73       1.1   mycroft #include <sys/param.h>
     74       1.1   mycroft #include <sys/systm.h>
     75       1.1   mycroft #include <sys/kernel.h>
     76       1.1   mycroft #include <sys/errno.h>
     77       1.1   mycroft #include <sys/ioctl.h>
     78       1.1   mycroft #include <sys/device.h>
     79       1.1   mycroft #include <sys/malloc.h>
     80       1.1   mycroft #include <sys/buf.h>
     81       1.1   mycroft #include <sys/proc.h>
     82       1.1   mycroft #include <sys/user.h>
     83       1.1   mycroft 
     84      1.24   thorpej #include <uvm/uvm_extern.h>
     85      1.24   thorpej 
     86       1.1   mycroft #include <machine/bus.h>
     87       1.1   mycroft #include <machine/intr.h>
     88       1.1   mycroft 
     89       1.9    bouyer #include <dev/scsipi/scsi_all.h>
     90       1.9    bouyer #include <dev/scsipi/scsipi_all.h>
     91       1.9    bouyer #include <dev/scsipi/scsiconf.h>
     92       1.1   mycroft 
     93       1.1   mycroft #include <dev/ic/uhareg.h>
     94       1.1   mycroft #include <dev/ic/uhavar.h>
     95       1.1   mycroft 
     96       1.1   mycroft #ifndef	DDB
     97       1.5  jonathan #define Debugger() panic("should call debugger here (uha.c)")
     98       1.1   mycroft #endif /* ! DDB */
     99       1.1   mycroft 
    100       1.8   thorpej #define	UHA_MAXXFER	((UHA_NSEG - 1) << PGSHIFT)
    101       1.1   mycroft 
    102       1.4   mycroft integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
    103       1.1   mycroft void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
    104      1.10   thorpej integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
    105  1.24.2.2   nathanw struct uha_mscp *uha_get_mscp __P((struct uha_softc *));
    106       1.1   mycroft void uhaminphys __P((struct buf *));
    107  1.24.2.2   nathanw void uha_scsipi_request __P((struct scsipi_channel *,
    108  1.24.2.2   nathanw 	scsipi_adapter_req_t, void *));
    109      1.16   thorpej int uha_create_mscps __P((struct uha_softc *, struct uha_mscp *, int));
    110       1.1   mycroft 
    111       1.1   mycroft #define	UHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    112       1.8   thorpej 
    113       1.1   mycroft /*
    114       1.1   mycroft  * Attach all the sub-devices we can find
    115       1.1   mycroft  */
    116       1.1   mycroft void
    117       1.7   mycroft uha_attach(sc, upd)
    118       1.1   mycroft 	struct uha_softc *sc;
    119       1.7   mycroft 	struct uha_probe_data *upd;
    120       1.1   mycroft {
    121  1.24.2.2   nathanw 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    122  1.24.2.2   nathanw 	struct scsipi_channel *chan = &sc->sc_channel;
    123      1.16   thorpej 	bus_dma_segment_t seg;
    124      1.16   thorpej 	int i, error, rseg;
    125       1.1   mycroft 
    126       1.8   thorpej 	TAILQ_INIT(&sc->sc_free_mscp);
    127       1.8   thorpej 
    128       1.1   mycroft 	(sc->init)(sc);
    129       1.1   mycroft 
    130       1.1   mycroft 	/*
    131  1.24.2.2   nathanw 	 * Fill in the scsipi_adapter.
    132       1.1   mycroft 	 */
    133  1.24.2.2   nathanw 	memset(adapt, 0, sizeof(*adapt));
    134  1.24.2.2   nathanw 	adapt->adapt_dev = &sc->sc_dev;
    135  1.24.2.2   nathanw 	adapt->adapt_nchannels = 1;
    136  1.24.2.2   nathanw 	/* adapt_openings initialized below */
    137  1.24.2.2   nathanw 	/* adapt_max_periph initialized below */
    138  1.24.2.2   nathanw 	adapt->adapt_request = uha_scsipi_request;
    139  1.24.2.2   nathanw 	adapt->adapt_minphys = uhaminphys;
    140  1.24.2.2   nathanw 
    141  1.24.2.2   nathanw 	/*
    142  1.24.2.2   nathanw 	 * Fill in the scsipi_channel.
    143  1.24.2.2   nathanw 	 */
    144  1.24.2.2   nathanw 	memset(chan, 0, sizeof(*chan));
    145  1.24.2.2   nathanw 	chan->chan_adapter = adapt;
    146  1.24.2.2   nathanw 	chan->chan_bustype = &scsi_bustype;
    147  1.24.2.2   nathanw 	chan->chan_channel = 0;
    148  1.24.2.2   nathanw 	chan->chan_ntargets = 8;
    149  1.24.2.2   nathanw 	chan->chan_nluns = 8;
    150  1.24.2.2   nathanw 	chan->chan_id = upd->sc_scsi_dev;
    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.24.2.2   nathanw 	adapt->adapt_openings = i;
    204  1.24.2.2   nathanw 	adapt->adapt_max_periph = adapt->adapt_openings;
    205  1.24.2.2   nathanw 
    206       1.1   mycroft 	/*
    207       1.1   mycroft 	 * ask the adapter what subunits are present
    208       1.1   mycroft 	 */
    209  1.24.2.2   nathanw 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    210       1.1   mycroft }
    211       1.1   mycroft 
    212       1.1   mycroft integrate void
    213       1.1   mycroft uha_reset_mscp(sc, mscp)
    214       1.1   mycroft 	struct uha_softc *sc;
    215       1.1   mycroft 	struct uha_mscp *mscp;
    216       1.1   mycroft {
    217       1.1   mycroft 
    218       1.1   mycroft 	mscp->flags = 0;
    219       1.1   mycroft }
    220       1.1   mycroft 
    221       1.1   mycroft /*
    222       1.1   mycroft  * A mscp (and hence a mbx-out) is put onto the free list.
    223       1.1   mycroft  */
    224       1.1   mycroft void
    225       1.1   mycroft uha_free_mscp(sc, mscp)
    226       1.1   mycroft 	struct uha_softc *sc;
    227       1.1   mycroft 	struct uha_mscp *mscp;
    228       1.1   mycroft {
    229       1.1   mycroft 	int s;
    230       1.1   mycroft 
    231       1.1   mycroft 	s = splbio();
    232       1.1   mycroft 	uha_reset_mscp(sc, mscp);
    233       1.1   mycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
    234       1.1   mycroft 	splx(s);
    235       1.1   mycroft }
    236       1.1   mycroft 
    237      1.10   thorpej integrate int
    238       1.1   mycroft uha_init_mscp(sc, mscp)
    239       1.1   mycroft 	struct uha_softc *sc;
    240       1.1   mycroft 	struct uha_mscp *mscp;
    241       1.1   mycroft {
    242       1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    243      1.10   thorpej 	int hashnum, error;
    244       1.1   mycroft 
    245       1.8   thorpej 	/*
    246      1.16   thorpej 	 * Create the DMA map for this MSCP.
    247       1.8   thorpej 	 */
    248      1.10   thorpej 	error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
    249       1.8   thorpej 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    250      1.10   thorpej 	    &mscp->dmamap_xfer);
    251      1.10   thorpej 	if (error) {
    252      1.16   thorpej 		printf("%s: can't create mscp DMA map, error = %d\n",
    253      1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    254      1.10   thorpej 		return (error);
    255      1.10   thorpej 	}
    256       1.8   thorpej 
    257       1.1   mycroft 	/*
    258       1.1   mycroft 	 * put in the phystokv hash table
    259       1.1   mycroft 	 * Never gets taken out.
    260       1.1   mycroft 	 */
    261      1.16   thorpej 	mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    262      1.16   thorpej 	    UHA_MSCP_OFF(mscp);
    263       1.1   mycroft 	hashnum = MSCP_HASH(mscp->hashkey);
    264       1.1   mycroft 	mscp->nexthash = sc->sc_mscphash[hashnum];
    265       1.1   mycroft 	sc->sc_mscphash[hashnum] = mscp;
    266       1.1   mycroft 	uha_reset_mscp(sc, mscp);
    267      1.10   thorpej 	return (0);
    268       1.1   mycroft }
    269       1.1   mycroft 
    270       1.1   mycroft /*
    271       1.8   thorpej  * Create a set of MSCPs and add them to the free list.
    272       1.8   thorpej  */
    273       1.8   thorpej int
    274      1.16   thorpej uha_create_mscps(sc, mscpstore, count)
    275       1.8   thorpej 	struct uha_softc *sc;
    276      1.16   thorpej 	struct uha_mscp *mscpstore;
    277      1.16   thorpej 	int count;
    278       1.8   thorpej {
    279       1.8   thorpej 	struct uha_mscp *mscp;
    280      1.16   thorpej 	int i, error;
    281       1.8   thorpej 
    282  1.24.2.3   nathanw 	memset(mscpstore, 0, sizeof(struct uha_mscp) * count);
    283      1.16   thorpej 	for (i = 0; i < count; i++) {
    284      1.16   thorpej 		mscp = &mscpstore[i];
    285      1.16   thorpej 		if ((error = uha_init_mscp(sc, mscp)) != 0) {
    286      1.16   thorpej 			printf("%s: unable to initialize mscp, error = %d\n",
    287      1.16   thorpej 			    sc->sc_dev.dv_xname, error);
    288      1.16   thorpej 			goto out;
    289      1.10   thorpej 		}
    290       1.8   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
    291       1.8   thorpej 	}
    292      1.16   thorpej  out:
    293      1.16   thorpej 	return (i);
    294       1.8   thorpej }
    295       1.8   thorpej 
    296       1.8   thorpej /*
    297       1.1   mycroft  * Get a free mscp
    298       1.1   mycroft  *
    299       1.1   mycroft  * If there are none, see if we can allocate a new one.  If so, put it in the
    300       1.1   mycroft  * hash table too otherwise either return an error or sleep.
    301       1.1   mycroft  */
    302       1.1   mycroft struct uha_mscp *
    303  1.24.2.2   nathanw uha_get_mscp(sc)
    304       1.1   mycroft 	struct uha_softc *sc;
    305       1.1   mycroft {
    306       1.1   mycroft 	struct uha_mscp *mscp;
    307       1.1   mycroft 	int s;
    308       1.1   mycroft 
    309       1.1   mycroft 	s = splbio();
    310  1.24.2.2   nathanw 	mscp = TAILQ_FIRST(&sc->sc_free_mscp);
    311  1.24.2.2   nathanw 	if (mscp != NULL) {
    312  1.24.2.2   nathanw 		TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
    313  1.24.2.2   nathanw 		mscp->flags |= MSCP_ALLOC;
    314       1.1   mycroft 	}
    315       1.1   mycroft 	splx(s);
    316       1.1   mycroft 	return (mscp);
    317       1.1   mycroft }
    318       1.1   mycroft 
    319       1.1   mycroft /*
    320       1.1   mycroft  * given a physical address, find the mscp that it corresponds to.
    321       1.1   mycroft  */
    322       1.1   mycroft struct uha_mscp *
    323       1.1   mycroft uha_mscp_phys_kv(sc, mscp_phys)
    324       1.1   mycroft 	struct uha_softc *sc;
    325       1.1   mycroft 	u_long mscp_phys;
    326       1.1   mycroft {
    327       1.1   mycroft 	int hashnum = MSCP_HASH(mscp_phys);
    328       1.1   mycroft 	struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
    329       1.1   mycroft 
    330       1.1   mycroft 	while (mscp) {
    331       1.1   mycroft 		if (mscp->hashkey == mscp_phys)
    332       1.1   mycroft 			break;
    333       1.1   mycroft 		mscp = mscp->nexthash;
    334       1.1   mycroft 	}
    335       1.1   mycroft 	return (mscp);
    336       1.1   mycroft }
    337       1.1   mycroft 
    338       1.1   mycroft /*
    339       1.1   mycroft  * We have a mscp which has been processed by the adaptor, now we look to see
    340       1.1   mycroft  * how the operation went.
    341       1.1   mycroft  */
    342       1.1   mycroft void
    343       1.1   mycroft uha_done(sc, mscp)
    344       1.1   mycroft 	struct uha_softc *sc;
    345       1.1   mycroft 	struct uha_mscp *mscp;
    346       1.1   mycroft {
    347       1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    348       1.9    bouyer 	struct scsipi_sense_data *s1, *s2;
    349       1.9    bouyer 	struct scsipi_xfer *xs = mscp->xs;
    350       1.1   mycroft 
    351  1.24.2.2   nathanw 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("uha_done\n"));
    352       1.8   thorpej 
    353      1.16   thorpej 	bus_dmamap_sync(dmat, sc->sc_dmamap_mscp,
    354      1.16   thorpej 	    UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
    355      1.16   thorpej 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    356      1.16   thorpej 
    357       1.8   thorpej 	/*
    358       1.8   thorpej 	 * If we were a data transfer, unload the map that described
    359       1.8   thorpej 	 * the data buffer.
    360       1.8   thorpej 	 */
    361       1.8   thorpej 	if (xs->datalen) {
    362      1.15   thorpej 		bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
    363      1.15   thorpej 		    mscp->dmamap_xfer->dm_mapsize,
    364      1.22   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    365       1.8   thorpej 		    BUS_DMASYNC_POSTWRITE);
    366       1.8   thorpej 		bus_dmamap_unload(dmat, mscp->dmamap_xfer);
    367       1.8   thorpej 	}
    368       1.8   thorpej 
    369       1.1   mycroft 	/*
    370       1.1   mycroft 	 * Otherwise, put the results of the operation
    371       1.1   mycroft 	 * into the xfer and call whoever started it
    372       1.1   mycroft 	 */
    373       1.1   mycroft 	if ((mscp->flags & MSCP_ALLOC) == 0) {
    374       1.3  christos 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    375       1.1   mycroft 		Debugger();
    376       1.1   mycroft 		return;
    377       1.1   mycroft 	}
    378       1.1   mycroft 	if (xs->error == XS_NOERROR) {
    379       1.1   mycroft 		if (mscp->host_stat != UHA_NO_ERR) {
    380       1.1   mycroft 			switch (mscp->host_stat) {
    381       1.1   mycroft 			case UHA_SBUS_TIMEOUT:		/* No response */
    382       1.1   mycroft 				xs->error = XS_SELTIMEOUT;
    383       1.1   mycroft 				break;
    384       1.1   mycroft 			default:	/* Other scsi protocol messes */
    385       1.3  christos 				printf("%s: host_stat %x\n",
    386       1.1   mycroft 				    sc->sc_dev.dv_xname, mscp->host_stat);
    387       1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    388       1.1   mycroft 			}
    389       1.1   mycroft 		} else if (mscp->target_stat != SCSI_OK) {
    390       1.1   mycroft 			switch (mscp->target_stat) {
    391       1.1   mycroft 			case SCSI_CHECK:
    392       1.1   mycroft 				s1 = &mscp->mscp_sense;
    393       1.9    bouyer 				s2 = &xs->sense.scsi_sense;
    394       1.1   mycroft 				*s2 = *s1;
    395       1.1   mycroft 				xs->error = XS_SENSE;
    396       1.1   mycroft 				break;
    397       1.1   mycroft 			case SCSI_BUSY:
    398       1.1   mycroft 				xs->error = XS_BUSY;
    399       1.1   mycroft 				break;
    400       1.1   mycroft 			default:
    401       1.3  christos 				printf("%s: target_stat %x\n",
    402       1.1   mycroft 				    sc->sc_dev.dv_xname, mscp->target_stat);
    403       1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    404       1.1   mycroft 			}
    405       1.1   mycroft 		} else
    406       1.1   mycroft 			xs->resid = 0;
    407       1.1   mycroft 	}
    408       1.1   mycroft 	uha_free_mscp(sc, mscp);
    409       1.9    bouyer 	scsipi_done(xs);
    410       1.1   mycroft }
    411       1.1   mycroft 
    412       1.1   mycroft void
    413       1.1   mycroft uhaminphys(bp)
    414       1.1   mycroft 	struct buf *bp;
    415       1.1   mycroft {
    416       1.1   mycroft 
    417       1.8   thorpej 	if (bp->b_bcount > UHA_MAXXFER)
    418       1.8   thorpej 		bp->b_bcount = UHA_MAXXFER;
    419       1.1   mycroft 	minphys(bp);
    420       1.1   mycroft }
    421       1.1   mycroft 
    422       1.1   mycroft /*
    423       1.1   mycroft  * start a scsi operation given the command and the data address.  Also
    424       1.1   mycroft  * needs the unit, target and lu.
    425       1.1   mycroft  */
    426  1.24.2.2   nathanw 
    427  1.24.2.2   nathanw void
    428  1.24.2.2   nathanw uha_scsipi_request(chan, req, arg)
    429  1.24.2.2   nathanw 	struct scsipi_channel *chan;
    430  1.24.2.2   nathanw 	scsipi_adapter_req_t req;
    431  1.24.2.2   nathanw 	void *arg;
    432       1.1   mycroft {
    433  1.24.2.2   nathanw 	struct scsipi_xfer *xs;
    434  1.24.2.2   nathanw 	struct scsipi_periph *periph;
    435  1.24.2.2   nathanw 	struct uha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    436       1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    437       1.1   mycroft 	struct uha_mscp *mscp;
    438       1.1   mycroft 	struct uha_dma_seg *sg;
    439       1.8   thorpej 	int error, seg, flags, s;
    440      1.11   thorpej 
    441      1.11   thorpej 
    442  1.24.2.2   nathanw 	switch (req) {
    443  1.24.2.2   nathanw 	case ADAPTER_REQ_RUN_XFER:
    444  1.24.2.2   nathanw 		xs = arg;
    445  1.24.2.2   nathanw 		periph = xs->xs_periph;
    446  1.24.2.2   nathanw 		flags = xs->xs_control;
    447  1.24.2.2   nathanw 
    448  1.24.2.2   nathanw 		SC_DEBUG(periph, SCSIPI_DB2, ("uha_scsipi_request\n"));
    449  1.24.2.2   nathanw 
    450  1.24.2.2   nathanw 		/* Get an MSCP to use. */
    451  1.24.2.2   nathanw 		mscp = uha_get_mscp(sc);
    452  1.24.2.2   nathanw #ifdef DIAGNOSTIC
    453      1.11   thorpej 		/*
    454  1.24.2.2   nathanw 		 * This should never happen as we track the resources
    455  1.24.2.2   nathanw 		 * in the mid-layer.
    456      1.11   thorpej 		 */
    457  1.24.2.2   nathanw 		if (mscp == NULL) {
    458  1.24.2.2   nathanw 			scsipi_printaddr(periph);
    459  1.24.2.2   nathanw 			printf("unable to allocate mscp\n");
    460  1.24.2.2   nathanw 			panic("uha_scsipi_request");
    461      1.11   thorpej 		}
    462  1.24.2.2   nathanw #endif
    463      1.11   thorpej 
    464  1.24.2.2   nathanw 		mscp->xs = xs;
    465  1.24.2.2   nathanw 		mscp->timeout = xs->timeout;
    466      1.11   thorpej 
    467      1.11   thorpej 		/*
    468  1.24.2.2   nathanw 		 * Put all the arguments for the xfer in the mscp
    469      1.11   thorpej 		 */
    470  1.24.2.2   nathanw 		if (flags & XS_CTL_RESET) {
    471  1.24.2.2   nathanw 			mscp->opcode = UHA_SDR;
    472  1.24.2.2   nathanw 			mscp->ca = 0x01;
    473  1.24.2.2   nathanw 		} else {
    474  1.24.2.2   nathanw 			mscp->opcode = UHA_TSP;
    475  1.24.2.2   nathanw 			/* XXX Not for tapes. */
    476  1.24.2.2   nathanw 			mscp->ca = 0x01;
    477  1.24.2.3   nathanw 			memcpy(&mscp->scsi_cmd, xs->cmd, mscp->scsi_cmd_length);
    478      1.11   thorpej 		}
    479  1.24.2.2   nathanw 		mscp->xdir = UHA_SDET;
    480  1.24.2.2   nathanw 		mscp->dcn = 0x00;
    481  1.24.2.2   nathanw 		mscp->chan = 0x00;
    482  1.24.2.2   nathanw 		mscp->target = periph->periph_target;
    483  1.24.2.2   nathanw 		mscp->lun = periph->periph_lun;
    484  1.24.2.2   nathanw 		mscp->scsi_cmd_length = xs->cmdlen;
    485  1.24.2.2   nathanw 		mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    486  1.24.2.2   nathanw 		    UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense);
    487  1.24.2.2   nathanw 		mscp->req_sense_length = sizeof(mscp->mscp_sense);
    488  1.24.2.2   nathanw 		mscp->host_stat = 0x00;
    489  1.24.2.2   nathanw 		mscp->target_stat = 0x00;
    490  1.24.2.2   nathanw 
    491  1.24.2.2   nathanw 		if (xs->datalen) {
    492  1.24.2.2   nathanw 			sg = mscp->uha_dma;
    493  1.24.2.2   nathanw 			seg = 0;
    494  1.24.2.2   nathanw #ifdef	TFS
    495  1.24.2.2   nathanw 			if (flags & SCSI_DATA_UIO) {
    496  1.24.2.2   nathanw 				error = bus_dmamap_load_uio(dmat,
    497  1.24.2.2   nathanw 				    mscp->dmamap_xfer, (struct uio *)xs->data,
    498  1.24.2.2   nathanw 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    499  1.24.2.3   nathanw 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    500  1.24.2.3   nathanw 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
    501  1.24.2.3   nathanw 				      BUS_DMA_WRITE));
    502  1.24.2.2   nathanw 			} else
    503  1.24.2.2   nathanw #endif /*TFS */
    504  1.24.2.2   nathanw 			{
    505  1.24.2.2   nathanw 				error = bus_dmamap_load(dmat,
    506  1.24.2.2   nathanw 				    mscp->dmamap_xfer, xs->data, xs->datalen,
    507  1.24.2.2   nathanw 				    NULL,
    508  1.24.2.2   nathanw 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    509  1.24.2.3   nathanw 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    510  1.24.2.3   nathanw 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
    511  1.24.2.3   nathanw 				      BUS_DMA_WRITE));
    512  1.24.2.2   nathanw 			}
    513      1.11   thorpej 
    514  1.24.2.2   nathanw 			switch (error) {
    515  1.24.2.2   nathanw 			case 0:
    516  1.24.2.2   nathanw 				break;
    517      1.11   thorpej 
    518  1.24.2.2   nathanw 			case ENOMEM:
    519  1.24.2.2   nathanw 			case EAGAIN:
    520  1.24.2.2   nathanw 				xs->error = XS_RESOURCE_SHORTAGE;
    521  1.24.2.2   nathanw 				goto out_bad;
    522      1.11   thorpej 
    523  1.24.2.2   nathanw 			default:
    524  1.24.2.2   nathanw 				xs->error = XS_DRIVER_STUFFUP;
    525  1.24.2.2   nathanw 				printf("%s: error %d loading DMA map\n",
    526  1.24.2.2   nathanw 				    sc->sc_dev.dv_xname, error);
    527  1.24.2.2   nathanw  out_bad:
    528  1.24.2.2   nathanw 				uha_free_mscp(sc, mscp);
    529  1.24.2.2   nathanw 				scsipi_done(xs);
    530  1.24.2.2   nathanw 				return;
    531  1.24.2.2   nathanw 			}
    532       1.1   mycroft 
    533  1.24.2.2   nathanw 			bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
    534  1.24.2.2   nathanw 			    mscp->dmamap_xfer->dm_mapsize,
    535  1.24.2.2   nathanw 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    536  1.24.2.2   nathanw 			    BUS_DMASYNC_PREWRITE);
    537  1.24.2.2   nathanw 
    538  1.24.2.2   nathanw 			/*
    539  1.24.2.2   nathanw 			 * Load the hardware scatter/gather map with the
    540  1.24.2.2   nathanw 			 * contents of the DMA map.
    541  1.24.2.2   nathanw 			 */
    542  1.24.2.2   nathanw 			for (seg = 0;
    543  1.24.2.2   nathanw 			     seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
    544  1.24.2.2   nathanw 				mscp->uha_dma[seg].seg_addr =
    545  1.24.2.2   nathanw 				    mscp->dmamap_xfer->dm_segs[seg].ds_addr;
    546  1.24.2.2   nathanw 				mscp->uha_dma[seg].seg_len =
    547  1.24.2.2   nathanw 				    mscp->dmamap_xfer->dm_segs[seg].ds_len;
    548  1.24.2.2   nathanw 			}
    549       1.1   mycroft 
    550  1.24.2.2   nathanw 			mscp->data_addr =
    551  1.24.2.2   nathanw 			    sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    552  1.24.2.2   nathanw 			    UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp,
    553  1.24.2.2   nathanw 			    uha_dma);
    554  1.24.2.2   nathanw 			mscp->data_length = xs->datalen;
    555  1.24.2.2   nathanw 			mscp->sgth = 0x01;
    556  1.24.2.2   nathanw 			mscp->sg_num = seg;
    557  1.24.2.2   nathanw 		} else {		/* No data xfer, use non S/G values */
    558  1.24.2.2   nathanw 			mscp->data_addr = (physaddr)0;
    559  1.24.2.2   nathanw 			mscp->data_length = 0;
    560  1.24.2.2   nathanw 			mscp->sgth = 0x00;
    561  1.24.2.2   nathanw 			mscp->sg_num = 0;
    562       1.8   thorpej 		}
    563  1.24.2.2   nathanw 		mscp->link_id = 0;
    564  1.24.2.2   nathanw 		mscp->link_addr = (physaddr)0;
    565       1.8   thorpej 
    566  1.24.2.2   nathanw 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp,
    567  1.24.2.2   nathanw 		    UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
    568  1.24.2.2   nathanw 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    569       1.8   thorpej 
    570  1.24.2.2   nathanw 		s = splbio();
    571  1.24.2.2   nathanw 		(sc->start_mbox)(sc, mscp);
    572  1.24.2.2   nathanw 		splx(s);
    573  1.24.2.2   nathanw 
    574  1.24.2.2   nathanw 		if ((flags & XS_CTL_POLL) == 0)
    575  1.24.2.2   nathanw 			return;
    576       1.8   thorpej 
    577       1.8   thorpej 		/*
    578  1.24.2.2   nathanw 		 * If we can't use interrupts, poll on completion
    579       1.8   thorpej 		 */
    580  1.24.2.2   nathanw 		if ((sc->poll)(sc, xs, mscp->timeout)) {
    581  1.24.2.2   nathanw 			uha_timeout(mscp);
    582  1.24.2.2   nathanw 			if ((sc->poll)(sc, xs, mscp->timeout))
    583  1.24.2.2   nathanw 				uha_timeout(mscp);
    584       1.1   mycroft 		}
    585  1.24.2.2   nathanw 		return;
    586       1.8   thorpej 
    587  1.24.2.2   nathanw 	case ADAPTER_REQ_GROW_RESOURCES:
    588  1.24.2.2   nathanw 		/* XXX Not supported. */
    589  1.24.2.2   nathanw 		return;
    590       1.1   mycroft 
    591  1.24.2.2   nathanw 	case ADAPTER_REQ_SET_XFER_MODE:
    592  1.24.2.2   nathanw 		/*
    593  1.24.2.2   nathanw 		 * We can't really do this (the UltraStor controllers
    594  1.24.2.2   nathanw 		 * have their own config).
    595  1.24.2.2   nathanw 		 *
    596  1.24.2.2   nathanw 		 * XXX How do we query the config?
    597  1.24.2.2   nathanw 		 */
    598  1.24.2.2   nathanw 		return;
    599       1.1   mycroft 	}
    600       1.1   mycroft }
    601       1.1   mycroft void
    602       1.1   mycroft uha_timeout(arg)
    603       1.1   mycroft 	void *arg;
    604       1.1   mycroft {
    605       1.1   mycroft 	struct uha_mscp *mscp = arg;
    606       1.9    bouyer 	struct scsipi_xfer *xs = mscp->xs;
    607  1.24.2.2   nathanw 	struct scsipi_periph *periph = xs->xs_periph;
    608  1.24.2.2   nathanw 	struct uha_softc *sc =
    609  1.24.2.2   nathanw 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    610       1.1   mycroft 	int s;
    611       1.1   mycroft 
    612  1.24.2.2   nathanw 	scsipi_printaddr(periph);
    613       1.3  christos 	printf("timed out");
    614       1.1   mycroft 
    615       1.1   mycroft 	s = splbio();
    616       1.1   mycroft 
    617       1.1   mycroft 	if (mscp->flags & MSCP_ABORT) {
    618       1.1   mycroft 		/* abort timed out */
    619       1.3  christos 		printf(" AGAIN\n");
    620       1.1   mycroft 		/* XXX Must reset! */
    621       1.1   mycroft 	} else {
    622       1.1   mycroft 		/* abort the operation that has timed out */
    623       1.3  christos 		printf("\n");
    624       1.1   mycroft 		mscp->xs->error = XS_TIMEOUT;
    625       1.1   mycroft 		mscp->timeout = UHA_ABORT_TIMEOUT;
    626       1.1   mycroft 		mscp->flags |= MSCP_ABORT;
    627       1.1   mycroft 		(sc->start_mbox)(sc, mscp);
    628       1.1   mycroft 	}
    629       1.1   mycroft 
    630       1.1   mycroft 	splx(s);
    631       1.1   mycroft }
    632