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

Cache object: 1f6330d8818265a499cd5223889a121b


[ 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.