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