2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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 $
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.
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.
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.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
52 #include <sys/param.h>
53 #include <sys/bootmaj.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.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>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_NO_CHANGER = 0x04,
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
108 CD_CCB_BUFFER_IO = 0x02,
109 CD_CCB_WAITING = 0x03,
110 CD_CCB_TYPE_MASK = 0x0F,
111 CD_CCB_RETRY_UA = 0x10
115 CHANGER_TIMEOUT_SCHED = 0x01,
116 CHANGER_SHORT_TMOUT_SCHED = 0x02,
117 CHANGER_MANUAL_CALL = 0x04,
118 CHANGER_NEED_TIMEOUT = 0x08
121 #define ccb_state ppriv_field0
122 #define ccb_bp ppriv_ptr1
132 volatile cd_flags flags;
133 struct buf_queue_head buf_queue;
134 LIST_HEAD(, ccb_hdr) pending_ccbs;
135 struct cd_params params;
139 struct devstat device_stats;
140 STAILQ_ENTRY(cd_softc) changer_links;
141 struct cdchanger *changer;
143 struct cam_periph *periph;
146 struct cd_quirk_entry {
147 struct scsi_inquiry_pattern inq_pat;
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.
159 static struct cd_quirk_entry cd_quirk_table[] =
162 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
163 /*quirks*/ CD_Q_CHANGER
166 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
167 "*"}, /* quirks */ CD_Q_CHANGER
170 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
171 /* quirks */ CD_Q_CHANGER
174 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
175 /* quirks */ CD_Q_BCD_TRACKS
180 #define MIN(x,y) ((x<y) ? x : y)
183 static d_open_t cdopen;
184 static d_close_t cdclose;
185 static d_ioctl_t cdioctl;
186 static d_strategy_t cdstrategy;
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,
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,
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,
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,
222 static int cdreadsubchannel(struct cam_periph *periph,
223 u_int32_t mode, u_int32_t format,
225 struct cd_sub_channel_info *data,
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,
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);
246 static struct periph_driver cddriver =
249 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
252 DATA_SET(periphdriver_set, cddriver);
254 /* For 2.2-stable support */
258 static struct cdevsw cd_cdevsw = {
260 /* maj */ SCSICD_CDEV_MAJOR,
268 /* write */ physwrite,
272 /* strategy */ cdstrategy,
277 static struct extend_array *cdperiphs;
278 static int num_changers;
280 #ifndef CHANGER_MIN_BUSY_SECONDS
281 #define CHANGER_MIN_BUSY_SECONDS 5
283 #ifndef CHANGER_MAX_BUSY_SECONDS
284 #define CHANGER_MAX_BUSY_SECONDS 15
287 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
288 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
291 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
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");
304 target_id_t target_id;
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;
316 static STAILQ_HEAD(changerlist, cdchanger) changerq;
322 struct cam_path *path;
325 * Create our extend array for storing the devices we attach to.
327 cdperiphs = cam_extend_new();
328 if (cdperiphs == NULL) {
329 printf("cd: Failed to alloc extend array!\n");
334 * Install a global async callback. This callback will
335 * receive async callbacks like "new device found".
337 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
338 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
340 if (status == CAM_REQ_CMP) {
341 struct ccb_setasync csa;
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;
353 if (status != CAM_REQ_CMP) {
354 printf("cd: Failed to attach master async callback "
355 "due to status 0x%x!\n", status);
360 cdoninvalidate(struct cam_periph *periph)
363 struct cd_softc *softc;
365 struct ccb_setasync csa;
367 softc = (struct cd_softc *)periph->softc;
370 * De-register any async callbacks.
372 xpt_setup_ccb(&csa.ccb_h, periph->path,
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);
380 softc->flags |= CD_FLAG_INVALID;
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.
390 * Return all queued I/O with ENXIO.
391 * XXX Handle any transactions queued to the card
392 * with XPT_ABORT_CCB.
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;
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.
408 if ((softc->flags & CD_FLAG_CHANGER)
409 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
410 camq_remove(&softc->changer->devq, softc->pinfo.index);
412 xpt_print_path(periph->path);
413 printf("lost device\n");
417 cdcleanup(struct cam_periph *periph)
419 struct cd_softc *softc;
422 softc = (struct cd_softc *)periph->softc;
424 xpt_print_path(periph->path);
425 printf("removing device entry\n");
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)
434 if ((softc->flags & CD_FLAG_CHANGER)
435 && (softc->flags & CD_FLAG_ACTIVE)) {
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.
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.
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;
461 softc->changer->devq.qfrozen_cnt--;
462 softc->changer->flags |= CHANGER_MANUAL_CALL;
463 cdrunchangerqueue(softc->changer);
467 * If we're removing the last device on the changer, go ahead and
468 * remove the changer device structure.
470 if ((softc->flags & CD_FLAG_CHANGER)
471 && (--softc->changer->num_devices == 0)) {
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.
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;
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;
490 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
492 xpt_print_path(periph->path);
493 printf("removing changer entry\n");
494 free(softc->changer, M_DEVBUF);
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);
502 free(softc, M_DEVBUF);
507 cdasync(void *callback_arg, u_int32_t code,
508 struct cam_path *path, void *arg)
510 struct cam_periph *periph;
512 periph = (struct cam_periph *)callback_arg;
514 case AC_FOUND_DEVICE:
516 struct ccb_getdev *cgd;
519 cgd = (struct ccb_getdev *)arg;
521 if (SID_TYPE(&cgd->inq_data) != T_CDROM
522 && SID_TYPE(&cgd->inq_data) != T_WORM)
526 * Allocate a peripheral instance for
527 * this device and start the probe
530 status = cam_periph_alloc(cdregister, cdoninvalidate,
532 "cd", CAM_PERIPH_BIO,
533 cgd->ccb_h.path, cdasync,
534 AC_FOUND_DEVICE, cgd);
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);
546 struct cd_softc *softc;
547 struct ccb_hdr *ccbh;
550 softc = (struct cd_softc *)periph->softc;
553 * Don't fail on the expected unit attention
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;
564 cam_periph_async(periph, code, path, arg);
570 cdregister(struct cam_periph *periph, void *arg)
572 struct cd_softc *softc;
573 struct ccb_setasync csa;
574 struct ccb_getdev *cgd;
577 cgd = (struct ccb_getdev *)arg;
578 if (periph == NULL) {
579 printf("cdregister: periph was NULL!!\n");
580 return(CAM_REQ_CMP_ERR);
583 printf("cdregister: no getdev CCB, can't register device\n");
584 return(CAM_REQ_CMP_ERR);
587 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
590 printf("cdregister: Unable to probe new device. "
591 "Unable to allocate softc\n");
592 return(CAM_REQ_CMP_ERR);
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;
604 periph->softc = softc;
605 softc->periph = periph;
607 cam_extend_set(cdperiphs, periph->unit_number, periph);
610 * See if this device has any quirks.
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);
618 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
620 softc->quirks = CD_Q_NONE;
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.
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);
643 * Add an async callback so that we get
644 * notified if this device goes away.
646 xpt_setup_ccb(&csa.ccb_h, periph->path,
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);
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.
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;
672 /* Set the changer flag in the current device's softc */
673 softc->flags |= CD_FLAG_CHANGER;
675 if (num_changers == 0)
676 STAILQ_INIT(&changerq);
679 * Now, look around for an existing changer device with the
680 * same path and target ID as the current device.
683 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
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)) {
694 * If we found a matching entry, just add this device to
695 * the list of devices on this changer.
698 struct chdevlist *chlunhead;
700 chlunhead = &nchanger->chluns;
703 * XXX KDM look at consolidating this code with the
704 * code below in a separate function.
708 * Create a path with lun id 0, and see if we can
709 * find a matching device
711 status = xpt_create_path(&path, /*periph*/ periph,
713 cgd->ccb_h.target_id, 0);
715 if ((status == CAM_REQ_CMP)
716 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
717 struct cd_softc *nsoftc;
719 nsoftc = (struct cd_softc *)nperiph->softc;
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: "
732 nsoftc->changer = nchanger;
733 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
735 STAILQ_INSERT_TAIL(&nchanger->chluns,
736 nsoftc,changer_links);
739 } else if (status == CAM_REQ_CMP)
742 printf("cdregister: unable to allocate path\n"
743 "cdregister: changer support may be "
747 nchanger->num_devices++;
749 softc->changer = nchanger;
750 softc->pinfo.index = CAM_UNQUEUED_INDEX;
752 if (camq_resize(&nchanger->devq,
753 nchanger->num_devices) != CAM_REQ_CMP) {
754 printf("cdregister: camq_resize "
755 "failed, changer support may "
759 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
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.)
771 nchanger = malloc(sizeof(struct cdchanger),
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");
781 * Yes, gotos can be gross but in this case
782 * I think it's justified..
787 /* zero the structure */
788 bzero(nchanger, sizeof(struct cdchanger));
790 if (camq_init(&nchanger->devq, 1) != 0) {
791 softc->flags &= ~CD_FLAG_CHANGER;
792 printf("cdregister: changer support "
799 nchanger->path_id = cgd->ccb_h.path_id;
800 nchanger->target_id = cgd->ccb_h.target_id;
802 /* this is superfluous, but it makes things clearer */
803 nchanger->num_devices = 0;
805 STAILQ_INIT(&nchanger->chluns);
807 STAILQ_INSERT_TAIL(&changerq, nchanger,
811 * Create a path with lun id 0, and see if we can
812 * find a matching device
814 status = xpt_create_path(&path, /*periph*/ periph,
816 cgd->ccb_h.target_id, 0);
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.
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;
830 nsoftc = (struct cd_softc *)nperiph->softc;
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 "
840 nsoftc->changer = nchanger;
841 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
843 STAILQ_INSERT_TAIL(&nchanger->chluns,
844 nsoftc, changer_links);
846 } else if (status == CAM_REQ_CMP)
849 printf("cdregister: unable to allocate path\n"
850 "cdregister: changer support may be "
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 "
863 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
870 /* Lock this peripheral until we are setup */
872 cam_periph_lock(periph, 0);
874 if ((softc->flags & CD_FLAG_CHANGER) == 0)
875 xpt_schedule(periph, /*priority*/5);
877 cdschedule(periph, /*priority*/ 5);
883 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
885 struct disklabel *label;
886 struct cam_periph *periph;
887 struct cd_softc *softc;
888 struct ccb_getdev cgd;
894 periph = cam_extend_get(cdperiphs, unit);
899 softc = (struct cd_softc *)periph->softc;
902 * Grab splsoftcam and hold it until we lock the peripheral.
905 if (softc->flags & CD_FLAG_INVALID) {
910 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
917 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
920 cdprevent(periph, PR_PREVENT);
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);
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.
935 if ((error = cdfirsttrackisdata(periph)) != 0) {
936 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
937 softc->disk.d_dsflags |= DSO_NOLABELS;
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.
946 label = &softc->disk.d_label;
947 bzero(label, sizeof(*label));
948 label->d_type = DTYPE_SCSI;
951 * Grab the inquiry data to get the vendor and product names.
952 * Put them in the typename and packname for the label.
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);
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)));
963 label->d_secsize = softc->params.blksize;
964 label->d_secperunit = softc->params.disksize;
965 label->d_flags = D_REMOVABLE;
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.
973 label->d_partitions[0].p_size = label->d_secperunit;
974 label->d_partitions[0].p_fstype = FS_OTHER;
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?
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;
987 cam_periph_unlock(periph);
989 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
995 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
997 struct cam_periph *periph;
998 struct cd_softc *softc;
1002 periph = cam_extend_get(cdperiphs, unit);
1006 softc = (struct cd_softc *)periph->softc;
1008 if ((error = cam_periph_lock(periph, 0)) != 0)
1011 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1012 cdprevent(periph, PR_ALLOW);
1015 * Unconditionally set the dsopen() flags back to their default
1018 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1019 softc->disk.d_dsflags |= DSO_COMPATLABEL;
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.
1025 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1027 cam_periph_unlock(periph);
1028 cam_periph_release(periph);
1034 cdshorttimeout(void *arg)
1036 struct cdchanger *changer;
1041 changer = (struct cdchanger *)arg;
1043 /* Always clear the short timeout flag, since that's what we're in */
1044 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
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.
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);
1060 * This is a wrapper for xpt_schedule. It only applies to changers.
1063 cdschedule(struct cam_periph *periph, int priority)
1065 struct cd_softc *softc;
1070 softc = (struct cd_softc *)periph->softc;
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.
1080 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1081 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1083 * We don't do anything with the priority here.
1084 * This is strictly a fifo queue.
1086 softc->pinfo.priority = 1;
1087 softc->pinfo.generation = ++softc->changer->devq.generation;
1088 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
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
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);
1104 } else if ((softc->flags & CD_FLAG_ACTIVE)
1105 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1106 xpt_schedule(periph, priority);
1113 cdrunchangerqueue(void *arg)
1115 struct cd_softc *softc;
1116 struct cdchanger *changer;
1117 int called_from_timeout;
1122 changer = (struct cdchanger *)arg;
1125 * If we have NOT been called from cdstrategy() or cddone(), and
1126 * instead from a timeout routine, go ahead and clear the
1129 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1130 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1131 called_from_timeout = 1;
1133 called_from_timeout = 0;
1135 /* Always clear the manual call flag */
1136 changer->flags &= ~CHANGER_MANUAL_CALL;
1138 /* nothing to do if the queue is empty */
1139 if (changer->devq.entries <= 0) {
1145 * If the changer queue is frozen, that means we have an active
1148 if (changer->devq.qfrozen_cnt > 0) {
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;
1165 * We always need to reset the frozen count and clear the
1168 changer->devq.qfrozen_cnt--;
1169 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1170 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
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.
1177 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1179 changer->cur_device->pinfo.generation =
1180 ++changer->devq.generation;
1181 camq_insert(&changer->devq,
1182 (cam_pinfo *)changer->cur_device);
1186 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1188 changer->cur_device = softc;
1190 changer->devq.qfrozen_cnt++;
1191 softc->flags |= CD_FLAG_ACTIVE;
1193 /* Just in case this device is waiting */
1194 wakeup(&softc->changer);
1195 xpt_schedule(softc->periph, /*priority*/ 1);
1198 * Get rid of any pending timeouts, and set a flag to schedule new
1199 * ones so this device gets its full time quantum.
1201 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1202 untimeout(cdrunchangerqueue, changer, changer->long_handle);
1203 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1206 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1207 untimeout(cdshorttimeout, changer, changer->short_handle);
1208 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1212 * We need to schedule timeouts, but we only do this after the
1213 * first transaction has completed. This eliminates the changer
1216 changer->flags |= CHANGER_NEED_TIMEOUT;
1222 cdchangerschedule(struct cd_softc *softc)
1224 struct cdchanger *changer;
1229 changer = softc->changer;
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.
1236 if ((softc->flags & CD_FLAG_ACTIVE)
1237 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1238 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
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.
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);
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);
1264 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1265 && (softc->flags & CD_FLAG_ACTIVE)) {
1268 * Now that the first transaction to this
1269 * particular device has completed, we can go ahead
1270 * and schedule our timeouts.
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;
1278 printf("cdchangerschedule: already have a long"
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;
1287 printf("cdchangerschedule: already have a short "
1291 * We just scheduled timeouts, no need to schedule
1294 changer->flags &= ~CHANGER_NEED_TIMEOUT;
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)
1306 struct cd_softc *softc;
1307 struct cam_periph *periph;
1310 periph = xpt_path_periph(ccb->ccb_h.path);
1311 softc = (struct cd_softc *)periph->softc;
1313 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1314 &softc->device_stats);
1316 if (softc->flags & CD_FLAG_CHANGER)
1317 cdchangerschedule(softc);
1323 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1325 struct cd_softc *softc;
1328 softc = (struct cd_softc *)periph->softc;
1330 if (softc->flags & CD_FLAG_CHANGER) {
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.
1338 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1340 * If this changer isn't already queued, queue it up.
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);
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);
1356 tsleep(&softc->changer, 0, "cgticb", 0);
1360 return(cam_periph_getccb(periph, priority));
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.
1370 cdstrategy(struct buf *bp)
1372 struct cam_periph *periph;
1373 struct cd_softc *softc;
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;
1385 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1387 softc = (struct cd_softc *)periph->softc;
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.
1397 * If the device has been made invalid, error out
1399 if ((softc->flags & CD_FLAG_INVALID)) {
1401 bp->b_error = ENXIO;
1406 * Place it in the queue of disk activities for this disk
1408 bufqdisksort(&softc->buf_queue, bp);
1413 * Schedule ourselves for performing the work. We do things
1414 * differently for changers.
1416 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1417 xpt_schedule(periph, /* XXX priority */1);
1419 cdschedule(periph, /* priority */ 1);
1423 bp->b_flags |= B_ERROR;
1425 * Correctly set the buf to indicate a completed xfer
1427 bp->b_resid = bp->b_bcount;
1433 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1435 struct cd_softc *softc;
1437 struct ccb_scsiio *csio;
1438 struct scsi_read_capacity_data *rcap;
1441 softc = (struct cd_softc *)periph->softc;
1443 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1445 switch (softc->state) {
1446 case CD_STATE_NORMAL:
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;
1455 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1457 periph->immediate_priority = CAM_PRIORITY_NONE;
1459 wakeup(&periph->ccb_list);
1460 } else if (bp == NULL) {
1462 xpt_release_ccb(start_ccb);
1464 bufq_remove(&softc->buf_queue, bp);
1466 devstat_start_transaction(&softc->device_stats);
1468 scsi_read_write(&start_ccb->csio,
1470 /* cbfcnp */ cddone,
1471 (bp->b_flags & B_ORDERED) != 0 ?
1474 /* read */bp->b_flags & B_READ,
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;
1487 * Block out any asyncronous callbacks
1488 * while we touch the pending ccb list.
1491 LIST_INSERT_HEAD(&softc->pending_ccbs,
1492 &start_ccb->ccb_h, periph_links.le);
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;
1501 start_ccb->ccb_h.ccb_bp = bp;
1502 bp = bufq_first(&softc->buf_queue);
1505 xpt_action(start_ccb);
1508 /* Have more work to do, so ensure we stay scheduled */
1509 xpt_schedule(periph, /* XXX priority */1);
1513 case CD_STATE_PROBE:
1516 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1520 xpt_print_path(periph->path);
1521 printf("cdstart: Couldn't malloc read_capacity data\n");
1522 /* cd_free_periph??? */
1525 csio = &start_ccb->csio;
1526 scsi_read_capacity(csio,
1533 start_ccb->ccb_h.ccb_bp = NULL;
1534 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1535 xpt_action(start_ccb);
1542 cddone(struct cam_periph *periph, union ccb *done_ccb)
1544 struct cd_softc *softc;
1545 struct ccb_scsiio *csio;
1547 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1549 softc = (struct cd_softc *)periph->softc;
1550 csio = &done_ccb->csio;
1552 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1553 case CD_CCB_BUFFER_IO:
1559 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1562 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1565 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1570 /* Retry selection timeouts */
1571 sf |= SF_RETRY_SELTO;
1573 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1575 * A retry was scheuled, so
1586 xpt_print_path(periph->path);
1587 printf("cddone: got error %#x back\n", error);
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;
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,
1604 /*getcount_only*/0);
1607 bp->b_resid = csio->resid;
1609 if (bp->b_resid != 0) {
1610 /* Short transfer ??? */
1611 bp->b_flags |= B_ERROR;
1616 * Block out any asyncronous callbacks
1617 * while we touch the pending ccb list.
1620 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1623 if (softc->flags & CD_FLAG_CHANGER)
1624 cdchangerschedule(softc);
1626 devstat_end_transaction_buf(&softc->device_stats, bp);
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
1651 struct cd_params *cdp;
1653 cdp = &softc->params;
1655 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1657 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1658 cdp->blksize = scsi_4btoul (rdcap->length);
1660 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1662 snprintf(announce_buf, sizeof(announce_buf),
1663 "cd present [%lu x %lu byte records]",
1664 cdp->disksize, (u_long)cdp->blksize);
1669 * Retry any UNIT ATTENTION type errors. They
1670 * are expected at boot.
1672 error = cderror(done_ccb, 0, SF_RETRY_UA |
1673 SF_NO_PRINT | SF_RETRY_SELTO);
1674 if (error == ERESTART) {
1676 * A retry was scheuled, so
1680 } else if (error != 0) {
1682 struct scsi_sense_data *sense;
1684 int sense_key, error_code;
1687 struct ccb_getdev cgd;
1689 /* Don't wedge this device's queue */
1690 cam_release_devq(done_ccb->ccb_h.path,
1694 /*getcount_only*/0);
1696 status = done_ccb->ccb_h.status;
1698 xpt_setup_ccb(&cgd.ccb_h,
1699 done_ccb->ccb_h.path,
1701 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1702 xpt_action((union ccb *)&cgd);
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))
1712 sense = &csio->sense_data;
1713 scsi_extract_sense(sense, &error_code,
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.
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,
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) {
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
1755 * Just print out the error, not
1756 * the full probe message, when we
1763 xpt_print_path(periph->path);
1764 printf("got CAM status %#x\n",
1765 done_ccb->ccb_h.status);
1767 xpt_print_path(periph->path);
1768 printf("fatal error, failed"
1769 " to attach to device\n");
1772 * Invalidate this peripheral.
1774 cam_periph_invalidate(periph);
1776 announce_buf[0] = '\0';
1780 * Invalidate this peripheral.
1782 cam_periph_invalidate(periph);
1783 announce_buf[0] = '\0';
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);
1793 softc->state = CD_STATE_NORMAL;
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
1802 xpt_release_ccb(done_ccb);
1803 cam_periph_unlock(periph);
1806 case CD_CCB_WAITING:
1808 /* Caller will release the CCB */
1809 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1810 ("trying to wakeup ccbwait\n"));
1812 wakeup(&done_ccb->ccb_h.cbfcnp);
1818 xpt_release_ccb(done_ccb);
1822 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1825 struct cam_periph *periph;
1826 struct cd_softc *softc;
1831 periph = cam_extend_get(cdperiphs, unit);
1835 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1837 softc = (struct cd_softc *)periph->softc;
1839 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1840 ("trying to do ioctl %#lx\n", cmd));
1842 error = cam_periph_lock(periph, PCATCH);
1849 case CDIOCPLAYTRACKS:
1851 struct ioc_play_track *args
1852 = (struct ioc_play_track *) addr;
1853 struct cd_mode_data *data;
1855 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1858 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1859 ("trying to do CDIOCPLAYTRACKS\n"));
1861 error = cdgetmode(periph, data, AUDIO_PAGE);
1866 data->page.audio.flags &= ~CD_PA_SOTC;
1867 data->page.audio.flags |= CD_PA_IMMED;
1868 error = cdsetmode(periph, data);
1872 if (softc->quirks & CD_Q_BCD_TRACKS) {
1873 args->start_track = bin2bcd(args->start_track);
1874 args->end_track = bin2bcd(args->end_track);
1876 error = cdplaytracks(periph,
1885 struct ioc_play_msf *args
1886 = (struct ioc_play_msf *) addr;
1887 struct cd_mode_data *data;
1889 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1892 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1893 ("trying to do CDIOCPLAYMSF\n"));
1895 error = cdgetmode(periph, data, AUDIO_PAGE);
1900 data->page.audio.flags &= ~CD_PA_SOTC;
1901 data->page.audio.flags |= CD_PA_IMMED;
1902 error = cdsetmode(periph, data);
1906 error = cdplaymsf(periph,
1915 case CDIOCPLAYBLOCKS:
1917 struct ioc_play_blocks *args
1918 = (struct ioc_play_blocks *) addr;
1919 struct cd_mode_data *data;
1921 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1922 ("trying to do CDIOCPLAYBLOCKS\n"));
1924 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1927 error = cdgetmode(periph, data, AUDIO_PAGE);
1932 data->page.audio.flags &= ~CD_PA_SOTC;
1933 data->page.audio.flags |= CD_PA_IMMED;
1934 error = cdsetmode(periph, data);
1938 error = cdplay(periph, args->blk, args->len);
1941 case CDIOCREADSUBCHANNEL:
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;
1948 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1949 ("trying to do CDIOCREADSUBCHANNEL\n"));
1951 data = malloc(sizeof(struct cd_sub_channel_info),
1954 if ((len > sizeof(struct cd_sub_channel_info)) ||
1955 (len < sizeof(struct cd_sub_channel_header))) {
1957 "scsi_cd: cdioctl: "
1958 "cdioreadsubchannel: error, len=%d\n",
1965 if (softc->quirks & CD_Q_BCD_TRACKS)
1966 args->track = bin2bcd(args->track);
1968 error = cdreadsubchannel(periph, args->address_format,
1969 args->data_format, args->track, data, len);
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) {
1988 case CDIOREADTOCHEADER:
1990 struct ioc_toc_header *th;
1992 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1993 ("trying to do CDIOREADTOCHEADER\n"));
1995 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
1997 error = cdreadtoc(periph, 0, 0,
1998 (struct cd_toc_entry *)th,
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
2008 th->starting_track =
2009 bcd2bin(th->starting_track);
2010 th->ending_track = bcd2bin(th->ending_track);
2013 bcopy(th, addr, sizeof(*th));
2017 case CDIOREADTOCENTRYS:
2020 struct ioc_toc_header header;
2021 struct cd_toc_entry entries[100];
2024 struct ioc_toc_header header;
2025 struct cd_toc_entry entry;
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;
2036 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2037 ("trying to do CDIOREADTOCENTRYS\n"));
2039 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2040 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK);
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)) {
2047 printf("scsi_cd: error in readtocentries, "
2048 "returning EINVAL\n");
2055 error = cdreadtoc(periph, 0, 0,
2056 (struct cd_toc_entry *)th,
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
2068 th->starting_track =
2069 bcd2bin(th->starting_track);
2070 th->ending_track = bcd2bin(th->ending_track);
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");
2087 /* calculate reading length without leadout entry */
2088 readlen = (th->ending_track - starting_track + 1) *
2089 sizeof(struct cd_toc_entry);
2091 /* and with leadout entry */
2092 len = readlen + sizeof(struct cd_toc_entry);
2093 if (te->data_len < len) {
2098 if (len > sizeof(data->entries)) {
2099 printf("scsi_cd: error in readtocentries, "
2100 "returning EINVAL\n");
2106 num = len / sizeof(struct cd_toc_entry);
2109 error = cdreadtoc(periph, te->address_format,
2111 (struct cd_toc_entry *)data,
2112 readlen + sizeof (*th));
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,
2127 (struct cd_toc_entry *)lead,
2134 data->entries[idx - starting_track] =
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);
2144 error = copyout(data->entries, te->data, len);
2149 case CDIOREADTOCENTRY:
2151 /* yeah yeah, this is ugly */
2153 struct ioc_toc_header header;
2154 struct cd_toc_entry entry;
2158 struct ioc_read_toc_single_entry *te =
2159 (struct ioc_read_toc_single_entry *) addr;
2160 struct ioc_toc_header *th;
2163 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2164 ("trying to do CDIOREADTOCENTRY\n"));
2166 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2168 if (te->address_format != CD_MSF_FORMAT
2169 && te->address_format != CD_LBA_FORMAT) {
2170 printf("error in readtocentry, "
2171 " returning EINVAL\n");
2178 error = cdreadtoc(periph, 0, 0,
2179 (struct cd_toc_entry *)th,
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
2190 th->starting_track =
2191 bcd2bin(th->starting_track);
2192 th->ending_track = bcd2bin(th->ending_track);
2196 track = th->starting_track;
2197 else if (track == LEADOUT)
2199 else if (track < th->starting_track ||
2200 track > th->ending_track + 1) {
2201 printf("error in readtocentry, "
2202 " returning EINVAL\n");
2208 error = cdreadtoc(periph, te->address_format, track,
2209 (struct cd_toc_entry *)data,
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));
2225 struct ioc_patch *arg = (struct ioc_patch *) addr;
2226 struct cd_mode_data *data;
2228 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2229 ("trying to do CDIOCSETPATCH\n"));
2231 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2233 error = cdgetmode(periph, data, AUDIO_PAGE);
2238 data->page.audio.port[LEFT_PORT].channels =
2240 data->page.audio.port[RIGHT_PORT].channels =
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);
2250 struct ioc_vol *arg = (struct ioc_vol *) addr;
2251 struct cd_mode_data *data;
2253 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2254 ("trying to do CDIOCGETVOL\n"));
2256 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2258 error = cdgetmode(periph, data, AUDIO_PAGE);
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;
2274 struct ioc_vol *arg = (struct ioc_vol *) addr;
2275 struct cd_mode_data *data;
2277 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2278 ("trying to do CDIOCSETVOL\n"));
2280 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2282 error = cdgetmode(periph, data, AUDIO_PAGE);
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);
2301 struct cd_mode_data *data;
2303 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2304 ("trying to do CDIOCSETMONO\n"));
2306 data = malloc(sizeof(struct cd_mode_data),
2308 error = cdgetmode(periph, data, AUDIO_PAGE);
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);
2323 case CDIOCSETSTEREO:
2325 struct cd_mode_data *data;
2327 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2328 ("trying to do CDIOCSETSTEREO\n"));
2330 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2332 error = cdgetmode(periph, data, AUDIO_PAGE);
2337 data->page.audio.port[LEFT_PORT].channels =
2339 data->page.audio.port[RIGHT_PORT].channels =
2341 data->page.audio.port[2].channels = 0;
2342 data->page.audio.port[3].channels = 0;
2343 error = cdsetmode(periph, data);
2349 struct cd_mode_data *data;
2351 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2352 ("trying to do CDIOCSETMUTE\n"));
2354 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2356 error = cdgetmode(periph, data, AUDIO_PAGE);
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);
2371 struct cd_mode_data *data;
2373 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2374 ("trying to do CDIOCSETLEFT\n"));
2376 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2378 error = cdgetmode(periph, data, AUDIO_PAGE);
2383 data->page.audio.port[LEFT_PORT].channels =
2385 data->page.audio.port[RIGHT_PORT].channels =
2387 data->page.audio.port[2].channels = 0;
2388 data->page.audio.port[3].channels = 0;
2389 error = cdsetmode(periph, data);
2395 struct cd_mode_data *data;
2397 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2398 ("trying to do CDIOCSETRIGHT\n"));
2400 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2402 error = cdgetmode(periph, data, AUDIO_PAGE);
2407 data->page.audio.port[LEFT_PORT].channels =
2409 data->page.audio.port[RIGHT_PORT].channels =
2411 data->page.audio.port[2].channels = 0;
2412 data->page.audio.port[3].channels = 0;
2413 error = cdsetmode(periph, data);
2418 error = cdpause(periph, 1);
2421 error = cdpause(periph, 0);
2424 error = cdstartunit(periph);
2427 error = cdstopunit(periph, 0);
2430 error = cdstopunit(periph, 1);
2433 cdprevent(periph, PR_ALLOW);
2436 cdprevent(periph, PR_PREVENT);
2439 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2443 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2447 /* return (cd_reset(periph)); */
2450 case CDRIOCREADSPEED:
2451 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2453 case CDRIOCWRITESPEED:
2454 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2457 case DVDIOCREPORTKEY: {
2458 struct dvd_authinfo *authinfo;
2460 authinfo = (struct dvd_authinfo *)addr;
2462 if (cmd == DVDIOCREPORTKEY)
2463 error = cdreportkey(periph, authinfo);
2465 error = cdsendkey(periph, authinfo);
2468 case DVDIOCREADSTRUCTURE: {
2469 struct dvd_struct *dvdstruct;
2471 dvdstruct = (struct dvd_struct *)addr;
2473 error = cdreaddvdstructure(periph, dvdstruct);
2478 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2482 cam_periph_unlock(periph);
2484 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2490 cdprevent(struct cam_periph *periph, int action)
2493 struct cd_softc *softc;
2496 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2498 softc = (struct cd_softc *)periph->softc;
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)) {
2507 ccb = cdgetccb(periph, /* priority */ 1);
2509 scsi_prevent(&ccb->csio,
2515 /* timeout */60000);
2517 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2518 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2520 xpt_release_ccb(ccb);
2523 if (action == PR_ALLOW)
2524 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2526 softc->flags |= CD_FLAG_DISC_LOCKED;
2531 cdsize(dev_t dev, u_int32_t *size)
2533 struct cam_periph *periph;
2534 struct cd_softc *softc;
2536 struct scsi_read_capacity_data *rcap_buf;
2539 periph = cam_extend_get(cdperiphs, dkunit(dev));
2544 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2546 softc = (struct cd_softc *)periph->softc;
2548 ccb = cdgetccb(periph, /* priority */ 1);
2550 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2553 scsi_read_capacity(&ccb->csio,
2559 /* timeout */20000);
2561 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2562 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2564 xpt_release_ccb(ccb);
2566 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2567 softc->params.blksize = scsi_4btoul(rcap_buf->length);
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.
2573 * XXX we leave drives alone that might report 512 bytes, as
2574 * well as drives reporting more weird sizes like perhaps 4K.
2576 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2577 softc->params.blksize = 2048;
2579 free(rcap_buf, M_TEMP);
2580 *size = softc->params.disksize;
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
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.)
2597 cdfirsttrackisdata(struct cam_periph *periph)
2600 struct ioc_toc_header header;
2601 struct cd_toc_entry entries[100];
2603 struct cd_softc *softc;
2604 struct ioc_toc_header *th;
2605 struct cdtocdata *data;
2607 int error, first_track_audio;
2610 first_track_audio = -1;
2612 softc = (struct cd_softc *)periph->softc;
2614 data = malloc(sizeof(struct cdtocdata), M_TEMP, M_WAITOK);
2617 error = cdreadtoc(periph, 0, 0, (struct cd_toc_entry *)data,
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
2627 th->starting_track =
2628 bcd2bin(th->starting_track);
2629 th->ending_track = bcd2bin(th->ending_track);
2631 th->len = scsi_2btoul((u_int8_t *)&th->len);
2633 if ((th->len - 2) > 0)
2634 num_entries = (th->len - 2) / sizeof(struct cd_toc_entry);
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;
2643 first_track_audio = 1;
2648 if (first_track_audio == -1)
2650 else if (first_track_audio == 1)
2661 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2663 struct cd_softc *softc;
2664 struct cam_periph *periph;
2666 periph = xpt_path_periph(ccb->ccb_h.path);
2667 softc = (struct cd_softc *)periph->softc;
2671 * Until we have a better way of doing pack validation,
2672 * don't treat UAs as errors.
2674 sense_flags |= SF_RETRY_UA;
2675 return (cam_periph_error(ccb, cam_flags, sense_flags,
2676 &softc->saved_ccb));
2680 * Read table of contents
2683 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2684 struct cd_toc_entry *data, u_int32_t len)
2686 struct scsi_read_toc *scsi_cmd;
2688 struct ccb_scsiio *csio;
2695 ccb = cdgetccb(periph, /* priority */ 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);
2710 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2711 bzero (scsi_cmd, sizeof(*scsi_cmd));
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;
2720 scsi_cmd->op_code = READ_TOC;
2722 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2723 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2725 xpt_release_ccb(ccb);
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)
2735 struct scsi_read_subchannel *scsi_cmd;
2736 struct ccb_scsiio *csio;
2742 ccb = cdgetccb(periph, /* priority */ 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);
2757 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2758 bzero (scsi_cmd, sizeof(*scsi_cmd));
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;
2769 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2770 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2772 xpt_release_ccb(ccb);
2779 cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page)
2781 struct scsi_mode_sense_6 *scsi_cmd;
2782 struct ccb_scsiio *csio;
2786 ccb = cdgetccb(periph, /* priority */ 1);
2790 bzero(data, sizeof(*data));
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);
2802 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
2803 bzero (scsi_cmd, sizeof(*scsi_cmd));
2805 scsi_cmd->page = page;
2806 scsi_cmd->length = sizeof(*data) & 0xff;
2807 scsi_cmd->opcode = MODE_SENSE;
2809 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2810 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2812 xpt_release_ccb(ccb);
2818 cdsetmode(struct cam_periph *periph, struct cd_mode_data *data)
2820 struct scsi_mode_select_6 *scsi_cmd;
2821 struct ccb_scsiio *csio;
2825 ccb = cdgetccb(periph, /* priority */ 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);
2842 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
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;
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.
2854 data->header.medium_type = 0;
2856 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2857 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2859 xpt_release_ccb(ccb);
2866 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2868 struct ccb_scsiio *csio;
2874 ccb = cdgetccb(periph, /* priority */ 1);
2877 * Use the smallest possible command to perform the operation.
2879 if ((len & 0xffff0000) == 0) {
2881 * We can fit in a 10 byte cdb.
2883 struct scsi_play_10 *scsi_cmd;
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);
2892 struct scsi_play_12 *scsi_cmd;
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);
2904 /*flags*/CAM_DIR_NONE,
2908 /*sense_len*/SSD_FULL_SIZE,
2910 /*timeout*/50 * 1000);
2912 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2913 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2915 xpt_release_ccb(ccb);
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)
2924 struct scsi_play_msf *scsi_cmd;
2925 struct ccb_scsiio *csio;
2931 ccb = cdgetccb(periph, /* priority */ 1);
2937 /* cbfcnp */ cddone,
2938 /* flags */ CAM_DIR_NONE,
2939 /* tag_action */ MSG_SIMPLE_Q_TAG,
2940 /* data_ptr */ NULL,
2942 /* sense_len */ SSD_FULL_SIZE,
2943 sizeof(struct scsi_play_msf),
2944 /* timeout */ 50000);
2946 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2947 bzero (scsi_cmd, sizeof(*scsi_cmd));
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;
2957 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2958 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2960 xpt_release_ccb(ccb);
2967 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
2968 u_int32_t etrack, u_int32_t eindex)
2970 struct scsi_play_track *scsi_cmd;
2971 struct ccb_scsiio *csio;
2977 ccb = cdgetccb(periph, /* priority */ 1);
2983 /* cbfcnp */ cddone,
2984 /* flags */ CAM_DIR_NONE,
2985 /* tag_action */ MSG_SIMPLE_Q_TAG,
2986 /* data_ptr */ NULL,
2988 /* sense_len */ SSD_FULL_SIZE,
2989 sizeof(struct scsi_play_track),
2990 /* timeout */ 50000);
2992 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
2993 bzero (scsi_cmd, sizeof(*scsi_cmd));
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;
3001 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3002 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3004 xpt_release_ccb(ccb);
3010 cdpause(struct cam_periph *periph, u_int32_t go)
3012 struct scsi_pause *scsi_cmd;
3013 struct ccb_scsiio *csio;
3019 ccb = cdgetccb(periph, /* priority */ 1);
3025 /* cbfcnp */ cddone,
3026 /* flags */ CAM_DIR_NONE,
3027 /* tag_action */ MSG_SIMPLE_Q_TAG,
3028 /* data_ptr */ NULL,
3030 /* sense_len */ SSD_FULL_SIZE,
3031 sizeof(struct scsi_pause),
3032 /* timeout */ 50000);
3034 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3035 bzero (scsi_cmd, sizeof(*scsi_cmd));
3037 scsi_cmd->op_code = PAUSE;
3038 scsi_cmd->resume = go;
3040 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3041 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3043 xpt_release_ccb(ccb);
3049 cdstartunit(struct cam_periph *periph)
3056 ccb = cdgetccb(periph, /* priority */ 1);
3058 scsi_start_stop(&ccb->csio,
3060 /* cbfcnp */ cddone,
3061 /* tag_action */ MSG_SIMPLE_Q_TAG,
3063 /* load_eject */ FALSE,
3064 /* immediate */ FALSE,
3065 /* sense_len */ SSD_FULL_SIZE,
3066 /* timeout */ 50000);
3068 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3069 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3071 xpt_release_ccb(ccb);
3077 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3084 ccb = cdgetccb(periph, /* priority */ 1);
3086 scsi_start_stop(&ccb->csio,
3088 /* cbfcnp */ cddone,
3089 /* tag_action */ MSG_SIMPLE_Q_TAG,
3091 /* load_eject */ eject,
3092 /* immediate */ FALSE,
3093 /* sense_len */ SSD_FULL_SIZE,
3094 /* timeout */ 50000);
3096 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3097 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3099 xpt_release_ccb(ccb);
3105 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3107 struct scsi_set_speed *scsi_cmd;
3108 struct ccb_scsiio *csio;
3113 ccb = cdgetccb(periph, /* priority */ 1);
3116 /* Preserve old behavior: units in multiples of CDROM speed */
3124 /* cbfcnp */ cddone,
3125 /* flags */ CAM_DIR_NONE,
3126 /* tag_action */ MSG_SIMPLE_Q_TAG,
3127 /* data_ptr */ NULL,
3129 /* sense_len */ SSD_FULL_SIZE,
3130 sizeof(struct scsi_set_speed),
3131 /* timeout */ 50000);
3133 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3134 bzero(scsi_cmd, sizeof(*scsi_cmd));
3136 scsi_cmd->opcode = SET_CD_SPEED;
3137 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3138 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3140 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3141 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3143 xpt_release_ccb(ccb);
3149 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3161 ccb = cdgetccb(periph, /* priority */ 1);
3163 switch (authinfo->format) {
3164 case DVD_REPORT_AGID:
3165 length = sizeof(struct scsi_report_key_data_agid);
3167 case DVD_REPORT_CHALLENGE:
3168 length = sizeof(struct scsi_report_key_data_challenge);
3170 case DVD_REPORT_KEY1:
3171 length = sizeof(struct scsi_report_key_data_key1_key2);
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;
3178 case DVD_REPORT_ASF:
3179 length = sizeof(struct scsi_report_key_data_asf);
3181 case DVD_REPORT_RPC:
3182 length = sizeof(struct scsi_report_key_data_rpc);
3184 case DVD_INVALIDATE_AGID:
3190 break; /* NOTREACHED */
3194 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3199 scsi_report_key(&ccb->csio,
3201 /* cbfcnp */ cddone,
3202 /* tag_action */ MSG_SIMPLE_Q_TAG,
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);
3211 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3212 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3217 if (ccb->csio.resid != 0) {
3218 xpt_print_path(periph->path);
3219 printf("warning, residual for report key command is %d\n",
3223 switch(authinfo->format) {
3224 case DVD_REPORT_AGID: {
3225 struct scsi_report_key_data_agid *agid_data;
3227 agid_data = (struct scsi_report_key_data_agid *)databuf;
3229 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3233 case DVD_REPORT_CHALLENGE: {
3234 struct scsi_report_key_data_challenge *chal_data;
3236 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3238 bcopy(chal_data->challenge_key, authinfo->keychal,
3239 min(sizeof(chal_data->challenge_key),
3240 sizeof(authinfo->keychal)));
3243 case DVD_REPORT_KEY1: {
3244 struct scsi_report_key_data_key1_key2 *key1_data;
3246 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3248 bcopy(key1_data->key1, authinfo->keychal,
3249 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3252 case DVD_REPORT_TITLE_KEY: {
3253 struct scsi_report_key_data_title *title_data;
3255 title_data = (struct scsi_report_key_data_title *)databuf;
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)));
3268 case DVD_REPORT_ASF: {
3269 struct scsi_report_key_data_asf *asf_data;
3271 asf_data = (struct scsi_report_key_data_asf *)databuf;
3273 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3276 case DVD_REPORT_RPC: {
3277 struct scsi_report_key_data_rpc *rpc_data;
3279 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3281 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
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;
3291 case DVD_INVALIDATE_AGID:
3294 /* This should be impossible, since we checked above */
3297 break; /* NOTREACHED */
3300 if (databuf != NULL)
3301 free(databuf, M_DEVBUF);
3303 xpt_release_ccb(ccb);
3309 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3319 ccb = cdgetccb(periph, /* priority */ 1);
3321 switch(authinfo->format) {
3322 case DVD_SEND_CHALLENGE: {
3323 struct scsi_report_key_data_challenge *challenge_data;
3325 length = sizeof(*challenge_data);
3327 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3329 databuf = (u_int8_t *)challenge_data;
3331 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3332 challenge_data->data_len);
3334 bcopy(authinfo->keychal, challenge_data->challenge_key,
3335 min(sizeof(authinfo->keychal),
3336 sizeof(challenge_data->challenge_key)));
3339 case DVD_SEND_KEY2: {
3340 struct scsi_report_key_data_key1_key2 *key2_data;
3342 length = sizeof(*key2_data);
3344 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3346 databuf = (u_int8_t *)key2_data;
3348 scsi_ulto2b(length - sizeof(key2_data->data_len),
3349 key2_data->data_len);
3351 bcopy(authinfo->keychal, key2_data->key1,
3352 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3356 case DVD_SEND_RPC: {
3357 struct scsi_send_key_data_rpc *rpc_data;
3359 length = sizeof(*rpc_data);
3361 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3363 databuf = (u_int8_t *)rpc_data;
3365 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3366 rpc_data->data_len);
3368 rpc_data->region_code = authinfo->region;
3374 break; /* NOTREACHED */
3377 scsi_send_key(&ccb->csio,
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);
3388 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3389 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3393 if (databuf != NULL)
3394 free(databuf, M_DEVBUF);
3396 xpt_release_ccb(ccb);
3402 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3412 /* The address is reserved for many of the formats */
3415 ccb = cdgetccb(periph, /* priority */ 1);
3417 switch(dvdstruct->format) {
3418 case DVD_STRUCT_PHYSICAL:
3419 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3421 case DVD_STRUCT_COPYRIGHT:
3422 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3424 case DVD_STRUCT_DISCKEY:
3425 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3427 case DVD_STRUCT_BCA:
3428 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3430 case DVD_STRUCT_MANUFACT:
3431 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3433 case DVD_STRUCT_CMI:
3437 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
3438 address = dvdstruct->address;
3440 break; /* NOTREACHED */
3441 case DVD_STRUCT_PROTDISCID:
3442 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3444 case DVD_STRUCT_DISCKEYBLOCK:
3445 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3447 case DVD_STRUCT_DDS:
3448 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3450 case DVD_STRUCT_MEDIUM_STAT:
3451 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3453 case DVD_STRUCT_SPARE_AREA:
3454 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3456 case DVD_STRUCT_RMD_LAST:
3460 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
3461 address = dvdstruct->address;
3463 break; /* NOTREACHED */
3464 case DVD_STRUCT_RMD_RMA:
3468 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
3469 address = dvdstruct->address;
3471 break; /* NOTREACHED */
3472 case DVD_STRUCT_PRERECORDED:
3473 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3475 case DVD_STRUCT_UNIQUEID:
3476 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3478 case DVD_STRUCT_DCB:
3482 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
3483 address = dvdstruct->address;
3485 break; /* NOTREACHED */
3486 case DVD_STRUCT_LIST:
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.
3499 break; /* NOTREACHED */
3503 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3507 scsi_read_dvd_structure(&ccb->csio,
3509 /* cbfcnp */ cddone,
3510 /* tag_action */ MSG_SIMPLE_Q_TAG,
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);
3520 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3521 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
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;
3533 (struct scsi_read_dvd_struct_data_physical *)databuf;
3534 inlayer = &phys_data->layer_desc;
3535 outlayer = (struct dvd_layer *)&dvdstruct->data;
3537 dvdstruct->length = sizeof(*inlayer);
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) >>
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);
3565 case DVD_STRUCT_COPYRIGHT: {
3566 struct scsi_read_dvd_struct_data_copyright *copy_data;
3568 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3571 dvdstruct->cpst = copy_data->cps_type;
3572 dvdstruct->rmi = copy_data->region_info;
3573 dvdstruct->length = 0;
3579 * Tell the user what the overall length is, no matter
3580 * what we can actually fit in the data buffer.
3582 dvdstruct->length = length - ccb->csio.resid -
3583 sizeof(struct scsi_read_dvd_struct_data_header);
3586 * But only actually copy out the smaller of what we read
3587 * in or what the structure can take.
3589 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3591 min(sizeof(dvdstruct->data), dvdstruct->length));
3596 if (databuf != NULL)
3597 free(databuf, M_DEVBUF);
3599 xpt_release_ccb(ccb);
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)
3611 struct scsi_report_key *scsi_cmd;
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);
3624 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3626 /*data_ptr*/ data_ptr,
3627 /*dxfer_len*/ dxfer_len,
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,
3640 struct scsi_send_key *scsi_cmd;
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;
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);
3653 /*flags*/ CAM_DIR_OUT,
3655 /*data_ptr*/ data_ptr,
3656 /*dxfer_len*/ dxfer_len,
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)
3671 struct scsi_read_dvd_structure *scsi_cmd;
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;
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;
3687 /*flags*/ CAM_DIR_IN,
3689 /*data_ptr*/ data_ptr,
3690 /*dxfer_len*/ dxfer_len,