Home | History | Annotate | Line # | Download | only in ic
ld_icp.c revision 1.6
      1 /*	$NetBSD: ld_icp.c,v 1.6 2003/05/13 15:42:34 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.6 2003/05/13 15:42:34 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 CFATTACH_DECL(ld_icp, sizeof(struct ld_icp_softc),
     84     ld_icp_match, ld_icp_attach, NULL, NULL);
     85 
     86 int
     87 ld_icp_match(struct device *parent, struct cfdata *match, void *aux)
     88 {
     89 	struct icp_attach_args *icpa;
     90 
     91 	icpa = aux;
     92 
     93 	return (icpa->icpa_unit < ICPA_UNIT_SCSI);
     94 }
     95 
     96 void
     97 ld_icp_attach(struct device *parent, struct device *self, void *aux)
     98 {
     99 	struct icp_attach_args *icpa;
    100 	struct ld_icp_softc *sc;
    101 	struct ld_softc *ld;
    102 	struct icp_softc *icp;
    103 	struct icp_cachedrv *cd;
    104 	struct icp_cdevinfo *cdi;
    105 	const char *str;
    106 	int t;
    107 
    108 	sc = (struct ld_icp_softc *)self;
    109 	ld = &sc->sc_ld;
    110 	icp = (struct icp_softc *)parent;
    111 	icpa = aux;
    112 	cd = &icp->icp_cdr[icpa->icpa_unit];
    113 
    114 	sc->sc_hwunit = icpa->icpa_unit;
    115 	ld->sc_maxxfer = ICP_MAX_XFER;
    116 	ld->sc_secsize = ICP_SECTOR_SIZE;
    117 	ld->sc_start = ld_icp_start;
    118 	ld->sc_dump = ld_icp_dump;
    119 	ld->sc_flush = ld_icp_flush;
    120 	ld->sc_secperunit = cd->cd_size;
    121 	ld->sc_flags = LDF_ENABLED;
    122 	ld->sc_maxqueuecnt = icp->icp_openings;
    123 
    124 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
    125 	    sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
    126 		printf(": unable to retrieve device info\n");
    127 		ld->sc_flags = LDF_ENABLED;
    128 		goto out;
    129 	}
    130 	cdi = (struct icp_cdevinfo *)icp->icp_scr;
    131 
    132 	printf(": <%.8s>, ", cdi->ld_name);
    133 	t = le32toh(cdi->ld_dtype) >> 16;
    134 
    135 	/*
    136 	 * Print device type.
    137 	 */
    138 	if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
    139 		str = "RAID-1";
    140 	else if (t == 0)
    141 		str = "JBOD";
    142 	else if (t == 1)
    143 		str = "RAID-0";
    144 	else if (t == 2)
    145 		str = "Chain";
    146 	else
    147 		str = "unknown type";
    148 
    149 	printf("type: %s, ", str);
    150 
    151 	/*
    152 	 * Print device status.
    153 	 */
    154 	if (t > 2)
    155 		str = "missing";
    156 	else if ((cdi->ld_error & 1) != 0) {
    157 		str = "fault";
    158 		ld->sc_flags = LDF_ENABLED;
    159 	} else if ((cdi->ld_error & 2) != 0)
    160 		str = "invalid";
    161 	else {
    162 		str = "optimal";
    163 		ld->sc_flags = LDF_ENABLED;
    164 	}
    165 
    166 	printf("status: %s\n", str);
    167 
    168  out:
    169 	ldattach(ld);
    170 }
    171 
    172 int
    173 ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
    174 	     int dowrite, struct buf *bp)
    175 {
    176 	struct icp_cachecmd *cc;
    177 	struct icp_ccb *ic;
    178 	struct icp_softc *icp;
    179 	int s, rv;
    180 
    181 	icp = (struct icp_softc *)sc->sc_ld.sc_dv.dv_parent;
    182 
    183 	/*
    184 	 * Allocate a command control block.
    185 	 */
    186 	if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) {
    187 		/*
    188 		 * XXX Redo the way buffer queueing is done in
    189 		 * XXX the ld driver.
    190 		 */
    191 		return (EAGAIN);
    192 	}
    193 
    194 	/*
    195 	 * Map the data transfer.
    196 	 */
    197 	cc = &ic->ic_cmd.cmd_packet.cc;
    198 	ic->ic_sg = cc->cc_sg;
    199 	ic->ic_service = ICP_CACHESERVICE;
    200 
    201 	rv = icp_ccb_map(icp, ic, data, datasize,
    202 	    dowrite ? IC_XFER_OUT : IC_XFER_IN);
    203 	if (rv != 0) {
    204 		icp_ccb_free(icp, ic);
    205 		return (rv);
    206 	}
    207 
    208 	/*
    209 	 * Build the command.
    210 	 */
    211 	ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
    212 	cc->cc_deviceno = htole16(sc->sc_hwunit);
    213 	cc->cc_blockno = htole32(blkno);
    214 	cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
    215 	cc->cc_addr = ~0;	/* scatter gather */
    216 	cc->cc_nsgent = htole32(ic->ic_nsgent);
    217 
    218 	ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
    219 	    ic->ic_nsgent * sizeof(*ic->ic_sg);
    220 
    221 	/*
    222 	 * Fire it off to the controller.
    223 	 */
    224 	if (bp == NULL) {
    225 		s = splbio();
    226 		rv = icp_ccb_poll(icp, ic, 10000);
    227 		icp_ccb_unmap(icp, ic);
    228 		icp_ccb_free(icp, ic);
    229 		splx(s);
    230 	} else {
    231  		ic->ic_intr = ld_icp_intr;
    232 		ic->ic_context = bp;
    233 		ic->ic_dv = &sc->sc_ld.sc_dv;
    234 		icp_ccb_enqueue(icp, ic);
    235 	}
    236 
    237 	return (rv);
    238 }
    239 
    240 int
    241 ld_icp_start(struct ld_softc *ld, struct buf *bp)
    242 {
    243 
    244 	return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
    245 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    246 }
    247 
    248 int
    249 ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    250 {
    251 
    252 	return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
    253 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    254 }
    255 
    256 int
    257 ld_icp_flush(struct ld_softc *ld)
    258 {
    259 	struct ld_icp_softc *sc;
    260 	struct icp_softc *icp;
    261 	struct icp_cachecmd *cc;
    262 	struct icp_ccb *ic;
    263 	int rv;
    264 
    265 	sc = (struct ld_icp_softc *)ld;
    266 	icp = (struct icp_softc *)ld->sc_dv.dv_parent;
    267 
    268 	ic = icp_ccb_alloc_wait(icp);
    269 	ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
    270 
    271 	cc = &ic->ic_cmd.cmd_packet.cc;
    272 	cc->cc_deviceno = htole16(sc->sc_hwunit);
    273 	cc->cc_blockno = htole32(1);
    274 	cc->cc_blockcnt = 0;
    275 	cc->cc_addr = 0;
    276 	cc->cc_nsgent = 0;
    277 
    278 	ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
    279 	ic->ic_service = ICP_CACHESERVICE;
    280 
    281 	rv = icp_ccb_wait(icp, ic, 30000);
    282 	icp_ccb_free(icp, ic);
    283 
    284 	return (rv);
    285 }
    286 
    287 void
    288 ld_icp_intr(struct icp_ccb *ic)
    289 {
    290 	struct buf *bp;
    291 	struct ld_icp_softc *sc;
    292 	struct icp_softc *icp;
    293 
    294 	bp = ic->ic_context;
    295 	sc = (struct ld_icp_softc *)ic->ic_dv;
    296 	icp = (struct icp_softc *)sc->sc_ld.sc_dv.dv_parent;
    297 
    298 	if (ic->ic_status != ICP_S_OK) {
    299 		printf("%s: request failed; status=0x%04x\n",
    300 		    ic->ic_dv->dv_xname, ic->ic_status);
    301 		bp->b_flags |= B_ERROR;
    302 		bp->b_error = EIO;
    303 		bp->b_resid = bp->b_bcount;
    304 
    305 		icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync);
    306 		icp->icp_evt.eu.sync.ionode = icp->icp_dv.dv_unit;
    307 		icp->icp_evt.eu.sync.service = icp->icp_service;
    308 		icp->icp_evt.eu.sync.status = icp->icp_status;
    309 		icp->icp_evt.eu.sync.info = icp->icp_info;
    310 		icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit;
    311 		if (icp->icp_status >= 0x8000)
    312 			icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt);
    313 		else
    314 			icp_store_event(icp, GDT_ES_SYNC, icp->icp_service,
    315 			    &icp->icp_evt);
    316 	} else
    317 		bp->b_resid = 0;
    318 
    319 	icp_ccb_unmap(icp, ic);
    320 	icp_ccb_free(icp, ic);
    321 	lddone(&sc->sc_ld, bp);
    322 }
    323