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