1f366931cSScott Long /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4f366931cSScott Long * Copyright 2007 Scott Long 5f366931cSScott Long * All rights reserved. 6f366931cSScott Long * 7f366931cSScott Long * Redistribution and use in source and binary forms, with or without 8f366931cSScott Long * modification, are permitted provided that the following conditions 9f366931cSScott Long * are met: 10f366931cSScott Long * 1. Redistributions of source code must retain the above copyright 11f366931cSScott Long * notice, this list of conditions and the following disclaimer. 12f366931cSScott Long * 2. Redistributions in binary form must reproduce the above copyright 13f366931cSScott Long * notice, this list of conditions and the following disclaimer in the 14f366931cSScott Long * documentation and/or other materials provided with the distribution. 15f366931cSScott Long * 16f366931cSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17f366931cSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18f366931cSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19f366931cSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20f366931cSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21f366931cSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22f366931cSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23f366931cSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24f366931cSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25f366931cSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26f366931cSScott Long * SUCH DAMAGE. 27f366931cSScott Long */ 28f366931cSScott Long 29f366931cSScott Long #include <sys/cdefs.h> 30f366931cSScott Long __FBSDID("$FreeBSD$"); 31f366931cSScott Long 32f366931cSScott Long #include "opt_mfi.h" 33f366931cSScott Long 34f366931cSScott Long #include <sys/param.h> 35f366931cSScott Long #include <sys/systm.h> 36f366931cSScott Long #include <sys/kernel.h> 37f366931cSScott Long #include <sys/malloc.h> 38f366931cSScott Long #include <sys/module.h> 39f366931cSScott Long #include <sys/selinfo.h> 40f366931cSScott Long #include <sys/bus.h> 41f366931cSScott Long #include <sys/conf.h> 42f366931cSScott Long #include <sys/eventhandler.h> 43f366931cSScott Long #include <sys/rman.h> 44f366931cSScott Long #include <sys/bio.h> 45f366931cSScott Long #include <sys/ioccom.h> 46f366931cSScott Long #include <sys/uio.h> 47f366931cSScott Long #include <sys/proc.h> 48f366931cSScott Long #include <sys/signalvar.h> 49d1c5fc76SJohn Baldwin #include <sys/sysctl.h> 50f366931cSScott Long 51f366931cSScott Long #include <cam/cam.h> 52f366931cSScott Long #include <cam/cam_ccb.h> 53f366931cSScott Long #include <cam/cam_debug.h> 5459ddd3e9SDoug Ambrisko #include <cam/cam_periph.h> 55f366931cSScott Long #include <cam/cam_sim.h> 5659ddd3e9SDoug Ambrisko #include <cam/cam_xpt_periph.h> 57f366931cSScott Long #include <cam/cam_xpt_sim.h> 58f366931cSScott Long #include <cam/scsi/scsi_all.h> 59f366931cSScott Long #include <cam/scsi/scsi_message.h> 60f366931cSScott Long 61f366931cSScott Long #include <machine/md_var.h> 62f366931cSScott Long #include <machine/bus.h> 63f366931cSScott Long #include <machine/resource.h> 64f366931cSScott Long 65f366931cSScott Long #include <dev/mfi/mfireg.h> 66f366931cSScott Long #include <dev/mfi/mfi_ioctl.h> 67f366931cSScott Long #include <dev/mfi/mfivar.h> 68f366931cSScott Long 6959ddd3e9SDoug Ambrisko enum mfip_state { 7059ddd3e9SDoug Ambrisko MFIP_STATE_NONE, 7159ddd3e9SDoug Ambrisko MFIP_STATE_DETACH, 7259ddd3e9SDoug Ambrisko MFIP_STATE_RESCAN 7359ddd3e9SDoug Ambrisko }; 7459ddd3e9SDoug Ambrisko 75f366931cSScott Long struct mfip_softc { 76f366931cSScott Long device_t dev; 77f366931cSScott Long struct mfi_softc *mfi_sc; 78f366931cSScott Long struct cam_devq *devq; 79f366931cSScott Long struct cam_sim *sim; 80f366931cSScott Long struct cam_path *path; 8159ddd3e9SDoug Ambrisko enum mfip_state state; 82f366931cSScott Long }; 83f366931cSScott Long 84f366931cSScott Long static int mfip_probe(device_t); 85f366931cSScott Long static int mfip_attach(device_t); 86f366931cSScott Long static int mfip_detach(device_t); 87f366931cSScott Long static void mfip_cam_action(struct cam_sim *, union ccb *); 88f366931cSScott Long static void mfip_cam_poll(struct cam_sim *); 8959ddd3e9SDoug Ambrisko static void mfip_cam_rescan(struct mfi_softc *, uint32_t tid); 90f366931cSScott Long static struct mfi_command * mfip_start(void *); 91f366931cSScott Long static void mfip_done(struct mfi_command *cm); 92f366931cSScott Long 9358ef3154SDoug Ambrisko static int mfi_allow_disks = 0; 94af3b2549SHans Petter Selasky SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RDTUN, 9558ef3154SDoug Ambrisko &mfi_allow_disks, 0, "event message locale"); 9658ef3154SDoug Ambrisko 97f366931cSScott Long static device_method_t mfip_methods[] = { 98f366931cSScott Long DEVMETHOD(device_probe, mfip_probe), 99f366931cSScott Long DEVMETHOD(device_attach, mfip_attach), 100f366931cSScott Long DEVMETHOD(device_detach, mfip_detach), 10161bfd867SSofian Brabez 10261bfd867SSofian Brabez DEVMETHOD_END 103f366931cSScott Long }; 104354f6c1cSJohn Baldwin 105f366931cSScott Long static driver_t mfip_driver = { 106f366931cSScott Long "mfip", 107f366931cSScott Long mfip_methods, 108f366931cSScott Long sizeof(struct mfip_softc) 109f366931cSScott Long }; 110354f6c1cSJohn Baldwin 111354f6c1cSJohn Baldwin DRIVER_MODULE(mfip, mfi, mfip_driver, 0, 0); 112f366931cSScott Long MODULE_DEPEND(mfip, cam, 1, 1, 1); 1137765cff7SKonstantin Belousov MODULE_DEPEND(mfip, mfi, 1, 1, 1); 114f366931cSScott Long 115f366931cSScott Long #define ccb_mfip_ptr sim_priv.entries[0].ptr 116f366931cSScott Long 117f366931cSScott Long static int 118f366931cSScott Long mfip_probe(device_t dev) 119f366931cSScott Long { 120f366931cSScott Long 121f366931cSScott Long device_set_desc(dev, "SCSI Passthrough Bus"); 122f366931cSScott Long return (0); 123f366931cSScott Long } 124f366931cSScott Long 125f366931cSScott Long static int 126f366931cSScott Long mfip_attach(device_t dev) 127f366931cSScott Long { 128f366931cSScott Long struct mfip_softc *sc; 129f366931cSScott Long struct mfi_softc *mfisc; 130f366931cSScott Long 131f366931cSScott Long sc = device_get_softc(dev); 132f366931cSScott Long if (sc == NULL) 133f366931cSScott Long return (EINVAL); 134f366931cSScott Long 135f366931cSScott Long mfisc = device_get_softc(device_get_parent(dev)); 136f366931cSScott Long sc->dev = dev; 13759ddd3e9SDoug Ambrisko sc->state = MFIP_STATE_NONE; 138f366931cSScott Long sc->mfi_sc = mfisc; 139f366931cSScott Long mfisc->mfi_cam_start = mfip_start; 140f366931cSScott Long 141f366931cSScott Long if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL) 142f366931cSScott Long return (ENOMEM); 143f366931cSScott Long 144f366931cSScott Long sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc, 145f366931cSScott Long device_get_unit(dev), &mfisc->mfi_io_lock, 1, 146f366931cSScott Long MFI_SCSI_MAX_CMDS, sc->devq); 147f366931cSScott Long if (sc->sim == NULL) { 148f366931cSScott Long cam_simq_free(sc->devq); 14908c89430SSteven Hartland sc->devq = NULL; 150f366931cSScott Long device_printf(dev, "CAM SIM attach failed\n"); 151f366931cSScott Long return (EINVAL); 152f366931cSScott Long } 153f366931cSScott Long 15459ddd3e9SDoug Ambrisko mfisc->mfi_cam_rescan_cb = mfip_cam_rescan; 15559ddd3e9SDoug Ambrisko 156f366931cSScott Long mtx_lock(&mfisc->mfi_io_lock); 157b50569b7SScott Long if (xpt_bus_register(sc->sim, dev, 0) != 0) { 158f366931cSScott Long device_printf(dev, "XPT bus registration failed\n"); 159f366931cSScott Long cam_sim_free(sc->sim, FALSE); 16008c89430SSteven Hartland sc->sim = NULL; 161f366931cSScott Long cam_simq_free(sc->devq); 16208c89430SSteven Hartland sc->devq = NULL; 163f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 164f366931cSScott Long return (EINVAL); 165f366931cSScott Long } 166f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 167f366931cSScott Long 168f366931cSScott Long return (0); 169f366931cSScott Long } 170f366931cSScott Long 171f366931cSScott Long static int 172f366931cSScott Long mfip_detach(device_t dev) 173f366931cSScott Long { 174f366931cSScott Long struct mfip_softc *sc; 175f366931cSScott Long 176f366931cSScott Long sc = device_get_softc(dev); 177f366931cSScott Long if (sc == NULL) 178f366931cSScott Long return (EINVAL); 179f366931cSScott Long 18059ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_sc->mfi_io_lock); 18159ddd3e9SDoug Ambrisko if (sc->state == MFIP_STATE_RESCAN) { 18259ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 18359ddd3e9SDoug Ambrisko return (EBUSY); 18459ddd3e9SDoug Ambrisko } 18559ddd3e9SDoug Ambrisko sc->state = MFIP_STATE_DETACH; 18659ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_sc->mfi_io_lock); 18759ddd3e9SDoug Ambrisko 18859ddd3e9SDoug Ambrisko sc->mfi_sc->mfi_cam_rescan_cb = NULL; 18959ddd3e9SDoug Ambrisko 190f366931cSScott Long if (sc->sim != NULL) { 191f366931cSScott Long mtx_lock(&sc->mfi_sc->mfi_io_lock); 192f366931cSScott Long xpt_bus_deregister(cam_sim_path(sc->sim)); 193f366931cSScott Long cam_sim_free(sc->sim, FALSE); 19408c89430SSteven Hartland sc->sim = NULL; 195f366931cSScott Long mtx_unlock(&sc->mfi_sc->mfi_io_lock); 196f366931cSScott Long } 197f366931cSScott Long 19808c89430SSteven Hartland if (sc->devq != NULL) { 199f366931cSScott Long cam_simq_free(sc->devq); 20008c89430SSteven Hartland sc->devq = NULL; 20108c89430SSteven Hartland } 202f366931cSScott Long 203f366931cSScott Long return (0); 204f366931cSScott Long } 205f366931cSScott Long 206f366931cSScott Long static void 207f366931cSScott Long mfip_cam_action(struct cam_sim *sim, union ccb *ccb) 208f366931cSScott Long { 209f366931cSScott Long struct mfip_softc *sc = cam_sim_softc(sim); 210f366931cSScott Long struct mfi_softc *mfisc = sc->mfi_sc; 211f366931cSScott Long 212f366931cSScott Long mtx_assert(&mfisc->mfi_io_lock, MA_OWNED); 213f366931cSScott Long 214f366931cSScott Long switch (ccb->ccb_h.func_code) { 215f366931cSScott Long case XPT_PATH_INQ: 216f366931cSScott Long { 217f366931cSScott Long struct ccb_pathinq *cpi = &ccb->cpi; 218f366931cSScott Long 219f366931cSScott Long cpi->version_num = 1; 22023c628f7SAlexander Motin cpi->hba_inquiry = PI_TAG_ABLE; 221f366931cSScott Long cpi->target_sprt = 0; 222c33ce503SKonstantin Belousov cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN | PIM_UNMAPPED; 223f366931cSScott Long cpi->hba_eng_cnt = 0; 224f366931cSScott Long cpi->max_target = MFI_SCSI_MAX_TARGETS; 225f366931cSScott Long cpi->max_lun = MFI_SCSI_MAX_LUNS; 226f366931cSScott Long cpi->initiator_id = MFI_SCSI_INITIATOR_ID; 2274195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2284195c7deSAlan Somers strlcpy(cpi->hba_vid, "LSI", HBA_IDLEN); 2294195c7deSAlan Somers strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 230f366931cSScott Long cpi->unit_number = cam_sim_unit(sim); 231f366931cSScott Long cpi->bus_id = cam_sim_bus(sim); 232f366931cSScott Long cpi->base_transfer_speed = 150000; 233b05e6558SScott Long cpi->transport = XPORT_SAS; 234b05e6558SScott Long cpi->transport_version = 0; 235f366931cSScott Long cpi->protocol = PROTO_SCSI; 236f366931cSScott Long cpi->protocol_version = SCSI_REV_2; 237f366931cSScott Long cpi->ccb_h.status = CAM_REQ_CMP; 238f366931cSScott Long break; 239f366931cSScott Long } 240f366931cSScott Long case XPT_RESET_BUS: 241f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 242f366931cSScott Long break; 243f366931cSScott Long case XPT_RESET_DEV: 244f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 245f366931cSScott Long break; 246f366931cSScott Long case XPT_GET_TRAN_SETTINGS: 247f366931cSScott Long { 24823c628f7SAlexander Motin struct ccb_trans_settings_scsi *scsi = 24923c628f7SAlexander Motin &ccb->cts.proto_specific.scsi; 250b05e6558SScott Long struct ccb_trans_settings_sas *sas = 251b05e6558SScott Long &ccb->cts.xport_specific.sas; 252f366931cSScott Long 253f366931cSScott Long ccb->cts.protocol = PROTO_SCSI; 254b05e6558SScott Long ccb->cts.protocol_version = SCSI_REV_2; 255b05e6558SScott Long ccb->cts.transport = XPORT_SAS; 256b05e6558SScott Long ccb->cts.transport_version = 0; 257b05e6558SScott Long 25823c628f7SAlexander Motin scsi->valid = CTS_SCSI_VALID_TQ; 25923c628f7SAlexander Motin scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 26023c628f7SAlexander Motin 261b05e6558SScott Long sas->valid &= ~CTS_SAS_VALID_SPEED; 262b05e6558SScott Long sas->bitrate = 150000; 263b05e6558SScott Long 264f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 265f366931cSScott Long break; 266f366931cSScott Long } 267f366931cSScott Long case XPT_SET_TRAN_SETTINGS: 268f366931cSScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 269f366931cSScott Long break; 270f366931cSScott Long case XPT_SCSI_IO: 271f366931cSScott Long { 272f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 273f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 274f366931cSScott Long 275f366931cSScott Long ccbh->status = CAM_REQ_INPROG; 276f366931cSScott Long if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) { 277f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 278f366931cSScott Long break; 279f366931cSScott Long } 280f366931cSScott Long ccbh->ccb_mfip_ptr = sc; 281f366931cSScott Long TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe); 282f366931cSScott Long mfi_startio(mfisc); 283f366931cSScott Long return; 284f366931cSScott Long } 285f366931cSScott Long default: 286f366931cSScott Long ccb->ccb_h.status = CAM_REQ_INVALID; 287f366931cSScott Long break; 288f366931cSScott Long } 289f366931cSScott Long 290f366931cSScott Long xpt_done(ccb); 291f366931cSScott Long return; 292f366931cSScott Long } 293f366931cSScott Long 29459ddd3e9SDoug Ambrisko static void 29559ddd3e9SDoug Ambrisko mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid) 29659ddd3e9SDoug Ambrisko { 29759ddd3e9SDoug Ambrisko union ccb *ccb; 29859ddd3e9SDoug Ambrisko struct mfip_softc *camsc; 29959ddd3e9SDoug Ambrisko struct cam_sim *sim; 30059ddd3e9SDoug Ambrisko device_t mfip_dev; 30159ddd3e9SDoug Ambrisko 302c6df6f53SWarner Losh bus_topo_lock(); 30359ddd3e9SDoug Ambrisko mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1); 304c6df6f53SWarner Losh bus_topo_unlock(); 30559ddd3e9SDoug Ambrisko if (mfip_dev == NULL) { 30659ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n"); 30759ddd3e9SDoug Ambrisko return; 30859ddd3e9SDoug Ambrisko } 30959ddd3e9SDoug Ambrisko 31059ddd3e9SDoug Ambrisko mtx_lock(&sc->mfi_io_lock); 31159ddd3e9SDoug Ambrisko camsc = device_get_softc(mfip_dev); 31259ddd3e9SDoug Ambrisko if (camsc->state == MFIP_STATE_DETACH) { 31359ddd3e9SDoug Ambrisko mtx_unlock(&sc->mfi_io_lock); 31459ddd3e9SDoug Ambrisko return; 31559ddd3e9SDoug Ambrisko } 31659ddd3e9SDoug Ambrisko camsc->state = MFIP_STATE_RESCAN; 31759ddd3e9SDoug Ambrisko 31859ddd3e9SDoug Ambrisko ccb = xpt_alloc_ccb_nowait(); 31959ddd3e9SDoug Ambrisko if (ccb == NULL) { 32030e71983SMark Johnston mtx_unlock(&sc->mfi_io_lock); 32159ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 32259ddd3e9SDoug Ambrisko "Cannot allocate ccb for bus rescan.\n"); 32359ddd3e9SDoug Ambrisko return; 32459ddd3e9SDoug Ambrisko } 32559ddd3e9SDoug Ambrisko 32659ddd3e9SDoug Ambrisko sim = camsc->sim; 327e5dfa058SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim), 32859ddd3e9SDoug Ambrisko tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 32959ddd3e9SDoug Ambrisko xpt_free_ccb(ccb); 330f7469975SSean Bruno mtx_unlock(&sc->mfi_io_lock); 33159ddd3e9SDoug Ambrisko device_printf(sc->mfi_dev, 33259ddd3e9SDoug Ambrisko "Cannot create path for bus rescan.\n"); 33359ddd3e9SDoug Ambrisko return; 33459ddd3e9SDoug Ambrisko } 33559ddd3e9SDoug Ambrisko xpt_rescan(ccb); 33659ddd3e9SDoug Ambrisko 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; 383dd0b4fb6SKonstantin Belousov cm->cm_data = ccb; 384f366931cSScott Long cm->cm_len = csio->dxfer_len; 385f366931cSScott Long switch (ccbh->flags & CAM_DIR_MASK) { 386f366931cSScott Long case CAM_DIR_IN: 387dd0b4fb6SKonstantin Belousov cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB; 388f366931cSScott Long break; 389f366931cSScott Long case CAM_DIR_OUT: 390dd0b4fb6SKonstantin Belousov cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB; 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 mfi_pass_frame *pt; 411f366931cSScott Long 412f366931cSScott Long pt = &cm->cm_frame->pass; 413f366931cSScott Long 414f366931cSScott Long switch (pt->header.cmd_status) { 415f366931cSScott Long case MFI_STAT_OK: 416f366931cSScott Long { 417f366931cSScott Long uint8_t command, device; 418f366931cSScott Long 419f366931cSScott Long ccbh->status = CAM_REQ_CMP; 420f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 421f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 422b596082bSJohn Baldwin command = csio->cdb_io.cdb_ptr[0]; 423f366931cSScott Long else 424b596082bSJohn Baldwin command = csio->cdb_io.cdb_bytes[0]; 425f366931cSScott Long if (command == INQUIRY) { 426b596082bSJohn Baldwin device = csio->data_ptr[0] & 0x1f; 42758ef3154SDoug Ambrisko if ((!mfi_allow_disks && device == T_DIRECT) || 42858ef3154SDoug Ambrisko (device == T_PROCESSOR)) 429f366931cSScott Long csio->data_ptr[0] = 430b596082bSJohn Baldwin (csio->data_ptr[0] & 0xe0) | T_NODEVICE; 431f366931cSScott Long } 432f366931cSScott Long break; 433f366931cSScott Long } 434f366931cSScott Long case MFI_STAT_SCSI_DONE_WITH_ERROR: 435f366931cSScott Long { 436f366931cSScott Long int sense_len; 437f366931cSScott Long 438b05e6558SScott Long ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 439f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 4401e5addb7SMarius Strobl if (pt->header.sense_len < csio->sense_len) 4411e5addb7SMarius Strobl csio->sense_resid = csio->sense_len - 4421e5addb7SMarius Strobl pt->header.sense_len; 4431e5addb7SMarius Strobl else 4441e5addb7SMarius Strobl csio->sense_resid = 0; 4451e5addb7SMarius Strobl sense_len = min(pt->header.sense_len, 4461e5addb7SMarius Strobl sizeof(struct scsi_sense_data)); 447f366931cSScott Long bzero(&csio->sense_data, sizeof(struct scsi_sense_data)); 448f366931cSScott Long bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len); 449f366931cSScott Long break; 450f366931cSScott Long } 451f366931cSScott Long case MFI_STAT_DEVICE_NOT_FOUND: 452b05e6558SScott Long ccbh->status = CAM_SEL_TIMEOUT; 453f366931cSScott Long break; 454f366931cSScott Long case MFI_STAT_SCSI_IO_FAILED: 455f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 456f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 457f366931cSScott Long break; 458f366931cSScott Long default: 459f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 460f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 461f366931cSScott Long break; 462f366931cSScott Long } 463f366931cSScott Long 464f366931cSScott Long mfi_release_command(cm); 465f366931cSScott Long xpt_done(ccb); 466f366931cSScott Long } 467f366931cSScott Long 468f366931cSScott Long static void 469f366931cSScott Long mfip_cam_poll(struct cam_sim *sim) 470f366931cSScott Long { 47158ef3154SDoug Ambrisko struct mfip_softc *sc = cam_sim_softc(sim); 47258ef3154SDoug Ambrisko struct mfi_softc *mfisc = sc->mfi_sc; 47358ef3154SDoug Ambrisko 47458ef3154SDoug Ambrisko mfisc->mfi_intr_ptr(mfisc); 475f366931cSScott Long } 476