Home | History | Annotate | Line # | Download | only in ic
bha.c revision 1.51
      1  1.51       wiz /*	$NetBSD: bha.c,v 1.51 2003/01/06 13:26:26 wiz Exp $	*/
      2   1.1   mycroft 
      3  1.13   thorpej /*-
      4  1.42    bouyer  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  1.13   thorpej  * All rights reserved.
      6  1.13   thorpej  *
      7  1.13   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.25   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  1.25   mycroft  * Simulation Facility, NASA Ames Research Center.
     10  1.13   thorpej  *
     11  1.13   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.13   thorpej  * modification, are permitted provided that the following conditions
     13  1.13   thorpej  * are met:
     14  1.13   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.13   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.13   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.13   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.13   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.13   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.13   thorpej  *    must display the following acknowledgement:
     21  1.13   thorpej  *	This product includes software developed by the NetBSD
     22  1.13   thorpej  *	Foundation, Inc. and its contributors.
     23  1.13   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.13   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.13   thorpej  *    from this software without specific prior written permission.
     26  1.13   thorpej  *
     27  1.13   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.13   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.13   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.13   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.13   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.13   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.13   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.13   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.13   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.13   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.13   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1   mycroft  */
     39   1.1   mycroft 
     40   1.1   mycroft /*
     41   1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     42   1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     43   1.1   mycroft  *
     44   1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     45   1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     46   1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     47   1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     48   1.1   mycroft  * organisations permission to use or modify this software.
     49   1.1   mycroft  *
     50   1.1   mycroft  * TFS supplies this software to be publicly redistributed
     51   1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     52   1.1   mycroft  * functioning of this software in any circumstances.
     53   1.1   mycroft  */
     54  1.47     lukem 
     55  1.47     lukem #include <sys/cdefs.h>
     56  1.51       wiz __KERNEL_RCSID(0, "$NetBSD: bha.c,v 1.51 2003/01/06 13:26:26 wiz Exp $");
     57   1.1   mycroft 
     58  1.42    bouyer #include "opt_ddb.h"
     59  1.42    bouyer 
     60   1.1   mycroft #include <sys/param.h>
     61   1.1   mycroft #include <sys/systm.h>
     62  1.35   thorpej #include <sys/callout.h>
     63   1.1   mycroft #include <sys/kernel.h>
     64   1.1   mycroft #include <sys/errno.h>
     65   1.1   mycroft #include <sys/ioctl.h>
     66   1.1   mycroft #include <sys/device.h>
     67   1.1   mycroft #include <sys/malloc.h>
     68   1.1   mycroft #include <sys/buf.h>
     69   1.1   mycroft #include <sys/proc.h>
     70   1.1   mycroft #include <sys/user.h>
     71   1.1   mycroft 
     72  1.37       mrg #include <uvm/uvm_extern.h>
     73  1.31   thorpej 
     74   1.1   mycroft #include <machine/bus.h>
     75   1.1   mycroft #include <machine/intr.h>
     76   1.1   mycroft 
     77  1.14    bouyer #include <dev/scsipi/scsi_all.h>
     78  1.14    bouyer #include <dev/scsipi/scsipi_all.h>
     79  1.14    bouyer #include <dev/scsipi/scsiconf.h>
     80   1.1   mycroft 
     81   1.1   mycroft #include <dev/ic/bhareg.h>
     82   1.1   mycroft #include <dev/ic/bhavar.h>
     83   1.1   mycroft 
     84   1.1   mycroft #ifndef DDB
     85   1.1   mycroft #define Debugger() panic("should call debugger here (bha.c)")
     86   1.1   mycroft #endif /* ! DDB */
     87   1.1   mycroft 
     88  1.13   thorpej #define	BHA_MAXXFER	((BHA_NSEG - 1) << PGSHIFT)
     89   1.1   mycroft 
     90   1.1   mycroft #ifdef BHADEBUG
     91   1.1   mycroft int     bha_debug = 0;
     92   1.1   mycroft #endif /* BHADEBUG */
     93   1.1   mycroft 
     94  1.43      ross static int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, char *, int,
     95  1.43      ross 	    u_char *, int, u_char *));
     96  1.42    bouyer 
     97  1.43      ross static void bha_scsipi_request __P((struct scsipi_channel *,
     98  1.42    bouyer 	    scsipi_adapter_req_t, void *));
     99  1.43      ross static void bha_minphys __P((struct buf *));
    100  1.42    bouyer 
    101  1.43      ross static void bha_get_xfer_mode __P((struct bha_softc *,
    102  1.43      ross 	    struct scsipi_xfer_mode *));
    103  1.42    bouyer 
    104  1.43      ross static void bha_done __P((struct bha_softc *, struct bha_ccb *));
    105  1.43      ross int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
    106  1.43      ross static void bha_timeout __P((void *arg));
    107  1.42    bouyer 
    108  1.43      ross static int bha_init __P((struct bha_softc *));
    109  1.42    bouyer 
    110  1.43      ross static int bha_create_mailbox __P((struct bha_softc *));
    111  1.43      ross static void bha_collect_mbo __P((struct bha_softc *));
    112  1.42    bouyer 
    113  1.43      ross static void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
    114  1.43      ross static void bha_start_ccbs __P((struct bha_softc *));
    115  1.43      ross static void bha_finish_ccbs __P((struct bha_softc *));
    116  1.42    bouyer 
    117  1.33   mycroft struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, bus_addr_t));
    118  1.42    bouyer void	bha_create_ccbs __P((struct bha_softc *, int));
    119  1.42    bouyer int	bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
    120  1.42    bouyer struct bha_ccb *bha_get_ccb __P((struct bha_softc *));
    121  1.42    bouyer void	bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
    122   1.1   mycroft 
    123   1.1   mycroft #define BHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
    124   1.1   mycroft #define	BHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    125  1.13   thorpej 
    126   1.1   mycroft /*
    127  1.42    bouyer  * Number of CCBs in an allocation group; must be computed at run-time.
    128   1.1   mycroft  */
    129  1.42    bouyer int	bha_ccbs_per_group;
    130  1.42    bouyer 
    131  1.42    bouyer __inline struct bha_mbx_out *bha_nextmbo __P((struct bha_softc *,
    132  1.42    bouyer 	struct bha_mbx_out *));
    133  1.42    bouyer __inline struct bha_mbx_in *bha_nextmbi __P((struct bha_softc *,
    134  1.42    bouyer 	struct bha_mbx_in *));
    135  1.42    bouyer 
    136  1.42    bouyer __inline struct bha_mbx_out *
    137  1.42    bouyer bha_nextmbo(sc, mbo)
    138   1.1   mycroft 	struct bha_softc *sc;
    139  1.42    bouyer 	struct bha_mbx_out *mbo;
    140   1.1   mycroft {
    141  1.38    simonb 
    142  1.42    bouyer 	if (mbo == &sc->sc_mbo[sc->sc_mbox_count - 1])
    143  1.42    bouyer 		return (&sc->sc_mbo[0]);
    144  1.42    bouyer 	return (mbo + 1);
    145  1.42    bouyer }
    146   1.1   mycroft 
    147  1.42    bouyer __inline struct bha_mbx_in *
    148  1.42    bouyer bha_nextmbi(sc, mbi)
    149  1.42    bouyer 	struct bha_softc *sc;
    150  1.42    bouyer 	struct bha_mbx_in *mbi;
    151  1.42    bouyer {
    152  1.42    bouyer 	if (mbi == &sc->sc_mbi[sc->sc_mbox_count - 1])
    153  1.42    bouyer 		return (&sc->sc_mbi[0]);
    154  1.42    bouyer 	return (mbi + 1);
    155   1.1   mycroft }
    156   1.1   mycroft 
    157   1.1   mycroft /*
    158  1.42    bouyer  * bha_attach:
    159  1.42    bouyer  *
    160  1.42    bouyer  *	Finish attaching a Buslogic controller, and configure children.
    161   1.1   mycroft  */
    162   1.1   mycroft void
    163  1.43      ross bha_attach(sc)
    164   1.1   mycroft 	struct bha_softc *sc;
    165   1.1   mycroft {
    166  1.42    bouyer 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    167  1.42    bouyer 	struct scsipi_channel *chan = &sc->sc_channel;
    168  1.42    bouyer 	int initial_ccbs;
    169   1.1   mycroft 
    170   1.1   mycroft 	/*
    171  1.42    bouyer 	 * Initialize the number of CCBs per group.
    172  1.27   thorpej 	 */
    173  1.42    bouyer 	if (bha_ccbs_per_group == 0)
    174  1.42    bouyer 		bha_ccbs_per_group = BHA_CCBS_PER_GROUP;
    175  1.42    bouyer 
    176  1.42    bouyer 	initial_ccbs = bha_info(sc);
    177  1.42    bouyer 	if (initial_ccbs == 0) {
    178  1.42    bouyer 		printf("%s: unable to get adapter info\n",
    179  1.42    bouyer 		    sc->sc_dev.dv_xname);
    180  1.42    bouyer 		return;
    181  1.42    bouyer 	}
    182  1.27   thorpej 
    183  1.27   thorpej 	/*
    184  1.42    bouyer 	 * Fill in the scsipi_adapter.
    185   1.1   mycroft 	 */
    186  1.42    bouyer 	memset(adapt, 0, sizeof(*adapt));
    187  1.42    bouyer 	adapt->adapt_dev = &sc->sc_dev;
    188  1.42    bouyer 	adapt->adapt_nchannels = 1;
    189  1.42    bouyer 	/* adapt_openings initialized below */
    190  1.42    bouyer 	adapt->adapt_max_periph = sc->sc_mbox_count;
    191  1.42    bouyer 	adapt->adapt_request = bha_scsipi_request;
    192  1.42    bouyer 	adapt->adapt_minphys = bha_minphys;
    193  1.42    bouyer 
    194  1.42    bouyer 	/*
    195  1.42    bouyer 	 * Fill in the scsipi_channel.
    196  1.42    bouyer 	 */
    197  1.42    bouyer 	memset(chan, 0, sizeof(*chan));
    198  1.42    bouyer 	chan->chan_adapter = adapt;
    199  1.42    bouyer 	chan->chan_bustype = &scsi_bustype;
    200  1.42    bouyer 	chan->chan_channel = 0;
    201  1.42    bouyer 	chan->chan_flags = SCSIPI_CHAN_CANGROW;
    202  1.42    bouyer 	chan->chan_ntargets = (sc->sc_flags & BHAF_WIDE) ? 16 : 8;
    203  1.42    bouyer 	chan->chan_nluns = (sc->sc_flags & BHAF_WIDE_LUN) ? 32 : 8;
    204  1.42    bouyer 	chan->chan_id = sc->sc_scsi_id;
    205  1.12   thorpej 
    206  1.13   thorpej 	TAILQ_INIT(&sc->sc_free_ccb);
    207  1.13   thorpej 	TAILQ_INIT(&sc->sc_waiting_ccb);
    208  1.42    bouyer 	TAILQ_INIT(&sc->sc_allocating_ccbs);
    209  1.38    simonb 
    210  1.42    bouyer 	if (bha_create_mailbox(sc) != 0)
    211  1.22   thorpej 		return;
    212   1.1   mycroft 
    213  1.42    bouyer 	bha_create_ccbs(sc, initial_ccbs);
    214  1.42    bouyer 	if (sc->sc_cur_ccbs < 2) {
    215  1.42    bouyer 		printf("%s: not enough CCBs to run\n",
    216   1.1   mycroft 		    sc->sc_dev.dv_xname);
    217   1.1   mycroft 		return;
    218   1.1   mycroft 	}
    219   1.1   mycroft 
    220  1.42    bouyer 	adapt->adapt_openings = sc->sc_cur_ccbs;
    221  1.38    simonb 
    222  1.42    bouyer 	if (bha_init(sc) != 0)
    223  1.42    bouyer 		return;
    224   1.1   mycroft 
    225  1.42    bouyer 	(void) config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    226   1.1   mycroft }
    227   1.1   mycroft 
    228   1.1   mycroft /*
    229  1.42    bouyer  * bha_intr:
    230  1.42    bouyer  *
    231  1.42    bouyer  *	Interrupt service routine.
    232   1.1   mycroft  */
    233   1.1   mycroft int
    234   1.1   mycroft bha_intr(arg)
    235   1.1   mycroft 	void *arg;
    236   1.1   mycroft {
    237   1.1   mycroft 	struct bha_softc *sc = arg;
    238   1.4   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    239   1.4   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    240   1.1   mycroft 	u_char sts;
    241   1.1   mycroft 
    242   1.1   mycroft #ifdef BHADEBUG
    243   1.3  christos 	printf("%s: bha_intr ", sc->sc_dev.dv_xname);
    244   1.1   mycroft #endif /* BHADEBUG */
    245   1.1   mycroft 
    246   1.1   mycroft 	/*
    247   1.1   mycroft 	 * First acknowlege the interrupt, Then if it's not telling about
    248   1.1   mycroft 	 * a completed operation just return.
    249   1.1   mycroft 	 */
    250   1.4   thorpej 	sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    251   1.1   mycroft 	if ((sts & BHA_INTR_ANYINTR) == 0)
    252   1.1   mycroft 		return (0);
    253   1.4   thorpej 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    254   1.1   mycroft 
    255   1.1   mycroft #ifdef BHADIAG
    256   1.1   mycroft 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
    257   1.1   mycroft 	bha_collect_mbo(sc);
    258   1.1   mycroft #endif
    259   1.1   mycroft 
    260   1.1   mycroft 	/* Mail box out empty? */
    261   1.1   mycroft 	if (sts & BHA_INTR_MBOA) {
    262   1.1   mycroft 		struct bha_toggle toggle;
    263   1.1   mycroft 
    264   1.1   mycroft 		toggle.cmd.opcode = BHA_MBO_INTR_EN;
    265   1.1   mycroft 		toggle.cmd.enable = 0;
    266  1.43      ross 		bha_cmd(iot, ioh, sc->sc_dev.dv_xname,
    267   1.1   mycroft 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    268   1.1   mycroft 		    0, (u_char *)0);
    269   1.1   mycroft 		bha_start_ccbs(sc);
    270   1.1   mycroft 	}
    271   1.1   mycroft 
    272   1.1   mycroft 	/* Mail box in full? */
    273   1.1   mycroft 	if (sts & BHA_INTR_MBIF)
    274   1.1   mycroft 		bha_finish_ccbs(sc);
    275   1.1   mycroft 
    276   1.1   mycroft 	return (1);
    277   1.1   mycroft }
    278   1.1   mycroft 
    279  1.42    bouyer /*****************************************************************************
    280  1.42    bouyer  * SCSI interface routines
    281  1.42    bouyer  *****************************************************************************/
    282   1.1   mycroft 
    283   1.1   mycroft /*
    284  1.42    bouyer  * bha_scsipi_request:
    285  1.42    bouyer  *
    286  1.42    bouyer  *	Perform a request for the SCSIPI layer.
    287   1.1   mycroft  */
    288  1.38    simonb void
    289  1.42    bouyer bha_scsipi_request(chan, req, arg)
    290  1.42    bouyer 	struct scsipi_channel *chan;
    291  1.42    bouyer 	scsipi_adapter_req_t req;
    292  1.42    bouyer 	void *arg;
    293  1.42    bouyer {
    294  1.42    bouyer 	struct scsipi_adapter *adapt = chan->chan_adapter;
    295  1.42    bouyer 	struct bha_softc *sc = (void *)adapt->adapt_dev;
    296  1.42    bouyer 	struct scsipi_xfer *xs;
    297  1.42    bouyer 	struct scsipi_periph *periph;
    298  1.42    bouyer 	bus_dma_tag_t dmat = sc->sc_dmat;
    299  1.38    simonb 	struct bha_ccb *ccb;
    300  1.42    bouyer 	int error, seg, flags, s;
    301   1.1   mycroft 
    302  1.42    bouyer 	switch (req) {
    303  1.42    bouyer 	case ADAPTER_REQ_RUN_XFER:
    304  1.42    bouyer 		xs = arg;
    305  1.42    bouyer 		periph = xs->xs_periph;
    306  1.42    bouyer 		flags = xs->xs_control;
    307  1.42    bouyer 
    308  1.42    bouyer 		SC_DEBUG(periph, SCSIPI_DB2, ("bha_scsipi_request\n"));
    309  1.42    bouyer 
    310  1.42    bouyer 		/* Get a CCB to use. */
    311  1.42    bouyer 		ccb = bha_get_ccb(sc);
    312  1.42    bouyer #ifdef DIAGNOSTIC
    313  1.42    bouyer 		/*
    314  1.42    bouyer 		 * This should never happen as we track the resources
    315  1.42    bouyer 		 * in the mid-layer.
    316  1.42    bouyer 		 */
    317  1.42    bouyer 		if (ccb == NULL) {
    318  1.42    bouyer 			scsipi_printaddr(periph);
    319  1.42    bouyer 			printf("unable to allocate ccb\n");
    320  1.42    bouyer 			panic("bha_scsipi_request");
    321  1.42    bouyer 		}
    322  1.42    bouyer #endif
    323   1.1   mycroft 
    324  1.42    bouyer 		ccb->xs = xs;
    325  1.42    bouyer 		ccb->timeout = xs->timeout;
    326   1.1   mycroft 
    327  1.42    bouyer 		/*
    328  1.42    bouyer 		 * Put all the arguments for the xfer in the ccb
    329  1.42    bouyer 		 */
    330  1.42    bouyer 		if (flags & XS_CTL_RESET) {
    331  1.42    bouyer 			ccb->opcode = BHA_RESET_CCB;
    332  1.42    bouyer 			ccb->scsi_cmd_length = 0;
    333  1.42    bouyer 		} else {
    334  1.42    bouyer 			/* can't use S/G if zero length */
    335  1.42    bouyer 			ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
    336  1.42    bouyer 						   : BHA_INITIATOR_CCB);
    337  1.44   thorpej 			memcpy(&ccb->scsi_cmd, xs->cmd,
    338  1.42    bouyer 			    ccb->scsi_cmd_length = xs->cmdlen);
    339  1.42    bouyer 		}
    340  1.31   thorpej 
    341  1.42    bouyer 		if (xs->datalen) {
    342  1.42    bouyer 			/*
    343  1.42    bouyer 			 * Map the DMA transfer.
    344  1.42    bouyer 			 */
    345  1.42    bouyer #ifdef TFS
    346  1.42    bouyer 			if (flags & XS_CTL_DATA_UIO) {
    347  1.42    bouyer 				error = bus_dmamap_load_uio(dmat,
    348  1.42    bouyer 				    ccb->dmamap_xfer, (struct uio *)xs->data,
    349  1.42    bouyer 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    350  1.45   thorpej 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    351  1.45   thorpej 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
    352  1.45   thorpej 				      BUS_DMA_WRITE));
    353  1.42    bouyer 			} else
    354  1.42    bouyer #endif /* TFS */
    355  1.42    bouyer 			{
    356  1.42    bouyer 				error = bus_dmamap_load(dmat,
    357  1.42    bouyer 				    ccb->dmamap_xfer, xs->data, xs->datalen,
    358  1.42    bouyer 				    NULL,
    359  1.42    bouyer 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    360  1.45   thorpej 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    361  1.45   thorpej 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
    362  1.45   thorpej 				      BUS_DMA_WRITE));
    363  1.42    bouyer 			}
    364  1.38    simonb 
    365  1.42    bouyer 			switch (error) {
    366  1.42    bouyer 			case 0:
    367  1.42    bouyer 				break;
    368   1.1   mycroft 
    369  1.42    bouyer 			case ENOMEM:
    370  1.42    bouyer 			case EAGAIN:
    371  1.42    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
    372  1.42    bouyer 				goto out_bad;
    373  1.42    bouyer 
    374  1.42    bouyer 			default:
    375  1.42    bouyer 				xs->error = XS_DRIVER_STUFFUP;
    376  1.42    bouyer 				printf("%s: error %d loading DMA map\n",
    377  1.42    bouyer 				    sc->sc_dev.dv_xname, error);
    378  1.42    bouyer  out_bad:
    379  1.42    bouyer 				bha_free_ccb(sc, ccb);
    380  1.42    bouyer 				scsipi_done(xs);
    381  1.42    bouyer 				return;
    382  1.42    bouyer 			}
    383   1.1   mycroft 
    384  1.42    bouyer 			bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    385  1.42    bouyer 			    ccb->dmamap_xfer->dm_mapsize,
    386  1.42    bouyer 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    387  1.42    bouyer 			    BUS_DMASYNC_PREWRITE);
    388  1.38    simonb 
    389  1.42    bouyer 			/*
    390  1.42    bouyer 			 * Load the hardware scatter/gather map with the
    391  1.42    bouyer 			 * contents of the DMA map.
    392  1.42    bouyer 			 */
    393  1.42    bouyer 			for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
    394  1.42    bouyer 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
    395  1.42    bouyer 				    ccb->scat_gath[seg].seg_addr);
    396  1.42    bouyer 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
    397  1.42    bouyer 				    ccb->scat_gath[seg].seg_len);
    398  1.42    bouyer 			}
    399  1.38    simonb 
    400  1.42    bouyer 			ltophys(ccb->hashkey + offsetof(struct bha_ccb,
    401  1.42    bouyer 			    scat_gath), ccb->data_addr);
    402  1.42    bouyer 			ltophys(ccb->dmamap_xfer->dm_nsegs *
    403  1.42    bouyer 			    sizeof(struct bha_scat_gath), ccb->data_length);
    404  1.42    bouyer 		} else {
    405  1.42    bouyer 			/*
    406  1.42    bouyer 			 * No data xfer, use non S/G values.
    407  1.42    bouyer 			 */
    408  1.42    bouyer 			ltophys(0, ccb->data_addr);
    409  1.42    bouyer 			ltophys(0, ccb->data_length);
    410  1.31   thorpej 		}
    411  1.13   thorpej 
    412  1.42    bouyer 		if (XS_CTL_TAGTYPE(xs) != 0) {
    413  1.42    bouyer 			ccb->tag_enable = 1;
    414  1.42    bouyer 			ccb->tag_type = xs->xs_tag_type & 0x03;
    415  1.42    bouyer 		} else {
    416  1.42    bouyer 			ccb->tag_enable = 0;
    417  1.42    bouyer 			ccb->tag_type = 0;
    418  1.42    bouyer 		}
    419  1.42    bouyer 
    420  1.42    bouyer 		ccb->data_out = 0;
    421  1.42    bouyer 		ccb->data_in = 0;
    422  1.42    bouyer 		ccb->target = periph->periph_target;
    423  1.42    bouyer 		ccb->lun = periph->periph_lun;
    424  1.42    bouyer 		ltophys(ccb->hashkey + offsetof(struct bha_ccb, scsi_sense),
    425  1.42    bouyer 		    ccb->sense_ptr);
    426  1.42    bouyer 		ccb->req_sense_length = sizeof(ccb->scsi_sense);
    427  1.42    bouyer 		ccb->host_stat = 0x00;
    428  1.42    bouyer 		ccb->target_stat = 0x00;
    429  1.42    bouyer 		ccb->link_id = 0;
    430  1.42    bouyer 		ltophys(0, ccb->link_addr);
    431  1.31   thorpej 
    432  1.42    bouyer 		BHA_CCB_SYNC(sc, ccb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    433  1.31   thorpej 
    434  1.42    bouyer 		s = splbio();
    435  1.42    bouyer 		bha_queue_ccb(sc, ccb);
    436  1.42    bouyer 		splx(s);
    437   1.1   mycroft 
    438  1.42    bouyer 		SC_DEBUG(periph, SCSIPI_DB3, ("cmd_sent\n"));
    439  1.42    bouyer 		if ((flags & XS_CTL_POLL) == 0)
    440  1.42    bouyer 			return;
    441  1.31   thorpej 
    442  1.42    bouyer 		/*
    443  1.42    bouyer 		 * If we can't use interrupts, poll on completion
    444  1.42    bouyer 		 */
    445  1.42    bouyer 		if (bha_poll(sc, xs, ccb->timeout)) {
    446  1.42    bouyer 			bha_timeout(ccb);
    447  1.42    bouyer 			if (bha_poll(sc, xs, ccb->timeout))
    448  1.42    bouyer 				bha_timeout(ccb);
    449  1.42    bouyer 		}
    450  1.42    bouyer 		return;
    451   1.1   mycroft 
    452  1.42    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
    453  1.42    bouyer 		if (sc->sc_cur_ccbs == sc->sc_max_ccbs) {
    454  1.42    bouyer 			chan->chan_flags &= ~SCSIPI_CHAN_CANGROW;
    455  1.42    bouyer 			return;
    456  1.42    bouyer 		}
    457  1.42    bouyer 		seg = sc->sc_cur_ccbs;
    458  1.42    bouyer 		bha_create_ccbs(sc, bha_ccbs_per_group);
    459  1.42    bouyer 		adapt->adapt_openings += sc->sc_cur_ccbs - seg;
    460  1.42    bouyer 		return;
    461   1.1   mycroft 
    462  1.42    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
    463  1.42    bouyer 		/*
    464  1.42    bouyer 		 * Can't really do this on the Buslogic.  It has its
    465  1.42    bouyer 		 * own setup info.  But we do know how to query what
    466  1.42    bouyer 		 * the settings are.
    467  1.42    bouyer 		 */
    468  1.42    bouyer 		bha_get_xfer_mode(sc, (struct scsipi_xfer_mode *)arg);
    469  1.42    bouyer 		return;
    470   1.1   mycroft 	}
    471   1.1   mycroft }
    472   1.1   mycroft 
    473   1.1   mycroft /*
    474  1.42    bouyer  * bha_minphys:
    475  1.42    bouyer  *
    476  1.42    bouyer  *	Limit a transfer to our maximum transfer size.
    477   1.1   mycroft  */
    478   1.1   mycroft void
    479  1.42    bouyer bha_minphys(bp)
    480  1.42    bouyer 	struct buf *bp;
    481   1.1   mycroft {
    482   1.1   mycroft 
    483  1.42    bouyer 	if (bp->b_bcount > BHA_MAXXFER)
    484  1.42    bouyer 		bp->b_bcount = BHA_MAXXFER;
    485  1.42    bouyer 	minphys(bp);
    486   1.1   mycroft }
    487   1.1   mycroft 
    488  1.42    bouyer /*****************************************************************************
    489  1.42    bouyer  * SCSI job execution helper routines
    490  1.42    bouyer  *****************************************************************************/
    491  1.38    simonb 
    492  1.38    simonb /*
    493  1.42    bouyer  * bha_get_xfer_mode;
    494  1.42    bouyer  *
    495  1.42    bouyer  *	Negotiate the xfer mode for the specified periph, and report
    496  1.42    bouyer  *	back the mode to the midlayer.
    497  1.42    bouyer  *
    498  1.42    bouyer  *	NOTE: we must be called at splbio().
    499  1.38    simonb  */
    500  1.38    simonb void
    501  1.42    bouyer bha_get_xfer_mode(sc, xm)
    502  1.38    simonb 	struct bha_softc *sc;
    503  1.42    bouyer 	struct scsipi_xfer_mode *xm;
    504  1.38    simonb {
    505  1.42    bouyer 	struct bha_setup hwsetup;
    506  1.42    bouyer 	struct bha_period hwperiod;
    507  1.42    bouyer 	struct bha_sync *bs;
    508  1.42    bouyer 	int toff = xm->xm_target & 7, tmask = (1 << toff);
    509  1.42    bouyer 	int wide, period, offset, rlen;
    510  1.42    bouyer 
    511  1.42    bouyer 	/*
    512  1.42    bouyer 	 * Issue an Inquire Setup Information.  We can extract
    513  1.42    bouyer 	 * sync and wide information from here.
    514  1.42    bouyer 	 */
    515  1.42    bouyer 	rlen = sizeof(hwsetup.reply) +
    516  1.42    bouyer 	    ((sc->sc_flags & BHAF_WIDE) ? sizeof(hwsetup.reply_w) : 0);
    517  1.42    bouyer 	hwsetup.cmd.opcode = BHA_INQUIRE_SETUP;
    518  1.42    bouyer 	hwsetup.cmd.len = rlen;
    519  1.43      ross 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
    520  1.42    bouyer 	    sizeof(hwsetup.cmd), (u_char *)&hwsetup.cmd,
    521  1.42    bouyer 	    rlen, (u_char *)&hwsetup.reply);
    522  1.38    simonb 
    523  1.42    bouyer 	xm->xm_mode = 0;
    524  1.42    bouyer 	xm->xm_period = 0;
    525  1.42    bouyer 	xm->xm_offset = 0;
    526  1.38    simonb 
    527  1.42    bouyer 	/*
    528  1.42    bouyer 	 * First check for wide.  On later boards, we can check
    529  1.42    bouyer 	 * directly in the setup info if wide is currently active.
    530  1.42    bouyer 	 *
    531  1.42    bouyer 	 * On earlier boards, we have to make an educated guess.
    532  1.42    bouyer 	 */
    533  1.42    bouyer 	if (sc->sc_flags & BHAF_WIDE) {
    534  1.42    bouyer 		if (strcmp(sc->sc_firmware, "5.06L") >= 0) {
    535  1.42    bouyer 			if (xm->xm_target > 7) {
    536  1.42    bouyer 				wide =
    537  1.42    bouyer 				    hwsetup.reply_w.high_wide_active & tmask;
    538  1.42    bouyer 			} else {
    539  1.42    bouyer 				wide =
    540  1.42    bouyer 				    hwsetup.reply_w.low_wide_active & tmask;
    541  1.38    simonb 			}
    542  1.42    bouyer 			if (wide)
    543  1.42    bouyer 				xm->xm_mode |= PERIPH_CAP_WIDE16;
    544  1.42    bouyer 		} else {
    545  1.42    bouyer 			/* XXX Check `wide permitted' in the config info. */
    546  1.42    bouyer 			xm->xm_mode |= PERIPH_CAP_WIDE16;
    547  1.38    simonb 		}
    548  1.42    bouyer 	}
    549  1.38    simonb 
    550  1.42    bouyer 	/*
    551  1.42    bouyer 	 * Now get basic sync info.
    552  1.42    bouyer 	 */
    553  1.42    bouyer 	bs = (xm->xm_target > 7) ?
    554  1.42    bouyer 	     &hwsetup.reply_w.sync_high[toff] :
    555  1.42    bouyer 	     &hwsetup.reply.sync_low[toff];
    556  1.38    simonb 
    557  1.42    bouyer 	if (bs->valid) {
    558  1.42    bouyer 		xm->xm_mode |= PERIPH_CAP_SYNC;
    559  1.42    bouyer 		period = (bs->period * 50) + 20;
    560  1.42    bouyer 		offset = bs->offset;
    561  1.38    simonb 
    562  1.42    bouyer 		/*
    563  1.42    bouyer 		 * On boards that can do Fast and Ultra, use the Inquire Period
    564  1.42    bouyer 		 * command to get the period.
    565  1.42    bouyer 		 */
    566  1.42    bouyer 		if (sc->sc_firmware[0] >= '3') {
    567  1.42    bouyer 			rlen = sizeof(hwperiod.reply) +
    568  1.42    bouyer 			    ((sc->sc_flags & BHAF_WIDE) ?
    569  1.42    bouyer 			      sizeof(hwperiod.reply_w) : 0);
    570  1.42    bouyer 			hwperiod.cmd.opcode = BHA_INQUIRE_PERIOD;
    571  1.42    bouyer 			hwperiod.cmd.len = rlen;
    572  1.43      ross 			bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
    573  1.42    bouyer 			    sizeof(hwperiod.cmd), (u_char *)&hwperiod.cmd,
    574  1.42    bouyer 			    rlen, (u_char *)&hwperiod.reply);
    575  1.38    simonb 
    576  1.42    bouyer 			if (xm->xm_target > 7)
    577  1.42    bouyer 				period = hwperiod.reply_w.period[toff];
    578  1.42    bouyer 			else
    579  1.42    bouyer 				period = hwperiod.reply.period[toff];
    580  1.38    simonb 
    581  1.42    bouyer 			period *= 10;
    582  1.42    bouyer 		}
    583  1.38    simonb 
    584  1.42    bouyer 		xm->xm_period =
    585  1.42    bouyer 		    scsipi_sync_period_to_factor(period * 10);
    586  1.42    bouyer 		xm->xm_offset = offset;
    587  1.42    bouyer 	}
    588  1.38    simonb 
    589  1.42    bouyer 	/*
    590  1.42    bouyer 	 * Now check for tagged queueing support.
    591  1.42    bouyer 	 *
    592  1.42    bouyer 	 * XXX Check `tags permitted' in the config info.
    593  1.42    bouyer 	 */
    594  1.42    bouyer 	if (sc->sc_flags & BHAF_TAGGED_QUEUEING)
    595  1.42    bouyer 		xm->xm_mode |= PERIPH_CAP_TQING;
    596  1.38    simonb 
    597  1.42    bouyer 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, xm);
    598  1.38    simonb }
    599  1.38    simonb 
    600  1.38    simonb /*
    601  1.42    bouyer  * bha_done:
    602  1.42    bouyer  *
    603  1.42    bouyer  *	A CCB has completed execution.  Pass the status back to the
    604  1.42    bouyer  *	upper layer.
    605  1.38    simonb  */
    606  1.38    simonb void
    607  1.38    simonb bha_done(sc, ccb)
    608  1.38    simonb 	struct bha_softc *sc;
    609  1.38    simonb 	struct bha_ccb *ccb;
    610  1.38    simonb {
    611  1.38    simonb 	bus_dma_tag_t dmat = sc->sc_dmat;
    612  1.38    simonb 	struct scsipi_xfer *xs = ccb->xs;
    613  1.38    simonb 
    614  1.42    bouyer 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("bha_done\n"));
    615   1.1   mycroft 
    616  1.38    simonb #ifdef BHADIAG
    617  1.38    simonb 	if (ccb->flags & CCB_SENDING) {
    618  1.38    simonb 		printf("%s: exiting ccb still in transit!\n",
    619  1.38    simonb 		    sc->sc_dev.dv_xname);
    620  1.38    simonb 		Debugger();
    621  1.38    simonb 		return;
    622  1.38    simonb 	}
    623  1.38    simonb #endif
    624  1.38    simonb 	if ((ccb->flags & CCB_ALLOC) == 0) {
    625  1.38    simonb 		printf("%s: exiting ccb not allocated!\n",
    626  1.38    simonb 		    sc->sc_dev.dv_xname);
    627  1.38    simonb 		Debugger();
    628  1.38    simonb 		return;
    629  1.38    simonb 	}
    630  1.42    bouyer 
    631  1.42    bouyer 	/*
    632  1.42    bouyer 	 * If we were a data transfer, unload the map that described
    633  1.42    bouyer 	 * the data buffer.
    634  1.42    bouyer 	 */
    635  1.42    bouyer 	if (xs->datalen) {
    636  1.42    bouyer 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    637  1.42    bouyer 		    ccb->dmamap_xfer->dm_mapsize,
    638  1.42    bouyer 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    639  1.42    bouyer 		    BUS_DMASYNC_POSTWRITE);
    640  1.42    bouyer 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
    641  1.42    bouyer 	}
    642  1.42    bouyer 
    643  1.31   thorpej 	if (xs->error == XS_NOERROR) {
    644  1.31   thorpej 		if (ccb->host_stat != BHA_OK) {
    645  1.31   thorpej 			switch (ccb->host_stat) {
    646  1.31   thorpej 			case BHA_SEL_TIMEOUT:	/* No response */
    647  1.31   thorpej 				xs->error = XS_SELTIMEOUT;
    648  1.31   thorpej 				break;
    649  1.31   thorpej 			default:	/* Other scsi protocol messes */
    650  1.31   thorpej 				printf("%s: host_stat %x\n",
    651   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->host_stat);
    652   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    653   1.1   mycroft 				break;
    654   1.1   mycroft 			}
    655   1.1   mycroft 		} else if (ccb->target_stat != SCSI_OK) {
    656   1.1   mycroft 			switch (ccb->target_stat) {
    657   1.1   mycroft 			case SCSI_CHECK:
    658  1.42    bouyer 				memcpy(&xs->sense.scsi_sense,
    659  1.42    bouyer 				    &ccb->scsi_sense,
    660  1.42    bouyer 				    sizeof(xs->sense.scsi_sense));
    661   1.1   mycroft 				xs->error = XS_SENSE;
    662   1.1   mycroft 				break;
    663   1.1   mycroft 			case SCSI_BUSY:
    664   1.1   mycroft 				xs->error = XS_BUSY;
    665   1.1   mycroft 				break;
    666   1.1   mycroft 			default:
    667   1.3  christos 				printf("%s: target_stat %x\n",
    668   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->target_stat);
    669   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    670   1.1   mycroft 				break;
    671   1.1   mycroft 			}
    672   1.1   mycroft 		} else
    673   1.1   mycroft 			xs->resid = 0;
    674   1.1   mycroft 	}
    675  1.42    bouyer 
    676   1.1   mycroft 	bha_free_ccb(sc, ccb);
    677  1.14    bouyer 	scsipi_done(xs);
    678   1.1   mycroft }
    679   1.1   mycroft 
    680   1.1   mycroft /*
    681  1.42    bouyer  * bha_poll:
    682  1.42    bouyer  *
    683  1.42    bouyer  *	Poll for completion of the specified job.
    684  1.31   thorpej  */
    685  1.31   thorpej int
    686  1.42    bouyer bha_poll(sc, xs, count)
    687  1.42    bouyer 	struct bha_softc *sc;
    688  1.42    bouyer 	struct scsipi_xfer *xs;
    689  1.42    bouyer 	int count;
    690  1.31   thorpej {
    691  1.42    bouyer 	bus_space_tag_t iot = sc->sc_iot;
    692  1.42    bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    693  1.31   thorpej 
    694  1.42    bouyer 	/* timeouts are in msec, so we loop in 1000 usec cycles */
    695  1.42    bouyer 	while (count) {
    696  1.42    bouyer 		/*
    697  1.42    bouyer 		 * If we had interrupts enabled, would we
    698  1.42    bouyer 		 * have got an interrupt?
    699  1.42    bouyer 		 */
    700  1.42    bouyer 		if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
    701  1.42    bouyer 		    BHA_INTR_ANYINTR)
    702  1.42    bouyer 			bha_intr(sc);
    703  1.42    bouyer 		if (xs->xs_status & XS_STS_DONE)
    704  1.42    bouyer 			return (0);
    705  1.42    bouyer 		delay(1000);	/* only happens in boot so ok */
    706  1.42    bouyer 		count--;
    707  1.42    bouyer 	}
    708  1.42    bouyer 	return (1);
    709  1.42    bouyer }
    710  1.31   thorpej 
    711  1.42    bouyer /*
    712  1.42    bouyer  * bha_timeout:
    713  1.42    bouyer  *
    714  1.42    bouyer  *	CCB timeout handler.
    715  1.42    bouyer  */
    716  1.42    bouyer void
    717  1.42    bouyer bha_timeout(arg)
    718  1.42    bouyer 	void *arg;
    719  1.42    bouyer {
    720  1.42    bouyer 	struct bha_ccb *ccb = arg;
    721  1.42    bouyer 	struct scsipi_xfer *xs = ccb->xs;
    722  1.42    bouyer 	struct scsipi_periph *periph = xs->xs_periph;
    723  1.42    bouyer 	struct bha_softc *sc =
    724  1.42    bouyer 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    725  1.42    bouyer 	int s;
    726  1.31   thorpej 
    727  1.42    bouyer 	scsipi_printaddr(periph);
    728  1.42    bouyer 	printf("timed out");
    729  1.31   thorpej 
    730  1.42    bouyer 	s = splbio();
    731  1.42    bouyer 
    732  1.42    bouyer #ifdef BHADIAG
    733  1.42    bouyer 	/*
    734  1.42    bouyer 	 * If the ccb's mbx is not free, then the board has gone Far East?
    735  1.42    bouyer 	 */
    736  1.42    bouyer 	bha_collect_mbo(sc);
    737  1.42    bouyer 	if (ccb->flags & CCB_SENDING) {
    738  1.42    bouyer 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
    739  1.42    bouyer 		Debugger();
    740  1.42    bouyer 	}
    741  1.42    bouyer #endif
    742  1.42    bouyer 
    743  1.42    bouyer 	/*
    744  1.42    bouyer 	 * If it has been through before, then
    745  1.42    bouyer 	 * a previous abort has failed, don't
    746  1.42    bouyer 	 * try abort again
    747  1.42    bouyer 	 */
    748  1.42    bouyer 	if (ccb->flags & CCB_ABORT) {
    749  1.42    bouyer 		/* abort timed out */
    750  1.42    bouyer 		printf(" AGAIN\n");
    751  1.42    bouyer 		/* XXX Must reset! */
    752  1.42    bouyer 	} else {
    753  1.42    bouyer 		/* abort the operation that has timed out */
    754  1.42    bouyer 		printf("\n");
    755  1.42    bouyer 		ccb->xs->error = XS_TIMEOUT;
    756  1.42    bouyer 		ccb->timeout = BHA_ABORT_TIMEOUT;
    757  1.42    bouyer 		ccb->flags |= CCB_ABORT;
    758  1.42    bouyer 		bha_queue_ccb(sc, ccb);
    759  1.42    bouyer 	}
    760  1.42    bouyer 
    761  1.42    bouyer 	splx(s);
    762  1.42    bouyer }
    763  1.42    bouyer 
    764  1.42    bouyer /*****************************************************************************
    765  1.42    bouyer  * Misc. subroutines.
    766  1.42    bouyer  *****************************************************************************/
    767  1.42    bouyer 
    768  1.42    bouyer /*
    769  1.42    bouyer  * bha_cmd:
    770  1.42    bouyer  *
    771  1.42    bouyer  *	Send a command to the Buglogic controller.
    772  1.42    bouyer  */
    773  1.42    bouyer int
    774  1.43      ross bha_cmd(iot, ioh, name, icnt, ibuf, ocnt, obuf)
    775  1.42    bouyer 	bus_space_tag_t iot;
    776  1.42    bouyer 	bus_space_handle_t ioh;
    777  1.43      ross 	char *name;
    778  1.42    bouyer 	int icnt, ocnt;
    779  1.42    bouyer 	u_char *ibuf, *obuf;
    780  1.42    bouyer {
    781  1.42    bouyer 	int i;
    782  1.42    bouyer 	int wait;
    783  1.42    bouyer 	u_char sts;
    784  1.42    bouyer 	u_char opcode = ibuf[0];
    785  1.42    bouyer 
    786  1.42    bouyer 	/*
    787  1.42    bouyer 	 * Calculate a reasonable timeout for the command.
    788  1.42    bouyer 	 */
    789  1.42    bouyer 	switch (opcode) {
    790  1.42    bouyer 	case BHA_INQUIRE_DEVICES:
    791  1.42    bouyer 	case BHA_INQUIRE_DEVICES_2:
    792  1.42    bouyer 		wait = 90 * 20000;
    793  1.42    bouyer 		break;
    794  1.42    bouyer 	default:
    795  1.42    bouyer 		wait = 1 * 20000;
    796  1.42    bouyer 		break;
    797  1.42    bouyer 	}
    798  1.42    bouyer 
    799  1.42    bouyer 	/*
    800  1.42    bouyer 	 * Wait for the adapter to go idle, unless it's one of
    801  1.42    bouyer 	 * the commands which don't need this
    802  1.42    bouyer 	 */
    803  1.42    bouyer 	if (opcode != BHA_MBO_INTR_EN) {
    804  1.42    bouyer 		for (i = 20000; i; i--) {	/* 1 sec? */
    805  1.42    bouyer 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    806  1.42    bouyer 			if (sts & BHA_STAT_IDLE)
    807  1.42    bouyer 				break;
    808  1.42    bouyer 			delay(50);
    809  1.42    bouyer 		}
    810  1.42    bouyer 		if (!i) {
    811  1.42    bouyer 			printf("%s: bha_cmd, host not idle(0x%x)\n",
    812  1.42    bouyer 			    name, sts);
    813  1.42    bouyer 			return (1);
    814  1.42    bouyer 		}
    815  1.42    bouyer 	}
    816  1.42    bouyer 
    817  1.42    bouyer 	/*
    818  1.42    bouyer 	 * Now that it is idle, if we expect output, preflush the
    819  1.42    bouyer 	 * queue feeding to us.
    820  1.42    bouyer 	 */
    821  1.42    bouyer 	if (ocnt) {
    822  1.42    bouyer 		while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
    823  1.42    bouyer 		    BHA_STAT_DF)
    824  1.42    bouyer 			bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    825  1.42    bouyer 	}
    826  1.42    bouyer 
    827  1.42    bouyer 	/*
    828  1.42    bouyer 	 * Output the command and the number of arguments given
    829  1.42    bouyer 	 * for each byte, first check the port is empty.
    830  1.42    bouyer 	 */
    831  1.42    bouyer 	while (icnt--) {
    832  1.42    bouyer 		for (i = wait; i; i--) {
    833  1.42    bouyer 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    834  1.42    bouyer 			if (!(sts & BHA_STAT_CDF))
    835  1.42    bouyer 				break;
    836  1.42    bouyer 			delay(50);
    837  1.42    bouyer 		}
    838  1.42    bouyer 		if (!i) {
    839  1.42    bouyer 			if (opcode != BHA_INQUIRE_REVISION)
    840  1.42    bouyer 				printf("%s: bha_cmd, cmd/data port full\n",
    841  1.42    bouyer 				    name);
    842  1.42    bouyer 			goto bad;
    843  1.42    bouyer 		}
    844  1.42    bouyer 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
    845  1.42    bouyer 	}
    846  1.42    bouyer 
    847  1.42    bouyer 	/*
    848  1.42    bouyer 	 * If we expect input, loop that many times, each time,
    849  1.42    bouyer 	 * looking for the data register to have valid data
    850  1.42    bouyer 	 */
    851  1.42    bouyer 	while (ocnt--) {
    852  1.42    bouyer 		for (i = wait; i; i--) {
    853  1.42    bouyer 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    854  1.42    bouyer 			if (sts & BHA_STAT_DF)
    855  1.42    bouyer 				break;
    856  1.42    bouyer 			delay(50);
    857  1.42    bouyer 		}
    858  1.42    bouyer 		if (!i) {
    859  1.42    bouyer #ifdef BHADEBUG
    860  1.42    bouyer 			if (opcode != BHA_INQUIRE_REVISION)
    861  1.42    bouyer 				printf("%s: bha_cmd, cmd/data port empty %d\n",
    862  1.42    bouyer 				    name, ocnt);
    863  1.42    bouyer #endif /* BHADEBUG */
    864  1.42    bouyer 			goto bad;
    865  1.42    bouyer 		}
    866  1.42    bouyer 		*obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    867  1.42    bouyer 	}
    868  1.42    bouyer 
    869  1.42    bouyer 	/*
    870  1.42    bouyer 	 * Wait for the board to report a finished instruction.
    871  1.42    bouyer 	 * We may get an extra interrupt for the HACC signal, but this is
    872  1.42    bouyer 	 * unimportant.
    873  1.42    bouyer 	 */
    874  1.42    bouyer 	if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
    875  1.42    bouyer 		for (i = 20000; i; i--) {	/* 1 sec? */
    876  1.42    bouyer 			sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    877  1.42    bouyer 			/* XXX Need to save this in the interrupt handler? */
    878  1.42    bouyer 			if (sts & BHA_INTR_HACC)
    879  1.42    bouyer 				break;
    880  1.42    bouyer 			delay(50);
    881  1.42    bouyer 		}
    882  1.42    bouyer 		if (!i) {
    883  1.42    bouyer 			printf("%s: bha_cmd, host not finished(0x%x)\n",
    884  1.42    bouyer 			    name, sts);
    885  1.42    bouyer 			return (1);
    886  1.42    bouyer 		}
    887  1.42    bouyer 	}
    888  1.42    bouyer 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    889  1.42    bouyer 	return (0);
    890  1.42    bouyer 
    891  1.42    bouyer bad:
    892  1.42    bouyer 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
    893  1.42    bouyer 	return (1);
    894  1.42    bouyer }
    895  1.42    bouyer 
    896  1.42    bouyer /*
    897  1.42    bouyer  * bha_find:
    898  1.42    bouyer  *
    899  1.43      ross  *	Find the board.
    900  1.42    bouyer  */
    901  1.42    bouyer int
    902  1.43      ross bha_find(iot, ioh)
    903  1.42    bouyer 	bus_space_tag_t iot;
    904  1.42    bouyer 	bus_space_handle_t ioh;
    905  1.42    bouyer {
    906  1.42    bouyer 	int i;
    907  1.42    bouyer 	u_char sts;
    908  1.42    bouyer 	struct bha_extended_inquire inquire;
    909  1.42    bouyer 
    910  1.42    bouyer 	/* Check something is at the ports we need to access */
    911  1.42    bouyer 	sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    912  1.42    bouyer 	if (sts == 0xFF)
    913  1.42    bouyer 		return (0);
    914  1.42    bouyer 
    915  1.42    bouyer 	/*
    916  1.42    bouyer 	 * Reset board, If it doesn't respond, assume
    917  1.42    bouyer 	 * that it's not there.. good for the probe
    918  1.42    bouyer 	 */
    919  1.42    bouyer 
    920  1.42    bouyer 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
    921  1.42    bouyer 	    BHA_CTRL_HRST | BHA_CTRL_SRST);
    922  1.42    bouyer 
    923  1.42    bouyer 	delay(100);
    924  1.42    bouyer 	for (i = BHA_RESET_TIMEOUT; i; i--) {
    925  1.42    bouyer 		sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    926  1.42    bouyer 		if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
    927  1.42    bouyer 			break;
    928  1.42    bouyer 		delay(1000);
    929  1.42    bouyer 	}
    930  1.42    bouyer 	if (!i) {
    931  1.42    bouyer #ifdef BHADEBUG
    932  1.42    bouyer 		if (bha_debug)
    933  1.42    bouyer 			printf("bha_find: No answer from buslogic board\n");
    934  1.42    bouyer #endif /* BHADEBUG */
    935  1.42    bouyer 		return (0);
    936  1.42    bouyer 	}
    937  1.31   thorpej 
    938  1.31   thorpej 	/*
    939  1.38    simonb 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
    940  1.38    simonb 	 * interface. The native bha interface is not compatible with
    941  1.38    simonb 	 * an aha. 1542. We need to ensure that we never match an
    942  1.38    simonb 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
    943  1.38    simonb 	 * commands to a real bha, lest it go into 1542 emulation mode.
    944  1.38    simonb 	 * (On an indirect bus like ISA, we should always probe for BusLogic
    945  1.42    bouyer 	 * interfaces before Adaptec interfaces).
    946  1.31   thorpej 	 */
    947  1.31   thorpej 
    948  1.31   thorpej 	/*
    949  1.38    simonb 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
    950  1.38    simonb 	 * for an extended-geometry register.  The 1542[AB] don't have one.
    951  1.31   thorpej 	 */
    952  1.38    simonb 	sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
    953  1.38    simonb 	if (sts == 0xFF)
    954  1.38    simonb 		return (0);
    955  1.31   thorpej 
    956  1.38    simonb 	/*
    957  1.38    simonb 	 * Check that we actually know how to use this board.
    958  1.38    simonb 	 */
    959  1.38    simonb 	delay(1000);
    960  1.38    simonb 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
    961  1.38    simonb 	inquire.cmd.len = sizeof(inquire.reply);
    962  1.43      ross 	i = bha_cmd(iot, ioh, "(bha_find)",
    963  1.38    simonb 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
    964  1.38    simonb 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
    965  1.31   thorpej 
    966  1.38    simonb 	/*
    967  1.38    simonb 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
    968  1.38    simonb 	 * have the extended-geometry register and also respond to
    969  1.42    bouyer 	 * BHA_INQUIRE_EXTENDED.  Make sure we never match such cards,
    970  1.38    simonb 	 * by checking the size of the reply is what a BusLogic card returns.
    971  1.38    simonb 	 */
    972  1.38    simonb 	if (i) {
    973  1.38    simonb #ifdef BHADEBUG
    974  1.38    simonb 		printf("bha_find: board returned %d instead of %d to %s\n",
    975  1.38    simonb 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
    976  1.38    simonb #endif
    977  1.38    simonb 		return (0);
    978  1.38    simonb 	}
    979  1.31   thorpej 
    980  1.38    simonb 	/* OK, we know we've found a buslogic adaptor. */
    981   1.1   mycroft 
    982  1.38    simonb 	switch (inquire.reply.bus_type) {
    983  1.38    simonb 	case BHA_BUS_TYPE_24BIT:
    984  1.38    simonb 	case BHA_BUS_TYPE_32BIT:
    985  1.31   thorpej 		break;
    986  1.38    simonb 	case BHA_BUS_TYPE_MCA:
    987  1.38    simonb 		/* We don't grok MicroChannel (yet). */
    988  1.38    simonb 		return (0);
    989  1.31   thorpej 	default:
    990  1.38    simonb 		printf("bha_find: illegal bus type %c\n",
    991  1.38    simonb 		    inquire.reply.bus_type);
    992  1.38    simonb 		return (0);
    993   1.1   mycroft 	}
    994   1.1   mycroft 
    995  1.43      ross 	return (1);
    996  1.43      ross }
    997  1.43      ross 
    998  1.43      ross 
    999  1.43      ross /*
   1000  1.43      ross  * bha_inquire_config:
   1001  1.43      ross  *
   1002  1.43      ross  *	Determine irq/drq.
   1003  1.43      ross  */
   1004  1.43      ross int
   1005  1.43      ross bha_inquire_config(bus_space_tag_t iot, bus_space_handle_t ioh,
   1006  1.43      ross 	    struct bha_probe_data *sc)
   1007  1.43      ross {
   1008  1.43      ross 	int irq, drq;
   1009  1.43      ross 	struct bha_config config;
   1010  1.43      ross 
   1011   1.1   mycroft 	/*
   1012  1.38    simonb 	 * Assume we have a board at this stage setup dma channel from
   1013  1.38    simonb 	 * jumpers and save int level
   1014   1.5  jonathan 	 */
   1015  1.38    simonb 	delay(1000);
   1016  1.38    simonb 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
   1017  1.43      ross 	bha_cmd(iot, ioh, "(bha_inquire_config)",
   1018  1.38    simonb 	    sizeof(config.cmd), (u_char *)&config.cmd,
   1019  1.38    simonb 	    sizeof(config.reply), (u_char *)&config.reply);
   1020  1.38    simonb 	switch (config.reply.chan) {
   1021  1.38    simonb 	case EISADMA:
   1022  1.38    simonb 		drq = -1;
   1023  1.38    simonb 		break;
   1024  1.38    simonb 	case CHAN0:
   1025  1.38    simonb 		drq = 0;
   1026  1.38    simonb 		break;
   1027  1.38    simonb 	case CHAN5:
   1028   1.1   mycroft 		drq = 5;
   1029   1.1   mycroft 		break;
   1030   1.1   mycroft 	case CHAN6:
   1031   1.1   mycroft 		drq = 6;
   1032   1.1   mycroft 		break;
   1033   1.1   mycroft 	case CHAN7:
   1034   1.1   mycroft 		drq = 7;
   1035   1.1   mycroft 		break;
   1036   1.1   mycroft 	default:
   1037  1.43      ross 		printf("bha: illegal drq setting %x\n",
   1038   1.4   thorpej 		    config.reply.chan);
   1039   1.1   mycroft 		return (0);
   1040   1.1   mycroft 	}
   1041   1.1   mycroft 
   1042   1.1   mycroft 	switch (config.reply.intr) {
   1043   1.1   mycroft 	case INT9:
   1044   1.1   mycroft 		irq = 9;
   1045   1.1   mycroft 		break;
   1046   1.1   mycroft 	case INT10:
   1047   1.1   mycroft 		irq = 10;
   1048   1.1   mycroft 		break;
   1049   1.1   mycroft 	case INT11:
   1050   1.1   mycroft 		irq = 11;
   1051   1.1   mycroft 		break;
   1052   1.1   mycroft 	case INT12:
   1053   1.1   mycroft 		irq = 12;
   1054   1.1   mycroft 		break;
   1055   1.1   mycroft 	case INT14:
   1056   1.1   mycroft 		irq = 14;
   1057   1.1   mycroft 		break;
   1058   1.1   mycroft 	case INT15:
   1059   1.1   mycroft 		irq = 15;
   1060   1.1   mycroft 		break;
   1061   1.1   mycroft 	default:
   1062  1.43      ross 		printf("bha: illegal irq setting %x\n",
   1063   1.4   thorpej 		    config.reply.intr);
   1064   1.1   mycroft 		return (0);
   1065  1.31   thorpej 	}
   1066  1.31   thorpej 
   1067  1.31   thorpej 	/* if we want to fill in softc, do so now */
   1068  1.31   thorpej 	if (sc != NULL) {
   1069  1.31   thorpej 		sc->sc_irq = irq;
   1070  1.31   thorpej 		sc->sc_drq = drq;
   1071  1.31   thorpej 	}
   1072  1.31   thorpej 
   1073  1.31   thorpej 	return (1);
   1074  1.31   thorpej }
   1075  1.31   thorpej 
   1076  1.43      ross int
   1077  1.43      ross bha_probe_inquiry(bus_space_tag_t iot, bus_space_handle_t ioh,
   1078  1.43      ross     struct bha_probe_data *bpd)
   1079  1.43      ross {
   1080  1.43      ross 	return bha_find(iot, ioh) && bha_inquire_config(iot, ioh, bpd);
   1081  1.43      ross }
   1082  1.43      ross 
   1083  1.31   thorpej /*
   1084  1.42    bouyer  * bha_disable_isacompat:
   1085  1.42    bouyer  *
   1086  1.51       wiz  *	Disable the ISA-compatibility ioports on PCI bha devices,
   1087  1.42    bouyer  *	to ensure they're not autoconfigured a second time as an ISA bha.
   1088  1.31   thorpej  */
   1089  1.31   thorpej int
   1090  1.31   thorpej bha_disable_isacompat(sc)
   1091  1.31   thorpej 	struct bha_softc *sc;
   1092  1.31   thorpej {
   1093  1.31   thorpej 	struct bha_isadisable isa_disable;
   1094  1.31   thorpej 
   1095  1.31   thorpej 	isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
   1096  1.31   thorpej 	isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
   1097  1.43      ross 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
   1098  1.31   thorpej 	    sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
   1099  1.31   thorpej 	    0, (u_char *)0);
   1100  1.31   thorpej 	return (0);
   1101  1.31   thorpej }
   1102  1.31   thorpej 
   1103  1.31   thorpej /*
   1104  1.42    bouyer  * bha_info:
   1105  1.42    bouyer  *
   1106  1.42    bouyer  *	Get information about the board, and report it.  We
   1107  1.42    bouyer  *	return the initial number of CCBs, 0 if we failed.
   1108  1.31   thorpej  */
   1109  1.31   thorpej int
   1110  1.42    bouyer bha_info(sc)
   1111  1.31   thorpej 	struct bha_softc *sc;
   1112  1.31   thorpej {
   1113  1.31   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1114  1.31   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1115  1.42    bouyer 	struct bha_extended_inquire inquire;
   1116  1.42    bouyer 	struct bha_config config;
   1117  1.31   thorpej 	struct bha_devices devices;
   1118  1.31   thorpej 	struct bha_setup setup;
   1119  1.42    bouyer 	struct bha_model model;
   1120  1.42    bouyer 	struct bha_revision revision;
   1121  1.42    bouyer 	struct bha_digit digit;
   1122  1.42    bouyer 	int i, j, initial_ccbs, rlen;
   1123  1.43      ross 	char *name = sc->sc_dev.dv_xname;
   1124  1.42    bouyer 	char *p;
   1125  1.31   thorpej 
   1126  1.31   thorpej 	/*
   1127  1.42    bouyer 	 * Fetch the extended inquire information.
   1128  1.31   thorpej 	 */
   1129  1.42    bouyer 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
   1130  1.42    bouyer 	inquire.cmd.len = sizeof(inquire.reply);
   1131  1.43      ross 	bha_cmd(iot, ioh, name,
   1132  1.42    bouyer 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
   1133  1.42    bouyer 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
   1134  1.31   thorpej 
   1135  1.31   thorpej 	/*
   1136  1.42    bouyer 	 * Fetch the configuration information.
   1137  1.31   thorpej 	 */
   1138  1.42    bouyer 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
   1139  1.43      ross 	bha_cmd(iot, ioh, name,
   1140  1.42    bouyer 	    sizeof(config.cmd), (u_char *)&config.cmd,
   1141  1.42    bouyer 	    sizeof(config.reply), (u_char *)&config.reply);
   1142  1.31   thorpej 
   1143  1.42    bouyer 	sc->sc_scsi_id = config.reply.scsi_dev;
   1144  1.31   thorpej 
   1145  1.31   thorpej 	/*
   1146  1.42    bouyer 	 * Get the firmware revision.
   1147  1.38    simonb 	 */
   1148  1.42    bouyer 	p = sc->sc_firmware;
   1149  1.42    bouyer 	revision.cmd.opcode = BHA_INQUIRE_REVISION;
   1150  1.43      ross 	bha_cmd(iot, ioh, name,
   1151  1.42    bouyer 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
   1152  1.42    bouyer 	    sizeof(revision.reply), (u_char *)&revision.reply);
   1153  1.31   thorpej 	*p++ = revision.reply.firm_revision;
   1154  1.31   thorpej 	*p++ = '.';
   1155  1.31   thorpej 	*p++ = revision.reply.firm_version;
   1156  1.31   thorpej 	digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
   1157  1.43      ross 	bha_cmd(iot, ioh, name,
   1158  1.31   thorpej 	    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1159  1.31   thorpej 	    sizeof(digit.reply), (u_char *)&digit.reply);
   1160  1.31   thorpej 	*p++ = digit.reply.digit;
   1161  1.31   thorpej 	if (revision.reply.firm_revision >= '3' ||
   1162  1.31   thorpej 	    (revision.reply.firm_revision == '3' &&
   1163  1.31   thorpej 	     revision.reply.firm_version >= '3')) {
   1164  1.31   thorpej 		digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
   1165  1.43      ross 		bha_cmd(iot, ioh, name,
   1166  1.31   thorpej 		    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1167  1.31   thorpej 		    sizeof(digit.reply), (u_char *)&digit.reply);
   1168  1.31   thorpej 		*p++ = digit.reply.digit;
   1169  1.31   thorpej 	}
   1170  1.31   thorpej 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
   1171  1.31   thorpej 		p--;
   1172  1.31   thorpej 	*p = '\0';
   1173  1.31   thorpej 
   1174  1.31   thorpej 	/*
   1175  1.31   thorpej 	 * Get the model number.
   1176  1.42    bouyer 	 *
   1177  1.42    bouyer 	 * Some boards do not handle the Inquire Board Model Number
   1178  1.42    bouyer 	 * command correctly, or don't give correct information.
   1179  1.42    bouyer 	 *
   1180  1.42    bouyer 	 * So, we use the Firmware Revision and Extended Setup
   1181  1.42    bouyer 	 * information to fixup the model number in these cases.
   1182  1.42    bouyer 	 *
   1183  1.42    bouyer 	 * The firmware version indicates:
   1184  1.42    bouyer 	 *
   1185  1.42    bouyer 	 *	5.xx	BusLogic "W" Series Host Adapters
   1186  1.42    bouyer 	 *		BT-948/958/958D
   1187  1.42    bouyer 	 *
   1188  1.42    bouyer 	 *	4.xx	BusLogic "C" Series Host Adapters
   1189  1.42    bouyer 	 *		BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
   1190  1.42    bouyer 	 *
   1191  1.42    bouyer 	 *	3.xx	BusLogic "S" Series Host Adapters
   1192  1.42    bouyer 	 *		BT-747S/747D/757S/757D/445S/545S/542D
   1193  1.42    bouyer 	 *		BT-542B/742A (revision H)
   1194  1.42    bouyer 	 *
   1195  1.42    bouyer 	 *	2.xx	BusLogic "A" Series Host Adapters
   1196  1.42    bouyer 	 *		BT-542B/742A (revision G and below)
   1197  1.42    bouyer 	 *
   1198  1.42    bouyer 	 *	0.xx	AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
   1199  1.31   thorpej 	 */
   1200  1.42    bouyer 	if (inquire.reply.bus_type == BHA_BUS_TYPE_24BIT &&
   1201  1.42    bouyer 	    sc->sc_firmware[0] < '3')
   1202  1.42    bouyer 		sprintf(sc->sc_model, "542B");
   1203  1.42    bouyer 	else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
   1204  1.42    bouyer 	    sc->sc_firmware[0] == '2' &&
   1205  1.42    bouyer 	    (sc->sc_firmware[2] == '1' ||
   1206  1.42    bouyer 	     (sc->sc_firmware[2] == '2' && sc->sc_firmware[3] == '0')))
   1207  1.42    bouyer 		sprintf(sc->sc_model, "742A");
   1208  1.42    bouyer 	else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
   1209  1.42    bouyer 	    sc->sc_firmware[0] == '0')
   1210  1.42    bouyer 		sprintf(sc->sc_model, "747A");
   1211  1.42    bouyer 	else {
   1212  1.31   thorpej 		p = sc->sc_model;
   1213  1.31   thorpej 		model.cmd.opcode = BHA_INQUIRE_MODEL;
   1214  1.31   thorpej 		model.cmd.len = sizeof(model.reply);
   1215  1.43      ross 		bha_cmd(iot, ioh, name,
   1216  1.31   thorpej 		    sizeof(model.cmd), (u_char *)&model.cmd,
   1217  1.31   thorpej 		    sizeof(model.reply), (u_char *)&model.reply);
   1218  1.31   thorpej 		*p++ = model.reply.id[0];
   1219  1.31   thorpej 		*p++ = model.reply.id[1];
   1220  1.31   thorpej 		*p++ = model.reply.id[2];
   1221  1.31   thorpej 		*p++ = model.reply.id[3];
   1222  1.31   thorpej 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1223  1.31   thorpej 			p--;
   1224  1.31   thorpej 		*p++ = model.reply.version[0];
   1225  1.31   thorpej 		*p++ = model.reply.version[1];
   1226  1.31   thorpej 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1227  1.31   thorpej 			p--;
   1228  1.31   thorpej 		*p = '\0';
   1229  1.42    bouyer 	}
   1230  1.42    bouyer 
   1231  1.42    bouyer 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
   1232  1.42    bouyer 	if (strcmp(sc->sc_firmware, "3.31") >= 0)
   1233  1.42    bouyer 		sc->sc_flags |= BHAF_STRICT_ROUND_ROBIN;
   1234  1.42    bouyer 
   1235  1.42    bouyer 	/*
   1236  1.42    bouyer 	 * Determine some characteristics about our bus.
   1237  1.42    bouyer 	 */
   1238  1.42    bouyer 	if (inquire.reply.scsi_flags & BHA_SCSI_WIDE)
   1239  1.42    bouyer 		sc->sc_flags |= BHAF_WIDE;
   1240  1.42    bouyer 	if (inquire.reply.scsi_flags & BHA_SCSI_DIFFERENTIAL)
   1241  1.42    bouyer 		sc->sc_flags |= BHAF_DIFFERENTIAL;
   1242  1.42    bouyer 	if (inquire.reply.scsi_flags & BHA_SCSI_ULTRA)
   1243  1.42    bouyer 		sc->sc_flags |= BHAF_ULTRA;
   1244  1.31   thorpej 
   1245  1.42    bouyer 	/*
   1246  1.42    bouyer 	 * Determine some characterists of the board.
   1247  1.42    bouyer 	 */
   1248  1.42    bouyer 	sc->sc_max_dmaseg = inquire.reply.sg_limit;
   1249  1.31   thorpej 
   1250  1.42    bouyer 	/*
   1251  1.42    bouyer 	 * Determine the maximum CCB count and whether or not
   1252  1.42    bouyer 	 * tagged queueing is available on this host adapter.
   1253  1.42    bouyer 	 *
   1254  1.42    bouyer 	 * Tagged queueing works on:
   1255  1.42    bouyer 	 *
   1256  1.42    bouyer 	 *	"W" Series adapters
   1257  1.42    bouyer 	 *	"C" Series adapters with firmware >= 4.22
   1258  1.42    bouyer 	 *	"S" Series adapters with firmware >= 3.35
   1259  1.42    bouyer 	 *
   1260  1.42    bouyer 	 * The internal CCB counts are:
   1261  1.42    bouyer 	 *
   1262  1.42    bouyer 	 *	192	BT-948/958/958D
   1263  1.42    bouyer 	 *	100	BT-946C/956C/956CD/747C/757C/757CD/445C
   1264  1.42    bouyer 	 *	50	BT-545C/540CF
   1265  1.42    bouyer 	 *	30	BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
   1266  1.42    bouyer 	 */
   1267  1.42    bouyer 	switch (sc->sc_firmware[0]) {
   1268  1.42    bouyer 	case '5':
   1269  1.42    bouyer 		sc->sc_max_ccbs = 192;
   1270  1.42    bouyer 		sc->sc_flags |= BHAF_TAGGED_QUEUEING;
   1271  1.42    bouyer 		break;
   1272  1.31   thorpej 
   1273  1.42    bouyer 	case '4':
   1274  1.42    bouyer 		if (sc->sc_model[0] == '5')
   1275  1.42    bouyer 			sc->sc_max_ccbs = 50;
   1276  1.42    bouyer 		else
   1277  1.42    bouyer 			sc->sc_max_ccbs = 100;
   1278  1.42    bouyer 		if (strcmp(sc->sc_firmware, "4.22") >= 0)
   1279  1.42    bouyer 			sc->sc_flags |= BHAF_TAGGED_QUEUEING;
   1280  1.42    bouyer 		break;
   1281  1.38    simonb 
   1282  1.42    bouyer 	case '3':
   1283  1.42    bouyer 		if (strcmp(sc->sc_firmware, "3.35") >= 0)
   1284  1.42    bouyer 			sc->sc_flags |= BHAF_TAGGED_QUEUEING;
   1285  1.42    bouyer 		/* FALLTHROUGH */
   1286  1.38    simonb 
   1287  1.42    bouyer 	default:
   1288  1.42    bouyer 		sc->sc_max_ccbs = 30;
   1289  1.42    bouyer 	}
   1290  1.31   thorpej 
   1291  1.31   thorpej 	/*
   1292  1.42    bouyer 	 * Set the mailbox count to precisely the number of HW CCBs
   1293  1.42    bouyer 	 * available.  A mailbox isn't required while a CCB is executing,
   1294  1.42    bouyer 	 * but this allows us to actually enqueue up to our resource
   1295  1.42    bouyer 	 * limit.
   1296  1.42    bouyer 	 *
   1297  1.42    bouyer 	 * This will keep the mailbox count small on boards which don't
   1298  1.42    bouyer 	 * have strict round-robin (they have to scan the entire set of
   1299  1.42    bouyer 	 * mailboxes each time they run a command).
   1300  1.31   thorpej 	 */
   1301  1.42    bouyer 	sc->sc_mbox_count = sc->sc_max_ccbs;
   1302  1.31   thorpej 
   1303  1.31   thorpej 	/*
   1304  1.42    bouyer 	 * Obtain setup information.
   1305  1.31   thorpej 	 */
   1306  1.42    bouyer 	rlen = sizeof(setup.reply) +
   1307  1.42    bouyer 	    ((sc->sc_flags & BHAF_WIDE) ? sizeof(setup.reply_w) : 0);
   1308  1.42    bouyer 	setup.cmd.opcode = BHA_INQUIRE_SETUP;
   1309  1.42    bouyer 	setup.cmd.len = rlen;
   1310  1.43      ross 	bha_cmd(iot, ioh, name,
   1311  1.42    bouyer 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
   1312  1.42    bouyer 	    rlen, (u_char *)&setup.reply);
   1313  1.42    bouyer 
   1314  1.42    bouyer 	printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
   1315  1.42    bouyer 	    sc->sc_model, sc->sc_firmware);
   1316  1.38    simonb 
   1317  1.42    bouyer 	printf("%s: %d H/W CCBs", sc->sc_dev.dv_xname, sc->sc_max_ccbs);
   1318  1.42    bouyer 	if (setup.reply.sync_neg)
   1319  1.42    bouyer 		printf(", sync");
   1320  1.42    bouyer 	if (setup.reply.parity)
   1321  1.42    bouyer 		printf(", parity");
   1322  1.42    bouyer 	if (sc->sc_flags & BHAF_TAGGED_QUEUEING)
   1323  1.42    bouyer 		printf(", tagged queueing");
   1324  1.42    bouyer 	if (sc->sc_flags & BHAF_WIDE_LUN)
   1325  1.42    bouyer 		printf(", wide LUN support");
   1326  1.42    bouyer 	printf("\n");
   1327  1.31   thorpej 
   1328  1.31   thorpej 	/*
   1329  1.42    bouyer 	 * Poll targets 0 - 7.
   1330  1.31   thorpej 	 */
   1331  1.42    bouyer 	devices.cmd.opcode = BHA_INQUIRE_DEVICES;
   1332  1.43      ross 	bha_cmd(iot, ioh, name,
   1333  1.42    bouyer 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1334  1.42    bouyer 	    sizeof(devices.reply), (u_char *)&devices.reply);
   1335  1.31   thorpej 
   1336  1.42    bouyer 	/* Count installed units. */
   1337  1.42    bouyer 	initial_ccbs = 0;
   1338  1.42    bouyer 	for (i = 0; i < 8; i++) {
   1339  1.42    bouyer 		for (j = 0; j < 8; j++) {
   1340  1.42    bouyer 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1341  1.42    bouyer 				initial_ccbs++;
   1342  1.31   thorpej 		}
   1343  1.31   thorpej 	}
   1344  1.31   thorpej 
   1345  1.17   thorpej 	/*
   1346  1.42    bouyer 	 * Poll targets 8 - 15 if we have a wide bus.
   1347  1.17   thorpej 	 */
   1348  1.42    bouyer 	if (sc->sc_flags & BHAF_WIDE) {
   1349  1.42    bouyer 		devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
   1350  1.43      ross 		bha_cmd(iot, ioh, name,
   1351  1.42    bouyer 		    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1352  1.42    bouyer 		    sizeof(devices.reply), (u_char *)&devices.reply);
   1353  1.17   thorpej 
   1354  1.42    bouyer 		for (i = 0; i < 8; i++) {
   1355  1.42    bouyer 			for (j = 0; j < 8; j++) {
   1356  1.42    bouyer 				if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1357  1.42    bouyer 					initial_ccbs++;
   1358  1.38    simonb 			}
   1359  1.38    simonb 		}
   1360  1.31   thorpej 	}
   1361  1.17   thorpej 
   1362   1.1   mycroft 	/*
   1363  1.42    bouyer 	 * Double the initial CCB count, for good measure.
   1364   1.1   mycroft 	 */
   1365  1.42    bouyer 	initial_ccbs *= 2;
   1366   1.1   mycroft 
   1367  1.31   thorpej 	/*
   1368  1.42    bouyer 	 * Sanity check the initial CCB count; don't create more than
   1369  1.42    bouyer 	 * we can enqueue (sc_max_ccbs), and make sure there are some
   1370  1.42    bouyer 	 * at all.
   1371  1.31   thorpej 	 */
   1372  1.42    bouyer 	if (initial_ccbs > sc->sc_max_ccbs)
   1373  1.42    bouyer 		initial_ccbs = sc->sc_max_ccbs;
   1374  1.42    bouyer 	if (initial_ccbs == 0)
   1375  1.42    bouyer 		initial_ccbs = 2;
   1376   1.1   mycroft 
   1377  1.42    bouyer 	return (initial_ccbs);
   1378  1.31   thorpej }
   1379   1.1   mycroft 
   1380  1.31   thorpej /*
   1381  1.42    bouyer  * bha_init:
   1382  1.42    bouyer  *
   1383  1.42    bouyer  *	Initialize the board.
   1384  1.31   thorpej  */
   1385  1.31   thorpej int
   1386  1.42    bouyer bha_init(sc)
   1387  1.31   thorpej 	struct bha_softc *sc;
   1388  1.31   thorpej {
   1389  1.43      ross 	char *name = sc->sc_dev.dv_xname;
   1390  1.42    bouyer 	struct bha_toggle toggle;
   1391  1.42    bouyer 	struct bha_mailbox mailbox;
   1392  1.42    bouyer 	struct bha_mbx_out *mbo;
   1393  1.42    bouyer 	struct bha_mbx_in *mbi;
   1394  1.42    bouyer 	int i;
   1395  1.42    bouyer 
   1396  1.42    bouyer 	/*
   1397  1.42    bouyer 	 * Set up the mailbox.  We always run the mailbox in round-robin.
   1398  1.42    bouyer 	 */
   1399  1.42    bouyer 	for (i = 0; i < sc->sc_mbox_count; i++) {
   1400  1.42    bouyer 		mbo = &sc->sc_mbo[i];
   1401  1.42    bouyer 		mbi = &sc->sc_mbi[i];
   1402  1.42    bouyer 
   1403  1.42    bouyer 		mbo->cmd = BHA_MBO_FREE;
   1404  1.42    bouyer 		BHA_MBO_SYNC(sc, mbo, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1405   1.1   mycroft 
   1406  1.42    bouyer 		mbi->comp_stat = BHA_MBI_FREE;
   1407  1.42    bouyer 		BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1408  1.31   thorpej 	}
   1409  1.31   thorpej 
   1410  1.42    bouyer 	sc->sc_cmbo = sc->sc_tmbo = &sc->sc_mbo[0];
   1411  1.42    bouyer 	sc->sc_tmbi = &sc->sc_mbi[0];
   1412  1.38    simonb 
   1413  1.42    bouyer 	sc->sc_mbofull = 0;
   1414   1.1   mycroft 
   1415  1.38    simonb 	/*
   1416  1.42    bouyer 	 * If the board supports strict round-robin, enable that.
   1417  1.38    simonb 	 */
   1418  1.42    bouyer 	if (sc->sc_flags & BHAF_STRICT_ROUND_ROBIN) {
   1419  1.42    bouyer 		toggle.cmd.opcode = BHA_ROUND_ROBIN;
   1420  1.42    bouyer 		toggle.cmd.enable = 1;
   1421  1.43      ross 		bha_cmd(sc->sc_iot, sc->sc_ioh, name,
   1422  1.42    bouyer 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
   1423  1.42    bouyer 		    0, NULL);
   1424   1.1   mycroft 	}
   1425   1.1   mycroft 
   1426   1.1   mycroft 	/*
   1427  1.42    bouyer 	 * Give the mailbox to the board.
   1428  1.42    bouyer 	 */
   1429  1.42    bouyer 	mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
   1430  1.42    bouyer 	mailbox.cmd.nmbx = sc->sc_mbox_count;
   1431  1.42    bouyer 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, mailbox.cmd.addr);
   1432  1.43      ross 	bha_cmd(sc->sc_iot, sc->sc_ioh, name,
   1433  1.42    bouyer 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
   1434  1.42    bouyer 	    0, (u_char *)0);
   1435  1.42    bouyer 
   1436  1.42    bouyer 	return (0);
   1437  1.42    bouyer }
   1438  1.42    bouyer 
   1439  1.42    bouyer /*****************************************************************************
   1440  1.42    bouyer  * CCB execution engine
   1441  1.42    bouyer  *****************************************************************************/
   1442  1.42    bouyer 
   1443  1.42    bouyer /*
   1444  1.42    bouyer  * bha_queue_ccb:
   1445  1.42    bouyer  *
   1446  1.42    bouyer  *	Queue a CCB to be sent to the controller, and send it if possible.
   1447  1.42    bouyer  */
   1448  1.42    bouyer void
   1449  1.42    bouyer bha_queue_ccb(sc, ccb)
   1450  1.42    bouyer 	struct bha_softc *sc;
   1451  1.42    bouyer 	struct bha_ccb *ccb;
   1452  1.42    bouyer {
   1453  1.42    bouyer 
   1454  1.42    bouyer 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
   1455  1.42    bouyer 	bha_start_ccbs(sc);
   1456  1.42    bouyer }
   1457  1.42    bouyer 
   1458  1.42    bouyer /*
   1459  1.42    bouyer  * bha_start_ccbs:
   1460  1.42    bouyer  *
   1461  1.42    bouyer  *	Send as many CCBs as we have empty mailboxes for.
   1462  1.42    bouyer  */
   1463  1.42    bouyer void
   1464  1.42    bouyer bha_start_ccbs(sc)
   1465  1.42    bouyer 	struct bha_softc *sc;
   1466  1.42    bouyer {
   1467  1.42    bouyer 	bus_space_tag_t iot = sc->sc_iot;
   1468  1.42    bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
   1469  1.42    bouyer 	struct bha_ccb_group *bcg;
   1470  1.42    bouyer 	struct bha_mbx_out *mbo;
   1471  1.42    bouyer 	struct bha_ccb *ccb;
   1472  1.42    bouyer 
   1473  1.42    bouyer 	mbo = sc->sc_tmbo;
   1474  1.42    bouyer 
   1475  1.42    bouyer 	while ((ccb = TAILQ_FIRST(&sc->sc_waiting_ccb)) != NULL) {
   1476  1.42    bouyer 		if (sc->sc_mbofull >= sc->sc_mbox_count) {
   1477  1.42    bouyer #ifdef DIAGNOSTIC
   1478  1.42    bouyer 			if (sc->sc_mbofull > sc->sc_mbox_count)
   1479  1.42    bouyer 				panic("bha_start_ccbs: mbofull > mbox_count");
   1480  1.42    bouyer #endif
   1481  1.42    bouyer 			/*
   1482  1.42    bouyer 			 * No mailboxes available; attempt to collect ones
   1483  1.42    bouyer 			 * that have already been used.
   1484  1.42    bouyer 			 */
   1485  1.42    bouyer 			bha_collect_mbo(sc);
   1486  1.42    bouyer 			if (sc->sc_mbofull == sc->sc_mbox_count) {
   1487  1.42    bouyer 				/*
   1488  1.42    bouyer 				 * Still no more available; have the
   1489  1.42    bouyer 				 * controller interrupt us when it
   1490  1.42    bouyer 				 * frees one.
   1491  1.42    bouyer 				 */
   1492  1.42    bouyer 				struct bha_toggle toggle;
   1493  1.42    bouyer 
   1494  1.42    bouyer 				toggle.cmd.opcode = BHA_MBO_INTR_EN;
   1495  1.42    bouyer 				toggle.cmd.enable = 1;
   1496  1.43      ross 				bha_cmd(iot, ioh, sc->sc_dev.dv_xname,
   1497  1.42    bouyer 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
   1498  1.42    bouyer 				    0, (u_char *)0);
   1499  1.42    bouyer 				break;
   1500  1.42    bouyer 			}
   1501  1.42    bouyer 		}
   1502  1.42    bouyer 
   1503  1.42    bouyer 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
   1504  1.42    bouyer #ifdef BHADIAG
   1505  1.42    bouyer 		ccb->flags |= CCB_SENDING;
   1506  1.42    bouyer #endif
   1507  1.42    bouyer 
   1508  1.42    bouyer 		/*
   1509  1.42    bouyer 		 * Put the CCB in the mailbox.
   1510  1.42    bouyer 		 */
   1511  1.42    bouyer 		bcg = BHA_CCB_GROUP(ccb);
   1512  1.42    bouyer 		ltophys(bcg->bcg_dmamap->dm_segs[0].ds_addr +
   1513  1.42    bouyer 		    BHA_CCB_OFFSET(ccb), mbo->ccb_addr);
   1514  1.42    bouyer 		if (ccb->flags & CCB_ABORT)
   1515  1.42    bouyer 			mbo->cmd = BHA_MBO_ABORT;
   1516  1.42    bouyer 		else
   1517  1.42    bouyer 			mbo->cmd = BHA_MBO_START;
   1518  1.42    bouyer 
   1519  1.42    bouyer 		BHA_MBO_SYNC(sc, mbo,
   1520  1.42    bouyer 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1521  1.42    bouyer 
   1522  1.42    bouyer 		/* Tell the card to poll immediately. */
   1523  1.42    bouyer 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
   1524  1.42    bouyer 
   1525  1.42    bouyer 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
   1526  1.42    bouyer 			callout_reset(&ccb->xs->xs_callout,
   1527  1.49    bouyer 			    mstohz(ccb->timeout), bha_timeout, ccb);
   1528  1.42    bouyer 
   1529  1.42    bouyer 		++sc->sc_mbofull;
   1530  1.42    bouyer 		mbo = bha_nextmbo(sc, mbo);
   1531  1.42    bouyer 	}
   1532  1.42    bouyer 
   1533  1.42    bouyer 	sc->sc_tmbo = mbo;
   1534  1.42    bouyer }
   1535  1.42    bouyer 
   1536  1.42    bouyer /*
   1537  1.42    bouyer  * bha_finish_ccbs:
   1538  1.42    bouyer  *
   1539  1.42    bouyer  *	Finalize the execution of CCBs in our incoming mailbox.
   1540  1.42    bouyer  */
   1541  1.42    bouyer void
   1542  1.42    bouyer bha_finish_ccbs(sc)
   1543  1.42    bouyer 	struct bha_softc *sc;
   1544  1.42    bouyer {
   1545  1.42    bouyer 	struct bha_mbx_in *mbi;
   1546  1.42    bouyer 	struct bha_ccb *ccb;
   1547  1.42    bouyer 	int i;
   1548  1.42    bouyer 
   1549  1.42    bouyer 	mbi = sc->sc_tmbi;
   1550  1.42    bouyer 
   1551  1.42    bouyer 	BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1552  1.42    bouyer 
   1553  1.42    bouyer 	if (mbi->comp_stat == BHA_MBI_FREE) {
   1554  1.42    bouyer 		for (i = 0; i < sc->sc_mbox_count; i++) {
   1555  1.42    bouyer 			if (mbi->comp_stat != BHA_MBI_FREE) {
   1556  1.42    bouyer #ifdef BHADIAG
   1557  1.42    bouyer 				/*
   1558  1.42    bouyer 				 * This can happen in normal operation if
   1559  1.42    bouyer 				 * we use all mailbox slots.
   1560  1.42    bouyer 				 */
   1561  1.42    bouyer 				printf("%s: mbi not in round-robin order\n",
   1562  1.42    bouyer 				    sc->sc_dev.dv_xname);
   1563  1.42    bouyer #endif
   1564  1.42    bouyer 				goto again;
   1565  1.42    bouyer 			}
   1566  1.42    bouyer 			mbi = bha_nextmbi(sc, mbi);
   1567  1.42    bouyer 			BHA_MBI_SYNC(sc, mbi,
   1568  1.42    bouyer 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1569  1.42    bouyer 		}
   1570  1.42    bouyer #ifdef BHADIAGnot
   1571  1.42    bouyer 		printf("%s: mbi interrupt with no full mailboxes\n",
   1572  1.42    bouyer 		    sc->sc_dev.dv_xname);
   1573  1.42    bouyer #endif
   1574  1.42    bouyer 		return;
   1575  1.42    bouyer 	}
   1576  1.42    bouyer 
   1577  1.42    bouyer  again:
   1578  1.42    bouyer 	do {
   1579  1.42    bouyer 		ccb = bha_ccb_phys_kv(sc, phystol(mbi->ccb_addr));
   1580  1.42    bouyer 		if (ccb == NULL) {
   1581  1.42    bouyer 			printf("%s: bad mbi ccb pointer 0x%08x; skipping\n",
   1582  1.42    bouyer 			    sc->sc_dev.dv_xname, phystol(mbi->ccb_addr));
   1583  1.42    bouyer 			goto next;
   1584  1.42    bouyer 		}
   1585  1.42    bouyer 
   1586  1.42    bouyer 		BHA_CCB_SYNC(sc, ccb,
   1587  1.42    bouyer 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1588  1.42    bouyer 
   1589  1.42    bouyer #ifdef BHADEBUG
   1590  1.42    bouyer 		if (bha_debug) {
   1591  1.42    bouyer 			struct scsi_generic *cmd = &ccb->scsi_cmd;
   1592  1.42    bouyer 			printf("op=%x %x %x %x %x %x\n",
   1593  1.42    bouyer 			    cmd->opcode, cmd->bytes[0], cmd->bytes[1],
   1594  1.42    bouyer 			    cmd->bytes[2], cmd->bytes[3], cmd->bytes[4]);
   1595  1.42    bouyer 			printf("comp_stat %x for mbi addr = 0x%p, ",
   1596  1.42    bouyer 			    mbi->comp_stat, mbi);
   1597  1.42    bouyer 			printf("ccb addr = %p\n", ccb);
   1598  1.42    bouyer 		}
   1599  1.42    bouyer #endif /* BHADEBUG */
   1600  1.42    bouyer 
   1601  1.42    bouyer 		switch (mbi->comp_stat) {
   1602  1.42    bouyer 		case BHA_MBI_OK:
   1603  1.42    bouyer 		case BHA_MBI_ERROR:
   1604  1.42    bouyer 			if ((ccb->flags & CCB_ABORT) != 0) {
   1605  1.42    bouyer 				/*
   1606  1.42    bouyer 				 * If we already started an abort, wait for it
   1607  1.42    bouyer 				 * to complete before clearing the CCB.  We
   1608  1.42    bouyer 				 * could instead just clear CCB_SENDING, but
   1609  1.42    bouyer 				 * what if the mailbox was already received?
   1610  1.42    bouyer 				 * The worst that happens here is that we clear
   1611  1.42    bouyer 				 * the CCB a bit later than we need to.  BFD.
   1612  1.42    bouyer 				 */
   1613  1.42    bouyer 				goto next;
   1614  1.42    bouyer 			}
   1615  1.42    bouyer 			break;
   1616  1.42    bouyer 
   1617  1.42    bouyer 		case BHA_MBI_ABORT:
   1618  1.42    bouyer 		case BHA_MBI_UNKNOWN:
   1619  1.42    bouyer 			/*
   1620  1.42    bouyer 			 * Even if the CCB wasn't found, we clear it anyway.
   1621  1.46       wiz 			 * See preceding comment.
   1622  1.42    bouyer 			 */
   1623  1.42    bouyer 			break;
   1624  1.42    bouyer 
   1625  1.42    bouyer 		default:
   1626  1.42    bouyer 			printf("%s: bad mbi comp_stat %02x; skipping\n",
   1627  1.42    bouyer 			    sc->sc_dev.dv_xname, mbi->comp_stat);
   1628  1.42    bouyer 			goto next;
   1629  1.42    bouyer 		}
   1630  1.42    bouyer 
   1631  1.42    bouyer 		callout_stop(&ccb->xs->xs_callout);
   1632  1.42    bouyer 		bha_done(sc, ccb);
   1633  1.42    bouyer 
   1634  1.42    bouyer 	next:
   1635  1.42    bouyer 		mbi->comp_stat = BHA_MBI_FREE;
   1636  1.42    bouyer 		BHA_CCB_SYNC(sc, ccb,
   1637  1.42    bouyer 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1638  1.42    bouyer 
   1639  1.42    bouyer 		mbi = bha_nextmbi(sc, mbi);
   1640  1.42    bouyer 		BHA_MBI_SYNC(sc, mbi,
   1641  1.42    bouyer 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1642  1.42    bouyer 	} while (mbi->comp_stat != BHA_MBI_FREE);
   1643  1.42    bouyer 
   1644  1.42    bouyer 	sc->sc_tmbi = mbi;
   1645  1.42    bouyer }
   1646  1.42    bouyer 
   1647  1.42    bouyer /*****************************************************************************
   1648  1.42    bouyer  * Mailbox management functions.
   1649  1.42    bouyer  *****************************************************************************/
   1650  1.42    bouyer 
   1651  1.42    bouyer /*
   1652  1.42    bouyer  * bha_create_mailbox:
   1653  1.42    bouyer  *
   1654  1.42    bouyer  *	Create the mailbox structures.  Helper function for bha_attach().
   1655  1.42    bouyer  *
   1656  1.42    bouyer  *	NOTE: The Buslogic hardware only gets one DMA address for the
   1657  1.42    bouyer  *	mailbox!  It expects:
   1658  1.42    bouyer  *
   1659  1.42    bouyer  *		mailbox_out[mailbox_size]
   1660  1.42    bouyer  *		mailbox_in[mailbox_size]
   1661  1.42    bouyer  */
   1662  1.42    bouyer int
   1663  1.42    bouyer bha_create_mailbox(sc)
   1664  1.42    bouyer 	struct bha_softc *sc;
   1665  1.42    bouyer {
   1666  1.42    bouyer 	bus_dma_segment_t seg;
   1667  1.42    bouyer 	size_t size;
   1668  1.42    bouyer 	int error, rseg;
   1669  1.42    bouyer 
   1670  1.42    bouyer 	size = (sizeof(struct bha_mbx_out) * sc->sc_mbox_count) +
   1671  1.42    bouyer 	       (sizeof(struct bha_mbx_in)  * sc->sc_mbox_count);
   1672  1.42    bouyer 
   1673  1.42    bouyer 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
   1674  1.42    bouyer 	    1, &rseg, sc->sc_dmaflags);
   1675  1.42    bouyer 	if (error) {
   1676  1.42    bouyer 		printf("%s: unable to allocate mailboxes, error = %d\n",
   1677  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1678  1.42    bouyer 		goto bad_0;
   1679  1.42    bouyer 	}
   1680  1.42    bouyer 
   1681  1.42    bouyer 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
   1682  1.42    bouyer 	    (caddr_t *)&sc->sc_mbo, sc->sc_dmaflags | BUS_DMA_COHERENT);
   1683  1.42    bouyer 	if (error) {
   1684  1.42    bouyer 		printf("%s: unable to map mailboxes, error = %d\n",
   1685  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1686  1.42    bouyer 		goto bad_1;
   1687  1.42    bouyer 	}
   1688  1.42    bouyer 
   1689  1.42    bouyer 	memset(sc->sc_mbo, 0, size);
   1690  1.42    bouyer 
   1691  1.42    bouyer 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   1692  1.42    bouyer 	    sc->sc_dmaflags, &sc->sc_dmamap_mbox);
   1693  1.42    bouyer 	if (error) {
   1694  1.42    bouyer 		printf("%s: unable to create mailbox DMA map, error = %d\n",
   1695  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1696  1.42    bouyer 		goto bad_2;
   1697  1.42    bouyer 	}
   1698  1.42    bouyer 
   1699  1.42    bouyer 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox,
   1700  1.42    bouyer 	    sc->sc_mbo, size, NULL, 0);
   1701  1.42    bouyer 	if (error) {
   1702  1.42    bouyer 		printf("%s: unable to load mailbox DMA map, error = %d\n",
   1703  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1704  1.42    bouyer 		goto bad_3;
   1705  1.42    bouyer 	}
   1706  1.42    bouyer 
   1707  1.42    bouyer 	sc->sc_mbi = (struct bha_mbx_in *)(sc->sc_mbo + sc->sc_mbox_count);
   1708  1.42    bouyer 
   1709  1.42    bouyer 	return (0);
   1710  1.42    bouyer 
   1711  1.42    bouyer  bad_3:
   1712  1.42    bouyer 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_mbox);
   1713  1.42    bouyer  bad_2:
   1714  1.42    bouyer 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_mbo, size);
   1715  1.42    bouyer  bad_1:
   1716  1.42    bouyer 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
   1717  1.42    bouyer  bad_0:
   1718  1.42    bouyer 	return (error);
   1719  1.42    bouyer }
   1720  1.42    bouyer 
   1721  1.42    bouyer /*
   1722  1.42    bouyer  * bha_collect_mbo:
   1723  1.42    bouyer  *
   1724  1.42    bouyer  *	Garbage collect mailboxes that are no longer in use.
   1725  1.42    bouyer  */
   1726  1.42    bouyer void
   1727  1.42    bouyer bha_collect_mbo(sc)
   1728  1.42    bouyer 	struct bha_softc *sc;
   1729  1.42    bouyer {
   1730  1.42    bouyer 	struct bha_mbx_out *mbo;
   1731  1.42    bouyer #ifdef BHADIAG
   1732  1.42    bouyer 	struct bha_ccb *ccb;
   1733  1.42    bouyer #endif
   1734  1.42    bouyer 
   1735  1.42    bouyer 	mbo = sc->sc_cmbo;
   1736  1.42    bouyer 
   1737  1.42    bouyer 	while (sc->sc_mbofull > 0) {
   1738  1.42    bouyer 		BHA_MBO_SYNC(sc, mbo,
   1739  1.42    bouyer 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1740  1.42    bouyer 		if (mbo->cmd != BHA_MBO_FREE)
   1741  1.42    bouyer 			break;
   1742  1.42    bouyer 
   1743  1.42    bouyer #ifdef BHADIAG
   1744  1.42    bouyer 		ccb = bha_ccb_phys_kv(sc, phystol(mbo->ccb_addr));
   1745  1.42    bouyer 		ccb->flags &= ~CCB_SENDING;
   1746  1.42    bouyer #endif
   1747  1.42    bouyer 
   1748  1.42    bouyer 		--sc->sc_mbofull;
   1749  1.42    bouyer 		mbo = bha_nextmbo(sc, mbo);
   1750  1.42    bouyer 	}
   1751  1.42    bouyer 
   1752  1.42    bouyer 	sc->sc_cmbo = mbo;
   1753  1.42    bouyer }
   1754  1.42    bouyer 
   1755  1.42    bouyer /*****************************************************************************
   1756  1.42    bouyer  * CCB management functions
   1757  1.42    bouyer  *****************************************************************************/
   1758  1.42    bouyer 
   1759  1.42    bouyer __inline void bha_reset_ccb __P((struct bha_ccb *));
   1760  1.42    bouyer 
   1761  1.42    bouyer __inline void
   1762  1.42    bouyer bha_reset_ccb(ccb)
   1763  1.42    bouyer 	struct bha_ccb *ccb;
   1764  1.42    bouyer {
   1765  1.42    bouyer 
   1766  1.42    bouyer 	ccb->flags = 0;
   1767  1.42    bouyer }
   1768  1.42    bouyer 
   1769  1.42    bouyer /*
   1770  1.42    bouyer  * bha_create_ccbs:
   1771  1.42    bouyer  *
   1772  1.42    bouyer  *	Create a set of CCBs.
   1773  1.42    bouyer  *
   1774  1.42    bouyer  *	We determine the target CCB count, and then keep creating them
   1775  1.42    bouyer  *	until we reach the target, or fail.  CCBs that are allocated
   1776  1.42    bouyer  *	but not "created" are left on the allocating list.
   1777  1.42    bouyer  */
   1778  1.42    bouyer void
   1779  1.42    bouyer bha_create_ccbs(sc, count)
   1780  1.42    bouyer 	struct bha_softc *sc;
   1781  1.42    bouyer 	int count;
   1782  1.42    bouyer {
   1783  1.42    bouyer 	struct bha_ccb_group *bcg;
   1784  1.42    bouyer 	struct bha_ccb *ccb;
   1785  1.42    bouyer 	bus_dma_segment_t seg;
   1786  1.42    bouyer 	bus_dmamap_t ccbmap;
   1787  1.42    bouyer 	int target, i, error, rseg;
   1788  1.42    bouyer 
   1789  1.42    bouyer 	/*
   1790  1.42    bouyer 	 * If the current CCB count is already the max number we're
   1791  1.42    bouyer 	 * allowed to have, bail out now.
   1792  1.42    bouyer 	 */
   1793  1.42    bouyer 	if (sc->sc_cur_ccbs == sc->sc_max_ccbs)
   1794  1.42    bouyer 		return;
   1795  1.42    bouyer 
   1796  1.42    bouyer 	/*
   1797  1.42    bouyer 	 * Compute our target count, and clamp it down to the max
   1798  1.42    bouyer 	 * number we're allowed to have.
   1799  1.42    bouyer 	 */
   1800  1.42    bouyer 	target = sc->sc_cur_ccbs + count;
   1801  1.42    bouyer 	if (target > sc->sc_max_ccbs)
   1802  1.42    bouyer 		target = sc->sc_max_ccbs;
   1803  1.42    bouyer 
   1804  1.42    bouyer 	/*
   1805  1.42    bouyer 	 * If there are CCBs on the allocating list, don't allocate a
   1806  1.42    bouyer 	 * CCB group yet.
   1807  1.42    bouyer 	 */
   1808  1.42    bouyer 	if (TAILQ_FIRST(&sc->sc_allocating_ccbs) != NULL)
   1809  1.42    bouyer 		goto have_allocating_ccbs;
   1810  1.42    bouyer 
   1811  1.42    bouyer  allocate_group:
   1812  1.42    bouyer 	error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
   1813  1.42    bouyer 	    PAGE_SIZE, 0, &seg, 1, &rseg, sc->sc_dmaflags | BUS_DMA_NOWAIT);
   1814  1.42    bouyer 	if (error) {
   1815  1.42    bouyer 		printf("%s: unable to allocate CCB group, error = %d\n",
   1816  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1817  1.42    bouyer 		goto bad_0;
   1818  1.42    bouyer 	}
   1819  1.42    bouyer 
   1820  1.42    bouyer 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
   1821  1.50   thorpej 	    (void *)&bcg,
   1822  1.42    bouyer 	    sc->sc_dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
   1823  1.42    bouyer 	if (error) {
   1824  1.42    bouyer 		printf("%s: unable to map CCB group, error = %d\n",
   1825  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1826  1.42    bouyer 		goto bad_1;
   1827  1.42    bouyer 	}
   1828  1.42    bouyer 
   1829  1.42    bouyer 	memset(bcg, 0, PAGE_SIZE);
   1830  1.42    bouyer 
   1831  1.42    bouyer 	error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE,
   1832  1.42    bouyer 	    1, PAGE_SIZE, 0, sc->sc_dmaflags | BUS_DMA_NOWAIT, &ccbmap);
   1833  1.42    bouyer 	if (error) {
   1834  1.42    bouyer 		printf("%s: unable to create CCB group DMA map, error = %d\n",
   1835  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1836  1.42    bouyer 		goto bad_2;
   1837  1.42    bouyer 	}
   1838  1.42    bouyer 
   1839  1.42    bouyer 	error = bus_dmamap_load(sc->sc_dmat, ccbmap, bcg, PAGE_SIZE, NULL,
   1840  1.42    bouyer 	    sc->sc_dmaflags | BUS_DMA_NOWAIT);
   1841  1.42    bouyer 	if (error) {
   1842  1.42    bouyer 		printf("%s: unable to load CCB group DMA map, error = %d\n",
   1843  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1844  1.42    bouyer 		goto bad_3;
   1845  1.42    bouyer 	}
   1846  1.42    bouyer 
   1847  1.42    bouyer 	bcg->bcg_dmamap = ccbmap;
   1848  1.42    bouyer 
   1849  1.42    bouyer #ifdef DIAGNOSTIC
   1850  1.42    bouyer 	if (BHA_CCB_GROUP(&bcg->bcg_ccbs[0]) !=
   1851  1.42    bouyer 	    BHA_CCB_GROUP(&bcg->bcg_ccbs[bha_ccbs_per_group - 1]))
   1852  1.42    bouyer 		panic("bha_create_ccbs: CCB group size botch");
   1853  1.42    bouyer #endif
   1854  1.42    bouyer 
   1855  1.42    bouyer 	/*
   1856  1.42    bouyer 	 * Add all of the CCBs in this group to the allocating list.
   1857  1.42    bouyer 	 */
   1858  1.42    bouyer 	for (i = 0; i < bha_ccbs_per_group; i++) {
   1859  1.42    bouyer 		ccb = &bcg->bcg_ccbs[i];
   1860  1.42    bouyer 		TAILQ_INSERT_TAIL(&sc->sc_allocating_ccbs, ccb, chain);
   1861  1.42    bouyer 	}
   1862  1.42    bouyer 
   1863  1.42    bouyer  have_allocating_ccbs:
   1864  1.42    bouyer 	/*
   1865  1.42    bouyer 	 * Loop over the allocating list until we reach our CCB target.
   1866  1.42    bouyer 	 * If we run out on the list, we'll allocate another group's
   1867  1.42    bouyer 	 * worth.
   1868  1.42    bouyer 	 */
   1869  1.42    bouyer 	while (sc->sc_cur_ccbs < target) {
   1870  1.42    bouyer 		ccb = TAILQ_FIRST(&sc->sc_allocating_ccbs);
   1871  1.42    bouyer 		if (ccb == NULL)
   1872  1.42    bouyer 			goto allocate_group;
   1873  1.42    bouyer 		if (bha_init_ccb(sc, ccb) != 0) {
   1874  1.42    bouyer 			/*
   1875  1.42    bouyer 			 * We were unable to initialize the CCB.
   1876  1.42    bouyer 			 * This is likely due to a resource shortage,
   1877  1.42    bouyer 			 * so bail out now.
   1878  1.42    bouyer 			 */
   1879  1.42    bouyer 			return;
   1880  1.42    bouyer 		}
   1881  1.42    bouyer 	}
   1882  1.42    bouyer 
   1883  1.42    bouyer 	/*
   1884  1.42    bouyer 	 * If we got here, we've reached our target!
   1885  1.42    bouyer 	 */
   1886  1.42    bouyer 	return;
   1887  1.42    bouyer 
   1888  1.42    bouyer  bad_3:
   1889  1.42    bouyer 	bus_dmamap_destroy(sc->sc_dmat, ccbmap);
   1890  1.42    bouyer  bad_2:
   1891  1.42    bouyer 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)bcg, PAGE_SIZE);
   1892  1.42    bouyer  bad_1:
   1893  1.42    bouyer 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
   1894  1.42    bouyer  bad_0:
   1895  1.42    bouyer 	return;
   1896  1.42    bouyer }
   1897  1.42    bouyer 
   1898  1.42    bouyer /*
   1899  1.42    bouyer  * bha_init_ccb:
   1900  1.42    bouyer  *
   1901  1.42    bouyer  *	Initialize a CCB; helper function for bha_create_ccbs().
   1902  1.42    bouyer  */
   1903  1.42    bouyer int
   1904  1.42    bouyer bha_init_ccb(sc, ccb)
   1905  1.42    bouyer 	struct bha_softc *sc;
   1906  1.42    bouyer 	struct bha_ccb *ccb;
   1907  1.42    bouyer {
   1908  1.42    bouyer 	struct bha_ccb_group *bcg = BHA_CCB_GROUP(ccb);
   1909  1.42    bouyer 	int hashnum, error;
   1910  1.42    bouyer 
   1911  1.42    bouyer 	/*
   1912  1.42    bouyer 	 * Create the DMA map for this CCB.
   1913  1.42    bouyer 	 *
   1914  1.42    bouyer 	 * XXX ALLOCNOW is a hack to prevent bounce buffer shortages
   1915  1.42    bouyer 	 * XXX in the ISA case.  A better solution is needed.
   1916  1.42    bouyer 	 */
   1917  1.42    bouyer 	error = bus_dmamap_create(sc->sc_dmat, BHA_MAXXFER, BHA_NSEG,
   1918  1.42    bouyer 	    BHA_MAXXFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
   1919  1.42    bouyer 	    &ccb->dmamap_xfer);
   1920  1.42    bouyer 	if (error) {
   1921  1.42    bouyer 		printf("%s: unable to create CCB DMA map, error = %d\n",
   1922  1.42    bouyer 		    sc->sc_dev.dv_xname, error);
   1923  1.42    bouyer 		return (error);
   1924  1.42    bouyer 	}
   1925  1.42    bouyer 
   1926  1.42    bouyer 	TAILQ_REMOVE(&sc->sc_allocating_ccbs, ccb, chain);
   1927  1.42    bouyer 
   1928  1.42    bouyer 	/*
   1929  1.42    bouyer 	 * Put the CCB into the phystokv hash table.
   1930   1.1   mycroft 	 */
   1931  1.42    bouyer 	ccb->hashkey = bcg->bcg_dmamap->dm_segs[0].ds_addr +
   1932  1.42    bouyer 	    BHA_CCB_OFFSET(ccb);
   1933  1.42    bouyer 	hashnum = CCB_HASH(ccb->hashkey);
   1934  1.42    bouyer 	ccb->nexthash = sc->sc_ccbhash[hashnum];
   1935  1.42    bouyer 	sc->sc_ccbhash[hashnum] = ccb;
   1936  1.42    bouyer 	bha_reset_ccb(ccb);
   1937  1.42    bouyer 
   1938  1.42    bouyer 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
   1939  1.42    bouyer 	sc->sc_cur_ccbs++;
   1940  1.42    bouyer 
   1941  1.42    bouyer 	return (0);
   1942  1.42    bouyer }
   1943  1.42    bouyer 
   1944  1.42    bouyer /*
   1945  1.42    bouyer  * bha_get_ccb:
   1946  1.42    bouyer  *
   1947  1.42    bouyer  *	Get a CCB for the SCSI operation.  If there are none left,
   1948  1.42    bouyer  *	wait until one becomes available, if we can.
   1949  1.42    bouyer  */
   1950  1.42    bouyer struct bha_ccb *
   1951  1.42    bouyer bha_get_ccb(sc)
   1952  1.42    bouyer 	struct bha_softc *sc;
   1953  1.42    bouyer {
   1954  1.42    bouyer 	struct bha_ccb *ccb;
   1955  1.42    bouyer 	int s;
   1956  1.42    bouyer 
   1957  1.42    bouyer 	s = splbio();
   1958  1.42    bouyer 	ccb = TAILQ_FIRST(&sc->sc_free_ccb);
   1959  1.42    bouyer 	if (ccb != NULL) {
   1960  1.42    bouyer 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
   1961  1.42    bouyer 		ccb->flags |= CCB_ALLOC;
   1962  1.38    simonb 	}
   1963  1.42    bouyer 	splx(s);
   1964  1.42    bouyer 	return (ccb);
   1965  1.42    bouyer }
   1966   1.1   mycroft 
   1967  1.42    bouyer /*
   1968  1.42    bouyer  * bha_free_ccb:
   1969  1.42    bouyer  *
   1970  1.42    bouyer  *	Put a CCB back onto the free list.
   1971  1.42    bouyer  */
   1972  1.42    bouyer void
   1973  1.42    bouyer bha_free_ccb(sc, ccb)
   1974  1.42    bouyer 	struct bha_softc *sc;
   1975  1.42    bouyer 	struct bha_ccb *ccb;
   1976  1.42    bouyer {
   1977  1.42    bouyer 	int s;
   1978  1.42    bouyer 
   1979  1.42    bouyer 	s = splbio();
   1980  1.42    bouyer 	bha_reset_ccb(ccb);
   1981  1.42    bouyer 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
   1982   1.1   mycroft 	splx(s);
   1983  1.42    bouyer }
   1984  1.42    bouyer 
   1985  1.42    bouyer /*
   1986  1.42    bouyer  * bha_ccb_phys_kv:
   1987  1.42    bouyer  *
   1988  1.42    bouyer  *	Given a CCB DMA address, locate the CCB in kernel virtual space.
   1989  1.42    bouyer  */
   1990  1.42    bouyer struct bha_ccb *
   1991  1.42    bouyer bha_ccb_phys_kv(sc, ccb_phys)
   1992  1.42    bouyer 	struct bha_softc *sc;
   1993  1.42    bouyer 	bus_addr_t ccb_phys;
   1994  1.42    bouyer {
   1995  1.42    bouyer 	int hashnum = CCB_HASH(ccb_phys);
   1996  1.42    bouyer 	struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
   1997  1.42    bouyer 
   1998  1.42    bouyer 	while (ccb) {
   1999  1.42    bouyer 		if (ccb->hashkey == ccb_phys)
   2000  1.42    bouyer 			break;
   2001  1.42    bouyer 		ccb = ccb->nexthash;
   2002  1.42    bouyer 	}
   2003  1.42    bouyer 	return (ccb);
   2004   1.1   mycroft }
   2005