Home | History | Annotate | Line # | Download | only in pci
ld_twe.c revision 1.3
      1 /*	$NetBSD: ld_twe.c,v 1.3 2001/01/22 17:40:14 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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  * 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 "rnd.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/buf.h>
     50 #include <sys/endian.h>
     51 #include <sys/dkio.h>
     52 #include <sys/disk.h>
     53 #if NRND > 0
     54 #include <sys/rnd.h>
     55 #endif
     56 
     57 #include <machine/bus.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <dev/ldvar.h>
     62 
     63 #include <dev/pci/twereg.h>
     64 #include <dev/pci/twevar.h>
     65 
     66 struct ld_twe_softc {
     67 	struct	ld_softc sc_ld;
     68 	int	sc_hwunit;
     69 };
     70 
     71 static void	ld_twe_attach(struct device *, struct device *, void *);
     72 static int	ld_twe_dobio(struct ld_twe_softc *, int, void *, int, int,
     73 			      int, struct twe_context *);
     74 static int	ld_twe_dump(struct ld_softc *, void *, int, 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 struct cfattach ld_twe_ca = {
     80 	sizeof(struct ld_twe_softc), ld_twe_match, ld_twe_attach
     81 };
     82 
     83 static int
     84 ld_twe_match(struct device *parent, struct cfdata *match, void *aux)
     85 {
     86 
     87 	return (1);
     88 }
     89 
     90 static void
     91 ld_twe_attach(struct device *parent, struct device *self, void *aux)
     92 {
     93 	struct twe_attach_args *twea;
     94 	struct ld_twe_softc *sc;
     95 	struct ld_softc *ld;
     96 	struct twe_softc *twe;
     97 
     98 	sc = (struct ld_twe_softc *)self;
     99 	ld = &sc->sc_ld;
    100 	twe = (struct twe_softc *)parent;
    101 	twea = aux;
    102 
    103 	sc->sc_hwunit = twea->twea_unit;
    104 	ld->sc_flags = LDF_ENABLED;
    105 	ld->sc_maxxfer = TWE_MAX_XFER;
    106 	ld->sc_secperunit = twe->sc_dsize[twea->twea_unit];
    107 	ld->sc_secsize = TWE_SECTOR_SIZE;
    108 	ld->sc_maxqueuecnt = (TWE_MAX_QUEUECNT - 1) / twe->sc_nunits;
    109 	ld->sc_start = ld_twe_start;
    110 	ld->sc_dump = ld_twe_dump;
    111 
    112 	/* Build synthetic geometry as per controller internal rules. */
    113 	if (ld->sc_secperunit > 0x200000) {
    114 		ld->sc_nheads = 255;
    115 		ld->sc_nsectors = 63;
    116 	} else {
    117 		ld->sc_nheads = 64;
    118 		ld->sc_nsectors = 32;
    119 	}
    120 	ld->sc_ncylinders = ld->sc_secperunit /
    121 	    (ld->sc_nheads * ld->sc_nsectors);
    122 
    123 	printf("\n");
    124 	ldattach(ld);
    125 }
    126 
    127 static int
    128 ld_twe_dobio(struct ld_twe_softc *sc, int unit, void *data, int datasize,
    129 	      int blkno, int dowrite, struct twe_context *tx)
    130 {
    131 	struct twe_ccb *ccb;
    132 	struct twe_cmd *tc;
    133 	struct twe_softc *twe;
    134 	int s, rv, flags;
    135 
    136 	twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
    137 
    138 	flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
    139 	if ((rv = twe_ccb_alloc(twe, &ccb, flags)) != 0)
    140 		return (rv);
    141 
    142 	ccb->ccb_data = data;
    143 	ccb->ccb_datasize = datasize;
    144 	tc = ccb->ccb_cmd;
    145 
    146 	/* Build the command. */
    147 	tc->tc_size = 3;
    148 	tc->tc_unit = unit;
    149 	tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
    150 	tc->tc_args.io.lba = htole32(blkno);
    151 
    152 	if (dowrite)
    153 		tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
    154 	else
    155 		tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
    156 
    157 	/* Map the data transfer. */
    158 	if ((rv = twe_ccb_map(twe, ccb)) != 0) {
    159 		twe_ccb_free(twe, ccb);
    160 		return (rv);
    161 	}
    162 
    163 	if (tx == NULL) {
    164 		/*
    165 		 * Polled commands must not sit on the software queue.  Wait
    166 		 * up to 2 seconds for the command to complete.
    167 		 */
    168 		s = splbio();
    169 		if ((rv = twe_ccb_submit(twe, ccb)) == 0)
    170 			rv = twe_ccb_poll(twe, ccb, 2000);
    171 		twe_ccb_unmap(twe, ccb);
    172 		twe_ccb_free(twe, ccb);
    173 		splx(s);
    174 	} else {
    175 		memcpy(&ccb->ccb_tx, tx, sizeof(struct twe_context));
    176 		twe_ccb_enqueue(twe, ccb);
    177 		rv = 0;
    178 	}
    179 
    180 	return (rv);
    181 }
    182 
    183 static int
    184 ld_twe_start(struct ld_softc *ld, struct buf *bp)
    185 {
    186 	struct twe_context tx;
    187 	struct ld_twe_softc *sc;
    188 	struct twe_softc *twe;
    189 
    190 	sc = (struct ld_twe_softc *)ld;
    191 	twe = (struct twe_softc *)ld->sc_dv.dv_parent;
    192 
    193 	tx.tx_handler = ld_twe_handler;
    194 	tx.tx_context = bp;
    195 	tx.tx_dv = &ld->sc_dv;
    196 
    197 	return (ld_twe_dobio(sc, sc->sc_hwunit, bp->b_data, bp->b_bcount,
    198 	    bp->b_rawblkno, (bp->b_flags & B_READ) == 0, &tx));
    199 }
    200 
    201 static void
    202 ld_twe_handler(struct twe_ccb *ccb, int error)
    203 {
    204 	struct buf *bp;
    205 	struct twe_context *tx;
    206 	struct ld_twe_softc *sc;
    207 	struct twe_softc *twe;
    208 
    209 	tx = &ccb->ccb_tx;
    210 	bp = tx->tx_context;
    211 	sc = (struct ld_twe_softc *)tx->tx_dv;
    212 	twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
    213 
    214 	twe_ccb_unmap(twe, ccb);
    215 	twe_ccb_free(twe, ccb);
    216 
    217 	if (error) {
    218 		bp->b_flags |= B_ERROR;
    219 		bp->b_error = error;
    220 		bp->b_resid = bp->b_bcount;
    221 	} else
    222 		bp->b_resid = 0;
    223 
    224 	lddone(&sc->sc_ld, bp);
    225 }
    226 
    227 static int
    228 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    229 {
    230 	struct ld_twe_softc *sc;
    231 
    232 	sc = (struct ld_twe_softc *)ld;
    233 
    234 	return (ld_twe_dobio(sc, sc->sc_hwunit, data, blkcnt * ld->sc_secsize,
    235 	    blkno, 1, NULL));
    236 }
    237