1f366931cSScott Long /*- 2f366931cSScott Long * Copyright 2007 Scott Long 3f366931cSScott Long * All rights reserved. 4f366931cSScott Long * 5f366931cSScott Long * Redistribution and use in source and binary forms, with or without 6f366931cSScott Long * modification, are permitted provided that the following conditions 7f366931cSScott Long * are met: 8f366931cSScott Long * 1. Redistributions of source code must retain the above copyright 9f366931cSScott Long * notice, this list of conditions and the following disclaimer. 10f366931cSScott Long * 2. Redistributions in binary form must reproduce the above copyright 11f366931cSScott Long * notice, this list of conditions and the following disclaimer in the 12f366931cSScott Long * documentation and/or other materials provided with the distribution. 13f366931cSScott Long * 14f366931cSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15f366931cSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16f366931cSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17f366931cSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18f366931cSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19f366931cSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20f366931cSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f366931cSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f366931cSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f366931cSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24f366931cSScott Long * SUCH DAMAGE. 25f366931cSScott Long */ 26f366931cSScott Long 27f366931cSScott Long #include <sys/cdefs.h> 28f366931cSScott Long __FBSDID("$FreeBSD$"); 29f366931cSScott Long 30f366931cSScott Long #include "opt_mfi.h" 31f366931cSScott Long 32f366931cSScott Long #include <sys/param.h> 33f366931cSScott Long #include <sys/systm.h> 34f366931cSScott Long #include <sys/kernel.h> 35f366931cSScott Long #include <sys/malloc.h> 36f366931cSScott Long #include <sys/module.h> 37f366931cSScott Long #include <sys/selinfo.h> 38f366931cSScott Long #include <sys/bus.h> 39f366931cSScott Long #include <sys/conf.h> 40f366931cSScott Long #include <sys/eventhandler.h> 41f366931cSScott Long #include <sys/rman.h> 42f366931cSScott Long #include <sys/bus_dma.h> 43f366931cSScott Long #include <sys/bio.h> 44f366931cSScott Long #include <sys/ioccom.h> 45f366931cSScott Long #include <sys/uio.h> 46f366931cSScott Long #include <sys/proc.h> 47f366931cSScott Long #include <sys/signalvar.h> 48d1c5fc76SJohn Baldwin #include <sys/sysctl.h> 49f366931cSScott Long 50f366931cSScott Long #include <cam/cam.h> 51f366931cSScott Long #include <cam/cam_ccb.h> 52f366931cSScott Long #include <cam/cam_debug.h> 5359ddd3e9SDoug Ambrisko #include <cam/cam_periph.h> 54f366931cSScott Long #include <cam/cam_sim.h> 5559ddd3e9SDoug Ambrisko #include <cam/cam_xpt_periph.h> 56f366931cSScott Long #include <cam/cam_xpt_sim.h> 57f366931cSScott Long #include <cam/scsi/scsi_all.h> 58f366931cSScott Long #include <cam/scsi/scsi_message.h> 59f366931cSScott Long 60f366931cSScott Long #include <machine/md_var.h> 61f366931cSScott Long #include <machine/bus.h> 62f366931cSScott Long #include <machine/resource.h> 63f366931cSScott Long 64f366931cSScott Long #include <dev/mfi/mfireg.h> 65f366931cSScott Long #include <dev/mfi/mfi_ioctl.h> 66f366931cSScott Long #include <dev/mfi/mfivar.h> 67f366931cSScott Long 6859ddd3e9SDoug Ambrisko enum mfip_state { 6959ddd3e9SDoug Ambrisko MFIP_STATE_NONE, 7059ddd3e9SDoug Ambrisko MFIP_STATE_DETACH, 7159ddd3e9SDoug Ambrisko MFIP_STATE_RESCAN 7259ddd3e9SDoug Ambrisko }; 7359ddd3e9SDoug Ambrisko 74f366931cSScott Long struct mfip_softc { 75f366931cSScott Long device_t dev; 76f366931cSScott Long struct mfi_softc *mfi_sc; 77f366931cSScott Long struct cam_devq *devq; 78f366931cSScott Long struct cam_sim *sim; 79f366931cSScott Long struct cam_path *path; 8059ddd3e9SDoug Ambrisko enum mfip_state state; 81f366931cSScott Long }; 82f366931cSScott Long 83f366931cSScott Long static int mfip_probe(device_t); 84f366931cSScott Long static int mfip_attach(device_t); 85f366931cSScott Long static int mfip_detach(device_t); 86f366931cSScott Long static void mfip_cam_action(struct cam_sim *, union ccb *); 87f366931cSScott Long static void mfip_cam_poll(struct cam_sim *); 8859ddd3e9SDoug Ambrisko static void mfip_cam_rescan(struct mfi_softc *, uint32_t tid); 89f366931cSScott Long static struct mfi_command * mfip_start(void *); 90f366931cSScott Long static void mfip_done(struct mfi_command *cm); 91f366931cSScott Long 9258ef3154SDoug Ambrisko static int mfi_allow_disks = 0; 9358ef3154SDoug Ambrisko TUNABLE_INT("hw.mfi.allow_cam_disk_passthrough", &mfi_allow_disks); 9458ef3154SDoug Ambrisko SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RD, 9558ef3154SDoug Ambrisko &mfi_allow_disks, 0, "event message locale"); 9658ef3154SDoug Ambrisko 97f366931cSScott Long static devclass_t mfip_devclass; 98f366931cSScott Long static device_method_t mfip_methods[] = { 99f366931cSScott Long DEVMETHOD(device_probe, mfip_probe), 100f366931cSScott Long DEVMETHOD(device_attach, mfip_attach), 101f366931cSScott Long DEVMETHOD(device_detach, mfip_detach), 10261bfd867SSofian Brabez 10361bfd867SSofian Brabez DEVMETHOD_END 104f366931cSScott Long }; 105f366931cSScott Long static driver_t mfip_driver = { 106f366931cSScott Long "mfip", 107f366931cSScott Long mfip_methods, 108f366931cSScott Long sizeof(struct mfip_softc) 109f366931cSScott Long }; 110f366931cSScott Long DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0); 111f366931cSScott Long MODULE_DEPEND(mfip, cam, 1, 1, 1); 1127765cff7SKonstantin Belousov MODULE_DEPEND(mfip, mfi, 1, 1, 1); 113f366931cSScott Long 114f366931cSScott Long #define ccb_mfip_ptr sim_priv.entries[0].ptr 115f366931cSScott Long 116f366931cSScott Long static int 117f366931cSScott Long mfip_probe(device_t dev) 118f366931cSScott Long { 119f366931cSScott Long 120f366931cSScott Long device_set_desc(dev, "SCSI Passthrough Bus"); 121f366931cSScott Long return (0); 122f366931cSScott Long } 123f366931cSScott Long 124f366931cSScott Long static int 125f366931cSScott Long mfip_attach(device_t dev) 126f366931cSScott Long { 127f366931cSScott Long struct mfip_softc *sc; 128f366931cSScott Long struct mfi_softc *mfisc; 129f366931cSScott Long 130f366931cSScott Long sc = device_get_softc(dev); 131f366931cSScott Long if (sc == NULL) 132f366931cSScott Long return (EINVAL); 133f366931cSScott Long 134f366931cSScott Long mfisc = device_get_softc(device_get_parent(dev)); 135f366931cSScott Long sc->dev = dev; 13659ddd3e9SDoug Ambrisko sc->state = MFIP_STATE_NONE; 137f366931cSScott Long sc->mfi_sc = mfisc; 138f366931cSScott Long mfisc->mfi_cam_start = mfip_start; 139f366931cSScott Long 140f366931cSScott Long if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL) 141f366931cSScott Long return (ENOMEM); 142f366931cSScott Long 143f366931cSScott Long sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc, 144f366931cSScott Long device_get_unit(dev), &mfisc->mfi_io_lock, 1, 145f366931cSScott Long MFI_SCSI_MAX_CMDS, sc->devq); 146f366931cSScott Long if (sc->sim == NULL) { 147f366931cSScott Long cam_simq_free(sc->devq); 14808c89430SSteven Hartland sc->devq = NULL; 149f366931cSScott Long device_printf(dev, "CAM SIM attach failed\n"); 150f366931cSScott Long return (EINVAL); 151f366931cSScott Long } 152f366931cSScott Long 15359ddd3e9SDoug Ambrisko mfisc->mfi_cam_rescan_cb = mfip_cam_rescan; 15459ddd3e9SDoug Ambrisko 155f366931cSScott Long mtx_lock(&mfisc->mfi_io_lock); 156b50569b7SScott Long if (xpt_bus_register(sc->sim, dev, 0) != 0) { 157f366931cSScott Long device_printf(dev, "XPT bus registration failed\n"); 158f366931cSScott Long cam_sim_free(sc->sim, FALSE); 15908c89430SSteven Hartland sc->sim = NULL; 160f366931cSScott Long cam_simq_free(sc->devq); 16108c89430SSteven Hartland sc->devq = NULL; 162f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 163f366931cSScott Long return (EINVAL); 164f366931cSScott Long } 165f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 166f366931cSScott Long 167f366931cSScott Long return (0); 168f366931cSScott Long } 169f366931cSScott Long 170f366931cSScott Long static int 171f366931cSScott Long mfip_detach(device_t dev) 172f366931cSScott Long { 173f366931cSScott Long struct mfip_softc *sc; 174f366931cSScott Long 175f366931cSScott Long sc = device_get_softc(dev); 176f366931cSScott Long if (sc == NULL) 177f366931cSScott Long return (EINVAL); 178f366931cSScott Long 17959ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_sc->mfi_io_lock); 18059ddd3e9SDoug Ambrisko if (sc->state == MFIP_STATE_RESCAN) { 18159ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 18259ddd3e9SDoug Ambrisko return (EBUSY); 18359ddd3e9SDoug Ambrisko } 18459ddd3e9SDoug Ambrisko sc->state = MFIP_STATE_DETACH; 18559ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 18659ddd3e9SDoug Ambrisko 18759ddd3e9SDoug Ambrisko sc->mfi_sc->mfi_cam_rescan_cb = NULL; 18859ddd3e9SDoug Ambrisko 189f366931cSScott Long if (sc->sim != NULL) { 190f366931cSScott Long mtx_lock(&sc->mfi_sc->mfi_io_lock); 191f366931cSScott Long xpt_bus_deregister(cam_sim_path(sc->sim)); 192f366931cSScott Long cam_sim_free(sc->sim, FALSE); 19308c89430SSteven Hartland sc->sim = NULL; 194f366931cSScott Long mtx_unlock(&sc->mfi_sc->mfi_io_lock); 195f366931cSScott Long } 196f366931cSScott Long 19708c89430SSteven Hartland if (sc->devq != NULL) { 198f366931cSScott Long cam_simq_free(sc->devq); 19908c89430SSteven Hartland sc->devq = NULL; 20008c89430SSteven Hartland } 201f366931cSScott Long 202f366931cSScott Long return (0); 203f366931cSScott Long } 204f366931cSScott Long 205f366931cSScott Long static void 206f366931cSScott Long mfip_cam_action(struct cam_sim *sim, union ccb *ccb) 207f366931cSScott Long { 208f366931cSScott Long struct mfip_softc *sc = cam_sim_softc(sim); 209f366931cSScott Long struct mfi_softc *mfisc = sc->mfi_sc; 210f366931cSScott Long 211f366931cSScott Long mtx_assert(&mfisc->mfi_io_lock, MA_OWNED); 212f366931cSScott Long 213f366931cSScott Long switch (ccb->ccb_h.func_code) { 214f366931cSScott Long case XPT_PATH_INQ: 215f366931cSScott Long { 216f366931cSScott Long struct ccb_pathinq *cpi = &ccb->cpi; 217f366931cSScott Long 218f366931cSScott Long cpi->version_num = 1; 219f366931cSScott Long cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 220f366931cSScott Long cpi->target_sprt = 0; 221b05e6558SScott Long cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN; 222f366931cSScott Long cpi->hba_eng_cnt = 0; 223f366931cSScott Long cpi->max_target = MFI_SCSI_MAX_TARGETS; 224f366931cSScott Long cpi->max_lun = MFI_SCSI_MAX_LUNS; 225f366931cSScott Long cpi->initiator_id = MFI_SCSI_INITIATOR_ID; 226f366931cSScott Long strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 227f366931cSScott Long strncpy(cpi->hba_vid, "LSI", HBA_IDLEN); 228f366931cSScott Long strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 229f366931cSScott Long cpi->unit_number = cam_sim_unit(sim); 230f366931cSScott Long cpi->bus_id = cam_sim_bus(sim); 231f366931cSScott Long cpi->base_transfer_speed = 150000; 232b05e6558SScott Long cpi->transport = XPORT_SAS; 233b05e6558SScott Long cpi->transport_version = 0; 234f366931cSScott Long cpi->protocol = PROTO_SCSI; 235f366931cSScott Long cpi->protocol_version = SCSI_REV_2; 236f366931cSScott Long cpi->ccb_h.status = CAM_REQ_CMP; 237f366931cSScott Long break; 238f366931cSScott Long } 239f366931cSScott Long case XPT_RESET_BUS: 240f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 241f366931cSScott Long break; 242f366931cSScott Long case XPT_RESET_DEV: 243f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 244f366931cSScott Long break; 245f366931cSScott Long case XPT_GET_TRAN_SETTINGS: 246f366931cSScott Long { 247b05e6558SScott Long struct ccb_trans_settings_sas *sas = 248b05e6558SScott Long &ccb->cts.xport_specific.sas; 249f366931cSScott Long 250f366931cSScott Long ccb->cts.protocol = PROTO_SCSI; 251b05e6558SScott Long ccb->cts.protocol_version = SCSI_REV_2; 252b05e6558SScott Long ccb->cts.transport = XPORT_SAS; 253b05e6558SScott Long ccb->cts.transport_version = 0; 254b05e6558SScott Long 255b05e6558SScott Long sas->valid &= ~CTS_SAS_VALID_SPEED; 256b05e6558SScott Long sas->bitrate = 150000; 257b05e6558SScott Long 258f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 259f366931cSScott Long break; 260f366931cSScott Long } 261f366931cSScott Long case XPT_SET_TRAN_SETTINGS: 262f366931cSScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 263f366931cSScott Long break; 264f366931cSScott Long case XPT_SCSI_IO: 265f366931cSScott Long { 266f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 267f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 268f366931cSScott Long 269f366931cSScott Long ccbh->status = CAM_REQ_INPROG; 270f366931cSScott Long if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) { 271f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 272f366931cSScott Long break; 273f366931cSScott Long } 274f366931cSScott Long ccbh->ccb_mfip_ptr = sc; 275f366931cSScott Long TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe); 276f366931cSScott Long mfi_startio(mfisc); 277f366931cSScott Long return; 278f366931cSScott Long } 279f366931cSScott Long default: 280f366931cSScott Long ccb->ccb_h.status = CAM_REQ_INVALID; 281f366931cSScott Long break; 282f366931cSScott Long } 283f366931cSScott Long 284f366931cSScott Long xpt_done(ccb); 285f366931cSScott Long return; 286f366931cSScott Long } 287f366931cSScott Long 28859ddd3e9SDoug Ambrisko static void 28959ddd3e9SDoug Ambrisko mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid) 29059ddd3e9SDoug Ambrisko { 29159ddd3e9SDoug Ambrisko union ccb *ccb; 29259ddd3e9SDoug Ambrisko struct mfip_softc *camsc; 29359ddd3e9SDoug Ambrisko struct cam_sim *sim; 29459ddd3e9SDoug Ambrisko device_t mfip_dev; 29559ddd3e9SDoug Ambrisko 29659ddd3e9SDoug Ambrisko mtx_lock(&Giant); 29759ddd3e9SDoug Ambrisko mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1); 29859ddd3e9SDoug Ambrisko mtx_unlock(&Giant); 29959ddd3e9SDoug Ambrisko if (mfip_dev == NULL) { 30059ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n"); 30159ddd3e9SDoug Ambrisko return; 30259ddd3e9SDoug Ambrisko } 30359ddd3e9SDoug Ambrisko 30459ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_io_lock); 30559ddd3e9SDoug Ambrisko camsc = device_get_softc(mfip_dev); 30659ddd3e9SDoug Ambrisko if (camsc->state == MFIP_STATE_DETACH) { 30759ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 30859ddd3e9SDoug Ambrisko return; 30959ddd3e9SDoug Ambrisko } 31059ddd3e9SDoug Ambrisko camsc->state = MFIP_STATE_RESCAN; 31159ddd3e9SDoug Ambrisko 31259ddd3e9SDoug Ambrisko ccb = xpt_alloc_ccb_nowait(); 31359ddd3e9SDoug Ambrisko if (ccb == NULL) { 314*30e71983SMark Johnston mtx_unlock(&sc->mfi_io_lock); 31559ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 31659ddd3e9SDoug Ambrisko "Cannot allocate ccb for bus rescan.\n"); 31759ddd3e9SDoug Ambrisko return; 31859ddd3e9SDoug Ambrisko } 31959ddd3e9SDoug Ambrisko 32059ddd3e9SDoug Ambrisko sim = camsc->sim; 321e5dfa058SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim), 32259ddd3e9SDoug Ambrisko tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 32359ddd3e9SDoug Ambrisko xpt_free_ccb(ccb); 324f7469975SSean Bruno mtx_unlock(&sc->mfi_io_lock); 32559ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 32659ddd3e9SDoug Ambrisko "Cannot create path for bus rescan.\n"); 32759ddd3e9SDoug Ambrisko return; 32859ddd3e9SDoug Ambrisko } 32959ddd3e9SDoug Ambrisko xpt_rescan(ccb); 33059ddd3e9SDoug Ambrisko 33159ddd3e9SDoug Ambrisko camsc->state = MFIP_STATE_NONE; 33259ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 33359ddd3e9SDoug Ambrisko } 33459ddd3e9SDoug Ambrisko 335f366931cSScott Long static struct mfi_command * 336f366931cSScott Long mfip_start(void *data) 337f366931cSScott Long { 338f366931cSScott Long union ccb *ccb = data; 339f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 340f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 341f366931cSScott Long struct mfip_softc *sc; 342f366931cSScott Long struct mfi_pass_frame *pt; 343f366931cSScott Long struct mfi_command *cm; 3440d9a4ef3SDoug Ambrisko uint32_t context = 0; 345f366931cSScott Long 346f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 347f366931cSScott Long 348f366931cSScott Long if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL) 349f366931cSScott Long return (NULL); 350f366931cSScott Long 3510d9a4ef3SDoug Ambrisko /* Zero out the MFI frame */ 3520d9a4ef3SDoug Ambrisko context = cm->cm_frame->header.context; 3530d9a4ef3SDoug Ambrisko bzero(cm->cm_frame, sizeof(union mfi_frame)); 3540d9a4ef3SDoug Ambrisko cm->cm_frame->header.context = context; 3550d9a4ef3SDoug Ambrisko 356f366931cSScott Long pt = &cm->cm_frame->pass; 357f366931cSScott Long pt->header.cmd = MFI_CMD_PD_SCSI_IO; 358f366931cSScott Long pt->header.cmd_status = 0; 359f366931cSScott Long pt->header.scsi_status = 0; 360f366931cSScott Long pt->header.target_id = ccbh->target_id; 361f366931cSScott Long pt->header.lun_id = ccbh->target_lun; 362f366931cSScott Long pt->header.flags = 0; 363f366931cSScott Long pt->header.timeout = 0; 364f366931cSScott Long pt->header.data_len = csio->dxfer_len; 365f366931cSScott Long pt->header.sense_len = MFI_SENSE_LEN; 366f366931cSScott Long pt->header.cdb_len = csio->cdb_len; 3677d96f12cSSean Bruno pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr; 3687d96f12cSSean Bruno pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32); 369f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 370f366931cSScott Long bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len); 371f366931cSScott Long else 372f366931cSScott Long bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len); 373f366931cSScott Long cm->cm_complete = mfip_done; 374f366931cSScott Long cm->cm_private = ccb; 375f366931cSScott Long cm->cm_sg = &pt->sgl; 376f366931cSScott Long cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE; 377dd0b4fb6SKonstantin Belousov cm->cm_data = ccb; 378f366931cSScott Long cm->cm_len = csio->dxfer_len; 379f366931cSScott Long switch (ccbh->flags & CAM_DIR_MASK) { 380f366931cSScott Long case CAM_DIR_IN: 381dd0b4fb6SKonstantin Belousov cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB; 382f366931cSScott Long break; 383f366931cSScott Long case CAM_DIR_OUT: 384dd0b4fb6SKonstantin Belousov cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB; 385f366931cSScott Long break; 386f366931cSScott Long case CAM_DIR_NONE: 387f366931cSScott Long default: 388f366931cSScott Long cm->cm_data = NULL; 389f366931cSScott Long cm->cm_len = 0; 390f366931cSScott Long cm->cm_flags = 0; 391f366931cSScott Long break; 392f366931cSScott Long } 393f366931cSScott Long 394f366931cSScott Long TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe); 395f366931cSScott Long return (cm); 396f366931cSScott Long } 397f366931cSScott Long 398f366931cSScott Long static void 399f366931cSScott Long mfip_done(struct mfi_command *cm) 400f366931cSScott Long { 401f366931cSScott Long union ccb *ccb = cm->cm_private; 402f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 403f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 404f366931cSScott Long struct mfip_softc *sc; 405f366931cSScott Long struct mfi_pass_frame *pt; 406f366931cSScott Long 407f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 408f366931cSScott Long pt = &cm->cm_frame->pass; 409f366931cSScott Long 410f366931cSScott Long switch (pt->header.cmd_status) { 411f366931cSScott Long case MFI_STAT_OK: 412f366931cSScott Long { 413f366931cSScott Long uint8_t command, device; 414f366931cSScott Long 415f366931cSScott Long ccbh->status = CAM_REQ_CMP; 416f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 417f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 418b596082bSJohn Baldwin command = csio->cdb_io.cdb_ptr[0]; 419f366931cSScott Long else 420b596082bSJohn Baldwin command = csio->cdb_io.cdb_bytes[0]; 421f366931cSScott Long if (command == INQUIRY) { 422b596082bSJohn Baldwin device = csio->data_ptr[0] & 0x1f; 42358ef3154SDoug Ambrisko if ((!mfi_allow_disks && device == T_DIRECT) || 42458ef3154SDoug Ambrisko (device == T_PROCESSOR)) 425f366931cSScott Long csio->data_ptr[0] = 426b596082bSJohn Baldwin (csio->data_ptr[0] & 0xe0) | T_NODEVICE; 427f366931cSScott Long } 428f366931cSScott Long break; 429f366931cSScott Long } 430f366931cSScott Long case MFI_STAT_SCSI_DONE_WITH_ERROR: 431f366931cSScott Long { 432f366931cSScott Long int sense_len; 433f366931cSScott Long 434b05e6558SScott Long ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 435f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 4361e5addb7SMarius Strobl if (pt->header.sense_len < csio->sense_len) 4371e5addb7SMarius Strobl csio->sense_resid = csio->sense_len - 4381e5addb7SMarius Strobl pt->header.sense_len; 4391e5addb7SMarius Strobl else 4401e5addb7SMarius Strobl csio->sense_resid = 0; 4411e5addb7SMarius Strobl sense_len = min(pt->header.sense_len, 4421e5addb7SMarius Strobl sizeof(struct scsi_sense_data)); 443f366931cSScott Long bzero(&csio->sense_data, sizeof(struct scsi_sense_data)); 444f366931cSScott Long bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len); 445f366931cSScott Long break; 446f366931cSScott Long } 447f366931cSScott Long case MFI_STAT_DEVICE_NOT_FOUND: 448b05e6558SScott Long ccbh->status = CAM_SEL_TIMEOUT; 449f366931cSScott Long break; 450f366931cSScott Long case MFI_STAT_SCSI_IO_FAILED: 451f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 452f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 453f366931cSScott Long break; 454f366931cSScott Long default: 455f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 456f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 457f366931cSScott Long break; 458f366931cSScott Long } 459f366931cSScott Long 460f366931cSScott Long mfi_release_command(cm); 461f366931cSScott Long xpt_done(ccb); 462f366931cSScott Long } 463f366931cSScott Long 464f366931cSScott Long static void 465f366931cSScott Long mfip_cam_poll(struct cam_sim *sim) 466f366931cSScott Long { 46758ef3154SDoug Ambrisko struct mfip_softc *sc = cam_sim_softc(sim); 46858ef3154SDoug Ambrisko struct mfi_softc *mfisc = sc->mfi_sc; 46958ef3154SDoug Ambrisko 47058ef3154SDoug Ambrisko mfisc->mfi_intr_ptr(mfisc); 471f366931cSScott Long } 472f366931cSScott Long 473