Home | History | Annotate | Line # | Download | only in dti
btl.c revision 1.5
      1 /*	$NetBSD: btl.c,v 1.5 2000/06/09 05:30:51 soda Exp $	*/
      2 /*	NetBSD: bt.c,v 1.10 1996/05/12 23:51:54 mycroft Exp 	*/
      3 
      4 #undef BTDIAG
      5 #define integrate
      6 
      7 #define notyet /* XXX - #undef this, if this driver does actually work */
      8 
      9 /*
     10  * Copyright (c) 1994, 1996 Charles M. Hannum.  All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by Charles M. Hannum.
     23  * 4. The name of the author may not be used to endorse or promote products
     24  *    derived from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Originally written by Julian Elischer (julian (at) tfs.com)
     40  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     41  *
     42  * TRW Financial Systems, in accordance with their agreement with Carnegie
     43  * Mellon University, makes this software available to CMU to distribute
     44  * or use in any manner that they see fit as long as this message is kept with
     45  * the software. For this reason TFS also grants any other persons or
     46  * organisations permission to use or modify this software.
     47  *
     48  * TFS supplies this software to be publicly redistributed
     49  * on the understanding that TFS is not responsible for the correct
     50  * functioning of this software in any circumstances.
     51  */
     52 
     53 #include <sys/types.h>
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/errno.h>
     58 #include <sys/malloc.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/device.h>
     61 #include <sys/buf.h>
     62 #include <sys/proc.h>
     63 #include <sys/user.h>
     64 
     65 #include <machine/intr.h>
     66 #include <machine/pio.h>
     67 
     68 #include <arc/dti/desktech.h>
     69 
     70 #include <dev/scsipi/scsi_all.h>
     71 #include <dev/scsipi/scsipi_all.h>
     72 #include <dev/scsipi/scsiconf.h>
     73 
     74 #include <dev/isa/isavar.h>
     75 #include <arc/dti/btlreg.h>
     76 #include <arc/arc/arctype.h>    /* XXX for cpu types */
     77 
     78 #ifndef DDB
     79 #define Debugger() panic("should call debugger here (bt742a.c)")
     80 #endif /* ! DDB */
     81 
     82 /*
     83  * Mail box defs  etc.
     84  * these could be bigger but we need the bt_softc to fit on a single page..
     85  */
     86 #define BT_MBX_SIZE	32	/* mail box size  (MAX 255 MBxs) */
     87 				/* don't need that many really */
     88 #define BT_CCB_MAX	32	/* store up to 32 CCBs at one time */
     89 #define	CCB_HASH_SIZE	32	/* hash table size for phystokv */
     90 #define	CCB_HASH_SHIFT	9
     91 #define CCB_HASH(x)	((((long)(x))>>CCB_HASH_SHIFT) & (CCB_HASH_SIZE - 1))
     92 
     93 #define bt_nextmbx(wmb, mbx, mbio) \
     94 	if ((wmb) == &(mbx)->mbio[BT_MBX_SIZE - 1])	\
     95 		(wmb) = &(mbx)->mbio[0];		\
     96 	else						\
     97 		(wmb)++;
     98 
     99 struct bt_mbx {
    100 	struct bt_mbx_out mbo[BT_MBX_SIZE];
    101 	struct bt_mbx_in mbi[BT_MBX_SIZE];
    102 	struct bt_mbx_out *cmbo;	/* Collection Mail Box out */
    103 	struct bt_mbx_out *tmbo;	/* Target Mail Box out */
    104 	struct bt_mbx_in *tmbi;		/* Target Mail Box in */
    105 };
    106 
    107 #define KVTOPHYS(x)	((cputype == DESKSTATION_TYNE) ? \
    108 	(((int)(x) & 0x7fffff) | 0x800000) : ((int)(x)))
    109 #define PHYSTOKV(x)	((cputype == DESKSTATION_TYNE) ? \
    110 	(((int)(x) & 0x7fffff) | TYNE_V_BOUNCE) : ((int)(x)))
    111 
    112 struct bt_softc {
    113 	struct device sc_dev;
    114 	void *sc_ih;
    115 
    116 	int sc_iobase;
    117 	int sc_irq, sc_drq;
    118 
    119 	char sc_model[7],
    120 	     sc_firmware[6];
    121 
    122 	struct bt_mbx *sc_mbx;		/* all our mailboxes */
    123 #define	wmbx	(sc->sc_mbx)
    124 	struct bt_ccb *sc_ccbhash[CCB_HASH_SIZE];
    125 	TAILQ_HEAD(, bt_ccb) sc_free_ccb, sc_waiting_ccb;
    126 	TAILQ_HEAD(, bt_buf) sc_free_buf;
    127 	int sc_numccbs, sc_mbofull;
    128 	int sc_numbufs;
    129 	int sc_scsi_dev;		/* adapters scsi id */
    130 	struct scsipi_link sc_link;	/* prototype for devs */
    131 	struct scsipi_adapter sc_adapter;
    132 };
    133 
    134 #ifdef BTDEBUG
    135 int     bt_debug = 0;
    136 #endif /* BTDEBUG */
    137 
    138 int bt_cmd __P((int, struct bt_softc *, int, u_char *, int, u_char *));
    139 integrate void bt_finish_ccbs __P((struct bt_softc *));
    140 int btintr __P((void *));
    141 integrate void bt_reset_ccb __P((struct bt_softc *, struct bt_ccb *));
    142 void bt_free_ccb __P((struct bt_softc *, struct bt_ccb *));
    143 integrate void bt_init_ccb __P((struct bt_softc *, struct bt_ccb *));
    144 struct bt_ccb *bt_get_ccb __P((struct bt_softc *, int));
    145 struct bt_ccb *bt_ccb_phys_kv __P((struct bt_softc *, u_long));
    146 void bt_queue_ccb __P((struct bt_softc *, struct bt_ccb *));
    147 void bt_collect_mbo __P((struct bt_softc *));
    148 void bt_start_ccbs __P((struct bt_softc *));
    149 void bt_done __P((struct bt_softc *, struct bt_ccb *));
    150 int bt_find __P((struct isa_attach_args *, struct bt_softc *));
    151 void bt_init __P((struct bt_softc *));
    152 void bt_inquire_setup_information __P((struct bt_softc *));
    153 void btminphys __P((struct buf *));
    154 int bt_scsi_cmd __P((struct scsipi_xfer *));
    155 int bt_poll __P((struct bt_softc *, struct scsipi_xfer *, int));
    156 void bt_timeout __P((void *arg));
    157 void bt_free_buf __P((struct bt_softc *, struct bt_buf *));
    158 struct bt_buf * bt_get_buf __P((struct bt_softc *, int));
    159 
    160 /* XXX static buffer as a kludge.  DMA isn't cache coherent on the rpc44, so
    161  * we always use uncached buffers for DMA. */
    162 static char rpc44_buffer[ TYNE_S_BOUNCE ];
    163 
    164 /* the below structure is so we have a default dev struct for out link struct */
    165 struct scsipi_device bt_dev = {
    166 	NULL,			/* Use default error handler */
    167 	NULL,			/* have a queue, served by this */
    168 	NULL,			/* have no async handler */
    169 	NULL,			/* Use default 'done' routine */
    170 };
    171 
    172 int	btprobe __P((struct device *, struct cfdata *, void *));
    173 void	btattach __P((struct device *, struct device *, void *));
    174 int	btprint __P((void *, const char *));
    175 
    176 struct cfattach btl_ca = {
    177 	sizeof(struct bt_softc), btprobe, btattach
    178 };
    179 
    180 #define BT_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
    181 #define	BT_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    182 
    183 /*
    184  * bt_cmd(iobase, sc, icnt, ibuf, ocnt, obuf)
    185  *
    186  * Activate Adapter command
    187  *    icnt:   number of args (outbound bytes including opcode)
    188  *    ibuf:   argument buffer
    189  *    ocnt:   number of expected returned bytes
    190  *    obuf:   result buffer
    191  *    wait:   number of seconds to wait for response
    192  *
    193  * Performs an adapter command through the ports.  Not to be confused with a
    194  * scsi command, which is read in via the dma; one of the adapter commands
    195  * tells it to read in a scsi command.
    196  */
    197 int
    198 bt_cmd(iobase, sc, icnt, ibuf, ocnt, obuf)
    199 	int iobase;
    200 	struct bt_softc *sc;
    201 	int icnt, ocnt;
    202 	u_char *ibuf, *obuf;
    203 {
    204 	const char *name;
    205 	register int i;
    206 	int wait;
    207 	u_char sts;
    208 	u_char opcode = ibuf[0];
    209 
    210 	if (sc != NULL)
    211 		name = sc->sc_dev.dv_xname;
    212 	else
    213 		name = "(bt probe)";
    214 
    215 	/*
    216 	 * Calculate a reasonable timeout for the command.
    217 	 */
    218 	switch (opcode) {
    219 	case BT_INQUIRE_DEVICES:
    220 		wait = 15 * 20000;
    221 		break;
    222 	default:
    223 		wait = 1 * 20000;
    224 		break;
    225 	}
    226 
    227 	/*
    228 	 * Wait for the adapter to go idle, unless it's one of
    229 	 * the commands which don't need this
    230 	 */
    231 	if (opcode != BT_MBO_INTR_EN) {
    232 		for (i = 20000; i; i--) {	/* 1 sec? */
    233 			sts = isa_inb(iobase + BT_STAT_PORT);
    234 			if (sts & BT_STAT_IDLE)
    235 				break;
    236 			delay(50);
    237 		}
    238 		if (!i) {
    239 			printf("%s: bt_cmd, host not idle(0x%x)\n",
    240 			    name, sts);
    241 			return ENXIO;
    242 		}
    243 	}
    244 	/*
    245 	 * Now that it is idle, if we expect output, preflush the
    246 	 * queue feeding to us.
    247 	 */
    248 	if (ocnt) {
    249 		while ((isa_inb(iobase + BT_STAT_PORT)) & BT_STAT_DF)
    250 			isa_inb(iobase + BT_DATA_PORT);
    251 	}
    252 	/*
    253 	 * Output the command and the number of arguments given
    254 	 * for each byte, first check the port is empty.
    255 	 */
    256 	while (icnt--) {
    257 		for (i = wait; i; i--) {
    258 			sts = isa_inb(iobase + BT_STAT_PORT);
    259 			if (!(sts & BT_STAT_CDF))
    260 				break;
    261 			delay(50);
    262 		}
    263 		if (!i) {
    264 			if (opcode != BT_INQUIRE_REVISION &&
    265 			    opcode != BT_INQUIRE_REVISION_3)
    266 				printf("%s: bt_cmd, cmd/data port full\n", name);
    267 			isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
    268 			return ENXIO;
    269 		}
    270 		isa_outb(iobase + BT_CMD_PORT, *ibuf++);
    271 	}
    272 	/*
    273 	 * If we expect input, loop that many times, each time,
    274 	 * looking for the data register to have valid data
    275 	 */
    276 	while (ocnt--) {
    277 		for (i = wait; i; i--) {
    278 			sts = isa_inb(iobase + BT_STAT_PORT);
    279 			if (sts & BT_STAT_DF)
    280 				break;
    281 			delay(50);
    282 		}
    283 		if (!i) {
    284 			if (opcode != BT_INQUIRE_REVISION &&
    285 			    opcode != BT_INQUIRE_REVISION_3)
    286 				printf("%s: bt_cmd, cmd/data port empty %d\n",
    287 				    name, ocnt);
    288 			isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
    289 			return ENXIO;
    290 		}
    291 		*obuf++ = isa_inb(iobase + BT_DATA_PORT);
    292 	}
    293 	/*
    294 	 * Wait for the board to report a finished instruction.
    295 	 * We may get an extra interrupt for the HACC signal, but this is
    296 	 * unimportant.
    297 	 */
    298 	if (opcode != BT_MBO_INTR_EN) {
    299 		for (i = 20000; i; i--) {	/* 1 sec? */
    300 			sts = isa_inb(iobase + BT_INTR_PORT);
    301 			/* XXX Need to save this in the interrupt handler? */
    302 			if (sts & BT_INTR_HACC)
    303 				break;
    304 			delay(50);
    305 		}
    306 		if (!i) {
    307 			printf("%s: bt_cmd, host not finished(0x%x)\n",
    308 			    name, sts);
    309 			return ENXIO;
    310 		}
    311 	}
    312 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
    313 	return 0;
    314 }
    315 
    316 /*
    317  * Check if the device can be found at the port given
    318  * and if so, set it up ready for further work
    319  * as an argument, takes the isa_device structure from
    320  * autoconf.c
    321  */
    322 int
    323 btprobe(parent, match, aux)
    324 	struct device *parent;
    325 	struct cfdata *match;
    326 	void *aux;
    327 {
    328 	register struct isa_attach_args *ia = aux;
    329 
    330 #ifdef NEWCONFIG
    331 	if (ia->ia_iobase == IOBASEUNK)
    332 		return 0;
    333 #endif
    334 
    335 	/* See if there is a unit at this location. */
    336 	if (bt_find(ia, NULL) != 0)
    337 		return 0;
    338 
    339 	ia->ia_msize = 0;
    340 	ia->ia_iosize = 4;
    341 	/* IRQ and DRQ set by bt_find(). */
    342 	return 1;
    343 }
    344 
    345 /*
    346  * Attach all the sub-devices we can find
    347  */
    348 void
    349 btattach(parent, self, aux)
    350 	struct device *parent, *self;
    351 	void *aux;
    352 {
    353 	struct isa_attach_args *ia = aux;
    354 	struct bt_softc *sc = (void *)self;
    355 	struct bt_ccb *ccb;
    356 	struct bt_buf *buf;
    357 	u_int bouncearea;
    358 	u_int bouncebase;
    359 	u_int bouncesize;
    360 
    361 	if (bt_find(ia, sc) != 0)
    362 		panic("btattach: bt_find of %s failed", self->dv_xname);
    363 	sc->sc_iobase = ia->ia_iobase;
    364 
    365 	/*
    366 	 * create mbox area
    367 	 */
    368 	if (cputype == DESKSTATION_TYNE) {
    369 		bouncebase = TYNE_V_BOUNCE;
    370 		bouncesize = TYNE_S_BOUNCE;
    371 	} else {
    372 		bouncesize = TYNE_S_BOUNCE; /* Good enough? XXX */
    373 /*		bouncebase = (u_int) malloc( bouncesize, M_DEVBUF, M_NOWAIT);*/
    374 		bouncebase = (u_int) rpc44_buffer | 0xa0000000;
    375 	}
    376 	bouncearea = bouncebase + sizeof(struct bt_mbx);
    377 	sc->sc_mbx = (struct bt_mbx *)bouncebase;
    378 
    379 	bt_inquire_setup_information(sc);
    380 	bt_init(sc);
    381 	TAILQ_INIT(&sc->sc_free_ccb);
    382 	TAILQ_INIT(&sc->sc_free_buf);
    383 	TAILQ_INIT(&sc->sc_waiting_ccb);
    384 
    385 	/*
    386 	 * fill up with ccb's
    387 	 */
    388 	while (sc->sc_numccbs < BT_CCB_MAX) {
    389 		ccb = (struct bt_ccb *)bouncearea;
    390 		bouncearea +=  sizeof(struct bt_ccb);
    391 		bt_init_ccb(sc, ccb);
    392 		TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    393 		sc->sc_numccbs++;
    394 	}
    395 	/*
    396 	 * fill up with bufs's
    397 	 */
    398 	while ((bouncearea + sizeof(struct bt_buf)) < bouncebase + bouncesize) {
    399 		buf = (struct bt_buf *)bouncearea;
    400 		bouncearea +=  sizeof(struct bt_buf);
    401 		TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
    402 		sc->sc_numbufs++;
    403 	}
    404 	/*
    405 	 * Fill in the adapter.
    406 	 */
    407 	sc->sc_adapter.scsipi_cmd = bt_scsi_cmd;
    408 	sc->sc_adapter.scsipi_minphys = btminphys;
    409 	/*
    410 	 * fill in the prototype scsipi_link.
    411 	 */
    412 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    413 	sc->sc_link.adapter_softc = sc;
    414 	sc->sc_link.scsipi_scsi.adapter_target = sc->sc_scsi_dev;
    415 	sc->sc_link.adapter = &sc->sc_adapter;
    416 	sc->sc_link.device = &bt_dev;
    417 	sc->sc_link.openings = 1;
    418 	sc->sc_link.scsipi_scsi.max_target = 7;
    419 	sc->sc_link.scsipi_scsi.max_lun = 7;
    420 	sc->sc_link.type = BUS_SCSI;
    421 
    422 	sc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq, IST_EDGE,
    423 	    IPL_BIO, btintr, sc);
    424 
    425 	/*
    426 	 * ask the adapter what subunits are present
    427 	 */
    428 	config_found(self, &sc->sc_link, scsiprint);
    429 }
    430 
    431 integrate void
    432 bt_finish_ccbs(sc)
    433 	struct bt_softc *sc;
    434 {
    435 	struct bt_mbx_in *wmbi;
    436 	struct bt_ccb *ccb;
    437 	int i;
    438 
    439 	wmbi = wmbx->tmbi;
    440 
    441 	if (wmbi->stat == BT_MBI_FREE) {
    442 		for (i = 0; i < BT_MBX_SIZE; i++) {
    443 			if (wmbi->stat != BT_MBI_FREE) {
    444 				printf("%s: mbi not in round-robin order\n",
    445 				    sc->sc_dev.dv_xname);
    446 				goto AGAIN;
    447 			}
    448 			bt_nextmbx(wmbi, wmbx, mbi);
    449 		}
    450 #ifdef BTDIAGnot
    451 		printf("%s: mbi interrupt with no full mailboxes\n",
    452 		    sc->sc_dev.dv_xname);
    453 #endif
    454 		return;
    455 	}
    456 
    457 AGAIN:
    458 	do {
    459 		ccb = bt_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
    460 		if (!ccb) {
    461 			printf("%s: bad mbi ccb pointer; skipping\n",
    462 			    sc->sc_dev.dv_xname);
    463 			goto next;
    464 		}
    465 
    466 #ifdef BTDEBUG
    467 		if (bt_debug) {
    468 			u_char *cp = (u_char *) &ccb->scsi_cmd;
    469 			printf("op=%x %x %x %x %x %x\n",
    470 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    471 			printf("stat %x for mbi addr = 0x%08x, ",
    472 			    wmbi->stat, wmbi);
    473 			printf("ccb addr = 0x%x\n", ccb);
    474 		}
    475 #endif /* BTDEBUG */
    476 
    477 		switch (wmbi->stat) {
    478 		case BT_MBI_OK:
    479 		case BT_MBI_ERROR:
    480 			if ((ccb->flags & CCB_ABORT) != 0) {
    481 				/*
    482 				 * If we already started an abort, wait for it
    483 				 * to complete before clearing the CCB.  We
    484 				 * could instead just clear CCB_SENDING, but
    485 				 * what if the mailbox was already received?
    486 				 * The worst that happens here is that we clear
    487 				 * the CCB a bit later than we need to.  BFD.
    488 				 */
    489 				goto next;
    490 			}
    491 			break;
    492 
    493 		case BT_MBI_ABORT:
    494 		case BT_MBI_UNKNOWN:
    495 			/*
    496 			 * Even if the CCB wasn't found, we clear it anyway.
    497 			 * See preceeding comment.
    498 			 */
    499 			break;
    500 
    501 		default:
    502 			printf("%s: bad mbi status %02x; skipping\n",
    503 			    sc->sc_dev.dv_xname, wmbi->stat);
    504 			goto next;
    505 		}
    506 
    507 		callout_stop(&ccb->xs->xs_callout);
    508 		bt_done(sc, ccb);
    509 
    510 	next:
    511 		wmbi->stat = BT_MBI_FREE;
    512 		bt_nextmbx(wmbi, wmbx, mbi);
    513 	} while (wmbi->stat != BT_MBI_FREE);
    514 
    515 	wmbx->tmbi = wmbi;
    516 }
    517 
    518 /*
    519  * Catch an interrupt from the adaptor
    520  */
    521 int
    522 btintr(arg)
    523 	void *arg;
    524 {
    525 	struct bt_softc *sc = arg;
    526 	int iobase = sc->sc_iobase;
    527 	u_char sts;
    528 
    529 #ifdef BTDEBUG
    530 	printf("%s: btintr ", sc->sc_dev.dv_xname);
    531 #endif /* BTDEBUG */
    532 
    533 	/*
    534 	 * First acknowlege the interrupt, Then if it's not telling about
    535 	 * a completed operation just return.
    536 	 */
    537 	sts = isa_inb(iobase + BT_INTR_PORT);
    538 	if ((sts & BT_INTR_ANYINTR) == 0)
    539 		return 0;
    540 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
    541 
    542 #ifdef BTDIAG
    543 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
    544 	bt_collect_mbo(sc);
    545 #endif
    546 
    547 	/* Mail box out empty? */
    548 	if (sts & BT_INTR_MBOA) {
    549 		struct bt_toggle toggle;
    550 
    551 		toggle.cmd.opcode = BT_MBO_INTR_EN;
    552 		toggle.cmd.enable = 0;
    553 		bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd, 0,
    554 		    (u_char *)0);
    555 		bt_start_ccbs(sc);
    556 	}
    557 
    558 	/* Mail box in full? */
    559 	if (sts & BT_INTR_MBIF)
    560 		bt_finish_ccbs(sc);
    561 
    562 	return 1;
    563 }
    564 
    565 integrate void
    566 bt_reset_ccb(sc, ccb)
    567 	struct bt_softc *sc;
    568 	struct bt_ccb *ccb;
    569 {
    570 
    571 	ccb->flags = 0;
    572 }
    573 
    574 /*
    575  * A ccb is put onto the free list.
    576  */
    577 void
    578 bt_free_ccb(sc, ccb)
    579 	struct bt_softc *sc;
    580 	struct bt_ccb *ccb;
    581 {
    582 	int s;
    583 
    584 	s = splbio();
    585 
    586 	bt_reset_ccb(sc, ccb);
    587 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    588 
    589 	/*
    590 	 * If there were none, wake anybody waiting for one to come free,
    591 	 * starting with queued entries.
    592 	 */
    593 	if (ccb->chain.tqe_next == 0)
    594 		wakeup(&sc->sc_free_ccb);
    595 
    596 	splx(s);
    597 }
    598 
    599 /*
    600  * A buf is put onto the free list.
    601  */
    602 void
    603 bt_free_buf(sc, buf)
    604 	struct bt_softc *sc;
    605 	struct bt_buf *buf;
    606 {
    607 	int s;
    608 
    609 	s = splbio();
    610 
    611 	TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
    612 	sc->sc_numbufs++;
    613 
    614 	/*
    615 	 * If there were none, wake anybody waiting for one to come free,
    616 	 * starting with queued entries.
    617 	 */
    618 	if (buf->chain.tqe_next == 0)
    619 		wakeup(&sc->sc_free_buf);
    620 
    621 	splx(s);
    622 }
    623 
    624 integrate void
    625 bt_init_ccb(sc, ccb)
    626 	struct bt_softc *sc;
    627 	struct bt_ccb *ccb;
    628 {
    629 	int hashnum;
    630 
    631 	bzero(ccb, sizeof(struct bt_ccb));
    632 	/*
    633 	 * put in the phystokv hash table
    634 	 * Never gets taken out.
    635 	 */
    636 	ccb->hashkey = KVTOPHYS(ccb);
    637 	hashnum = CCB_HASH(ccb->hashkey);
    638 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    639 	sc->sc_ccbhash[hashnum] = ccb;
    640 	bt_reset_ccb(sc, ccb);
    641 }
    642 
    643 /*
    644  * Get a free ccb
    645  *
    646  * If there are none, either return an error or sleep.
    647  */
    648 struct bt_ccb *
    649 bt_get_ccb(sc, nosleep)
    650 	struct bt_softc *sc;
    651 	int nosleep;
    652 {
    653 	struct bt_ccb *ccb;
    654 	int s;
    655 
    656 	s = splbio();
    657 
    658 	/*
    659 	 * If we can and have to, sleep waiting for one to come free.
    660 	 */
    661 	for (;;) {
    662 		ccb = sc->sc_free_ccb.tqh_first;
    663 		if (ccb) {
    664 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    665 			break;
    666 		}
    667 		if (nosleep)
    668 			goto out;
    669 		tsleep(&sc->sc_free_ccb, PRIBIO, "btccb", 0);
    670 	}
    671 
    672 	ccb->flags |= CCB_ALLOC;
    673 
    674 out:
    675 	splx(s);
    676 	return ccb;
    677 }
    678 
    679 /*
    680  * Get a free buf
    681  *
    682  * If there are none, either return an error or sleep.
    683  */
    684 struct bt_buf *
    685 bt_get_buf(sc, nosleep)
    686 	struct bt_softc *sc;
    687 	int nosleep;
    688 {
    689 	struct bt_buf *buf;
    690 	int s;
    691 
    692 	s = splbio();
    693 
    694 	/*
    695 	 * If we can and have to, sleep waiting for one to come free.
    696 	 */
    697 	for (;;) {
    698 		buf = sc->sc_free_buf.tqh_first;
    699 		if (buf) {
    700 			TAILQ_REMOVE(&sc->sc_free_buf, buf, chain);
    701 			sc->sc_numbufs--;
    702 			break;
    703 		}
    704 		if (nosleep)
    705 			goto out;
    706 		tsleep(&sc->sc_free_buf, PRIBIO, "btbuf", 0);
    707 	}
    708 
    709 out:
    710 	splx(s);
    711 	return buf;
    712 }
    713 
    714 /*
    715  * Given a physical address, find the ccb that it corresponds to.
    716  */
    717 struct bt_ccb *
    718 bt_ccb_phys_kv(sc, ccb_phys)
    719 	struct bt_softc *sc;
    720 	u_long ccb_phys;
    721 {
    722 	int hashnum = CCB_HASH(ccb_phys);
    723 	struct bt_ccb *ccb = sc->sc_ccbhash[hashnum];
    724 
    725 	while (ccb) {
    726 		if (ccb->hashkey == ccb_phys)
    727 			break;
    728 		ccb = ccb->nexthash;
    729 	}
    730 	return ccb;
    731 }
    732 
    733 /*
    734  * Queue a CCB to be sent to the controller, and send it if possible.
    735  */
    736 void
    737 bt_queue_ccb(sc, ccb)
    738 	struct bt_softc *sc;
    739 	struct bt_ccb *ccb;
    740 {
    741 
    742 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    743 	bt_start_ccbs(sc);
    744 }
    745 
    746 /*
    747  * Garbage collect mailboxes that are no longer in use.
    748  */
    749 void
    750 bt_collect_mbo(sc)
    751 	struct bt_softc *sc;
    752 {
    753 	struct bt_mbx_out *wmbo;	/* Mail Box Out pointer */
    754 
    755 	wmbo = wmbx->cmbo;
    756 
    757 	while (sc->sc_mbofull > 0) {
    758 		if (wmbo->cmd != BT_MBO_FREE)
    759 			break;
    760 
    761 #ifdef BTDIAG
    762 		ccb = bt_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
    763 		ccb->flags &= ~CCB_SENDING;
    764 #endif
    765 
    766 		--sc->sc_mbofull;
    767 		bt_nextmbx(wmbo, wmbx, mbo);
    768 	}
    769 
    770 	wmbx->cmbo = wmbo;
    771 }
    772 
    773 /*
    774  * Send as many CCBs as we have empty mailboxes for.
    775  */
    776 void
    777 bt_start_ccbs(sc)
    778 	struct bt_softc *sc;
    779 {
    780 	int iobase = sc->sc_iobase;
    781 	struct bt_mbx_out *wmbo;	/* Mail Box Out pointer */
    782 	struct bt_ccb *ccb;
    783 
    784 	wmbo = wmbx->tmbo;
    785 
    786 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    787 		if (sc->sc_mbofull >= BT_MBX_SIZE) {
    788 			bt_collect_mbo(sc);
    789 			if (sc->sc_mbofull >= BT_MBX_SIZE) {
    790 				struct bt_toggle toggle;
    791 
    792 				toggle.cmd.opcode = BT_MBO_INTR_EN;
    793 				toggle.cmd.enable = 1;
    794 				bt_cmd(iobase, sc, sizeof(toggle.cmd),
    795 				    (u_char *)&toggle.cmd, 0, (u_char *)0);
    796 				break;
    797 			}
    798 		}
    799 
    800 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    801 #ifdef BTDIAG
    802 		ccb->flags |= CCB_SENDING;
    803 #endif
    804 
    805 		/* Link ccb to mbo. */
    806 		ltophys(KVTOPHYS(ccb), wmbo->ccb_addr);
    807 		if (ccb->flags & CCB_ABORT)
    808 			wmbo->cmd = BT_MBO_ABORT;
    809 		else
    810 			wmbo->cmd = BT_MBO_START;
    811 
    812 		/* Tell the card to poll immediately. */
    813 		isa_outb(iobase + BT_CMD_PORT, BT_START_SCSI);
    814 
    815 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
    816 			callout_reset(&ccb->xs->xs_callout,
    817 			    (ccb->timeout * hz) / 1000, bt_timeout, ccb);
    818 
    819 		++sc->sc_mbofull;
    820 		bt_nextmbx(wmbo, wmbx, mbo);
    821 	}
    822 
    823 	wmbx->tmbo = wmbo;
    824 }
    825 
    826 /*
    827  * We have a ccb which has been processed by the
    828  * adaptor, now we look to see how the operation
    829  * went. Wake up the owner if waiting
    830  */
    831 void
    832 bt_done(sc, ccb)
    833 	struct bt_softc *sc;
    834 	struct bt_ccb *ccb;
    835 {
    836 	struct scsipi_sense_data *s1, *s2;
    837 	struct scsipi_xfer *xs = ccb->xs;
    838 
    839 	u_long thiskv, thisbounce;
    840 	int bytes_this_page, datalen;
    841 	struct bt_scat_gath *sg;
    842 	int seg;
    843 
    844 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("bt_done\n"));
    845 	/*
    846 	 * Otherwise, put the results of the operation
    847 	 * into the xfer and call whoever started it
    848 	 */
    849 #ifdef BTDIAG
    850 	if (ccb->flags & CCB_SENDING) {
    851 		printf("%s: exiting ccb still in transit!\n", sc->sc_dev.dv_xname);
    852 		Debugger();
    853 		return;
    854 	}
    855 #endif
    856 	if ((ccb->flags & CCB_ALLOC) == 0) {
    857 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    858 		Debugger();
    859 		return;
    860 	}
    861 	if (xs->error == XS_NOERROR) {
    862 		if (ccb->host_stat != BT_OK) {
    863 			switch (ccb->host_stat) {
    864 			case BT_SEL_TIMEOUT:	/* No response */
    865 				xs->error = XS_SELTIMEOUT;
    866 				break;
    867 			default:	/* Other scsi protocol messes */
    868 				printf("%s: host_stat %x\n",
    869 				    sc->sc_dev.dv_xname, ccb->host_stat);
    870 				xs->error = XS_DRIVER_STUFFUP;
    871 				break;
    872 			}
    873 		} else if (ccb->target_stat != SCSI_OK) {
    874 			switch (ccb->target_stat) {
    875 			case SCSI_CHECK:
    876 				s1 = &ccb->scsi_sense;
    877 				s2 = &xs->sense.scsi_sense;
    878 				*s2 = *s1;
    879 				xs->error = XS_SENSE;
    880 				break;
    881 			case SCSI_BUSY:
    882 				xs->error = XS_BUSY;
    883 				break;
    884 			default:
    885 				printf("%s: target_stat %x\n",
    886 				    sc->sc_dev.dv_xname, ccb->target_stat);
    887 				xs->error = XS_DRIVER_STUFFUP;
    888 				break;
    889 			}
    890 		} else
    891 			xs->resid = 0;
    892 	}
    893 
    894 	if((datalen = xs->datalen) != 0) {
    895 		thiskv = (int)xs->data;
    896 		sg = ccb->scat_gath;
    897 		seg = phystol(ccb->data_length) / sizeof(struct bt_scat_gath);
    898 
    899 		while (seg) {
    900 			thisbounce = PHYSTOKV(phystol(sg->seg_addr));
    901 			bytes_this_page = phystol(sg->seg_len);
    902 			if(xs->xs_control & XS_CTL_DATA_IN) {
    903 				bcopy((void *)thisbounce, (void *)thiskv, bytes_this_page);
    904 			}
    905 			bt_free_buf(sc, (struct bt_buf *)thisbounce);
    906 			thiskv += bytes_this_page;
    907 			datalen -= bytes_this_page;
    908 
    909 			sg++;
    910 			seg--;
    911 		}
    912 	}
    913 
    914 	bt_free_ccb(sc, ccb);
    915 	xs->xs_status |= XS_STS_DONE;
    916 	scsipi_done(xs);
    917 }
    918 
    919 /*
    920  * Find the board and find it's irq/drq
    921  */
    922 int
    923 bt_find(ia, sc)
    924 	struct isa_attach_args *ia;
    925 	struct bt_softc *sc;
    926 {
    927 	int iobase = ia->ia_iobase;
    928 	int i;
    929 	u_char sts;
    930 	struct bt_extended_inquire inquire;
    931 	struct bt_config config;
    932 	int irq, drq;
    933 
    934 #ifndef notyet
    935 	/* Check something is at the ports we need to access */
    936 	sts = isa_inb(iobase + BHA_STAT_PORT);
    937 	if (sts == 0xFF)
    938 		return (0);
    939 #endif
    940 
    941 	/*
    942 	 * reset board, If it doesn't respond, assume
    943 	 * that it's not there.. good for the probe
    944 	 */
    945 
    946 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_HRST | BT_CTRL_SRST);
    947 
    948 	delay(100);
    949 	for (i = BT_RESET_TIMEOUT; i; i--) {
    950 		sts = isa_inb(iobase + BT_STAT_PORT);
    951 		if (sts == (BT_STAT_IDLE | BT_STAT_INIT))
    952 			break;
    953 		delay(1000);
    954 	}
    955 	if (!i) {
    956 #ifdef BTDEBUG
    957 		if (bt_debug)
    958 			printf("bt_find: No answer from buslogic board\n");
    959 #endif /* BTDEBUG */
    960 		return 1;
    961 	}
    962 
    963 #ifndef notyet
    964 	/*
    965 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
    966 	 * interface. The native bha interface is not compatible with
    967 	 * an aha. 1542. We need to ensure that we never match an
    968 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
    969 	 * commands to a real bha, lest it go into 1542 emulation mode.
    970 	 * (On an indirect bus like ISA, we should always probe for BusLogic
    971 	 * interfaces before Adaptec interfaces).
    972 	 */
    973 
    974 	/*
    975 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
    976 	 * for an extended-geometry register.  The 1542[AB] don't have one.
    977 	 */
    978 	sts = isa_inb(iobase +  BT_EXTGEOM_PORT);
    979 	if (sts == 0xFF)
    980 		return (0);
    981 #endif /* notyet */
    982 
    983 	/*
    984 	 * Check that we actually know how to use this board.
    985 	 */
    986 	delay(1000);
    987 	bzero(&inquire, sizeof inquire);
    988 	inquire.cmd.opcode = BT_INQUIRE_EXTENDED;
    989 	inquire.cmd.len = sizeof(inquire.reply);
    990 	i = bt_cmd(iobase, sc, sizeof(inquire.cmd), (u_char *)&inquire.cmd,
    991 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
    992 
    993 #ifndef notyet
    994 	/*
    995 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
    996 	 * have the extended-geometry register and also respond to
    997 	 * BHA_INQUIRE_EXTENDED.  Make sure we never match such cards,
    998 	 * by checking the size of the reply is what a BusLogic card returns.
    999 	 */
   1000 	if (i) { /* XXX - this doesn't really check the size. ??? see bha.c */
   1001 #ifdef BTDEBUG
   1002 		printf("bt_find: board returned %d instead of %d to %s\n",
   1003 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
   1004 #endif
   1005 		return (0);
   1006 	}
   1007 
   1008 	/* OK, we know we've found a buslogic adaptor. */
   1009 #endif /* notyet */
   1010 
   1011 	switch (inquire.reply.bus_type) {
   1012 	case BT_BUS_TYPE_24BIT:
   1013 	case BT_BUS_TYPE_32BIT:
   1014 		break;
   1015 	case BT_BUS_TYPE_MCA:
   1016 		/* We don't grok MicroChannel (yet). */
   1017 		return 1;
   1018 	default:
   1019 		printf("bt_find: illegal bus type %c\n", inquire.reply.bus_type);
   1020 		return 1;
   1021 	}
   1022 
   1023 	/*
   1024 	 * Assume we have a board at this stage setup dma channel from
   1025 	 * jumpers and save int level
   1026 	 */
   1027 	delay(1000);
   1028 	config.cmd.opcode = BT_INQUIRE_CONFIG;
   1029 	bt_cmd(iobase, sc, sizeof(config.cmd), (u_char *)&config.cmd,
   1030 	    sizeof(config.reply), (u_char *)&config.reply);
   1031 	switch (config.reply.chan) {
   1032 	case EISADMA:
   1033 		drq = DRQUNK;
   1034 		break;
   1035 	case CHAN0:
   1036 		drq = 0;
   1037 		break;
   1038 	case CHAN5:
   1039 		drq = 5;
   1040 		break;
   1041 	case CHAN6:
   1042 		drq = 6;
   1043 		break;
   1044 	case CHAN7:
   1045 		drq = 7;
   1046 		break;
   1047 	default:
   1048 		printf("bt_find: illegal drq setting %x\n", config.reply.chan);
   1049 		return 1;
   1050 	}
   1051 
   1052 	switch (config.reply.intr) {
   1053 	case INT9:
   1054 		irq = 9;
   1055 		break;
   1056 	case INT10:
   1057 		irq = 10;
   1058 		break;
   1059 	case INT11:
   1060 		irq = 11;
   1061 		break;
   1062 	case INT12:
   1063 		irq = 12;
   1064 		break;
   1065 	case INT14:
   1066 		irq = 14;
   1067 		break;
   1068 	case INT15:
   1069 		irq = 15;
   1070 		break;
   1071 	default:
   1072 		printf("bt_find: illegal irq setting %x\n", config.reply.intr);
   1073 		return 1;
   1074 	}
   1075 
   1076 	if (sc != NULL) {
   1077 		/* who are we on the scsi bus? */
   1078 		sc->sc_scsi_dev = config.reply.scsi_dev;
   1079 
   1080 		sc->sc_iobase = iobase;
   1081 		sc->sc_irq = irq;
   1082 		sc->sc_drq = drq;
   1083 	} else {
   1084 		if (ia->ia_irq == IRQUNK)
   1085 			ia->ia_irq = irq;
   1086 		else if (ia->ia_irq != irq)
   1087 			return 1;
   1088 		if (ia->ia_drq == DRQUNK)
   1089 			ia->ia_drq = drq;
   1090 		else if (ia->ia_drq != drq)
   1091 			return 1;
   1092 	}
   1093 
   1094 	return 0;
   1095 }
   1096 
   1097 /*
   1098  * Start the board, ready for normal operation
   1099  */
   1100 void
   1101 bt_init(sc)
   1102 	struct bt_softc *sc;
   1103 {
   1104 	int iobase = sc->sc_iobase;
   1105 	struct bt_devices devices;
   1106 	struct bt_setup setup;
   1107 	struct bt_mailbox mailbox;
   1108 	struct bt_period period;
   1109 	int i;
   1110 
   1111 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
   1112 	if (strcmp(sc->sc_firmware, "3.31") >= 0) {
   1113 		struct bt_toggle toggle;
   1114 
   1115 		toggle.cmd.opcode = BT_ROUND_ROBIN;
   1116 		toggle.cmd.enable = 1;
   1117 		bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd,
   1118 		    0, (u_char *)0);
   1119 	}
   1120 
   1121 	/* Inquire Installed Devices (to force synchronous negotiation). */
   1122 	devices.cmd.opcode = BT_INQUIRE_DEVICES;
   1123 	bt_cmd(iobase, sc, sizeof(devices.cmd), (u_char *)&devices.cmd,
   1124 	    sizeof(devices.reply), (u_char *)&devices.reply);
   1125 
   1126 	/* Obtain setup information from. */
   1127 	setup.cmd.opcode = BT_INQUIRE_SETUP;
   1128 	setup.cmd.len = sizeof(setup.reply);
   1129 	bt_cmd(iobase, sc, sizeof(setup.cmd), (u_char *)&setup.cmd,
   1130 	    sizeof(setup.reply), (u_char *)&setup.reply);
   1131 
   1132 	printf("%s: %s, %s\n",
   1133 	    sc->sc_dev.dv_xname,
   1134 	    setup.reply.sync_neg ? "sync" : "async",
   1135 	    setup.reply.parity ? "parity" : "no parity");
   1136 
   1137 	for (i = 0; i < 8; i++)
   1138 		period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
   1139 
   1140 	if (sc->sc_firmware[0] >= '3') {
   1141 		period.cmd.opcode = BT_INQUIRE_PERIOD;
   1142 		period.cmd.len = sizeof(period.reply);
   1143 		bt_cmd(iobase, sc, sizeof(period.cmd), (u_char *)&period.cmd,
   1144 		    sizeof(period.reply), (u_char *)&period.reply);
   1145 	}
   1146 
   1147 	for (i = 0; i < 8; i++) {
   1148 		if (!setup.reply.sync[i].valid ||
   1149 		    (!setup.reply.sync[i].offset && !setup.reply.sync[i].period))
   1150 			continue;
   1151 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
   1152 		    sc->sc_dev.dv_xname, i,
   1153 		    setup.reply.sync[i].offset, period.reply.period[i] * 10);
   1154 	}
   1155 
   1156 	/*
   1157 	 * Set up initial mail box for round-robin operation.
   1158 	 */
   1159 	for (i = 0; i < BT_MBX_SIZE; i++) {
   1160 		wmbx->mbo[i].cmd = BT_MBO_FREE;
   1161 		wmbx->mbi[i].stat = BT_MBI_FREE;
   1162 	}
   1163 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
   1164 	wmbx->tmbi = &wmbx->mbi[0];
   1165 	sc->sc_mbofull = 0;
   1166 
   1167 	/* Initialize mail box. */
   1168 	mailbox.cmd.opcode = BT_MBX_INIT_EXTENDED;
   1169 	mailbox.cmd.nmbx = BT_MBX_SIZE;
   1170 	ltophys(KVTOPHYS(wmbx), mailbox.cmd.addr);
   1171 	bt_cmd(iobase, sc, sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
   1172 	    0, (u_char *)0);
   1173 }
   1174 
   1175 void
   1176 bt_inquire_setup_information(sc)
   1177 	struct bt_softc *sc;
   1178 {
   1179 	int iobase = sc->sc_iobase;
   1180 	struct bt_model model;
   1181 	struct bt_revision revision;
   1182 	struct bt_digit digit;
   1183 	char *p;
   1184 
   1185 	/*
   1186 	 * Get the firmware revision.
   1187 	 */
   1188 	p = sc->sc_firmware;
   1189 	revision.cmd.opcode = BT_INQUIRE_REVISION;
   1190 	bt_cmd(iobase, sc, sizeof(revision.cmd), (u_char *)&revision.cmd,
   1191 	    sizeof(revision.reply), (u_char *)&revision.reply);
   1192 	*p++ = revision.reply.firm_revision;
   1193 	*p++ = '.';
   1194 	*p++ = revision.reply.firm_version;
   1195 	digit.cmd.opcode = BT_INQUIRE_REVISION_3;
   1196 	bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
   1197 	    sizeof(digit.reply), (u_char *)&digit.reply);
   1198 	*p++ = digit.reply.digit;
   1199 	if (revision.reply.firm_revision >= '3' ||
   1200 	    (revision.reply.firm_revision == '3' && revision.reply.firm_version >= '3')) {
   1201 		digit.cmd.opcode = BT_INQUIRE_REVISION_4;
   1202 		bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
   1203 		    sizeof(digit.reply), (u_char *)&digit.reply);
   1204 		*p++ = digit.reply.digit;
   1205 	}
   1206 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
   1207 		p--;
   1208 	*p = '\0';
   1209 
   1210 	/*
   1211 	 * Get the model number.
   1212 	 */
   1213 	if (revision.reply.firm_revision >= '3') {
   1214 		p = sc->sc_model;
   1215 		model.cmd.opcode = BT_INQUIRE_MODEL;
   1216 		model.cmd.len = sizeof(model.reply);
   1217 		bt_cmd(iobase, sc, sizeof(model.cmd), (u_char *)&model.cmd,
   1218 		    sizeof(model.reply), (u_char *)&model.reply);
   1219 		*p++ = model.reply.id[0];
   1220 		*p++ = model.reply.id[1];
   1221 		*p++ = model.reply.id[2];
   1222 		*p++ = model.reply.id[3];
   1223 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1224 			p--;
   1225 		*p++ = model.reply.version[0];
   1226 		*p++ = model.reply.version[1];
   1227 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1228 			p--;
   1229 		*p = '\0';
   1230 	} else
   1231 		strcpy(sc->sc_model, "542B");
   1232 
   1233 	printf(": model BT-%s, firmware %s\n", sc->sc_model, sc->sc_firmware);
   1234 }
   1235 
   1236 void
   1237 btminphys(bp)
   1238 	struct buf *bp;
   1239 {
   1240 
   1241 	if (bp->b_bcount > ((BT_NSEG - 1) << PGSHIFT))
   1242 		bp->b_bcount = ((BT_NSEG - 1) << PGSHIFT);
   1243 	minphys(bp);
   1244 }
   1245 
   1246 /*
   1247  * start a scsi operation given the command and the data address.  Also needs
   1248  * the unit, target and lu.
   1249  */
   1250 int
   1251 bt_scsi_cmd(xs)
   1252 	struct scsipi_xfer *xs;
   1253 {
   1254 	struct scsipi_link *sc_link = xs->sc_link;
   1255 	struct bt_softc *sc = sc_link->adapter_softc;
   1256 	struct bt_ccb *ccb;
   1257 	struct bt_scat_gath *sg;
   1258 	int seg;		/* scatter gather seg being worked on */
   1259 	u_long thiskv, thisbounce;
   1260 	int bytes_this_page, datalen, control;
   1261 	int s;
   1262 
   1263 	SC_DEBUG(sc_link, SDEV_DB2, ("bt_scsi_cmd\n"));
   1264 	/*
   1265 	 * get a ccb to use. If the transfer
   1266 	 * is from a buf (possibly from interrupt time)
   1267 	 * then we can't allow it to sleep
   1268 	 */
   1269 	control = xs->xs_control;
   1270 	if ((ccb = bt_get_ccb(sc, control & XS_CTL_NOSLEEP)) == NULL) {
   1271 		xs->error = XS_DRIVER_STUFFUP;
   1272 		return TRY_AGAIN_LATER;
   1273 	}
   1274 	ccb->xs = xs;
   1275 	ccb->timeout = xs->timeout;
   1276 
   1277 	/*
   1278 	 * Put all the arguments for the xfer in the ccb
   1279 	 */
   1280 	if (control & XS_CTL_RESET) {
   1281 		ccb->opcode = BT_RESET_CCB;
   1282 		ccb->scsi_cmd_length = 0;
   1283 	} else {
   1284 		/* can't use S/G if zero length */
   1285 		ccb->opcode = (xs->datalen ? BT_INIT_SCAT_GATH_CCB
   1286 					   : BT_INITIATOR_CCB);
   1287 		bcopy(xs->cmd, &ccb->scsi_cmd,
   1288 		    ccb->scsi_cmd_length = xs->cmdlen);
   1289 	}
   1290 
   1291 	if (xs->datalen) {
   1292 		sg = ccb->scat_gath;
   1293 		seg = 0;
   1294 		/*
   1295 		 * Set up the scatter-gather block.
   1296 		 */
   1297 		SC_DEBUG(sc_link, SDEV_DB4,
   1298 		    ("%d @0x%x:- ", xs->datalen, xs->data));
   1299 
   1300 		datalen = xs->datalen;
   1301 		thiskv = (int)xs->data;
   1302 
   1303 		while (datalen && seg < BT_NSEG) {
   1304 
   1305 			/* put in the base address of a buf */
   1306 			thisbounce = (u_long)
   1307 				bt_get_buf(sc, control & XS_CTL_NOSLEEP);
   1308 			if(thisbounce == 0)
   1309 				break;
   1310 			ltophys(KVTOPHYS(thisbounce), sg->seg_addr);
   1311 			bytes_this_page = min(sizeof(struct bt_buf), datalen);
   1312 			if (control & XS_CTL_DATA_OUT) {
   1313 				bcopy((void *)thiskv, (void *)thisbounce, bytes_this_page);
   1314 			}
   1315 			thiskv += bytes_this_page;
   1316 			datalen -= bytes_this_page;
   1317 
   1318 			ltophys(bytes_this_page, sg->seg_len);
   1319 			sg++;
   1320 			seg++;
   1321 		}
   1322 		SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
   1323 		if (datalen) {
   1324 			printf("%s: bt_scsi_cmd, out of bufs %d of %d left.\n",
   1325 					sc->sc_dev.dv_xname, datalen, xs->datalen);
   1326 			goto badbuf;
   1327 		}
   1328 		ltophys(KVTOPHYS(ccb->scat_gath), ccb->data_addr);
   1329 		ltophys(seg * sizeof(struct bt_scat_gath), ccb->data_length);
   1330 	} else {		/* No data xfer, use non S/G values */
   1331 		ltophys(0, ccb->data_addr);
   1332 		ltophys(0, ccb->data_length);
   1333 	}
   1334 
   1335 	ccb->data_out = 0;
   1336 	ccb->data_in = 0;
   1337 	ccb->target = sc_link->scsipi_scsi.target;
   1338 	ccb->lun = sc_link->scsipi_scsi.lun;
   1339 	ltophys(KVTOPHYS(&ccb->scsi_sense), ccb->sense_ptr);
   1340 	ccb->req_sense_length = sizeof(ccb->scsi_sense);
   1341 	ccb->host_stat = 0x00;
   1342 	ccb->target_stat = 0x00;
   1343 	ccb->link_id = 0;
   1344 	ltophys(0, ccb->link_addr);
   1345 
   1346 	s = splbio();
   1347 	bt_queue_ccb(sc, ccb);
   1348 	splx(s);
   1349 
   1350 	/*
   1351 	 * Usually return SUCCESSFULLY QUEUED
   1352 	 */
   1353 	SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
   1354 	if ((control & XS_CTL_POLL) == 0)
   1355 		return SUCCESSFULLY_QUEUED;
   1356 
   1357 	/*
   1358 	 * If we can't use interrupts, poll on completion
   1359 	 */
   1360 	if (bt_poll(sc, xs, ccb->timeout)) {
   1361 		bt_timeout(ccb);
   1362 		if (bt_poll(sc, xs, ccb->timeout))
   1363 			bt_timeout(ccb);
   1364 	}
   1365 	return COMPLETE;
   1366 
   1367 badbuf:
   1368 	sg = ccb->scat_gath;
   1369 	while (seg) {
   1370 		thisbounce = PHYSTOKV(phystol(sg->seg_addr));
   1371 		bt_free_buf(sc, (struct bt_buf *)thisbounce);
   1372 		sg++;
   1373 		seg--;
   1374 	}
   1375 	xs->error = XS_DRIVER_STUFFUP;
   1376 	bt_free_ccb(sc, ccb);
   1377 	return TRY_AGAIN_LATER;
   1378 }
   1379 
   1380 /*
   1381  * Poll a particular unit, looking for a particular xs
   1382  */
   1383 int
   1384 bt_poll(sc, xs, count)
   1385 	struct bt_softc *sc;
   1386 	struct scsipi_xfer *xs;
   1387 	int count;
   1388 {
   1389 	int iobase = sc->sc_iobase;
   1390 
   1391 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1392 	while (count) {
   1393 		/*
   1394 		 * If we had interrupts enabled, would we
   1395 		 * have got an interrupt?
   1396 		 */
   1397 		if (isa_inb(iobase + BT_INTR_PORT) & BT_INTR_ANYINTR)
   1398 			btintr(sc);
   1399 		if (xs->xs_status & XS_STS_DONE)
   1400 			return 0;
   1401 		delay(1000);	/* only happens in boot so ok */
   1402 		count--;
   1403 	}
   1404 	return 1;
   1405 }
   1406 
   1407 void
   1408 bt_timeout(arg)
   1409 	void *arg;
   1410 {
   1411 	struct bt_ccb *ccb = arg;
   1412 	struct scsipi_xfer *xs = ccb->xs;
   1413 	struct scsipi_link *sc_link = xs->sc_link;
   1414 	struct bt_softc *sc = sc_link->adapter_softc;
   1415 	int s;
   1416 
   1417 	scsi_print_addr(sc_link);
   1418 	printf("timed out");
   1419 
   1420 	s = splbio();
   1421 
   1422 #ifdef BTDIAG
   1423 	/*
   1424 	 * If the ccb's mbx is not free, then the board has gone Far East?
   1425 	 */
   1426 	bt_collect_mbo(sc);
   1427 	if (ccb->flags & CCB_SENDING) {
   1428 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1429 		Debugger();
   1430 	}
   1431 #endif
   1432 
   1433 	/*
   1434 	 * If it has been through before, then
   1435 	 * a previous abort has failed, don't
   1436 	 * try abort again
   1437 	 */
   1438 	if (ccb->flags & CCB_ABORT) {
   1439 		/* abort timed out */
   1440 		printf(" AGAIN\n");
   1441 		/* XXX Must reset! */
   1442 	} else {
   1443 		/* abort the operation that has timed out */
   1444 		printf("\n");
   1445 		ccb->xs->error = XS_TIMEOUT;
   1446 		ccb->timeout = BT_ABORT_TIMEOUT;
   1447 		ccb->flags |= CCB_ABORT;
   1448 		bt_queue_ccb(sc, ccb);
   1449 	}
   1450 
   1451 	splx(s);
   1452 }
   1453