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