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