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