Home | History | Annotate | Line # | Download | only in ic
ciss.c revision 1.13
      1  1.13    mhitch /*	$NetBSD: ciss.c,v 1.13 2008/09/04 16:47:04 mhitch 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.13    mhitch __KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.13 2008/09/04 16:47:04 mhitch Exp $");
     23  1.12    mhitch 
     24  1.12    mhitch #include "bio.h"
     25   1.1        he 
     26   1.1        he /* #define CISS_DEBUG */
     27   1.1        he 
     28   1.1        he #include <sys/param.h>
     29   1.1        he #include <sys/systm.h>
     30   1.1        he #include <sys/buf.h>
     31   1.1        he #include <sys/ioctl.h>
     32   1.1        he #include <sys/device.h>
     33   1.1        he #include <sys/kernel.h>
     34   1.1        he #include <sys/malloc.h>
     35   1.1        he #include <sys/proc.h>
     36   1.1        he 
     37   1.1        he #include <uvm/uvm_extern.h>
     38   1.1        he 
     39   1.9        ad #include <sys/bus.h>
     40   1.1        he 
     41   1.1        he #include <dev/scsipi/scsi_all.h>
     42   1.1        he #include <dev/scsipi/scsi_disk.h>
     43   1.1        he #include <dev/scsipi/scsiconf.h>
     44   1.1        he 
     45   1.1        he #include <dev/ic/cissreg.h>
     46   1.1        he #include <dev/ic/cissvar.h>
     47   1.1        he 
     48  1.12    mhitch #if NBIO > 0
     49  1.12    mhitch #include <dev/biovar.h>
     50  1.12    mhitch #endif /* NBIO > 0 */
     51  1.12    mhitch 
     52   1.1        he #ifdef CISS_DEBUG
     53   1.1        he #define	CISS_DPRINTF(m,a)	if (ciss_debug & (m)) printf a
     54   1.1        he #define	CISS_D_CMD	0x0001
     55   1.1        he #define	CISS_D_INTR	0x0002
     56   1.1        he #define	CISS_D_MISC	0x0004
     57   1.1        he #define	CISS_D_DMA	0x0008
     58   1.1        he #define	CISS_D_IOCTL	0x0010
     59   1.1        he #define	CISS_D_ERR	0x0020
     60   1.1        he int ciss_debug = 0
     61   1.1        he 	| CISS_D_CMD
     62   1.1        he 	| CISS_D_INTR
     63   1.1        he 	| CISS_D_MISC
     64   1.1        he 	| CISS_D_DMA
     65   1.1        he 	| CISS_D_IOCTL
     66   1.1        he 	| CISS_D_ERR
     67   1.1        he 	;
     68   1.1        he #else
     69   1.1        he #define	CISS_DPRINTF(m,a)	/* m, a */
     70   1.1        he #endif
     71   1.1        he 
     72   1.1        he static void	ciss_scsi_cmd(struct scsipi_channel *chan,
     73   1.1        he 			scsipi_adapter_req_t req, void *arg);
     74   1.1        he static int	ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
     75   1.7  christos 	    void *addr, int flag, struct proc *p);
     76   1.1        he static void	cissminphys(struct buf *bp);
     77   1.1        he 
     78   1.1        he #if 0
     79   1.1        he static void	ciss_scsi_raw_cmd(struct scsipi_channel *chan,
     80   1.1        he 			scsipi_adapter_req_t req, void *arg);
     81   1.1        he #endif
     82   1.1        he 
     83   1.1        he static int	ciss_sync(struct ciss_softc *sc);
     84   1.1        he static void	ciss_heartbeat(void *v);
     85   1.1        he static void	ciss_shutdown(void *v);
     86   1.1        he 
     87   1.1        he static struct ciss_ccb *ciss_get_ccb(struct ciss_softc *sc);
     88   1.1        he static void	ciss_put_ccb(struct ciss_ccb *ccb);
     89   1.1        he static int	ciss_cmd(struct ciss_ccb *ccb, int flags, int wait);
     90   1.1        he static int	ciss_done(struct ciss_ccb *ccb);
     91   1.1        he static int	ciss_error(struct ciss_ccb *ccb);
     92  1.12    mhitch struct ciss_ld *ciss_pdscan(struct ciss_softc *sc, int ld);
     93   1.1        he static int	ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq);
     94  1.12    mhitch int	ciss_ldid(struct ciss_softc *, int, struct ciss_ldid *);
     95  1.12    mhitch int	ciss_ldstat(struct ciss_softc *, int, struct ciss_ldstat *);
     96   1.1        he static int	ciss_ldmap(struct ciss_softc *sc);
     97  1.12    mhitch int	ciss_pdid(struct ciss_softc *, u_int8_t, struct ciss_pdid *, int);
     98  1.12    mhitch 
     99  1.12    mhitch #if NBIO > 0
    100  1.12    mhitch int		ciss_ioctl(struct device *, u_long, void *);
    101  1.12    mhitch int		ciss_ioctl_vol(struct ciss_softc *, struct bioc_vol *);
    102  1.12    mhitch int		ciss_blink(struct ciss_softc *, int, int, int, struct ciss_blink *);
    103  1.12    mhitch int		ciss_create_sensors(struct ciss_softc *);
    104  1.12    mhitch void		ciss_sensor_refresh(struct sysmon_envsys *, envsys_data_t *);
    105  1.12    mhitch #endif /* NBIO > 0 */
    106   1.1        he 
    107   1.1        he static struct ciss_ccb *
    108   1.1        he ciss_get_ccb(struct ciss_softc *sc)
    109   1.1        he {
    110   1.1        he 	struct ciss_ccb *ccb;
    111   1.1        he 
    112  1.12    mhitch 	mutex_enter(&sc->sc_mutex);
    113   1.1        he 	if ((ccb = TAILQ_LAST(&sc->sc_free_ccb, ciss_queue_head))) {
    114   1.1        he 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_link);
    115   1.1        he 		ccb->ccb_state = CISS_CCB_READY;
    116   1.1        he 	}
    117  1.12    mhitch 	mutex_exit(&sc->sc_mutex);
    118   1.1        he 	return ccb;
    119   1.1        he }
    120   1.1        he 
    121   1.1        he static void
    122   1.1        he ciss_put_ccb(struct ciss_ccb *ccb)
    123   1.1        he {
    124   1.1        he 	struct ciss_softc *sc = ccb->ccb_sc;
    125   1.1        he 
    126   1.1        he 	ccb->ccb_state = CISS_CCB_FREE;
    127  1.12    mhitch 	mutex_enter(&sc->sc_mutex);
    128   1.1        he 	TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
    129  1.12    mhitch 	mutex_exit(&sc->sc_mutex);
    130   1.1        he }
    131   1.1        he 
    132   1.1        he int
    133   1.1        he ciss_attach(struct ciss_softc *sc)
    134   1.1        he {
    135   1.1        he 	struct ciss_ccb *ccb;
    136   1.1        he 	struct ciss_cmd *cmd;
    137   1.1        he 	struct ciss_inquiry *inq;
    138   1.1        he 	bus_dma_segment_t seg[1];
    139   1.1        he 	int error, i, total, rseg, maxfer;
    140   1.1        he 	paddr_t pa;
    141   1.1        he 
    142   1.1        he 	bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    143   1.1        he 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    144   1.1        he 
    145   1.1        he 	if (sc->cfg.signature != CISS_SIGNATURE) {
    146   1.1        he 		printf(": bad sign 0x%08x\n", sc->cfg.signature);
    147   1.1        he 		return -1;
    148   1.1        he 	}
    149   1.1        he 
    150   1.1        he 	if (!(sc->cfg.methods & CISS_METH_SIMPL)) {
    151   1.1        he 		printf(": not simple 0x%08x\n", sc->cfg.methods);
    152   1.1        he 		return -1;
    153   1.1        he 	}
    154   1.1        he 
    155   1.1        he 	sc->cfg.rmethod = CISS_METH_SIMPL;
    156   1.1        he 	sc->cfg.paddr_lim = 0;			/* 32bit addrs */
    157   1.1        he 	sc->cfg.int_delay = 0;			/* disable coalescing */
    158   1.1        he 	sc->cfg.int_count = 0;
    159   1.1        he 	strlcpy(sc->cfg.hostname, "HUMPPA", sizeof(sc->cfg.hostname));
    160   1.1        he 	sc->cfg.driverf |= CISS_DRV_PRF;	/* enable prefetch */
    161   1.1        he 	if (!sc->cfg.maxsg)
    162   1.1        he 		sc->cfg.maxsg = MAXPHYS / PAGE_SIZE + 1;
    163   1.1        he 
    164   1.1        he 	bus_space_write_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    165   1.1        he 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    166   1.1        he 	bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, sizeof(sc->cfg),
    167   1.1        he 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
    168   1.1        he 
    169   1.1        he 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IDB, CISS_IDB_CFG);
    170   1.1        he 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
    171   1.1        he 	    BUS_SPACE_BARRIER_WRITE);
    172   1.1        he 	for (i = 1000; i--; DELAY(1000)) {
    173   1.1        he 		/* XXX maybe IDB is really 64bit? - hp dl380 needs this */
    174   1.1        he 		(void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB + 4);
    175   1.1        he 		if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG))
    176   1.1        he 			break;
    177   1.1        he 		bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
    178   1.1        he 		    BUS_SPACE_BARRIER_READ);
    179   1.1        he 	}
    180   1.1        he 
    181   1.1        he 	if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG) {
    182   1.1        he 		printf(": cannot set config\n");
    183   1.1        he 		return -1;
    184   1.1        he 	}
    185   1.1        he 
    186   1.1        he 	bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
    187   1.1        he 	    (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
    188   1.1        he 
    189   1.1        he 	if (!(sc->cfg.amethod & CISS_METH_SIMPL)) {
    190   1.1        he 		printf(": cannot simplify 0x%08x\n", sc->cfg.amethod);
    191   1.1        he 		return -1;
    192   1.1        he 	}
    193   1.1        he 
    194   1.1        he 	/* i'm ready for you and i hope you're ready for me */
    195   1.1        he 	for (i = 30000; i--; DELAY(1000)) {
    196   1.1        he 		if (bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    197   1.1        he 		    offsetof(struct ciss_config, amethod)) & CISS_METH_READY)
    198   1.1        he 			break;
    199   1.1        he 		bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    200   1.1        he 		    offsetof(struct ciss_config, amethod), 4,
    201   1.1        he 		    BUS_SPACE_BARRIER_READ);
    202   1.1        he 	}
    203   1.1        he 
    204   1.1        he 	if (!(bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
    205   1.1        he 	    offsetof(struct ciss_config, amethod)) & CISS_METH_READY)) {
    206   1.1        he 		printf(": she never came ready for me 0x%08x\n",
    207   1.1        he 		    sc->cfg.amethod);
    208   1.1        he 		return -1;
    209   1.1        he 	}
    210   1.1        he 
    211  1.12    mhitch 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    212  1.12    mhitch 	mutex_init(&sc->sc_mutex_scratch, MUTEX_DEFAULT, IPL_VM);
    213  1.12    mhitch 	cv_init(&sc->sc_condvar, "ciss_cmd");
    214   1.1        he 	sc->maxcmd = sc->cfg.maxcmd;
    215   1.1        he 	sc->maxsg = sc->cfg.maxsg;
    216   1.1        he 	if (sc->maxsg > MAXPHYS / PAGE_SIZE + 1)
    217   1.1        he 		sc->maxsg = MAXPHYS / PAGE_SIZE + 1;
    218   1.1        he 	i = sizeof(struct ciss_ccb) +
    219   1.1        he 	    sizeof(ccb->ccb_cmd.sgl[0]) * (sc->maxsg - 1);
    220   1.1        he 	for (sc->ccblen = 0x10; sc->ccblen < i; sc->ccblen <<= 1);
    221   1.1        he 
    222   1.1        he 	total = sc->ccblen * sc->maxcmd;
    223   1.1        he 	if ((error = bus_dmamem_alloc(sc->sc_dmat, total, PAGE_SIZE, 0,
    224   1.1        he 	    sc->cmdseg, 1, &rseg, BUS_DMA_NOWAIT))) {
    225   1.1        he 		printf(": cannot allocate CCBs (%d)\n", error);
    226   1.1        he 		return -1;
    227   1.1        he 	}
    228   1.1        he 
    229   1.1        he 	if ((error = bus_dmamem_map(sc->sc_dmat, sc->cmdseg, rseg, total,
    230   1.7  christos 	    (void **)&sc->ccbs, BUS_DMA_NOWAIT))) {
    231   1.1        he 		printf(": cannot map CCBs (%d)\n", error);
    232   1.1        he 		return -1;
    233   1.1        he 	}
    234   1.1        he 	bzero(sc->ccbs, total);
    235   1.1        he 
    236   1.1        he 	if ((error = bus_dmamap_create(sc->sc_dmat, total, 1,
    237   1.1        he 	    total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->cmdmap))) {
    238   1.1        he 		printf(": cannot create CCBs dmamap (%d)\n", error);
    239   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    240   1.1        he 		return -1;
    241   1.1        he 	}
    242   1.1        he 
    243   1.1        he 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->cmdmap, sc->ccbs, total,
    244   1.1        he 	    NULL, BUS_DMA_NOWAIT))) {
    245   1.1        he 		printf(": cannot load CCBs dmamap (%d)\n", error);
    246   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    247   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    248   1.1        he 		return -1;
    249   1.1        he 	}
    250   1.1        he 
    251   1.1        he 	TAILQ_INIT(&sc->sc_ccbq);
    252   1.1        he 	TAILQ_INIT(&sc->sc_ccbdone);
    253   1.1        he 	TAILQ_INIT(&sc->sc_free_ccb);
    254   1.1        he 
    255   1.1        he 	maxfer = sc->maxsg * PAGE_SIZE;
    256   1.1        he 	for (i = 0; total > 0 && i < sc->maxcmd; i++, total -= sc->ccblen) {
    257   1.7  christos 		ccb = (struct ciss_ccb *) ((char *)sc->ccbs + i * sc->ccblen);
    258   1.1        he 		cmd = &ccb->ccb_cmd;
    259   1.1        he 		pa = sc->cmdseg[0].ds_addr + i * sc->ccblen;
    260   1.1        he 
    261   1.1        he 		ccb->ccb_sc = sc;
    262   1.1        he 		ccb->ccb_cmdpa = pa + offsetof(struct ciss_ccb, ccb_cmd);
    263   1.1        he 		ccb->ccb_state = CISS_CCB_FREE;
    264   1.1        he 
    265   1.1        he 		cmd->id = htole32(i << 2);
    266   1.1        he 		cmd->id_hi = htole32(0);
    267   1.1        he 		cmd->sgin = sc->maxsg;
    268   1.1        he 		cmd->sglen = htole16((u_int16_t)cmd->sgin);
    269   1.1        he 		cmd->err_len = htole32(sizeof(ccb->ccb_err));
    270   1.1        he 		pa += offsetof(struct ciss_ccb, ccb_err);
    271   1.1        he 		cmd->err_pa = htole64((u_int64_t)pa);
    272   1.1        he 
    273   1.1        he 		if ((error = bus_dmamap_create(sc->sc_dmat, maxfer, sc->maxsg,
    274   1.1        he 		    maxfer, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    275   1.1        he 		    &ccb->ccb_dmamap)))
    276   1.1        he 			break;
    277   1.1        he 
    278   1.1        he 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
    279   1.1        he 	}
    280   1.1        he 
    281   1.1        he 	if (i < sc->maxcmd) {
    282   1.1        he 		printf(": cannot create ccb#%d dmamap (%d)\n", i, error);
    283   1.1        he 		if (i == 0) {
    284   1.1        he 			/* TODO leaking cmd's dmamaps and shitz */
    285   1.1        he 			bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    286   1.1        he 			bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    287   1.1        he 			return -1;
    288   1.1        he 		}
    289   1.1        he 	}
    290   1.1        he 
    291   1.1        he 	if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    292   1.1        he 	    seg, 1, &rseg, BUS_DMA_NOWAIT))) {
    293   1.1        he 		printf(": cannot allocate scratch buffer (%d)\n", error);
    294   1.1        he 		return -1;
    295   1.1        he 	}
    296   1.1        he 
    297   1.1        he 	if ((error = bus_dmamem_map(sc->sc_dmat, seg, rseg, PAGE_SIZE,
    298   1.7  christos 	    (void **)&sc->scratch, BUS_DMA_NOWAIT))) {
    299   1.1        he 		printf(": cannot map scratch buffer (%d)\n", error);
    300   1.1        he 		return -1;
    301   1.1        he 	}
    302   1.1        he 	bzero(sc->scratch, PAGE_SIZE);
    303  1.12    mhitch 	sc->sc_waitflag = XS_CTL_NOSLEEP;		/* can't sleep yet */
    304   1.1        he 
    305  1.12    mhitch 	mutex_enter(&sc->sc_mutex_scratch);		/* is this really needed? */
    306   1.1        he 	inq = sc->scratch;
    307   1.1        he 	if (ciss_inq(sc, inq)) {
    308   1.1        he 		printf(": adapter inquiry failed\n");
    309  1.12    mhitch 		mutex_exit(&sc->sc_mutex_scratch);
    310   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    311   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    312   1.1        he 		return -1;
    313   1.1        he 	}
    314   1.1        he 
    315   1.1        he 	if (!(inq->flags & CISS_INQ_BIGMAP)) {
    316   1.1        he 		printf(": big map is not supported, flags=0x%x\n",
    317   1.1        he 		    inq->flags);
    318  1.12    mhitch 		mutex_exit(&sc->sc_mutex_scratch);
    319   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    320   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    321   1.1        he 		return -1;
    322   1.1        he 	}
    323   1.1        he 
    324   1.1        he 	sc->maxunits = inq->numld;
    325   1.1        he 	sc->nbus = inq->nscsi_bus;
    326   1.1        he 	sc->ndrives = inq->buswidth;
    327   1.1        he 	printf(": %d LD%s, HW rev %d, FW %4.4s/%4.4s\n",
    328   1.1        he 	    inq->numld, inq->numld == 1? "" : "s",
    329   1.1        he 	    inq->hw_rev, inq->fw_running, inq->fw_stored);
    330   1.1        he 
    331  1.12    mhitch 	mutex_exit(&sc->sc_mutex_scratch);
    332   1.1        he 
    333   1.8        ad 	callout_init(&sc->sc_hb, 0);
    334   1.1        he 	callout_setfunc(&sc->sc_hb, ciss_heartbeat, sc);
    335   1.1        he 	callout_schedule(&sc->sc_hb, hz * 3);
    336   1.1        he 
    337   1.1        he 	/* map LDs */
    338   1.1        he 	if (ciss_ldmap(sc)) {
    339  1.11    cegger 		aprint_error_dev(&sc->sc_dev, "adapter LD map failed\n");
    340   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    341   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    342   1.1        he 		return -1;
    343   1.1        he 	}
    344   1.1        he 
    345  1.12    mhitch 	if (!(sc->sc_lds = malloc(sc->maxunits * sizeof(*sc->sc_lds),
    346  1.12    mhitch 	    M_DEVBUF, M_NOWAIT))) {
    347   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    348   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    349   1.1        he 		return -1;
    350   1.1        he 	}
    351  1.12    mhitch 	bzero(sc->sc_lds, sc->maxunits * sizeof(*sc->sc_lds));
    352   1.1        he 
    353  1.12    mhitch 	sc->sc_flush = CISS_FLUSH_ENABLE;
    354  1.12    mhitch 	if (!(sc->sc_sh = shutdownhook_establish(ciss_shutdown, sc))) {
    355  1.12    mhitch 		printf(": unable to establish shutdown hook\n");
    356   1.1        he 		bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
    357   1.1        he 		bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
    358   1.1        he 		return -1;
    359   1.1        he 	}
    360   1.1        he 
    361   1.1        he 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    362   1.1        he 	sc->sc_channel.chan_bustype = &scsi_bustype;
    363   1.1        he 	sc->sc_channel.chan_channel = 0;
    364   1.1        he 	sc->sc_channel.chan_ntargets = sc->maxunits;
    365  1.13    mhitch 	sc->sc_channel.chan_nluns = 1;	/* ciss doesn't really have SCSI luns */
    366   1.1        he 	sc->sc_channel.chan_openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
    367  1.12    mhitch #if NBIO > 0
    368  1.12    mhitch 	/* XXX Reserve some ccb's for sensor and bioctl. */
    369  1.12    mhitch 	if (sc->sc_channel.chan_openings > 2)
    370  1.12    mhitch 		sc->sc_channel.chan_openings -= 2;
    371  1.12    mhitch #endif
    372   1.1        he 	sc->sc_channel.chan_flags = 0;
    373   1.1        he 	sc->sc_channel.chan_id = sc->maxunits;
    374   1.1        he 
    375   1.1        he 	sc->sc_adapter.adapt_dev = (struct device *) sc;
    376  1.12    mhitch 	sc->sc_adapter.adapt_openings = sc->sc_channel.chan_openings;
    377   1.1        he 	sc->sc_adapter.adapt_max_periph = sc->maxunits;
    378   1.1        he 	sc->sc_adapter.adapt_request = ciss_scsi_cmd;
    379   1.1        he 	sc->sc_adapter.adapt_minphys = cissminphys;
    380   1.1        he 	sc->sc_adapter.adapt_ioctl = ciss_scsi_ioctl;
    381   1.1        he 	sc->sc_adapter.adapt_nchannels = 1;
    382   1.1        he 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    383   1.1        he 
    384   1.1        he #if 0
    385   1.1        he 	sc->sc_link_raw.adapter_softc = sc;
    386  1.12    mhitch 	sc->sc_link.openings = sc->sc_channel.chan_openings;
    387   1.1        he 	sc->sc_link_raw.adapter = &ciss_raw_switch;
    388   1.1        he 	sc->sc_link_raw.adapter_target = sc->ndrives;
    389   1.1        he 	sc->sc_link_raw.adapter_buswidth = sc->ndrives;
    390   1.1        he 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    391   1.1        he #endif
    392   1.1        he 
    393  1.12    mhitch #if NBIO > 0
    394  1.12    mhitch 	/* now map all the physdevs into their lds */
    395  1.12    mhitch 	/* XXX currently we assign all of them into ld0 */
    396  1.12    mhitch 	for (i = 0; i < sc->maxunits && i < 1; i++)
    397  1.12    mhitch 		if (!(sc->sc_lds[i] = ciss_pdscan(sc, i))) {
    398  1.12    mhitch 			sc->sc_waitflag = 0;	/* we can sleep now */
    399  1.12    mhitch 			return 0;
    400  1.12    mhitch 		}
    401  1.12    mhitch 
    402   1.1        he 	if (bio_register(&sc->sc_dev, ciss_ioctl) != 0)
    403  1.11    cegger 		aprint_error_dev(&sc->sc_dev, "controller registration failed");
    404  1.12    mhitch 	else
    405  1.12    mhitch 		sc->sc_ioctl = ciss_ioctl;
    406  1.12    mhitch 	if (ciss_create_sensors(sc) != 0)
    407  1.12    mhitch 		aprint_error_dev(&sc->sc_dev, "unable to create sensors");
    408   1.1        he #endif
    409  1.12    mhitch 	sc->sc_waitflag = 0;			/* we can sleep now */
    410   1.1        he 
    411   1.1        he 	return 0;
    412   1.1        he }
    413   1.1        he 
    414   1.1        he static void
    415   1.1        he ciss_shutdown(void *v)
    416   1.1        he {
    417   1.1        he 	struct ciss_softc *sc = v;
    418   1.1        he 
    419   1.1        he 	sc->sc_flush = CISS_FLUSH_DISABLE;
    420   1.1        he 	/* timeout_del(&sc->sc_hb); */
    421   1.1        he 	ciss_sync(sc);
    422   1.1        he }
    423   1.1        he 
    424   1.1        he static void
    425   1.1        he cissminphys(struct buf *bp)
    426   1.1        he {
    427   1.1        he #if 0	/* TOSO */
    428   1.1        he #define	CISS_MAXFER	(PAGE_SIZE * (sc->maxsg + 1))
    429   1.1        he 	if (bp->b_bcount > CISS_MAXFER)
    430   1.1        he 		bp->b_bcount = CISS_MAXFER;
    431   1.1        he #endif
    432   1.1        he 	minphys(bp);
    433   1.2    martti }
    434   1.1        he 
    435   1.1        he /*
    436   1.1        he  * submit a command and optionally wait for completition.
    437   1.1        he  * wait arg abuses XS_CTL_POLL|XS_CTL_NOSLEEP flags to request
    438   1.1        he  * to wait (XS_CTL_POLL) and to allow tsleep() (!XS_CTL_NOSLEEP)
    439   1.1        he  * instead of busy loop waiting
    440   1.1        he  */
    441   1.1        he static int
    442   1.1        he ciss_cmd(struct ciss_ccb *ccb, int flags, int wait)
    443   1.1        he {
    444   1.1        he 	struct ciss_softc *sc = ccb->ccb_sc;
    445   1.1        he 	struct ciss_cmd *cmd = &ccb->ccb_cmd;
    446   1.1        he 	struct ciss_ccb *ccb1;
    447   1.1        he 	bus_dmamap_t dmap = ccb->ccb_dmamap;
    448   1.1        he 	u_int32_t id;
    449   1.1        he 	int i, tohz, error = 0;
    450   1.1        he 
    451   1.1        he 	if (ccb->ccb_state != CISS_CCB_READY) {
    452  1.11    cegger 		printf("%s: ccb %d not ready state=0x%x\n", device_xname(&sc->sc_dev),
    453   1.1        he 		    cmd->id, ccb->ccb_state);
    454   1.1        he 		return (EINVAL);
    455   1.1        he 	}
    456   1.1        he 
    457   1.1        he 	if (ccb->ccb_data) {
    458   1.1        he 		bus_dma_segment_t *sgd;
    459   1.1        he 
    460   1.1        he 		if ((error = bus_dmamap_load(sc->sc_dmat, dmap, ccb->ccb_data,
    461   1.1        he 		    ccb->ccb_len, NULL, flags))) {
    462   1.1        he 			if (error == EFBIG)
    463   1.1        he 				printf("more than %d dma segs\n", sc->maxsg);
    464   1.1        he 			else
    465   1.1        he 				printf("error %d loading dma map\n", error);
    466   1.1        he 			ciss_put_ccb(ccb);
    467   1.1        he 			return (error);
    468   1.1        he 		}
    469   1.1        he 		cmd->sgin = dmap->dm_nsegs;
    470   1.1        he 
    471   1.1        he 		sgd = dmap->dm_segs;
    472   1.1        he 		CISS_DPRINTF(CISS_D_DMA, ("data=%p/%u<0x%lx/%lu",
    473   1.1        he 		    ccb->ccb_data, ccb->ccb_len, sgd->ds_addr, sgd->ds_len));
    474   1.1        he 
    475   1.1        he 		for (i = 0; i < dmap->dm_nsegs; sgd++, i++) {
    476   1.1        he 			cmd->sgl[i].addr_lo = htole32(sgd->ds_addr);
    477   1.1        he 			cmd->sgl[i].addr_hi =
    478   1.1        he 			    htole32((u_int64_t)sgd->ds_addr >> 32);
    479   1.1        he 			cmd->sgl[i].len = htole32(sgd->ds_len);
    480   1.1        he 			cmd->sgl[i].flags = htole32(0);
    481   1.4  christos 			if (i) {
    482   1.1        he 				CISS_DPRINTF(CISS_D_DMA,
    483   1.1        he 				    (",0x%lx/%lu", sgd->ds_addr, sgd->ds_len));
    484   1.4  christos 			}
    485   1.1        he 		}
    486   1.1        he 
    487   1.1        he 		CISS_DPRINTF(CISS_D_DMA, ("> "));
    488   1.1        he 
    489   1.1        he 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    490   1.1        he 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    491   1.1        he 	} else
    492   1.1        he 		cmd->sgin = 0;
    493   1.1        he 	cmd->sglen = htole16((u_int16_t)cmd->sgin);
    494   1.1        he 	bzero(&ccb->ccb_err, sizeof(ccb->ccb_err));
    495   1.1        he 
    496   1.1        he 	bus_dmamap_sync(sc->sc_dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize,
    497   1.1        he 	    BUS_DMASYNC_PREWRITE);
    498   1.1        he 
    499   1.1        he 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    500   1.1        he 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    501   1.1        he 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem);
    502   1.1        he 
    503  1.12    mhitch 	mutex_enter(&sc->sc_mutex);
    504   1.1        he 	TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link);
    505  1.12    mhitch 	mutex_exit(&sc->sc_mutex);
    506   1.1        he 	ccb->ccb_state = CISS_CCB_ONQ;
    507   1.1        he 	CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x ", cmd->id));
    508   1.1        he 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ, ccb->ccb_cmdpa);
    509   1.1        he 
    510   1.1        he 	if (wait & XS_CTL_POLL) {
    511   1.1        he 		int etick;
    512   1.1        he 		CISS_DPRINTF(CISS_D_CMD, ("waiting "));
    513   1.1        he 
    514   1.1        he 		i = ccb->ccb_xs? ccb->ccb_xs->timeout : 60000;
    515   1.1        he 		tohz = (i / 1000) * hz + (i % 1000) * (hz / 1000);
    516   1.1        he 		if (tohz == 0)
    517   1.1        he 			tohz = 1;
    518   1.1        he 		for (i *= 100, etick = tick + tohz; i--; ) {
    519   1.1        he 			if (!(wait & XS_CTL_NOSLEEP)) {
    520   1.1        he 				ccb->ccb_state = CISS_CCB_POLL;
    521  1.12    mhitch 				CISS_DPRINTF(CISS_D_CMD, ("cv_timedwait(%d) ", tohz));
    522  1.12    mhitch 				mutex_enter(&sc->sc_mutex);
    523  1.12    mhitch 				if (cv_timedwait(&sc->sc_condvar,
    524  1.12    mhitch 				    &sc->sc_mutex, tohz) == EWOULDBLOCK) {
    525  1.12    mhitch 					mutex_exit(&sc->sc_mutex);
    526   1.1        he 					break;
    527   1.1        he 				}
    528  1.12    mhitch 				mutex_exit(&sc->sc_mutex);
    529   1.1        he 				if (ccb->ccb_state != CISS_CCB_ONQ) {
    530   1.1        he 					tohz = etick - tick;
    531   1.1        he 					if (tohz <= 0)
    532   1.1        he 						break;
    533   1.1        he 					CISS_DPRINTF(CISS_D_CMD, ("T"));
    534   1.1        he 					continue;
    535   1.1        he 				}
    536   1.1        he 				ccb1 = ccb;
    537   1.1        he 			} else {
    538   1.1        he 				DELAY(10);
    539   1.1        he 
    540   1.1        he 				if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    541   1.1        he 				    CISS_ISR) & sc->iem)) {
    542   1.1        he 					CISS_DPRINTF(CISS_D_CMD, ("N"));
    543   1.1        he 					continue;
    544   1.1        he 				}
    545   1.1        he 
    546   1.1        he 				if ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    547   1.1        he 				    CISS_OUTQ)) == 0xffffffff) {
    548   1.1        he 					CISS_DPRINTF(CISS_D_CMD, ("Q"));
    549   1.1        he 					continue;
    550   1.1        he 				}
    551   1.1        he 
    552   1.1        he 				CISS_DPRINTF(CISS_D_CMD, ("got=0x%x ", id));
    553   1.1        he 				ccb1 = (struct ciss_ccb *)
    554   1.7  christos 					((char *)sc->ccbs + (id >> 2) * sc->ccblen);
    555   1.1        he 				ccb1->ccb_cmd.id = htole32(id);
    556   1.1        he 			}
    557   1.1        he 
    558   1.1        he 			error = ciss_done(ccb1);
    559   1.1        he 			if (ccb1 == ccb)
    560   1.1        he 				break;
    561   1.1        he 		}
    562   1.1        he 
    563   1.1        he 		/* if never got a chance to be done above... */
    564   1.1        he 		if (ccb->ccb_state != CISS_CCB_FREE) {
    565   1.1        he 			ccb->ccb_err.cmd_stat = CISS_ERR_TMO;
    566   1.1        he 			error = ciss_done(ccb);
    567   1.1        he 		}
    568   1.1        he 
    569   1.1        he 		CISS_DPRINTF(CISS_D_CMD, ("done %d:%d",
    570   1.1        he 		    ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat));
    571   1.1        he 	}
    572   1.1        he 
    573   1.1        he 	if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
    574   1.1        he 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
    575   1.1        he 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
    576   1.1        he 
    577   1.1        he 	return (error);
    578   1.1        he }
    579   1.1        he 
    580   1.1        he static int
    581   1.1        he ciss_done(struct ciss_ccb *ccb)
    582   1.1        he {
    583   1.1        he 	struct ciss_softc *sc = ccb->ccb_sc;
    584   1.1        he 	struct scsipi_xfer *xs = ccb->ccb_xs;
    585   1.2    martti 	struct ciss_cmd *cmd;
    586   1.1        he 	int error = 0;
    587   1.1        he 
    588   1.1        he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) ", ccb));
    589   1.1        he 
    590   1.1        he 	if (ccb->ccb_state != CISS_CCB_ONQ) {
    591   1.1        he 		printf("%s: unqueued ccb %p ready, state=0x%x\n",
    592  1.11    cegger 		    device_xname(&sc->sc_dev), ccb, ccb->ccb_state);
    593   1.1        he 		return 1;
    594   1.1        he 	}
    595   1.1        he 
    596   1.1        he 	ccb->ccb_state = CISS_CCB_READY;
    597  1.12    mhitch 	mutex_enter(&sc->sc_mutex);
    598   1.1        he 	TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
    599  1.12    mhitch 	mutex_exit(&sc->sc_mutex);
    600   1.1        he 
    601   1.1        he 	if (ccb->ccb_cmd.id & CISS_CMD_ERR)
    602   1.1        he 		error = ciss_error(ccb);
    603   1.1        he 
    604   1.2    martti 	cmd = &ccb->ccb_cmd;
    605   1.1        he 	if (ccb->ccb_data) {
    606   1.1        he 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
    607   1.2    martti 		    ccb->ccb_dmamap->dm_mapsize, (cmd->flags & CISS_CDB_IN) ?
    608   1.2    martti 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    609   1.1        he 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
    610   1.1        he 		ccb->ccb_xs = NULL;
    611   1.1        he 		ccb->ccb_data = NULL;
    612   1.1        he 	}
    613   1.1        he 
    614   1.1        he 	ciss_put_ccb(ccb);
    615   1.1        he 
    616   1.1        he 	if (xs) {
    617   1.1        he 		xs->resid = 0;
    618   1.1        he 		CISS_DPRINTF(CISS_D_CMD, ("scsipi_done(%p) ", xs));
    619   1.1        he 		scsipi_done(xs);
    620   1.1        he 	}
    621   1.1        he 
    622   1.1        he 	return error;
    623   1.1        he }
    624   1.1        he 
    625   1.1        he static int
    626   1.1        he ciss_error(struct ciss_ccb *ccb)
    627   1.1        he {
    628   1.1        he 	struct ciss_softc *sc = ccb->ccb_sc;
    629   1.1        he 	struct ciss_error *err = &ccb->ccb_err;
    630   1.1        he 	struct scsipi_xfer *xs = ccb->ccb_xs;
    631   1.1        he 	int rv;
    632   1.1        he 
    633   1.1        he 	switch ((rv = le16toh(err->cmd_stat))) {
    634   1.1        he 	case CISS_ERR_OK:
    635   1.1        he 		break;
    636   1.1        he 
    637   1.1        he 	case CISS_ERR_INVCMD:
    638   1.1        he 		printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n",
    639  1.11    cegger 		    device_xname(&sc->sc_dev), ccb->ccb_cmd.id,
    640   1.1        he 		    err->err_info, err->err_type[3], err->err_type[2]);
    641   1.1        he 		if (xs) {
    642   1.1        he 			bzero(&xs->sense, sizeof(xs->sense));
    643   1.1        he 			xs->sense.scsi_sense.response_code =
    644   1.1        he 				SSD_RCODE_CURRENT | SSD_RCODE_VALID;
    645   1.1        he 			xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
    646   1.1        he 			xs->sense.scsi_sense.asc = 0x24; /* ill field */
    647   1.1        he 			xs->sense.scsi_sense.ascq = 0x0;
    648   1.1        he 			xs->error = XS_SENSE;
    649   1.1        he 		}
    650   1.1        he 		break;
    651   1.1        he 
    652   1.1        he 	case CISS_ERR_TMO:
    653   1.1        he 		xs->error = XS_TIMEOUT;
    654   1.1        he 		break;
    655   1.1        he 
    656   1.1        he 	case CISS_ERR_UNRUN:
    657   1.1        he 		/* Underrun */
    658   1.1        he 		xs->resid = le32toh(err->resid);
    659   1.1        he 		CISS_DPRINTF(CISS_D_CMD, (" underrun resid=0x%x ",
    660   1.1        he 					  xs->resid));
    661   1.1        he 		break;
    662   1.1        he 	default:
    663   1.1        he 		if (xs) {
    664   1.1        he 			CISS_DPRINTF(CISS_D_CMD, ("scsi_stat=%x ", err->scsi_stat));
    665   1.1        he 			switch (err->scsi_stat) {
    666   1.1        he 			case SCSI_CHECK:
    667   1.1        he 				xs->error = XS_SENSE;
    668   1.1        he 				bcopy(&err->sense[0], &xs->sense,
    669   1.1        he 				    sizeof(xs->sense));
    670   1.1        he 				CISS_DPRINTF(CISS_D_CMD, (" sense=%02x %02x %02x %02x ",
    671   1.1        he 					     err->sense[0], err->sense[1], err->sense[2], err->sense[3]));
    672   1.1        he 				break;
    673   1.1        he 
    674   1.1        he 			case XS_BUSY:
    675   1.1        he 				xs->error = XS_BUSY;
    676   1.1        he 				break;
    677   1.1        he 
    678   1.1        he 			default:
    679   1.1        he 				CISS_DPRINTF(CISS_D_ERR, ("%s: "
    680   1.1        he 				    "cmd_stat=%x scsi_stat=0x%x resid=0x%x\n",
    681  1.11    cegger 				    device_xname(&sc->sc_dev), rv, err->scsi_stat,
    682   1.1        he 				    le32toh(err->resid)));
    683   1.1        he 				printf("ciss driver stuffup in %s:%d: %s()\n",
    684  1.10     perry 				       __FILE__, __LINE__, __func__);
    685   1.1        he 				xs->error = XS_DRIVER_STUFFUP;
    686   1.1        he 				break;
    687   1.1        he 			}
    688   1.1        he 			xs->resid = le32toh(err->resid);
    689   1.1        he 		}
    690   1.1        he 	}
    691   1.1        he 	ccb->ccb_cmd.id &= htole32(~3);
    692   1.1        he 
    693   1.1        he 	return rv;
    694   1.1        he }
    695   1.1        he 
    696   1.1        he static int
    697   1.1        he ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq)
    698   1.1        he {
    699   1.1        he 	struct ciss_ccb *ccb;
    700   1.1        he 	struct ciss_cmd *cmd;
    701   1.1        he 
    702   1.1        he 	ccb = ciss_get_ccb(sc);
    703   1.1        he 	ccb->ccb_len = sizeof(*inq);
    704   1.1        he 	ccb->ccb_data = inq;
    705   1.2    martti 	ccb->ccb_xs = NULL;
    706   1.1        he 	cmd = &ccb->ccb_cmd;
    707   1.1        he 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    708   1.1        he 	cmd->tgt2 = 0;
    709   1.1        he 	cmd->cdblen = 10;
    710   1.1        he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    711   1.1        he 	cmd->tmo = htole16(0);
    712   1.1        he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    713   1.1        he 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    714   1.1        he 	cmd->cdb[6] = CISS_CMS_CTRL_CTRL;
    715   1.1        he 	cmd->cdb[7] = sizeof(*inq) >> 8;	/* biiiig endian */
    716   1.1        he 	cmd->cdb[8] = sizeof(*inq) & 0xff;
    717   1.1        he 
    718   1.1        he 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    719   1.1        he }
    720   1.1        he 
    721   1.1        he static int
    722   1.1        he ciss_ldmap(struct ciss_softc *sc)
    723   1.1        he {
    724   1.1        he 	struct ciss_ccb *ccb;
    725   1.1        he 	struct ciss_cmd *cmd;
    726   1.1        he 	struct ciss_ldmap *lmap;
    727   1.1        he 	int total, rv;
    728   1.1        he 
    729  1.12    mhitch 	mutex_enter(&sc->sc_mutex_scratch);
    730   1.1        he 	lmap = sc->scratch;
    731   1.1        he 	lmap->size = htobe32(sc->maxunits * sizeof(lmap->map));
    732   1.1        he 	total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map);
    733   1.1        he 
    734   1.1        he 	ccb = ciss_get_ccb(sc);
    735   1.1        he 	ccb->ccb_len = total;
    736   1.1        he 	ccb->ccb_data = lmap;
    737   1.2    martti 	ccb->ccb_xs = NULL;
    738   1.1        he 	cmd = &ccb->ccb_cmd;
    739   1.1        he 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    740   1.1        he 	cmd->tgt2 = 0;
    741   1.1        he 	cmd->cdblen = 12;
    742   1.1        he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    743   1.1        he 	cmd->tmo = htole16(30);
    744   1.1        he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    745   1.1        he 	cmd->cdb[0] = CISS_CMD_LDMAP;
    746   1.1        he 	cmd->cdb[8] = total >> 8;	/* biiiig endian */
    747   1.1        he 	cmd->cdb[9] = total & 0xff;
    748   1.1        he 
    749   1.1        he 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    750   1.1        he 
    751  1.12    mhitch 	if (rv) {
    752  1.12    mhitch 		mutex_exit(&sc->sc_mutex_scratch);
    753   1.1        he 		return rv;
    754  1.12    mhitch 	}
    755   1.1        he 
    756   1.1        he 	CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n",
    757   1.1        he 	    lmap->map[0].tgt, lmap->map[0].tgt2));
    758   1.1        he 
    759  1.12    mhitch 	mutex_exit(&sc->sc_mutex_scratch);
    760   1.1        he 	return 0;
    761   1.1        he }
    762   1.1        he 
    763   1.1        he static int
    764   1.1        he ciss_sync(struct ciss_softc *sc)
    765   1.1        he {
    766   1.1        he 	struct ciss_ccb *ccb;
    767   1.1        he 	struct ciss_cmd *cmd;
    768   1.1        he 	struct ciss_flush *flush;
    769   1.1        he 	int rv;
    770   1.1        he 
    771  1.12    mhitch 	mutex_enter(&sc->sc_mutex_scratch);
    772   1.1        he 	flush = sc->scratch;
    773   1.1        he 	bzero(flush, sizeof(*flush));
    774   1.1        he 	flush->flush = sc->sc_flush;
    775   1.1        he 
    776   1.1        he 	ccb = ciss_get_ccb(sc);
    777   1.1        he 	ccb->ccb_len = sizeof(*flush);
    778   1.1        he 	ccb->ccb_data = flush;
    779   1.2    martti 	ccb->ccb_xs = NULL;
    780   1.1        he 	cmd = &ccb->ccb_cmd;
    781   1.1        he 	cmd->tgt = CISS_CMD_MODE_PERIPH;
    782   1.1        he 	cmd->tgt2 = 0;
    783   1.1        he 	cmd->cdblen = 10;
    784   1.1        he 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
    785   1.1        he 	cmd->tmo = 0;
    786   1.1        he 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    787   1.1        he 	cmd->cdb[0] = CISS_CMD_CTRL_SET;
    788   1.1        he 	cmd->cdb[6] = CISS_CMS_CTRL_FLUSH;
    789   1.1        he 	cmd->cdb[7] = sizeof(*flush) >> 8;	/* biiiig endian */
    790   1.1        he 	cmd->cdb[8] = sizeof(*flush) & 0xff;
    791   1.1        he 
    792   1.1        he 	rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
    793  1.12    mhitch 	mutex_exit(&sc->sc_mutex_scratch);
    794   1.1        he 
    795   1.1        he 	return rv;
    796   1.1        he }
    797   1.1        he 
    798  1.12    mhitch int
    799  1.12    mhitch ciss_ldid(struct ciss_softc *sc, int target, struct ciss_ldid *id)
    800  1.12    mhitch {
    801  1.12    mhitch 	struct ciss_ccb *ccb;
    802  1.12    mhitch 	struct ciss_cmd *cmd;
    803  1.12    mhitch 
    804  1.12    mhitch 	ccb = ciss_get_ccb(sc);
    805  1.12    mhitch 	if (ccb == NULL)
    806  1.12    mhitch 		return ENOMEM;
    807  1.12    mhitch 	ccb->ccb_len = sizeof(*id);
    808  1.12    mhitch 	ccb->ccb_data = id;
    809  1.12    mhitch 	ccb->ccb_xs = NULL;
    810  1.12    mhitch 	cmd = &ccb->ccb_cmd;
    811  1.12    mhitch 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    812  1.12    mhitch 	cmd->tgt2 = 0;
    813  1.12    mhitch 	cmd->cdblen = 10;
    814  1.12    mhitch 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    815  1.12    mhitch 	cmd->tmo = htole16(0);
    816  1.12    mhitch 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    817  1.12    mhitch 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    818  1.12    mhitch 	cmd->cdb[1] = target;
    819  1.12    mhitch 	cmd->cdb[6] = CISS_CMS_CTRL_LDIDEXT;
    820  1.12    mhitch 	cmd->cdb[7] = sizeof(*id) >> 8;	/* biiiig endian */
    821  1.12    mhitch 	cmd->cdb[8] = sizeof(*id) & 0xff;
    822  1.12    mhitch 
    823  1.12    mhitch 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL | sc->sc_waitflag);
    824  1.12    mhitch }
    825  1.12    mhitch 
    826  1.12    mhitch int
    827  1.12    mhitch ciss_ldstat(struct ciss_softc *sc, int target, struct ciss_ldstat *stat)
    828  1.12    mhitch {
    829  1.12    mhitch 	struct ciss_ccb *ccb;
    830  1.12    mhitch 	struct ciss_cmd *cmd;
    831  1.12    mhitch 
    832  1.12    mhitch 	ccb = ciss_get_ccb(sc);
    833  1.12    mhitch 	if (ccb == NULL)
    834  1.12    mhitch 		return ENOMEM;
    835  1.12    mhitch 	ccb->ccb_len = sizeof(*stat);
    836  1.12    mhitch 	ccb->ccb_data = stat;
    837  1.12    mhitch 	ccb->ccb_xs = NULL;
    838  1.12    mhitch 	cmd = &ccb->ccb_cmd;
    839  1.12    mhitch 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    840  1.12    mhitch 	cmd->tgt2 = 0;
    841  1.12    mhitch 	cmd->cdblen = 10;
    842  1.12    mhitch 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    843  1.12    mhitch 	cmd->tmo = htole16(0);
    844  1.12    mhitch 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    845  1.12    mhitch 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    846  1.12    mhitch 	cmd->cdb[1] = target;
    847  1.12    mhitch 	cmd->cdb[6] = CISS_CMS_CTRL_LDSTAT;
    848  1.12    mhitch 	cmd->cdb[7] = sizeof(*stat) >> 8;	/* biiiig endian */
    849  1.12    mhitch 	cmd->cdb[8] = sizeof(*stat) & 0xff;
    850  1.12    mhitch 
    851  1.12    mhitch 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL | sc->sc_waitflag);
    852  1.12    mhitch }
    853  1.12    mhitch 
    854  1.12    mhitch int
    855  1.12    mhitch ciss_pdid(struct ciss_softc *sc, u_int8_t drv, struct ciss_pdid *id, int wait)
    856  1.12    mhitch {
    857  1.12    mhitch 	struct ciss_ccb *ccb;
    858  1.12    mhitch 	struct ciss_cmd *cmd;
    859  1.12    mhitch 
    860  1.12    mhitch 	ccb = ciss_get_ccb(sc);
    861  1.12    mhitch 	if (ccb == NULL)
    862  1.12    mhitch 		return ENOMEM;
    863  1.12    mhitch 	ccb->ccb_len = sizeof(*id);
    864  1.12    mhitch 	ccb->ccb_data = id;
    865  1.12    mhitch 	ccb->ccb_xs = NULL;
    866  1.12    mhitch 	cmd = &ccb->ccb_cmd;
    867  1.12    mhitch 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
    868  1.12    mhitch 	cmd->tgt2 = 0;
    869  1.12    mhitch 	cmd->cdblen = 10;
    870  1.12    mhitch 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
    871  1.12    mhitch 	cmd->tmo = htole16(0);
    872  1.12    mhitch 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    873  1.12    mhitch 	cmd->cdb[0] = CISS_CMD_CTRL_GET;
    874  1.12    mhitch 	cmd->cdb[2] = drv;
    875  1.12    mhitch 	cmd->cdb[6] = CISS_CMS_CTRL_PDID;
    876  1.12    mhitch 	cmd->cdb[7] = sizeof(*id) >> 8;	/* biiiig endian */
    877  1.12    mhitch 	cmd->cdb[8] = sizeof(*id) & 0xff;
    878  1.12    mhitch 
    879  1.12    mhitch 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, wait);
    880  1.12    mhitch }
    881  1.12    mhitch 
    882  1.12    mhitch 
    883  1.12    mhitch struct ciss_ld *
    884  1.12    mhitch ciss_pdscan(struct ciss_softc *sc, int ld)
    885  1.12    mhitch {
    886  1.12    mhitch 	struct ciss_pdid *pdid;
    887  1.12    mhitch 	struct ciss_ld *ldp;
    888  1.12    mhitch 	u_int8_t drv, buf[128];
    889  1.12    mhitch 	int i, j, k = 0;
    890  1.12    mhitch 
    891  1.12    mhitch 	mutex_enter(&sc->sc_mutex_scratch);
    892  1.12    mhitch 	pdid = sc->scratch;
    893  1.12    mhitch 	for (i = 0; i < sc->nbus; i++)
    894  1.12    mhitch 		for (j = 0; j < sc->ndrives; j++) {
    895  1.12    mhitch 			drv = CISS_BIGBIT + i * sc->ndrives + j;
    896  1.12    mhitch 			if (!ciss_pdid(sc, drv, pdid, XS_CTL_POLL|XS_CTL_NOSLEEP))
    897  1.12    mhitch 				buf[k++] = drv;
    898  1.12    mhitch 		}
    899  1.12    mhitch 	mutex_exit(&sc->sc_mutex_scratch);
    900  1.12    mhitch 
    901  1.12    mhitch 	if (!k)
    902  1.12    mhitch 		return NULL;
    903  1.12    mhitch 
    904  1.12    mhitch 	ldp = malloc(sizeof(*ldp) + (k-1), M_DEVBUF, M_NOWAIT);
    905  1.12    mhitch 	if (!ldp)
    906  1.12    mhitch 		return NULL;
    907  1.12    mhitch 
    908  1.12    mhitch 	bzero(&ldp->bling, sizeof(ldp->bling));
    909  1.12    mhitch 	ldp->ndrives = k;
    910  1.12    mhitch 	ldp->xname[0] = 0;
    911  1.12    mhitch 	bcopy(buf, ldp->tgts, k);
    912  1.12    mhitch 	return ldp;
    913  1.12    mhitch }
    914  1.12    mhitch 
    915   1.1        he #if 0
    916   1.1        he static void
    917   1.1        he ciss_scsi_raw_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    918   1.1        he 	void *arg)				/* TODO */
    919   1.1        he {
    920   1.1        he 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    921   1.1        he 	struct ciss_rawsoftc *rsc =
    922   1.1        he 		(struct ciss_rawsoftc *) chan->chan_adapter->adapt_dev;
    923   1.1        he 	struct ciss_softc *sc = rsc->sc_softc;
    924   1.1        he 	struct ciss_ccb *ccb;
    925   1.1        he 	struct ciss_cmd *cmd;
    926   1.1        he 	int error;
    927   1.1        he 
    928   1.1        he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_raw_cmd "));
    929   1.1        he 
    930   1.1        he 	switch (req)
    931   1.1        he 	{
    932   1.1        he 	case ADAPTER_REQ_RUN_XFER:
    933   1.1        he 		if (xs->cmdlen > CISS_MAX_CDB) {
    934   1.1        he 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
    935   1.1        he 			bzero(&xs->sense, sizeof(xs->sense));
    936   1.1        he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    937  1.10     perry 			       __FILE__, __LINE__, __func__);
    938   1.1        he 			xs->error = XS_DRIVER_STUFFUP;
    939   1.1        he 			scsipi_done(xs);
    940   1.1        he 			break;
    941   1.1        he 		}
    942   1.1        he 
    943   1.1        he 		error = 0;
    944   1.1        he 		xs->error = XS_NOERROR;
    945   1.1        he 
    946   1.1        he 		/* TODO check this target has not yet employed w/ any volume */
    947   1.1        he 
    948   1.1        he 		ccb = ciss_get_ccb(sc);
    949   1.1        he 		cmd = &ccb->ccb_cmd;
    950   1.1        he 		ccb->ccb_len = xs->datalen;
    951   1.1        he 		ccb->ccb_data = xs->data;
    952   1.1        he 		ccb->ccb_xs = xs;
    953   1.1        he 
    954   1.1        he 		cmd->cdblen = xs->cmdlen;
    955   1.1        he 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
    956   1.1        he 		if (xs->xs_control & XS_CTL_DATA_IN)
    957   1.1        he 			cmd->flags |= CISS_CDB_IN;
    958   1.1        he 		else if (xs->xs_control & XS_CTL_DATA_OUT)
    959   1.1        he 			cmd->flags |= CISS_CDB_OUT;
    960   1.1        he 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
    961   1.1        he 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
    962   1.1        he 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
    963   1.1        he 
    964   1.1        he 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
    965   1.1        he 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
    966   1.1        he 			printf("ciss driver stuffup in %s:%d: %s()\n",
    967  1.10     perry 			       __FILE__, __LINE__, __func__);
    968   1.1        he 			xs->error = XS_DRIVER_STUFFUP;
    969   1.1        he 			scsipi_done(xs);
    970   1.1        he 			break;
    971   1.1        he 		}
    972   1.1        he 
    973   1.1        he 		break;
    974   1.1        he 
    975   1.1        he 	case ADAPTER_REQ_GROW_RESOURCES:
    976   1.1        he 		/*
    977   1.1        he 		 * Not supported.
    978   1.1        he 		 */
    979   1.1        he 		break;
    980   1.1        he 
    981   1.1        he 	case ADAPTER_REQ_SET_XFER_MODE:
    982   1.1        he 		/*
    983   1.1        he 		 * We can't change the transfer mode, but at least let
    984   1.1        he 		 * scsipi know what the adapter has negociated.
    985   1.1        he 		 */
    986   1.1        he 		 /* Get xfer mode and return it */
    987   1.1        he 		break;
    988   1.1        he 	}
    989   1.1        he }
    990   1.1        he #endif
    991   1.1        he 
    992   1.1        he static void
    993   1.1        he ciss_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    994   1.1        he 	void *arg)
    995   1.1        he {
    996   1.1        he 	struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
    997   1.1        he 	struct ciss_softc *sc =
    998   1.1        he 		(struct ciss_softc *) chan->chan_adapter->adapt_dev;
    999   1.1        he 	u_int8_t target;
   1000   1.1        he 	struct ciss_ccb *ccb;
   1001   1.1        he 	struct ciss_cmd *cmd;
   1002   1.1        he 	int error;
   1003   1.1        he 
   1004   1.1        he 	CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd "));
   1005   1.1        he 
   1006   1.1        he 	switch (req)
   1007   1.1        he 	{
   1008   1.1        he 	case ADAPTER_REQ_RUN_XFER:
   1009   1.1        he 		target = xs->xs_periph->periph_target;
   1010   1.1        he 		CISS_DPRINTF(CISS_D_CMD, ("targ=%d ", target));
   1011   1.1        he 		if (xs->cmdlen > CISS_MAX_CDB) {
   1012   1.1        he 			CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
   1013   1.1        he 			bzero(&xs->sense, sizeof(xs->sense));
   1014   1.1        he 			printf("ciss driver stuffup in %s:%d: %s()\n",
   1015  1.10     perry 			       __FILE__, __LINE__, __func__);
   1016   1.1        he 			xs->error = XS_DRIVER_STUFFUP;
   1017   1.1        he 			scsipi_done(xs);
   1018   1.1        he 			break;
   1019   1.1        he 		}
   1020   1.1        he 
   1021   1.1        he 		error = 0;
   1022   1.1        he 		xs->error = XS_NOERROR;
   1023   1.1        he 
   1024   1.1        he 		/* XXX emulate SYNCHRONIZE_CACHE ??? */
   1025   1.1        he 
   1026   1.1        he 		ccb = ciss_get_ccb(sc);
   1027   1.1        he 		cmd = &ccb->ccb_cmd;
   1028   1.1        he 		ccb->ccb_len = xs->datalen;
   1029   1.1        he 		ccb->ccb_data = xs->data;
   1030   1.1        he 		ccb->ccb_xs = xs;
   1031   1.1        he 		cmd->tgt = CISS_CMD_MODE_LD | target;
   1032   1.1        he 		cmd->tgt2 = 0;
   1033   1.1        he 		cmd->cdblen = xs->cmdlen;
   1034   1.1        he 		cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
   1035   1.1        he 		if (xs->xs_control & XS_CTL_DATA_IN)
   1036   1.1        he 			cmd->flags |= CISS_CDB_IN;
   1037   1.1        he 		else if (xs->xs_control & XS_CTL_DATA_OUT)
   1038   1.1        he 			cmd->flags |= CISS_CDB_OUT;
   1039   1.1        he 		cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
   1040   1.1        he 		bzero(&cmd->cdb[0], sizeof(cmd->cdb));
   1041   1.1        he 		bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
   1042   1.1        he 		CISS_DPRINTF(CISS_D_CMD, ("cmd=%02x %02x %02x %02x %02x %02x ",
   1043   1.1        he 			     cmd->cdb[0], cmd->cdb[1], cmd->cdb[2],
   1044   1.1        he 			     cmd->cdb[3], cmd->cdb[4], cmd->cdb[5]));
   1045   1.1        he 
   1046   1.1        he 		if (ciss_cmd(ccb, BUS_DMA_WAITOK,
   1047   1.1        he 		    xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
   1048   1.1        he 			printf("ciss driver stuffup in %s:%d: %s()\n",
   1049  1.10     perry 			       __FILE__, __LINE__, __func__);
   1050   1.1        he 			xs->error = XS_DRIVER_STUFFUP;
   1051   1.1        he 			scsipi_done(xs);
   1052   1.1        he 			return;
   1053   1.1        he 		}
   1054   1.1        he 
   1055   1.1        he 		break;
   1056   1.1        he 	case ADAPTER_REQ_GROW_RESOURCES:
   1057   1.1        he 		/*
   1058   1.1        he 		 * Not supported.
   1059   1.1        he 		 */
   1060   1.1        he 		break;
   1061   1.1        he 	case ADAPTER_REQ_SET_XFER_MODE:
   1062   1.1        he 		/*
   1063   1.1        he 		 * We can't change the transfer mode, but at least let
   1064   1.1        he 		 * scsipi know what the adapter has negociated.
   1065   1.1        he 		 */
   1066   1.1        he 		/* FIXME: get xfer mode and write it into arg */
   1067   1.1        he 		break;
   1068   1.1        he 	}
   1069   1.1        he }
   1070   1.1        he 
   1071   1.1        he int
   1072   1.1        he ciss_intr(void *v)
   1073   1.1        he {
   1074   1.1        he 	struct ciss_softc *sc = v;
   1075   1.1        he 	struct ciss_ccb *ccb;
   1076   1.1        he 	u_int32_t id;
   1077   1.1        he 	int hit = 0;
   1078   1.1        he 
   1079   1.1        he 	CISS_DPRINTF(CISS_D_INTR, ("intr "));
   1080   1.1        he 
   1081   1.1        he 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem))
   1082   1.1        he 		return 0;
   1083   1.1        he 
   1084   1.1        he 	while ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ)) !=
   1085   1.1        he 	    0xffffffff) {
   1086   1.1        he 
   1087   1.7  christos 		ccb = (struct ciss_ccb *) ((char *)sc->ccbs + (id >> 2) * sc->ccblen);
   1088   1.1        he 		ccb->ccb_cmd.id = htole32(id);
   1089   1.1        he 		if (ccb->ccb_state == CISS_CCB_POLL) {
   1090   1.1        he 			ccb->ccb_state = CISS_CCB_ONQ;
   1091  1.12    mhitch 			mutex_enter(&sc->sc_mutex);
   1092  1.12    mhitch 			cv_broadcast(&sc->sc_condvar);
   1093  1.12    mhitch 			mutex_exit(&sc->sc_mutex);
   1094   1.1        he 		} else
   1095   1.1        he 			ciss_done(ccb);
   1096   1.1        he 
   1097   1.1        he 		hit = 1;
   1098   1.1        he 	}
   1099   1.1        he 
   1100   1.1        he 	CISS_DPRINTF(CISS_D_INTR, ("exit\n"));
   1101   1.1        he 	return hit;
   1102   1.1        he }
   1103   1.1        he 
   1104   1.1        he static void
   1105   1.1        he ciss_heartbeat(void *v)
   1106   1.1        he {
   1107   1.1        he 	struct ciss_softc *sc = v;
   1108   1.1        he 	u_int32_t hb;
   1109   1.1        he 
   1110   1.1        he 	hb = bus_space_read_4(sc->sc_iot, sc->cfg_ioh,
   1111   1.1        he 	    sc->cfgoff + offsetof(struct ciss_config, heartbeat));
   1112   1.1        he 	if (hb == sc->heartbeat)
   1113   1.1        he 		panic("ciss: dead");	/* XX reset! */
   1114   1.1        he 	else
   1115   1.1        he 		sc->heartbeat = hb;
   1116   1.1        he 
   1117   1.1        he 	callout_schedule(&sc->sc_hb, hz * 3);
   1118   1.1        he }
   1119   1.1        he 
   1120   1.1        he static int
   1121   1.6  christos ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
   1122   1.7  christos     void *addr, int flag, struct proc *p)
   1123   1.1        he {
   1124   1.1        he #if NBIO > 0
   1125   1.1        he 	return ciss_ioctl(chan->chan_adapter->adapt_dev, cmd, addr);
   1126   1.1        he #else
   1127   1.1        he 	return ENOTTY;
   1128   1.1        he #endif
   1129   1.1        he }
   1130   1.1        he 
   1131   1.1        he #if NBIO > 0
   1132  1.12    mhitch const int ciss_level[] = { 0, 4, 1, 5, 51, 7 };
   1133  1.12    mhitch const int ciss_stat[] = { BIOC_SVONLINE, BIOC_SVOFFLINE, BIOC_SVOFFLINE,
   1134  1.12    mhitch     BIOC_SVDEGRADED, BIOC_SVREBUILD, BIOC_SVREBUILD, BIOC_SVDEGRADED,
   1135  1.12    mhitch     BIOC_SVDEGRADED, BIOC_SVINVALID, BIOC_SVINVALID, BIOC_SVBUILDING,
   1136  1.12    mhitch     BIOC_SVOFFLINE, BIOC_SVBUILDING };
   1137  1.12    mhitch 
   1138  1.12    mhitch int
   1139  1.12    mhitch ciss_ioctl(struct device *dev, u_long cmd, void *addr)
   1140   1.1        he {
   1141  1.12    mhitch 	struct ciss_softc	*sc = (struct ciss_softc *)dev;
   1142  1.12    mhitch 	struct bioc_inq *bi;
   1143  1.12    mhitch 	struct bioc_disk *bd;
   1144  1.12    mhitch 	struct bioc_blink *bb;
   1145  1.12    mhitch 	struct ciss_ldstat *ldstat;
   1146  1.12    mhitch 	struct ciss_pdid *pdid;
   1147  1.12    mhitch 	struct ciss_blink *blink;
   1148  1.12    mhitch 	struct ciss_ld *ldp;
   1149  1.12    mhitch 	int ld, pd, error = 0;
   1150   1.1        he 
   1151   1.1        he 	switch (cmd) {
   1152   1.1        he 	case BIOCINQ:
   1153  1.12    mhitch 		bi = (struct bioc_inq *)addr;
   1154  1.12    mhitch 		strlcpy(bi->bi_dev, device_xname(&sc->sc_dev), sizeof(bi->bi_dev));
   1155  1.12    mhitch 		bi->bi_novol = sc->maxunits;
   1156  1.12    mhitch 		bi->bi_nodisk = sc->sc_lds[0]->ndrives;
   1157  1.12    mhitch 		break;
   1158  1.12    mhitch 
   1159   1.1        he 	case BIOCVOL:
   1160  1.12    mhitch 		error = ciss_ioctl_vol(sc, (struct bioc_vol *)addr);
   1161  1.12    mhitch 		break;
   1162  1.12    mhitch 
   1163  1.12    mhitch 	case BIOCDISK_NOVOL:
   1164  1.12    mhitch /*
   1165  1.12    mhitch  * XXX since we don't know how to associate physical drives with logical drives
   1166  1.12    mhitch  * yet, BIOCDISK_NOVOL is equivalent to BIOCDISK to the volume that we've
   1167  1.12    mhitch  * associated all physical drives to.
   1168  1.12    mhitch  * Maybe assoicate all physical drives to all logical volumes, but only return
   1169  1.12    mhitch  * physical drives on one logical volume.  Which one?  Either 1st volume that
   1170  1.12    mhitch  * is degraded, rebuilding, or failed?
   1171  1.12    mhitch  */
   1172  1.12    mhitch 		bd = (struct bioc_disk *)addr;
   1173  1.12    mhitch 		bd->bd_volid = 0;
   1174  1.12    mhitch 		bd->bd_disknovol = true;
   1175  1.12    mhitch 		/* FALLTHROUGH */
   1176   1.1        he 	case BIOCDISK:
   1177  1.12    mhitch 		bd = (struct bioc_disk *)addr;
   1178  1.12    mhitch 		if (bd->bd_volid > sc->maxunits) {
   1179  1.12    mhitch 			error = EINVAL;
   1180  1.12    mhitch 			break;
   1181  1.12    mhitch 		}
   1182  1.12    mhitch 		ldp = sc->sc_lds[0];
   1183  1.12    mhitch 		if (!ldp || (pd = bd->bd_diskid) > ldp->ndrives) {
   1184  1.12    mhitch 			error = EINVAL;
   1185  1.12    mhitch 			break;
   1186  1.12    mhitch 		}
   1187  1.12    mhitch 		ldstat = sc->scratch;
   1188  1.12    mhitch 		if ((error = ciss_ldstat(sc, bd->bd_volid, ldstat))) {
   1189  1.12    mhitch 			break;
   1190  1.12    mhitch 		}
   1191  1.12    mhitch 		bd->bd_status = -1;
   1192  1.12    mhitch 		if (ldstat->bigrebuild == ldp->tgts[pd])
   1193  1.12    mhitch 			bd->bd_status = BIOC_SDREBUILD;
   1194  1.12    mhitch 		if (ciss_bitset(ldp->tgts[pd] & (~CISS_BIGBIT),
   1195  1.12    mhitch 		    ldstat->bigfailed)) {
   1196  1.12    mhitch 			bd->bd_status = BIOC_SDFAILED;
   1197  1.12    mhitch 			bd->bd_size = 0;
   1198  1.12    mhitch 			bd->bd_channel = (ldp->tgts[pd] & (~CISS_BIGBIT)) /
   1199  1.12    mhitch 			    sc->ndrives;
   1200  1.12    mhitch 			bd->bd_target = ldp->tgts[pd] % sc->ndrives;
   1201  1.12    mhitch 			bd->bd_lun = 0;
   1202  1.12    mhitch 			bd->bd_vendor[0] = '\0';
   1203  1.12    mhitch 			bd->bd_serial[0] = '\0';
   1204  1.12    mhitch 			bd->bd_procdev[0] = '\0';
   1205  1.12    mhitch 		} else {
   1206  1.12    mhitch 			pdid = sc->scratch;
   1207  1.12    mhitch 			if ((error = ciss_pdid(sc, ldp->tgts[pd], pdid,
   1208  1.12    mhitch 			    XS_CTL_POLL))) {
   1209  1.12    mhitch 				bd->bd_status = BIOC_SDFAILED;
   1210  1.12    mhitch 				bd->bd_size = 0;
   1211  1.12    mhitch 				bd->bd_channel = (ldp->tgts[pd] & (~CISS_BIGBIT)) /
   1212  1.12    mhitch 				    sc->ndrives;
   1213  1.12    mhitch 				bd->bd_target = ldp->tgts[pd] % sc->ndrives;
   1214  1.12    mhitch 				bd->bd_lun = 0;
   1215  1.12    mhitch 				bd->bd_vendor[0] = '\0';
   1216  1.12    mhitch 				bd->bd_serial[0] = '\0';
   1217  1.12    mhitch 				bd->bd_procdev[0] = '\0';
   1218  1.12    mhitch 				error = 0;
   1219  1.12    mhitch 				break;
   1220  1.12    mhitch 			}
   1221  1.12    mhitch 			if (bd->bd_status < 0) {
   1222  1.12    mhitch 				if (pdid->config & CISS_PD_SPARE)
   1223  1.12    mhitch 					bd->bd_status = BIOC_SDHOTSPARE;
   1224  1.12    mhitch 				else if (pdid->present & CISS_PD_PRESENT)
   1225  1.12    mhitch 					bd->bd_status = BIOC_SDONLINE;
   1226  1.12    mhitch 				else
   1227  1.12    mhitch 					bd->bd_status = BIOC_SDINVALID;
   1228  1.12    mhitch 			}
   1229  1.12    mhitch 			bd->bd_size = (u_int64_t)le32toh(pdid->nblocks) *
   1230  1.12    mhitch 			    le16toh(pdid->blksz);
   1231  1.12    mhitch 			bd->bd_channel = pdid->bus;
   1232  1.12    mhitch 			bd->bd_target = pdid->target;
   1233  1.12    mhitch 			bd->bd_lun = 0;
   1234  1.12    mhitch 			strlcpy(bd->bd_vendor, pdid->model,
   1235  1.12    mhitch 			    sizeof(bd->bd_vendor));
   1236  1.12    mhitch 			strlcpy(bd->bd_serial, pdid->serial,
   1237  1.12    mhitch 			    sizeof(bd->bd_serial));
   1238  1.12    mhitch 			bd->bd_procdev[0] = '\0';
   1239  1.12    mhitch 		}
   1240  1.12    mhitch 		break;
   1241  1.12    mhitch 
   1242  1.12    mhitch 	case BIOCBLINK:
   1243  1.12    mhitch 		bb = (struct bioc_blink *)addr;
   1244  1.12    mhitch 		blink = sc->scratch;
   1245  1.12    mhitch 		error = EINVAL;
   1246  1.12    mhitch 		/* XXX workaround completely dumb scsi addressing */
   1247  1.12    mhitch 		for (ld = 0; ld < sc->maxunits; ld++) {
   1248  1.12    mhitch 			ldp = sc->sc_lds[ld];
   1249  1.12    mhitch 			if (!ldp)
   1250  1.12    mhitch 				continue;
   1251  1.12    mhitch 			for (pd = 0; pd < ldp->ndrives; pd++)
   1252  1.12    mhitch 				if (ldp->tgts[pd] == (CISS_BIGBIT +
   1253  1.12    mhitch 				    bb->bb_channel * sc->ndrives +
   1254  1.12    mhitch 				    bb->bb_target))
   1255  1.12    mhitch 					error = ciss_blink(sc, ld, pd,
   1256  1.12    mhitch 					    bb->bb_status, blink);
   1257  1.12    mhitch 		}
   1258  1.12    mhitch 		break;
   1259  1.12    mhitch 
   1260   1.1        he 	case BIOCALARM:
   1261   1.1        he 	case BIOCSETSTATE:
   1262   1.1        he 	default:
   1263  1.12    mhitch 		error = EINVAL;
   1264   1.1        he 	}
   1265   1.1        he 
   1266  1.12    mhitch 	return (error);
   1267  1.12    mhitch }
   1268  1.12    mhitch 
   1269  1.12    mhitch int
   1270  1.12    mhitch ciss_ioctl_vol(struct ciss_softc *sc, struct bioc_vol *bv)
   1271  1.12    mhitch {
   1272  1.12    mhitch 	struct ciss_ldid *ldid;
   1273  1.12    mhitch 	struct ciss_ld *ldp;
   1274  1.12    mhitch 	struct ciss_ldstat *ldstat;
   1275  1.12    mhitch 	struct ciss_pdid *pdid;
   1276  1.12    mhitch 	int error = 0;
   1277  1.12    mhitch 	u_int blks;
   1278  1.12    mhitch 
   1279  1.12    mhitch 	if (bv->bv_volid > sc->maxunits) {
   1280  1.12    mhitch 		return EINVAL;
   1281  1.12    mhitch 	}
   1282  1.12    mhitch 	ldp = sc->sc_lds[bv->bv_volid];
   1283  1.12    mhitch 	ldid = sc->scratch;
   1284  1.12    mhitch 	if ((error = ciss_ldid(sc, bv->bv_volid, ldid))) {
   1285  1.12    mhitch 		return error;
   1286  1.12    mhitch 	}
   1287  1.12    mhitch 	bv->bv_status = BIOC_SVINVALID;
   1288  1.12    mhitch 	blks = (u_int)le16toh(ldid->nblocks[1]) << 16 |
   1289  1.12    mhitch 	    le16toh(ldid->nblocks[0]);
   1290  1.12    mhitch 	bv->bv_size = blks * (u_quad_t)le16toh(ldid->blksize);
   1291  1.12    mhitch 	bv->bv_level = ciss_level[ldid->type];
   1292  1.12    mhitch /*
   1293  1.12    mhitch  * XXX Should only return bv_nodisk for logigal volume that we've associated
   1294  1.12    mhitch  * the physical drives to:  either the 1st degraded, rebuilding, or failed
   1295  1.12    mhitch  * volume else volume 0?
   1296  1.12    mhitch  */
   1297  1.12    mhitch 	if (ldp) {
   1298  1.12    mhitch 		bv->bv_nodisk = ldp->ndrives;
   1299  1.12    mhitch 		strlcpy(bv->bv_dev, ldp->xname, sizeof(bv->bv_dev));
   1300  1.12    mhitch 	}
   1301  1.12    mhitch 	strlcpy(bv->bv_vendor, "CISS", sizeof(bv->bv_vendor));
   1302  1.12    mhitch 	ldstat = sc->scratch;
   1303  1.12    mhitch 	bzero(ldstat, sizeof(*ldstat));
   1304  1.12    mhitch 	if ((error = ciss_ldstat(sc, bv->bv_volid, ldstat))) {
   1305  1.12    mhitch 		return error;
   1306  1.12    mhitch 	}
   1307  1.12    mhitch 	bv->bv_percent = -1;
   1308  1.12    mhitch 	bv->bv_seconds = 0;
   1309  1.12    mhitch 	if (ldstat->stat < sizeof(ciss_stat)/sizeof(ciss_stat[0]))
   1310  1.12    mhitch 		bv->bv_status = ciss_stat[ldstat->stat];
   1311  1.12    mhitch 	if (bv->bv_status == BIOC_SVREBUILD ||
   1312  1.12    mhitch 	    bv->bv_status == BIOC_SVBUILDING) {
   1313  1.12    mhitch 	 	u_int64_t prog;
   1314  1.12    mhitch 
   1315  1.12    mhitch 		ldp = sc->sc_lds[0];
   1316  1.12    mhitch 		if (ldp) {
   1317  1.12    mhitch 			bv->bv_nodisk = ldp->ndrives;
   1318  1.12    mhitch 			strlcpy(bv->bv_dev, ldp->xname, sizeof(bv->bv_dev));
   1319  1.12    mhitch 		}
   1320  1.12    mhitch /*
   1321  1.12    mhitch  * XXX ldstat->prog is blocks remaining on physical drive being rebuilt
   1322  1.12    mhitch  * blks is only correct for a RAID1 set;  RAID5 needs to determine the
   1323  1.12    mhitch  * size of the physical device - which we don't yet know.
   1324  1.12    mhitch  * ldstat->bigrebuild has physical device target, so could be used with
   1325  1.12    mhitch  * pdid to get size.   Another way is to save pd information in sc so it's
   1326  1.12    mhitch  * easy to reference.
   1327  1.12    mhitch  */
   1328  1.12    mhitch 		prog = (u_int64_t)((ldstat->prog[3] << 24) |
   1329  1.12    mhitch 		    (ldstat->prog[2] << 16) | (ldstat->prog[1] << 8) |
   1330  1.12    mhitch 		    ldstat->prog[0]);
   1331  1.12    mhitch 		pdid = sc->scratch;
   1332  1.12    mhitch 		if (!ciss_pdid(sc, ldstat->bigrebuild, pdid, XS_CTL_POLL)) {
   1333  1.12    mhitch 			blks = le32toh(pdid->nblocks);
   1334  1.12    mhitch 			bv->bv_percent = (blks - prog) * 1000ULL / blks;
   1335  1.12    mhitch 		 }
   1336  1.12    mhitch 	}
   1337  1.12    mhitch 	return 0;
   1338  1.12    mhitch }
   1339  1.12    mhitch 
   1340  1.12    mhitch int
   1341  1.12    mhitch ciss_blink(struct ciss_softc *sc, int ld, int pd, int stat,
   1342  1.12    mhitch     struct ciss_blink *blink)
   1343  1.12    mhitch {
   1344  1.12    mhitch 	struct ciss_ccb *ccb;
   1345  1.12    mhitch 	struct ciss_cmd *cmd;
   1346  1.12    mhitch 	struct ciss_ld *ldp;
   1347  1.12    mhitch 
   1348  1.12    mhitch 	if (ld > sc->maxunits)
   1349  1.12    mhitch 		return EINVAL;
   1350  1.12    mhitch 
   1351  1.12    mhitch 	ldp = sc->sc_lds[ld];
   1352  1.12    mhitch 	if (!ldp || pd > ldp->ndrives)
   1353  1.12    mhitch 		return EINVAL;
   1354  1.12    mhitch 
   1355  1.12    mhitch 	ldp->bling.pdtab[ldp->tgts[pd]] = stat == BIOC_SBUNBLINK? 0 :
   1356  1.12    mhitch 	    CISS_BLINK_ALL;
   1357  1.12    mhitch 	bcopy(&ldp->bling, blink, sizeof(*blink));
   1358  1.12    mhitch 
   1359  1.12    mhitch 	ccb = ciss_get_ccb(sc);
   1360  1.12    mhitch 	if (ccb == NULL)
   1361  1.12    mhitch 		return ENOMEM;
   1362  1.12    mhitch 	ccb->ccb_len = sizeof(*blink);
   1363  1.12    mhitch 	ccb->ccb_data = blink;
   1364  1.12    mhitch 	ccb->ccb_xs = NULL;
   1365  1.12    mhitch 	cmd = &ccb->ccb_cmd;
   1366  1.12    mhitch 	cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
   1367  1.12    mhitch 	cmd->tgt2 = 0;
   1368  1.12    mhitch 	cmd->cdblen = 10;
   1369  1.12    mhitch 	cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
   1370  1.12    mhitch 	cmd->tmo = htole16(0);
   1371  1.12    mhitch 	bzero(&cmd->cdb[0], sizeof(cmd->cdb));
   1372  1.12    mhitch 	cmd->cdb[0] = CISS_CMD_CTRL_SET;
   1373  1.12    mhitch 	cmd->cdb[6] = CISS_CMS_CTRL_PDBLINK;
   1374  1.12    mhitch 	cmd->cdb[7] = sizeof(*blink) >> 8;	/* biiiig endian */
   1375  1.12    mhitch 	cmd->cdb[8] = sizeof(*blink) & 0xff;
   1376  1.12    mhitch 
   1377  1.12    mhitch 	return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL);
   1378  1.12    mhitch }
   1379  1.12    mhitch 
   1380  1.12    mhitch int
   1381  1.12    mhitch ciss_create_sensors(struct ciss_softc *sc)
   1382  1.12    mhitch {
   1383  1.12    mhitch 	int			i;
   1384  1.12    mhitch 	int nsensors = sc->maxunits;
   1385  1.12    mhitch 
   1386  1.12    mhitch 	sc->sc_sme = sysmon_envsys_create();
   1387  1.12    mhitch 	sc->sc_sensor = malloc(sizeof(envsys_data_t) * nsensors,
   1388  1.12    mhitch 		M_DEVBUF, M_NOWAIT | M_ZERO);
   1389  1.12    mhitch 	if (sc->sc_sensor == NULL) {
   1390  1.12    mhitch 		aprint_error_dev(&sc->sc_dev, "can't allocate envsys_data");
   1391  1.12    mhitch 		return(ENOMEM);
   1392  1.12    mhitch 	}
   1393  1.12    mhitch 
   1394  1.12    mhitch 	for (i = 0; i < nsensors; i++) {
   1395  1.12    mhitch 		sc->sc_sensor[i].units = ENVSYS_DRIVE;
   1396  1.12    mhitch 		sc->sc_sensor[i].monitor = true;
   1397  1.12    mhitch 		/* Enable monitoring for drive state changes */
   1398  1.12    mhitch 		sc->sc_sensor[i].flags |= ENVSYS_FMONSTCHANGED;
   1399  1.12    mhitch 		/* logical drives */
   1400  1.12    mhitch 		snprintf(sc->sc_sensor[i].desc,
   1401  1.12    mhitch 		    sizeof(sc->sc_sensor[i].desc), "%s:%d",
   1402  1.12    mhitch 		    device_xname(&sc->sc_dev), i);
   1403  1.12    mhitch 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
   1404  1.12    mhitch 		    &sc->sc_sensor[i]))
   1405  1.12    mhitch 			goto out;
   1406  1.12    mhitch 	}
   1407  1.12    mhitch 
   1408  1.12    mhitch 	sc->sc_sme->sme_name = device_xname(&sc->sc_dev);
   1409  1.12    mhitch 	sc->sc_sme->sme_cookie = sc;
   1410  1.12    mhitch 	sc->sc_sme->sme_refresh = ciss_sensor_refresh;
   1411  1.12    mhitch 	if (sysmon_envsys_register(sc->sc_sme)) {
   1412  1.12    mhitch 		printf("%s: unable to register with sysmon\n", device_xname(&sc->sc_dev));
   1413  1.12    mhitch 		return(1);
   1414  1.12    mhitch 	}
   1415  1.12    mhitch 	return (0);
   1416  1.12    mhitch 
   1417  1.12    mhitch out:
   1418  1.12    mhitch 	free(sc->sc_sensor, M_DEVBUF);
   1419  1.12    mhitch 	sysmon_envsys_destroy(sc->sc_sme);
   1420  1.12    mhitch 	return EINVAL;
   1421  1.12    mhitch }
   1422  1.12    mhitch 
   1423  1.12    mhitch void
   1424  1.12    mhitch ciss_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
   1425  1.12    mhitch {
   1426  1.12    mhitch 	struct ciss_softc	*sc = sme->sme_cookie;
   1427  1.12    mhitch 	struct bioc_vol		bv;
   1428  1.12    mhitch 
   1429  1.12    mhitch 	if (edata->sensor >= sc->maxunits)
   1430  1.12    mhitch 		return;
   1431  1.12    mhitch 
   1432  1.12    mhitch 	bzero(&bv, sizeof(bv));
   1433  1.12    mhitch 	bv.bv_volid = edata->sensor;
   1434  1.12    mhitch 	if (ciss_ioctl_vol(sc, &bv)) {
   1435  1.12    mhitch 		return;
   1436  1.12    mhitch 	}
   1437  1.12    mhitch 
   1438  1.12    mhitch 	switch(bv.bv_status) {
   1439  1.12    mhitch 	case BIOC_SVOFFLINE:
   1440  1.12    mhitch 		edata->value_cur = ENVSYS_DRIVE_FAIL;
   1441  1.12    mhitch 		edata->state = ENVSYS_SCRITICAL;
   1442  1.12    mhitch 		break;
   1443  1.12    mhitch 
   1444  1.12    mhitch 	case BIOC_SVDEGRADED:
   1445  1.12    mhitch 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
   1446  1.12    mhitch 		edata->state = ENVSYS_SCRITICAL;
   1447  1.12    mhitch 		break;
   1448  1.12    mhitch 
   1449  1.12    mhitch 	case BIOC_SVSCRUB:
   1450  1.12    mhitch 	case BIOC_SVONLINE:
   1451  1.12    mhitch 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
   1452  1.12    mhitch 		edata->state = ENVSYS_SVALID;
   1453  1.12    mhitch 		break;
   1454  1.12    mhitch 
   1455  1.12    mhitch 	case BIOC_SVREBUILD:
   1456  1.12    mhitch 	case BIOC_SVBUILDING:
   1457  1.12    mhitch 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
   1458  1.12    mhitch 		edata->state = ENVSYS_SVALID;
   1459  1.12    mhitch 		break;
   1460  1.12    mhitch 
   1461  1.12    mhitch 	case BIOC_SVINVALID:
   1462  1.12    mhitch 		/* FALLTRHOUGH */
   1463  1.12    mhitch 	default:
   1464  1.12    mhitch 		edata->value_cur = 0; /* unknown */
   1465  1.12    mhitch 		edata->state = ENVSYS_SINVALID;
   1466  1.12    mhitch 	}
   1467   1.1        he }
   1468  1.12    mhitch #endif /* NBIO > 0 */
   1469