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