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