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.5 2003/07/19 21:14:14 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/systm.h>
54 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/cdrio.h>
61 #include <sys/dvdio.h>
62 #include <sys/devicestat.h>
63 #include <sys/sysctl.h>
68 #include <cam/cam_ccb.h>
69 #include <cam/cam_extend.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
74 #include <cam/scsi/scsi_message.h>
75 #include <cam/scsi/scsi_da.h>
76 #include <cam/scsi/scsi_cd.h>
78 #define LEADOUT 0xaa /* leadout toc entry */
88 CD_Q_BCD_TRACKS = 0x02,
89 CD_Q_NO_CHANGER = 0x04,
94 CD_FLAG_INVALID = 0x001,
95 CD_FLAG_NEW_DISC = 0x002,
96 CD_FLAG_DISC_LOCKED = 0x004,
97 CD_FLAG_DISC_REMOVABLE = 0x008,
98 CD_FLAG_TAGGED_QUEUING = 0x010,
99 CD_FLAG_CHANGER = 0x040,
100 CD_FLAG_ACTIVE = 0x080,
101 CD_FLAG_SCHED_ON_COMP = 0x100,
102 CD_FLAG_RETRY_UA = 0x200
107 CD_CCB_BUFFER_IO = 0x02,
108 CD_CCB_WAITING = 0x03,
109 CD_CCB_TYPE_MASK = 0x0F,
110 CD_CCB_RETRY_UA = 0x10
114 CHANGER_TIMEOUT_SCHED = 0x01,
115 CHANGER_SHORT_TMOUT_SCHED = 0x02,
116 CHANGER_MANUAL_CALL = 0x04,
117 CHANGER_NEED_TIMEOUT = 0x08
120 #define ccb_state ppriv_field0
121 #define ccb_bp ppriv_ptr1
131 volatile cd_flags flags;
132 struct buf_queue_head buf_queue;
133 LIST_HEAD(, ccb_hdr) pending_ccbs;
134 struct cd_params params;
138 struct devstat device_stats;
139 STAILQ_ENTRY(cd_softc) changer_links;
140 struct cdchanger *changer;
142 struct cam_periph *periph;
145 struct cd_quirk_entry {
146 struct scsi_inquiry_pattern inq_pat;
151 * These quirk entries aren't strictly necessary. Basically, what they do
152 * is tell cdregister() up front that a device is a changer. Otherwise, it
153 * will figure that fact out once it sees a LUN on the device that is
154 * greater than 0. If it is known up front that a device is a changer, all
155 * I/O to the device will go through the changer scheduling routines, as
156 * opposed to the "normal" CD code.
158 static struct cd_quirk_entry cd_quirk_table[] =
161 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
162 /*quirks*/ CD_Q_CHANGER
165 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
166 "*"}, /* quirks */ CD_Q_CHANGER
169 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
170 /* quirks */ CD_Q_CHANGER
173 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
174 /* quirks */ CD_Q_BCD_TRACKS
179 #define MIN(x,y) ((x<y) ? x : y)
182 #define CD_CDEV_MAJOR 15
183 #define CD_BDEV_MAJOR 6
185 static d_open_t cdopen;
186 static d_close_t cdclose;
187 static d_ioctl_t cdioctl;
188 static d_strategy_t cdstrategy;
190 static periph_init_t cdinit;
191 static periph_ctor_t cdregister;
192 static periph_dtor_t cdcleanup;
193 static periph_start_t cdstart;
194 static periph_oninv_t cdoninvalidate;
195 static void cdasync(void *callback_arg, u_int32_t code,
196 struct cam_path *path, void *arg);
197 static void cdshorttimeout(void *arg);
198 static void cdschedule(struct cam_periph *periph, int priority);
199 static void cdrunchangerqueue(void *arg);
200 static void cdchangerschedule(struct cd_softc *softc);
201 static int cdrunccb(union ccb *ccb,
202 int (*error_routine)(union ccb *ccb,
204 u_int32_t sense_flags),
205 u_int32_t cam_flags, u_int32_t sense_flags);
206 static union ccb *cdgetccb(struct cam_periph *periph,
208 static void cddone(struct cam_periph *periph,
209 union ccb *start_ccb);
210 static int cderror(union ccb *ccb, u_int32_t cam_flags,
211 u_int32_t sense_flags);
212 static void cdprevent(struct cam_periph *periph, int action);
213 static int cdsize(dev_t dev, u_int32_t *size);
214 static int cdfirsttrackisdata(struct cam_periph *periph);
215 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
216 u_int32_t start, struct cd_toc_entry *data,
218 static int cdgetmode(struct cam_periph *periph,
219 struct cd_mode_data *data, u_int32_t page);
220 static int cdsetmode(struct cam_periph *periph,
221 struct cd_mode_data *data);
222 static int cdplay(struct cam_periph *periph, u_int32_t blk,
224 static int cdreadsubchannel(struct cam_periph *periph,
225 u_int32_t mode, u_int32_t format,
227 struct cd_sub_channel_info *data,
229 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
230 u_int32_t starts, u_int32_t startf,
231 u_int32_t endm, u_int32_t ends,
233 static int cdplaytracks(struct cam_periph *periph,
234 u_int32_t strack, u_int32_t sindex,
235 u_int32_t etrack, u_int32_t eindex);
236 static int cdpause(struct cam_periph *periph, u_int32_t go);
237 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
238 static int cdstartunit(struct cam_periph *periph);
239 static int cdsetspeed(struct cam_periph *periph,
240 u_int32_t rdspeed, u_int32_t wrspeed);
241 static int cdreportkey(struct cam_periph *periph,
242 struct dvd_authinfo *authinfo);
243 static int cdsendkey(struct cam_periph *periph,
244 struct dvd_authinfo *authinfo);
245 static int cdreaddvdstructure(struct cam_periph *periph,
246 struct dvd_struct *dvdstruct);
248 static struct periph_driver cddriver =
251 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
254 DATA_SET(periphdriver_set, cddriver);
256 /* For 2.2-stable support */
260 static struct cdevsw cd_cdevsw = {
264 /* write */ physwrite,
268 /* strategy */ cdstrategy,
270 /* maj */ CD_CDEV_MAJOR,
274 /* bmaj */ CD_BDEV_MAJOR
276 static struct cdevsw cddisk_cdevsw;
278 static struct extend_array *cdperiphs;
279 static int num_changers;
281 #ifndef CHANGER_MIN_BUSY_SECONDS
282 #define CHANGER_MIN_BUSY_SECONDS 5
284 #ifndef CHANGER_MAX_BUSY_SECONDS
285 #define CHANGER_MAX_BUSY_SECONDS 15
288 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
289 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
292 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
295 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
296 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
297 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
298 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
299 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
300 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
301 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
305 target_id_t target_id;
308 struct timeval start_time;
309 struct cd_softc *cur_device;
310 struct callout_handle short_handle;
311 struct callout_handle long_handle;
312 volatile cd_changer_flags flags;
313 STAILQ_ENTRY(cdchanger) changer_links;
314 STAILQ_HEAD(chdevlist, cd_softc) chluns;
317 static STAILQ_HEAD(changerlist, cdchanger) changerq;
323 struct cam_path *path;
326 * Create our extend array for storing the devices we attach to.
328 cdperiphs = cam_extend_new();
329 if (cdperiphs == NULL) {
330 printf("cd: Failed to alloc extend array!\n");
335 * Install a global async callback. This callback will
336 * receive async callbacks like "new device found".
338 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
339 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
341 if (status == CAM_REQ_CMP) {
342 struct ccb_setasync csa;
344 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
345 csa.ccb_h.func_code = XPT_SASYNC_CB;
346 csa.event_enable = AC_FOUND_DEVICE;
347 csa.callback = cdasync;
348 csa.callback_arg = NULL;
349 xpt_action((union ccb *)&csa);
350 status = csa.ccb_h.status;
354 if (status != CAM_REQ_CMP) {
355 printf("cd: Failed to attach master async callback "
356 "due to status 0x%x!\n", status);
361 cdoninvalidate(struct cam_periph *periph)
364 struct cd_softc *softc;
366 struct ccb_setasync csa;
368 softc = (struct cd_softc *)periph->softc;
371 * De-register any async callbacks.
373 xpt_setup_ccb(&csa.ccb_h, periph->path,
375 csa.ccb_h.func_code = XPT_SASYNC_CB;
376 csa.event_enable = 0;
377 csa.callback = cdasync;
378 csa.callback_arg = periph;
379 xpt_action((union ccb *)&csa);
381 softc->flags |= CD_FLAG_INVALID;
384 * Although the oninvalidate() routines are always called at
385 * splsoftcam, we need to be at splbio() here to keep the buffer
386 * queue from being modified while we traverse it.
391 * Return all queued I/O with ENXIO.
392 * XXX Handle any transactions queued to the card
393 * with XPT_ABORT_CCB.
395 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
396 bufq_remove(&softc->buf_queue, q_bp);
397 q_bp->b_resid = q_bp->b_bcount;
398 q_bp->b_error = ENXIO;
399 q_bp->b_flags |= B_ERROR;
405 * If this device is part of a changer, and it was scheduled
406 * to run, remove it from the run queue since we just nuked
407 * all of its scheduled I/O.
409 if ((softc->flags & CD_FLAG_CHANGER)
410 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
411 camq_remove(&softc->changer->devq, softc->pinfo.index);
413 xpt_print_path(periph->path);
414 printf("lost device\n");
418 cdcleanup(struct cam_periph *periph)
420 struct cd_softc *softc;
423 softc = (struct cd_softc *)periph->softc;
425 xpt_print_path(periph->path);
426 printf("removing device entry\n");
430 * In the queued, non-active case, the device in question
431 * has already been removed from the changer run queue. Since this
432 * device is active, we need to de-activate it, and schedule
433 * another device to run. (if there is another one to run)
435 if ((softc->flags & CD_FLAG_CHANGER)
436 && (softc->flags & CD_FLAG_ACTIVE)) {
439 * The purpose of the short timeout is soley to determine
440 * whether the current device has finished or not. Well,
441 * since we're removing the active device, we know that it
442 * is finished. So, get rid of the short timeout.
443 * Otherwise, if we're in the time period before the short
444 * timeout fires, and there are no other devices in the
445 * queue to run, there won't be any other device put in the
446 * active slot. i.e., when we call cdrunchangerqueue()
447 * below, it won't do anything. Then, when the short
448 * timeout fires, it'll look at the "current device", which
449 * we are free below, and possibly panic the kernel on a
450 * bogus pointer reference.
452 * The long timeout doesn't really matter, since we
453 * decrement the qfrozen_cnt to indicate that there is
454 * nothing in the active slot now. Therefore, there won't
455 * be any bogus pointer references there.
457 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
458 untimeout(cdshorttimeout, softc->changer,
459 softc->changer->short_handle);
460 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
462 softc->changer->devq.qfrozen_cnt--;
463 softc->changer->flags |= CHANGER_MANUAL_CALL;
464 cdrunchangerqueue(softc->changer);
468 * If we're removing the last device on the changer, go ahead and
469 * remove the changer device structure.
471 if ((softc->flags & CD_FLAG_CHANGER)
472 && (--softc->changer->num_devices == 0)) {
475 * Theoretically, there shouldn't be any timeouts left, but
476 * I'm not completely sure that that will be the case. So,
477 * it won't hurt to check and see if there are any left.
479 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
480 untimeout(cdrunchangerqueue, softc->changer,
481 softc->changer->long_handle);
482 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
485 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
486 untimeout(cdshorttimeout, softc->changer,
487 softc->changer->short_handle);
488 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
491 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
493 xpt_print_path(periph->path);
494 printf("removing changer entry\n");
495 free(softc->changer, M_DEVBUF);
498 devstat_remove_entry(&softc->device_stats);
499 cam_extend_release(cdperiphs, periph->unit_number);
500 if (softc->disk.d_dev) {
501 disk_destroy(softc->disk.d_dev);
503 free(softc, M_DEVBUF);
508 cdasync(void *callback_arg, u_int32_t code,
509 struct cam_path *path, void *arg)
511 struct cam_periph *periph;
513 periph = (struct cam_periph *)callback_arg;
515 case AC_FOUND_DEVICE:
517 struct ccb_getdev *cgd;
520 cgd = (struct ccb_getdev *)arg;
522 if (SID_TYPE(&cgd->inq_data) != T_CDROM
523 && SID_TYPE(&cgd->inq_data) != T_WORM)
527 * Allocate a peripheral instance for
528 * this device and start the probe
531 status = cam_periph_alloc(cdregister, cdoninvalidate,
533 "cd", CAM_PERIPH_BIO,
534 cgd->ccb_h.path, cdasync,
535 AC_FOUND_DEVICE, cgd);
537 if (status != CAM_REQ_CMP
538 && status != CAM_REQ_INPROG)
539 printf("cdasync: Unable to attach new device "
540 "due to status 0x%x\n", status);
547 struct cd_softc *softc;
548 struct ccb_hdr *ccbh;
551 softc = (struct cd_softc *)periph->softc;
554 * Don't fail on the expected unit attention
557 softc->flags |= CD_FLAG_RETRY_UA;
558 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
559 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
560 ccbh->ccb_state |= CD_CCB_RETRY_UA;
565 cam_periph_async(periph, code, path, arg);
571 cdregister(struct cam_periph *periph, void *arg)
573 struct cd_softc *softc;
574 struct ccb_setasync csa;
575 struct ccb_getdev *cgd;
578 cgd = (struct ccb_getdev *)arg;
579 if (periph == NULL) {
580 printf("cdregister: periph was NULL!!\n");
581 return(CAM_REQ_CMP_ERR);
584 printf("cdregister: no getdev CCB, can't register device\n");
585 return(CAM_REQ_CMP_ERR);
588 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
591 printf("cdregister: Unable to probe new device. "
592 "Unable to allocate softc\n");
593 return(CAM_REQ_CMP_ERR);
596 bzero(softc, sizeof(*softc));
597 LIST_INIT(&softc->pending_ccbs);
598 softc->state = CD_STATE_PROBE;
599 bufq_init(&softc->buf_queue);
600 if (SID_IS_REMOVABLE(&cgd->inq_data))
601 softc->flags |= CD_FLAG_DISC_REMOVABLE;
602 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
603 softc->flags |= CD_FLAG_TAGGED_QUEUING;
605 periph->softc = softc;
606 softc->periph = periph;
608 cam_extend_set(cdperiphs, periph->unit_number, periph);
611 * See if this device has any quirks.
613 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
614 (caddr_t)cd_quirk_table,
615 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
616 sizeof(*cd_quirk_table), scsi_inquiry_match);
619 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
621 softc->quirks = CD_Q_NONE;
624 * We need to register the statistics structure for this device,
625 * but we don't have the blocksize yet for it. So, we register
626 * the structure and indicate that we don't have the blocksize
627 * yet. Unlike other SCSI peripheral drivers, we explicitly set
628 * the device type here to be CDROM, rather than just ORing in
629 * the device type. This is because this driver can attach to either
630 * CDROM or WORM devices, and we want this peripheral driver to
631 * show up in the devstat list as a CD peripheral driver, not a
632 * WORM peripheral driver. WORM drives will also have the WORM
633 * driver attached to them.
635 devstat_add_entry(&softc->device_stats, "cd",
636 periph->unit_number, 0,
637 DEVSTAT_BS_UNAVAILABLE,
638 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
639 DEVSTAT_PRIORITY_CD);
640 disk_create(periph->unit_number, &softc->disk,
641 DSO_ONESLICE | DSO_COMPATLABEL,
642 &cd_cdevsw, &cddisk_cdevsw);
645 * Add an async callback so that we get
646 * notified if this device goes away.
648 xpt_setup_ccb(&csa.ccb_h, periph->path,
650 csa.ccb_h.func_code = XPT_SASYNC_CB;
651 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
652 csa.callback = cdasync;
653 csa.callback_arg = periph;
654 xpt_action((union ccb *)&csa);
657 * If the target lun is greater than 0, we most likely have a CD
658 * changer device. Check the quirk entries as well, though, just
659 * in case someone has a CD tower with one lun per drive or
660 * something like that. Also, if we know up front that a
661 * particular device is a changer, we can mark it as such starting
662 * with lun 0, instead of lun 1. It shouldn't be necessary to have
663 * a quirk entry to define something as a changer, however.
665 if (((cgd->ccb_h.target_lun > 0)
666 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
667 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
668 struct cdchanger *nchanger;
669 struct cam_periph *nperiph;
670 struct cam_path *path;
674 /* Set the changer flag in the current device's softc */
675 softc->flags |= CD_FLAG_CHANGER;
677 if (num_changers == 0)
678 STAILQ_INIT(&changerq);
681 * Now, look around for an existing changer device with the
682 * same path and target ID as the current device.
685 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
687 nchanger = STAILQ_NEXT(nchanger, changer_links)){
688 if ((nchanger->path_id == cgd->ccb_h.path_id)
689 && (nchanger->target_id == cgd->ccb_h.target_id)) {
696 * If we found a matching entry, just add this device to
697 * the list of devices on this changer.
700 struct chdevlist *chlunhead;
702 chlunhead = &nchanger->chluns;
705 * XXX KDM look at consolidating this code with the
706 * code below in a separate function.
710 * Create a path with lun id 0, and see if we can
711 * find a matching device
713 status = xpt_create_path(&path, /*periph*/ periph,
715 cgd->ccb_h.target_id, 0);
717 if ((status == CAM_REQ_CMP)
718 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
719 struct cd_softc *nsoftc;
721 nsoftc = (struct cd_softc *)nperiph->softc;
723 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
724 nsoftc->flags |= CD_FLAG_CHANGER;
725 nchanger->num_devices++;
726 if (camq_resize(&nchanger->devq,
727 nchanger->num_devices)!=CAM_REQ_CMP){
728 printf("cdregister: "
734 nsoftc->changer = nchanger;
735 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
737 STAILQ_INSERT_TAIL(&nchanger->chluns,
738 nsoftc,changer_links);
741 } else if (status == CAM_REQ_CMP)
744 printf("cdregister: unable to allocate path\n"
745 "cdregister: changer support may be "
749 nchanger->num_devices++;
751 softc->changer = nchanger;
752 softc->pinfo.index = CAM_UNQUEUED_INDEX;
754 if (camq_resize(&nchanger->devq,
755 nchanger->num_devices) != CAM_REQ_CMP) {
756 printf("cdregister: camq_resize "
757 "failed, changer support may "
761 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
764 * In this case, we don't already have an entry for this
765 * particular changer, so we need to create one, add it to
766 * the queue, and queue this device on the list for this
767 * changer. Before we queue this device, however, we need
768 * to search for lun id 0 on this target, and add it to the
769 * queue first, if it exists. (and if it hasn't already
770 * been marked as part of the changer.)
773 nchanger = malloc(sizeof(struct cdchanger),
776 if (nchanger == NULL) {
777 softc->flags &= ~CD_FLAG_CHANGER;
778 printf("cdregister: unable to malloc "
779 "changer structure\ncdregister: "
780 "changer support disabled\n");
783 * Yes, gotos can be gross but in this case
784 * I think it's justified..
789 /* zero the structure */
790 bzero(nchanger, sizeof(struct cdchanger));
792 if (camq_init(&nchanger->devq, 1) != 0) {
793 softc->flags &= ~CD_FLAG_CHANGER;
794 printf("cdregister: changer support "
801 nchanger->path_id = cgd->ccb_h.path_id;
802 nchanger->target_id = cgd->ccb_h.target_id;
804 /* this is superfluous, but it makes things clearer */
805 nchanger->num_devices = 0;
807 STAILQ_INIT(&nchanger->chluns);
809 STAILQ_INSERT_TAIL(&changerq, nchanger,
813 * Create a path with lun id 0, and see if we can
814 * find a matching device
816 status = xpt_create_path(&path, /*periph*/ periph,
818 cgd->ccb_h.target_id, 0);
821 * If we were able to allocate the path, and if we
822 * find a matching device and it isn't already
823 * marked as part of a changer, then we add it to
824 * the current changer.
826 if ((status == CAM_REQ_CMP)
827 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
828 && ((((struct cd_softc *)periph->softc)->flags &
829 CD_FLAG_CHANGER) == 0)) {
830 struct cd_softc *nsoftc;
832 nsoftc = (struct cd_softc *)nperiph->softc;
834 nsoftc->flags |= CD_FLAG_CHANGER;
835 nchanger->num_devices++;
836 if (camq_resize(&nchanger->devq,
837 nchanger->num_devices) != CAM_REQ_CMP) {
838 printf("cdregister: camq_resize "
839 "failed, changer support may "
842 nsoftc->changer = nchanger;
843 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
845 STAILQ_INSERT_TAIL(&nchanger->chluns,
846 nsoftc, changer_links);
848 } else if (status == CAM_REQ_CMP)
851 printf("cdregister: unable to allocate path\n"
852 "cdregister: changer support may be "
856 softc->changer = nchanger;
857 softc->pinfo.index = CAM_UNQUEUED_INDEX;
858 nchanger->num_devices++;
859 if (camq_resize(&nchanger->devq,
860 nchanger->num_devices) != CAM_REQ_CMP) {
861 printf("cdregister: camq_resize "
862 "failed, changer support may "
865 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
872 /* Lock this peripheral until we are setup */
874 cam_periph_lock(periph, 0);
876 if ((softc->flags & CD_FLAG_CHANGER) == 0)
877 xpt_schedule(periph, /*priority*/5);
879 cdschedule(periph, /*priority*/ 5);
885 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
887 struct disklabel *label;
888 struct cam_periph *periph;
889 struct cd_softc *softc;
890 struct ccb_getdev cgd;
896 periph = cam_extend_get(cdperiphs, unit);
901 softc = (struct cd_softc *)periph->softc;
904 * Grab splsoftcam and hold it until we lock the peripheral.
907 if (softc->flags & CD_FLAG_INVALID) {
912 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
919 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
922 cdprevent(periph, PR_PREVENT);
924 /* find out the size */
925 if ((error = cdsize(dev, &size)) != 0) {
926 cdprevent(periph, PR_ALLOW);
927 cam_periph_unlock(periph);
928 cam_periph_release(periph);
933 * If we get a non-zero return, revert back to not reading the
934 * label off the disk. The first track is likely audio, which
935 * won't have a disklabel.
937 if ((error = cdfirsttrackisdata(periph)) != 0) {
938 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
939 softc->disk.d_dsflags |= DSO_NOLABELS;
944 * Build prototype label for whole disk.
945 * Should take information about different data tracks from the
946 * TOC and put it in the partition table.
948 label = &softc->disk.d_label;
949 bzero(label, sizeof(*label));
950 label->d_type = DTYPE_SCSI;
953 * Grab the inquiry data to get the vendor and product names.
954 * Put them in the typename and packname for the label.
956 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
957 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
958 xpt_action((union ccb *)&cgd);
960 strncpy(label->d_typename, cgd.inq_data.vendor,
961 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
962 strncpy(label->d_packname, cgd.inq_data.product,
963 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
965 label->d_secsize = softc->params.blksize;
966 label->d_secperunit = softc->params.disksize;
967 label->d_flags = D_REMOVABLE;
969 * Make partition 'a' cover the whole disk. This is a temporary
970 * compatibility hack. The 'a' partition should not exist, so
971 * the slice code won't create it. The slice code will make
972 * partition (RAW_PART + 'a') cover the whole disk and fill in
973 * some more defaults.
975 label->d_partitions[0].p_size = label->d_secperunit;
976 label->d_partitions[0].p_fstype = FS_OTHER;
979 * We unconditionally (re)set the blocksize each time the
980 * CD device is opened. This is because the CD can change,
981 * and therefore the blocksize might change.
982 * XXX problems here if some slice or partition is still
983 * open with the old size?
985 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
986 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
987 softc->device_stats.block_size = softc->params.blksize;
989 cam_periph_unlock(periph);
991 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
997 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
999 struct cam_periph *periph;
1000 struct cd_softc *softc;
1004 periph = cam_extend_get(cdperiphs, unit);
1008 softc = (struct cd_softc *)periph->softc;
1010 if ((error = cam_periph_lock(periph, 0)) != 0)
1013 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1014 cdprevent(periph, PR_ALLOW);
1017 * Unconditionally set the dsopen() flags back to their default
1020 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1021 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1024 * Since we're closing this CD, mark the blocksize as unavailable.
1025 * It will be marked as available whence the CD is opened again.
1027 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1029 cam_periph_unlock(periph);
1030 cam_periph_release(periph);
1036 cdshorttimeout(void *arg)
1038 struct cdchanger *changer;
1043 changer = (struct cdchanger *)arg;
1045 /* Always clear the short timeout flag, since that's what we're in */
1046 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1049 * Check to see if there is any more pending or outstanding I/O for
1050 * this device. If not, move it out of the active slot.
1052 if ((bufq_first(&changer->cur_device->buf_queue) == NULL)
1053 && (changer->cur_device->device_stats.busy_count == 0)) {
1054 changer->flags |= CHANGER_MANUAL_CALL;
1055 cdrunchangerqueue(changer);
1062 * This is a wrapper for xpt_schedule. It only applies to changers.
1065 cdschedule(struct cam_periph *periph, int priority)
1067 struct cd_softc *softc;
1072 softc = (struct cd_softc *)periph->softc;
1075 * If this device isn't currently queued, and if it isn't
1076 * the active device, then we queue this device and run the
1077 * changer queue if there is no timeout scheduled to do it.
1078 * If this device is the active device, just schedule it
1079 * to run again. If this device is queued, there should be
1080 * a timeout in place already that will make sure it runs.
1082 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1083 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1085 * We don't do anything with the priority here.
1086 * This is strictly a fifo queue.
1088 softc->pinfo.priority = 1;
1089 softc->pinfo.generation = ++softc->changer->devq.generation;
1090 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1093 * Since we just put a device in the changer queue,
1094 * check and see if there is a timeout scheduled for
1095 * this changer. If so, let the timeout handle
1096 * switching this device into the active slot. If
1097 * not, manually call the timeout routine to
1100 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1101 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1102 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1103 softc->changer->flags |= CHANGER_MANUAL_CALL;
1104 cdrunchangerqueue(softc->changer);
1106 } else if ((softc->flags & CD_FLAG_ACTIVE)
1107 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1108 xpt_schedule(periph, priority);
1115 cdrunchangerqueue(void *arg)
1117 struct cd_softc *softc;
1118 struct cdchanger *changer;
1119 int called_from_timeout;
1124 changer = (struct cdchanger *)arg;
1127 * If we have NOT been called from cdstrategy() or cddone(), and
1128 * instead from a timeout routine, go ahead and clear the
1131 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1132 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1133 called_from_timeout = 1;
1135 called_from_timeout = 0;
1137 /* Always clear the manual call flag */
1138 changer->flags &= ~CHANGER_MANUAL_CALL;
1140 /* nothing to do if the queue is empty */
1141 if (changer->devq.entries <= 0) {
1147 * If the changer queue is frozen, that means we have an active
1150 if (changer->devq.qfrozen_cnt > 0) {
1152 if (changer->cur_device->device_stats.busy_count > 0) {
1153 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1154 changer->cur_device->bufs_left =
1155 changer->cur_device->device_stats.busy_count;
1156 if (called_from_timeout) {
1157 changer->long_handle =
1158 timeout(cdrunchangerqueue, changer,
1159 changer_max_busy_seconds * hz);
1160 changer->flags |= CHANGER_TIMEOUT_SCHED;
1167 * We always need to reset the frozen count and clear the
1170 changer->devq.qfrozen_cnt--;
1171 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1172 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1175 * Check to see whether the current device has any I/O left
1176 * to do. If so, requeue it at the end of the queue. If
1177 * not, there is no need to requeue it.
1179 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1181 changer->cur_device->pinfo.generation =
1182 ++changer->devq.generation;
1183 camq_insert(&changer->devq,
1184 (cam_pinfo *)changer->cur_device);
1188 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1190 changer->cur_device = softc;
1192 changer->devq.qfrozen_cnt++;
1193 softc->flags |= CD_FLAG_ACTIVE;
1195 /* Just in case this device is waiting */
1196 wakeup(&softc->changer);
1197 xpt_schedule(softc->periph, /*priority*/ 1);
1200 * Get rid of any pending timeouts, and set a flag to schedule new
1201 * ones so this device gets its full time quantum.
1203 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1204 untimeout(cdrunchangerqueue, changer, changer->long_handle);
1205 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1208 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1209 untimeout(cdshorttimeout, changer, changer->short_handle);
1210 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1214 * We need to schedule timeouts, but we only do this after the
1215 * first transaction has completed. This eliminates the changer
1218 changer->flags |= CHANGER_NEED_TIMEOUT;
1224 cdchangerschedule(struct cd_softc *softc)
1226 struct cdchanger *changer;
1231 changer = softc->changer;
1234 * If this is a changer, and this is the current device,
1235 * and this device has at least the minimum time quantum to
1236 * run, see if we can switch it out.
1238 if ((softc->flags & CD_FLAG_ACTIVE)
1239 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1240 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1242 * We try three things here. The first is that we
1243 * check to see whether the schedule on completion
1244 * flag is set. If it is, we decrement the number
1245 * of buffers left, and if it's zero, we reschedule.
1246 * Next, we check to see whether the pending buffer
1247 * queue is empty and whether there are no
1248 * outstanding transactions. If so, we reschedule.
1249 * Next, we see if the pending buffer queue is empty.
1250 * If it is, we set the number of buffers left to
1251 * the current active buffer count and set the
1252 * schedule on complete flag.
1254 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1255 if (--softc->bufs_left == 0) {
1256 softc->changer->flags |=
1257 CHANGER_MANUAL_CALL;
1258 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1259 cdrunchangerqueue(softc->changer);
1261 } else if ((bufq_first(&softc->buf_queue) == NULL)
1262 && (softc->device_stats.busy_count == 0)) {
1263 softc->changer->flags |= CHANGER_MANUAL_CALL;
1264 cdrunchangerqueue(softc->changer);
1266 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1267 && (softc->flags & CD_FLAG_ACTIVE)) {
1270 * Now that the first transaction to this
1271 * particular device has completed, we can go ahead
1272 * and schedule our timeouts.
1274 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1275 changer->long_handle =
1276 timeout(cdrunchangerqueue, changer,
1277 changer_max_busy_seconds * hz);
1278 changer->flags |= CHANGER_TIMEOUT_SCHED;
1280 printf("cdchangerschedule: already have a long"
1283 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1284 changer->short_handle =
1285 timeout(cdshorttimeout, changer,
1286 changer_min_busy_seconds * hz);
1287 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1289 printf("cdchangerschedule: already have a short "
1293 * We just scheduled timeouts, no need to schedule
1296 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1303 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1304 u_int32_t cam_flags,
1305 u_int32_t sense_flags),
1306 u_int32_t cam_flags, u_int32_t sense_flags)
1308 struct cd_softc *softc;
1309 struct cam_periph *periph;
1312 periph = xpt_path_periph(ccb->ccb_h.path);
1313 softc = (struct cd_softc *)periph->softc;
1315 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1316 &softc->device_stats);
1318 if (softc->flags & CD_FLAG_CHANGER)
1319 cdchangerschedule(softc);
1325 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1327 struct cd_softc *softc;
1330 softc = (struct cd_softc *)periph->softc;
1332 if (softc->flags & CD_FLAG_CHANGER) {
1337 * This should work the first time this device is woken up,
1338 * but just in case it doesn't, we use a while loop.
1340 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1342 * If this changer isn't already queued, queue it up.
1344 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1345 softc->pinfo.priority = 1;
1346 softc->pinfo.generation =
1347 ++softc->changer->devq.generation;
1348 camq_insert(&softc->changer->devq,
1349 (cam_pinfo *)softc);
1351 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1352 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1353 && ((softc->changer->flags
1354 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1355 softc->changer->flags |= CHANGER_MANUAL_CALL;
1356 cdrunchangerqueue(softc->changer);
1358 tsleep(&softc->changer, 0, "cgticb", 0);
1362 return(cam_periph_getccb(periph, priority));
1367 * Actually translate the requested transfer into one the physical driver
1368 * can understand. The transfer is described by a buf and will include
1369 * only one physical transfer.
1372 cdstrategy(struct buf *bp)
1374 struct cam_periph *periph;
1375 struct cd_softc *softc;
1379 unit = dkunit(bp->b_dev);
1380 part = dkpart(bp->b_dev);
1381 periph = cam_extend_get(cdperiphs, unit);
1382 if (periph == NULL) {
1383 bp->b_error = ENXIO;
1387 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1389 softc = (struct cd_softc *)periph->softc;
1392 * Mask interrupts so that the pack cannot be invalidated until
1393 * after we are in the queue. Otherwise, we might not properly
1394 * clean up one of the buffers.
1399 * If the device has been made invalid, error out
1401 if ((softc->flags & CD_FLAG_INVALID)) {
1403 bp->b_error = ENXIO;
1408 * Place it in the queue of disk activities for this disk
1410 bufqdisksort(&softc->buf_queue, bp);
1415 * Schedule ourselves for performing the work. We do things
1416 * differently for changers.
1418 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1419 xpt_schedule(periph, /* XXX priority */1);
1421 cdschedule(periph, /* priority */ 1);
1425 bp->b_flags |= B_ERROR;
1427 * Correctly set the buf to indicate a completed xfer
1429 bp->b_resid = bp->b_bcount;
1435 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1437 struct cd_softc *softc;
1439 struct ccb_scsiio *csio;
1440 struct scsi_read_capacity_data *rcap;
1443 softc = (struct cd_softc *)periph->softc;
1445 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1447 switch (softc->state) {
1448 case CD_STATE_NORMAL:
1453 bp = bufq_first(&softc->buf_queue);
1454 if (periph->immediate_priority <= periph->pinfo.priority) {
1455 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1457 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1459 periph->immediate_priority = CAM_PRIORITY_NONE;
1461 wakeup(&periph->ccb_list);
1462 } else if (bp == NULL) {
1464 xpt_release_ccb(start_ccb);
1466 bufq_remove(&softc->buf_queue, bp);
1468 devstat_start_transaction(&softc->device_stats);
1470 scsi_read_write(&start_ccb->csio,
1472 /* cbfcnp */ cddone,
1473 (bp->b_flags & B_ORDERED) != 0 ?
1476 /* read */bp->b_flags & B_READ,
1478 /* minimum_cmd_size */ 10,
1479 /* lba */ bp->b_pblkno,
1480 bp->b_bcount / softc->params.blksize,
1481 /* data_ptr */ bp->b_data,
1482 /* dxfer_len */ bp->b_bcount,
1483 /* sense_len */ SSD_FULL_SIZE,
1484 /* timeout */ 30000);
1485 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1489 * Block out any asyncronous callbacks
1490 * while we touch the pending ccb list.
1493 LIST_INSERT_HEAD(&softc->pending_ccbs,
1494 &start_ccb->ccb_h, periph_links.le);
1497 /* We expect a unit attention from this device */
1498 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1499 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1500 softc->flags &= ~CD_FLAG_RETRY_UA;
1503 start_ccb->ccb_h.ccb_bp = bp;
1504 bp = bufq_first(&softc->buf_queue);
1507 xpt_action(start_ccb);
1510 /* Have more work to do, so ensure we stay scheduled */
1511 xpt_schedule(periph, /* XXX priority */1);
1515 case CD_STATE_PROBE:
1518 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1522 xpt_print_path(periph->path);
1523 printf("cdstart: Couldn't malloc read_capacity data\n");
1524 /* cd_free_periph??? */
1527 csio = &start_ccb->csio;
1528 scsi_read_capacity(csio,
1535 start_ccb->ccb_h.ccb_bp = NULL;
1536 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1537 xpt_action(start_ccb);
1544 cddone(struct cam_periph *periph, union ccb *done_ccb)
1546 struct cd_softc *softc;
1547 struct ccb_scsiio *csio;
1549 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1551 softc = (struct cd_softc *)periph->softc;
1552 csio = &done_ccb->csio;
1554 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1555 case CD_CCB_BUFFER_IO:
1561 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1564 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1567 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1572 /* Retry selection timeouts */
1573 sf |= SF_RETRY_SELTO;
1575 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1577 * A retry was scheuled, so
1588 xpt_print_path(periph->path);
1589 printf("cddone: got error %#x back\n", error);
1591 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1592 bufq_remove(&softc->buf_queue, q_bp);
1593 q_bp->b_resid = q_bp->b_bcount;
1594 q_bp->b_error = EIO;
1595 q_bp->b_flags |= B_ERROR;
1599 bp->b_resid = bp->b_bcount;
1600 bp->b_error = error;
1601 bp->b_flags |= B_ERROR;
1602 cam_release_devq(done_ccb->ccb_h.path,
1606 /*getcount_only*/0);
1609 bp->b_resid = csio->resid;
1611 if (bp->b_resid != 0) {
1612 /* Short transfer ??? */
1613 bp->b_flags |= B_ERROR;
1618 * Block out any asyncronous callbacks
1619 * while we touch the pending ccb list.
1622 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1625 if (softc->flags & CD_FLAG_CHANGER)
1626 cdchangerschedule(softc);
1628 devstat_end_transaction_buf(&softc->device_stats, bp);
1634 struct scsi_read_capacity_data *rdcap;
1635 char announce_buf[120]; /*
1636 * Currently (9/30/97) the
1637 * longest possible announce
1638 * buffer is 108 bytes, for the
1639 * first error case below.
1640 * That is 39 bytes for the
1641 * basic string, 16 bytes for the
1642 * biggest sense key (hardware
1643 * error), 52 bytes for the
1644 * text of the largest sense
1645 * qualifier valid for a CDROM,
1646 * (0x72, 0x03 or 0x04,
1647 * 0x03), and one byte for the
1648 * null terminating character.
1649 * To allow for longer strings,
1650 * the announce buffer is 120
1653 struct cd_params *cdp;
1655 cdp = &softc->params;
1657 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1659 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1660 cdp->blksize = scsi_4btoul (rdcap->length);
1662 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1664 snprintf(announce_buf, sizeof(announce_buf),
1665 "cd present [%lu x %lu byte records]",
1666 cdp->disksize, (u_long)cdp->blksize);
1671 * Retry any UNIT ATTENTION type errors. They
1672 * are expected at boot.
1674 error = cderror(done_ccb, 0, SF_RETRY_UA |
1675 SF_NO_PRINT | SF_RETRY_SELTO);
1676 if (error == ERESTART) {
1678 * A retry was scheuled, so
1682 } else if (error != 0) {
1684 struct scsi_sense_data *sense;
1686 int sense_key, error_code;
1689 struct ccb_getdev cgd;
1691 /* Don't wedge this device's queue */
1692 cam_release_devq(done_ccb->ccb_h.path,
1696 /*getcount_only*/0);
1698 status = done_ccb->ccb_h.status;
1700 xpt_setup_ccb(&cgd.ccb_h,
1701 done_ccb->ccb_h.path,
1703 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1704 xpt_action((union ccb *)&cgd);
1706 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1707 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1708 || ((status & CAM_AUTOSNS_VALID) == 0))
1714 sense = &csio->sense_data;
1715 scsi_extract_sense(sense, &error_code,
1720 * Attach to anything that claims to be a
1721 * CDROM or WORM device, as long as it
1722 * doesn't return a "Logical unit not
1723 * supported" (0x25) error.
1725 if ((have_sense) && (asc != 0x25)
1726 && (error_code == SSD_CURRENT_ERROR))
1727 snprintf(announce_buf,
1728 sizeof(announce_buf),
1729 "Attempt to query device "
1730 "size failed: %s, %s",
1731 scsi_sense_key_text[sense_key],
1732 scsi_sense_desc(asc,ascq,
1734 else if ((have_sense == 0)
1735 && ((status & CAM_STATUS_MASK) ==
1736 CAM_SCSI_STATUS_ERROR)
1737 && (csio->scsi_status ==
1738 SCSI_STATUS_BUSY)) {
1739 snprintf(announce_buf,
1740 sizeof(announce_buf),
1741 "Attempt to query device "
1742 "size failed: SCSI status: BUSY");
1743 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1745 * We only print out an error for
1746 * CDROM type devices. For WORM
1747 * devices, we don't print out an
1748 * error since a few WORM devices
1749 * don't support CDROM commands.
1750 * If we have sense information, go
1751 * ahead and print it out.
1752 * Otherwise, just say that we
1757 * Just print out the error, not
1758 * the full probe message, when we
1765 xpt_print_path(periph->path);
1766 printf("got CAM status %#x\n",
1767 done_ccb->ccb_h.status);
1769 xpt_print_path(periph->path);
1770 printf("fatal error, failed"
1771 " to attach to device\n");
1774 * Invalidate this peripheral.
1776 cam_periph_invalidate(periph);
1778 announce_buf[0] = '\0';
1782 * Invalidate this peripheral.
1784 cam_periph_invalidate(periph);
1785 announce_buf[0] = '\0';
1789 free(rdcap, M_TEMP);
1790 if (announce_buf[0] != '\0') {
1791 xpt_announce_periph(periph, announce_buf);
1792 if (softc->flags & CD_FLAG_CHANGER)
1793 cdchangerschedule(softc);
1795 softc->state = CD_STATE_NORMAL;
1797 * Since our peripheral may be invalidated by an error
1798 * above or an external event, we must release our CCB
1799 * before releasing the probe lock on the peripheral.
1800 * The peripheral will only go away once the last lock
1801 * is removed, and we need it around for the CCB release
1804 xpt_release_ccb(done_ccb);
1805 cam_periph_unlock(periph);
1808 case CD_CCB_WAITING:
1810 /* Caller will release the CCB */
1811 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1812 ("trying to wakeup ccbwait\n"));
1814 wakeup(&done_ccb->ccb_h.cbfcnp);
1820 xpt_release_ccb(done_ccb);
1824 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1827 struct cam_periph *periph;
1828 struct cd_softc *softc;
1833 periph = cam_extend_get(cdperiphs, unit);
1837 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1839 softc = (struct cd_softc *)periph->softc;
1841 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1842 ("trying to do ioctl %#lx\n", cmd));
1844 error = cam_periph_lock(periph, PCATCH);
1851 case CDIOCPLAYTRACKS:
1853 struct ioc_play_track *args
1854 = (struct ioc_play_track *) addr;
1855 struct cd_mode_data *data;
1857 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1860 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1861 ("trying to do CDIOCPLAYTRACKS\n"));
1863 error = cdgetmode(periph, data, AUDIO_PAGE);
1868 data->page.audio.flags &= ~CD_PA_SOTC;
1869 data->page.audio.flags |= CD_PA_IMMED;
1870 error = cdsetmode(periph, data);
1874 if (softc->quirks & CD_Q_BCD_TRACKS) {
1875 args->start_track = bin2bcd(args->start_track);
1876 args->end_track = bin2bcd(args->end_track);
1878 error = cdplaytracks(periph,
1887 struct ioc_play_msf *args
1888 = (struct ioc_play_msf *) addr;
1889 struct cd_mode_data *data;
1891 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1894 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1895 ("trying to do CDIOCPLAYMSF\n"));
1897 error = cdgetmode(periph, data, AUDIO_PAGE);
1902 data->page.audio.flags &= ~CD_PA_SOTC;
1903 data->page.audio.flags |= CD_PA_IMMED;
1904 error = cdsetmode(periph, data);
1908 error = cdplaymsf(periph,
1917 case CDIOCPLAYBLOCKS:
1919 struct ioc_play_blocks *args
1920 = (struct ioc_play_blocks *) addr;
1921 struct cd_mode_data *data;
1923 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1924 ("trying to do CDIOCPLAYBLOCKS\n"));
1926 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
1929 error = cdgetmode(periph, data, AUDIO_PAGE);
1934 data->page.audio.flags &= ~CD_PA_SOTC;
1935 data->page.audio.flags |= CD_PA_IMMED;
1936 error = cdsetmode(periph, data);
1940 error = cdplay(periph, args->blk, args->len);
1943 case CDIOCREADSUBCHANNEL:
1945 struct ioc_read_subchannel *args
1946 = (struct ioc_read_subchannel *) addr;
1947 struct cd_sub_channel_info *data;
1948 u_int32_t len = args->data_len;
1950 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1951 ("trying to do CDIOCREADSUBCHANNEL\n"));
1953 data = malloc(sizeof(struct cd_sub_channel_info),
1956 if ((len > sizeof(struct cd_sub_channel_info)) ||
1957 (len < sizeof(struct cd_sub_channel_header))) {
1959 "scsi_cd: cdioctl: "
1960 "cdioreadsubchannel: error, len=%d\n",
1967 if (softc->quirks & CD_Q_BCD_TRACKS)
1968 args->track = bin2bcd(args->track);
1970 error = cdreadsubchannel(periph, args->address_format,
1971 args->data_format, args->track, data, len);
1977 if (softc->quirks & CD_Q_BCD_TRACKS)
1978 data->what.track_info.track_number =
1979 bcd2bin(data->what.track_info.track_number);
1980 len = min(len, ((data->header.data_len[0] << 8) +
1981 data->header.data_len[1] +
1982 sizeof(struct cd_sub_channel_header)));
1983 if (copyout(data, args->data, len) != 0) {
1990 case CDIOREADTOCHEADER:
1992 struct ioc_toc_header *th;
1994 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1995 ("trying to do CDIOREADTOCHEADER\n"));
1997 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
1999 error = cdreadtoc(periph, 0, 0,
2000 (struct cd_toc_entry *)th,
2006 if (softc->quirks & CD_Q_BCD_TRACKS) {
2007 /* we are going to have to convert the BCD
2008 * encoding on the cd to what is expected
2010 th->starting_track =
2011 bcd2bin(th->starting_track);
2012 th->ending_track = bcd2bin(th->ending_track);
2015 bcopy(th, addr, sizeof(*th));
2019 case CDIOREADTOCENTRYS:
2022 struct ioc_toc_header header;
2023 struct cd_toc_entry entries[100];
2026 struct ioc_toc_header header;
2027 struct cd_toc_entry entry;
2032 struct ioc_read_toc_entry *te =
2033 (struct ioc_read_toc_entry *) addr;
2034 struct ioc_toc_header *th;
2035 u_int32_t len, readlen, idx, num;
2036 u_int32_t starting_track = te->starting_track;
2038 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2039 ("trying to do CDIOREADTOCENTRYS\n"));
2041 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2042 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK);
2044 if (te->data_len < sizeof(struct cd_toc_entry)
2045 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2046 || (te->address_format != CD_MSF_FORMAT
2047 && te->address_format != CD_LBA_FORMAT)) {
2049 printf("scsi_cd: error in readtocentries, "
2050 "returning EINVAL\n");
2057 error = cdreadtoc(periph, 0, 0,
2058 (struct cd_toc_entry *)th,
2066 if (softc->quirks & CD_Q_BCD_TRACKS) {
2067 /* we are going to have to convert the BCD
2068 * encoding on the cd to what is expected
2070 th->starting_track =
2071 bcd2bin(th->starting_track);
2072 th->ending_track = bcd2bin(th->ending_track);
2075 if (starting_track == 0)
2076 starting_track = th->starting_track;
2077 else if (starting_track == LEADOUT)
2078 starting_track = th->ending_track + 1;
2079 else if (starting_track < th->starting_track ||
2080 starting_track > th->ending_track + 1) {
2081 printf("scsi_cd: error in readtocentries, "
2082 "returning EINVAL\n");
2089 /* calculate reading length without leadout entry */
2090 readlen = (th->ending_track - starting_track + 1) *
2091 sizeof(struct cd_toc_entry);
2093 /* and with leadout entry */
2094 len = readlen + sizeof(struct cd_toc_entry);
2095 if (te->data_len < len) {
2100 if (len > sizeof(data->entries)) {
2101 printf("scsi_cd: error in readtocentries, "
2102 "returning EINVAL\n");
2108 num = len / sizeof(struct cd_toc_entry);
2111 error = cdreadtoc(periph, te->address_format,
2113 (struct cd_toc_entry *)data,
2114 readlen + sizeof (*th));
2122 /* make leadout entry if needed */
2123 idx = starting_track + num - 1;
2124 if (softc->quirks & CD_Q_BCD_TRACKS)
2125 th->ending_track = bcd2bin(th->ending_track);
2126 if (idx == th->ending_track + 1) {
2127 error = cdreadtoc(periph, te->address_format,
2129 (struct cd_toc_entry *)lead,
2136 data->entries[idx - starting_track] =
2139 if (softc->quirks & CD_Q_BCD_TRACKS) {
2140 for (idx = 0; idx < num - 1; idx++) {
2141 data->entries[idx].track =
2142 bcd2bin(data->entries[idx].track);
2146 error = copyout(data->entries, te->data, len);
2151 case CDIOREADTOCENTRY:
2153 /* yeah yeah, this is ugly */
2155 struct ioc_toc_header header;
2156 struct cd_toc_entry entry;
2160 struct ioc_read_toc_single_entry *te =
2161 (struct ioc_read_toc_single_entry *) addr;
2162 struct ioc_toc_header *th;
2165 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2166 ("trying to do CDIOREADTOCENTRY\n"));
2168 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK);
2170 if (te->address_format != CD_MSF_FORMAT
2171 && te->address_format != CD_LBA_FORMAT) {
2172 printf("error in readtocentry, "
2173 " returning EINVAL\n");
2180 error = cdreadtoc(periph, 0, 0,
2181 (struct cd_toc_entry *)th,
2188 if (softc->quirks & CD_Q_BCD_TRACKS) {
2189 /* we are going to have to convert the BCD
2190 * encoding on the cd to what is expected
2192 th->starting_track =
2193 bcd2bin(th->starting_track);
2194 th->ending_track = bcd2bin(th->ending_track);
2198 track = th->starting_track;
2199 else if (track == LEADOUT)
2201 else if (track < th->starting_track ||
2202 track > th->ending_track + 1) {
2203 printf("error in readtocentry, "
2204 " returning EINVAL\n");
2210 error = cdreadtoc(periph, te->address_format, track,
2211 (struct cd_toc_entry *)data,
2218 if (softc->quirks & CD_Q_BCD_TRACKS)
2219 data->entry.track = bcd2bin(data->entry.track);
2220 bcopy(&data->entry, &te->entry,
2221 sizeof(struct cd_toc_entry));
2227 struct ioc_patch *arg = (struct ioc_patch *) addr;
2228 struct cd_mode_data *data;
2230 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2231 ("trying to do CDIOCSETPATCH\n"));
2233 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2235 error = cdgetmode(periph, data, AUDIO_PAGE);
2240 data->page.audio.port[LEFT_PORT].channels =
2242 data->page.audio.port[RIGHT_PORT].channels =
2244 data->page.audio.port[2].channels = arg->patch[2];
2245 data->page.audio.port[3].channels = arg->patch[3];
2246 error = cdsetmode(periph, data);
2252 struct ioc_vol *arg = (struct ioc_vol *) addr;
2253 struct cd_mode_data *data;
2255 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2256 ("trying to do CDIOCGETVOL\n"));
2258 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2260 error = cdgetmode(periph, data, AUDIO_PAGE);
2265 arg->vol[LEFT_PORT] =
2266 data->page.audio.port[LEFT_PORT].volume;
2267 arg->vol[RIGHT_PORT] =
2268 data->page.audio.port[RIGHT_PORT].volume;
2269 arg->vol[2] = data->page.audio.port[2].volume;
2270 arg->vol[3] = data->page.audio.port[3].volume;
2276 struct ioc_vol *arg = (struct ioc_vol *) addr;
2277 struct cd_mode_data *data;
2279 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2280 ("trying to do CDIOCSETVOL\n"));
2282 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2284 error = cdgetmode(periph, data, AUDIO_PAGE);
2289 data->page.audio.port[LEFT_PORT].channels = CHANNEL_0;
2290 data->page.audio.port[LEFT_PORT].volume =
2291 arg->vol[LEFT_PORT];
2292 data->page.audio.port[RIGHT_PORT].channels = CHANNEL_1;
2293 data->page.audio.port[RIGHT_PORT].volume =
2294 arg->vol[RIGHT_PORT];
2295 data->page.audio.port[2].volume = arg->vol[2];
2296 data->page.audio.port[3].volume = arg->vol[3];
2297 error = cdsetmode(periph, data);
2303 struct cd_mode_data *data;
2305 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2306 ("trying to do CDIOCSETMONO\n"));
2308 data = malloc(sizeof(struct cd_mode_data),
2310 error = cdgetmode(periph, data, AUDIO_PAGE);
2315 data->page.audio.port[LEFT_PORT].channels =
2316 LEFT_CHANNEL | RIGHT_CHANNEL;
2317 data->page.audio.port[RIGHT_PORT].channels =
2318 LEFT_CHANNEL | RIGHT_CHANNEL;
2319 data->page.audio.port[2].channels = 0;
2320 data->page.audio.port[3].channels = 0;
2321 error = cdsetmode(periph, data);
2325 case CDIOCSETSTEREO:
2327 struct cd_mode_data *data;
2329 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2330 ("trying to do CDIOCSETSTEREO\n"));
2332 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2334 error = cdgetmode(periph, data, AUDIO_PAGE);
2339 data->page.audio.port[LEFT_PORT].channels =
2341 data->page.audio.port[RIGHT_PORT].channels =
2343 data->page.audio.port[2].channels = 0;
2344 data->page.audio.port[3].channels = 0;
2345 error = cdsetmode(periph, data);
2351 struct cd_mode_data *data;
2353 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2354 ("trying to do CDIOCSETMUTE\n"));
2356 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2358 error = cdgetmode(periph, data, AUDIO_PAGE);
2363 data->page.audio.port[LEFT_PORT].channels = 0;
2364 data->page.audio.port[RIGHT_PORT].channels = 0;
2365 data->page.audio.port[2].channels = 0;
2366 data->page.audio.port[3].channels = 0;
2367 error = cdsetmode(periph, data);
2373 struct cd_mode_data *data;
2375 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2376 ("trying to do CDIOCSETLEFT\n"));
2378 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2380 error = cdgetmode(periph, data, AUDIO_PAGE);
2385 data->page.audio.port[LEFT_PORT].channels =
2387 data->page.audio.port[RIGHT_PORT].channels =
2389 data->page.audio.port[2].channels = 0;
2390 data->page.audio.port[3].channels = 0;
2391 error = cdsetmode(periph, data);
2397 struct cd_mode_data *data;
2399 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2400 ("trying to do CDIOCSETRIGHT\n"));
2402 data = malloc(sizeof(struct cd_mode_data), M_TEMP,
2404 error = cdgetmode(periph, data, AUDIO_PAGE);
2409 data->page.audio.port[LEFT_PORT].channels =
2411 data->page.audio.port[RIGHT_PORT].channels =
2413 data->page.audio.port[2].channels = 0;
2414 data->page.audio.port[3].channels = 0;
2415 error = cdsetmode(periph, data);
2420 error = cdpause(periph, 1);
2423 error = cdpause(periph, 0);
2426 error = cdstartunit(periph);
2429 error = cdstopunit(periph, 0);
2432 error = cdstopunit(periph, 1);
2435 cdprevent(periph, PR_ALLOW);
2438 cdprevent(periph, PR_PREVENT);
2441 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2445 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2449 /* return (cd_reset(periph)); */
2452 case CDRIOCREADSPEED:
2453 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2455 case CDRIOCWRITESPEED:
2456 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2459 case DVDIOCREPORTKEY: {
2460 struct dvd_authinfo *authinfo;
2462 authinfo = (struct dvd_authinfo *)addr;
2464 if (cmd == DVDIOCREPORTKEY)
2465 error = cdreportkey(periph, authinfo);
2467 error = cdsendkey(periph, authinfo);
2470 case DVDIOCREADSTRUCTURE: {
2471 struct dvd_struct *dvdstruct;
2473 dvdstruct = (struct dvd_struct *)addr;
2475 error = cdreaddvdstructure(periph, dvdstruct);
2480 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2484 cam_periph_unlock(periph);
2486 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2492 cdprevent(struct cam_periph *periph, int action)
2495 struct cd_softc *softc;
2498 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2500 softc = (struct cd_softc *)periph->softc;
2502 if (((action == PR_ALLOW)
2503 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2504 || ((action == PR_PREVENT)
2505 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2509 ccb = cdgetccb(periph, /* priority */ 1);
2511 scsi_prevent(&ccb->csio,
2517 /* timeout */60000);
2519 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2520 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2522 xpt_release_ccb(ccb);
2525 if (action == PR_ALLOW)
2526 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2528 softc->flags |= CD_FLAG_DISC_LOCKED;
2533 cdsize(dev_t dev, u_int32_t *size)
2535 struct cam_periph *periph;
2536 struct cd_softc *softc;
2538 struct scsi_read_capacity_data *rcap_buf;
2541 periph = cam_extend_get(cdperiphs, dkunit(dev));
2546 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2548 softc = (struct cd_softc *)periph->softc;
2550 ccb = cdgetccb(periph, /* priority */ 1);
2552 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2555 scsi_read_capacity(&ccb->csio,
2561 /* timeout */20000);
2563 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2564 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2566 xpt_release_ccb(ccb);
2568 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2569 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2571 * SCSI-3 mandates that the reported blocksize shall be 2048.
2572 * Older drives sometimes report funny values, trim it down to
2573 * 2048, or other parts of the kernel will get confused.
2575 * XXX we leave drives alone that might report 512 bytes, as
2576 * well as drives reporting more weird sizes like perhaps 4K.
2578 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2579 softc->params.blksize = 2048;
2581 free(rcap_buf, M_TEMP);
2582 *size = softc->params.disksize;
2589 * The idea here is to try to figure out whether the first track is data or
2590 * audio. If it is data, we can at least attempt to read a disklabel off
2591 * the first sector of the disk. If it is audio, there won't be a
2594 * This routine returns 0 if the first track is data, and non-zero if there
2595 * is an error or the first track is audio. (If either non-zero case, we
2596 * should not attempt to read the disklabel.)
2599 cdfirsttrackisdata(struct cam_periph *periph)
2602 struct ioc_toc_header header;
2603 struct cd_toc_entry entries[100];
2605 struct cd_softc *softc;
2606 struct ioc_toc_header *th;
2607 struct cdtocdata *data;
2609 int error, first_track_audio;
2612 first_track_audio = -1;
2614 softc = (struct cd_softc *)periph->softc;
2616 data = malloc(sizeof(struct cdtocdata), M_TEMP, M_WAITOK);
2619 error = cdreadtoc(periph, 0, 0, (struct cd_toc_entry *)data,
2625 if (softc->quirks & CD_Q_BCD_TRACKS) {
2626 /* we are going to have to convert the BCD
2627 * encoding on the cd to what is expected
2629 th->starting_track =
2630 bcd2bin(th->starting_track);
2631 th->ending_track = bcd2bin(th->ending_track);
2633 th->len = scsi_2btoul((u_int8_t *)&th->len);
2635 if ((th->len - 2) > 0)
2636 num_entries = (th->len - 2) / sizeof(struct cd_toc_entry);
2640 for (i = 0; i < num_entries; i++) {
2641 if (data->entries[i].track == th->starting_track) {
2642 if (data->entries[i].control & 0x4)
2643 first_track_audio = 0;
2645 first_track_audio = 1;
2650 if (first_track_audio == -1)
2652 else if (first_track_audio == 1)
2663 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2665 struct cd_softc *softc;
2666 struct cam_periph *periph;
2668 periph = xpt_path_periph(ccb->ccb_h.path);
2669 softc = (struct cd_softc *)periph->softc;
2673 * Until we have a better way of doing pack validation,
2674 * don't treat UAs as errors.
2676 sense_flags |= SF_RETRY_UA;
2677 return (cam_periph_error(ccb, cam_flags, sense_flags,
2678 &softc->saved_ccb));
2682 * Read table of contents
2685 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2686 struct cd_toc_entry *data, u_int32_t len)
2688 struct scsi_read_toc *scsi_cmd;
2690 struct ccb_scsiio *csio;
2697 ccb = cdgetccb(periph, /* priority */ 1);
2703 /* cbfcnp */ cddone,
2704 /* flags */ CAM_DIR_IN,
2705 /* tag_action */ MSG_SIMPLE_Q_TAG,
2706 /* data_ptr */ (u_int8_t *)data,
2707 /* dxfer_len */ len,
2708 /* sense_len */ SSD_FULL_SIZE,
2709 sizeof(struct scsi_read_toc),
2710 /* timeout */ 50000);
2712 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2713 bzero (scsi_cmd, sizeof(*scsi_cmd));
2715 if (mode == CD_MSF_FORMAT)
2716 scsi_cmd->byte2 |= CD_MSF;
2717 scsi_cmd->from_track = start;
2718 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
2719 scsi_cmd->data_len[0] = (ntoc) >> 8;
2720 scsi_cmd->data_len[1] = (ntoc) & 0xff;
2722 scsi_cmd->op_code = READ_TOC;
2724 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2725 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2727 xpt_release_ccb(ccb);
2733 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
2734 u_int32_t format, int track,
2735 struct cd_sub_channel_info *data, u_int32_t len)
2737 struct scsi_read_subchannel *scsi_cmd;
2738 struct ccb_scsiio *csio;
2744 ccb = cdgetccb(periph, /* priority */ 1);
2750 /* cbfcnp */ cddone,
2751 /* flags */ CAM_DIR_IN,
2752 /* tag_action */ MSG_SIMPLE_Q_TAG,
2753 /* data_ptr */ (u_int8_t *)data,
2754 /* dxfer_len */ len,
2755 /* sense_len */ SSD_FULL_SIZE,
2756 sizeof(struct scsi_read_subchannel),
2757 /* timeout */ 50000);
2759 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2760 bzero (scsi_cmd, sizeof(*scsi_cmd));
2762 scsi_cmd->op_code = READ_SUBCHANNEL;
2763 if (mode == CD_MSF_FORMAT)
2764 scsi_cmd->byte1 |= CD_MSF;
2765 scsi_cmd->byte2 = SRS_SUBQ;
2766 scsi_cmd->subchan_format = format;
2767 scsi_cmd->track = track;
2768 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
2769 scsi_cmd->control = 0;
2771 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2772 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2774 xpt_release_ccb(ccb);
2781 cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page)
2783 struct scsi_mode_sense_6 *scsi_cmd;
2784 struct ccb_scsiio *csio;
2788 ccb = cdgetccb(periph, /* priority */ 1);
2792 bzero(data, sizeof(*data));
2795 /* cbfcnp */ cddone,
2796 /* flags */ CAM_DIR_IN,
2797 /* tag_action */ MSG_SIMPLE_Q_TAG,
2798 /* data_ptr */ (u_int8_t *)data,
2799 /* dxfer_len */ sizeof(*data),
2800 /* sense_len */ SSD_FULL_SIZE,
2801 sizeof(struct scsi_mode_sense_6),
2802 /* timeout */ 50000);
2804 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
2805 bzero (scsi_cmd, sizeof(*scsi_cmd));
2807 scsi_cmd->page = page;
2808 scsi_cmd->length = sizeof(*data) & 0xff;
2809 scsi_cmd->opcode = MODE_SENSE;
2811 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2812 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
2814 xpt_release_ccb(ccb);
2820 cdsetmode(struct cam_periph *periph, struct cd_mode_data *data)
2822 struct scsi_mode_select_6 *scsi_cmd;
2823 struct ccb_scsiio *csio;
2827 ccb = cdgetccb(periph, /* priority */ 1);
2835 /* cbfcnp */ cddone,
2836 /* flags */ CAM_DIR_OUT,
2837 /* tag_action */ MSG_SIMPLE_Q_TAG,
2838 /* data_ptr */ (u_int8_t *)data,
2839 /* dxfer_len */ sizeof(*data),
2840 /* sense_len */ SSD_FULL_SIZE,
2841 sizeof(struct scsi_mode_select_6),
2842 /* timeout */ 50000);
2844 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
2846 bzero(scsi_cmd, sizeof(*scsi_cmd));
2847 scsi_cmd->opcode = MODE_SELECT;
2848 scsi_cmd->byte2 |= SMS_PF;
2849 scsi_cmd->length = sizeof(*data) & 0xff;
2850 data->header.data_length = 0;
2852 * SONY drives do not allow a mode select with a medium_type
2853 * value that has just been returned by a mode sense; use a
2854 * medium_type of 0 (Default) instead.
2856 data->header.medium_type = 0;
2858 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2859 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2861 xpt_release_ccb(ccb);
2868 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2870 struct ccb_scsiio *csio;
2876 ccb = cdgetccb(periph, /* priority */ 1);
2879 * Use the smallest possible command to perform the operation.
2881 if ((len & 0xffff0000) == 0) {
2883 * We can fit in a 10 byte cdb.
2885 struct scsi_play_10 *scsi_cmd;
2887 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
2888 bzero (scsi_cmd, sizeof(*scsi_cmd));
2889 scsi_cmd->op_code = PLAY_10;
2890 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2891 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
2892 cdb_len = sizeof(*scsi_cmd);
2894 struct scsi_play_12 *scsi_cmd;
2896 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
2897 bzero (scsi_cmd, sizeof(*scsi_cmd));
2898 scsi_cmd->op_code = PLAY_12;
2899 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2900 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
2901 cdb_len = sizeof(*scsi_cmd);
2906 /*flags*/CAM_DIR_NONE,
2910 /*sense_len*/SSD_FULL_SIZE,
2912 /*timeout*/50 * 1000);
2914 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2915 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2917 xpt_release_ccb(ccb);
2923 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
2924 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
2926 struct scsi_play_msf *scsi_cmd;
2927 struct ccb_scsiio *csio;
2933 ccb = cdgetccb(periph, /* priority */ 1);
2939 /* cbfcnp */ cddone,
2940 /* flags */ CAM_DIR_NONE,
2941 /* tag_action */ MSG_SIMPLE_Q_TAG,
2942 /* data_ptr */ NULL,
2944 /* sense_len */ SSD_FULL_SIZE,
2945 sizeof(struct scsi_play_msf),
2946 /* timeout */ 50000);
2948 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2949 bzero (scsi_cmd, sizeof(*scsi_cmd));
2951 scsi_cmd->op_code = PLAY_MSF;
2952 scsi_cmd->start_m = startm;
2953 scsi_cmd->start_s = starts;
2954 scsi_cmd->start_f = startf;
2955 scsi_cmd->end_m = endm;
2956 scsi_cmd->end_s = ends;
2957 scsi_cmd->end_f = endf;
2959 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2960 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
2962 xpt_release_ccb(ccb);
2969 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
2970 u_int32_t etrack, u_int32_t eindex)
2972 struct scsi_play_track *scsi_cmd;
2973 struct ccb_scsiio *csio;
2979 ccb = cdgetccb(periph, /* priority */ 1);
2985 /* cbfcnp */ cddone,
2986 /* flags */ CAM_DIR_NONE,
2987 /* tag_action */ MSG_SIMPLE_Q_TAG,
2988 /* data_ptr */ NULL,
2990 /* sense_len */ SSD_FULL_SIZE,
2991 sizeof(struct scsi_play_track),
2992 /* timeout */ 50000);
2994 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
2995 bzero (scsi_cmd, sizeof(*scsi_cmd));
2997 scsi_cmd->op_code = PLAY_TRACK;
2998 scsi_cmd->start_track = strack;
2999 scsi_cmd->start_index = sindex;
3000 scsi_cmd->end_track = etrack;
3001 scsi_cmd->end_index = eindex;
3003 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3004 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3006 xpt_release_ccb(ccb);
3012 cdpause(struct cam_periph *periph, u_int32_t go)
3014 struct scsi_pause *scsi_cmd;
3015 struct ccb_scsiio *csio;
3021 ccb = cdgetccb(periph, /* priority */ 1);
3027 /* cbfcnp */ cddone,
3028 /* flags */ CAM_DIR_NONE,
3029 /* tag_action */ MSG_SIMPLE_Q_TAG,
3030 /* data_ptr */ NULL,
3032 /* sense_len */ SSD_FULL_SIZE,
3033 sizeof(struct scsi_pause),
3034 /* timeout */ 50000);
3036 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3037 bzero (scsi_cmd, sizeof(*scsi_cmd));
3039 scsi_cmd->op_code = PAUSE;
3040 scsi_cmd->resume = go;
3042 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3043 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3045 xpt_release_ccb(ccb);
3051 cdstartunit(struct cam_periph *periph)
3058 ccb = cdgetccb(periph, /* priority */ 1);
3060 scsi_start_stop(&ccb->csio,
3062 /* cbfcnp */ cddone,
3063 /* tag_action */ MSG_SIMPLE_Q_TAG,
3065 /* load_eject */ FALSE,
3066 /* immediate */ FALSE,
3067 /* sense_len */ SSD_FULL_SIZE,
3068 /* timeout */ 50000);
3070 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3071 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3073 xpt_release_ccb(ccb);
3079 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3086 ccb = cdgetccb(periph, /* priority */ 1);
3088 scsi_start_stop(&ccb->csio,
3090 /* cbfcnp */ cddone,
3091 /* tag_action */ MSG_SIMPLE_Q_TAG,
3093 /* load_eject */ eject,
3094 /* immediate */ FALSE,
3095 /* sense_len */ SSD_FULL_SIZE,
3096 /* timeout */ 50000);
3098 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3099 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3101 xpt_release_ccb(ccb);
3107 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3109 struct scsi_set_speed *scsi_cmd;
3110 struct ccb_scsiio *csio;
3115 ccb = cdgetccb(periph, /* priority */ 1);
3118 /* Preserve old behavior: units in multiples of CDROM speed */
3126 /* cbfcnp */ cddone,
3127 /* flags */ CAM_DIR_NONE,
3128 /* tag_action */ MSG_SIMPLE_Q_TAG,
3129 /* data_ptr */ NULL,
3131 /* sense_len */ SSD_FULL_SIZE,
3132 sizeof(struct scsi_set_speed),
3133 /* timeout */ 50000);
3135 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3136 bzero(scsi_cmd, sizeof(*scsi_cmd));
3138 scsi_cmd->opcode = SET_CD_SPEED;
3139 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3140 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3142 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3143 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3145 xpt_release_ccb(ccb);
3151 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3163 ccb = cdgetccb(periph, /* priority */ 1);
3165 switch (authinfo->format) {
3166 case DVD_REPORT_AGID:
3167 length = sizeof(struct scsi_report_key_data_agid);
3169 case DVD_REPORT_CHALLENGE:
3170 length = sizeof(struct scsi_report_key_data_challenge);
3172 case DVD_REPORT_KEY1:
3173 length = sizeof(struct scsi_report_key_data_key1_key2);
3175 case DVD_REPORT_TITLE_KEY:
3176 length = sizeof(struct scsi_report_key_data_title);
3177 /* The lba field is only set for the title key */
3178 lba = authinfo->lba;
3180 case DVD_REPORT_ASF:
3181 length = sizeof(struct scsi_report_key_data_asf);
3183 case DVD_REPORT_RPC:
3184 length = sizeof(struct scsi_report_key_data_rpc);
3186 case DVD_INVALIDATE_AGID:
3192 break; /* NOTREACHED */
3196 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3201 scsi_report_key(&ccb->csio,
3203 /* cbfcnp */ cddone,
3204 /* tag_action */ MSG_SIMPLE_Q_TAG,
3206 /* agid */ authinfo->agid,
3207 /* key_format */ authinfo->format,
3208 /* data_ptr */ databuf,
3209 /* dxfer_len */ length,
3210 /* sense_len */ SSD_FULL_SIZE,
3211 /* timeout */ 50000);
3213 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3214 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3219 if (ccb->csio.resid != 0) {
3220 xpt_print_path(periph->path);
3221 printf("warning, residual for report key command is %d\n",
3225 switch(authinfo->format) {
3226 case DVD_REPORT_AGID: {
3227 struct scsi_report_key_data_agid *agid_data;
3229 agid_data = (struct scsi_report_key_data_agid *)databuf;
3231 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3235 case DVD_REPORT_CHALLENGE: {
3236 struct scsi_report_key_data_challenge *chal_data;
3238 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3240 bcopy(chal_data->challenge_key, authinfo->keychal,
3241 min(sizeof(chal_data->challenge_key),
3242 sizeof(authinfo->keychal)));
3245 case DVD_REPORT_KEY1: {
3246 struct scsi_report_key_data_key1_key2 *key1_data;
3248 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3250 bcopy(key1_data->key1, authinfo->keychal,
3251 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3254 case DVD_REPORT_TITLE_KEY: {
3255 struct scsi_report_key_data_title *title_data;
3257 title_data = (struct scsi_report_key_data_title *)databuf;
3259 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3260 RKD_TITLE_CPM_SHIFT;
3261 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3262 RKD_TITLE_CP_SEC_SHIFT;
3263 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3264 RKD_TITLE_CMGS_SHIFT;
3265 bcopy(title_data->title_key, authinfo->keychal,
3266 min(sizeof(title_data->title_key),
3267 sizeof(authinfo->keychal)));
3270 case DVD_REPORT_ASF: {
3271 struct scsi_report_key_data_asf *asf_data;
3273 asf_data = (struct scsi_report_key_data_asf *)databuf;
3275 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3278 case DVD_REPORT_RPC: {
3279 struct scsi_report_key_data_rpc *rpc_data;
3281 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3283 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3285 authinfo->vend_rsts =
3286 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3287 RKD_RPC_VENDOR_RESET_SHIFT;
3288 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3289 authinfo->region = rpc_data->region_mask;
3290 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3293 case DVD_INVALIDATE_AGID:
3296 /* This should be impossible, since we checked above */
3299 break; /* NOTREACHED */
3302 if (databuf != NULL)
3303 free(databuf, M_DEVBUF);
3305 xpt_release_ccb(ccb);
3311 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3321 ccb = cdgetccb(periph, /* priority */ 1);
3323 switch(authinfo->format) {
3324 case DVD_SEND_CHALLENGE: {
3325 struct scsi_report_key_data_challenge *challenge_data;
3327 length = sizeof(*challenge_data);
3329 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3331 databuf = (u_int8_t *)challenge_data;
3333 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3334 challenge_data->data_len);
3336 bcopy(authinfo->keychal, challenge_data->challenge_key,
3337 min(sizeof(authinfo->keychal),
3338 sizeof(challenge_data->challenge_key)));
3341 case DVD_SEND_KEY2: {
3342 struct scsi_report_key_data_key1_key2 *key2_data;
3344 length = sizeof(*key2_data);
3346 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3348 databuf = (u_int8_t *)key2_data;
3350 scsi_ulto2b(length - sizeof(key2_data->data_len),
3351 key2_data->data_len);
3353 bcopy(authinfo->keychal, key2_data->key1,
3354 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3358 case DVD_SEND_RPC: {
3359 struct scsi_send_key_data_rpc *rpc_data;
3361 length = sizeof(*rpc_data);
3363 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3365 databuf = (u_int8_t *)rpc_data;
3367 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3368 rpc_data->data_len);
3370 rpc_data->region_code = authinfo->region;
3376 break; /* NOTREACHED */
3379 scsi_send_key(&ccb->csio,
3381 /* cbfcnp */ cddone,
3382 /* tag_action */ MSG_SIMPLE_Q_TAG,
3383 /* agid */ authinfo->agid,
3384 /* key_format */ authinfo->format,
3385 /* data_ptr */ databuf,
3386 /* dxfer_len */ length,
3387 /* sense_len */ SSD_FULL_SIZE,
3388 /* timeout */ 50000);
3390 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3391 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3395 if (databuf != NULL)
3396 free(databuf, M_DEVBUF);
3398 xpt_release_ccb(ccb);
3404 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3414 /* The address is reserved for many of the formats */
3417 ccb = cdgetccb(periph, /* priority */ 1);
3419 switch(dvdstruct->format) {
3420 case DVD_STRUCT_PHYSICAL:
3421 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3423 case DVD_STRUCT_COPYRIGHT:
3424 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3426 case DVD_STRUCT_DISCKEY:
3427 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3429 case DVD_STRUCT_BCA:
3430 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3432 case DVD_STRUCT_MANUFACT:
3433 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3435 case DVD_STRUCT_CMI:
3439 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
3440 address = dvdstruct->address;
3442 break; /* NOTREACHED */
3443 case DVD_STRUCT_PROTDISCID:
3444 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3446 case DVD_STRUCT_DISCKEYBLOCK:
3447 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3449 case DVD_STRUCT_DDS:
3450 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3452 case DVD_STRUCT_MEDIUM_STAT:
3453 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3455 case DVD_STRUCT_SPARE_AREA:
3456 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3458 case DVD_STRUCT_RMD_LAST:
3462 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
3463 address = dvdstruct->address;
3465 break; /* NOTREACHED */
3466 case DVD_STRUCT_RMD_RMA:
3470 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
3471 address = dvdstruct->address;
3473 break; /* NOTREACHED */
3474 case DVD_STRUCT_PRERECORDED:
3475 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3477 case DVD_STRUCT_UNIQUEID:
3478 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3480 case DVD_STRUCT_DCB:
3484 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
3485 address = dvdstruct->address;
3487 break; /* NOTREACHED */
3488 case DVD_STRUCT_LIST:
3490 * This is the maximum allocation length for the READ DVD
3491 * STRUCTURE command. There's nothing in the MMC3 spec
3492 * that indicates a limit in the amount of data that can
3493 * be returned from this call, other than the limits
3494 * imposed by the 2-byte length variables.
3501 break; /* NOTREACHED */
3505 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3509 scsi_read_dvd_structure(&ccb->csio,
3511 /* cbfcnp */ cddone,
3512 /* tag_action */ MSG_SIMPLE_Q_TAG,
3514 /* layer_number */ dvdstruct->layer_num,
3515 /* key_format */ dvdstruct->format,
3516 /* agid */ dvdstruct->agid,
3517 /* data_ptr */ databuf,
3518 /* dxfer_len */ length,
3519 /* sense_len */ SSD_FULL_SIZE,
3520 /* timeout */ 50000);
3522 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3523 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3528 switch(dvdstruct->format) {
3529 case DVD_STRUCT_PHYSICAL: {
3530 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
3531 struct dvd_layer *outlayer;
3532 struct scsi_read_dvd_struct_data_physical *phys_data;
3535 (struct scsi_read_dvd_struct_data_physical *)databuf;
3536 inlayer = &phys_data->layer_desc;
3537 outlayer = (struct dvd_layer *)&dvdstruct->data;
3539 dvdstruct->length = sizeof(*inlayer);
3541 outlayer->book_type = (inlayer->book_type_version &
3542 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
3543 outlayer->book_version = (inlayer->book_type_version &
3544 RDSD_BOOK_VERSION_MASK);
3545 outlayer->disc_size = (inlayer->disc_size_max_rate &
3546 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
3547 outlayer->max_rate = (inlayer->disc_size_max_rate &
3548 RDSD_MAX_RATE_MASK);
3549 outlayer->nlayers = (inlayer->layer_info &
3550 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
3551 outlayer->track_path = (inlayer->layer_info &
3552 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
3553 outlayer->layer_type = (inlayer->layer_info &
3554 RDSD_LAYER_TYPE_MASK);
3555 outlayer->linear_density = (inlayer->density &
3556 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
3557 outlayer->track_density = (inlayer->density &
3558 RDSD_TRACK_DENSITY_MASK);
3559 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
3561 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
3562 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
3563 outlayer->end_sector_l0 =
3564 scsi_3btoul(inlayer->end_sector_layer0);
3567 case DVD_STRUCT_COPYRIGHT: {
3568 struct scsi_read_dvd_struct_data_copyright *copy_data;
3570 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3573 dvdstruct->cpst = copy_data->cps_type;
3574 dvdstruct->rmi = copy_data->region_info;
3575 dvdstruct->length = 0;
3581 * Tell the user what the overall length is, no matter
3582 * what we can actually fit in the data buffer.
3584 dvdstruct->length = length - ccb->csio.resid -
3585 sizeof(struct scsi_read_dvd_struct_data_header);
3588 * But only actually copy out the smaller of what we read
3589 * in or what the structure can take.
3591 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3593 min(sizeof(dvdstruct->data), dvdstruct->length));
3598 if (databuf != NULL)
3599 free(databuf, M_DEVBUF);
3601 xpt_release_ccb(ccb);
3607 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
3608 void (*cbfcnp)(struct cam_periph *, union ccb *),
3609 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
3610 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
3611 u_int8_t sense_len, u_int32_t timeout)
3613 struct scsi_report_key *scsi_cmd;
3615 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
3616 bzero(scsi_cmd, sizeof(*scsi_cmd));
3617 scsi_cmd->opcode = REPORT_KEY;
3618 scsi_ulto4b(lba, scsi_cmd->lba);
3619 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3620 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3621 (key_format & RK_KF_KEYFORMAT_MASK);
3626 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3628 /*data_ptr*/ data_ptr,
3629 /*dxfer_len*/ dxfer_len,
3636 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
3637 void (*cbfcnp)(struct cam_periph *, union ccb *),
3638 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
3639 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3642 struct scsi_send_key *scsi_cmd;
3644 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
3645 bzero(scsi_cmd, sizeof(*scsi_cmd));
3646 scsi_cmd->opcode = SEND_KEY;
3648 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
3649 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3650 (key_format & RK_KF_KEYFORMAT_MASK);
3655 /*flags*/ CAM_DIR_OUT,
3657 /*data_ptr*/ data_ptr,
3658 /*dxfer_len*/ dxfer_len,
3666 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
3667 void (*cbfcnp)(struct cam_periph *, union ccb *),
3668 u_int8_t tag_action, u_int32_t address,
3669 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
3670 u_int8_t *data_ptr, u_int32_t dxfer_len,
3671 u_int8_t sense_len, u_int32_t timeout)
3673 struct scsi_read_dvd_structure *scsi_cmd;
3675 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
3676 bzero(scsi_cmd, sizeof(*scsi_cmd));
3677 scsi_cmd->opcode = READ_DVD_STRUCTURE;
3679 scsi_ulto4b(address, scsi_cmd->address);
3680 scsi_cmd->layer_number = layer_number;
3681 scsi_cmd->format = format;
3682 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3683 /* The AGID is the top two bits of this byte */
3684 scsi_cmd->agid = agid << 6;
3689 /*flags*/ CAM_DIR_IN,
3691 /*data_ptr*/ data_ptr,
3692 /*dxfer_len*/ dxfer_len,