1 /*- 2 * Copyright 2007 Scott Long 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_mfi.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/selinfo.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/eventhandler.h> 41 #include <sys/rman.h> 42 #include <sys/bus_dma.h> 43 #include <sys/bio.h> 44 #include <sys/ioccom.h> 45 #include <sys/uio.h> 46 #include <sys/proc.h> 47 #include <sys/signalvar.h> 48 49 #include <cam/cam.h> 50 #include <cam/cam_ccb.h> 51 #include <cam/cam_debug.h> 52 #include <cam/cam_sim.h> 53 #include <cam/cam_xpt_sim.h> 54 #include <cam/scsi/scsi_all.h> 55 #include <cam/scsi/scsi_message.h> 56 57 #include <sys/bus.h> 58 #include <sys/conf.h> 59 #include <machine/md_var.h> 60 #include <machine/bus.h> 61 #include <machine/resource.h> 62 #include <sys/rman.h> 63 64 #include <dev/mfi/mfireg.h> 65 #include <dev/mfi/mfi_ioctl.h> 66 #include <dev/mfi/mfivar.h> 67 68 struct mfip_softc { 69 device_t dev; 70 struct mfi_softc *mfi_sc; 71 struct cam_devq *devq; 72 struct cam_sim *sim; 73 struct cam_path *path; 74 }; 75 76 static int mfip_probe(device_t); 77 static int mfip_attach(device_t); 78 static int mfip_detach(device_t); 79 static void mfip_cam_action(struct cam_sim *, union ccb *); 80 static void mfip_cam_poll(struct cam_sim *); 81 static struct mfi_command * mfip_start(void *); 82 static void mfip_done(struct mfi_command *cm); 83 84 static devclass_t mfip_devclass; 85 static device_method_t mfip_methods[] = { 86 DEVMETHOD(device_probe, mfip_probe), 87 DEVMETHOD(device_attach, mfip_attach), 88 DEVMETHOD(device_detach, mfip_detach), 89 {0, 0} 90 }; 91 static driver_t mfip_driver = { 92 "mfip", 93 mfip_methods, 94 sizeof(struct mfip_softc) 95 }; 96 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0); 97 MODULE_DEPEND(mfip, cam, 1, 1, 1); 98 99 #define ccb_mfip_ptr sim_priv.entries[0].ptr 100 101 static int 102 mfip_probe(device_t dev) 103 { 104 105 device_set_desc(dev, "SCSI Passthrough Bus"); 106 return (0); 107 } 108 109 static int 110 mfip_attach(device_t dev) 111 { 112 struct mfip_softc *sc; 113 struct mfi_softc *mfisc; 114 115 sc = device_get_softc(dev); 116 if (sc == NULL) 117 return (EINVAL); 118 119 mfisc = device_get_softc(device_get_parent(dev)); 120 sc->dev = dev; 121 sc->mfi_sc = mfisc; 122 mfisc->mfi_cam_start = mfip_start; 123 124 if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL) 125 return (ENOMEM); 126 127 sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc, 128 device_get_unit(dev), &mfisc->mfi_io_lock, 1, 129 MFI_SCSI_MAX_CMDS, sc->devq); 130 if (sc->sim == NULL) { 131 cam_simq_free(sc->devq); 132 device_printf(dev, "CAM SIM attach failed\n"); 133 return (EINVAL); 134 } 135 136 mtx_lock(&mfisc->mfi_io_lock); 137 if (xpt_bus_register(sc->sim, dev, 0) != 0) { 138 device_printf(dev, "XPT bus registration failed\n"); 139 cam_sim_free(sc->sim, FALSE); 140 cam_simq_free(sc->devq); 141 mtx_unlock(&mfisc->mfi_io_lock); 142 return (EINVAL); 143 } 144 mtx_unlock(&mfisc->mfi_io_lock); 145 146 return (0); 147 } 148 149 static int 150 mfip_detach(device_t dev) 151 { 152 struct mfip_softc *sc; 153 154 sc = device_get_softc(dev); 155 if (sc == NULL) 156 return (EINVAL); 157 158 if (sc->sim != NULL) { 159 mtx_lock(&sc->mfi_sc->mfi_io_lock); 160 xpt_bus_deregister(cam_sim_path(sc->sim)); 161 cam_sim_free(sc->sim, FALSE); 162 mtx_unlock(&sc->mfi_sc->mfi_io_lock); 163 } 164 165 if (sc->devq != NULL) 166 cam_simq_free(sc->devq); 167 168 return (0); 169 } 170 171 static void 172 mfip_cam_action(struct cam_sim *sim, union ccb *ccb) 173 { 174 struct mfip_softc *sc = cam_sim_softc(sim); 175 struct mfi_softc *mfisc = sc->mfi_sc; 176 177 mtx_assert(&mfisc->mfi_io_lock, MA_OWNED); 178 179 switch (ccb->ccb_h.func_code) { 180 case XPT_PATH_INQ: 181 { 182 struct ccb_pathinq *cpi = &ccb->cpi; 183 184 cpi->version_num = 1; 185 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 186 cpi->target_sprt = 0; 187 cpi->hba_misc = PIM_NOBUSRESET; 188 cpi->hba_eng_cnt = 0; 189 cpi->max_target = MFI_SCSI_MAX_TARGETS; 190 cpi->max_lun = MFI_SCSI_MAX_LUNS; 191 cpi->initiator_id = MFI_SCSI_INITIATOR_ID; 192 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 193 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN); 194 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 195 cpi->unit_number = cam_sim_unit(sim); 196 cpi->bus_id = cam_sim_bus(sim); 197 cpi->base_transfer_speed = 150000; 198 cpi->transport = XPORT_SPI; 199 cpi->transport_version = 2; 200 cpi->protocol = PROTO_SCSI; 201 cpi->protocol_version = SCSI_REV_2; 202 cpi->ccb_h.status = CAM_REQ_CMP; 203 break; 204 } 205 case XPT_RESET_BUS: 206 ccb->ccb_h.status = CAM_REQ_CMP; 207 break; 208 case XPT_RESET_DEV: 209 ccb->ccb_h.status = CAM_REQ_CMP; 210 break; 211 case XPT_GET_TRAN_SETTINGS: 212 { 213 struct ccb_trans_settings_scsi *scsi = 214 &ccb->cts.proto_specific.scsi; 215 struct ccb_trans_settings_spi *spi = 216 &ccb->cts.xport_specific.spi; 217 218 ccb->cts.protocol = PROTO_SCSI; 219 ccb->cts.protocol = SCSI_REV_2; 220 ccb->cts.transport = XPORT_SPI; 221 ccb->cts.transport_version = 2; 222 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 223 scsi->valid = CTS_SCSI_VALID_TQ; 224 spi->valid |= CTS_SPI_VALID_DISC; 225 } else 226 scsi->valid = 0; 227 ccb->ccb_h.status = CAM_REQ_CMP; 228 break; 229 } 230 case XPT_SET_TRAN_SETTINGS: 231 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 232 break; 233 case XPT_SCSI_IO: 234 { 235 struct ccb_hdr *ccbh = &ccb->ccb_h; 236 struct ccb_scsiio *csio = &ccb->csio; 237 238 ccbh->status = CAM_REQ_INPROG; 239 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) { 240 ccbh->status = CAM_REQ_INVALID; 241 break; 242 } 243 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 244 if (ccbh->flags & CAM_DATA_PHYS) { 245 ccbh->status = CAM_REQ_INVALID; 246 break; 247 } 248 if (ccbh->flags & CAM_SCATTER_VALID) { 249 ccbh->status = CAM_REQ_INVALID; 250 break; 251 } 252 } 253 254 ccbh->ccb_mfip_ptr = sc; 255 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe); 256 mfi_startio(mfisc); 257 return; 258 } 259 default: 260 ccb->ccb_h.status = CAM_REQ_INVALID; 261 break; 262 } 263 264 xpt_done(ccb); 265 return; 266 } 267 268 static struct mfi_command * 269 mfip_start(void *data) 270 { 271 union ccb *ccb = data; 272 struct ccb_hdr *ccbh = &ccb->ccb_h; 273 struct ccb_scsiio *csio = &ccb->csio; 274 struct mfip_softc *sc; 275 struct mfi_pass_frame *pt; 276 struct mfi_command *cm; 277 278 sc = ccbh->ccb_mfip_ptr; 279 280 if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL) 281 return (NULL); 282 283 pt = &cm->cm_frame->pass; 284 pt->header.cmd = MFI_CMD_PD_SCSI_IO; 285 pt->header.cmd_status = 0; 286 pt->header.scsi_status = 0; 287 pt->header.target_id = ccbh->target_id; 288 pt->header.lun_id = ccbh->target_lun; 289 pt->header.flags = 0; 290 pt->header.timeout = 0; 291 pt->header.data_len = csio->dxfer_len; 292 pt->header.sense_len = MFI_SENSE_LEN; 293 pt->header.cdb_len = csio->cdb_len; 294 pt->sense_addr_lo = cm->cm_sense_busaddr; 295 pt->sense_addr_hi = 0; 296 if (ccbh->flags & CAM_CDB_POINTER) 297 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len); 298 else 299 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len); 300 cm->cm_complete = mfip_done; 301 cm->cm_private = ccb; 302 cm->cm_sg = &pt->sgl; 303 cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE; 304 cm->cm_data = csio->data_ptr; 305 cm->cm_len = csio->dxfer_len; 306 switch (ccbh->flags & CAM_DIR_MASK) { 307 case CAM_DIR_IN: 308 cm->cm_flags = MFI_CMD_DATAIN; 309 break; 310 case CAM_DIR_OUT: 311 cm->cm_flags = MFI_CMD_DATAOUT; 312 break; 313 case CAM_DIR_NONE: 314 default: 315 cm->cm_data = NULL; 316 cm->cm_len = 0; 317 cm->cm_flags = 0; 318 break; 319 } 320 321 TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe); 322 return (cm); 323 } 324 325 static void 326 mfip_done(struct mfi_command *cm) 327 { 328 union ccb *ccb = cm->cm_private; 329 struct ccb_hdr *ccbh = &ccb->ccb_h; 330 struct ccb_scsiio *csio = &ccb->csio; 331 struct mfip_softc *sc; 332 struct mfi_pass_frame *pt; 333 334 sc = ccbh->ccb_mfip_ptr; 335 pt = &cm->cm_frame->pass; 336 337 switch (pt->header.cmd_status) { 338 case MFI_STAT_OK: 339 { 340 uint8_t command, device; 341 342 ccbh->status = CAM_REQ_CMP; 343 csio->scsi_status = pt->header.scsi_status; 344 if (ccbh->flags & CAM_CDB_POINTER) 345 command = ccb->csio.cdb_io.cdb_ptr[0]; 346 else 347 command = ccb->csio.cdb_io.cdb_bytes[0]; 348 if (command == INQUIRY) { 349 device = ccb->csio.data_ptr[0] & 0x1f; 350 if ((device == T_DIRECT) || (device == T_PROCESSOR)) 351 csio->data_ptr[0] = 352 (device & 0xe0) | T_NODEVICE; 353 } 354 break; 355 } 356 case MFI_STAT_SCSI_DONE_WITH_ERROR: 357 { 358 int sense_len; 359 360 ccbh->status = CAM_REQ_CMP_ERR | CAM_AUTOSNS_VALID; 361 csio->scsi_status = pt->header.scsi_status; 362 sense_len = min(pt->header.sense_len, sizeof(struct scsi_sense_data)); 363 bzero(&csio->sense_data, sizeof(struct scsi_sense_data)); 364 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len); 365 break; 366 } 367 case MFI_STAT_DEVICE_NOT_FOUND: 368 ccbh->status = CAM_DEV_NOT_THERE; 369 break; 370 case MFI_STAT_SCSI_IO_FAILED: 371 ccbh->status = CAM_REQ_CMP_ERR; 372 csio->scsi_status = pt->header.scsi_status; 373 break; 374 default: 375 ccbh->status = CAM_REQ_CMP_ERR; 376 csio->scsi_status = pt->header.scsi_status; 377 break; 378 } 379 380 mfi_release_command(cm); 381 xpt_done(ccb); 382 } 383 384 static void 385 mfip_cam_poll(struct cam_sim *sim) 386 { 387 return; 388 } 389 390