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