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_cd.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, 2000, 2001, 2002, 2003 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  * Portions of this driver taken from the original FreeBSD cd driver.
   30  * Written by Julian Elischer (julian@tfs.com)
   31  * for TRW Financial Systems for use under the MACH(2.5) operating system.
   32  *
   33  * TRW Financial Systems, in accordance with their agreement with Carnegie
   34  * Mellon University, makes this software available to CMU to distribute
   35  * or use in any manner that they see fit as long as this message is kept with
   36  * the software. For this reason TFS also grants any other persons or
   37  * organisations permission to use or modify this software.
   38  *
   39  * TFS supplies this software to be publicly redistributed
   40  * on the understanding that TFS is not responsible for the correct
   41  * functioning of this software in any circumstances.
   42  *
   43  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
   44  *
   45  *      from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
   46  */
   47 
   48 #include <sys/cdefs.h>
   49 __FBSDID("$FreeBSD$");
   50 
   51 #include "opt_cd.h"
   52 
   53 #include <sys/param.h>
   54 #include <sys/systm.h>
   55 #include <sys/kernel.h>
   56 #include <sys/bio.h>
   57 #include <sys/conf.h>
   58 #include <sys/disk.h>
   59 #include <sys/malloc.h>
   60 #include <sys/cdio.h>
   61 #include <sys/cdrio.h>
   62 #include <sys/dvdio.h>
   63 #include <sys/devicestat.h>
   64 #include <sys/sysctl.h>
   65 #include <sys/taskqueue.h>
   66 #include <geom/geom_disk.h>
   67 
   68 #include <cam/cam.h>
   69 #include <cam/cam_ccb.h>
   70 #include <cam/cam_periph.h>
   71 #include <cam/cam_xpt_periph.h>
   72 #include <cam/cam_queue.h>
   73 
   74 #include <cam/scsi/scsi_message.h>
   75 #include <cam/scsi/scsi_da.h>
   76 #include <cam/scsi/scsi_cd.h>
   77 
   78 #define LEADOUT         0xaa            /* leadout toc entry */
   79 
   80 struct cd_params {
   81         u_int32_t blksize;
   82         u_long    disksize;
   83 };
   84 
   85 typedef enum {
   86         CD_Q_NONE               = 0x00,
   87         CD_Q_NO_TOUCH           = 0x01,
   88         CD_Q_BCD_TRACKS         = 0x02,
   89         CD_Q_NO_CHANGER         = 0x04,
   90         CD_Q_CHANGER            = 0x08,
   91         CD_Q_10_BYTE_ONLY       = 0x10
   92 } cd_quirks;
   93 
   94 typedef enum {
   95         CD_FLAG_INVALID         = 0x0001,
   96         CD_FLAG_NEW_DISC        = 0x0002,
   97         CD_FLAG_DISC_LOCKED     = 0x0004,
   98         CD_FLAG_DISC_REMOVABLE  = 0x0008,
   99         CD_FLAG_TAGGED_QUEUING  = 0x0010,
  100         CD_FLAG_CHANGER         = 0x0040,
  101         CD_FLAG_ACTIVE          = 0x0080,
  102         CD_FLAG_SCHED_ON_COMP   = 0x0100,
  103         CD_FLAG_RETRY_UA        = 0x0200,
  104         CD_FLAG_VALID_MEDIA     = 0x0400,
  105         CD_FLAG_VALID_TOC       = 0x0800,
  106         CD_FLAG_SCTX_INIT       = 0x1000
  107 } cd_flags;
  108 
  109 typedef enum {
  110         CD_CCB_PROBE            = 0x01,
  111         CD_CCB_BUFFER_IO        = 0x02,
  112         CD_CCB_WAITING          = 0x03,
  113         CD_CCB_TYPE_MASK        = 0x0F,
  114         CD_CCB_RETRY_UA         = 0x10
  115 } cd_ccb_state;
  116 
  117 typedef enum {
  118         CHANGER_TIMEOUT_SCHED           = 0x01,
  119         CHANGER_SHORT_TMOUT_SCHED       = 0x02,
  120         CHANGER_MANUAL_CALL             = 0x04,
  121         CHANGER_NEED_TIMEOUT            = 0x08
  122 } cd_changer_flags;
  123 
  124 #define ccb_state ppriv_field0
  125 #define ccb_bp ppriv_ptr1
  126 
  127 struct cd_tocdata {
  128         struct ioc_toc_header header;
  129         struct cd_toc_entry entries[100];
  130 };
  131 
  132 struct cd_toc_single {
  133         struct ioc_toc_header header;
  134         struct cd_toc_entry entry;
  135 };
  136 
  137 typedef enum {
  138         CD_STATE_PROBE,
  139         CD_STATE_NORMAL
  140 } cd_state;
  141 
  142 struct cd_softc {
  143         cam_pinfo               pinfo;
  144         cd_state                state;
  145         volatile cd_flags       flags;
  146         struct bio_queue_head   bio_queue;
  147         LIST_HEAD(, ccb_hdr)    pending_ccbs;
  148         struct cd_params        params;
  149         union ccb               saved_ccb;
  150         cd_quirks               quirks;
  151         STAILQ_ENTRY(cd_softc)  changer_links;
  152         struct cdchanger        *changer;
  153         int                     bufs_left;
  154         struct cam_periph       *periph;
  155         int                     minimum_command_size;
  156         int                     outstanding_cmds;
  157         struct task             sysctl_task;
  158         struct sysctl_ctx_list  sysctl_ctx;
  159         struct sysctl_oid       *sysctl_tree;
  160         STAILQ_HEAD(, cd_mode_params)   mode_queue;
  161         struct cd_tocdata       toc;
  162         struct disk             *disk;
  163 };
  164 
  165 struct cd_page_sizes {
  166         int page;
  167         int page_size;
  168 };
  169 
  170 static struct cd_page_sizes cd_page_size_table[] =
  171 {
  172         { AUDIO_PAGE, sizeof(struct cd_audio_page)}
  173 };
  174 
  175 struct cd_quirk_entry {
  176         struct scsi_inquiry_pattern inq_pat;
  177         cd_quirks quirks;
  178 };
  179 
  180 /*
  181  * The changer quirk entries aren't strictly necessary.  Basically, what
  182  * they do is tell cdregister() up front that a device is a changer.
  183  * Otherwise, it will figure that fact out once it sees a LUN on the device
  184  * that is greater than 0.  If it is known up front that a device is a changer,
  185  * all I/O to the device will go through the changer scheduling routines, as
  186  * opposed to the "normal" CD code.
  187  *
  188  * NOTE ON 10_BYTE_ONLY quirks:  Any 10_BYTE_ONLY quirks MUST be because
  189  * your device hangs when it gets a 10 byte command.  Adding a quirk just
  190  * to get rid of the informative diagnostic message is not acceptable.  All
  191  * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
  192  * referenced in a comment along with the quirk) , and must be approved by
  193  * ken@FreeBSD.org.  Any quirks added that don't adhere to this policy may
  194  * be removed until the submitter can explain why they are needed.
  195  * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
  196  * when the CAM_NEW_TRAN_CODE work is done.
  197  */
  198 static struct cd_quirk_entry cd_quirk_table[] =
  199 {
  200         {
  201                 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
  202                  /*quirks*/ CD_Q_CHANGER
  203         },
  204         {
  205                 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
  206                   "*"}, /* quirks */ CD_Q_CHANGER
  207         },
  208         {
  209                 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
  210                  /* quirks */ CD_Q_CHANGER
  211         },
  212         {
  213                 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
  214                 /* quirks */ CD_Q_BCD_TRACKS
  215         }
  216 };
  217 
  218 static  disk_open_t     cdopen;
  219 static  disk_close_t    cdclose;
  220 static  disk_ioctl_t    cdioctl;
  221 static  disk_strategy_t cdstrategy;
  222 
  223 static  periph_init_t   cdinit;
  224 static  periph_ctor_t   cdregister;
  225 static  periph_dtor_t   cdcleanup;
  226 static  periph_start_t  cdstart;
  227 static  periph_oninv_t  cdoninvalidate;
  228 static  void            cdasync(void *callback_arg, u_int32_t code,
  229                                 struct cam_path *path, void *arg);
  230 static  int             cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
  231 static  void            cdshorttimeout(void *arg);
  232 static  void            cdschedule(struct cam_periph *periph, int priority);
  233 static  void            cdrunchangerqueue(void *arg);
  234 static  void            cdchangerschedule(struct cd_softc *softc);
  235 static  int             cdrunccb(union ccb *ccb,
  236                                  int (*error_routine)(union ccb *ccb,
  237                                                       u_int32_t cam_flags,
  238                                                       u_int32_t sense_flags),
  239                                  u_int32_t cam_flags, u_int32_t sense_flags);
  240 static  union ccb       *cdgetccb(struct cam_periph *periph,
  241                                   u_int32_t priority);
  242 static  void            cddone(struct cam_periph *periph,
  243                                union ccb *start_ccb);
  244 static  union cd_pages  *cdgetpage(struct cd_mode_params *mode_params);
  245 static  int             cdgetpagesize(int page_num);
  246 static  void            cdprevent(struct cam_periph *periph, int action);
  247 static  int             cdcheckmedia(struct cam_periph *periph);
  248 static  int             cdsize(struct cam_periph *periph, u_int32_t *size);
  249 static  int             cd6byteworkaround(union ccb *ccb);
  250 static  int             cderror(union ccb *ccb, u_int32_t cam_flags,
  251                                 u_int32_t sense_flags);
  252 static  int             cdreadtoc(struct cam_periph *periph, u_int32_t mode, 
  253                                   u_int32_t start, u_int8_t *data, 
  254                                   u_int32_t len, u_int32_t sense_flags);
  255 static  int             cdgetmode(struct cam_periph *periph, 
  256                                   struct cd_mode_params *data, u_int32_t page);
  257 static  int             cdsetmode(struct cam_periph *periph,
  258                                   struct cd_mode_params *data);
  259 static  int             cdplay(struct cam_periph *periph, u_int32_t blk, 
  260                                u_int32_t len);
  261 static  int             cdreadsubchannel(struct cam_periph *periph, 
  262                                          u_int32_t mode, u_int32_t format, 
  263                                          int track, 
  264                                          struct cd_sub_channel_info *data, 
  265                                          u_int32_t len);
  266 static  int             cdplaymsf(struct cam_periph *periph, u_int32_t startm, 
  267                                   u_int32_t starts, u_int32_t startf, 
  268                                   u_int32_t endm, u_int32_t ends, 
  269                                   u_int32_t endf);
  270 static  int             cdplaytracks(struct cam_periph *periph, 
  271                                      u_int32_t strack, u_int32_t sindex,
  272                                      u_int32_t etrack, u_int32_t eindex);
  273 static  int             cdpause(struct cam_periph *periph, u_int32_t go);
  274 static  int             cdstopunit(struct cam_periph *periph, u_int32_t eject);
  275 static  int             cdstartunit(struct cam_periph *periph, int load);
  276 static  int             cdsetspeed(struct cam_periph *periph,
  277                                    u_int32_t rdspeed, u_int32_t wrspeed);
  278 static  int             cdreportkey(struct cam_periph *periph,
  279                                     struct dvd_authinfo *authinfo);
  280 static  int             cdsendkey(struct cam_periph *periph,
  281                                   struct dvd_authinfo *authinfo);
  282 static  int             cdreaddvdstructure(struct cam_periph *periph,
  283                                            struct dvd_struct *dvdstruct);
  284 
  285 static struct periph_driver cddriver =
  286 {
  287         cdinit, "cd",
  288         TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
  289 };
  290 
  291 PERIPHDRIVER_DECLARE(cd, cddriver);
  292 
  293 
  294 static int num_changers;
  295 
  296 #ifndef CHANGER_MIN_BUSY_SECONDS
  297 #define CHANGER_MIN_BUSY_SECONDS        5
  298 #endif
  299 #ifndef CHANGER_MAX_BUSY_SECONDS
  300 #define CHANGER_MAX_BUSY_SECONDS        15
  301 #endif
  302 
  303 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
  304 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
  305 
  306 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
  307 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
  308 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
  309            &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
  310 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
  311 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
  312            &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
  313 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
  314 
  315 struct cdchanger {
  316         path_id_t                        path_id;
  317         target_id_t                      target_id;
  318         int                              num_devices;
  319         struct camq                      devq;
  320         struct timeval                   start_time;
  321         struct cd_softc                  *cur_device;
  322         struct callout_handle            short_handle;
  323         struct callout_handle            long_handle;
  324         volatile cd_changer_flags        flags;
  325         STAILQ_ENTRY(cdchanger)          changer_links;
  326         STAILQ_HEAD(chdevlist, cd_softc) chluns;
  327 };
  328 
  329 static STAILQ_HEAD(changerlist, cdchanger) changerq;
  330 
  331 
  332 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
  333 
  334 static void
  335 cdinit(void)
  336 {
  337         cam_status status;
  338         struct cam_path *path;
  339 
  340         /*
  341          * Install a global async callback.  This callback will
  342          * receive async callbacks like "new device found".
  343          */
  344         status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
  345                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
  346 
  347         if (status == CAM_REQ_CMP) {
  348                 struct ccb_setasync csa;
  349 
  350                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
  351                 csa.ccb_h.func_code = XPT_SASYNC_CB;
  352                 csa.event_enable = AC_FOUND_DEVICE;
  353                 csa.callback = cdasync;
  354                 csa.callback_arg = NULL;
  355                 xpt_action((union ccb *)&csa);
  356                 status = csa.ccb_h.status;
  357                 xpt_free_path(path);
  358         }
  359 
  360         if (status != CAM_REQ_CMP) {
  361                 printf("cd: Failed to attach master async callback "
  362                        "due to status 0x%x!\n", status);
  363         }
  364 }
  365 
  366 static void
  367 cdoninvalidate(struct cam_periph *periph)
  368 {
  369         int s;
  370         struct cd_softc *softc;
  371         struct ccb_setasync csa;
  372 
  373         softc = (struct cd_softc *)periph->softc;
  374 
  375         /*
  376          * De-register any async callbacks.
  377          */
  378         xpt_setup_ccb(&csa.ccb_h, periph->path,
  379                       /* priority */ 5);
  380         csa.ccb_h.func_code = XPT_SASYNC_CB;
  381         csa.event_enable = 0;
  382         csa.callback = cdasync;
  383         csa.callback_arg = periph;
  384         xpt_action((union ccb *)&csa);
  385 
  386         softc->flags |= CD_FLAG_INVALID;
  387 
  388         /*
  389          * Although the oninvalidate() routines are always called at
  390          * splsoftcam, we need to be at splbio() here to keep the buffer
  391          * queue from being modified while we traverse it.
  392          */
  393         s = splbio();
  394 
  395         /*
  396          * Return all queued I/O with ENXIO.
  397          * XXX Handle any transactions queued to the card
  398          *     with XPT_ABORT_CCB.
  399          */
  400         bioq_flush(&softc->bio_queue, NULL, ENXIO);
  401         splx(s);
  402 
  403         /*
  404          * If this device is part of a changer, and it was scheduled
  405          * to run, remove it from the run queue since we just nuked
  406          * all of its scheduled I/O.
  407          */
  408         if ((softc->flags & CD_FLAG_CHANGER)
  409          && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
  410                 camq_remove(&softc->changer->devq, softc->pinfo.index);
  411 
  412         disk_gone(softc->disk);
  413         xpt_print_path(periph->path);
  414         printf("lost device\n");
  415 }
  416 
  417 static void
  418 cdcleanup(struct cam_periph *periph)
  419 {
  420         struct cd_softc *softc;
  421         int s;
  422 
  423         softc = (struct cd_softc *)periph->softc;
  424 
  425         xpt_print_path(periph->path);
  426         printf("removing device entry\n");
  427 
  428         if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
  429             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
  430                 xpt_print_path(periph->path);
  431                 printf("can't remove sysctl context\n");
  432         }
  433 
  434         s = splsoftcam();
  435         /*
  436          * In the queued, non-active case, the device in question
  437          * has already been removed from the changer run queue.  Since this
  438          * device is active, we need to de-activate it, and schedule
  439          * another device to run.  (if there is another one to run)
  440          */
  441         if ((softc->flags & CD_FLAG_CHANGER)
  442          && (softc->flags & CD_FLAG_ACTIVE)) {
  443 
  444                 /*
  445                  * The purpose of the short timeout is soley to determine
  446                  * whether the current device has finished or not.  Well,
  447                  * since we're removing the active device, we know that it
  448                  * is finished.  So, get rid of the short timeout.
  449                  * Otherwise, if we're in the time period before the short
  450                  * timeout fires, and there are no other devices in the
  451                  * queue to run, there won't be any other device put in the
  452                  * active slot.  i.e., when we call cdrunchangerqueue()
  453                  * below, it won't do anything.  Then, when the short
  454                  * timeout fires, it'll look at the "current device", which
  455                  * we are free below, and possibly panic the kernel on a
  456                  * bogus pointer reference.
  457                  *
  458                  * The long timeout doesn't really matter, since we
  459                  * decrement the qfrozen_cnt to indicate that there is
  460                  * nothing in the active slot now.  Therefore, there won't
  461                  * be any bogus pointer references there.
  462                  */
  463                 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
  464                         untimeout(cdshorttimeout, softc->changer,
  465                                   softc->changer->short_handle);
  466                         softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
  467                 }
  468                 softc->changer->devq.qfrozen_cnt--;
  469                 softc->changer->flags |= CHANGER_MANUAL_CALL;
  470                 cdrunchangerqueue(softc->changer);
  471         }
  472 
  473         /*
  474          * If we're removing the last device on the changer, go ahead and
  475          * remove the changer device structure.
  476          */
  477         if ((softc->flags & CD_FLAG_CHANGER)
  478          && (--softc->changer->num_devices == 0)) {
  479 
  480                 /*
  481                  * Theoretically, there shouldn't be any timeouts left, but
  482                  * I'm not completely sure that that will be the case.  So,
  483                  * it won't hurt to check and see if there are any left.
  484                  */
  485                 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
  486                         untimeout(cdrunchangerqueue, softc->changer,
  487                                   softc->changer->long_handle);
  488                         softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
  489                 }
  490 
  491                 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
  492                         untimeout(cdshorttimeout, softc->changer,
  493                                   softc->changer->short_handle);
  494                         softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
  495                 }
  496 
  497                 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
  498                               changer_links);
  499                 xpt_print_path(periph->path);
  500                 printf("removing changer entry\n");
  501                 free(softc->changer, M_DEVBUF);
  502                 num_changers--;
  503         }
  504         disk_destroy(softc->disk);
  505         free(softc, M_DEVBUF);
  506         splx(s);
  507 }
  508 
  509 static void
  510 cdasync(void *callback_arg, u_int32_t code,
  511         struct cam_path *path, void *arg)
  512 {
  513         struct cam_periph *periph;
  514 
  515         periph = (struct cam_periph *)callback_arg;
  516         switch (code) {
  517         case AC_FOUND_DEVICE:
  518         {
  519                 struct ccb_getdev *cgd;
  520                 cam_status status;
  521 
  522                 cgd = (struct ccb_getdev *)arg;
  523                 if (cgd == NULL)
  524                         break;
  525 
  526                 if (SID_TYPE(&cgd->inq_data) != T_CDROM
  527                     && SID_TYPE(&cgd->inq_data) != T_WORM)
  528                         break;
  529 
  530                 /*
  531                  * Allocate a peripheral instance for
  532                  * this device and start the probe
  533                  * process.
  534                  */
  535                 status = cam_periph_alloc(cdregister, cdoninvalidate,
  536                                           cdcleanup, cdstart,
  537                                           "cd", CAM_PERIPH_BIO,
  538                                           cgd->ccb_h.path, cdasync,
  539                                           AC_FOUND_DEVICE, cgd);
  540 
  541                 if (status != CAM_REQ_CMP
  542                  && status != CAM_REQ_INPROG)
  543                         printf("cdasync: Unable to attach new device "
  544                                "due to status 0x%x\n", status);
  545 
  546                 break;
  547         }
  548         case AC_SENT_BDR:
  549         case AC_BUS_RESET:
  550         {
  551                 struct cd_softc *softc;
  552                 struct ccb_hdr *ccbh;
  553                 int s;
  554 
  555                 softc = (struct cd_softc *)periph->softc;
  556                 s = splsoftcam();
  557                 /*
  558                  * Don't fail on the expected unit attention
  559                  * that will occur.
  560                  */
  561                 softc->flags |= CD_FLAG_RETRY_UA;
  562                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
  563                         ccbh->ccb_state |= CD_CCB_RETRY_UA;
  564                 splx(s);
  565                 /* FALLTHROUGH */
  566         }
  567         default:
  568                 cam_periph_async(periph, code, path, arg);
  569                 break;
  570         }
  571 }
  572 
  573 static void
  574 cdsysctlinit(void *context, int pending)
  575 {
  576         struct cam_periph *periph;
  577         struct cd_softc *softc;
  578         char tmpstr[80], tmpstr2[80];
  579 
  580         periph = (struct cam_periph *)context;
  581         softc = (struct cd_softc *)periph->softc;
  582 
  583         snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
  584         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
  585 
  586         mtx_lock(&Giant);
  587 
  588         sysctl_ctx_init(&softc->sysctl_ctx);
  589         softc->flags |= CD_FLAG_SCTX_INIT;
  590         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
  591                 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
  592                 tmpstr2, CTLFLAG_RD, 0, tmpstr);
  593 
  594         if (softc->sysctl_tree == NULL) {
  595                 printf("cdsysctlinit: unable to allocate sysctl tree\n");
  596                 mtx_unlock(&Giant);
  597                 return;
  598         }
  599 
  600         /*
  601          * Now register the sysctl handler, so the user can the value on
  602          * the fly.
  603          */
  604         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
  605                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
  606                 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
  607                 "Minimum CDB size");
  608 
  609         mtx_unlock(&Giant);
  610 }
  611 
  612 /*
  613  * We have a handler function for this so we can check the values when the
  614  * user sets them, instead of every time we look at them.
  615  */
  616 static int
  617 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
  618 {
  619         int error, value;
  620 
  621         value = *(int *)arg1;
  622 
  623         error = sysctl_handle_int(oidp, &value, 0, req);
  624 
  625         if ((error != 0)
  626          || (req->newptr == NULL))
  627                 return (error);
  628 
  629         /*
  630          * The only real values we can have here are 6 or 10.  I don't
  631          * really forsee having 12 be an option at any time in the future.
  632          * So if the user sets something less than or equal to 6, we'll set
  633          * it to 6.  If he sets something greater than 6, we'll set it to 10.
  634          *
  635          * I suppose we could just return an error here for the wrong values,
  636          * but I don't think it's necessary to do so, as long as we can
  637          * determine the user's intent without too much trouble.
  638          */
  639         if (value < 6)
  640                 value = 6;
  641         else if (value > 6)
  642                 value = 10;
  643 
  644         *(int *)arg1 = value;
  645 
  646         return (0);
  647 }
  648 
  649 static cam_status
  650 cdregister(struct cam_periph *periph, void *arg)
  651 {
  652         struct cd_softc *softc;
  653         struct ccb_setasync csa;
  654         struct ccb_pathinq cpi;
  655         struct ccb_getdev *cgd;
  656         char tmpstr[80];
  657         caddr_t match;
  658 
  659         cgd = (struct ccb_getdev *)arg;
  660         if (periph == NULL) {
  661                 printf("cdregister: periph was NULL!!\n");
  662                 return(CAM_REQ_CMP_ERR);
  663         }
  664         if (cgd == NULL) {
  665                 printf("cdregister: no getdev CCB, can't register device\n");
  666                 return(CAM_REQ_CMP_ERR);
  667         }
  668 
  669         softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
  670 
  671         if (softc == NULL) {
  672                 printf("cdregister: Unable to probe new device. "
  673                        "Unable to allocate softc\n");                           
  674                 return(CAM_REQ_CMP_ERR);
  675         }
  676 
  677         bzero(softc, sizeof(*softc));
  678         LIST_INIT(&softc->pending_ccbs);
  679         STAILQ_INIT(&softc->mode_queue);
  680         softc->state = CD_STATE_PROBE;
  681         bioq_init(&softc->bio_queue);
  682         if (SID_IS_REMOVABLE(&cgd->inq_data))
  683                 softc->flags |= CD_FLAG_DISC_REMOVABLE;
  684         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
  685                 softc->flags |= CD_FLAG_TAGGED_QUEUING;
  686 
  687         periph->softc = softc;
  688         softc->periph = periph;
  689 
  690         /*
  691          * See if this device has any quirks.
  692          */
  693         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
  694                                (caddr_t)cd_quirk_table,
  695                                sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
  696                                sizeof(*cd_quirk_table), scsi_inquiry_match);
  697 
  698         if (match != NULL)
  699                 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
  700         else
  701                 softc->quirks = CD_Q_NONE;
  702 
  703         /* Check if the SIM does not want 6 byte commands */
  704         xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
  705         cpi.ccb_h.func_code = XPT_PATH_INQ;
  706         xpt_action((union ccb *)&cpi);
  707         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
  708                 softc->quirks |= CD_Q_10_BYTE_ONLY;
  709 
  710         TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
  711 
  712         /* The default is 6 byte commands, unless quirked otherwise */
  713         if (softc->quirks & CD_Q_10_BYTE_ONLY)
  714                 softc->minimum_command_size = 10;
  715         else
  716                 softc->minimum_command_size = 6;
  717 
  718         /*
  719          * Load the user's default, if any.
  720          */
  721         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
  722                  periph->unit_number);
  723         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
  724 
  725         /* 6 and 10 are the only permissible values here. */
  726         if (softc->minimum_command_size < 6)
  727                 softc->minimum_command_size = 6;
  728         else if (softc->minimum_command_size > 6)
  729                 softc->minimum_command_size = 10;
  730 
  731         /*
  732          * We need to register the statistics structure for this device,
  733          * but we don't have the blocksize yet for it.  So, we register
  734          * the structure and indicate that we don't have the blocksize
  735          * yet.  Unlike other SCSI peripheral drivers, we explicitly set
  736          * the device type here to be CDROM, rather than just ORing in
  737          * the device type.  This is because this driver can attach to either
  738          * CDROM or WORM devices, and we want this peripheral driver to
  739          * show up in the devstat list as a CD peripheral driver, not a
  740          * WORM peripheral driver.  WORM drives will also have the WORM
  741          * driver attached to them.
  742          */
  743         softc->disk = disk_alloc();
  744         softc->disk->d_devstat = devstat_new_entry("cd", 
  745                           periph->unit_number, 0,
  746                           DEVSTAT_BS_UNAVAILABLE,
  747                           DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
  748                           DEVSTAT_PRIORITY_CD);
  749         softc->disk->d_open = cdopen;
  750         softc->disk->d_close = cdclose;
  751         softc->disk->d_strategy = cdstrategy;
  752         softc->disk->d_ioctl = cdioctl;
  753         softc->disk->d_name = "cd";
  754         softc->disk->d_unit = periph->unit_number;
  755         softc->disk->d_drv1 = periph;
  756         softc->disk->d_flags = DISKFLAG_NEEDSGIANT;
  757         disk_create(softc->disk, DISK_VERSION);
  758 
  759         /*
  760          * Add an async callback so that we get
  761          * notified if this device goes away.
  762          */
  763         xpt_setup_ccb(&csa.ccb_h, periph->path,
  764                       /* priority */ 5);
  765         csa.ccb_h.func_code = XPT_SASYNC_CB;
  766         csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
  767         csa.callback = cdasync;
  768         csa.callback_arg = periph;
  769         xpt_action((union ccb *)&csa);
  770 
  771         /*
  772          * If the target lun is greater than 0, we most likely have a CD
  773          * changer device.  Check the quirk entries as well, though, just
  774          * in case someone has a CD tower with one lun per drive or
  775          * something like that.  Also, if we know up front that a
  776          * particular device is a changer, we can mark it as such starting
  777          * with lun 0, instead of lun 1.  It shouldn't be necessary to have
  778          * a quirk entry to define something as a changer, however.
  779          */
  780         if (((cgd->ccb_h.target_lun > 0)
  781           && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
  782          || ((softc->quirks & CD_Q_CHANGER) != 0)) {
  783                 struct cdchanger *nchanger;
  784                 struct cam_periph *nperiph;
  785                 struct cam_path *path;
  786                 cam_status status;
  787                 int found;
  788 
  789                 /* Set the changer flag in the current device's softc */
  790                 softc->flags |= CD_FLAG_CHANGER;
  791 
  792                 if (num_changers == 0)
  793                         STAILQ_INIT(&changerq);
  794 
  795                 /*
  796                  * Now, look around for an existing changer device with the
  797                  * same path and target ID as the current device.
  798                  */
  799                 for (found = 0,
  800                      nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
  801                      nchanger != NULL;
  802                      nchanger = STAILQ_NEXT(nchanger, changer_links)){
  803                         if ((nchanger->path_id == cgd->ccb_h.path_id) 
  804                          && (nchanger->target_id == cgd->ccb_h.target_id)) {
  805                                 found = 1;
  806                                 break;
  807                         }
  808                 }
  809 
  810                 /*
  811                  * If we found a matching entry, just add this device to
  812                  * the list of devices on this changer.
  813                  */
  814                 if (found == 1) {
  815                         struct chdevlist *chlunhead;
  816 
  817                         chlunhead = &nchanger->chluns;
  818 
  819                         /*
  820                          * XXX KDM look at consolidating this code with the
  821                          * code below in a separate function.
  822                          */
  823 
  824                         /*
  825                          * Create a path with lun id 0, and see if we can
  826                          * find a matching device
  827                          */
  828                         status = xpt_create_path(&path, /*periph*/ periph,
  829                                                  cgd->ccb_h.path_id,
  830                                                  cgd->ccb_h.target_id, 0);
  831 
  832                         if ((status == CAM_REQ_CMP)
  833                          && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
  834                                 struct cd_softc *nsoftc;
  835 
  836                                 nsoftc = (struct cd_softc *)nperiph->softc;
  837 
  838                                 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
  839                                         nsoftc->flags |= CD_FLAG_CHANGER;
  840                                         nchanger->num_devices++;
  841                                         if (camq_resize(&nchanger->devq,
  842                                            nchanger->num_devices)!=CAM_REQ_CMP){
  843                                                 printf("cdregister: "
  844                                                        "camq_resize "
  845                                                        "failed, changer "
  846                                                        "support may "
  847                                                        "be messed up\n");
  848                                         }
  849                                         nsoftc->changer = nchanger;
  850                                         nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
  851 
  852                                         STAILQ_INSERT_TAIL(&nchanger->chluns,
  853                                                           nsoftc,changer_links);
  854                                 }
  855                                 xpt_free_path(path);
  856                         } else if (status == CAM_REQ_CMP)
  857                                 xpt_free_path(path);
  858                         else {
  859                                 printf("cdregister: unable to allocate path\n"
  860                                        "cdregister: changer support may be "
  861                                        "broken\n");
  862                         }
  863 
  864                         nchanger->num_devices++;
  865 
  866                         softc->changer = nchanger;
  867                         softc->pinfo.index = CAM_UNQUEUED_INDEX;
  868 
  869                         if (camq_resize(&nchanger->devq,
  870                             nchanger->num_devices) != CAM_REQ_CMP) {
  871                                 printf("cdregister: camq_resize "
  872                                        "failed, changer support may "
  873                                        "be messed up\n");
  874                         }
  875 
  876                         STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
  877                 }
  878                 /*
  879                  * In this case, we don't already have an entry for this
  880                  * particular changer, so we need to create one, add it to
  881                  * the queue, and queue this device on the list for this
  882                  * changer.  Before we queue this device, however, we need
  883                  * to search for lun id 0 on this target, and add it to the
  884                  * queue first, if it exists.  (and if it hasn't already
  885                  * been marked as part of the changer.)
  886                  */
  887                 else {
  888                         nchanger = malloc(sizeof(struct cdchanger),
  889                                 M_DEVBUF, M_NOWAIT);
  890 
  891                         if (nchanger == NULL) {
  892                                 softc->flags &= ~CD_FLAG_CHANGER;
  893                                 printf("cdregister: unable to malloc "
  894                                        "changer structure\ncdregister: "
  895                                        "changer support disabled\n");
  896 
  897                                 /*
  898                                  * Yes, gotos can be gross but in this case
  899                                  * I think it's justified..
  900                                  */
  901                                 goto cdregisterexit;
  902                         }
  903 
  904                         /* zero the structure */
  905                         bzero(nchanger, sizeof(struct cdchanger));
  906 
  907                         if (camq_init(&nchanger->devq, 1) != 0) {
  908                                 softc->flags &= ~CD_FLAG_CHANGER;
  909                                 printf("cdregister: changer support "
  910                                        "disabled\n");
  911                                 goto cdregisterexit;
  912                         }
  913 
  914                         num_changers++;
  915 
  916                         nchanger->path_id = cgd->ccb_h.path_id;
  917                         nchanger->target_id = cgd->ccb_h.target_id;
  918 
  919                         /* this is superfluous, but it makes things clearer */
  920                         nchanger->num_devices = 0;
  921 
  922                         STAILQ_INIT(&nchanger->chluns);
  923 
  924                         STAILQ_INSERT_TAIL(&changerq, nchanger,
  925                                            changer_links);
  926                         
  927                         /*
  928                          * Create a path with lun id 0, and see if we can
  929                          * find a matching device
  930                          */
  931                         status = xpt_create_path(&path, /*periph*/ periph,
  932                                                  cgd->ccb_h.path_id,
  933                                                  cgd->ccb_h.target_id, 0);
  934 
  935                         /*
  936                          * If we were able to allocate the path, and if we
  937                          * find a matching device and it isn't already
  938                          * marked as part of a changer, then we add it to
  939                          * the current changer.
  940                          */
  941                         if ((status == CAM_REQ_CMP)
  942                          && ((nperiph = cam_periph_find(path, "cd")) != NULL)
  943                          && ((((struct cd_softc *)periph->softc)->flags &
  944                                CD_FLAG_CHANGER) == 0)) {
  945                                 struct cd_softc *nsoftc;
  946 
  947                                 nsoftc = (struct cd_softc *)nperiph->softc;
  948 
  949                                 nsoftc->flags |= CD_FLAG_CHANGER;
  950                                 nchanger->num_devices++;
  951                                 if (camq_resize(&nchanger->devq,
  952                                     nchanger->num_devices) != CAM_REQ_CMP) {
  953                                         printf("cdregister: camq_resize "
  954                                                "failed, changer support may "
  955                                                "be messed up\n");
  956                                 }
  957                                 nsoftc->changer = nchanger;
  958                                 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
  959 
  960                                 STAILQ_INSERT_TAIL(&nchanger->chluns,
  961                                                    nsoftc, changer_links);
  962                                 xpt_free_path(path);
  963                         } else if (status == CAM_REQ_CMP)
  964                                 xpt_free_path(path);
  965                         else {
  966                                 printf("cdregister: unable to allocate path\n"
  967                                        "cdregister: changer support may be "
  968                                        "broken\n");
  969                         }
  970 
  971                         softc->changer = nchanger;
  972                         softc->pinfo.index = CAM_UNQUEUED_INDEX;
  973                         nchanger->num_devices++;
  974                         if (camq_resize(&nchanger->devq,
  975                             nchanger->num_devices) != CAM_REQ_CMP) {
  976                                 printf("cdregister: camq_resize "
  977                                        "failed, changer support may "
  978                                        "be messed up\n");
  979                         }
  980                         STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
  981                                            changer_links);
  982                 }
  983         }
  984 
  985 cdregisterexit:
  986 
  987         /* Lock this peripheral until we are setup */
  988         /* Can't block */
  989         cam_periph_lock(periph, PRIBIO); 
  990 
  991         if ((softc->flags & CD_FLAG_CHANGER) == 0)
  992                 xpt_schedule(periph, /*priority*/5);
  993         else
  994                 cdschedule(periph, /*priority*/ 5);
  995 
  996         return(CAM_REQ_CMP);
  997 }
  998 
  999 static int
 1000 cdopen(struct disk *dp)
 1001 {
 1002         struct cam_periph *periph;
 1003         struct cd_softc *softc;
 1004         int error;
 1005         int s;
 1006 
 1007         periph = (struct cam_periph *)dp->d_drv1;
 1008         if (periph == NULL)
 1009                 return (ENXIO);
 1010 
 1011         softc = (struct cd_softc *)periph->softc;
 1012 
 1013         /*
 1014          * Grab splsoftcam and hold it until we lock the peripheral.
 1015          */
 1016         s = splsoftcam();
 1017         if (softc->flags & CD_FLAG_INVALID) {
 1018                 splx(s);
 1019                 return(ENXIO);
 1020         }
 1021 
 1022         if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
 1023                 splx(s);
 1024                 return (error);
 1025         }
 1026 
 1027         splx(s);
 1028 
 1029         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
 1030                 return(ENXIO);
 1031 
 1032         /*
 1033          * Check for media, and set the appropriate flags.  We don't bail
 1034          * if we don't have media, but then we don't allow anything but the
 1035          * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
 1036          */
 1037         cdcheckmedia(periph);
 1038 
 1039         cam_periph_unlock(periph);
 1040 
 1041         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
 1042 
 1043         return (error);
 1044 }
 1045 
 1046 static int
 1047 cdclose(struct disk *dp)
 1048 {
 1049         struct  cam_periph *periph;
 1050         struct  cd_softc *softc;
 1051         int     error;
 1052 
 1053         periph = (struct cam_periph *)dp->d_drv1;
 1054         if (periph == NULL)
 1055                 return (ENXIO); 
 1056 
 1057         softc = (struct cd_softc *)periph->softc;
 1058 
 1059         if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
 1060                 return (error);
 1061 
 1062         if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
 1063                 cdprevent(periph, PR_ALLOW);
 1064 
 1065         /*
 1066          * Since we're closing this CD, mark the blocksize as unavailable.
 1067          * It will be marked as available when the CD is opened again.
 1068          */
 1069         softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
 1070 
 1071         /*
 1072          * We'll check the media and toc again at the next open().
 1073          */
 1074         softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
 1075 
 1076         cam_periph_unlock(periph);
 1077         cam_periph_release(periph);
 1078 
 1079         return (0);
 1080 }
 1081 
 1082 static void
 1083 cdshorttimeout(void *arg)
 1084 {
 1085         struct cdchanger *changer;
 1086         int s;
 1087 
 1088         s = splsoftcam();
 1089 
 1090         changer = (struct cdchanger *)arg;
 1091 
 1092         /* Always clear the short timeout flag, since that's what we're in */
 1093         changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
 1094 
 1095         /*
 1096          * Check to see if there is any more pending or outstanding I/O for
 1097          * this device.  If not, move it out of the active slot.
 1098          */
 1099         if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
 1100          && (changer->cur_device->outstanding_cmds == 0)) {
 1101                 changer->flags |= CHANGER_MANUAL_CALL;
 1102                 cdrunchangerqueue(changer);
 1103         }
 1104 
 1105         splx(s);
 1106 }
 1107 
 1108 /*
 1109  * This is a wrapper for xpt_schedule.  It only applies to changers.
 1110  */
 1111 static void
 1112 cdschedule(struct cam_periph *periph, int priority)
 1113 {
 1114         struct cd_softc *softc;
 1115         int s;
 1116 
 1117         s = splsoftcam();
 1118 
 1119         softc = (struct cd_softc *)periph->softc;
 1120 
 1121         /*
 1122          * If this device isn't currently queued, and if it isn't
 1123          * the active device, then we queue this device and run the
 1124          * changer queue if there is no timeout scheduled to do it.
 1125          * If this device is the active device, just schedule it
 1126          * to run again.  If this device is queued, there should be
 1127          * a timeout in place already that will make sure it runs.
 1128          */
 1129         if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 
 1130          && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
 1131                 /*
 1132                  * We don't do anything with the priority here.
 1133                  * This is strictly a fifo queue.
 1134                  */
 1135                 softc->pinfo.priority = 1;
 1136                 softc->pinfo.generation = ++softc->changer->devq.generation;
 1137                 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
 1138 
 1139                 /*
 1140                  * Since we just put a device in the changer queue,
 1141                  * check and see if there is a timeout scheduled for
 1142                  * this changer.  If so, let the timeout handle
 1143                  * switching this device into the active slot.  If
 1144                  * not, manually call the timeout routine to
 1145                  * bootstrap things.
 1146                  */
 1147                 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
 1148                  && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
 1149                  && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
 1150                         softc->changer->flags |= CHANGER_MANUAL_CALL;
 1151                         cdrunchangerqueue(softc->changer);
 1152                 }
 1153         } else if ((softc->flags & CD_FLAG_ACTIVE)
 1154                 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
 1155                 xpt_schedule(periph, priority);
 1156 
 1157         splx(s);
 1158 
 1159 }
 1160 
 1161 static void
 1162 cdrunchangerqueue(void *arg)
 1163 {
 1164         struct cd_softc *softc;
 1165         struct cdchanger *changer;
 1166         int called_from_timeout;
 1167         int s;
 1168 
 1169         s = splsoftcam();
 1170 
 1171         changer = (struct cdchanger *)arg;
 1172 
 1173         /*
 1174          * If we have NOT been called from cdstrategy() or cddone(), and
 1175          * instead from a timeout routine, go ahead and clear the
 1176          * timeout flag.
 1177          */
 1178         if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
 1179                 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
 1180                 called_from_timeout = 1;
 1181         } else
 1182                 called_from_timeout = 0;
 1183 
 1184         /* Always clear the manual call flag */
 1185         changer->flags &= ~CHANGER_MANUAL_CALL;
 1186 
 1187         /* nothing to do if the queue is empty */
 1188         if (changer->devq.entries <= 0) {
 1189                 splx(s);
 1190                 return;
 1191         }
 1192 
 1193         /*
 1194          * If the changer queue is frozen, that means we have an active
 1195          * device.
 1196          */
 1197         if (changer->devq.qfrozen_cnt > 0) {
 1198 
 1199                 if (changer->cur_device->outstanding_cmds > 0) {
 1200                         changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
 1201                         changer->cur_device->bufs_left = 
 1202                                 changer->cur_device->outstanding_cmds;
 1203                         if (called_from_timeout) {
 1204                                 changer->long_handle =
 1205                                         timeout(cdrunchangerqueue, changer,
 1206                                         changer_max_busy_seconds * hz);
 1207                                 changer->flags |= CHANGER_TIMEOUT_SCHED;
 1208                         }
 1209                         splx(s);
 1210                         return;
 1211                 }
 1212 
 1213                 /*
 1214                  * We always need to reset the frozen count and clear the
 1215                  * active flag.
 1216                  */
 1217                 changer->devq.qfrozen_cnt--;
 1218                 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
 1219                 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
 1220 
 1221                 /*
 1222                  * Check to see whether the current device has any I/O left
 1223                  * to do.  If so, requeue it at the end of the queue.  If
 1224                  * not, there is no need to requeue it.
 1225                  */
 1226                 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
 1227 
 1228                         changer->cur_device->pinfo.generation =
 1229                                 ++changer->devq.generation;
 1230                         camq_insert(&changer->devq,
 1231                                 (cam_pinfo *)changer->cur_device);
 1232                 } 
 1233         }
 1234 
 1235         softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
 1236 
 1237         changer->cur_device = softc;
 1238 
 1239         changer->devq.qfrozen_cnt++;
 1240         softc->flags |= CD_FLAG_ACTIVE;
 1241 
 1242         /* Just in case this device is waiting */
 1243         wakeup(&softc->changer);
 1244         xpt_schedule(softc->periph, /*priority*/ 1);
 1245 
 1246         /*
 1247          * Get rid of any pending timeouts, and set a flag to schedule new
 1248          * ones so this device gets its full time quantum.
 1249          */
 1250         if (changer->flags & CHANGER_TIMEOUT_SCHED) {
 1251                 untimeout(cdrunchangerqueue, changer, changer->long_handle);
 1252                 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
 1253         }
 1254 
 1255         if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
 1256                 untimeout(cdshorttimeout, changer, changer->short_handle);
 1257                 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
 1258         }
 1259 
 1260         /*
 1261          * We need to schedule timeouts, but we only do this after the
 1262          * first transaction has completed.  This eliminates the changer
 1263          * switch time.
 1264          */
 1265         changer->flags |= CHANGER_NEED_TIMEOUT;
 1266 
 1267         splx(s);
 1268 }
 1269 
 1270 static void
 1271 cdchangerschedule(struct cd_softc *softc)
 1272 {
 1273         struct cdchanger *changer;
 1274         int s;
 1275 
 1276         s = splsoftcam();
 1277 
 1278         changer = softc->changer;
 1279 
 1280         /*
 1281          * If this is a changer, and this is the current device,
 1282          * and this device has at least the minimum time quantum to
 1283          * run, see if we can switch it out.
 1284          */
 1285         if ((softc->flags & CD_FLAG_ACTIVE) 
 1286          && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
 1287          && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
 1288                 /*
 1289                  * We try three things here.  The first is that we
 1290                  * check to see whether the schedule on completion
 1291                  * flag is set.  If it is, we decrement the number
 1292                  * of buffers left, and if it's zero, we reschedule.
 1293                  * Next, we check to see whether the pending buffer
 1294                  * queue is empty and whether there are no
 1295                  * outstanding transactions.  If so, we reschedule.
 1296                  * Next, we see if the pending buffer queue is empty.
 1297                  * If it is, we set the number of buffers left to
 1298                  * the current active buffer count and set the
 1299                  * schedule on complete flag.
 1300                  */
 1301                 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
 1302                         if (--softc->bufs_left == 0) {
 1303                                 softc->changer->flags |=
 1304                                         CHANGER_MANUAL_CALL;
 1305                                 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
 1306                                 cdrunchangerqueue(softc->changer);
 1307                         }
 1308                 } else if ((bioq_first(&softc->bio_queue) == NULL)
 1309                         && (softc->outstanding_cmds == 0)) {
 1310                         softc->changer->flags |= CHANGER_MANUAL_CALL;
 1311                         cdrunchangerqueue(softc->changer);
 1312                 }
 1313         } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 
 1314                 && (softc->flags & CD_FLAG_ACTIVE)) {
 1315 
 1316                 /*
 1317                  * Now that the first transaction to this
 1318                  * particular device has completed, we can go ahead
 1319                  * and schedule our timeouts.
 1320                  */
 1321                 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
 1322                         changer->long_handle =
 1323                             timeout(cdrunchangerqueue, changer,
 1324                                     changer_max_busy_seconds * hz);
 1325                         changer->flags |= CHANGER_TIMEOUT_SCHED;
 1326                 } else
 1327                         printf("cdchangerschedule: already have a long"
 1328                                " timeout!\n");
 1329 
 1330                 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
 1331                         changer->short_handle =
 1332                             timeout(cdshorttimeout, changer,
 1333                                     changer_min_busy_seconds * hz);
 1334                         changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
 1335                 } else
 1336                         printf("cdchangerschedule: already have a short "
 1337                                "timeout!\n");
 1338 
 1339                 /*
 1340                  * We just scheduled timeouts, no need to schedule
 1341                  * more.
 1342                  */
 1343                 changer->flags &= ~CHANGER_NEED_TIMEOUT;
 1344 
 1345         }
 1346         splx(s);
 1347 }
 1348 
 1349 static int
 1350 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
 1351                                               u_int32_t cam_flags,
 1352                                               u_int32_t sense_flags),
 1353          u_int32_t cam_flags, u_int32_t sense_flags)
 1354 {
 1355         struct cd_softc *softc;
 1356         struct cam_periph *periph;
 1357         int error;
 1358 
 1359         periph = xpt_path_periph(ccb->ccb_h.path);
 1360         softc = (struct cd_softc *)periph->softc;
 1361 
 1362         error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
 1363                                   softc->disk->d_devstat);
 1364 
 1365         if (softc->flags & CD_FLAG_CHANGER)
 1366                 cdchangerschedule(softc);
 1367 
 1368         return(error);
 1369 }
 1370 
 1371 static union ccb *
 1372 cdgetccb(struct cam_periph *periph, u_int32_t priority)
 1373 {
 1374         struct cd_softc *softc;
 1375         int s;
 1376 
 1377         softc = (struct cd_softc *)periph->softc;
 1378 
 1379         if (softc->flags & CD_FLAG_CHANGER) {
 1380 
 1381                 s = splsoftcam();
 1382 
 1383                 /*
 1384                  * This should work the first time this device is woken up,
 1385                  * but just in case it doesn't, we use a while loop.
 1386                  */
 1387                 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
 1388                         /*
 1389                          * If this changer isn't already queued, queue it up.
 1390                          */
 1391                         if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
 1392                                 softc->pinfo.priority = 1;
 1393                                 softc->pinfo.generation =
 1394                                         ++softc->changer->devq.generation;
 1395                                 camq_insert(&softc->changer->devq,
 1396                                             (cam_pinfo *)softc);
 1397                         }
 1398                         if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
 1399                          && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
 1400                          && ((softc->changer->flags
 1401                               & CHANGER_SHORT_TMOUT_SCHED)==0)) {
 1402                                 softc->changer->flags |= CHANGER_MANUAL_CALL;
 1403                                 cdrunchangerqueue(softc->changer);
 1404                         } else
 1405                                 tsleep(&softc->changer, PRIBIO, "cgticb", 0);
 1406                 }
 1407                 splx(s);
 1408         }
 1409         return(cam_periph_getccb(periph, priority));
 1410 }
 1411 
 1412 
 1413 /*
 1414  * Actually translate the requested transfer into one the physical driver
 1415  * can understand.  The transfer is described by a buf and will include
 1416  * only one physical transfer.
 1417  */
 1418 static void
 1419 cdstrategy(struct bio *bp)
 1420 {
 1421         struct cam_periph *periph;
 1422         struct cd_softc *softc;
 1423         int    s;
 1424 
 1425         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
 1426         if (periph == NULL) {
 1427                 biofinish(bp, NULL, ENXIO);
 1428                 return;
 1429         }
 1430 
 1431         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
 1432 
 1433         softc = (struct cd_softc *)periph->softc;
 1434 
 1435         /*
 1436          * Mask interrupts so that the pack cannot be invalidated until
 1437          * after we are in the queue.  Otherwise, we might not properly
 1438          * clean up one of the buffers.
 1439          */
 1440         s = splbio();
 1441         
 1442         /*
 1443          * If the device has been made invalid, error out
 1444          */
 1445         if ((softc->flags & CD_FLAG_INVALID)) {
 1446                 splx(s);
 1447                 biofinish(bp, NULL, ENXIO);
 1448                 return;
 1449         }
 1450 
 1451         /*
 1452          * If we don't have valid media, look for it before trying to
 1453          * schedule the I/O.
 1454          */
 1455         if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
 1456                 int error;
 1457 
 1458                 error = cdcheckmedia(periph);
 1459                 if (error != 0) {
 1460                         splx(s);
 1461                         biofinish(bp, NULL, error);
 1462                         return;
 1463                 }
 1464         }
 1465 
 1466         /*
 1467          * Place it in the queue of disk activities for this disk
 1468          */
 1469         bioq_disksort(&softc->bio_queue, bp);
 1470 
 1471         splx(s);
 1472         
 1473         /*
 1474          * Schedule ourselves for performing the work.  We do things
 1475          * differently for changers.
 1476          */
 1477         if ((softc->flags & CD_FLAG_CHANGER) == 0)
 1478                 xpt_schedule(periph, /* XXX priority */1);
 1479         else
 1480                 cdschedule(periph, /* priority */ 1);
 1481 
 1482         return;
 1483 }
 1484 
 1485 static void
 1486 cdstart(struct cam_periph *periph, union ccb *start_ccb)
 1487 {
 1488         struct cd_softc *softc;
 1489         struct bio *bp;
 1490         struct ccb_scsiio *csio;
 1491         struct scsi_read_capacity_data *rcap;
 1492         int s;
 1493 
 1494         softc = (struct cd_softc *)periph->softc;
 1495 
 1496         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
 1497 
 1498         switch (softc->state) {
 1499         case CD_STATE_NORMAL:
 1500         {
 1501                 int oldspl;
 1502 
 1503                 s = splbio();
 1504                 bp = bioq_first(&softc->bio_queue);
 1505                 if (periph->immediate_priority <= periph->pinfo.priority) {
 1506                         start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
 1507 
 1508                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
 1509                                           periph_links.sle);
 1510                         periph->immediate_priority = CAM_PRIORITY_NONE;
 1511                         splx(s);
 1512                         wakeup(&periph->ccb_list);
 1513                 } else if (bp == NULL) {
 1514                         splx(s);
 1515                         xpt_release_ccb(start_ccb);
 1516                 } else {
 1517                         bioq_remove(&softc->bio_queue, bp);
 1518 
 1519                         devstat_start_transaction_bio(softc->disk->d_devstat, bp);
 1520 
 1521                         scsi_read_write(&start_ccb->csio,
 1522                                         /*retries*/4,
 1523                                         /* cbfcnp */ cddone,
 1524                                         MSG_SIMPLE_Q_TAG,
 1525                                         /* read */bp->bio_cmd == BIO_READ,
 1526                                         /* byte2 */ 0,
 1527                                         /* minimum_cmd_size */ 10,
 1528                                         /* lba */ bp->bio_offset /
 1529                                           softc->params.blksize,
 1530                                         bp->bio_bcount / softc->params.blksize,
 1531                                         /* data_ptr */ bp->bio_data,
 1532                                         /* dxfer_len */ bp->bio_bcount,
 1533                                         /* sense_len */ SSD_FULL_SIZE,
 1534                                         /* timeout */ 30000);
 1535                         start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
 1536 
 1537                         
 1538                         /*
 1539                          * Block out any asyncronous callbacks
 1540                          * while we touch the pending ccb list.
 1541                          */
 1542                         oldspl = splcam();
 1543                         LIST_INSERT_HEAD(&softc->pending_ccbs,
 1544                                          &start_ccb->ccb_h, periph_links.le);
 1545                         softc->outstanding_cmds++;
 1546                         splx(oldspl);
 1547 
 1548                         /* We expect a unit attention from this device */
 1549                         if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
 1550                                 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
 1551                                 softc->flags &= ~CD_FLAG_RETRY_UA;
 1552                         }
 1553 
 1554                         start_ccb->ccb_h.ccb_bp = bp;
 1555                         bp = bioq_first(&softc->bio_queue);
 1556                         splx(s);
 1557 
 1558                         xpt_action(start_ccb);
 1559                 }
 1560                 if (bp != NULL) {
 1561                         /* Have more work to do, so ensure we stay scheduled */
 1562                         xpt_schedule(periph, /* XXX priority */1);
 1563                 }
 1564                 break;
 1565         }
 1566         case CD_STATE_PROBE:
 1567         {
 1568 
 1569                 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
 1570                                                                 M_SCSICD,
 1571                                                                 M_NOWAIT);
 1572                 if (rcap == NULL) {
 1573                         xpt_print_path(periph->path);
 1574                         printf("cdstart: Couldn't malloc read_capacity data\n");
 1575                         /* cd_free_periph??? */
 1576                         break;
 1577                 }
 1578                 csio = &start_ccb->csio;
 1579                 scsi_read_capacity(csio,
 1580                                    /*retries*/1,
 1581                                    cddone,
 1582                                    MSG_SIMPLE_Q_TAG,
 1583                                    rcap,
 1584                                    SSD_FULL_SIZE,
 1585                                    /*timeout*/20000);
 1586                 start_ccb->ccb_h.ccb_bp = NULL;
 1587                 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
 1588                 xpt_action(start_ccb);
 1589                 break;
 1590         }
 1591         }
 1592 }
 1593 
 1594 static void
 1595 cddone(struct cam_periph *periph, union ccb *done_ccb)
 1596 { 
 1597         struct cd_softc *softc;
 1598         struct ccb_scsiio *csio;
 1599 
 1600         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
 1601 
 1602         softc = (struct cd_softc *)periph->softc;
 1603         csio = &done_ccb->csio;
 1604 
 1605         switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
 1606         case CD_CCB_BUFFER_IO:
 1607         {
 1608                 struct bio      *bp;
 1609                 int             error;
 1610                 int             oldspl;
 1611 
 1612                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
 1613                 error = 0;
 1614 
 1615                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1616                         int sf;
 1617 
 1618                         if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
 1619                                 sf = SF_RETRY_UA;
 1620                         else
 1621                                 sf = 0;
 1622 
 1623                         error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
 1624                         if (error == ERESTART) {
 1625                                 /*
 1626                                  * A retry was scheuled, so
 1627                                  * just return.
 1628                                  */
 1629                                 return;
 1630                         }
 1631                 }
 1632 
 1633                 if (error != 0) {
 1634                         int s;
 1635 
 1636                         xpt_print_path(periph->path);
 1637                         printf("cddone: got error %#x back\n", error);
 1638                         s = splbio();
 1639                         bioq_flush(&softc->bio_queue, NULL, EIO);
 1640                         splx(s);
 1641                         bp->bio_resid = bp->bio_bcount;
 1642                         bp->bio_error = error;
 1643                         bp->bio_flags |= BIO_ERROR;
 1644                         cam_release_devq(done_ccb->ccb_h.path,
 1645                                          /*relsim_flags*/0,
 1646                                          /*reduction*/0,
 1647                                          /*timeout*/0,
 1648                                          /*getcount_only*/0);
 1649 
 1650                 } else {
 1651                         bp->bio_resid = csio->resid;
 1652                         bp->bio_error = 0;
 1653                         if (bp->bio_resid != 0) {
 1654                                 /*
 1655                                  * Short transfer ??? 
 1656                                  * XXX: not sure this is correct for partial
 1657                                  * transfers at EOM
 1658                                  */
 1659                                 bp->bio_flags |= BIO_ERROR;
 1660                         }
 1661                 }
 1662 
 1663                 /*
 1664                  * Block out any asyncronous callbacks
 1665                  * while we touch the pending ccb list.
 1666                  */
 1667                 oldspl = splcam();
 1668                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
 1669                 softc->outstanding_cmds--;
 1670                 splx(oldspl);
 1671 
 1672                 if (softc->flags & CD_FLAG_CHANGER)
 1673                         cdchangerschedule(softc);
 1674 
 1675                 biofinish(bp, NULL, 0);
 1676                 break;
 1677         }
 1678         case CD_CCB_PROBE:
 1679         {
 1680                 struct     scsi_read_capacity_data *rdcap;
 1681                 char       announce_buf[120]; /*
 1682                                                * Currently (9/30/97) the 
 1683                                                * longest possible announce 
 1684                                                * buffer is 108 bytes, for the 
 1685                                                * first error case below.  
 1686                                                * That is 39 bytes for the 
 1687                                                * basic string, 16 bytes for the
 1688                                                * biggest sense key (hardware 
 1689                                                * error), 52 bytes for the
 1690                                                * text of the largest sense 
 1691                                                * qualifier valid for a CDROM,
 1692                                                * (0x72, 0x03 or 0x04,
 1693                                                * 0x03), and one byte for the
 1694                                                * null terminating character.
 1695                                                * To allow for longer strings, 
 1696                                                * the announce buffer is 120
 1697                                                * bytes.
 1698                                                */
 1699                 struct     cd_params *cdp;
 1700 
 1701                 cdp = &softc->params;
 1702 
 1703                 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
 1704                 
 1705                 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
 1706                 cdp->blksize = scsi_4btoul (rdcap->length);
 1707 
 1708                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 1709 
 1710                         snprintf(announce_buf, sizeof(announce_buf),
 1711                                 "cd present [%lu x %lu byte records]",
 1712                                 cdp->disksize, (u_long)cdp->blksize);
 1713 
 1714                 } else {
 1715                         int     error;
 1716                         /*
 1717                          * Retry any UNIT ATTENTION type errors.  They
 1718                          * are expected at boot.
 1719                          */
 1720                         error = cderror(done_ccb, CAM_RETRY_SELTO,
 1721                                         SF_RETRY_UA | SF_NO_PRINT);
 1722                         if (error == ERESTART) {
 1723                                 /*
 1724                                  * A retry was scheuled, so
 1725                                  * just return.
 1726                                  */
 1727                                 return;
 1728                         } else if (error != 0) {
 1729 
 1730                                 struct scsi_sense_data *sense;
 1731                                 int asc, ascq;
 1732                                 int sense_key, error_code;
 1733                                 int have_sense;
 1734                                 cam_status status;
 1735                                 struct ccb_getdev cgd;
 1736 
 1737                                 /* Don't wedge this device's queue */
 1738                                 cam_release_devq(done_ccb->ccb_h.path,
 1739                                                  /*relsim_flags*/0,
 1740                                                  /*reduction*/0,
 1741                                                  /*timeout*/0,
 1742                                                  /*getcount_only*/0);
 1743 
 1744                                 status = done_ccb->ccb_h.status;
 1745 
 1746                                 xpt_setup_ccb(&cgd.ccb_h, 
 1747                                               done_ccb->ccb_h.path,
 1748                                               /* priority */ 1);
 1749                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 1750                                 xpt_action((union ccb *)&cgd);
 1751 
 1752                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
 1753                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
 1754                                  || ((status & CAM_AUTOSNS_VALID) == 0))
 1755                                         have_sense = FALSE;
 1756                                 else
 1757                                         have_sense = TRUE;
 1758 
 1759                                 if (have_sense) {
 1760                                         sense = &csio->sense_data;
 1761                                         scsi_extract_sense(sense, &error_code,
 1762                                                            &sense_key, 
 1763                                                            &asc, &ascq);
 1764                                 }
 1765                                 /*
 1766                                  * Attach to anything that claims to be a
 1767                                  * CDROM or WORM device, as long as it
 1768                                  * doesn't return a "Logical unit not
 1769                                  * supported" (0x25) error.
 1770                                  */
 1771                                 if ((have_sense) && (asc != 0x25)
 1772                                  && (error_code == SSD_CURRENT_ERROR)) {
 1773                                         const char *sense_key_desc;
 1774                                         const char *asc_desc;
 1775 
 1776                                         scsi_sense_desc(sense_key, asc, ascq,
 1777                                                         &cgd.inq_data,
 1778                                                         &sense_key_desc,
 1779                                                         &asc_desc);
 1780                                         snprintf(announce_buf,
 1781                                             sizeof(announce_buf),
 1782                                                 "Attempt to query device "
 1783                                                 "size failed: %s, %s",
 1784                                                 sense_key_desc,
 1785                                                 asc_desc);
 1786                                 } else if ((have_sense == 0) 
 1787                                       && ((status & CAM_STATUS_MASK) ==
 1788                                            CAM_SCSI_STATUS_ERROR)
 1789                                       && (csio->scsi_status ==
 1790                                           SCSI_STATUS_BUSY)) {
 1791                                         snprintf(announce_buf,
 1792                                             sizeof(announce_buf),
 1793                                             "Attempt to query device "
 1794                                             "size failed: SCSI Status: %s",
 1795                                             scsi_status_string(csio));
 1796                                 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
 1797                                         /*
 1798                                          * We only print out an error for
 1799                                          * CDROM type devices.  For WORM
 1800                                          * devices, we don't print out an
 1801                                          * error since a few WORM devices
 1802                                          * don't support CDROM commands.
 1803                                          * If we have sense information, go
 1804                                          * ahead and print it out.
 1805                                          * Otherwise, just say that we 
 1806                                          * couldn't attach.
 1807                                          */
 1808 
 1809                                         /*
 1810                                          * Just print out the error, not
 1811                                          * the full probe message, when we
 1812                                          * don't attach.
 1813                                          */
 1814                                         if (have_sense)
 1815                                                 scsi_sense_print(
 1816                                                         &done_ccb->csio);
 1817                                         else {
 1818                                                 xpt_print_path(periph->path);
 1819                                                 printf("got CAM status %#x\n",
 1820                                                        done_ccb->ccb_h.status);
 1821                                         }
 1822                                         xpt_print_path(periph->path);
 1823                                         printf("fatal error, failed" 
 1824                                                " to attach to device\n");
 1825 
 1826                                         /*
 1827                                          * Invalidate this peripheral.
 1828                                          */
 1829                                         cam_periph_invalidate(periph);
 1830 
 1831                                         announce_buf[0] = '\0';
 1832                                 } else {
 1833 
 1834                                         /*
 1835                                          * Invalidate this peripheral.
 1836                                          */
 1837                                         cam_periph_invalidate(periph);
 1838                                         announce_buf[0] = '\0';
 1839                                 }
 1840                         }
 1841                 }
 1842                 free(rdcap, M_SCSICD);
 1843                 if (announce_buf[0] != '\0') {
 1844                         xpt_announce_periph(periph, announce_buf);
 1845                         if (softc->flags & CD_FLAG_CHANGER)
 1846                                 cdchangerschedule(softc);
 1847                         /*
 1848                          * Create our sysctl variables, now that we know
 1849                          * we have successfully attached.
 1850                          */
 1851                         taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
 1852                 }
 1853                 softc->state = CD_STATE_NORMAL;         
 1854                 /*
 1855                  * Since our peripheral may be invalidated by an error
 1856                  * above or an external event, we must release our CCB
 1857                  * before releasing the probe lock on the peripheral.
 1858                  * The peripheral will only go away once the last lock
 1859                  * is removed, and we need it around for the CCB release
 1860                  * operation.
 1861                  */
 1862                 xpt_release_ccb(done_ccb);
 1863                 cam_periph_unlock(periph);
 1864                 return;
 1865         }
 1866         case CD_CCB_WAITING:
 1867         {
 1868                 /* Caller will release the CCB */
 1869                 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
 1870                           ("trying to wakeup ccbwait\n"));
 1871 
 1872                 wakeup(&done_ccb->ccb_h.cbfcnp);
 1873                 return;
 1874         }
 1875         default:
 1876                 break;
 1877         }
 1878         xpt_release_ccb(done_ccb);
 1879 }
 1880 
 1881 static union cd_pages *
 1882 cdgetpage(struct cd_mode_params *mode_params)
 1883 {
 1884         union cd_pages *page;
 1885 
 1886         if (mode_params->cdb_size == 10)
 1887                 page = (union cd_pages *)find_mode_page_10(
 1888                         (struct scsi_mode_header_10 *)mode_params->mode_buf);
 1889         else
 1890                 page = (union cd_pages *)find_mode_page_6(
 1891                         (struct scsi_mode_header_6 *)mode_params->mode_buf);
 1892 
 1893         return (page);
 1894 }
 1895 
 1896 static int
 1897 cdgetpagesize(int page_num)
 1898 {
 1899         int i;
 1900 
 1901         for (i = 0; i < (sizeof(cd_page_size_table)/
 1902              sizeof(cd_page_size_table[0])); i++) {
 1903                 if (cd_page_size_table[i].page == page_num)
 1904                         return (cd_page_size_table[i].page_size);
 1905         }
 1906 
 1907         return (-1);
 1908 }
 1909 
 1910 static int
 1911 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
 1912 {
 1913 
 1914         struct  cam_periph *periph;
 1915         struct  cd_softc *softc;
 1916         int     error, nocopyout;
 1917 
 1918         periph = (struct cam_periph *)dp->d_drv1;
 1919         if (periph == NULL)
 1920                 return(ENXIO);  
 1921 
 1922         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
 1923 
 1924         softc = (struct cd_softc *)periph->softc;
 1925 
 1926         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
 1927                   ("trying to do ioctl %#lx\n", cmd));
 1928 
 1929         error = cam_periph_lock(periph, PRIBIO | PCATCH);
 1930 
 1931         if (error != 0)
 1932                 return(error);
 1933         /*
 1934          * If we don't have media loaded, check for it.  If still don't
 1935          * have media loaded, we can only do a load or eject.
 1936          *
 1937          * We only care whether media is loaded if this is a cd-specific ioctl
 1938          * (thus the IOCGROUP check below).  Note that this will break if
 1939          * anyone adds any ioctls into the switch statement below that don't
 1940          * have their ioctl group set to 'c'.
 1941          */
 1942         if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
 1943          && ((cmd != CDIOCCLOSE)
 1944           && (cmd != CDIOCEJECT))
 1945          && (IOCGROUP(cmd) == 'c')) {
 1946                 error = cdcheckmedia(periph);
 1947                 if (error != 0) {
 1948                         cam_periph_unlock(periph);
 1949                         return (error);
 1950                 }
 1951         }
 1952 
 1953         nocopyout = 0;
 1954         switch (cmd) {
 1955 
 1956         case CDIOCPLAYTRACKS:
 1957                 {
 1958                         struct ioc_play_track *args
 1959                             = (struct ioc_play_track *) addr;
 1960                         struct cd_mode_params params;
 1961                         union cd_pages *page;
 1962 
 1963                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 1964                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 1965                                                  M_WAITOK | M_ZERO);
 1966 
 1967                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 1968                                   ("trying to do CDIOCPLAYTRACKS\n"));
 1969 
 1970                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 1971                         if (error) {
 1972                                 free(params.mode_buf, M_SCSICD);
 1973                                 break;
 1974                         }
 1975                         page = cdgetpage(&params);
 1976 
 1977                         page->audio.flags &= ~CD_PA_SOTC;
 1978                         page->audio.flags |= CD_PA_IMMED;
 1979                         error = cdsetmode(periph, &params);
 1980                         free(params.mode_buf, M_SCSICD);
 1981                         if (error)
 1982                                 break;
 1983 
 1984                         /*
 1985                          * This was originally implemented with the PLAY
 1986                          * AUDIO TRACK INDEX command, but that command was
 1987                          * deprecated after SCSI-2.  Most (all?) SCSI CDROM
 1988                          * drives support it but ATAPI and ATAPI-derivative
 1989                          * drives don't seem to support it.  So we keep a
 1990                          * cache of the table of contents and translate
 1991                          * track numbers to MSF format.
 1992                          */
 1993                         if (softc->flags & CD_FLAG_VALID_TOC) {
 1994                                 union msf_lba *sentry, *eentry;
 1995                                 int st, et;
 1996 
 1997                                 if (args->end_track <
 1998                                     softc->toc.header.ending_track + 1)
 1999                                         args->end_track++;
 2000                                 if (args->end_track >
 2001                                     softc->toc.header.ending_track + 1)
 2002                                         args->end_track =
 2003                                             softc->toc.header.ending_track + 1;
 2004                                 st = args->start_track -
 2005                                         softc->toc.header.starting_track;
 2006                                 et = args->end_track -
 2007                                         softc->toc.header.starting_track;
 2008                                 if ((st < 0)
 2009                                  || (et < 0)
 2010                                  || (st > (softc->toc.header.ending_track -
 2011                                      softc->toc.header.starting_track))) {
 2012                                         error = EINVAL;
 2013                                         break;
 2014                                 }
 2015                                 sentry = &softc->toc.entries[st].addr;
 2016                                 eentry = &softc->toc.entries[et].addr;
 2017                                 error = cdplaymsf(periph,
 2018                                                   sentry->msf.minute,
 2019                                                   sentry->msf.second,
 2020                                                   sentry->msf.frame,
 2021                                                   eentry->msf.minute,
 2022                                                   eentry->msf.second,
 2023                                                   eentry->msf.frame);
 2024                         } else {
 2025                                 /*
 2026                                  * If we don't have a valid TOC, try the
 2027                                  * play track index command.  It is part of
 2028                                  * the SCSI-2 spec, but was removed in the
 2029                                  * MMC specs.  ATAPI and ATAPI-derived
 2030                                  * drives don't support it.
 2031                                  */
 2032                                 if (softc->quirks & CD_Q_BCD_TRACKS) {
 2033                                         args->start_track =
 2034                                                 bin2bcd(args->start_track);
 2035                                         args->end_track =
 2036                                                 bin2bcd(args->end_track);
 2037                                 }
 2038                                 error = cdplaytracks(periph,
 2039                                                      args->start_track,
 2040                                                      args->start_index,
 2041                                                      args->end_track,
 2042                                                      args->end_index);
 2043                         }
 2044                 }
 2045                 break;
 2046         case CDIOCPLAYMSF:
 2047                 {
 2048                         struct ioc_play_msf *args
 2049                                 = (struct ioc_play_msf *) addr;
 2050                         struct cd_mode_params params;
 2051                         union cd_pages *page;
 2052 
 2053                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2054                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2055                                                  M_WAITOK | M_ZERO);
 2056 
 2057                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2058                                   ("trying to do CDIOCPLAYMSF\n"));
 2059 
 2060                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2061                         if (error) {
 2062                                 free(params.mode_buf, M_SCSICD);
 2063                                 break;
 2064                         }
 2065                         page = cdgetpage(&params);
 2066 
 2067                         page->audio.flags &= ~CD_PA_SOTC;
 2068                         page->audio.flags |= CD_PA_IMMED;
 2069                         error = cdsetmode(periph, &params);
 2070                         free(params.mode_buf, M_SCSICD);
 2071                         if (error)
 2072                                 break;
 2073                         error = cdplaymsf(periph,
 2074                                           args->start_m,
 2075                                           args->start_s,
 2076                                           args->start_f,
 2077                                           args->end_m,
 2078                                           args->end_s,
 2079                                           args->end_f);
 2080                 }
 2081                 break;
 2082         case CDIOCPLAYBLOCKS:
 2083                 {
 2084                         struct ioc_play_blocks *args
 2085                                 = (struct ioc_play_blocks *) addr;
 2086                         struct cd_mode_params params;
 2087                         union cd_pages *page;
 2088 
 2089                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2090                                   ("trying to do CDIOCPLAYBLOCKS\n"));
 2091 
 2092                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2093                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2094                                                  M_WAITOK | M_ZERO);
 2095 
 2096                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2097                         if (error) {
 2098                                 free(params.mode_buf, M_SCSICD);
 2099                                 break;
 2100                         }
 2101                         page = cdgetpage(&params);
 2102 
 2103                         page->audio.flags &= ~CD_PA_SOTC;
 2104                         page->audio.flags |= CD_PA_IMMED;
 2105                         error = cdsetmode(periph, &params);
 2106                         free(params.mode_buf, M_SCSICD);
 2107                         if (error)
 2108                                 break;
 2109                         error = cdplay(periph, args->blk, args->len);
 2110                 }
 2111                 break;
 2112         case CDIOCREADSUBCHANNEL_SYSSPACE:
 2113                 nocopyout = 1;
 2114                 /* Fallthrough */
 2115         case CDIOCREADSUBCHANNEL:
 2116                 {
 2117                         struct ioc_read_subchannel *args
 2118                                 = (struct ioc_read_subchannel *) addr;
 2119                         struct cd_sub_channel_info *data;
 2120                         u_int32_t len = args->data_len;
 2121 
 2122                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2123                                   ("trying to do CDIOCREADSUBCHANNEL\n"));
 2124 
 2125                         data = malloc(sizeof(struct cd_sub_channel_info), 
 2126                                       M_SCSICD, M_WAITOK);
 2127 
 2128                         if ((len > sizeof(struct cd_sub_channel_info)) ||
 2129                             (len < sizeof(struct cd_sub_channel_header))) {
 2130                                 printf(
 2131                                         "scsi_cd: cdioctl: "
 2132                                         "cdioreadsubchannel: error, len=%d\n",
 2133                                         len);
 2134                                 error = EINVAL;
 2135                                 free(data, M_SCSICD);
 2136                                 break;
 2137                         }
 2138 
 2139                         if (softc->quirks & CD_Q_BCD_TRACKS)
 2140                                 args->track = bin2bcd(args->track);
 2141 
 2142                         error = cdreadsubchannel(periph, args->address_format,
 2143                                 args->data_format, args->track, data, len);
 2144 
 2145                         if (error) {
 2146                                 free(data, M_SCSICD);
 2147                                 break;
 2148                         }
 2149                         if (softc->quirks & CD_Q_BCD_TRACKS)
 2150                                 data->what.track_info.track_number =
 2151                                     bcd2bin(data->what.track_info.track_number);
 2152                         len = min(len, ((data->header.data_len[0] << 8) +
 2153                                 data->header.data_len[1] +
 2154                                 sizeof(struct cd_sub_channel_header)));
 2155                         if (nocopyout == 0) {
 2156                                 if (copyout(data, args->data, len) != 0) {
 2157                                         error = EFAULT;
 2158                                 }
 2159                         } else {
 2160                                 bcopy(data, args->data, len);
 2161                         }
 2162                         free(data, M_SCSICD);
 2163                 }
 2164                 break;
 2165 
 2166         case CDIOREADTOCHEADER:
 2167                 {
 2168                         struct ioc_toc_header *th;
 2169 
 2170                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2171                                   ("trying to do CDIOREADTOCHEADER\n"));
 2172 
 2173                         th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
 2174                                     M_WAITOK);
 2175                         error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 
 2176                                           sizeof (*th), /*sense_flags*/0);
 2177                         if (error) {
 2178                                 free(th, M_SCSICD);
 2179                                 break;
 2180                         }
 2181                         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2182                                 /* we are going to have to convert the BCD
 2183                                  * encoding on the cd to what is expected
 2184                                  */
 2185                                 th->starting_track = 
 2186                                         bcd2bin(th->starting_track);
 2187                                 th->ending_track = bcd2bin(th->ending_track);
 2188                         }
 2189                         th->len = ntohs(th->len);
 2190                         bcopy(th, addr, sizeof(*th));
 2191                         free(th, M_SCSICD);
 2192                 }
 2193                 break;
 2194         case CDIOREADTOCENTRYS:
 2195                 {
 2196                         struct cd_tocdata *data;
 2197                         struct cd_toc_single *lead;
 2198                         struct ioc_read_toc_entry *te =
 2199                                 (struct ioc_read_toc_entry *) addr;
 2200                         struct ioc_toc_header *th;
 2201                         u_int32_t len, readlen, idx, num;
 2202                         u_int32_t starting_track = te->starting_track;
 2203 
 2204                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2205                                   ("trying to do CDIOREADTOCENTRYS\n"));
 2206 
 2207                         data = malloc(sizeof(*data), M_SCSICD, M_WAITOK);
 2208                         lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK);
 2209 
 2210                         if (te->data_len < sizeof(struct cd_toc_entry)
 2211                          || (te->data_len % sizeof(struct cd_toc_entry)) != 0
 2212                          || (te->address_format != CD_MSF_FORMAT
 2213                           && te->address_format != CD_LBA_FORMAT)) {
 2214                                 error = EINVAL;
 2215                                 printf("scsi_cd: error in readtocentries, "
 2216                                        "returning EINVAL\n");
 2217                                 free(data, M_SCSICD);
 2218                                 free(lead, M_SCSICD);
 2219                                 break;
 2220                         }
 2221 
 2222                         th = &data->header;
 2223                         error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 
 2224                                           sizeof (*th), /*sense_flags*/0);
 2225                         if (error) {
 2226                                 free(data, M_SCSICD);
 2227                                 free(lead, M_SCSICD);
 2228                                 break;
 2229                         }
 2230 
 2231                         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2232                                 /* we are going to have to convert the BCD
 2233                                  * encoding on the cd to what is expected
 2234                                  */
 2235                                 th->starting_track =
 2236                                     bcd2bin(th->starting_track);
 2237                                 th->ending_track = bcd2bin(th->ending_track);
 2238                         }
 2239 
 2240                         if (starting_track == 0)
 2241                                 starting_track = th->starting_track;
 2242                         else if (starting_track == LEADOUT)
 2243                                 starting_track = th->ending_track + 1;
 2244                         else if (starting_track < th->starting_track ||
 2245                                  starting_track > th->ending_track + 1) {
 2246                                 printf("scsi_cd: error in readtocentries, "
 2247                                        "returning EINVAL\n");
 2248                                 free(data, M_SCSICD);
 2249                                 free(lead, M_SCSICD);
 2250                                 error = EINVAL;
 2251                                 break;
 2252                         }
 2253 
 2254                         /* calculate reading length without leadout entry */
 2255                         readlen = (th->ending_track - starting_track + 1) *
 2256                                   sizeof(struct cd_toc_entry);
 2257 
 2258                         /* and with leadout entry */
 2259                         len = readlen + sizeof(struct cd_toc_entry);
 2260                         if (te->data_len < len) {
 2261                                 len = te->data_len;
 2262                                 if (readlen > len)
 2263                                         readlen = len;
 2264                         }
 2265                         if (len > sizeof(data->entries)) {
 2266                                 printf("scsi_cd: error in readtocentries, "
 2267                                        "returning EINVAL\n");
 2268                                 error = EINVAL;
 2269                                 free(data, M_SCSICD);
 2270                                 free(lead, M_SCSICD);
 2271                                 break;
 2272                         }
 2273                         num = len / sizeof(struct cd_toc_entry);
 2274 
 2275                         if (readlen > 0) {
 2276                                 error = cdreadtoc(periph, te->address_format,
 2277                                                   starting_track,
 2278                                                   (u_int8_t *)data,
 2279                                                   readlen + sizeof (*th),
 2280                                                   /*sense_flags*/0);
 2281                                 if (error) {
 2282                                         free(data, M_SCSICD);
 2283                                         free(lead, M_SCSICD);
 2284                                         break;
 2285                                 }
 2286                         }
 2287 
 2288                         /* make leadout entry if needed */
 2289                         idx = starting_track + num - 1;
 2290                         if (softc->quirks & CD_Q_BCD_TRACKS)
 2291                                 th->ending_track = bcd2bin(th->ending_track);
 2292                         if (idx == th->ending_track + 1) {
 2293                                 error = cdreadtoc(periph, te->address_format,
 2294                                                   LEADOUT, (u_int8_t *)lead,
 2295                                                   sizeof(*lead),
 2296                                                   /*sense_flags*/0);
 2297                                 if (error) {
 2298                                         free(data, M_SCSICD);
 2299                                         free(lead, M_SCSICD);
 2300                                         break;
 2301                                 }
 2302                                 data->entries[idx - starting_track] = 
 2303                                         lead->entry;
 2304                         }
 2305                         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2306                                 for (idx = 0; idx < num - 1; idx++) {
 2307                                         data->entries[idx].track =
 2308                                             bcd2bin(data->entries[idx].track);
 2309                                 }
 2310                         }
 2311 
 2312                         error = copyout(data->entries, te->data, len);
 2313                         free(data, M_SCSICD);
 2314                         free(lead, M_SCSICD);
 2315                 }
 2316                 break;
 2317         case CDIOREADTOCENTRY:
 2318                 {
 2319                         struct cd_toc_single *data;
 2320                         struct ioc_read_toc_single_entry *te =
 2321                                 (struct ioc_read_toc_single_entry *) addr;
 2322                         struct ioc_toc_header *th;
 2323                         u_int32_t track;
 2324 
 2325                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2326                                   ("trying to do CDIOREADTOCENTRY\n"));
 2327 
 2328                         data = malloc(sizeof(*data), M_SCSICD, M_WAITOK);
 2329 
 2330                         if (te->address_format != CD_MSF_FORMAT
 2331                             && te->address_format != CD_LBA_FORMAT) {
 2332                                 printf("error in readtocentry, "
 2333                                        " returning EINVAL\n");
 2334                                 free(data, M_SCSICD);
 2335                                 error = EINVAL;
 2336                                 break;
 2337                         }
 2338 
 2339                         th = &data->header;
 2340                         error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
 2341                                           sizeof (*th), /*sense_flags*/0);
 2342                         if (error) {
 2343                                 free(data, M_SCSICD);
 2344                                 break;
 2345                         }
 2346 
 2347                         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2348                                 /* we are going to have to convert the BCD
 2349                                  * encoding on the cd to what is expected
 2350                                  */
 2351                                 th->starting_track =
 2352                                     bcd2bin(th->starting_track);
 2353                                 th->ending_track = bcd2bin(th->ending_track);
 2354                         }
 2355                         track = te->track;
 2356                         if (track == 0)
 2357                                 track = th->starting_track;
 2358                         else if (track == LEADOUT)
 2359                                 /* OK */;
 2360                         else if (track < th->starting_track ||
 2361                                  track > th->ending_track + 1) {
 2362                                 printf("error in readtocentry, "
 2363                                        " returning EINVAL\n");
 2364                                 free(data, M_SCSICD);
 2365                                 error = EINVAL;
 2366                                 break;
 2367                         }
 2368 
 2369                         error = cdreadtoc(periph, te->address_format, track,
 2370                                           (u_int8_t *)data, sizeof(*data),
 2371                                           /*sense_flags*/0);
 2372                         if (error) {
 2373                                 free(data, M_SCSICD);
 2374                                 break;
 2375                         }
 2376 
 2377                         if (softc->quirks & CD_Q_BCD_TRACKS)
 2378                                 data->entry.track = bcd2bin(data->entry.track);
 2379                         bcopy(&data->entry, &te->entry,
 2380                               sizeof(struct cd_toc_entry));
 2381                         free(data, M_SCSICD);
 2382                 }
 2383                 break;
 2384         case CDIOCSETPATCH:
 2385                 {
 2386                         struct ioc_patch *arg = (struct ioc_patch *)addr;
 2387                         struct cd_mode_params params;
 2388                         union cd_pages *page;
 2389 
 2390                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2391                                   ("trying to do CDIOCSETPATCH\n"));
 2392 
 2393                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2394                         params.mode_buf = malloc(params.alloc_len, M_SCSICD, 
 2395                                                  M_WAITOK | M_ZERO);
 2396                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2397                         if (error) {
 2398                                 free(params.mode_buf, M_SCSICD);
 2399                                 break;
 2400                         }
 2401                         page = cdgetpage(&params);
 2402 
 2403                         page->audio.port[LEFT_PORT].channels = 
 2404                                 arg->patch[0];
 2405                         page->audio.port[RIGHT_PORT].channels = 
 2406                                 arg->patch[1];
 2407                         page->audio.port[2].channels = arg->patch[2];
 2408                         page->audio.port[3].channels = arg->patch[3];
 2409                         error = cdsetmode(periph, &params);
 2410                         free(params.mode_buf, M_SCSICD);
 2411                 }
 2412                 break;
 2413         case CDIOCGETVOL:
 2414                 {
 2415                         struct ioc_vol *arg = (struct ioc_vol *) addr;
 2416                         struct cd_mode_params params;
 2417                         union cd_pages *page;
 2418 
 2419                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2420                                   ("trying to do CDIOCGETVOL\n"));
 2421 
 2422                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2423                         params.mode_buf = malloc(params.alloc_len, M_SCSICD, 
 2424                                                  M_WAITOK | M_ZERO);
 2425                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2426                         if (error) {
 2427                                 free(params.mode_buf, M_SCSICD);
 2428                                 break;
 2429                         }
 2430                         page = cdgetpage(&params);
 2431 
 2432                         arg->vol[LEFT_PORT] = 
 2433                                 page->audio.port[LEFT_PORT].volume;
 2434                         arg->vol[RIGHT_PORT] = 
 2435                                 page->audio.port[RIGHT_PORT].volume;
 2436                         arg->vol[2] = page->audio.port[2].volume;
 2437                         arg->vol[3] = page->audio.port[3].volume;
 2438                         free(params.mode_buf, M_SCSICD);
 2439                 }
 2440                 break;
 2441         case CDIOCSETVOL:
 2442                 {
 2443                         struct ioc_vol *arg = (struct ioc_vol *) addr;
 2444                         struct cd_mode_params params;
 2445                         union cd_pages *page;
 2446 
 2447                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2448                                   ("trying to do CDIOCSETVOL\n"));
 2449 
 2450                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2451                         params.mode_buf = malloc(params.alloc_len, M_SCSICD, 
 2452                                                  M_WAITOK | M_ZERO);
 2453                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2454                         if (error) {
 2455                                 free(params.mode_buf, M_SCSICD);
 2456                                 break;
 2457                         }
 2458                         page = cdgetpage(&params);
 2459 
 2460                         page->audio.port[LEFT_PORT].channels = CHANNEL_0;
 2461                         page->audio.port[LEFT_PORT].volume = 
 2462                                 arg->vol[LEFT_PORT];
 2463                         page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
 2464                         page->audio.port[RIGHT_PORT].volume = 
 2465                                 arg->vol[RIGHT_PORT];
 2466                         page->audio.port[2].volume = arg->vol[2];
 2467                         page->audio.port[3].volume = arg->vol[3];
 2468                         error = cdsetmode(periph, &params);
 2469                         free(params.mode_buf, M_SCSICD);
 2470                 }
 2471                 break;
 2472         case CDIOCSETMONO:
 2473                 {
 2474                         struct cd_mode_params params;
 2475                         union cd_pages *page;
 2476 
 2477                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2478                                   ("trying to do CDIOCSETMONO\n"));
 2479 
 2480                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2481                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2482                                                  M_WAITOK | M_ZERO);
 2483                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2484                         if (error) {
 2485                                 free(params.mode_buf, M_SCSICD);
 2486                                 break;
 2487                         }
 2488                         page = cdgetpage(&params);
 2489 
 2490                         page->audio.port[LEFT_PORT].channels = 
 2491                                 LEFT_CHANNEL | RIGHT_CHANNEL;
 2492                         page->audio.port[RIGHT_PORT].channels = 
 2493                                 LEFT_CHANNEL | RIGHT_CHANNEL;
 2494                         page->audio.port[2].channels = 0;
 2495                         page->audio.port[3].channels = 0;
 2496                         error = cdsetmode(periph, &params);
 2497                         free(params.mode_buf, M_SCSICD);
 2498                 }
 2499                 break;
 2500         case CDIOCSETSTEREO:
 2501                 {
 2502                         struct cd_mode_params params;
 2503                         union cd_pages *page;
 2504 
 2505                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2506                                   ("trying to do CDIOCSETSTEREO\n"));
 2507 
 2508                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2509                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2510                                                  M_WAITOK | M_ZERO);
 2511                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2512                         if (error) {
 2513                                 free(params.mode_buf, M_SCSICD);
 2514                                 break;
 2515                         }
 2516                         page = cdgetpage(&params);
 2517 
 2518                         page->audio.port[LEFT_PORT].channels = 
 2519                                 LEFT_CHANNEL;
 2520                         page->audio.port[RIGHT_PORT].channels = 
 2521                                 RIGHT_CHANNEL;
 2522                         page->audio.port[2].channels = 0;
 2523                         page->audio.port[3].channels = 0;
 2524                         error = cdsetmode(periph, &params);
 2525                         free(params.mode_buf, M_SCSICD);
 2526                 }
 2527                 break;
 2528         case CDIOCSETMUTE:
 2529                 {
 2530                         struct cd_mode_params params;
 2531                         union cd_pages *page;
 2532 
 2533                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2534                                   ("trying to do CDIOCSETMUTE\n"));
 2535 
 2536                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2537                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2538                                                  M_WAITOK | M_ZERO);
 2539                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2540                         if (error) {
 2541                                 free(&params, M_SCSICD);
 2542                                 break;
 2543                         }
 2544                         page = cdgetpage(&params);
 2545 
 2546                         page->audio.port[LEFT_PORT].channels = 0;
 2547                         page->audio.port[RIGHT_PORT].channels = 0;
 2548                         page->audio.port[2].channels = 0;
 2549                         page->audio.port[3].channels = 0;
 2550                         error = cdsetmode(periph, &params);
 2551                         free(params.mode_buf, M_SCSICD);
 2552                 }
 2553                 break;
 2554         case CDIOCSETLEFT:
 2555                 {
 2556                         struct cd_mode_params params;
 2557                         union cd_pages *page;
 2558 
 2559                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2560                                   ("trying to do CDIOCSETLEFT\n"));
 2561 
 2562                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2563                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2564                                                  M_WAITOK | M_ZERO);
 2565                         
 2566                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2567                         if (error) {
 2568                                 free(params.mode_buf, M_SCSICD);
 2569                                 break;
 2570                         }
 2571                         page = cdgetpage(&params);
 2572 
 2573                         page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
 2574                         page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
 2575                         page->audio.port[2].channels = 0;
 2576                         page->audio.port[3].channels = 0;
 2577                         error = cdsetmode(periph, &params);
 2578                         free(params.mode_buf, M_SCSICD);
 2579                 }
 2580                 break;
 2581         case CDIOCSETRIGHT:
 2582                 {
 2583                         struct cd_mode_params params;
 2584                         union cd_pages *page;
 2585 
 2586                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 
 2587                                   ("trying to do CDIOCSETRIGHT\n"));
 2588 
 2589                         params.alloc_len = sizeof(union cd_mode_data_6_10);
 2590                         params.mode_buf = malloc(params.alloc_len, M_SCSICD,
 2591                                                  M_WAITOK | M_ZERO);
 2592 
 2593                         error = cdgetmode(periph, &params, AUDIO_PAGE);
 2594                         if (error) {
 2595                                 free(params.mode_buf, M_SCSICD);
 2596                                 break;
 2597                         }
 2598                         page = cdgetpage(&params);
 2599 
 2600                         page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
 2601                         page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
 2602                         page->audio.port[2].channels = 0;
 2603                         page->audio.port[3].channels = 0;
 2604                         error = cdsetmode(periph, &params);
 2605                         free(params.mode_buf, M_SCSICD);
 2606                 }
 2607                 break;
 2608         case CDIOCRESUME:
 2609                 error = cdpause(periph, 1);
 2610                 break;
 2611         case CDIOCPAUSE:
 2612                 error = cdpause(periph, 0);
 2613                 break;
 2614         case CDIOCSTART:
 2615                 error = cdstartunit(periph, 0);
 2616                 break;
 2617         case CDIOCCLOSE:
 2618                 error = cdstartunit(periph, 1);
 2619                 break;
 2620         case CDIOCSTOP:
 2621                 error = cdstopunit(periph, 0);
 2622                 break;
 2623         case CDIOCEJECT:
 2624                 error = cdstopunit(periph, 1);
 2625                 break;
 2626         case CDIOCALLOW:
 2627                 cdprevent(periph, PR_ALLOW);
 2628                 break;
 2629         case CDIOCPREVENT:
 2630                 cdprevent(periph, PR_PREVENT);
 2631                 break;
 2632         case CDIOCSETDEBUG:
 2633                 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
 2634                 error = ENOTTY;
 2635                 break;
 2636         case CDIOCCLRDEBUG:
 2637                 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
 2638                 error = ENOTTY;
 2639                 break;
 2640         case CDIOCRESET:
 2641                 /* return (cd_reset(periph)); */
 2642                 error = ENOTTY;
 2643                 break;
 2644         case CDRIOCREADSPEED:
 2645                 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
 2646                 break;
 2647         case CDRIOCWRITESPEED:
 2648                 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
 2649                 break;
 2650         case DVDIOCSENDKEY:
 2651         case DVDIOCREPORTKEY: {
 2652                 struct dvd_authinfo *authinfo;
 2653 
 2654                 authinfo = (struct dvd_authinfo *)addr;
 2655 
 2656                 if (cmd == DVDIOCREPORTKEY)
 2657                         error = cdreportkey(periph, authinfo);
 2658                 else
 2659                         error = cdsendkey(periph, authinfo);
 2660                 break;
 2661                 }
 2662         case DVDIOCREADSTRUCTURE: {
 2663                 struct dvd_struct *dvdstruct;
 2664 
 2665                 dvdstruct = (struct dvd_struct *)addr;
 2666 
 2667                 error = cdreaddvdstructure(periph, dvdstruct);
 2668 
 2669                 break;
 2670         }
 2671         default:
 2672                 error = cam_periph_ioctl(periph, cmd, addr, cderror);
 2673                 break;
 2674         }
 2675 
 2676         cam_periph_unlock(periph);
 2677 
 2678         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
 2679         if (error && bootverbose) {
 2680                 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
 2681         }
 2682 
 2683         return (error);
 2684 }
 2685 
 2686 static void
 2687 cdprevent(struct cam_periph *periph, int action)
 2688 {
 2689         union   ccb *ccb;
 2690         struct  cd_softc *softc;
 2691         int     error;
 2692 
 2693         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
 2694 
 2695         softc = (struct cd_softc *)periph->softc;
 2696         
 2697         if (((action == PR_ALLOW)
 2698           && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
 2699          || ((action == PR_PREVENT)
 2700           && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
 2701                 return;
 2702         }
 2703             
 2704         ccb = cdgetccb(periph, /* priority */ 1);
 2705 
 2706         scsi_prevent(&ccb->csio, 
 2707                      /*retries*/ 1,
 2708                      cddone,
 2709                      MSG_SIMPLE_Q_TAG,
 2710                      action,
 2711                      SSD_FULL_SIZE,
 2712                      /* timeout */60000);
 2713         
 2714         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 2715                         /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
 2716 
 2717         xpt_release_ccb(ccb);
 2718 
 2719         if (error == 0) {
 2720                 if (action == PR_ALLOW)
 2721                         softc->flags &= ~CD_FLAG_DISC_LOCKED;
 2722                 else
 2723                         softc->flags |= CD_FLAG_DISC_LOCKED;
 2724         }
 2725 }
 2726 
 2727 /*
 2728  * XXX: the disk media and sector size is only really able to change
 2729  * XXX: while the device is closed.
 2730  */
 2731 static int
 2732 cdcheckmedia(struct cam_periph *periph)
 2733 {
 2734         struct cd_softc *softc;
 2735         struct ioc_toc_header *toch;
 2736         struct cd_toc_single leadout;
 2737         u_int32_t size, toclen;
 2738         int error, num_entries, cdindex;
 2739 
 2740         softc = (struct cd_softc *)periph->softc;
 2741 
 2742         cdprevent(periph, PR_PREVENT);
 2743         softc->disk->d_maxsize = DFLTPHYS;
 2744         softc->disk->d_sectorsize = 2048;
 2745         softc->disk->d_mediasize = 0;
 2746 
 2747         /*
 2748          * Get the disc size and block size.  If we can't get it, we don't
 2749          * have media, most likely.
 2750          */
 2751         if ((error = cdsize(periph, &size)) != 0) {
 2752                 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
 2753                 cdprevent(periph, PR_ALLOW);
 2754                 return (error);
 2755         } else
 2756                 softc->flags |= CD_FLAG_VALID_MEDIA;
 2757 
 2758         /*
 2759          * Now we check the table of contents.  This (currently) is only
 2760          * used for the CDIOCPLAYTRACKS ioctl.  It may be used later to do
 2761          * things like present a separate entry in /dev for each track,
 2762          * like that acd(4) driver does.
 2763          */
 2764         bzero(&softc->toc, sizeof(softc->toc));
 2765         toch = &softc->toc.header;
 2766         /*
 2767          * We will get errors here for media that doesn't have a table of
 2768          * contents.  According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
 2769          * command is presented for a DDCD/CD-R/RW media, where the first TOC
 2770          * has not been recorded (no complete session) and the Format codes
 2771          * 0000b, 0001b, or 0010b are specified, this command shall be rejected
 2772          * with an INVALID FIELD IN CDB.  Devices that are not capable of
 2773          * reading an incomplete session on DDC/CD-R/RW media shall report
 2774          * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
 2775          *
 2776          * So this isn't fatal if we can't read the table of contents, it
 2777          * just means that the user won't be able to issue the play tracks
 2778          * ioctl, and likely lots of other stuff won't work either.  They
 2779          * need to burn the CD before we can do a whole lot with it.  So
 2780          * we don't print anything here if we get an error back.
 2781          */
 2782         error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
 2783                           SF_NO_PRINT);
 2784         /*
 2785          * Errors in reading the table of contents aren't fatal, we just
 2786          * won't have a valid table of contents cached.
 2787          */
 2788         if (error != 0) {
 2789                 error = 0;
 2790                 bzero(&softc->toc, sizeof(softc->toc));
 2791                 goto bailout;
 2792         }
 2793 
 2794         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2795                 toch->starting_track = bcd2bin(toch->starting_track);
 2796                 toch->ending_track = bcd2bin(toch->ending_track);
 2797         }
 2798 
 2799         /* Number of TOC entries, plus leadout */
 2800         num_entries = (toch->ending_track - toch->starting_track) + 2;
 2801 
 2802         if (num_entries <= 0)
 2803                 goto bailout;
 2804 
 2805         toclen = num_entries * sizeof(struct cd_toc_entry);
 2806 
 2807         error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
 2808                           (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
 2809                           SF_NO_PRINT);
 2810         if (error != 0) {
 2811                 error = 0;
 2812                 bzero(&softc->toc, sizeof(softc->toc));
 2813                 goto bailout;
 2814         }
 2815 
 2816         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2817                 toch->starting_track = bcd2bin(toch->starting_track);
 2818                 toch->ending_track = bcd2bin(toch->ending_track);
 2819         }
 2820         /*
 2821          * XXX KDM is this necessary?  Probably only if the drive doesn't
 2822          * return leadout information with the table of contents.
 2823          */
 2824         cdindex = toch->starting_track + num_entries -1;
 2825         if (cdindex == toch->ending_track + 1) {
 2826 
 2827                 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 
 2828                                   (u_int8_t *)&leadout, sizeof(leadout),
 2829                                   SF_NO_PRINT);
 2830                 if (error != 0) {
 2831                         error = 0;
 2832                         goto bailout;
 2833                 }
 2834                 softc->toc.entries[cdindex - toch->starting_track] =
 2835                         leadout.entry;
 2836         }
 2837         if (softc->quirks & CD_Q_BCD_TRACKS) {
 2838                 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
 2839                         softc->toc.entries[cdindex].track =
 2840                                 bcd2bin(softc->toc.entries[cdindex].track);
 2841                 }
 2842         }
 2843 
 2844         softc->flags |= CD_FLAG_VALID_TOC;
 2845         softc->disk->d_maxsize = DFLTPHYS;
 2846         softc->disk->d_sectorsize = softc->params.blksize;
 2847         softc->disk->d_mediasize =
 2848             (off_t)softc->params.blksize * softc->params.disksize;
 2849 
 2850 bailout:
 2851 
 2852         /*
 2853          * We unconditionally (re)set the blocksize each time the
 2854          * CD device is opened.  This is because the CD can change,
 2855          * and therefore the blocksize might change.
 2856          * XXX problems here if some slice or partition is still
 2857          * open with the old size?
 2858          */
 2859         if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
 2860                 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
 2861         softc->disk->d_devstat->block_size = softc->params.blksize;
 2862 
 2863         return (error);
 2864 }
 2865 
 2866 static int
 2867 cdsize(struct cam_periph *periph, u_int32_t *size)
 2868 {
 2869         struct cd_softc *softc;
 2870         union ccb *ccb;
 2871         struct scsi_read_capacity_data *rcap_buf;
 2872         int error;
 2873 
 2874         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
 2875 
 2876         softc = (struct cd_softc *)periph->softc;
 2877              
 2878         ccb = cdgetccb(periph, /* priority */ 1);
 2879 
 2880         rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 
 2881                           M_SCSICD, M_WAITOK);
 2882 
 2883         scsi_read_capacity(&ccb->csio, 
 2884                            /*retries*/ 1,
 2885                            cddone,
 2886                            MSG_SIMPLE_Q_TAG,
 2887                            rcap_buf,
 2888                            SSD_FULL_SIZE,
 2889                            /* timeout */20000);
 2890 
 2891         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 2892                          /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
 2893 
 2894         xpt_release_ccb(ccb);
 2895 
 2896         softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
 2897         softc->params.blksize  = scsi_4btoul(rcap_buf->length);
 2898         /*
 2899          * SCSI-3 mandates that the reported blocksize shall be 2048.
 2900          * Older drives sometimes report funny values, trim it down to
 2901          * 2048, or other parts of the kernel will get confused.
 2902          *
 2903          * XXX we leave drives alone that might report 512 bytes, as
 2904          * well as drives reporting more weird sizes like perhaps 4K.
 2905          */
 2906         if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
 2907                 softc->params.blksize = 2048;
 2908 
 2909         free(rcap_buf, M_SCSICD);
 2910         *size = softc->params.disksize;
 2911 
 2912         return (error);
 2913 
 2914 }
 2915 
 2916 static int
 2917 cd6byteworkaround(union ccb *ccb)
 2918 {
 2919         u_int8_t *cdb;
 2920         struct cam_periph *periph;
 2921         struct cd_softc *softc;
 2922         struct cd_mode_params *params;
 2923         int frozen, found;
 2924 
 2925         periph = xpt_path_periph(ccb->ccb_h.path);
 2926         softc = (struct cd_softc *)periph->softc;
 2927 
 2928         cdb = ccb->csio.cdb_io.cdb_bytes;
 2929 
 2930         if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
 2931          || ((cdb[0] != MODE_SENSE_6)
 2932           && (cdb[0] != MODE_SELECT_6)))
 2933                 return (0);
 2934 
 2935         /*
 2936          * Because there is no convenient place to stash the overall
 2937          * cd_mode_params structure pointer, we have to grab it like this.
 2938          * This means that ALL MODE_SENSE and MODE_SELECT requests in the
 2939          * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
 2940          *
 2941          * XXX It would be nice if, at some point, we could increase the
 2942          * number of available peripheral private pointers.  Both pointers
 2943          * are currently used in most every peripheral driver.
 2944          */
 2945         found = 0;
 2946 
 2947         STAILQ_FOREACH(params, &softc->mode_queue, links) {
 2948                 if (params->mode_buf == ccb->csio.data_ptr) {
 2949                         found = 1;
 2950                         break;
 2951                 }
 2952         }
 2953 
 2954         /*
 2955          * This shouldn't happen.  All mode sense and mode select
 2956          * operations in the cd(4) driver MUST go through cdgetmode() and
 2957          * cdsetmode()!
 2958          */
 2959         if (found == 0) {
 2960                 xpt_print_path(periph->path);
 2961                 printf("mode buffer not found in mode queue!\n");
 2962                 return (0);
 2963         }
 2964 
 2965         params->cdb_size = 10;
 2966         softc->minimum_command_size = 10;
 2967         xpt_print_path(ccb->ccb_h.path);
 2968         printf("%s(6) failed, increasing minimum CDB size to 10 bytes\n",
 2969                (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
 2970 
 2971         if (cdb[0] == MODE_SENSE_6) {
 2972                 struct scsi_mode_sense_10 ms10;
 2973                 struct scsi_mode_sense_6 *ms6;
 2974                 int len;
 2975 
 2976                 ms6 = (struct scsi_mode_sense_6 *)cdb;
 2977 
 2978                 bzero(&ms10, sizeof(ms10));
 2979                 ms10.opcode = MODE_SENSE_10;
 2980                 ms10.byte2 = ms6->byte2;
 2981                 ms10.page = ms6->page;
 2982 
 2983                 /*
 2984                  * 10 byte mode header, block descriptor,
 2985                  * sizeof(union cd_pages)
 2986                  */
 2987                 len = sizeof(struct cd_mode_data_10);
 2988                 ccb->csio.dxfer_len = len;
 2989 
 2990                 scsi_ulto2b(len, ms10.length);
 2991                 ms10.control = ms6->control;
 2992                 bcopy(&ms10, cdb, 10);
 2993                 ccb->csio.cdb_len = 10;
 2994         } else {
 2995                 struct scsi_mode_select_10 ms10;
 2996                 struct scsi_mode_select_6 *ms6;
 2997                 struct scsi_mode_header_6 *header6;
 2998                 struct scsi_mode_header_10 *header10;
 2999                 struct scsi_mode_page_header *page_header;
 3000                 int blk_desc_len, page_num, page_size, len;
 3001 
 3002                 ms6 = (struct scsi_mode_select_6 *)cdb;
 3003 
 3004                 bzero(&ms10, sizeof(ms10));
 3005                 ms10.opcode = MODE_SELECT_10;
 3006                 ms10.byte2 = ms6->byte2;
 3007 
 3008                 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
 3009                 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
 3010 
 3011                 page_header = find_mode_page_6(header6);
 3012                 page_num = page_header->page_code;
 3013 
 3014                 blk_desc_len = header6->blk_desc_len;
 3015 
 3016                 page_size = cdgetpagesize(page_num);
 3017 
 3018                 if (page_size != (page_header->page_length +
 3019                     sizeof(*page_header)))
 3020                         page_size = page_header->page_length +
 3021                                 sizeof(*page_header);
 3022 
 3023                 len = sizeof(*header10) + blk_desc_len + page_size;
 3024 
 3025                 len = min(params->alloc_len, len);
 3026 
 3027                 /*
 3028                  * Since the 6 byte parameter header is shorter than the 10
 3029                  * byte parameter header, we need to copy the actual mode
 3030                  * page data, and the block descriptor, if any, so things wind
 3031                  * up in the right place.  The regions will overlap, but
 3032                  * bcopy() does the right thing.
 3033                  */
 3034                 bcopy(params->mode_buf + sizeof(*header6),
 3035                       params->mode_buf + sizeof(*header10),
 3036                       len - sizeof(*header10));
 3037 
 3038                 /* Make sure these fields are set correctly. */
 3039                 scsi_ulto2b(0, header10->data_length);
 3040                 header10->medium_type = 0;
 3041                 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
 3042 
 3043                 ccb->csio.dxfer_len = len;
 3044 
 3045                 scsi_ulto2b(len, ms10.length);
 3046                 ms10.control = ms6->control;
 3047                 bcopy(&ms10, cdb, 10);
 3048                 ccb->csio.cdb_len = 10;
 3049         }
 3050 
 3051         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
 3052         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 3053         xpt_action(ccb);
 3054         if (frozen) {
 3055                 cam_release_devq(ccb->ccb_h.path,
 3056                                  /*relsim_flags*/0,
 3057                                  /*openings*/0,
 3058                                  /*timeout*/0,
 3059                                  /*getcount_only*/0);
 3060         }
 3061 
 3062         return (ERESTART);
 3063 }
 3064 
 3065 static int
 3066 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 3067 {
 3068         struct cd_softc *softc;
 3069         struct cam_periph *periph;
 3070         int error;
 3071 
 3072         periph = xpt_path_periph(ccb->ccb_h.path);
 3073         softc = (struct cd_softc *)periph->softc;
 3074 
 3075         error = 0;
 3076 
 3077         /*
 3078          * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
 3079          * CDB comes back with this particular error, try transforming it
 3080          * into the 10 byte version.
 3081          */
 3082         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
 3083                 error = cd6byteworkaround(ccb);
 3084         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
 3085                      CAM_SCSI_STATUS_ERROR)
 3086          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
 3087          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
 3088          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
 3089          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
 3090                 int sense_key, error_code, asc, ascq;
 3091 
 3092                 scsi_extract_sense(&ccb->csio.sense_data,
 3093                                    &error_code, &sense_key, &asc, &ascq);
 3094                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
 3095                         error = cd6byteworkaround(ccb);
 3096         }
 3097 
 3098         if (error == ERESTART)
 3099                 return (error);
 3100 
 3101         /*
 3102          * XXX
 3103          * Until we have a better way of doing pack validation,
 3104          * don't treat UAs as errors.
 3105          */
 3106         sense_flags |= SF_RETRY_UA;
 3107         return (cam_periph_error(ccb, cam_flags, sense_flags, 
 3108                                  &softc->saved_ccb));
 3109 }
 3110 
 3111 /*
 3112  * Read table of contents
 3113  */
 3114 static int 
 3115 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 
 3116           u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
 3117 {
 3118         struct scsi_read_toc *scsi_cmd;
 3119         u_int32_t ntoc;
 3120         struct ccb_scsiio *csio;
 3121         union ccb *ccb;
 3122         int error;
 3123 
 3124         ntoc = len;
 3125         error = 0;
 3126 
 3127         ccb = cdgetccb(periph, /* priority */ 1);
 3128 
 3129         csio = &ccb->csio;
 3130 
 3131         cam_fill_csio(csio, 
 3132                       /* retries */ 1, 
 3133                       /* cbfcnp */ cddone, 
 3134                       /* flags */ CAM_DIR_IN,
 3135                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3136                       /* data_ptr */ data,
 3137                       /* dxfer_len */ len,
 3138                       /* sense_len */ SSD_FULL_SIZE,
 3139                       sizeof(struct scsi_read_toc),
 3140                       /* timeout */ 50000);
 3141 
 3142         scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
 3143         bzero (scsi_cmd, sizeof(*scsi_cmd));
 3144 
 3145         if (mode == CD_MSF_FORMAT)
 3146                 scsi_cmd->byte2 |= CD_MSF;
 3147         scsi_cmd->from_track = start;
 3148         /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
 3149         scsi_cmd->data_len[0] = (ntoc) >> 8;
 3150         scsi_cmd->data_len[1] = (ntoc) & 0xff;
 3151 
 3152         scsi_cmd->op_code = READ_TOC;
 3153 
 3154         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3155                          /*sense_flags*/SF_RETRY_UA | sense_flags);
 3156 
 3157         xpt_release_ccb(ccb);
 3158 
 3159         return(error);
 3160 }
 3161 
 3162 static int
 3163 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 
 3164                  u_int32_t format, int track, 
 3165                  struct cd_sub_channel_info *data, u_int32_t len) 
 3166 {
 3167         struct scsi_read_subchannel *scsi_cmd;
 3168         struct ccb_scsiio *csio;
 3169         union ccb *ccb;
 3170         int error;
 3171 
 3172         error = 0;
 3173 
 3174         ccb = cdgetccb(periph, /* priority */ 1);
 3175 
 3176         csio = &ccb->csio;
 3177 
 3178         cam_fill_csio(csio, 
 3179                       /* retries */ 1, 
 3180                       /* cbfcnp */ cddone, 
 3181                       /* flags */ CAM_DIR_IN,
 3182                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3183                       /* data_ptr */ (u_int8_t *)data,
 3184                       /* dxfer_len */ len,
 3185                       /* sense_len */ SSD_FULL_SIZE,
 3186                       sizeof(struct scsi_read_subchannel),
 3187                       /* timeout */ 50000);
 3188 
 3189         scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
 3190         bzero (scsi_cmd, sizeof(*scsi_cmd));
 3191 
 3192         scsi_cmd->op_code = READ_SUBCHANNEL;
 3193         if (mode == CD_MSF_FORMAT)
 3194                 scsi_cmd->byte1 |= CD_MSF;
 3195         scsi_cmd->byte2 = SRS_SUBQ;
 3196         scsi_cmd->subchan_format = format;
 3197         scsi_cmd->track = track;
 3198         scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
 3199         scsi_cmd->control = 0;
 3200 
 3201         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3202                          /*sense_flags*/SF_RETRY_UA);
 3203 
 3204         xpt_release_ccb(ccb);
 3205 
 3206         return(error);
 3207 }
 3208 
 3209 
 3210 /*
 3211  * All MODE_SENSE requests in the cd(4) driver MUST go through this
 3212  * routine.  See comments in cd6byteworkaround() for details.
 3213  */
 3214 static int
 3215 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
 3216           u_int32_t page)
 3217 {
 3218         struct ccb_scsiio *csio;
 3219         struct cd_softc *softc;
 3220         union ccb *ccb;
 3221         int param_len;
 3222         int error;
 3223 
 3224         softc = (struct cd_softc *)periph->softc;
 3225 
 3226         ccb = cdgetccb(periph, /* priority */ 1);
 3227 
 3228         csio = &ccb->csio;
 3229 
 3230         data->cdb_size = softc->minimum_command_size;
 3231         if (data->cdb_size < 10)
 3232                 param_len = sizeof(struct cd_mode_data);
 3233         else
 3234                 param_len = sizeof(struct cd_mode_data_10);
 3235 
 3236         /* Don't say we've got more room than we actually allocated */
 3237         param_len = min(param_len, data->alloc_len);
 3238 
 3239         scsi_mode_sense_len(csio,
 3240                             /* retries */ 1,
 3241                             /* cbfcnp */ cddone,
 3242                             /* tag_action */ MSG_SIMPLE_Q_TAG,
 3243                             /* dbd */ 0,
 3244                             /* page_code */ SMS_PAGE_CTRL_CURRENT,
 3245                             /* page */ page,
 3246                             /* param_buf */ data->mode_buf,
 3247                             /* param_len */ param_len,
 3248                             /* minimum_cmd_size */ softc->minimum_command_size,
 3249                             /* sense_len */ SSD_FULL_SIZE,
 3250                             /* timeout */ 50000);
 3251 
 3252         /*
 3253          * It would be nice not to have to do this, but there's no
 3254          * available pointer in the CCB that would allow us to stuff the
 3255          * mode params structure in there and retrieve it in
 3256          * cd6byteworkaround(), so we can set the cdb size.  The cdb size
 3257          * lets the caller know what CDB size we ended up using, so they
 3258          * can find the actual mode page offset.
 3259          */
 3260         STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
 3261 
 3262         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3263                          /*sense_flags*/SF_RETRY_UA);
 3264 
 3265         xpt_release_ccb(ccb);
 3266 
 3267         STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
 3268 
 3269         /*
 3270          * This is a bit of belt-and-suspenders checking, but if we run
 3271          * into a situation where the target sends back multiple block
 3272          * descriptors, we might not have enough space in the buffer to
 3273          * see the whole mode page.  Better to return an error than
 3274          * potentially access memory beyond our malloced region.
 3275          */
 3276         if (error == 0) {
 3277                 u_int32_t data_len;
 3278 
 3279                 if (data->cdb_size == 10) {
 3280                         struct scsi_mode_header_10 *hdr10;
 3281 
 3282                         hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
 3283                         data_len = scsi_2btoul(hdr10->data_length);
 3284                         data_len += sizeof(hdr10->data_length);
 3285                 } else {
 3286                         struct scsi_mode_header_6 *hdr6;
 3287 
 3288                         hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
 3289                         data_len = hdr6->data_length;
 3290                         data_len += sizeof(hdr6->data_length);
 3291                 }
 3292 
 3293                 /*
 3294                  * Complain if there is more mode data available than we
 3295                  * allocated space for.  This could potentially happen if
 3296                  * we miscalculated the page length for some reason, if the
 3297                  * drive returns multiple block descriptors, or if it sets
 3298                  * the data length incorrectly.
 3299                  */
 3300                 if (data_len > data->alloc_len) {
 3301                         xpt_print_path(periph->path);
 3302                         printf("allocated modepage %d length %d < returned "
 3303                                "length %d\n", page, data->alloc_len, data_len);
 3304 
 3305                         error = ENOSPC;
 3306                 }
 3307         }
 3308         return (error);
 3309 }
 3310 
 3311 /*
 3312  * All MODE_SELECT requests in the cd(4) driver MUST go through this
 3313  * routine.  See comments in cd6byteworkaround() for details.
 3314  */
 3315 static int
 3316 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
 3317 {
 3318         struct ccb_scsiio *csio;
 3319         struct cd_softc *softc;
 3320         union ccb *ccb;
 3321         int cdb_size, param_len;
 3322         int error;
 3323 
 3324         softc = (struct cd_softc *)periph->softc;
 3325 
 3326         ccb = cdgetccb(periph, /* priority */ 1);
 3327 
 3328         csio = &ccb->csio;
 3329 
 3330         error = 0;
 3331 
 3332         /*
 3333          * If the data is formatted for the 10 byte version of the mode
 3334          * select parameter list, we need to use the 10 byte CDB.
 3335          * Otherwise, we use whatever the stored minimum command size.
 3336          */
 3337         if (data->cdb_size == 10)
 3338                 cdb_size = data->cdb_size;
 3339         else
 3340                 cdb_size = softc->minimum_command_size;
 3341 
 3342         if (cdb_size >= 10) {
 3343                 struct scsi_mode_header_10 *mode_header;
 3344                 u_int32_t data_len;
 3345 
 3346                 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
 3347 
 3348                 data_len = scsi_2btoul(mode_header->data_length);
 3349 
 3350                 scsi_ulto2b(0, mode_header->data_length);
 3351                 /*
 3352                  * SONY drives do not allow a mode select with a medium_type
 3353                  * value that has just been returned by a mode sense; use a
 3354                  * medium_type of 0 (Default) instead.
 3355                  */
 3356                 mode_header->medium_type = 0;
 3357 
 3358                 /*
 3359                  * Pass back whatever the drive passed to us, plus the size
 3360                  * of the data length field.
 3361                  */
 3362                 param_len = data_len + sizeof(mode_header->data_length);
 3363 
 3364         } else {
 3365                 struct scsi_mode_header_6 *mode_header;
 3366 
 3367                 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
 3368 
 3369                 param_len = mode_header->data_length + 1;
 3370 
 3371                 mode_header->data_length = 0;
 3372                 /*
 3373                  * SONY drives do not allow a mode select with a medium_type
 3374                  * value that has just been returned by a mode sense; use a
 3375                  * medium_type of 0 (Default) instead.
 3376                  */
 3377                 mode_header->medium_type = 0;
 3378         }
 3379 
 3380         /* Don't say we've got more room than we actually allocated */
 3381         param_len = min(param_len, data->alloc_len);
 3382 
 3383         scsi_mode_select_len(csio,
 3384                              /* retries */ 1,
 3385                              /* cbfcnp */ cddone,
 3386                              /* tag_action */ MSG_SIMPLE_Q_TAG,
 3387                              /* scsi_page_fmt */ 1,
 3388                              /* save_pages */ 0,
 3389                              /* param_buf */ data->mode_buf,
 3390                              /* param_len */ param_len,
 3391                              /* minimum_cmd_size */ cdb_size,
 3392                              /* sense_len */ SSD_FULL_SIZE,
 3393                              /* timeout */ 50000);
 3394 
 3395         /* See comments in cdgetmode() and cd6byteworkaround(). */
 3396         STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
 3397 
 3398         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3399                          /*sense_flags*/SF_RETRY_UA);
 3400 
 3401         xpt_release_ccb(ccb);
 3402 
 3403         STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
 3404 
 3405         return (error);
 3406 }
 3407 
 3408 
 3409 static int 
 3410 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
 3411 {
 3412         struct ccb_scsiio *csio;
 3413         union ccb *ccb;
 3414         int error;
 3415         u_int8_t cdb_len;
 3416 
 3417         error = 0;
 3418         ccb = cdgetccb(periph, /* priority */ 1);
 3419         csio = &ccb->csio;
 3420         /*
 3421          * Use the smallest possible command to perform the operation.
 3422          */
 3423         if ((len & 0xffff0000) == 0) {
 3424                 /*
 3425                  * We can fit in a 10 byte cdb.
 3426                  */
 3427                 struct scsi_play_10 *scsi_cmd;
 3428 
 3429                 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
 3430                 bzero (scsi_cmd, sizeof(*scsi_cmd));
 3431                 scsi_cmd->op_code = PLAY_10;
 3432                 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
 3433                 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
 3434                 cdb_len = sizeof(*scsi_cmd);
 3435         } else  {
 3436                 struct scsi_play_12 *scsi_cmd;
 3437 
 3438                 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
 3439                 bzero (scsi_cmd, sizeof(*scsi_cmd));
 3440                 scsi_cmd->op_code = PLAY_12;
 3441                 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
 3442                 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
 3443                 cdb_len = sizeof(*scsi_cmd);
 3444         }
 3445         cam_fill_csio(csio,
 3446                       /*retries*/2,
 3447                       cddone,
 3448                       /*flags*/CAM_DIR_NONE,
 3449                       MSG_SIMPLE_Q_TAG,
 3450                       /*dataptr*/NULL,
 3451                       /*datalen*/0,
 3452                       /*sense_len*/SSD_FULL_SIZE,
 3453                       cdb_len,
 3454                       /*timeout*/50 * 1000);
 3455 
 3456         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3457                          /*sense_flags*/SF_RETRY_UA);
 3458 
 3459         xpt_release_ccb(ccb);
 3460 
 3461         return(error);
 3462 }
 3463 
 3464 static int
 3465 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
 3466           u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
 3467 {
 3468         struct scsi_play_msf *scsi_cmd;
 3469         struct ccb_scsiio *csio;
 3470         union ccb *ccb;
 3471         int error;
 3472 
 3473         error = 0;
 3474 
 3475         ccb = cdgetccb(periph, /* priority */ 1);
 3476 
 3477         csio = &ccb->csio;
 3478 
 3479         cam_fill_csio(csio, 
 3480                       /* retries */ 1, 
 3481                       /* cbfcnp */ cddone, 
 3482                       /* flags */ CAM_DIR_NONE,
 3483                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3484                       /* data_ptr */ NULL,
 3485                       /* dxfer_len */ 0,
 3486                       /* sense_len */ SSD_FULL_SIZE,
 3487                       sizeof(struct scsi_play_msf),
 3488                       /* timeout */ 50000);
 3489 
 3490         scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
 3491         bzero (scsi_cmd, sizeof(*scsi_cmd));
 3492 
 3493         scsi_cmd->op_code = PLAY_MSF;
 3494         scsi_cmd->start_m = startm;
 3495         scsi_cmd->start_s = starts;
 3496         scsi_cmd->start_f = startf;
 3497         scsi_cmd->end_m = endm;
 3498         scsi_cmd->end_s = ends;
 3499         scsi_cmd->end_f = endf; 
 3500 
 3501         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3502                          /*sense_flags*/SF_RETRY_UA);
 3503         
 3504         xpt_release_ccb(ccb);
 3505 
 3506         return(error);
 3507 }
 3508 
 3509 
 3510 static int
 3511 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
 3512              u_int32_t etrack, u_int32_t eindex)
 3513 {
 3514         struct scsi_play_track *scsi_cmd;
 3515         struct ccb_scsiio *csio;
 3516         union ccb *ccb;
 3517         int error;
 3518 
 3519         error = 0;
 3520 
 3521         ccb = cdgetccb(periph, /* priority */ 1);
 3522 
 3523         csio = &ccb->csio;
 3524 
 3525         cam_fill_csio(csio, 
 3526                       /* retries */ 1, 
 3527                       /* cbfcnp */ cddone, 
 3528                       /* flags */ CAM_DIR_NONE,
 3529                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3530                       /* data_ptr */ NULL,
 3531                       /* dxfer_len */ 0,
 3532                       /* sense_len */ SSD_FULL_SIZE,
 3533                       sizeof(struct scsi_play_track),
 3534                       /* timeout */ 50000);
 3535 
 3536         scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
 3537         bzero (scsi_cmd, sizeof(*scsi_cmd));
 3538 
 3539         scsi_cmd->op_code = PLAY_TRACK;
 3540         scsi_cmd->start_track = strack;
 3541         scsi_cmd->start_index = sindex;
 3542         scsi_cmd->end_track = etrack;
 3543         scsi_cmd->end_index = eindex;
 3544 
 3545         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3546                          /*sense_flags*/SF_RETRY_UA);
 3547 
 3548         xpt_release_ccb(ccb);
 3549 
 3550         return(error);
 3551 }
 3552 
 3553 static int
 3554 cdpause(struct cam_periph *periph, u_int32_t go)
 3555 {
 3556         struct scsi_pause *scsi_cmd;
 3557         struct ccb_scsiio *csio;
 3558         union ccb *ccb;
 3559         int error;
 3560 
 3561         error = 0;
 3562 
 3563         ccb = cdgetccb(periph, /* priority */ 1);
 3564 
 3565         csio = &ccb->csio;
 3566 
 3567         cam_fill_csio(csio, 
 3568                       /* retries */ 1, 
 3569                       /* cbfcnp */ cddone, 
 3570                       /* flags */ CAM_DIR_NONE,
 3571                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3572                       /* data_ptr */ NULL,
 3573                       /* dxfer_len */ 0,
 3574                       /* sense_len */ SSD_FULL_SIZE,
 3575                       sizeof(struct scsi_pause),
 3576                       /* timeout */ 50000);
 3577 
 3578         scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
 3579         bzero (scsi_cmd, sizeof(*scsi_cmd));
 3580 
 3581         scsi_cmd->op_code = PAUSE;
 3582         scsi_cmd->resume = go;
 3583 
 3584         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3585                          /*sense_flags*/SF_RETRY_UA);
 3586 
 3587         xpt_release_ccb(ccb);
 3588 
 3589         return(error);
 3590 }
 3591 
 3592 static int
 3593 cdstartunit(struct cam_periph *periph, int load)
 3594 {
 3595         union ccb *ccb;
 3596         int error;
 3597 
 3598         error = 0;
 3599 
 3600         ccb = cdgetccb(periph, /* priority */ 1);
 3601 
 3602         scsi_start_stop(&ccb->csio,
 3603                         /* retries */ 1,
 3604                         /* cbfcnp */ cddone,
 3605                         /* tag_action */ MSG_SIMPLE_Q_TAG,
 3606                         /* start */ TRUE,
 3607                         /* load_eject */ load,
 3608                         /* immediate */ FALSE,
 3609                         /* sense_len */ SSD_FULL_SIZE,
 3610                         /* timeout */ 50000);
 3611 
 3612         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3613                          /*sense_flags*/SF_RETRY_UA);
 3614 
 3615         xpt_release_ccb(ccb);
 3616 
 3617         return(error);
 3618 }
 3619 
 3620 static int
 3621 cdstopunit(struct cam_periph *periph, u_int32_t eject)
 3622 {
 3623         union ccb *ccb;
 3624         int error;
 3625 
 3626         error = 0;
 3627 
 3628         ccb = cdgetccb(periph, /* priority */ 1);
 3629 
 3630         scsi_start_stop(&ccb->csio,
 3631                         /* retries */ 1,
 3632                         /* cbfcnp */ cddone,
 3633                         /* tag_action */ MSG_SIMPLE_Q_TAG,
 3634                         /* start */ FALSE,
 3635                         /* load_eject */ eject,
 3636                         /* immediate */ FALSE,
 3637                         /* sense_len */ SSD_FULL_SIZE,
 3638                         /* timeout */ 50000);
 3639 
 3640         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3641                          /*sense_flags*/SF_RETRY_UA);
 3642 
 3643         xpt_release_ccb(ccb);
 3644 
 3645         return(error);
 3646 }
 3647 
 3648 static int
 3649 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
 3650 {
 3651         struct scsi_set_speed *scsi_cmd;
 3652         struct ccb_scsiio *csio;
 3653         union ccb *ccb;
 3654         int error;
 3655 
 3656         error = 0;
 3657         ccb = cdgetccb(periph, /* priority */ 1);
 3658         csio = &ccb->csio;
 3659 
 3660         /* Preserve old behavior: units in multiples of CDROM speed */
 3661         if (rdspeed < 177)
 3662                 rdspeed *= 177;
 3663         if (wrspeed < 177)
 3664                 wrspeed *= 177;
 3665 
 3666         cam_fill_csio(csio,
 3667                       /* retries */ 1,
 3668                       /* cbfcnp */ cddone,
 3669                       /* flags */ CAM_DIR_NONE,
 3670                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3671                       /* data_ptr */ NULL,
 3672                       /* dxfer_len */ 0,
 3673                       /* sense_len */ SSD_FULL_SIZE,
 3674                       sizeof(struct scsi_set_speed),
 3675                       /* timeout */ 50000);
 3676 
 3677         scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
 3678         bzero(scsi_cmd, sizeof(*scsi_cmd));
 3679 
 3680         scsi_cmd->opcode = SET_CD_SPEED;
 3681         scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
 3682         scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
 3683 
 3684         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3685                          /*sense_flags*/SF_RETRY_UA);
 3686 
 3687         xpt_release_ccb(ccb);
 3688 
 3689         return(error);
 3690 }
 3691 
 3692 static int
 3693 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
 3694 {
 3695         union ccb *ccb;
 3696         u_int8_t *databuf;
 3697         u_int32_t lba;
 3698         int error;
 3699         int length;
 3700 
 3701         error = 0;
 3702         databuf = NULL;
 3703         lba = 0;
 3704 
 3705         ccb = cdgetccb(periph, /* priority */ 1);
 3706 
 3707         switch (authinfo->format) {
 3708         case DVD_REPORT_AGID:
 3709                 length = sizeof(struct scsi_report_key_data_agid);
 3710                 break;
 3711         case DVD_REPORT_CHALLENGE:
 3712                 length = sizeof(struct scsi_report_key_data_challenge);
 3713                 break;
 3714         case DVD_REPORT_KEY1:
 3715                 length = sizeof(struct scsi_report_key_data_key1_key2);
 3716                 break;
 3717         case DVD_REPORT_TITLE_KEY:
 3718                 length = sizeof(struct scsi_report_key_data_title);
 3719                 /* The lba field is only set for the title key */
 3720                 lba = authinfo->lba;
 3721                 break;
 3722         case DVD_REPORT_ASF:
 3723                 length = sizeof(struct scsi_report_key_data_asf);
 3724                 break;
 3725         case DVD_REPORT_RPC:
 3726                 length = sizeof(struct scsi_report_key_data_rpc);
 3727                 break;
 3728         case DVD_INVALIDATE_AGID:
 3729                 length = 0;
 3730                 break;
 3731         default:
 3732                 error = EINVAL;
 3733                 goto bailout;
 3734                 break; /* NOTREACHED */
 3735         }
 3736 
 3737         if (length != 0) {
 3738                 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
 3739         } else
 3740                 databuf = NULL;
 3741 
 3742 
 3743         scsi_report_key(&ccb->csio,
 3744                         /* retries */ 1,
 3745                         /* cbfcnp */ cddone,
 3746                         /* tag_action */ MSG_SIMPLE_Q_TAG,
 3747                         /* lba */ lba,
 3748                         /* agid */ authinfo->agid,
 3749                         /* key_format */ authinfo->format,
 3750                         /* data_ptr */ databuf,
 3751                         /* dxfer_len */ length,
 3752                         /* sense_len */ SSD_FULL_SIZE,
 3753                         /* timeout */ 50000);
 3754 
 3755         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3756                          /*sense_flags*/SF_RETRY_UA);
 3757 
 3758         if (error != 0)
 3759                 goto bailout;
 3760 
 3761         if (ccb->csio.resid != 0) {
 3762                 xpt_print_path(periph->path);
 3763                 printf("warning, residual for report key command is %d\n",
 3764                        ccb->csio.resid);
 3765         }
 3766 
 3767         switch(authinfo->format) {
 3768         case DVD_REPORT_AGID: {
 3769                 struct scsi_report_key_data_agid *agid_data;
 3770 
 3771                 agid_data = (struct scsi_report_key_data_agid *)databuf;
 3772 
 3773                 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
 3774                         RKD_AGID_SHIFT;
 3775                 break;
 3776         }
 3777         case DVD_REPORT_CHALLENGE: {
 3778                 struct scsi_report_key_data_challenge *chal_data;
 3779 
 3780                 chal_data = (struct scsi_report_key_data_challenge *)databuf;
 3781 
 3782                 bcopy(chal_data->challenge_key, authinfo->keychal,
 3783                       min(sizeof(chal_data->challenge_key),
 3784                           sizeof(authinfo->keychal)));
 3785                 break;
 3786         }
 3787         case DVD_REPORT_KEY1: {
 3788                 struct scsi_report_key_data_key1_key2 *key1_data;
 3789 
 3790                 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
 3791 
 3792                 bcopy(key1_data->key1, authinfo->keychal,
 3793                       min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
 3794                 break;
 3795         }
 3796         case DVD_REPORT_TITLE_KEY: {
 3797                 struct scsi_report_key_data_title *title_data;
 3798 
 3799                 title_data = (struct scsi_report_key_data_title *)databuf;
 3800 
 3801                 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
 3802                         RKD_TITLE_CPM_SHIFT;
 3803                 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
 3804                         RKD_TITLE_CP_SEC_SHIFT;
 3805                 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
 3806                         RKD_TITLE_CMGS_SHIFT;
 3807                 bcopy(title_data->title_key, authinfo->keychal,
 3808                       min(sizeof(title_data->title_key),
 3809                           sizeof(authinfo->keychal)));
 3810                 break;
 3811         }
 3812         case DVD_REPORT_ASF: {
 3813                 struct scsi_report_key_data_asf *asf_data;
 3814 
 3815                 asf_data = (struct scsi_report_key_data_asf *)databuf;
 3816 
 3817                 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
 3818                 break;
 3819         }
 3820         case DVD_REPORT_RPC: {
 3821                 struct scsi_report_key_data_rpc *rpc_data;
 3822 
 3823                 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
 3824 
 3825                 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
 3826                         RKD_RPC_TYPE_SHIFT;
 3827                 authinfo->vend_rsts =
 3828                         (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
 3829                         RKD_RPC_VENDOR_RESET_SHIFT;
 3830                 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
 3831                 authinfo->region = rpc_data->region_mask;
 3832                 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
 3833                 break;
 3834         }
 3835         case DVD_INVALIDATE_AGID:
 3836                 break;
 3837         default:
 3838                 /* This should be impossible, since we checked above */
 3839                 error = EINVAL;
 3840                 goto bailout;
 3841                 break; /* NOTREACHED */
 3842         }
 3843 bailout:
 3844         if (databuf != NULL)
 3845                 free(databuf, M_DEVBUF);
 3846 
 3847         xpt_release_ccb(ccb);
 3848 
 3849         return(error);
 3850 }
 3851 
 3852 static int
 3853 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
 3854 {
 3855         union ccb *ccb;
 3856         u_int8_t *databuf;
 3857         int length;
 3858         int error;
 3859 
 3860         error = 0;
 3861         databuf = NULL;
 3862 
 3863         ccb = cdgetccb(periph, /* priority */ 1);
 3864 
 3865         switch(authinfo->format) {
 3866         case DVD_SEND_CHALLENGE: {
 3867                 struct scsi_report_key_data_challenge *challenge_data;
 3868 
 3869                 length = sizeof(*challenge_data);
 3870 
 3871                 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
 3872 
 3873                 databuf = (u_int8_t *)challenge_data;
 3874 
 3875                 scsi_ulto2b(length - sizeof(challenge_data->data_len),
 3876                             challenge_data->data_len);
 3877 
 3878                 bcopy(authinfo->keychal, challenge_data->challenge_key,
 3879                       min(sizeof(authinfo->keychal),
 3880                           sizeof(challenge_data->challenge_key)));
 3881                 break;
 3882         }
 3883         case DVD_SEND_KEY2: {
 3884                 struct scsi_report_key_data_key1_key2 *key2_data;
 3885 
 3886                 length = sizeof(*key2_data);
 3887 
 3888                 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
 3889 
 3890                 databuf = (u_int8_t *)key2_data;
 3891 
 3892                 scsi_ulto2b(length - sizeof(key2_data->data_len),
 3893                             key2_data->data_len);
 3894 
 3895                 bcopy(authinfo->keychal, key2_data->key1,
 3896                       min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
 3897 
 3898                 break;
 3899         }
 3900         case DVD_SEND_RPC: {
 3901                 struct scsi_send_key_data_rpc *rpc_data;
 3902 
 3903                 length = sizeof(*rpc_data);
 3904 
 3905                 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
 3906 
 3907                 databuf = (u_int8_t *)rpc_data;
 3908 
 3909                 scsi_ulto2b(length - sizeof(rpc_data->data_len),
 3910                             rpc_data->data_len);
 3911 
 3912                 rpc_data->region_code = authinfo->region;
 3913                 break;
 3914         }
 3915         default:
 3916                 error = EINVAL;
 3917                 goto bailout;
 3918                 break; /* NOTREACHED */
 3919         }
 3920 
 3921         scsi_send_key(&ccb->csio,
 3922                       /* retries */ 1,
 3923                       /* cbfcnp */ cddone,
 3924                       /* tag_action */ MSG_SIMPLE_Q_TAG,
 3925                       /* agid */ authinfo->agid,
 3926                       /* key_format */ authinfo->format,
 3927                       /* data_ptr */ databuf,
 3928                       /* dxfer_len */ length,
 3929                       /* sense_len */ SSD_FULL_SIZE,
 3930                       /* timeout */ 50000);
 3931 
 3932         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 3933                          /*sense_flags*/SF_RETRY_UA);
 3934 
 3935 bailout:
 3936 
 3937         if (databuf != NULL)
 3938                 free(databuf, M_DEVBUF);
 3939 
 3940         xpt_release_ccb(ccb);
 3941 
 3942         return(error);
 3943 }
 3944 
 3945 static int
 3946 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
 3947 {
 3948         union ccb *ccb;
 3949         u_int8_t *databuf;
 3950         u_int32_t address;
 3951         int error;
 3952         int length;
 3953 
 3954         error = 0;
 3955         databuf = NULL;
 3956         /* The address is reserved for many of the formats */
 3957         address = 0;
 3958 
 3959         ccb = cdgetccb(periph, /* priority */ 1);
 3960 
 3961         switch(dvdstruct->format) {
 3962         case DVD_STRUCT_PHYSICAL:
 3963                 length = sizeof(struct scsi_read_dvd_struct_data_physical);
 3964                 break;
 3965         case DVD_STRUCT_COPYRIGHT:
 3966                 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
 3967                 break;
 3968         case DVD_STRUCT_DISCKEY:
 3969                 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
 3970                 break;
 3971         case DVD_STRUCT_BCA:
 3972                 length = sizeof(struct scsi_read_dvd_struct_data_bca);
 3973                 break;
 3974         case DVD_STRUCT_MANUFACT:
 3975                 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
 3976                 break;
 3977         case DVD_STRUCT_CMI:
 3978                 error = ENODEV;
 3979                 goto bailout;
 3980 #ifdef notyet
 3981                 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
 3982                 address = dvdstruct->address;
 3983 #endif
 3984                 break; /* NOTREACHED */
 3985         case DVD_STRUCT_PROTDISCID:
 3986                 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
 3987                 break;
 3988         case DVD_STRUCT_DISCKEYBLOCK:
 3989                 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
 3990                 break;
 3991         case DVD_STRUCT_DDS:
 3992                 length = sizeof(struct scsi_read_dvd_struct_data_dds);
 3993                 break;
 3994         case DVD_STRUCT_MEDIUM_STAT:
 3995                 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
 3996                 break;
 3997         case DVD_STRUCT_SPARE_AREA:
 3998                 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
 3999                 break;
 4000         case DVD_STRUCT_RMD_LAST:
 4001                 error = ENODEV;
 4002                 goto bailout;
 4003 #ifdef notyet
 4004                 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
 4005                 address = dvdstruct->address;
 4006 #endif
 4007                 break; /* NOTREACHED */
 4008         case DVD_STRUCT_RMD_RMA:
 4009                 error = ENODEV;
 4010                 goto bailout;
 4011 #ifdef notyet
 4012                 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
 4013                 address = dvdstruct->address;
 4014 #endif
 4015                 break; /* NOTREACHED */
 4016         case DVD_STRUCT_PRERECORDED:
 4017                 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
 4018                 break;
 4019         case DVD_STRUCT_UNIQUEID:
 4020                 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
 4021                 break;
 4022         case DVD_STRUCT_DCB:
 4023                 error = ENODEV;
 4024                 goto bailout;
 4025 #ifdef notyet
 4026                 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
 4027                 address = dvdstruct->address;
 4028 #endif
 4029                 break; /* NOTREACHED */
 4030         case DVD_STRUCT_LIST:
 4031                 /*
 4032                  * This is the maximum allocation length for the READ DVD
 4033                  * STRUCTURE command.  There's nothing in the MMC3 spec
 4034                  * that indicates a limit in the amount of data that can
 4035                  * be returned from this call, other than the limits
 4036                  * imposed by the 2-byte length variables.
 4037                  */
 4038                 length = 65535;
 4039                 break;
 4040         default:
 4041                 error = EINVAL;
 4042                 goto bailout;
 4043                 break; /* NOTREACHED */
 4044         }
 4045 
 4046         if (length != 0) {
 4047                 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
 4048         } else
 4049                 databuf = NULL;
 4050 
 4051         scsi_read_dvd_structure(&ccb->csio,
 4052                                 /* retries */ 1,
 4053                                 /* cbfcnp */ cddone,
 4054                                 /* tag_action */ MSG_SIMPLE_Q_TAG,
 4055                                 /* lba */ address,
 4056                                 /* layer_number */ dvdstruct->layer_num,
 4057                                 /* key_format */ dvdstruct->format,
 4058                                 /* agid */ dvdstruct->agid,
 4059                                 /* data_ptr */ databuf,
 4060                                 /* dxfer_len */ length,
 4061                                 /* sense_len */ SSD_FULL_SIZE,
 4062                                 /* timeout */ 50000);
 4063 
 4064         error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
 4065                          /*sense_flags*/SF_RETRY_UA);
 4066 
 4067         if (error != 0)
 4068                 goto bailout;
 4069 
 4070         switch(dvdstruct->format) {
 4071         case DVD_STRUCT_PHYSICAL: {
 4072                 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
 4073                 struct dvd_layer *outlayer;
 4074                 struct scsi_read_dvd_struct_data_physical *phys_data;
 4075 
 4076                 phys_data =
 4077                         (struct scsi_read_dvd_struct_data_physical *)databuf;
 4078                 inlayer = &phys_data->layer_desc;
 4079                 outlayer = (struct dvd_layer *)&dvdstruct->data;
 4080 
 4081                 dvdstruct->length = sizeof(*inlayer);
 4082 
 4083                 outlayer->book_type = (inlayer->book_type_version &
 4084                         RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
 4085                 outlayer->book_version = (inlayer->book_type_version &
 4086                         RDSD_BOOK_VERSION_MASK);
 4087                 outlayer->disc_size = (inlayer->disc_size_max_rate &
 4088                         RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
 4089                 outlayer->max_rate = (inlayer->disc_size_max_rate &
 4090                         RDSD_MAX_RATE_MASK);
 4091                 outlayer->nlayers = (inlayer->layer_info &
 4092                         RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
 4093                 outlayer->track_path = (inlayer->layer_info &
 4094                         RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
 4095                 outlayer->layer_type = (inlayer->layer_info &
 4096                         RDSD_LAYER_TYPE_MASK);
 4097                 outlayer->linear_density = (inlayer->density &
 4098                         RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
 4099                 outlayer->track_density = (inlayer->density &
 4100                         RDSD_TRACK_DENSITY_MASK);
 4101                 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
 4102                         RDSD_BCA_SHIFT;
 4103                 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
 4104                 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
 4105                 outlayer->end_sector_l0 =
 4106                         scsi_3btoul(inlayer->end_sector_layer0);
 4107                 break;
 4108         }
 4109         case DVD_STRUCT_COPYRIGHT: {
 4110                 struct scsi_read_dvd_struct_data_copyright *copy_data;
 4111 
 4112                 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
 4113                         databuf;
 4114 
 4115                 dvdstruct->cpst = copy_data->cps_type;
 4116                 dvdstruct->rmi = copy_data->region_info;
 4117                 dvdstruct->length = 0;
 4118 
 4119                 break;
 4120         }
 4121         default:
 4122                 /*
 4123                  * Tell the user what the overall length is, no matter
 4124                  * what we can actually fit in the data buffer.
 4125                  */
 4126                 dvdstruct->length = length - ccb->csio.resid - 
 4127                         sizeof(struct scsi_read_dvd_struct_data_header);
 4128 
 4129                 /*
 4130                  * But only actually copy out the smaller of what we read
 4131                  * in or what the structure can take.
 4132                  */
 4133                 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
 4134                       dvdstruct->data,
 4135                       min(sizeof(dvdstruct->data), dvdstruct->length));
 4136                 break;
 4137         }
 4138 bailout:
 4139 
 4140         if (databuf != NULL)
 4141                 free(databuf, M_DEVBUF);
 4142 
 4143         xpt_release_ccb(ccb);
 4144 
 4145         return(error);
 4146 }
 4147 
 4148 void
 4149 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
 4150                 void (*cbfcnp)(struct cam_periph *, union ccb *),
 4151                 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
 4152                 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
 4153                 u_int8_t sense_len, u_int32_t timeout)
 4154 {
 4155         struct scsi_report_key *scsi_cmd;
 4156 
 4157         scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
 4158         bzero(scsi_cmd, sizeof(*scsi_cmd));
 4159         scsi_cmd->opcode = REPORT_KEY;
 4160         scsi_ulto4b(lba, scsi_cmd->lba);
 4161         scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
 4162         scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
 4163                 (key_format & RK_KF_KEYFORMAT_MASK);
 4164 
 4165         cam_fill_csio(csio,
 4166                       retries,
 4167                       cbfcnp,
 4168                       /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
 4169                       tag_action,
 4170                       /*data_ptr*/ data_ptr,
 4171                       /*dxfer_len*/ dxfer_len,
 4172                       sense_len,
 4173                       sizeof(*scsi_cmd),
 4174                       timeout);
 4175 }
 4176 
 4177 void
 4178 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
 4179               void (*cbfcnp)(struct cam_periph *, union ccb *),
 4180               u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
 4181               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 4182               u_int32_t timeout)
 4183 {
 4184         struct scsi_send_key *scsi_cmd;
 4185 
 4186         scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
 4187         bzero(scsi_cmd, sizeof(*scsi_cmd));
 4188         scsi_cmd->opcode = SEND_KEY;
 4189 
 4190         scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
 4191         scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
 4192                 (key_format & RK_KF_KEYFORMAT_MASK);
 4193 
 4194         cam_fill_csio(csio,
 4195                       retries,
 4196                       cbfcnp,
 4197                       /*flags*/ CAM_DIR_OUT,
 4198                       tag_action,
 4199                       /*data_ptr*/ data_ptr,
 4200                       /*dxfer_len*/ dxfer_len,
 4201                       sense_len,
 4202                       sizeof(*scsi_cmd),
 4203                       timeout);
 4204 }
 4205 
 4206 
 4207 void
 4208 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
 4209                         void (*cbfcnp)(struct cam_periph *, union ccb *),
 4210                         u_int8_t tag_action, u_int32_t address,
 4211                         u_int8_t layer_number, u_int8_t format, u_int8_t agid,
 4212                         u_int8_t *data_ptr, u_int32_t dxfer_len,
 4213                         u_int8_t sense_len, u_int32_t timeout)
 4214 {
 4215         struct scsi_read_dvd_structure *scsi_cmd;
 4216 
 4217         scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
 4218         bzero(scsi_cmd, sizeof(*scsi_cmd));
 4219         scsi_cmd->opcode = READ_DVD_STRUCTURE;
 4220 
 4221         scsi_ulto4b(address, scsi_cmd->address);
 4222         scsi_cmd->layer_number = layer_number;
 4223         scsi_cmd->format = format;
 4224         scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
 4225         /* The AGID is the top two bits of this byte */
 4226         scsi_cmd->agid = agid << 6;
 4227 
 4228         cam_fill_csio(csio,
 4229                       retries,
 4230                       cbfcnp,
 4231                       /*flags*/ CAM_DIR_IN,
 4232                       tag_action,
 4233                       /*data_ptr*/ data_ptr,
 4234                       /*dxfer_len*/ dxfer_len,
 4235                       sense_len,
 4236                       sizeof(*scsi_cmd),
 4237                       timeout);
 4238 }

Cache object: 6f6dd3c7d093173523da16f66845c815


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