1 /* $wasabi: ld_twa.c,v 1.9 2006/02/14 18:44:37 jordanr Exp $ */ 2 /* $NetBSD: ld_twa.c,v 1.2 2006/05/25 01:37:08 wrstuden Exp $ */ 3 /*- 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jordan Rhody 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 "Apache" RAID controller front-end for ld(4) driver. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: ld_twa.c,v 1.2 2006/05/25 01:37:08 wrstuden 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/scsipi/scsipi_all.h> 68 #include <dev/scsipi/scsipi_disk.h> 69 #include <dev/scsipi/scsipiconf.h> 70 #include <dev/scsipi/scsi_disk.h> 71 72 73 #include <dev/pci/pcireg.h> 74 #include <dev/pci/pcivar.h> 75 #include <dev/pci/twareg.h> 76 #include <dev/pci/twavar.h> 77 78 struct ld_twa_softc { 79 struct ld_softc sc_ld; 80 int sc_hwunit; 81 }; 82 83 static void ld_twa_attach(struct device *, struct device *, void *); 84 static int ld_twa_detach(struct device *, int); 85 static int ld_twa_dobio(struct ld_twa_softc *, void *, int, int, 86 struct buf *); 87 static int ld_twa_dump(struct ld_softc *, void *, int, int); 88 static int ld_twa_flush(struct ld_softc *); 89 static void ld_twa_handler(struct twa_request *); 90 static int ld_twa_match(struct device *, struct cfdata *, void *); 91 static int ld_twa_start(struct ld_softc *, struct buf *); 92 93 static void ld_twa_adjqparam(struct device *, int); 94 95 static int ld_twa_scsicmd(struct ld_twa_softc *, 96 struct twa_request *, struct buf *); 97 98 CFATTACH_DECL(ld_twa, sizeof(struct ld_twa_softc), 99 ld_twa_match, ld_twa_attach, ld_twa_detach, NULL); 100 101 static const struct twa_callbacks ld_twa_callbacks = { 102 ld_twa_adjqparam, 103 }; 104 105 static int 106 ld_twa_match(struct device *parent, struct cfdata *match, void *aux) 107 { 108 109 return (1); 110 } 111 112 static void 113 ld_twa_attach(struct device *parent, struct device *self, void *aux) 114 { 115 struct twa_attach_args *twa_args; 116 struct ld_twa_softc *sc; 117 struct ld_softc *ld; 118 struct twa_softc *twa; 119 120 sc = (struct ld_twa_softc *)self; 121 ld = &sc->sc_ld; 122 twa = (struct twa_softc *)parent; 123 twa_args = aux; 124 125 twa_register_callbacks(twa, twa_args->twaa_unit, &ld_twa_callbacks); 126 127 sc->sc_hwunit = twa_args->twaa_unit; 128 ld->sc_maxxfer = twa_get_maxxfer(twa_get_maxsegs()); 129 ld->sc_secperunit = twa->sc_units[sc->sc_hwunit].td_size; 130 ld->sc_flags = LDF_ENABLED; 131 ld->sc_secsize = TWA_SECTOR_SIZE; 132 ld->sc_maxqueuecnt = twa->sc_openings; 133 ld->sc_start = ld_twa_start; 134 ld->sc_dump = ld_twa_dump; 135 ld->sc_flush = ld_twa_flush; 136 ldattach(ld); 137 } 138 139 static int 140 ld_twa_detach(struct device *self, int flags) 141 { 142 int error; 143 144 if ((error = ldbegindetach((struct ld_softc *)self, flags)) != 0) 145 return (error); 146 ldenddetach((struct ld_softc *)self); 147 148 return (0); 149 } 150 151 static int 152 ld_twa_dobio(struct ld_twa_softc *sc, void *data, int datasize, int blkno, 153 struct buf *bp) 154 { 155 int rv; 156 struct twa_request *tr; 157 struct twa_softc *twa; 158 159 twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent; 160 161 if ((tr = twa_get_request(twa, 0)) == NULL) { 162 return (EAGAIN); 163 } 164 if (bp->b_flags & B_READ) { 165 tr->tr_flags = TWA_CMD_DATA_OUT; 166 } else { 167 tr->tr_flags = TWA_CMD_DATA_IN; 168 } 169 170 tr->tr_data = data; 171 tr->tr_length = datasize; 172 tr->tr_cmd_pkt_type = 173 (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL); 174 175 tr->tr_command->cmd_hdr.header_desc.size_header = 128; 176 177 tr->tr_command->command.cmd_pkt_9k.command.opcode = 178 TWA_OP_EXECUTE_SCSI_COMMAND; 179 tr->tr_command->command.cmd_pkt_9k.unit = 180 sc->sc_hwunit; 181 tr->tr_command->command.cmd_pkt_9k.request_id = 182 tr->tr_request_id; 183 tr->tr_command->command.cmd_pkt_9k.status = 0; 184 tr->tr_command->command.cmd_pkt_9k.sgl_entries = 1; 185 tr->tr_command->command.cmd_pkt_9k.sgl_offset = 16; 186 187 /* offset from end of hdr = max cdb len */ 188 ld_twa_scsicmd(sc, tr, bp); 189 190 tr->tr_callback = ld_twa_handler; 191 tr->tr_ld_sc = sc; 192 193 tr->bp = bp; 194 195 rv = twa_map_request(tr); 196 197 return (rv); 198 } 199 200 static int 201 ld_twa_start(struct ld_softc *ld, struct buf *bp) 202 { 203 204 return (ld_twa_dobio((struct ld_twa_softc *)ld, bp->b_data, 205 bp->b_bcount, bp->b_rawblkno, bp)); 206 } 207 208 static void 209 ld_twa_handler(struct twa_request *tr) 210 { 211 uint8_t status; 212 struct buf *bp; 213 struct ld_twa_softc *sc; 214 struct twa_softc *twa; 215 216 bp = tr->bp; 217 sc = (struct ld_twa_softc *)tr->tr_ld_sc; 218 twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent; 219 220 status = tr->tr_command->command.cmd_pkt_9k.status; 221 222 if (status != 0) { 223 bp->b_flags |= B_ERROR; 224 bp->b_error = EIO; 225 bp->b_resid = bp->b_bcount; 226 } else { 227 bp->b_resid = 0; 228 bp->b_error = 0; 229 } 230 twa_release_request(tr); 231 232 lddone(&sc->sc_ld, bp); 233 } 234 235 static int 236 ld_twa_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 237 { 238 239 return (ld_twa_dobio((struct ld_twa_softc *)ld, data, 240 blkcnt * ld->sc_secsize, blkno, NULL)); 241 } 242 243 244 static int 245 ld_twa_flush(struct ld_softc *ld) 246 { 247 int s, rv = 0; 248 struct twa_request *tr; 249 struct twa_softc *twa = (void *)ld->sc_dv.dv_parent; 250 struct ld_twa_softc *sc = (void *)ld; 251 struct twa_command_generic *generic_cmd; 252 253 /* Get a request packet. */ 254 tr = twa_get_request_wait(twa, 0); 255 KASSERT(tr != NULL); 256 257 tr->tr_cmd_pkt_type = 258 (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL); 259 260 tr->tr_callback = twa_request_wait_handler; 261 tr->tr_ld_sc = sc; 262 263 tr->tr_command->cmd_hdr.header_desc.size_header = 128; 264 265 generic_cmd = &(tr->tr_command->command.cmd_pkt_7k.generic); 266 generic_cmd->opcode = TWA_OP_FLUSH; 267 generic_cmd->size = 2; 268 generic_cmd->unit = sc->sc_hwunit; 269 generic_cmd->request_id = tr->tr_request_id; 270 generic_cmd->sgl_offset = 0; 271 generic_cmd->host_id = 0; 272 generic_cmd->status = 0; 273 generic_cmd->flags = 0; 274 generic_cmd->count = 0; 275 rv = twa_map_request(tr); 276 s = splbio(); 277 while (tr->tr_status != TWA_CMD_COMPLETE) 278 if ((rv = tsleep(tr, PRIBIO, "twaflush", 60 * hz)) != 0) 279 break; 280 twa_release_request(tr); 281 splx(s); 282 283 return (rv); 284 } 285 286 static void 287 ld_twa_adjqparam(struct device *self, int openings) 288 { 289 290 ldadjqparam((struct ld_softc *)self, openings); 291 } 292 293 294 static int 295 ld_twa_scsicmd(struct ld_twa_softc *sc, 296 struct twa_request *tr, struct buf *bp) 297 { 298 if (tr->tr_flags == TWA_CMD_DATA_IN) { 299 tr->tr_command->command.cmd_pkt_9k.cdb[0] = WRITE_16; 300 } else { 301 tr->tr_command->command.cmd_pkt_9k.cdb[0] = READ_16; 302 } 303 tr->tr_command->command.cmd_pkt_9k.cdb[1] = 304 (sc->sc_hwunit << 5); /* lun for CDB */ 305 306 _lto8b(htole64(bp->b_rawblkno), 307 &tr->tr_command->command.cmd_pkt_9k.cdb[2]); 308 _lto4b(htole32((bp->b_bcount / TWA_SECTOR_SIZE)), 309 &tr->tr_command->command.cmd_pkt_9k.cdb[10]); 310 311 tr->tr_command->command.cmd_pkt_9k.cdb[14] = 0; 312 tr->tr_command->command.cmd_pkt_9k.cdb[15] = 0; 313 314 return (0); 315 } 316