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