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