The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/scsipi/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 /*      $NetBSD: ch.c,v 1.58.2.2 2004/09/11 12:51:07 he Exp $   */
    2 
    3 /*-
    4  * Copyright (c) 1996, 1997, 1998, 1999 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
    9  * NASA Ames Research Center.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __KERNEL_RCSID(0, "$NetBSD: ch.c,v 1.58.2.2 2004/09/11 12:51:07 he Exp $");
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/kernel.h>
   46 #include <sys/errno.h>
   47 #include <sys/ioctl.h>
   48 #include <sys/buf.h>
   49 #include <sys/proc.h>
   50 #include <sys/user.h>
   51 #include <sys/chio.h> 
   52 #include <sys/device.h>
   53 #include <sys/malloc.h>
   54 #include <sys/conf.h>
   55 #include <sys/fcntl.h>
   56 #include <sys/vnode.h>
   57 #include <sys/time.h>
   58 #include <sys/select.h>
   59 #include <sys/poll.h>
   60 
   61 #include <dev/scsipi/scsipi_all.h>
   62 #include <dev/scsipi/scsi_all.h>
   63 #include <dev/scsipi/scsi_changer.h>
   64 #include <dev/scsipi/scsiconf.h>
   65 
   66 #define CHRETRIES       2
   67 #define CHUNIT(x)       (minor((x)))
   68 
   69 struct ch_softc {
   70         struct device   sc_dev;         /* generic device info */
   71         struct scsipi_periph *sc_periph;/* our periph data */
   72 
   73         u_int           sc_events;      /* event bitmask */
   74         struct selinfo  sc_selq;        /* select/poll queue for events */
   75 
   76         int             sc_flags;       /* misc. info */
   77 
   78         int             sc_picker;      /* current picker */
   79 
   80         /*
   81          * The following information is obtained from the
   82          * element address assignment page.
   83          */
   84         int             sc_firsts[4];   /* firsts, indexed by CHET_* */
   85         int             sc_counts[4];   /* counts, indexed by CHET_* */
   86 
   87         /*
   88          * The following mask defines the legal combinations
   89          * of elements for the MOVE MEDIUM command.
   90          */
   91         u_int8_t        sc_movemask[4];
   92 
   93         /*
   94          * As above, but for EXCHANGE MEDIUM.
   95          */
   96         u_int8_t        sc_exchangemask[4];
   97 
   98         /*
   99          * Quirks; see below.
  100          */
  101         int             sc_settledelay; /* delay for settle */
  102 
  103 };
  104 
  105 /* sc_flags */
  106 #define CHF_ROTATE              0x01    /* picker can rotate */
  107 
  108 /* Autoconfiguration glue */
  109 int     chmatch __P((struct device *, struct cfdata *, void *));
  110 void    chattach __P((struct device *, struct device *, void *));
  111 
  112 CFATTACH_DECL(ch, sizeof(struct ch_softc),
  113     chmatch, chattach, NULL, NULL);
  114 
  115 extern struct cfdriver ch_cd;
  116 
  117 struct scsipi_inquiry_pattern ch_patterns[] = {
  118         {T_CHANGER, T_REMOV,
  119          "",            "",             ""},
  120 };
  121 
  122 dev_type_open(chopen);
  123 dev_type_close(chclose);
  124 dev_type_read(chread);
  125 dev_type_ioctl(chioctl);
  126 dev_type_poll(chpoll);
  127 dev_type_kqfilter(chkqfilter);
  128 
  129 const struct cdevsw ch_cdevsw = {
  130         chopen, chclose, chread, nowrite, chioctl,
  131         nostop, notty, chpoll, nommap, chkqfilter,
  132 };
  133 
  134 /* SCSI glue */
  135 int     ch_interpret_sense __P((struct scsipi_xfer *));
  136 
  137 const struct scsipi_periphsw ch_switch = {
  138         ch_interpret_sense,     /* check our error handler first */
  139         NULL,                   /* no queue; our commands are synchronous */
  140         NULL,                   /* have no async handler */
  141         NULL,                   /* nothing to be done when xfer is done */
  142 };
  143 
  144 int     ch_move __P((struct ch_softc *, struct changer_move_request *));
  145 int     ch_exchange __P((struct ch_softc *, struct changer_exchange_request *));
  146 int     ch_position __P((struct ch_softc *, struct changer_position_request *));
  147 int     ch_ielem __P((struct ch_softc *));
  148 int     ch_ousergetelemstatus __P((struct ch_softc *, int, u_int8_t *));
  149 int     ch_usergetelemstatus __P((struct ch_softc *,
  150             struct changer_element_status_request *));
  151 int     ch_getelemstatus __P((struct ch_softc *, int, int, void *,
  152             size_t, int, int));
  153 int     ch_setvoltag __P((struct ch_softc *,
  154             struct changer_set_voltag_request *));
  155 int     ch_get_params __P((struct ch_softc *, int));
  156 void    ch_get_quirks __P((struct ch_softc *,
  157             struct scsipi_inquiry_pattern *));
  158 void    ch_event __P((struct ch_softc *, u_int));
  159 int     ch_map_element __P((struct ch_softc *, u_int16_t, int *, int *));
  160 
  161 void    ch_voltag_convert_in __P((const struct changer_volume_tag *,
  162             struct changer_voltag *));
  163 int     ch_voltag_convert_out __P((const struct changer_voltag *,
  164             struct changer_volume_tag *));
  165 
  166 /*
  167  * SCSI changer quirks.
  168  */
  169 struct chquirk {
  170         struct  scsipi_inquiry_pattern cq_match; /* device id pattern */
  171         int     cq_settledelay; /* settle delay, in seconds */
  172 };
  173 
  174 const struct chquirk chquirks[] = {
  175         {{T_CHANGER, T_REMOV,
  176           "SPECTRA",    "9000",         "0200"},
  177          75},
  178 };
  179 
  180 int
  181 chmatch(parent, match, aux)
  182         struct device *parent;
  183         struct cfdata *match;
  184         void *aux;
  185 {
  186         struct scsipibus_attach_args *sa = aux;
  187         int priority;
  188 
  189         (void)scsipi_inqmatch(&sa->sa_inqbuf,
  190             (caddr_t)ch_patterns, sizeof(ch_patterns) / sizeof(ch_patterns[0]),
  191             sizeof(ch_patterns[0]), &priority);
  192 
  193         return (priority);
  194 }
  195 
  196 void
  197 chattach(parent, self, aux)
  198         struct device *parent, *self;
  199         void *aux;
  200 {
  201         struct ch_softc *sc = (struct ch_softc *)self;
  202         struct scsipibus_attach_args *sa = aux;
  203         struct scsipi_periph *periph = sa->sa_periph;
  204 
  205         /* Glue into the SCSI bus */
  206         sc->sc_periph = periph;
  207         periph->periph_dev = &sc->sc_dev;
  208         periph->periph_switch = &ch_switch;
  209 
  210         printf("\n");
  211 
  212         /*
  213          * Find out our device's quirks.
  214          */
  215         ch_get_quirks(sc, &sa->sa_inqbuf);
  216 
  217         /*
  218          * Some changers require a long time to settle out, to do
  219          * tape inventory, for instance.
  220          */
  221         if (sc->sc_settledelay) {
  222                 printf("%s: waiting %d seconds for changer to settle...\n",
  223                     sc->sc_dev.dv_xname, sc->sc_settledelay);
  224                 delay(1000000 * sc->sc_settledelay);
  225         }
  226 
  227         /*
  228          * Get information about the device.  Note we can't use
  229          * interrupts yet.
  230          */
  231         if (ch_get_params(sc, XS_CTL_DISCOVERY|XS_CTL_IGNORE_MEDIA_CHANGE))
  232                 printf("%s: offline\n", sc->sc_dev.dv_xname);
  233         else {
  234 #define PLURAL(c)       (c) == 1 ? "" : "s"
  235                 printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n",
  236                     sc->sc_dev.dv_xname,
  237                     sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]),
  238                     sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]),
  239                     sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]),
  240                     sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE]));
  241 #undef PLURAL
  242 #ifdef CHANGER_DEBUG
  243                 printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
  244                     sc->sc_dev.dv_xname,
  245                     sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
  246                     sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
  247                 printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
  248                     sc->sc_dev.dv_xname,
  249                     sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
  250                     sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
  251 #endif /* CHANGER_DEBUG */
  252         }
  253 
  254         /* Default the current picker. */
  255         sc->sc_picker = sc->sc_firsts[CHET_MT];
  256 }
  257 
  258 int
  259 chopen(dev, flags, fmt, p)
  260         dev_t dev;
  261         int flags, fmt;
  262         struct proc *p;
  263 {
  264         struct ch_softc *sc;
  265         struct scsipi_periph *periph;
  266         struct scsipi_adapter *adapt;
  267         int unit, error;
  268 
  269         unit = CHUNIT(dev);
  270         if ((unit >= ch_cd.cd_ndevs) ||
  271             ((sc = ch_cd.cd_devs[unit]) == NULL))
  272                 return (ENXIO);
  273 
  274         periph = sc->sc_periph;
  275         adapt = periph->periph_channel->chan_adapter;
  276 
  277         /*
  278          * Only allow one open at a time.
  279          */
  280         if (periph->periph_flags & PERIPH_OPEN)
  281                 return (EBUSY);
  282 
  283         if ((error = scsipi_adapter_addref(adapt)) != 0)
  284                 return (error);
  285 
  286         /*
  287          * Make sure the unit is on-line.  If a UNIT ATTENTION
  288          * occurs, we will mark that an Init-Element-Status is
  289          * needed in ch_get_params().
  290          *
  291          * We ignore NOT READY in case e.g a magazine isn't actually
  292          * loaded into the changer or a tape isn't in the drive.
  293          */
  294         error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_NOT_READY);
  295         if (error)
  296                 goto bad;
  297 
  298         periph->periph_flags |= PERIPH_OPEN;
  299 
  300         /*
  301          * Make sure our parameters are up to date.
  302          */
  303         if ((error = ch_get_params(sc, 0)) != 0)
  304                 goto bad;
  305 
  306         return (0);
  307 
  308  bad:
  309         scsipi_adapter_delref(adapt);
  310         periph->periph_flags &= ~PERIPH_OPEN;
  311         return (error);
  312 }
  313 
  314 int
  315 chclose(dev, flags, fmt, p)
  316         dev_t dev;
  317         int flags, fmt;
  318         struct proc *p;
  319 {
  320         struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
  321         struct scsipi_periph *periph = sc->sc_periph;
  322         struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
  323 
  324         scsipi_wait_drain(periph);
  325 
  326         scsipi_adapter_delref(adapt);
  327 
  328         sc->sc_events = 0;
  329 
  330         periph->periph_flags &= ~PERIPH_OPEN;
  331         return (0);
  332 }
  333 
  334 int
  335 chread(dev, uio, flags)
  336         dev_t dev;
  337         struct uio *uio;
  338         int flags;
  339 {
  340         struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
  341         int error;
  342 
  343         if (uio->uio_resid != CHANGER_EVENT_SIZE)
  344                 return (EINVAL);
  345 
  346         /*
  347          * Read never blocks; if there are no events pending, we just
  348          * return an all-clear bitmask.
  349          */
  350         error = uiomove(&sc->sc_events, CHANGER_EVENT_SIZE, uio);
  351         if (error == 0)
  352                 sc->sc_events = 0;
  353         return (error);
  354 }
  355 
  356 int
  357 chioctl(dev, cmd, data, flags, p)
  358         dev_t dev;
  359         u_long cmd;
  360         caddr_t data;
  361         int flags;
  362         struct proc *p;
  363 {
  364         struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
  365         int error = 0;
  366 
  367         /*
  368          * If this command can change the device's state, we must
  369          * have the device open for writing.
  370          */
  371         switch (cmd) {
  372         case CHIOGPICKER:
  373         case CHIOGPARAMS:
  374         case OCHIOGSTATUS:
  375                 break;
  376 
  377         default:
  378                 if ((flags & FWRITE) == 0)
  379                         return (EBADF);
  380         }
  381 
  382         switch (cmd) {
  383         case CHIOMOVE:
  384                 error = ch_move(sc, (struct changer_move_request *)data);
  385                 break;
  386 
  387         case CHIOEXCHANGE:
  388                 error = ch_exchange(sc,
  389                     (struct changer_exchange_request *)data);
  390                 break;
  391 
  392         case CHIOPOSITION:
  393                 error = ch_position(sc,
  394                     (struct changer_position_request *)data);
  395                 break;
  396 
  397         case CHIOGPICKER:
  398                 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
  399                 break;
  400 
  401         case CHIOSPICKER:
  402             {
  403                 int new_picker = *(int *)data;
  404 
  405                 if (new_picker > (sc->sc_counts[CHET_MT] - 1))
  406                         return (EINVAL);
  407                 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
  408                 break;
  409             }
  410 
  411         case CHIOGPARAMS:
  412             {
  413                 struct changer_params *cp = (struct changer_params *)data;
  414 
  415                 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
  416                 cp->cp_npickers = sc->sc_counts[CHET_MT];
  417                 cp->cp_nslots = sc->sc_counts[CHET_ST];
  418                 cp->cp_nportals = sc->sc_counts[CHET_IE];
  419                 cp->cp_ndrives = sc->sc_counts[CHET_DT];
  420                 break;
  421             }
  422 
  423         case CHIOIELEM:
  424                 error = ch_ielem(sc);
  425                 if (error == 0) {
  426                         sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
  427                 }
  428                 break;
  429 
  430         case OCHIOGSTATUS:
  431             {
  432                 struct ochanger_element_status_request *cesr =
  433                     (struct ochanger_element_status_request *)data;
  434 
  435                 error = ch_ousergetelemstatus(sc, cesr->cesr_type,
  436                     cesr->cesr_data);
  437                 break;
  438             }
  439 
  440         case CHIOGSTATUS:
  441                 error = ch_usergetelemstatus(sc,
  442                     (struct changer_element_status_request *)data);
  443                 break;
  444 
  445         case CHIOSVOLTAG:
  446                 error = ch_setvoltag(sc,
  447                     (struct changer_set_voltag_request *)data);
  448                 break;
  449 
  450         /* Implement prevent/allow? */
  451 
  452         default:
  453                 error = scsipi_do_ioctl(sc->sc_periph, dev, cmd, data,
  454                     flags, p);
  455                 break;
  456         }
  457 
  458         return (error);
  459 }
  460 
  461 int
  462 chpoll(dev, events, p)
  463         dev_t dev;
  464         int events;
  465         struct proc *p;
  466 {
  467         struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
  468         int revents;
  469 
  470         revents = events & (POLLOUT | POLLWRNORM);
  471 
  472         if ((events & (POLLIN | POLLRDNORM)) == 0)
  473                 return (revents);
  474 
  475         if (sc->sc_events == 0)
  476                 revents |= events & (POLLIN | POLLRDNORM);
  477         else
  478                 selrecord(p, &sc->sc_selq);
  479 
  480         return (revents);
  481 }
  482 
  483 static void
  484 filt_chdetach(struct knote *kn)
  485 {
  486         struct ch_softc *sc = kn->kn_hook;
  487 
  488         SLIST_REMOVE(&sc->sc_selq.sel_klist, kn, knote, kn_selnext);
  489 }
  490 
  491 static int
  492 filt_chread(struct knote *kn, long hint)
  493 {
  494         struct ch_softc *sc = kn->kn_hook;
  495 
  496         if (sc->sc_events == 0)
  497                 return (0);
  498         kn->kn_data = CHANGER_EVENT_SIZE;
  499         return (1);
  500 }
  501 
  502 static const struct filterops chread_filtops =
  503         { 1, NULL, filt_chdetach, filt_chread };
  504 
  505 static const struct filterops chwrite_filtops =
  506         { 1, NULL, filt_chdetach, filt_seltrue };
  507 
  508 int
  509 chkqfilter(dev_t dev, struct knote *kn)
  510 {
  511         struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
  512         struct klist *klist;
  513 
  514         switch (kn->kn_filter) {
  515         case EVFILT_READ:
  516                 klist = &sc->sc_selq.sel_klist;
  517                 kn->kn_fop = &chread_filtops;
  518                 break;
  519 
  520         case EVFILT_WRITE:
  521                 klist = &sc->sc_selq.sel_klist;
  522                 kn->kn_fop = &chwrite_filtops;
  523                 break;
  524 
  525         default:
  526                 return (1);
  527         }
  528 
  529         kn->kn_hook = sc;
  530 
  531         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
  532 
  533         return (0);
  534 }
  535 
  536 int
  537 ch_interpret_sense(xs)
  538         struct scsipi_xfer *xs;
  539 {
  540         struct scsipi_periph *periph = xs->xs_periph;
  541         struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
  542         struct ch_softc *sc = (void *)periph->periph_dev;
  543         u_int16_t asc_ascq;
  544 
  545         /*
  546          * If the periph is already recovering, just do the
  547          * normal error recovering.
  548          */
  549         if (periph->periph_flags & PERIPH_RECOVERING)
  550                 return (EJUSTRETURN);
  551 
  552         /*
  553          * If it isn't an extended or extended/deferred error, let
  554          * the generic code handle it.
  555          */
  556         if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
  557             (sense->error_code & SSD_ERRCODE) != 0x71)
  558                 return (EJUSTRETURN);
  559 
  560         /*
  561          * We're only interested in condtions that
  562          * indicate potential inventory violation.
  563          *
  564          * We use ASC/ASCQ codes for this.
  565          */
  566 
  567         asc_ascq = (((u_int16_t) sense->add_sense_code) << 8) |
  568             sense->add_sense_code_qual;
  569 
  570         switch (asc_ascq) {
  571         case 0x2800:
  572                 /* "Not Ready To Ready Transition (Medium May Have Changed)" */
  573         case 0x2900:
  574                 /* "Power On, Reset, or Bus Device Reset Occurred" */
  575                 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
  576                 /*
  577                  * Enqueue an Element-Status-Changed event, and wake up
  578                  * any processes waiting for them.
  579                  */
  580                 if ((xs->xs_control & XS_CTL_IGNORE_MEDIA_CHANGE) == 0)
  581                         ch_event(sc, CHEV_ELEMENT_STATUS_CHANGED);
  582                 break;
  583         default:
  584                 break;
  585         }
  586 
  587         return (EJUSTRETURN);
  588 }
  589 
  590 void
  591 ch_event(sc, event)
  592         struct ch_softc *sc;
  593         u_int event;
  594 {
  595 
  596         sc->sc_events |= event;
  597         selnotify(&sc->sc_selq, 0);
  598 }
  599 
  600 int
  601 ch_move(sc, cm)
  602         struct ch_softc *sc;
  603         struct changer_move_request *cm;
  604 {
  605         struct scsi_move_medium cmd;
  606         u_int16_t fromelem, toelem;
  607 
  608         /*
  609          * Check arguments.
  610          */
  611         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
  612                 return (EINVAL);
  613         if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
  614             (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
  615                 return (ENODEV);
  616 
  617         /*
  618          * Check the request against the changer's capabilities.
  619          */
  620         if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
  621                 return (ENODEV);
  622 
  623         /*
  624          * Calculate the source and destination elements.
  625          */
  626         fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
  627         toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
  628 
  629         /*
  630          * Build the SCSI command.
  631          */
  632         memset(&cmd, 0, sizeof(cmd));
  633         cmd.opcode = MOVE_MEDIUM;
  634         _lto2b(sc->sc_picker, cmd.tea);
  635         _lto2b(fromelem, cmd.src);
  636         _lto2b(toelem, cmd.dst);
  637         if (cm->cm_flags & CM_INVERT)
  638                 cmd.flags |= MOVE_MEDIUM_INVERT;
  639 
  640         /*
  641          * Send command to changer.
  642          */
  643         return (scsipi_command(sc->sc_periph, NULL,
  644             (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
  645             100000, NULL, 0));
  646 }
  647 
  648 int
  649 ch_exchange(sc, ce)
  650         struct ch_softc *sc;
  651         struct changer_exchange_request *ce;
  652 {
  653         struct scsi_exchange_medium cmd;
  654         u_int16_t src, dst1, dst2;
  655 
  656         /*
  657          * Check arguments.
  658          */
  659         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
  660             (ce->ce_sdsttype > CHET_DT))
  661                 return (EINVAL);
  662         if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
  663             (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
  664             (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
  665                 return (ENODEV);
  666 
  667         /*
  668          * Check the request against the changer's capabilities.
  669          */
  670         if (((sc->sc_exchangemask[ce->ce_srctype] &
  671              (1 << ce->ce_fdsttype)) == 0) ||
  672             ((sc->sc_exchangemask[ce->ce_fdsttype] &
  673              (1 << ce->ce_sdsttype)) == 0))
  674                 return (ENODEV);
  675 
  676         /*
  677          * Calculate the source and destination elements.
  678          */
  679         src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
  680         dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
  681         dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
  682 
  683         /*
  684          * Build the SCSI command.
  685          */
  686         memset(&cmd, 0, sizeof(cmd));
  687         cmd.opcode = EXCHANGE_MEDIUM;
  688         _lto2b(sc->sc_picker, cmd.tea);
  689         _lto2b(src, cmd.src);
  690         _lto2b(dst1, cmd.fdst);
  691         _lto2b(dst2, cmd.sdst);
  692         if (ce->ce_flags & CE_INVERT1)
  693                 cmd.flags |= EXCHANGE_MEDIUM_INV1;
  694         if (ce->ce_flags & CE_INVERT2)
  695                 cmd.flags |= EXCHANGE_MEDIUM_INV2;
  696 
  697         /*
  698          * Send command to changer.
  699          */
  700         return (scsipi_command(sc->sc_periph, NULL,
  701             (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
  702             100000, NULL, 0));
  703 }
  704 
  705 int
  706 ch_position(sc, cp)
  707         struct ch_softc *sc;
  708         struct changer_position_request *cp;
  709 {
  710         struct scsi_position_to_element cmd;
  711         u_int16_t dst;
  712 
  713         /*
  714          * Check arguments.
  715          */
  716         if (cp->cp_type > CHET_DT)
  717                 return (EINVAL);
  718         if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
  719                 return (ENODEV);
  720 
  721         /*
  722          * Calculate the destination element.
  723          */
  724         dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
  725 
  726         /*
  727          * Build the SCSI command.
  728          */
  729         memset(&cmd, 0, sizeof(cmd));
  730         cmd.opcode = POSITION_TO_ELEMENT;
  731         _lto2b(sc->sc_picker, cmd.tea);
  732         _lto2b(dst, cmd.dst);
  733         if (cp->cp_flags & CP_INVERT)
  734                 cmd.flags |= POSITION_TO_ELEMENT_INVERT;
  735 
  736         /*
  737          * Send command to changer.
  738          */
  739         return (scsipi_command(sc->sc_periph, NULL,
  740             (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
  741             100000, NULL, 0));
  742 }
  743 
  744 /*
  745  * Perform a READ ELEMENT STATUS on behalf of the user, and return to
  746  * the user only the data the user is interested in.  This returns the
  747  * old data format.
  748  */
  749 int
  750 ch_ousergetelemstatus(sc, chet, uptr)
  751         struct ch_softc *sc;
  752         int chet;
  753         u_int8_t *uptr;
  754 {
  755         struct read_element_status_header *st_hdrp, st_hdr;
  756         struct read_element_status_page_header *pg_hdrp;
  757         struct read_element_status_descriptor *desc;
  758         size_t size, desclen;
  759         caddr_t data;
  760         int avail, i, error = 0;
  761         u_int8_t user_data;
  762 
  763         /*
  764          * If there are no elements of the requested type in the changer,
  765          * the request is invalid.
  766          */
  767         if (sc->sc_counts[chet] == 0)
  768                 return (EINVAL);
  769 
  770         /*
  771          * Do the request the user wants, but only read the status header.
  772          * This will tell us the amount of storage we must allocate in
  773          * order to read all data.
  774          */
  775         error = ch_getelemstatus(sc, sc->sc_firsts[chet],
  776             sc->sc_counts[chet], &st_hdr, sizeof(st_hdr),
  777             XS_CTL_DATA_ONSTACK, 0);
  778         if (error)
  779                 return (error);
  780 
  781         size = sizeof(struct read_element_status_header) +
  782             _3btol(st_hdr.nbytes);
  783 
  784         /*
  785          * We must have at least room for the status header and
  786          * one page header (since we only ask for one element type
  787          * at a time).
  788          */
  789         if (size < (sizeof(struct read_element_status_header) +
  790             sizeof(struct read_element_status_page_header)))
  791                 return (EIO);
  792 
  793         /*
  794          * Allocate the storage and do the request again.
  795          */
  796         data = malloc(size, M_DEVBUF, M_WAITOK);
  797         error = ch_getelemstatus(sc, sc->sc_firsts[chet],
  798             sc->sc_counts[chet], data, size, 0, 0);
  799         if (error)
  800                 goto done;
  801 
  802         st_hdrp = (struct read_element_status_header *)data;
  803         pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
  804             sizeof(struct read_element_status_header));
  805         desclen = _2btol(pg_hdrp->edl);
  806 
  807         /*
  808          * Fill in the user status array.
  809          */
  810         avail = _2btol(st_hdrp->count);
  811 
  812         if (avail != sc->sc_counts[chet])
  813                 printf("%s: warning, READ ELEMENT STATUS avail != count\n",
  814                     sc->sc_dev.dv_xname);
  815 
  816         desc = (struct read_element_status_descriptor *)((u_long)data +
  817             sizeof(struct read_element_status_header) +
  818             sizeof(struct read_element_status_page_header));
  819         for (i = 0; i < avail; ++i) {
  820                 user_data = desc->flags1;
  821                 error = copyout(&user_data, &uptr[i], avail);
  822                 if (error)
  823                         break;
  824                 desc = (struct read_element_status_descriptor *)((u_long)desc
  825                     + desclen);
  826         }
  827 
  828  done:
  829         if (data != NULL)
  830                 free(data, M_DEVBUF);
  831         return (error);
  832 }
  833 
  834 /*
  835  * Perform a READ ELEMENT STATUS on behalf of the user.  This returns
  836  * the new (more complete) data format.
  837  */
  838 int
  839 ch_usergetelemstatus(sc, cesr)
  840         struct ch_softc *sc;
  841         struct changer_element_status_request *cesr;
  842 {
  843         struct scsipi_channel *chan = sc->sc_periph->periph_channel;
  844         struct scsipi_periph *dtperiph;
  845         struct read_element_status_header *st_hdrp, st_hdr;
  846         struct read_element_status_page_header *pg_hdrp;
  847         struct read_element_status_descriptor *desc;
  848         struct changer_volume_tag *avol, *pvol;
  849         size_t size, desclen, stddesclen, offset;
  850         int first, avail, i, error = 0;
  851         caddr_t data;
  852         void *uvendptr;
  853         struct changer_element_status ces;
  854 
  855         /*
  856          * Check arguments.
  857          */
  858         if (cesr->cesr_type > CHET_DT)
  859                 return (EINVAL);
  860         if (sc->sc_counts[cesr->cesr_type] == 0)
  861                 return (ENODEV);
  862         if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1))
  863                 return (ENODEV);
  864         if (cesr->cesr_count >
  865             (sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit))
  866                 return (EINVAL);
  867 
  868         /*
  869          * Do the request the user wants, but only read the status header.
  870          * This will tell us the amount of storage we must allocate
  871          * in order to read all the data.
  872          */
  873         error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
  874             cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0,
  875             cesr->cesr_flags);
  876         if (error)
  877                 return (error);
  878 
  879         size = sizeof(struct read_element_status_header) +
  880             _3btol(st_hdr.nbytes);
  881 
  882         /*
  883          * We must have at least room for the status header and
  884          * one page header (since we only ask for oen element type
  885          * at a time).
  886          */
  887         if (size < (sizeof(struct read_element_status_header) +
  888             sizeof(struct read_element_status_page_header)))
  889                 return (EIO);
  890 
  891         /*
  892          * Allocate the storage and do the request again.
  893          */
  894         data = malloc(size, M_DEVBUF, M_WAITOK);
  895         error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
  896             cesr->cesr_unit, cesr->cesr_count, data, size, 0,
  897             cesr->cesr_flags);
  898         if (error)
  899                 goto done;
  900 
  901         st_hdrp = (struct read_element_status_header *)data;
  902         pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
  903             sizeof(struct read_element_status_header));
  904         desclen = _2btol(pg_hdrp->edl);
  905 
  906         /*
  907          * Fill in the user status array.
  908          */
  909         first = _2btol(st_hdrp->fear);
  910         if (first <  (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) ||
  911             first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit +
  912                       cesr->cesr_count)) {
  913                 error = EIO;
  914                 goto done;
  915         }
  916         first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit;
  917 
  918         avail = _2btol(st_hdrp->count);
  919         if (avail <= 0 || avail > cesr->cesr_count) {
  920                 error = EIO;
  921                 goto done;
  922         }
  923 
  924         offset = sizeof(struct read_element_status_header) +
  925                  sizeof(struct read_element_status_page_header);
  926 
  927         for (i = 0; i < cesr->cesr_count; i++) {
  928                 memset(&ces, 0, sizeof(ces));
  929                 if (i < first || i >= (first + avail)) {
  930                         error = copyout(&ces, &cesr->cesr_data[i],
  931                             sizeof(ces));
  932                         if (error)
  933                                 goto done;
  934                 }
  935 
  936                 desc = (struct read_element_status_descriptor *)
  937                     (data + offset);
  938                 stddesclen = sizeof(struct read_element_status_descriptor);
  939                 offset += desclen;
  940 
  941                 ces.ces_flags = CESTATUS_STATUS_VALID;
  942 
  943                 /*
  944                  * The SCSI flags conveniently map directly to the
  945                  * chio API flags.
  946                  */
  947                 ces.ces_flags |= (desc->flags1 & 0x3f);
  948 
  949                 ces.ces_asc = desc->sense_code;
  950                 ces.ces_ascq = desc->sense_qual;
  951 
  952                 /*
  953                  * For Data Transport elemenets, get the SCSI ID and LUN,
  954                  * and attempt to map them to a device name if they're
  955                  * on the same SCSI bus.
  956                  */
  957                 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
  958                         ces.ces_target = desc->dt_scsi_addr;
  959                         ces.ces_flags |= CESTATUS_TARGET_VALID;
  960                 }
  961                 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) {
  962                         ces.ces_lun = desc->dt_scsi_flags &
  963                             READ_ELEMENT_STATUS_DT_LUNMASK;
  964                         ces.ces_flags |= CESTATUS_LUN_VALID;
  965                 }
  966                 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS)
  967                         ces.ces_flags |= CESTATUS_NOTBUS;
  968                 else if ((ces.ces_flags &
  969                           (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) ==
  970                          (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) {
  971                         if (ces.ces_target < chan->chan_ntargets &&
  972                             ces.ces_lun < chan->chan_nluns &&
  973                             (dtperiph = scsipi_lookup_periph(chan,
  974                              ces.ces_target, ces.ces_lun)) != NULL &&
  975                             dtperiph->periph_dev != NULL) {
  976                                 strcpy(ces.ces_xname,
  977                                     dtperiph->periph_dev->dv_xname);
  978                                 ces.ces_flags |= CESTATUS_XNAME_VALID;
  979                         }
  980                 }
  981 
  982                 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
  983                         ces.ces_flags |= CESTATUS_INVERTED;
  984 
  985                 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
  986                         if (ch_map_element(sc, _2btol(desc->ssea),
  987                             &ces.ces_from_type, &ces.ces_from_unit))
  988                                 ces.ces_flags |= CESTATUS_FROM_VALID;
  989                 }
  990 
  991                 /*
  992                  * Extract volume tag information.
  993                  */
  994                 switch (pg_hdrp->flags &
  995                     (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG)) {
  996                 case (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG):
  997                         pvol = (struct changer_volume_tag *)(desc + 1);
  998                         avol = pvol + 1;
  999                         break;
 1000 
 1001                 case READ_ELEMENT_STATUS_PVOLTAG:
 1002                         pvol = (struct changer_volume_tag *)(desc + 1);
 1003                         avol = NULL;
 1004                         break;
 1005 
 1006                 case READ_ELEMENT_STATUS_AVOLTAG:
 1007                         pvol = NULL;
 1008                         avol = (struct changer_volume_tag *)(desc + 1);
 1009                         break;
 1010 
 1011                 default:
 1012                         avol = pvol = NULL;
 1013                         break;
 1014                 }
 1015 
 1016                 if (pvol != NULL) {
 1017                         ch_voltag_convert_in(pvol, &ces.ces_pvoltag);
 1018                         ces.ces_flags |= CESTATUS_PVOL_VALID;
 1019                         stddesclen += sizeof(struct changer_volume_tag);
 1020                 }
 1021                 if (avol != NULL) {
 1022                         ch_voltag_convert_in(avol, &ces.ces_avoltag);
 1023                         ces.ces_flags |= CESTATUS_AVOL_VALID;
 1024                         stddesclen += sizeof(struct changer_volume_tag);
 1025                 }
 1026 
 1027                 /*
 1028                  * Compute vendor-specific length.  Note the 4 reserved
 1029                  * bytes between the volume tags and the vendor-specific
 1030                  * data.  Copy it out of the user wants it.
 1031                  */
 1032                 stddesclen += 4;
 1033                 if (desclen > stddesclen)
 1034                         ces.ces_vendor_len = desclen - stddesclen;
 1035 
 1036                 if (ces.ces_vendor_len != 0 && cesr->cesr_vendor_data != NULL) {
 1037                         error = copyin(&cesr->cesr_vendor_data[i], &uvendptr,
 1038                             sizeof(uvendptr));
 1039                         if (error)
 1040                                 goto done;
 1041                         error = copyout((void *)((u_long)desc + stddesclen),
 1042                             uvendptr, ces.ces_vendor_len);
 1043                         if (error)
 1044                                 goto done;
 1045                 }
 1046 
 1047                 /*
 1048                  * Now copy out the status descriptor we've constructed.
 1049                  */
 1050                 error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces));
 1051                 if (error)
 1052                         goto done;
 1053         }
 1054 
 1055  done:
 1056         if (data != NULL)
 1057                 free(data, M_DEVBUF);
 1058         return (error);
 1059 }
 1060 
 1061 int
 1062 ch_getelemstatus(sc, first, count, data, datalen, scsiflags, flags)
 1063         struct ch_softc *sc;
 1064         int first, count;
 1065         void *data;
 1066         size_t datalen;
 1067         int scsiflags;
 1068         int flags;
 1069 {
 1070         struct scsi_read_element_status cmd;
 1071 
 1072         /*
 1073          * Build SCSI command.
 1074          */
 1075         memset(&cmd, 0, sizeof(cmd));
 1076         cmd.opcode = READ_ELEMENT_STATUS;
 1077         cmd.byte2 = ELEMENT_TYPE_ALL;
 1078         if (flags & CESR_VOLTAGS)
 1079                 cmd.byte2 |= READ_ELEMENT_STATUS_VOLTAG;
 1080         _lto2b(first, cmd.sea);
 1081         _lto2b(count, cmd.count);
 1082         _lto3b(datalen, cmd.len);
 1083 
 1084         /*
 1085          * Send command to changer.
 1086          */
 1087         return (scsipi_command(sc->sc_periph, NULL,
 1088             (struct scsipi_generic *)&cmd, sizeof(cmd),
 1089             (u_char *)data, datalen, CHRETRIES, 100000, NULL,
 1090             scsiflags | XS_CTL_DATA_IN));
 1091 }
 1092 
 1093 int
 1094 ch_setvoltag(sc, csvr)
 1095         struct ch_softc *sc;
 1096         struct changer_set_voltag_request *csvr;
 1097 {
 1098         struct scsi_send_volume_tag cmd;
 1099         struct changer_volume_tag voltag;
 1100         void *data = NULL;
 1101         size_t datalen = 0;
 1102         int error;
 1103         u_int16_t dst;
 1104 
 1105         /*
 1106          * Check arguments.
 1107          */
 1108         if (csvr->csvr_type > CHET_DT)
 1109                 return (EINVAL);
 1110         if (csvr->csvr_unit > (sc->sc_counts[csvr->csvr_type] - 1))
 1111                 return (ENODEV);
 1112 
 1113         dst = sc->sc_firsts[csvr->csvr_type] + csvr->csvr_unit;
 1114 
 1115         /*
 1116          * Build the SCSI command.
 1117          */
 1118         memset(&cmd, 0, sizeof(cmd));
 1119         cmd.opcode = SEND_VOLUME_TAG;
 1120         _lto2b(dst, cmd.eaddr);
 1121 
 1122 #define ALTERNATE       (csvr->csvr_flags & CSVR_ALTERNATE)
 1123 
 1124         switch (csvr->csvr_flags & CSVR_MODE_MASK) {
 1125         case CSVR_MODE_SET:
 1126                 cmd.sac = ALTERNATE ? SAC_ASSERT_ALT : SAC_ASSERT_PRIMARY;
 1127                 break;
 1128 
 1129         case CSVR_MODE_REPLACE:
 1130                 cmd.sac = ALTERNATE ? SAC_REPLACE_ALT : SAC_REPLACE_PRIMARY;
 1131                 break;
 1132 
 1133         case CSVR_MODE_CLEAR:
 1134                 cmd.sac = ALTERNATE ? SAC_UNDEFINED_ALT : SAC_UNDEFINED_PRIMARY;
 1135                 break;
 1136 
 1137         default:
 1138                 return (EINVAL);
 1139         }
 1140 
 1141 #undef ALTERNATE
 1142 
 1143         if (cmd.sac < SAC_UNDEFINED_PRIMARY) {
 1144                 error = ch_voltag_convert_out(&csvr->csvr_voltag, &voltag);
 1145                 if (error)
 1146                         return (error);
 1147                 data = &voltag;
 1148                 datalen = sizeof(voltag);
 1149                 _lto2b(datalen, cmd.length);
 1150         }
 1151 
 1152         /*
 1153          * Send command to changer.
 1154          */
 1155         return (scsipi_command(sc->sc_periph, NULL,
 1156             (struct scsipi_generic *)&cmd, sizeof(cmd),
 1157             (u_char *)data, datalen, CHRETRIES, 100000, NULL,
 1158             datalen ? XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK : 0));
 1159 }
 1160 
 1161 int
 1162 ch_ielem(sc)
 1163         struct ch_softc *sc;
 1164 {
 1165         int tmo;
 1166         struct scsi_initialize_element_status cmd;
 1167 
 1168         /*
 1169          * Build SCSI command.
 1170          */
 1171         memset(&cmd, 0, sizeof(cmd));
 1172         cmd.opcode = INITIALIZE_ELEMENT_STATUS;
 1173 
 1174         /*
 1175          * Send command to changer.
 1176          *
 1177          * The problem is, how long to allow for the command?
 1178          * It can take a *really* long time, and also depends
 1179          * on unknowable factors such as whether there are
 1180          * *almost* readable labels on tapes that a barcode
 1181          * reader is trying to decipher.
 1182          *
 1183          * I'm going to make this long enough to allow 5 minutes
 1184          * per element plus an initial 10 minute wait.
 1185          */
 1186         tmo =   sc->sc_counts[CHET_MT] +
 1187                 sc->sc_counts[CHET_ST] +
 1188                 sc->sc_counts[CHET_IE] +
 1189                 sc->sc_counts[CHET_DT];
 1190         tmo *= 5 * 60 * 1000;
 1191         tmo += (10 * 60 * 1000);
 1192 
 1193         return (scsipi_command(sc->sc_periph, NULL,
 1194             (struct scsipi_generic *)&cmd, sizeof(cmd),
 1195             NULL, 0, CHRETRIES, tmo, NULL, XS_CTL_IGNORE_ILLEGAL_REQUEST));
 1196 }
 1197 
 1198 /*
 1199  * Ask the device about itself and fill in the parameters in our
 1200  * softc.
 1201  */
 1202 int
 1203 ch_get_params(sc, scsiflags)
 1204         struct ch_softc *sc;
 1205         int scsiflags;
 1206 {
 1207         struct scsi_mode_sense_data {
 1208                 struct scsipi_mode_header header;
 1209                 union {
 1210                         struct page_element_address_assignment ea;
 1211                         struct page_transport_geometry_parameters tg;
 1212                         struct page_device_capabilities cap;
 1213                 } pages;
 1214         } sense_data;
 1215         int error, from;
 1216         u_int8_t *moves, *exchanges;
 1217 
 1218         /*
 1219          * Grab info from the element address assignment page.
 1220          */
 1221         memset(&sense_data, 0, sizeof(sense_data));
 1222         error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1d,
 1223             &sense_data.header, sizeof(sense_data),
 1224             scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
 1225         if (error) {
 1226                 printf("%s: could not sense element address page\n",
 1227                     sc->sc_dev.dv_xname);
 1228                 return (error);
 1229         }
 1230 
 1231         sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
 1232         sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
 1233         sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
 1234         sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
 1235         sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
 1236         sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
 1237         sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
 1238         sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
 1239 
 1240         /* XXX ask for transport geometry page XXX */
 1241 
 1242         /*
 1243          * Grab info from the capabilities page.
 1244          */
 1245         memset(&sense_data, 0, sizeof(sense_data));
 1246         /*
 1247          * XXX: Note: not all changers can deal with disabled block descriptors
 1248          */
 1249         error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1f,
 1250             &sense_data.header, sizeof(sense_data),
 1251             scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
 1252         if (error) {
 1253                 printf("%s: could not sense capabilities page\n",
 1254                     sc->sc_dev.dv_xname);
 1255                 return (error);
 1256         }
 1257 
 1258         memset(sc->sc_movemask, 0, sizeof(sc->sc_movemask));
 1259         memset(sc->sc_exchangemask, 0, sizeof(sc->sc_exchangemask));
 1260         moves = &sense_data.pages.cap.move_from_mt;
 1261         exchanges = &sense_data.pages.cap.exchange_with_mt;
 1262         for (from = CHET_MT; from <= CHET_DT; ++from) {
 1263                 sc->sc_movemask[from] = moves[from];
 1264                 sc->sc_exchangemask[from] = exchanges[from];
 1265         }
 1266 
 1267 #ifdef CH_AUTOMATIC_IELEM_POLICY
 1268         /*
 1269          * If we need to do an Init-Element-Status,
 1270          * do that now that we know what's in the changer.
 1271          */
 1272         if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) {
 1273                 if ((sc->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 1274                         error = ch_ielem(sc);
 1275                 if (error == 0)
 1276                         sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
 1277                 else
 1278                         sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
 1279         }
 1280 #endif
 1281         return (error);
 1282 }
 1283 
 1284 void
 1285 ch_get_quirks(sc, inqbuf)
 1286         struct ch_softc *sc;
 1287         struct scsipi_inquiry_pattern *inqbuf;
 1288 {
 1289         struct chquirk *match;
 1290         int priority;
 1291 
 1292         sc->sc_settledelay = 0;
 1293 
 1294         match = (struct chquirk *)scsipi_inqmatch(inqbuf,
 1295             (caddr_t)chquirks,
 1296             sizeof(chquirks) / sizeof(chquirks[0]),
 1297             sizeof(chquirks[0]), &priority);
 1298         if (priority != 0)
 1299                 sc->sc_settledelay = match->cq_settledelay;
 1300 }
 1301 
 1302 int
 1303 ch_map_element(sc, elem, typep, unitp)
 1304         struct ch_softc *sc;
 1305         u_int16_t elem;
 1306         int *typep, *unitp;
 1307 {
 1308         int chet;
 1309 
 1310         for (chet = CHET_MT; chet <= CHET_DT; chet++) {
 1311                 if (elem >= sc->sc_firsts[chet] &&
 1312                     elem < (sc->sc_firsts[chet] + sc->sc_counts[chet])) {
 1313                         *typep = chet;
 1314                         *unitp = elem - sc->sc_firsts[chet];
 1315                         return (1);
 1316                 }
 1317         }
 1318         return (0);
 1319 }
 1320 
 1321 void
 1322 ch_voltag_convert_in(sv, cv)
 1323         const struct changer_volume_tag *sv;
 1324         struct changer_voltag *cv;
 1325 {
 1326         int i;
 1327 
 1328         memset(cv, 0, sizeof(struct changer_voltag));
 1329 
 1330         /*
 1331          * Copy the volume tag string from the SCSI representation.
 1332          * Per the SCSI-2 spec, we stop at the first blank character.
 1333          */
 1334         for (i = 0; i < sizeof(sv->volid); i++) {
 1335                 if (sv->volid[i] == ' ')
 1336                         break;
 1337                 cv->cv_tag[i] = sv->volid[i];
 1338         }
 1339         cv->cv_tag[i] = '\0';
 1340 
 1341         cv->cv_serial = _2btol(sv->volseq);
 1342 }
 1343 
 1344 int
 1345 ch_voltag_convert_out(cv, sv)
 1346         const struct changer_voltag *cv;
 1347         struct changer_volume_tag *sv;
 1348 {
 1349         int i;
 1350 
 1351         memset(sv, ' ', sizeof(struct changer_volume_tag));
 1352 
 1353         for (i = 0; i < sizeof(sv->volid); i++) {
 1354                 if (cv->cv_tag[i] == '\0')
 1355                         break;
 1356                 /*
 1357                  * Limit the character set to what is suggested in
 1358                  * the SCSI-2 spec.
 1359                  */
 1360                 if ((cv->cv_tag[i] < '' || cv->cv_tag[i] > '9') &&
 1361                     (cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') &&
 1362                     (cv->cv_tag[i] != '_'))
 1363                         return (EINVAL);
 1364                 sv->volid[i] = cv->cv_tag[i];
 1365         }
 1366 
 1367         _lto2b(cv->cv_serial, sv->volseq);
 1368 
 1369         return (0);
 1370 }

Cache object: 9937a13a7fb0f8be96ef6fcffb7f6fb8


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