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> 48*d1c5fc76SJohn 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> 53f366931cSScott Long #include <cam/cam_sim.h> 54f366931cSScott Long #include <cam/cam_xpt_sim.h> 55f366931cSScott Long #include <cam/scsi/scsi_all.h> 56f366931cSScott Long #include <cam/scsi/scsi_message.h> 57f366931cSScott Long 58f366931cSScott Long #include <machine/md_var.h> 59f366931cSScott Long #include <machine/bus.h> 60f366931cSScott Long #include <machine/resource.h> 61f366931cSScott Long 62f366931cSScott Long #include <dev/mfi/mfireg.h> 63f366931cSScott Long #include <dev/mfi/mfi_ioctl.h> 64f366931cSScott Long #include <dev/mfi/mfivar.h> 65f366931cSScott Long 66f366931cSScott Long struct mfip_softc { 67f366931cSScott Long device_t dev; 68f366931cSScott Long struct mfi_softc *mfi_sc; 69f366931cSScott Long struct cam_devq *devq; 70f366931cSScott Long struct cam_sim *sim; 71f366931cSScott Long struct cam_path *path; 72f366931cSScott Long }; 73f366931cSScott Long 74f366931cSScott Long static int mfip_probe(device_t); 75f366931cSScott Long static int mfip_attach(device_t); 76f366931cSScott Long static int mfip_detach(device_t); 77f366931cSScott Long static void mfip_cam_action(struct cam_sim *, union ccb *); 78f366931cSScott Long static void mfip_cam_poll(struct cam_sim *); 79f366931cSScott Long static struct mfi_command * mfip_start(void *); 80f366931cSScott Long static void mfip_done(struct mfi_command *cm); 81f366931cSScott Long 82f366931cSScott Long static devclass_t mfip_devclass; 83f366931cSScott Long static device_method_t mfip_methods[] = { 84f366931cSScott Long DEVMETHOD(device_probe, mfip_probe), 85f366931cSScott Long DEVMETHOD(device_attach, mfip_attach), 86f366931cSScott Long DEVMETHOD(device_detach, mfip_detach), 87f366931cSScott Long {0, 0} 88f366931cSScott Long }; 89f366931cSScott Long static driver_t mfip_driver = { 90f366931cSScott Long "mfip", 91f366931cSScott Long mfip_methods, 92f366931cSScott Long sizeof(struct mfip_softc) 93f366931cSScott Long }; 94f366931cSScott Long DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0); 95f366931cSScott Long MODULE_DEPEND(mfip, cam, 1, 1, 1); 967765cff7SKonstantin Belousov MODULE_DEPEND(mfip, mfi, 1, 1, 1); 97f366931cSScott Long 98f366931cSScott Long #define ccb_mfip_ptr sim_priv.entries[0].ptr 99f366931cSScott Long 100f366931cSScott Long static int 101f366931cSScott Long mfip_probe(device_t dev) 102f366931cSScott Long { 103f366931cSScott Long 104f366931cSScott Long device_set_desc(dev, "SCSI Passthrough Bus"); 105f366931cSScott Long return (0); 106f366931cSScott Long } 107f366931cSScott Long 108f366931cSScott Long static int 109f366931cSScott Long mfip_attach(device_t dev) 110f366931cSScott Long { 111f366931cSScott Long struct mfip_softc *sc; 112f366931cSScott Long struct mfi_softc *mfisc; 113f366931cSScott Long 114f366931cSScott Long sc = device_get_softc(dev); 115f366931cSScott Long if (sc == NULL) 116f366931cSScott Long return (EINVAL); 117f366931cSScott Long 118f366931cSScott Long mfisc = device_get_softc(device_get_parent(dev)); 119f366931cSScott Long sc->dev = dev; 120f366931cSScott Long sc->mfi_sc = mfisc; 121f366931cSScott Long mfisc->mfi_cam_start = mfip_start; 122f366931cSScott Long 123f366931cSScott Long if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL) 124f366931cSScott Long return (ENOMEM); 125f366931cSScott Long 126f366931cSScott Long sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc, 127f366931cSScott Long device_get_unit(dev), &mfisc->mfi_io_lock, 1, 128f366931cSScott Long MFI_SCSI_MAX_CMDS, sc->devq); 129f366931cSScott Long if (sc->sim == NULL) { 130f366931cSScott Long cam_simq_free(sc->devq); 131f366931cSScott Long device_printf(dev, "CAM SIM attach failed\n"); 132f366931cSScott Long return (EINVAL); 133f366931cSScott Long } 134f366931cSScott Long 135f366931cSScott Long mtx_lock(&mfisc->mfi_io_lock); 136b50569b7SScott Long if (xpt_bus_register(sc->sim, dev, 0) != 0) { 137f366931cSScott Long device_printf(dev, "XPT bus registration failed\n"); 138f366931cSScott Long cam_sim_free(sc->sim, FALSE); 139f366931cSScott Long cam_simq_free(sc->devq); 140f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 141f366931cSScott Long return (EINVAL); 142f366931cSScott Long } 143f366931cSScott Long mtx_unlock(&mfisc->mfi_io_lock); 144f366931cSScott Long 145f366931cSScott Long return (0); 146f366931cSScott Long } 147f366931cSScott Long 148f366931cSScott Long static int 149f366931cSScott Long mfip_detach(device_t dev) 150f366931cSScott Long { 151f366931cSScott Long struct mfip_softc *sc; 152f366931cSScott Long 153f366931cSScott Long sc = device_get_softc(dev); 154f366931cSScott Long if (sc == NULL) 155f366931cSScott Long return (EINVAL); 156f366931cSScott Long 157f366931cSScott Long if (sc->sim != NULL) { 158f366931cSScott Long mtx_lock(&sc->mfi_sc->mfi_io_lock); 159f366931cSScott Long xpt_bus_deregister(cam_sim_path(sc->sim)); 160f366931cSScott Long cam_sim_free(sc->sim, FALSE); 161f366931cSScott Long mtx_unlock(&sc->mfi_sc->mfi_io_lock); 162f366931cSScott Long } 163f366931cSScott Long 164f366931cSScott Long if (sc->devq != NULL) 165f366931cSScott Long cam_simq_free(sc->devq); 166f366931cSScott Long 167f366931cSScott Long return (0); 168f366931cSScott Long } 169f366931cSScott Long 170f366931cSScott Long static void 171f366931cSScott Long mfip_cam_action(struct cam_sim *sim, union ccb *ccb) 172f366931cSScott Long { 173f366931cSScott Long struct mfip_softc *sc = cam_sim_softc(sim); 174f366931cSScott Long struct mfi_softc *mfisc = sc->mfi_sc; 175f366931cSScott Long 176f366931cSScott Long mtx_assert(&mfisc->mfi_io_lock, MA_OWNED); 177f366931cSScott Long 178f366931cSScott Long switch (ccb->ccb_h.func_code) { 179f366931cSScott Long case XPT_PATH_INQ: 180f366931cSScott Long { 181f366931cSScott Long struct ccb_pathinq *cpi = &ccb->cpi; 182f366931cSScott Long 183f366931cSScott Long cpi->version_num = 1; 184f366931cSScott Long cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 185f366931cSScott Long cpi->target_sprt = 0; 186b05e6558SScott Long cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN; 187f366931cSScott Long cpi->hba_eng_cnt = 0; 188f366931cSScott Long cpi->max_target = MFI_SCSI_MAX_TARGETS; 189f366931cSScott Long cpi->max_lun = MFI_SCSI_MAX_LUNS; 190f366931cSScott Long cpi->initiator_id = MFI_SCSI_INITIATOR_ID; 191f366931cSScott Long strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 192f366931cSScott Long strncpy(cpi->hba_vid, "LSI", HBA_IDLEN); 193f366931cSScott Long strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 194f366931cSScott Long cpi->unit_number = cam_sim_unit(sim); 195f366931cSScott Long cpi->bus_id = cam_sim_bus(sim); 196f366931cSScott Long cpi->base_transfer_speed = 150000; 197b05e6558SScott Long cpi->transport = XPORT_SAS; 198b05e6558SScott Long cpi->transport_version = 0; 199f366931cSScott Long cpi->protocol = PROTO_SCSI; 200f366931cSScott Long cpi->protocol_version = SCSI_REV_2; 201f366931cSScott Long cpi->ccb_h.status = CAM_REQ_CMP; 202f366931cSScott Long break; 203f366931cSScott Long } 204f366931cSScott Long case XPT_RESET_BUS: 205f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 206f366931cSScott Long break; 207f366931cSScott Long case XPT_RESET_DEV: 208f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 209f366931cSScott Long break; 210f366931cSScott Long case XPT_GET_TRAN_SETTINGS: 211f366931cSScott Long { 212b05e6558SScott Long struct ccb_trans_settings_sas *sas = 213b05e6558SScott Long &ccb->cts.xport_specific.sas; 214f366931cSScott Long 215f366931cSScott Long ccb->cts.protocol = PROTO_SCSI; 216b05e6558SScott Long ccb->cts.protocol_version = SCSI_REV_2; 217b05e6558SScott Long ccb->cts.transport = XPORT_SAS; 218b05e6558SScott Long ccb->cts.transport_version = 0; 219b05e6558SScott Long 220b05e6558SScott Long sas->valid &= ~CTS_SAS_VALID_SPEED; 221b05e6558SScott Long sas->bitrate = 150000; 222b05e6558SScott Long 223f366931cSScott Long ccb->ccb_h.status = CAM_REQ_CMP; 224f366931cSScott Long break; 225f366931cSScott Long } 226f366931cSScott Long case XPT_SET_TRAN_SETTINGS: 227f366931cSScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 228f366931cSScott Long break; 229f366931cSScott Long case XPT_SCSI_IO: 230f366931cSScott Long { 231f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 232f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 233f366931cSScott Long 234f366931cSScott Long ccbh->status = CAM_REQ_INPROG; 235f366931cSScott Long if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) { 236f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 237f366931cSScott Long break; 238f366931cSScott Long } 239f366931cSScott Long if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 240f366931cSScott Long if (ccbh->flags & CAM_DATA_PHYS) { 241f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 242f366931cSScott Long break; 243f366931cSScott Long } 244f366931cSScott Long if (ccbh->flags & CAM_SCATTER_VALID) { 245f366931cSScott Long ccbh->status = CAM_REQ_INVALID; 246f366931cSScott Long break; 247f366931cSScott Long } 248f366931cSScott Long } 249f366931cSScott Long 250f366931cSScott Long ccbh->ccb_mfip_ptr = sc; 251f366931cSScott Long TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe); 252f366931cSScott Long mfi_startio(mfisc); 253f366931cSScott Long return; 254f366931cSScott Long } 255f366931cSScott Long default: 256f366931cSScott Long ccb->ccb_h.status = CAM_REQ_INVALID; 257f366931cSScott Long break; 258f366931cSScott Long } 259f366931cSScott Long 260f366931cSScott Long xpt_done(ccb); 261f366931cSScott Long return; 262f366931cSScott Long } 263f366931cSScott Long 264f366931cSScott Long static struct mfi_command * 265f366931cSScott Long mfip_start(void *data) 266f366931cSScott Long { 267f366931cSScott Long union ccb *ccb = data; 268f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 269f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 270f366931cSScott Long struct mfip_softc *sc; 271f366931cSScott Long struct mfi_pass_frame *pt; 272f366931cSScott Long struct mfi_command *cm; 273f366931cSScott Long 274f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 275f366931cSScott Long 276f366931cSScott Long if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL) 277f366931cSScott Long return (NULL); 278f366931cSScott Long 279f366931cSScott Long pt = &cm->cm_frame->pass; 280f366931cSScott Long pt->header.cmd = MFI_CMD_PD_SCSI_IO; 281f366931cSScott Long pt->header.cmd_status = 0; 282f366931cSScott Long pt->header.scsi_status = 0; 283f366931cSScott Long pt->header.target_id = ccbh->target_id; 284f366931cSScott Long pt->header.lun_id = ccbh->target_lun; 285f366931cSScott Long pt->header.flags = 0; 286f366931cSScott Long pt->header.timeout = 0; 287f366931cSScott Long pt->header.data_len = csio->dxfer_len; 288f366931cSScott Long pt->header.sense_len = MFI_SENSE_LEN; 289f366931cSScott Long pt->header.cdb_len = csio->cdb_len; 290f366931cSScott Long pt->sense_addr_lo = cm->cm_sense_busaddr; 291f366931cSScott Long pt->sense_addr_hi = 0; 292f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 293f366931cSScott Long bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len); 294f366931cSScott Long else 295f366931cSScott Long bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len); 296f366931cSScott Long cm->cm_complete = mfip_done; 297f366931cSScott Long cm->cm_private = ccb; 298f366931cSScott Long cm->cm_sg = &pt->sgl; 299f366931cSScott Long cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE; 300f366931cSScott Long cm->cm_data = csio->data_ptr; 301f366931cSScott Long cm->cm_len = csio->dxfer_len; 302f366931cSScott Long switch (ccbh->flags & CAM_DIR_MASK) { 303f366931cSScott Long case CAM_DIR_IN: 304f366931cSScott Long cm->cm_flags = MFI_CMD_DATAIN; 305f366931cSScott Long break; 306f366931cSScott Long case CAM_DIR_OUT: 307f366931cSScott Long cm->cm_flags = MFI_CMD_DATAOUT; 308f366931cSScott Long break; 309f366931cSScott Long case CAM_DIR_NONE: 310f366931cSScott Long default: 311f366931cSScott Long cm->cm_data = NULL; 312f366931cSScott Long cm->cm_len = 0; 313f366931cSScott Long cm->cm_flags = 0; 314f366931cSScott Long break; 315f366931cSScott Long } 316f366931cSScott Long 317f366931cSScott Long TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe); 318f366931cSScott Long return (cm); 319f366931cSScott Long } 320f366931cSScott Long 321f366931cSScott Long static void 322f366931cSScott Long mfip_done(struct mfi_command *cm) 323f366931cSScott Long { 324f366931cSScott Long union ccb *ccb = cm->cm_private; 325f366931cSScott Long struct ccb_hdr *ccbh = &ccb->ccb_h; 326f366931cSScott Long struct ccb_scsiio *csio = &ccb->csio; 327f366931cSScott Long struct mfip_softc *sc; 328f366931cSScott Long struct mfi_pass_frame *pt; 329f366931cSScott Long 330f366931cSScott Long sc = ccbh->ccb_mfip_ptr; 331f366931cSScott Long pt = &cm->cm_frame->pass; 332f366931cSScott Long 333f366931cSScott Long switch (pt->header.cmd_status) { 334f366931cSScott Long case MFI_STAT_OK: 335f366931cSScott Long { 336f366931cSScott Long uint8_t command, device; 337f366931cSScott Long 338f366931cSScott Long ccbh->status = CAM_REQ_CMP; 339f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 340f366931cSScott Long if (ccbh->flags & CAM_CDB_POINTER) 341b596082bSJohn Baldwin command = csio->cdb_io.cdb_ptr[0]; 342f366931cSScott Long else 343b596082bSJohn Baldwin command = csio->cdb_io.cdb_bytes[0]; 344f366931cSScott Long if (command == INQUIRY) { 345b596082bSJohn Baldwin device = csio->data_ptr[0] & 0x1f; 346f366931cSScott Long if ((device == T_DIRECT) || (device == T_PROCESSOR)) 347f366931cSScott Long csio->data_ptr[0] = 348b596082bSJohn Baldwin (csio->data_ptr[0] & 0xe0) | T_NODEVICE; 349f366931cSScott Long } 350f366931cSScott Long break; 351f366931cSScott Long } 352f366931cSScott Long case MFI_STAT_SCSI_DONE_WITH_ERROR: 353f366931cSScott Long { 354f366931cSScott Long int sense_len; 355f366931cSScott Long 356b05e6558SScott Long ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 357f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 3581e5addb7SMarius Strobl if (pt->header.sense_len < csio->sense_len) 3591e5addb7SMarius Strobl csio->sense_resid = csio->sense_len - 3601e5addb7SMarius Strobl pt->header.sense_len; 3611e5addb7SMarius Strobl else 3621e5addb7SMarius Strobl csio->sense_resid = 0; 3631e5addb7SMarius Strobl sense_len = min(pt->header.sense_len, 3641e5addb7SMarius Strobl sizeof(struct scsi_sense_data)); 365f366931cSScott Long bzero(&csio->sense_data, sizeof(struct scsi_sense_data)); 366f366931cSScott Long bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len); 367f366931cSScott Long break; 368f366931cSScott Long } 369f366931cSScott Long case MFI_STAT_DEVICE_NOT_FOUND: 370b05e6558SScott Long ccbh->status = CAM_SEL_TIMEOUT; 371f366931cSScott Long break; 372f366931cSScott Long case MFI_STAT_SCSI_IO_FAILED: 373f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 374f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 375f366931cSScott Long break; 376f366931cSScott Long default: 377f366931cSScott Long ccbh->status = CAM_REQ_CMP_ERR; 378f366931cSScott Long csio->scsi_status = pt->header.scsi_status; 379f366931cSScott Long break; 380f366931cSScott Long } 381f366931cSScott Long 382f366931cSScott Long mfi_release_command(cm); 383f366931cSScott Long xpt_done(ccb); 384f366931cSScott Long } 385f366931cSScott Long 386f366931cSScott Long static void 387f366931cSScott Long mfip_cam_poll(struct cam_sim *sim) 388f366931cSScott Long { 389f366931cSScott Long return; 390f366931cSScott Long } 391f366931cSScott Long 392