1 /* $wasabi: ld_twa.c,v 1.9 2006/02/14 18:44:37 jordanr Exp $ */ 2 /* $NetBSD: ld_twa.c,v 1.14 2010/11/13 13:52:07 uebayasi Exp $ */ 3 4 /*- 5 * Copyright (c) 2000, 2001, 2002, 2003, 2004 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Doran, and by Jason R. Thorpe and Jordan Rhody of Wasabi 10 * Systems, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * 3ware "Apache" RAID controller front-end for ld(4) driver. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: ld_twa.c,v 1.14 2010/11/13 13:52:07 uebayasi Exp $"); 40 41 #include "rnd.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/device.h> 47 #include <sys/buf.h> 48 #include <sys/bufq.h> 49 #include <sys/endian.h> 50 #include <sys/dkio.h> 51 #include <sys/disk.h> 52 #include <sys/proc.h> 53 #if NRND > 0 54 #include <sys/rnd.h> 55 #endif 56 57 #include <sys/bus.h> 58 59 #include <dev/ldvar.h> 60 61 #include <dev/scsipi/scsipi_all.h> 62 #include <dev/scsipi/scsipi_disk.h> 63 #include <dev/scsipi/scsipiconf.h> 64 #include <dev/scsipi/scsi_disk.h> 65 66 67 #include <dev/pci/pcireg.h> 68 #include <dev/pci/pcivar.h> 69 #include <dev/pci/twareg.h> 70 #include <dev/pci/twavar.h> 71 72 struct ld_twa_softc { 73 struct ld_softc sc_ld; 74 int sc_hwunit; 75 }; 76 77 static void ld_twa_attach(device_t, device_t, void *); 78 static int ld_twa_detach(device_t, int); 79 static int ld_twa_dobio(struct ld_twa_softc *, void *, size_t, daddr_t, 80 struct buf *); 81 static int ld_twa_dump(struct ld_softc *, void *, int, int); 82 static int ld_twa_flush(struct ld_softc *, int); 83 static void ld_twa_handler(struct twa_request *); 84 static int ld_twa_match(device_t, cfdata_t, void *); 85 static int ld_twa_start(struct ld_softc *, struct buf *); 86 87 static void ld_twa_adjqparam(device_t, int); 88 89 static int ld_twa_scsicmd(struct ld_twa_softc *, 90 struct twa_request *, struct buf *); 91 92 CFATTACH_DECL_NEW(ld_twa, sizeof(struct ld_twa_softc), 93 ld_twa_match, ld_twa_attach, ld_twa_detach, NULL); 94 95 static const struct twa_callbacks ld_twa_callbacks = { 96 ld_twa_adjqparam, 97 }; 98 99 static int 100 ld_twa_match(device_t parent, cfdata_t match, void *aux) 101 { 102 103 return (1); 104 } 105 106 static void 107 ld_twa_attach(device_t parent, device_t self, void *aux) 108 { 109 struct twa_attach_args *twa_args = aux; 110 struct ld_twa_softc *sc = device_private(self); 111 struct ld_softc *ld = &sc->sc_ld; 112 struct twa_softc *twa = device_private(parent); 113 114 ld->sc_dv = self; 115 116 twa_register_callbacks(twa, twa_args->twaa_unit, &ld_twa_callbacks); 117 118 sc->sc_hwunit = twa_args->twaa_unit; 119 ld->sc_maxxfer = twa_get_maxxfer(twa_get_maxsegs()); 120 ld->sc_secperunit = twa->sc_units[sc->sc_hwunit].td_size; 121 ld->sc_flags = LDF_ENABLED; 122 ld->sc_secsize = TWA_SECTOR_SIZE; 123 ld->sc_maxqueuecnt = twa->sc_units[sc->sc_hwunit].td_openings; 124 ld->sc_start = ld_twa_start; 125 ld->sc_dump = ld_twa_dump; 126 ld->sc_flush = ld_twa_flush; 127 ldattach(ld); 128 } 129 130 static int 131 ld_twa_detach(device_t self, int flags) 132 { 133 struct ld_twa_softc *sc = device_private(self); 134 struct ld_softc *ld = &sc->sc_ld; 135 int error; 136 137 if ((error = ldbegindetach(ld, flags)) != 0) 138 return (error); 139 ldenddetach(ld); 140 141 return (0); 142 } 143 144 static int 145 ld_twa_dobio(struct ld_twa_softc *sc, void *data, size_t datasize, 146 daddr_t blkno, struct buf *bp) 147 { 148 int rv; 149 struct twa_request *tr; 150 struct twa_softc *twa; 151 152 twa = device_private(device_parent(sc->sc_ld.sc_dv)); 153 154 if ((tr = twa_get_request(twa, 0)) == NULL) { 155 return (EAGAIN); 156 } 157 if (bp->b_flags & B_READ) { 158 tr->tr_flags = TWA_CMD_DATA_OUT; 159 } else { 160 tr->tr_flags = TWA_CMD_DATA_IN; 161 } 162 163 tr->tr_data = data; 164 tr->tr_length = datasize; 165 tr->tr_cmd_pkt_type = 166 (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL); 167 168 tr->tr_command->cmd_hdr.header_desc.size_header = 128; 169 170 tr->tr_command->command.cmd_pkt_9k.command.opcode = 171 TWA_OP_EXECUTE_SCSI_COMMAND; 172 tr->tr_command->command.cmd_pkt_9k.unit = 173 sc->sc_hwunit; 174 tr->tr_command->command.cmd_pkt_9k.request_id = 175 tr->tr_request_id; 176 tr->tr_command->command.cmd_pkt_9k.status = 0; 177 tr->tr_command->command.cmd_pkt_9k.sgl_entries = 1; 178 tr->tr_command->command.cmd_pkt_9k.sgl_offset = 16; 179 180 /* offset from end of hdr = max cdb len */ 181 ld_twa_scsicmd(sc, tr, bp); 182 183 tr->tr_callback = ld_twa_handler; 184 tr->tr_ld_sc = sc; 185 186 tr->bp = bp; 187 188 rv = twa_map_request(tr); 189 190 return (rv); 191 } 192 193 static int 194 ld_twa_start(struct ld_softc *ld, struct buf *bp) 195 { 196 197 return (ld_twa_dobio((struct ld_twa_softc *)ld, bp->b_data, 198 bp->b_bcount, bp->b_rawblkno, bp)); 199 } 200 201 static void 202 ld_twa_handler(struct twa_request *tr) 203 { 204 uint8_t status; 205 struct buf *bp; 206 struct ld_twa_softc *sc; 207 struct twa_softc *twa; 208 209 bp = tr->bp; 210 sc = (struct ld_twa_softc *)tr->tr_ld_sc; 211 twa = device_private(device_parent(sc->sc_ld.sc_dv)); 212 213 status = tr->tr_command->command.cmd_pkt_9k.status; 214 215 if (status != 0) { 216 bp->b_error = EIO; 217 bp->b_resid = bp->b_bcount; 218 } else { 219 bp->b_resid = 0; 220 bp->b_error = 0; 221 } 222 twa_release_request(tr); 223 224 lddone(&sc->sc_ld, bp); 225 } 226 227 static int 228 ld_twa_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 229 { 230 231 #if 0 232 /* XXX Unsafe right now. */ 233 return (ld_twa_dobio((struct ld_twa_softc *)ld, data, 234 blkcnt * ld->sc_secsize, blkno, NULL)); 235 #else 236 return EIO; 237 #endif 238 239 } 240 241 242 static int 243 ld_twa_flush(struct ld_softc *ld, int flags) 244 { 245 int s, rv = 0; 246 struct twa_request *tr; 247 struct twa_softc *twa = device_private(device_parent(ld->sc_dv)); 248 struct ld_twa_softc *sc = (void *)ld; 249 struct twa_command_generic *generic_cmd; 250 251 /* Get a request packet. */ 252 tr = twa_get_request_wait(twa, 0); 253 KASSERT(tr != NULL); 254 255 tr->tr_cmd_pkt_type = 256 (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL); 257 258 tr->tr_callback = twa_request_wait_handler; 259 tr->tr_ld_sc = sc; 260 261 tr->tr_command->cmd_hdr.header_desc.size_header = 128; 262 263 generic_cmd = &(tr->tr_command->command.cmd_pkt_7k.generic); 264 generic_cmd->opcode = TWA_OP_FLUSH; 265 generic_cmd->size = 2; 266 generic_cmd->unit = sc->sc_hwunit; 267 generic_cmd->request_id = tr->tr_request_id; 268 generic_cmd->sgl_offset = 0; 269 generic_cmd->host_id = 0; 270 generic_cmd->status = 0; 271 generic_cmd->flags = 0; 272 generic_cmd->count = 0; 273 rv = twa_map_request(tr); 274 s = splbio(); 275 while (tr->tr_status != TWA_CMD_COMPLETE) 276 if ((rv = tsleep(tr, PRIBIO, "twaflush", 60 * hz)) != 0) 277 break; 278 twa_release_request(tr); 279 splx(s); 280 281 return (rv); 282 } 283 284 static void 285 ld_twa_adjqparam(device_t self, int openings) 286 { 287 struct ld_twa_softc *sc = device_private(self); 288 struct ld_softc *ld = &sc->sc_ld; 289 290 ldadjqparam(ld, 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