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 $
30 * Portions of this driver taken from the original FreeBSD cd driver.
31 * Written by Julian Elischer (julian@tfs.com)
32 * for TRW Financial Systems for use under the MACH(2.5) operating system.
34 * TRW Financial Systems, in accordance with their agreement with Carnegie
35 * Mellon University, makes this software available to CMU to distribute
36 * or use in any manner that they see fit as long as this message is kept with
37 * the software. For this reason TFS also grants any other persons or
38 * organisations permission to use or modify this software.
40 * TFS supplies this software to be publicly redistributed
41 * on the understanding that TFS is not responsible for the correct
42 * functioning of this software in any circumstances.
44 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
46 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/cdrio.h>
60 #include <sys/dvdio.h>
61 #include <sys/devicestat.h>
62 #include <sys/sysctl.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_extend.h>
67 #include <cam/cam_periph.h>
68 #include <cam/cam_xpt_periph.h>
69 #include <cam/cam_queue.h>
71 #include <cam/scsi/scsi_message.h>
72 #include <cam/scsi/scsi_da.h>
73 #include <cam/scsi/scsi_cd.h>
75 #define LEADOUT 0xaa /* leadout toc entry */
85 CD_Q_BCD_TRACKS = 0x02,
86 CD_Q_NO_CHANGER = 0x04,
91 CD_FLAG_INVALID = 0x001,
92 CD_FLAG_NEW_DISC = 0x002,
93 CD_FLAG_DISC_LOCKED = 0x004,
94 CD_FLAG_DISC_REMOVABLE = 0x008,
95 CD_FLAG_TAGGED_QUEUING = 0x010,
96 CD_FLAG_CHANGER = 0x040,
97 CD_FLAG_ACTIVE = 0x080,
98 CD_FLAG_SCHED_ON_COMP = 0x100,
99 CD_FLAG_RETRY_UA = 0x200
104 CD_CCB_BUFFER_IO = 0x02,
105 CD_CCB_WAITING = 0x03,
106 CD_CCB_TYPE_MASK = 0x0F,
107 CD_CCB_RETRY_UA = 0x10
111 CHANGER_TIMEOUT_SCHED = 0x01,
112 CHANGER_SHORT_TMOUT_SCHED = 0x02,
113 CHANGER_MANUAL_CALL = 0x04,
114 CHANGER_NEED_TIMEOUT = 0x08
117 #define ccb_state ppriv_field0
118 #define ccb_bp ppriv_ptr1
128 volatile cd_flags flags;
129 struct buf_queue_head buf_queue;
130 LIST_HEAD(, ccb_hdr) pending_ccbs;
131 struct cd_params params;
135 struct devstat device_stats;
136 STAILQ_ENTRY(cd_softc) changer_links;
137 struct cdchanger *changer;
139 struct cam_periph *periph;
142 struct cd_quirk_entry {
143 struct scsi_inquiry_pattern inq_pat;
148 * These quirk entries aren't strictly necessary. Basically, what they do
149 * is tell cdregister() up front that a device is a changer. Otherwise, it
150 * will figure that fact out once it sees a LUN on the device that is
151 * greater than 0. If it is known up front that a device is a changer, all
152 * I/O to the device will go through the changer scheduling routines, as
153 * opposed to the "normal" CD code.
155 static struct cd_quirk_entry cd_quirk_table[] =
158 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
159 /*quirks*/ CD_Q_CHANGER
162 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
163 "*"}, /* quirks */ CD_Q_CHANGER
166 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
167 /* quirks */ CD_Q_CHANGER
170 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
171 /* quirks */ CD_Q_BCD_TRACKS
176 #define MIN(x,y) ((x<y) ? x : y)
179 #define CD_CDEV_MAJOR 15
180 #define CD_BDEV_MAJOR 6
182 static d_open_t cdopen;
183 static d_close_t cdclose;
184 static d_ioctl_t cdioctl;
185 static d_strategy_t cdstrategy;
187 static periph_init_t cdinit;
188 static periph_ctor_t cdregister;
189 static periph_dtor_t cdcleanup;
190 static periph_start_t cdstart;
191 static periph_oninv_t cdoninvalidate;
192 static void cdasync(void *callback_arg, u_int32_t code,
193 struct cam_path *path, void *arg);
194 static void cdshorttimeout(void *arg);
195 static void cdschedule(struct cam_periph *periph, int priority);
196 static void cdrunchangerqueue(void *arg);
197 static void cdchangerschedule(struct cd_softc *softc);
198 static int cdrunccb(union ccb *ccb,
199 int (*error_routine)(union ccb *ccb,
201 u_int32_t sense_flags),
202 u_int32_t cam_flags, u_int32_t sense_flags);
203 static union ccb *cdgetccb(struct cam_periph *periph,
205 static void cddone(struct cam_periph *periph,
206 union ccb *start_ccb);
207 static int cderror(union ccb *ccb, u_int32_t cam_flags,
208 u_int32_t sense_flags);
209 static void cdprevent(struct cam_periph *periph, int action);
210 static int cdsize(dev_t dev, u_int32_t *size);
211 static int cdfirsttrackisdata(struct cam_periph *periph);
212 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
213 u_int32_t start, struct cd_toc_entry *data,
215 static int cdgetmode(struct cam_periph *periph,
216 struct cd_mode_data *data, u_int32_t page);
217 static int cdsetmode(struct cam_periph *periph,
218 struct cd_mode_data *data);
219 static int cdplay(struct cam_periph *periph, u_int32_t blk,
221 static int cdreadsubchannel(struct cam_periph *periph,
222 u_int32_t mode, u_int32_t format,
224 struct cd_sub_channel_info *data,
226 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
227 u_int32_t starts, u_int32_t startf,
228 u_int32_t endm, u_int32_t ends,
230 static int cdplaytracks(struct cam_periph *periph,
231 u_int32_t strack, u_int32_t sindex,
232 u_int32_t etrack, u_int32_t eindex);
233 static int cdpause(struct cam_periph *periph, u_int32_t go);
234 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
235 static int cdstartunit(struct cam_periph *periph);
236 static int cdsetspeed(struct cam_periph *periph,
237 u_int32_t rdspeed, u_int32_t wrspeed);
238 static int cdreportkey(struct cam_periph *periph,
239 struct dvd_authinfo *authinfo);
240 static int cdsendkey(struct cam_periph *periph,
241 struct dvd_authinfo *authinfo);
242 static int cdreaddvdstructure(struct cam_periph *periph,
243 struct dvd_struct *dvdstruct);
245 static struct periph_driver cddriver =
248 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
251 DATA_SET(periphdriver_set, cddriver);
253 /* For 2.2-stable support */
257 static struct cdevsw cd_cdevsw = {
261 /* write */ physwrite,
265 /* strategy */ cdstrategy,
267 /* maj */ CD_CDEV_MAJOR,
271 /* bmaj */ CD_BDEV_MAJOR
273 static struct cdevsw cddisk_cdevsw;
275 static struct extend_array *cdperiphs;
276 static int num_changers;
278 #ifndef CHANGER_MIN_BUSY_SECONDS
279 #define CHANGER_MIN_BUSY_SECONDS 5
281 #ifndef CHANGER_MAX_BUSY_SECONDS
282 #define CHANGER_MAX_BUSY_SECONDS 15
285 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
286 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
289 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
292 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
293 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
294 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
295 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
296 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
297 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
298 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
302 target_id_t target_id;
305 struct timeval start_time;
306 struct cd_softc *cur_device;
307 struct callout_handle short_handle;
308 struct callout_handle long_handle;
309 volatile cd_changer_flags flags;
310 STAILQ_ENTRY(cdchanger) changer_links;
311 STAILQ_HEAD(chdevlist, cd_softc) chluns;
314 static STAILQ_HEAD(changerlist, cdchanger) changerq;
320 struct cam_path *path;
323 * Create our extend array for storing the devices we attach to.
325 cdperiphs = cam_extend_new();
326 if (cdperiphs == NULL) {
327 printf("cd: Failed to alloc extend array!\n");
332 * Install a global async callback. This callback will
333 * receive async callbacks like "new device found".
335 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
336 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
338 if (status == CAM_REQ_CMP) {
339 struct ccb_setasync csa;
341 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
342 csa.ccb_h.func_code = XPT_SASYNC_CB;
343 csa.event_enable = AC_FOUND_DEVICE;
344 csa.callback = cdasync;
345 csa.callback_arg = NULL;
346 xpt_action((union ccb *)&csa);
347 status = csa.ccb_h.status;
351 if (status != CAM_REQ_CMP) {
352 printf("cd: Failed to attach master async callback "
353 "due to status 0x%x!\n", status);
358 cdoninvalidate(struct cam_periph *periph)
361 struct cd_softc *softc;
363 struct ccb_setasync csa;
365 softc = (struct cd_softc *)periph->softc;
368 * De-register any async callbacks.
370 xpt_setup_ccb(&csa.ccb_h, periph->path,
372 csa.ccb_h.func_code = XPT_SASYNC_CB;
373 csa.event_enable = 0;
374 csa.callback = cdasync;
375 csa.callback_arg = periph;
376 xpt_action((union ccb *)&csa);
378 softc->flags |= CD_FLAG_INVALID;
381 * Although the oninvalidate() routines are always called at
382 * splsoftcam, we need to be at splbio() here to keep the buffer
383 * queue from being modified while we traverse it.
388 * Return all queued I/O with ENXIO.
389 * XXX Handle any transactions queued to the card
390 * with XPT_ABORT_CCB.
392 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
393 bufq_remove(&softc->buf_queue, q_bp);
394 q_bp->b_resid = q_bp->b_bcount;
395 q_bp->b_error = ENXIO;
396 q_bp->b_flags |= B_ERROR;
402 * If this device is part of a changer, and it was scheduled
403 * to run, remove it from the run queue since we just nuked
404 * all of its scheduled I/O.
406 if ((softc->flags & CD_FLAG_CHANGER)
407 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
408 camq_remove(&softc->changer->devq, softc->pinfo.index);
410 xpt_print_path(periph->path);
411 printf("lost device\n");
415 cdcleanup(struct cam_periph *periph)
417 struct cd_softc *softc;
420 softc = (struct cd_softc *)periph->softc;
422 xpt_print_path(periph->path);
423 printf("removing device entry\n");
427 * In the queued, non-active case, the device in question
428 * has already been removed from the changer run queue. Since this
429 * device is active, we need to de-activate it, and schedule
430 * another device to run. (if there is another one to run)
432 if ((softc->flags & CD_FLAG_CHANGER)
433 && (softc->flags & CD_FLAG_ACTIVE)) {
436 * The purpose of the short timeout is soley to determine
437 * whether the current device has finished or not. Well,
438 * since we're removing the active device, we know that it
439 * is finished. So, get rid of the short timeout.
440 * Otherwise, if we're in the time period before the short
441 * timeout fires, and there are no other devices in the
442 * queue to run, there won't be any other device put in the
443 * active slot. i.e., when we call cdrunchangerqueue()
444 * below, it won't do anything. Then, when the short
445 * timeout fires, it'll look at the "current device", which
446 * we are free below, and possibly panic the kernel on a
447 * bogus pointer reference.
449 * The long timeout doesn't really matter, since we
450 * decrement the qfrozen_cnt to indicate that there is
451 * nothing in the active slot now. Therefore, there won't
452 * be any bogus pointer references there.
454 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
455 untimeout(cdshorttimeout, softc->changer,
456 softc->changer->short_handle);
457 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
459 softc->changer->devq.qfrozen_cnt--;
460 softc->changer->flags |= CHANGER_MANUAL_CALL;
461 cdrunchangerqueue(softc->changer);
465 * If we're removing the last device on the changer, go ahead and
466 * remove the changer device structure.
468 if ((softc->flags & CD_FLAG_CHANGER)
469 && (--softc->changer->num_devices == 0)) {
472 * Theoretically, there shouldn't be any timeouts left, but
473 * I'm not completely sure that that will be the case. So,
474 * it won't hurt to check and see if there are any left.
476 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
477 untimeout(cdrunchangerqueue, softc->changer,
478 softc->changer->long_handle);
479 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
482 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
483 untimeout(cdshorttimeout, softc->changer,
484 softc->changer->short_handle);
485 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
488 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
490 xpt_print_path(periph->path);
491 printf("removing changer entry\n");
492 free(softc->changer, M_DEVBUF);
495 devstat_remove_entry(&softc->device_stats);
496 cam_extend_release(cdperiphs, periph->unit_number);
497 if (softc->disk.d_dev) {
498 disk_destroy(softc->disk.d_dev);
500 free(softc, M_DEVBUF);
505 cdasync(void *callback_arg, u_int32_t code,
506 struct cam_path *path, void *arg)
508 struct cam_periph *periph;
510 periph = (struct cam_periph *)callback_arg;
512 case AC_FOUND_DEVICE:
514 struct ccb_getdev *cgd;
517 cgd = (struct ccb_getdev *)arg;
519 if (SID_TYPE(&cgd->inq_data) != T_CDROM
520 && SID_TYPE(&cgd->inq_data) != T_WORM)
524 * Allocate a peripheral instance for
525 * this device and start the probe
528 status = cam_periph_alloc(cdregister, cdoninvalidate,
530 "cd", CAM_PERIPH_BIO,
531 cgd->ccb_h.path, cdasync,
532 AC_FOUND_DEVICE, cgd);
534 if (status != CAM_REQ_CMP
535 && status != CAM_REQ_INPROG)
536 printf("cdasync: Unable to attach new device "
537 "due to status 0x%x\n", status);
544 struct cd_softc *softc;
545 struct ccb_hdr *ccbh;
548 softc = (struct cd_softc *)periph->softc;
551 * Don't fail on the expected unit attention
554 softc->flags |= CD_FLAG_RETRY_UA;
555 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
556 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
557 ccbh->ccb_state |= CD_CCB_RETRY_UA;
562 cam_periph_async(periph, code, path, arg);
568 cdregister(struct cam_periph *periph, void *arg)
570 struct cd_softc *softc;
571 struct ccb_setasync csa;
572 struct ccb_getdev *cgd;
575 cgd = (struct ccb_getdev *)arg;
576 if (periph == NULL) {
577 printf("cdregister: periph was NULL!!\n");
578 return(CAM_REQ_CMP_ERR);
581 printf("cdregister: no getdev CCB, can't register device\n");
582 return(CAM_REQ_CMP_ERR);
585 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
588 printf("cdregister: Unable to probe new device. "
589 "Unable to allocate softc\n");
590 return(CAM_REQ_CMP_ERR);
593 bzero(softc, sizeof(*softc));
594 LIST_INIT(&softc->pending_ccbs);
595 softc->state = CD_STATE_PROBE;
596 bufq_init(&softc->buf_queue);
597 if (SID_IS_REMOVABLE(&cgd->inq_data))
598 softc->flags |= CD_FLAG_DISC_REMOVABLE;
599 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
600 softc->flags |= CD_FLAG_TAGGED_QUEUING;
602 periph->softc = softc;
603 softc->periph = periph;
605 cam_extend_set(cdperiphs, periph->unit_number, periph);
608 * See if this device has any quirks.
610 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
611 (caddr_t)cd_quirk_table,
612 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
613 sizeof(*cd_quirk_table), scsi_inquiry_match);
616 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
618 softc->quirks = CD_Q_NONE;
621 * We need to register the statistics structure for this device,
622 * but we don't have the blocksize yet for it. So, we register
623 * the structure and indicate that we don't have the blocksize
624 * yet. Unlike other SCSI peripheral drivers, we explicitly set
625 * the device type here to be CDROM, rather than just ORing in
626 * the device type. This is because this driver can attach to either
627 * CDROM or WORM devices, and we want this peripheral driver to
628 * show up in the devstat list as a CD peripheral driver, not a
629 * WORM peripheral driver. WORM drives will also have the WORM
630 * driver attached to them.
632 devstat_add_entry(&softc->device_stats, "cd",
633 periph->unit_number, 0,
634 DEVSTAT_BS_UNAVAILABLE,
635 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
636 DEVSTAT_PRIORITY_CD);
637 disk_create(periph->unit_number, &softc->disk,
638 DSO_ONESLICE | DSO_COMPATLABEL,
639 &cd_cdevsw, &cddisk_cdevsw);
642 * Add an async callback so that we get
643 * notified if this device goes away.
645 xpt_setup_ccb(&csa.ccb_h, periph->path,
647 csa.ccb_h.func_code = XPT_SASYNC_CB;
648 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
649 csa.callback = cdasync;
650 csa.callback_arg = periph;
651 xpt_action((union ccb *)&csa);
654 * If the target lun is greater than 0, we most likely have a CD
655 * changer device. Check the quirk entries as well, though, just
656 * in case someone has a CD tower with one lun per drive or
657 * something like that. Also, if we know up front that a
658 * particular device is a changer, we can mark it as such starting
659 * with lun 0, instead of lun 1. It shouldn't be necessary to have
660 * a quirk entry to define something as a changer, however.
662 if (((cgd->ccb_h.target_lun > 0)
663 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
664 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
665 struct cdchanger *nchanger;
666 struct cam_periph *nperiph;
667 struct cam_path *path;
671 /* Set the changer flag in the current device's softc */
672 softc->flags |= CD_FLAG_CHANGER;
674 if (num_changers == 0)
675 STAILQ_INIT(&changerq);
678 * Now, look around for an existing changer device with the
679 * same path and target ID as the current device.
682 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
684 nchanger = STAILQ_NEXT(nchanger, changer_links)){
685 if ((nchanger->path_id == cgd->ccb_h.path_id)
686 && (nchanger->target_id == cgd->ccb_h.target_id)) {
693 * If we found a matching entry, just add this device to
694 * the list of devices on this changer.
697 struct chdevlist *chlunhead;
699 chlunhead = &nchanger->chluns;
702 * XXX KDM look at consolidating this code with the
703 * code below in a separate function.
707 * Create a path with lun id 0, and see if we can
708 * find a matching device
710 status = xpt_create_path(&path, /*periph*/ periph,
712 cgd->ccb_h.target_id, 0);
714 if ((status == CAM_REQ_CMP)
715 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
716 struct cd_softc *nsoftc;
718 nsoftc = (struct cd_softc *)nperiph->softc;
720 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
721 nsoftc->flags |= CD_FLAG_CHANGER;
722 nchanger->num_devices++;
723 if (camq_resize(&nchanger->devq,
724 nchanger->num_devices)!=CAM_REQ_CMP){
725 printf("cdregister: "
731 nsoftc->changer = nchanger;
732 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
734 STAILQ_INSERT_TAIL(&nchanger->chluns,
735 nsoftc,changer_links);
738 } else if (status == CAM_REQ_CMP)
741 printf("cdregister: unable to allocate path\n"
742 "cdregister: changer support may be "
746 nchanger->num_devices++;
748 softc->changer = nchanger;
749 softc->pinfo.index = CAM_UNQUEUED_INDEX;
751 if (camq_resize(&nchanger->devq,
752 nchanger->num_devices) != CAM_REQ_CMP) {
753 printf("cdregister: camq_resize "
754 "failed, changer support may "
758 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
761 * In this case, we don't already have an entry for this
762 * particular changer, so we need to create one, add it to
763 * the queue, and queue this device on the list for this
764 * changer. Before we queue this device, however, we need
765 * to search for lun id 0 on this target, and add it to the
766 * queue first, if it exists. (and if it hasn't already
767 * been marked as part of the changer.)
770 nchanger = malloc(sizeof(struct cdchanger),
773 if (nchanger == NULL) {
774 softc->flags &= ~CD_FLAG_CHANGER;
775 printf("cdregister: unable to malloc "
776 "changer structure\ncdregister: "
777 "changer support disabled\n");
780 * Yes, gotos can be gross but in this case
781 * I think it's justified..
786 /* zero the structure */
787 bzero(nchanger, sizeof(struct cdchanger));
789 if (camq_init(&nchanger->devq, 1) != 0) {
790 softc->flags &= ~CD_FLAG_CHANGER;
791 printf("cdregister: changer support "
798 nchanger->path_id = cgd->ccb_h.path_id;
799 nchanger->target_id = cgd->ccb_h.target_id;
801 /* this is superfluous, but it makes things clearer */
802 nchanger->num_devices = 0;
804 STAILQ_INIT(&nchanger->chluns);
806 STAILQ_INSERT_TAIL(&changerq, nchanger,
810 * Create a path with lun id 0, and see if we can
811 * find a matching device
813 status = xpt_create_path(&path, /*periph*/ periph,
815 cgd->ccb_h.target_id, 0);
818 * If we were able to allocate the path, and if we
819 * find a matching device and it isn't already
820 * marked as part of a changer, then we add it to
821 * the current changer.
823 if ((status == CAM_REQ_CMP)
824 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
825 && ((((struct cd_softc *)periph->softc)->flags &
826 CD_FLAG_CHANGER) == 0)) {
827 struct cd_softc *nsoftc;
829 nsoftc = (struct cd_softc *)nperiph->softc;
831 nsoftc->flags |= CD_FLAG_CHANGER;
832 nchanger->num_devices++;
833 if (camq_resize(&nchanger->devq,
834 nchanger->num_devices) != CAM_REQ_CMP) {
835 printf("cdregister: camq_resize "
836 "failed, changer support may "
839 nsoftc->changer = nchanger;
840 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
842 STAILQ_INSERT_TAIL(&nchanger->chluns,
843 nsoftc, changer_links);
845 } else if (status == CAM_REQ_CMP)
848 printf("cdregister: unable to allocate path\n"
849 "cdregister: changer support may be "
853 softc->changer = nchanger;
854 softc->pinfo.index = CAM_UNQUEUED_INDEX;
855 nchanger->num_devices++;
856 if (camq_resize(&nchanger->devq,
857 nchanger->num_devices) != CAM_REQ_CMP) {
858 printf("cdregister: camq_resize "
859 "failed, changer support may "
862 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
869 /* Lock this peripheral until we are setup */
871 cam_periph_lock(periph, PRIBIO);
873 if ((softc->flags & CD_FLAG_CHANGER) == 0)
874 xpt_schedule(periph, /*priority*/5);
876 cdschedule(periph, /*priority*/ 5);
882 cdopen(dev_t dev, int flags, int fmt, struct proc *p)
884 struct disklabel *label;
885 struct cam_periph *periph;
886 struct cd_softc *softc;
887 struct ccb_getdev cgd;
893 periph = cam_extend_get(cdperiphs, unit);
898 softc = (struct cd_softc *)periph->softc;
901 * Grab splsoftcam and hold it until we lock the peripheral.
904 if (softc->flags & CD_FLAG_INVALID) {
909 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
916 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
919 cdprevent(periph, PR_PREVENT);
921 /* find out the size */
922 if ((error = cdsize(dev, &size)) != 0) {
923 cdprevent(periph, PR_ALLOW);
924 cam_periph_unlock(periph);
925 cam_periph_release(periph);
930 * If we get a non-zero return, revert back to not reading the
931 * label off the disk. The first track is likely audio, which
932 * won't have a disklabel.
934 if ((error = cdfirsttrackisdata(periph)) != 0) {
935 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
936 softc->disk.d_dsflags |= DSO_NOLABELS;
941 * Build prototype label for whole disk.
942 * Should take information about different data tracks from the
943 * TOC and put it in the partition table.
945 label = &softc->disk.d_label;
946 bzero(label, sizeof(*label));
947 label->d_type = DTYPE_SCSI;
950 * Grab the inquiry data to get the vendor and product names.
951 * Put them in the typename and packname for the label.
953 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
954 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
955 xpt_action((union ccb *)&cgd);
957 strncpy(label->d_typename, cgd.inq_data.vendor,
958 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
959 strncpy(label->d_packname, cgd.inq_data.product,
960 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
962 label->d_secsize = softc->params.blksize;
963 label->d_secperunit = softc->params.disksize;
964 label->d_flags = D_REMOVABLE;
966 * Make partition 'a' cover the whole disk. This is a temporary
967 * compatibility hack. The 'a' partition should not exist, so
968 * the slice code won't create it. The slice code will make
969 * partition (RAW_PART + 'a') cover the whole disk and fill in
970 * some more defaults.
972 label->d_partitions[0].p_size = label->d_secperunit;
973 label->d_partitions[0].p_fstype = FS_OTHER;
976 * We unconditionally (re)set the blocksize each time the
977 * CD device is opened. This is because the CD can change,
978 * and therefore the blocksize might change.
979 * XXX problems here if some slice or partition is still
980 * open with the old size?
982 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
983 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
984 softc->device_stats.block_size = softc->params.blksize;
986 cam_periph_unlock(periph);
988 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
994 cdclose(dev_t dev, int flag, int fmt, struct proc *p)
996 struct cam_periph *periph;
997 struct cd_softc *softc;
1001 periph = cam_extend_get(cdperiphs, unit);
1005 softc = (struct cd_softc *)periph->softc;
1007 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
1010 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1011 cdprevent(periph, PR_ALLOW);
1014 * Unconditionally set the dsopen() flags back to their default
1017 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1018 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1021 * Since we're closing this CD, mark the blocksize as unavailable.
1022 * It will be marked as available whence the CD is opened again.
1024 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1026 cam_periph_unlock(periph);
1027 cam_periph_release(periph);
1033 cdshorttimeout(void *arg)
1035 struct cdchanger *changer;
1040 changer = (struct cdchanger *)arg;
1042 /* Always clear the short timeout flag, since that's what we're in */
1043 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1046 * Check to see if there is any more pending or outstanding I/O for
1047 * this device. If not, move it out of the active slot.
1049 if ((bufq_first(&changer->cur_device->buf_queue) == NULL)
1050 && (changer->cur_device->device_stats.busy_count == 0)) {
1051 changer->flags |= CHANGER_MANUAL_CALL;
1052 cdrunchangerqueue(changer);
1059 * This is a wrapper for xpt_schedule. It only applies to changers.
1062 cdschedule(struct cam_periph *periph, int priority)
1064 struct cd_softc *softc;
1069 softc = (struct cd_softc *)periph->softc;
1072 * If this device isn't currently queued, and if it isn't
1073 * the active device, then we queue this device and run the
1074 * changer queue if there is no timeout scheduled to do it.
1075 * If this device is the active device, just schedule it
1076 * to run again. If this device is queued, there should be
1077 * a timeout in place already that will make sure it runs.
1079 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1080 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1082 * We don't do anything with the priority here.
1083 * This is strictly a fifo queue.
1085 softc->pinfo.priority = 1;
1086 softc->pinfo.generation = ++softc->changer->devq.generation;
1087 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1090 * Since we just put a device in the changer queue,
1091 * check and see if there is a timeout scheduled for
1092 * this changer. If so, let the timeout handle
1093 * switching this device into the active slot. If
1094 * not, manually call the timeout routine to
1097 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1098 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1099 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1100 softc->changer->flags |= CHANGER_MANUAL_CALL;
1101 cdrunchangerqueue(softc->changer);
1103 } else if ((softc->flags & CD_FLAG_ACTIVE)
1104 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1105 xpt_schedule(periph, priority);
1112 cdrunchangerqueue(void *arg)
1114 struct cd_softc *softc;
1115 struct cdchanger *changer;
1116 int called_from_timeout;
1121 changer = (struct cdchanger *)arg;
1124 * If we have NOT been called from cdstrategy() or cddone(), and
1125 * instead from a timeout routine, go ahead and clear the
1128 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1129 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1130 called_from_timeout = 1;
1132 called_from_timeout = 0;
1134 /* Always clear the manual call flag */
1135 changer->flags &= ~CHANGER_MANUAL_CALL;
1137 /* nothing to do if the queue is empty */
1138 if (changer->devq.entries <= 0) {
1144 * If the changer queue is frozen, that means we have an active
1147 if (changer->devq.qfrozen_cnt > 0) {
1149 if (changer->cur_device->device_stats.busy_count > 0) {
1150 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1151 changer->cur_device->bufs_left =
1152 changer->cur_device->device_stats.busy_count;
1153 if (called_from_timeout) {
1154 changer->long_handle =
1155 timeout(cdrunchangerqueue, changer,
1156 changer_max_busy_seconds * hz);
1157 changer->flags |= CHANGER_TIMEOUT_SCHED;
1164 * We always need to reset the frozen count and clear the
1167 changer->devq.qfrozen_cnt--;
1168 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1169 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1172 * Check to see whether the current device has any I/O left
1173 * to do. If so, requeue it at the end of the queue. If
1174 * not, there is no need to requeue it.
1176 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1178 changer->cur_device->pinfo.generation =
1179 ++changer->devq.generation;
1180 camq_insert(&changer->devq,
1181 (cam_pinfo *)changer->cur_device);
1185 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1187 changer->cur_device = softc;
1189 changer->devq.qfrozen_cnt++;
1190 softc->flags |= CD_FLAG_ACTIVE;
1192 /* Just in case this device is waiting */
1193 wakeup(&softc->changer);
1194 xpt_schedule(softc->periph, /*priority*/ 1);
1197 * Get rid of any pending timeouts, and set a flag to schedule new
1198 * ones so this device gets its full time quantum.
1200 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1201 untimeout(cdrunchangerqueue, changer, changer->long_handle);
1202 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1205 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1206 untimeout(cdshorttimeout, changer, changer->short_handle);
1207 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1211 * We need to schedule timeouts, but we only do this after the
1212 * first transaction has completed. This eliminates the changer
1215 changer->flags |= CHANGER_NEED_TIMEOUT;
1221 cdchangerschedule(struct cd_softc *softc)
1223 struct cdchanger *changer;
1228 changer = softc->changer;
1231 * If this is a changer, and this is the current device,
1232 * and this device has at least the minimum time quantum to
1233 * run, see if we can switch it out.
1235 if ((softc->flags & CD_FLAG_ACTIVE)
1236 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1237 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1239 * We try three things here. The first is that we
1240 * check to see whether the schedule on completion
1241 * flag is set. If it is, we decrement the number
1242 * of buffers left, and if it's zero, we reschedule.
1243 * Next, we check to see whether the pending buffer
1244 * queue is empty and whether there are no
1245 * outstanding transactions. If so, we reschedule.
1246 * Next, we see if the pending buffer queue is empty.
1247 * If it is, we set the number of buffers left to
1248 * the current active buffer count and set the
1249 * schedule on complete flag.
1251 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1252 if (--softc->bufs_left == 0) {
1253 softc->changer->flags |=
1254 CHANGER_MANUAL_CALL;
1255 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1256 cdrunchangerqueue(softc->changer);
1258 } else if ((bufq_first(&softc->buf_queue) == NULL)
1259 && (softc->device_stats.busy_count == 0)) {
1260 softc->changer->flags |= CHANGER_MANUAL_CALL;
1261 cdrunchangerqueue(softc->changer);
1263 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1264 && (softc->flags & CD_FLAG_ACTIVE)) {
1267 * Now that the first transaction to this
1268 * particular device has completed, we can go ahead
1269 * and schedule our timeouts.
1271 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1272 changer->long_handle =
1273 timeout(cdrunchangerqueue, changer,
1274 changer_max_busy_seconds * hz);
1275 changer->flags |= CHANGER_TIMEOUT_SCHED;
1277 printf("cdchangerschedule: already have a long"
1280 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1281 changer->short_handle =
1282 timeout(cdshorttimeout, changer,
1283 changer_min_busy_seconds * hz);
1284 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1286 printf("cdchangerschedule: already have a short "
1290 * We just scheduled timeouts, no need to schedule
1293 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1300 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1301 u_int32_t cam_flags,
1302 u_int32_t sense_flags),
1303 u_int32_t cam_flags, u_int32_t sense_flags)
1305 struct cd_softc *softc;
1306 struct cam_periph *periph;
1309 periph = xpt_path_periph(ccb->ccb_h.path);
1310 softc = (struct cd_softc *)periph->softc;
1312 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1313 &softc->device_stats);
1315 if (softc->flags & CD_FLAG_CHANGER)
1316 cdchangerschedule(softc);
1322 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1324 struct cd_softc *softc;
1327 softc = (struct cd_softc *)periph->softc;
1329 if (softc->flags & CD_FLAG_CHANGER) {
1334 * This should work the first time this device is woken up,
1335 * but just in case it doesn't, we use a while loop.
1337 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1339 * If this changer isn't already queued, queue it up.
1341 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1342 softc->pinfo.priority = 1;
1343 softc->pinfo.generation =
1344 ++softc->changer->devq.generation;
1345 camq_insert(&softc->changer->devq,
1346 (cam_pinfo *)softc);
1348 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1349 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1350 && ((softc->changer->flags
1351 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1352 softc->changer->flags |= CHANGER_MANUAL_CALL;
1353 cdrunchangerqueue(softc->changer);
1355 tsleep(&softc->changer, PRIBIO, "cgticb", 0);
1359 return(cam_periph_getccb(periph, priority));
1364 * Actually translate the requested transfer into one the physical driver
1365 * can understand. The transfer is described by a buf and will include
1366 * only one physical transfer.
1369 cdstrategy(struct buf *bp)
1371 struct cam_periph *periph;
1372 struct cd_softc *softc;
1376 unit = dkunit(bp->b_dev);
1377 part = dkpart(bp->b_dev);
1378 periph = cam_extend_get(cdperiphs, unit);
1379 if (periph == NULL) {
1380 bp->b_error = ENXIO;
1384 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1386 softc = (struct cd_softc *)periph->softc;
1389 * Mask interrupts so that the pack cannot be invalidated until
1390 * after we are in the queue. Otherwise, we might not properly
1391 * clean up one of the buffers.
1396 * If the device has been made invalid, error out
1398 if ((softc->flags & CD_FLAG_INVALID)) {
1400 bp->b_error = ENXIO;
1405 * Place it in the queue of disk activities for this disk
1407 bufqdisksort(&softc->buf_queue, bp);
1412 * Schedule ourselves for performing the work. We do things
1413 * differently for changers.
1415 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1416 xpt_schedule(periph, /* XXX priority */1);
1418 cdschedule(periph, /* priority */ 1);
1422 bp->b_flags |= B_ERROR;
1424 * Correctly set the buf to indicate a completed xfer
1426 bp->b_resid = bp->b_bcount;
1432 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1434 struct cd_softc *softc;
1436 struct ccb_scsiio *csio;
1437 struct scsi_read_capacity_data *rcap;
1440 softc = (struct cd_softc *)periph->softc;
1442 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1444 switch (softc->state) {
1445 case CD_STATE_NORMAL:
1450 bp = bufq_first(&softc->buf_queue);
1451 if (periph->immediate_priority <= periph->pinfo.priority) {
1452 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1454 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1456 periph->immediate_priority = CAM_PRIORITY_NONE;
1458 wakeup(&periph->ccb_list);
1459 } else if (bp == NULL) {
1461 xpt_release_ccb(start_ccb);
1463 bufq_remove(&softc->buf_queue, bp);
1465 devstat_start_transaction(&softc->device_stats);
1467 scsi_read_write(&start_ccb->csio,
1469 /* cbfcnp */ cddone,
1470 (bp->b_flags & B_ORDERED) != 0 ?
1473 /* read */bp->b_flags & B_READ,
1475 /* minimum_cmd_size */ 10,
1476 /* lba */ bp->b_pblkno,
1477 bp->b_bcount / softc->params.blksize,
1478 /* data_ptr */ bp->b_data,
1479 /* dxfer_len */ bp->b_bcount,
1480 /* sense_len */ SSD_FULL_SIZE,
1481 /* timeout */ 30000);
1482 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1486 * Block out any asyncronous callbacks
1487 * while we touch the pending ccb list.
1490 LIST_INSERT_HEAD(&softc->pending_ccbs,
1491 &start_ccb->ccb_h, periph_links.le);
1494 /* We expect a unit attention from this device */
1495 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1496 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1497 softc->flags &= ~CD_FLAG_RETRY_UA;
1500 start_ccb->ccb_h.ccb_bp = bp;
1501 bp = bufq_first(&softc->buf_queue);
1504 xpt_action(start_ccb);
1507 /* Have more work to do, so ensure we stay scheduled */
1508 xpt_schedule(periph, /* XXX priority */1);
1512 case CD_STATE_PROBE:
1515 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1519 xpt_print_path(periph->path);
1520 printf("cdstart: Couldn't malloc read_capacity data\n");
1521 /* cd_free_periph??? */
1524 csio = &start_ccb->csio;
1525 scsi_read_capacity(csio,
1532 start_ccb->ccb_h.ccb_bp = NULL;
1533 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1534 xpt_action(start_ccb);
1541 cddone(struct cam_periph *periph, union ccb *done_ccb)
1543 struct cd_softc *softc;
1544 struct ccb_scsiio *csio;
1546 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1548 softc = (struct cd_softc *)periph->softc;
1549 csio = &done_ccb->csio;
1551 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1552 case CD_CCB_BUFFER_IO:
1558 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1561 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1564 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1569 /* Retry selection timeouts */
1570 sf |= SF_RETRY_SELTO;
1572 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1574 * A retry was scheuled, so
1585 xpt_print_path(periph->path);
1586 printf("cddone: got error %#x back\n", error);
1588 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1589 bufq_remove(&softc->buf_queue, q_bp);
1590 q_bp->b_resid = q_bp->b_bcount;
1591 q_bp->b_error = EIO;
1592 q_bp->b_flags |= B_ERROR;
1596 bp->b_resid = bp->b_bcount;
1597 bp->b_error = error;
1598 bp->b_flags |= B_ERROR;
1599 cam_release_devq(done_ccb->ccb_h.path,
1603 /*getcount_only*/0);
1606 bp->b_resid = csio->resid;
1608 if (bp->b_resid != 0) {
1609 /* Short transfer ??? */
1610 bp->b_flags |= B_ERROR;
1615 * Block out any asyncronous callbacks
1616 * while we touch the pending ccb list.
1619 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1622 if (softc->flags & CD_FLAG_CHANGER)
1623 cdchangerschedule(softc);
1625 devstat_end_transaction_buf(&softc->device_stats, bp);
1631 struct scsi_read_capacity_data *rdcap;
1632 char announce_buf[120]; /*
1633 * Currently (9/30/97) the
1634 * longest possible announce
1635 * buffer is 108 bytes, for the
1636 * first error case below.
1637 * That is 39 bytes for the
1638 * basic string, 16 bytes for the
1639 * biggest sense key (hardware
1640 * error), 52 bytes for the
1641 * text of the largest sense
1642 * qualifier valid for a CDROM,
1643 * (0x72, 0x03 or 0x04,
1644 * 0x03), and one byte for the
1645 * null terminating character.
1646 * To allow for longer strings,
1647 * the announce buffer is 120
1650 struct cd_params *cdp;
1652 cdp = &softc->params;
1654 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1656 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1657 cdp->blksize = scsi_4btoul (rdcap->length);
1659 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1661 snprintf(announce_buf, sizeof(announce_buf),
1662 "cd present [%lu x %lu byte records]",
1663 cdp->disksize, (u_long)cdp->blksize);
1668 * Retry any UNIT ATTENTION type errors. They
1669 * are expected at boot.
1671 error = cderror(done_ccb, 0, SF_RETRY_UA |
1672 SF_NO_PRINT | SF_RETRY_SELTO);
1673 if (error == ERESTART) {
1675 * A retry was scheuled, so
1679 } else if (error != 0) {
1681 struct scsi_sense_data *sense;
1683 int sense_key, error_code;
1686 struct ccb_getdev cgd;
1688 /* Don't wedge this device's queue */
1689 cam_release_devq(done_ccb->ccb_h.path,
1693 /*getcount_only*/0);
1695 status = done_ccb->ccb_h.status;
1697 xpt_setup_ccb(&cgd.ccb_h,
1698 done_ccb->ccb_h.path,
1700 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1701 xpt_action((union ccb *)&cgd);
1703 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1704 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1705 || ((status & CAM_AUTOSNS_VALID) == 0))
1711 sense = &csio->sense_data;
1712 scsi_extract_sense(sense, &error_code,
1717 * Attach to anything that claims to be a
1718 * CDROM or WORM device, as long as it
1719 * doesn't return a "Logical unit not
1720 * supported" (0x25) error.
1722 if ((have_sense) && (asc != 0x25)
1723 && (error_code == SSD_CURRENT_ERROR))
1724 snprintf(announce_buf,
1725 sizeof(announce_buf),
1726 "Attempt to query device "
1727 "size failed: %s, %s",
1728 scsi_sense_key_text[sense_key],
1729 scsi_sense_desc(asc,ascq,
1731 else if ((have_sense == 0)
1732 && ((status & CAM_STATUS_MASK) ==
1733 CAM_SCSI_STATUS_ERROR)
1734 && (csio->scsi_status ==
1735 SCSI_STATUS_BUSY)) {
1736 snprintf(announce_buf,
1737 sizeof(announce_buf),
1738 "Attempt to query device "
1739 "size failed: SCSI status: BUSY");
1740 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1742 * We only print out an error for
1743 * CDROM type devices. For WORM
1744 * devices, we don't print out an
1745 * error since a few WORM devices
1746 * don't support CDROM commands.
1747 * If we have sense information, go
1748 * ahead and print it out.
1749 * Otherwise, just say that we
1754 * Just print out the error, not
1755 * the full probe message, when we
1762 xpt_print_path(periph->path);
1763 printf("got CAM status %#x\n",
1764 done_ccb->ccb_h.status);
1766 xpt_print_path(periph->path);
1767 printf("fatal error, failed"
1768 " to attach to device\n");
1771 * Invalidate this peripheral.
1773 cam_periph_invalidate(periph);
1775 announce_buf[0] = '\0';
1779 * Invalidate this peripheral.
1781 cam_periph_invalidate(periph);
1782 announce_buf[0] = '\0';
1786 free(rdcap, M_TEMP);
1787 if (announce_buf[0] != '\0') {
1788 xpt_announce_periph(periph, announce_buf);
1789 if (softc->flags & CD_FLAG_CHANGER)
1790 cdchangerschedule(softc);
1792 softc->state = CD_STATE_NORMAL;
1794 * Since our peripheral may be invalidated by an error
1795 * above or an external event, we must release our CCB
1796 * before releasing the probe lock on the peripheral.
1797 * The peripheral will only go away once the last lock
1798 * is removed, and we need it around for the CCB release
1801 xpt_release_ccb(done_ccb);
1802 cam_periph_unlock(periph);
1805 case CD_CCB_WAITING:
1807 /* Caller will release the CCB */
1808 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1809 ("trying to wakeup ccbwait\n"));
1811 wakeup(&done_ccb->ccb_h.cbfcnp);
1817 xpt_release_ccb(done_ccb);
1821 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1824 struct cam_periph *periph;
1825 struct cd_softc *softc;
1830 periph = cam_extend_get(cdperiphs, unit);
1834 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1836 softc = (struct cd_softc *)periph->softc;
1838 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1839 ("trying to do ioctl %#lx\n", cmd));
1841 error = cam_periph_lock(periph, PRIBIO | PCATCH);
1848 case CDIOCPLAYTRACKS:
1850 struct ioc_play_track *args
1851 = (struct ioc_play_track *) addr;
1852 struct cd_mode_data *data;
1854 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1857 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1858 ("trying to do CDIOCPLAYTRACKS\n"));
1860 error = cdgetmode(periph, data, AUDIO_PAGE);
1865 data->page.audio.flags &= ~CD_PA_SOTC;
1866 data->page.audio.flags |= CD_PA_IMMED;
1867 error = cdsetmode(periph, data);
1871 if (softc->quirks & CD_Q_BCD_TRACKS) {
1872 args->start_track = bin2bcd(args->start_track);
1873 args->end_track = bin2bcd(args->end_track);
1875 error = cdplaytracks(periph,
1884 struct ioc_play_msf *args
1885 = (struct ioc_play_msf *) addr;
1886 struct cd_mode_data *data;
1888 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1891 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1892 ("trying to do CDIOCPLAYMSF\n"));
1894 error = cdgetmode(periph, data, AUDIO_PAGE);
1899 data->page.audio.flags &= ~CD_PA_SOTC;
1900 data->page.audio.flags |= CD_PA_IMMED;
1901 error = cdsetmode(periph, data);
1905 error = cdplaymsf(periph,
1914 case CDIOCPLAYBLOCKS:
1916 struct ioc_play_blocks *args
1917 = (struct ioc_play_blocks *) addr;
1918 struct cd_mode_data *data;
1920 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1921 ("trying to do CDIOCPLAYBLOCKS\n"));
1923 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1926 error = cdgetmode(periph, data, AUDIO_PAGE);
1931 data->page.audio.flags &= ~CD_PA_SOTC;
1932 data->page.audio.flags |= CD_PA_IMMED;
1933 error = cdsetmode(periph, data);
1937 error = cdplay(periph, args->blk, args->len);
1940 case CDIOCREADSUBCHANNEL:
1942 struct ioc_read_subchannel *args
1943 = (struct ioc_read_subchannel *) addr;
1944 struct cd_sub_channel_info *data;
1945 u_int32_t len = args->data_len;
1947 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1948 ("trying to do CDIOCREADSUBCHANNEL\n"));
1950 data = malloc(sizeof(struct cd_sub_channel_info),
1953 if ((len > sizeof(struct cd_sub_channel_info)) ||
1954 (len < sizeof(struct cd_sub_channel_header))) {
1956 "scsi_cd: cdioctl: "
1957 "cdioreadsubchannel: error, len=%d\n",
1964 if (softc->quirks & CD_Q_BCD_TRACKS)
1965 args->track = bin2bcd(args->track);
1967 error = cdreadsubchannel(periph, args->address_format,
1968 args->data_format, args->track, data, len);
1974 if (softc->quirks & CD_Q_BCD_TRACKS)
1975 data->what.track_info.track_number =
1976 bcd2bin(data->what.track_info.track_number);
1977 len = min(len, ((data->header.data_len[0] << 8) +
1978 data->header.data_len[1] +
1979 sizeof(struct cd_sub_channel_header)));
1980 if (copyout(data, args->data, len) != 0) {
1987 case CDIOREADTOCHEADER:
1989 struct ioc_toc_header *th;
1991 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1992 ("trying to do CDIOREADTOCHEADER\n"));
1994 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
1996 error = cdreadtoc(periph, 0, 0,
1997 (struct cd_toc_entry *)th,
2003 if (softc->quirks & CD_Q_BCD_TRACKS) {
2004 /* we are going to have to convert the BCD
2005 * encoding on the cd to what is expected
2007 th->starting_track =
2008 bcd2bin(th->starting_track);
2009 th->ending_track = bcd2bin(th->ending_track);
2012 bcopy(th, addr, sizeof(*th));
2016 case CDIOREADTOCENTRYS:
2019 struct ioc_toc_header header;
2020 struct cd_toc_entry entries[100];
2023 struct ioc_toc_header header;
2024 struct cd_toc_entry entry;
2029 struct ioc_read_toc_entry *te =
2030 (struct ioc_read_toc_entry *) addr;
2031 struct ioc_toc_header *th;
2032 u_int32_t len, readlen, idx, num;
2033 u_int32_t starting_track = te->starting_track;
2035 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2036 ("trying to do CDIOREADTOCENTRYS\n"));
2038 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2039 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK);
2041 if (te->data_len < sizeof(struct cd_toc_entry)
2042 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2043 || (te->address_format != CD_MSF_FORMAT
2044 && te->address_format != CD_LBA_FORMAT)) {
2046 printf("scsi_cd: error in readtocentries, "
2047 "returning EINVAL\n");
2054 error = cdreadtoc(periph, 0, 0,
2055 (struct cd_toc_entry *)th,
2063 if (softc->quirks & CD_Q_BCD_TRACKS) {
2064 /* we are going to have to convert the BCD
2065 * encoding on the cd to what is expected
2067 th->starting_track =
2068 bcd2bin(th->starting_track);
2069 th->ending_track = bcd2bin(th->ending_track);
2072 if (starting_track == 0)
2073 starting_track = th->starting_track;
2074 else if (starting_track == LEADOUT)
2075 starting_track = th->ending_track + 1;
2076 else if (starting_track < th->starting_track ||
2077 starting_track > th->ending_track + 1) {
2078 printf("scsi_cd: error in readtocentries, "
2079 "returning EINVAL\n");
2086 /* calculate reading length without leadout entry */
2087 readlen = (th->ending_track - starting_track + 1) *
2088 sizeof(struct cd_toc_entry);
2090 /* and with leadout entry */
2091 len = readlen + sizeof(struct cd_toc_entry);
2092 if (te->data_len < len) {
2097 if (len > sizeof(data->entries)) {
2098 printf("scsi_cd: error in readtocentries, "
2099 "returning EINVAL\n");
2105 num = len / sizeof(struct cd_toc_entry);
2108 error = cdreadtoc(periph, te->address_format,
2110 (struct cd_toc_entry *)data,
2111 readlen + sizeof (*th));
2119 /* make leadout entry if needed */
2120 idx = starting_track + num - 1;
2121 if (softc->quirks & CD_Q_BCD_TRACKS)
2122 th->ending_track = bcd2bin(th->ending_track);
2123 if (idx == th->ending_track + 1) {
2124 error = cdreadtoc(periph, te->address_format,
2126 (struct cd_toc_entry *)lead,
2133 data->entries[idx - starting_track] =
2136 if (softc->quirks & CD_Q_BCD_TRACKS) {
2137 for (idx = 0; idx < num - 1; idx++) {
2138 data->entries[idx].track =
2139 bcd2bin(data->entries[idx].track);
2143 error = copyout(data->entries, te->data, len);
2148 case CDIOREADTOCENTRY:
2150 /* yeah yeah, this is ugly */
2152 struct ioc_toc_header header;
2153 struct cd_toc_entry entry;
2157 struct ioc_read_toc_single_entry *te =
2158 (struct ioc_read_toc_single_entry *) addr;
2159 struct ioc_toc_header *th;
2162 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2163 ("trying to do CDIOREADTOCENTRY\n"));
2165 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2167 if (te->address_format != CD_MSF_FORMAT
2168 && te->address_format != CD_LBA_FORMAT) {
2169 printf("error in readtocentry, "
2170 " returning EINVAL\n");
2177 error = cdreadtoc(periph, 0, 0,
2178 (struct cd_toc_entry *)th,
2185 if (softc->quirks & CD_Q_BCD_TRACKS) {
2186 /* we are going to have to convert the BCD
2187 * encoding on the cd to what is expected
2189 th->starting_track =
2190 bcd2bin(th->starting_track);
2191 th->ending_track = bcd2bin(th->ending_track);
2195 track = th->starting_track;
2196 else if (track == LEADOUT)
2198 else if (track < th->starting_track ||
2199 track > th->ending_track + 1) {
2200 printf("error in readtocentry, "
2201 " returning EINVAL\n");
2207 error = cdreadtoc(periph, te->address_format, track,
2208 (struct cd_toc_entry *)data,
2215 if (softc->quirks & CD_Q_BCD_TRACKS)
2216 data->entry.track = bcd2bin(data->entry.track);
2217 bcopy(&data->entry, &te->entry,
2218 sizeof(struct cd_toc_entry));
2224 struct ioc_patch *arg = (struct ioc_patch *) addr;
2225 struct cd_mode_data *data;
2227 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2228 ("trying to do CDIOCSETPATCH\n"));
2230 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2232 error = cdgetmode(periph, data, AUDIO_PAGE);
2237 data->page.audio.port[LEFT_PORT].channels =
2239 data->page.audio.port[RIGHT_PORT].channels =
2241 data->page.audio.port[2].channels = arg->patch[2];
2242 data->page.audio.port[3].channels = arg->patch[3];
2243 error = cdsetmode(periph, data);
2249 struct ioc_vol *arg = (struct ioc_vol *) addr;
2250 struct cd_mode_data *data;
2252 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2253 ("trying to do CDIOCGETVOL\n"));
2255 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2257 error = cdgetmode(periph, data, AUDIO_PAGE);
2262 arg->vol[LEFT_PORT] =
2263 data->page.audio.port[LEFT_PORT].volume;
2264 arg->vol[RIGHT_PORT] =
2265 data->page.audio.port[RIGHT_PORT].volume;
2266 arg->vol[2] = data->page.audio.port[2].volume;
2267 arg->vol[3] = data->page.audio.port[3].volume;
2273 struct ioc_vol *arg = (struct ioc_vol *) addr;
2274 struct cd_mode_data *data;
2276 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2277 ("trying to do CDIOCSETVOL\n"));
2279 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2281 error = cdgetmode(periph, data, AUDIO_PAGE);
2286 data->page.audio.port[LEFT_PORT].channels = CHANNEL_0;
2287 data->page.audio.port[LEFT_PORT].volume =
2288 arg->vol[LEFT_PORT];
2289 data->page.audio.port[RIGHT_PORT].channels = CHANNEL_1;
2290 data->page.audio.port[RIGHT_PORT].volume =
2291 arg->vol[RIGHT_PORT];
2292 data->page.audio.port[2].volume = arg->vol[2];
2293 data->page.audio.port[3].volume = arg->vol[3];
2294 error = cdsetmode(periph, data);
2300 struct cd_mode_data *data;
2302 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2303 ("trying to do CDIOCSETMONO\n"));
2305 data = malloc(sizeof(struct cd_mode_data),
2307 error = cdgetmode(periph, data, AUDIO_PAGE);
2312 data->page.audio.port[LEFT_PORT].channels =
2313 LEFT_CHANNEL | RIGHT_CHANNEL;
2314 data->page.audio.port[RIGHT_PORT].channels =
2315 LEFT_CHANNEL | RIGHT_CHANNEL;
2316 data->page.audio.port[2].channels = 0;
2317 data->page.audio.port[3].channels = 0;
2318 error = cdsetmode(periph, data);
2322 case CDIOCSETSTEREO:
2324 struct cd_mode_data *data;
2326 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2327 ("trying to do CDIOCSETSTEREO\n"));
2329 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2331 error = cdgetmode(periph, data, AUDIO_PAGE);
2336 data->page.audio.port[LEFT_PORT].channels =
2338 data->page.audio.port[RIGHT_PORT].channels =
2340 data->page.audio.port[2].channels = 0;
2341 data->page.audio.port[3].channels = 0;
2342 error = cdsetmode(periph, data);
2348 struct cd_mode_data *data;
2350 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2351 ("trying to do CDIOCSETMUTE\n"));
2353 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2355 error = cdgetmode(periph, data, AUDIO_PAGE);
2360 data->page.audio.port[LEFT_PORT].channels = 0;
2361 data->page.audio.port[RIGHT_PORT].channels = 0;
2362 data->page.audio.port[2].channels = 0;
2363 data->page.audio.port[3].channels = 0;
2364 error = cdsetmode(periph, data);
2370 struct cd_mode_data *data;
2372 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2373 ("trying to do CDIOCSETLEFT\n"));
2375 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2377 error = cdgetmode(periph, data, AUDIO_PAGE);
2382 data->page.audio.port[LEFT_PORT].channels =
2384 data->page.audio.port[RIGHT_PORT].channels =
2386 data->page.audio.port[2].channels = 0;
2387 data->page.audio.port[3].channels = 0;
2388 error = cdsetmode(periph, data);
2394 struct cd_mode_data *data;
2396 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2397 ("trying to do CDIOCSETRIGHT\n"));
2399 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2401 error = cdgetmode(periph, data, AUDIO_PAGE);
2406 data->page.audio.port[LEFT_PORT].channels =
2408 data->page.audio.port[RIGHT_PORT].channels =
2410 data->page.audio.port[2].channels = 0;
2411 data->page.audio.port[3].channels = 0;
2412 error = cdsetmode(periph, data);
2417 error = cdpause(periph, 1);
2420 error = cdpause(periph, 0);
2423 error = cdstartunit(periph);
2426 error = cdstopunit(periph, 0);
2429 error = cdstopunit(periph, 1);
2432 cdprevent(periph, PR_ALLOW);
2435 cdprevent(periph, PR_PREVENT);
2438 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2442 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2446 /* return (cd_reset(periph)); */
2449 case CDRIOCREADSPEED:
2450 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2452 case CDRIOCWRITESPEED:
2453 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2456 case DVDIOCREPORTKEY: {
2457 struct dvd_authinfo *authinfo;
2459 authinfo = (struct dvd_authinfo *)addr;
2461 if (cmd == DVDIOCREPORTKEY)
2462 error = cdreportkey(periph, authinfo);
2464 error = cdsendkey(periph, authinfo);
2467 case DVDIOCREADSTRUCTURE: {
2468 struct dvd_struct *dvdstruct;
2470 dvdstruct = (struct dvd_struct *)addr;
2472 error = cdreaddvdstructure(periph, dvdstruct);
2477 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2481 cam_periph_unlock(periph);
2483 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2489 cdprevent(struct cam_periph *periph, int action)
2492 struct cd_softc *softc;
2495 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2497 softc = (struct cd_softc *)periph->softc;
2499 if (((action == PR_ALLOW)
2500 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2501 || ((action == PR_PREVENT)
2502 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2506 ccb = cdgetccb(periph, /* priority */ 1);
2508 scsi_prevent(&ccb->csio,
2514 /* timeout */60000);
2516 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2517 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2519 xpt_release_ccb(ccb);
2522 if (action == PR_ALLOW)
2523 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2525 softc->flags |= CD_FLAG_DISC_LOCKED;
2530 cdsize(dev_t dev, u_int32_t *size)
2532 struct cam_periph *periph;
2533 struct cd_softc *softc;
2535 struct scsi_read_capacity_data *rcap_buf;
2538 periph = cam_extend_get(cdperiphs, dkunit(dev));
2543 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2545 softc = (struct cd_softc *)periph->softc;
2547 ccb = cdgetccb(periph, /* priority */ 1);
2549 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2552 scsi_read_capacity(&ccb->csio,
2558 /* timeout */20000);
2560 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2561 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2563 xpt_release_ccb(ccb);
2565 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2566 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2568 * SCSI-3 mandates that the reported blocksize shall be 2048.
2569 * Older drives sometimes report funny values, trim it down to
2570 * 2048, or other parts of the kernel will get confused.
2572 * XXX we leave drives alone that might report 512 bytes, as
2573 * well as drives reporting more weird sizes like perhaps 4K.
2575 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2576 softc->params.blksize = 2048;
2578 free(rcap_buf, M_TEMP);
2579 *size = softc->params.disksize;
2586 * The idea here is to try to figure out whether the first track is data or
2587 * audio. If it is data, we can at least attempt to read a disklabel off
2588 * the first sector of the disk. If it is audio, there won't be a
2591 * This routine returns 0 if the first track is data, and non-zero if there
2592 * is an error or the first track is audio. (If either non-zero case, we
2593 * should not attempt to read the disklabel.)
2596 cdfirsttrackisdata(struct cam_periph *periph)
2599 struct ioc_toc_header header;
2600 struct cd_toc_entry entries[100];
2602 struct cd_softc *softc;
2603 struct ioc_toc_header *th;
2604 struct cdtocdata *data;
2606 int error, first_track_audio;
2609 first_track_audio = -1;
2611 softc = (struct cd_softc *)periph->softc;
2613 data = malloc(sizeof(struct cdtocdata), M_TEMP, M_WAITOK);
2616 error = cdreadtoc(periph, 0, 0, (struct cd_toc_entry *)data,
2622 if (softc->quirks & CD_Q_BCD_TRACKS) {
2623 /* we are going to have to convert the BCD
2624 * encoding on the cd to what is expected
2626 th->starting_track =
2627 bcd2bin(th->starting_track);
2628 th->ending_track = bcd2bin(th->ending_track);
2630 th->len = scsi_2btoul((u_int8_t *)&th->len);
2632 if ((th->len - 2) > 0)
2633 num_entries = (th->len - 2) / sizeof(struct cd_toc_entry);
2637 for (i = 0; i < num_entries; i++) {
2638 if (data->entries[i].track == th->starting_track) {
2639 if (data->entries[i].control & 0x4)
2640 first_track_audio = 0;
2642 first_track_audio = 1;
2647 if (first_track_audio == -1)
2649 else if (first_track_audio == 1)
2660 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2662 struct cd_softc *softc;
2663 struct cam_periph *periph;
2665 periph = xpt_path_periph(ccb->ccb_h.path);
2666 softc = (struct cd_softc *)periph->softc;
2670 * Until we have a better way of doing pack validation,
2671 * don't treat UAs as errors.
2673 sense_flags |= SF_RETRY_UA;
2674 return (cam_periph_error(ccb, cam_flags, sense_flags,
2675 &softc->saved_ccb));
2679 * Read table of contents
2682 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2683 struct cd_toc_entry *data, u_int32_t len)
2685 struct scsi_read_toc *scsi_cmd;
2687 struct ccb_scsiio *csio;
2694 ccb = cdgetccb(periph, /* priority */ 1);
2700 /* cbfcnp */ cddone,
2701 /* flags */ CAM_DIR_IN,
2702 /* tag_action */ MSG_SIMPLE_Q_TAG,
2703 /* data_ptr */ (u_int8_t *)data,
2704 /* dxfer_len */ len,
2705 /* sense_len */ SSD_FULL_SIZE,
2706 sizeof(struct scsi_read_toc),
2707 /* timeout */ 50000);
2709 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2710 bzero (scsi_cmd, sizeof(*scsi_cmd));
2712 if (mode == CD_MSF_FORMAT)
2713 scsi_cmd->byte2 |= CD_MSF;
2714 scsi_cmd->from_track = start;
2715 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
2716 scsi_cmd->data_len[0] = (ntoc) >> 8;
2717 scsi_cmd->data_len[1] = (ntoc) & 0xff;
2719 scsi_cmd->op_code = READ_TOC;
2721 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2722 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2724 xpt_release_ccb(ccb);
2730 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
2731 u_int32_t format, int track,
2732 struct cd_sub_channel_info *data, u_int32_t len)
2734 struct scsi_read_subchannel *scsi_cmd;
2735 struct ccb_scsiio *csio;
2741 ccb = cdgetccb(periph, /* priority */ 1);
2747 /* cbfcnp */ cddone,
2748 /* flags */ CAM_DIR_IN,
2749 /* tag_action */ MSG_SIMPLE_Q_TAG,
2750 /* data_ptr */ (u_int8_t *)data,
2751 /* dxfer_len */ len,
2752 /* sense_len */ SSD_FULL_SIZE,
2753 sizeof(struct scsi_read_subchannel),
2754 /* timeout */ 50000);
2756 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2757 bzero (scsi_cmd, sizeof(*scsi_cmd));
2759 scsi_cmd->op_code = READ_SUBCHANNEL;
2760 if (mode == CD_MSF_FORMAT)
2761 scsi_cmd->byte1 |= CD_MSF;
2762 scsi_cmd->byte2 = SRS_SUBQ;
2763 scsi_cmd->subchan_format = format;
2764 scsi_cmd->track = track;
2765 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
2766 scsi_cmd->control = 0;
2768 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2769 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2771 xpt_release_ccb(ccb);
2778 cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page)
2780 struct scsi_mode_sense_6 *scsi_cmd;
2781 struct ccb_scsiio *csio;
2785 ccb = cdgetccb(periph, /* priority */ 1);
2789 bzero(data, sizeof(*data));
2792 /* cbfcnp */ cddone,
2793 /* flags */ CAM_DIR_IN,
2794 /* tag_action */ MSG_SIMPLE_Q_TAG,
2795 /* data_ptr */ (u_int8_t *)data,
2796 /* dxfer_len */ sizeof(*data),
2797 /* sense_len */ SSD_FULL_SIZE,
2798 sizeof(struct scsi_mode_sense_6),
2799 /* timeout */ 50000);
2801 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
2802 bzero (scsi_cmd, sizeof(*scsi_cmd));
2804 scsi_cmd->page = page;
2805 scsi_cmd->length = sizeof(*data) & 0xff;
2806 scsi_cmd->opcode = MODE_SENSE;
2808 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2809 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2811 xpt_release_ccb(ccb);
2817 cdsetmode(struct cam_periph *periph, struct cd_mode_data *data)
2819 struct scsi_mode_select_6 *scsi_cmd;
2820 struct ccb_scsiio *csio;
2824 ccb = cdgetccb(periph, /* priority */ 1);
2832 /* cbfcnp */ cddone,
2833 /* flags */ CAM_DIR_OUT,
2834 /* tag_action */ MSG_SIMPLE_Q_TAG,
2835 /* data_ptr */ (u_int8_t *)data,
2836 /* dxfer_len */ sizeof(*data),
2837 /* sense_len */ SSD_FULL_SIZE,
2838 sizeof(struct scsi_mode_select_6),
2839 /* timeout */ 50000);
2841 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
2843 bzero(scsi_cmd, sizeof(*scsi_cmd));
2844 scsi_cmd->opcode = MODE_SELECT;
2845 scsi_cmd->byte2 |= SMS_PF;
2846 scsi_cmd->length = sizeof(*data) & 0xff;
2847 data->header.data_length = 0;
2849 * SONY drives do not allow a mode select with a medium_type
2850 * value that has just been returned by a mode sense; use a
2851 * medium_type of 0 (Default) instead.
2853 data->header.medium_type = 0;
2855 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2856 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2858 xpt_release_ccb(ccb);
2865 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2867 struct ccb_scsiio *csio;
2873 ccb = cdgetccb(periph, /* priority */ 1);
2876 * Use the smallest possible command to perform the operation.
2878 if ((len & 0xffff0000) == 0) {
2880 * We can fit in a 10 byte cdb.
2882 struct scsi_play_10 *scsi_cmd;
2884 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
2885 bzero (scsi_cmd, sizeof(*scsi_cmd));
2886 scsi_cmd->op_code = PLAY_10;
2887 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2888 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
2889 cdb_len = sizeof(*scsi_cmd);
2891 struct scsi_play_12 *scsi_cmd;
2893 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
2894 bzero (scsi_cmd, sizeof(*scsi_cmd));
2895 scsi_cmd->op_code = PLAY_12;
2896 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2897 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
2898 cdb_len = sizeof(*scsi_cmd);
2903 /*flags*/CAM_DIR_NONE,
2907 /*sense_len*/SSD_FULL_SIZE,
2909 /*timeout*/50 * 1000);
2911 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2912 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2914 xpt_release_ccb(ccb);
2920 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
2921 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
2923 struct scsi_play_msf *scsi_cmd;
2924 struct ccb_scsiio *csio;
2930 ccb = cdgetccb(periph, /* priority */ 1);
2936 /* cbfcnp */ cddone,
2937 /* flags */ CAM_DIR_NONE,
2938 /* tag_action */ MSG_SIMPLE_Q_TAG,
2939 /* data_ptr */ NULL,
2941 /* sense_len */ SSD_FULL_SIZE,
2942 sizeof(struct scsi_play_msf),
2943 /* timeout */ 50000);
2945 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2946 bzero (scsi_cmd, sizeof(*scsi_cmd));
2948 scsi_cmd->op_code = PLAY_MSF;
2949 scsi_cmd->start_m = startm;
2950 scsi_cmd->start_s = starts;
2951 scsi_cmd->start_f = startf;
2952 scsi_cmd->end_m = endm;
2953 scsi_cmd->end_s = ends;
2954 scsi_cmd->end_f = endf;
2956 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2957 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2959 xpt_release_ccb(ccb);
2966 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
2967 u_int32_t etrack, u_int32_t eindex)
2969 struct scsi_play_track *scsi_cmd;
2970 struct ccb_scsiio *csio;
2976 ccb = cdgetccb(periph, /* priority */ 1);
2982 /* cbfcnp */ cddone,
2983 /* flags */ CAM_DIR_NONE,
2984 /* tag_action */ MSG_SIMPLE_Q_TAG,
2985 /* data_ptr */ NULL,
2987 /* sense_len */ SSD_FULL_SIZE,
2988 sizeof(struct scsi_play_track),
2989 /* timeout */ 50000);
2991 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
2992 bzero (scsi_cmd, sizeof(*scsi_cmd));
2994 scsi_cmd->op_code = PLAY_TRACK;
2995 scsi_cmd->start_track = strack;
2996 scsi_cmd->start_index = sindex;
2997 scsi_cmd->end_track = etrack;
2998 scsi_cmd->end_index = eindex;
3000 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3001 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3003 xpt_release_ccb(ccb);
3009 cdpause(struct cam_periph *periph, u_int32_t go)
3011 struct scsi_pause *scsi_cmd;
3012 struct ccb_scsiio *csio;
3018 ccb = cdgetccb(periph, /* priority */ 1);
3024 /* cbfcnp */ cddone,
3025 /* flags */ CAM_DIR_NONE,
3026 /* tag_action */ MSG_SIMPLE_Q_TAG,
3027 /* data_ptr */ NULL,
3029 /* sense_len */ SSD_FULL_SIZE,
3030 sizeof(struct scsi_pause),
3031 /* timeout */ 50000);
3033 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3034 bzero (scsi_cmd, sizeof(*scsi_cmd));
3036 scsi_cmd->op_code = PAUSE;
3037 scsi_cmd->resume = go;
3039 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3040 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3042 xpt_release_ccb(ccb);
3048 cdstartunit(struct cam_periph *periph)
3055 ccb = cdgetccb(periph, /* priority */ 1);
3057 scsi_start_stop(&ccb->csio,
3059 /* cbfcnp */ cddone,
3060 /* tag_action */ MSG_SIMPLE_Q_TAG,
3062 /* load_eject */ FALSE,
3063 /* immediate */ FALSE,
3064 /* sense_len */ SSD_FULL_SIZE,
3065 /* timeout */ 50000);
3067 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3068 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3070 xpt_release_ccb(ccb);
3076 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3083 ccb = cdgetccb(periph, /* priority */ 1);
3085 scsi_start_stop(&ccb->csio,
3087 /* cbfcnp */ cddone,
3088 /* tag_action */ MSG_SIMPLE_Q_TAG,
3090 /* load_eject */ eject,
3091 /* immediate */ FALSE,
3092 /* sense_len */ SSD_FULL_SIZE,
3093 /* timeout */ 50000);
3095 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3096 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3098 xpt_release_ccb(ccb);
3104 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3106 struct scsi_set_speed *scsi_cmd;
3107 struct ccb_scsiio *csio;
3112 ccb = cdgetccb(periph, /* priority */ 1);
3115 /* Preserve old behavior: units in multiples of CDROM speed */
3123 /* cbfcnp */ cddone,
3124 /* flags */ CAM_DIR_NONE,
3125 /* tag_action */ MSG_SIMPLE_Q_TAG,
3126 /* data_ptr */ NULL,
3128 /* sense_len */ SSD_FULL_SIZE,
3129 sizeof(struct scsi_set_speed),
3130 /* timeout */ 50000);
3132 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3133 bzero(scsi_cmd, sizeof(*scsi_cmd));
3135 scsi_cmd->opcode = SET_CD_SPEED;
3136 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3137 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3139 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3140 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3142 xpt_release_ccb(ccb);
3148 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3160 ccb = cdgetccb(periph, /* priority */ 1);
3162 switch (authinfo->format) {
3163 case DVD_REPORT_AGID:
3164 length = sizeof(struct scsi_report_key_data_agid);
3166 case DVD_REPORT_CHALLENGE:
3167 length = sizeof(struct scsi_report_key_data_challenge);
3169 case DVD_REPORT_KEY1:
3170 length = sizeof(struct scsi_report_key_data_key1_key2);
3172 case DVD_REPORT_TITLE_KEY:
3173 length = sizeof(struct scsi_report_key_data_title);
3174 /* The lba field is only set for the title key */
3175 lba = authinfo->lba;
3177 case DVD_REPORT_ASF:
3178 length = sizeof(struct scsi_report_key_data_asf);
3180 case DVD_REPORT_RPC:
3181 length = sizeof(struct scsi_report_key_data_rpc);
3183 case DVD_INVALIDATE_AGID:
3189 break; /* NOTREACHED */
3193 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3198 scsi_report_key(&ccb->csio,
3200 /* cbfcnp */ cddone,
3201 /* tag_action */ MSG_SIMPLE_Q_TAG,
3203 /* agid */ authinfo->agid,
3204 /* key_format */ authinfo->format,
3205 /* data_ptr */ databuf,
3206 /* dxfer_len */ length,
3207 /* sense_len */ SSD_FULL_SIZE,
3208 /* timeout */ 50000);
3210 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3211 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3216 if (ccb->csio.resid != 0) {
3217 xpt_print_path(periph->path);
3218 printf("warning, residual for report key command is %d\n",
3222 switch(authinfo->format) {
3223 case DVD_REPORT_AGID: {
3224 struct scsi_report_key_data_agid *agid_data;
3226 agid_data = (struct scsi_report_key_data_agid *)databuf;
3228 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3232 case DVD_REPORT_CHALLENGE: {
3233 struct scsi_report_key_data_challenge *chal_data;
3235 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3237 bcopy(chal_data->challenge_key, authinfo->keychal,
3238 min(sizeof(chal_data->challenge_key),
3239 sizeof(authinfo->keychal)));
3242 case DVD_REPORT_KEY1: {
3243 struct scsi_report_key_data_key1_key2 *key1_data;
3245 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3247 bcopy(key1_data->key1, authinfo->keychal,
3248 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3251 case DVD_REPORT_TITLE_KEY: {
3252 struct scsi_report_key_data_title *title_data;
3254 title_data = (struct scsi_report_key_data_title *)databuf;
3256 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3257 RKD_TITLE_CPM_SHIFT;
3258 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3259 RKD_TITLE_CP_SEC_SHIFT;
3260 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3261 RKD_TITLE_CMGS_SHIFT;
3262 bcopy(title_data->title_key, authinfo->keychal,
3263 min(sizeof(title_data->title_key),
3264 sizeof(authinfo->keychal)));
3267 case DVD_REPORT_ASF: {
3268 struct scsi_report_key_data_asf *asf_data;
3270 asf_data = (struct scsi_report_key_data_asf *)databuf;
3272 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3275 case DVD_REPORT_RPC: {
3276 struct scsi_report_key_data_rpc *rpc_data;
3278 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3280 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3282 authinfo->vend_rsts =
3283 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3284 RKD_RPC_VENDOR_RESET_SHIFT;
3285 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3286 authinfo->region = rpc_data->region_mask;
3287 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3290 case DVD_INVALIDATE_AGID:
3293 /* This should be impossible, since we checked above */
3296 break; /* NOTREACHED */
3299 if (databuf != NULL)
3300 free(databuf, M_DEVBUF);
3302 xpt_release_ccb(ccb);
3308 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3318 ccb = cdgetccb(periph, /* priority */ 1);
3320 switch(authinfo->format) {
3321 case DVD_SEND_CHALLENGE: {
3322 struct scsi_report_key_data_challenge *challenge_data;
3324 length = sizeof(*challenge_data);
3326 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3328 databuf = (u_int8_t *)challenge_data;
3330 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3331 challenge_data->data_len);
3333 bcopy(authinfo->keychal, challenge_data->challenge_key,
3334 min(sizeof(authinfo->keychal),
3335 sizeof(challenge_data->challenge_key)));
3338 case DVD_SEND_KEY2: {
3339 struct scsi_report_key_data_key1_key2 *key2_data;
3341 length = sizeof(*key2_data);
3343 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3345 databuf = (u_int8_t *)key2_data;
3347 scsi_ulto2b(length - sizeof(key2_data->data_len),
3348 key2_data->data_len);
3350 bcopy(authinfo->keychal, key2_data->key1,
3351 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3355 case DVD_SEND_RPC: {
3356 struct scsi_send_key_data_rpc *rpc_data;
3358 length = sizeof(*rpc_data);
3360 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3362 databuf = (u_int8_t *)rpc_data;
3364 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3365 rpc_data->data_len);
3367 rpc_data->region_code = authinfo->region;
3373 break; /* NOTREACHED */
3376 scsi_send_key(&ccb->csio,
3378 /* cbfcnp */ cddone,
3379 /* tag_action */ MSG_SIMPLE_Q_TAG,
3380 /* agid */ authinfo->agid,
3381 /* key_format */ authinfo->format,
3382 /* data_ptr */ databuf,
3383 /* dxfer_len */ length,
3384 /* sense_len */ SSD_FULL_SIZE,
3385 /* timeout */ 50000);
3387 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3388 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3392 if (databuf != NULL)
3393 free(databuf, M_DEVBUF);
3395 xpt_release_ccb(ccb);
3401 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3411 /* The address is reserved for many of the formats */
3414 ccb = cdgetccb(periph, /* priority */ 1);
3416 switch(dvdstruct->format) {
3417 case DVD_STRUCT_PHYSICAL:
3418 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3420 case DVD_STRUCT_COPYRIGHT:
3421 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3423 case DVD_STRUCT_DISCKEY:
3424 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3426 case DVD_STRUCT_BCA:
3427 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3429 case DVD_STRUCT_MANUFACT:
3430 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3432 case DVD_STRUCT_CMI:
3436 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
3437 address = dvdstruct->address;
3439 break; /* NOTREACHED */
3440 case DVD_STRUCT_PROTDISCID:
3441 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3443 case DVD_STRUCT_DISCKEYBLOCK:
3444 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3446 case DVD_STRUCT_DDS:
3447 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3449 case DVD_STRUCT_MEDIUM_STAT:
3450 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3452 case DVD_STRUCT_SPARE_AREA:
3453 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3455 case DVD_STRUCT_RMD_LAST:
3459 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
3460 address = dvdstruct->address;
3462 break; /* NOTREACHED */
3463 case DVD_STRUCT_RMD_RMA:
3467 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
3468 address = dvdstruct->address;
3470 break; /* NOTREACHED */
3471 case DVD_STRUCT_PRERECORDED:
3472 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3474 case DVD_STRUCT_UNIQUEID:
3475 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3477 case DVD_STRUCT_DCB:
3481 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
3482 address = dvdstruct->address;
3484 break; /* NOTREACHED */
3485 case DVD_STRUCT_LIST:
3487 * This is the maximum allocation length for the READ DVD
3488 * STRUCTURE command. There's nothing in the MMC3 spec
3489 * that indicates a limit in the amount of data that can
3490 * be returned from this call, other than the limits
3491 * imposed by the 2-byte length variables.
3498 break; /* NOTREACHED */
3502 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3506 scsi_read_dvd_structure(&ccb->csio,
3508 /* cbfcnp */ cddone,
3509 /* tag_action */ MSG_SIMPLE_Q_TAG,
3511 /* layer_number */ dvdstruct->layer_num,
3512 /* key_format */ dvdstruct->format,
3513 /* agid */ dvdstruct->agid,
3514 /* data_ptr */ databuf,
3515 /* dxfer_len */ length,
3516 /* sense_len */ SSD_FULL_SIZE,
3517 /* timeout */ 50000);
3519 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3520 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3525 switch(dvdstruct->format) {
3526 case DVD_STRUCT_PHYSICAL: {
3527 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
3528 struct dvd_layer *outlayer;
3529 struct scsi_read_dvd_struct_data_physical *phys_data;
3532 (struct scsi_read_dvd_struct_data_physical *)databuf;
3533 inlayer = &phys_data->layer_desc;
3534 outlayer = (struct dvd_layer *)&dvdstruct->data;
3536 dvdstruct->length = sizeof(*inlayer);
3538 outlayer->book_type = (inlayer->book_type_version &
3539 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
3540 outlayer->book_version = (inlayer->book_type_version &
3541 RDSD_BOOK_VERSION_MASK);
3542 outlayer->disc_size = (inlayer->disc_size_max_rate &
3543 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
3544 outlayer->max_rate = (inlayer->disc_size_max_rate &
3545 RDSD_MAX_RATE_MASK);
3546 outlayer->nlayers = (inlayer->layer_info &
3547 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
3548 outlayer->track_path = (inlayer->layer_info &
3549 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
3550 outlayer->layer_type = (inlayer->layer_info &
3551 RDSD_LAYER_TYPE_MASK);
3552 outlayer->linear_density = (inlayer->density &
3553 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
3554 outlayer->track_density = (inlayer->density &
3555 RDSD_TRACK_DENSITY_MASK);
3556 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
3558 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
3559 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
3560 outlayer->end_sector_l0 =
3561 scsi_3btoul(inlayer->end_sector_layer0);
3564 case DVD_STRUCT_COPYRIGHT: {
3565 struct scsi_read_dvd_struct_data_copyright *copy_data;
3567 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3570 dvdstruct->cpst = copy_data->cps_type;
3571 dvdstruct->rmi = copy_data->region_info;
3572 dvdstruct->length = 0;
3578 * Tell the user what the overall length is, no matter
3579 * what we can actually fit in the data buffer.
3581 dvdstruct->length = length - ccb->csio.resid -
3582 sizeof(struct scsi_read_dvd_struct_data_header);
3585 * But only actually copy out the smaller of what we read
3586 * in or what the structure can take.
3588 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3590 min(sizeof(dvdstruct->data), dvdstruct->length));
3595 if (databuf != NULL)
3596 free(databuf, M_DEVBUF);
3598 xpt_release_ccb(ccb);
3604 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
3605 void (*cbfcnp)(struct cam_periph *, union ccb *),
3606 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
3607 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
3608 u_int8_t sense_len, u_int32_t timeout)
3610 struct scsi_report_key *scsi_cmd;
3612 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
3613 bzero(scsi_cmd, sizeof(*scsi_cmd));
3614 scsi_cmd->opcode = REPORT_KEY;
3615 scsi_ulto4b(lba, scsi_cmd->lba);
3616 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3617 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3618 (key_format & RK_KF_KEYFORMAT_MASK);
3623 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3625 /*data_ptr*/ data_ptr,
3626 /*dxfer_len*/ dxfer_len,
3633 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
3634 void (*cbfcnp)(struct cam_periph *, union ccb *),
3635 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
3636 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3639 struct scsi_send_key *scsi_cmd;
3641 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
3642 bzero(scsi_cmd, sizeof(*scsi_cmd));
3643 scsi_cmd->opcode = SEND_KEY;
3645 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
3646 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3647 (key_format & RK_KF_KEYFORMAT_MASK);
3652 /*flags*/ CAM_DIR_OUT,
3654 /*data_ptr*/ data_ptr,
3655 /*dxfer_len*/ dxfer_len,
3663 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
3664 void (*cbfcnp)(struct cam_periph *, union ccb *),
3665 u_int8_t tag_action, u_int32_t address,
3666 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
3667 u_int8_t *data_ptr, u_int32_t dxfer_len,
3668 u_int8_t sense_len, u_int32_t timeout)
3670 struct scsi_read_dvd_structure *scsi_cmd;
3672 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
3673 bzero(scsi_cmd, sizeof(*scsi_cmd));
3674 scsi_cmd->opcode = READ_DVD_STRUCTURE;
3676 scsi_ulto4b(address, scsi_cmd->address);
3677 scsi_cmd->layer_number = layer_number;
3678 scsi_cmd->format = format;
3679 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3680 /* The AGID is the top two bits of this byte */
3681 scsi_cmd->agid = agid << 6;
3686 /*flags*/ CAM_DIR_IN,
3688 /*data_ptr*/ data_ptr,
3689 /*dxfer_len*/ dxfer_len,