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