2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 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.16 2003/10/21 22:26:11 thomas Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_cd.c,v 1.17 2004/09/18 19:40:24 dillon Exp $
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
52 #include <sys/param.h>
53 #include <sys/bootmaj.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
70 #include "../cam_ccb.h"
71 #include "../cam_extend.h"
72 #include "../cam_periph.h"
73 #include "../cam_xpt_periph.h"
74 #include "../cam_queue.h"
76 #include "scsi_message.h"
80 #define LEADOUT 0xaa /* leadout toc entry */
90 CD_Q_BCD_TRACKS = 0x02,
91 CD_Q_NO_CHANGER = 0x04,
93 CD_Q_10_BYTE_ONLY = 0x10
97 CD_FLAG_INVALID = 0x0001,
98 CD_FLAG_NEW_DISC = 0x0002,
99 CD_FLAG_DISC_LOCKED = 0x0004,
100 CD_FLAG_DISC_REMOVABLE = 0x0008,
101 CD_FLAG_TAGGED_QUEUING = 0x0010,
102 CD_FLAG_CHANGER = 0x0040,
103 CD_FLAG_ACTIVE = 0x0080,
104 CD_FLAG_SCHED_ON_COMP = 0x0100,
105 CD_FLAG_RETRY_UA = 0x0200,
106 CD_FLAG_VALID_MEDIA = 0x0400,
107 CD_FLAG_VALID_TOC = 0x0800,
108 CD_FLAG_OPEN = 0x1000,
109 CD_FLAG_SCTX_INIT = 0x2000
114 CD_CCB_BUFFER_IO = 0x02,
115 CD_CCB_WAITING = 0x03,
116 CD_CCB_TYPE_MASK = 0x0F,
117 CD_CCB_RETRY_UA = 0x10
121 CHANGER_TIMEOUT_SCHED = 0x01,
122 CHANGER_SHORT_TMOUT_SCHED = 0x02,
123 CHANGER_MANUAL_CALL = 0x04,
124 CHANGER_NEED_TIMEOUT = 0x08
127 #define ccb_state ppriv_field0
128 #define ccb_bp ppriv_ptr1
131 struct ioc_toc_header header;
132 struct cd_toc_entry entries[100];
135 struct cd_toc_single {
136 struct ioc_toc_header header;
137 struct cd_toc_entry entry;
148 volatile cd_flags flags;
149 struct buf_queue_head buf_queue;
150 LIST_HEAD(, ccb_hdr) pending_ccbs;
151 struct cd_params params;
155 struct devstat device_stats;
156 STAILQ_ENTRY(cd_softc) changer_links;
157 struct cdchanger *changer;
159 struct cam_periph *periph;
160 int minimum_command_size;
161 struct task sysctl_task;
162 struct sysctl_ctx_list sysctl_ctx;
163 struct sysctl_oid *sysctl_tree;
164 STAILQ_HEAD(, cd_mode_params) mode_queue;
165 struct cd_tocdata toc;
168 struct cd_page_sizes {
173 static struct cd_page_sizes cd_page_size_table[] =
175 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
178 struct cd_quirk_entry {
179 struct scsi_inquiry_pattern inq_pat;
184 * The changer quirk entries aren't strictly necessary. Basically, what
185 * they do is tell cdregister() up front that a device is a changer.
186 * Otherwise, it will figure that fact out once it sees a LUN on the device
187 * that is greater than 0. If it is known up front that a device is a changer,
188 * all I/O to the device will go through the changer scheduling routines, as
189 * opposed to the "normal" CD code.
191 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
192 * your device hangs when it gets a 10 byte command. Adding a quirk just
193 * to get rid of the informative diagnostic message is not acceptable. All
194 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
195 * referenced in a comment along with the quirk) , and must be approved by
196 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
197 * be removed until the submitter can explain why they are needed.
198 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
199 * when the CAM_NEW_TRAN_CODE work is done.
201 static struct cd_quirk_entry cd_quirk_table[] =
204 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
205 /*quirks*/ CD_Q_CHANGER
208 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
209 "*"}, /* quirks */ CD_Q_CHANGER
212 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
213 /* quirks */ CD_Q_CHANGER
216 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
217 /* quirks */ CD_Q_BCD_TRACKS
221 static d_open_t cdopen;
222 static d_close_t cdclose;
223 static d_ioctl_t cdioctl;
224 static d_strategy_t cdstrategy;
226 static periph_init_t cdinit;
227 static periph_ctor_t cdregister;
228 static periph_dtor_t cdcleanup;
229 static periph_start_t cdstart;
230 static periph_oninv_t cdoninvalidate;
231 static void cdasync(void *callback_arg, u_int32_t code,
232 struct cam_path *path, void *arg);
233 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
234 static void cdshorttimeout(void *arg);
235 static void cdschedule(struct cam_periph *periph, int priority);
236 static void cdrunchangerqueue(void *arg);
237 static void cdchangerschedule(struct cd_softc *softc);
238 static int cdrunccb(union ccb *ccb,
239 int (*error_routine)(union ccb *ccb,
241 u_int32_t sense_flags),
242 u_int32_t cam_flags, u_int32_t sense_flags);
243 static union ccb *cdgetccb(struct cam_periph *periph,
245 static void cddone(struct cam_periph *periph,
246 union ccb *start_ccb);
247 static int cderror(union ccb *ccb, u_int32_t cam_flags,
248 u_int32_t sense_flags);
249 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
250 static int cdgetpagesize(int page_num);
251 static void cdprevent(struct cam_periph *periph, int action);
252 static int cdcheckmedia(struct cam_periph *periph);
253 static int cdsize(struct cam_periph *periph, u_int32_t *size);
254 static int cd6byteworkaround(union ccb *ccb);
255 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
256 u_int32_t start, u_int8_t *data,
257 u_int32_t len, u_int32_t sense_flags);
258 static int cdgetmode(struct cam_periph *periph,
259 struct cd_mode_params *data, u_int32_t page);
260 static int cdsetmode(struct cam_periph *periph,
261 struct cd_mode_params *data);
262 static int cdplay(struct cam_periph *periph, u_int32_t blk,
264 static int cdreadsubchannel(struct cam_periph *periph,
265 u_int32_t mode, u_int32_t format,
267 struct cd_sub_channel_info *data,
269 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
270 u_int32_t starts, u_int32_t startf,
271 u_int32_t endm, u_int32_t ends,
273 static int cdplaytracks(struct cam_periph *periph,
274 u_int32_t strack, u_int32_t sindex,
275 u_int32_t etrack, u_int32_t eindex);
276 static int cdpause(struct cam_periph *periph, u_int32_t go);
277 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
278 static int cdstartunit(struct cam_periph *periph, int load);
279 static int cdsetspeed(struct cam_periph *periph,
280 u_int32_t rdspeed, u_int32_t wrspeed);
281 static int cdreportkey(struct cam_periph *periph,
282 struct dvd_authinfo *authinfo);
283 static int cdsendkey(struct cam_periph *periph,
284 struct dvd_authinfo *authinfo);
285 static int cdreaddvdstructure(struct cam_periph *periph,
286 struct dvd_struct *dvdstruct);
288 static struct periph_driver cddriver =
291 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
294 DATA_SET(periphdriver_set, cddriver);
296 /* For 2.2-stable support */
300 static struct cdevsw cd_cdevsw = {
302 /* maj */ SCSICD_CDEV_MAJOR,
310 /* write */ physwrite,
314 /* strategy */ cdstrategy,
319 static struct extend_array *cdperiphs;
320 static int num_changers;
322 #ifndef CHANGER_MIN_BUSY_SECONDS
323 #define CHANGER_MIN_BUSY_SECONDS 5
325 #ifndef CHANGER_MAX_BUSY_SECONDS
326 #define CHANGER_MAX_BUSY_SECONDS 15
329 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
330 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
333 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
336 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
337 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
338 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
339 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
340 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
341 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
342 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
343 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
347 target_id_t target_id;
350 struct timeval start_time;
351 struct cd_softc *cur_device;
352 struct callout short_handle;
353 struct callout long_handle;
354 volatile cd_changer_flags flags;
355 STAILQ_ENTRY(cdchanger) changer_links;
356 STAILQ_HEAD(chdevlist, cd_softc) chluns;
359 static STAILQ_HEAD(changerlist, cdchanger) changerq;
365 struct cam_path *path;
368 * Create our extend array for storing the devices we attach to.
370 cdperiphs = cam_extend_new();
371 if (cdperiphs == NULL) {
372 printf("cd: Failed to alloc extend array!\n");
377 * Install a global async callback. This callback will
378 * receive async callbacks like "new device found".
380 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
381 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
383 if (status == CAM_REQ_CMP) {
384 struct ccb_setasync csa;
386 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
387 csa.ccb_h.func_code = XPT_SASYNC_CB;
388 csa.event_enable = AC_FOUND_DEVICE;
389 csa.callback = cdasync;
390 csa.callback_arg = NULL;
391 xpt_action((union ccb *)&csa);
392 status = csa.ccb_h.status;
396 if (status != CAM_REQ_CMP) {
397 printf("cd: Failed to attach master async callback "
398 "due to status 0x%x!\n", status);
403 cdoninvalidate(struct cam_periph *periph)
406 struct cd_softc *softc;
408 struct ccb_setasync csa;
410 softc = (struct cd_softc *)periph->softc;
413 * De-register any async callbacks.
415 xpt_setup_ccb(&csa.ccb_h, periph->path,
417 csa.ccb_h.func_code = XPT_SASYNC_CB;
418 csa.event_enable = 0;
419 csa.callback = cdasync;
420 csa.callback_arg = periph;
421 xpt_action((union ccb *)&csa);
423 softc->flags |= CD_FLAG_INVALID;
426 * Although the oninvalidate() routines are always called at
427 * splsoftcam, we need to be at splbio() here to keep the buffer
428 * queue from being modified while we traverse it.
433 * Return all queued I/O with ENXIO.
434 * XXX Handle any transactions queued to the card
435 * with XPT_ABORT_CCB.
437 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
438 bufq_remove(&softc->buf_queue, q_bp);
439 q_bp->b_resid = q_bp->b_bcount;
440 q_bp->b_error = ENXIO;
441 q_bp->b_flags |= B_ERROR;
447 * If this device is part of a changer, and it was scheduled
448 * to run, remove it from the run queue since we just nuked
449 * all of its scheduled I/O.
451 if ((softc->flags & CD_FLAG_CHANGER)
452 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
453 camq_remove(&softc->changer->devq, softc->pinfo.index);
455 xpt_print_path(periph->path);
456 printf("lost device\n");
460 cdcleanup(struct cam_periph *periph)
462 struct cd_softc *softc;
465 softc = (struct cd_softc *)periph->softc;
467 xpt_print_path(periph->path);
468 printf("removing device entry\n");
470 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
471 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
472 xpt_print_path(periph->path);
473 printf("can't remove sysctl context\n");
478 * In the queued, non-active case, the device in question
479 * has already been removed from the changer run queue. Since this
480 * device is active, we need to de-activate it, and schedule
481 * another device to run. (if there is another one to run)
483 if ((softc->flags & CD_FLAG_CHANGER)
484 && (softc->flags & CD_FLAG_ACTIVE)) {
487 * The purpose of the short timeout is soley to determine
488 * whether the current device has finished or not. Well,
489 * since we're removing the active device, we know that it
490 * is finished. So, get rid of the short timeout.
491 * Otherwise, if we're in the time period before the short
492 * timeout fires, and there are no other devices in the
493 * queue to run, there won't be any other device put in the
494 * active slot. i.e., when we call cdrunchangerqueue()
495 * below, it won't do anything. Then, when the short
496 * timeout fires, it'll look at the "current device", which
497 * we are free below, and possibly panic the kernel on a
498 * bogus pointer reference.
500 * The long timeout doesn't really matter, since we
501 * decrement the qfrozen_cnt to indicate that there is
502 * nothing in the active slot now. Therefore, there won't
503 * be any bogus pointer references there.
505 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
506 callout_stop(&softc->changer->short_handle);
507 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
509 softc->changer->devq.qfrozen_cnt--;
510 softc->changer->flags |= CHANGER_MANUAL_CALL;
511 cdrunchangerqueue(softc->changer);
515 * If we're removing the last device on the changer, go ahead and
516 * remove the changer device structure.
518 if ((softc->flags & CD_FLAG_CHANGER)
519 && (--softc->changer->num_devices == 0)) {
522 * Theoretically, there shouldn't be any timeouts left, but
523 * I'm not completely sure that that will be the case. So,
524 * it won't hurt to check and see if there are any left.
526 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
527 callout_stop(&softc->changer->long_handle);
528 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
531 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
532 callout_stop(&softc->changer->short_handle);
533 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
536 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
538 xpt_print_path(periph->path);
539 printf("removing changer entry\n");
540 free(softc->changer, M_DEVBUF);
543 devstat_remove_entry(&softc->device_stats);
544 cam_extend_release(cdperiphs, periph->unit_number);
545 if (softc->disk.d_rawdev) {
546 disk_destroy(&softc->disk);
548 free(softc, M_DEVBUF);
553 cdasync(void *callback_arg, u_int32_t code,
554 struct cam_path *path, void *arg)
556 struct cam_periph *periph;
558 periph = (struct cam_periph *)callback_arg;
560 case AC_FOUND_DEVICE:
562 struct ccb_getdev *cgd;
565 cgd = (struct ccb_getdev *)arg;
567 if (SID_TYPE(&cgd->inq_data) != T_CDROM
568 && SID_TYPE(&cgd->inq_data) != T_WORM)
572 * Allocate a peripheral instance for
573 * this device and start the probe
576 status = cam_periph_alloc(cdregister, cdoninvalidate,
578 "cd", CAM_PERIPH_BIO,
579 cgd->ccb_h.path, cdasync,
580 AC_FOUND_DEVICE, cgd);
582 if (status != CAM_REQ_CMP
583 && status != CAM_REQ_INPROG)
584 printf("cdasync: Unable to attach new device "
585 "due to status 0x%x\n", status);
592 struct cd_softc *softc;
593 struct ccb_hdr *ccbh;
596 softc = (struct cd_softc *)periph->softc;
599 * Don't fail on the expected unit attention
602 softc->flags |= CD_FLAG_RETRY_UA;
603 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
604 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
605 ccbh->ccb_state |= CD_CCB_RETRY_UA;
610 cam_periph_async(periph, code, path, arg);
616 cdsysctlinit(void *context, int pending)
618 struct cam_periph *periph;
619 struct cd_softc *softc;
620 char tmpstr[80], tmpstr2[80];
622 periph = (struct cam_periph *)context;
623 softc = (struct cd_softc *)periph->softc;
625 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
626 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
628 sysctl_ctx_init(&softc->sysctl_ctx);
629 softc->flags |= CD_FLAG_SCTX_INIT;
630 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
631 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
632 tmpstr2, CTLFLAG_RD, 0, tmpstr);
634 if (softc->sysctl_tree == NULL) {
635 printf("cdsysctlinit: unable to allocate sysctl tree\n");
640 * Now register the sysctl handler, so the user can the value on
643 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
644 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
645 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
650 * We have a handler function for this so we can check the values when the
651 * user sets them, instead of every time we look at them.
654 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
658 value = *(int *)arg1;
660 error = sysctl_handle_int(oidp, &value, 0, req);
662 if ((error != 0) || (req->newptr == NULL))
666 * The only real values we can have here are 6 or 10. I don't
667 * really forsee having 12 be an option at any time in the future.
668 * So if the user sets something less than or equal to 6, we'll set
669 * it to 6. If he sets something greater than 6, we'll set it to 10.
671 * I suppose we could just return an error here for the wrong values,
672 * but I don't think it's necessary to do so, as long as we can
673 * determine the user's intent without too much trouble.
680 *(int *)arg1 = value;
687 cdregister(struct cam_periph *periph, void *arg)
689 struct cd_softc *softc;
690 struct ccb_setasync csa;
691 struct ccb_pathinq cpi;
692 struct ccb_getdev *cgd;
696 cgd = (struct ccb_getdev *)arg;
697 if (periph == NULL) {
698 printf("cdregister: periph was NULL!!\n");
699 return(CAM_REQ_CMP_ERR);
702 printf("cdregister: no getdev CCB, can't register device\n");
703 return(CAM_REQ_CMP_ERR);
706 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
707 LIST_INIT(&softc->pending_ccbs);
708 STAILQ_INIT(&softc->mode_queue);
709 softc->state = CD_STATE_PROBE;
710 bufq_init(&softc->buf_queue);
711 if (SID_IS_REMOVABLE(&cgd->inq_data))
712 softc->flags |= CD_FLAG_DISC_REMOVABLE;
713 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
714 softc->flags |= CD_FLAG_TAGGED_QUEUING;
716 periph->softc = softc;
717 softc->periph = periph;
719 cam_extend_set(cdperiphs, periph->unit_number, periph);
722 * See if this device has any quirks.
724 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
725 (caddr_t)cd_quirk_table,
726 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
727 sizeof(*cd_quirk_table), scsi_inquiry_match);
730 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
732 softc->quirks = CD_Q_NONE;
734 /* Check if the SIM does not want 6 byte commands */
735 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
736 cpi.ccb_h.func_code = XPT_PATH_INQ;
737 xpt_action((union ccb *)&cpi);
738 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
739 softc->quirks |= CD_Q_10_BYTE_ONLY;
741 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
743 /* The default is 6 byte commands, unless quirked otherwise */
744 if (softc->quirks & CD_Q_10_BYTE_ONLY)
745 softc->minimum_command_size = 10;
747 softc->minimum_command_size = 6;
750 * Load the user's default, if any.
752 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
753 periph->unit_number);
754 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
756 /* 6 and 10 are the only permissible values here. */
757 if (softc->minimum_command_size < 6)
758 softc->minimum_command_size = 6;
759 else if (softc->minimum_command_size > 6)
760 softc->minimum_command_size = 10;
763 * We need to register the statistics structure for this device,
764 * but we don't have the blocksize yet for it. So, we register
765 * the structure and indicate that we don't have the blocksize
766 * yet. Unlike other SCSI peripheral drivers, we explicitly set
767 * the device type here to be CDROM, rather than just ORing in
768 * the device type. This is because this driver can attach to either
769 * CDROM or WORM devices, and we want this peripheral driver to
770 * show up in the devstat list as a CD peripheral driver, not a
771 * WORM peripheral driver. WORM drives will also have the WORM
772 * driver attached to them.
774 devstat_add_entry(&softc->device_stats, "cd",
775 periph->unit_number, 0,
776 DEVSTAT_BS_UNAVAILABLE,
777 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
778 DEVSTAT_PRIORITY_CD);
779 disk_create(periph->unit_number, &softc->disk,
780 DSO_ONESLICE | DSO_COMPATLABEL, &cd_cdevsw);
783 * Add an async callback so that we get
784 * notified if this device goes away.
786 xpt_setup_ccb(&csa.ccb_h, periph->path,
788 csa.ccb_h.func_code = XPT_SASYNC_CB;
789 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
790 csa.callback = cdasync;
791 csa.callback_arg = periph;
792 xpt_action((union ccb *)&csa);
795 * If the target lun is greater than 0, we most likely have a CD
796 * changer device. Check the quirk entries as well, though, just
797 * in case someone has a CD tower with one lun per drive or
798 * something like that. Also, if we know up front that a
799 * particular device is a changer, we can mark it as such starting
800 * with lun 0, instead of lun 1. It shouldn't be necessary to have
801 * a quirk entry to define something as a changer, however.
803 if (((cgd->ccb_h.target_lun > 0)
804 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
805 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
806 struct cdchanger *nchanger;
807 struct cam_periph *nperiph;
808 struct cam_path *path;
812 /* Set the changer flag in the current device's softc */
813 softc->flags |= CD_FLAG_CHANGER;
815 if (num_changers == 0)
816 STAILQ_INIT(&changerq);
819 * Now, look around for an existing changer device with the
820 * same path and target ID as the current device.
823 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
825 nchanger = STAILQ_NEXT(nchanger, changer_links)){
826 if ((nchanger->path_id == cgd->ccb_h.path_id)
827 && (nchanger->target_id == cgd->ccb_h.target_id)) {
834 * If we found a matching entry, just add this device to
835 * the list of devices on this changer.
838 struct chdevlist *chlunhead;
840 chlunhead = &nchanger->chluns;
843 * XXX KDM look at consolidating this code with the
844 * code below in a separate function.
848 * Create a path with lun id 0, and see if we can
849 * find a matching device
851 status = xpt_create_path(&path, /*periph*/ periph,
853 cgd->ccb_h.target_id, 0);
855 if ((status == CAM_REQ_CMP)
856 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
857 struct cd_softc *nsoftc;
859 nsoftc = (struct cd_softc *)nperiph->softc;
861 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
862 nsoftc->flags |= CD_FLAG_CHANGER;
863 nchanger->num_devices++;
864 if (camq_resize(&nchanger->devq,
865 nchanger->num_devices)!=CAM_REQ_CMP){
866 printf("cdregister: "
872 nsoftc->changer = nchanger;
873 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
875 STAILQ_INSERT_TAIL(&nchanger->chluns,
876 nsoftc,changer_links);
879 } else if (status == CAM_REQ_CMP)
882 printf("cdregister: unable to allocate path\n"
883 "cdregister: changer support may be "
887 nchanger->num_devices++;
889 softc->changer = nchanger;
890 softc->pinfo.index = CAM_UNQUEUED_INDEX;
892 if (camq_resize(&nchanger->devq,
893 nchanger->num_devices) != CAM_REQ_CMP) {
894 printf("cdregister: camq_resize "
895 "failed, changer support may "
899 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
902 * In this case, we don't already have an entry for this
903 * particular changer, so we need to create one, add it to
904 * the queue, and queue this device on the list for this
905 * changer. Before we queue this device, however, we need
906 * to search for lun id 0 on this target, and add it to the
907 * queue first, if it exists. (and if it hasn't already
908 * been marked as part of the changer.)
911 nchanger = malloc(sizeof(struct cdchanger),
912 M_DEVBUF, M_INTWAIT | M_ZERO);
913 callout_init(&nchanger->short_handle);
914 callout_init(&nchanger->long_handle);
915 if (camq_init(&nchanger->devq, 1) != 0) {
916 softc->flags &= ~CD_FLAG_CHANGER;
917 printf("cdregister: changer support "
924 nchanger->path_id = cgd->ccb_h.path_id;
925 nchanger->target_id = cgd->ccb_h.target_id;
927 /* this is superfluous, but it makes things clearer */
928 nchanger->num_devices = 0;
930 STAILQ_INIT(&nchanger->chluns);
932 STAILQ_INSERT_TAIL(&changerq, nchanger,
936 * Create a path with lun id 0, and see if we can
937 * find a matching device
939 status = xpt_create_path(&path, /*periph*/ periph,
941 cgd->ccb_h.target_id, 0);
944 * If we were able to allocate the path, and if we
945 * find a matching device and it isn't already
946 * marked as part of a changer, then we add it to
947 * the current changer.
949 if ((status == CAM_REQ_CMP)
950 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
951 && ((((struct cd_softc *)periph->softc)->flags &
952 CD_FLAG_CHANGER) == 0)) {
953 struct cd_softc *nsoftc;
955 nsoftc = (struct cd_softc *)nperiph->softc;
957 nsoftc->flags |= CD_FLAG_CHANGER;
958 nchanger->num_devices++;
959 if (camq_resize(&nchanger->devq,
960 nchanger->num_devices) != CAM_REQ_CMP) {
961 printf("cdregister: camq_resize "
962 "failed, changer support may "
965 nsoftc->changer = nchanger;
966 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
968 STAILQ_INSERT_TAIL(&nchanger->chluns,
969 nsoftc, changer_links);
971 } else if (status == CAM_REQ_CMP)
974 printf("cdregister: unable to allocate path\n"
975 "cdregister: changer support may be "
979 softc->changer = nchanger;
980 softc->pinfo.index = CAM_UNQUEUED_INDEX;
981 nchanger->num_devices++;
982 if (camq_resize(&nchanger->devq,
983 nchanger->num_devices) != CAM_REQ_CMP) {
984 printf("cdregister: camq_resize "
985 "failed, changer support may "
988 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
995 /* Lock this peripheral until we are setup */
997 cam_periph_lock(periph, 0);
999 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1000 xpt_schedule(periph, /*priority*/5);
1002 cdschedule(periph, /*priority*/ 5);
1004 return(CAM_REQ_CMP);
1008 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
1010 struct cam_periph *periph;
1011 struct cd_softc *softc;
1016 periph = cam_extend_get(cdperiphs, unit);
1021 softc = (struct cd_softc *)periph->softc;
1024 * Grab splsoftcam and hold it until we lock the peripheral.
1027 if (softc->flags & CD_FLAG_INVALID) {
1032 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
1039 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1043 * Check for media, and set the appropriate flags. We don't bail
1044 * if we don't have media, but then we don't allow anything but the
1045 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1047 * XXX KDM for now, we do fail the open if we don't have media. We
1048 * can change this once we've figured out how to make the slice
1049 * code work well with media changing underneath it.
1051 error = cdcheckmedia(periph);
1054 softc->flags |= CD_FLAG_OPEN;
1056 cam_periph_unlock(periph);
1058 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1064 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
1066 struct cam_periph *periph;
1067 struct cd_softc *softc;
1071 periph = cam_extend_get(cdperiphs, unit);
1075 softc = (struct cd_softc *)periph->softc;
1077 if ((error = cam_periph_lock(periph, 0)) != 0)
1080 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1081 cdprevent(periph, PR_ALLOW);
1084 * Unconditionally set the dsopen() flags back to their default
1087 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1088 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1091 * Since we're closing this CD, mark the blocksize as unavailable.
1092 * It will be marked as available when the CD is opened again.
1094 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1097 * We'll check the media and toc again at the next open().
1099 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1101 softc->flags &= ~CD_FLAG_OPEN;
1103 cam_periph_unlock(periph);
1104 cam_periph_release(periph);
1110 cdshorttimeout(void *arg)
1112 struct cdchanger *changer;
1117 changer = (struct cdchanger *)arg;
1119 /* Always clear the short timeout flag, since that's what we're in */
1120 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1123 * Check to see if there is any more pending or outstanding I/O for
1124 * this device. If not, move it out of the active slot.
1126 if ((bufq_first(&changer->cur_device->buf_queue) == NULL)
1127 && (changer->cur_device->device_stats.busy_count == 0)) {
1128 changer->flags |= CHANGER_MANUAL_CALL;
1129 cdrunchangerqueue(changer);
1136 * This is a wrapper for xpt_schedule. It only applies to changers.
1139 cdschedule(struct cam_periph *periph, int priority)
1141 struct cd_softc *softc;
1146 softc = (struct cd_softc *)periph->softc;
1149 * If this device isn't currently queued, and if it isn't
1150 * the active device, then we queue this device and run the
1151 * changer queue if there is no timeout scheduled to do it.
1152 * If this device is the active device, just schedule it
1153 * to run again. If this device is queued, there should be
1154 * a timeout in place already that will make sure it runs.
1156 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1157 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1159 * We don't do anything with the priority here.
1160 * This is strictly a fifo queue.
1162 softc->pinfo.priority = 1;
1163 softc->pinfo.generation = ++softc->changer->devq.generation;
1164 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1167 * Since we just put a device in the changer queue,
1168 * check and see if there is a timeout scheduled for
1169 * this changer. If so, let the timeout handle
1170 * switching this device into the active slot. If
1171 * not, manually call the timeout routine to
1174 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1175 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1176 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1177 softc->changer->flags |= CHANGER_MANUAL_CALL;
1178 cdrunchangerqueue(softc->changer);
1180 } else if ((softc->flags & CD_FLAG_ACTIVE)
1181 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1182 xpt_schedule(periph, priority);
1189 cdrunchangerqueue(void *arg)
1191 struct cd_softc *softc;
1192 struct cdchanger *changer;
1193 int called_from_timeout;
1198 changer = (struct cdchanger *)arg;
1201 * If we have NOT been called from cdstrategy() or cddone(), and
1202 * instead from a timeout routine, go ahead and clear the
1205 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1206 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1207 called_from_timeout = 1;
1209 called_from_timeout = 0;
1211 /* Always clear the manual call flag */
1212 changer->flags &= ~CHANGER_MANUAL_CALL;
1214 /* nothing to do if the queue is empty */
1215 if (changer->devq.entries <= 0) {
1221 * If the changer queue is frozen, that means we have an active
1224 if (changer->devq.qfrozen_cnt > 0) {
1226 if (changer->cur_device->device_stats.busy_count > 0) {
1227 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1228 changer->cur_device->bufs_left =
1229 changer->cur_device->device_stats.busy_count;
1230 if (called_from_timeout) {
1231 callout_reset(&changer->long_handle,
1232 changer_max_busy_seconds * hz,
1233 cdrunchangerqueue, changer);
1234 changer->flags |= CHANGER_TIMEOUT_SCHED;
1241 * We always need to reset the frozen count and clear the
1244 changer->devq.qfrozen_cnt--;
1245 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1246 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1249 * Check to see whether the current device has any I/O left
1250 * to do. If so, requeue it at the end of the queue. If
1251 * not, there is no need to requeue it.
1253 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1255 changer->cur_device->pinfo.generation =
1256 ++changer->devq.generation;
1257 camq_insert(&changer->devq,
1258 (cam_pinfo *)changer->cur_device);
1262 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1264 changer->cur_device = softc;
1266 changer->devq.qfrozen_cnt++;
1267 softc->flags |= CD_FLAG_ACTIVE;
1269 /* Just in case this device is waiting */
1270 wakeup(&softc->changer);
1271 xpt_schedule(softc->periph, /*priority*/ 1);
1274 * Get rid of any pending timeouts, and set a flag to schedule new
1275 * ones so this device gets its full time quantum.
1277 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1278 callout_stop(&changer->long_handle);
1279 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1282 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1283 callout_stop(&changer->short_handle);
1284 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1288 * We need to schedule timeouts, but we only do this after the
1289 * first transaction has completed. This eliminates the changer
1292 changer->flags |= CHANGER_NEED_TIMEOUT;
1298 cdchangerschedule(struct cd_softc *softc)
1300 struct cdchanger *changer;
1305 changer = softc->changer;
1308 * If this is a changer, and this is the current device,
1309 * and this device has at least the minimum time quantum to
1310 * run, see if we can switch it out.
1312 if ((softc->flags & CD_FLAG_ACTIVE)
1313 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1314 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1316 * We try three things here. The first is that we
1317 * check to see whether the schedule on completion
1318 * flag is set. If it is, we decrement the number
1319 * of buffers left, and if it's zero, we reschedule.
1320 * Next, we check to see whether the pending buffer
1321 * queue is empty and whether there are no
1322 * outstanding transactions. If so, we reschedule.
1323 * Next, we see if the pending buffer queue is empty.
1324 * If it is, we set the number of buffers left to
1325 * the current active buffer count and set the
1326 * schedule on complete flag.
1328 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1329 if (--softc->bufs_left == 0) {
1330 softc->changer->flags |=
1331 CHANGER_MANUAL_CALL;
1332 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1333 cdrunchangerqueue(softc->changer);
1335 } else if ((bufq_first(&softc->buf_queue) == NULL)
1336 && (softc->device_stats.busy_count == 0)) {
1337 softc->changer->flags |= CHANGER_MANUAL_CALL;
1338 cdrunchangerqueue(softc->changer);
1340 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1341 && (softc->flags & CD_FLAG_ACTIVE)) {
1344 * Now that the first transaction to this
1345 * particular device has completed, we can go ahead
1346 * and schedule our timeouts.
1348 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1349 callout_reset(&changer->long_handle,
1350 changer_max_busy_seconds * hz,
1351 cdrunchangerqueue, changer);
1352 changer->flags |= CHANGER_TIMEOUT_SCHED;
1354 printf("cdchangerschedule: already have a long"
1357 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1358 callout_reset(&changer->short_handle,
1359 changer_min_busy_seconds * hz,
1360 cdshorttimeout, changer);
1361 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1363 printf("cdchangerschedule: already have a short "
1367 * We just scheduled timeouts, no need to schedule
1370 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1377 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1378 u_int32_t cam_flags,
1379 u_int32_t sense_flags),
1380 u_int32_t cam_flags, u_int32_t sense_flags)
1382 struct cd_softc *softc;
1383 struct cam_periph *periph;
1386 periph = xpt_path_periph(ccb->ccb_h.path);
1387 softc = (struct cd_softc *)periph->softc;
1389 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1390 &softc->device_stats);
1392 if (softc->flags & CD_FLAG_CHANGER)
1393 cdchangerschedule(softc);
1399 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1401 struct cd_softc *softc;
1404 softc = (struct cd_softc *)periph->softc;
1406 if (softc->flags & CD_FLAG_CHANGER) {
1411 * This should work the first time this device is woken up,
1412 * but just in case it doesn't, we use a while loop.
1414 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1416 * If this changer isn't already queued, queue it up.
1418 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1419 softc->pinfo.priority = 1;
1420 softc->pinfo.generation =
1421 ++softc->changer->devq.generation;
1422 camq_insert(&softc->changer->devq,
1423 (cam_pinfo *)softc);
1425 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1426 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1427 && ((softc->changer->flags
1428 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1429 softc->changer->flags |= CHANGER_MANUAL_CALL;
1430 cdrunchangerqueue(softc->changer);
1432 tsleep(&softc->changer, 0, "cgticb", 0);
1436 return(cam_periph_getccb(periph, priority));
1441 * Actually translate the requested transfer into one the physical driver
1442 * can understand. The transfer is described by a buf and will include
1443 * only one physical transfer.
1446 cdstrategy(struct buf *bp)
1448 struct cam_periph *periph;
1449 struct cd_softc *softc;
1453 unit = dkunit(bp->b_dev);
1454 part = dkpart(bp->b_dev);
1455 periph = cam_extend_get(cdperiphs, unit);
1456 if (periph == NULL) {
1457 bp->b_error = ENXIO;
1461 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1463 softc = (struct cd_softc *)periph->softc;
1466 * Mask interrupts so that the pack cannot be invalidated until
1467 * after we are in the queue. Otherwise, we might not properly
1468 * clean up one of the buffers.
1473 * If the device has been made invalid, error out
1475 if ((softc->flags & CD_FLAG_INVALID)) {
1477 bp->b_error = ENXIO;
1482 * If we don't have valid media, look for it before trying to
1485 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1488 error = cdcheckmedia(periph);
1491 bp->b_error = error;
1497 * Place it in the queue of disk activities for this disk
1499 bufqdisksort(&softc->buf_queue, bp);
1504 * Schedule ourselves for performing the work. We do things
1505 * differently for changers.
1507 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1508 xpt_schedule(periph, /* XXX priority */1);
1510 cdschedule(periph, /* priority */ 1);
1514 bp->b_flags |= B_ERROR;
1516 * Correctly set the buf to indicate a completed xfer
1518 bp->b_resid = bp->b_bcount;
1524 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1526 struct cd_softc *softc;
1528 struct ccb_scsiio *csio;
1529 struct scsi_read_capacity_data *rcap;
1532 softc = (struct cd_softc *)periph->softc;
1534 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1536 switch (softc->state) {
1537 case CD_STATE_NORMAL:
1542 bp = bufq_first(&softc->buf_queue);
1543 if (periph->immediate_priority <= periph->pinfo.priority) {
1544 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1546 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1548 periph->immediate_priority = CAM_PRIORITY_NONE;
1550 wakeup(&periph->ccb_list);
1551 } else if (bp == NULL) {
1553 xpt_release_ccb(start_ccb);
1555 bufq_remove(&softc->buf_queue, bp);
1557 devstat_start_transaction(&softc->device_stats);
1559 scsi_read_write(&start_ccb->csio,
1561 /* cbfcnp */ cddone,
1562 (bp->b_flags & B_ORDERED) != 0 ?
1565 /* read */bp->b_flags & B_READ,
1567 /* minimum_cmd_size */ 10,
1568 /* lba */ bp->b_pblkno,
1569 bp->b_bcount / softc->params.blksize,
1570 /* data_ptr */ bp->b_data,
1571 /* dxfer_len */ bp->b_bcount,
1572 /* sense_len */ SSD_FULL_SIZE,
1573 /* timeout */ 30000);
1574 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1578 * Block out any asyncronous callbacks
1579 * while we touch the pending ccb list.
1582 LIST_INSERT_HEAD(&softc->pending_ccbs,
1583 &start_ccb->ccb_h, periph_links.le);
1586 /* We expect a unit attention from this device */
1587 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1588 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1589 softc->flags &= ~CD_FLAG_RETRY_UA;
1592 start_ccb->ccb_h.ccb_bp = bp;
1593 bp = bufq_first(&softc->buf_queue);
1596 xpt_action(start_ccb);
1599 /* Have more work to do, so ensure we stay scheduled */
1600 xpt_schedule(periph, /* XXX priority */1);
1604 case CD_STATE_PROBE:
1607 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT);
1608 csio = &start_ccb->csio;
1609 scsi_read_capacity(csio,
1616 start_ccb->ccb_h.ccb_bp = NULL;
1617 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1618 xpt_action(start_ccb);
1625 cddone(struct cam_periph *periph, union ccb *done_ccb)
1627 struct cd_softc *softc;
1628 struct ccb_scsiio *csio;
1630 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1632 softc = (struct cd_softc *)periph->softc;
1633 csio = &done_ccb->csio;
1635 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1636 case CD_CCB_BUFFER_IO:
1642 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1645 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1648 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1653 /* Retry selection timeouts */
1654 sf |= SF_RETRY_SELTO;
1656 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1658 * A retry was scheuled, so
1669 xpt_print_path(periph->path);
1670 printf("cddone: got error %#x back\n", error);
1672 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1673 bufq_remove(&softc->buf_queue, q_bp);
1674 q_bp->b_resid = q_bp->b_bcount;
1675 q_bp->b_error = EIO;
1676 q_bp->b_flags |= B_ERROR;
1680 bp->b_resid = bp->b_bcount;
1681 bp->b_error = error;
1682 bp->b_flags |= B_ERROR;
1683 cam_release_devq(done_ccb->ccb_h.path,
1687 /*getcount_only*/0);
1690 bp->b_resid = csio->resid;
1692 if (bp->b_resid != 0) {
1693 /* Short transfer ??? */
1694 bp->b_flags |= B_ERROR;
1699 * Block out any asyncronous callbacks
1700 * while we touch the pending ccb list.
1703 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1706 if (softc->flags & CD_FLAG_CHANGER)
1707 cdchangerschedule(softc);
1709 devstat_end_transaction_buf(&softc->device_stats, bp);
1715 struct scsi_read_capacity_data *rdcap;
1716 char announce_buf[120]; /*
1717 * Currently (9/30/97) the
1718 * longest possible announce
1719 * buffer is 108 bytes, for the
1720 * first error case below.
1721 * That is 39 bytes for the
1722 * basic string, 16 bytes for the
1723 * biggest sense key (hardware
1724 * error), 52 bytes for the
1725 * text of the largest sense
1726 * qualifier valid for a CDROM,
1727 * (0x72, 0x03 or 0x04,
1728 * 0x03), and one byte for the
1729 * null terminating character.
1730 * To allow for longer strings,
1731 * the announce buffer is 120
1734 struct cd_params *cdp;
1736 cdp = &softc->params;
1738 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1740 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1741 cdp->blksize = scsi_4btoul (rdcap->length);
1743 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1745 snprintf(announce_buf, sizeof(announce_buf),
1746 "cd present [%lu x %lu byte records]",
1747 cdp->disksize, (u_long)cdp->blksize);
1752 * Retry any UNIT ATTENTION type errors. They
1753 * are expected at boot.
1755 error = cderror(done_ccb, 0, SF_RETRY_UA |
1756 SF_NO_PRINT | SF_RETRY_SELTO);
1757 if (error == ERESTART) {
1759 * A retry was scheuled, so
1763 } else if (error != 0) {
1765 struct scsi_sense_data *sense;
1767 int sense_key, error_code;
1770 struct ccb_getdev cgd;
1772 /* Don't wedge this device's queue */
1773 cam_release_devq(done_ccb->ccb_h.path,
1777 /*getcount_only*/0);
1779 status = done_ccb->ccb_h.status;
1781 xpt_setup_ccb(&cgd.ccb_h,
1782 done_ccb->ccb_h.path,
1784 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1785 xpt_action((union ccb *)&cgd);
1787 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1788 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1789 || ((status & CAM_AUTOSNS_VALID) == 0))
1795 sense = &csio->sense_data;
1796 scsi_extract_sense(sense, &error_code,
1801 * Attach to anything that claims to be a
1802 * CDROM or WORM device, as long as it
1803 * doesn't return a "Logical unit not
1804 * supported" (0x25) error.
1806 if ((have_sense) && (asc != 0x25)
1807 && (error_code == SSD_CURRENT_ERROR))
1808 snprintf(announce_buf,
1809 sizeof(announce_buf),
1810 "Attempt to query device "
1811 "size failed: %s, %s",
1812 scsi_sense_key_text[sense_key],
1813 scsi_sense_desc(asc,ascq,
1815 else if ((have_sense == 0)
1816 && ((status & CAM_STATUS_MASK) ==
1817 CAM_SCSI_STATUS_ERROR)
1818 && (csio->scsi_status ==
1819 SCSI_STATUS_BUSY)) {
1820 snprintf(announce_buf,
1821 sizeof(announce_buf),
1822 "Attempt to query device "
1823 "size failed: SCSI status: BUSY");
1824 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1826 * We only print out an error for
1827 * CDROM type devices. For WORM
1828 * devices, we don't print out an
1829 * error since a few WORM devices
1830 * don't support CDROM commands.
1831 * If we have sense information, go
1832 * ahead and print it out.
1833 * Otherwise, just say that we
1838 * Just print out the error, not
1839 * the full probe message, when we
1846 xpt_print_path(periph->path);
1847 printf("got CAM status %#x\n",
1848 done_ccb->ccb_h.status);
1850 xpt_print_path(periph->path);
1851 printf("fatal error, failed"
1852 " to attach to device\n");
1855 * Invalidate this peripheral.
1857 cam_periph_invalidate(periph);
1859 announce_buf[0] = '\0';
1863 * Invalidate this peripheral.
1865 cam_periph_invalidate(periph);
1866 announce_buf[0] = '\0';
1870 free(rdcap, M_TEMP);
1871 if (announce_buf[0] != '\0') {
1872 xpt_announce_periph(periph, announce_buf);
1873 if (softc->flags & CD_FLAG_CHANGER)
1874 cdchangerschedule(softc);
1876 * Create our sysctl variables, now that we know
1877 * we have successfully attached.
1879 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1881 softc->state = CD_STATE_NORMAL;
1883 * Since our peripheral may be invalidated by an error
1884 * above or an external event, we must release our CCB
1885 * before releasing the probe lock on the peripheral.
1886 * The peripheral will only go away once the last lock
1887 * is removed, and we need it around for the CCB release
1890 xpt_release_ccb(done_ccb);
1891 cam_periph_unlock(periph);
1894 case CD_CCB_WAITING:
1896 /* Caller will release the CCB */
1897 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1898 ("trying to wakeup ccbwait\n"));
1900 wakeup(&done_ccb->ccb_h.cbfcnp);
1906 xpt_release_ccb(done_ccb);
1909 static union cd_pages *
1910 cdgetpage(struct cd_mode_params *mode_params)
1912 union cd_pages *page;
1914 if (mode_params->cdb_size == 10)
1915 page = (union cd_pages *)find_mode_page_10(
1916 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1918 page = (union cd_pages *)find_mode_page_6(
1919 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1925 cdgetpagesize(int page_num)
1929 for (i = 0; i < (sizeof(cd_page_size_table)/
1930 sizeof(cd_page_size_table[0])); i++) {
1931 if (cd_page_size_table[i].page == page_num)
1932 return (cd_page_size_table[i].page_size);
1938 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1941 struct cam_periph *periph;
1942 struct cd_softc *softc;
1947 periph = cam_extend_get(cdperiphs, unit);
1951 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1953 softc = (struct cd_softc *)periph->softc;
1955 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1956 ("trying to do ioctl %#lx\n", cmd));
1958 error = cam_periph_lock(periph, PCATCH);
1964 * If we don't have media loaded, check for it. If still don't
1965 * have media loaded, we can only do a load or eject.
1967 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1968 && ((cmd != CDIOCCLOSE)
1969 && (cmd != CDIOCEJECT))) {
1970 error = cdcheckmedia(periph);
1972 cam_periph_unlock(periph);
1979 case CDIOCPLAYTRACKS:
1981 struct ioc_play_track *args
1982 = (struct ioc_play_track *) addr;
1983 struct cd_mode_params params;
1984 union cd_pages *page;
1986 params.alloc_len = sizeof(union cd_mode_data_6_10);
1987 params.mode_buf = malloc(params.alloc_len, M_TEMP,
1990 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1991 ("trying to do CDIOCPLAYTRACKS\n"));
1993 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1995 free(params.mode_buf, M_TEMP);
1998 page = cdgetpage(¶ms);
2000 page->audio.flags &= ~CD_PA_SOTC;
2001 page->audio.flags |= CD_PA_IMMED;
2002 error = cdsetmode(periph, ¶ms);
2003 free(params.mode_buf, M_TEMP);
2008 * This was originally implemented with the PLAY
2009 * AUDIO TRACK INDEX command, but that command was
2010 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2011 * drives support it but ATAPI and ATAPI-derivative
2012 * drives don't seem to support it. So we keep a
2013 * cache of the table of contents and translate
2014 * track numbers to MSF format.
2016 if (softc->flags & CD_FLAG_VALID_TOC) {
2017 union msf_lba *sentry, *eentry;
2020 if (args->end_track <
2021 softc->toc.header.ending_track + 1)
2023 if (args->end_track >
2024 softc->toc.header.ending_track + 1)
2026 softc->toc.header.ending_track + 1;
2027 st = args->start_track -
2028 softc->toc.header.starting_track;
2029 et = args->end_track -
2030 softc->toc.header.starting_track;
2033 || (st > (softc->toc.header.ending_track -
2034 softc->toc.header.starting_track))) {
2038 sentry = &softc->toc.entries[st].addr;
2039 eentry = &softc->toc.entries[et].addr;
2040 error = cdplaymsf(periph,
2049 * If we don't have a valid TOC, try the
2050 * play track index command. It is part of
2051 * the SCSI-2 spec, but was removed in the
2052 * MMC specs. ATAPI and ATAPI-derived
2053 * drives don't support it.
2055 if (softc->quirks & CD_Q_BCD_TRACKS) {
2057 bin2bcd(args->start_track);
2059 bin2bcd(args->end_track);
2061 error = cdplaytracks(periph,
2071 struct ioc_play_msf *args
2072 = (struct ioc_play_msf *) addr;
2073 struct cd_mode_params params;
2074 union cd_pages *page;
2076 params.alloc_len = sizeof(union cd_mode_data_6_10);
2077 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2080 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2081 ("trying to do CDIOCPLAYMSF\n"));
2083 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2085 free(params.mode_buf, M_TEMP);
2088 page = cdgetpage(¶ms);
2090 page->audio.flags &= ~CD_PA_SOTC;
2091 page->audio.flags |= CD_PA_IMMED;
2092 error = cdsetmode(periph, ¶ms);
2093 free(params.mode_buf, M_TEMP);
2096 error = cdplaymsf(periph,
2105 case CDIOCPLAYBLOCKS:
2107 struct ioc_play_blocks *args
2108 = (struct ioc_play_blocks *) addr;
2109 struct cd_mode_params params;
2110 union cd_pages *page;
2112 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2113 ("trying to do CDIOCPLAYBLOCKS\n"));
2115 params.alloc_len = sizeof(union cd_mode_data_6_10);
2116 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2119 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2121 free(params.mode_buf, M_TEMP);
2124 page = cdgetpage(¶ms);
2126 page->audio.flags &= ~CD_PA_SOTC;
2127 page->audio.flags |= CD_PA_IMMED;
2128 error = cdsetmode(periph, ¶ms);
2129 free(params.mode_buf, M_TEMP);
2132 error = cdplay(periph, args->blk, args->len);
2135 case CDIOCREADSUBCHANNEL:
2137 struct ioc_read_subchannel *args
2138 = (struct ioc_read_subchannel *) addr;
2139 struct cd_sub_channel_info *data;
2140 u_int32_t len = args->data_len;
2142 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2143 ("trying to do CDIOCREADSUBCHANNEL\n"));
2145 data = malloc(sizeof(struct cd_sub_channel_info),
2148 if ((len > sizeof(struct cd_sub_channel_info)) ||
2149 (len < sizeof(struct cd_sub_channel_header))) {
2151 "scsi_cd: cdioctl: "
2152 "cdioreadsubchannel: error, len=%d\n",
2159 if (softc->quirks & CD_Q_BCD_TRACKS)
2160 args->track = bin2bcd(args->track);
2162 error = cdreadsubchannel(periph, args->address_format,
2163 args->data_format, args->track, data, len);
2169 if (softc->quirks & CD_Q_BCD_TRACKS)
2170 data->what.track_info.track_number =
2171 bcd2bin(data->what.track_info.track_number);
2172 len = min(len, ((data->header.data_len[0] << 8) +
2173 data->header.data_len[1] +
2174 sizeof(struct cd_sub_channel_header)));
2175 if (copyout(data, args->data, len) != 0) {
2182 case CDIOREADTOCHEADER:
2184 struct ioc_toc_header *th;
2186 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2187 ("trying to do CDIOREADTOCHEADER\n"));
2189 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
2191 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2192 sizeof (*th), /*sense_flags*/0);
2197 if (softc->quirks & CD_Q_BCD_TRACKS) {
2198 /* we are going to have to convert the BCD
2199 * encoding on the cd to what is expected
2201 th->starting_track =
2202 bcd2bin(th->starting_track);
2203 th->ending_track = bcd2bin(th->ending_track);
2205 th->len = ntohs(th->len);
2206 bcopy(th, addr, sizeof(*th));
2210 case CDIOREADTOCENTRYS:
2212 struct cd_tocdata *data;
2213 struct cd_toc_single *lead;
2214 struct ioc_read_toc_entry *te =
2215 (struct ioc_read_toc_entry *) addr;
2216 struct ioc_toc_header *th;
2217 u_int32_t len, readlen, idx, num;
2218 u_int32_t starting_track = te->starting_track;
2220 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2221 ("trying to do CDIOREADTOCENTRYS\n"));
2223 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2224 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK);
2226 if (te->data_len < sizeof(struct cd_toc_entry)
2227 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2228 || (te->address_format != CD_MSF_FORMAT
2229 && te->address_format != CD_LBA_FORMAT)) {
2231 printf("scsi_cd: error in readtocentries, "
2232 "returning EINVAL\n");
2239 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2240 sizeof (*th), /*sense_flags*/0);
2247 if (softc->quirks & CD_Q_BCD_TRACKS) {
2248 /* we are going to have to convert the BCD
2249 * encoding on the cd to what is expected
2251 th->starting_track =
2252 bcd2bin(th->starting_track);
2253 th->ending_track = bcd2bin(th->ending_track);
2256 if (starting_track == 0)
2257 starting_track = th->starting_track;
2258 else if (starting_track == LEADOUT)
2259 starting_track = th->ending_track + 1;
2260 else if (starting_track < th->starting_track ||
2261 starting_track > th->ending_track + 1) {
2262 printf("scsi_cd: error in readtocentries, "
2263 "returning EINVAL\n");
2270 /* calculate reading length without leadout entry */
2271 readlen = (th->ending_track - starting_track + 1) *
2272 sizeof(struct cd_toc_entry);
2274 /* and with leadout entry */
2275 len = readlen + sizeof(struct cd_toc_entry);
2276 if (te->data_len < len) {
2281 if (len > sizeof(data->entries)) {
2282 printf("scsi_cd: error in readtocentries, "
2283 "returning EINVAL\n");
2289 num = len / sizeof(struct cd_toc_entry);
2292 error = cdreadtoc(periph, te->address_format,
2295 readlen + sizeof (*th),
2304 /* make leadout entry if needed */
2305 idx = starting_track + num - 1;
2306 if (softc->quirks & CD_Q_BCD_TRACKS)
2307 th->ending_track = bcd2bin(th->ending_track);
2308 if (idx == th->ending_track + 1) {
2309 error = cdreadtoc(periph, te->address_format,
2310 LEADOUT, (u_int8_t *)lead,
2318 data->entries[idx - starting_track] =
2321 if (softc->quirks & CD_Q_BCD_TRACKS) {
2322 for (idx = 0; idx < num - 1; idx++) {
2323 data->entries[idx].track =
2324 bcd2bin(data->entries[idx].track);
2328 error = copyout(data->entries, te->data, len);
2333 case CDIOREADTOCENTRY:
2335 struct cd_toc_single *data;
2336 struct ioc_read_toc_single_entry *te =
2337 (struct ioc_read_toc_single_entry *) addr;
2338 struct ioc_toc_header *th;
2341 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2342 ("trying to do CDIOREADTOCENTRY\n"));
2344 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2346 if (te->address_format != CD_MSF_FORMAT
2347 && te->address_format != CD_LBA_FORMAT) {
2348 printf("error in readtocentry, "
2349 " returning EINVAL\n");
2356 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2357 sizeof (*th), /*sense_flags*/0);
2363 if (softc->quirks & CD_Q_BCD_TRACKS) {
2364 /* we are going to have to convert the BCD
2365 * encoding on the cd to what is expected
2367 th->starting_track =
2368 bcd2bin(th->starting_track);
2369 th->ending_track = bcd2bin(th->ending_track);
2373 track = th->starting_track;
2374 else if (track == LEADOUT)
2376 else if (track < th->starting_track ||
2377 track > th->ending_track + 1) {
2378 printf("error in readtocentry, "
2379 " returning EINVAL\n");
2385 error = cdreadtoc(periph, te->address_format, track,
2386 (u_int8_t *)data, sizeof(*data),
2393 if (softc->quirks & CD_Q_BCD_TRACKS)
2394 data->entry.track = bcd2bin(data->entry.track);
2395 bcopy(&data->entry, &te->entry,
2396 sizeof(struct cd_toc_entry));
2402 struct ioc_patch *arg = (struct ioc_patch *)addr;
2403 struct cd_mode_params params;
2404 union cd_pages *page;
2406 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2407 ("trying to do CDIOCSETPATCH\n"));
2409 params.alloc_len = sizeof(union cd_mode_data_6_10);
2410 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2412 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2414 free(params.mode_buf, M_TEMP);
2417 page = cdgetpage(¶ms);
2419 page->audio.port[LEFT_PORT].channels =
2421 page->audio.port[RIGHT_PORT].channels =
2423 page->audio.port[2].channels = arg->patch[2];
2424 page->audio.port[3].channels = arg->patch[3];
2425 error = cdsetmode(periph, ¶ms);
2426 free(params.mode_buf, M_TEMP);
2431 struct ioc_vol *arg = (struct ioc_vol *) addr;
2432 struct cd_mode_params params;
2433 union cd_pages *page;
2435 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2436 ("trying to do CDIOCGETVOL\n"));
2438 params.alloc_len = sizeof(union cd_mode_data_6_10);
2439 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2441 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2443 free(params.mode_buf, M_TEMP);
2446 page = cdgetpage(¶ms);
2448 arg->vol[LEFT_PORT] =
2449 page->audio.port[LEFT_PORT].volume;
2450 arg->vol[RIGHT_PORT] =
2451 page->audio.port[RIGHT_PORT].volume;
2452 arg->vol[2] = page->audio.port[2].volume;
2453 arg->vol[3] = page->audio.port[3].volume;
2454 free(params.mode_buf, M_TEMP);
2459 struct ioc_vol *arg = (struct ioc_vol *) addr;
2460 struct cd_mode_params params;
2461 union cd_pages *page;
2463 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2464 ("trying to do CDIOCSETVOL\n"));
2466 params.alloc_len = sizeof(union cd_mode_data_6_10);
2467 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2469 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2471 free(params.mode_buf, M_TEMP);
2474 page = cdgetpage(¶ms);
2476 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2477 page->audio.port[LEFT_PORT].volume =
2478 arg->vol[LEFT_PORT];
2479 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2480 page->audio.port[RIGHT_PORT].volume =
2481 arg->vol[RIGHT_PORT];
2482 page->audio.port[2].volume = arg->vol[2];
2483 page->audio.port[3].volume = arg->vol[3];
2484 error = cdsetmode(periph, ¶ms);
2485 free(params.mode_buf, M_TEMP);
2490 struct cd_mode_params params;
2491 union cd_pages *page;
2493 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2494 ("trying to do CDIOCSETMONO\n"));
2496 params.alloc_len = sizeof(union cd_mode_data_6_10);
2497 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2499 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2501 free(params.mode_buf, M_TEMP);
2504 page = cdgetpage(¶ms);
2506 page->audio.port[LEFT_PORT].channels =
2507 LEFT_CHANNEL | RIGHT_CHANNEL;
2508 page->audio.port[RIGHT_PORT].channels =
2509 LEFT_CHANNEL | RIGHT_CHANNEL;
2510 page->audio.port[2].channels = 0;
2511 page->audio.port[3].channels = 0;
2512 error = cdsetmode(periph, ¶ms);
2513 free(params.mode_buf, M_TEMP);
2516 case CDIOCSETSTEREO:
2518 struct cd_mode_params params;
2519 union cd_pages *page;
2521 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2522 ("trying to do CDIOCSETSTEREO\n"));
2524 params.alloc_len = sizeof(union cd_mode_data_6_10);
2525 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2527 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2529 free(params.mode_buf, M_TEMP);
2532 page = cdgetpage(¶ms);
2534 page->audio.port[LEFT_PORT].channels =
2536 page->audio.port[RIGHT_PORT].channels =
2538 page->audio.port[2].channels = 0;
2539 page->audio.port[3].channels = 0;
2540 error = cdsetmode(periph, ¶ms);
2541 free(params.mode_buf, M_TEMP);
2546 struct cd_mode_params params;
2547 union cd_pages *page;
2549 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2550 ("trying to do CDIOCSETMUTE\n"));
2552 params.alloc_len = sizeof(union cd_mode_data_6_10);
2553 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2555 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2557 free(¶ms, M_TEMP);
2560 page = cdgetpage(¶ms);
2562 page->audio.port[LEFT_PORT].channels = 0;
2563 page->audio.port[RIGHT_PORT].channels = 0;
2564 page->audio.port[2].channels = 0;
2565 page->audio.port[3].channels = 0;
2566 error = cdsetmode(periph, ¶ms);
2567 free(params.mode_buf, M_TEMP);
2572 struct cd_mode_params params;
2573 union cd_pages *page;
2575 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2576 ("trying to do CDIOCSETLEFT\n"));
2578 params.alloc_len = sizeof(union cd_mode_data_6_10);
2579 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2582 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2584 free(params.mode_buf, M_TEMP);
2587 page = cdgetpage(¶ms);
2589 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2590 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2591 page->audio.port[2].channels = 0;
2592 page->audio.port[3].channels = 0;
2593 error = cdsetmode(periph, ¶ms);
2594 free(params.mode_buf, M_TEMP);
2599 struct cd_mode_params params;
2600 union cd_pages *page;
2602 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2603 ("trying to do CDIOCSETRIGHT\n"));
2605 params.alloc_len = sizeof(union cd_mode_data_6_10);
2606 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2609 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2611 free(params.mode_buf, M_TEMP);
2614 page = cdgetpage(¶ms);
2616 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2617 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2618 page->audio.port[2].channels = 0;
2619 page->audio.port[3].channels = 0;
2620 error = cdsetmode(periph, ¶ms);
2621 free(params.mode_buf, M_TEMP);
2625 error = cdpause(periph, 1);
2628 error = cdpause(periph, 0);
2631 error = cdstartunit(periph, 0);
2634 error = cdstartunit(periph, 1);
2641 * The user successfully closed the tray, run
2642 * cdcheckmedia() again so we can re-sync the disklabel
2645 cdcheckmedia(periph);
2649 error = cdstopunit(periph, 0);
2652 error = cdstopunit(periph, 1);
2659 * Since we've successfully ejected the media, run
2660 * cdcheckmedia() again so we re-sync the disklabel
2663 cdcheckmedia(periph);
2667 cdprevent(periph, PR_ALLOW);
2670 cdprevent(periph, PR_PREVENT);
2673 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2677 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2681 /* return (cd_reset(periph)); */
2684 case CDRIOCREADSPEED:
2685 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2687 case CDRIOCWRITESPEED:
2688 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2691 case DVDIOCREPORTKEY: {
2692 struct dvd_authinfo *authinfo;
2694 authinfo = (struct dvd_authinfo *)addr;
2696 if (cmd == DVDIOCREPORTKEY)
2697 error = cdreportkey(periph, authinfo);
2699 error = cdsendkey(periph, authinfo);
2702 case DVDIOCREADSTRUCTURE: {
2703 struct dvd_struct *dvdstruct;
2705 dvdstruct = (struct dvd_struct *)addr;
2707 error = cdreaddvdstructure(periph, dvdstruct);
2712 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2716 cam_periph_unlock(periph);
2718 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2724 cdprevent(struct cam_periph *periph, int action)
2727 struct cd_softc *softc;
2730 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2732 softc = (struct cd_softc *)periph->softc;
2734 if (((action == PR_ALLOW)
2735 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2736 || ((action == PR_PREVENT)
2737 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2741 ccb = cdgetccb(periph, /* priority */ 1);
2743 scsi_prevent(&ccb->csio,
2749 /* timeout */60000);
2751 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2752 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2754 xpt_release_ccb(ccb);
2757 if (action == PR_ALLOW)
2758 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2760 softc->flags |= CD_FLAG_DISC_LOCKED;
2765 cdcheckmedia(struct cam_periph *periph)
2767 struct cd_softc *softc;
2768 struct ioc_toc_header *toch;
2769 struct cd_toc_single leadout;
2770 struct ccb_getdev cgd;
2771 u_int32_t size, toclen;
2772 int error, num_entries, cdindex;
2773 int first_track_audio;
2774 struct disklabel *label;
2776 softc = (struct cd_softc *)periph->softc;
2778 first_track_audio = -1;
2781 cdprevent(periph, PR_PREVENT);
2784 * Build prototype label for whole disk.
2785 * Should take information about different data tracks from the
2786 * TOC and put it in the partition table.
2788 label = &softc->disk.d_label;
2789 bzero(label, sizeof(*label));
2790 label->d_type = DTYPE_SCSI;
2793 * Grab the inquiry data to get the vendor and product names.
2794 * Put them in the typename and packname for the label.
2796 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2797 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2798 xpt_action((union ccb *)&cgd);
2800 strncpy(label->d_typename, cgd.inq_data.vendor,
2801 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2802 strncpy(label->d_packname, cgd.inq_data.product,
2803 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2805 label->d_flags = D_REMOVABLE;
2807 * Make partition 'a' cover the whole disk. This is a temporary
2808 * compatibility hack. The 'a' partition should not exist, so
2809 * the slice code won't create it. The slice code will make
2810 * partition (RAW_PART + 'a') cover the whole disk and fill in
2811 * some more defaults.
2813 label->d_partitions[0].p_size = label->d_secperunit;
2814 label->d_partitions[0].p_fstype = FS_OTHER;
2817 * Default to not reading the disklabel off the disk, until we can
2818 * verify that we have media, that we have a table of contents, and
2819 * that the first track is a data track (which could theoretically
2820 * contain a disklabel).
2822 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
2823 softc->disk.d_dsflags |= DSO_NOLABELS;
2826 * Clear the valid media and TOC flags until we've verified that we
2829 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2832 * Get the disc size and block size. If we can't get it, we don't
2833 * have media, most likely.
2835 if ((error = cdsize(periph, &size)) != 0) {
2837 * Set a bogus sector size, so the slice code won't try to
2838 * divide by 0 and panic the kernel.
2840 label->d_secsize = 2048;
2842 label->d_secperunit = 0;
2845 * XXX KDM is this a good idea? Seems to cause more
2848 if (softc->flags & CD_FLAG_OPEN) {
2854 * We don't bother checking the return value here,
2855 * since we already have an error...
2857 dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2858 /*data*/(caddr_t)&force, /*flags*/ 0,
2859 &softc->disk.d_slice);
2863 * Tell devstat(9) we don't have a blocksize.
2865 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2867 cdprevent(periph, PR_ALLOW);
2872 label->d_secsize = softc->params.blksize;
2873 label->d_secperunit = softc->params.disksize;
2876 * Force a re-sync of slice information, like the blocksize,
2877 * now that we know it. It isn't pretty...but according to
2878 * Bruce Evans, this is probably the best way to do it in
2879 * -stable. We only do this if we're already open, and
2880 * therefore dsopen() has already run. If CD_FLAG_OPEN
2881 * isn't set, this isn't necessary.
2883 if (softc->flags & CD_FLAG_OPEN) {
2888 error = dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2889 /*data*/(caddr_t)&force, /*flags*/ 0,
2890 &softc->disk.d_slice);
2893 * Set a bogus sector size, so the slice code
2894 * won't try to divide by 0 and panic the
2897 label->d_secsize = 2048;
2899 label->d_secperunit = 0;
2902 * Tell devstat(9) we don't have a blocksize.
2904 softc->device_stats.flags |=
2905 DEVSTAT_BS_UNAVAILABLE;
2907 cdprevent(periph, PR_ALLOW);
2912 * We unconditionally (re)set the blocksize each time the
2913 * CD device is opened. This is because the CD can change,
2914 * and therefore the blocksize might change.
2915 * XXX problems here if some slice or partition is still
2916 * open with the old size?
2918 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2919 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2920 softc->device_stats.block_size = softc->params.blksize;
2922 softc->flags |= CD_FLAG_VALID_MEDIA;
2926 * Now we check the table of contents. This (currently) is only
2927 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2928 * things like present a separate entry in /dev for each track,
2929 * like that acd(4) driver does.
2931 bzero(&softc->toc, sizeof(softc->toc));
2932 toch = &softc->toc.header;
2935 * We will get errors here for media that doesn't have a table of
2936 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2937 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2938 * has not been recorded (no complete session) and the Format codes
2939 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2940 * with an INVALID FIELD IN CDB. Devices that are not capable of
2941 * reading an incomplete session on DDC/CD-R/RW media shall report
2942 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2944 * So this isn't fatal if we can't read the table of contents, it
2945 * just means that the user won't be able to issue the play tracks
2946 * ioctl, and likely lots of other stuff won't work either. They
2947 * need to burn the CD before we can do a whole lot with it. So
2948 * we don't print anything here if we get an error back.
2950 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2953 * Errors in reading the table of contents aren't fatal, we just
2954 * won't have a valid table of contents cached.
2958 bzero(&softc->toc, sizeof(softc->toc));
2962 if (softc->quirks & CD_Q_BCD_TRACKS) {
2963 toch->starting_track = bcd2bin(toch->starting_track);
2964 toch->ending_track = bcd2bin(toch->ending_track);
2967 /* Number of TOC entries, plus leadout */
2968 num_entries = (toch->ending_track - toch->starting_track) + 2;
2970 if (num_entries <= 0)
2973 toclen = num_entries * sizeof(struct cd_toc_entry);
2975 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2976 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2980 bzero(&softc->toc, sizeof(softc->toc));
2984 if (softc->quirks & CD_Q_BCD_TRACKS) {
2985 toch->starting_track = bcd2bin(toch->starting_track);
2986 toch->ending_track = bcd2bin(toch->ending_track);
2988 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2991 * XXX KDM is this necessary? Probably only if the drive doesn't
2992 * return leadout information with the table of contents.
2994 cdindex = toch->starting_track + num_entries -1;
2995 if (cdindex == toch->ending_track + 1) {
2997 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2998 (u_int8_t *)&leadout, sizeof(leadout),
3004 softc->toc.entries[cdindex - toch->starting_track] =
3007 if (softc->quirks & CD_Q_BCD_TRACKS) {
3008 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3009 softc->toc.entries[cdindex].track =
3010 bcd2bin(softc->toc.entries[cdindex].track);
3015 * Run through the TOC entries, find the first entry and determine
3016 * whether it is an audio or data track.
3018 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3019 if (softc->toc.entries[cdindex].track == toch->starting_track) {
3020 if (softc->toc.entries[cdindex].control & 0x04)
3021 first_track_audio = 0;
3023 first_track_audio = 1;
3029 * If first_track_audio is non-zero, we either have an error (e.g.
3030 * couldn't find the starting track) or the first track is an audio
3031 * track. If first_track_audio is 0, the first track is a data
3032 * track that could have a disklabel. Attempt to read the
3033 * disklabel off the media, just in case the user put one there.
3035 if (first_track_audio == 0) {
3036 softc->disk.d_dsflags |= DSO_COMPATLABEL;
3037 softc->disk.d_dsflags &= ~DSO_NOLABELS;
3039 softc->flags |= CD_FLAG_VALID_TOC;
3046 cdsize(struct cam_periph *periph, u_int32_t *size)
3048 struct cd_softc *softc;
3050 struct scsi_read_capacity_data *rcap_buf;
3053 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3055 softc = (struct cd_softc *)periph->softc;
3057 ccb = cdgetccb(periph, /* priority */ 1);
3059 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3060 M_TEMP, M_INTWAIT | M_ZERO);
3062 scsi_read_capacity(&ccb->csio,
3068 /* timeout */20000);
3070 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3071 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
3073 xpt_release_ccb(ccb);
3075 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3076 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3078 * SCSI-3 mandates that the reported blocksize shall be 2048.
3079 * Older drives sometimes report funny values, trim it down to
3080 * 2048, or other parts of the kernel will get confused.
3082 * XXX we leave drives alone that might report 512 bytes, as
3083 * well as drives reporting more weird sizes like perhaps 4K.
3085 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3086 softc->params.blksize = 2048;
3088 free(rcap_buf, M_TEMP);
3089 *size = softc->params.disksize;
3096 cd6byteworkaround(union ccb *ccb)
3099 struct cam_periph *periph;
3100 struct cd_softc *softc;
3101 struct cd_mode_params *params;
3104 periph = xpt_path_periph(ccb->ccb_h.path);
3105 softc = (struct cd_softc *)periph->softc;
3107 cdb = ccb->csio.cdb_io.cdb_bytes;
3109 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3110 || ((cdb[0] != MODE_SENSE_6)
3111 && (cdb[0] != MODE_SELECT_6)))
3115 * Because there is no convenient place to stash the overall
3116 * cd_mode_params structure pointer, we have to grab it like this.
3117 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3118 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3120 * XXX It would be nice if, at some point, we could increase the
3121 * number of available peripheral private pointers. Both pointers
3122 * are currently used in most every peripheral driver.
3126 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3127 if (params->mode_buf == ccb->csio.data_ptr) {
3134 * This shouldn't happen. All mode sense and mode select
3135 * operations in the cd(4) driver MUST go through cdgetmode() and
3139 xpt_print_path(periph->path);
3140 printf("mode buffer not found in mode queue!\n");
3144 params->cdb_size = 10;
3145 softc->minimum_command_size = 10;
3146 xpt_print_path(ccb->ccb_h.path);
3147 printf("%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3148 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3150 if (cdb[0] == MODE_SENSE_6) {
3151 struct scsi_mode_sense_10 ms10;
3152 struct scsi_mode_sense_6 *ms6;
3155 ms6 = (struct scsi_mode_sense_6 *)cdb;
3157 bzero(&ms10, sizeof(ms10));
3158 ms10.opcode = MODE_SENSE_10;
3159 ms10.byte2 = ms6->byte2;
3160 ms10.page = ms6->page;
3163 * 10 byte mode header, block descriptor,
3164 * sizeof(union cd_pages)
3166 len = sizeof(struct cd_mode_data_10);
3167 ccb->csio.dxfer_len = len;
3169 scsi_ulto2b(len, ms10.length);
3170 ms10.control = ms6->control;
3171 bcopy(&ms10, cdb, 10);
3172 ccb->csio.cdb_len = 10;
3174 struct scsi_mode_select_10 ms10;
3175 struct scsi_mode_select_6 *ms6;
3176 struct scsi_mode_header_6 *header6;
3177 struct scsi_mode_header_10 *header10;
3178 struct scsi_mode_page_header *page_header;
3179 int blk_desc_len, page_num, page_size, len;
3181 ms6 = (struct scsi_mode_select_6 *)cdb;
3183 bzero(&ms10, sizeof(ms10));
3184 ms10.opcode = MODE_SELECT_10;
3185 ms10.byte2 = ms6->byte2;
3187 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3188 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3190 page_header = find_mode_page_6(header6);
3191 page_num = page_header->page_code;
3193 blk_desc_len = header6->blk_desc_len;
3195 page_size = cdgetpagesize(page_num);
3197 if (page_size != (page_header->page_length +
3198 sizeof(*page_header)))
3199 page_size = page_header->page_length +
3200 sizeof(*page_header);
3202 len = sizeof(*header10) + blk_desc_len + page_size;
3204 len = min(params->alloc_len, len);
3207 * Since the 6 byte parameter header is shorter than the 10
3208 * byte parameter header, we need to copy the actual mode
3209 * page data, and the block descriptor, if any, so things wind
3210 * up in the right place. The regions will overlap, but
3211 * bcopy() does the right thing.
3213 bcopy(params->mode_buf + sizeof(*header6),
3214 params->mode_buf + sizeof(*header10),
3215 len - sizeof(*header10));
3217 /* Make sure these fields are set correctly. */
3218 scsi_ulto2b(0, header10->data_length);
3219 header10->medium_type = 0;
3220 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3222 ccb->csio.dxfer_len = len;
3224 scsi_ulto2b(len, ms10.length);
3225 ms10.control = ms6->control;
3226 bcopy(&ms10, cdb, 10);
3227 ccb->csio.cdb_len = 10;
3230 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3231 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3234 cam_release_devq(ccb->ccb_h.path,
3238 /*getcount_only*/0);
3245 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3247 struct cd_softc *softc;
3248 struct cam_periph *periph;
3251 periph = xpt_path_periph(ccb->ccb_h.path);
3252 softc = (struct cd_softc *)periph->softc;
3257 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3258 * CDB comes back with this particular error, try transforming it
3259 * into the 10 byte version.
3261 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3262 error = cd6byteworkaround(ccb);
3263 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3264 CAM_SCSI_STATUS_ERROR)
3265 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3266 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3267 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3268 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3269 int sense_key, error_code, asc, ascq;
3271 scsi_extract_sense(&ccb->csio.sense_data,
3272 &error_code, &sense_key, &asc, &ascq);
3273 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3274 error = cd6byteworkaround(ccb);
3277 if (error == ERESTART)
3282 * Until we have a better way of doing pack validation,
3283 * don't treat UAs as errors.
3285 sense_flags |= SF_RETRY_UA;
3286 return (cam_periph_error(ccb, cam_flags, sense_flags,
3287 &softc->saved_ccb));
3291 * Read table of contents
3294 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3295 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3297 struct scsi_read_toc *scsi_cmd;
3299 struct ccb_scsiio *csio;
3306 ccb = cdgetccb(periph, /* priority */ 1);
3312 /* cbfcnp */ cddone,
3313 /* flags */ CAM_DIR_IN,
3314 /* tag_action */ MSG_SIMPLE_Q_TAG,
3315 /* data_ptr */ data,
3316 /* dxfer_len */ len,
3317 /* sense_len */ SSD_FULL_SIZE,
3318 sizeof(struct scsi_read_toc),
3319 /* timeout */ 50000);
3321 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3322 bzero (scsi_cmd, sizeof(*scsi_cmd));
3324 if (mode == CD_MSF_FORMAT)
3325 scsi_cmd->byte2 |= CD_MSF;
3326 scsi_cmd->from_track = start;
3327 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3328 scsi_cmd->data_len[0] = (ntoc) >> 8;
3329 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3331 scsi_cmd->op_code = READ_TOC;
3333 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3334 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO |
3337 xpt_release_ccb(ccb);
3343 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3344 u_int32_t format, int track,
3345 struct cd_sub_channel_info *data, u_int32_t len)
3347 struct scsi_read_subchannel *scsi_cmd;
3348 struct ccb_scsiio *csio;
3354 ccb = cdgetccb(periph, /* priority */ 1);
3360 /* cbfcnp */ cddone,
3361 /* flags */ CAM_DIR_IN,
3362 /* tag_action */ MSG_SIMPLE_Q_TAG,
3363 /* data_ptr */ (u_int8_t *)data,
3364 /* dxfer_len */ len,
3365 /* sense_len */ SSD_FULL_SIZE,
3366 sizeof(struct scsi_read_subchannel),
3367 /* timeout */ 50000);
3369 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3370 bzero (scsi_cmd, sizeof(*scsi_cmd));
3372 scsi_cmd->op_code = READ_SUBCHANNEL;
3373 if (mode == CD_MSF_FORMAT)
3374 scsi_cmd->byte1 |= CD_MSF;
3375 scsi_cmd->byte2 = SRS_SUBQ;
3376 scsi_cmd->subchan_format = format;
3377 scsi_cmd->track = track;
3378 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3379 scsi_cmd->control = 0;
3381 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3382 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3384 xpt_release_ccb(ccb);
3390 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3391 * routine. See comments in cd6byteworkaround() for details.
3394 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3397 struct ccb_scsiio *csio;
3398 struct cd_softc *softc;
3403 softc = (struct cd_softc *)periph->softc;
3405 ccb = cdgetccb(periph, /* priority */ 1);
3409 data->cdb_size = softc->minimum_command_size;
3410 if (data->cdb_size < 10)
3411 param_len = sizeof(struct cd_mode_data);
3413 param_len = sizeof(struct cd_mode_data_10);
3415 /* Don't say we've got more room than we actually allocated */
3416 param_len = min(param_len, data->alloc_len);
3418 scsi_mode_sense_len(csio,
3420 /* cbfcnp */ cddone,
3421 /* tag_action */ MSG_SIMPLE_Q_TAG,
3423 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3425 /* param_buf */ data->mode_buf,
3426 /* param_len */ param_len,
3427 /* minimum_cmd_size */ softc->minimum_command_size,
3428 /* sense_len */ SSD_FULL_SIZE,
3429 /* timeout */ 50000);
3432 * It would be nice not to have to do this, but there's no
3433 * available pointer in the CCB that would allow us to stuff the
3434 * mode params structure in there and retrieve it in
3435 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3436 * lets the caller know what CDB size we ended up using, so they
3437 * can find the actual mode page offset.
3439 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3441 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3442 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3444 xpt_release_ccb(ccb);
3446 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3449 * This is a bit of belt-and-suspenders checking, but if we run
3450 * into a situation where the target sends back multiple block
3451 * descriptors, we might not have enough space in the buffer to
3452 * see the whole mode page. Better to return an error than
3453 * potentially access memory beyond our malloced region.
3458 if (data->cdb_size == 10) {
3459 struct scsi_mode_header_10 *hdr10;
3461 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3462 data_len = scsi_2btoul(hdr10->data_length);
3463 data_len += sizeof(hdr10->data_length);
3465 struct scsi_mode_header_6 *hdr6;
3467 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3468 data_len = hdr6->data_length;
3469 data_len += sizeof(hdr6->data_length);
3473 * Complain if there is more mode data available than we
3474 * allocated space for. This could potentially happen if
3475 * we miscalculated the page length for some reason, if the
3476 * drive returns multiple block descriptors, or if it sets
3477 * the data length incorrectly.
3479 if (data_len > data->alloc_len) {
3480 xpt_print_path(periph->path);
3481 printf("allocated modepage %d length %d < returned "
3482 "length %d\n", page, data->alloc_len, data_len);
3491 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3492 * routine. See comments in cd6byteworkaround() for details.
3495 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3497 struct ccb_scsiio *csio;
3498 struct cd_softc *softc;
3500 int cdb_size, param_len;
3503 softc = (struct cd_softc *)periph->softc;
3505 ccb = cdgetccb(periph, /* priority */ 1);
3512 * If the data is formatted for the 10 byte version of the mode
3513 * select parameter list, we need to use the 10 byte CDB.
3514 * Otherwise, we use whatever the stored minimum command size.
3516 if (data->cdb_size == 10)
3517 cdb_size = data->cdb_size;
3519 cdb_size = softc->minimum_command_size;
3521 if (cdb_size >= 10) {
3522 struct scsi_mode_header_10 *mode_header;
3525 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3527 data_len = scsi_2btoul(mode_header->data_length);
3529 scsi_ulto2b(0, mode_header->data_length);
3531 * SONY drives do not allow a mode select with a medium_type
3532 * value that has just been returned by a mode sense; use a
3533 * medium_type of 0 (Default) instead.
3535 mode_header->medium_type = 0;
3538 * Pass back whatever the drive passed to us, plus the size
3539 * of the data length field.
3541 param_len = data_len + sizeof(mode_header->data_length);
3544 struct scsi_mode_header_6 *mode_header;
3546 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3548 param_len = mode_header->data_length + 1;
3550 mode_header->data_length = 0;
3552 * SONY drives do not allow a mode select with a medium_type
3553 * value that has just been returned by a mode sense; use a
3554 * medium_type of 0 (Default) instead.
3556 mode_header->medium_type = 0;
3559 /* Don't say we've got more room than we actually allocated */
3560 param_len = min(param_len, data->alloc_len);
3562 scsi_mode_select_len(csio,
3564 /* cbfcnp */ cddone,
3565 /* tag_action */ MSG_SIMPLE_Q_TAG,
3566 /* scsi_page_fmt */ 1,
3568 /* param_buf */ data->mode_buf,
3569 /* param_len */ param_len,
3570 /* minimum_cmd_size */ cdb_size,
3571 /* sense_len */ SSD_FULL_SIZE,
3572 /* timeout */ 50000);
3574 /* See comments in cdgetmode() and cd6byteworkaround(). */
3575 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3577 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3578 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3580 xpt_release_ccb(ccb);
3582 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3590 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3592 struct ccb_scsiio *csio;
3598 ccb = cdgetccb(periph, /* priority */ 1);
3601 * Use the smallest possible command to perform the operation.
3603 if ((len & 0xffff0000) == 0) {
3605 * We can fit in a 10 byte cdb.
3607 struct scsi_play_10 *scsi_cmd;
3609 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3610 bzero (scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->op_code = PLAY_10;
3612 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3613 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3614 cdb_len = sizeof(*scsi_cmd);
3616 struct scsi_play_12 *scsi_cmd;
3618 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3619 bzero (scsi_cmd, sizeof(*scsi_cmd));
3620 scsi_cmd->op_code = PLAY_12;
3621 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3622 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3623 cdb_len = sizeof(*scsi_cmd);
3628 /*flags*/CAM_DIR_NONE,
3632 /*sense_len*/SSD_FULL_SIZE,
3634 /*timeout*/50 * 1000);
3636 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3637 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3639 xpt_release_ccb(ccb);
3645 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3646 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3648 struct scsi_play_msf *scsi_cmd;
3649 struct ccb_scsiio *csio;
3655 ccb = cdgetccb(periph, /* priority */ 1);
3661 /* cbfcnp */ cddone,
3662 /* flags */ CAM_DIR_NONE,
3663 /* tag_action */ MSG_SIMPLE_Q_TAG,
3664 /* data_ptr */ NULL,
3666 /* sense_len */ SSD_FULL_SIZE,
3667 sizeof(struct scsi_play_msf),
3668 /* timeout */ 50000);
3670 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3671 bzero (scsi_cmd, sizeof(*scsi_cmd));
3673 scsi_cmd->op_code = PLAY_MSF;
3674 scsi_cmd->start_m = startm;
3675 scsi_cmd->start_s = starts;
3676 scsi_cmd->start_f = startf;
3677 scsi_cmd->end_m = endm;
3678 scsi_cmd->end_s = ends;
3679 scsi_cmd->end_f = endf;
3681 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3682 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3684 xpt_release_ccb(ccb);
3691 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3692 u_int32_t etrack, u_int32_t eindex)
3694 struct scsi_play_track *scsi_cmd;
3695 struct ccb_scsiio *csio;
3701 ccb = cdgetccb(periph, /* priority */ 1);
3707 /* cbfcnp */ cddone,
3708 /* flags */ CAM_DIR_NONE,
3709 /* tag_action */ MSG_SIMPLE_Q_TAG,
3710 /* data_ptr */ NULL,
3712 /* sense_len */ SSD_FULL_SIZE,
3713 sizeof(struct scsi_play_track),
3714 /* timeout */ 50000);
3716 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3717 bzero (scsi_cmd, sizeof(*scsi_cmd));
3719 scsi_cmd->op_code = PLAY_TRACK;
3720 scsi_cmd->start_track = strack;
3721 scsi_cmd->start_index = sindex;
3722 scsi_cmd->end_track = etrack;
3723 scsi_cmd->end_index = eindex;
3725 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3726 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3728 xpt_release_ccb(ccb);
3734 cdpause(struct cam_periph *periph, u_int32_t go)
3736 struct scsi_pause *scsi_cmd;
3737 struct ccb_scsiio *csio;
3743 ccb = cdgetccb(periph, /* priority */ 1);
3749 /* cbfcnp */ cddone,
3750 /* flags */ CAM_DIR_NONE,
3751 /* tag_action */ MSG_SIMPLE_Q_TAG,
3752 /* data_ptr */ NULL,
3754 /* sense_len */ SSD_FULL_SIZE,
3755 sizeof(struct scsi_pause),
3756 /* timeout */ 50000);
3758 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3759 bzero (scsi_cmd, sizeof(*scsi_cmd));
3761 scsi_cmd->op_code = PAUSE;
3762 scsi_cmd->resume = go;
3764 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3765 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3767 xpt_release_ccb(ccb);
3773 cdstartunit(struct cam_periph *periph, int load)
3780 ccb = cdgetccb(periph, /* priority */ 1);
3782 scsi_start_stop(&ccb->csio,
3784 /* cbfcnp */ cddone,
3785 /* tag_action */ MSG_SIMPLE_Q_TAG,
3787 /* load_eject */ load,
3788 /* immediate */ FALSE,
3789 /* sense_len */ SSD_FULL_SIZE,
3790 /* timeout */ 50000);
3792 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3793 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3795 xpt_release_ccb(ccb);
3801 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3808 ccb = cdgetccb(periph, /* priority */ 1);
3810 scsi_start_stop(&ccb->csio,
3812 /* cbfcnp */ cddone,
3813 /* tag_action */ MSG_SIMPLE_Q_TAG,
3815 /* load_eject */ eject,
3816 /* immediate */ FALSE,
3817 /* sense_len */ SSD_FULL_SIZE,
3818 /* timeout */ 50000);
3820 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3821 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3823 xpt_release_ccb(ccb);
3829 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3831 struct scsi_set_speed *scsi_cmd;
3832 struct ccb_scsiio *csio;
3837 ccb = cdgetccb(periph, /* priority */ 1);
3840 /* Preserve old behavior: units in multiples of CDROM speed */
3848 /* cbfcnp */ cddone,
3849 /* flags */ CAM_DIR_NONE,
3850 /* tag_action */ MSG_SIMPLE_Q_TAG,
3851 /* data_ptr */ NULL,
3853 /* sense_len */ SSD_FULL_SIZE,
3854 sizeof(struct scsi_set_speed),
3855 /* timeout */ 50000);
3857 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3858 bzero(scsi_cmd, sizeof(*scsi_cmd));
3860 scsi_cmd->opcode = SET_CD_SPEED;
3861 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3862 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3864 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3865 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3867 xpt_release_ccb(ccb);
3873 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3885 ccb = cdgetccb(periph, /* priority */ 1);
3887 switch (authinfo->format) {
3888 case DVD_REPORT_AGID:
3889 length = sizeof(struct scsi_report_key_data_agid);
3891 case DVD_REPORT_CHALLENGE:
3892 length = sizeof(struct scsi_report_key_data_challenge);
3894 case DVD_REPORT_KEY1:
3895 length = sizeof(struct scsi_report_key_data_key1_key2);
3897 case DVD_REPORT_TITLE_KEY:
3898 length = sizeof(struct scsi_report_key_data_title);
3899 /* The lba field is only set for the title key */
3900 lba = authinfo->lba;
3902 case DVD_REPORT_ASF:
3903 length = sizeof(struct scsi_report_key_data_asf);
3905 case DVD_REPORT_RPC:
3906 length = sizeof(struct scsi_report_key_data_rpc);
3908 case DVD_INVALIDATE_AGID:
3914 break; /* NOTREACHED */
3918 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3924 scsi_report_key(&ccb->csio,
3926 /* cbfcnp */ cddone,
3927 /* tag_action */ MSG_SIMPLE_Q_TAG,
3929 /* agid */ authinfo->agid,
3930 /* key_format */ authinfo->format,
3931 /* data_ptr */ databuf,
3932 /* dxfer_len */ length,
3933 /* sense_len */ SSD_FULL_SIZE,
3934 /* timeout */ 50000);
3936 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3937 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3942 if (ccb->csio.resid != 0) {
3943 xpt_print_path(periph->path);
3944 printf("warning, residual for report key command is %d\n",
3948 switch(authinfo->format) {
3949 case DVD_REPORT_AGID: {
3950 struct scsi_report_key_data_agid *agid_data;
3952 agid_data = (struct scsi_report_key_data_agid *)databuf;
3954 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3958 case DVD_REPORT_CHALLENGE: {
3959 struct scsi_report_key_data_challenge *chal_data;
3961 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3963 bcopy(chal_data->challenge_key, authinfo->keychal,
3964 min(sizeof(chal_data->challenge_key),
3965 sizeof(authinfo->keychal)));
3968 case DVD_REPORT_KEY1: {
3969 struct scsi_report_key_data_key1_key2 *key1_data;
3971 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3973 bcopy(key1_data->key1, authinfo->keychal,
3974 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3977 case DVD_REPORT_TITLE_KEY: {
3978 struct scsi_report_key_data_title *title_data;
3980 title_data = (struct scsi_report_key_data_title *)databuf;
3982 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3983 RKD_TITLE_CPM_SHIFT;
3984 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3985 RKD_TITLE_CP_SEC_SHIFT;
3986 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3987 RKD_TITLE_CMGS_SHIFT;
3988 bcopy(title_data->title_key, authinfo->keychal,
3989 min(sizeof(title_data->title_key),
3990 sizeof(authinfo->keychal)));
3993 case DVD_REPORT_ASF: {
3994 struct scsi_report_key_data_asf *asf_data;
3996 asf_data = (struct scsi_report_key_data_asf *)databuf;
3998 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
4001 case DVD_REPORT_RPC: {
4002 struct scsi_report_key_data_rpc *rpc_data;
4004 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
4006 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
4008 authinfo->vend_rsts =
4009 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
4010 RKD_RPC_VENDOR_RESET_SHIFT;
4011 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4012 authinfo->region = rpc_data->region_mask;
4013 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4016 case DVD_INVALIDATE_AGID:
4019 /* This should be impossible, since we checked above */
4022 break; /* NOTREACHED */
4025 if (databuf != NULL)
4026 free(databuf, M_DEVBUF);
4028 xpt_release_ccb(ccb);
4034 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4044 ccb = cdgetccb(periph, /* priority */ 1);
4046 switch(authinfo->format) {
4047 case DVD_SEND_CHALLENGE: {
4048 struct scsi_report_key_data_challenge *challenge_data;
4050 length = sizeof(*challenge_data);
4052 challenge_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4054 databuf = (u_int8_t *)challenge_data;
4056 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4057 challenge_data->data_len);
4059 bcopy(authinfo->keychal, challenge_data->challenge_key,
4060 min(sizeof(authinfo->keychal),
4061 sizeof(challenge_data->challenge_key)));
4064 case DVD_SEND_KEY2: {
4065 struct scsi_report_key_data_key1_key2 *key2_data;
4067 length = sizeof(*key2_data);
4069 key2_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4071 databuf = (u_int8_t *)key2_data;
4073 scsi_ulto2b(length - sizeof(key2_data->data_len),
4074 key2_data->data_len);
4076 bcopy(authinfo->keychal, key2_data->key1,
4077 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4081 case DVD_SEND_RPC: {
4082 struct scsi_send_key_data_rpc *rpc_data;
4084 length = sizeof(*rpc_data);
4086 rpc_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4088 databuf = (u_int8_t *)rpc_data;
4090 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4091 rpc_data->data_len);
4093 rpc_data->region_code = authinfo->region;
4099 break; /* NOTREACHED */
4102 scsi_send_key(&ccb->csio,
4104 /* cbfcnp */ cddone,
4105 /* tag_action */ MSG_SIMPLE_Q_TAG,
4106 /* agid */ authinfo->agid,
4107 /* key_format */ authinfo->format,
4108 /* data_ptr */ databuf,
4109 /* dxfer_len */ length,
4110 /* sense_len */ SSD_FULL_SIZE,
4111 /* timeout */ 50000);
4113 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4114 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4118 if (databuf != NULL)
4119 free(databuf, M_DEVBUF);
4121 xpt_release_ccb(ccb);
4127 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4137 /* The address is reserved for many of the formats */
4140 ccb = cdgetccb(periph, /* priority */ 1);
4142 switch(dvdstruct->format) {
4143 case DVD_STRUCT_PHYSICAL:
4144 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4146 case DVD_STRUCT_COPYRIGHT:
4147 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4149 case DVD_STRUCT_DISCKEY:
4150 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4152 case DVD_STRUCT_BCA:
4153 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4155 case DVD_STRUCT_MANUFACT:
4156 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4158 case DVD_STRUCT_CMI:
4162 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4163 address = dvdstruct->address;
4165 break; /* NOTREACHED */
4166 case DVD_STRUCT_PROTDISCID:
4167 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4169 case DVD_STRUCT_DISCKEYBLOCK:
4170 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4172 case DVD_STRUCT_DDS:
4173 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4175 case DVD_STRUCT_MEDIUM_STAT:
4176 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4178 case DVD_STRUCT_SPARE_AREA:
4179 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4181 case DVD_STRUCT_RMD_LAST:
4185 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4186 address = dvdstruct->address;
4188 break; /* NOTREACHED */
4189 case DVD_STRUCT_RMD_RMA:
4193 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4194 address = dvdstruct->address;
4196 break; /* NOTREACHED */
4197 case DVD_STRUCT_PRERECORDED:
4198 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4200 case DVD_STRUCT_UNIQUEID:
4201 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4203 case DVD_STRUCT_DCB:
4207 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4208 address = dvdstruct->address;
4210 break; /* NOTREACHED */
4211 case DVD_STRUCT_LIST:
4213 * This is the maximum allocation length for the READ DVD
4214 * STRUCTURE command. There's nothing in the MMC3 spec
4215 * that indicates a limit in the amount of data that can
4216 * be returned from this call, other than the limits
4217 * imposed by the 2-byte length variables.
4224 break; /* NOTREACHED */
4228 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4233 scsi_read_dvd_structure(&ccb->csio,
4235 /* cbfcnp */ cddone,
4236 /* tag_action */ MSG_SIMPLE_Q_TAG,
4238 /* layer_number */ dvdstruct->layer_num,
4239 /* key_format */ dvdstruct->format,
4240 /* agid */ dvdstruct->agid,
4241 /* data_ptr */ databuf,
4242 /* dxfer_len */ length,
4243 /* sense_len */ SSD_FULL_SIZE,
4244 /* timeout */ 50000);
4246 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4247 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4252 switch(dvdstruct->format) {
4253 case DVD_STRUCT_PHYSICAL: {
4254 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4255 struct dvd_layer *outlayer;
4256 struct scsi_read_dvd_struct_data_physical *phys_data;
4259 (struct scsi_read_dvd_struct_data_physical *)databuf;
4260 inlayer = &phys_data->layer_desc;
4261 outlayer = (struct dvd_layer *)&dvdstruct->data;
4263 dvdstruct->length = sizeof(*inlayer);
4265 outlayer->book_type = (inlayer->book_type_version &
4266 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4267 outlayer->book_version = (inlayer->book_type_version &
4268 RDSD_BOOK_VERSION_MASK);
4269 outlayer->disc_size = (inlayer->disc_size_max_rate &
4270 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4271 outlayer->max_rate = (inlayer->disc_size_max_rate &
4272 RDSD_MAX_RATE_MASK);
4273 outlayer->nlayers = (inlayer->layer_info &
4274 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4275 outlayer->track_path = (inlayer->layer_info &
4276 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4277 outlayer->layer_type = (inlayer->layer_info &
4278 RDSD_LAYER_TYPE_MASK);
4279 outlayer->linear_density = (inlayer->density &
4280 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4281 outlayer->track_density = (inlayer->density &
4282 RDSD_TRACK_DENSITY_MASK);
4283 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4285 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4286 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4287 outlayer->end_sector_l0 =
4288 scsi_3btoul(inlayer->end_sector_layer0);
4291 case DVD_STRUCT_COPYRIGHT: {
4292 struct scsi_read_dvd_struct_data_copyright *copy_data;
4294 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4297 dvdstruct->cpst = copy_data->cps_type;
4298 dvdstruct->rmi = copy_data->region_info;
4299 dvdstruct->length = 0;
4305 * Tell the user what the overall length is, no matter
4306 * what we can actually fit in the data buffer.
4308 dvdstruct->length = length - ccb->csio.resid -
4309 sizeof(struct scsi_read_dvd_struct_data_header);
4312 * But only actually copy out the smaller of what we read
4313 * in or what the structure can take.
4315 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4317 min(sizeof(dvdstruct->data), dvdstruct->length));
4322 if (databuf != NULL)
4323 free(databuf, M_DEVBUF);
4325 xpt_release_ccb(ccb);
4331 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4332 void (*cbfcnp)(struct cam_periph *, union ccb *),
4333 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4334 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4335 u_int8_t sense_len, u_int32_t timeout)
4337 struct scsi_report_key *scsi_cmd;
4339 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4340 bzero(scsi_cmd, sizeof(*scsi_cmd));
4341 scsi_cmd->opcode = REPORT_KEY;
4342 scsi_ulto4b(lba, scsi_cmd->lba);
4343 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4344 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4345 (key_format & RK_KF_KEYFORMAT_MASK);
4350 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4352 /*data_ptr*/ data_ptr,
4353 /*dxfer_len*/ dxfer_len,
4360 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4361 void (*cbfcnp)(struct cam_periph *, union ccb *),
4362 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4363 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4366 struct scsi_send_key *scsi_cmd;
4368 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4369 bzero(scsi_cmd, sizeof(*scsi_cmd));
4370 scsi_cmd->opcode = SEND_KEY;
4372 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4373 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4374 (key_format & RK_KF_KEYFORMAT_MASK);
4379 /*flags*/ CAM_DIR_OUT,
4381 /*data_ptr*/ data_ptr,
4382 /*dxfer_len*/ dxfer_len,
4390 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4391 void (*cbfcnp)(struct cam_periph *, union ccb *),
4392 u_int8_t tag_action, u_int32_t address,
4393 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4394 u_int8_t *data_ptr, u_int32_t dxfer_len,
4395 u_int8_t sense_len, u_int32_t timeout)
4397 struct scsi_read_dvd_structure *scsi_cmd;
4399 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4400 bzero(scsi_cmd, sizeof(*scsi_cmd));
4401 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4403 scsi_ulto4b(address, scsi_cmd->address);
4404 scsi_cmd->layer_number = layer_number;
4405 scsi_cmd->format = format;
4406 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4407 /* The AGID is the top two bits of this byte */
4408 scsi_cmd->agid = agid << 6;
4413 /*flags*/ CAM_DIR_IN,
4415 /*data_ptr*/ data_ptr,
4416 /*dxfer_len*/ dxfer_len,