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