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