Home | History | Annotate | Line # | Download | only in ic
bha.c revision 1.29
      1 /*	$NetBSD: bha.c,v 1.29 1998/12/09 08:47:19 thorpej Exp $	*/
      2 
      3 #include "opt_ddb.h"
      4 #undef BHADIAG
      5 #ifdef DDB
      6 #define	integrate
      7 #else
      8 #define	integrate	static inline
      9 #endif
     10 
     11 /*-
     12  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     13  * All rights reserved.
     14  *
     15  * This code is derived from software contributed to The NetBSD Foundation
     16  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     17  * Simulation Facility, NASA Ames Research Center.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notice, this list of conditions and the following disclaimer.
     24  * 2. Redistributions in binary form must reproduce the above copyright
     25  *    notice, this list of conditions and the following disclaimer in the
     26  *    documentation and/or other materials provided with the distribution.
     27  * 3. All advertising materials mentioning features or use of this software
     28  *    must display the following acknowledgement:
     29  *	This product includes software developed by the NetBSD
     30  *	Foundation, Inc. and its contributors.
     31  * 4. Neither the name of The NetBSD Foundation nor the names of its
     32  *    contributors may be used to endorse or promote products derived
     33  *    from this software without specific prior written permission.
     34  *
     35  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     37  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     40  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     41  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     43  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     45  * POSSIBILITY OF SUCH DAMAGE.
     46  */
     47 
     48 /*
     49  * Originally written by Julian Elischer (julian (at) tfs.com)
     50  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     51  *
     52  * TRW Financial Systems, in accordance with their agreement with Carnegie
     53  * Mellon University, makes this software available to CMU to distribute
     54  * or use in any manner that they see fit as long as this message is kept with
     55  * the software. For this reason TFS also grants any other persons or
     56  * organisations permission to use or modify this software.
     57  *
     58  * TFS supplies this software to be publicly redistributed
     59  * on the understanding that TFS is not responsible for the correct
     60  * functioning of this software in any circumstances.
     61  */
     62 
     63 #include <sys/types.h>
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/errno.h>
     68 #include <sys/ioctl.h>
     69 #include <sys/device.h>
     70 #include <sys/malloc.h>
     71 #include <sys/buf.h>
     72 #include <sys/proc.h>
     73 #include <sys/user.h>
     74 
     75 #include <machine/bus.h>
     76 #include <machine/intr.h>
     77 
     78 #include <dev/scsipi/scsi_all.h>
     79 #include <dev/scsipi/scsipi_all.h>
     80 #include <dev/scsipi/scsiconf.h>
     81 
     82 #include <dev/ic/bhareg.h>
     83 #include <dev/ic/bhavar.h>
     84 
     85 #ifndef DDB
     86 #define Debugger() panic("should call debugger here (bha.c)")
     87 #endif /* ! DDB */
     88 
     89 #define	BHA_MAXXFER	((BHA_NSEG - 1) << PGSHIFT)
     90 
     91 #ifdef BHADEBUG
     92 int     bha_debug = 0;
     93 #endif /* BHADEBUG */
     94 
     95 int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
     96 		 int, u_char *, int, u_char *));
     97 integrate void bha_finish_ccbs __P((struct bha_softc *));
     98 integrate void bha_reset_ccb __P((struct bha_softc *, struct bha_ccb *));
     99 void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
    100 integrate int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
    101 struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
    102 struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, u_long));
    103 void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
    104 void bha_collect_mbo __P((struct bha_softc *));
    105 void bha_start_ccbs __P((struct bha_softc *));
    106 void bha_done __P((struct bha_softc *, struct bha_ccb *));
    107 int bha_init __P((struct bha_softc *));
    108 void bhaminphys __P((struct buf *));
    109 int bha_scsi_cmd __P((struct scsipi_xfer *));
    110 int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
    111 void bha_timeout __P((void *arg));
    112 int bha_create_ccbs __P((struct bha_softc *, struct bha_ccb *, int));
    113 
    114 /* the below structure is so we have a default dev struct for out link struct */
    115 struct scsipi_device bha_dev = {
    116 	NULL,			/* Use default error handler */
    117 	NULL,			/* have a queue, served by this */
    118 	NULL,			/* have no async handler */
    119 	NULL,			/* Use default 'done' routine */
    120 };
    121 
    122 #define BHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
    123 #define	BHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    124 
    125 /*
    126  * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    127  *
    128  * Activate Adapter command
    129  *    icnt:   number of args (outbound bytes including opcode)
    130  *    ibuf:   argument buffer
    131  *    ocnt:   number of expected returned bytes
    132  *    obuf:   result buffer
    133  *    wait:   number of seconds to wait for response
    134  *
    135  * Performs an adapter command through the ports.  Not to be confused with a
    136  * scsi command, which is read in via the dma; one of the adapter commands
    137  * tells it to read in a scsi command.
    138  */
    139 int
    140 bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    141 	bus_space_tag_t iot;
    142 	bus_space_handle_t ioh;
    143 	struct bha_softc *sc;
    144 	int icnt, ocnt;
    145 	u_char *ibuf, *obuf;
    146 {
    147 	const char *name;
    148 	register int i;
    149 	int wait;
    150 	u_char sts;
    151 	u_char opcode = ibuf[0];
    152 
    153 	if (sc != NULL)
    154 		name = sc->sc_dev.dv_xname;
    155 	else
    156 		name = "(bha probe)";
    157 
    158 	/*
    159 	 * Calculate a reasonable timeout for the command.
    160 	 */
    161 	switch (opcode) {
    162 	case BHA_INQUIRE_DEVICES:
    163 	case BHA_INQUIRE_DEVICES_2:
    164 		wait = 90 * 20000;
    165 		break;
    166 	default:
    167 		wait = 1 * 20000;
    168 		break;
    169 	}
    170 
    171 	/*
    172 	 * Wait for the adapter to go idle, unless it's one of
    173 	 * the commands which don't need this
    174 	 */
    175 	if (opcode != BHA_MBO_INTR_EN) {
    176 		for (i = 20000; i; i--) {	/* 1 sec? */
    177 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    178 			if (sts & BHA_STAT_IDLE)
    179 				break;
    180 			delay(50);
    181 		}
    182 		if (!i) {
    183 			printf("%s: bha_cmd, host not idle(0x%x)\n",
    184 			    name, sts);
    185 			return (1);
    186 		}
    187 	}
    188 	/*
    189 	 * Now that it is idle, if we expect output, preflush the
    190 	 * queue feeding to us.
    191 	 */
    192 	if (ocnt) {
    193 		while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
    194 		    BHA_STAT_DF)
    195 			bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    196 	}
    197 	/*
    198 	 * Output the command and the number of arguments given
    199 	 * for each byte, first check the port is empty.
    200 	 */
    201 	while (icnt--) {
    202 		for (i = wait; i; i--) {
    203 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    204 			if (!(sts & BHA_STAT_CDF))
    205 				break;
    206 			delay(50);
    207 		}
    208 		if (!i) {
    209 			if (opcode != BHA_INQUIRE_REVISION)
    210 				printf("%s: bha_cmd, cmd/data port full\n",
    211 				    name);
    212 			goto bad;
    213 		}
    214 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
    215 	}
    216 	/*
    217 	 * If we expect input, loop that many times, each time,
    218 	 * looking for the data register to have valid data
    219 	 */
    220 	while (ocnt--) {
    221 		for (i = wait; i; i--) {
    222 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    223 			if (sts & BHA_STAT_DF)
    224 				break;
    225 			delay(50);
    226 		}
    227 		if (!i) {
    228 			if (opcode != BHA_INQUIRE_REVISION)
    229 				printf("%s: bha_cmd, cmd/data port empty %d\n",
    230 				    name, ocnt);
    231 			goto bad;
    232 		}
    233 		*obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    234 	}
    235 	/*
    236 	 * Wait for the board to report a finished instruction.
    237 	 * We may get an extra interrupt for the HACC signal, but this is
    238 	 * unimportant.
    239 	 */
    240 	if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
    241 		for (i = 20000; i; i--) {	/* 1 sec? */
    242 			sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    243 			/* XXX Need to save this in the interrupt handler? */
    244 			if (sts & BHA_INTR_HACC)
    245 				break;
    246 			delay(50);
    247 		}
    248 		if (!i) {
    249 			printf("%s: bha_cmd, host not finished(0x%x)\n",
    250 			    name, sts);
    251 			return (1);
    252 		}
    253 	}
    254 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    255 	return (0);
    256 
    257 bad:
    258 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
    259 	return (1);
    260 }
    261 
    262 /*
    263  * Attach all the sub-devices we can find
    264  */
    265 void
    266 bha_attach(sc, bpd)
    267 	struct bha_softc *sc;
    268 	struct bha_probe_data *bpd;
    269 {
    270 
    271 	/*
    272 	 * Fill in the adapter.
    273 	 */
    274 	sc->sc_adapter.scsipi_cmd = bha_scsi_cmd;
    275 	sc->sc_adapter.scsipi_minphys = bhaminphys;
    276 
    277 	/*
    278 	 * fill in the prototype scsipi_link.
    279 	 */
    280 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    281 	sc->sc_link.adapter_softc = sc;
    282 	sc->sc_link.scsipi_scsi.adapter_target = bpd->sc_scsi_dev;
    283 	sc->sc_link.adapter = &sc->sc_adapter;
    284 	sc->sc_link.device = &bha_dev;
    285 	sc->sc_link.openings = 4;
    286 	sc->sc_link.scsipi_scsi.max_target = bpd->sc_iswide ? 15 : 7;
    287 	sc->sc_link.scsipi_scsi.max_lun = 7;
    288 	sc->sc_link.type = BUS_SCSI;
    289 
    290 	TAILQ_INIT(&sc->sc_free_ccb);
    291 	TAILQ_INIT(&sc->sc_waiting_ccb);
    292 	TAILQ_INIT(&sc->sc_queue);
    293 
    294 	bha_inquire_setup_information(sc);
    295 
    296 	printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
    297 	    sc->sc_model, sc->sc_firmware);
    298 
    299 	if (bha_init(sc) != 0) {
    300 		/* Error during initialization! */
    301 		return;
    302 	}
    303 
    304 	/*
    305 	 * ask the adapter what subunits are present
    306 	 */
    307 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    308 }
    309 
    310 integrate void
    311 bha_finish_ccbs(sc)
    312 	struct bha_softc *sc;
    313 {
    314 	struct bha_mbx_in *wmbi;
    315 	struct bha_ccb *ccb;
    316 	int i;
    317 
    318 	wmbi = wmbx->tmbi;
    319 
    320 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    321 	    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    322 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    323 
    324 	if (wmbi->stat == BHA_MBI_FREE) {
    325 		for (i = 0; i < BHA_MBX_SIZE; i++) {
    326 			if (wmbi->stat != BHA_MBI_FREE) {
    327 				printf("%s: mbi not in round-robin order\n",
    328 				    sc->sc_dev.dv_xname);
    329 				goto AGAIN;
    330 			}
    331 			bha_nextmbx(wmbi, wmbx, mbi);
    332 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    333 			    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    334 			    BUS_DMASYNC_POSTREAD);
    335 		}
    336 #ifdef BHADIAGnot
    337 		printf("%s: mbi interrupt with no full mailboxes\n",
    338 		    sc->sc_dev.dv_xname);
    339 #endif
    340 		return;
    341 	}
    342 
    343 AGAIN:
    344 	do {
    345 		ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
    346 		if (!ccb) {
    347 			printf("%s: bad mbi ccb pointer; skipping\n",
    348 			    sc->sc_dev.dv_xname);
    349 			goto next;
    350 		}
    351 
    352 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    353 		    BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
    354 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    355 
    356 #ifdef BHADEBUG
    357 		if (bha_debug) {
    358 			u_char *cp = &ccb->scsi_cmd;
    359 			printf("op=%x %x %x %x %x %x\n",
    360 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    361 			printf("stat %x for mbi addr = 0x%08x, ",
    362 			    wmbi->stat, wmbi);
    363 			printf("ccb addr = 0x%x\n", ccb);
    364 		}
    365 #endif /* BHADEBUG */
    366 
    367 		switch (wmbi->stat) {
    368 		case BHA_MBI_OK:
    369 		case BHA_MBI_ERROR:
    370 			if ((ccb->flags & CCB_ABORT) != 0) {
    371 				/*
    372 				 * If we already started an abort, wait for it
    373 				 * to complete before clearing the CCB.  We
    374 				 * could instead just clear CCB_SENDING, but
    375 				 * what if the mailbox was already received?
    376 				 * The worst that happens here is that we clear
    377 				 * the CCB a bit later than we need to.  BFD.
    378 				 */
    379 				goto next;
    380 			}
    381 			break;
    382 
    383 		case BHA_MBI_ABORT:
    384 		case BHA_MBI_UNKNOWN:
    385 			/*
    386 			 * Even if the CCB wasn't found, we clear it anyway.
    387 			 * See preceeding comment.
    388 			 */
    389 			break;
    390 
    391 		default:
    392 			printf("%s: bad mbi status %02x; skipping\n",
    393 			    sc->sc_dev.dv_xname, wmbi->stat);
    394 			goto next;
    395 		}
    396 
    397 		untimeout(bha_timeout, ccb);
    398 		bha_done(sc, ccb);
    399 
    400 	next:
    401 		wmbi->stat = BHA_MBI_FREE;
    402 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    403 		    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    404 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    405 		bha_nextmbx(wmbi, wmbx, mbi);
    406 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    407 		    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    408 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    409 	} while (wmbi->stat != BHA_MBI_FREE);
    410 
    411 	wmbx->tmbi = wmbi;
    412 }
    413 
    414 /*
    415  * Catch an interrupt from the adaptor
    416  */
    417 int
    418 bha_intr(arg)
    419 	void *arg;
    420 {
    421 	struct bha_softc *sc = arg;
    422 	bus_space_tag_t iot = sc->sc_iot;
    423 	bus_space_handle_t ioh = sc->sc_ioh;
    424 	u_char sts;
    425 
    426 #ifdef BHADEBUG
    427 	printf("%s: bha_intr ", sc->sc_dev.dv_xname);
    428 #endif /* BHADEBUG */
    429 
    430 	/*
    431 	 * First acknowlege the interrupt, Then if it's not telling about
    432 	 * a completed operation just return.
    433 	 */
    434 	sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    435 	if ((sts & BHA_INTR_ANYINTR) == 0)
    436 		return (0);
    437 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    438 
    439 #ifdef BHADIAG
    440 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
    441 	bha_collect_mbo(sc);
    442 #endif
    443 
    444 	/* Mail box out empty? */
    445 	if (sts & BHA_INTR_MBOA) {
    446 		struct bha_toggle toggle;
    447 
    448 		toggle.cmd.opcode = BHA_MBO_INTR_EN;
    449 		toggle.cmd.enable = 0;
    450 		bha_cmd(iot, ioh, sc,
    451 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    452 		    0, (u_char *)0);
    453 		bha_start_ccbs(sc);
    454 	}
    455 
    456 	/* Mail box in full? */
    457 	if (sts & BHA_INTR_MBIF)
    458 		bha_finish_ccbs(sc);
    459 
    460 	return (1);
    461 }
    462 
    463 integrate void
    464 bha_reset_ccb(sc, ccb)
    465 	struct bha_softc *sc;
    466 	struct bha_ccb *ccb;
    467 {
    468 
    469 	ccb->flags = 0;
    470 }
    471 
    472 /*
    473  * A ccb is put onto the free list.
    474  */
    475 void
    476 bha_free_ccb(sc, ccb)
    477 	struct bha_softc *sc;
    478 	struct bha_ccb *ccb;
    479 {
    480 	int s;
    481 
    482 	s = splbio();
    483 
    484 	bha_reset_ccb(sc, ccb);
    485 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    486 
    487 	/*
    488 	 * If there were none, wake anybody waiting for one to come free,
    489 	 * starting with queued entries.
    490 	 */
    491 	if (ccb->chain.tqe_next == 0)
    492 		wakeup(&sc->sc_free_ccb);
    493 
    494 	splx(s);
    495 }
    496 
    497 integrate int
    498 bha_init_ccb(sc, ccb)
    499 	struct bha_softc *sc;
    500 	struct bha_ccb *ccb;
    501 {
    502 	bus_dma_tag_t dmat = sc->sc_dmat;
    503 	int hashnum, error;
    504 
    505 	/*
    506 	 * Create the DMA map for this CCB.
    507 	 */
    508 	error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
    509 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    510 	    &ccb->dmamap_xfer);
    511 	if (error) {
    512 		printf("%s: unable to create ccb DMA map, error = %d\n",
    513 		    sc->sc_dev.dv_xname, error);
    514 		return (error);
    515 	}
    516 
    517 	/*
    518 	 * put in the phystokv hash table
    519 	 * Never gets taken out.
    520 	 */
    521 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    522 	    BHA_CCB_OFF(ccb);
    523 	hashnum = CCB_HASH(ccb->hashkey);
    524 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    525 	sc->sc_ccbhash[hashnum] = ccb;
    526 	bha_reset_ccb(sc, ccb);
    527 	return (0);
    528 }
    529 
    530 /*
    531  * Create a set of ccbs and add them to the free list.  Called once
    532  * by bha_init().  We return the number of CCBs successfully created.
    533  */
    534 int
    535 bha_create_ccbs(sc, ccbstore, count)
    536 	struct bha_softc *sc;
    537 	struct bha_ccb *ccbstore;
    538 	int count;
    539 {
    540 	struct bha_ccb *ccb;
    541 	int i, error;
    542 
    543 	bzero(ccbstore, sizeof(struct bha_ccb) * count);
    544 	for (i = 0; i < count; i++) {
    545 		ccb = &ccbstore[i];
    546 		if ((error = bha_init_ccb(sc, ccb)) != 0) {
    547 			printf("%s: unable to initialize ccb, error = %d\n",
    548 			    sc->sc_dev.dv_xname, error);
    549 			goto out;
    550 		}
    551 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    552 	}
    553  out:
    554 	return (i);
    555 }
    556 
    557 /*
    558  * Get a free ccb
    559  *
    560  * If there are none, see if we can allocate a new one.  If so, put it in
    561  * the hash table too otherwise either return an error or sleep.
    562  */
    563 struct bha_ccb *
    564 bha_get_ccb(sc, flags)
    565 	struct bha_softc *sc;
    566 	int flags;
    567 {
    568 	struct bha_ccb *ccb;
    569 	int s;
    570 
    571 	s = splbio();
    572 
    573 	/*
    574 	 * If we can and have to, sleep waiting for one to come free
    575 	 * but only if we can't allocate a new one.
    576 	 */
    577 	for (;;) {
    578 		ccb = sc->sc_free_ccb.tqh_first;
    579 		if (ccb) {
    580 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    581 			break;
    582 		}
    583 		if ((flags & SCSI_NOSLEEP) != 0)
    584 			goto out;
    585 		tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
    586 	}
    587 
    588 	ccb->flags |= CCB_ALLOC;
    589 
    590 out:
    591 	splx(s);
    592 	return (ccb);
    593 }
    594 
    595 /*
    596  * Given a physical address, find the ccb that it corresponds to.
    597  */
    598 struct bha_ccb *
    599 bha_ccb_phys_kv(sc, ccb_phys)
    600 	struct bha_softc *sc;
    601 	u_long ccb_phys;
    602 {
    603 	int hashnum = CCB_HASH(ccb_phys);
    604 	struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
    605 
    606 	while (ccb) {
    607 		if (ccb->hashkey == ccb_phys)
    608 			break;
    609 		ccb = ccb->nexthash;
    610 	}
    611 	return (ccb);
    612 }
    613 
    614 /*
    615  * Queue a CCB to be sent to the controller, and send it if possible.
    616  */
    617 void
    618 bha_queue_ccb(sc, ccb)
    619 	struct bha_softc *sc;
    620 	struct bha_ccb *ccb;
    621 {
    622 
    623 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    624 	bha_start_ccbs(sc);
    625 }
    626 
    627 /*
    628  * Garbage collect mailboxes that are no longer in use.
    629  */
    630 void
    631 bha_collect_mbo(sc)
    632 	struct bha_softc *sc;
    633 {
    634 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
    635 #ifdef BHADIAG
    636 	struct bha_ccb *ccb;
    637 #endif
    638 
    639 	wmbo = wmbx->cmbo;
    640 
    641 	while (sc->sc_mbofull > 0) {
    642 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    643 		    BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
    644 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    645 		if (wmbo->cmd != BHA_MBO_FREE)
    646 			break;
    647 
    648 #ifdef BHADIAG
    649 		ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
    650 		ccb->flags &= ~CCB_SENDING;
    651 #endif
    652 
    653 		--sc->sc_mbofull;
    654 		bha_nextmbx(wmbo, wmbx, mbo);
    655 	}
    656 
    657 	wmbx->cmbo = wmbo;
    658 }
    659 
    660 /*
    661  * Send as many CCBs as we have empty mailboxes for.
    662  */
    663 void
    664 bha_start_ccbs(sc)
    665 	struct bha_softc *sc;
    666 {
    667 	bus_space_tag_t iot = sc->sc_iot;
    668 	bus_space_handle_t ioh = sc->sc_ioh;
    669 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
    670 	struct bha_ccb *ccb;
    671 
    672 	wmbo = wmbx->tmbo;
    673 
    674 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    675 		if (sc->sc_mbofull >= BHA_MBX_SIZE) {
    676 			bha_collect_mbo(sc);
    677 			if (sc->sc_mbofull >= BHA_MBX_SIZE) {
    678 				struct bha_toggle toggle;
    679 
    680 				toggle.cmd.opcode = BHA_MBO_INTR_EN;
    681 				toggle.cmd.enable = 1;
    682 				bha_cmd(iot, ioh, sc,
    683 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    684 				    0, (u_char *)0);
    685 				break;
    686 			}
    687 		}
    688 
    689 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    690 #ifdef BHADIAG
    691 		ccb->flags |= CCB_SENDING;
    692 #endif
    693 
    694 		/* Link ccb to mbo. */
    695 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
    696 		    BHA_CCB_OFF(ccb), wmbo->ccb_addr);
    697 		if (ccb->flags & CCB_ABORT)
    698 			wmbo->cmd = BHA_MBO_ABORT;
    699 		else
    700 			wmbo->cmd = BHA_MBO_START;
    701 
    702 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    703 		    BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
    704 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    705 
    706 		/* Tell the card to poll immediately. */
    707 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
    708 
    709 		if ((ccb->xs->flags & SCSI_POLL) == 0)
    710 			timeout(bha_timeout, ccb, (ccb->timeout * hz) / 1000);
    711 
    712 		++sc->sc_mbofull;
    713 		bha_nextmbx(wmbo, wmbx, mbo);
    714 	}
    715 
    716 	wmbx->tmbo = wmbo;
    717 }
    718 
    719 /*
    720  * We have a ccb which has been processed by the
    721  * adaptor, now we look to see how the operation
    722  * went. Wake up the owner if waiting
    723  */
    724 void
    725 bha_done(sc, ccb)
    726 	struct bha_softc *sc;
    727 	struct bha_ccb *ccb;
    728 {
    729 	bus_dma_tag_t dmat = sc->sc_dmat;
    730 	struct scsipi_sense_data *s1, *s2;
    731 	struct scsipi_xfer *xs = ccb->xs;
    732 
    733 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
    734 
    735 	/*
    736 	 * If we were a data transfer, unload the map that described
    737 	 * the data buffer.
    738 	 */
    739 	if (xs->datalen) {
    740 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    741 		    ccb->dmamap_xfer->dm_mapsize,
    742 		    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    743 		    BUS_DMASYNC_POSTWRITE);
    744 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
    745 	}
    746 
    747 	/*
    748 	 * Otherwise, put the results of the operation
    749 	 * into the xfer and call whoever started it
    750 	 */
    751 #ifdef BHADIAG
    752 	if (ccb->flags & CCB_SENDING) {
    753 		printf("%s: exiting ccb still in transit!\n",
    754 		    sc->sc_dev.dv_xname);
    755 		Debugger();
    756 		return;
    757 	}
    758 #endif
    759 	if ((ccb->flags & CCB_ALLOC) == 0) {
    760 		printf("%s: exiting ccb not allocated!\n",
    761 		    sc->sc_dev.dv_xname);
    762 		Debugger();
    763 		return;
    764 	}
    765 	if (xs->error == XS_NOERROR) {
    766 		if (ccb->host_stat != BHA_OK) {
    767 			switch (ccb->host_stat) {
    768 			case BHA_SEL_TIMEOUT:	/* No response */
    769 				xs->error = XS_SELTIMEOUT;
    770 				break;
    771 			default:	/* Other scsi protocol messes */
    772 				printf("%s: host_stat %x\n",
    773 				    sc->sc_dev.dv_xname, ccb->host_stat);
    774 				xs->error = XS_DRIVER_STUFFUP;
    775 				break;
    776 			}
    777 		} else if (ccb->target_stat != SCSI_OK) {
    778 			switch (ccb->target_stat) {
    779 			case SCSI_CHECK:
    780 				s1 = &ccb->scsi_sense;
    781 				s2 = &xs->sense.scsi_sense;
    782 				*s2 = *s1;
    783 				xs->error = XS_SENSE;
    784 				break;
    785 			case SCSI_BUSY:
    786 				xs->error = XS_BUSY;
    787 				break;
    788 			default:
    789 				printf("%s: target_stat %x\n",
    790 				    sc->sc_dev.dv_xname, ccb->target_stat);
    791 				xs->error = XS_DRIVER_STUFFUP;
    792 				break;
    793 			}
    794 		} else
    795 			xs->resid = 0;
    796 	}
    797 	bha_free_ccb(sc, ccb);
    798 	xs->flags |= ITSDONE;
    799 	scsipi_done(xs);
    800 
    801 	/*
    802 	 * If there are queue entries in the software queue, try to
    803 	 * run the first one.  We should be more or less guaranteed
    804 	 * to succeed, since we just freed a CCB.
    805 	 *
    806 	 * NOTE: bha_scsi_cmd() relies on our calling it with
    807 	 * the first entry in the queue.
    808 	 */
    809 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    810 		(void) bha_scsi_cmd(xs);
    811 }
    812 
    813 /*
    814  * Find the board and find it's irq/drq
    815  */
    816 int
    817 bha_find(iot, ioh, sc)
    818 	bus_space_tag_t iot;
    819 	bus_space_handle_t ioh;
    820 	struct bha_probe_data *sc;
    821 {
    822 	int i, iswide;
    823 	u_char sts;
    824 	struct bha_extended_inquire inquire;
    825 	struct bha_config config;
    826 	int irq, drq;
    827 
    828 	/* Check something is at the ports we need to access */
    829 	sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    830 	if (sts == 0xFF)
    831 		return (0);
    832 
    833 	/*
    834 	 * Reset board, If it doesn't respond, assume
    835 	 * that it's not there.. good for the probe
    836 	 */
    837 
    838 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
    839 	    BHA_CTRL_HRST | BHA_CTRL_SRST);
    840 
    841 	delay(100);
    842 	for (i = BHA_RESET_TIMEOUT; i; i--) {
    843 		sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    844 		if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
    845 			break;
    846 		delay(1000);
    847 	}
    848 	if (!i) {
    849 #ifdef BHADEBUG
    850 		if (bha_debug)
    851 			printf("bha_find: No answer from buslogic board\n");
    852 #endif /* BHADEBUG */
    853 		return (0);
    854 	}
    855 
    856 	/*
    857 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
    858 	 * interface. The native bha interface is not compatible with
    859 	 * an aha. 1542. We need to ensure that we never match an
    860 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
    861 	 * commands to a real bha, lest it go into 1542 emulation mode.
    862 	 * (On an indirect bus like ISA, we should always probe for BusLogic
    863 	 * interfaces  before Adaptec interfaces).
    864 	 */
    865 
    866 	/*
    867 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
    868 	 * for an extended-geometry register.  The 1542[AB] don't have one.
    869 	 */
    870 	sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
    871 	if (sts == 0xFF)
    872 		return (0);
    873 
    874 	/*
    875 	 * Check that we actually know how to use this board.
    876 	 */
    877 	delay(1000);
    878 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
    879 	inquire.cmd.len = sizeof(inquire.reply);
    880 	i = bha_cmd(iot, ioh, (struct bha_softc *)0,
    881 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
    882 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
    883 
    884 	/*
    885 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
    886 	 * have the extended-geometry register and also respond to
    887 	 * BHA_INQUIRE_EXTENDED.  Make sure we never  match such cards,
    888 	 * by checking the size of the reply is what a BusLogic card returns.
    889 	 */
    890 	if (i) {
    891 #ifdef BHADEBUG
    892 		printf("bha_find: board returned %d instead of %d to %s\n",
    893 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
    894 #endif
    895 		return (0);
    896 	}
    897 
    898 	/* OK, we know we've found a buslogic adaptor. */
    899 
    900 	switch (inquire.reply.bus_type) {
    901 	case BHA_BUS_TYPE_24BIT:
    902 	case BHA_BUS_TYPE_32BIT:
    903 		break;
    904 	case BHA_BUS_TYPE_MCA:
    905 		/* We don't grok MicroChannel (yet). */
    906 		return (0);
    907 	default:
    908 		printf("bha_find: illegal bus type %c\n",
    909 		    inquire.reply.bus_type);
    910 		return (0);
    911 	}
    912 
    913 	/* Note if we have a wide bus. */
    914 	iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
    915 
    916 	/*
    917 	 * Assume we have a board at this stage setup dma channel from
    918 	 * jumpers and save int level
    919 	 */
    920 	delay(1000);
    921 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
    922 	bha_cmd(iot, ioh, (struct bha_softc *)0,
    923 	    sizeof(config.cmd), (u_char *)&config.cmd,
    924 	    sizeof(config.reply), (u_char *)&config.reply);
    925 	switch (config.reply.chan) {
    926 	case EISADMA:
    927 		drq = -1;
    928 		break;
    929 	case CHAN0:
    930 		drq = 0;
    931 		break;
    932 	case CHAN5:
    933 		drq = 5;
    934 		break;
    935 	case CHAN6:
    936 		drq = 6;
    937 		break;
    938 	case CHAN7:
    939 		drq = 7;
    940 		break;
    941 	default:
    942 		printf("bha_find: illegal drq setting %x\n",
    943 		    config.reply.chan);
    944 		return (0);
    945 	}
    946 
    947 	switch (config.reply.intr) {
    948 	case INT9:
    949 		irq = 9;
    950 		break;
    951 	case INT10:
    952 		irq = 10;
    953 		break;
    954 	case INT11:
    955 		irq = 11;
    956 		break;
    957 	case INT12:
    958 		irq = 12;
    959 		break;
    960 	case INT14:
    961 		irq = 14;
    962 		break;
    963 	case INT15:
    964 		irq = 15;
    965 		break;
    966 	default:
    967 		printf("bha_find: illegal irq setting %x\n",
    968 		    config.reply.intr);
    969 		return (0);
    970 	}
    971 
    972 	/* if we want to fill in softc, do so now */
    973 	if (sc != NULL) {
    974 		sc->sc_irq = irq;
    975 		sc->sc_drq = drq;
    976 		sc->sc_scsi_dev = config.reply.scsi_dev;
    977 		sc->sc_iswide = iswide;
    978 	}
    979 
    980 	return (1);
    981 }
    982 
    983 
    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 /*
   1004  * Start the board, ready for normal operation
   1005  */
   1006 int
   1007 bha_init(sc)
   1008 	struct bha_softc *sc;
   1009 {
   1010 	bus_space_tag_t iot = sc->sc_iot;
   1011 	bus_space_handle_t ioh = sc->sc_ioh;
   1012 	bus_dma_segment_t seg;
   1013 	struct bha_devices devices;
   1014 	struct bha_setup setup;
   1015 	struct bha_mailbox mailbox;
   1016 	struct bha_period period;
   1017 	int error, i, j, initial_ccbs, rlen, rseg;
   1018 
   1019 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
   1020 	if (strcmp(sc->sc_firmware, "3.31") >= 0) {
   1021 		struct bha_toggle toggle;
   1022 
   1023 		toggle.cmd.opcode = BHA_ROUND_ROBIN;
   1024 		toggle.cmd.enable = 1;
   1025 		bha_cmd(iot, ioh, sc,
   1026 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
   1027 		    0, (u_char *)0);
   1028 	}
   1029 
   1030 	/*
   1031 	 * Inquire installed devices (to force synchronous negotiation).
   1032 	 */
   1033 
   1034 	/*
   1035 	 * Poll targets 0 - 7.
   1036 	 */
   1037 	devices.cmd.opcode = BHA_INQUIRE_DEVICES;
   1038 	bha_cmd(iot, ioh, sc,
   1039 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1040 	    sizeof(devices.reply), (u_char *)&devices.reply);
   1041 
   1042 	/* Count installed units. */
   1043 	initial_ccbs = 0;
   1044 	for (i = 0; i < 8; i++) {
   1045 		for (j = 0; j < 8; j++) {
   1046 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1047 				initial_ccbs++;
   1048 		}
   1049 	}
   1050 
   1051 	/*
   1052 	 * Poll targets 8 - 15 if we have a wide bus.
   1053 	 */
   1054 	if (ISWIDE(sc)) {
   1055 		devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
   1056 		bha_cmd(iot, ioh, sc,
   1057 		    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1058 		    sizeof(devices.reply), (u_char *)&devices.reply);
   1059 
   1060 		for (i = 0; i < 8; i++) {
   1061 			for (j = 0; j < 8; j++) {
   1062 				if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1063 					initial_ccbs++;
   1064 			}
   1065 		}
   1066 	}
   1067 
   1068 	initial_ccbs *= sc->sc_link.openings;
   1069 	if (initial_ccbs > BHA_CCB_MAX)
   1070 		initial_ccbs = BHA_CCB_MAX;
   1071 	if (initial_ccbs == 0)	/* yes, this can happen */
   1072 		initial_ccbs = sc->sc_link.openings;
   1073 
   1074 	/* Obtain setup information from. */
   1075 	rlen = sizeof(setup.reply) +
   1076 	    (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
   1077 	setup.cmd.opcode = BHA_INQUIRE_SETUP;
   1078 	setup.cmd.len = rlen;
   1079 	bha_cmd(iot, ioh, sc,
   1080 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
   1081 	    rlen, (u_char *)&setup.reply);
   1082 
   1083 	printf("%s: %s, %s\n",
   1084 	    sc->sc_dev.dv_xname,
   1085 	    setup.reply.sync_neg ? "sync" : "async",
   1086 	    setup.reply.parity ? "parity" : "no parity");
   1087 
   1088 	for (i = 0; i < 8; i++)
   1089 		period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
   1090 	if (ISWIDE(sc)) {
   1091 		for (i = 0; i < 8; i++)
   1092 			period.reply_w.period[i] =
   1093 			    setup.reply_w.sync[i].period * 5 + 20;
   1094 	}
   1095 
   1096 	if (sc->sc_firmware[0] >= '3') {
   1097 		rlen = sizeof(period.reply) +
   1098 		    (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
   1099 		period.cmd.opcode = BHA_INQUIRE_PERIOD;
   1100 		period.cmd.len = rlen;
   1101 		bha_cmd(iot, ioh, sc,
   1102 		    sizeof(period.cmd), (u_char *)&period.cmd,
   1103 		    rlen, (u_char *)&period.reply);
   1104 	}
   1105 
   1106 	for (i = 0; i < 8; i++) {
   1107 		if (!setup.reply.sync[i].valid ||
   1108 		    (!setup.reply.sync[i].offset &&
   1109 		     !setup.reply.sync[i].period))
   1110 			continue;
   1111 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
   1112 		    sc->sc_dev.dv_xname, i,
   1113 		    setup.reply.sync[i].offset, period.reply.period[i] * 10);
   1114 	}
   1115 	if (ISWIDE(sc)) {
   1116 		for (i = 0; i < 8; i++) {
   1117 			if (!setup.reply_w.sync[i].valid ||
   1118 			    (!setup.reply_w.sync[i].offset &&
   1119 			     !setup.reply_w.sync[i].period))
   1120 				continue;
   1121 			printf("%s targ %d: sync, offset %d, period %dnsec\n",
   1122 			    sc->sc_dev.dv_xname, i + 8,
   1123 			    setup.reply_w.sync[i].offset,
   1124 			    period.reply_w.period[i] * 10);
   1125 		}
   1126 	}
   1127 
   1128 	/*
   1129 	 * Allocate the mailbox and control blocks.
   1130 	 */
   1131 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct bha_control),
   1132 	    NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
   1133 		printf("%s: unable to allocate control structures, "
   1134 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1135 		return (error);
   1136 	}
   1137 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1138 	    sizeof(struct bha_control), (caddr_t *)&sc->sc_control,
   1139 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1140 		printf("%s: unable to map control structures, error = %d\n",
   1141 		    sc->sc_dev.dv_xname, error);
   1142 		return (error);
   1143 	}
   1144 
   1145 	/*
   1146 	 * Create and load the DMA map used for the mailbox and
   1147 	 * control blocks.
   1148 	 */
   1149 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_control),
   1150 	    1, sizeof(struct bha_control), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
   1151 	    &sc->sc_dmamap_control)) != 0) {
   1152 		printf("%s: unable to create control DMA map, error = %d\n",
   1153 		    sc->sc_dev.dv_xname, error);
   1154 		return (error);
   1155 	}
   1156 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
   1157 	    sc->sc_control, sizeof(struct bha_control), NULL,
   1158 	    BUS_DMA_NOWAIT)) != 0) {
   1159 		printf("%s: unable to load control DMA map, error = %d\n",
   1160 		    sc->sc_dev.dv_xname, error);
   1161 		return (error);
   1162 	}
   1163 
   1164 	/*
   1165 	 * Initialize the control blocks.
   1166 	 */
   1167 	i = bha_create_ccbs(sc, sc->sc_control->bc_ccbs, initial_ccbs);
   1168 	if (i == 0) {
   1169 		printf("%s: unable to create control blocks\n",
   1170 		    sc->sc_dev.dv_xname);
   1171 		return (ENOMEM);
   1172 	} else if (i != initial_ccbs) {
   1173 		printf("%s: WARNING: only %d of %d control blocks created\n",
   1174 		    sc->sc_dev.dv_xname, i, initial_ccbs);
   1175 	}
   1176 
   1177 	/*
   1178 	 * Set up initial mail box for round-robin operation.
   1179 	 */
   1180 	for (i = 0; i < BHA_MBX_SIZE; i++) {
   1181 		wmbx->mbo[i].cmd = BHA_MBO_FREE;
   1182 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1183 		    BHA_MBO_OFF(&wmbx->mbo[i]), sizeof(struct bha_mbx_out),
   1184 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1185 		wmbx->mbi[i].stat = BHA_MBI_FREE;
   1186 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1187 		    BHA_MBI_OFF(&wmbx->mbi[i]), sizeof(struct bha_mbx_in),
   1188 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1189 	}
   1190 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
   1191 	wmbx->tmbi = &wmbx->mbi[0];
   1192 	sc->sc_mbofull = 0;
   1193 
   1194 	/* Initialize mail box. */
   1195 	mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
   1196 	mailbox.cmd.nmbx = BHA_MBX_SIZE;
   1197 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1198 	    offsetof(struct bha_control, bc_mbx), mailbox.cmd.addr);
   1199 	bha_cmd(iot, ioh, sc,
   1200 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
   1201 	    0, (u_char *)0);
   1202 	return (0);
   1203 }
   1204 
   1205 void
   1206 bha_inquire_setup_information(sc)
   1207 	struct bha_softc *sc;
   1208 {
   1209 	bus_space_tag_t iot = sc->sc_iot;
   1210 	bus_space_handle_t ioh = sc->sc_ioh;
   1211 	struct bha_model model;
   1212 	struct bha_revision revision;
   1213 	struct bha_digit digit;
   1214 	char *p;
   1215 
   1216 	/*
   1217 	 * Get the firmware revision.
   1218 	 */
   1219 	p = sc->sc_firmware;
   1220 	revision.cmd.opcode = BHA_INQUIRE_REVISION;
   1221 	bha_cmd(iot, ioh, sc,
   1222 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
   1223 	    sizeof(revision.reply), (u_char *)&revision.reply);
   1224 	*p++ = revision.reply.firm_revision;
   1225 	*p++ = '.';
   1226 	*p++ = revision.reply.firm_version;
   1227 	digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
   1228 	bha_cmd(iot, ioh, sc,
   1229 	    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1230 	    sizeof(digit.reply), (u_char *)&digit.reply);
   1231 	*p++ = digit.reply.digit;
   1232 	if (revision.reply.firm_revision >= '3' ||
   1233 	    (revision.reply.firm_revision == '3' &&
   1234 	     revision.reply.firm_version >= '3')) {
   1235 		digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
   1236 		bha_cmd(iot, ioh, sc,
   1237 		    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1238 		    sizeof(digit.reply), (u_char *)&digit.reply);
   1239 		*p++ = digit.reply.digit;
   1240 	}
   1241 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
   1242 		p--;
   1243 	*p = '\0';
   1244 
   1245 	/*
   1246 	 * Get the model number.
   1247 	 */
   1248 	if (revision.reply.firm_revision >= '3') {
   1249 		p = sc->sc_model;
   1250 		model.cmd.opcode = BHA_INQUIRE_MODEL;
   1251 		model.cmd.len = sizeof(model.reply);
   1252 		bha_cmd(iot, ioh, sc,
   1253 		    sizeof(model.cmd), (u_char *)&model.cmd,
   1254 		    sizeof(model.reply), (u_char *)&model.reply);
   1255 		*p++ = model.reply.id[0];
   1256 		*p++ = model.reply.id[1];
   1257 		*p++ = model.reply.id[2];
   1258 		*p++ = model.reply.id[3];
   1259 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1260 			p--;
   1261 		*p++ = model.reply.version[0];
   1262 		*p++ = model.reply.version[1];
   1263 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1264 			p--;
   1265 		*p = '\0';
   1266 	} else
   1267 		strcpy(sc->sc_model, "542B");
   1268 }
   1269 
   1270 void
   1271 bhaminphys(bp)
   1272 	struct buf *bp;
   1273 {
   1274 
   1275 	if (bp->b_bcount > BHA_MAXXFER)
   1276 		bp->b_bcount = BHA_MAXXFER;
   1277 	minphys(bp);
   1278 }
   1279 
   1280 /*
   1281  * start a scsi operation given the command and the data address.  Also needs
   1282  * the unit, target and lu.
   1283  */
   1284 int
   1285 bha_scsi_cmd(xs)
   1286 	struct scsipi_xfer *xs;
   1287 {
   1288 	struct scsipi_link *sc_link = xs->sc_link;
   1289 	struct bha_softc *sc = sc_link->adapter_softc;
   1290 	bus_dma_tag_t dmat = sc->sc_dmat;
   1291 	struct bha_ccb *ccb;
   1292 	int error, seg, flags, s;
   1293 	int fromqueue = 0, dontqueue = 0;
   1294 
   1295 	SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
   1296 
   1297 	s = splbio();		/* protect the queue */
   1298 
   1299 	/*
   1300 	 * If we're running the queue from bha_done(), we've been
   1301 	 * called with the first queue entry as our argument.
   1302 	 */
   1303 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
   1304 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
   1305 		fromqueue = 1;
   1306 		goto get_ccb;
   1307 	}
   1308 
   1309 	/* Polled requests can't be queued for later. */
   1310 	dontqueue = xs->flags & SCSI_POLL;
   1311 
   1312 	/*
   1313 	 * If there are jobs in the queue, run them first.
   1314 	 */
   1315 	if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
   1316 		/*
   1317 		 * If we can't queue, we have to abort, since
   1318 		 * we have to preserve order.
   1319 		 */
   1320 		if (dontqueue) {
   1321 			splx(s);
   1322 			xs->error = XS_DRIVER_STUFFUP;
   1323 			return (TRY_AGAIN_LATER);
   1324 		}
   1325 
   1326 		/*
   1327 		 * Swap with the first queue entry.
   1328 		 */
   1329 		TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
   1330 		xs = TAILQ_FIRST(&sc->sc_queue);
   1331 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
   1332 		fromqueue = 1;
   1333 	}
   1334 
   1335  get_ccb:
   1336 	/*
   1337 	 * get a ccb to use. If the transfer
   1338 	 * is from a buf (possibly from interrupt time)
   1339 	 * then we can't allow it to sleep
   1340 	 */
   1341 	flags = xs->flags;
   1342 	if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
   1343 		/*
   1344 		 * If we can't queue, we lose.
   1345 		 */
   1346 		if (dontqueue) {
   1347 			splx(s);
   1348 			xs->error = XS_DRIVER_STUFFUP;
   1349 			return (TRY_AGAIN_LATER);
   1350 		}
   1351 
   1352 		/*
   1353 		 * Stuff ourselves into the queue, in front
   1354 		 * if we came off in the first place.
   1355 		 */
   1356 		if (fromqueue)
   1357 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
   1358 		else
   1359 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
   1360 		splx(s);
   1361 		return (SUCCESSFULLY_QUEUED);
   1362 	}
   1363 
   1364 	splx(s);		/* done playing with the queue */
   1365 
   1366 	ccb->xs = xs;
   1367 	ccb->timeout = xs->timeout;
   1368 
   1369 	/*
   1370 	 * Put all the arguments for the xfer in the ccb
   1371 	 */
   1372 	if (flags & SCSI_RESET) {
   1373 		ccb->opcode = BHA_RESET_CCB;
   1374 		ccb->scsi_cmd_length = 0;
   1375 	} else {
   1376 		/* can't use S/G if zero length */
   1377 		ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
   1378 					   : BHA_INITIATOR_CCB);
   1379 		bcopy(xs->cmd, &ccb->scsi_cmd,
   1380 		    ccb->scsi_cmd_length = xs->cmdlen);
   1381 	}
   1382 
   1383 	if (xs->datalen) {
   1384 		/*
   1385 		 * Map the DMA transfer.
   1386 		 */
   1387 #ifdef TFS
   1388 		if (flags & SCSI_DATA_UIO) {
   1389 			error = bus_dmamap_load_uio(dmat,
   1390 			    ccb->dmamap_xfer, (struct uio *)xs->data,
   1391 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
   1392 			    BUS_DMA_WAITOK);
   1393 		} else
   1394 #endif /* TFS */
   1395 		{
   1396 			error = bus_dmamap_load(dmat,
   1397 			    ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
   1398 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
   1399 			    BUS_DMA_WAITOK);
   1400 		}
   1401 
   1402 		if (error) {
   1403 			if (error == EFBIG) {
   1404 				printf("%s: bha_scsi_cmd, more than %d"
   1405 				    " dma segments\n",
   1406 				    sc->sc_dev.dv_xname, BHA_NSEG);
   1407 			} else {
   1408 				printf("%s: bha_scsi_cmd, error %d loading"
   1409 				    " dma map\n",
   1410 				    sc->sc_dev.dv_xname, error);
   1411 			}
   1412 			goto bad;
   1413 		}
   1414 
   1415 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
   1416 		    ccb->dmamap_xfer->dm_mapsize,
   1417 		    (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
   1418 		    BUS_DMASYNC_PREWRITE);
   1419 
   1420 		/*
   1421 		 * Load the hardware scatter/gather map with the
   1422 		 * contents of the DMA map.
   1423 		 */
   1424 		for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
   1425 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
   1426 			    ccb->scat_gath[seg].seg_addr);
   1427 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
   1428 			    ccb->scat_gath[seg].seg_len);
   1429 		}
   1430 
   1431 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1432 		    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scat_gath),
   1433 		    ccb->data_addr);
   1434 		ltophys(ccb->dmamap_xfer->dm_nsegs *
   1435 		    sizeof(struct bha_scat_gath), ccb->data_length);
   1436 	} else {
   1437 		/*
   1438 		 * No data xfer, use non S/G values.
   1439 		 */
   1440 		ltophys(0, ccb->data_addr);
   1441 		ltophys(0, ccb->data_length);
   1442 	}
   1443 
   1444 	ccb->data_out = 0;
   1445 	ccb->data_in = 0;
   1446 	ccb->target = sc_link->scsipi_scsi.target;
   1447 	ccb->lun = sc_link->scsipi_scsi.lun;
   1448 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1449 	    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scsi_sense),
   1450 	    ccb->sense_ptr);
   1451 	ccb->req_sense_length = sizeof(ccb->scsi_sense);
   1452 	ccb->host_stat = 0x00;
   1453 	ccb->target_stat = 0x00;
   1454 	ccb->link_id = 0;
   1455 	ltophys(0, ccb->link_addr);
   1456 
   1457 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1458 	    BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
   1459 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1460 
   1461 	s = splbio();
   1462 	bha_queue_ccb(sc, ccb);
   1463 	splx(s);
   1464 
   1465 	/*
   1466 	 * Usually return SUCCESSFULLY QUEUED
   1467 	 */
   1468 	SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
   1469 	if ((flags & SCSI_POLL) == 0)
   1470 		return (SUCCESSFULLY_QUEUED);
   1471 
   1472 	/*
   1473 	 * If we can't use interrupts, poll on completion
   1474 	 */
   1475 	if (bha_poll(sc, xs, ccb->timeout)) {
   1476 		bha_timeout(ccb);
   1477 		if (bha_poll(sc, xs, ccb->timeout))
   1478 			bha_timeout(ccb);
   1479 	}
   1480 	return (COMPLETE);
   1481 
   1482 bad:
   1483 	xs->error = XS_DRIVER_STUFFUP;
   1484 	bha_free_ccb(sc, ccb);
   1485 	return (COMPLETE);
   1486 }
   1487 
   1488 /*
   1489  * Poll a particular unit, looking for a particular xs
   1490  */
   1491 int
   1492 bha_poll(sc, xs, count)
   1493 	struct bha_softc *sc;
   1494 	struct scsipi_xfer *xs;
   1495 	int count;
   1496 {
   1497 	bus_space_tag_t iot = sc->sc_iot;
   1498 	bus_space_handle_t ioh = sc->sc_ioh;
   1499 
   1500 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1501 	while (count) {
   1502 		/*
   1503 		 * If we had interrupts enabled, would we
   1504 		 * have got an interrupt?
   1505 		 */
   1506 		if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
   1507 		    BHA_INTR_ANYINTR)
   1508 			bha_intr(sc);
   1509 		if (xs->flags & ITSDONE)
   1510 			return (0);
   1511 		delay(1000);	/* only happens in boot so ok */
   1512 		count--;
   1513 	}
   1514 	return (1);
   1515 }
   1516 
   1517 void
   1518 bha_timeout(arg)
   1519 	void *arg;
   1520 {
   1521 	struct bha_ccb *ccb = arg;
   1522 	struct scsipi_xfer *xs = ccb->xs;
   1523 	struct scsipi_link *sc_link = xs->sc_link;
   1524 	struct bha_softc *sc = sc_link->adapter_softc;
   1525 	int s;
   1526 
   1527 	scsi_print_addr(sc_link);
   1528 	printf("timed out");
   1529 
   1530 	s = splbio();
   1531 
   1532 #ifdef BHADIAG
   1533 	/*
   1534 	 * If the ccb's mbx is not free, then the board has gone Far East?
   1535 	 */
   1536 	bha_collect_mbo(sc);
   1537 	if (ccb->flags & CCB_SENDING) {
   1538 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1539 		Debugger();
   1540 	}
   1541 #endif
   1542 
   1543 	/*
   1544 	 * If it has been through before, then
   1545 	 * a previous abort has failed, don't
   1546 	 * try abort again
   1547 	 */
   1548 	if (ccb->flags & CCB_ABORT) {
   1549 		/* abort timed out */
   1550 		printf(" AGAIN\n");
   1551 		/* XXX Must reset! */
   1552 	} else {
   1553 		/* abort the operation that has timed out */
   1554 		printf("\n");
   1555 		ccb->xs->error = XS_TIMEOUT;
   1556 		ccb->timeout = BHA_ABORT_TIMEOUT;
   1557 		ccb->flags |= CCB_ABORT;
   1558 		bha_queue_ccb(sc, ccb);
   1559 	}
   1560 
   1561 	splx(s);
   1562 }
   1563