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/cam/scsi/scsi_pass.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
    5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions, and the following disclaimer,
   13  *    without modification, immediately at the beginning of the file.
   14  * 2. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/kernel.h>
   36 #include <sys/conf.h>
   37 #include <sys/types.h>
   38 #include <sys/bio.h>
   39 #include <sys/bus.h>
   40 #include <sys/devicestat.h>
   41 #include <sys/errno.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/malloc.h>
   44 #include <sys/proc.h>
   45 #include <sys/poll.h>
   46 #include <sys/selinfo.h>
   47 #include <sys/sdt.h>
   48 #include <sys/sysent.h>
   49 #include <sys/taskqueue.h>
   50 #include <vm/uma.h>
   51 #include <vm/vm.h>
   52 #include <vm/vm_extern.h>
   53 
   54 #include <machine/bus.h>
   55 
   56 #include <cam/cam.h>
   57 #include <cam/cam_ccb.h>
   58 #include <cam/cam_periph.h>
   59 #include <cam/cam_queue.h>
   60 #include <cam/cam_xpt.h>
   61 #include <cam/cam_xpt_periph.h>
   62 #include <cam/cam_debug.h>
   63 #include <cam/cam_compat.h>
   64 #include <cam/cam_xpt_periph.h>
   65 
   66 #include <cam/scsi/scsi_all.h>
   67 #include <cam/scsi/scsi_pass.h>
   68 
   69 typedef enum {
   70         PASS_FLAG_OPEN                  = 0x01,
   71         PASS_FLAG_LOCKED                = 0x02,
   72         PASS_FLAG_INVALID               = 0x04,
   73         PASS_FLAG_INITIAL_PHYSPATH      = 0x08,
   74         PASS_FLAG_ZONE_INPROG           = 0x10,
   75         PASS_FLAG_ZONE_VALID            = 0x20,
   76         PASS_FLAG_UNMAPPED_CAPABLE      = 0x40,
   77         PASS_FLAG_ABANDONED_REF_SET     = 0x80
   78 } pass_flags;
   79 
   80 typedef enum {
   81         PASS_STATE_NORMAL
   82 } pass_state;
   83 
   84 typedef enum {
   85         PASS_CCB_BUFFER_IO,
   86         PASS_CCB_QUEUED_IO
   87 } pass_ccb_types;
   88 
   89 #define ccb_type        ppriv_field0
   90 #define ccb_ioreq       ppriv_ptr1
   91 
   92 /*
   93  * The maximum number of memory segments we preallocate.
   94  */
   95 #define PASS_MAX_SEGS   16
   96 
   97 typedef enum {
   98         PASS_IO_NONE            = 0x00,
   99         PASS_IO_USER_SEG_MALLOC = 0x01,
  100         PASS_IO_KERN_SEG_MALLOC = 0x02,
  101         PASS_IO_ABANDONED       = 0x04
  102 } pass_io_flags; 
  103 
  104 struct pass_io_req {
  105         union ccb                        ccb;
  106         union ccb                       *alloced_ccb;
  107         union ccb                       *user_ccb_ptr;
  108         camq_entry                       user_periph_links;
  109         ccb_ppriv_area                   user_periph_priv;
  110         struct cam_periph_map_info       mapinfo;
  111         pass_io_flags                    flags;
  112         ccb_flags                        data_flags;
  113         int                              num_user_segs;
  114         bus_dma_segment_t                user_segs[PASS_MAX_SEGS];
  115         int                              num_kern_segs;
  116         bus_dma_segment_t                kern_segs[PASS_MAX_SEGS];
  117         bus_dma_segment_t               *user_segptr;
  118         bus_dma_segment_t               *kern_segptr;
  119         int                              num_bufs;
  120         uint32_t                         dirs[CAM_PERIPH_MAXMAPS];
  121         uint32_t                         lengths[CAM_PERIPH_MAXMAPS];
  122         uint8_t                         *user_bufs[CAM_PERIPH_MAXMAPS];
  123         uint8_t                         *kern_bufs[CAM_PERIPH_MAXMAPS];
  124         struct bintime                   start_time;
  125         TAILQ_ENTRY(pass_io_req)         links;
  126 };
  127 
  128 struct pass_softc {
  129         pass_state                state;
  130         pass_flags                flags;
  131         u_int8_t                  pd_type;
  132         int                       open_count;
  133         u_int                     maxio;
  134         struct devstat           *device_stats;
  135         struct cdev              *dev;
  136         struct cdev              *alias_dev;
  137         struct task               add_physpath_task;
  138         struct task               shutdown_kqueue_task;
  139         struct selinfo            read_select;
  140         TAILQ_HEAD(, pass_io_req) incoming_queue;
  141         TAILQ_HEAD(, pass_io_req) active_queue;
  142         TAILQ_HEAD(, pass_io_req) abandoned_queue;
  143         TAILQ_HEAD(, pass_io_req) done_queue;
  144         struct cam_periph        *periph;
  145         char                      zone_name[12];
  146         char                      io_zone_name[12];
  147         uma_zone_t                pass_zone;
  148         uma_zone_t                pass_io_zone;
  149         size_t                    io_zone_size;
  150 };
  151 
  152 static  d_open_t        passopen;
  153 static  d_close_t       passclose;
  154 static  d_ioctl_t       passioctl;
  155 static  d_ioctl_t       passdoioctl;
  156 static  d_poll_t        passpoll;
  157 static  d_kqfilter_t    passkqfilter;
  158 static  void            passreadfiltdetach(struct knote *kn);
  159 static  int             passreadfilt(struct knote *kn, long hint);
  160 
  161 static  periph_init_t   passinit;
  162 static  periph_ctor_t   passregister;
  163 static  periph_oninv_t  passoninvalidate;
  164 static  periph_dtor_t   passcleanup;
  165 static  periph_start_t  passstart;
  166 static  void            pass_shutdown_kqueue(void *context, int pending);
  167 static  void            pass_add_physpath(void *context, int pending);
  168 static  void            passasync(void *callback_arg, u_int32_t code,
  169                                   struct cam_path *path, void *arg);
  170 static  void            passdone(struct cam_periph *periph, 
  171                                  union ccb *done_ccb);
  172 static  int             passcreatezone(struct cam_periph *periph);
  173 static  void            passiocleanup(struct pass_softc *softc, 
  174                                       struct pass_io_req *io_req);
  175 static  int             passcopysglist(struct cam_periph *periph,
  176                                        struct pass_io_req *io_req,
  177                                        ccb_flags direction);
  178 static  int             passmemsetup(struct cam_periph *periph,
  179                                      struct pass_io_req *io_req);
  180 static  int             passmemdone(struct cam_periph *periph,
  181                                     struct pass_io_req *io_req);
  182 static  int             passerror(union ccb *ccb, u_int32_t cam_flags, 
  183                                   u_int32_t sense_flags);
  184 static  int             passsendccb(struct cam_periph *periph, union ccb *ccb,
  185                                     union ccb *inccb);
  186 
  187 static struct periph_driver passdriver =
  188 {
  189         passinit, "pass",
  190         TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
  191 };
  192 
  193 PERIPHDRIVER_DECLARE(pass, passdriver);
  194 
  195 static struct cdevsw pass_cdevsw = {
  196         .d_version =    D_VERSION,
  197         .d_flags =      D_TRACKCLOSE,
  198         .d_open =       passopen,
  199         .d_close =      passclose,
  200         .d_ioctl =      passioctl,
  201         .d_poll =       passpoll,
  202         .d_kqfilter =   passkqfilter,
  203         .d_name =       "pass",
  204 };
  205 
  206 static struct filterops passread_filtops = {
  207         .f_isfd =       1,
  208         .f_detach =     passreadfiltdetach,
  209         .f_event =      passreadfilt
  210 };
  211 
  212 static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers");
  213 
  214 static void
  215 passinit(void)
  216 {
  217         cam_status status;
  218 
  219         /*
  220          * Install a global async callback.  This callback will
  221          * receive async callbacks like "new device found".
  222          */
  223         status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
  224 
  225         if (status != CAM_REQ_CMP) {
  226                 printf("pass: Failed to attach master async callback "
  227                        "due to status 0x%x!\n", status);
  228         }
  229 
  230 }
  231 
  232 static void
  233 passrejectios(struct cam_periph *periph)
  234 {
  235         struct pass_io_req *io_req, *io_req2;
  236         struct pass_softc *softc;
  237 
  238         softc = (struct pass_softc *)periph->softc;
  239 
  240         /*
  241          * The user can no longer get status for I/O on the done queue, so
  242          * clean up all outstanding I/O on the done queue.
  243          */
  244         TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
  245                 TAILQ_REMOVE(&softc->done_queue, io_req, links);
  246                 passiocleanup(softc, io_req);
  247                 uma_zfree(softc->pass_zone, io_req);
  248         }
  249 
  250         /*
  251          * The underlying device is gone, so we can't issue these I/Os.
  252          * The devfs node has been shut down, so we can't return status to
  253          * the user.  Free any I/O left on the incoming queue.
  254          */
  255         TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) {
  256                 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
  257                 passiocleanup(softc, io_req);
  258                 uma_zfree(softc->pass_zone, io_req);
  259         }
  260 
  261         /*
  262          * Normally we would put I/Os on the abandoned queue and acquire a
  263          * reference when we saw the final close.  But, the device went
  264          * away and devfs may have moved everything off to deadfs by the
  265          * time the I/O done callback is called; as a result, we won't see
  266          * any more closes.  So, if we have any active I/Os, we need to put
  267          * them on the abandoned queue.  When the abandoned queue is empty,
  268          * we'll release the remaining reference (see below) to the peripheral.
  269          */
  270         TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) {
  271                 TAILQ_REMOVE(&softc->active_queue, io_req, links);
  272                 io_req->flags |= PASS_IO_ABANDONED;
  273                 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links);
  274         }
  275 
  276         /*
  277          * If we put any I/O on the abandoned queue, acquire a reference.
  278          */
  279         if ((!TAILQ_EMPTY(&softc->abandoned_queue))
  280          && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) {
  281                 cam_periph_doacquire(periph);
  282                 softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
  283         }
  284 }
  285 
  286 static void
  287 passdevgonecb(void *arg)
  288 {
  289         struct cam_periph *periph;
  290         struct mtx *mtx;
  291         struct pass_softc *softc;
  292         int i;
  293 
  294         periph = (struct cam_periph *)arg;
  295         mtx = cam_periph_mtx(periph);
  296         mtx_lock(mtx);
  297 
  298         softc = (struct pass_softc *)periph->softc;
  299         KASSERT(softc->open_count >= 0, ("Negative open count %d",
  300                 softc->open_count));
  301 
  302         /*
  303          * When we get this callback, we will get no more close calls from
  304          * devfs.  So if we have any dangling opens, we need to release the
  305          * reference held for that particular context.
  306          */
  307         for (i = 0; i < softc->open_count; i++)
  308                 cam_periph_release_locked(periph);
  309 
  310         softc->open_count = 0;
  311 
  312         /*
  313          * Release the reference held for the device node, it is gone now.
  314          * Accordingly, inform all queued I/Os of their fate.
  315          */
  316         cam_periph_release_locked(periph);
  317         passrejectios(periph);
  318 
  319         /*
  320          * We reference the SIM lock directly here, instead of using
  321          * cam_periph_unlock().  The reason is that the final call to
  322          * cam_periph_release_locked() above could result in the periph
  323          * getting freed.  If that is the case, dereferencing the periph
  324          * with a cam_periph_unlock() call would cause a page fault.
  325          */
  326         mtx_unlock(mtx);
  327 
  328         /*
  329          * We have to remove our kqueue context from a thread because it
  330          * may sleep.  It would be nice if we could get a callback from
  331          * kqueue when it is done cleaning up resources.
  332          */
  333         taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task);
  334 }
  335 
  336 static void
  337 passoninvalidate(struct cam_periph *periph)
  338 {
  339         struct pass_softc *softc;
  340 
  341         softc = (struct pass_softc *)periph->softc;
  342 
  343         /*
  344          * De-register any async callbacks.
  345          */
  346         xpt_register_async(0, passasync, periph, periph->path);
  347 
  348         softc->flags |= PASS_FLAG_INVALID;
  349 
  350         /*
  351          * Tell devfs this device has gone away, and ask for a callback
  352          * when it has cleaned up its state.
  353          */
  354         destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
  355 }
  356 
  357 static void
  358 passcleanup(struct cam_periph *periph)
  359 {
  360         struct pass_softc *softc;
  361 
  362         softc = (struct pass_softc *)periph->softc;
  363 
  364         cam_periph_assert(periph, MA_OWNED);
  365         KASSERT(TAILQ_EMPTY(&softc->active_queue),
  366                 ("%s called when there are commands on the active queue!\n",
  367                 __func__));
  368         KASSERT(TAILQ_EMPTY(&softc->abandoned_queue),
  369                 ("%s called when there are commands on the abandoned queue!\n",
  370                 __func__));
  371         KASSERT(TAILQ_EMPTY(&softc->incoming_queue),
  372                 ("%s called when there are commands on the incoming queue!\n",
  373                 __func__));
  374         KASSERT(TAILQ_EMPTY(&softc->done_queue),
  375                 ("%s called when there are commands on the done queue!\n",
  376                 __func__));
  377 
  378         devstat_remove_entry(softc->device_stats);
  379 
  380         cam_periph_unlock(periph);
  381 
  382         /*
  383          * We call taskqueue_drain() for the physpath task to make sure it
  384          * is complete.  We drop the lock because this can potentially
  385          * sleep.  XXX KDM that is bad.  Need a way to get a callback when
  386          * a taskqueue is drained.
  387          *
  388          * Note that we don't drain the kqueue shutdown task queue.  This
  389          * is because we hold a reference on the periph for kqueue, and
  390          * release that reference from the kqueue shutdown task queue.  So
  391          * we cannot come into this routine unless we've released that
  392          * reference.  Also, because that could be the last reference, we
  393          * could be called from the cam_periph_release() call in
  394          * pass_shutdown_kqueue().  In that case, the taskqueue_drain()
  395          * would deadlock.  It would be preferable if we had a way to
  396          * get a callback when a taskqueue is done.
  397          */
  398         taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
  399 
  400         /*
  401          * It should be safe to destroy the zones from here, because all
  402          * of the references to this peripheral have been freed, and all
  403          * I/O has been terminated and freed.  We check the zones for NULL
  404          * because they may not have been allocated yet if the device went
  405          * away before any asynchronous I/O has been issued.
  406          */
  407         if (softc->pass_zone != NULL)
  408                 uma_zdestroy(softc->pass_zone);
  409         if (softc->pass_io_zone != NULL)
  410                 uma_zdestroy(softc->pass_io_zone);
  411 
  412         cam_periph_lock(periph);
  413 
  414         free(softc, M_DEVBUF);
  415 }
  416 
  417 static void
  418 pass_shutdown_kqueue(void *context, int pending)
  419 {
  420         struct cam_periph *periph;
  421         struct pass_softc *softc;
  422 
  423         periph = context;
  424         softc = periph->softc;
  425 
  426         knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0);
  427         knlist_destroy(&softc->read_select.si_note);
  428 
  429         /*
  430          * Release the reference we held for kqueue.
  431          */
  432         cam_periph_release(periph);
  433 }
  434 
  435 static void
  436 pass_add_physpath(void *context, int pending)
  437 {
  438         struct cam_periph *periph;
  439         struct pass_softc *softc;
  440         struct mtx *mtx;
  441         char *physpath;
  442 
  443         /*
  444          * If we have one, create a devfs alias for our
  445          * physical path.
  446          */
  447         periph = context;
  448         softc = periph->softc;
  449         physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
  450         mtx = cam_periph_mtx(periph);
  451         mtx_lock(mtx);
  452 
  453         if (periph->flags & CAM_PERIPH_INVALID)
  454                 goto out;
  455 
  456         if (xpt_getattr(physpath, MAXPATHLEN,
  457                         "GEOM::physpath", periph->path) == 0
  458          && strlen(physpath) != 0) {
  459                 mtx_unlock(mtx);
  460                 make_dev_physpath_alias(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME,
  461                                 &softc->alias_dev, softc->dev,
  462                                 softc->alias_dev, physpath);
  463                 mtx_lock(mtx);
  464         }
  465 
  466 out:
  467         /*
  468          * Now that we've made our alias, we no longer have to have a
  469          * reference to the device.
  470          */
  471         if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0)
  472                 softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
  473 
  474         /*
  475          * We always acquire a reference to the periph before queueing this
  476          * task queue function, so it won't go away before we run.
  477          */
  478         while (pending-- > 0)
  479                 cam_periph_release_locked(periph);
  480         mtx_unlock(mtx);
  481 
  482         free(physpath, M_DEVBUF);
  483 }
  484 
  485 static void
  486 passasync(void *callback_arg, u_int32_t code,
  487           struct cam_path *path, void *arg)
  488 {
  489         struct cam_periph *periph;
  490 
  491         periph = (struct cam_periph *)callback_arg;
  492 
  493         switch (code) {
  494         case AC_FOUND_DEVICE:
  495         {
  496                 struct ccb_getdev *cgd;
  497                 cam_status status;
  498 
  499                 cgd = (struct ccb_getdev *)arg;
  500                 if (cgd == NULL)
  501                         break;
  502 
  503                 /*
  504                  * Allocate a peripheral instance for
  505                  * this device and start the probe
  506                  * process.
  507                  */
  508                 status = cam_periph_alloc(passregister, passoninvalidate,
  509                                           passcleanup, passstart, "pass",
  510                                           CAM_PERIPH_BIO, path,
  511                                           passasync, AC_FOUND_DEVICE, cgd);
  512 
  513                 if (status != CAM_REQ_CMP
  514                  && status != CAM_REQ_INPROG) {
  515                         const struct cam_status_entry *entry;
  516 
  517                         entry = cam_fetch_status_entry(status);
  518 
  519                         printf("passasync: Unable to attach new device "
  520                                "due to status %#x: %s\n", status, entry ?
  521                                entry->status_text : "Unknown");
  522                 }
  523 
  524                 break;
  525         }
  526         case AC_ADVINFO_CHANGED:
  527         {
  528                 uintptr_t buftype;
  529 
  530                 buftype = (uintptr_t)arg;
  531                 if (buftype == CDAI_TYPE_PHYS_PATH) {
  532                         struct pass_softc *softc;
  533 
  534                         softc = (struct pass_softc *)periph->softc;
  535                         /*
  536                          * Acquire a reference to the periph before we
  537                          * start the taskqueue, so that we don't run into
  538                          * a situation where the periph goes away before
  539                          * the task queue has a chance to run.
  540                          */
  541                         if (cam_periph_acquire(periph) != 0)
  542                                 break;
  543 
  544                         taskqueue_enqueue(taskqueue_thread,
  545                                           &softc->add_physpath_task);
  546                 }
  547                 break;
  548         }
  549         default:
  550                 cam_periph_async(periph, code, path, arg);
  551                 break;
  552         }
  553 }
  554 
  555 static cam_status
  556 passregister(struct cam_periph *periph, void *arg)
  557 {
  558         struct pass_softc *softc;
  559         struct ccb_getdev *cgd;
  560         struct ccb_pathinq cpi;
  561         struct make_dev_args args;
  562         int error, no_tags;
  563 
  564         cgd = (struct ccb_getdev *)arg;
  565         if (cgd == NULL) {
  566                 printf("%s: no getdev CCB, can't register device\n", __func__);
  567                 return(CAM_REQ_CMP_ERR);
  568         }
  569 
  570         softc = (struct pass_softc *)malloc(sizeof(*softc),
  571                                             M_DEVBUF, M_NOWAIT);
  572 
  573         if (softc == NULL) {
  574                 printf("%s: Unable to probe new device. "
  575                        "Unable to allocate softc\n", __func__);
  576                 return(CAM_REQ_CMP_ERR);
  577         }
  578 
  579         bzero(softc, sizeof(*softc));
  580         softc->state = PASS_STATE_NORMAL;
  581         if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
  582                 softc->pd_type = SID_TYPE(&cgd->inq_data);
  583         else if (cgd->protocol == PROTO_SATAPM)
  584                 softc->pd_type = T_ENCLOSURE;
  585         else
  586                 softc->pd_type = T_DIRECT;
  587 
  588         periph->softc = softc;
  589         softc->periph = periph;
  590         TAILQ_INIT(&softc->incoming_queue);
  591         TAILQ_INIT(&softc->active_queue);
  592         TAILQ_INIT(&softc->abandoned_queue);
  593         TAILQ_INIT(&softc->done_queue);
  594         snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d",
  595                  periph->periph_name, periph->unit_number);
  596         snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO",
  597                  periph->periph_name, periph->unit_number);
  598         softc->io_zone_size = maxphys;
  599         knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph));
  600 
  601         xpt_path_inq(&cpi, periph->path);
  602 
  603         if (cpi.maxio == 0)
  604                 softc->maxio = DFLTPHYS;        /* traditional default */
  605         else if (cpi.maxio > maxphys)
  606                 softc->maxio = maxphys;         /* for safety */
  607         else
  608                 softc->maxio = cpi.maxio;       /* real value */
  609 
  610         if (cpi.hba_misc & PIM_UNMAPPED)
  611                 softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE;
  612 
  613         /*
  614          * We pass in 0 for a blocksize, since we don't 
  615          * know what the blocksize of this device is, if 
  616          * it even has a blocksize.
  617          */
  618         cam_periph_unlock(periph);
  619         no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
  620         softc->device_stats = devstat_new_entry("pass",
  621                           periph->unit_number, 0,
  622                           DEVSTAT_NO_BLOCKSIZE
  623                           | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
  624                           softc->pd_type |
  625                           XPORT_DEVSTAT_TYPE(cpi.transport) |
  626                           DEVSTAT_TYPE_PASS,
  627                           DEVSTAT_PRIORITY_PASS);
  628 
  629         /*
  630          * Initialize the taskqueue handler for shutting down kqueue.
  631          */
  632         TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0,
  633                   pass_shutdown_kqueue, periph);
  634 
  635         /*
  636          * Acquire a reference to the periph that we can release once we've
  637          * cleaned up the kqueue.
  638          */
  639         if (cam_periph_acquire(periph) != 0) {
  640                 xpt_print(periph->path, "%s: lost periph during "
  641                           "registration!\n", __func__);
  642                 cam_periph_lock(periph);
  643                 return (CAM_REQ_CMP_ERR);
  644         }
  645 
  646         /*
  647          * Acquire a reference to the periph before we create the devfs
  648          * instance for it.  We'll release this reference once the devfs
  649          * instance has been freed.
  650          */
  651         if (cam_periph_acquire(periph) != 0) {
  652                 xpt_print(periph->path, "%s: lost periph during "
  653                           "registration!\n", __func__);
  654                 cam_periph_lock(periph);
  655                 return (CAM_REQ_CMP_ERR);
  656         }
  657 
  658         /* Register the device */
  659         make_dev_args_init(&args);
  660         args.mda_devsw = &pass_cdevsw;
  661         args.mda_unit = periph->unit_number;
  662         args.mda_uid = UID_ROOT;
  663         args.mda_gid = GID_OPERATOR;
  664         args.mda_mode = 0600;
  665         args.mda_si_drv1 = periph;
  666         args.mda_flags = MAKEDEV_NOWAIT;
  667         error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
  668             periph->unit_number);
  669         if (error != 0) {
  670                 cam_periph_lock(periph);
  671                 cam_periph_release_locked(periph);
  672                 return (CAM_REQ_CMP_ERR);
  673         }
  674 
  675         /*
  676          * Hold a reference to the periph before we create the physical
  677          * path alias so it can't go away.
  678          */
  679         if (cam_periph_acquire(periph) != 0) {
  680                 xpt_print(periph->path, "%s: lost periph during "
  681                           "registration!\n", __func__);
  682                 cam_periph_lock(periph);
  683                 return (CAM_REQ_CMP_ERR);
  684         }
  685 
  686         cam_periph_lock(periph);
  687 
  688         TASK_INIT(&softc->add_physpath_task, /*priority*/0,
  689                   pass_add_physpath, periph);
  690 
  691         /*
  692          * See if physical path information is already available.
  693          */
  694         taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
  695 
  696         /*
  697          * Add an async callback so that we get notified if
  698          * this device goes away or its physical path
  699          * (stored in the advanced info data of the EDT) has
  700          * changed.
  701          */
  702         xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
  703                            passasync, periph, periph->path);
  704 
  705         if (bootverbose)
  706                 xpt_announce_periph(periph, NULL);
  707 
  708         return(CAM_REQ_CMP);
  709 }
  710 
  711 static int
  712 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
  713 {
  714         struct cam_periph *periph;
  715         struct pass_softc *softc;
  716         int error;
  717 
  718         periph = (struct cam_periph *)dev->si_drv1;
  719         if (cam_periph_acquire(periph) != 0)
  720                 return (ENXIO);
  721 
  722         cam_periph_lock(periph);
  723 
  724         softc = (struct pass_softc *)periph->softc;
  725 
  726         if (softc->flags & PASS_FLAG_INVALID) {
  727                 cam_periph_release_locked(periph);
  728                 cam_periph_unlock(periph);
  729                 return(ENXIO);
  730         }
  731 
  732         /*
  733          * Don't allow access when we're running at a high securelevel.
  734          */
  735         error = securelevel_gt(td->td_ucred, 1);
  736         if (error) {
  737                 cam_periph_release_locked(periph);
  738                 cam_periph_unlock(periph);
  739                 return(error);
  740         }
  741 
  742         /*
  743          * Only allow read-write access.
  744          */
  745         if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
  746                 cam_periph_release_locked(periph);
  747                 cam_periph_unlock(periph);
  748                 return(EPERM);
  749         }
  750 
  751         /*
  752          * We don't allow nonblocking access.
  753          */
  754         if ((flags & O_NONBLOCK) != 0) {
  755                 xpt_print(periph->path, "can't do nonblocking access\n");
  756                 cam_periph_release_locked(periph);
  757                 cam_periph_unlock(periph);
  758                 return(EINVAL);
  759         }
  760 
  761         softc->open_count++;
  762 
  763         cam_periph_unlock(periph);
  764 
  765         return (error);
  766 }
  767 
  768 static int
  769 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
  770 {
  771         struct  cam_periph *periph;
  772         struct  pass_softc *softc;
  773         struct mtx *mtx;
  774 
  775         periph = (struct cam_periph *)dev->si_drv1;
  776         mtx = cam_periph_mtx(periph);
  777         mtx_lock(mtx);
  778 
  779         softc = periph->softc;
  780         softc->open_count--;
  781 
  782         if (softc->open_count == 0) {
  783                 struct pass_io_req *io_req, *io_req2;
  784 
  785                 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
  786                         TAILQ_REMOVE(&softc->done_queue, io_req, links);
  787                         passiocleanup(softc, io_req);
  788                         uma_zfree(softc->pass_zone, io_req);
  789                 }
  790 
  791                 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links,
  792                                    io_req2) {
  793                         TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
  794                         passiocleanup(softc, io_req);
  795                         uma_zfree(softc->pass_zone, io_req);
  796                 }
  797 
  798                 /*
  799                  * If there are any active I/Os, we need to forcibly acquire a
  800                  * reference to the peripheral so that we don't go away
  801                  * before they complete.  We'll release the reference when
  802                  * the abandoned queue is empty.
  803                  */
  804                 io_req = TAILQ_FIRST(&softc->active_queue);
  805                 if ((io_req != NULL)
  806                  && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) {
  807                         cam_periph_doacquire(periph);
  808                         softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
  809                 }
  810 
  811                 /*
  812                  * Since the I/O in the active queue is not under our
  813                  * control, just set a flag so that we can clean it up when
  814                  * it completes and put it on the abandoned queue.  This
  815                  * will prevent our sending spurious completions in the
  816                  * event that the device is opened again before these I/Os
  817                  * complete.
  818                  */
  819                 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links,
  820                                    io_req2) {
  821                         TAILQ_REMOVE(&softc->active_queue, io_req, links);
  822                         io_req->flags |= PASS_IO_ABANDONED;
  823                         TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req,
  824                                           links);
  825                 }
  826         }
  827 
  828         cam_periph_release_locked(periph);
  829 
  830         /*
  831          * We reference the lock directly here, instead of using
  832          * cam_periph_unlock().  The reason is that the call to
  833          * cam_periph_release_locked() above could result in the periph
  834          * getting freed.  If that is the case, dereferencing the periph
  835          * with a cam_periph_unlock() call would cause a page fault.
  836          *
  837          * cam_periph_release() avoids this problem using the same method,
  838          * but we're manually acquiring and dropping the lock here to
  839          * protect the open count and avoid another lock acquisition and
  840          * release.
  841          */
  842         mtx_unlock(mtx);
  843 
  844         return (0);
  845 }
  846 
  847 static void
  848 passstart(struct cam_periph *periph, union ccb *start_ccb)
  849 {
  850         struct pass_softc *softc;
  851 
  852         softc = (struct pass_softc *)periph->softc;
  853 
  854         switch (softc->state) {
  855         case PASS_STATE_NORMAL: {
  856                 struct pass_io_req *io_req;
  857 
  858                 /*
  859                  * Check for any queued I/O requests that require an
  860                  * allocated slot.
  861                  */
  862                 io_req = TAILQ_FIRST(&softc->incoming_queue);
  863                 if (io_req == NULL) {
  864                         xpt_release_ccb(start_ccb);
  865                         break;
  866                 }
  867                 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
  868                 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
  869                 /*
  870                  * Merge the user's CCB into the allocated CCB.
  871                  */
  872                 xpt_merge_ccb(start_ccb, &io_req->ccb);
  873                 start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO;
  874                 start_ccb->ccb_h.ccb_ioreq = io_req;
  875                 start_ccb->ccb_h.cbfcnp = passdone;
  876                 io_req->alloced_ccb = start_ccb;
  877                 binuptime(&io_req->start_time);
  878                 devstat_start_transaction(softc->device_stats,
  879                                           &io_req->start_time);
  880 
  881                 xpt_action(start_ccb);
  882 
  883                 /*
  884                  * If we have any more I/O waiting, schedule ourselves again.
  885                  */
  886                 if (!TAILQ_EMPTY(&softc->incoming_queue))
  887                         xpt_schedule(periph, CAM_PRIORITY_NORMAL);
  888                 break;
  889         }
  890         default:
  891                 break;
  892         }
  893 }
  894 
  895 static void
  896 passdone(struct cam_periph *periph, union ccb *done_ccb)
  897 { 
  898         struct pass_softc *softc;
  899         struct ccb_scsiio *csio;
  900 
  901         softc = (struct pass_softc *)periph->softc;
  902 
  903         cam_periph_assert(periph, MA_OWNED);
  904 
  905         csio = &done_ccb->csio;
  906         switch (csio->ccb_h.ccb_type) {
  907         case PASS_CCB_QUEUED_IO: {
  908                 struct pass_io_req *io_req;
  909 
  910                 io_req = done_ccb->ccb_h.ccb_ioreq;
  911 #if 0
  912                 xpt_print(periph->path, "%s: called for user CCB %p\n",
  913                           __func__, io_req->user_ccb_ptr);
  914 #endif
  915                 if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
  916                  && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER)
  917                  && ((io_req->flags & PASS_IO_ABANDONED) == 0)) {
  918                         int error;
  919 
  920                         error = passerror(done_ccb, CAM_RETRY_SELTO,
  921                                           SF_RETRY_UA | SF_NO_PRINT);
  922 
  923                         if (error == ERESTART) {
  924                                 /*
  925                                  * A retry was scheduled, so
  926                                  * just return.
  927                                  */
  928                                 return;
  929                         }
  930                 }
  931 
  932                 /*
  933                  * Copy the allocated CCB contents back to the malloced CCB
  934                  * so we can give status back to the user when he requests it.
  935                  */
  936                 bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb));
  937 
  938                 /*
  939                  * Log data/transaction completion with devstat(9).
  940                  */
  941                 switch (done_ccb->ccb_h.func_code) {
  942                 case XPT_SCSI_IO:
  943                         devstat_end_transaction(softc->device_stats,
  944                             done_ccb->csio.dxfer_len - done_ccb->csio.resid,
  945                             done_ccb->csio.tag_action & 0x3,
  946                             ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
  947                             CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
  948                             (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
  949                             DEVSTAT_WRITE : DEVSTAT_READ, NULL,
  950                             &io_req->start_time);
  951                         break;
  952                 case XPT_ATA_IO:
  953                         devstat_end_transaction(softc->device_stats,
  954                             done_ccb->ataio.dxfer_len - done_ccb->ataio.resid,
  955                             0, /* Not used in ATA */
  956                             ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
  957                             CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 
  958                             (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
  959                             DEVSTAT_WRITE : DEVSTAT_READ, NULL,
  960                             &io_req->start_time);
  961                         break;
  962                 case XPT_SMP_IO:
  963                         /*
  964                          * XXX KDM this isn't quite right, but there isn't
  965                          * currently an easy way to represent a bidirectional 
  966                          * transfer in devstat.  The only way to do it
  967                          * and have the byte counts come out right would
  968                          * mean that we would have to record two
  969                          * transactions, one for the request and one for the
  970                          * response.  For now, so that we report something,
  971                          * just treat the entire thing as a read.
  972                          */
  973                         devstat_end_transaction(softc->device_stats,
  974                             done_ccb->smpio.smp_request_len +
  975                             done_ccb->smpio.smp_response_len,
  976                             DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL,
  977                             &io_req->start_time);
  978                         break;
  979                 default:
  980                         devstat_end_transaction(softc->device_stats, 0,
  981                             DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL,
  982                             &io_req->start_time);
  983                         break;
  984                 }
  985 
  986                 /*
  987                  * In the normal case, take the completed I/O off of the
  988                  * active queue and put it on the done queue.  Notitfy the
  989                  * user that we have a completed I/O.
  990                  */
  991                 if ((io_req->flags & PASS_IO_ABANDONED) == 0) {
  992                         TAILQ_REMOVE(&softc->active_queue, io_req, links);
  993                         TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
  994                         selwakeuppri(&softc->read_select, PRIBIO);
  995                         KNOTE_LOCKED(&softc->read_select.si_note, 0);
  996                 } else {
  997                         /*
  998                          * In the case of an abandoned I/O (final close
  999                          * without fetching the I/O), take it off of the
 1000                          * abandoned queue and free it.
 1001                          */
 1002                         TAILQ_REMOVE(&softc->abandoned_queue, io_req, links);
 1003                         passiocleanup(softc, io_req);
 1004                         uma_zfree(softc->pass_zone, io_req);
 1005 
 1006                         /*
 1007                          * Release the done_ccb here, since we may wind up
 1008                          * freeing the peripheral when we decrement the
 1009                          * reference count below.
 1010                          */
 1011                         xpt_release_ccb(done_ccb);
 1012 
 1013                         /*
 1014                          * If the abandoned queue is empty, we can release
 1015                          * our reference to the periph since we won't have
 1016                          * any more completions coming.
 1017                          */
 1018                         if ((TAILQ_EMPTY(&softc->abandoned_queue))
 1019                          && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) {
 1020                                 softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET;
 1021                                 cam_periph_release_locked(periph);
 1022                         }
 1023 
 1024                         /*
 1025                          * We have already released the CCB, so we can
 1026                          * return.
 1027                          */
 1028                         return;
 1029                 }
 1030                 break;
 1031         }
 1032         }
 1033         xpt_release_ccb(done_ccb);
 1034 }
 1035 
 1036 static int
 1037 passcreatezone(struct cam_periph *periph)
 1038 {
 1039         struct pass_softc *softc;
 1040         int error;
 1041 
 1042         error = 0;
 1043         softc = (struct pass_softc *)periph->softc;
 1044 
 1045         cam_periph_assert(periph, MA_OWNED);
 1046         KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 
 1047                 ("%s called when the pass(4) zone is valid!\n", __func__));
 1048         KASSERT((softc->pass_zone == NULL), 
 1049                 ("%s called when the pass(4) zone is allocated!\n", __func__));
 1050 
 1051         if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) {
 1052                 /*
 1053                  * We're the first context through, so we need to create
 1054                  * the pass(4) UMA zone for I/O requests.
 1055                  */
 1056                 softc->flags |= PASS_FLAG_ZONE_INPROG;
 1057 
 1058                 /*
 1059                  * uma_zcreate() does a blocking (M_WAITOK) allocation,
 1060                  * so we cannot hold a mutex while we call it.
 1061                  */
 1062                 cam_periph_unlock(periph);
 1063 
 1064                 softc->pass_zone = uma_zcreate(softc->zone_name,
 1065                     sizeof(struct pass_io_req), NULL, NULL, NULL, NULL,
 1066                     /*align*/ 0, /*flags*/ 0);
 1067 
 1068                 softc->pass_io_zone = uma_zcreate(softc->io_zone_name,
 1069                     softc->io_zone_size, NULL, NULL, NULL, NULL,
 1070                     /*align*/ 0, /*flags*/ 0);
 1071 
 1072                 cam_periph_lock(periph);
 1073 
 1074                 if ((softc->pass_zone == NULL)
 1075                  || (softc->pass_io_zone == NULL)) {
 1076                         if (softc->pass_zone == NULL)
 1077                                 xpt_print(periph->path, "unable to allocate "
 1078                                     "IO Req UMA zone\n");
 1079                         else
 1080                                 xpt_print(periph->path, "unable to allocate "
 1081                                     "IO UMA zone\n");
 1082                         softc->flags &= ~PASS_FLAG_ZONE_INPROG;
 1083                         goto bailout;
 1084                 }
 1085 
 1086                 /*
 1087                  * Set the flags appropriately and notify any other waiters.
 1088                  */
 1089                 softc->flags &= ~PASS_FLAG_ZONE_INPROG;
 1090                 softc->flags |= PASS_FLAG_ZONE_VALID;
 1091                 wakeup(&softc->pass_zone);
 1092         } else {
 1093                 /*
 1094                  * In this case, the UMA zone has not yet been created, but
 1095                  * another context is in the process of creating it.  We
 1096                  * need to sleep until the creation is either done or has
 1097                  * failed.
 1098                  */
 1099                 while ((softc->flags & PASS_FLAG_ZONE_INPROG)
 1100                     && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) {
 1101                         error = msleep(&softc->pass_zone,
 1102                                        cam_periph_mtx(periph), PRIBIO,
 1103                                        "paszon", 0);
 1104                         if (error != 0)
 1105                                 goto bailout;
 1106                 }
 1107                 /*
 1108                  * If the zone creation failed, no luck for the user.
 1109                  */
 1110                 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){
 1111                         error = ENOMEM;
 1112                         goto bailout;
 1113                 }
 1114         }
 1115 bailout:
 1116         return (error);
 1117 }
 1118 
 1119 static void
 1120 passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req)
 1121 {
 1122         union ccb *ccb;
 1123         u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
 1124         int i, numbufs;
 1125 
 1126         ccb = &io_req->ccb;
 1127 
 1128         switch (ccb->ccb_h.func_code) {
 1129         case XPT_DEV_MATCH:
 1130                 numbufs = min(io_req->num_bufs, 2);
 1131 
 1132                 if (numbufs == 1) {
 1133                         data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
 1134                 } else {
 1135                         data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
 1136                         data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
 1137                 }
 1138                 break;
 1139         case XPT_SCSI_IO:
 1140         case XPT_CONT_TARGET_IO:
 1141                 data_ptrs[0] = &ccb->csio.data_ptr;
 1142                 numbufs = min(io_req->num_bufs, 1);
 1143                 break;
 1144         case XPT_ATA_IO:
 1145                 data_ptrs[0] = &ccb->ataio.data_ptr;
 1146                 numbufs = min(io_req->num_bufs, 1);
 1147                 break;
 1148         case XPT_SMP_IO:
 1149                 numbufs = min(io_req->num_bufs, 2);
 1150                 data_ptrs[0] = &ccb->smpio.smp_request;
 1151                 data_ptrs[1] = &ccb->smpio.smp_response;
 1152                 break;
 1153         case XPT_DEV_ADVINFO:
 1154                 numbufs = min(io_req->num_bufs, 1);
 1155                 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
 1156                 break;
 1157         case XPT_NVME_IO:
 1158         case XPT_NVME_ADMIN:
 1159                 data_ptrs[0] = &ccb->nvmeio.data_ptr;
 1160                 numbufs = min(io_req->num_bufs, 1);
 1161                 break;
 1162         default:
 1163                 /* allow ourselves to be swapped once again */
 1164                 return;
 1165                 break; /* NOTREACHED */ 
 1166         }
 1167 
 1168         if (io_req->flags & PASS_IO_USER_SEG_MALLOC) {
 1169                 free(io_req->user_segptr, M_SCSIPASS);
 1170                 io_req->user_segptr = NULL;
 1171         }
 1172 
 1173         /*
 1174          * We only want to free memory we malloced.
 1175          */
 1176         if (io_req->data_flags == CAM_DATA_VADDR) {
 1177                 for (i = 0; i < io_req->num_bufs; i++) {
 1178                         if (io_req->kern_bufs[i] == NULL)
 1179                                 continue;
 1180 
 1181                         free(io_req->kern_bufs[i], M_SCSIPASS);
 1182                         io_req->kern_bufs[i] = NULL;
 1183                 }
 1184         } else if (io_req->data_flags == CAM_DATA_SG) {
 1185                 for (i = 0; i < io_req->num_kern_segs; i++) {
 1186                         if ((uint8_t *)(uintptr_t)
 1187                             io_req->kern_segptr[i].ds_addr == NULL)
 1188                                 continue;
 1189 
 1190                         uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t)
 1191                             io_req->kern_segptr[i].ds_addr);
 1192                         io_req->kern_segptr[i].ds_addr = 0;
 1193                 }
 1194         }
 1195 
 1196         if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) {
 1197                 free(io_req->kern_segptr, M_SCSIPASS);
 1198                 io_req->kern_segptr = NULL;
 1199         }
 1200 
 1201         if (io_req->data_flags != CAM_DATA_PADDR) {
 1202                 for (i = 0; i < numbufs; i++) {
 1203                         /*
 1204                          * Restore the user's buffer pointers to their
 1205                          * previous values.
 1206                          */
 1207                         if (io_req->user_bufs[i] != NULL)
 1208                                 *data_ptrs[i] = io_req->user_bufs[i];
 1209                 }
 1210         }
 1211 
 1212 }
 1213 
 1214 static int
 1215 passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req,
 1216                ccb_flags direction)
 1217 {
 1218         bus_size_t kern_watermark, user_watermark, len_to_copy;
 1219         bus_dma_segment_t *user_sglist, *kern_sglist;
 1220         int i, j, error;
 1221 
 1222         error = 0;
 1223         kern_watermark = 0;
 1224         user_watermark = 0;
 1225         len_to_copy = 0;
 1226         user_sglist = io_req->user_segptr;
 1227         kern_sglist = io_req->kern_segptr;
 1228 
 1229         for (i = 0, j = 0; i < io_req->num_user_segs &&
 1230              j < io_req->num_kern_segs;) {
 1231                 uint8_t *user_ptr, *kern_ptr;
 1232 
 1233                 len_to_copy = min(user_sglist[i].ds_len -user_watermark,
 1234                     kern_sglist[j].ds_len - kern_watermark);
 1235 
 1236                 user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr;
 1237                 user_ptr = user_ptr + user_watermark;
 1238                 kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr;
 1239                 kern_ptr = kern_ptr + kern_watermark;
 1240 
 1241                 user_watermark += len_to_copy;
 1242                 kern_watermark += len_to_copy;
 1243 
 1244                 if (direction == CAM_DIR_IN) {
 1245                         error = copyout(kern_ptr, user_ptr, len_to_copy);
 1246                         if (error != 0) {
 1247                                 xpt_print(periph->path, "%s: copyout of %u "
 1248                                           "bytes from %p to %p failed with "
 1249                                           "error %d\n", __func__, len_to_copy,
 1250                                           kern_ptr, user_ptr, error);
 1251                                 goto bailout;
 1252                         }
 1253                 } else {
 1254                         error = copyin(user_ptr, kern_ptr, len_to_copy);
 1255                         if (error != 0) {
 1256                                 xpt_print(periph->path, "%s: copyin of %u "
 1257                                           "bytes from %p to %p failed with "
 1258                                           "error %d\n", __func__, len_to_copy,
 1259                                           user_ptr, kern_ptr, error);
 1260                                 goto bailout;
 1261                         }
 1262                 }
 1263 
 1264                 if (user_sglist[i].ds_len == user_watermark) {
 1265                         i++;
 1266                         user_watermark = 0;
 1267                 }
 1268 
 1269                 if (kern_sglist[j].ds_len == kern_watermark) {
 1270                         j++;
 1271                         kern_watermark = 0;
 1272                 }
 1273         }
 1274 
 1275 bailout:
 1276 
 1277         return (error);
 1278 }
 1279 
 1280 static int
 1281 passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req)
 1282 {
 1283         union ccb *ccb;
 1284         struct pass_softc *softc;
 1285         int numbufs, i;
 1286         uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
 1287         uint32_t lengths[CAM_PERIPH_MAXMAPS];
 1288         uint32_t dirs[CAM_PERIPH_MAXMAPS];
 1289         uint32_t num_segs;
 1290         uint16_t *seg_cnt_ptr;
 1291         size_t maxmap;
 1292         int error;
 1293 
 1294         cam_periph_assert(periph, MA_NOTOWNED);
 1295 
 1296         softc = periph->softc;
 1297 
 1298         error = 0;
 1299         ccb = &io_req->ccb;
 1300         maxmap = 0;
 1301         num_segs = 0;
 1302         seg_cnt_ptr = NULL;
 1303 
 1304         switch(ccb->ccb_h.func_code) {
 1305         case XPT_DEV_MATCH:
 1306                 if (ccb->cdm.match_buf_len == 0) {
 1307                         printf("%s: invalid match buffer length 0\n", __func__);
 1308                         return(EINVAL);
 1309                 }
 1310                 if (ccb->cdm.pattern_buf_len > 0) {
 1311                         data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
 1312                         lengths[0] = ccb->cdm.pattern_buf_len;
 1313                         dirs[0] = CAM_DIR_OUT;
 1314                         data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
 1315                         lengths[1] = ccb->cdm.match_buf_len;
 1316                         dirs[1] = CAM_DIR_IN;
 1317                         numbufs = 2;
 1318                 } else {
 1319                         data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
 1320                         lengths[0] = ccb->cdm.match_buf_len;
 1321                         dirs[0] = CAM_DIR_IN;
 1322                         numbufs = 1;
 1323                 }
 1324                 io_req->data_flags = CAM_DATA_VADDR;
 1325                 break;
 1326         case XPT_SCSI_IO:
 1327         case XPT_CONT_TARGET_IO:
 1328                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
 1329                         return(0);
 1330 
 1331                 /*
 1332                  * The user shouldn't be able to supply a bio.
 1333                  */
 1334                 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
 1335                         return (EINVAL);
 1336 
 1337                 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
 1338 
 1339                 data_ptrs[0] = &ccb->csio.data_ptr;
 1340                 lengths[0] = ccb->csio.dxfer_len;
 1341                 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
 1342                 num_segs = ccb->csio.sglist_cnt;
 1343                 seg_cnt_ptr = &ccb->csio.sglist_cnt;
 1344                 numbufs = 1;
 1345                 maxmap = softc->maxio;
 1346                 break;
 1347         case XPT_ATA_IO:
 1348                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
 1349                         return(0);
 1350 
 1351                 /*
 1352                  * We only support a single virtual address for ATA I/O.
 1353                  */
 1354                 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
 1355                         return (EINVAL);
 1356 
 1357                 io_req->data_flags = CAM_DATA_VADDR;
 1358 
 1359                 data_ptrs[0] = &ccb->ataio.data_ptr;
 1360                 lengths[0] = ccb->ataio.dxfer_len;
 1361                 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
 1362                 numbufs = 1;
 1363                 maxmap = softc->maxio;
 1364                 break;
 1365         case XPT_SMP_IO:
 1366                 io_req->data_flags = CAM_DATA_VADDR;
 1367 
 1368                 data_ptrs[0] = &ccb->smpio.smp_request;
 1369                 lengths[0] = ccb->smpio.smp_request_len;
 1370                 dirs[0] = CAM_DIR_OUT;
 1371                 data_ptrs[1] = &ccb->smpio.smp_response;
 1372                 lengths[1] = ccb->smpio.smp_response_len;
 1373                 dirs[1] = CAM_DIR_IN;
 1374                 numbufs = 2;
 1375                 maxmap = softc->maxio;
 1376                 break;
 1377         case XPT_DEV_ADVINFO:
 1378                 if (ccb->cdai.bufsiz == 0)
 1379                         return (0);
 1380 
 1381                 io_req->data_flags = CAM_DATA_VADDR;
 1382 
 1383                 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
 1384                 lengths[0] = ccb->cdai.bufsiz;
 1385                 dirs[0] = CAM_DIR_IN;
 1386                 numbufs = 1;
 1387                 break;
 1388         case XPT_NVME_ADMIN:
 1389         case XPT_NVME_IO:
 1390                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
 1391                         return (0);
 1392 
 1393                 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
 1394 
 1395                 data_ptrs[0] = &ccb->nvmeio.data_ptr;
 1396                 lengths[0] = ccb->nvmeio.dxfer_len;
 1397                 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
 1398                 num_segs = ccb->nvmeio.sglist_cnt;
 1399                 seg_cnt_ptr = &ccb->nvmeio.sglist_cnt;
 1400                 numbufs = 1;
 1401                 maxmap = softc->maxio;
 1402                 break;
 1403         default:
 1404                 return(EINVAL);
 1405                 break; /* NOTREACHED */
 1406         }
 1407 
 1408         io_req->num_bufs = numbufs;
 1409 
 1410         /*
 1411          * If there is a maximum, check to make sure that the user's
 1412          * request fits within the limit.  In general, we should only have
 1413          * a maximum length for requests that go to hardware.  Otherwise it
 1414          * is whatever we're able to malloc.
 1415          */
 1416         for (i = 0; i < numbufs; i++) {
 1417                 io_req->user_bufs[i] = *data_ptrs[i];
 1418                 io_req->dirs[i] = dirs[i];
 1419                 io_req->lengths[i] = lengths[i];
 1420 
 1421                 if (maxmap == 0)
 1422                         continue;
 1423 
 1424                 if (lengths[i] <= maxmap)
 1425                         continue;
 1426 
 1427                 xpt_print(periph->path, "%s: data length %u > max allowed %u "
 1428                           "bytes\n", __func__, lengths[i], maxmap);
 1429                 error = EINVAL;
 1430                 goto bailout;
 1431         }
 1432 
 1433         switch (io_req->data_flags) {
 1434         case CAM_DATA_VADDR:
 1435                 /* Map or copy the buffer into kernel address space */
 1436                 for (i = 0; i < numbufs; i++) {
 1437                         uint8_t *tmp_buf;
 1438 
 1439                         /*
 1440                          * If for some reason no length is specified, we
 1441                          * don't need to allocate anything.
 1442                          */
 1443                         if (io_req->lengths[i] == 0)
 1444                                 continue;
 1445 
 1446                         tmp_buf = malloc(lengths[i], M_SCSIPASS,
 1447                                          M_WAITOK | M_ZERO);
 1448                         io_req->kern_bufs[i] = tmp_buf;
 1449                         *data_ptrs[i] = tmp_buf;
 1450 
 1451 #if 0
 1452                         xpt_print(periph->path, "%s: malloced %p len %u, user "
 1453                                   "buffer %p, operation: %s\n", __func__,
 1454                                   tmp_buf, lengths[i], io_req->user_bufs[i],
 1455                                   (dirs[i] == CAM_DIR_IN) ? "read" : "write");
 1456 #endif
 1457                         /*
 1458                          * We only need to copy in if the user is writing.
 1459                          */
 1460                         if (dirs[i] != CAM_DIR_OUT)
 1461                                 continue;
 1462 
 1463                         error = copyin(io_req->user_bufs[i],
 1464                                        io_req->kern_bufs[i], lengths[i]);
 1465                         if (error != 0) {
 1466                                 xpt_print(periph->path, "%s: copy of user "
 1467                                           "buffer from %p to %p failed with "
 1468                                           "error %d\n", __func__,
 1469                                           io_req->user_bufs[i],
 1470                                           io_req->kern_bufs[i], error);
 1471                                 goto bailout;
 1472                         }
 1473                 }
 1474                 break;
 1475         case CAM_DATA_PADDR:
 1476                 /* Pass down the pointer as-is */
 1477                 break;
 1478         case CAM_DATA_SG: {
 1479                 size_t sg_length, size_to_go, alloc_size;
 1480                 uint32_t num_segs_needed;
 1481 
 1482                 /*
 1483                  * Copy the user S/G list in, and then copy in the
 1484                  * individual segments.
 1485                  */
 1486                 /*
 1487                  * We shouldn't see this, but check just in case.
 1488                  */
 1489                 if (numbufs != 1) {
 1490                         xpt_print(periph->path, "%s: cannot currently handle "
 1491                                   "more than one S/G list per CCB\n", __func__);
 1492                         error = EINVAL;
 1493                         goto bailout;
 1494                 }
 1495 
 1496                 /*
 1497                  * We have to have at least one segment.
 1498                  */
 1499                 if (num_segs == 0) {
 1500                         xpt_print(periph->path, "%s: CAM_DATA_SG flag set, "
 1501                                   "but sglist_cnt=0!\n", __func__);
 1502                         error = EINVAL;
 1503                         goto bailout;
 1504                 }
 1505 
 1506                 /*
 1507                  * Make sure the user specified the total length and didn't
 1508                  * just leave it to us to decode the S/G list.
 1509                  */
 1510                 if (lengths[0] == 0) {
 1511                         xpt_print(periph->path, "%s: no dxfer_len specified, "
 1512                                   "but CAM_DATA_SG flag is set!\n", __func__);
 1513                         error = EINVAL;
 1514                         goto bailout;
 1515                 }
 1516 
 1517                 /*
 1518                  * We allocate buffers in io_zone_size increments for an
 1519                  * S/G list.  This will generally be maxphys.
 1520                  */
 1521                 if (lengths[0] <= softc->io_zone_size)
 1522                         num_segs_needed = 1;
 1523                 else {
 1524                         num_segs_needed = lengths[0] / softc->io_zone_size;
 1525                         if ((lengths[0] % softc->io_zone_size) != 0)
 1526                                 num_segs_needed++;
 1527                 }
 1528 
 1529                 /* Figure out the size of the S/G list */
 1530                 sg_length = num_segs * sizeof(bus_dma_segment_t);
 1531                 io_req->num_user_segs = num_segs;
 1532                 io_req->num_kern_segs = num_segs_needed;
 1533 
 1534                 /* Save the user's S/G list pointer for later restoration */
 1535                 io_req->user_bufs[0] = *data_ptrs[0];
 1536 
 1537                 /*
 1538                  * If we have enough segments allocated by default to handle
 1539                  * the length of the user's S/G list,
 1540                  */
 1541                 if (num_segs > PASS_MAX_SEGS) {
 1542                         io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
 1543                             num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
 1544                         io_req->flags |= PASS_IO_USER_SEG_MALLOC;
 1545                 } else
 1546                         io_req->user_segptr = io_req->user_segs;
 1547 
 1548                 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
 1549                 if (error != 0) {
 1550                         xpt_print(periph->path, "%s: copy of user S/G list "
 1551                                   "from %p to %p failed with error %d\n",
 1552                                   __func__, *data_ptrs[0], io_req->user_segptr,
 1553                                   error);
 1554                         goto bailout;
 1555                 }
 1556 
 1557                 if (num_segs_needed > PASS_MAX_SEGS) {
 1558                         io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) *
 1559                             num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO);
 1560                         io_req->flags |= PASS_IO_KERN_SEG_MALLOC;
 1561                 } else {
 1562                         io_req->kern_segptr = io_req->kern_segs;
 1563                 }
 1564 
 1565                 /*
 1566                  * Allocate the kernel S/G list.
 1567                  */
 1568                 for (size_to_go = lengths[0], i = 0;
 1569                      size_to_go > 0 && i < num_segs_needed;
 1570                      i++, size_to_go -= alloc_size) {
 1571                         uint8_t *kern_ptr;
 1572 
 1573                         alloc_size = min(size_to_go, softc->io_zone_size);
 1574                         kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK);
 1575                         io_req->kern_segptr[i].ds_addr =
 1576                             (bus_addr_t)(uintptr_t)kern_ptr;
 1577                         io_req->kern_segptr[i].ds_len = alloc_size;
 1578                 }
 1579                 if (size_to_go > 0) {
 1580                         printf("%s: size_to_go = %zu, software error!\n",
 1581                                __func__, size_to_go);
 1582                         error = EINVAL;
 1583                         goto bailout;
 1584                 }
 1585 
 1586                 *data_ptrs[0] = (uint8_t *)io_req->kern_segptr;
 1587                 *seg_cnt_ptr = io_req->num_kern_segs;
 1588 
 1589                 /*
 1590                  * We only need to copy data here if the user is writing.
 1591                  */
 1592                 if (dirs[0] == CAM_DIR_OUT)
 1593                         error = passcopysglist(periph, io_req, dirs[0]);
 1594                 break;
 1595         }
 1596         case CAM_DATA_SG_PADDR: {
 1597                 size_t sg_length;
 1598 
 1599                 /*
 1600                  * We shouldn't see this, but check just in case.
 1601                  */
 1602                 if (numbufs != 1) {
 1603                         printf("%s: cannot currently handle more than one "
 1604                                "S/G list per CCB\n", __func__);
 1605                         error = EINVAL;
 1606                         goto bailout;
 1607                 }
 1608 
 1609                 /*
 1610                  * We have to have at least one segment.
 1611                  */
 1612                 if (num_segs == 0) {
 1613                         xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag "
 1614                                   "set, but sglist_cnt=0!\n", __func__);
 1615                         error = EINVAL;
 1616                         goto bailout;
 1617                 }
 1618 
 1619                 /*
 1620                  * Make sure the user specified the total length and didn't
 1621                  * just leave it to us to decode the S/G list.
 1622                  */
 1623                 if (lengths[0] == 0) {
 1624                         xpt_print(periph->path, "%s: no dxfer_len specified, "
 1625                                   "but CAM_DATA_SG flag is set!\n", __func__);
 1626                         error = EINVAL;
 1627                         goto bailout;
 1628                 }
 1629 
 1630                 /* Figure out the size of the S/G list */
 1631                 sg_length = num_segs * sizeof(bus_dma_segment_t);
 1632                 io_req->num_user_segs = num_segs;
 1633                 io_req->num_kern_segs = io_req->num_user_segs;
 1634 
 1635                 /* Save the user's S/G list pointer for later restoration */
 1636                 io_req->user_bufs[0] = *data_ptrs[0];
 1637 
 1638                 if (num_segs > PASS_MAX_SEGS) {
 1639                         io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
 1640                             num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
 1641                         io_req->flags |= PASS_IO_USER_SEG_MALLOC;
 1642                 } else
 1643                         io_req->user_segptr = io_req->user_segs;
 1644 
 1645                 io_req->kern_segptr = io_req->user_segptr;
 1646 
 1647                 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
 1648                 if (error != 0) {
 1649                         xpt_print(periph->path, "%s: copy of user S/G list "
 1650                                   "from %p to %p failed with error %d\n",
 1651                                   __func__, *data_ptrs[0], io_req->user_segptr,
 1652                                   error);
 1653                         goto bailout;
 1654                 }
 1655                 break;
 1656         }
 1657         default:
 1658         case CAM_DATA_BIO:
 1659                 /*
 1660                  * A user shouldn't be attaching a bio to the CCB.  It
 1661                  * isn't a user-accessible structure.
 1662                  */
 1663                 error = EINVAL;
 1664                 break;
 1665         }
 1666 
 1667 bailout:
 1668         if (error != 0)
 1669                 passiocleanup(softc, io_req);
 1670 
 1671         return (error);
 1672 }
 1673 
 1674 static int
 1675 passmemdone(struct cam_periph *periph, struct pass_io_req *io_req)
 1676 {
 1677         struct pass_softc *softc;
 1678         int error;
 1679         int i;
 1680 
 1681         error = 0;
 1682         softc = (struct pass_softc *)periph->softc;
 1683 
 1684         switch (io_req->data_flags) {
 1685         case CAM_DATA_VADDR:
 1686                 /*
 1687                  * Copy back to the user buffer if this was a read.
 1688                  */
 1689                 for (i = 0; i < io_req->num_bufs; i++) {
 1690                         if (io_req->dirs[i] != CAM_DIR_IN)
 1691                                 continue;
 1692 
 1693                         error = copyout(io_req->kern_bufs[i],
 1694                             io_req->user_bufs[i], io_req->lengths[i]);
 1695                         if (error != 0) {
 1696                                 xpt_print(periph->path, "Unable to copy %u "
 1697                                           "bytes from %p to user address %p\n",
 1698                                           io_req->lengths[i],
 1699                                           io_req->kern_bufs[i],
 1700                                           io_req->user_bufs[i]);
 1701                                 goto bailout;
 1702                         }
 1703                 }
 1704                 break;
 1705         case CAM_DATA_PADDR:
 1706                 /* Do nothing.  The pointer is a physical address already */
 1707                 break;
 1708         case CAM_DATA_SG:
 1709                 /*
 1710                  * Copy back to the user buffer if this was a read.
 1711                  * Restore the user's S/G list buffer pointer.
 1712                  */
 1713                 if (io_req->dirs[0] == CAM_DIR_IN)
 1714                         error = passcopysglist(periph, io_req, io_req->dirs[0]);
 1715                 break;
 1716         case CAM_DATA_SG_PADDR:
 1717                 /*
 1718                  * Restore the user's S/G list buffer pointer.  No need to
 1719                  * copy.
 1720                  */
 1721                 break;
 1722         default:
 1723         case CAM_DATA_BIO:
 1724                 error = EINVAL;
 1725                 break;
 1726         }
 1727 
 1728 bailout:
 1729         /*
 1730          * Reset the user's pointers to their original values and free
 1731          * allocated memory.
 1732          */
 1733         passiocleanup(softc, io_req);
 1734 
 1735         return (error);
 1736 }
 1737 
 1738 static int
 1739 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
 1740 {
 1741         int error;
 1742 
 1743         if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
 1744                 error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl);
 1745         }
 1746         return (error);
 1747 }
 1748 
 1749 static int
 1750 passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
 1751 {
 1752         struct  cam_periph *periph;
 1753         struct  pass_softc *softc;
 1754         int     error;
 1755         uint32_t priority;
 1756 
 1757         periph = (struct cam_periph *)dev->si_drv1;
 1758         cam_periph_lock(periph);
 1759         softc = (struct pass_softc *)periph->softc;
 1760 
 1761         error = 0;
 1762 
 1763         switch (cmd) {
 1764         case CAMIOCOMMAND:
 1765         {
 1766                 union ccb *inccb;
 1767                 union ccb *ccb;
 1768                 int ccb_malloced;
 1769 
 1770                 inccb = (union ccb *)addr;
 1771 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
 1772                 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
 1773                         inccb->csio.bio = NULL;
 1774 #endif
 1775 
 1776                 if (inccb->ccb_h.flags & CAM_UNLOCKED) {
 1777                         error = EINVAL;
 1778                         break;
 1779                 }
 1780 
 1781                 /*
 1782                  * Some CCB types, like scan bus and scan lun can only go
 1783                  * through the transport layer device.
 1784                  */
 1785                 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
 1786                         xpt_print(periph->path, "CCB function code %#x is "
 1787                             "restricted to the XPT device\n",
 1788                             inccb->ccb_h.func_code);
 1789                         error = ENODEV;
 1790                         break;
 1791                 }
 1792 
 1793                 /* Compatibility for RL/priority-unaware code. */
 1794                 priority = inccb->ccb_h.pinfo.priority;
 1795                 if (priority <= CAM_PRIORITY_OOB)
 1796                     priority += CAM_PRIORITY_OOB + 1;
 1797 
 1798                 /*
 1799                  * Non-immediate CCBs need a CCB from the per-device pool
 1800                  * of CCBs, which is scheduled by the transport layer.
 1801                  * Immediate CCBs and user-supplied CCBs should just be
 1802                  * malloced.
 1803                  */
 1804                 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
 1805                  && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
 1806                         ccb = cam_periph_getccb(periph, priority);
 1807                         ccb_malloced = 0;
 1808                 } else {
 1809                         ccb = xpt_alloc_ccb_nowait();
 1810 
 1811                         if (ccb != NULL)
 1812                                 xpt_setup_ccb(&ccb->ccb_h, periph->path,
 1813                                               priority);
 1814                         ccb_malloced = 1;
 1815                 }
 1816 
 1817                 if (ccb == NULL) {
 1818                         xpt_print(periph->path, "unable to allocate CCB\n");
 1819                         error = ENOMEM;
 1820                         break;
 1821                 }
 1822 
 1823                 error = passsendccb(periph, ccb, inccb);
 1824 
 1825                 if (ccb_malloced)
 1826                         xpt_free_ccb(ccb);
 1827                 else
 1828                         xpt_release_ccb(ccb);
 1829 
 1830                 break;
 1831         }
 1832         case CAMIOQUEUE:
 1833         {
 1834                 struct pass_io_req *io_req;
 1835                 union ccb **user_ccb, *ccb;
 1836                 xpt_opcode fc;
 1837 
 1838 #ifdef COMPAT_FREEBSD32
 1839                 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
 1840                         error = ENOTTY;
 1841                         goto bailout;
 1842                 }
 1843 #endif
 1844                 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) {
 1845                         error = passcreatezone(periph);
 1846                         if (error != 0)
 1847                                 goto bailout;
 1848                 }
 1849 
 1850                 /*
 1851                  * We're going to do a blocking allocation for this I/O
 1852                  * request, so we have to drop the lock.
 1853                  */
 1854                 cam_periph_unlock(periph);
 1855 
 1856                 io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO);
 1857                 ccb = &io_req->ccb;
 1858                 user_ccb = (union ccb **)addr;
 1859 
 1860                 /*
 1861                  * Unlike the CAMIOCOMMAND ioctl above, we only have a
 1862                  * pointer to the user's CCB, so we have to copy the whole
 1863                  * thing in to a buffer we have allocated (above) instead
 1864                  * of allowing the ioctl code to malloc a buffer and copy
 1865                  * it in.
 1866                  *
 1867                  * This is an advantage for this asynchronous interface,
 1868                  * since we don't want the memory to get freed while the
 1869                  * CCB is outstanding.
 1870                  */
 1871 #if 0
 1872                 xpt_print(periph->path, "Copying user CCB %p to "
 1873                           "kernel address %p\n", *user_ccb, ccb);
 1874 #endif
 1875                 error = copyin(*user_ccb, ccb, sizeof(*ccb));
 1876                 if (error != 0) {
 1877                         xpt_print(periph->path, "Copy of user CCB %p to "
 1878                                   "kernel address %p failed with error %d\n",
 1879                                   *user_ccb, ccb, error);
 1880                         goto camioqueue_error;
 1881                 }
 1882 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
 1883                 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
 1884                         ccb->csio.bio = NULL;
 1885 #endif
 1886 
 1887                 if (ccb->ccb_h.flags & CAM_UNLOCKED) {
 1888                         error = EINVAL;
 1889                         goto camioqueue_error;
 1890                 }
 1891 
 1892                 if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
 1893                         if (ccb->csio.cdb_len > IOCDBLEN) {
 1894                                 error = EINVAL;
 1895                                 goto camioqueue_error;
 1896                         }
 1897                         error = copyin(ccb->csio.cdb_io.cdb_ptr,
 1898                             ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len);
 1899                         if (error != 0)
 1900                                 goto camioqueue_error;
 1901                         ccb->ccb_h.flags &= ~CAM_CDB_POINTER;
 1902                 }
 1903 
 1904                 /*
 1905                  * Some CCB types, like scan bus and scan lun can only go
 1906                  * through the transport layer device.
 1907                  */
 1908                 if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
 1909                         xpt_print(periph->path, "CCB function code %#x is "
 1910                             "restricted to the XPT device\n",
 1911                             ccb->ccb_h.func_code);
 1912                         error = ENODEV;
 1913                         goto camioqueue_error;
 1914                 }
 1915 
 1916                 /*
 1917                  * Save the user's CCB pointer as well as his linked list
 1918                  * pointers and peripheral private area so that we can
 1919                  * restore these later.
 1920                  */
 1921                 io_req->user_ccb_ptr = *user_ccb;
 1922                 io_req->user_periph_links = ccb->ccb_h.periph_links;
 1923                 io_req->user_periph_priv = ccb->ccb_h.periph_priv;
 1924 
 1925                 /*
 1926                  * Now that we've saved the user's values, we can set our
 1927                  * own peripheral private entry.
 1928                  */
 1929                 ccb->ccb_h.ccb_ioreq = io_req;
 1930 
 1931                 /* Compatibility for RL/priority-unaware code. */
 1932                 priority = ccb->ccb_h.pinfo.priority;
 1933                 if (priority <= CAM_PRIORITY_OOB)
 1934                     priority += CAM_PRIORITY_OOB + 1;
 1935 
 1936                 /*
 1937                  * Setup fields in the CCB like the path and the priority.
 1938                  * The path in particular cannot be done in userland, since
 1939                  * it is a pointer to a kernel data structure.
 1940                  */
 1941                 xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority,
 1942                                     ccb->ccb_h.flags);
 1943 
 1944                 /*
 1945                  * Setup our done routine.  There is no way for the user to
 1946                  * have a valid pointer here.
 1947                  */
 1948                 ccb->ccb_h.cbfcnp = passdone;
 1949 
 1950                 fc = ccb->ccb_h.func_code;
 1951                 /*
 1952                  * If this function code has memory that can be mapped in
 1953                  * or out, we need to call passmemsetup().
 1954                  */
 1955                 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO)
 1956                  || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH)
 1957                  || (fc == XPT_DEV_ADVINFO)
 1958                  || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
 1959                         error = passmemsetup(periph, io_req);
 1960                         if (error != 0)
 1961                                 goto camioqueue_error;
 1962                 } else
 1963                         io_req->mapinfo.num_bufs_used = 0;
 1964 
 1965                 cam_periph_lock(periph);
 1966 
 1967                 /*
 1968                  * Everything goes on the incoming queue initially.
 1969                  */
 1970                 TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links);
 1971 
 1972                 /*
 1973                  * If the CCB is queued, and is not a user CCB, then
 1974                  * we need to allocate a slot for it.  Call xpt_schedule()
 1975                  * so that our start routine will get called when a CCB is
 1976                  * available.
 1977                  */
 1978                 if ((fc & XPT_FC_QUEUED)
 1979                  && ((fc & XPT_FC_USER_CCB) == 0)) {
 1980                         xpt_schedule(periph, priority);
 1981                         break;
 1982                 } 
 1983 
 1984                 /*
 1985                  * At this point, the CCB in question is either an
 1986                  * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB
 1987                  * and therefore should be malloced, not allocated via a slot.
 1988                  * Remove the CCB from the incoming queue and add it to the
 1989                  * active queue.
 1990                  */
 1991                 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
 1992                 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
 1993 
 1994                 xpt_action(ccb);
 1995 
 1996                 /*
 1997                  * If this is not a queued CCB (i.e. it is an immediate CCB),
 1998                  * then it is already done.  We need to put it on the done
 1999                  * queue for the user to fetch.
 2000                  */
 2001                 if ((fc & XPT_FC_QUEUED) == 0) {
 2002                         TAILQ_REMOVE(&softc->active_queue, io_req, links);
 2003                         TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
 2004                 }
 2005                 break;
 2006 
 2007 camioqueue_error:
 2008                 uma_zfree(softc->pass_zone, io_req);
 2009                 cam_periph_lock(periph);
 2010                 break;
 2011         }
 2012         case CAMIOGET:
 2013         {
 2014                 union ccb **user_ccb;
 2015                 struct pass_io_req *io_req;
 2016                 int old_error;
 2017 
 2018 #ifdef COMPAT_FREEBSD32
 2019                 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
 2020                         error = ENOTTY;
 2021                         goto bailout;
 2022                 }
 2023 #endif
 2024                 user_ccb = (union ccb **)addr;
 2025                 old_error = 0;
 2026 
 2027                 io_req = TAILQ_FIRST(&softc->done_queue);
 2028                 if (io_req == NULL) {
 2029                         error = ENOENT;
 2030                         break;
 2031                 }
 2032 
 2033                 /*
 2034                  * Remove the I/O from the done queue.
 2035                  */
 2036                 TAILQ_REMOVE(&softc->done_queue, io_req, links);
 2037 
 2038                 /*
 2039                  * We have to drop the lock during the copyout because the
 2040                  * copyout can result in VM faults that require sleeping.
 2041                  */
 2042                 cam_periph_unlock(periph);
 2043 
 2044                 /*
 2045                  * Do any needed copies (e.g. for reads) and revert the
 2046                  * pointers in the CCB back to the user's pointers.
 2047                  */
 2048                 error = passmemdone(periph, io_req);
 2049 
 2050                 old_error = error;
 2051 
 2052                 io_req->ccb.ccb_h.periph_links = io_req->user_periph_links;
 2053                 io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv;
 2054 
 2055 #if 0
 2056                 xpt_print(periph->path, "Copying to user CCB %p from "
 2057                           "kernel address %p\n", *user_ccb, &io_req->ccb);
 2058 #endif
 2059 
 2060                 error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb));
 2061                 if (error != 0) {
 2062                         xpt_print(periph->path, "Copy to user CCB %p from "
 2063                                   "kernel address %p failed with error %d\n",
 2064                                   *user_ccb, &io_req->ccb, error);
 2065                 }
 2066 
 2067                 /*
 2068                  * Prefer the first error we got back, and make sure we
 2069                  * don't overwrite bad status with good.
 2070                  */
 2071                 if (old_error != 0)
 2072                         error = old_error;
 2073 
 2074                 cam_periph_lock(periph);
 2075 
 2076                 /*
 2077                  * At this point, if there was an error, we could potentially
 2078                  * re-queue the I/O and try again.  But why?  The error
 2079                  * would almost certainly happen again.  We might as well
 2080                  * not leak memory.
 2081                  */
 2082                 uma_zfree(softc->pass_zone, io_req);
 2083                 break;
 2084         }
 2085         default:
 2086                 error = cam_periph_ioctl(periph, cmd, addr, passerror);
 2087                 break;
 2088         }
 2089 
 2090 bailout:
 2091         cam_periph_unlock(periph);
 2092 
 2093         return(error);
 2094 }
 2095 
 2096 static int
 2097 passpoll(struct cdev *dev, int poll_events, struct thread *td)
 2098 {
 2099         struct cam_periph *periph;
 2100         struct pass_softc *softc;
 2101         int revents;
 2102 
 2103         periph = (struct cam_periph *)dev->si_drv1;
 2104         softc = (struct pass_softc *)periph->softc;
 2105 
 2106         revents = poll_events & (POLLOUT | POLLWRNORM);
 2107         if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
 2108                 cam_periph_lock(periph);
 2109 
 2110                 if (!TAILQ_EMPTY(&softc->done_queue)) {
 2111                         revents |= poll_events & (POLLIN | POLLRDNORM);
 2112                 }
 2113                 cam_periph_unlock(periph);
 2114                 if (revents == 0)
 2115                         selrecord(td, &softc->read_select);
 2116         }
 2117 
 2118         return (revents);
 2119 }
 2120 
 2121 static int
 2122 passkqfilter(struct cdev *dev, struct knote *kn)
 2123 {
 2124         struct cam_periph *periph;
 2125         struct pass_softc *softc;
 2126 
 2127         periph = (struct cam_periph *)dev->si_drv1;
 2128         softc = (struct pass_softc *)periph->softc;
 2129 
 2130         kn->kn_hook = (caddr_t)periph;
 2131         kn->kn_fop = &passread_filtops;
 2132         knlist_add(&softc->read_select.si_note, kn, 0);
 2133 
 2134         return (0);
 2135 }
 2136 
 2137 static void
 2138 passreadfiltdetach(struct knote *kn)
 2139 {
 2140         struct cam_periph *periph;
 2141         struct pass_softc *softc;
 2142 
 2143         periph = (struct cam_periph *)kn->kn_hook;
 2144         softc = (struct pass_softc *)periph->softc;
 2145 
 2146         knlist_remove(&softc->read_select.si_note, kn, 0);
 2147 }
 2148 
 2149 static int
 2150 passreadfilt(struct knote *kn, long hint)
 2151 {
 2152         struct cam_periph *periph;
 2153         struct pass_softc *softc;
 2154         int retval;
 2155 
 2156         periph = (struct cam_periph *)kn->kn_hook;
 2157         softc = (struct pass_softc *)periph->softc;
 2158 
 2159         cam_periph_assert(periph, MA_OWNED);
 2160 
 2161         if (TAILQ_EMPTY(&softc->done_queue))
 2162                 retval = 0;
 2163         else
 2164                 retval = 1;
 2165 
 2166         return (retval);
 2167 }
 2168 
 2169 /*
 2170  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
 2171  * should be the CCB that is copied in from the user.
 2172  */
 2173 static int
 2174 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
 2175 {
 2176         struct pass_softc *softc;
 2177         struct cam_periph_map_info mapinfo;
 2178         uint8_t *cmd;
 2179         xpt_opcode fc;
 2180         int error;
 2181 
 2182         softc = (struct pass_softc *)periph->softc;
 2183 
 2184         /*
 2185          * There are some fields in the CCB header that need to be
 2186          * preserved, the rest we get from the user.
 2187          */
 2188         xpt_merge_ccb(ccb, inccb);
 2189 
 2190         if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
 2191                 cmd = __builtin_alloca(ccb->csio.cdb_len);
 2192                 error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len);
 2193                 if (error)
 2194                         return (error);
 2195                 ccb->csio.cdb_io.cdb_ptr = cmd;
 2196         }
 2197 
 2198         /*
 2199          * Let cam_periph_mapmem do a sanity check on the data pointer format.
 2200          * Even if no data transfer is needed, it's a cheap check and it
 2201          * simplifies the code.
 2202          */
 2203         fc = ccb->ccb_h.func_code;
 2204         if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
 2205             || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO)
 2206             || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
 2207                 bzero(&mapinfo, sizeof(mapinfo));
 2208 
 2209                 /*
 2210                  * cam_periph_mapmem calls into proc and vm functions that can
 2211                  * sleep as well as trigger I/O, so we can't hold the lock.
 2212                  * Dropping it here is reasonably safe.
 2213                  */
 2214                 cam_periph_unlock(periph);
 2215                 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
 2216                 cam_periph_lock(periph);
 2217 
 2218                 /*
 2219                  * cam_periph_mapmem returned an error, we can't continue.
 2220                  * Return the error to the user.
 2221                  */
 2222                 if (error)
 2223                         return(error);
 2224         } else
 2225                 /* Ensure that the unmap call later on is a no-op. */
 2226                 mapinfo.num_bufs_used = 0;
 2227 
 2228         /*
 2229          * If the user wants us to perform any error recovery, then honor
 2230          * that request.  Otherwise, it's up to the user to perform any
 2231          * error recovery.
 2232          */
 2233         cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 
 2234             passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO,
 2235             /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
 2236             softc->device_stats);
 2237 
 2238         cam_periph_unlock(periph);
 2239         cam_periph_unmapmem(ccb, &mapinfo);
 2240         cam_periph_lock(periph);
 2241 
 2242         ccb->ccb_h.cbfcnp = NULL;
 2243         ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
 2244         bcopy(ccb, inccb, sizeof(union ccb));
 2245 
 2246         return(0);
 2247 }
 2248 
 2249 static int
 2250 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 2251 {
 2252 
 2253         return(cam_periph_error(ccb, cam_flags, sense_flags));
 2254 }

Cache object: 01a032cb212852aaa36da273fc8a30ea


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