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

Cache object: 7059033e192c723c4107979ac5fca95b


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