The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/mfi/mfi_cam.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright 2007 Scott Long
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.4/sys/dev/mfi/mfi_cam.c 243826 2012-12-03 18:47:25Z delphij $");
   29 
   30 #include "opt_mfi.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/kernel.h>
   35 #include <sys/malloc.h>
   36 #include <sys/module.h>
   37 #include <sys/selinfo.h>
   38 #include <sys/bus.h>
   39 #include <sys/conf.h>
   40 #include <sys/eventhandler.h>
   41 #include <sys/rman.h>
   42 #include <sys/bus_dma.h>
   43 #include <sys/bio.h>
   44 #include <sys/ioccom.h>
   45 #include <sys/uio.h>
   46 #include <sys/proc.h>
   47 #include <sys/signalvar.h>
   48 #include <sys/sysctl.h>
   49 
   50 #include <cam/cam.h>
   51 #include <cam/cam_ccb.h>
   52 #include <cam/cam_debug.h>
   53 #include <cam/cam_sim.h>
   54 #include <cam/cam_xpt_sim.h>
   55 #include <cam/scsi/scsi_all.h>
   56 #include <cam/scsi/scsi_message.h>
   57 
   58 #include <sys/bus.h>
   59 #include <sys/conf.h>
   60 #include <machine/md_var.h>
   61 #include <machine/bus.h>
   62 #include <machine/resource.h>
   63 #include <sys/rman.h>
   64 
   65 #include <dev/mfi/mfireg.h>
   66 #include <dev/mfi/mfi_ioctl.h>
   67 #include <dev/mfi/mfivar.h>
   68 
   69 struct mfip_softc {
   70         device_t        dev;
   71         struct mfi_softc *mfi_sc;
   72         struct cam_devq *devq;
   73         struct cam_sim  *sim;
   74         struct cam_path *path;
   75 };
   76 
   77 static int      mfip_probe(device_t);
   78 static int      mfip_attach(device_t);
   79 static int      mfip_detach(device_t);
   80 static void     mfip_cam_action(struct cam_sim *, union ccb *);
   81 static void     mfip_cam_poll(struct cam_sim *);
   82 static struct mfi_command * mfip_start(void *);
   83 static void     mfip_done(struct mfi_command *cm);
   84 
   85 static int mfi_allow_disks = 0;
   86 TUNABLE_INT("hw.mfi.allow_cam_disk_passthrough", &mfi_allow_disks);
   87 SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RD,
   88     &mfi_allow_disks, 0, "event message locale");
   89 
   90 static devclass_t       mfip_devclass;
   91 static device_method_t  mfip_methods[] = {
   92         DEVMETHOD(device_probe,         mfip_probe),
   93         DEVMETHOD(device_attach,        mfip_attach),
   94         DEVMETHOD(device_detach,        mfip_detach),
   95         {0, 0}
   96 };
   97 static driver_t mfip_driver = {
   98         "mfip",
   99         mfip_methods,
  100         sizeof(struct mfip_softc)
  101 };
  102 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
  103 MODULE_DEPEND(mfip, cam, 1, 1, 1);
  104 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
  105 
  106 #define ccb_mfip_ptr sim_priv.entries[0].ptr
  107 
  108 static int
  109 mfip_probe(device_t dev)
  110 {
  111 
  112         device_set_desc(dev, "SCSI Passthrough Bus");
  113         return (0);
  114 }
  115 
  116 static int
  117 mfip_attach(device_t dev)
  118 {
  119         struct mfip_softc *sc;
  120         struct mfi_softc *mfisc;
  121 
  122         sc = device_get_softc(dev);
  123         if (sc == NULL)
  124                 return (EINVAL);
  125 
  126         mfisc = device_get_softc(device_get_parent(dev));
  127         sc->dev = dev;
  128         sc->mfi_sc = mfisc;
  129         mfisc->mfi_cam_start = mfip_start;
  130 
  131         if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
  132                 return (ENOMEM);
  133 
  134         sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
  135                                 device_get_unit(dev), &mfisc->mfi_io_lock, 1,
  136                                 MFI_SCSI_MAX_CMDS, sc->devq);
  137         if (sc->sim == NULL) {
  138                 cam_simq_free(sc->devq);
  139                 device_printf(dev, "CAM SIM attach failed\n");
  140                 return (EINVAL);
  141         }
  142 
  143         mtx_lock(&mfisc->mfi_io_lock);
  144         if (xpt_bus_register(sc->sim, dev, 0) != 0) {
  145                 device_printf(dev, "XPT bus registration failed\n");
  146                 cam_sim_free(sc->sim, FALSE);
  147                 cam_simq_free(sc->devq);
  148                 mtx_unlock(&mfisc->mfi_io_lock);
  149                 return (EINVAL);
  150         }
  151         mtx_unlock(&mfisc->mfi_io_lock);
  152 
  153         return (0);
  154 }
  155 
  156 static int
  157 mfip_detach(device_t dev)
  158 {
  159         struct mfip_softc *sc;
  160 
  161         sc = device_get_softc(dev);
  162         if (sc == NULL)
  163                 return (EINVAL);
  164 
  165         if (sc->sim != NULL) {
  166                 mtx_lock(&sc->mfi_sc->mfi_io_lock);
  167                 xpt_bus_deregister(cam_sim_path(sc->sim));
  168                 cam_sim_free(sc->sim, FALSE);
  169                 mtx_unlock(&sc->mfi_sc->mfi_io_lock);
  170         }
  171 
  172         if (sc->devq != NULL)
  173                 cam_simq_free(sc->devq);
  174 
  175         return (0);
  176 }
  177 
  178 static void
  179 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
  180 {
  181         struct mfip_softc *sc = cam_sim_softc(sim);
  182         struct mfi_softc *mfisc = sc->mfi_sc;
  183 
  184         mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
  185 
  186         switch (ccb->ccb_h.func_code) {
  187         case XPT_PATH_INQ:
  188         {
  189                 struct ccb_pathinq *cpi = &ccb->cpi;
  190 
  191                 cpi->version_num = 1;
  192                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
  193                 cpi->target_sprt = 0;
  194                 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
  195                 cpi->hba_eng_cnt = 0;
  196                 cpi->max_target = MFI_SCSI_MAX_TARGETS;
  197                 cpi->max_lun = MFI_SCSI_MAX_LUNS;
  198                 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
  199                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
  200                 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
  201                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
  202                 cpi->unit_number = cam_sim_unit(sim);
  203                 cpi->bus_id = cam_sim_bus(sim);
  204                 cpi->base_transfer_speed = 150000;
  205                 cpi->transport = XPORT_SAS;
  206                 cpi->transport_version = 0;
  207                 cpi->protocol = PROTO_SCSI;
  208                 cpi->protocol_version = SCSI_REV_2;
  209                 cpi->ccb_h.status = CAM_REQ_CMP;
  210                 break;
  211         }
  212         case XPT_RESET_BUS:
  213                 ccb->ccb_h.status = CAM_REQ_CMP;
  214                 break;
  215         case XPT_RESET_DEV:
  216                 ccb->ccb_h.status = CAM_REQ_CMP;
  217                 break;
  218         case XPT_GET_TRAN_SETTINGS:
  219         {
  220                 struct ccb_trans_settings_sas *sas =
  221                     &ccb->cts.xport_specific.sas;
  222 
  223                 ccb->cts.protocol = PROTO_SCSI;
  224                 ccb->cts.protocol_version = SCSI_REV_2;
  225                 ccb->cts.transport = XPORT_SAS;
  226                 ccb->cts.transport_version = 0;
  227 
  228                 sas->valid &= ~CTS_SAS_VALID_SPEED;
  229                 sas->bitrate = 150000;
  230 
  231                 ccb->ccb_h.status = CAM_REQ_CMP;
  232                 break;
  233         }
  234         case XPT_SET_TRAN_SETTINGS:
  235                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  236                 break;
  237         case XPT_SCSI_IO:
  238         {
  239                 struct ccb_hdr          *ccbh = &ccb->ccb_h;
  240                 struct ccb_scsiio       *csio = &ccb->csio;
  241 
  242                 ccbh->status = CAM_REQ_INPROG;
  243                 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
  244                         ccbh->status = CAM_REQ_INVALID;
  245                         break;
  246                 }
  247                 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  248                         if (ccbh->flags & CAM_DATA_PHYS) {
  249                                 ccbh->status = CAM_REQ_INVALID;
  250                                 break;
  251                         }
  252                         if (ccbh->flags & CAM_SCATTER_VALID) {
  253                                 ccbh->status = CAM_REQ_INVALID;
  254                                 break;
  255                         }
  256                 }
  257 
  258                 ccbh->ccb_mfip_ptr = sc;
  259                 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
  260                 mfi_startio(mfisc);
  261                 return;
  262         }
  263         default:
  264                 ccb->ccb_h.status = CAM_REQ_INVALID;
  265                 break;
  266         }
  267 
  268         xpt_done(ccb);
  269         return;
  270 }
  271 
  272 static struct mfi_command *
  273 mfip_start(void *data)
  274 {
  275         union ccb *ccb = data;
  276         struct ccb_hdr *ccbh = &ccb->ccb_h;
  277         struct ccb_scsiio *csio = &ccb->csio;
  278         struct mfip_softc *sc;
  279         struct mfi_pass_frame *pt;
  280         struct mfi_command *cm;
  281         uint32_t context = 0;
  282 
  283         sc = ccbh->ccb_mfip_ptr;
  284 
  285         if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
  286                 return (NULL);
  287 
  288         /* Zero out the MFI frame */
  289         context = cm->cm_frame->header.context;
  290         bzero(cm->cm_frame, sizeof(union mfi_frame));
  291         cm->cm_frame->header.context = context;
  292 
  293         pt = &cm->cm_frame->pass;
  294         pt->header.cmd = MFI_CMD_PD_SCSI_IO;
  295         pt->header.cmd_status = 0;
  296         pt->header.scsi_status = 0;
  297         pt->header.target_id = ccbh->target_id;
  298         pt->header.lun_id = ccbh->target_lun;
  299         pt->header.flags = 0;
  300         pt->header.timeout = 0;
  301         pt->header.data_len = csio->dxfer_len;
  302         pt->header.sense_len = MFI_SENSE_LEN;
  303         pt->header.cdb_len = csio->cdb_len;
  304         pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
  305         pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
  306         if (ccbh->flags & CAM_CDB_POINTER)
  307                 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
  308         else
  309                 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
  310         cm->cm_complete = mfip_done;
  311         cm->cm_private = ccb;
  312         cm->cm_sg = &pt->sgl;
  313         cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
  314         cm->cm_data = csio->data_ptr;
  315         cm->cm_len = csio->dxfer_len;
  316         switch (ccbh->flags & CAM_DIR_MASK) {
  317         case CAM_DIR_IN:
  318                 cm->cm_flags = MFI_CMD_DATAIN;
  319                 break;
  320         case CAM_DIR_OUT:
  321                 cm->cm_flags = MFI_CMD_DATAOUT;
  322                 break;
  323         case CAM_DIR_NONE:
  324         default:
  325                 cm->cm_data = NULL;
  326                 cm->cm_len = 0;
  327                 cm->cm_flags = 0;
  328                 break;
  329         }
  330 
  331         TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
  332         return (cm);
  333 }
  334 
  335 static void
  336 mfip_done(struct mfi_command *cm)
  337 {
  338         union ccb *ccb = cm->cm_private;
  339         struct ccb_hdr *ccbh = &ccb->ccb_h;
  340         struct ccb_scsiio *csio = &ccb->csio;
  341         struct mfip_softc *sc;
  342         struct mfi_pass_frame *pt;
  343 
  344         sc = ccbh->ccb_mfip_ptr;
  345         pt = &cm->cm_frame->pass;
  346 
  347         switch (pt->header.cmd_status) {
  348         case MFI_STAT_OK:
  349         {
  350                 uint8_t command, device;
  351 
  352                 ccbh->status = CAM_REQ_CMP;
  353                 csio->scsi_status = pt->header.scsi_status;
  354                 if (ccbh->flags & CAM_CDB_POINTER)
  355                         command = csio->cdb_io.cdb_ptr[0];
  356                 else
  357                         command = csio->cdb_io.cdb_bytes[0];
  358                 if (command == INQUIRY) {
  359                         device = csio->data_ptr[0] & 0x1f;
  360                         if ((!mfi_allow_disks && device == T_DIRECT) ||
  361                             (device == T_PROCESSOR))
  362                                 csio->data_ptr[0] =
  363                                      (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
  364                 }
  365                 break;
  366         }
  367         case MFI_STAT_SCSI_DONE_WITH_ERROR:
  368         {
  369                 int sense_len;
  370 
  371                 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
  372                 csio->scsi_status = pt->header.scsi_status;
  373                 if (pt->header.sense_len < csio->sense_len)
  374                         csio->sense_resid = csio->sense_len -
  375                             pt->header.sense_len;
  376                 else
  377                         csio->sense_resid = 0;
  378                 sense_len = min(pt->header.sense_len,
  379                     sizeof(struct scsi_sense_data));
  380                 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
  381                 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
  382                 break;
  383         }
  384         case MFI_STAT_DEVICE_NOT_FOUND:
  385                 ccbh->status = CAM_SEL_TIMEOUT;
  386                 break;
  387         case MFI_STAT_SCSI_IO_FAILED:
  388                 ccbh->status = CAM_REQ_CMP_ERR;
  389                 csio->scsi_status = pt->header.scsi_status;
  390                 break;
  391         default:
  392                 ccbh->status = CAM_REQ_CMP_ERR;
  393                 csio->scsi_status = pt->header.scsi_status;
  394                 break;
  395         }
  396 
  397         mfi_release_command(cm);
  398         xpt_done(ccb);
  399 }
  400 
  401 static void
  402 mfip_cam_poll(struct cam_sim *sim)
  403 {
  404         struct mfip_softc *sc = cam_sim_softc(sim);
  405         struct mfi_softc *mfisc = sc->mfi_sc;
  406 
  407         mfisc->mfi_intr_ptr(mfisc);
  408 }
  409 

Cache object: 20768caa8c0f89e74de65233ab7084f6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.