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