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