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