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