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