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_ch.c

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

    1 /*-
    2  * Copyright (c) 1997 Justin T. Gibbs.
    3  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions, and the following disclaimer,
   11  *    without modification, immediately at the beginning of the file.
   12  * 2. The name of the author may not be used to endorse or promote products
   13  *    derived from this software without specific prior written permission.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 /*
   29  * Derived from the NetBSD SCSI changer driver.
   30  *
   31  *      $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
   32  *
   33  */
   34 /*-
   35  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
   36  * All rights reserved.
   37  *
   38  * Partially based on an autochanger driver written by Stefan Grefen
   39  * and on an autochanger driver written by the Systems Programming Group
   40  * at the University of Utah Computer Science Department.
   41  *
   42  * Redistribution and use in source and binary forms, with or without
   43  * modification, are permitted provided that the following conditions
   44  * are met:
   45  * 1. Redistributions of source code must retain the above copyright
   46  *    notice, this list of conditions and the following disclaimer.
   47  * 2. Redistributions in binary form must reproduce the above copyright
   48  *    notice, this list of conditions and the following disclaimer in the
   49  *    documentation and/or other materials provided with the distribution.
   50  * 3. All advertising materials mentioning features or use of this software
   51  *    must display the following acknowledgements:
   52  *      This product includes software developed by Jason R. Thorpe
   53  *      for And Communications, http://www.and.com/
   54  * 4. The name of the author may not be used to endorse or promote products
   55  *    derived from this software without specific prior written permission.
   56  *
   57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   62  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   64  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   65  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   67  * SUCH DAMAGE.
   68  */
   69 
   70 #include <sys/cdefs.h>
   71 __FBSDID("$FreeBSD$");
   72 
   73 #include <sys/param.h>
   74 #include <sys/queue.h>
   75 #include <sys/systm.h>
   76 #include <sys/kernel.h>
   77 #include <sys/types.h>
   78 #include <sys/malloc.h>
   79 #include <sys/fcntl.h>
   80 #include <sys/conf.h>
   81 #include <sys/chio.h>
   82 #include <sys/errno.h>
   83 #include <sys/devicestat.h>
   84 
   85 #include <cam/cam.h>
   86 #include <cam/cam_ccb.h>
   87 #include <cam/cam_periph.h>
   88 #include <cam/cam_xpt_periph.h>
   89 #include <cam/cam_debug.h>
   90 
   91 #include <cam/scsi/scsi_all.h>
   92 #include <cam/scsi/scsi_message.h>
   93 #include <cam/scsi/scsi_ch.h>
   94 
   95 /*
   96  * Timeout definitions for various changer related commands.  They may
   97  * be too short for some devices (especially the timeout for INITIALIZE
   98  * ELEMENT STATUS).
   99  */
  100 
  101 static const u_int32_t  CH_TIMEOUT_MODE_SENSE                = 6000;
  102 static const u_int32_t  CH_TIMEOUT_MOVE_MEDIUM               = 100000;
  103 static const u_int32_t  CH_TIMEOUT_EXCHANGE_MEDIUM           = 100000;
  104 static const u_int32_t  CH_TIMEOUT_POSITION_TO_ELEMENT       = 100000;
  105 static const u_int32_t  CH_TIMEOUT_READ_ELEMENT_STATUS       = 10000;
  106 static const u_int32_t  CH_TIMEOUT_SEND_VOLTAG               = 10000;
  107 static const u_int32_t  CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
  108 
  109 typedef enum {
  110         CH_FLAG_INVALID         = 0x001,
  111         CH_FLAG_OPEN            = 0x002
  112 } ch_flags;
  113 
  114 typedef enum {
  115         CH_STATE_PROBE,
  116         CH_STATE_NORMAL
  117 } ch_state;
  118 
  119 typedef enum {
  120         CH_CCB_PROBE,
  121         CH_CCB_WAITING
  122 } ch_ccb_types;
  123 
  124 typedef enum {
  125         CH_Q_NONE       = 0x00,
  126         CH_Q_NO_DBD     = 0x01
  127 } ch_quirks;
  128 
  129 #define ccb_state       ppriv_field0
  130 #define ccb_bp          ppriv_ptr1
  131 
  132 struct scsi_mode_sense_data {
  133         struct scsi_mode_header_6 header;
  134         struct scsi_mode_blk_desc blk_desc;
  135         union {
  136                 struct page_element_address_assignment ea;
  137                 struct page_transport_geometry_parameters tg;
  138                 struct page_device_capabilities cap;
  139         } pages;
  140 };
  141 
  142 struct ch_softc {
  143         ch_flags        flags;
  144         ch_state        state;
  145         ch_quirks       quirks;
  146         union ccb       saved_ccb;
  147         struct devstat  *device_stats;
  148         struct cdev *dev;
  149 
  150         int             sc_picker;      /* current picker */
  151 
  152         /*
  153          * The following information is obtained from the
  154          * element address assignment page.
  155          */
  156         int             sc_firsts[CHET_MAX + 1];        /* firsts */
  157         int             sc_counts[CHET_MAX + 1];        /* counts */
  158 
  159         /*
  160          * The following mask defines the legal combinations
  161          * of elements for the MOVE MEDIUM command.
  162          */
  163         u_int8_t        sc_movemask[CHET_MAX + 1];
  164 
  165         /*
  166          * As above, but for EXCHANGE MEDIUM.
  167          */
  168         u_int8_t        sc_exchangemask[CHET_MAX + 1];
  169 
  170         /*
  171          * Quirks; see below.  XXX KDM not implemented yet
  172          */
  173         int             sc_settledelay; /* delay for settle */
  174 };
  175 
  176 #define CHUNIT(x)       (minor((x)))
  177 
  178 static  d_open_t        chopen;
  179 static  d_close_t       chclose;
  180 static  d_ioctl_t       chioctl;
  181 static  periph_init_t   chinit;
  182 static  periph_ctor_t   chregister;
  183 static  periph_oninv_t  choninvalidate;
  184 static  periph_dtor_t   chcleanup;
  185 static  periph_start_t  chstart;
  186 static  void            chasync(void *callback_arg, u_int32_t code,
  187                                 struct cam_path *path, void *arg);
  188 static  void            chdone(struct cam_periph *periph,
  189                                union ccb *done_ccb);
  190 static  int             cherror(union ccb *ccb, u_int32_t cam_flags,
  191                                 u_int32_t sense_flags);
  192 static  int             chmove(struct cam_periph *periph,
  193                                struct changer_move *cm);
  194 static  int             chexchange(struct cam_periph *periph,
  195                                    struct changer_exchange *ce);
  196 static  int             chposition(struct cam_periph *periph,
  197                                    struct changer_position *cp);
  198 static  int             chgetelemstatus(struct cam_periph *periph,
  199                                 struct changer_element_status_request *csr);
  200 static  int             chsetvoltag(struct cam_periph *periph,
  201                                     struct changer_set_voltag_request *csvr);
  202 static  int             chielem(struct cam_periph *periph, 
  203                                 unsigned int timeout);
  204 static  int             chgetparams(struct cam_periph *periph);
  205 
  206 static struct periph_driver chdriver =
  207 {
  208         chinit, "ch",
  209         TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
  210 };
  211 
  212 PERIPHDRIVER_DECLARE(ch, chdriver);
  213 
  214 static struct cdevsw ch_cdevsw = {
  215         .d_version =    D_VERSION,
  216         .d_flags =      D_NEEDGIANT,
  217         .d_open =       chopen,
  218         .d_close =      chclose,
  219         .d_ioctl =      chioctl,
  220         .d_name =       "ch",
  221 };
  222 
  223 MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
  224 
  225 static void
  226 chinit(void)
  227 {
  228         cam_status status;
  229         struct cam_path *path;
  230 
  231         /*
  232          * Install a global async callback.  This callback will
  233          * receive async callbacks like "new device found".
  234          */
  235         status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
  236                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
  237 
  238         if (status == CAM_REQ_CMP) {
  239                 struct ccb_setasync csa;
  240 
  241                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
  242                 csa.ccb_h.func_code = XPT_SASYNC_CB;
  243                 csa.event_enable = AC_FOUND_DEVICE;
  244                 csa.callback = chasync;
  245                 csa.callback_arg = NULL;
  246                 xpt_action((union ccb *)&csa);
  247                 status = csa.ccb_h.status;
  248                 xpt_free_path(path);
  249         }
  250 
  251         if (status != CAM_REQ_CMP) {
  252                 printf("ch: Failed to attach master async callback "
  253                        "due to status 0x%x!\n", status);
  254         }
  255 }
  256 
  257 static void
  258 choninvalidate(struct cam_periph *periph)
  259 {
  260         struct ch_softc *softc;
  261         struct ccb_setasync csa;
  262 
  263         softc = (struct ch_softc *)periph->softc;
  264 
  265         /*
  266          * De-register any async callbacks.
  267          */
  268         xpt_setup_ccb(&csa.ccb_h, periph->path,
  269                       /* priority */ 5);
  270         csa.ccb_h.func_code = XPT_SASYNC_CB;
  271         csa.event_enable = 0;
  272         csa.callback = chasync;
  273         csa.callback_arg = periph;
  274         xpt_action((union ccb *)&csa);
  275 
  276         softc->flags |= CH_FLAG_INVALID;
  277 
  278         xpt_print_path(periph->path);
  279         printf("lost device\n");
  280 
  281 }
  282 
  283 static void
  284 chcleanup(struct cam_periph *periph)
  285 {
  286         struct ch_softc *softc;
  287 
  288         softc = (struct ch_softc *)periph->softc;
  289 
  290         devstat_remove_entry(softc->device_stats);
  291         destroy_dev(softc->dev);
  292         xpt_print_path(periph->path);
  293         printf("removing device entry\n");
  294         free(softc, M_DEVBUF);
  295 }
  296 
  297 static void
  298 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
  299 {
  300         struct cam_periph *periph;
  301 
  302         periph = (struct cam_periph *)callback_arg;
  303 
  304         switch(code) {
  305         case AC_FOUND_DEVICE:
  306         {
  307                 struct ccb_getdev *cgd;
  308                 cam_status status;
  309 
  310                 cgd = (struct ccb_getdev *)arg;
  311                 if (cgd == NULL)
  312                         break;
  313 
  314                 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
  315                         break;
  316 
  317                 /*
  318                  * Allocate a peripheral instance for
  319                  * this device and start the probe
  320                  * process.
  321                  */
  322                 status = cam_periph_alloc(chregister, choninvalidate,
  323                                           chcleanup, chstart, "ch",
  324                                           CAM_PERIPH_BIO, cgd->ccb_h.path,
  325                                           chasync, AC_FOUND_DEVICE, cgd);
  326 
  327                 if (status != CAM_REQ_CMP
  328                  && status != CAM_REQ_INPROG)
  329                         printf("chasync: Unable to probe new device "
  330                                "due to status 0x%x\n", status);
  331 
  332                 break;
  333 
  334         }
  335         default:
  336                 cam_periph_async(periph, code, path, arg);
  337                 break;
  338         }
  339 }
  340 
  341 static cam_status
  342 chregister(struct cam_periph *periph, void *arg)
  343 {
  344         struct ch_softc *softc;
  345         struct ccb_setasync csa;
  346         struct ccb_getdev *cgd;
  347 
  348         cgd = (struct ccb_getdev *)arg;
  349         if (periph == NULL) {
  350                 printf("chregister: periph was NULL!!\n");
  351                 return(CAM_REQ_CMP_ERR);
  352         }
  353 
  354         if (cgd == NULL) {
  355                 printf("chregister: no getdev CCB, can't register device\n");
  356                 return(CAM_REQ_CMP_ERR);
  357         }
  358 
  359         softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
  360 
  361         if (softc == NULL) {
  362                 printf("chregister: Unable to probe new device. "
  363                        "Unable to allocate softc\n");                           
  364                 return(CAM_REQ_CMP_ERR);
  365         }
  366 
  367         bzero(softc, sizeof(*softc));
  368         softc->state = CH_STATE_PROBE;
  369         periph->softc = softc;
  370         softc->quirks = CH_Q_NONE;
  371 
  372         /*
  373          * Changers don't have a blocksize, and obviously don't support
  374          * tagged queueing.
  375          */
  376         softc->device_stats = devstat_new_entry("ch",
  377                           periph->unit_number, 0,
  378                           DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
  379                           SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
  380                           DEVSTAT_PRIORITY_OTHER);
  381 
  382         /* Register the device */
  383         softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
  384                               GID_OPERATOR, 0600, "%s%d", periph->periph_name,
  385                               periph->unit_number);
  386         softc->dev->si_drv1 = periph;
  387 
  388         /*
  389          * Add an async callback so that we get
  390          * notified if this device goes away.
  391          */
  392         xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
  393         csa.ccb_h.func_code = XPT_SASYNC_CB;
  394         csa.event_enable = AC_LOST_DEVICE;
  395         csa.callback = chasync;
  396         csa.callback_arg = periph;
  397         xpt_action((union ccb *)&csa);
  398 
  399         /*
  400          * Lock this peripheral until we are setup.
  401          * This first call can't block
  402          */
  403         (void)cam_periph_lock(periph, PRIBIO);
  404         xpt_schedule(periph, /*priority*/5);
  405 
  406         return(CAM_REQ_CMP);
  407 }
  408 
  409 static int
  410 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
  411 {
  412         struct cam_periph *periph;
  413         struct ch_softc *softc;
  414         int error;
  415         int s;
  416 
  417         periph = (struct cam_periph *)dev->si_drv1;
  418         if (periph == NULL)
  419                 return(ENXIO);
  420 
  421         softc = (struct ch_softc *)periph->softc;
  422 
  423         s = splsoftcam();
  424         if (softc->flags & CH_FLAG_INVALID) {
  425                 splx(s);
  426                 return(ENXIO);
  427         }
  428 
  429         if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
  430                 splx(s);
  431                 return (error);
  432         }
  433         
  434         splx(s);
  435 
  436         if ((softc->flags & CH_FLAG_OPEN) == 0) {
  437                 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
  438                         return(ENXIO);
  439                 softc->flags |= CH_FLAG_OPEN;
  440         }
  441 
  442         /*
  443          * Load information about this changer device into the softc.
  444          */
  445         if ((error = chgetparams(periph)) != 0) {
  446                 softc->flags &= ~CH_FLAG_OPEN;
  447                 cam_periph_unlock(periph);
  448                 cam_periph_release(periph);
  449                 return(error);
  450         }
  451 
  452         cam_periph_unlock(periph);
  453 
  454         return(error);
  455 }
  456 
  457 static int
  458 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
  459 {
  460         struct  cam_periph *periph;
  461         struct  ch_softc *softc;
  462         int     error;
  463 
  464         error = 0;
  465 
  466         periph = (struct cam_periph *)dev->si_drv1;
  467         if (periph == NULL)
  468                 return(ENXIO);
  469 
  470         softc = (struct ch_softc *)periph->softc;
  471 
  472         if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
  473                 return(error);
  474 
  475         softc->flags &= ~CH_FLAG_OPEN;
  476 
  477         cam_periph_unlock(periph);
  478         cam_periph_release(periph);
  479 
  480         return(0);
  481 }
  482 
  483 static void
  484 chstart(struct cam_periph *periph, union ccb *start_ccb)
  485 {
  486         struct ch_softc *softc;
  487         int s;
  488 
  489         softc = (struct ch_softc *)periph->softc;
  490 
  491         switch (softc->state) {
  492         case CH_STATE_NORMAL:
  493         {
  494                 s = splbio();
  495                 if (periph->immediate_priority <= periph->pinfo.priority){
  496                         start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
  497 
  498                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
  499                                           periph_links.sle);
  500                         periph->immediate_priority = CAM_PRIORITY_NONE;
  501                         splx(s);
  502                         wakeup(&periph->ccb_list);
  503                 } else
  504                         splx(s);
  505                 break;
  506         }
  507         case CH_STATE_PROBE:
  508         {
  509                 int mode_buffer_len;
  510                 void *mode_buffer;
  511 
  512                 /*
  513                  * Include the block descriptor when calculating the mode
  514                  * buffer length,
  515                  */
  516                 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
  517                                   sizeof(struct scsi_mode_blk_desc) +
  518                                  sizeof(struct page_element_address_assignment);
  519 
  520                 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
  521 
  522                 if (mode_buffer == NULL) {
  523                         printf("chstart: couldn't malloc mode sense data\n");
  524                         break;
  525                 }
  526                 bzero(mode_buffer, mode_buffer_len);
  527 
  528                 /*
  529                  * Get the element address assignment page.
  530                  */
  531                 scsi_mode_sense(&start_ccb->csio,
  532                                 /* retries */ 1,
  533                                 /* cbfcnp */ chdone,
  534                                 /* tag_action */ MSG_SIMPLE_Q_TAG,
  535                                 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
  536                                         FALSE : TRUE,
  537                                 /* page_code */ SMS_PAGE_CTRL_CURRENT,
  538                                 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
  539                                 /* param_buf */ (u_int8_t *)mode_buffer,
  540                                 /* param_len */ mode_buffer_len,
  541                                 /* sense_len */ SSD_FULL_SIZE,
  542                                 /* timeout */ CH_TIMEOUT_MODE_SENSE);
  543 
  544                 start_ccb->ccb_h.ccb_bp = NULL;
  545                 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
  546                 xpt_action(start_ccb);
  547                 break;
  548         }
  549         }
  550 }
  551 
  552 static void
  553 chdone(struct cam_periph *periph, union ccb *done_ccb)
  554 {
  555         struct ch_softc *softc;
  556         struct ccb_scsiio *csio;
  557 
  558         softc = (struct ch_softc *)periph->softc;
  559         csio = &done_ccb->csio;
  560 
  561         switch(done_ccb->ccb_h.ccb_state) {
  562         case CH_CCB_PROBE:
  563         {
  564                 struct scsi_mode_header_6 *mode_header;
  565                 struct page_element_address_assignment *ea;
  566                 char announce_buf[80];
  567 
  568 
  569                 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
  570 
  571                 ea = (struct page_element_address_assignment *)
  572                         find_mode_page_6(mode_header);
  573 
  574                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
  575                         
  576                         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
  577                         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
  578                         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
  579                         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
  580                         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
  581                         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
  582                         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
  583                         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
  584                         softc->sc_picker = softc->sc_firsts[CHET_MT];
  585 
  586 #define PLURAL(c)       (c) == 1 ? "" : "s"
  587                         snprintf(announce_buf, sizeof(announce_buf),
  588                                 "%d slot%s, %d drive%s, "
  589                                 "%d picker%s, %d portal%s",
  590                                 softc->sc_counts[CHET_ST],
  591                                 PLURAL(softc->sc_counts[CHET_ST]),
  592                                 softc->sc_counts[CHET_DT],
  593                                 PLURAL(softc->sc_counts[CHET_DT]),
  594                                 softc->sc_counts[CHET_MT],
  595                                 PLURAL(softc->sc_counts[CHET_MT]),
  596                                 softc->sc_counts[CHET_IE],
  597                                 PLURAL(softc->sc_counts[CHET_IE]));
  598 #undef PLURAL
  599                 } else {
  600                         int error;
  601 
  602                         error = cherror(done_ccb, CAM_RETRY_SELTO,
  603                                         SF_RETRY_UA | SF_NO_PRINT);
  604                         /*
  605                          * Retry any UNIT ATTENTION type errors.  They
  606                          * are expected at boot.
  607                          */
  608                         if (error == ERESTART) {
  609                                 /*
  610                                  * A retry was scheuled, so
  611                                  * just return.
  612                                  */
  613                                 return;
  614                         } else if (error != 0) {
  615                                 int retry_scheduled;
  616                                 struct scsi_mode_sense_6 *sms;
  617 
  618                                 sms = (struct scsi_mode_sense_6 *)
  619                                         done_ccb->csio.cdb_io.cdb_bytes;
  620 
  621                                 /*
  622                                  * Check to see if block descriptors were
  623                                  * disabled.  Some devices don't like that.
  624                                  * We're taking advantage of the fact that
  625                                  * the first few bytes of the 6 and 10 byte
  626                                  * mode sense commands are the same.  If
  627                                  * block descriptors were disabled, enable
  628                                  * them and re-send the command.
  629                                  */
  630                                 if (sms->byte2 & SMS_DBD) {
  631                                         sms->byte2 &= ~SMS_DBD;
  632                                         xpt_action(done_ccb);
  633                                         softc->quirks |= CH_Q_NO_DBD;
  634                                         retry_scheduled = 1;
  635                                 } else
  636                                         retry_scheduled = 0;
  637 
  638                                 /* Don't wedge this device's queue */
  639                                 cam_release_devq(done_ccb->ccb_h.path,
  640                                                  /*relsim_flags*/0,
  641                                                  /*reduction*/0,
  642                                                  /*timeout*/0,
  643                                                  /*getcount_only*/0);
  644 
  645                                 if (retry_scheduled)
  646                                         return;
  647 
  648                                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
  649                                     == CAM_SCSI_STATUS_ERROR) 
  650                                         scsi_sense_print(&done_ccb->csio);
  651                                 else {
  652                                         xpt_print_path(periph->path);
  653                                         printf("got CAM status %#x\n",
  654                                                done_ccb->ccb_h.status);
  655                                 }
  656                                 xpt_print_path(periph->path);
  657                                 printf("fatal error, failed to attach to"
  658                                        " device\n");
  659 
  660                                 cam_periph_invalidate(periph);
  661 
  662                                 announce_buf[0] = '\0';
  663                         }
  664                 }
  665                 if (announce_buf[0] != '\0')
  666                         xpt_announce_periph(periph, announce_buf);
  667                 softc->state = CH_STATE_NORMAL;
  668                 free(mode_header, M_SCSICH);
  669                 /*
  670                  * Since our peripheral may be invalidated by an error
  671                  * above or an external event, we must release our CCB
  672                  * before releasing the probe lock on the peripheral.
  673                  * The peripheral will only go away once the last lock
  674                  * is removed, and we need it around for the CCB release
  675                  * operation.
  676                  */
  677                 xpt_release_ccb(done_ccb);
  678                 cam_periph_unlock(periph);
  679                 return;
  680         }
  681         case CH_CCB_WAITING:
  682         {
  683                 /* Caller will release the CCB */
  684                 wakeup(&done_ccb->ccb_h.cbfcnp);
  685                 return;
  686         }
  687         default:
  688                 break;
  689         }
  690         xpt_release_ccb(done_ccb);
  691 }
  692 
  693 static int
  694 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
  695 {
  696         struct ch_softc *softc;
  697         struct cam_periph *periph;
  698 
  699         periph = xpt_path_periph(ccb->ccb_h.path);
  700         softc = (struct ch_softc *)periph->softc;
  701 
  702         return (cam_periph_error(ccb, cam_flags, sense_flags,
  703                                  &softc->saved_ccb));
  704 }
  705 
  706 static int
  707 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
  708 {
  709         struct cam_periph *periph;
  710         struct ch_softc *softc;
  711         int error;
  712 
  713         periph = (struct cam_periph *)dev->si_drv1;
  714         if (periph == NULL)
  715                 return(ENXIO);
  716 
  717         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
  718 
  719         softc = (struct ch_softc *)periph->softc;
  720 
  721         error = 0;
  722 
  723         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
  724                   ("trying to do ioctl %#lx\n", cmd));
  725 
  726         /*
  727          * If this command can change the device's state, we must
  728          * have the device open for writing.
  729          */
  730         switch (cmd) {
  731         case CHIOGPICKER:
  732         case CHIOGPARAMS:
  733         case CHIOGSTATUS:
  734                 break;
  735 
  736         default:
  737                 if ((flag & FWRITE) == 0)
  738                         return (EBADF);
  739         }
  740 
  741         switch (cmd) {
  742         case CHIOMOVE:
  743                 error = chmove(periph, (struct changer_move *)addr);
  744                 break;
  745 
  746         case CHIOEXCHANGE:
  747                 error = chexchange(periph, (struct changer_exchange *)addr);
  748                 break;
  749 
  750         case CHIOPOSITION:
  751                 error = chposition(periph, (struct changer_position *)addr);
  752                 break;
  753 
  754         case CHIOGPICKER:
  755                 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
  756                 break;
  757 
  758         case CHIOSPICKER:
  759         {
  760                 int new_picker = *(int *)addr;
  761 
  762                 if (new_picker > (softc->sc_counts[CHET_MT] - 1))
  763                         return (EINVAL);
  764                 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
  765                 break;
  766         }
  767         case CHIOGPARAMS:
  768         {
  769                 struct changer_params *cp = (struct changer_params *)addr;
  770 
  771                 cp->cp_npickers = softc->sc_counts[CHET_MT];
  772                 cp->cp_nslots = softc->sc_counts[CHET_ST];
  773                 cp->cp_nportals = softc->sc_counts[CHET_IE];
  774                 cp->cp_ndrives = softc->sc_counts[CHET_DT];
  775                 break;
  776         }
  777         case CHIOIELEM:
  778                 error = chielem(periph, *(unsigned int *)addr);
  779                 break;
  780 
  781         case CHIOGSTATUS:
  782         {
  783                 error = chgetelemstatus(periph,
  784                                (struct changer_element_status_request *) addr);
  785                 break;
  786         }
  787 
  788         case CHIOSETVOLTAG:
  789         {
  790                 error = chsetvoltag(periph,
  791                                     (struct changer_set_voltag_request *) addr);
  792                 break;
  793         }
  794 
  795         /* Implement prevent/allow? */
  796 
  797         default:
  798                 error = cam_periph_ioctl(periph, cmd, addr, cherror);
  799                 break;
  800         }
  801 
  802         return (error);
  803 }
  804 
  805 static int
  806 chmove(struct cam_periph *periph, struct changer_move *cm)
  807 {
  808         struct ch_softc *softc;
  809         u_int16_t fromelem, toelem;
  810         union ccb *ccb;
  811         int error;
  812 
  813         error = 0;
  814         softc = (struct ch_softc *)periph->softc;
  815 
  816         /*
  817          * Check arguments.
  818          */
  819         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
  820                 return (EINVAL);
  821         if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
  822             (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
  823                 return (ENODEV);
  824 
  825         /*
  826          * Check the request against the changer's capabilities.
  827          */
  828         if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
  829                 return (ENODEV);
  830 
  831         /*
  832          * Calculate the source and destination elements.
  833          */
  834         fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
  835         toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
  836 
  837         ccb = cam_periph_getccb(periph, /*priority*/ 1);
  838 
  839         scsi_move_medium(&ccb->csio,
  840                          /* retries */ 1,
  841                          /* cbfcnp */ chdone,
  842                          /* tag_action */ MSG_SIMPLE_Q_TAG,
  843                          /* tea */ softc->sc_picker,
  844                          /* src */ fromelem,
  845                          /* dst */ toelem,
  846                          /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
  847                          /* sense_len */ SSD_FULL_SIZE,
  848                          /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
  849 
  850         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
  851                                   /*sense_flags*/ SF_RETRY_UA,
  852                                   softc->device_stats);
  853 
  854         xpt_release_ccb(ccb);
  855 
  856         return(error);
  857 }
  858 
  859 static int
  860 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
  861 {
  862         struct ch_softc *softc;
  863         u_int16_t src, dst1, dst2;
  864         union ccb *ccb;
  865         int error;
  866 
  867         error = 0;
  868         softc = (struct ch_softc *)periph->softc;
  869         /*
  870          * Check arguments.
  871          */
  872         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
  873             (ce->ce_sdsttype > CHET_DT))
  874                 return (EINVAL);
  875         if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
  876             (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
  877             (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
  878                 return (ENODEV);
  879 
  880         /*
  881          * Check the request against the changer's capabilities.
  882          */
  883         if (((softc->sc_exchangemask[ce->ce_srctype] &
  884              (1 << ce->ce_fdsttype)) == 0) ||
  885             ((softc->sc_exchangemask[ce->ce_fdsttype] &
  886              (1 << ce->ce_sdsttype)) == 0))
  887                 return (ENODEV);
  888 
  889         /*
  890          * Calculate the source and destination elements.
  891          */
  892         src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
  893         dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
  894         dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
  895 
  896         ccb = cam_periph_getccb(periph, /*priority*/ 1);
  897 
  898         scsi_exchange_medium(&ccb->csio,
  899                              /* retries */ 1,
  900                              /* cbfcnp */ chdone,
  901                              /* tag_action */ MSG_SIMPLE_Q_TAG,
  902                              /* tea */ softc->sc_picker,
  903                              /* src */ src,
  904                              /* dst1 */ dst1,
  905                              /* dst2 */ dst2,
  906                              /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
  907                                            TRUE : FALSE,
  908                              /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
  909                                            TRUE : FALSE,
  910                              /* sense_len */ SSD_FULL_SIZE,
  911                              /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
  912 
  913         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
  914                                   /*sense_flags*/ SF_RETRY_UA,
  915                                   softc->device_stats);
  916 
  917         xpt_release_ccb(ccb);
  918 
  919         return(error);
  920 }
  921 
  922 static int
  923 chposition(struct cam_periph *periph, struct changer_position *cp)
  924 {
  925         struct ch_softc *softc;
  926         u_int16_t dst;
  927         union ccb *ccb;
  928         int error;
  929 
  930         error = 0;
  931         softc = (struct ch_softc *)periph->softc;
  932 
  933         /*
  934          * Check arguments.
  935          */
  936         if (cp->cp_type > CHET_DT)
  937                 return (EINVAL);
  938         if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
  939                 return (ENODEV);
  940 
  941         /*
  942          * Calculate the destination element.
  943          */
  944         dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
  945 
  946         ccb = cam_periph_getccb(periph, /*priority*/ 1);
  947 
  948         scsi_position_to_element(&ccb->csio,
  949                                  /* retries */ 1,
  950                                  /* cbfcnp */ chdone,
  951                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
  952                                  /* tea */ softc->sc_picker,
  953                                  /* dst */ dst,
  954                                  /* invert */ (cp->cp_flags & CP_INVERT) ?
  955                                               TRUE : FALSE,
  956                                  /* sense_len */ SSD_FULL_SIZE,
  957                                  /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
  958 
  959         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
  960                                   /*sense_flags*/ SF_RETRY_UA,
  961                                   softc->device_stats);
  962 
  963         xpt_release_ccb(ccb);
  964 
  965         return(error);
  966 }
  967 
  968 /*
  969  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
  970  * to host native byte order in the volume serial number.  The volume
  971  * label as returned by the changer is transferred to user mode as
  972  * nul-terminated string.  Volume labels are truncated at the first
  973  * space, as suggested by SCSI-2.
  974  */
  975 static  void
  976 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
  977 {
  978         int i;
  979         for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
  980                 char c = voltag->vif[i];
  981                 if (c && c != ' ')
  982                         uvoltag->cv_volid[i] = c;
  983                 else
  984                         break;
  985         }
  986         uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
  987 }
  988 
  989 /*
  990  * Copy an an element status descriptor to a user-mode
  991  * changer_element_status structure.
  992  */
  993 
  994 static  void
  995 copy_element_status(struct ch_softc *softc,
  996                     u_int16_t flags,
  997                     struct read_element_status_descriptor *desc,
  998                     struct changer_element_status *ces)
  999 {
 1000         u_int16_t eaddr = scsi_2btoul(desc->eaddr);
 1001         u_int16_t et;
 1002 
 1003         ces->ces_int_addr = eaddr;
 1004         /* set up logical address in element status */
 1005         for (et = CHET_MT; et <= CHET_DT; et++) {
 1006                 if ((softc->sc_firsts[et] <= eaddr)
 1007                     && ((softc->sc_firsts[et] + softc->sc_counts[et])
 1008                         > eaddr)) {
 1009                         ces->ces_addr = eaddr - softc->sc_firsts[et];
 1010                         ces->ces_type = et;
 1011                         break;
 1012                 }
 1013         }
 1014 
 1015         ces->ces_flags = desc->flags1;
 1016 
 1017         ces->ces_sensecode = desc->sense_code;
 1018         ces->ces_sensequal = desc->sense_qual;
 1019 
 1020         if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
 1021                 ces->ces_flags |= CES_INVERT;
 1022 
 1023         if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
 1024 
 1025                 eaddr = scsi_2btoul(desc->ssea);
 1026 
 1027                 /* convert source address to logical format */
 1028                 for (et = CHET_MT; et <= CHET_DT; et++) {
 1029                         if ((softc->sc_firsts[et] <= eaddr)
 1030                             && ((softc->sc_firsts[et] + softc->sc_counts[et])
 1031                                 > eaddr)) {
 1032                                 ces->ces_source_addr = 
 1033                                         eaddr - softc->sc_firsts[et];
 1034                                 ces->ces_source_type = et;
 1035                                 ces->ces_flags |= CES_SOURCE_VALID;
 1036                                 break;
 1037                         }
 1038                 }
 1039 
 1040                 if (!(ces->ces_flags & CES_SOURCE_VALID))
 1041                         printf("ch: warning: could not map element source "
 1042                                "address %ud to a valid element type\n",
 1043                                eaddr);
 1044         }
 1045                         
 1046 
 1047         if (flags & READ_ELEMENT_STATUS_PVOLTAG)
 1048                 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
 1049         if (flags & READ_ELEMENT_STATUS_AVOLTAG)
 1050                 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
 1051 
 1052         if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
 1053                 ces->ces_flags |= CES_SCSIID_VALID;
 1054                 ces->ces_scsi_id = desc->dt_scsi_addr;
 1055         }
 1056 
 1057         if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
 1058                 ces->ces_flags |= CES_LUN_VALID;
 1059                 ces->ces_scsi_lun = 
 1060                         desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
 1061         }
 1062 }
 1063 
 1064 static int
 1065 chgetelemstatus(struct cam_periph *periph, 
 1066                 struct changer_element_status_request *cesr)
 1067 {
 1068         struct read_element_status_header *st_hdr;
 1069         struct read_element_status_page_header *pg_hdr;
 1070         struct read_element_status_descriptor *desc;
 1071         caddr_t data = NULL;
 1072         size_t size, desclen;
 1073         int avail, i, error = 0;
 1074         struct changer_element_status *user_data = NULL;
 1075         struct ch_softc *softc;
 1076         union ccb *ccb;
 1077         int chet = cesr->cesr_element_type;
 1078         int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
 1079 
 1080         softc = (struct ch_softc *)periph->softc;
 1081 
 1082         /* perform argument checking */
 1083 
 1084         /*
 1085          * Perform a range check on the cesr_element_{base,count}
 1086          * request argument fields.
 1087          */
 1088         if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
 1089             || (cesr->cesr_element_base + cesr->cesr_element_count)
 1090                 > softc->sc_counts[chet])
 1091                 return (EINVAL);
 1092 
 1093         /*
 1094          * Request one descriptor for the given element type.  This
 1095          * is used to determine the size of the descriptor so that
 1096          * we can allocate enough storage for all of them.  We assume
 1097          * that the first one can fit into 1k.
 1098          */
 1099         data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
 1100 
 1101         ccb = cam_periph_getccb(periph, /*priority*/ 1);
 1102 
 1103         scsi_read_element_status(&ccb->csio,
 1104                                  /* retries */ 1,
 1105                                  /* cbfcnp */ chdone,
 1106                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
 1107                                  /* voltag */ want_voltags,
 1108                                  /* sea */ softc->sc_firsts[chet],
 1109                                  /* count */ 1,
 1110                                  /* data_ptr */ data,
 1111                                  /* dxfer_len */ 1024,
 1112                                  /* sense_len */ SSD_FULL_SIZE,
 1113                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
 1114 
 1115         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1116                                   /*sense_flags*/ SF_RETRY_UA,
 1117                                   softc->device_stats);
 1118 
 1119         if (error)
 1120                 goto done;
 1121 
 1122         st_hdr = (struct read_element_status_header *)data;
 1123         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
 1124                   sizeof(struct read_element_status_header));
 1125         desclen = scsi_2btoul(pg_hdr->edl);
 1126 
 1127         size = sizeof(struct read_element_status_header) +
 1128                sizeof(struct read_element_status_page_header) +
 1129                (desclen * cesr->cesr_element_count);
 1130 
 1131         /*
 1132          * Reallocate storage for descriptors and get them from the
 1133          * device.
 1134          */
 1135         free(data, M_DEVBUF);
 1136         data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
 1137 
 1138         scsi_read_element_status(&ccb->csio,
 1139                                  /* retries */ 1,
 1140                                  /* cbfcnp */ chdone,
 1141                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
 1142                                  /* voltag */ want_voltags,
 1143                                  /* sea */ softc->sc_firsts[chet]
 1144                                  + cesr->cesr_element_base,
 1145                                  /* count */ cesr->cesr_element_count,
 1146                                  /* data_ptr */ data,
 1147                                  /* dxfer_len */ size,
 1148                                  /* sense_len */ SSD_FULL_SIZE,
 1149                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
 1150         
 1151         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1152                                   /*sense_flags*/ SF_RETRY_UA,
 1153                                   softc->device_stats);
 1154 
 1155         if (error)
 1156                 goto done;
 1157 
 1158         /*
 1159          * Fill in the user status array.
 1160          */
 1161         st_hdr = (struct read_element_status_header *)data;
 1162         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
 1163                   sizeof(struct read_element_status_header));
 1164         avail = scsi_2btoul(st_hdr->count);
 1165 
 1166         if (avail != cesr->cesr_element_count) {
 1167                 xpt_print_path(periph->path);
 1168                 printf("warning, READ ELEMENT STATUS avail != count\n");
 1169         }
 1170 
 1171         user_data = (struct changer_element_status *)
 1172                 malloc(avail * sizeof(struct changer_element_status),
 1173                        M_DEVBUF, M_WAITOK | M_ZERO);
 1174 
 1175         desc = (struct read_element_status_descriptor *)((uintptr_t)data +
 1176                 sizeof(struct read_element_status_header) +
 1177                 sizeof(struct read_element_status_page_header));
 1178         /*
 1179          * Set up the individual element status structures
 1180          */
 1181         for (i = 0; i < avail; ++i) {
 1182                 struct changer_element_status *ces = &(user_data[i]);
 1183 
 1184                 copy_element_status(softc, pg_hdr->flags, desc, ces);
 1185 
 1186                 desc = (struct read_element_status_descriptor *)
 1187                        ((uintptr_t)desc + desclen);
 1188         }
 1189 
 1190         /* Copy element status structures out to userspace. */
 1191         error = copyout(user_data,
 1192                         cesr->cesr_element_status,
 1193                         avail * sizeof(struct changer_element_status));
 1194 
 1195  done:
 1196         xpt_release_ccb(ccb);
 1197 
 1198         if (data != NULL)
 1199                 free(data, M_DEVBUF);
 1200         if (user_data != NULL)
 1201                 free(user_data, M_DEVBUF);
 1202 
 1203         return (error);
 1204 }
 1205 
 1206 static int
 1207 chielem(struct cam_periph *periph,
 1208         unsigned int timeout)
 1209 {
 1210         union ccb *ccb;
 1211         struct ch_softc *softc;
 1212         int error;
 1213 
 1214         if (!timeout) {
 1215                 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
 1216         } else {
 1217                 timeout *= 1000;
 1218         }
 1219 
 1220         error = 0;
 1221         softc = (struct ch_softc *)periph->softc;
 1222 
 1223         ccb = cam_periph_getccb(periph, /*priority*/ 1);
 1224 
 1225         scsi_initialize_element_status(&ccb->csio,
 1226                                       /* retries */ 1,
 1227                                       /* cbfcnp */ chdone,
 1228                                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 1229                                       /* sense_len */ SSD_FULL_SIZE,
 1230                                       /* timeout */ timeout);
 1231 
 1232         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1233                                   /*sense_flags*/ SF_RETRY_UA,
 1234                                   softc->device_stats);
 1235 
 1236         xpt_release_ccb(ccb);
 1237 
 1238         return(error);
 1239 }
 1240 
 1241 static int
 1242 chsetvoltag(struct cam_periph *periph,
 1243             struct changer_set_voltag_request *csvr)
 1244 {
 1245         union ccb *ccb;
 1246         struct ch_softc *softc;
 1247         u_int16_t ea;
 1248         u_int8_t sac;
 1249         struct scsi_send_volume_tag_parameters ssvtp;
 1250         int error;
 1251         int i;
 1252 
 1253         error = 0;
 1254         softc = (struct ch_softc *)periph->softc;
 1255 
 1256         bzero(&ssvtp, sizeof(ssvtp));
 1257         for (i=0; i<sizeof(ssvtp.vitf); i++) {
 1258                 ssvtp.vitf[i] = ' ';
 1259         }
 1260 
 1261         /*
 1262          * Check arguments.
 1263          */
 1264         if (csvr->csvr_type > CHET_DT)
 1265                 return EINVAL;
 1266         if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
 1267                 return ENODEV;
 1268 
 1269         ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
 1270 
 1271         if (csvr->csvr_flags & CSVR_ALTERNATE) {
 1272                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
 1273                 case CSVR_MODE_SET:
 1274                         sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
 1275                         break;
 1276                 case CSVR_MODE_REPLACE:
 1277                         sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
 1278                         break;
 1279                 case CSVR_MODE_CLEAR:
 1280                         sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
 1281                         break;
 1282                 default:
 1283                         error = EINVAL;
 1284                         goto out;
 1285                 }
 1286         } else {
 1287                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
 1288                 case CSVR_MODE_SET:
 1289                         sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
 1290                         break;
 1291                 case CSVR_MODE_REPLACE:
 1292                         sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
 1293                         break;
 1294                 case CSVR_MODE_CLEAR:
 1295                         sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
 1296                         break;
 1297                 default:
 1298                         error = EINVAL;
 1299                         goto out;
 1300                 }
 1301         }
 1302 
 1303         memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
 1304                min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
 1305         scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
 1306 
 1307         ccb = cam_periph_getccb(periph, /*priority*/ 1);
 1308 
 1309         scsi_send_volume_tag(&ccb->csio,
 1310                              /* retries */ 1,
 1311                              /* cbfcnp */ chdone,
 1312                              /* tag_action */ MSG_SIMPLE_Q_TAG,
 1313                              /* element_address */ ea,
 1314                              /* send_action_code */ sac,
 1315                              /* parameters */ &ssvtp,
 1316                              /* sense_len */ SSD_FULL_SIZE,
 1317                              /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
 1318         
 1319         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1320                                   /*sense_flags*/ SF_RETRY_UA,
 1321                                   softc->device_stats);
 1322 
 1323         xpt_release_ccb(ccb);
 1324 
 1325  out:
 1326         return error;
 1327 }
 1328 
 1329 static int
 1330 chgetparams(struct cam_periph *periph)
 1331 {
 1332         union ccb *ccb;
 1333         struct ch_softc *softc;
 1334         void *mode_buffer;
 1335         int mode_buffer_len;
 1336         struct page_element_address_assignment *ea;
 1337         struct page_device_capabilities *cap;
 1338         int error, from, dbd;
 1339         u_int8_t *moves, *exchanges;
 1340 
 1341         error = 0;
 1342 
 1343         softc = (struct ch_softc *)periph->softc;
 1344 
 1345         ccb = cam_periph_getccb(periph, /*priority*/ 1);
 1346 
 1347         /*
 1348          * The scsi_mode_sense_data structure is just a convenience
 1349          * structure that allows us to easily calculate the worst-case
 1350          * storage size of the mode sense buffer.
 1351          */
 1352         mode_buffer_len = sizeof(struct scsi_mode_sense_data);
 1353 
 1354         mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
 1355 
 1356         if (mode_buffer == NULL) {
 1357                 printf("chgetparams: couldn't malloc mode sense data\n");
 1358                 return(ENOSPC);
 1359         }
 1360 
 1361         bzero(mode_buffer, mode_buffer_len);
 1362 
 1363         if (softc->quirks & CH_Q_NO_DBD)
 1364                 dbd = FALSE;
 1365         else
 1366                 dbd = TRUE;
 1367 
 1368         /*
 1369          * Get the element address assignment page.
 1370          */
 1371         scsi_mode_sense(&ccb->csio,
 1372                         /* retries */ 1,
 1373                         /* cbfcnp */ chdone,
 1374                         /* tag_action */ MSG_SIMPLE_Q_TAG,
 1375                         /* dbd */ dbd,
 1376                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
 1377                         /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
 1378                         /* param_buf */ (u_int8_t *)mode_buffer,
 1379                         /* param_len */ mode_buffer_len,
 1380                         /* sense_len */ SSD_FULL_SIZE,
 1381                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
 1382 
 1383         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1384                                   /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
 1385                                   softc->device_stats);
 1386 
 1387         if (error) {
 1388                 if (dbd) {
 1389                         struct scsi_mode_sense_6 *sms;
 1390 
 1391                         sms = (struct scsi_mode_sense_6 *)
 1392                                 ccb->csio.cdb_io.cdb_bytes;
 1393 
 1394                         sms->byte2 &= ~SMS_DBD;
 1395                         error = cam_periph_runccb(ccb, cherror,
 1396                                                   /*cam_flags*/ CAM_RETRY_SELTO,
 1397                                                   /*sense_flags*/ SF_RETRY_UA,
 1398                                                   softc->device_stats);
 1399                 } else {
 1400                         /*
 1401                          * Since we disabled sense printing above, print
 1402                          * out the sense here since we got an error.
 1403                          */
 1404                         scsi_sense_print(&ccb->csio);
 1405                 }
 1406 
 1407                 if (error) {
 1408                         xpt_print_path(periph->path);
 1409                         printf("chgetparams: error getting element "
 1410                                "address page\n");
 1411                         xpt_release_ccb(ccb);
 1412                         free(mode_buffer, M_SCSICH);
 1413                         return(error);
 1414                 }
 1415         }
 1416 
 1417         ea = (struct page_element_address_assignment *)
 1418                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
 1419 
 1420         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
 1421         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
 1422         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
 1423         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
 1424         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
 1425         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
 1426         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
 1427         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
 1428 
 1429         bzero(mode_buffer, mode_buffer_len);
 1430 
 1431         /*
 1432          * Now get the device capabilities page.
 1433          */
 1434         scsi_mode_sense(&ccb->csio,
 1435                         /* retries */ 1,
 1436                         /* cbfcnp */ chdone,
 1437                         /* tag_action */ MSG_SIMPLE_Q_TAG,
 1438                         /* dbd */ dbd,
 1439                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
 1440                         /* page */ CH_DEVICE_CAP_PAGE,
 1441                         /* param_buf */ (u_int8_t *)mode_buffer,
 1442                         /* param_len */ mode_buffer_len,
 1443                         /* sense_len */ SSD_FULL_SIZE,
 1444                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
 1445         
 1446         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
 1447                                   /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
 1448                                   softc->device_stats);
 1449 
 1450         if (error) {
 1451                 if (dbd) {
 1452                         struct scsi_mode_sense_6 *sms;
 1453 
 1454                         sms = (struct scsi_mode_sense_6 *)
 1455                                 ccb->csio.cdb_io.cdb_bytes;
 1456 
 1457                         sms->byte2 &= ~SMS_DBD;
 1458                         error = cam_periph_runccb(ccb, cherror,
 1459                                                   /*cam_flags*/ CAM_RETRY_SELTO,
 1460                                                   /*sense_flags*/ SF_RETRY_UA,
 1461                                                   softc->device_stats);
 1462                 } else {
 1463                         /*
 1464                          * Since we disabled sense printing above, print
 1465                          * out the sense here since we got an error.
 1466                          */
 1467                         scsi_sense_print(&ccb->csio);
 1468                 }
 1469 
 1470                 if (error) {
 1471                         xpt_print_path(periph->path);
 1472                         printf("chgetparams: error getting device "
 1473                                "capabilities page\n");
 1474                         xpt_release_ccb(ccb);
 1475                         free(mode_buffer, M_SCSICH);
 1476                         return(error);
 1477                 }
 1478         }
 1479 
 1480         xpt_release_ccb(ccb);
 1481 
 1482         cap = (struct page_device_capabilities *)
 1483                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
 1484 
 1485         bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
 1486         bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
 1487         moves = cap->move_from;
 1488         exchanges = cap->exchange_with;
 1489         for (from = CHET_MT; from <= CHET_MAX; ++from) {
 1490                 softc->sc_movemask[from] = moves[from];
 1491                 softc->sc_exchangemask[from] = exchanges[from];
 1492         }
 1493 
 1494         free(mode_buffer, M_SCSICH);
 1495 
 1496         return(error);
 1497 }
 1498 
 1499 void
 1500 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
 1501                  void (*cbfcnp)(struct cam_periph *, union ccb *),
 1502                  u_int8_t tag_action, u_int32_t tea, u_int32_t src,
 1503                  u_int32_t dst, int invert, u_int8_t sense_len,
 1504                  u_int32_t timeout)
 1505 {
 1506         struct scsi_move_medium *scsi_cmd;
 1507 
 1508         scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
 1509         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1510 
 1511         scsi_cmd->opcode = MOVE_MEDIUM;
 1512 
 1513         scsi_ulto2b(tea, scsi_cmd->tea);
 1514         scsi_ulto2b(src, scsi_cmd->src);
 1515         scsi_ulto2b(dst, scsi_cmd->dst);
 1516 
 1517         if (invert)
 1518                 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
 1519 
 1520         cam_fill_csio(csio,
 1521                       retries,
 1522                       cbfcnp,
 1523                       /*flags*/ CAM_DIR_NONE,
 1524                       tag_action,
 1525                       /*data_ptr*/ NULL,
 1526                       /*dxfer_len*/ 0,
 1527                       sense_len,
 1528                       sizeof(*scsi_cmd),
 1529                       timeout);
 1530 }
 1531 
 1532 void
 1533 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
 1534                      void (*cbfcnp)(struct cam_periph *, union ccb *),
 1535                      u_int8_t tag_action, u_int32_t tea, u_int32_t src,
 1536                      u_int32_t dst1, u_int32_t dst2, int invert1,
 1537                      int invert2, u_int8_t sense_len, u_int32_t timeout)
 1538 {
 1539         struct scsi_exchange_medium *scsi_cmd;
 1540 
 1541         scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
 1542         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1543 
 1544         scsi_cmd->opcode = EXCHANGE_MEDIUM;
 1545 
 1546         scsi_ulto2b(tea, scsi_cmd->tea);
 1547         scsi_ulto2b(src, scsi_cmd->src);
 1548         scsi_ulto2b(dst1, scsi_cmd->fdst);
 1549         scsi_ulto2b(dst2, scsi_cmd->sdst);
 1550 
 1551         if (invert1)
 1552                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
 1553 
 1554         if (invert2)
 1555                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
 1556 
 1557         cam_fill_csio(csio,
 1558                       retries,
 1559                       cbfcnp,
 1560                       /*flags*/ CAM_DIR_NONE,
 1561                       tag_action,
 1562                       /*data_ptr*/ NULL,
 1563                       /*dxfer_len*/ 0,
 1564                       sense_len,
 1565                       sizeof(*scsi_cmd),
 1566                       timeout);
 1567 }
 1568 
 1569 void
 1570 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
 1571                          void (*cbfcnp)(struct cam_periph *, union ccb *),
 1572                          u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
 1573                          int invert, u_int8_t sense_len, u_int32_t timeout)
 1574 {
 1575         struct scsi_position_to_element *scsi_cmd;
 1576 
 1577         scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
 1578         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1579 
 1580         scsi_cmd->opcode = POSITION_TO_ELEMENT;
 1581 
 1582         scsi_ulto2b(tea, scsi_cmd->tea);
 1583         scsi_ulto2b(dst, scsi_cmd->dst);
 1584 
 1585         if (invert)
 1586                 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
 1587 
 1588         cam_fill_csio(csio,
 1589                       retries,
 1590                       cbfcnp,
 1591                       /*flags*/ CAM_DIR_NONE,
 1592                       tag_action,
 1593                       /*data_ptr*/ NULL,
 1594                       /*dxfer_len*/ 0,
 1595                       sense_len,
 1596                       sizeof(*scsi_cmd),
 1597                       timeout);
 1598 }
 1599 
 1600 void
 1601 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
 1602                          void (*cbfcnp)(struct cam_periph *, union ccb *),
 1603                          u_int8_t tag_action, int voltag, u_int32_t sea,
 1604                          u_int32_t count, u_int8_t *data_ptr,
 1605                          u_int32_t dxfer_len, u_int8_t sense_len,
 1606                          u_int32_t timeout)
 1607 {
 1608         struct scsi_read_element_status *scsi_cmd;
 1609 
 1610         scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
 1611         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1612 
 1613         scsi_cmd->opcode = READ_ELEMENT_STATUS;
 1614 
 1615         scsi_ulto2b(sea, scsi_cmd->sea);
 1616         scsi_ulto2b(count, scsi_cmd->count);
 1617         scsi_ulto3b(dxfer_len, scsi_cmd->len);
 1618 
 1619         if (voltag)
 1620                 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
 1621 
 1622         cam_fill_csio(csio,
 1623                       retries,
 1624                       cbfcnp,
 1625                       /*flags*/ CAM_DIR_IN,
 1626                       tag_action,
 1627                       data_ptr,
 1628                       dxfer_len,
 1629                       sense_len,
 1630                       sizeof(*scsi_cmd),
 1631                       timeout);
 1632 }
 1633 
 1634 void
 1635 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
 1636                                void (*cbfcnp)(struct cam_periph *, union ccb *),
 1637                                u_int8_t tag_action, u_int8_t sense_len,
 1638                                u_int32_t timeout)
 1639 {
 1640         struct scsi_initialize_element_status *scsi_cmd;
 1641 
 1642         scsi_cmd = (struct scsi_initialize_element_status *)
 1643                     &csio->cdb_io.cdb_bytes;
 1644         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1645 
 1646         scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
 1647 
 1648         cam_fill_csio(csio,
 1649                       retries,
 1650                       cbfcnp,
 1651                       /*flags*/ CAM_DIR_NONE,
 1652                       tag_action,
 1653                       /* data_ptr */ NULL,
 1654                       /* dxfer_len */ 0,
 1655                       sense_len,
 1656                       sizeof(*scsi_cmd),
 1657                       timeout);
 1658 }
 1659 
 1660 void
 1661 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
 1662                      void (*cbfcnp)(struct cam_periph *, union ccb *),
 1663                      u_int8_t tag_action, 
 1664                      u_int16_t element_address,
 1665                      u_int8_t send_action_code,
 1666                      struct scsi_send_volume_tag_parameters *parameters,
 1667                      u_int8_t sense_len, u_int32_t timeout)
 1668 {
 1669         struct scsi_send_volume_tag *scsi_cmd;
 1670 
 1671         scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
 1672         bzero(scsi_cmd, sizeof(*scsi_cmd));
 1673 
 1674         scsi_cmd->opcode = SEND_VOLUME_TAG;
 1675         scsi_ulto2b(element_address, scsi_cmd->ea);
 1676         scsi_cmd->sac = send_action_code;
 1677         scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
 1678 
 1679         cam_fill_csio(csio,
 1680                       retries,
 1681                       cbfcnp,
 1682                       /*flags*/ CAM_DIR_OUT,
 1683                       tag_action,
 1684                       /* data_ptr */ (u_int8_t *) parameters,
 1685                       sizeof(*parameters),
 1686                       sense_len,
 1687                       sizeof(*scsi_cmd),
 1688                       timeout);
 1689 }

Cache object: 693ccae7b409cf6e5e85a837be87ecfd


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