Home | History | Annotate | Line # | Download | only in ic
ld_icp.c revision 1.16
      1  1.16  christos /*	$NetBSD: ld_icp.c,v 1.16 2006/11/16 01:32:51 christos Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4   1.1        ad  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1        ad  * by Andrew Doran.
      9   1.1        ad  *
     10   1.1        ad  * Redistribution and use in source and binary forms, with or without
     11   1.1        ad  * modification, are permitted provided that the following conditions
     12   1.1        ad  * are met:
     13   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        ad  *    documentation and/or other materials provided with the distribution.
     18   1.1        ad  * 3. All advertising materials mentioning features or use of this software
     19   1.1        ad  *    must display the following acknowledgement:
     20   1.1        ad  *        This product includes software developed by the NetBSD
     21   1.1        ad  *        Foundation, Inc. and its contributors.
     22   1.1        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1        ad  *    contributors may be used to endorse or promote products derived
     24   1.1        ad  *    from this software without specific prior written permission.
     25   1.1        ad  *
     26   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1        ad  */
     38   1.1        ad 
     39   1.1        ad /*
     40   1.1        ad  * ICP-Vortex "GDT" front-end for ld(4) driver.
     41   1.1        ad  */
     42   1.1        ad 
     43   1.1        ad #include <sys/cdefs.h>
     44  1.16  christos __KERNEL_RCSID(0, "$NetBSD: ld_icp.c,v 1.16 2006/11/16 01:32:51 christos Exp $");
     45   1.1        ad 
     46   1.1        ad #include "rnd.h"
     47   1.1        ad 
     48   1.1        ad #include <sys/param.h>
     49   1.1        ad #include <sys/systm.h>
     50   1.1        ad #include <sys/kernel.h>
     51   1.1        ad #include <sys/device.h>
     52   1.1        ad #include <sys/buf.h>
     53   1.9      yamt #include <sys/bufq.h>
     54   1.1        ad #include <sys/endian.h>
     55   1.1        ad #include <sys/dkio.h>
     56   1.1        ad #include <sys/disk.h>
     57   1.1        ad #if NRND > 0
     58   1.1        ad #include <sys/rnd.h>
     59   1.1        ad #endif
     60   1.1        ad 
     61   1.1        ad #include <uvm/uvm_extern.h>
     62   1.1        ad 
     63   1.1        ad #include <machine/bus.h>
     64   1.1        ad 
     65   1.1        ad #include <dev/ldvar.h>
     66   1.1        ad 
     67   1.1        ad #include <dev/ic/icpreg.h>
     68   1.1        ad #include <dev/ic/icpvar.h>
     69   1.1        ad 
     70   1.1        ad struct ld_icp_softc {
     71   1.1        ad 	struct	ld_softc sc_ld;
     72   1.1        ad 	int	sc_hwunit;
     73   1.1        ad };
     74   1.1        ad 
     75   1.1        ad void	ld_icp_attach(struct device *, struct device *, void *);
     76   1.8   thorpej int	ld_icp_detach(struct device *, int);
     77   1.1        ad int	ld_icp_dobio(struct ld_icp_softc *, void *, int, int, int,
     78   1.1        ad 		     struct buf *);
     79   1.1        ad int	ld_icp_dump(struct ld_softc *, void *, int, int);
     80   1.1        ad int	ld_icp_flush(struct ld_softc *);
     81   1.1        ad void	ld_icp_intr(struct icp_ccb *);
     82   1.1        ad int	ld_icp_match(struct device *, struct cfdata *, void *);
     83   1.1        ad int	ld_icp_start(struct ld_softc *, struct buf *);
     84   1.1        ad 
     85   1.8   thorpej void	ld_icp_adjqparam(struct device *, int);
     86   1.8   thorpej 
     87   1.4   thorpej CFATTACH_DECL(ld_icp, sizeof(struct ld_icp_softc),
     88   1.8   thorpej     ld_icp_match, ld_icp_attach, ld_icp_detach, NULL);
     89   1.8   thorpej 
     90   1.8   thorpej static const struct icp_servicecb ld_icp_servicecb = {
     91   1.8   thorpej 	ld_icp_adjqparam,
     92   1.8   thorpej };
     93   1.1        ad 
     94   1.1        ad int
     95  1.16  christos ld_icp_match(struct device *parent, struct cfdata *match,
     96  1.15  christos     void *aux)
     97   1.1        ad {
     98   1.1        ad 	struct icp_attach_args *icpa;
     99   1.1        ad 
    100   1.1        ad 	icpa = aux;
    101   1.1        ad 
    102   1.1        ad 	return (icpa->icpa_unit < ICPA_UNIT_SCSI);
    103   1.1        ad }
    104   1.1        ad 
    105   1.1        ad void
    106   1.1        ad ld_icp_attach(struct device *parent, struct device *self, void *aux)
    107   1.1        ad {
    108   1.1        ad 	struct icp_attach_args *icpa;
    109   1.1        ad 	struct ld_icp_softc *sc;
    110   1.1        ad 	struct ld_softc *ld;
    111   1.1        ad 	struct icp_softc *icp;
    112   1.1        ad 	struct icp_cachedrv *cd;
    113   1.1        ad 	struct icp_cdevinfo *cdi;
    114   1.1        ad 	const char *str;
    115   1.1        ad 	int t;
    116   1.1        ad 
    117   1.1        ad 	sc = (struct ld_icp_softc *)self;
    118   1.1        ad 	ld = &sc->sc_ld;
    119   1.1        ad 	icp = (struct icp_softc *)parent;
    120   1.1        ad 	icpa = aux;
    121   1.1        ad 	cd = &icp->icp_cdr[icpa->icpa_unit];
    122   1.1        ad 
    123   1.8   thorpej 	icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb);
    124   1.8   thorpej 
    125   1.1        ad 	sc->sc_hwunit = icpa->icpa_unit;
    126   1.1        ad 	ld->sc_maxxfer = ICP_MAX_XFER;
    127   1.1        ad 	ld->sc_secsize = ICP_SECTOR_SIZE;
    128   1.1        ad 	ld->sc_start = ld_icp_start;
    129   1.1        ad 	ld->sc_dump = ld_icp_dump;
    130   1.1        ad 	ld->sc_flush = ld_icp_flush;
    131   1.1        ad 	ld->sc_secperunit = cd->cd_size;
    132   1.1        ad 	ld->sc_flags = LDF_ENABLED;
    133   1.1        ad 	ld->sc_maxqueuecnt = icp->icp_openings;
    134   1.1        ad 
    135   1.1        ad 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
    136   1.1        ad 	    sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
    137  1.10    briggs 		aprint_error(": unable to retrieve device info\n");
    138   1.1        ad 		ld->sc_flags = LDF_ENABLED;
    139   1.1        ad 		goto out;
    140   1.1        ad 	}
    141   1.1        ad 	cdi = (struct icp_cdevinfo *)icp->icp_scr;
    142   1.1        ad 
    143  1.10    briggs 	aprint_normal(": <%.8s>, ", cdi->ld_name);
    144   1.1        ad 	t = le32toh(cdi->ld_dtype) >> 16;
    145   1.1        ad 
    146   1.1        ad 	/*
    147   1.1        ad 	 * Print device type.
    148   1.1        ad 	 */
    149   1.1        ad 	if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
    150   1.1        ad 		str = "RAID-1";
    151   1.1        ad 	else if (t == 0)
    152   1.1        ad 		str = "JBOD";
    153   1.1        ad 	else if (t == 1)
    154   1.1        ad 		str = "RAID-0";
    155   1.1        ad 	else if (t == 2)
    156   1.1        ad 		str = "Chain";
    157   1.1        ad 	else
    158   1.1        ad 		str = "unknown type";
    159   1.1        ad 
    160  1.10    briggs 	aprint_normal("type: %s, ", str);
    161   1.1        ad 
    162   1.1        ad 	/*
    163   1.1        ad 	 * Print device status.
    164   1.1        ad 	 */
    165   1.1        ad 	if (t > 2)
    166   1.1        ad 		str = "missing";
    167   1.1        ad 	else if ((cdi->ld_error & 1) != 0) {
    168   1.1        ad 		str = "fault";
    169   1.1        ad 		ld->sc_flags = LDF_ENABLED;
    170   1.1        ad 	} else if ((cdi->ld_error & 2) != 0)
    171   1.1        ad 		str = "invalid";
    172   1.1        ad 	else {
    173   1.1        ad 		str = "optimal";
    174   1.1        ad 		ld->sc_flags = LDF_ENABLED;
    175   1.1        ad 	}
    176  1.11     perry 
    177  1.10    briggs 	aprint_normal("status: %s\n", str);
    178   1.1        ad 
    179   1.1        ad  out:
    180   1.1        ad 	ldattach(ld);
    181   1.1        ad }
    182   1.1        ad 
    183   1.1        ad int
    184   1.8   thorpej ld_icp_detach(struct device *dv, int flags)
    185   1.8   thorpej {
    186   1.8   thorpej 	int rv;
    187   1.8   thorpej 
    188   1.8   thorpej 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
    189   1.8   thorpej 		return (rv);
    190   1.8   thorpej 	ldenddetach((struct ld_softc *) dv);
    191   1.8   thorpej 
    192   1.8   thorpej 	return (0);
    193   1.8   thorpej }
    194   1.8   thorpej 
    195   1.8   thorpej int
    196   1.1        ad ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
    197   1.1        ad 	     int dowrite, struct buf *bp)
    198   1.1        ad {
    199   1.1        ad 	struct icp_cachecmd *cc;
    200   1.1        ad 	struct icp_ccb *ic;
    201   1.1        ad 	struct icp_softc *icp;
    202   1.1        ad 	int s, rv;
    203   1.1        ad 
    204  1.13   thorpej 	icp = (struct icp_softc *)device_parent(&sc->sc_ld.sc_dv);
    205   1.1        ad 
    206   1.1        ad 	/*
    207   1.1        ad 	 * Allocate a command control block.
    208   1.1        ad 	 */
    209   1.7   thorpej 	if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL))
    210   1.6   thorpej 		return (EAGAIN);
    211   1.1        ad 
    212   1.1        ad 	/*
    213   1.1        ad 	 * Map the data transfer.
    214   1.1        ad 	 */
    215   1.1        ad 	cc = &ic->ic_cmd.cmd_packet.cc;
    216   1.1        ad 	ic->ic_sg = cc->cc_sg;
    217   1.1        ad 	ic->ic_service = ICP_CACHESERVICE;
    218   1.1        ad 
    219   1.1        ad 	rv = icp_ccb_map(icp, ic, data, datasize,
    220   1.1        ad 	    dowrite ? IC_XFER_OUT : IC_XFER_IN);
    221   1.1        ad 	if (rv != 0) {
    222   1.1        ad 		icp_ccb_free(icp, ic);
    223   1.1        ad 		return (rv);
    224   1.1        ad 	}
    225   1.1        ad 
    226   1.1        ad 	/*
    227   1.1        ad 	 * Build the command.
    228   1.1        ad 	 */
    229   1.1        ad 	ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
    230   1.1        ad 	cc->cc_deviceno = htole16(sc->sc_hwunit);
    231   1.1        ad 	cc->cc_blockno = htole32(blkno);
    232   1.1        ad 	cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
    233   1.1        ad 	cc->cc_addr = ~0;	/* scatter gather */
    234   1.1        ad 	cc->cc_nsgent = htole32(ic->ic_nsgent);
    235   1.1        ad 
    236   1.1        ad 	ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
    237   1.1        ad 	    ic->ic_nsgent * sizeof(*ic->ic_sg);
    238   1.1        ad 
    239   1.1        ad 	/*
    240   1.1        ad 	 * Fire it off to the controller.
    241   1.1        ad 	 */
    242   1.1        ad 	if (bp == NULL) {
    243   1.1        ad 		s = splbio();
    244   1.1        ad 		rv = icp_ccb_poll(icp, ic, 10000);
    245   1.1        ad 		icp_ccb_unmap(icp, ic);
    246   1.1        ad 		icp_ccb_free(icp, ic);
    247   1.1        ad 		splx(s);
    248   1.1        ad 	} else {
    249   1.1        ad  		ic->ic_intr = ld_icp_intr;
    250   1.1        ad 		ic->ic_context = bp;
    251   1.1        ad 		ic->ic_dv = &sc->sc_ld.sc_dv;
    252   1.1        ad 		icp_ccb_enqueue(icp, ic);
    253   1.1        ad 	}
    254   1.1        ad 
    255   1.1        ad 	return (rv);
    256   1.1        ad }
    257   1.1        ad 
    258   1.1        ad int
    259   1.1        ad ld_icp_start(struct ld_softc *ld, struct buf *bp)
    260   1.1        ad {
    261   1.1        ad 
    262   1.1        ad 	return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
    263   1.1        ad 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    264   1.1        ad }
    265   1.1        ad 
    266   1.1        ad int
    267   1.1        ad ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    268   1.1        ad {
    269   1.1        ad 
    270   1.1        ad 	return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
    271   1.1        ad 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    272   1.1        ad }
    273   1.1        ad 
    274   1.1        ad int
    275   1.1        ad ld_icp_flush(struct ld_softc *ld)
    276   1.1        ad {
    277   1.1        ad 	struct ld_icp_softc *sc;
    278   1.1        ad 	struct icp_softc *icp;
    279   1.1        ad 	struct icp_cachecmd *cc;
    280   1.1        ad 	struct icp_ccb *ic;
    281   1.1        ad 	int rv;
    282   1.1        ad 
    283   1.1        ad 	sc = (struct ld_icp_softc *)ld;
    284  1.13   thorpej 	icp = (struct icp_softc *)device_parent(&ld->sc_dv);
    285   1.1        ad 
    286   1.6   thorpej 	ic = icp_ccb_alloc_wait(icp);
    287   1.1        ad 	ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
    288   1.1        ad 
    289   1.1        ad 	cc = &ic->ic_cmd.cmd_packet.cc;
    290   1.1        ad 	cc->cc_deviceno = htole16(sc->sc_hwunit);
    291   1.1        ad 	cc->cc_blockno = htole32(1);
    292   1.1        ad 	cc->cc_blockcnt = 0;
    293   1.1        ad 	cc->cc_addr = 0;
    294   1.1        ad 	cc->cc_nsgent = 0;
    295   1.1        ad 
    296   1.1        ad 	ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
    297   1.1        ad 	ic->ic_service = ICP_CACHESERVICE;
    298   1.1        ad 
    299   1.1        ad 	rv = icp_ccb_wait(icp, ic, 30000);
    300   1.1        ad 	icp_ccb_free(icp, ic);
    301   1.1        ad 
    302   1.1        ad 	return (rv);
    303   1.1        ad }
    304   1.1        ad 
    305   1.1        ad void
    306   1.1        ad ld_icp_intr(struct icp_ccb *ic)
    307   1.1        ad {
    308   1.1        ad 	struct buf *bp;
    309   1.1        ad 	struct ld_icp_softc *sc;
    310   1.1        ad 	struct icp_softc *icp;
    311   1.1        ad 
    312   1.1        ad 	bp = ic->ic_context;
    313   1.1        ad 	sc = (struct ld_icp_softc *)ic->ic_dv;
    314  1.13   thorpej 	icp = (struct icp_softc *)device_parent(&sc->sc_ld.sc_dv);
    315   1.1        ad 
    316   1.1        ad 	if (ic->ic_status != ICP_S_OK) {
    317   1.1        ad 		printf("%s: request failed; status=0x%04x\n",
    318   1.1        ad 		    ic->ic_dv->dv_xname, ic->ic_status);
    319   1.1        ad 		bp->b_flags |= B_ERROR;
    320   1.1        ad 		bp->b_error = EIO;
    321   1.1        ad 		bp->b_resid = bp->b_bcount;
    322   1.6   thorpej 
    323   1.6   thorpej 		icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync);
    324  1.14   thorpej 		icp->icp_evt.eu.sync.ionode = device_unit(&icp->icp_dv);
    325   1.6   thorpej 		icp->icp_evt.eu.sync.service = icp->icp_service;
    326   1.6   thorpej 		icp->icp_evt.eu.sync.status = icp->icp_status;
    327   1.6   thorpej 		icp->icp_evt.eu.sync.info = icp->icp_info;
    328   1.6   thorpej 		icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit;
    329   1.6   thorpej 		if (icp->icp_status >= 0x8000)
    330   1.6   thorpej 			icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt);
    331   1.6   thorpej 		else
    332   1.6   thorpej 			icp_store_event(icp, GDT_ES_SYNC, icp->icp_service,
    333   1.6   thorpej 			    &icp->icp_evt);
    334   1.1        ad 	} else
    335   1.1        ad 		bp->b_resid = 0;
    336   1.1        ad 
    337   1.1        ad 	icp_ccb_unmap(icp, ic);
    338   1.1        ad 	icp_ccb_free(icp, ic);
    339   1.1        ad 	lddone(&sc->sc_ld, bp);
    340   1.8   thorpej }
    341   1.8   thorpej 
    342   1.8   thorpej void
    343   1.8   thorpej ld_icp_adjqparam(struct device *dv, int openings)
    344   1.8   thorpej {
    345   1.8   thorpej 
    346   1.8   thorpej 	ldadjqparam((struct ld_softc *) dv, openings);
    347   1.1        ad }
    348