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

Cache object: c2318baeb1f539ed17647129492b3a47


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