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), 102*61bfd867SSofian Brabez 103*61bfd867SSofian 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); 148f366931cSScott Long device_printf(dev, "CAM SIM attach failed\n"); 149f366931cSScott Long return (EINVAL); 150f366931cSScott Long } 151f366931cSScott Long 15259ddd3e9SDoug Ambrisko mfisc->mfi_cam_rescan_cb = mfip_cam_rescan; 15359ddd3e9SDoug Ambrisko 154f366931cSScott Long mtx_lock(&mfisc->mfi_io_lock); 155b50569b7SScott Long if (xpt_bus_register(sc->sim, dev, 0) != 0) { 156f366931cSScott Long device_printf(dev, "XPT bus registration failed\n"); 157f366931cSScott Long cam_sim_free(sc->sim, FALSE); 158f366931cSScott Long cam_simq_free(sc->devq); 159f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 160f366931cSScott Long return (EINVAL); 161f366931cSScott Long } 162f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 163f366931cSScott Long 164f366931cSScott Long return (0); 165f366931cSScott Long } 166f366931cSScott Long 167f366931cSScott Long static int 168f366931cSScott Long mfip_detach(device_t dev) 169f366931cSScott Long { 170f366931cSScott Long struct mfip_softc *sc; 171f366931cSScott Long 172f366931cSScott Long sc = device_get_softc(dev); 173f366931cSScott Long if (sc == NULL) 174f366931cSScott Long return (EINVAL); 175f366931cSScott Long 17659ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_sc->mfi_io_lock); 17759ddd3e9SDoug Ambrisko if (sc->state == MFIP_STATE_RESCAN) { 17859ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 17959ddd3e9SDoug Ambrisko return (EBUSY); 18059ddd3e9SDoug Ambrisko } 18159ddd3e9SDoug Ambrisko sc->state = MFIP_STATE_DETACH; 18259ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 18359ddd3e9SDoug Ambrisko 18459ddd3e9SDoug Ambrisko sc->mfi_sc->mfi_cam_rescan_cb = NULL; 18559ddd3e9SDoug Ambrisko 186f366931cSScott Long if (sc->sim != NULL) { 187f366931cSScott Long mtx_lock(&sc->mfi_sc->mfi_io_lock); 188f366931cSScott Long xpt_bus_deregister(cam_sim_path(sc->sim)); 189f366931cSScott Long cam_sim_free(sc->sim, FALSE); 190f366931cSScott Long mtx_unlock(&sc->mfi_sc->mfi_io_lock); 191f366931cSScott Long } 192f366931cSScott Long 193f366931cSScott Long if (sc->devq != NULL) 194f366931cSScott Long cam_simq_free(sc->devq); 195f366931cSScott Long 196f366931cSScott Long return (0); 197f366931cSScott Long } 198f366931cSScott Long 199f366931cSScott Long static void 200f366931cSScott Long mfip_cam_action(struct cam_sim *sim, union ccb *ccb) 201f366931cSScott Long { 202f366931cSScott Long struct mfip_softc *sc = cam_sim_softc(sim); 203f366931cSScott Long struct mfi_softc *mfisc = sc->mfi_sc; 204f366931cSScott Long 205f366931cSScott Long mtx_assert(&mfisc->mfi_io_lock, MA_OWNED); 206f366931cSScott Long 207f366931cSScott Long switch (ccb->ccb_h.func_code) { 208f366931cSScott Long case XPT_PATH_INQ: 209f366931cSScott Long { 210f366931cSScott Long struct ccb_pathinq *cpi = &ccb->cpi; 211f366931cSScott Long 212f366931cSScott Long cpi->version_num = 1; 213f366931cSScott Long cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 214f366931cSScott Long cpi->target_sprt = 0; 215b05e6558SScott Long cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN; 216f366931cSScott Long cpi->hba_eng_cnt = 0; 217f366931cSScott Long cpi->max_target = MFI_SCSI_MAX_TARGETS; 218f366931cSScott Long cpi->max_lun = MFI_SCSI_MAX_LUNS; 219f366931cSScott Long cpi->initiator_id = MFI_SCSI_INITIATOR_ID; 220f366931cSScott Long strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 221f366931cSScott Long strncpy(cpi->hba_vid, "LSI", HBA_IDLEN); 222f366931cSScott Long strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 223f366931cSScott Long cpi->unit_number = cam_sim_unit(sim); 224f366931cSScott Long cpi->bus_id = cam_sim_bus(sim); 225f366931cSScott Long cpi->base_transfer_speed = 150000; 226b05e6558SScott Long cpi->transport = XPORT_SAS; 227b05e6558SScott Long cpi->transport_version = 0; 228f366931cSScott Long cpi->protocol = PROTO_SCSI; 229f366931cSScott Long cpi->protocol_version = SCSI_REV_2; 230f366931cSScott Long cpi->ccb_h.status = CAM_REQ_CMP; 231f366931cSScott Long break; 232f366931cSScott Long } 233f366931cSScott Long case XPT_RESET_BUS: 234f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 235f366931cSScott Long break; 236f366931cSScott Long case XPT_RESET_DEV: 237f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 238f366931cSScott Long break; 239f366931cSScott Long case XPT_GET_TRAN_SETTINGS: 240f366931cSScott Long { 241b05e6558SScott Long struct ccb_trans_settings_sas *sas = 242b05e6558SScott Long &ccb->cts.xport_specific.sas; 243f366931cSScott Long 244f366931cSScott Long ccb->cts.protocol = PROTO_SCSI; 245b05e6558SScott Long ccb->cts.protocol_version = SCSI_REV_2; 246b05e6558SScott Long ccb->cts.transport = XPORT_SAS; 247b05e6558SScott Long ccb->cts.transport_version = 0; 248b05e6558SScott Long 249b05e6558SScott Long sas->valid &= ~CTS_SAS_VALID_SPEED; 250b05e6558SScott Long sas->bitrate = 150000; 251b05e6558SScott Long 252f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 253f366931cSScott Long break; 254f366931cSScott Long } 255f366931cSScott Long case XPT_SET_TRAN_SETTINGS: 256f366931cSScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 257f366931cSScott Long break; 258f366931cSScott Long case XPT_SCSI_IO: 259f366931cSScott Long { 260f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 261f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 262f366931cSScott Long 263f366931cSScott Long ccbh->status = CAM_REQ_INPROG; 264f366931cSScott Long if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) { 265f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 266f366931cSScott Long break; 267f366931cSScott Long } 268f366931cSScott Long if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 269f366931cSScott Long if (ccbh->flags & CAM_DATA_PHYS) { 270f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 271f366931cSScott Long break; 272f366931cSScott Long } 273f366931cSScott Long if (ccbh->flags & CAM_SCATTER_VALID) { 274f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 275f366931cSScott Long break; 276f366931cSScott Long } 277f366931cSScott Long } 278f366931cSScott Long 279f366931cSScott Long ccbh->ccb_mfip_ptr = sc; 280f366931cSScott Long TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe); 281f366931cSScott Long mfi_startio(mfisc); 282f366931cSScott Long return; 283f366931cSScott Long } 284f366931cSScott Long default: 285f366931cSScott Long ccb->ccb_h.status = CAM_REQ_INVALID; 286f366931cSScott Long break; 287f366931cSScott Long } 288f366931cSScott Long 289f366931cSScott Long xpt_done(ccb); 290f366931cSScott Long return; 291f366931cSScott Long } 292f366931cSScott Long 29359ddd3e9SDoug Ambrisko static void 29459ddd3e9SDoug Ambrisko mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid) 29559ddd3e9SDoug Ambrisko { 29659ddd3e9SDoug Ambrisko union ccb *ccb; 29759ddd3e9SDoug Ambrisko struct mfip_softc *camsc; 29859ddd3e9SDoug Ambrisko struct cam_sim *sim; 29959ddd3e9SDoug Ambrisko device_t mfip_dev; 30059ddd3e9SDoug Ambrisko 30159ddd3e9SDoug Ambrisko mtx_lock(&Giant); 30259ddd3e9SDoug Ambrisko mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1); 30359ddd3e9SDoug Ambrisko mtx_unlock(&Giant); 30459ddd3e9SDoug Ambrisko if (mfip_dev == NULL) { 30559ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n"); 30659ddd3e9SDoug Ambrisko return; 30759ddd3e9SDoug Ambrisko } 30859ddd3e9SDoug Ambrisko 30959ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_io_lock); 31059ddd3e9SDoug Ambrisko camsc = device_get_softc(mfip_dev); 31159ddd3e9SDoug Ambrisko if (camsc->state == MFIP_STATE_DETACH) { 31259ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 31359ddd3e9SDoug Ambrisko return; 31459ddd3e9SDoug Ambrisko } 31559ddd3e9SDoug Ambrisko camsc->state = MFIP_STATE_RESCAN; 31659ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 31759ddd3e9SDoug Ambrisko 31859ddd3e9SDoug Ambrisko ccb = xpt_alloc_ccb_nowait(); 31959ddd3e9SDoug Ambrisko if (ccb == NULL) { 32059ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 32159ddd3e9SDoug Ambrisko "Cannot allocate ccb for bus rescan.\n"); 32259ddd3e9SDoug Ambrisko return; 32359ddd3e9SDoug Ambrisko } 32459ddd3e9SDoug Ambrisko 32559ddd3e9SDoug Ambrisko sim = camsc->sim; 32659ddd3e9SDoug Ambrisko if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim), 32759ddd3e9SDoug Ambrisko tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 32859ddd3e9SDoug Ambrisko xpt_free_ccb(ccb); 32959ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 33059ddd3e9SDoug Ambrisko "Cannot create path for bus rescan.\n"); 33159ddd3e9SDoug Ambrisko return; 33259ddd3e9SDoug Ambrisko } 33359ddd3e9SDoug Ambrisko 33459ddd3e9SDoug Ambrisko xpt_rescan(ccb); 33559ddd3e9SDoug Ambrisko 33659ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_io_lock); 33759ddd3e9SDoug Ambrisko camsc->state = MFIP_STATE_NONE; 33859ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 33959ddd3e9SDoug Ambrisko } 34059ddd3e9SDoug Ambrisko 341f366931cSScott Long static struct mfi_command * 342f366931cSScott Long mfip_start(void *data) 343f366931cSScott Long { 344f366931cSScott Long union ccb *ccb = data; 345f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 346f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 347f366931cSScott Long struct mfip_softc *sc; 348f366931cSScott Long struct mfi_pass_frame *pt; 349f366931cSScott Long struct mfi_command *cm; 3500d9a4ef3SDoug Ambrisko uint32_t context = 0; 351f366931cSScott Long 352f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 353f366931cSScott Long 354f366931cSScott Long if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL) 355f366931cSScott Long return (NULL); 356f366931cSScott Long 3570d9a4ef3SDoug Ambrisko /* Zero out the MFI frame */ 3580d9a4ef3SDoug Ambrisko context = cm->cm_frame->header.context; 3590d9a4ef3SDoug Ambrisko bzero(cm->cm_frame, sizeof(union mfi_frame)); 3600d9a4ef3SDoug Ambrisko cm->cm_frame->header.context = context; 3610d9a4ef3SDoug Ambrisko 362f366931cSScott Long pt = &cm->cm_frame->pass; 363f366931cSScott Long pt->header.cmd = MFI_CMD_PD_SCSI_IO; 364f366931cSScott Long pt->header.cmd_status = 0; 365f366931cSScott Long pt->header.scsi_status = 0; 366f366931cSScott Long pt->header.target_id = ccbh->target_id; 367f366931cSScott Long pt->header.lun_id = ccbh->target_lun; 368f366931cSScott Long pt->header.flags = 0; 369f366931cSScott Long pt->header.timeout = 0; 370f366931cSScott Long pt->header.data_len = csio->dxfer_len; 371f366931cSScott Long pt->header.sense_len = MFI_SENSE_LEN; 372f366931cSScott Long pt->header.cdb_len = csio->cdb_len; 3737d96f12cSSean Bruno pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr; 3747d96f12cSSean Bruno pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32); 375f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 376f366931cSScott Long bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len); 377f366931cSScott Long else 378f366931cSScott Long bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len); 379f366931cSScott Long cm->cm_complete = mfip_done; 380f366931cSScott Long cm->cm_private = ccb; 381f366931cSScott Long cm->cm_sg = &pt->sgl; 382f366931cSScott Long cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE; 383f366931cSScott Long cm->cm_data = csio->data_ptr; 384f366931cSScott Long cm->cm_len = csio->dxfer_len; 385f366931cSScott Long switch (ccbh->flags & CAM_DIR_MASK) { 386f366931cSScott Long case CAM_DIR_IN: 387f366931cSScott Long cm->cm_flags = MFI_CMD_DATAIN; 388f366931cSScott Long break; 389f366931cSScott Long case CAM_DIR_OUT: 390f366931cSScott Long cm->cm_flags = MFI_CMD_DATAOUT; 391f366931cSScott Long break; 392f366931cSScott Long case CAM_DIR_NONE: 393f366931cSScott Long default: 394f366931cSScott Long cm->cm_data = NULL; 395f366931cSScott Long cm->cm_len = 0; 396f366931cSScott Long cm->cm_flags = 0; 397f366931cSScott Long break; 398f366931cSScott Long } 399f366931cSScott Long 400f366931cSScott Long TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe); 401f366931cSScott Long return (cm); 402f366931cSScott Long } 403f366931cSScott Long 404f366931cSScott Long static void 405f366931cSScott Long mfip_done(struct mfi_command *cm) 406f366931cSScott Long { 407f366931cSScott Long union ccb *ccb = cm->cm_private; 408f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 409f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 410f366931cSScott Long struct mfip_softc *sc; 411f366931cSScott Long struct mfi_pass_frame *pt; 412f366931cSScott Long 413f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 414f366931cSScott Long pt = &cm->cm_frame->pass; 415f366931cSScott Long 416f366931cSScott Long switch (pt->header.cmd_status) { 417f366931cSScott Long case MFI_STAT_OK: 418f366931cSScott Long { 419f366931cSScott Long uint8_t command, device; 420f366931cSScott Long 421f366931cSScott Long ccbh->status = CAM_REQ_CMP; 422f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 423f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 424b596082bSJohn Baldwin command = csio->cdb_io.cdb_ptr[0]; 425f366931cSScott Long else 426b596082bSJohn Baldwin command = csio->cdb_io.cdb_bytes[0]; 427f366931cSScott Long if (command == INQUIRY) { 428b596082bSJohn Baldwin device = csio->data_ptr[0] & 0x1f; 42958ef3154SDoug Ambrisko if ((!mfi_allow_disks && device == T_DIRECT) || 43058ef3154SDoug Ambrisko (device == T_PROCESSOR)) 431f366931cSScott Long csio->data_ptr[0] = 432b596082bSJohn Baldwin (csio->data_ptr[0] & 0xe0) | T_NODEVICE; 433f366931cSScott Long } 434f366931cSScott Long break; 435f366931cSScott Long } 436f366931cSScott Long case MFI_STAT_SCSI_DONE_WITH_ERROR: 437f366931cSScott Long { 438f366931cSScott Long int sense_len; 439f366931cSScott Long 440b05e6558SScott Long ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 441f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 4421e5addb7SMarius Strobl if (pt->header.sense_len < csio->sense_len) 4431e5addb7SMarius Strobl csio->sense_resid = csio->sense_len - 4441e5addb7SMarius Strobl pt->header.sense_len; 4451e5addb7SMarius Strobl else 4461e5addb7SMarius Strobl csio->sense_resid = 0; 4471e5addb7SMarius Strobl sense_len = min(pt->header.sense_len, 4481e5addb7SMarius Strobl sizeof(struct scsi_sense_data)); 449f366931cSScott Long bzero(&csio->sense_data, sizeof(struct scsi_sense_data)); 450f366931cSScott Long bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len); 451f366931cSScott Long break; 452f366931cSScott Long } 453f366931cSScott Long case MFI_STAT_DEVICE_NOT_FOUND: 454b05e6558SScott Long ccbh->status = CAM_SEL_TIMEOUT; 455f366931cSScott Long break; 456f366931cSScott Long case MFI_STAT_SCSI_IO_FAILED: 457f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 458f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 459f366931cSScott Long break; 460f366931cSScott Long default: 461f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 462f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 463f366931cSScott Long break; 464f366931cSScott Long } 465f366931cSScott Long 466f366931cSScott Long mfi_release_command(cm); 467f366931cSScott Long xpt_done(ccb); 468f366931cSScott Long } 469f366931cSScott Long 470f366931cSScott Long static void 471f366931cSScott Long mfip_cam_poll(struct cam_sim *sim) 472f366931cSScott Long { 47358ef3154SDoug Ambrisko struct mfip_softc *sc = cam_sim_softc(sim); 47458ef3154SDoug Ambrisko struct mfi_softc *mfisc = sc->mfi_sc; 47558ef3154SDoug Ambrisko 47658ef3154SDoug Ambrisko mfisc->mfi_intr_ptr(mfisc); 477f366931cSScott Long } 478f366931cSScott Long 479