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.9 2003/08/07 21:16:44 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_ccb.h"
70 #include "../cam_extend.h"
71 #include "../cam_periph.h"
72 #include "../cam_xpt_periph.h"
73 #include "../cam_queue.h"
75 #include "scsi_message.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
179 static d_open_t cdopen;
180 static d_close_t cdclose;
181 static d_ioctl_t cdioctl;
182 static d_strategy_t cdstrategy;
184 static periph_init_t cdinit;
185 static periph_ctor_t cdregister;
186 static periph_dtor_t cdcleanup;
187 static periph_start_t cdstart;
188 static periph_oninv_t cdoninvalidate;
189 static void cdasync(void *callback_arg, u_int32_t code,
190 struct cam_path *path, void *arg);
191 static void cdshorttimeout(void *arg);
192 static void cdschedule(struct cam_periph *periph, int priority);
193 static void cdrunchangerqueue(void *arg);
194 static void cdchangerschedule(struct cd_softc *softc);
195 static int cdrunccb(union ccb *ccb,
196 int (*error_routine)(union ccb *ccb,
198 u_int32_t sense_flags),
199 u_int32_t cam_flags, u_int32_t sense_flags);
200 static union ccb *cdgetccb(struct cam_periph *periph,
202 static void cddone(struct cam_periph *periph,
203 union ccb *start_ccb);
204 static int cderror(union ccb *ccb, u_int32_t cam_flags,
205 u_int32_t sense_flags);
206 static void cdprevent(struct cam_periph *periph, int action);
207 static int cdsize(dev_t dev, u_int32_t *size);
208 static int cdfirsttrackisdata(struct cam_periph *periph);
209 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
210 u_int32_t start, struct cd_toc_entry *data,
212 static int cdgetmode(struct cam_periph *periph,
213 struct cd_mode_data *data, u_int32_t page);
214 static int cdsetmode(struct cam_periph *periph,
215 struct cd_mode_data *data);
216 static int cdplay(struct cam_periph *periph, u_int32_t blk,
218 static int cdreadsubchannel(struct cam_periph *periph,
219 u_int32_t mode, u_int32_t format,
221 struct cd_sub_channel_info *data,
223 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
224 u_int32_t starts, u_int32_t startf,
225 u_int32_t endm, u_int32_t ends,
227 static int cdplaytracks(struct cam_periph *periph,
228 u_int32_t strack, u_int32_t sindex,
229 u_int32_t etrack, u_int32_t eindex);
230 static int cdpause(struct cam_periph *periph, u_int32_t go);
231 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
232 static int cdstartunit(struct cam_periph *periph);
233 static int cdsetspeed(struct cam_periph *periph,
234 u_int32_t rdspeed, u_int32_t wrspeed);
235 static int cdreportkey(struct cam_periph *periph,
236 struct dvd_authinfo *authinfo);
237 static int cdsendkey(struct cam_periph *periph,
238 struct dvd_authinfo *authinfo);
239 static int cdreaddvdstructure(struct cam_periph *periph,
240 struct dvd_struct *dvdstruct);
242 static struct periph_driver cddriver =
245 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
248 DATA_SET(periphdriver_set, cddriver);
250 /* For 2.2-stable support */
254 static struct cdevsw cd_cdevsw = {
256 /* maj */ SCSICD_CDEV_MAJOR,
264 /* write */ physwrite,
268 /* strategy */ cdstrategy,
273 static struct extend_array *cdperiphs;
274 static int num_changers;
276 #ifndef CHANGER_MIN_BUSY_SECONDS
277 #define CHANGER_MIN_BUSY_SECONDS 5
279 #ifndef CHANGER_MAX_BUSY_SECONDS
280 #define CHANGER_MAX_BUSY_SECONDS 15
283 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
284 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
287 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
290 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
291 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
292 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
293 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
294 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
295 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
296 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
300 target_id_t target_id;
303 struct timeval start_time;
304 struct cd_softc *cur_device;
305 struct callout_handle short_handle;
306 struct callout_handle long_handle;
307 volatile cd_changer_flags flags;
308 STAILQ_ENTRY(cdchanger) changer_links;
309 STAILQ_HEAD(chdevlist, cd_softc) chluns;
312 static STAILQ_HEAD(changerlist, cdchanger) changerq;
318 struct cam_path *path;
321 * Create our extend array for storing the devices we attach to.
323 cdperiphs = cam_extend_new();
324 if (cdperiphs == NULL) {
325 printf("cd: Failed to alloc extend array!\n");
330 * Install a global async callback. This callback will
331 * receive async callbacks like "new device found".
333 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
334 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
336 if (status == CAM_REQ_CMP) {
337 struct ccb_setasync csa;
339 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
340 csa.ccb_h.func_code = XPT_SASYNC_CB;
341 csa.event_enable = AC_FOUND_DEVICE;
342 csa.callback = cdasync;
343 csa.callback_arg = NULL;
344 xpt_action((union ccb *)&csa);
345 status = csa.ccb_h.status;
349 if (status != CAM_REQ_CMP) {
350 printf("cd: Failed to attach master async callback "
351 "due to status 0x%x!\n", status);
356 cdoninvalidate(struct cam_periph *periph)
359 struct cd_softc *softc;
361 struct ccb_setasync csa;
363 softc = (struct cd_softc *)periph->softc;
366 * De-register any async callbacks.
368 xpt_setup_ccb(&csa.ccb_h, periph->path,
370 csa.ccb_h.func_code = XPT_SASYNC_CB;
371 csa.event_enable = 0;
372 csa.callback = cdasync;
373 csa.callback_arg = periph;
374 xpt_action((union ccb *)&csa);
376 softc->flags |= CD_FLAG_INVALID;
379 * Although the oninvalidate() routines are always called at
380 * splsoftcam, we need to be at splbio() here to keep the buffer
381 * queue from being modified while we traverse it.
386 * Return all queued I/O with ENXIO.
387 * XXX Handle any transactions queued to the card
388 * with XPT_ABORT_CCB.
390 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
391 bufq_remove(&softc->buf_queue, q_bp);
392 q_bp->b_resid = q_bp->b_bcount;
393 q_bp->b_error = ENXIO;
394 q_bp->b_flags |= B_ERROR;
400 * If this device is part of a changer, and it was scheduled
401 * to run, remove it from the run queue since we just nuked
402 * all of its scheduled I/O.
404 if ((softc->flags & CD_FLAG_CHANGER)
405 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
406 camq_remove(&softc->changer->devq, softc->pinfo.index);
408 xpt_print_path(periph->path);
409 printf("lost device\n");
413 cdcleanup(struct cam_periph *periph)
415 struct cd_softc *softc;
418 softc = (struct cd_softc *)periph->softc;
420 xpt_print_path(periph->path);
421 printf("removing device entry\n");
425 * In the queued, non-active case, the device in question
426 * has already been removed from the changer run queue. Since this
427 * device is active, we need to de-activate it, and schedule
428 * another device to run. (if there is another one to run)
430 if ((softc->flags & CD_FLAG_CHANGER)
431 && (softc->flags & CD_FLAG_ACTIVE)) {
434 * The purpose of the short timeout is soley to determine
435 * whether the current device has finished or not. Well,
436 * since we're removing the active device, we know that it
437 * is finished. So, get rid of the short timeout.
438 * Otherwise, if we're in the time period before the short
439 * timeout fires, and there are no other devices in the
440 * queue to run, there won't be any other device put in the
441 * active slot. i.e., when we call cdrunchangerqueue()
442 * below, it won't do anything. Then, when the short
443 * timeout fires, it'll look at the "current device", which
444 * we are free below, and possibly panic the kernel on a
445 * bogus pointer reference.
447 * The long timeout doesn't really matter, since we
448 * decrement the qfrozen_cnt to indicate that there is
449 * nothing in the active slot now. Therefore, there won't
450 * be any bogus pointer references there.
452 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
453 untimeout(cdshorttimeout, softc->changer,
454 softc->changer->short_handle);
455 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
457 softc->changer->devq.qfrozen_cnt--;
458 softc->changer->flags |= CHANGER_MANUAL_CALL;
459 cdrunchangerqueue(softc->changer);
463 * If we're removing the last device on the changer, go ahead and
464 * remove the changer device structure.
466 if ((softc->flags & CD_FLAG_CHANGER)
467 && (--softc->changer->num_devices == 0)) {
470 * Theoretically, there shouldn't be any timeouts left, but
471 * I'm not completely sure that that will be the case. So,
472 * it won't hurt to check and see if there are any left.
474 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
475 untimeout(cdrunchangerqueue, softc->changer,
476 softc->changer->long_handle);
477 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
480 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
481 untimeout(cdshorttimeout, softc->changer,
482 softc->changer->short_handle);
483 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
486 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
488 xpt_print_path(periph->path);
489 printf("removing changer entry\n");
490 free(softc->changer, M_DEVBUF);
493 devstat_remove_entry(&softc->device_stats);
494 cam_extend_release(cdperiphs, periph->unit_number);
495 if (softc->disk.d_dev) {
496 disk_destroy(&softc->disk);
498 free(softc, M_DEVBUF);
503 cdasync(void *callback_arg, u_int32_t code,
504 struct cam_path *path, void *arg)
506 struct cam_periph *periph;
508 periph = (struct cam_periph *)callback_arg;
510 case AC_FOUND_DEVICE:
512 struct ccb_getdev *cgd;
515 cgd = (struct ccb_getdev *)arg;
517 if (SID_TYPE(&cgd->inq_data) != T_CDROM
518 && SID_TYPE(&cgd->inq_data) != T_WORM)
522 * Allocate a peripheral instance for
523 * this device and start the probe
526 status = cam_periph_alloc(cdregister, cdoninvalidate,
528 "cd", CAM_PERIPH_BIO,
529 cgd->ccb_h.path, cdasync,
530 AC_FOUND_DEVICE, cgd);
532 if (status != CAM_REQ_CMP
533 && status != CAM_REQ_INPROG)
534 printf("cdasync: Unable to attach new device "
535 "due to status 0x%x\n", status);
542 struct cd_softc *softc;
543 struct ccb_hdr *ccbh;
546 softc = (struct cd_softc *)periph->softc;
549 * Don't fail on the expected unit attention
552 softc->flags |= CD_FLAG_RETRY_UA;
553 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
554 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
555 ccbh->ccb_state |= CD_CCB_RETRY_UA;
560 cam_periph_async(periph, code, path, arg);
566 cdregister(struct cam_periph *periph, void *arg)
568 struct cd_softc *softc;
569 struct ccb_setasync csa;
570 struct ccb_getdev *cgd;
573 cgd = (struct ccb_getdev *)arg;
574 if (periph == NULL) {
575 printf("cdregister: periph was NULL!!\n");
576 return(CAM_REQ_CMP_ERR);
579 printf("cdregister: no getdev CCB, can't register device\n");
580 return(CAM_REQ_CMP_ERR);
583 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
586 printf("cdregister: Unable to probe new device. "
587 "Unable to allocate softc\n");
588 return(CAM_REQ_CMP_ERR);
591 bzero(softc, sizeof(*softc));
592 LIST_INIT(&softc->pending_ccbs);
593 softc->state = CD_STATE_PROBE;
594 bufq_init(&softc->buf_queue);
595 if (SID_IS_REMOVABLE(&cgd->inq_data))
596 softc->flags |= CD_FLAG_DISC_REMOVABLE;
597 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
598 softc->flags |= CD_FLAG_TAGGED_QUEUING;
600 periph->softc = softc;
601 softc->periph = periph;
603 cam_extend_set(cdperiphs, periph->unit_number, periph);
606 * See if this device has any quirks.
608 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
609 (caddr_t)cd_quirk_table,
610 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
611 sizeof(*cd_quirk_table), scsi_inquiry_match);
614 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
616 softc->quirks = CD_Q_NONE;
619 * We need to register the statistics structure for this device,
620 * but we don't have the blocksize yet for it. So, we register
621 * the structure and indicate that we don't have the blocksize
622 * yet. Unlike other SCSI peripheral drivers, we explicitly set
623 * the device type here to be CDROM, rather than just ORing in
624 * the device type. This is because this driver can attach to either
625 * CDROM or WORM devices, and we want this peripheral driver to
626 * show up in the devstat list as a CD peripheral driver, not a
627 * WORM peripheral driver. WORM drives will also have the WORM
628 * driver attached to them.
630 devstat_add_entry(&softc->device_stats, "cd",
631 periph->unit_number, 0,
632 DEVSTAT_BS_UNAVAILABLE,
633 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
634 DEVSTAT_PRIORITY_CD);
635 disk_create(periph->unit_number, &softc->disk,
636 DSO_ONESLICE | DSO_COMPATLABEL, &cd_cdevsw);
639 * Add an async callback so that we get
640 * notified if this device goes away.
642 xpt_setup_ccb(&csa.ccb_h, periph->path,
644 csa.ccb_h.func_code = XPT_SASYNC_CB;
645 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
646 csa.callback = cdasync;
647 csa.callback_arg = periph;
648 xpt_action((union ccb *)&csa);
651 * If the target lun is greater than 0, we most likely have a CD
652 * changer device. Check the quirk entries as well, though, just
653 * in case someone has a CD tower with one lun per drive or
654 * something like that. Also, if we know up front that a
655 * particular device is a changer, we can mark it as such starting
656 * with lun 0, instead of lun 1. It shouldn't be necessary to have
657 * a quirk entry to define something as a changer, however.
659 if (((cgd->ccb_h.target_lun > 0)
660 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
661 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
662 struct cdchanger *nchanger;
663 struct cam_periph *nperiph;
664 struct cam_path *path;
668 /* Set the changer flag in the current device's softc */
669 softc->flags |= CD_FLAG_CHANGER;
671 if (num_changers == 0)
672 STAILQ_INIT(&changerq);
675 * Now, look around for an existing changer device with the
676 * same path and target ID as the current device.
679 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
681 nchanger = STAILQ_NEXT(nchanger, changer_links)){
682 if ((nchanger->path_id == cgd->ccb_h.path_id)
683 && (nchanger->target_id == cgd->ccb_h.target_id)) {
690 * If we found a matching entry, just add this device to
691 * the list of devices on this changer.
694 struct chdevlist *chlunhead;
696 chlunhead = &nchanger->chluns;
699 * XXX KDM look at consolidating this code with the
700 * code below in a separate function.
704 * Create a path with lun id 0, and see if we can
705 * find a matching device
707 status = xpt_create_path(&path, /*periph*/ periph,
709 cgd->ccb_h.target_id, 0);
711 if ((status == CAM_REQ_CMP)
712 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
713 struct cd_softc *nsoftc;
715 nsoftc = (struct cd_softc *)nperiph->softc;
717 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
718 nsoftc->flags |= CD_FLAG_CHANGER;
719 nchanger->num_devices++;
720 if (camq_resize(&nchanger->devq,
721 nchanger->num_devices)!=CAM_REQ_CMP){
722 printf("cdregister: "
728 nsoftc->changer = nchanger;
729 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
731 STAILQ_INSERT_TAIL(&nchanger->chluns,
732 nsoftc,changer_links);
735 } else if (status == CAM_REQ_CMP)
738 printf("cdregister: unable to allocate path\n"
739 "cdregister: changer support may be "
743 nchanger->num_devices++;
745 softc->changer = nchanger;
746 softc->pinfo.index = CAM_UNQUEUED_INDEX;
748 if (camq_resize(&nchanger->devq,
749 nchanger->num_devices) != CAM_REQ_CMP) {
750 printf("cdregister: camq_resize "
751 "failed, changer support may "
755 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
758 * In this case, we don't already have an entry for this
759 * particular changer, so we need to create one, add it to
760 * the queue, and queue this device on the list for this
761 * changer. Before we queue this device, however, we need
762 * to search for lun id 0 on this target, and add it to the
763 * queue first, if it exists. (and if it hasn't already
764 * been marked as part of the changer.)
767 nchanger = malloc(sizeof(struct cdchanger),
770 if (nchanger == NULL) {
771 softc->flags &= ~CD_FLAG_CHANGER;
772 printf("cdregister: unable to malloc "
773 "changer structure\ncdregister: "
774 "changer support disabled\n");
777 * Yes, gotos can be gross but in this case
778 * I think it's justified..
783 /* zero the structure */
784 bzero(nchanger, sizeof(struct cdchanger));
786 if (camq_init(&nchanger->devq, 1) != 0) {
787 softc->flags &= ~CD_FLAG_CHANGER;
788 printf("cdregister: changer support "
795 nchanger->path_id = cgd->ccb_h.path_id;
796 nchanger->target_id = cgd->ccb_h.target_id;
798 /* this is superfluous, but it makes things clearer */
799 nchanger->num_devices = 0;
801 STAILQ_INIT(&nchanger->chluns);
803 STAILQ_INSERT_TAIL(&changerq, nchanger,
807 * Create a path with lun id 0, and see if we can
808 * find a matching device
810 status = xpt_create_path(&path, /*periph*/ periph,
812 cgd->ccb_h.target_id, 0);
815 * If we were able to allocate the path, and if we
816 * find a matching device and it isn't already
817 * marked as part of a changer, then we add it to
818 * the current changer.
820 if ((status == CAM_REQ_CMP)
821 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
822 && ((((struct cd_softc *)periph->softc)->flags &
823 CD_FLAG_CHANGER) == 0)) {
824 struct cd_softc *nsoftc;
826 nsoftc = (struct cd_softc *)nperiph->softc;
828 nsoftc->flags |= CD_FLAG_CHANGER;
829 nchanger->num_devices++;
830 if (camq_resize(&nchanger->devq,
831 nchanger->num_devices) != CAM_REQ_CMP) {
832 printf("cdregister: camq_resize "
833 "failed, changer support may "
836 nsoftc->changer = nchanger;
837 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
839 STAILQ_INSERT_TAIL(&nchanger->chluns,
840 nsoftc, changer_links);
842 } else if (status == CAM_REQ_CMP)
845 printf("cdregister: unable to allocate path\n"
846 "cdregister: changer support may be "
850 softc->changer = nchanger;
851 softc->pinfo.index = CAM_UNQUEUED_INDEX;
852 nchanger->num_devices++;
853 if (camq_resize(&nchanger->devq,
854 nchanger->num_devices) != CAM_REQ_CMP) {
855 printf("cdregister: camq_resize "
856 "failed, changer support may "
859 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
866 /* Lock this peripheral until we are setup */
868 cam_periph_lock(periph, 0);
870 if ((softc->flags & CD_FLAG_CHANGER) == 0)
871 xpt_schedule(periph, /*priority*/5);
873 cdschedule(periph, /*priority*/ 5);
879 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
881 struct disklabel *label;
882 struct cam_periph *periph;
883 struct cd_softc *softc;
884 struct ccb_getdev cgd;
890 periph = cam_extend_get(cdperiphs, unit);
895 softc = (struct cd_softc *)periph->softc;
898 * Grab splsoftcam and hold it until we lock the peripheral.
901 if (softc->flags & CD_FLAG_INVALID) {
906 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
913 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
916 cdprevent(periph, PR_PREVENT);
918 /* find out the size */
919 if ((error = cdsize(dev, &size)) != 0) {
920 cdprevent(periph, PR_ALLOW);
921 cam_periph_unlock(periph);
922 cam_periph_release(periph);
927 * If we get a non-zero return, revert back to not reading the
928 * label off the disk. The first track is likely audio, which
929 * won't have a disklabel.
931 if ((error = cdfirsttrackisdata(periph)) != 0) {
932 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
933 softc->disk.d_dsflags |= DSO_NOLABELS;
938 * Build prototype label for whole disk.
939 * Should take information about different data tracks from the
940 * TOC and put it in the partition table.
942 label = &softc->disk.d_label;
943 bzero(label, sizeof(*label));
944 label->d_type = DTYPE_SCSI;
947 * Grab the inquiry data to get the vendor and product names.
948 * Put them in the typename and packname for the label.
950 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
951 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
952 xpt_action((union ccb *)&cgd);
954 strncpy(label->d_typename, cgd.inq_data.vendor,
955 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
956 strncpy(label->d_packname, cgd.inq_data.product,
957 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
959 label->d_secsize = softc->params.blksize;
960 label->d_secperunit = softc->params.disksize;
961 label->d_flags = D_REMOVABLE;
963 * Make partition 'a' cover the whole disk. This is a temporary
964 * compatibility hack. The 'a' partition should not exist, so
965 * the slice code won't create it. The slice code will make
966 * partition (RAW_PART + 'a') cover the whole disk and fill in
967 * some more defaults.
969 label->d_partitions[0].p_size = label->d_secperunit;
970 label->d_partitions[0].p_fstype = FS_OTHER;
973 * We unconditionally (re)set the blocksize each time the
974 * CD device is opened. This is because the CD can change,
975 * and therefore the blocksize might change.
976 * XXX problems here if some slice or partition is still
977 * open with the old size?
979 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
980 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
981 softc->device_stats.block_size = softc->params.blksize;
983 cam_periph_unlock(periph);
985 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
991 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
993 struct cam_periph *periph;
994 struct cd_softc *softc;
998 periph = cam_extend_get(cdperiphs, unit);
1002 softc = (struct cd_softc *)periph->softc;
1004 if ((error = cam_periph_lock(periph, 0)) != 0)
1007 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1008 cdprevent(periph, PR_ALLOW);
1011 * Unconditionally set the dsopen() flags back to their default
1014 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1015 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1018 * Since we're closing this CD, mark the blocksize as unavailable.
1019 * It will be marked as available whence the CD is opened again.
1021 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1023 cam_periph_unlock(periph);
1024 cam_periph_release(periph);
1030 cdshorttimeout(void *arg)
1032 struct cdchanger *changer;
1037 changer = (struct cdchanger *)arg;
1039 /* Always clear the short timeout flag, since that's what we're in */
1040 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1043 * Check to see if there is any more pending or outstanding I/O for
1044 * this device. If not, move it out of the active slot.
1046 if ((bufq_first(&changer->cur_device->buf_queue) == NULL)
1047 && (changer->cur_device->device_stats.busy_count == 0)) {
1048 changer->flags |= CHANGER_MANUAL_CALL;
1049 cdrunchangerqueue(changer);
1056 * This is a wrapper for xpt_schedule. It only applies to changers.
1059 cdschedule(struct cam_periph *periph, int priority)
1061 struct cd_softc *softc;
1066 softc = (struct cd_softc *)periph->softc;
1069 * If this device isn't currently queued, and if it isn't
1070 * the active device, then we queue this device and run the
1071 * changer queue if there is no timeout scheduled to do it.
1072 * If this device is the active device, just schedule it
1073 * to run again. If this device is queued, there should be
1074 * a timeout in place already that will make sure it runs.
1076 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1077 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1079 * We don't do anything with the priority here.
1080 * This is strictly a fifo queue.
1082 softc->pinfo.priority = 1;
1083 softc->pinfo.generation = ++softc->changer->devq.generation;
1084 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1087 * Since we just put a device in the changer queue,
1088 * check and see if there is a timeout scheduled for
1089 * this changer. If so, let the timeout handle
1090 * switching this device into the active slot. If
1091 * not, manually call the timeout routine to
1094 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1095 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1096 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1097 softc->changer->flags |= CHANGER_MANUAL_CALL;
1098 cdrunchangerqueue(softc->changer);
1100 } else if ((softc->flags & CD_FLAG_ACTIVE)
1101 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1102 xpt_schedule(periph, priority);
1109 cdrunchangerqueue(void *arg)
1111 struct cd_softc *softc;
1112 struct cdchanger *changer;
1113 int called_from_timeout;
1118 changer = (struct cdchanger *)arg;
1121 * If we have NOT been called from cdstrategy() or cddone(), and
1122 * instead from a timeout routine, go ahead and clear the
1125 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1126 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1127 called_from_timeout = 1;
1129 called_from_timeout = 0;
1131 /* Always clear the manual call flag */
1132 changer->flags &= ~CHANGER_MANUAL_CALL;
1134 /* nothing to do if the queue is empty */
1135 if (changer->devq.entries <= 0) {
1141 * If the changer queue is frozen, that means we have an active
1144 if (changer->devq.qfrozen_cnt > 0) {
1146 if (changer->cur_device->device_stats.busy_count > 0) {
1147 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1148 changer->cur_device->bufs_left =
1149 changer->cur_device->device_stats.busy_count;
1150 if (called_from_timeout) {
1151 changer->long_handle =
1152 timeout(cdrunchangerqueue, changer,
1153 changer_max_busy_seconds * hz);
1154 changer->flags |= CHANGER_TIMEOUT_SCHED;
1161 * We always need to reset the frozen count and clear the
1164 changer->devq.qfrozen_cnt--;
1165 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1166 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1169 * Check to see whether the current device has any I/O left
1170 * to do. If so, requeue it at the end of the queue. If
1171 * not, there is no need to requeue it.
1173 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1175 changer->cur_device->pinfo.generation =
1176 ++changer->devq.generation;
1177 camq_insert(&changer->devq,
1178 (cam_pinfo *)changer->cur_device);
1182 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1184 changer->cur_device = softc;
1186 changer->devq.qfrozen_cnt++;
1187 softc->flags |= CD_FLAG_ACTIVE;
1189 /* Just in case this device is waiting */
1190 wakeup(&softc->changer);
1191 xpt_schedule(softc->periph, /*priority*/ 1);
1194 * Get rid of any pending timeouts, and set a flag to schedule new
1195 * ones so this device gets its full time quantum.
1197 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1198 untimeout(cdrunchangerqueue, changer, changer->long_handle);
1199 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1202 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1203 untimeout(cdshorttimeout, changer, changer->short_handle);
1204 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1208 * We need to schedule timeouts, but we only do this after the
1209 * first transaction has completed. This eliminates the changer
1212 changer->flags |= CHANGER_NEED_TIMEOUT;
1218 cdchangerschedule(struct cd_softc *softc)
1220 struct cdchanger *changer;
1225 changer = softc->changer;
1228 * If this is a changer, and this is the current device,
1229 * and this device has at least the minimum time quantum to
1230 * run, see if we can switch it out.
1232 if ((softc->flags & CD_FLAG_ACTIVE)
1233 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1234 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1236 * We try three things here. The first is that we
1237 * check to see whether the schedule on completion
1238 * flag is set. If it is, we decrement the number
1239 * of buffers left, and if it's zero, we reschedule.
1240 * Next, we check to see whether the pending buffer
1241 * queue is empty and whether there are no
1242 * outstanding transactions. If so, we reschedule.
1243 * Next, we see if the pending buffer queue is empty.
1244 * If it is, we set the number of buffers left to
1245 * the current active buffer count and set the
1246 * schedule on complete flag.
1248 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1249 if (--softc->bufs_left == 0) {
1250 softc->changer->flags |=
1251 CHANGER_MANUAL_CALL;
1252 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1253 cdrunchangerqueue(softc->changer);
1255 } else if ((bufq_first(&softc->buf_queue) == NULL)
1256 && (softc->device_stats.busy_count == 0)) {
1257 softc->changer->flags |= CHANGER_MANUAL_CALL;
1258 cdrunchangerqueue(softc->changer);
1260 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1261 && (softc->flags & CD_FLAG_ACTIVE)) {
1264 * Now that the first transaction to this
1265 * particular device has completed, we can go ahead
1266 * and schedule our timeouts.
1268 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1269 changer->long_handle =
1270 timeout(cdrunchangerqueue, changer,
1271 changer_max_busy_seconds * hz);
1272 changer->flags |= CHANGER_TIMEOUT_SCHED;
1274 printf("cdchangerschedule: already have a long"
1277 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1278 changer->short_handle =
1279 timeout(cdshorttimeout, changer,
1280 changer_min_busy_seconds * hz);
1281 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1283 printf("cdchangerschedule: already have a short "
1287 * We just scheduled timeouts, no need to schedule
1290 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1297 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1298 u_int32_t cam_flags,
1299 u_int32_t sense_flags),
1300 u_int32_t cam_flags, u_int32_t sense_flags)
1302 struct cd_softc *softc;
1303 struct cam_periph *periph;
1306 periph = xpt_path_periph(ccb->ccb_h.path);
1307 softc = (struct cd_softc *)periph->softc;
1309 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1310 &softc->device_stats);
1312 if (softc->flags & CD_FLAG_CHANGER)
1313 cdchangerschedule(softc);
1319 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1321 struct cd_softc *softc;
1324 softc = (struct cd_softc *)periph->softc;
1326 if (softc->flags & CD_FLAG_CHANGER) {
1331 * This should work the first time this device is woken up,
1332 * but just in case it doesn't, we use a while loop.
1334 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1336 * If this changer isn't already queued, queue it up.
1338 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1339 softc->pinfo.priority = 1;
1340 softc->pinfo.generation =
1341 ++softc->changer->devq.generation;
1342 camq_insert(&softc->changer->devq,
1343 (cam_pinfo *)softc);
1345 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1346 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1347 && ((softc->changer->flags
1348 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1349 softc->changer->flags |= CHANGER_MANUAL_CALL;
1350 cdrunchangerqueue(softc->changer);
1352 tsleep(&softc->changer, 0, "cgticb", 0);
1356 return(cam_periph_getccb(periph, priority));
1361 * Actually translate the requested transfer into one the physical driver
1362 * can understand. The transfer is described by a buf and will include
1363 * only one physical transfer.
1366 cdstrategy(struct buf *bp)
1368 struct cam_periph *periph;
1369 struct cd_softc *softc;
1373 unit = dkunit(bp->b_dev);
1374 part = dkpart(bp->b_dev);
1375 periph = cam_extend_get(cdperiphs, unit);
1376 if (periph == NULL) {
1377 bp->b_error = ENXIO;
1381 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1383 softc = (struct cd_softc *)periph->softc;
1386 * Mask interrupts so that the pack cannot be invalidated until
1387 * after we are in the queue. Otherwise, we might not properly
1388 * clean up one of the buffers.
1393 * If the device has been made invalid, error out
1395 if ((softc->flags & CD_FLAG_INVALID)) {
1397 bp->b_error = ENXIO;
1402 * Place it in the queue of disk activities for this disk
1404 bufqdisksort(&softc->buf_queue, bp);
1409 * Schedule ourselves for performing the work. We do things
1410 * differently for changers.
1412 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1413 xpt_schedule(periph, /* XXX priority */1);
1415 cdschedule(periph, /* priority */ 1);
1419 bp->b_flags |= B_ERROR;
1421 * Correctly set the buf to indicate a completed xfer
1423 bp->b_resid = bp->b_bcount;
1429 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1431 struct cd_softc *softc;
1433 struct ccb_scsiio *csio;
1434 struct scsi_read_capacity_data *rcap;
1437 softc = (struct cd_softc *)periph->softc;
1439 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1441 switch (softc->state) {
1442 case CD_STATE_NORMAL:
1447 bp = bufq_first(&softc->buf_queue);
1448 if (periph->immediate_priority <= periph->pinfo.priority) {
1449 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1451 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1453 periph->immediate_priority = CAM_PRIORITY_NONE;
1455 wakeup(&periph->ccb_list);
1456 } else if (bp == NULL) {
1458 xpt_release_ccb(start_ccb);
1460 bufq_remove(&softc->buf_queue, bp);
1462 devstat_start_transaction(&softc->device_stats);
1464 scsi_read_write(&start_ccb->csio,
1466 /* cbfcnp */ cddone,
1467 (bp->b_flags & B_ORDERED) != 0 ?
1470 /* read */bp->b_flags & B_READ,
1472 /* minimum_cmd_size */ 10,
1473 /* lba */ bp->b_pblkno,
1474 bp->b_bcount / softc->params.blksize,
1475 /* data_ptr */ bp->b_data,
1476 /* dxfer_len */ bp->b_bcount,
1477 /* sense_len */ SSD_FULL_SIZE,
1478 /* timeout */ 30000);
1479 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1483 * Block out any asyncronous callbacks
1484 * while we touch the pending ccb list.
1487 LIST_INSERT_HEAD(&softc->pending_ccbs,
1488 &start_ccb->ccb_h, periph_links.le);
1491 /* We expect a unit attention from this device */
1492 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1493 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1494 softc->flags &= ~CD_FLAG_RETRY_UA;
1497 start_ccb->ccb_h.ccb_bp = bp;
1498 bp = bufq_first(&softc->buf_queue);
1501 xpt_action(start_ccb);
1504 /* Have more work to do, so ensure we stay scheduled */
1505 xpt_schedule(periph, /* XXX priority */1);
1509 case CD_STATE_PROBE:
1512 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1516 xpt_print_path(periph->path);
1517 printf("cdstart: Couldn't malloc read_capacity data\n");
1518 /* cd_free_periph??? */
1521 csio = &start_ccb->csio;
1522 scsi_read_capacity(csio,
1529 start_ccb->ccb_h.ccb_bp = NULL;
1530 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1531 xpt_action(start_ccb);
1538 cddone(struct cam_periph *periph, union ccb *done_ccb)
1540 struct cd_softc *softc;
1541 struct ccb_scsiio *csio;
1543 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1545 softc = (struct cd_softc *)periph->softc;
1546 csio = &done_ccb->csio;
1548 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1549 case CD_CCB_BUFFER_IO:
1555 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1558 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1561 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1566 /* Retry selection timeouts */
1567 sf |= SF_RETRY_SELTO;
1569 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1571 * A retry was scheuled, so
1582 xpt_print_path(periph->path);
1583 printf("cddone: got error %#x back\n", error);
1585 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1586 bufq_remove(&softc->buf_queue, q_bp);
1587 q_bp->b_resid = q_bp->b_bcount;
1588 q_bp->b_error = EIO;
1589 q_bp->b_flags |= B_ERROR;
1593 bp->b_resid = bp->b_bcount;
1594 bp->b_error = error;
1595 bp->b_flags |= B_ERROR;
1596 cam_release_devq(done_ccb->ccb_h.path,
1600 /*getcount_only*/0);
1603 bp->b_resid = csio->resid;
1605 if (bp->b_resid != 0) {
1606 /* Short transfer ??? */
1607 bp->b_flags |= B_ERROR;
1612 * Block out any asyncronous callbacks
1613 * while we touch the pending ccb list.
1616 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1619 if (softc->flags & CD_FLAG_CHANGER)
1620 cdchangerschedule(softc);
1622 devstat_end_transaction_buf(&softc->device_stats, bp);
1628 struct scsi_read_capacity_data *rdcap;
1629 char announce_buf[120]; /*
1630 * Currently (9/30/97) the
1631 * longest possible announce
1632 * buffer is 108 bytes, for the
1633 * first error case below.
1634 * That is 39 bytes for the
1635 * basic string, 16 bytes for the
1636 * biggest sense key (hardware
1637 * error), 52 bytes for the
1638 * text of the largest sense
1639 * qualifier valid for a CDROM,
1640 * (0x72, 0x03 or 0x04,
1641 * 0x03), and one byte for the
1642 * null terminating character.
1643 * To allow for longer strings,
1644 * the announce buffer is 120
1647 struct cd_params *cdp;
1649 cdp = &softc->params;
1651 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1653 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1654 cdp->blksize = scsi_4btoul (rdcap->length);
1656 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1658 snprintf(announce_buf, sizeof(announce_buf),
1659 "cd present [%lu x %lu byte records]",
1660 cdp->disksize, (u_long)cdp->blksize);
1665 * Retry any UNIT ATTENTION type errors. They
1666 * are expected at boot.
1668 error = cderror(done_ccb, 0, SF_RETRY_UA |
1669 SF_NO_PRINT | SF_RETRY_SELTO);
1670 if (error == ERESTART) {
1672 * A retry was scheuled, so
1676 } else if (error != 0) {
1678 struct scsi_sense_data *sense;
1680 int sense_key, error_code;
1683 struct ccb_getdev cgd;
1685 /* Don't wedge this device's queue */
1686 cam_release_devq(done_ccb->ccb_h.path,
1690 /*getcount_only*/0);
1692 status = done_ccb->ccb_h.status;
1694 xpt_setup_ccb(&cgd.ccb_h,
1695 done_ccb->ccb_h.path,
1697 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1698 xpt_action((union ccb *)&cgd);
1700 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1701 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1702 || ((status & CAM_AUTOSNS_VALID) == 0))
1708 sense = &csio->sense_data;
1709 scsi_extract_sense(sense, &error_code,
1714 * Attach to anything that claims to be a
1715 * CDROM or WORM device, as long as it
1716 * doesn't return a "Logical unit not
1717 * supported" (0x25) error.
1719 if ((have_sense) && (asc != 0x25)
1720 && (error_code == SSD_CURRENT_ERROR))
1721 snprintf(announce_buf,
1722 sizeof(announce_buf),
1723 "Attempt to query device "
1724 "size failed: %s, %s",
1725 scsi_sense_key_text[sense_key],
1726 scsi_sense_desc(asc,ascq,
1728 else if ((have_sense == 0)
1729 && ((status & CAM_STATUS_MASK) ==
1730 CAM_SCSI_STATUS_ERROR)
1731 && (csio->scsi_status ==
1732 SCSI_STATUS_BUSY)) {
1733 snprintf(announce_buf,
1734 sizeof(announce_buf),
1735 "Attempt to query device "
1736 "size failed: SCSI status: BUSY");
1737 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1739 * We only print out an error for
1740 * CDROM type devices. For WORM
1741 * devices, we don't print out an
1742 * error since a few WORM devices
1743 * don't support CDROM commands.
1744 * If we have sense information, go
1745 * ahead and print it out.
1746 * Otherwise, just say that we
1751 * Just print out the error, not
1752 * the full probe message, when we
1759 xpt_print_path(periph->path);
1760 printf("got CAM status %#x\n",
1761 done_ccb->ccb_h.status);
1763 xpt_print_path(periph->path);
1764 printf("fatal error, failed"
1765 " to attach to device\n");
1768 * Invalidate this peripheral.
1770 cam_periph_invalidate(periph);
1772 announce_buf[0] = '\0';
1776 * Invalidate this peripheral.
1778 cam_periph_invalidate(periph);
1779 announce_buf[0] = '\0';
1783 free(rdcap, M_TEMP);
1784 if (announce_buf[0] != '\0') {
1785 xpt_announce_periph(periph, announce_buf);
1786 if (softc->flags & CD_FLAG_CHANGER)
1787 cdchangerschedule(softc);
1789 softc->state = CD_STATE_NORMAL;
1791 * Since our peripheral may be invalidated by an error
1792 * above or an external event, we must release our CCB
1793 * before releasing the probe lock on the peripheral.
1794 * The peripheral will only go away once the last lock
1795 * is removed, and we need it around for the CCB release
1798 xpt_release_ccb(done_ccb);
1799 cam_periph_unlock(periph);
1802 case CD_CCB_WAITING:
1804 /* Caller will release the CCB */
1805 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1806 ("trying to wakeup ccbwait\n"));
1808 wakeup(&done_ccb->ccb_h.cbfcnp);
1814 xpt_release_ccb(done_ccb);
1818 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1821 struct cam_periph *periph;
1822 struct cd_softc *softc;
1827 periph = cam_extend_get(cdperiphs, unit);
1831 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1833 softc = (struct cd_softc *)periph->softc;
1835 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1836 ("trying to do ioctl %#lx\n", cmd));
1838 error = cam_periph_lock(periph, PCATCH);
1845 case CDIOCPLAYTRACKS:
1847 struct ioc_play_track *args
1848 = (struct ioc_play_track *) addr;
1849 struct cd_mode_data *data;
1851 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1854 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1855 ("trying to do CDIOCPLAYTRACKS\n"));
1857 error = cdgetmode(periph, data, AUDIO_PAGE);
1862 data->page.audio.flags &= ~CD_PA_SOTC;
1863 data->page.audio.flags |= CD_PA_IMMED;
1864 error = cdsetmode(periph, data);
1868 if (softc->quirks & CD_Q_BCD_TRACKS) {
1869 args->start_track = bin2bcd(args->start_track);
1870 args->end_track = bin2bcd(args->end_track);
1872 error = cdplaytracks(periph,
1881 struct ioc_play_msf *args
1882 = (struct ioc_play_msf *) addr;
1883 struct cd_mode_data *data;
1885 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1888 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1889 ("trying to do CDIOCPLAYMSF\n"));
1891 error = cdgetmode(periph, data, AUDIO_PAGE);
1896 data->page.audio.flags &= ~CD_PA_SOTC;
1897 data->page.audio.flags |= CD_PA_IMMED;
1898 error = cdsetmode(periph, data);
1902 error = cdplaymsf(periph,
1911 case CDIOCPLAYBLOCKS:
1913 struct ioc_play_blocks *args
1914 = (struct ioc_play_blocks *) addr;
1915 struct cd_mode_data *data;
1917 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1918 ("trying to do CDIOCPLAYBLOCKS\n"));
1920 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1923 error = cdgetmode(periph, data, AUDIO_PAGE);
1928 data->page.audio.flags &= ~CD_PA_SOTC;
1929 data->page.audio.flags |= CD_PA_IMMED;
1930 error = cdsetmode(periph, data);
1934 error = cdplay(periph, args->blk, args->len);
1937 case CDIOCREADSUBCHANNEL:
1939 struct ioc_read_subchannel *args
1940 = (struct ioc_read_subchannel *) addr;
1941 struct cd_sub_channel_info *data;
1942 u_int32_t len = args->data_len;
1944 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1945 ("trying to do CDIOCREADSUBCHANNEL\n"));
1947 data = malloc(sizeof(struct cd_sub_channel_info),
1950 if ((len > sizeof(struct cd_sub_channel_info)) ||
1951 (len < sizeof(struct cd_sub_channel_header))) {
1953 "scsi_cd: cdioctl: "
1954 "cdioreadsubchannel: error, len=%d\n",
1961 if (softc->quirks & CD_Q_BCD_TRACKS)
1962 args->track = bin2bcd(args->track);
1964 error = cdreadsubchannel(periph, args->address_format,
1965 args->data_format, args->track, data, len);
1971 if (softc->quirks & CD_Q_BCD_TRACKS)
1972 data->what.track_info.track_number =
1973 bcd2bin(data->what.track_info.track_number);
1974 len = min(len, ((data->header.data_len[0] << 8) +
1975 data->header.data_len[1] +
1976 sizeof(struct cd_sub_channel_header)));
1977 if (copyout(data, args->data, len) != 0) {
1984 case CDIOREADTOCHEADER:
1986 struct ioc_toc_header *th;
1988 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1989 ("trying to do CDIOREADTOCHEADER\n"));
1991 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
1993 error = cdreadtoc(periph, 0, 0,
1994 (struct cd_toc_entry *)th,
2000 if (softc->quirks & CD_Q_BCD_TRACKS) {
2001 /* we are going to have to convert the BCD
2002 * encoding on the cd to what is expected
2004 th->starting_track =
2005 bcd2bin(th->starting_track);
2006 th->ending_track = bcd2bin(th->ending_track);
2009 bcopy(th, addr, sizeof(*th));
2013 case CDIOREADTOCENTRYS:
2016 struct ioc_toc_header header;
2017 struct cd_toc_entry entries[100];
2020 struct ioc_toc_header header;
2021 struct cd_toc_entry entry;
2026 struct ioc_read_toc_entry *te =
2027 (struct ioc_read_toc_entry *) addr;
2028 struct ioc_toc_header *th;
2029 u_int32_t len, readlen, idx, num;
2030 u_int32_t starting_track = te->starting_track;
2032 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2033 ("trying to do CDIOREADTOCENTRYS\n"));
2035 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2036 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK);
2038 if (te->data_len < sizeof(struct cd_toc_entry)
2039 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2040 || (te->address_format != CD_MSF_FORMAT
2041 && te->address_format != CD_LBA_FORMAT)) {
2043 printf("scsi_cd: error in readtocentries, "
2044 "returning EINVAL\n");
2051 error = cdreadtoc(periph, 0, 0,
2052 (struct cd_toc_entry *)th,
2060 if (softc->quirks & CD_Q_BCD_TRACKS) {
2061 /* we are going to have to convert the BCD
2062 * encoding on the cd to what is expected
2064 th->starting_track =
2065 bcd2bin(th->starting_track);
2066 th->ending_track = bcd2bin(th->ending_track);
2069 if (starting_track == 0)
2070 starting_track = th->starting_track;
2071 else if (starting_track == LEADOUT)
2072 starting_track = th->ending_track + 1;
2073 else if (starting_track < th->starting_track ||
2074 starting_track > th->ending_track + 1) {
2075 printf("scsi_cd: error in readtocentries, "
2076 "returning EINVAL\n");
2083 /* calculate reading length without leadout entry */
2084 readlen = (th->ending_track - starting_track + 1) *
2085 sizeof(struct cd_toc_entry);
2087 /* and with leadout entry */
2088 len = readlen + sizeof(struct cd_toc_entry);
2089 if (te->data_len < len) {
2094 if (len > sizeof(data->entries)) {
2095 printf("scsi_cd: error in readtocentries, "
2096 "returning EINVAL\n");
2102 num = len / sizeof(struct cd_toc_entry);
2105 error = cdreadtoc(periph, te->address_format,
2107 (struct cd_toc_entry *)data,
2108 readlen + sizeof (*th));
2116 /* make leadout entry if needed */
2117 idx = starting_track + num - 1;
2118 if (softc->quirks & CD_Q_BCD_TRACKS)
2119 th->ending_track = bcd2bin(th->ending_track);
2120 if (idx == th->ending_track + 1) {
2121 error = cdreadtoc(periph, te->address_format,
2123 (struct cd_toc_entry *)lead,
2130 data->entries[idx - starting_track] =
2133 if (softc->quirks & CD_Q_BCD_TRACKS) {
2134 for (idx = 0; idx < num - 1; idx++) {
2135 data->entries[idx].track =
2136 bcd2bin(data->entries[idx].track);
2140 error = copyout(data->entries, te->data, len);
2145 case CDIOREADTOCENTRY:
2147 /* yeah yeah, this is ugly */
2149 struct ioc_toc_header header;
2150 struct cd_toc_entry entry;
2154 struct ioc_read_toc_single_entry *te =
2155 (struct ioc_read_toc_single_entry *) addr;
2156 struct ioc_toc_header *th;
2159 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2160 ("trying to do CDIOREADTOCENTRY\n"));
2162 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2164 if (te->address_format != CD_MSF_FORMAT
2165 && te->address_format != CD_LBA_FORMAT) {
2166 printf("error in readtocentry, "
2167 " returning EINVAL\n");
2174 error = cdreadtoc(periph, 0, 0,
2175 (struct cd_toc_entry *)th,
2182 if (softc->quirks & CD_Q_BCD_TRACKS) {
2183 /* we are going to have to convert the BCD
2184 * encoding on the cd to what is expected
2186 th->starting_track =
2187 bcd2bin(th->starting_track);
2188 th->ending_track = bcd2bin(th->ending_track);
2192 track = th->starting_track;
2193 else if (track == LEADOUT)
2195 else if (track < th->starting_track ||
2196 track > th->ending_track + 1) {
2197 printf("error in readtocentry, "
2198 " returning EINVAL\n");
2204 error = cdreadtoc(periph, te->address_format, track,
2205 (struct cd_toc_entry *)data,
2212 if (softc->quirks & CD_Q_BCD_TRACKS)
2213 data->entry.track = bcd2bin(data->entry.track);
2214 bcopy(&data->entry, &te->entry,
2215 sizeof(struct cd_toc_entry));
2221 struct ioc_patch *arg = (struct ioc_patch *) addr;
2222 struct cd_mode_data *data;
2224 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2225 ("trying to do CDIOCSETPATCH\n"));
2227 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2229 error = cdgetmode(periph, data, AUDIO_PAGE);
2234 data->page.audio.port[LEFT_PORT].channels =
2236 data->page.audio.port[RIGHT_PORT].channels =
2238 data->page.audio.port[2].channels = arg->patch[2];
2239 data->page.audio.port[3].channels = arg->patch[3];
2240 error = cdsetmode(periph, data);
2246 struct ioc_vol *arg = (struct ioc_vol *) addr;
2247 struct cd_mode_data *data;
2249 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2250 ("trying to do CDIOCGETVOL\n"));
2252 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2254 error = cdgetmode(periph, data, AUDIO_PAGE);
2259 arg->vol[LEFT_PORT] =
2260 data->page.audio.port[LEFT_PORT].volume;
2261 arg->vol[RIGHT_PORT] =
2262 data->page.audio.port[RIGHT_PORT].volume;
2263 arg->vol[2] = data->page.audio.port[2].volume;
2264 arg->vol[3] = data->page.audio.port[3].volume;
2270 struct ioc_vol *arg = (struct ioc_vol *) addr;
2271 struct cd_mode_data *data;
2273 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2274 ("trying to do CDIOCSETVOL\n"));
2276 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2278 error = cdgetmode(periph, data, AUDIO_PAGE);
2283 data->page.audio.port[LEFT_PORT].channels = CHANNEL_0;
2284 data->page.audio.port[LEFT_PORT].volume =
2285 arg->vol[LEFT_PORT];
2286 data->page.audio.port[RIGHT_PORT].channels = CHANNEL_1;
2287 data->page.audio.port[RIGHT_PORT].volume =
2288 arg->vol[RIGHT_PORT];
2289 data->page.audio.port[2].volume = arg->vol[2];
2290 data->page.audio.port[3].volume = arg->vol[3];
2291 error = cdsetmode(periph, data);
2297 struct cd_mode_data *data;
2299 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2300 ("trying to do CDIOCSETMONO\n"));
2302 data = malloc(sizeof(struct cd_mode_data),
2304 error = cdgetmode(periph, data, AUDIO_PAGE);
2309 data->page.audio.port[LEFT_PORT].channels =
2310 LEFT_CHANNEL | RIGHT_CHANNEL;
2311 data->page.audio.port[RIGHT_PORT].channels =
2312 LEFT_CHANNEL | RIGHT_CHANNEL;
2313 data->page.audio.port[2].channels = 0;
2314 data->page.audio.port[3].channels = 0;
2315 error = cdsetmode(periph, data);
2319 case CDIOCSETSTEREO:
2321 struct cd_mode_data *data;
2323 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2324 ("trying to do CDIOCSETSTEREO\n"));
2326 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2328 error = cdgetmode(periph, data, AUDIO_PAGE);
2333 data->page.audio.port[LEFT_PORT].channels =
2335 data->page.audio.port[RIGHT_PORT].channels =
2337 data->page.audio.port[2].channels = 0;
2338 data->page.audio.port[3].channels = 0;
2339 error = cdsetmode(periph, data);
2345 struct cd_mode_data *data;
2347 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2348 ("trying to do CDIOCSETMUTE\n"));
2350 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2352 error = cdgetmode(periph, data, AUDIO_PAGE);
2357 data->page.audio.port[LEFT_PORT].channels = 0;
2358 data->page.audio.port[RIGHT_PORT].channels = 0;
2359 data->page.audio.port[2].channels = 0;
2360 data->page.audio.port[3].channels = 0;
2361 error = cdsetmode(periph, data);
2367 struct cd_mode_data *data;
2369 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2370 ("trying to do CDIOCSETLEFT\n"));
2372 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2374 error = cdgetmode(periph, data, AUDIO_PAGE);
2379 data->page.audio.port[LEFT_PORT].channels =
2381 data->page.audio.port[RIGHT_PORT].channels =
2383 data->page.audio.port[2].channels = 0;
2384 data->page.audio.port[3].channels = 0;
2385 error = cdsetmode(periph, data);
2391 struct cd_mode_data *data;
2393 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2394 ("trying to do CDIOCSETRIGHT\n"));
2396 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2398 error = cdgetmode(periph, data, AUDIO_PAGE);
2403 data->page.audio.port[LEFT_PORT].channels =
2405 data->page.audio.port[RIGHT_PORT].channels =
2407 data->page.audio.port[2].channels = 0;
2408 data->page.audio.port[3].channels = 0;
2409 error = cdsetmode(periph, data);
2414 error = cdpause(periph, 1);
2417 error = cdpause(periph, 0);
2420 error = cdstartunit(periph);
2423 error = cdstopunit(periph, 0);
2426 error = cdstopunit(periph, 1);
2429 cdprevent(periph, PR_ALLOW);
2432 cdprevent(periph, PR_PREVENT);
2435 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2439 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2443 /* return (cd_reset(periph)); */
2446 case CDRIOCREADSPEED:
2447 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2449 case CDRIOCWRITESPEED:
2450 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2453 case DVDIOCREPORTKEY: {
2454 struct dvd_authinfo *authinfo;
2456 authinfo = (struct dvd_authinfo *)addr;
2458 if (cmd == DVDIOCREPORTKEY)
2459 error = cdreportkey(periph, authinfo);
2461 error = cdsendkey(periph, authinfo);
2464 case DVDIOCREADSTRUCTURE: {
2465 struct dvd_struct *dvdstruct;
2467 dvdstruct = (struct dvd_struct *)addr;
2469 error = cdreaddvdstructure(periph, dvdstruct);
2474 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2478 cam_periph_unlock(periph);
2480 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2486 cdprevent(struct cam_periph *periph, int action)
2489 struct cd_softc *softc;
2492 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2494 softc = (struct cd_softc *)periph->softc;
2496 if (((action == PR_ALLOW)
2497 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2498 || ((action == PR_PREVENT)
2499 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2503 ccb = cdgetccb(periph, /* priority */ 1);
2505 scsi_prevent(&ccb->csio,
2511 /* timeout */60000);
2513 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2514 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2516 xpt_release_ccb(ccb);
2519 if (action == PR_ALLOW)
2520 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2522 softc->flags |= CD_FLAG_DISC_LOCKED;
2527 cdsize(dev_t dev, u_int32_t *size)
2529 struct cam_periph *periph;
2530 struct cd_softc *softc;
2532 struct scsi_read_capacity_data *rcap_buf;
2535 periph = cam_extend_get(cdperiphs, dkunit(dev));
2540 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2542 softc = (struct cd_softc *)periph->softc;
2544 ccb = cdgetccb(periph, /* priority */ 1);
2546 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2549 scsi_read_capacity(&ccb->csio,
2555 /* timeout */20000);
2557 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2558 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2560 xpt_release_ccb(ccb);
2562 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2563 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2565 * SCSI-3 mandates that the reported blocksize shall be 2048.
2566 * Older drives sometimes report funny values, trim it down to
2567 * 2048, or other parts of the kernel will get confused.
2569 * XXX we leave drives alone that might report 512 bytes, as
2570 * well as drives reporting more weird sizes like perhaps 4K.
2572 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2573 softc->params.blksize = 2048;
2575 free(rcap_buf, M_TEMP);
2576 *size = softc->params.disksize;
2583 * The idea here is to try to figure out whether the first track is data or
2584 * audio. If it is data, we can at least attempt to read a disklabel off
2585 * the first sector of the disk. If it is audio, there won't be a
2588 * This routine returns 0 if the first track is data, and non-zero if there
2589 * is an error or the first track is audio. (If either non-zero case, we
2590 * should not attempt to read the disklabel.)
2593 cdfirsttrackisdata(struct cam_periph *periph)
2596 struct ioc_toc_header header;
2597 struct cd_toc_entry entries[100];
2599 struct cd_softc *softc;
2600 struct ioc_toc_header *th;
2601 struct cdtocdata *data;
2603 int error, first_track_audio;
2606 first_track_audio = -1;
2608 softc = (struct cd_softc *)periph->softc;
2610 data = malloc(sizeof(struct cdtocdata), M_TEMP, M_WAITOK);
2613 error = cdreadtoc(periph, 0, 0, (struct cd_toc_entry *)data,
2619 if (softc->quirks & CD_Q_BCD_TRACKS) {
2620 /* we are going to have to convert the BCD
2621 * encoding on the cd to what is expected
2623 th->starting_track =
2624 bcd2bin(th->starting_track);
2625 th->ending_track = bcd2bin(th->ending_track);
2627 th->len = scsi_2btoul((u_int8_t *)&th->len);
2629 if ((th->len - 2) > 0)
2630 num_entries = (th->len - 2) / sizeof(struct cd_toc_entry);
2634 for (i = 0; i < num_entries; i++) {
2635 if (data->entries[i].track == th->starting_track) {
2636 if (data->entries[i].control & 0x4)
2637 first_track_audio = 0;
2639 first_track_audio = 1;
2644 if (first_track_audio == -1)
2646 else if (first_track_audio == 1)
2657 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2659 struct cd_softc *softc;
2660 struct cam_periph *periph;
2662 periph = xpt_path_periph(ccb->ccb_h.path);
2663 softc = (struct cd_softc *)periph->softc;
2667 * Until we have a better way of doing pack validation,
2668 * don't treat UAs as errors.
2670 sense_flags |= SF_RETRY_UA;
2671 return (cam_periph_error(ccb, cam_flags, sense_flags,
2672 &softc->saved_ccb));
2676 * Read table of contents
2679 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2680 struct cd_toc_entry *data, u_int32_t len)
2682 struct scsi_read_toc *scsi_cmd;
2684 struct ccb_scsiio *csio;
2691 ccb = cdgetccb(periph, /* priority */ 1);
2697 /* cbfcnp */ cddone,
2698 /* flags */ CAM_DIR_IN,
2699 /* tag_action */ MSG_SIMPLE_Q_TAG,
2700 /* data_ptr */ (u_int8_t *)data,
2701 /* dxfer_len */ len,
2702 /* sense_len */ SSD_FULL_SIZE,
2703 sizeof(struct scsi_read_toc),
2704 /* timeout */ 50000);
2706 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2707 bzero (scsi_cmd, sizeof(*scsi_cmd));
2709 if (mode == CD_MSF_FORMAT)
2710 scsi_cmd->byte2 |= CD_MSF;
2711 scsi_cmd->from_track = start;
2712 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
2713 scsi_cmd->data_len[0] = (ntoc) >> 8;
2714 scsi_cmd->data_len[1] = (ntoc) & 0xff;
2716 scsi_cmd->op_code = READ_TOC;
2718 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2719 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2721 xpt_release_ccb(ccb);
2727 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
2728 u_int32_t format, int track,
2729 struct cd_sub_channel_info *data, u_int32_t len)
2731 struct scsi_read_subchannel *scsi_cmd;
2732 struct ccb_scsiio *csio;
2738 ccb = cdgetccb(periph, /* priority */ 1);
2744 /* cbfcnp */ cddone,
2745 /* flags */ CAM_DIR_IN,
2746 /* tag_action */ MSG_SIMPLE_Q_TAG,
2747 /* data_ptr */ (u_int8_t *)data,
2748 /* dxfer_len */ len,
2749 /* sense_len */ SSD_FULL_SIZE,
2750 sizeof(struct scsi_read_subchannel),
2751 /* timeout */ 50000);
2753 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2754 bzero (scsi_cmd, sizeof(*scsi_cmd));
2756 scsi_cmd->op_code = READ_SUBCHANNEL;
2757 if (mode == CD_MSF_FORMAT)
2758 scsi_cmd->byte1 |= CD_MSF;
2759 scsi_cmd->byte2 = SRS_SUBQ;
2760 scsi_cmd->subchan_format = format;
2761 scsi_cmd->track = track;
2762 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
2763 scsi_cmd->control = 0;
2765 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2766 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2768 xpt_release_ccb(ccb);
2775 cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page)
2777 struct scsi_mode_sense_6 *scsi_cmd;
2778 struct ccb_scsiio *csio;
2782 ccb = cdgetccb(periph, /* priority */ 1);
2786 bzero(data, sizeof(*data));
2789 /* cbfcnp */ cddone,
2790 /* flags */ CAM_DIR_IN,
2791 /* tag_action */ MSG_SIMPLE_Q_TAG,
2792 /* data_ptr */ (u_int8_t *)data,
2793 /* dxfer_len */ sizeof(*data),
2794 /* sense_len */ SSD_FULL_SIZE,
2795 sizeof(struct scsi_mode_sense_6),
2796 /* timeout */ 50000);
2798 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
2799 bzero (scsi_cmd, sizeof(*scsi_cmd));
2801 scsi_cmd->page = page;
2802 scsi_cmd->length = sizeof(*data) & 0xff;
2803 scsi_cmd->opcode = MODE_SENSE;
2805 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2806 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2808 xpt_release_ccb(ccb);
2814 cdsetmode(struct cam_periph *periph, struct cd_mode_data *data)
2816 struct scsi_mode_select_6 *scsi_cmd;
2817 struct ccb_scsiio *csio;
2821 ccb = cdgetccb(periph, /* priority */ 1);
2829 /* cbfcnp */ cddone,
2830 /* flags */ CAM_DIR_OUT,
2831 /* tag_action */ MSG_SIMPLE_Q_TAG,
2832 /* data_ptr */ (u_int8_t *)data,
2833 /* dxfer_len */ sizeof(*data),
2834 /* sense_len */ SSD_FULL_SIZE,
2835 sizeof(struct scsi_mode_select_6),
2836 /* timeout */ 50000);
2838 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
2840 bzero(scsi_cmd, sizeof(*scsi_cmd));
2841 scsi_cmd->opcode = MODE_SELECT;
2842 scsi_cmd->byte2 |= SMS_PF;
2843 scsi_cmd->length = sizeof(*data) & 0xff;
2844 data->header.data_length = 0;
2846 * SONY drives do not allow a mode select with a medium_type
2847 * value that has just been returned by a mode sense; use a
2848 * medium_type of 0 (Default) instead.
2850 data->header.medium_type = 0;
2852 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2853 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2855 xpt_release_ccb(ccb);
2862 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2864 struct ccb_scsiio *csio;
2870 ccb = cdgetccb(periph, /* priority */ 1);
2873 * Use the smallest possible command to perform the operation.
2875 if ((len & 0xffff0000) == 0) {
2877 * We can fit in a 10 byte cdb.
2879 struct scsi_play_10 *scsi_cmd;
2881 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
2882 bzero (scsi_cmd, sizeof(*scsi_cmd));
2883 scsi_cmd->op_code = PLAY_10;
2884 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2885 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
2886 cdb_len = sizeof(*scsi_cmd);
2888 struct scsi_play_12 *scsi_cmd;
2890 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
2891 bzero (scsi_cmd, sizeof(*scsi_cmd));
2892 scsi_cmd->op_code = PLAY_12;
2893 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2894 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
2895 cdb_len = sizeof(*scsi_cmd);
2900 /*flags*/CAM_DIR_NONE,
2904 /*sense_len*/SSD_FULL_SIZE,
2906 /*timeout*/50 * 1000);
2908 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2909 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2911 xpt_release_ccb(ccb);
2917 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
2918 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
2920 struct scsi_play_msf *scsi_cmd;
2921 struct ccb_scsiio *csio;
2927 ccb = cdgetccb(periph, /* priority */ 1);
2933 /* cbfcnp */ cddone,
2934 /* flags */ CAM_DIR_NONE,
2935 /* tag_action */ MSG_SIMPLE_Q_TAG,
2936 /* data_ptr */ NULL,
2938 /* sense_len */ SSD_FULL_SIZE,
2939 sizeof(struct scsi_play_msf),
2940 /* timeout */ 50000);
2942 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2943 bzero (scsi_cmd, sizeof(*scsi_cmd));
2945 scsi_cmd->op_code = PLAY_MSF;
2946 scsi_cmd->start_m = startm;
2947 scsi_cmd->start_s = starts;
2948 scsi_cmd->start_f = startf;
2949 scsi_cmd->end_m = endm;
2950 scsi_cmd->end_s = ends;
2951 scsi_cmd->end_f = endf;
2953 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2954 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2956 xpt_release_ccb(ccb);
2963 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
2964 u_int32_t etrack, u_int32_t eindex)
2966 struct scsi_play_track *scsi_cmd;
2967 struct ccb_scsiio *csio;
2973 ccb = cdgetccb(periph, /* priority */ 1);
2979 /* cbfcnp */ cddone,
2980 /* flags */ CAM_DIR_NONE,
2981 /* tag_action */ MSG_SIMPLE_Q_TAG,
2982 /* data_ptr */ NULL,
2984 /* sense_len */ SSD_FULL_SIZE,
2985 sizeof(struct scsi_play_track),
2986 /* timeout */ 50000);
2988 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
2989 bzero (scsi_cmd, sizeof(*scsi_cmd));
2991 scsi_cmd->op_code = PLAY_TRACK;
2992 scsi_cmd->start_track = strack;
2993 scsi_cmd->start_index = sindex;
2994 scsi_cmd->end_track = etrack;
2995 scsi_cmd->end_index = eindex;
2997 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2998 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3000 xpt_release_ccb(ccb);
3006 cdpause(struct cam_periph *periph, u_int32_t go)
3008 struct scsi_pause *scsi_cmd;
3009 struct ccb_scsiio *csio;
3015 ccb = cdgetccb(periph, /* priority */ 1);
3021 /* cbfcnp */ cddone,
3022 /* flags */ CAM_DIR_NONE,
3023 /* tag_action */ MSG_SIMPLE_Q_TAG,
3024 /* data_ptr */ NULL,
3026 /* sense_len */ SSD_FULL_SIZE,
3027 sizeof(struct scsi_pause),
3028 /* timeout */ 50000);
3030 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3031 bzero (scsi_cmd, sizeof(*scsi_cmd));
3033 scsi_cmd->op_code = PAUSE;
3034 scsi_cmd->resume = go;
3036 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3037 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3039 xpt_release_ccb(ccb);
3045 cdstartunit(struct cam_periph *periph)
3052 ccb = cdgetccb(periph, /* priority */ 1);
3054 scsi_start_stop(&ccb->csio,
3056 /* cbfcnp */ cddone,
3057 /* tag_action */ MSG_SIMPLE_Q_TAG,
3059 /* load_eject */ FALSE,
3060 /* immediate */ FALSE,
3061 /* sense_len */ SSD_FULL_SIZE,
3062 /* timeout */ 50000);
3064 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3065 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3067 xpt_release_ccb(ccb);
3073 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3080 ccb = cdgetccb(periph, /* priority */ 1);
3082 scsi_start_stop(&ccb->csio,
3084 /* cbfcnp */ cddone,
3085 /* tag_action */ MSG_SIMPLE_Q_TAG,
3087 /* load_eject */ eject,
3088 /* immediate */ FALSE,
3089 /* sense_len */ SSD_FULL_SIZE,
3090 /* timeout */ 50000);
3092 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3093 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3095 xpt_release_ccb(ccb);
3101 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3103 struct scsi_set_speed *scsi_cmd;
3104 struct ccb_scsiio *csio;
3109 ccb = cdgetccb(periph, /* priority */ 1);
3112 /* Preserve old behavior: units in multiples of CDROM speed */
3120 /* cbfcnp */ cddone,
3121 /* flags */ CAM_DIR_NONE,
3122 /* tag_action */ MSG_SIMPLE_Q_TAG,
3123 /* data_ptr */ NULL,
3125 /* sense_len */ SSD_FULL_SIZE,
3126 sizeof(struct scsi_set_speed),
3127 /* timeout */ 50000);
3129 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3130 bzero(scsi_cmd, sizeof(*scsi_cmd));
3132 scsi_cmd->opcode = SET_CD_SPEED;
3133 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3134 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3136 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3137 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3139 xpt_release_ccb(ccb);
3145 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3157 ccb = cdgetccb(periph, /* priority */ 1);
3159 switch (authinfo->format) {
3160 case DVD_REPORT_AGID:
3161 length = sizeof(struct scsi_report_key_data_agid);
3163 case DVD_REPORT_CHALLENGE:
3164 length = sizeof(struct scsi_report_key_data_challenge);
3166 case DVD_REPORT_KEY1:
3167 length = sizeof(struct scsi_report_key_data_key1_key2);
3169 case DVD_REPORT_TITLE_KEY:
3170 length = sizeof(struct scsi_report_key_data_title);
3171 /* The lba field is only set for the title key */
3172 lba = authinfo->lba;
3174 case DVD_REPORT_ASF:
3175 length = sizeof(struct scsi_report_key_data_asf);
3177 case DVD_REPORT_RPC:
3178 length = sizeof(struct scsi_report_key_data_rpc);
3180 case DVD_INVALIDATE_AGID:
3186 break; /* NOTREACHED */
3190 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3195 scsi_report_key(&ccb->csio,
3197 /* cbfcnp */ cddone,
3198 /* tag_action */ MSG_SIMPLE_Q_TAG,
3200 /* agid */ authinfo->agid,
3201 /* key_format */ authinfo->format,
3202 /* data_ptr */ databuf,
3203 /* dxfer_len */ length,
3204 /* sense_len */ SSD_FULL_SIZE,
3205 /* timeout */ 50000);
3207 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3208 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3213 if (ccb->csio.resid != 0) {
3214 xpt_print_path(periph->path);
3215 printf("warning, residual for report key command is %d\n",
3219 switch(authinfo->format) {
3220 case DVD_REPORT_AGID: {
3221 struct scsi_report_key_data_agid *agid_data;
3223 agid_data = (struct scsi_report_key_data_agid *)databuf;
3225 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3229 case DVD_REPORT_CHALLENGE: {
3230 struct scsi_report_key_data_challenge *chal_data;
3232 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3234 bcopy(chal_data->challenge_key, authinfo->keychal,
3235 min(sizeof(chal_data->challenge_key),
3236 sizeof(authinfo->keychal)));
3239 case DVD_REPORT_KEY1: {
3240 struct scsi_report_key_data_key1_key2 *key1_data;
3242 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3244 bcopy(key1_data->key1, authinfo->keychal,
3245 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3248 case DVD_REPORT_TITLE_KEY: {
3249 struct scsi_report_key_data_title *title_data;
3251 title_data = (struct scsi_report_key_data_title *)databuf;
3253 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3254 RKD_TITLE_CPM_SHIFT;
3255 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3256 RKD_TITLE_CP_SEC_SHIFT;
3257 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3258 RKD_TITLE_CMGS_SHIFT;
3259 bcopy(title_data->title_key, authinfo->keychal,
3260 min(sizeof(title_data->title_key),
3261 sizeof(authinfo->keychal)));
3264 case DVD_REPORT_ASF: {
3265 struct scsi_report_key_data_asf *asf_data;
3267 asf_data = (struct scsi_report_key_data_asf *)databuf;
3269 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3272 case DVD_REPORT_RPC: {
3273 struct scsi_report_key_data_rpc *rpc_data;
3275 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3277 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3279 authinfo->vend_rsts =
3280 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3281 RKD_RPC_VENDOR_RESET_SHIFT;
3282 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3283 authinfo->region = rpc_data->region_mask;
3284 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3287 case DVD_INVALIDATE_AGID:
3290 /* This should be impossible, since we checked above */
3293 break; /* NOTREACHED */
3296 if (databuf != NULL)
3297 free(databuf, M_DEVBUF);
3299 xpt_release_ccb(ccb);
3305 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3315 ccb = cdgetccb(periph, /* priority */ 1);
3317 switch(authinfo->format) {
3318 case DVD_SEND_CHALLENGE: {
3319 struct scsi_report_key_data_challenge *challenge_data;
3321 length = sizeof(*challenge_data);
3323 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3325 databuf = (u_int8_t *)challenge_data;
3327 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3328 challenge_data->data_len);
3330 bcopy(authinfo->keychal, challenge_data->challenge_key,
3331 min(sizeof(authinfo->keychal),
3332 sizeof(challenge_data->challenge_key)));
3335 case DVD_SEND_KEY2: {
3336 struct scsi_report_key_data_key1_key2 *key2_data;
3338 length = sizeof(*key2_data);
3340 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3342 databuf = (u_int8_t *)key2_data;
3344 scsi_ulto2b(length - sizeof(key2_data->data_len),
3345 key2_data->data_len);
3347 bcopy(authinfo->keychal, key2_data->key1,
3348 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3352 case DVD_SEND_RPC: {
3353 struct scsi_send_key_data_rpc *rpc_data;
3355 length = sizeof(*rpc_data);
3357 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3359 databuf = (u_int8_t *)rpc_data;
3361 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3362 rpc_data->data_len);
3364 rpc_data->region_code = authinfo->region;
3370 break; /* NOTREACHED */
3373 scsi_send_key(&ccb->csio,
3375 /* cbfcnp */ cddone,
3376 /* tag_action */ MSG_SIMPLE_Q_TAG,
3377 /* agid */ authinfo->agid,
3378 /* key_format */ authinfo->format,
3379 /* data_ptr */ databuf,
3380 /* dxfer_len */ length,
3381 /* sense_len */ SSD_FULL_SIZE,
3382 /* timeout */ 50000);
3384 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3385 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3389 if (databuf != NULL)
3390 free(databuf, M_DEVBUF);
3392 xpt_release_ccb(ccb);
3398 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3408 /* The address is reserved for many of the formats */
3411 ccb = cdgetccb(periph, /* priority */ 1);
3413 switch(dvdstruct->format) {
3414 case DVD_STRUCT_PHYSICAL:
3415 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3417 case DVD_STRUCT_COPYRIGHT:
3418 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3420 case DVD_STRUCT_DISCKEY:
3421 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3423 case DVD_STRUCT_BCA:
3424 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3426 case DVD_STRUCT_MANUFACT:
3427 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3429 case DVD_STRUCT_CMI:
3433 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
3434 address = dvdstruct->address;
3436 break; /* NOTREACHED */
3437 case DVD_STRUCT_PROTDISCID:
3438 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3440 case DVD_STRUCT_DISCKEYBLOCK:
3441 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3443 case DVD_STRUCT_DDS:
3444 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3446 case DVD_STRUCT_MEDIUM_STAT:
3447 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3449 case DVD_STRUCT_SPARE_AREA:
3450 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3452 case DVD_STRUCT_RMD_LAST:
3456 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
3457 address = dvdstruct->address;
3459 break; /* NOTREACHED */
3460 case DVD_STRUCT_RMD_RMA:
3464 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
3465 address = dvdstruct->address;
3467 break; /* NOTREACHED */
3468 case DVD_STRUCT_PRERECORDED:
3469 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3471 case DVD_STRUCT_UNIQUEID:
3472 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3474 case DVD_STRUCT_DCB:
3478 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
3479 address = dvdstruct->address;
3481 break; /* NOTREACHED */
3482 case DVD_STRUCT_LIST:
3484 * This is the maximum allocation length for the READ DVD
3485 * STRUCTURE command. There's nothing in the MMC3 spec
3486 * that indicates a limit in the amount of data that can
3487 * be returned from this call, other than the limits
3488 * imposed by the 2-byte length variables.
3495 break; /* NOTREACHED */
3499 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3503 scsi_read_dvd_structure(&ccb->csio,
3505 /* cbfcnp */ cddone,
3506 /* tag_action */ MSG_SIMPLE_Q_TAG,
3508 /* layer_number */ dvdstruct->layer_num,
3509 /* key_format */ dvdstruct->format,
3510 /* agid */ dvdstruct->agid,
3511 /* data_ptr */ databuf,
3512 /* dxfer_len */ length,
3513 /* sense_len */ SSD_FULL_SIZE,
3514 /* timeout */ 50000);
3516 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3517 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3522 switch(dvdstruct->format) {
3523 case DVD_STRUCT_PHYSICAL: {
3524 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
3525 struct dvd_layer *outlayer;
3526 struct scsi_read_dvd_struct_data_physical *phys_data;
3529 (struct scsi_read_dvd_struct_data_physical *)databuf;
3530 inlayer = &phys_data->layer_desc;
3531 outlayer = (struct dvd_layer *)&dvdstruct->data;
3533 dvdstruct->length = sizeof(*inlayer);
3535 outlayer->book_type = (inlayer->book_type_version &
3536 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
3537 outlayer->book_version = (inlayer->book_type_version &
3538 RDSD_BOOK_VERSION_MASK);
3539 outlayer->disc_size = (inlayer->disc_size_max_rate &
3540 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
3541 outlayer->max_rate = (inlayer->disc_size_max_rate &
3542 RDSD_MAX_RATE_MASK);
3543 outlayer->nlayers = (inlayer->layer_info &
3544 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
3545 outlayer->track_path = (inlayer->layer_info &
3546 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
3547 outlayer->layer_type = (inlayer->layer_info &
3548 RDSD_LAYER_TYPE_MASK);
3549 outlayer->linear_density = (inlayer->density &
3550 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
3551 outlayer->track_density = (inlayer->density &
3552 RDSD_TRACK_DENSITY_MASK);
3553 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
3555 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
3556 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
3557 outlayer->end_sector_l0 =
3558 scsi_3btoul(inlayer->end_sector_layer0);
3561 case DVD_STRUCT_COPYRIGHT: {
3562 struct scsi_read_dvd_struct_data_copyright *copy_data;
3564 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3567 dvdstruct->cpst = copy_data->cps_type;
3568 dvdstruct->rmi = copy_data->region_info;
3569 dvdstruct->length = 0;
3575 * Tell the user what the overall length is, no matter
3576 * what we can actually fit in the data buffer.
3578 dvdstruct->length = length - ccb->csio.resid -
3579 sizeof(struct scsi_read_dvd_struct_data_header);
3582 * But only actually copy out the smaller of what we read
3583 * in or what the structure can take.
3585 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3587 min(sizeof(dvdstruct->data), dvdstruct->length));
3592 if (databuf != NULL)
3593 free(databuf, M_DEVBUF);
3595 xpt_release_ccb(ccb);
3601 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
3602 void (*cbfcnp)(struct cam_periph *, union ccb *),
3603 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
3604 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
3605 u_int8_t sense_len, u_int32_t timeout)
3607 struct scsi_report_key *scsi_cmd;
3609 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
3610 bzero(scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->opcode = REPORT_KEY;
3612 scsi_ulto4b(lba, scsi_cmd->lba);
3613 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3614 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3615 (key_format & RK_KF_KEYFORMAT_MASK);
3620 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3622 /*data_ptr*/ data_ptr,
3623 /*dxfer_len*/ dxfer_len,
3630 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
3631 void (*cbfcnp)(struct cam_periph *, union ccb *),
3632 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
3633 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3636 struct scsi_send_key *scsi_cmd;
3638 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
3639 bzero(scsi_cmd, sizeof(*scsi_cmd));
3640 scsi_cmd->opcode = SEND_KEY;
3642 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
3643 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3644 (key_format & RK_KF_KEYFORMAT_MASK);
3649 /*flags*/ CAM_DIR_OUT,
3651 /*data_ptr*/ data_ptr,
3652 /*dxfer_len*/ dxfer_len,
3660 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
3661 void (*cbfcnp)(struct cam_periph *, union ccb *),
3662 u_int8_t tag_action, u_int32_t address,
3663 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
3664 u_int8_t *data_ptr, u_int32_t dxfer_len,
3665 u_int8_t sense_len, u_int32_t timeout)
3667 struct scsi_read_dvd_structure *scsi_cmd;
3669 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
3670 bzero(scsi_cmd, sizeof(*scsi_cmd));
3671 scsi_cmd->opcode = READ_DVD_STRUCTURE;
3673 scsi_ulto4b(address, scsi_cmd->address);
3674 scsi_cmd->layer_number = layer_number;
3675 scsi_cmd->format = format;
3676 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3677 /* The AGID is the top two bits of this byte */
3678 scsi_cmd->agid = agid << 6;
3683 /*flags*/ CAM_DIR_IN,
3685 /*data_ptr*/ data_ptr,
3686 /*dxfer_len*/ dxfer_len,