1 /* $NetBSD: ld_twe.c,v 1.34 2009/05/12 08:23:01 cegger 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * 3ware "Escalade" RAID controller front-end for ld(4) driver. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.34 2009/05/12 08:23:01 cegger Exp $"); 38 39 #include "rnd.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/buf.h> 46 #include <sys/bufq.h> 47 #include <sys/endian.h> 48 #include <sys/dkio.h> 49 #include <sys/disk.h> 50 #include <sys/proc.h> 51 #if NRND > 0 52 #include <sys/rnd.h> 53 #endif 54 55 #include <sys/bus.h> 56 57 #include <uvm/uvm_extern.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(device_t, device_t, void *); 70 static int ld_twe_detach(device_t, int); 71 static int ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int, 72 struct buf *); 73 static int ld_twe_dump(struct ld_softc *, void *, int, int); 74 static int ld_twe_flush(struct ld_softc *, int); 75 static void ld_twe_handler(struct twe_ccb *, int); 76 static int ld_twe_match(device_t, cfdata_t, void *); 77 static int ld_twe_start(struct ld_softc *, struct buf *); 78 79 static void ld_twe_adjqparam(device_t, int); 80 81 CFATTACH_DECL_NEW(ld_twe, sizeof(struct ld_twe_softc), 82 ld_twe_match, ld_twe_attach, ld_twe_detach, NULL); 83 84 static const struct twe_callbacks ld_twe_callbacks = { 85 ld_twe_adjqparam, 86 }; 87 88 static int 89 ld_twe_match(device_t parent, cfdata_t match, void *aux) 90 { 91 92 return (1); 93 } 94 95 static void 96 ld_twe_attach(device_t parent, device_t self, void *aux) 97 { 98 struct twe_attach_args *twea = aux; 99 struct ld_twe_softc *sc = device_private(self); 100 struct ld_softc *ld = &sc->sc_ld; 101 struct twe_softc *twe = device_private(parent); 102 struct twe_drive *td = &twe->sc_units[twea->twea_unit]; 103 const char *typestr, *stripestr, *statstr; 104 char unktype[16], stripebuf[32], unkstat[32]; 105 int error; 106 uint8_t status; 107 108 ld->sc_dv = self; 109 110 twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks); 111 112 sc->sc_hwunit = twea->twea_unit; 113 ld->sc_flags = LDF_ENABLED; 114 ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs()); 115 ld->sc_secperunit = td->td_size; 116 ld->sc_secsize = TWE_SECTOR_SIZE; 117 ld->sc_maxqueuecnt = twe->sc_openings; 118 ld->sc_start = ld_twe_start; 119 ld->sc_dump = ld_twe_dump; 120 ld->sc_flush = ld_twe_flush; 121 122 typestr = twe_describe_code(twe_table_unittype, td->td_type); 123 if (typestr == NULL) { 124 snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type); 125 typestr = unktype; 126 } 127 switch (td->td_type) { 128 case TWE_AD_CONFIG_RAID0: 129 case TWE_AD_CONFIG_RAID5: 130 case TWE_AD_CONFIG_RAID10: 131 stripestr = twe_describe_code(twe_table_stripedepth, 132 td->td_stripe); 133 if (stripestr == NULL) 134 snprintf(stripebuf, sizeof(stripebuf), 135 "<stripe code 0x%02x> ", td->td_stripe); 136 else 137 snprintf(stripebuf, sizeof(stripebuf), "%s stripe ", 138 stripestr); 139 break; 140 default: 141 stripebuf[0] = '\0'; 142 } 143 144 error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit, 145 TWE_PARAM_UNITINFO_Status, &status); 146 status &= TWE_PARAM_UNITSTATUS_MASK; 147 if (error) { 148 snprintf(unkstat, sizeof(unkstat), "<unknown>"); 149 statstr = unkstat; 150 } else if ((statstr = 151 twe_describe_code(twe_table_unitstate, status)) == NULL) { 152 snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>", 153 status); 154 statstr = unkstat; 155 } 156 157 aprint_normal(": %s%s, status: %s\n", stripebuf, typestr, statstr); 158 ldattach(ld); 159 } 160 161 static int 162 ld_twe_detach(device_t self, int flags) 163 { 164 struct ld_twe_softc *sc = device_private(self); 165 struct ld_softc *ld = &sc->sc_ld; 166 int rv; 167 168 if ((rv = ldbegindetach(ld, flags)) != 0) 169 return (rv); 170 ldenddetach(ld); 171 172 return (0); 173 } 174 175 static int 176 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno, 177 int dowrite, struct buf *bp) 178 { 179 struct twe_ccb *ccb; 180 struct twe_cmd *tc; 181 struct twe_softc *twe; 182 int s, rv, flags; 183 184 twe = device_private(device_parent(sc->sc_ld.sc_dv)); 185 186 flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN); 187 if ((ccb = twe_ccb_alloc(twe, flags)) == NULL) 188 return (EAGAIN); 189 190 ccb->ccb_data = data; 191 ccb->ccb_datasize = datasize; 192 tc = ccb->ccb_cmd; 193 194 /* Build the command. */ 195 tc->tc_size = 3; 196 tc->tc_unit = sc->sc_hwunit; 197 tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE); 198 tc->tc_args.io.lba = htole32(blkno); 199 200 if (dowrite) 201 tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5); 202 else 203 tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5); 204 205 /* Map the data transfer. */ 206 if ((rv = twe_ccb_map(twe, ccb)) != 0) { 207 twe_ccb_free(twe, ccb); 208 return (rv); 209 } 210 211 if (bp == NULL) { 212 /* 213 * Polled commands must not sit on the software queue. Wait 214 * up to 2 seconds for the command to complete. 215 */ 216 s = splbio(); 217 rv = twe_ccb_poll(twe, ccb, 2000); 218 twe_ccb_unmap(twe, ccb); 219 twe_ccb_free(twe, ccb); 220 splx(s); 221 } else { 222 ccb->ccb_tx.tx_handler = ld_twe_handler; 223 ccb->ccb_tx.tx_context = bp; 224 ccb->ccb_tx.tx_dv = sc->sc_ld.sc_dv; 225 twe_ccb_enqueue(twe, ccb); 226 rv = 0; 227 } 228 229 return (rv); 230 } 231 232 static int 233 ld_twe_start(struct ld_softc *ld, struct buf *bp) 234 { 235 236 return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data, 237 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp)); 238 } 239 240 static void 241 ld_twe_handler(struct twe_ccb *ccb, int error) 242 { 243 struct buf *bp; 244 struct twe_context *tx; 245 struct ld_twe_softc *sc; 246 struct twe_softc *twe; 247 248 tx = &ccb->ccb_tx; 249 bp = tx->tx_context; 250 sc = device_private(tx->tx_dv); 251 twe = device_private(device_parent(sc->sc_ld.sc_dv)); 252 253 twe_ccb_unmap(twe, ccb); 254 twe_ccb_free(twe, ccb); 255 256 if (error) { 257 bp->b_error = error; 258 bp->b_resid = bp->b_bcount; 259 } else 260 bp->b_resid = 0; 261 262 lddone(&sc->sc_ld, bp); 263 } 264 265 static int 266 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 267 { 268 269 return (ld_twe_dobio((struct ld_twe_softc *)ld, data, 270 blkcnt * ld->sc_secsize, blkno, 1, NULL)); 271 } 272 273 static int 274 ld_twe_flush(struct ld_softc *ld, int flags) 275 { 276 struct ld_twe_softc *sc = (void *) ld; 277 struct twe_softc *twe = device_private(device_parent(ld->sc_dv)); 278 struct twe_ccb *ccb; 279 struct twe_cmd *tc; 280 int s, rv; 281 282 ccb = twe_ccb_alloc_wait(twe, 0); 283 KASSERT(ccb != NULL); 284 285 ccb->ccb_data = NULL; 286 ccb->ccb_datasize = 0; 287 288 tc = ccb->ccb_cmd; 289 tc->tc_size = 2; 290 tc->tc_opcode = TWE_OP_FLUSH; 291 tc->tc_unit = sc->sc_hwunit; 292 tc->tc_count = 0; 293 294 if (flags & LDFL_POLL) { 295 /* 296 * Polled commands must not sit on the software queue. Wait 297 * up to 2 seconds for the command to complete. 298 */ 299 s = splbio(); 300 rv = twe_ccb_poll(twe, ccb, 2000); 301 twe_ccb_unmap(twe, ccb); 302 twe_ccb_free(twe, ccb); 303 splx(s); 304 } else { 305 ccb->ccb_tx.tx_handler = twe_ccb_wait_handler; 306 ccb->ccb_tx.tx_context = NULL; 307 ccb->ccb_tx.tx_dv = ld->sc_dv; 308 twe_ccb_enqueue(twe, ccb); 309 310 rv = 0; 311 s = splbio(); 312 while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0) 313 if ((rv = tsleep(ccb, PRIBIO, "tweflush", 314 60 * hz)) != 0) 315 break; 316 twe_ccb_free(twe, ccb); 317 splx(s); 318 } 319 320 return (rv); 321 } 322 323 static void 324 ld_twe_adjqparam(device_t self, int openings) 325 { 326 struct ld_twe_softc *sc = device_private(self); 327 struct ld_softc *ld = &sc->sc_ld; 328 329 ldadjqparam(ld, openings); 330 } 331