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