Home | History | Annotate | Line # | Download | only in ic
ciss.c revision 1.1
      1  1.1  he /*	$NetBSD: ciss.c,v 1.1 2006/03/21 20:42:14 he Exp $	*/
      2  1.1  he /*	$OpenBSD: ciss.c,v 1.13 2006/02/02 22:13:04 brad 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.1  he __KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.1 2006/03/21 20:42:14 he 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.1  he #include <machine/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.1  he 	    caddr_t 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.1  he static int	ciss_ioctl(struct device *, u_long, caddr_t);
     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.1  he 	    (caddr_t *)&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.1  he 		ccb = (struct ciss_ccb *) (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.1  he 	    (caddr_t *)&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.1  he 	callout_init(&sc->sc_hb);
    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.1  he }
    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.1  he 			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.1  he 		}
    455  1.1  he 
    456  1.1  he 		CISS_DPRINTF(CISS_D_DMA, ("> "));
    457  1.1  he 
    458  1.1  he 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    459  1.1  he 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    460  1.1  he 	} else
    461  1.1  he 		cmd->sgin = 0;
    462  1.1  he 	cmd->sglen = htole16((u_int16_t)cmd->sgin);
    463  1.1  he 	bzero(&ccb->ccb_err, sizeof(ccb->ccb_err));
    464  1.1  he 
    465  1.1  he 	bus_dmamap_sync(sc->sc_dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize,
    466  1.1  he 	    BUS_DMASYNC_PREWRITE);
    467  1.1  he 
    468  1.1  he 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    469  1.1  he 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    470  1.1  he 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem);
    471  1.1  he 
    472  1.1  he 	TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link);
    473  1.1  he 	ccb->ccb_state = CISS_CCB_ONQ;
    474  1.1  he 	CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x ", cmd->id));
    475  1.1  he 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ, ccb->ccb_cmdpa);
    476  1.1  he 
    477  1.1  he 	if (wait & XS_CTL_POLL) {
    478  1.1  he 		int etick;
    479  1.1  he 		CISS_DPRINTF(CISS_D_CMD, ("waiting "));
    480  1.1  he 
    481  1.1  he 		i = ccb->ccb_xs? ccb->ccb_xs->timeout : 60000;
    482  1.1  he 		tohz = (i / 1000) * hz + (i % 1000) * (hz / 1000);
    483  1.1  he 		if (tohz == 0)
    484  1.1  he 			tohz = 1;
    485  1.1  he 		for (i *= 100, etick = tick + tohz; i--; ) {
    486  1.1  he 			if (!(wait & XS_CTL_NOSLEEP)) {
    487  1.1  he 				ccb->ccb_state = CISS_CCB_POLL;
    488  1.1  he 				CISS_DPRINTF(CISS_D_CMD, ("tsleep(%d) ", tohz));
    489  1.1  he 				if (tsleep(ccb, PRIBIO + 1, "ciss_cmd",
    490  1.1  he 				    tohz) == EWOULDBLOCK) {
    491  1.1  he 					break;
    492  1.1  he 				}
    493  1.1  he 				if (ccb->ccb_state != CISS_CCB_ONQ) {
    494  1.1  he 					tohz = etick - tick;
    495  1.1  he 					if (tohz <= 0)
    496  1.1  he 						break;
    497  1.1  he 					CISS_DPRINTF(CISS_D_CMD, ("T"));
    498  1.1  he 					continue;
    499  1.1  he 				}
    500  1.1  he 				ccb1 = ccb;
    501  1.1  he 			} else {
    502  1.1  he 				DELAY(10);
    503  1.1  he 
    504  1.1  he 				if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    505  1.1  he 				    CISS_ISR) & sc->iem)) {
    506  1.1  he 					CISS_DPRINTF(CISS_D_CMD, ("N"));
    507  1.1  he 					continue;
    508  1.1  he 				}
    509  1.1  he 
    510  1.1  he 				if ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    511  1.1  he 				    CISS_OUTQ)) == 0xffffffff) {
    512  1.1  he 					CISS_DPRINTF(CISS_D_CMD, ("Q"));
    513  1.1  he 					continue;
    514  1.1  he 				}
    515  1.1  he 
    516  1.1  he 				CISS_DPRINTF(CISS_D_CMD, ("got=0x%x ", id));
    517  1.1  he 				ccb1 = (struct ciss_ccb *)
    518  1.1  he 					(sc->ccbs + (id >> 2) * sc->ccblen);
    519  1.1  he 				ccb1->ccb_cmd.id = htole32(id);
    520  1.1  he 			}
    521  1.1  he 
    522  1.1  he 			error = ciss_done(ccb1);
    523  1.1  he 			if (ccb1 == ccb)
    524  1.1  he 				break;
    525  1.1  he 		}
    526  1.1  he 
    527  1.1  he 		/* if never got a chance to be done above... */
    528  1.1  he 		if (ccb->ccb_state != CISS_CCB_FREE) {
    529  1.1  he 			ccb->ccb_err.cmd_stat = CISS_ERR_TMO;
    530  1.1  he 			error = ciss_done(ccb);
    531  1.1  he 		}
    532  1.1  he 
    533  1.1  he 		CISS_DPRINTF(CISS_D_CMD, ("done %d:%d",
    534  1.1  he 		    ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat));
    535  1.1  he 	}
    536  1.1  he 
    537  1.1  he 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    538  1.1  he 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    539  1.1  he 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
    540  1.1  he 
    541  1.1  he 	return (error);
    542  1.1  he }
    543  1.1  he 
    544  1.1  he static int
    545  1.1  he ciss_done(struct ciss_ccb *ccb)
    546  1.1  he {
    547  1.1  he 	struct ciss_softc *sc = ccb->ccb_sc;
    548  1.1  he 	struct scsipi_xfer *xs = ccb->ccb_xs;
    549  1.1  he 	ciss_lock_t lock;
    550  1.1  he 	int error = 0;
    551  1.1  he 
    552  1.1  he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) ", ccb));
    553  1.1  he 
    554  1.1  he 	if (ccb->ccb_state != CISS_CCB_ONQ) {
    555  1.1  he 		printf("%s: unqueued ccb %p ready, state=0x%x\n",
    556  1.1  he 		    sc->sc_dev.dv_xname, ccb, ccb->ccb_state);
    557  1.1  he 		return 1;
    558  1.1  he 	}
    559  1.1  he 
    560  1.1  he 	lock = CISS_LOCK(sc);
    561  1.1  he 	ccb->ccb_state = CISS_CCB_READY;
    562  1.1  he 	TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
    563  1.1  he 
    564  1.1  he 	if (ccb->ccb_cmd.id & CISS_CMD_ERR)
    565  1.1  he 		error = ciss_error(ccb);
    566  1.1  he 
    567  1.1  he 	if (ccb->ccb_data) {
    568  1.1  he 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
    569  1.1  he 		    ccb->ccb_dmamap->dm_mapsize,
    570  1.1  he 		    (xs  && xs->xs_control & XS_CTL_DATA_IN) ?
    571  1.1  he 		     BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    572  1.1  he 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
    573  1.1  he 		ccb->ccb_xs = NULL;
    574  1.1  he 		ccb->ccb_data = NULL;
    575  1.1  he 	}
    576  1.1  he 
    577  1.1  he 	ciss_put_ccb(ccb);
    578  1.1  he 
    579  1.1  he 	if (xs) {
    580  1.1  he 		xs->resid = 0;
    581  1.1  he 		xs->xs_status |= XS_STS_DONE;
    582  1.1  he 		CISS_DPRINTF(CISS_D_CMD, ("scsipi_done(%p) ", xs));
    583  1.1  he 		scsipi_done(xs);
    584  1.1  he 	}
    585  1.1  he 	CISS_UNLOCK(sc, lock);
    586  1.1  he 
    587  1.1  he 	return error;
    588  1.1  he }
    589  1.1  he 
    590  1.1  he static int
    591  1.1  he ciss_error(struct ciss_ccb *ccb)
    592  1.1  he {
    593  1.1  he 	struct ciss_softc *sc = ccb->ccb_sc;
    594  1.1  he 	struct ciss_error *err = &ccb->ccb_err;
    595  1.1  he 	struct scsipi_xfer *xs = ccb->ccb_xs;
    596  1.1  he 	int rv;
    597  1.1  he 
    598  1.1  he 	switch ((rv = le16toh(err->cmd_stat))) {
    599  1.1  he 	case CISS_ERR_OK:
    600  1.1  he 		break;
    601  1.1  he 
    602  1.1  he 	case CISS_ERR_INVCMD:
    603  1.1  he 		printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n",
    604  1.1  he 		    sc->sc_dev.dv_xname, ccb->ccb_cmd.id,
    605  1.1  he 		    err->err_info, err->err_type[3], err->err_type[2]);
    606  1.1  he 		if (xs) {
    607  1.1  he 			bzero(&xs->sense, sizeof(xs->sense));
    608  1.1  he 			xs->sense.scsi_sense.response_code =
    609  1.1  he 				SSD_RCODE_CURRENT | SSD_RCODE_VALID;
    610  1.1  he 			xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
    611  1.1  he 			xs->sense.scsi_sense.asc = 0x24; /* ill field */
    612  1.1  he 			xs->sense.scsi_sense.ascq = 0x0;
    613  1.1  he 			xs->error = XS_SENSE;
    614  1.1  he 		}
    615  1.1  he 		break;
    616  1.1  he 
    617  1.1  he 	case CISS_ERR_TMO:
    618  1.1  he 		xs->error = XS_TIMEOUT;
    619  1.1  he 		break;
    620  1.1  he 
    621  1.1  he 	case CISS_ERR_UNRUN:
    622  1.1  he 		/* Underrun */
    623  1.1  he 		xs->resid = le32toh(err->resid);
    624  1.1  he 		CISS_DPRINTF(CISS_D_CMD, (" underrun resid=0x%x ",
    625  1.1  he 					  xs->resid));
    626  1.1  he 		break;
    627  1.1  he 	default:
    628  1.1  he 		if (xs) {
    629  1.1  he 			CISS_DPRINTF(CISS_D_CMD, ("scsi_stat=%x ", err->scsi_stat));
    630  1.1  he 			switch (err->scsi_stat) {
    631  1.1  he 			case SCSI_CHECK:
    632  1.1  he 				xs->error = XS_SENSE;
    633  1.1  he 				bcopy(&err->sense[0], &xs->sense,
    634  1.1  he 				    sizeof(xs->sense));
    635  1.1  he 				CISS_DPRINTF(CISS_D_CMD, (" sense=%02x %02x %02x %02x ",
    636  1.1  he 					     err->sense[0], err->sense[1], err->sense[2], err->sense[3]));
    637  1.1  he 				break;
    638  1.1  he 
    639  1.1  he 			case XS_BUSY:
    640  1.1  he 				xs->error = XS_BUSY;
    641  1.1  he 				break;
    642  1.1  he 
    643  1.1  he 			default:
    644  1.1  he 				CISS_DPRINTF(CISS_D_ERR, ("%s: "
    645  1.1  he 				    "cmd_stat=%x scsi_stat=0x%x resid=0x%x\n",
    646  1.1  he 				    sc->sc_dev.dv_xname, rv, err->scsi_stat,
    647  1.1  he 				    le32toh(err->resid)));
    648  1.1  he 				printf("ciss driver stuffup in %s:%d: %s()\n",
    649  1.1  he 				       __FILE__, __LINE__, __FUNCTION__);
    650  1.1  he 				xs->error = XS_DRIVER_STUFFUP;
    651  1.1  he 				break;
    652  1.1  he 			}
    653  1.1  he 			xs->resid = le32toh(err->resid);
    654  1.1  he 		}
    655  1.1  he 	}
    656  1.1  he 	ccb->ccb_cmd.id &= htole32(~3);
    657  1.1  he 
    658  1.1  he 	return rv;
    659  1.1  he }
    660  1.1  he 
    661  1.1  he static int
    662  1.1  he ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq)
    663  1.1  he {
    664  1.1  he 	struct ciss_ccb *ccb;
    665  1.1  he 	struct ciss_cmd *cmd;
    666  1.1  he 
    667  1.1  he 	ccb = ciss_get_ccb(sc);
    668  1.1  he 	ccb->ccb_len = sizeof(*inq);
    669  1.1  he 	ccb->ccb_data = inq;
    670  1.1  he 	cmd = &ccb->ccb_cmd;
    671  1.1  he 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    672  1.1  he 	cmd->tgt2 = 0;
    673  1.1  he 	cmd->cdblen = 10;
    674  1.1  he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    675  1.1  he 	cmd->tmo = htole16(0);
    676  1.1  he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    677  1.1  he 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    678  1.1  he 	cmd->cdb[6] = CISS_CMS_CTRL_CTRL;
    679  1.1  he 	cmd->cdb[7] = sizeof(*inq) >> 8;	/* biiiig endian */
    680  1.1  he 	cmd->cdb[8] = sizeof(*inq) & 0xff;
    681  1.1  he 
    682  1.1  he 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    683  1.1  he }
    684  1.1  he 
    685  1.1  he static int
    686  1.1  he ciss_ldmap(struct ciss_softc *sc)
    687  1.1  he {
    688  1.1  he 	struct ciss_ccb *ccb;
    689  1.1  he 	struct ciss_cmd *cmd;
    690  1.1  he 	struct ciss_ldmap *lmap;
    691  1.1  he 	ciss_lock_t lock;
    692  1.1  he 	int total, rv;
    693  1.1  he 
    694  1.1  he 	lock = CISS_LOCK_SCRATCH(sc);
    695  1.1  he 	lmap = sc->scratch;
    696  1.1  he 	lmap->size = htobe32(sc->maxunits * sizeof(lmap->map));
    697  1.1  he 	total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map);
    698  1.1  he 
    699  1.1  he 	ccb = ciss_get_ccb(sc);
    700  1.1  he 	ccb->ccb_len = total;
    701  1.1  he 	ccb->ccb_data = lmap;
    702  1.1  he 	cmd = &ccb->ccb_cmd;
    703  1.1  he 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    704  1.1  he 	cmd->tgt2 = 0;
    705  1.1  he 	cmd->cdblen = 12;
    706  1.1  he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    707  1.1  he 	cmd->tmo = htole16(30);
    708  1.1  he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    709  1.1  he 	cmd->cdb[0] = CISS_CMD_LDMAP;
    710  1.1  he 	cmd->cdb[8] = total >> 8;	/* biiiig endian */
    711  1.1  he 	cmd->cdb[9] = total & 0xff;
    712  1.1  he 
    713  1.1  he 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    714  1.1  he 	CISS_UNLOCK_SCRATCH(sc, lock);
    715  1.1  he 
    716  1.1  he 	if (rv)
    717  1.1  he 		return rv;
    718  1.1  he 
    719  1.1  he 	CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n",
    720  1.1  he 	    lmap->map[0].tgt, lmap->map[0].tgt2));
    721  1.1  he 
    722  1.1  he 	return 0;
    723  1.1  he }
    724  1.1  he 
    725  1.1  he static int
    726  1.1  he ciss_sync(struct ciss_softc *sc)
    727  1.1  he {
    728  1.1  he 	struct ciss_ccb *ccb;
    729  1.1  he 	struct ciss_cmd *cmd;
    730  1.1  he 	struct ciss_flush *flush;
    731  1.1  he 	ciss_lock_t lock;
    732  1.1  he 	int rv;
    733  1.1  he 
    734  1.1  he 	lock = CISS_LOCK_SCRATCH(sc);
    735  1.1  he 	flush = sc->scratch;
    736  1.1  he 	bzero(flush, sizeof(*flush));
    737  1.1  he 	flush->flush = sc->sc_flush;
    738  1.1  he 
    739  1.1  he 	ccb = ciss_get_ccb(sc);
    740  1.1  he 	ccb->ccb_len = sizeof(*flush);
    741  1.1  he 	ccb->ccb_data = flush;
    742  1.1  he 	cmd = &ccb->ccb_cmd;
    743  1.1  he 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    744  1.1  he 	cmd->tgt2 = 0;
    745  1.1  he 	cmd->cdblen = 10;
    746  1.1  he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
    747  1.1  he 	cmd->tmo = 0;
    748  1.1  he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    749  1.1  he 	cmd->cdb[0] = CISS_CMD_CTRL_SET;
    750  1.1  he 	cmd->cdb[6] = CISS_CMS_CTRL_FLUSH;
    751  1.1  he 	cmd->cdb[7] = sizeof(*flush) >> 8;	/* biiiig endian */
    752  1.1  he 	cmd->cdb[8] = sizeof(*flush) & 0xff;
    753  1.1  he 
    754  1.1  he 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    755  1.1  he 	CISS_UNLOCK_SCRATCH(sc, lock);
    756  1.1  he 
    757  1.1  he 	return rv;
    758  1.1  he }
    759  1.1  he 
    760  1.1  he #if 0
    761  1.1  he static void
    762  1.1  he ciss_scsi_raw_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    763  1.1  he 	void *arg)				/* TODO */
    764  1.1  he {
    765  1.1  he 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    766  1.1  he 	struct ciss_rawsoftc *rsc =
    767  1.1  he 		(struct ciss_rawsoftc *) chan->chan_adapter->adapt_dev;
    768  1.1  he 	struct ciss_softc *sc = rsc->sc_softc;
    769  1.1  he 	struct ciss_ccb *ccb;
    770  1.1  he 	struct ciss_cmd *cmd;
    771  1.1  he 	ciss_lock_t lock;
    772  1.1  he 	int error;
    773  1.1  he 
    774  1.1  he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_raw_cmd "));
    775  1.1  he 
    776  1.1  he 	switch (req)
    777  1.1  he 	{
    778  1.1  he 	case ADAPTER_REQ_RUN_XFER:
    779  1.1  he 		if (xs->cmdlen > CISS_MAX_CDB) {
    780  1.1  he 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
    781  1.1  he 			bzero(&xs->sense, sizeof(xs->sense));
    782  1.1  he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    783  1.1  he 			       __FILE__, __LINE__, __FUNCTION__);
    784  1.1  he 			xs->error = XS_DRIVER_STUFFUP;
    785  1.1  he 			scsipi_done(xs);
    786  1.1  he 			break;
    787  1.1  he 		}
    788  1.1  he 
    789  1.1  he 		lock = CISS_LOCK(sc);
    790  1.1  he 		error = 0;
    791  1.1  he 		xs->error = XS_NOERROR;
    792  1.1  he 
    793  1.1  he 		/* TODO check this target has not yet employed w/ any volume */
    794  1.1  he 
    795  1.1  he 		ccb = ciss_get_ccb(sc);
    796  1.1  he 		cmd = &ccb->ccb_cmd;
    797  1.1  he 		ccb->ccb_len = xs->datalen;
    798  1.1  he 		ccb->ccb_data = xs->data;
    799  1.1  he 		ccb->ccb_xs = xs;
    800  1.1  he 
    801  1.1  he 		cmd->cdblen = xs->cmdlen;
    802  1.1  he 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
    803  1.1  he 		if (xs->xs_control & XS_CTL_DATA_IN)
    804  1.1  he 			cmd->flags |= CISS_CDB_IN;
    805  1.1  he 		else if (xs->xs_control & XS_CTL_DATA_OUT)
    806  1.1  he 			cmd->flags |= CISS_CDB_OUT;
    807  1.1  he 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
    808  1.1  he 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    809  1.1  he 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
    810  1.1  he 
    811  1.1  he 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
    812  1.1  he 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
    813  1.1  he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    814  1.1  he 			       __FILE__, __LINE__, __FUNCTION__);
    815  1.1  he 			xs->error = XS_DRIVER_STUFFUP;
    816  1.1  he 			scsipi_done(xs);
    817  1.1  he 			CISS_UNLOCK(sc, lock);
    818  1.1  he 			break;
    819  1.1  he 		}
    820  1.1  he 
    821  1.1  he 		CISS_UNLOCK(sc, lock);
    822  1.1  he 		break;
    823  1.1  he 
    824  1.1  he 	case ADAPTER_REQ_GROW_RESOURCES:
    825  1.1  he 		/*
    826  1.1  he 		 * Not supported.
    827  1.1  he 		 */
    828  1.1  he 		break;
    829  1.1  he 
    830  1.1  he 	case ADAPTER_REQ_SET_XFER_MODE:
    831  1.1  he 		/*
    832  1.1  he 		 * We can't change the transfer mode, but at least let
    833  1.1  he 		 * scsipi know what the adapter has negociated.
    834  1.1  he 		 */
    835  1.1  he 		 /* Get xfer mode and return it */
    836  1.1  he 		break;
    837  1.1  he 	}
    838  1.1  he }
    839  1.1  he #endif
    840  1.1  he 
    841  1.1  he static void
    842  1.1  he ciss_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    843  1.1  he 	void *arg)
    844  1.1  he {
    845  1.1  he 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    846  1.1  he 	struct ciss_softc *sc =
    847  1.1  he 		(struct ciss_softc *) chan->chan_adapter->adapt_dev;
    848  1.1  he 	u_int8_t target;
    849  1.1  he 	struct ciss_ccb *ccb;
    850  1.1  he 	struct ciss_cmd *cmd;
    851  1.1  he 	int error;
    852  1.1  he 	ciss_lock_t lock;
    853  1.1  he 
    854  1.1  he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd "));
    855  1.1  he 
    856  1.1  he 	switch (req)
    857  1.1  he 	{
    858  1.1  he 	case ADAPTER_REQ_RUN_XFER:
    859  1.1  he 		target = xs->xs_periph->periph_target;
    860  1.1  he 		CISS_DPRINTF(CISS_D_CMD, ("targ=%d ", target));
    861  1.1  he 		if (xs->cmdlen > CISS_MAX_CDB) {
    862  1.1  he 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
    863  1.1  he 			bzero(&xs->sense, sizeof(xs->sense));
    864  1.1  he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    865  1.1  he 			       __FILE__, __LINE__, __FUNCTION__);
    866  1.1  he 			xs->error = XS_DRIVER_STUFFUP;
    867  1.1  he 			scsipi_done(xs);
    868  1.1  he 			break;
    869  1.1  he 		}
    870  1.1  he 
    871  1.1  he 		lock = CISS_LOCK(sc);
    872  1.1  he 		error = 0;
    873  1.1  he 		xs->error = XS_NOERROR;
    874  1.1  he 
    875  1.1  he 		/* XXX emulate SYNCHRONIZE_CACHE ??? */
    876  1.1  he 
    877  1.1  he 		ccb = ciss_get_ccb(sc);
    878  1.1  he 		cmd = &ccb->ccb_cmd;
    879  1.1  he 		ccb->ccb_len = xs->datalen;
    880  1.1  he 		ccb->ccb_data = xs->data;
    881  1.1  he 		ccb->ccb_xs = xs;
    882  1.1  he 		cmd->tgt = CISS_CMD_MODE_LD | target;
    883  1.1  he 		cmd->tgt2 = 0;
    884  1.1  he 		cmd->cdblen = xs->cmdlen;
    885  1.1  he 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
    886  1.1  he 		if (xs->xs_control & XS_CTL_DATA_IN)
    887  1.1  he 			cmd->flags |= CISS_CDB_IN;
    888  1.1  he 		else if (xs->xs_control & XS_CTL_DATA_OUT)
    889  1.1  he 			cmd->flags |= CISS_CDB_OUT;
    890  1.1  he 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
    891  1.1  he 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    892  1.1  he 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
    893  1.1  he 		CISS_DPRINTF(CISS_D_CMD, ("cmd=%02x %02x %02x %02x %02x %02x ",
    894  1.1  he 			     cmd->cdb[0], cmd->cdb[1], cmd->cdb[2],
    895  1.1  he 			     cmd->cdb[3], cmd->cdb[4], cmd->cdb[5]));
    896  1.1  he 
    897  1.1  he 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
    898  1.1  he 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
    899  1.1  he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    900  1.1  he 			       __FILE__, __LINE__, __FUNCTION__);
    901  1.1  he 			xs->error = XS_DRIVER_STUFFUP;
    902  1.1  he 			scsipi_done(xs);
    903  1.1  he 			CISS_UNLOCK(sc, lock);
    904  1.1  he 			return;
    905  1.1  he 		}
    906  1.1  he 
    907  1.1  he 		CISS_UNLOCK(sc, lock);
    908  1.1  he 		break;
    909  1.1  he 	case ADAPTER_REQ_GROW_RESOURCES:
    910  1.1  he 		/*
    911  1.1  he 		 * Not supported.
    912  1.1  he 		 */
    913  1.1  he 		break;
    914  1.1  he 	case ADAPTER_REQ_SET_XFER_MODE:
    915  1.1  he 		/*
    916  1.1  he 		 * We can't change the transfer mode, but at least let
    917  1.1  he 		 * scsipi know what the adapter has negociated.
    918  1.1  he 		 */
    919  1.1  he 		/* FIXME: get xfer mode and write it into arg */
    920  1.1  he 		break;
    921  1.1  he 	}
    922  1.1  he }
    923  1.1  he 
    924  1.1  he int
    925  1.1  he ciss_intr(void *v)
    926  1.1  he {
    927  1.1  he 	struct ciss_softc *sc = v;
    928  1.1  he 	struct ciss_ccb *ccb;
    929  1.1  he 	ciss_lock_t lock;
    930  1.1  he 	u_int32_t id;
    931  1.1  he 	int hit = 0;
    932  1.1  he 
    933  1.1  he 	CISS_DPRINTF(CISS_D_INTR, ("intr "));
    934  1.1  he 
    935  1.1  he 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem))
    936  1.1  he 		return 0;
    937  1.1  he 
    938  1.1  he 	lock = CISS_LOCK(sc);
    939  1.1  he 	while ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ)) !=
    940  1.1  he 	    0xffffffff) {
    941  1.1  he 
    942  1.1  he 		ccb = (struct ciss_ccb *) (sc->ccbs + (id >> 2) * sc->ccblen);
    943  1.1  he 		ccb->ccb_cmd.id = htole32(id);
    944  1.1  he 		if (ccb->ccb_state == CISS_CCB_POLL) {
    945  1.1  he 			ccb->ccb_state = CISS_CCB_ONQ;
    946  1.1  he 			wakeup(ccb);
    947  1.1  he 		} else
    948  1.1  he 			ciss_done(ccb);
    949  1.1  he 
    950  1.1  he 		hit = 1;
    951  1.1  he 	}
    952  1.1  he 	CISS_UNLOCK(sc, lock);
    953  1.1  he 
    954  1.1  he 	CISS_DPRINTF(CISS_D_INTR, ("exit\n"));
    955  1.1  he 	return hit;
    956  1.1  he }
    957  1.1  he 
    958  1.1  he static void
    959  1.1  he ciss_heartbeat(void *v)
    960  1.1  he {
    961  1.1  he 	struct ciss_softc *sc = v;
    962  1.1  he 	u_int32_t hb;
    963  1.1  he 
    964  1.1  he 	hb = bus_space_read_4(sc->sc_iot, sc->cfg_ioh,
    965  1.1  he 	    sc->cfgoff + offsetof(struct ciss_config, heartbeat));
    966  1.1  he 	if (hb == sc->heartbeat)
    967  1.1  he 		panic("ciss: dead");	/* XX reset! */
    968  1.1  he 	else
    969  1.1  he 		sc->heartbeat = hb;
    970  1.1  he 
    971  1.1  he 	callout_schedule(&sc->sc_hb, hz * 3);
    972  1.1  he }
    973  1.1  he 
    974  1.1  he #if 0
    975  1.1  he static void
    976  1.1  he ciss_kthread(void *v)
    977  1.1  he {
    978  1.1  he 	struct ciss_softc *sc = v;
    979  1.1  he 	ciss_lock_t lock;
    980  1.1  he 
    981  1.1  he 	for (;;) {
    982  1.1  he 		tsleep(sc, PRIBIO, sc->sc_dev.dv_xname, 0);
    983  1.1  he 
    984  1.1  he 		lock = CISS_LOCK(sc);
    985  1.1  he 
    986  1.1  he 
    987  1.1  he 
    988  1.1  he 		CISS_UNLOCK(sc, lock);
    989  1.1  he 	}
    990  1.1  he }
    991  1.1  he #endif
    992  1.1  he 
    993  1.1  he static int
    994  1.1  he ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
    995  1.1  he     caddr_t addr, int flag, struct proc *p)
    996  1.1  he {
    997  1.1  he #if NBIO > 0
    998  1.1  he 	return ciss_ioctl(chan->chan_adapter->adapt_dev, cmd, addr);
    999  1.1  he #else
   1000  1.1  he 	return ENOTTY;
   1001  1.1  he #endif
   1002  1.1  he }
   1003  1.1  he 
   1004  1.1  he #if NBIO > 0
   1005  1.1  he static int
   1006  1.1  he ciss_ioctl(struct device *dev, u_long cmd, caddr_t addr)	/* TODO */
   1007  1.1  he {
   1008  1.1  he 	/* struct ciss_softc *sc = (struct ciss_softc *)dev; */
   1009  1.1  he 	ciss_lock_t lock;
   1010  1.1  he 	int error;
   1011  1.1  he 
   1012  1.1  he 	lock = CISS_LOCK(sc);
   1013  1.1  he 	switch (cmd) {
   1014  1.1  he 	case BIOCINQ:
   1015  1.1  he 	case BIOCVOL:
   1016  1.1  he 	case BIOCDISK:
   1017  1.1  he 	case BIOCALARM:
   1018  1.1  he 	case BIOCBLINK:
   1019  1.1  he 	case BIOCSETSTATE:
   1020  1.1  he 	default:
   1021  1.1  he 		CISS_DPRINTF(CISS_D_IOCTL, ("%s: invalid ioctl\n",
   1022  1.1  he 		    sc->sc_dev.dv_xname));
   1023  1.1  he 		error = ENOTTY;
   1024  1.1  he 	}
   1025  1.1  he 	CISS_UNLOCK(sc, lock);
   1026  1.1  he 
   1027  1.1  he 	return error;
   1028  1.1  he }
   1029  1.1  he #endif
   1030