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