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