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