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