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