Home | History | Annotate | Line # | Download | only in ic
ciss.c revision 1.2.4.2
      1  1.2.4.2  simonb /*	$NetBSD: ciss.c,v 1.2.4.2 2006/04/22 11:38:55 simonb Exp $	*/
      2  1.2.4.2  simonb /*	$OpenBSD: ciss.c,v 1.14 2006/03/13 16:02:23 mickey Exp $	*/
      3  1.2.4.2  simonb 
      4  1.2.4.2  simonb /*
      5  1.2.4.2  simonb  * Copyright (c) 2005 Michael Shalayeff
      6  1.2.4.2  simonb  * All rights reserved.
      7  1.2.4.2  simonb  *
      8  1.2.4.2  simonb  * Permission to use, copy, modify, and distribute this software for any
      9  1.2.4.2  simonb  * purpose with or without fee is hereby granted, provided that the above
     10  1.2.4.2  simonb  * copyright notice and this permission notice appear in all copies.
     11  1.2.4.2  simonb  *
     12  1.2.4.2  simonb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  1.2.4.2  simonb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  1.2.4.2  simonb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  1.2.4.2  simonb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  1.2.4.2  simonb  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
     17  1.2.4.2  simonb  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     18  1.2.4.2  simonb  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  1.2.4.2  simonb  */
     20  1.2.4.2  simonb 
     21  1.2.4.2  simonb #include <sys/cdefs.h>
     22  1.2.4.2  simonb __KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.2.4.2 2006/04/22 11:38:55 simonb Exp $");
     23  1.2.4.2  simonb 
     24  1.2.4.2  simonb /* #define CISS_DEBUG */
     25  1.2.4.2  simonb 
     26  1.2.4.2  simonb #include <sys/param.h>
     27  1.2.4.2  simonb #include <sys/systm.h>
     28  1.2.4.2  simonb #include <sys/buf.h>
     29  1.2.4.2  simonb #include <sys/ioctl.h>
     30  1.2.4.2  simonb #include <sys/device.h>
     31  1.2.4.2  simonb #include <sys/kernel.h>
     32  1.2.4.2  simonb #include <sys/malloc.h>
     33  1.2.4.2  simonb #include <sys/proc.h>
     34  1.2.4.2  simonb #include <sys/kthread.h>
     35  1.2.4.2  simonb 
     36  1.2.4.2  simonb #include <uvm/uvm_extern.h>
     37  1.2.4.2  simonb 
     38  1.2.4.2  simonb #include <machine/bus.h>
     39  1.2.4.2  simonb 
     40  1.2.4.2  simonb #include <dev/scsipi/scsi_all.h>
     41  1.2.4.2  simonb #include <dev/scsipi/scsi_disk.h>
     42  1.2.4.2  simonb #include <dev/scsipi/scsiconf.h>
     43  1.2.4.2  simonb 
     44  1.2.4.2  simonb #include <dev/ic/cissreg.h>
     45  1.2.4.2  simonb #include <dev/ic/cissvar.h>
     46  1.2.4.2  simonb 
     47  1.2.4.2  simonb #ifdef CISS_DEBUG
     48  1.2.4.2  simonb #define	CISS_DPRINTF(m,a)	if (ciss_debug & (m)) printf a
     49  1.2.4.2  simonb #define	CISS_D_CMD	0x0001
     50  1.2.4.2  simonb #define	CISS_D_INTR	0x0002
     51  1.2.4.2  simonb #define	CISS_D_MISC	0x0004
     52  1.2.4.2  simonb #define	CISS_D_DMA	0x0008
     53  1.2.4.2  simonb #define	CISS_D_IOCTL	0x0010
     54  1.2.4.2  simonb #define	CISS_D_ERR	0x0020
     55  1.2.4.2  simonb int ciss_debug = 0
     56  1.2.4.2  simonb 	| CISS_D_CMD
     57  1.2.4.2  simonb 	| CISS_D_INTR
     58  1.2.4.2  simonb 	| CISS_D_MISC
     59  1.2.4.2  simonb 	| CISS_D_DMA
     60  1.2.4.2  simonb 	| CISS_D_IOCTL
     61  1.2.4.2  simonb 	| CISS_D_ERR
     62  1.2.4.2  simonb 	;
     63  1.2.4.2  simonb #else
     64  1.2.4.2  simonb #define	CISS_DPRINTF(m,a)	/* m, a */
     65  1.2.4.2  simonb #endif
     66  1.2.4.2  simonb 
     67  1.2.4.2  simonb static void	ciss_scsi_cmd(struct scsipi_channel *chan,
     68  1.2.4.2  simonb 			scsipi_adapter_req_t req, void *arg);
     69  1.2.4.2  simonb static int	ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
     70  1.2.4.2  simonb 	    caddr_t addr, int flag, struct proc *p);
     71  1.2.4.2  simonb static void	cissminphys(struct buf *bp);
     72  1.2.4.2  simonb 
     73  1.2.4.2  simonb #if 0
     74  1.2.4.2  simonb static void	ciss_scsi_raw_cmd(struct scsipi_channel *chan,
     75  1.2.4.2  simonb 			scsipi_adapter_req_t req, void *arg);
     76  1.2.4.2  simonb #endif
     77  1.2.4.2  simonb 
     78  1.2.4.2  simonb #if NBIO > 0
     79  1.2.4.2  simonb static int	ciss_ioctl(struct device *, u_long, caddr_t);
     80  1.2.4.2  simonb #endif
     81  1.2.4.2  simonb static int	ciss_sync(struct ciss_softc *sc);
     82  1.2.4.2  simonb static void	ciss_heartbeat(void *v);
     83  1.2.4.2  simonb static void	ciss_shutdown(void *v);
     84  1.2.4.2  simonb #if 0
     85  1.2.4.2  simonb static void	ciss_kthread(void *v);
     86  1.2.4.2  simonb #endif
     87  1.2.4.2  simonb 
     88  1.2.4.2  simonb static struct ciss_ccb *ciss_get_ccb(struct ciss_softc *sc);
     89  1.2.4.2  simonb static void	ciss_put_ccb(struct ciss_ccb *ccb);
     90  1.2.4.2  simonb static int	ciss_cmd(struct ciss_ccb *ccb, int flags, int wait);
     91  1.2.4.2  simonb static int	ciss_done(struct ciss_ccb *ccb);
     92  1.2.4.2  simonb static int	ciss_error(struct ciss_ccb *ccb);
     93  1.2.4.2  simonb static int	ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq);
     94  1.2.4.2  simonb static int	ciss_ldmap(struct ciss_softc *sc);
     95  1.2.4.2  simonb 
     96  1.2.4.2  simonb static struct ciss_ccb *
     97  1.2.4.2  simonb ciss_get_ccb(struct ciss_softc *sc)
     98  1.2.4.2  simonb {
     99  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    100  1.2.4.2  simonb 
    101  1.2.4.2  simonb 	if ((ccb = TAILQ_LAST(&sc->sc_free_ccb, ciss_queue_head))) {
    102  1.2.4.2  simonb 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_link);
    103  1.2.4.2  simonb 		ccb->ccb_state = CISS_CCB_READY;
    104  1.2.4.2  simonb 	}
    105  1.2.4.2  simonb 	return ccb;
    106  1.2.4.2  simonb }
    107  1.2.4.2  simonb 
    108  1.2.4.2  simonb static void
    109  1.2.4.2  simonb ciss_put_ccb(struct ciss_ccb *ccb)
    110  1.2.4.2  simonb {
    111  1.2.4.2  simonb 	struct ciss_softc *sc = ccb->ccb_sc;
    112  1.2.4.2  simonb 
    113  1.2.4.2  simonb 	ccb->ccb_state = CISS_CCB_FREE;
    114  1.2.4.2  simonb 	TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
    115  1.2.4.2  simonb }
    116  1.2.4.2  simonb 
    117  1.2.4.2  simonb int
    118  1.2.4.2  simonb ciss_attach(struct ciss_softc *sc)
    119  1.2.4.2  simonb {
    120  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    121  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    122  1.2.4.2  simonb 	struct ciss_inquiry *inq;
    123  1.2.4.2  simonb 	bus_dma_segment_t seg[1];
    124  1.2.4.2  simonb 	int error, i, total, rseg, maxfer;
    125  1.2.4.2  simonb 	ciss_lock_t lock;
    126  1.2.4.2  simonb 	paddr_t pa;
    127  1.2.4.2  simonb 
    128  1.2.4.2  simonb 	bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    129  1.2.4.2  simonb 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    130  1.2.4.2  simonb 
    131  1.2.4.2  simonb 	if (sc->cfg.signature != CISS_SIGNATURE) {
    132  1.2.4.2  simonb 		printf(": bad sign 0x%08x\n", sc->cfg.signature);
    133  1.2.4.2  simonb 		return -1;
    134  1.2.4.2  simonb 	}
    135  1.2.4.2  simonb 
    136  1.2.4.2  simonb 	if (!(sc->cfg.methods & CISS_METH_SIMPL)) {
    137  1.2.4.2  simonb 		printf(": not simple 0x%08x\n", sc->cfg.methods);
    138  1.2.4.2  simonb 		return -1;
    139  1.2.4.2  simonb 	}
    140  1.2.4.2  simonb 
    141  1.2.4.2  simonb 	sc->cfg.rmethod = CISS_METH_SIMPL;
    142  1.2.4.2  simonb 	sc->cfg.paddr_lim = 0;			/* 32bit addrs */
    143  1.2.4.2  simonb 	sc->cfg.int_delay = 0;			/* disable coalescing */
    144  1.2.4.2  simonb 	sc->cfg.int_count = 0;
    145  1.2.4.2  simonb 	strlcpy(sc->cfg.hostname, "HUMPPA", sizeof(sc->cfg.hostname));
    146  1.2.4.2  simonb 	sc->cfg.driverf |= CISS_DRV_PRF;	/* enable prefetch */
    147  1.2.4.2  simonb 	if (!sc->cfg.maxsg)
    148  1.2.4.2  simonb 		sc->cfg.maxsg = MAXPHYS / PAGE_SIZE + 1;
    149  1.2.4.2  simonb 
    150  1.2.4.2  simonb 	bus_space_write_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    151  1.2.4.2  simonb 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    152  1.2.4.2  simonb 	bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, sizeof(sc->cfg),
    153  1.2.4.2  simonb 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
    154  1.2.4.2  simonb 
    155  1.2.4.2  simonb 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IDB, CISS_IDB_CFG);
    156  1.2.4.2  simonb 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
    157  1.2.4.2  simonb 	    BUS_SPACE_BARRIER_WRITE);
    158  1.2.4.2  simonb 	for (i = 1000; i--; DELAY(1000)) {
    159  1.2.4.2  simonb 		/* XXX maybe IDB is really 64bit? - hp dl380 needs this */
    160  1.2.4.2  simonb 		(void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB + 4);
    161  1.2.4.2  simonb 		if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG))
    162  1.2.4.2  simonb 			break;
    163  1.2.4.2  simonb 		bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
    164  1.2.4.2  simonb 		    BUS_SPACE_BARRIER_READ);
    165  1.2.4.2  simonb 	}
    166  1.2.4.2  simonb 
    167  1.2.4.2  simonb 	if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG) {
    168  1.2.4.2  simonb 		printf(": cannot set config\n");
    169  1.2.4.2  simonb 		return -1;
    170  1.2.4.2  simonb 	}
    171  1.2.4.2  simonb 
    172  1.2.4.2  simonb 	bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    173  1.2.4.2  simonb 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    174  1.2.4.2  simonb 
    175  1.2.4.2  simonb 	if (!(sc->cfg.amethod & CISS_METH_SIMPL)) {
    176  1.2.4.2  simonb 		printf(": cannot simplify 0x%08x\n", sc->cfg.amethod);
    177  1.2.4.2  simonb 		return -1;
    178  1.2.4.2  simonb 	}
    179  1.2.4.2  simonb 
    180  1.2.4.2  simonb 	/* i'm ready for you and i hope you're ready for me */
    181  1.2.4.2  simonb 	for (i = 30000; i--; DELAY(1000)) {
    182  1.2.4.2  simonb 		if (bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    183  1.2.4.2  simonb 		    offsetof(struct ciss_config, amethod)) & CISS_METH_READY)
    184  1.2.4.2  simonb 			break;
    185  1.2.4.2  simonb 		bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    186  1.2.4.2  simonb 		    offsetof(struct ciss_config, amethod), 4,
    187  1.2.4.2  simonb 		    BUS_SPACE_BARRIER_READ);
    188  1.2.4.2  simonb 	}
    189  1.2.4.2  simonb 
    190  1.2.4.2  simonb 	if (!(bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    191  1.2.4.2  simonb 	    offsetof(struct ciss_config, amethod)) & CISS_METH_READY)) {
    192  1.2.4.2  simonb 		printf(": she never came ready for me 0x%08x\n",
    193  1.2.4.2  simonb 		    sc->cfg.amethod);
    194  1.2.4.2  simonb 		return -1;
    195  1.2.4.2  simonb 	}
    196  1.2.4.2  simonb 
    197  1.2.4.2  simonb 	sc->maxcmd = sc->cfg.maxcmd;
    198  1.2.4.2  simonb 	sc->maxsg = sc->cfg.maxsg;
    199  1.2.4.2  simonb 	if (sc->maxsg > MAXPHYS / PAGE_SIZE + 1)
    200  1.2.4.2  simonb 		sc->maxsg = MAXPHYS / PAGE_SIZE + 1;
    201  1.2.4.2  simonb 	i = sizeof(struct ciss_ccb) +
    202  1.2.4.2  simonb 	    sizeof(ccb->ccb_cmd.sgl[0]) * (sc->maxsg - 1);
    203  1.2.4.2  simonb 	for (sc->ccblen = 0x10; sc->ccblen < i; sc->ccblen <<= 1);
    204  1.2.4.2  simonb 
    205  1.2.4.2  simonb 	total = sc->ccblen * sc->maxcmd;
    206  1.2.4.2  simonb 	if ((error = bus_dmamem_alloc(sc->sc_dmat, total, PAGE_SIZE, 0,
    207  1.2.4.2  simonb 	    sc->cmdseg, 1, &rseg, BUS_DMA_NOWAIT))) {
    208  1.2.4.2  simonb 		printf(": cannot allocate CCBs (%d)\n", error);
    209  1.2.4.2  simonb 		return -1;
    210  1.2.4.2  simonb 	}
    211  1.2.4.2  simonb 
    212  1.2.4.2  simonb 	if ((error = bus_dmamem_map(sc->sc_dmat, sc->cmdseg, rseg, total,
    213  1.2.4.2  simonb 	    (caddr_t *)&sc->ccbs, BUS_DMA_NOWAIT))) {
    214  1.2.4.2  simonb 		printf(": cannot map CCBs (%d)\n", error);
    215  1.2.4.2  simonb 		return -1;
    216  1.2.4.2  simonb 	}
    217  1.2.4.2  simonb 	bzero(sc->ccbs, total);
    218  1.2.4.2  simonb 
    219  1.2.4.2  simonb 	if ((error = bus_dmamap_create(sc->sc_dmat, total, 1,
    220  1.2.4.2  simonb 	    total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->cmdmap))) {
    221  1.2.4.2  simonb 		printf(": cannot create CCBs dmamap (%d)\n", error);
    222  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    223  1.2.4.2  simonb 		return -1;
    224  1.2.4.2  simonb 	}
    225  1.2.4.2  simonb 
    226  1.2.4.2  simonb 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->cmdmap, sc->ccbs, total,
    227  1.2.4.2  simonb 	    NULL, BUS_DMA_NOWAIT))) {
    228  1.2.4.2  simonb 		printf(": cannot load CCBs dmamap (%d)\n", error);
    229  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    230  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    231  1.2.4.2  simonb 		return -1;
    232  1.2.4.2  simonb 	}
    233  1.2.4.2  simonb 
    234  1.2.4.2  simonb 	TAILQ_INIT(&sc->sc_ccbq);
    235  1.2.4.2  simonb 	TAILQ_INIT(&sc->sc_ccbdone);
    236  1.2.4.2  simonb 	TAILQ_INIT(&sc->sc_free_ccb);
    237  1.2.4.2  simonb 
    238  1.2.4.2  simonb 	maxfer = sc->maxsg * PAGE_SIZE;
    239  1.2.4.2  simonb 	for (i = 0; total > 0 && i < sc->maxcmd; i++, total -= sc->ccblen) {
    240  1.2.4.2  simonb 		ccb = (struct ciss_ccb *) (sc->ccbs + i * sc->ccblen);
    241  1.2.4.2  simonb 		cmd = &ccb->ccb_cmd;
    242  1.2.4.2  simonb 		pa = sc->cmdseg[0].ds_addr + i * sc->ccblen;
    243  1.2.4.2  simonb 
    244  1.2.4.2  simonb 		ccb->ccb_sc = sc;
    245  1.2.4.2  simonb 		ccb->ccb_cmdpa = pa + offsetof(struct ciss_ccb, ccb_cmd);
    246  1.2.4.2  simonb 		ccb->ccb_state = CISS_CCB_FREE;
    247  1.2.4.2  simonb 
    248  1.2.4.2  simonb 		cmd->id = htole32(i << 2);
    249  1.2.4.2  simonb 		cmd->id_hi = htole32(0);
    250  1.2.4.2  simonb 		cmd->sgin = sc->maxsg;
    251  1.2.4.2  simonb 		cmd->sglen = htole16((u_int16_t)cmd->sgin);
    252  1.2.4.2  simonb 		cmd->err_len = htole32(sizeof(ccb->ccb_err));
    253  1.2.4.2  simonb 		pa += offsetof(struct ciss_ccb, ccb_err);
    254  1.2.4.2  simonb 		cmd->err_pa = htole64((u_int64_t)pa);
    255  1.2.4.2  simonb 
    256  1.2.4.2  simonb 		if ((error = bus_dmamap_create(sc->sc_dmat, maxfer, sc->maxsg,
    257  1.2.4.2  simonb 		    maxfer, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    258  1.2.4.2  simonb 		    &ccb->ccb_dmamap)))
    259  1.2.4.2  simonb 			break;
    260  1.2.4.2  simonb 
    261  1.2.4.2  simonb 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
    262  1.2.4.2  simonb 	}
    263  1.2.4.2  simonb 
    264  1.2.4.2  simonb 	if (i < sc->maxcmd) {
    265  1.2.4.2  simonb 		printf(": cannot create ccb#%d dmamap (%d)\n", i, error);
    266  1.2.4.2  simonb 		if (i == 0) {
    267  1.2.4.2  simonb 			/* TODO leaking cmd's dmamaps and shitz */
    268  1.2.4.2  simonb 			bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    269  1.2.4.2  simonb 			bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    270  1.2.4.2  simonb 			return -1;
    271  1.2.4.2  simonb 		}
    272  1.2.4.2  simonb 	}
    273  1.2.4.2  simonb 
    274  1.2.4.2  simonb 	if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    275  1.2.4.2  simonb 	    seg, 1, &rseg, BUS_DMA_NOWAIT))) {
    276  1.2.4.2  simonb 		printf(": cannot allocate scratch buffer (%d)\n", error);
    277  1.2.4.2  simonb 		return -1;
    278  1.2.4.2  simonb 	}
    279  1.2.4.2  simonb 
    280  1.2.4.2  simonb 	if ((error = bus_dmamem_map(sc->sc_dmat, seg, rseg, PAGE_SIZE,
    281  1.2.4.2  simonb 	    (caddr_t *)&sc->scratch, BUS_DMA_NOWAIT))) {
    282  1.2.4.2  simonb 		printf(": cannot map scratch buffer (%d)\n", error);
    283  1.2.4.2  simonb 		return -1;
    284  1.2.4.2  simonb 	}
    285  1.2.4.2  simonb 	bzero(sc->scratch, PAGE_SIZE);
    286  1.2.4.2  simonb 
    287  1.2.4.2  simonb 	lock = CISS_LOCK_SCRATCH(sc);
    288  1.2.4.2  simonb 	inq = sc->scratch;
    289  1.2.4.2  simonb 	if (ciss_inq(sc, inq)) {
    290  1.2.4.2  simonb 		printf(": adapter inquiry failed\n");
    291  1.2.4.2  simonb 		CISS_UNLOCK_SCRATCH(sc, lock);
    292  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    293  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    294  1.2.4.2  simonb 		return -1;
    295  1.2.4.2  simonb 	}
    296  1.2.4.2  simonb 
    297  1.2.4.2  simonb 	if (!(inq->flags & CISS_INQ_BIGMAP)) {
    298  1.2.4.2  simonb 		printf(": big map is not supported, flags=0x%x\n",
    299  1.2.4.2  simonb 		    inq->flags);
    300  1.2.4.2  simonb 		CISS_UNLOCK_SCRATCH(sc, lock);
    301  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    302  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    303  1.2.4.2  simonb 		return -1;
    304  1.2.4.2  simonb 	}
    305  1.2.4.2  simonb 
    306  1.2.4.2  simonb 	sc->maxunits = inq->numld;
    307  1.2.4.2  simonb 	sc->nbus = inq->nscsi_bus;
    308  1.2.4.2  simonb 	sc->ndrives = inq->buswidth;
    309  1.2.4.2  simonb 	printf(": %d LD%s, HW rev %d, FW %4.4s/%4.4s\n",
    310  1.2.4.2  simonb 	    inq->numld, inq->numld == 1? "" : "s",
    311  1.2.4.2  simonb 	    inq->hw_rev, inq->fw_running, inq->fw_stored);
    312  1.2.4.2  simonb 
    313  1.2.4.2  simonb 	CISS_UNLOCK_SCRATCH(sc, lock);
    314  1.2.4.2  simonb 
    315  1.2.4.2  simonb 	callout_init(&sc->sc_hb);
    316  1.2.4.2  simonb 	callout_setfunc(&sc->sc_hb, ciss_heartbeat, sc);
    317  1.2.4.2  simonb 	callout_schedule(&sc->sc_hb, hz * 3);
    318  1.2.4.2  simonb 
    319  1.2.4.2  simonb 	/* map LDs */
    320  1.2.4.2  simonb 	if (ciss_ldmap(sc)) {
    321  1.2.4.2  simonb 		printf("%s: adapter LD map failed\n", sc->sc_dev.dv_xname);
    322  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    323  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    324  1.2.4.2  simonb 		return -1;
    325  1.2.4.2  simonb 	}
    326  1.2.4.2  simonb 
    327  1.2.4.2  simonb /* TODO scan all physdev */
    328  1.2.4.2  simonb /* TODO scan all logdev */
    329  1.2.4.2  simonb 
    330  1.2.4.2  simonb 	sc->sc_flush = CISS_FLUSH_ENABLE;
    331  1.2.4.2  simonb 	if (!(sc->sc_sh = shutdownhook_establish(ciss_shutdown, sc))) {
    332  1.2.4.2  simonb 		printf(": unable to establish shutdown hook\n");
    333  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    334  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    335  1.2.4.2  simonb 		return -1;
    336  1.2.4.2  simonb 	}
    337  1.2.4.2  simonb 
    338  1.2.4.2  simonb #if 0
    339  1.2.4.2  simonb 	if (kthread_create(ciss_kthread, sc, NULL, "%s", sc->sc_dev.dv_xname)) {
    340  1.2.4.2  simonb 		printf(": unable to create kernel thread\n");
    341  1.2.4.2  simonb 		shutdownhook_disestablish(sc->sc_sh);
    342  1.2.4.2  simonb 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    343  1.2.4.2  simonb 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    344  1.2.4.2  simonb 		return -1;
    345  1.2.4.2  simonb 	}
    346  1.2.4.2  simonb #endif
    347  1.2.4.2  simonb 
    348  1.2.4.2  simonb 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    349  1.2.4.2  simonb 	sc->sc_channel.chan_bustype = &scsi_bustype;
    350  1.2.4.2  simonb 	sc->sc_channel.chan_channel = 0;
    351  1.2.4.2  simonb 	sc->sc_channel.chan_ntargets = sc->maxunits;
    352  1.2.4.2  simonb 	sc->sc_channel.chan_nluns = 8;
    353  1.2.4.2  simonb 	sc->sc_channel.chan_openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
    354  1.2.4.2  simonb 	sc->sc_channel.chan_flags = 0;
    355  1.2.4.2  simonb 	sc->sc_channel.chan_id = sc->maxunits;
    356  1.2.4.2  simonb 
    357  1.2.4.2  simonb 	sc->sc_adapter.adapt_dev = (struct device *) sc;
    358  1.2.4.2  simonb 	sc->sc_adapter.adapt_openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
    359  1.2.4.2  simonb 	sc->sc_adapter.adapt_max_periph = sc->maxunits;
    360  1.2.4.2  simonb 	sc->sc_adapter.adapt_request = ciss_scsi_cmd;
    361  1.2.4.2  simonb 	sc->sc_adapter.adapt_minphys = cissminphys;
    362  1.2.4.2  simonb 	sc->sc_adapter.adapt_ioctl = ciss_scsi_ioctl;
    363  1.2.4.2  simonb 	sc->sc_adapter.adapt_nchannels = 1;
    364  1.2.4.2  simonb 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    365  1.2.4.2  simonb 
    366  1.2.4.2  simonb #if 0
    367  1.2.4.2  simonb 	sc->sc_link_raw.adapter_softc = sc;
    368  1.2.4.2  simonb 	sc->sc_link.openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
    369  1.2.4.2  simonb 	sc->sc_link_raw.adapter = &ciss_raw_switch;
    370  1.2.4.2  simonb 	sc->sc_link_raw.adapter_target = sc->ndrives;
    371  1.2.4.2  simonb 	sc->sc_link_raw.adapter_buswidth = sc->ndrives;
    372  1.2.4.2  simonb 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    373  1.2.4.2  simonb #endif
    374  1.2.4.2  simonb 
    375  1.2.4.2  simonb #if NBIO1 > 0
    376  1.2.4.2  simonb 	if (bio_register(&sc->sc_dev, ciss_ioctl) != 0)
    377  1.2.4.2  simonb 		printf("%s: controller registration failed",
    378  1.2.4.2  simonb 		    sc->sc_dev.dv_xname);
    379  1.2.4.2  simonb #endif
    380  1.2.4.2  simonb 
    381  1.2.4.2  simonb 	return 0;
    382  1.2.4.2  simonb }
    383  1.2.4.2  simonb 
    384  1.2.4.2  simonb static void
    385  1.2.4.2  simonb ciss_shutdown(void *v)
    386  1.2.4.2  simonb {
    387  1.2.4.2  simonb 	struct ciss_softc *sc = v;
    388  1.2.4.2  simonb 
    389  1.2.4.2  simonb 	sc->sc_flush = CISS_FLUSH_DISABLE;
    390  1.2.4.2  simonb 	/* timeout_del(&sc->sc_hb); */
    391  1.2.4.2  simonb 	ciss_sync(sc);
    392  1.2.4.2  simonb }
    393  1.2.4.2  simonb 
    394  1.2.4.2  simonb static void
    395  1.2.4.2  simonb cissminphys(struct buf *bp)
    396  1.2.4.2  simonb {
    397  1.2.4.2  simonb #if 0	/* TOSO */
    398  1.2.4.2  simonb #define	CISS_MAXFER	(PAGE_SIZE * (sc->maxsg + 1))
    399  1.2.4.2  simonb 	if (bp->b_bcount > CISS_MAXFER)
    400  1.2.4.2  simonb 		bp->b_bcount = CISS_MAXFER;
    401  1.2.4.2  simonb #endif
    402  1.2.4.2  simonb 	minphys(bp);
    403  1.2.4.2  simonb }
    404  1.2.4.2  simonb 
    405  1.2.4.2  simonb /*
    406  1.2.4.2  simonb  * submit a command and optionally wait for completition.
    407  1.2.4.2  simonb  * wait arg abuses XS_CTL_POLL|XS_CTL_NOSLEEP flags to request
    408  1.2.4.2  simonb  * to wait (XS_CTL_POLL) and to allow tsleep() (!XS_CTL_NOSLEEP)
    409  1.2.4.2  simonb  * instead of busy loop waiting
    410  1.2.4.2  simonb  */
    411  1.2.4.2  simonb static int
    412  1.2.4.2  simonb ciss_cmd(struct ciss_ccb *ccb, int flags, int wait)
    413  1.2.4.2  simonb {
    414  1.2.4.2  simonb 	struct ciss_softc *sc = ccb->ccb_sc;
    415  1.2.4.2  simonb 	struct ciss_cmd *cmd = &ccb->ccb_cmd;
    416  1.2.4.2  simonb 	struct ciss_ccb *ccb1;
    417  1.2.4.2  simonb 	bus_dmamap_t dmap = ccb->ccb_dmamap;
    418  1.2.4.2  simonb 	u_int32_t id;
    419  1.2.4.2  simonb 	int i, tohz, error = 0;
    420  1.2.4.2  simonb 
    421  1.2.4.2  simonb 	if (ccb->ccb_state != CISS_CCB_READY) {
    422  1.2.4.2  simonb 		printf("%s: ccb %d not ready state=0x%x\n", sc->sc_dev.dv_xname,
    423  1.2.4.2  simonb 		    cmd->id, ccb->ccb_state);
    424  1.2.4.2  simonb 		return (EINVAL);
    425  1.2.4.2  simonb 	}
    426  1.2.4.2  simonb 
    427  1.2.4.2  simonb 	if (ccb->ccb_data) {
    428  1.2.4.2  simonb 		bus_dma_segment_t *sgd;
    429  1.2.4.2  simonb 
    430  1.2.4.2  simonb 		if ((error = bus_dmamap_load(sc->sc_dmat, dmap, ccb->ccb_data,
    431  1.2.4.2  simonb 		    ccb->ccb_len, NULL, flags))) {
    432  1.2.4.2  simonb 			if (error == EFBIG)
    433  1.2.4.2  simonb 				printf("more than %d dma segs\n", sc->maxsg);
    434  1.2.4.2  simonb 			else
    435  1.2.4.2  simonb 				printf("error %d loading dma map\n", error);
    436  1.2.4.2  simonb 			ciss_put_ccb(ccb);
    437  1.2.4.2  simonb 			return (error);
    438  1.2.4.2  simonb 		}
    439  1.2.4.2  simonb 		cmd->sgin = dmap->dm_nsegs;
    440  1.2.4.2  simonb 
    441  1.2.4.2  simonb 		sgd = dmap->dm_segs;
    442  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_DMA, ("data=%p/%u<0x%lx/%lu",
    443  1.2.4.2  simonb 		    ccb->ccb_data, ccb->ccb_len, sgd->ds_addr, sgd->ds_len));
    444  1.2.4.2  simonb 
    445  1.2.4.2  simonb 		for (i = 0; i < dmap->dm_nsegs; sgd++, i++) {
    446  1.2.4.2  simonb 			cmd->sgl[i].addr_lo = htole32(sgd->ds_addr);
    447  1.2.4.2  simonb 			cmd->sgl[i].addr_hi =
    448  1.2.4.2  simonb 			    htole32((u_int64_t)sgd->ds_addr >> 32);
    449  1.2.4.2  simonb 			cmd->sgl[i].len = htole32(sgd->ds_len);
    450  1.2.4.2  simonb 			cmd->sgl[i].flags = htole32(0);
    451  1.2.4.2  simonb 			if (i)
    452  1.2.4.2  simonb 				CISS_DPRINTF(CISS_D_DMA,
    453  1.2.4.2  simonb 				    (",0x%lx/%lu", sgd->ds_addr, sgd->ds_len));
    454  1.2.4.2  simonb 		}
    455  1.2.4.2  simonb 
    456  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_DMA, ("> "));
    457  1.2.4.2  simonb 
    458  1.2.4.2  simonb 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    459  1.2.4.2  simonb 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    460  1.2.4.2  simonb 	} else
    461  1.2.4.2  simonb 		cmd->sgin = 0;
    462  1.2.4.2  simonb 	cmd->sglen = htole16((u_int16_t)cmd->sgin);
    463  1.2.4.2  simonb 	bzero(&ccb->ccb_err, sizeof(ccb->ccb_err));
    464  1.2.4.2  simonb 
    465  1.2.4.2  simonb 	bus_dmamap_sync(sc->sc_dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize,
    466  1.2.4.2  simonb 	    BUS_DMASYNC_PREWRITE);
    467  1.2.4.2  simonb 
    468  1.2.4.2  simonb 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    469  1.2.4.2  simonb 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    470  1.2.4.2  simonb 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem);
    471  1.2.4.2  simonb 
    472  1.2.4.2  simonb 	TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link);
    473  1.2.4.2  simonb 	ccb->ccb_state = CISS_CCB_ONQ;
    474  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x ", cmd->id));
    475  1.2.4.2  simonb 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ, ccb->ccb_cmdpa);
    476  1.2.4.2  simonb 
    477  1.2.4.2  simonb 	if (wait & XS_CTL_POLL) {
    478  1.2.4.2  simonb 		int etick;
    479  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, ("waiting "));
    480  1.2.4.2  simonb 
    481  1.2.4.2  simonb 		i = ccb->ccb_xs? ccb->ccb_xs->timeout : 60000;
    482  1.2.4.2  simonb 		tohz = (i / 1000) * hz + (i % 1000) * (hz / 1000);
    483  1.2.4.2  simonb 		if (tohz == 0)
    484  1.2.4.2  simonb 			tohz = 1;
    485  1.2.4.2  simonb 		for (i *= 100, etick = tick + tohz; i--; ) {
    486  1.2.4.2  simonb 			if (!(wait & XS_CTL_NOSLEEP)) {
    487  1.2.4.2  simonb 				ccb->ccb_state = CISS_CCB_POLL;
    488  1.2.4.2  simonb 				CISS_DPRINTF(CISS_D_CMD, ("tsleep(%d) ", tohz));
    489  1.2.4.2  simonb 				if (tsleep(ccb, PRIBIO + 1, "ciss_cmd",
    490  1.2.4.2  simonb 				    tohz) == EWOULDBLOCK) {
    491  1.2.4.2  simonb 					break;
    492  1.2.4.2  simonb 				}
    493  1.2.4.2  simonb 				if (ccb->ccb_state != CISS_CCB_ONQ) {
    494  1.2.4.2  simonb 					tohz = etick - tick;
    495  1.2.4.2  simonb 					if (tohz <= 0)
    496  1.2.4.2  simonb 						break;
    497  1.2.4.2  simonb 					CISS_DPRINTF(CISS_D_CMD, ("T"));
    498  1.2.4.2  simonb 					continue;
    499  1.2.4.2  simonb 				}
    500  1.2.4.2  simonb 				ccb1 = ccb;
    501  1.2.4.2  simonb 			} else {
    502  1.2.4.2  simonb 				DELAY(10);
    503  1.2.4.2  simonb 
    504  1.2.4.2  simonb 				if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    505  1.2.4.2  simonb 				    CISS_ISR) & sc->iem)) {
    506  1.2.4.2  simonb 					CISS_DPRINTF(CISS_D_CMD, ("N"));
    507  1.2.4.2  simonb 					continue;
    508  1.2.4.2  simonb 				}
    509  1.2.4.2  simonb 
    510  1.2.4.2  simonb 				if ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    511  1.2.4.2  simonb 				    CISS_OUTQ)) == 0xffffffff) {
    512  1.2.4.2  simonb 					CISS_DPRINTF(CISS_D_CMD, ("Q"));
    513  1.2.4.2  simonb 					continue;
    514  1.2.4.2  simonb 				}
    515  1.2.4.2  simonb 
    516  1.2.4.2  simonb 				CISS_DPRINTF(CISS_D_CMD, ("got=0x%x ", id));
    517  1.2.4.2  simonb 				ccb1 = (struct ciss_ccb *)
    518  1.2.4.2  simonb 					(sc->ccbs + (id >> 2) * sc->ccblen);
    519  1.2.4.2  simonb 				ccb1->ccb_cmd.id = htole32(id);
    520  1.2.4.2  simonb 			}
    521  1.2.4.2  simonb 
    522  1.2.4.2  simonb 			error = ciss_done(ccb1);
    523  1.2.4.2  simonb 			if (ccb1 == ccb)
    524  1.2.4.2  simonb 				break;
    525  1.2.4.2  simonb 		}
    526  1.2.4.2  simonb 
    527  1.2.4.2  simonb 		/* if never got a chance to be done above... */
    528  1.2.4.2  simonb 		if (ccb->ccb_state != CISS_CCB_FREE) {
    529  1.2.4.2  simonb 			ccb->ccb_err.cmd_stat = CISS_ERR_TMO;
    530  1.2.4.2  simonb 			error = ciss_done(ccb);
    531  1.2.4.2  simonb 		}
    532  1.2.4.2  simonb 
    533  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, ("done %d:%d",
    534  1.2.4.2  simonb 		    ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat));
    535  1.2.4.2  simonb 	}
    536  1.2.4.2  simonb 
    537  1.2.4.2  simonb 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    538  1.2.4.2  simonb 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    539  1.2.4.2  simonb 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
    540  1.2.4.2  simonb 
    541  1.2.4.2  simonb 	return (error);
    542  1.2.4.2  simonb }
    543  1.2.4.2  simonb 
    544  1.2.4.2  simonb static int
    545  1.2.4.2  simonb ciss_done(struct ciss_ccb *ccb)
    546  1.2.4.2  simonb {
    547  1.2.4.2  simonb 	struct ciss_softc *sc = ccb->ccb_sc;
    548  1.2.4.2  simonb 	struct scsipi_xfer *xs = ccb->ccb_xs;
    549  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    550  1.2.4.2  simonb 	ciss_lock_t lock;
    551  1.2.4.2  simonb 	int error = 0;
    552  1.2.4.2  simonb 
    553  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) ", ccb));
    554  1.2.4.2  simonb 
    555  1.2.4.2  simonb 	if (ccb->ccb_state != CISS_CCB_ONQ) {
    556  1.2.4.2  simonb 		printf("%s: unqueued ccb %p ready, state=0x%x\n",
    557  1.2.4.2  simonb 		    sc->sc_dev.dv_xname, ccb, ccb->ccb_state);
    558  1.2.4.2  simonb 		return 1;
    559  1.2.4.2  simonb 	}
    560  1.2.4.2  simonb 
    561  1.2.4.2  simonb 	lock = CISS_LOCK(sc);
    562  1.2.4.2  simonb 	ccb->ccb_state = CISS_CCB_READY;
    563  1.2.4.2  simonb 	TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
    564  1.2.4.2  simonb 
    565  1.2.4.2  simonb 	if (ccb->ccb_cmd.id & CISS_CMD_ERR)
    566  1.2.4.2  simonb 		error = ciss_error(ccb);
    567  1.2.4.2  simonb 
    568  1.2.4.2  simonb 	cmd = &ccb->ccb_cmd;
    569  1.2.4.2  simonb 	if (ccb->ccb_data) {
    570  1.2.4.2  simonb 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
    571  1.2.4.2  simonb 		    ccb->ccb_dmamap->dm_mapsize, (cmd->flags & CISS_CDB_IN) ?
    572  1.2.4.2  simonb 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    573  1.2.4.2  simonb 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
    574  1.2.4.2  simonb 		ccb->ccb_xs = NULL;
    575  1.2.4.2  simonb 		ccb->ccb_data = NULL;
    576  1.2.4.2  simonb 	}
    577  1.2.4.2  simonb 
    578  1.2.4.2  simonb 	ciss_put_ccb(ccb);
    579  1.2.4.2  simonb 
    580  1.2.4.2  simonb 	if (xs) {
    581  1.2.4.2  simonb 		xs->resid = 0;
    582  1.2.4.2  simonb 		xs->xs_status |= XS_STS_DONE;
    583  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, ("scsipi_done(%p) ", xs));
    584  1.2.4.2  simonb 		scsipi_done(xs);
    585  1.2.4.2  simonb 	}
    586  1.2.4.2  simonb 	CISS_UNLOCK(sc, lock);
    587  1.2.4.2  simonb 
    588  1.2.4.2  simonb 	return error;
    589  1.2.4.2  simonb }
    590  1.2.4.2  simonb 
    591  1.2.4.2  simonb static int
    592  1.2.4.2  simonb ciss_error(struct ciss_ccb *ccb)
    593  1.2.4.2  simonb {
    594  1.2.4.2  simonb 	struct ciss_softc *sc = ccb->ccb_sc;
    595  1.2.4.2  simonb 	struct ciss_error *err = &ccb->ccb_err;
    596  1.2.4.2  simonb 	struct scsipi_xfer *xs = ccb->ccb_xs;
    597  1.2.4.2  simonb 	int rv;
    598  1.2.4.2  simonb 
    599  1.2.4.2  simonb 	switch ((rv = le16toh(err->cmd_stat))) {
    600  1.2.4.2  simonb 	case CISS_ERR_OK:
    601  1.2.4.2  simonb 		break;
    602  1.2.4.2  simonb 
    603  1.2.4.2  simonb 	case CISS_ERR_INVCMD:
    604  1.2.4.2  simonb 		printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n",
    605  1.2.4.2  simonb 		    sc->sc_dev.dv_xname, ccb->ccb_cmd.id,
    606  1.2.4.2  simonb 		    err->err_info, err->err_type[3], err->err_type[2]);
    607  1.2.4.2  simonb 		if (xs) {
    608  1.2.4.2  simonb 			bzero(&xs->sense, sizeof(xs->sense));
    609  1.2.4.2  simonb 			xs->sense.scsi_sense.response_code =
    610  1.2.4.2  simonb 				SSD_RCODE_CURRENT | SSD_RCODE_VALID;
    611  1.2.4.2  simonb 			xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
    612  1.2.4.2  simonb 			xs->sense.scsi_sense.asc = 0x24; /* ill field */
    613  1.2.4.2  simonb 			xs->sense.scsi_sense.ascq = 0x0;
    614  1.2.4.2  simonb 			xs->error = XS_SENSE;
    615  1.2.4.2  simonb 		}
    616  1.2.4.2  simonb 		break;
    617  1.2.4.2  simonb 
    618  1.2.4.2  simonb 	case CISS_ERR_TMO:
    619  1.2.4.2  simonb 		xs->error = XS_TIMEOUT;
    620  1.2.4.2  simonb 		break;
    621  1.2.4.2  simonb 
    622  1.2.4.2  simonb 	case CISS_ERR_UNRUN:
    623  1.2.4.2  simonb 		/* Underrun */
    624  1.2.4.2  simonb 		xs->resid = le32toh(err->resid);
    625  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, (" underrun resid=0x%x ",
    626  1.2.4.2  simonb 					  xs->resid));
    627  1.2.4.2  simonb 		break;
    628  1.2.4.2  simonb 	default:
    629  1.2.4.2  simonb 		if (xs) {
    630  1.2.4.2  simonb 			CISS_DPRINTF(CISS_D_CMD, ("scsi_stat=%x ", err->scsi_stat));
    631  1.2.4.2  simonb 			switch (err->scsi_stat) {
    632  1.2.4.2  simonb 			case SCSI_CHECK:
    633  1.2.4.2  simonb 				xs->error = XS_SENSE;
    634  1.2.4.2  simonb 				bcopy(&err->sense[0], &xs->sense,
    635  1.2.4.2  simonb 				    sizeof(xs->sense));
    636  1.2.4.2  simonb 				CISS_DPRINTF(CISS_D_CMD, (" sense=%02x %02x %02x %02x ",
    637  1.2.4.2  simonb 					     err->sense[0], err->sense[1], err->sense[2], err->sense[3]));
    638  1.2.4.2  simonb 				break;
    639  1.2.4.2  simonb 
    640  1.2.4.2  simonb 			case XS_BUSY:
    641  1.2.4.2  simonb 				xs->error = XS_BUSY;
    642  1.2.4.2  simonb 				break;
    643  1.2.4.2  simonb 
    644  1.2.4.2  simonb 			default:
    645  1.2.4.2  simonb 				CISS_DPRINTF(CISS_D_ERR, ("%s: "
    646  1.2.4.2  simonb 				    "cmd_stat=%x scsi_stat=0x%x resid=0x%x\n",
    647  1.2.4.2  simonb 				    sc->sc_dev.dv_xname, rv, err->scsi_stat,
    648  1.2.4.2  simonb 				    le32toh(err->resid)));
    649  1.2.4.2  simonb 				printf("ciss driver stuffup in %s:%d: %s()\n",
    650  1.2.4.2  simonb 				       __FILE__, __LINE__, __FUNCTION__);
    651  1.2.4.2  simonb 				xs->error = XS_DRIVER_STUFFUP;
    652  1.2.4.2  simonb 				break;
    653  1.2.4.2  simonb 			}
    654  1.2.4.2  simonb 			xs->resid = le32toh(err->resid);
    655  1.2.4.2  simonb 		}
    656  1.2.4.2  simonb 	}
    657  1.2.4.2  simonb 	ccb->ccb_cmd.id &= htole32(~3);
    658  1.2.4.2  simonb 
    659  1.2.4.2  simonb 	return rv;
    660  1.2.4.2  simonb }
    661  1.2.4.2  simonb 
    662  1.2.4.2  simonb static int
    663  1.2.4.2  simonb ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq)
    664  1.2.4.2  simonb {
    665  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    666  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    667  1.2.4.2  simonb 
    668  1.2.4.2  simonb 	ccb = ciss_get_ccb(sc);
    669  1.2.4.2  simonb 	ccb->ccb_len = sizeof(*inq);
    670  1.2.4.2  simonb 	ccb->ccb_data = inq;
    671  1.2.4.2  simonb 	ccb->ccb_xs = NULL;
    672  1.2.4.2  simonb 	cmd = &ccb->ccb_cmd;
    673  1.2.4.2  simonb 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    674  1.2.4.2  simonb 	cmd->tgt2 = 0;
    675  1.2.4.2  simonb 	cmd->cdblen = 10;
    676  1.2.4.2  simonb 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    677  1.2.4.2  simonb 	cmd->tmo = htole16(0);
    678  1.2.4.2  simonb 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    679  1.2.4.2  simonb 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    680  1.2.4.2  simonb 	cmd->cdb[6] = CISS_CMS_CTRL_CTRL;
    681  1.2.4.2  simonb 	cmd->cdb[7] = sizeof(*inq) >> 8;	/* biiiig endian */
    682  1.2.4.2  simonb 	cmd->cdb[8] = sizeof(*inq) & 0xff;
    683  1.2.4.2  simonb 
    684  1.2.4.2  simonb 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    685  1.2.4.2  simonb }
    686  1.2.4.2  simonb 
    687  1.2.4.2  simonb static int
    688  1.2.4.2  simonb ciss_ldmap(struct ciss_softc *sc)
    689  1.2.4.2  simonb {
    690  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    691  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    692  1.2.4.2  simonb 	struct ciss_ldmap *lmap;
    693  1.2.4.2  simonb 	ciss_lock_t lock;
    694  1.2.4.2  simonb 	int total, rv;
    695  1.2.4.2  simonb 
    696  1.2.4.2  simonb 	lock = CISS_LOCK_SCRATCH(sc);
    697  1.2.4.2  simonb 	lmap = sc->scratch;
    698  1.2.4.2  simonb 	lmap->size = htobe32(sc->maxunits * sizeof(lmap->map));
    699  1.2.4.2  simonb 	total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map);
    700  1.2.4.2  simonb 
    701  1.2.4.2  simonb 	ccb = ciss_get_ccb(sc);
    702  1.2.4.2  simonb 	ccb->ccb_len = total;
    703  1.2.4.2  simonb 	ccb->ccb_data = lmap;
    704  1.2.4.2  simonb 	ccb->ccb_xs = NULL;
    705  1.2.4.2  simonb 	cmd = &ccb->ccb_cmd;
    706  1.2.4.2  simonb 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    707  1.2.4.2  simonb 	cmd->tgt2 = 0;
    708  1.2.4.2  simonb 	cmd->cdblen = 12;
    709  1.2.4.2  simonb 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    710  1.2.4.2  simonb 	cmd->tmo = htole16(30);
    711  1.2.4.2  simonb 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    712  1.2.4.2  simonb 	cmd->cdb[0] = CISS_CMD_LDMAP;
    713  1.2.4.2  simonb 	cmd->cdb[8] = total >> 8;	/* biiiig endian */
    714  1.2.4.2  simonb 	cmd->cdb[9] = total & 0xff;
    715  1.2.4.2  simonb 
    716  1.2.4.2  simonb 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    717  1.2.4.2  simonb 	CISS_UNLOCK_SCRATCH(sc, lock);
    718  1.2.4.2  simonb 
    719  1.2.4.2  simonb 	if (rv)
    720  1.2.4.2  simonb 		return rv;
    721  1.2.4.2  simonb 
    722  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n",
    723  1.2.4.2  simonb 	    lmap->map[0].tgt, lmap->map[0].tgt2));
    724  1.2.4.2  simonb 
    725  1.2.4.2  simonb 	return 0;
    726  1.2.4.2  simonb }
    727  1.2.4.2  simonb 
    728  1.2.4.2  simonb static int
    729  1.2.4.2  simonb ciss_sync(struct ciss_softc *sc)
    730  1.2.4.2  simonb {
    731  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    732  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    733  1.2.4.2  simonb 	struct ciss_flush *flush;
    734  1.2.4.2  simonb 	ciss_lock_t lock;
    735  1.2.4.2  simonb 	int rv;
    736  1.2.4.2  simonb 
    737  1.2.4.2  simonb 	lock = CISS_LOCK_SCRATCH(sc);
    738  1.2.4.2  simonb 	flush = sc->scratch;
    739  1.2.4.2  simonb 	bzero(flush, sizeof(*flush));
    740  1.2.4.2  simonb 	flush->flush = sc->sc_flush;
    741  1.2.4.2  simonb 
    742  1.2.4.2  simonb 	ccb = ciss_get_ccb(sc);
    743  1.2.4.2  simonb 	ccb->ccb_len = sizeof(*flush);
    744  1.2.4.2  simonb 	ccb->ccb_data = flush;
    745  1.2.4.2  simonb 	ccb->ccb_xs = NULL;
    746  1.2.4.2  simonb 	cmd = &ccb->ccb_cmd;
    747  1.2.4.2  simonb 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    748  1.2.4.2  simonb 	cmd->tgt2 = 0;
    749  1.2.4.2  simonb 	cmd->cdblen = 10;
    750  1.2.4.2  simonb 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
    751  1.2.4.2  simonb 	cmd->tmo = 0;
    752  1.2.4.2  simonb 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    753  1.2.4.2  simonb 	cmd->cdb[0] = CISS_CMD_CTRL_SET;
    754  1.2.4.2  simonb 	cmd->cdb[6] = CISS_CMS_CTRL_FLUSH;
    755  1.2.4.2  simonb 	cmd->cdb[7] = sizeof(*flush) >> 8;	/* biiiig endian */
    756  1.2.4.2  simonb 	cmd->cdb[8] = sizeof(*flush) & 0xff;
    757  1.2.4.2  simonb 
    758  1.2.4.2  simonb 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    759  1.2.4.2  simonb 	CISS_UNLOCK_SCRATCH(sc, lock);
    760  1.2.4.2  simonb 
    761  1.2.4.2  simonb 	return rv;
    762  1.2.4.2  simonb }
    763  1.2.4.2  simonb 
    764  1.2.4.2  simonb #if 0
    765  1.2.4.2  simonb static void
    766  1.2.4.2  simonb ciss_scsi_raw_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    767  1.2.4.2  simonb 	void *arg)				/* TODO */
    768  1.2.4.2  simonb {
    769  1.2.4.2  simonb 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    770  1.2.4.2  simonb 	struct ciss_rawsoftc *rsc =
    771  1.2.4.2  simonb 		(struct ciss_rawsoftc *) chan->chan_adapter->adapt_dev;
    772  1.2.4.2  simonb 	struct ciss_softc *sc = rsc->sc_softc;
    773  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    774  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    775  1.2.4.2  simonb 	ciss_lock_t lock;
    776  1.2.4.2  simonb 	int error;
    777  1.2.4.2  simonb 
    778  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_raw_cmd "));
    779  1.2.4.2  simonb 
    780  1.2.4.2  simonb 	switch (req)
    781  1.2.4.2  simonb 	{
    782  1.2.4.2  simonb 	case ADAPTER_REQ_RUN_XFER:
    783  1.2.4.2  simonb 		if (xs->cmdlen > CISS_MAX_CDB) {
    784  1.2.4.2  simonb 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
    785  1.2.4.2  simonb 			bzero(&xs->sense, sizeof(xs->sense));
    786  1.2.4.2  simonb 			printf("ciss driver stuffup in %s:%d: %s()\n",
    787  1.2.4.2  simonb 			       __FILE__, __LINE__, __FUNCTION__);
    788  1.2.4.2  simonb 			xs->error = XS_DRIVER_STUFFUP;
    789  1.2.4.2  simonb 			scsipi_done(xs);
    790  1.2.4.2  simonb 			break;
    791  1.2.4.2  simonb 		}
    792  1.2.4.2  simonb 
    793  1.2.4.2  simonb 		lock = CISS_LOCK(sc);
    794  1.2.4.2  simonb 		error = 0;
    795  1.2.4.2  simonb 		xs->error = XS_NOERROR;
    796  1.2.4.2  simonb 
    797  1.2.4.2  simonb 		/* TODO check this target has not yet employed w/ any volume */
    798  1.2.4.2  simonb 
    799  1.2.4.2  simonb 		ccb = ciss_get_ccb(sc);
    800  1.2.4.2  simonb 		cmd = &ccb->ccb_cmd;
    801  1.2.4.2  simonb 		ccb->ccb_len = xs->datalen;
    802  1.2.4.2  simonb 		ccb->ccb_data = xs->data;
    803  1.2.4.2  simonb 		ccb->ccb_xs = xs;
    804  1.2.4.2  simonb 
    805  1.2.4.2  simonb 		cmd->cdblen = xs->cmdlen;
    806  1.2.4.2  simonb 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
    807  1.2.4.2  simonb 		if (xs->xs_control & XS_CTL_DATA_IN)
    808  1.2.4.2  simonb 			cmd->flags |= CISS_CDB_IN;
    809  1.2.4.2  simonb 		else if (xs->xs_control & XS_CTL_DATA_OUT)
    810  1.2.4.2  simonb 			cmd->flags |= CISS_CDB_OUT;
    811  1.2.4.2  simonb 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
    812  1.2.4.2  simonb 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    813  1.2.4.2  simonb 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
    814  1.2.4.2  simonb 
    815  1.2.4.2  simonb 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
    816  1.2.4.2  simonb 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
    817  1.2.4.2  simonb 			printf("ciss driver stuffup in %s:%d: %s()\n",
    818  1.2.4.2  simonb 			       __FILE__, __LINE__, __FUNCTION__);
    819  1.2.4.2  simonb 			xs->error = XS_DRIVER_STUFFUP;
    820  1.2.4.2  simonb 			scsipi_done(xs);
    821  1.2.4.2  simonb 			CISS_UNLOCK(sc, lock);
    822  1.2.4.2  simonb 			break;
    823  1.2.4.2  simonb 		}
    824  1.2.4.2  simonb 
    825  1.2.4.2  simonb 		CISS_UNLOCK(sc, lock);
    826  1.2.4.2  simonb 		break;
    827  1.2.4.2  simonb 
    828  1.2.4.2  simonb 	case ADAPTER_REQ_GROW_RESOURCES:
    829  1.2.4.2  simonb 		/*
    830  1.2.4.2  simonb 		 * Not supported.
    831  1.2.4.2  simonb 		 */
    832  1.2.4.2  simonb 		break;
    833  1.2.4.2  simonb 
    834  1.2.4.2  simonb 	case ADAPTER_REQ_SET_XFER_MODE:
    835  1.2.4.2  simonb 		/*
    836  1.2.4.2  simonb 		 * We can't change the transfer mode, but at least let
    837  1.2.4.2  simonb 		 * scsipi know what the adapter has negociated.
    838  1.2.4.2  simonb 		 */
    839  1.2.4.2  simonb 		 /* Get xfer mode and return it */
    840  1.2.4.2  simonb 		break;
    841  1.2.4.2  simonb 	}
    842  1.2.4.2  simonb }
    843  1.2.4.2  simonb #endif
    844  1.2.4.2  simonb 
    845  1.2.4.2  simonb static void
    846  1.2.4.2  simonb ciss_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    847  1.2.4.2  simonb 	void *arg)
    848  1.2.4.2  simonb {
    849  1.2.4.2  simonb 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    850  1.2.4.2  simonb 	struct ciss_softc *sc =
    851  1.2.4.2  simonb 		(struct ciss_softc *) chan->chan_adapter->adapt_dev;
    852  1.2.4.2  simonb 	u_int8_t target;
    853  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    854  1.2.4.2  simonb 	struct ciss_cmd *cmd;
    855  1.2.4.2  simonb 	int error;
    856  1.2.4.2  simonb 	ciss_lock_t lock;
    857  1.2.4.2  simonb 
    858  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd "));
    859  1.2.4.2  simonb 
    860  1.2.4.2  simonb 	switch (req)
    861  1.2.4.2  simonb 	{
    862  1.2.4.2  simonb 	case ADAPTER_REQ_RUN_XFER:
    863  1.2.4.2  simonb 		target = xs->xs_periph->periph_target;
    864  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, ("targ=%d ", target));
    865  1.2.4.2  simonb 		if (xs->cmdlen > CISS_MAX_CDB) {
    866  1.2.4.2  simonb 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
    867  1.2.4.2  simonb 			bzero(&xs->sense, sizeof(xs->sense));
    868  1.2.4.2  simonb 			printf("ciss driver stuffup in %s:%d: %s()\n",
    869  1.2.4.2  simonb 			       __FILE__, __LINE__, __FUNCTION__);
    870  1.2.4.2  simonb 			xs->error = XS_DRIVER_STUFFUP;
    871  1.2.4.2  simonb 			scsipi_done(xs);
    872  1.2.4.2  simonb 			break;
    873  1.2.4.2  simonb 		}
    874  1.2.4.2  simonb 
    875  1.2.4.2  simonb 		lock = CISS_LOCK(sc);
    876  1.2.4.2  simonb 		error = 0;
    877  1.2.4.2  simonb 		xs->error = XS_NOERROR;
    878  1.2.4.2  simonb 
    879  1.2.4.2  simonb 		/* XXX emulate SYNCHRONIZE_CACHE ??? */
    880  1.2.4.2  simonb 
    881  1.2.4.2  simonb 		ccb = ciss_get_ccb(sc);
    882  1.2.4.2  simonb 		cmd = &ccb->ccb_cmd;
    883  1.2.4.2  simonb 		ccb->ccb_len = xs->datalen;
    884  1.2.4.2  simonb 		ccb->ccb_data = xs->data;
    885  1.2.4.2  simonb 		ccb->ccb_xs = xs;
    886  1.2.4.2  simonb 		cmd->tgt = CISS_CMD_MODE_LD | target;
    887  1.2.4.2  simonb 		cmd->tgt2 = 0;
    888  1.2.4.2  simonb 		cmd->cdblen = xs->cmdlen;
    889  1.2.4.2  simonb 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
    890  1.2.4.2  simonb 		if (xs->xs_control & XS_CTL_DATA_IN)
    891  1.2.4.2  simonb 			cmd->flags |= CISS_CDB_IN;
    892  1.2.4.2  simonb 		else if (xs->xs_control & XS_CTL_DATA_OUT)
    893  1.2.4.2  simonb 			cmd->flags |= CISS_CDB_OUT;
    894  1.2.4.2  simonb 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
    895  1.2.4.2  simonb 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    896  1.2.4.2  simonb 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
    897  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_CMD, ("cmd=%02x %02x %02x %02x %02x %02x ",
    898  1.2.4.2  simonb 			     cmd->cdb[0], cmd->cdb[1], cmd->cdb[2],
    899  1.2.4.2  simonb 			     cmd->cdb[3], cmd->cdb[4], cmd->cdb[5]));
    900  1.2.4.2  simonb 
    901  1.2.4.2  simonb 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
    902  1.2.4.2  simonb 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
    903  1.2.4.2  simonb 			printf("ciss driver stuffup in %s:%d: %s()\n",
    904  1.2.4.2  simonb 			       __FILE__, __LINE__, __FUNCTION__);
    905  1.2.4.2  simonb 			xs->error = XS_DRIVER_STUFFUP;
    906  1.2.4.2  simonb 			scsipi_done(xs);
    907  1.2.4.2  simonb 			CISS_UNLOCK(sc, lock);
    908  1.2.4.2  simonb 			return;
    909  1.2.4.2  simonb 		}
    910  1.2.4.2  simonb 
    911  1.2.4.2  simonb 		CISS_UNLOCK(sc, lock);
    912  1.2.4.2  simonb 		break;
    913  1.2.4.2  simonb 	case ADAPTER_REQ_GROW_RESOURCES:
    914  1.2.4.2  simonb 		/*
    915  1.2.4.2  simonb 		 * Not supported.
    916  1.2.4.2  simonb 		 */
    917  1.2.4.2  simonb 		break;
    918  1.2.4.2  simonb 	case ADAPTER_REQ_SET_XFER_MODE:
    919  1.2.4.2  simonb 		/*
    920  1.2.4.2  simonb 		 * We can't change the transfer mode, but at least let
    921  1.2.4.2  simonb 		 * scsipi know what the adapter has negociated.
    922  1.2.4.2  simonb 		 */
    923  1.2.4.2  simonb 		/* FIXME: get xfer mode and write it into arg */
    924  1.2.4.2  simonb 		break;
    925  1.2.4.2  simonb 	}
    926  1.2.4.2  simonb }
    927  1.2.4.2  simonb 
    928  1.2.4.2  simonb int
    929  1.2.4.2  simonb ciss_intr(void *v)
    930  1.2.4.2  simonb {
    931  1.2.4.2  simonb 	struct ciss_softc *sc = v;
    932  1.2.4.2  simonb 	struct ciss_ccb *ccb;
    933  1.2.4.2  simonb 	ciss_lock_t lock;
    934  1.2.4.2  simonb 	u_int32_t id;
    935  1.2.4.2  simonb 	int hit = 0;
    936  1.2.4.2  simonb 
    937  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_INTR, ("intr "));
    938  1.2.4.2  simonb 
    939  1.2.4.2  simonb 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem))
    940  1.2.4.2  simonb 		return 0;
    941  1.2.4.2  simonb 
    942  1.2.4.2  simonb 	lock = CISS_LOCK(sc);
    943  1.2.4.2  simonb 	while ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ)) !=
    944  1.2.4.2  simonb 	    0xffffffff) {
    945  1.2.4.2  simonb 
    946  1.2.4.2  simonb 		ccb = (struct ciss_ccb *) (sc->ccbs + (id >> 2) * sc->ccblen);
    947  1.2.4.2  simonb 		ccb->ccb_cmd.id = htole32(id);
    948  1.2.4.2  simonb 		if (ccb->ccb_state == CISS_CCB_POLL) {
    949  1.2.4.2  simonb 			ccb->ccb_state = CISS_CCB_ONQ;
    950  1.2.4.2  simonb 			wakeup(ccb);
    951  1.2.4.2  simonb 		} else
    952  1.2.4.2  simonb 			ciss_done(ccb);
    953  1.2.4.2  simonb 
    954  1.2.4.2  simonb 		hit = 1;
    955  1.2.4.2  simonb 	}
    956  1.2.4.2  simonb 	CISS_UNLOCK(sc, lock);
    957  1.2.4.2  simonb 
    958  1.2.4.2  simonb 	CISS_DPRINTF(CISS_D_INTR, ("exit\n"));
    959  1.2.4.2  simonb 	return hit;
    960  1.2.4.2  simonb }
    961  1.2.4.2  simonb 
    962  1.2.4.2  simonb static void
    963  1.2.4.2  simonb ciss_heartbeat(void *v)
    964  1.2.4.2  simonb {
    965  1.2.4.2  simonb 	struct ciss_softc *sc = v;
    966  1.2.4.2  simonb 	u_int32_t hb;
    967  1.2.4.2  simonb 
    968  1.2.4.2  simonb 	hb = bus_space_read_4(sc->sc_iot, sc->cfg_ioh,
    969  1.2.4.2  simonb 	    sc->cfgoff + offsetof(struct ciss_config, heartbeat));
    970  1.2.4.2  simonb 	if (hb == sc->heartbeat)
    971  1.2.4.2  simonb 		panic("ciss: dead");	/* XX reset! */
    972  1.2.4.2  simonb 	else
    973  1.2.4.2  simonb 		sc->heartbeat = hb;
    974  1.2.4.2  simonb 
    975  1.2.4.2  simonb 	callout_schedule(&sc->sc_hb, hz * 3);
    976  1.2.4.2  simonb }
    977  1.2.4.2  simonb 
    978  1.2.4.2  simonb #if 0
    979  1.2.4.2  simonb static void
    980  1.2.4.2  simonb ciss_kthread(void *v)
    981  1.2.4.2  simonb {
    982  1.2.4.2  simonb 	struct ciss_softc *sc = v;
    983  1.2.4.2  simonb 	ciss_lock_t lock;
    984  1.2.4.2  simonb 
    985  1.2.4.2  simonb 	for (;;) {
    986  1.2.4.2  simonb 		tsleep(sc, PRIBIO, sc->sc_dev.dv_xname, 0);
    987  1.2.4.2  simonb 
    988  1.2.4.2  simonb 		lock = CISS_LOCK(sc);
    989  1.2.4.2  simonb 
    990  1.2.4.2  simonb 
    991  1.2.4.2  simonb 
    992  1.2.4.2  simonb 		CISS_UNLOCK(sc, lock);
    993  1.2.4.2  simonb 	}
    994  1.2.4.2  simonb }
    995  1.2.4.2  simonb #endif
    996  1.2.4.2  simonb 
    997  1.2.4.2  simonb static int
    998  1.2.4.2  simonb ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
    999  1.2.4.2  simonb     caddr_t addr, int flag, struct proc *p)
   1000  1.2.4.2  simonb {
   1001  1.2.4.2  simonb #if NBIO > 0
   1002  1.2.4.2  simonb 	return ciss_ioctl(chan->chan_adapter->adapt_dev, cmd, addr);
   1003  1.2.4.2  simonb #else
   1004  1.2.4.2  simonb 	return ENOTTY;
   1005  1.2.4.2  simonb #endif
   1006  1.2.4.2  simonb }
   1007  1.2.4.2  simonb 
   1008  1.2.4.2  simonb #if NBIO > 0
   1009  1.2.4.2  simonb static int
   1010  1.2.4.2  simonb ciss_ioctl(struct device *dev, u_long cmd, caddr_t addr)	/* TODO */
   1011  1.2.4.2  simonb {
   1012  1.2.4.2  simonb 	/* struct ciss_softc *sc = (struct ciss_softc *)dev; */
   1013  1.2.4.2  simonb 	ciss_lock_t lock;
   1014  1.2.4.2  simonb 	int error;
   1015  1.2.4.2  simonb 
   1016  1.2.4.2  simonb 	lock = CISS_LOCK(sc);
   1017  1.2.4.2  simonb 	switch (cmd) {
   1018  1.2.4.2  simonb 	case BIOCINQ:
   1019  1.2.4.2  simonb 	case BIOCVOL:
   1020  1.2.4.2  simonb 	case BIOCDISK:
   1021  1.2.4.2  simonb 	case BIOCALARM:
   1022  1.2.4.2  simonb 	case BIOCBLINK:
   1023  1.2.4.2  simonb 	case BIOCSETSTATE:
   1024  1.2.4.2  simonb 	default:
   1025  1.2.4.2  simonb 		CISS_DPRINTF(CISS_D_IOCTL, ("%s: invalid ioctl\n",
   1026  1.2.4.2  simonb 		    sc->sc_dev.dv_xname));
   1027  1.2.4.2  simonb 		error = ENOTTY;
   1028  1.2.4.2  simonb 	}
   1029  1.2.4.2  simonb 	CISS_UNLOCK(sc, lock);
   1030  1.2.4.2  simonb 
   1031  1.2.4.2  simonb 	return error;
   1032  1.2.4.2  simonb }
   1033  1.2.4.2  simonb #endif
   1034