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