Home | History | Annotate | Line # | Download | only in pci
ld_twe.c revision 1.31
      1 /*	$NetBSD: ld_twe.c,v 1.31 2008/08/11 06:46:10 simonb Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001, 2002, 2003 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; and by Jason R. Thorpe of Wasabi Systems, Inc.
      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  * 3ware "Escalade" RAID controller front-end for ld(4) driver.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.31 2008/08/11 06:46:10 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 #include <sys/proc.h>
     51 #if NRND > 0
     52 #include <sys/rnd.h>
     53 #endif
     54 
     55 #include <sys/bus.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <dev/ldvar.h>
     60 
     61 #include <dev/pci/twereg.h>
     62 #include <dev/pci/twevar.h>
     63 
     64 struct ld_twe_softc {
     65 	struct	ld_softc sc_ld;
     66 	int	sc_hwunit;
     67 };
     68 
     69 static void	ld_twe_attach(struct device *, struct device *, void *);
     70 static int	ld_twe_detach(struct device *, int);
     71 static int	ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
     72 			     struct buf *);
     73 static int	ld_twe_dump(struct ld_softc *, void *, int, int);
     74 static int	ld_twe_flush(struct ld_softc *, int);
     75 static void	ld_twe_handler(struct twe_ccb *, int);
     76 static int	ld_twe_match(struct device *, struct cfdata *, void *);
     77 static int	ld_twe_start(struct ld_softc *, struct buf *);
     78 
     79 static void	ld_twe_adjqparam(struct device *, int);
     80 
     81 CFATTACH_DECL(ld_twe, sizeof(struct ld_twe_softc),
     82     ld_twe_match, ld_twe_attach, ld_twe_detach, NULL);
     83 
     84 static const struct twe_callbacks ld_twe_callbacks = {
     85 	ld_twe_adjqparam,
     86 };
     87 
     88 static int
     89 ld_twe_match(struct device *parent, struct cfdata *match,
     90     void *aux)
     91 {
     92 
     93 	return (1);
     94 }
     95 
     96 static void
     97 ld_twe_attach(struct device *parent, struct device *self, void *aux)
     98 {
     99 	struct twe_attach_args *twea;
    100 	struct ld_twe_softc *sc;
    101 	struct ld_softc *ld;
    102 	struct twe_softc *twe;
    103 	struct twe_drive *td;
    104 	const char *typestr, *stripestr, *statstr;
    105 	char unktype[16], stripebuf[32], unkstat[32];
    106 	int error;
    107 	uint8_t status;
    108 
    109 	sc = (struct ld_twe_softc *)self;
    110 	ld = &sc->sc_ld;
    111 	twe = (struct twe_softc *)parent;
    112 	twea = aux;
    113 	td = &twe->sc_units[twea->twea_unit];
    114 
    115 	twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks);
    116 
    117 	sc->sc_hwunit = twea->twea_unit;
    118 	ld->sc_flags = LDF_ENABLED;
    119 	ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
    120 	ld->sc_secperunit = td->td_size;
    121 	ld->sc_secsize = TWE_SECTOR_SIZE;
    122 	ld->sc_maxqueuecnt = twe->sc_openings;
    123 	ld->sc_start = ld_twe_start;
    124 	ld->sc_dump = ld_twe_dump;
    125 	ld->sc_flush = ld_twe_flush;
    126 
    127 	typestr = twe_describe_code(twe_table_unittype, td->td_type);
    128 	if (typestr == NULL) {
    129 		snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type);
    130 		typestr = unktype;
    131 	}
    132 	switch (td->td_type) {
    133 	case TWE_AD_CONFIG_RAID0:
    134 	case TWE_AD_CONFIG_RAID5:
    135 	case TWE_AD_CONFIG_RAID10:
    136 		stripestr = twe_describe_code(twe_table_stripedepth,
    137 		    td->td_stripe);
    138 		if (stripestr == NULL)
    139 			snprintf(stripebuf, sizeof(stripebuf),
    140 			    "<stripe code 0x%02x> ", td->td_stripe);
    141 		else
    142 			snprintf(stripebuf, sizeof(stripebuf), "%s stripe ",
    143 			    stripestr);
    144 		break;
    145 	default:
    146 		stripebuf[0] = '\0';
    147 	}
    148 
    149 	error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit,
    150 	    TWE_PARAM_UNITINFO_Status, &status);
    151 	status &= TWE_PARAM_UNITSTATUS_MASK;
    152 	if (error) {
    153 		snprintf(unkstat, sizeof(unkstat), "<unknown>");
    154 		statstr = unkstat;
    155 	} else if ((statstr =
    156 		    twe_describe_code(twe_table_unitstate, status)) == NULL) {
    157 		snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>",
    158 		    status);
    159 		statstr = unkstat;
    160 	}
    161 
    162 	aprint_normal(": %s%s, status: %s\n", stripebuf, typestr, statstr);
    163 	ldattach(ld);
    164 }
    165 
    166 static int
    167 ld_twe_detach(struct device *self, int flags)
    168 {
    169 	int rv;
    170 
    171 	if ((rv = ldbegindetach((struct ld_softc *)self, flags)) != 0)
    172 		return (rv);
    173 	ldenddetach((struct ld_softc *)self);
    174 
    175 	return (0);
    176 }
    177 
    178 static int
    179 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
    180 	     int dowrite, struct buf *bp)
    181 {
    182 	struct twe_ccb *ccb;
    183 	struct twe_cmd *tc;
    184 	struct twe_softc *twe;
    185 	int s, rv, flags;
    186 
    187 	twe = (struct twe_softc *)device_parent(&sc->sc_ld.sc_dv);
    188 
    189 	flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
    190 	if ((ccb = twe_ccb_alloc(twe, flags)) == NULL)
    191 		return (EAGAIN);
    192 
    193 	ccb->ccb_data = data;
    194 	ccb->ccb_datasize = datasize;
    195 	tc = ccb->ccb_cmd;
    196 
    197 	/* Build the command. */
    198 	tc->tc_size = 3;
    199 	tc->tc_unit = sc->sc_hwunit;
    200 	tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
    201 	tc->tc_args.io.lba = htole32(blkno);
    202 
    203 	if (dowrite)
    204 		tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
    205 	else
    206 		tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
    207 
    208 	/* Map the data transfer. */
    209 	if ((rv = twe_ccb_map(twe, ccb)) != 0) {
    210 		twe_ccb_free(twe, ccb);
    211 		return (rv);
    212 	}
    213 
    214 	if (bp == NULL) {
    215 		/*
    216 		 * Polled commands must not sit on the software queue.  Wait
    217 		 * up to 2 seconds for the command to complete.
    218 		 */
    219 		s = splbio();
    220 		rv = twe_ccb_poll(twe, ccb, 2000);
    221 		twe_ccb_unmap(twe, ccb);
    222 		twe_ccb_free(twe, ccb);
    223 		splx(s);
    224 	} else {
    225 		ccb->ccb_tx.tx_handler = ld_twe_handler;
    226 		ccb->ccb_tx.tx_context = bp;
    227 		ccb->ccb_tx.tx_dv = (struct device *)sc;
    228 		twe_ccb_enqueue(twe, ccb);
    229 		rv = 0;
    230 	}
    231 
    232 	return (rv);
    233 }
    234 
    235 static int
    236 ld_twe_start(struct ld_softc *ld, struct buf *bp)
    237 {
    238 
    239 	return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
    240 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    241 }
    242 
    243 static void
    244 ld_twe_handler(struct twe_ccb *ccb, int error)
    245 {
    246 	struct buf *bp;
    247 	struct twe_context *tx;
    248 	struct ld_twe_softc *sc;
    249 	struct twe_softc *twe;
    250 
    251 	tx = &ccb->ccb_tx;
    252 	bp = tx->tx_context;
    253 	sc = (struct ld_twe_softc *)tx->tx_dv;
    254 	twe = (struct twe_softc *)device_parent(&sc->sc_ld.sc_dv);
    255 
    256 	twe_ccb_unmap(twe, ccb);
    257 	twe_ccb_free(twe, ccb);
    258 
    259 	if (error) {
    260 		bp->b_error = error;
    261 		bp->b_resid = bp->b_bcount;
    262 	} else
    263 		bp->b_resid = 0;
    264 
    265 	lddone(&sc->sc_ld, bp);
    266 }
    267 
    268 static int
    269 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    270 {
    271 
    272 	return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
    273 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    274 }
    275 
    276 static int
    277 ld_twe_flush(struct ld_softc *ld, int flags)
    278 {
    279 	struct ld_twe_softc *sc = (void *) ld;
    280 	struct twe_softc *twe = (void *) device_parent(&ld->sc_dv);
    281 	struct twe_ccb *ccb;
    282 	struct twe_cmd *tc;
    283 	int s, rv;
    284 
    285 	ccb = twe_ccb_alloc_wait(twe, 0);
    286 	KASSERT(ccb != NULL);
    287 
    288 	ccb->ccb_data = NULL;
    289 	ccb->ccb_datasize = 0;
    290 
    291 	tc = ccb->ccb_cmd;
    292 	tc->tc_size = 2;
    293 	tc->tc_opcode = TWE_OP_FLUSH;
    294 	tc->tc_unit = sc->sc_hwunit;
    295 	tc->tc_count = 0;
    296 
    297 	if (flags & LDFL_POLL) {
    298 		/*
    299 		 * Polled commands must not sit on the software queue.  Wait
    300 		 * up to 2 seconds for the command to complete.
    301 		 */
    302 		s = splbio();
    303 		rv = twe_ccb_poll(twe, ccb, 2000);
    304 		twe_ccb_unmap(twe, ccb);
    305 		twe_ccb_free(twe, ccb);
    306 		splx(s);
    307 	} else {
    308 		ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
    309 		ccb->ccb_tx.tx_context = NULL;
    310 		ccb->ccb_tx.tx_dv = &ld->sc_dv;
    311 		twe_ccb_enqueue(twe, ccb);
    312 
    313 		rv = 0;
    314 		s = splbio();
    315 		while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
    316 			if ((rv = tsleep(ccb, PRIBIO, "tweflush",
    317 			    60 * hz)) != 0)
    318 				break;
    319 		twe_ccb_free(twe, ccb);
    320 		splx(s);
    321 	}
    322 
    323 	return (rv);
    324 }
    325 
    326 static void
    327 ld_twe_adjqparam(struct device *self, int openings)
    328 {
    329 
    330 	ldadjqparam((struct ld_softc *)self, openings);
    331 }
    332