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