2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 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
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.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>
66 #include <geom/geom_disk.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
73 #include <cam/cam_sim.h>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_NO_CHANGER = 0x04,
92 CD_Q_10_BYTE_ONLY = 0x10
96 CD_FLAG_INVALID = 0x0001,
97 CD_FLAG_NEW_DISC = 0x0002,
98 CD_FLAG_DISC_LOCKED = 0x0004,
99 CD_FLAG_DISC_REMOVABLE = 0x0008,
100 CD_FLAG_CHANGER = 0x0040,
101 CD_FLAG_ACTIVE = 0x0080,
102 CD_FLAG_SCHED_ON_COMP = 0x0100,
103 CD_FLAG_RETRY_UA = 0x0200,
104 CD_FLAG_VALID_MEDIA = 0x0400,
105 CD_FLAG_VALID_TOC = 0x0800,
106 CD_FLAG_SCTX_INIT = 0x1000,
107 CD_FLAG_OPEN = 0x2000
112 CD_CCB_BUFFER_IO = 0x02,
113 CD_CCB_WAITING = 0x03,
114 CD_CCB_TYPE_MASK = 0x0F,
115 CD_CCB_RETRY_UA = 0x10
119 CHANGER_TIMEOUT_SCHED = 0x01,
120 CHANGER_SHORT_TMOUT_SCHED = 0x02,
121 CHANGER_MANUAL_CALL = 0x04,
122 CHANGER_NEED_TIMEOUT = 0x08
125 #define ccb_state ppriv_field0
126 #define ccb_bp ppriv_ptr1
129 struct ioc_toc_header header;
130 struct cd_toc_entry entries[100];
133 struct cd_toc_single {
134 struct ioc_toc_header header;
135 struct cd_toc_entry entry;
146 volatile cd_flags flags;
147 struct bio_queue_head bio_queue;
148 LIST_HEAD(, ccb_hdr) pending_ccbs;
149 struct cd_params params;
152 STAILQ_ENTRY(cd_softc) changer_links;
153 struct cdchanger *changer;
155 struct cam_periph *periph;
156 int minimum_command_size;
157 int outstanding_cmds;
158 struct task sysctl_task;
159 struct sysctl_ctx_list sysctl_ctx;
160 struct sysctl_oid *sysctl_tree;
161 STAILQ_HEAD(, cd_mode_params) mode_queue;
162 struct cd_tocdata toc;
166 struct cd_page_sizes {
171 static struct cd_page_sizes cd_page_size_table[] =
173 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
176 struct cd_quirk_entry {
177 struct scsi_inquiry_pattern inq_pat;
182 * The changer quirk entries aren't strictly necessary. Basically, what
183 * they do is tell cdregister() up front that a device is a changer.
184 * Otherwise, it will figure that fact out once it sees a LUN on the device
185 * that is greater than 0. If it is known up front that a device is a changer,
186 * all I/O to the device will go through the changer scheduling routines, as
187 * opposed to the "normal" CD code.
189 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
190 * your device hangs when it gets a 10 byte command. Adding a quirk just
191 * to get rid of the informative diagnostic message is not acceptable. All
192 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
193 * referenced in a comment along with the quirk) , and must be approved by
194 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
195 * be removed until the submitter can explain why they are needed.
196 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
197 * when the CAM_NEW_TRAN_CODE work is done.
199 static struct cd_quirk_entry cd_quirk_table[] =
202 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
203 /*quirks*/ CD_Q_CHANGER
206 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
207 "*"}, /* quirks */ CD_Q_CHANGER
210 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
211 /* quirks */ CD_Q_CHANGER
214 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
215 /* quirks */ CD_Q_BCD_TRACKS
219 static disk_open_t cdopen;
220 static disk_close_t cdclose;
221 static disk_ioctl_t cdioctl;
222 static disk_strategy_t cdstrategy;
224 static periph_init_t cdinit;
225 static periph_ctor_t cdregister;
226 static periph_dtor_t cdcleanup;
227 static periph_start_t cdstart;
228 static periph_oninv_t cdoninvalidate;
229 static void cdasync(void *callback_arg, u_int32_t code,
230 struct cam_path *path, void *arg);
231 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
232 static void cdshorttimeout(void *arg);
233 static void cdschedule(struct cam_periph *periph, int priority);
234 static void cdrunchangerqueue(void *arg);
235 static void cdchangerschedule(struct cd_softc *softc);
236 static int cdrunccb(union ccb *ccb,
237 int (*error_routine)(union ccb *ccb,
239 u_int32_t sense_flags),
240 u_int32_t cam_flags, u_int32_t sense_flags);
241 static union ccb *cdgetccb(struct cam_periph *periph,
243 static void cddone(struct cam_periph *periph,
244 union ccb *start_ccb);
245 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
246 static int cdgetpagesize(int page_num);
247 static void cdprevent(struct cam_periph *periph, int action);
248 static int cdcheckmedia(struct cam_periph *periph);
249 static int cdsize(struct cam_periph *periph, u_int32_t *size);
250 static int cd6byteworkaround(union ccb *ccb);
251 static int cderror(union ccb *ccb, u_int32_t cam_flags,
252 u_int32_t sense_flags);
253 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
254 u_int32_t start, u_int8_t *data,
255 u_int32_t len, u_int32_t sense_flags);
256 static int cdgetmode(struct cam_periph *periph,
257 struct cd_mode_params *data, u_int32_t page);
258 static int cdsetmode(struct cam_periph *periph,
259 struct cd_mode_params *data);
260 static int cdplay(struct cam_periph *periph, u_int32_t blk,
262 static int cdreadsubchannel(struct cam_periph *periph,
263 u_int32_t mode, u_int32_t format,
265 struct cd_sub_channel_info *data,
267 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
268 u_int32_t starts, u_int32_t startf,
269 u_int32_t endm, u_int32_t ends,
271 static int cdplaytracks(struct cam_periph *periph,
272 u_int32_t strack, u_int32_t sindex,
273 u_int32_t etrack, u_int32_t eindex);
274 static int cdpause(struct cam_periph *periph, u_int32_t go);
275 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
276 static int cdstartunit(struct cam_periph *periph, int load);
277 static int cdsetspeed(struct cam_periph *periph,
278 u_int32_t rdspeed, u_int32_t wrspeed);
279 static int cdreportkey(struct cam_periph *periph,
280 struct dvd_authinfo *authinfo);
281 static int cdsendkey(struct cam_periph *periph,
282 struct dvd_authinfo *authinfo);
283 static int cdreaddvdstructure(struct cam_periph *periph,
284 struct dvd_struct *dvdstruct);
286 static struct periph_driver cddriver =
289 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
292 PERIPHDRIVER_DECLARE(cd, cddriver);
294 #ifndef CD_DEFAULT_RETRY
295 #define CD_DEFAULT_RETRY 4
297 #ifndef CHANGER_MIN_BUSY_SECONDS
298 #define CHANGER_MIN_BUSY_SECONDS 5
300 #ifndef CHANGER_MAX_BUSY_SECONDS
301 #define CHANGER_MAX_BUSY_SECONDS 15
304 static int cd_retry_count = CD_DEFAULT_RETRY;
305 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
306 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
308 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
309 static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
311 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
312 &cd_retry_count, 0, "Normal I/O retry count");
313 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
314 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
315 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
316 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
317 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
318 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
319 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
323 target_id_t target_id;
326 struct timeval start_time;
327 struct cd_softc *cur_device;
328 struct callout short_handle;
329 struct callout long_handle;
330 volatile cd_changer_flags flags;
331 STAILQ_ENTRY(cdchanger) changer_links;
332 STAILQ_HEAD(chdevlist, cd_softc) chluns;
335 static struct mtx changerq_mtx;
336 static STAILQ_HEAD(changerlist, cdchanger) changerq;
337 static int num_changers;
339 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
346 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
347 STAILQ_INIT(&changerq);
350 * Install a global async callback. This callback will
351 * receive async callbacks like "new device found".
353 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
355 if (status != CAM_REQ_CMP) {
356 printf("cd: Failed to attach master async callback "
357 "due to status 0x%x!\n", status);
362 cdoninvalidate(struct cam_periph *periph)
364 struct cd_softc *softc;
366 softc = (struct cd_softc *)periph->softc;
369 * De-register any async callbacks.
371 xpt_register_async(0, cdasync, periph, periph->path);
373 softc->flags |= CD_FLAG_INVALID;
376 * Return all queued I/O with ENXIO.
377 * XXX Handle any transactions queued to the card
378 * with XPT_ABORT_CCB.
380 bioq_flush(&softc->bio_queue, NULL, ENXIO);
383 * If this device is part of a changer, and it was scheduled
384 * to run, remove it from the run queue since we just nuked
385 * all of its scheduled I/O.
387 if ((softc->flags & CD_FLAG_CHANGER)
388 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
389 camq_remove(&softc->changer->devq, softc->pinfo.index);
391 disk_gone(softc->disk);
392 xpt_print(periph->path, "lost device\n");
396 cdcleanup(struct cam_periph *periph)
398 struct cd_softc *softc;
400 softc = (struct cd_softc *)periph->softc;
402 xpt_print(periph->path, "removing device entry\n");
405 * In the queued, non-active case, the device in question
406 * has already been removed from the changer run queue. Since this
407 * device is active, we need to de-activate it, and schedule
408 * another device to run. (if there is another one to run)
410 if ((softc->flags & CD_FLAG_CHANGER)
411 && (softc->flags & CD_FLAG_ACTIVE)) {
414 * The purpose of the short timeout is soley to determine
415 * whether the current device has finished or not. Well,
416 * since we're removing the active device, we know that it
417 * is finished. So, get rid of the short timeout.
418 * Otherwise, if we're in the time period before the short
419 * timeout fires, and there are no other devices in the
420 * queue to run, there won't be any other device put in the
421 * active slot. i.e., when we call cdrunchangerqueue()
422 * below, it won't do anything. Then, when the short
423 * timeout fires, it'll look at the "current device", which
424 * we are free below, and possibly panic the kernel on a
425 * bogus pointer reference.
427 * The long timeout doesn't really matter, since we
428 * decrement the qfrozen_cnt to indicate that there is
429 * nothing in the active slot now. Therefore, there won't
430 * be any bogus pointer references there.
432 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
433 callout_stop(&softc->changer->short_handle);
434 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
436 softc->changer->devq.qfrozen_cnt[0]--;
437 softc->changer->flags |= CHANGER_MANUAL_CALL;
438 cdrunchangerqueue(softc->changer);
442 * If we're removing the last device on the changer, go ahead and
443 * remove the changer device structure.
445 if ((softc->flags & CD_FLAG_CHANGER)
446 && (--softc->changer->num_devices == 0)) {
449 * Theoretically, there shouldn't be any timeouts left, but
450 * I'm not completely sure that that will be the case. So,
451 * it won't hurt to check and see if there are any left.
453 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
454 callout_stop(&softc->changer->long_handle);
455 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
458 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
459 callout_stop(&softc->changer->short_handle);
460 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
463 mtx_lock(&changerq_mtx);
464 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
467 mtx_unlock(&changerq_mtx);
468 xpt_print(periph->path, "removing changer entry\n");
469 free(softc->changer, M_DEVBUF);
471 cam_periph_unlock(periph);
472 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
473 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
474 xpt_print(periph->path, "can't remove sysctl context\n");
477 disk_destroy(softc->disk);
478 free(softc, M_DEVBUF);
479 cam_periph_lock(periph);
483 cdasync(void *callback_arg, u_int32_t code,
484 struct cam_path *path, void *arg)
486 struct cam_periph *periph;
488 periph = (struct cam_periph *)callback_arg;
490 case AC_FOUND_DEVICE:
492 struct ccb_getdev *cgd;
495 cgd = (struct ccb_getdev *)arg;
499 if (cgd->protocol != PROTO_SCSI)
502 if (SID_TYPE(&cgd->inq_data) != T_CDROM
503 && SID_TYPE(&cgd->inq_data) != T_WORM)
507 * Allocate a peripheral instance for
508 * this device and start the probe
511 status = cam_periph_alloc(cdregister, cdoninvalidate,
513 "cd", CAM_PERIPH_BIO,
514 cgd->ccb_h.path, cdasync,
515 AC_FOUND_DEVICE, cgd);
517 if (status != CAM_REQ_CMP
518 && status != CAM_REQ_INPROG)
519 printf("cdasync: Unable to attach new device "
520 "due to status 0x%x\n", status);
527 struct cd_softc *softc;
528 struct ccb_hdr *ccbh;
530 softc = (struct cd_softc *)periph->softc;
532 * Don't fail on the expected unit attention
535 softc->flags |= CD_FLAG_RETRY_UA;
536 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
537 ccbh->ccb_state |= CD_CCB_RETRY_UA;
541 cam_periph_async(periph, code, path, arg);
547 cdsysctlinit(void *context, int pending)
549 struct cam_periph *periph;
550 struct cd_softc *softc;
551 char tmpstr[80], tmpstr2[80];
553 periph = (struct cam_periph *)context;
554 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
557 softc = (struct cd_softc *)periph->softc;
558 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
559 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
561 sysctl_ctx_init(&softc->sysctl_ctx);
562 softc->flags |= CD_FLAG_SCTX_INIT;
563 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
564 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
565 tmpstr2, CTLFLAG_RD, 0, tmpstr);
567 if (softc->sysctl_tree == NULL) {
568 printf("cdsysctlinit: unable to allocate sysctl tree\n");
569 cam_periph_release(periph);
574 * Now register the sysctl handler, so the user can the value on
577 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
578 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
579 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
582 cam_periph_release(periph);
586 * We have a handler function for this so we can check the values when the
587 * user sets them, instead of every time we look at them.
590 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
594 value = *(int *)arg1;
596 error = sysctl_handle_int(oidp, &value, 0, req);
599 || (req->newptr == NULL))
603 * The only real values we can have here are 6 or 10. I don't
604 * really forsee having 12 be an option at any time in the future.
605 * So if the user sets something less than or equal to 6, we'll set
606 * it to 6. If he sets something greater than 6, we'll set it to 10.
608 * I suppose we could just return an error here for the wrong values,
609 * but I don't think it's necessary to do so, as long as we can
610 * determine the user's intent without too much trouble.
617 *(int *)arg1 = value;
623 cdregister(struct cam_periph *periph, void *arg)
625 struct cd_softc *softc;
626 struct ccb_pathinq cpi;
627 struct ccb_getdev *cgd;
631 cgd = (struct ccb_getdev *)arg;
632 if (periph == NULL) {
633 printf("cdregister: periph was NULL!!\n");
634 return(CAM_REQ_CMP_ERR);
637 printf("cdregister: no getdev CCB, can't register device\n");
638 return(CAM_REQ_CMP_ERR);
641 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
644 printf("cdregister: Unable to probe new device. "
645 "Unable to allocate softc\n");
646 return(CAM_REQ_CMP_ERR);
649 LIST_INIT(&softc->pending_ccbs);
650 STAILQ_INIT(&softc->mode_queue);
651 softc->state = CD_STATE_PROBE;
652 bioq_init(&softc->bio_queue);
653 if (SID_IS_REMOVABLE(&cgd->inq_data))
654 softc->flags |= CD_FLAG_DISC_REMOVABLE;
656 periph->softc = softc;
657 softc->periph = periph;
660 * See if this device has any quirks.
662 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
663 (caddr_t)cd_quirk_table,
664 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
665 sizeof(*cd_quirk_table), scsi_inquiry_match);
668 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
670 softc->quirks = CD_Q_NONE;
672 /* Check if the SIM does not want 6 byte commands */
673 bzero(&cpi, sizeof(cpi));
674 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
675 cpi.ccb_h.func_code = XPT_PATH_INQ;
676 xpt_action((union ccb *)&cpi);
677 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
678 softc->quirks |= CD_Q_10_BYTE_ONLY;
680 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
682 /* The default is 6 byte commands, unless quirked otherwise */
683 if (softc->quirks & CD_Q_10_BYTE_ONLY)
684 softc->minimum_command_size = 10;
686 softc->minimum_command_size = 6;
689 * Refcount and block open attempts until we are setup
692 (void)cam_periph_hold(periph, PRIBIO);
693 cam_periph_unlock(periph);
695 * Load the user's default, if any.
697 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
698 periph->unit_number);
699 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
701 /* 6 and 10 are the only permissible values here. */
702 if (softc->minimum_command_size < 6)
703 softc->minimum_command_size = 6;
704 else if (softc->minimum_command_size > 6)
705 softc->minimum_command_size = 10;
708 * We need to register the statistics structure for this device,
709 * but we don't have the blocksize yet for it. So, we register
710 * the structure and indicate that we don't have the blocksize
711 * yet. Unlike other SCSI peripheral drivers, we explicitly set
712 * the device type here to be CDROM, rather than just ORing in
713 * the device type. This is because this driver can attach to either
714 * CDROM or WORM devices, and we want this peripheral driver to
715 * show up in the devstat list as a CD peripheral driver, not a
716 * WORM peripheral driver. WORM drives will also have the WORM
717 * driver attached to them.
719 softc->disk = disk_alloc();
720 softc->disk->d_devstat = devstat_new_entry("cd",
721 periph->unit_number, 0,
722 DEVSTAT_BS_UNAVAILABLE,
724 XPORT_DEVSTAT_TYPE(cpi.transport),
725 DEVSTAT_PRIORITY_CD);
726 softc->disk->d_open = cdopen;
727 softc->disk->d_close = cdclose;
728 softc->disk->d_strategy = cdstrategy;
729 softc->disk->d_ioctl = cdioctl;
730 softc->disk->d_name = "cd";
731 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
732 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
733 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
734 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
735 cgd->inq_data.product, sizeof(cgd->inq_data.product),
736 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
737 softc->disk->d_unit = periph->unit_number;
738 softc->disk->d_drv1 = periph;
740 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
741 else if (cpi.maxio > MAXPHYS)
742 softc->disk->d_maxsize = MAXPHYS; /* for safety */
744 softc->disk->d_maxsize = cpi.maxio;
745 softc->disk->d_flags = 0;
746 softc->disk->d_hba_vendor = cpi.hba_vendor;
747 softc->disk->d_hba_device = cpi.hba_device;
748 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
749 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
750 disk_create(softc->disk, DISK_VERSION);
751 cam_periph_lock(periph);
754 * Add an async callback so that we get
755 * notified if this device goes away.
757 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
758 cdasync, periph, periph->path);
761 * If the target lun is greater than 0, we most likely have a CD
762 * changer device. Check the quirk entries as well, though, just
763 * in case someone has a CD tower with one lun per drive or
764 * something like that. Also, if we know up front that a
765 * particular device is a changer, we can mark it as such starting
766 * with lun 0, instead of lun 1. It shouldn't be necessary to have
767 * a quirk entry to define something as a changer, however.
769 if (((cgd->ccb_h.target_lun > 0)
770 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
771 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
772 struct cdchanger *nchanger;
773 struct cam_periph *nperiph;
774 struct cam_path *path;
778 /* Set the changer flag in the current device's softc */
779 softc->flags |= CD_FLAG_CHANGER;
782 * Now, look around for an existing changer device with the
783 * same path and target ID as the current device.
785 mtx_lock(&changerq_mtx);
787 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
789 nchanger = STAILQ_NEXT(nchanger, changer_links)){
790 if ((nchanger->path_id == cgd->ccb_h.path_id)
791 && (nchanger->target_id == cgd->ccb_h.target_id)) {
796 mtx_unlock(&changerq_mtx);
799 * If we found a matching entry, just add this device to
800 * the list of devices on this changer.
803 struct chdevlist *chlunhead;
805 chlunhead = &nchanger->chluns;
808 * XXX KDM look at consolidating this code with the
809 * code below in a separate function.
813 * Create a path with lun id 0, and see if we can
814 * find a matching device
816 status = xpt_create_path(&path, /*periph*/ periph,
818 cgd->ccb_h.target_id, 0);
820 if ((status == CAM_REQ_CMP)
821 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
822 struct cd_softc *nsoftc;
824 nsoftc = (struct cd_softc *)nperiph->softc;
826 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
827 nsoftc->flags |= CD_FLAG_CHANGER;
828 nchanger->num_devices++;
829 if (camq_resize(&nchanger->devq,
830 nchanger->num_devices)!=CAM_REQ_CMP){
831 printf("cdregister: "
837 nsoftc->changer = nchanger;
838 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
840 STAILQ_INSERT_TAIL(&nchanger->chluns,
841 nsoftc,changer_links);
844 } else if (status == CAM_REQ_CMP)
847 printf("cdregister: unable to allocate path\n"
848 "cdregister: changer support may be "
852 nchanger->num_devices++;
854 softc->changer = nchanger;
855 softc->pinfo.index = CAM_UNQUEUED_INDEX;
857 if (camq_resize(&nchanger->devq,
858 nchanger->num_devices) != CAM_REQ_CMP) {
859 printf("cdregister: camq_resize "
860 "failed, changer support may "
864 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
867 * In this case, we don't already have an entry for this
868 * particular changer, so we need to create one, add it to
869 * the queue, and queue this device on the list for this
870 * changer. Before we queue this device, however, we need
871 * to search for lun id 0 on this target, and add it to the
872 * queue first, if it exists. (and if it hasn't already
873 * been marked as part of the changer.)
876 nchanger = malloc(sizeof(struct cdchanger),
877 M_DEVBUF, M_NOWAIT | M_ZERO);
878 if (nchanger == NULL) {
879 softc->flags &= ~CD_FLAG_CHANGER;
880 printf("cdregister: unable to malloc "
881 "changer structure\ncdregister: "
882 "changer support disabled\n");
885 * Yes, gotos can be gross but in this case
886 * I think it's justified..
890 if (camq_init(&nchanger->devq, 1) != 0) {
891 softc->flags &= ~CD_FLAG_CHANGER;
892 printf("cdregister: changer support "
897 nchanger->path_id = cgd->ccb_h.path_id;
898 nchanger->target_id = cgd->ccb_h.target_id;
900 /* this is superfluous, but it makes things clearer */
901 nchanger->num_devices = 0;
903 STAILQ_INIT(&nchanger->chluns);
905 callout_init_mtx(&nchanger->long_handle,
906 periph->sim->mtx, 0);
907 callout_init_mtx(&nchanger->short_handle,
908 periph->sim->mtx, 0);
910 mtx_lock(&changerq_mtx);
912 STAILQ_INSERT_TAIL(&changerq, nchanger,
914 mtx_unlock(&changerq_mtx);
917 * Create a path with lun id 0, and see if we can
918 * find a matching device
920 status = xpt_create_path(&path, /*periph*/ periph,
922 cgd->ccb_h.target_id, 0);
925 * If we were able to allocate the path, and if we
926 * find a matching device and it isn't already
927 * marked as part of a changer, then we add it to
928 * the current changer.
930 if ((status == CAM_REQ_CMP)
931 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
932 && ((((struct cd_softc *)periph->softc)->flags &
933 CD_FLAG_CHANGER) == 0)) {
934 struct cd_softc *nsoftc;
936 nsoftc = (struct cd_softc *)nperiph->softc;
938 nsoftc->flags |= CD_FLAG_CHANGER;
939 nchanger->num_devices++;
940 if (camq_resize(&nchanger->devq,
941 nchanger->num_devices) != CAM_REQ_CMP) {
942 printf("cdregister: camq_resize "
943 "failed, changer support may "
946 nsoftc->changer = nchanger;
947 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
949 STAILQ_INSERT_TAIL(&nchanger->chluns,
950 nsoftc, changer_links);
952 } else if (status == CAM_REQ_CMP)
955 printf("cdregister: unable to allocate path\n"
956 "cdregister: changer support may be "
960 softc->changer = nchanger;
961 softc->pinfo.index = CAM_UNQUEUED_INDEX;
962 nchanger->num_devices++;
963 if (camq_resize(&nchanger->devq,
964 nchanger->num_devices) != CAM_REQ_CMP) {
965 printf("cdregister: camq_resize "
966 "failed, changer support may "
969 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
976 if ((softc->flags & CD_FLAG_CHANGER) == 0)
977 xpt_schedule(periph, CAM_PRIORITY_DEV);
979 cdschedule(periph, CAM_PRIORITY_DEV);
985 cdopen(struct disk *dp)
987 struct cam_periph *periph;
988 struct cd_softc *softc;
991 periph = (struct cam_periph *)dp->d_drv1;
995 softc = (struct cd_softc *)periph->softc;
997 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1000 cam_periph_lock(periph);
1002 if (softc->flags & CD_FLAG_INVALID) {
1003 cam_periph_unlock(periph);
1004 cam_periph_release(periph);
1008 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1009 cam_periph_unlock(periph);
1010 cam_periph_release(periph);
1014 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1018 * Check for media, and set the appropriate flags. We don't bail
1019 * if we don't have media, but then we don't allow anything but the
1020 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1022 cdcheckmedia(periph);
1024 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1025 cam_periph_unhold(periph);
1027 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1028 if ((softc->flags & CD_FLAG_OPEN) == 0) {
1029 softc->flags |= CD_FLAG_OPEN;
1030 cam_periph_unlock(periph);
1032 cam_periph_unlock(periph);
1033 cam_periph_release(periph);
1040 cdclose(struct disk *dp)
1042 struct cam_periph *periph;
1043 struct cd_softc *softc;
1045 periph = (struct cam_periph *)dp->d_drv1;
1049 softc = (struct cd_softc *)periph->softc;
1051 cam_periph_lock(periph);
1052 if (cam_periph_hold(periph, PRIBIO) != 0) {
1053 cam_periph_unlock(periph);
1054 cam_periph_release(periph);
1058 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1061 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1062 cdprevent(periph, PR_ALLOW);
1065 * Since we're closing this CD, mark the blocksize as unavailable.
1066 * It will be marked as available when the CD is opened again.
1068 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1071 * We'll check the media and toc again at the next open().
1073 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1075 cam_periph_unhold(periph);
1076 cam_periph_unlock(periph);
1077 cam_periph_release(periph);
1083 cdshorttimeout(void *arg)
1085 struct cdchanger *changer;
1087 changer = (struct cdchanger *)arg;
1089 /* Always clear the short timeout flag, since that's what we're in */
1090 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1093 * Check to see if there is any more pending or outstanding I/O for
1094 * this device. If not, move it out of the active slot.
1096 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1097 && (changer->cur_device->outstanding_cmds == 0)) {
1098 changer->flags |= CHANGER_MANUAL_CALL;
1099 cdrunchangerqueue(changer);
1104 * This is a wrapper for xpt_schedule. It only applies to changers.
1107 cdschedule(struct cam_periph *periph, int priority)
1109 struct cd_softc *softc;
1111 softc = (struct cd_softc *)periph->softc;
1114 * If this device isn't currently queued, and if it isn't
1115 * the active device, then we queue this device and run the
1116 * changer queue if there is no timeout scheduled to do it.
1117 * If this device is the active device, just schedule it
1118 * to run again. If this device is queued, there should be
1119 * a timeout in place already that will make sure it runs.
1121 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1122 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1124 * We don't do anything with the priority here.
1125 * This is strictly a fifo queue.
1127 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1128 softc->pinfo.generation = ++softc->changer->devq.generation;
1129 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1132 * Since we just put a device in the changer queue,
1133 * check and see if there is a timeout scheduled for
1134 * this changer. If so, let the timeout handle
1135 * switching this device into the active slot. If
1136 * not, manually call the timeout routine to
1139 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1140 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1141 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1142 softc->changer->flags |= CHANGER_MANUAL_CALL;
1143 cdrunchangerqueue(softc->changer);
1145 } else if ((softc->flags & CD_FLAG_ACTIVE)
1146 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1147 xpt_schedule(periph, priority);
1151 cdrunchangerqueue(void *arg)
1153 struct cd_softc *softc;
1154 struct cdchanger *changer;
1155 int called_from_timeout;
1157 changer = (struct cdchanger *)arg;
1160 * If we have NOT been called from cdstrategy() or cddone(), and
1161 * instead from a timeout routine, go ahead and clear the
1164 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1165 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1166 called_from_timeout = 1;
1168 called_from_timeout = 0;
1170 /* Always clear the manual call flag */
1171 changer->flags &= ~CHANGER_MANUAL_CALL;
1173 /* nothing to do if the queue is empty */
1174 if (changer->devq.entries <= 0) {
1179 * If the changer queue is frozen, that means we have an active
1182 if (changer->devq.qfrozen_cnt[0] > 0) {
1185 * We always need to reset the frozen count and clear the
1188 changer->devq.qfrozen_cnt[0]--;
1189 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1190 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1192 if (changer->cur_device->outstanding_cmds > 0) {
1193 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1194 changer->cur_device->bufs_left =
1195 changer->cur_device->outstanding_cmds;
1196 if (called_from_timeout) {
1197 callout_reset(&changer->long_handle,
1198 changer_max_busy_seconds * hz,
1199 cdrunchangerqueue, changer);
1200 changer->flags |= CHANGER_TIMEOUT_SCHED;
1206 * Check to see whether the current device has any I/O left
1207 * to do. If so, requeue it at the end of the queue. If
1208 * not, there is no need to requeue it.
1210 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1212 changer->cur_device->pinfo.generation =
1213 ++changer->devq.generation;
1214 camq_insert(&changer->devq,
1215 (cam_pinfo *)changer->cur_device);
1219 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1221 changer->cur_device = softc;
1223 changer->devq.qfrozen_cnt[0]++;
1224 softc->flags |= CD_FLAG_ACTIVE;
1226 /* Just in case this device is waiting */
1227 wakeup(&softc->changer);
1228 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1231 * Get rid of any pending timeouts, and set a flag to schedule new
1232 * ones so this device gets its full time quantum.
1234 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1235 callout_stop(&changer->long_handle);
1236 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1239 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1240 callout_stop(&changer->short_handle);
1241 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1245 * We need to schedule timeouts, but we only do this after the
1246 * first transaction has completed. This eliminates the changer
1249 changer->flags |= CHANGER_NEED_TIMEOUT;
1253 cdchangerschedule(struct cd_softc *softc)
1255 struct cdchanger *changer;
1257 changer = softc->changer;
1260 * If this is a changer, and this is the current device,
1261 * and this device has at least the minimum time quantum to
1262 * run, see if we can switch it out.
1264 if ((softc->flags & CD_FLAG_ACTIVE)
1265 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1266 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1268 * We try three things here. The first is that we
1269 * check to see whether the schedule on completion
1270 * flag is set. If it is, we decrement the number
1271 * of buffers left, and if it's zero, we reschedule.
1272 * Next, we check to see whether the pending buffer
1273 * queue is empty and whether there are no
1274 * outstanding transactions. If so, we reschedule.
1275 * Next, we see if the pending buffer queue is empty.
1276 * If it is, we set the number of buffers left to
1277 * the current active buffer count and set the
1278 * schedule on complete flag.
1280 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1281 if (--softc->bufs_left == 0) {
1282 softc->changer->flags |=
1283 CHANGER_MANUAL_CALL;
1284 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1285 cdrunchangerqueue(softc->changer);
1287 } else if ((bioq_first(&softc->bio_queue) == NULL)
1288 && (softc->outstanding_cmds == 0)) {
1289 softc->changer->flags |= CHANGER_MANUAL_CALL;
1290 cdrunchangerqueue(softc->changer);
1292 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1293 && (softc->flags & CD_FLAG_ACTIVE)) {
1296 * Now that the first transaction to this
1297 * particular device has completed, we can go ahead
1298 * and schedule our timeouts.
1300 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1301 callout_reset(&changer->long_handle,
1302 changer_max_busy_seconds * hz,
1303 cdrunchangerqueue, changer);
1304 changer->flags |= CHANGER_TIMEOUT_SCHED;
1306 printf("cdchangerschedule: already have a long"
1309 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1310 callout_reset(&changer->short_handle,
1311 changer_min_busy_seconds * hz,
1312 cdshorttimeout, changer);
1313 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1315 printf("cdchangerschedule: already have a short "
1319 * We just scheduled timeouts, no need to schedule
1322 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1328 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1329 u_int32_t cam_flags,
1330 u_int32_t sense_flags),
1331 u_int32_t cam_flags, u_int32_t sense_flags)
1333 struct cd_softc *softc;
1334 struct cam_periph *periph;
1337 periph = xpt_path_periph(ccb->ccb_h.path);
1338 softc = (struct cd_softc *)periph->softc;
1340 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1341 softc->disk->d_devstat);
1343 if (softc->flags & CD_FLAG_CHANGER)
1344 cdchangerschedule(softc);
1350 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1352 struct cd_softc *softc;
1354 softc = (struct cd_softc *)periph->softc;
1356 if (softc->flags & CD_FLAG_CHANGER) {
1358 * This should work the first time this device is woken up,
1359 * but just in case it doesn't, we use a while loop.
1361 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1363 * If this changer isn't already queued, queue it up.
1365 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1366 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1367 softc->pinfo.generation =
1368 ++softc->changer->devq.generation;
1369 camq_insert(&softc->changer->devq,
1370 (cam_pinfo *)softc);
1372 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1373 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1374 && ((softc->changer->flags
1375 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1376 softc->changer->flags |= CHANGER_MANUAL_CALL;
1377 cdrunchangerqueue(softc->changer);
1379 msleep(&softc->changer, periph->sim->mtx,
1380 PRIBIO, "cgticb", 0);
1383 return(cam_periph_getccb(periph, priority));
1388 * Actually translate the requested transfer into one the physical driver
1389 * can understand. The transfer is described by a buf and will include
1390 * only one physical transfer.
1393 cdstrategy(struct bio *bp)
1395 struct cam_periph *periph;
1396 struct cd_softc *softc;
1398 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1399 if (periph == NULL) {
1400 biofinish(bp, NULL, ENXIO);
1404 cam_periph_lock(periph);
1405 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1406 ("cdstrategy(%p)\n", bp));
1408 softc = (struct cd_softc *)periph->softc;
1411 * If the device has been made invalid, error out
1413 if ((softc->flags & CD_FLAG_INVALID)) {
1414 cam_periph_unlock(periph);
1415 biofinish(bp, NULL, ENXIO);
1420 * If we don't have valid media, look for it before trying to
1423 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1426 error = cdcheckmedia(periph);
1428 cam_periph_unlock(periph);
1429 biofinish(bp, NULL, error);
1435 * Place it in the queue of disk activities for this disk
1437 bioq_disksort(&softc->bio_queue, bp);
1440 * Schedule ourselves for performing the work. We do things
1441 * differently for changers.
1443 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1444 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1446 cdschedule(periph, CAM_PRIORITY_NORMAL);
1448 cam_periph_unlock(periph);
1453 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1455 struct cd_softc *softc;
1457 struct ccb_scsiio *csio;
1458 struct scsi_read_capacity_data *rcap;
1460 softc = (struct cd_softc *)periph->softc;
1462 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1464 switch (softc->state) {
1465 case CD_STATE_NORMAL:
1467 bp = bioq_first(&softc->bio_queue);
1468 if (periph->immediate_priority <= periph->pinfo.priority) {
1469 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1471 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1473 periph->immediate_priority = CAM_PRIORITY_NONE;
1474 wakeup(&periph->ccb_list);
1475 } else if (bp == NULL) {
1476 xpt_release_ccb(start_ccb);
1478 bioq_remove(&softc->bio_queue, bp);
1480 scsi_read_write(&start_ccb->csio,
1481 /*retries*/ cd_retry_count,
1482 /* cbfcnp */ cddone,
1484 /* read */bp->bio_cmd == BIO_READ,
1486 /* minimum_cmd_size */ 10,
1487 /* lba */ bp->bio_offset /
1488 softc->params.blksize,
1489 bp->bio_bcount / softc->params.blksize,
1490 /* data_ptr */ bp->bio_data,
1491 /* dxfer_len */ bp->bio_bcount,
1492 /* sense_len */ SSD_FULL_SIZE,
1493 /* timeout */ 30000);
1494 /* Use READ CD command for audio tracks. */
1495 if (softc->params.blksize == 2352) {
1496 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1497 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1498 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1499 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1500 start_ccb->csio.cdb_len = 12;
1502 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1505 LIST_INSERT_HEAD(&softc->pending_ccbs,
1506 &start_ccb->ccb_h, periph_links.le);
1507 softc->outstanding_cmds++;
1509 /* We expect a unit attention from this device */
1510 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1511 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1512 softc->flags &= ~CD_FLAG_RETRY_UA;
1515 start_ccb->ccb_h.ccb_bp = bp;
1516 bp = bioq_first(&softc->bio_queue);
1518 xpt_action(start_ccb);
1521 /* Have more work to do, so ensure we stay scheduled */
1522 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1526 case CD_STATE_PROBE:
1529 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1530 M_SCSICD, M_NOWAIT | M_ZERO);
1532 xpt_print(periph->path,
1533 "cdstart: Couldn't malloc read_capacity data\n");
1534 /* cd_free_periph??? */
1537 csio = &start_ccb->csio;
1538 scsi_read_capacity(csio,
1539 /*retries*/ cd_retry_count,
1545 start_ccb->ccb_h.ccb_bp = NULL;
1546 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1547 xpt_action(start_ccb);
1554 cddone(struct cam_periph *periph, union ccb *done_ccb)
1556 struct cd_softc *softc;
1557 struct ccb_scsiio *csio;
1559 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1561 softc = (struct cd_softc *)periph->softc;
1562 csio = &done_ccb->csio;
1564 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1565 case CD_CCB_BUFFER_IO:
1570 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1573 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1576 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1581 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1582 if (error == ERESTART) {
1584 * A retry was scheuled, so
1592 xpt_print(periph->path,
1593 "cddone: got error %#x back\n", error);
1594 bioq_flush(&softc->bio_queue, NULL, EIO);
1595 bp->bio_resid = bp->bio_bcount;
1596 bp->bio_error = error;
1597 bp->bio_flags |= BIO_ERROR;
1598 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1599 cam_release_devq(done_ccb->ccb_h.path,
1603 /*getcount_only*/0);
1606 bp->bio_resid = csio->resid;
1608 if (bp->bio_resid != 0) {
1610 * Short transfer ???
1611 * XXX: not sure this is correct for partial
1614 bp->bio_flags |= BIO_ERROR;
1618 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1619 softc->outstanding_cmds--;
1621 if (softc->flags & CD_FLAG_CHANGER)
1622 cdchangerschedule(softc);
1624 biofinish(bp, NULL, 0);
1629 struct scsi_read_capacity_data *rdcap;
1630 char announce_buf[120]; /*
1631 * Currently (9/30/97) the
1632 * longest possible announce
1633 * buffer is 108 bytes, for the
1634 * first error case below.
1635 * That is 39 bytes for the
1636 * basic string, 16 bytes for the
1637 * biggest sense key (hardware
1638 * error), 52 bytes for the
1639 * text of the largest sense
1640 * qualifier valid for a CDROM,
1641 * (0x72, 0x03 or 0x04,
1642 * 0x03), and one byte for the
1643 * null terminating character.
1644 * To allow for longer strings,
1645 * the announce buffer is 120
1648 struct cd_params *cdp;
1650 cdp = &softc->params;
1652 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1654 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1655 cdp->blksize = scsi_4btoul (rdcap->length);
1657 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1659 snprintf(announce_buf, sizeof(announce_buf),
1660 "cd present [%lu x %lu byte records]",
1661 cdp->disksize, (u_long)cdp->blksize);
1666 * Retry any UNIT ATTENTION type errors. They
1667 * are expected at boot.
1669 error = cderror(done_ccb, CAM_RETRY_SELTO,
1670 SF_RETRY_UA | SF_NO_PRINT);
1671 if (error == ERESTART) {
1673 * A retry was scheuled, so
1677 } else if (error != 0) {
1679 struct scsi_sense_data *sense;
1681 int sense_key, error_code;
1684 struct ccb_getdev cgd;
1686 /* Don't wedge this device's queue */
1687 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1688 cam_release_devq(done_ccb->ccb_h.path,
1692 /*getcount_only*/0);
1694 status = done_ccb->ccb_h.status;
1696 xpt_setup_ccb(&cgd.ccb_h,
1697 done_ccb->ccb_h.path,
1698 CAM_PRIORITY_NORMAL);
1699 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1700 xpt_action((union ccb *)&cgd);
1702 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1703 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1704 || ((status & CAM_AUTOSNS_VALID) == 0))
1710 sense = &csio->sense_data;
1711 scsi_extract_sense_len(sense,
1712 csio->sense_len - csio->sense_resid,
1713 &error_code, &sense_key, &asc,
1714 &ascq, /*show_errors*/ 1);
1717 * Attach to anything that claims to be a
1718 * CDROM or WORM device, as long as it
1719 * doesn't return a "Logical unit not
1720 * supported" (0x25) error.
1722 if ((have_sense) && (asc != 0x25)
1723 && (error_code == SSD_CURRENT_ERROR)) {
1724 const char *sense_key_desc;
1725 const char *asc_desc;
1727 scsi_sense_desc(sense_key, asc, ascq,
1731 snprintf(announce_buf,
1732 sizeof(announce_buf),
1733 "Attempt to query device "
1734 "size failed: %s, %s",
1737 } else if ((have_sense == 0)
1738 && ((status & CAM_STATUS_MASK) ==
1739 CAM_SCSI_STATUS_ERROR)
1740 && (csio->scsi_status ==
1741 SCSI_STATUS_BUSY)) {
1742 snprintf(announce_buf,
1743 sizeof(announce_buf),
1744 "Attempt to query device "
1745 "size failed: SCSI Status: %s",
1746 scsi_status_string(csio));
1747 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1749 * We only print out an error for
1750 * CDROM type devices. For WORM
1751 * devices, we don't print out an
1752 * error since a few WORM devices
1753 * don't support CDROM commands.
1754 * If we have sense information, go
1755 * ahead and print it out.
1756 * Otherwise, just say that we
1761 * Just print out the error, not
1762 * the full probe message, when we
1769 xpt_print(periph->path,
1770 "got CAM status %#x\n",
1771 done_ccb->ccb_h.status);
1773 xpt_print(periph->path, "fatal error, "
1774 "failed to attach to device\n");
1776 * Invalidate this peripheral.
1778 cam_periph_invalidate(periph);
1780 announce_buf[0] = '\0';
1784 * Invalidate this peripheral.
1786 cam_periph_invalidate(periph);
1787 announce_buf[0] = '\0';
1791 free(rdcap, M_SCSICD);
1792 if (announce_buf[0] != '\0') {
1793 xpt_announce_periph(periph, announce_buf);
1794 if (softc->flags & CD_FLAG_CHANGER)
1795 cdchangerschedule(softc);
1797 * Create our sysctl variables, now that we know
1798 * we have successfully attached.
1800 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1802 softc->state = CD_STATE_NORMAL;
1804 * Since our peripheral may be invalidated by an error
1805 * above or an external event, we must release our CCB
1806 * before releasing the probe lock on the peripheral.
1807 * The peripheral will only go away once the last lock
1808 * is removed, and we need it around for the CCB release
1811 xpt_release_ccb(done_ccb);
1812 cam_periph_unhold(periph);
1815 case CD_CCB_WAITING:
1817 /* Caller will release the CCB */
1818 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1819 ("trying to wakeup ccbwait\n"));
1821 wakeup(&done_ccb->ccb_h.cbfcnp);
1827 xpt_release_ccb(done_ccb);
1830 static union cd_pages *
1831 cdgetpage(struct cd_mode_params *mode_params)
1833 union cd_pages *page;
1835 if (mode_params->cdb_size == 10)
1836 page = (union cd_pages *)find_mode_page_10(
1837 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1839 page = (union cd_pages *)find_mode_page_6(
1840 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1846 cdgetpagesize(int page_num)
1850 for (i = 0; i < (sizeof(cd_page_size_table)/
1851 sizeof(cd_page_size_table[0])); i++) {
1852 if (cd_page_size_table[i].page == page_num)
1853 return (cd_page_size_table[i].page_size);
1860 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1863 struct cam_periph *periph;
1864 struct cd_softc *softc;
1865 int nocopyout, error = 0;
1867 periph = (struct cam_periph *)dp->d_drv1;
1871 cam_periph_lock(periph);
1873 softc = (struct cd_softc *)periph->softc;
1875 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1876 ("cdioctl(%#lx)\n", cmd));
1878 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1879 cam_periph_unlock(periph);
1880 cam_periph_release(periph);
1885 * If we don't have media loaded, check for it. If still don't
1886 * have media loaded, we can only do a load or eject.
1888 * We only care whether media is loaded if this is a cd-specific ioctl
1889 * (thus the IOCGROUP check below). Note that this will break if
1890 * anyone adds any ioctls into the switch statement below that don't
1891 * have their ioctl group set to 'c'.
1893 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1894 && ((cmd != CDIOCCLOSE)
1895 && (cmd != CDIOCEJECT))
1896 && (IOCGROUP(cmd) == 'c')) {
1897 error = cdcheckmedia(periph);
1899 cam_periph_unhold(periph);
1900 cam_periph_unlock(periph);
1905 * Drop the lock here so later mallocs can use WAITOK. The periph
1906 * is essentially locked still with the cam_periph_hold call above.
1908 cam_periph_unlock(periph);
1913 case CDIOCPLAYTRACKS:
1915 struct ioc_play_track *args
1916 = (struct ioc_play_track *) addr;
1917 struct cd_mode_params params;
1918 union cd_pages *page;
1920 params.alloc_len = sizeof(union cd_mode_data_6_10);
1921 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1924 cam_periph_lock(periph);
1925 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1926 ("trying to do CDIOCPLAYTRACKS\n"));
1928 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1930 free(params.mode_buf, M_SCSICD);
1931 cam_periph_unlock(periph);
1934 page = cdgetpage(¶ms);
1936 page->audio.flags &= ~CD_PA_SOTC;
1937 page->audio.flags |= CD_PA_IMMED;
1938 error = cdsetmode(periph, ¶ms);
1939 free(params.mode_buf, M_SCSICD);
1941 cam_periph_unlock(periph);
1946 * This was originally implemented with the PLAY
1947 * AUDIO TRACK INDEX command, but that command was
1948 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1949 * drives support it but ATAPI and ATAPI-derivative
1950 * drives don't seem to support it. So we keep a
1951 * cache of the table of contents and translate
1952 * track numbers to MSF format.
1954 if (softc->flags & CD_FLAG_VALID_TOC) {
1955 union msf_lba *sentry, *eentry;
1958 if (args->end_track <
1959 softc->toc.header.ending_track + 1)
1961 if (args->end_track >
1962 softc->toc.header.ending_track + 1)
1964 softc->toc.header.ending_track + 1;
1965 st = args->start_track -
1966 softc->toc.header.starting_track;
1967 et = args->end_track -
1968 softc->toc.header.starting_track;
1971 || (st > (softc->toc.header.ending_track -
1972 softc->toc.header.starting_track))) {
1976 sentry = &softc->toc.entries[st].addr;
1977 eentry = &softc->toc.entries[et].addr;
1978 error = cdplaymsf(periph,
1987 * If we don't have a valid TOC, try the
1988 * play track index command. It is part of
1989 * the SCSI-2 spec, but was removed in the
1990 * MMC specs. ATAPI and ATAPI-derived
1991 * drives don't support it.
1993 if (softc->quirks & CD_Q_BCD_TRACKS) {
1995 bin2bcd(args->start_track);
1997 bin2bcd(args->end_track);
1999 error = cdplaytracks(periph,
2005 cam_periph_unlock(periph);
2010 struct ioc_play_msf *args
2011 = (struct ioc_play_msf *) addr;
2012 struct cd_mode_params params;
2013 union cd_pages *page;
2015 params.alloc_len = sizeof(union cd_mode_data_6_10);
2016 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2019 cam_periph_lock(periph);
2020 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2021 ("trying to do CDIOCPLAYMSF\n"));
2023 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2025 free(params.mode_buf, M_SCSICD);
2026 cam_periph_unlock(periph);
2029 page = cdgetpage(¶ms);
2031 page->audio.flags &= ~CD_PA_SOTC;
2032 page->audio.flags |= CD_PA_IMMED;
2033 error = cdsetmode(periph, ¶ms);
2034 free(params.mode_buf, M_SCSICD);
2036 cam_periph_unlock(periph);
2039 error = cdplaymsf(periph,
2046 cam_periph_unlock(periph);
2049 case CDIOCPLAYBLOCKS:
2051 struct ioc_play_blocks *args
2052 = (struct ioc_play_blocks *) addr;
2053 struct cd_mode_params params;
2054 union cd_pages *page;
2056 params.alloc_len = sizeof(union cd_mode_data_6_10);
2057 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2060 cam_periph_lock(periph);
2061 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2062 ("trying to do CDIOCPLAYBLOCKS\n"));
2065 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2067 free(params.mode_buf, M_SCSICD);
2068 cam_periph_unlock(periph);
2071 page = cdgetpage(¶ms);
2073 page->audio.flags &= ~CD_PA_SOTC;
2074 page->audio.flags |= CD_PA_IMMED;
2075 error = cdsetmode(periph, ¶ms);
2076 free(params.mode_buf, M_SCSICD);
2078 cam_periph_unlock(periph);
2081 error = cdplay(periph, args->blk, args->len);
2082 cam_periph_unlock(periph);
2085 case CDIOCREADSUBCHANNEL_SYSSPACE:
2088 case CDIOCREADSUBCHANNEL:
2090 struct ioc_read_subchannel *args
2091 = (struct ioc_read_subchannel *) addr;
2092 struct cd_sub_channel_info *data;
2093 u_int32_t len = args->data_len;
2095 data = malloc(sizeof(struct cd_sub_channel_info),
2096 M_SCSICD, M_WAITOK | M_ZERO);
2098 cam_periph_lock(periph);
2099 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2100 ("trying to do CDIOCREADSUBCHANNEL\n"));
2102 if ((len > sizeof(struct cd_sub_channel_info)) ||
2103 (len < sizeof(struct cd_sub_channel_header))) {
2105 "scsi_cd: cdioctl: "
2106 "cdioreadsubchannel: error, len=%d\n",
2109 free(data, M_SCSICD);
2110 cam_periph_unlock(periph);
2114 if (softc->quirks & CD_Q_BCD_TRACKS)
2115 args->track = bin2bcd(args->track);
2117 error = cdreadsubchannel(periph, args->address_format,
2118 args->data_format, args->track, data, len);
2121 free(data, M_SCSICD);
2122 cam_periph_unlock(periph);
2125 if (softc->quirks & CD_Q_BCD_TRACKS)
2126 data->what.track_info.track_number =
2127 bcd2bin(data->what.track_info.track_number);
2128 len = min(len, ((data->header.data_len[0] << 8) +
2129 data->header.data_len[1] +
2130 sizeof(struct cd_sub_channel_header)));
2131 cam_periph_unlock(periph);
2132 if (nocopyout == 0) {
2133 if (copyout(data, args->data, len) != 0) {
2137 bcopy(data, args->data, len);
2139 free(data, M_SCSICD);
2143 case CDIOREADTOCHEADER:
2145 struct ioc_toc_header *th;
2147 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2150 cam_periph_lock(periph);
2151 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2152 ("trying to do CDIOREADTOCHEADER\n"));
2154 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2155 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2158 cam_periph_unlock(periph);
2161 if (softc->quirks & CD_Q_BCD_TRACKS) {
2162 /* we are going to have to convert the BCD
2163 * encoding on the cd to what is expected
2165 th->starting_track =
2166 bcd2bin(th->starting_track);
2167 th->ending_track = bcd2bin(th->ending_track);
2169 th->len = ntohs(th->len);
2170 bcopy(th, addr, sizeof(*th));
2172 cam_periph_unlock(periph);
2175 case CDIOREADTOCENTRYS:
2177 struct cd_tocdata *data;
2178 struct cd_toc_single *lead;
2179 struct ioc_read_toc_entry *te =
2180 (struct ioc_read_toc_entry *) addr;
2181 struct ioc_toc_header *th;
2182 u_int32_t len, readlen, idx, num;
2183 u_int32_t starting_track = te->starting_track;
2185 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2186 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2188 cam_periph_lock(periph);
2189 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2190 ("trying to do CDIOREADTOCENTRYS\n"));
2192 if (te->data_len < sizeof(struct cd_toc_entry)
2193 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2194 || (te->address_format != CD_MSF_FORMAT
2195 && te->address_format != CD_LBA_FORMAT)) {
2197 printf("scsi_cd: error in readtocentries, "
2198 "returning EINVAL\n");
2199 free(data, M_SCSICD);
2200 free(lead, M_SCSICD);
2201 cam_periph_unlock(periph);
2206 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2207 sizeof (*th), /*sense_flags*/0);
2209 free(data, M_SCSICD);
2210 free(lead, M_SCSICD);
2211 cam_periph_unlock(periph);
2215 if (softc->quirks & CD_Q_BCD_TRACKS) {
2216 /* we are going to have to convert the BCD
2217 * encoding on the cd to what is expected
2219 th->starting_track =
2220 bcd2bin(th->starting_track);
2221 th->ending_track = bcd2bin(th->ending_track);
2224 if (starting_track == 0)
2225 starting_track = th->starting_track;
2226 else if (starting_track == LEADOUT)
2227 starting_track = th->ending_track + 1;
2228 else if (starting_track < th->starting_track ||
2229 starting_track > th->ending_track + 1) {
2230 printf("scsi_cd: error in readtocentries, "
2231 "returning EINVAL\n");
2232 free(data, M_SCSICD);
2233 free(lead, M_SCSICD);
2234 cam_periph_unlock(periph);
2239 /* calculate reading length without leadout entry */
2240 readlen = (th->ending_track - starting_track + 1) *
2241 sizeof(struct cd_toc_entry);
2243 /* and with leadout entry */
2244 len = readlen + sizeof(struct cd_toc_entry);
2245 if (te->data_len < len) {
2250 if (len > sizeof(data->entries)) {
2251 printf("scsi_cd: error in readtocentries, "
2252 "returning EINVAL\n");
2254 free(data, M_SCSICD);
2255 free(lead, M_SCSICD);
2256 cam_periph_unlock(periph);
2259 num = len / sizeof(struct cd_toc_entry);
2262 error = cdreadtoc(periph, te->address_format,
2265 readlen + sizeof (*th),
2268 free(data, M_SCSICD);
2269 free(lead, M_SCSICD);
2270 cam_periph_unlock(periph);
2275 /* make leadout entry if needed */
2276 idx = starting_track + num - 1;
2277 if (softc->quirks & CD_Q_BCD_TRACKS)
2278 th->ending_track = bcd2bin(th->ending_track);
2279 if (idx == th->ending_track + 1) {
2280 error = cdreadtoc(periph, te->address_format,
2281 LEADOUT, (u_int8_t *)lead,
2285 free(data, M_SCSICD);
2286 free(lead, M_SCSICD);
2287 cam_periph_unlock(periph);
2290 data->entries[idx - starting_track] =
2293 if (softc->quirks & CD_Q_BCD_TRACKS) {
2294 for (idx = 0; idx < num - 1; idx++) {
2295 data->entries[idx].track =
2296 bcd2bin(data->entries[idx].track);
2300 cam_periph_unlock(periph);
2301 error = copyout(data->entries, te->data, len);
2302 free(data, M_SCSICD);
2303 free(lead, M_SCSICD);
2306 case CDIOREADTOCENTRY:
2308 struct cd_toc_single *data;
2309 struct ioc_read_toc_single_entry *te =
2310 (struct ioc_read_toc_single_entry *) addr;
2311 struct ioc_toc_header *th;
2314 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2316 cam_periph_lock(periph);
2317 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2318 ("trying to do CDIOREADTOCENTRY\n"));
2320 if (te->address_format != CD_MSF_FORMAT
2321 && te->address_format != CD_LBA_FORMAT) {
2322 printf("error in readtocentry, "
2323 " returning EINVAL\n");
2324 free(data, M_SCSICD);
2326 cam_periph_unlock(periph);
2331 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2332 sizeof (*th), /*sense_flags*/0);
2334 free(data, M_SCSICD);
2335 cam_periph_unlock(periph);
2339 if (softc->quirks & CD_Q_BCD_TRACKS) {
2340 /* we are going to have to convert the BCD
2341 * encoding on the cd to what is expected
2343 th->starting_track =
2344 bcd2bin(th->starting_track);
2345 th->ending_track = bcd2bin(th->ending_track);
2349 track = th->starting_track;
2350 else if (track == LEADOUT)
2352 else if (track < th->starting_track ||
2353 track > th->ending_track + 1) {
2354 printf("error in readtocentry, "
2355 " returning EINVAL\n");
2356 free(data, M_SCSICD);
2358 cam_periph_unlock(periph);
2362 error = cdreadtoc(periph, te->address_format, track,
2363 (u_int8_t *)data, sizeof(*data),
2366 free(data, M_SCSICD);
2367 cam_periph_unlock(periph);
2371 if (softc->quirks & CD_Q_BCD_TRACKS)
2372 data->entry.track = bcd2bin(data->entry.track);
2373 bcopy(&data->entry, &te->entry,
2374 sizeof(struct cd_toc_entry));
2375 free(data, M_SCSICD);
2376 cam_periph_unlock(periph);
2381 struct ioc_patch *arg = (struct ioc_patch *)addr;
2382 struct cd_mode_params params;
2383 union cd_pages *page;
2385 params.alloc_len = sizeof(union cd_mode_data_6_10);
2386 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2389 cam_periph_lock(periph);
2390 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2391 ("trying to do CDIOCSETPATCH\n"));
2393 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2395 free(params.mode_buf, M_SCSICD);
2396 cam_periph_unlock(periph);
2399 page = cdgetpage(¶ms);
2401 page->audio.port[LEFT_PORT].channels =
2403 page->audio.port[RIGHT_PORT].channels =
2405 page->audio.port[2].channels = arg->patch[2];
2406 page->audio.port[3].channels = arg->patch[3];
2407 error = cdsetmode(periph, ¶ms);
2408 free(params.mode_buf, M_SCSICD);
2409 cam_periph_unlock(periph);
2414 struct ioc_vol *arg = (struct ioc_vol *) addr;
2415 struct cd_mode_params params;
2416 union cd_pages *page;
2418 params.alloc_len = sizeof(union cd_mode_data_6_10);
2419 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2422 cam_periph_lock(periph);
2423 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2424 ("trying to do CDIOCGETVOL\n"));
2426 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2428 free(params.mode_buf, M_SCSICD);
2429 cam_periph_unlock(periph);
2432 page = cdgetpage(¶ms);
2434 arg->vol[LEFT_PORT] =
2435 page->audio.port[LEFT_PORT].volume;
2436 arg->vol[RIGHT_PORT] =
2437 page->audio.port[RIGHT_PORT].volume;
2438 arg->vol[2] = page->audio.port[2].volume;
2439 arg->vol[3] = page->audio.port[3].volume;
2440 free(params.mode_buf, M_SCSICD);
2441 cam_periph_unlock(periph);
2446 struct ioc_vol *arg = (struct ioc_vol *) addr;
2447 struct cd_mode_params params;
2448 union cd_pages *page;
2450 params.alloc_len = sizeof(union cd_mode_data_6_10);
2451 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2454 cam_periph_lock(periph);
2455 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2456 ("trying to do CDIOCSETVOL\n"));
2458 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2460 free(params.mode_buf, M_SCSICD);
2461 cam_periph_unlock(periph);
2464 page = cdgetpage(¶ms);
2466 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2467 page->audio.port[LEFT_PORT].volume =
2468 arg->vol[LEFT_PORT];
2469 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2470 page->audio.port[RIGHT_PORT].volume =
2471 arg->vol[RIGHT_PORT];
2472 page->audio.port[2].volume = arg->vol[2];
2473 page->audio.port[3].volume = arg->vol[3];
2474 error = cdsetmode(periph, ¶ms);
2475 cam_periph_unlock(periph);
2476 free(params.mode_buf, M_SCSICD);
2481 struct cd_mode_params params;
2482 union cd_pages *page;
2484 params.alloc_len = sizeof(union cd_mode_data_6_10);
2485 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2488 cam_periph_lock(periph);
2489 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2490 ("trying to do CDIOCSETMONO\n"));
2492 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2494 free(params.mode_buf, M_SCSICD);
2495 cam_periph_unlock(periph);
2498 page = cdgetpage(¶ms);
2500 page->audio.port[LEFT_PORT].channels =
2501 LEFT_CHANNEL | RIGHT_CHANNEL;
2502 page->audio.port[RIGHT_PORT].channels =
2503 LEFT_CHANNEL | RIGHT_CHANNEL;
2504 page->audio.port[2].channels = 0;
2505 page->audio.port[3].channels = 0;
2506 error = cdsetmode(periph, ¶ms);
2507 cam_periph_unlock(periph);
2508 free(params.mode_buf, M_SCSICD);
2511 case CDIOCSETSTEREO:
2513 struct cd_mode_params params;
2514 union cd_pages *page;
2516 params.alloc_len = sizeof(union cd_mode_data_6_10);
2517 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2520 cam_periph_lock(periph);
2521 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2522 ("trying to do CDIOCSETSTEREO\n"));
2524 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2526 free(params.mode_buf, M_SCSICD);
2527 cam_periph_unlock(periph);
2530 page = cdgetpage(¶ms);
2532 page->audio.port[LEFT_PORT].channels =
2534 page->audio.port[RIGHT_PORT].channels =
2536 page->audio.port[2].channels = 0;
2537 page->audio.port[3].channels = 0;
2538 error = cdsetmode(periph, ¶ms);
2539 free(params.mode_buf, M_SCSICD);
2540 cam_periph_unlock(periph);
2545 struct cd_mode_params params;
2546 union cd_pages *page;
2548 params.alloc_len = sizeof(union cd_mode_data_6_10);
2549 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2552 cam_periph_lock(periph);
2553 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2554 ("trying to do CDIOCSETMUTE\n"));
2556 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2558 free(params.mode_buf, M_SCSICD);
2559 cam_periph_unlock(periph);
2562 page = cdgetpage(¶ms);
2564 page->audio.port[LEFT_PORT].channels = 0;
2565 page->audio.port[RIGHT_PORT].channels = 0;
2566 page->audio.port[2].channels = 0;
2567 page->audio.port[3].channels = 0;
2568 error = cdsetmode(periph, ¶ms);
2569 free(params.mode_buf, M_SCSICD);
2570 cam_periph_unlock(periph);
2575 struct cd_mode_params params;
2576 union cd_pages *page;
2578 params.alloc_len = sizeof(union cd_mode_data_6_10);
2579 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2582 cam_periph_lock(periph);
2583 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2584 ("trying to do CDIOCSETLEFT\n"));
2586 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2588 free(params.mode_buf, M_SCSICD);
2589 cam_periph_unlock(periph);
2592 page = cdgetpage(¶ms);
2594 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2595 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2596 page->audio.port[2].channels = 0;
2597 page->audio.port[3].channels = 0;
2598 error = cdsetmode(periph, ¶ms);
2599 free(params.mode_buf, M_SCSICD);
2600 cam_periph_unlock(periph);
2605 struct cd_mode_params params;
2606 union cd_pages *page;
2608 params.alloc_len = sizeof(union cd_mode_data_6_10);
2609 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2612 cam_periph_lock(periph);
2613 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2614 ("trying to do CDIOCSETRIGHT\n"));
2616 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2618 free(params.mode_buf, M_SCSICD);
2619 cam_periph_unlock(periph);
2622 page = cdgetpage(¶ms);
2624 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2625 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2626 page->audio.port[2].channels = 0;
2627 page->audio.port[3].channels = 0;
2628 error = cdsetmode(periph, ¶ms);
2629 free(params.mode_buf, M_SCSICD);
2630 cam_periph_unlock(periph);
2634 cam_periph_lock(periph);
2635 error = cdpause(periph, 1);
2636 cam_periph_unlock(periph);
2639 cam_periph_lock(periph);
2640 error = cdpause(periph, 0);
2641 cam_periph_unlock(periph);
2644 cam_periph_lock(periph);
2645 error = cdstartunit(periph, 0);
2646 cam_periph_unlock(periph);
2649 cam_periph_lock(periph);
2650 error = cdstartunit(periph, 1);
2651 cam_periph_unlock(periph);
2654 cam_periph_lock(periph);
2655 error = cdstopunit(periph, 0);
2656 cam_periph_unlock(periph);
2659 cam_periph_lock(periph);
2660 error = cdstopunit(periph, 1);
2661 cam_periph_unlock(periph);
2664 cam_periph_lock(periph);
2665 cdprevent(periph, PR_ALLOW);
2666 cam_periph_unlock(periph);
2669 cam_periph_lock(periph);
2670 cdprevent(periph, PR_PREVENT);
2671 cam_periph_unlock(periph);
2674 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2678 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2682 /* return (cd_reset(periph)); */
2685 case CDRIOCREADSPEED:
2686 cam_periph_lock(periph);
2687 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2688 cam_periph_unlock(periph);
2690 case CDRIOCWRITESPEED:
2691 cam_periph_lock(periph);
2692 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2693 cam_periph_unlock(periph);
2695 case CDRIOCGETBLOCKSIZE:
2696 *(int *)addr = softc->params.blksize;
2698 case CDRIOCSETBLOCKSIZE:
2699 if (*(int *)addr <= 0) {
2703 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2706 case DVDIOCREPORTKEY: {
2707 struct dvd_authinfo *authinfo;
2709 authinfo = (struct dvd_authinfo *)addr;
2711 if (cmd == DVDIOCREPORTKEY)
2712 error = cdreportkey(periph, authinfo);
2714 error = cdsendkey(periph, authinfo);
2717 case DVDIOCREADSTRUCTURE: {
2718 struct dvd_struct *dvdstruct;
2720 dvdstruct = (struct dvd_struct *)addr;
2722 error = cdreaddvdstructure(periph, dvdstruct);
2727 cam_periph_lock(periph);
2728 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2729 cam_periph_unlock(periph);
2733 cam_periph_lock(periph);
2734 cam_periph_unhold(periph);
2736 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2737 if (error && bootverbose) {
2738 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2740 cam_periph_unlock(periph);
2746 cdprevent(struct cam_periph *periph, int action)
2749 struct cd_softc *softc;
2752 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2754 softc = (struct cd_softc *)periph->softc;
2756 if (((action == PR_ALLOW)
2757 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2758 || ((action == PR_PREVENT)
2759 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2763 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2765 scsi_prevent(&ccb->csio,
2766 /*retries*/ cd_retry_count,
2771 /* timeout */60000);
2773 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2774 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2776 xpt_release_ccb(ccb);
2779 if (action == PR_ALLOW)
2780 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2782 softc->flags |= CD_FLAG_DISC_LOCKED;
2787 * XXX: the disk media and sector size is only really able to change
2788 * XXX: while the device is closed.
2791 cdcheckmedia(struct cam_periph *periph)
2793 struct cd_softc *softc;
2794 struct ioc_toc_header *toch;
2795 struct cd_toc_single leadout;
2796 u_int32_t size, toclen;
2797 int error, num_entries, cdindex;
2799 softc = (struct cd_softc *)periph->softc;
2801 cdprevent(periph, PR_PREVENT);
2802 softc->disk->d_sectorsize = 2048;
2803 softc->disk->d_mediasize = 0;
2806 * Get the disc size and block size. If we can't get it, we don't
2807 * have media, most likely.
2809 if ((error = cdsize(periph, &size)) != 0) {
2810 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2811 cdprevent(periph, PR_ALLOW);
2814 softc->flags |= CD_FLAG_VALID_MEDIA;
2815 softc->disk->d_sectorsize = softc->params.blksize;
2816 softc->disk->d_mediasize =
2817 (off_t)softc->params.blksize * softc->params.disksize;
2821 * Now we check the table of contents. This (currently) is only
2822 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2823 * things like present a separate entry in /dev for each track,
2824 * like that acd(4) driver does.
2826 bzero(&softc->toc, sizeof(softc->toc));
2827 toch = &softc->toc.header;
2829 * We will get errors here for media that doesn't have a table of
2830 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2831 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2832 * has not been recorded (no complete session) and the Format codes
2833 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2834 * with an INVALID FIELD IN CDB. Devices that are not capable of
2835 * reading an incomplete session on DDC/CD-R/RW media shall report
2836 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2838 * So this isn't fatal if we can't read the table of contents, it
2839 * just means that the user won't be able to issue the play tracks
2840 * ioctl, and likely lots of other stuff won't work either. They
2841 * need to burn the CD before we can do a whole lot with it. So
2842 * we don't print anything here if we get an error back.
2844 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2847 * Errors in reading the table of contents aren't fatal, we just
2848 * won't have a valid table of contents cached.
2852 bzero(&softc->toc, sizeof(softc->toc));
2856 if (softc->quirks & CD_Q_BCD_TRACKS) {
2857 toch->starting_track = bcd2bin(toch->starting_track);
2858 toch->ending_track = bcd2bin(toch->ending_track);
2861 /* Number of TOC entries, plus leadout */
2862 num_entries = (toch->ending_track - toch->starting_track) + 2;
2864 if (num_entries <= 0)
2867 toclen = num_entries * sizeof(struct cd_toc_entry);
2869 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2870 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2874 bzero(&softc->toc, sizeof(softc->toc));
2878 if (softc->quirks & CD_Q_BCD_TRACKS) {
2879 toch->starting_track = bcd2bin(toch->starting_track);
2880 toch->ending_track = bcd2bin(toch->ending_track);
2883 * XXX KDM is this necessary? Probably only if the drive doesn't
2884 * return leadout information with the table of contents.
2886 cdindex = toch->starting_track + num_entries -1;
2887 if (cdindex == toch->ending_track + 1) {
2889 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2890 (u_int8_t *)&leadout, sizeof(leadout),
2896 softc->toc.entries[cdindex - toch->starting_track] =
2899 if (softc->quirks & CD_Q_BCD_TRACKS) {
2900 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2901 softc->toc.entries[cdindex].track =
2902 bcd2bin(softc->toc.entries[cdindex].track);
2906 softc->flags |= CD_FLAG_VALID_TOC;
2908 /* If the first track is audio, correct sector size. */
2909 if ((softc->toc.entries[0].control & 4) == 0) {
2910 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2911 softc->disk->d_mediasize =
2912 (off_t)softc->params.blksize * softc->params.disksize;
2918 * We unconditionally (re)set the blocksize each time the
2919 * CD device is opened. This is because the CD can change,
2920 * and therefore the blocksize might change.
2921 * XXX problems here if some slice or partition is still
2922 * open with the old size?
2924 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2925 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2926 softc->disk->d_devstat->block_size = softc->params.blksize;
2932 cdsize(struct cam_periph *periph, u_int32_t *size)
2934 struct cd_softc *softc;
2936 struct scsi_read_capacity_data *rcap_buf;
2939 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2941 softc = (struct cd_softc *)periph->softc;
2943 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2945 /* XXX Should be M_WAITOK */
2946 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2947 M_SCSICD, M_NOWAIT | M_ZERO);
2948 if (rcap_buf == NULL)
2951 scsi_read_capacity(&ccb->csio,
2952 /*retries*/ cd_retry_count,
2957 /* timeout */20000);
2959 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2960 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2962 xpt_release_ccb(ccb);
2964 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2965 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2966 /* Make sure we got at least some block size. */
2967 if (error == 0 && softc->params.blksize == 0)
2970 * SCSI-3 mandates that the reported blocksize shall be 2048.
2971 * Older drives sometimes report funny values, trim it down to
2972 * 2048, or other parts of the kernel will get confused.
2974 * XXX we leave drives alone that might report 512 bytes, as
2975 * well as drives reporting more weird sizes like perhaps 4K.
2977 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2978 softc->params.blksize = 2048;
2980 free(rcap_buf, M_SCSICD);
2981 *size = softc->params.disksize;
2988 cd6byteworkaround(union ccb *ccb)
2991 struct cam_periph *periph;
2992 struct cd_softc *softc;
2993 struct cd_mode_params *params;
2996 periph = xpt_path_periph(ccb->ccb_h.path);
2997 softc = (struct cd_softc *)periph->softc;
2999 cdb = ccb->csio.cdb_io.cdb_bytes;
3001 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3002 || ((cdb[0] != MODE_SENSE_6)
3003 && (cdb[0] != MODE_SELECT_6)))
3007 * Because there is no convenient place to stash the overall
3008 * cd_mode_params structure pointer, we have to grab it like this.
3009 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3010 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3012 * XXX It would be nice if, at some point, we could increase the
3013 * number of available peripheral private pointers. Both pointers
3014 * are currently used in most every peripheral driver.
3018 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3019 if (params->mode_buf == ccb->csio.data_ptr) {
3026 * This shouldn't happen. All mode sense and mode select
3027 * operations in the cd(4) driver MUST go through cdgetmode() and
3031 xpt_print(periph->path,
3032 "mode buffer not found in mode queue!\n");
3036 params->cdb_size = 10;
3037 softc->minimum_command_size = 10;
3038 xpt_print(ccb->ccb_h.path,
3039 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3040 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3042 if (cdb[0] == MODE_SENSE_6) {
3043 struct scsi_mode_sense_10 ms10;
3044 struct scsi_mode_sense_6 *ms6;
3047 ms6 = (struct scsi_mode_sense_6 *)cdb;
3049 bzero(&ms10, sizeof(ms10));
3050 ms10.opcode = MODE_SENSE_10;
3051 ms10.byte2 = ms6->byte2;
3052 ms10.page = ms6->page;
3055 * 10 byte mode header, block descriptor,
3056 * sizeof(union cd_pages)
3058 len = sizeof(struct cd_mode_data_10);
3059 ccb->csio.dxfer_len = len;
3061 scsi_ulto2b(len, ms10.length);
3062 ms10.control = ms6->control;
3063 bcopy(&ms10, cdb, 10);
3064 ccb->csio.cdb_len = 10;
3066 struct scsi_mode_select_10 ms10;
3067 struct scsi_mode_select_6 *ms6;
3068 struct scsi_mode_header_6 *header6;
3069 struct scsi_mode_header_10 *header10;
3070 struct scsi_mode_page_header *page_header;
3071 int blk_desc_len, page_num, page_size, len;
3073 ms6 = (struct scsi_mode_select_6 *)cdb;
3075 bzero(&ms10, sizeof(ms10));
3076 ms10.opcode = MODE_SELECT_10;
3077 ms10.byte2 = ms6->byte2;
3079 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3080 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3082 page_header = find_mode_page_6(header6);
3083 page_num = page_header->page_code;
3085 blk_desc_len = header6->blk_desc_len;
3087 page_size = cdgetpagesize(page_num);
3089 if (page_size != (page_header->page_length +
3090 sizeof(*page_header)))
3091 page_size = page_header->page_length +
3092 sizeof(*page_header);
3094 len = sizeof(*header10) + blk_desc_len + page_size;
3096 len = min(params->alloc_len, len);
3099 * Since the 6 byte parameter header is shorter than the 10
3100 * byte parameter header, we need to copy the actual mode
3101 * page data, and the block descriptor, if any, so things wind
3102 * up in the right place. The regions will overlap, but
3103 * bcopy() does the right thing.
3105 bcopy(params->mode_buf + sizeof(*header6),
3106 params->mode_buf + sizeof(*header10),
3107 len - sizeof(*header10));
3109 /* Make sure these fields are set correctly. */
3110 scsi_ulto2b(0, header10->data_length);
3111 header10->medium_type = 0;
3112 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3114 ccb->csio.dxfer_len = len;
3116 scsi_ulto2b(len, ms10.length);
3117 ms10.control = ms6->control;
3118 bcopy(&ms10, cdb, 10);
3119 ccb->csio.cdb_len = 10;
3122 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3123 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3126 cam_release_devq(ccb->ccb_h.path,
3130 /*getcount_only*/0);
3137 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3139 struct cd_softc *softc;
3140 struct cam_periph *periph;
3143 periph = xpt_path_periph(ccb->ccb_h.path);
3144 softc = (struct cd_softc *)periph->softc;
3149 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3150 * CDB comes back with this particular error, try transforming it
3151 * into the 10 byte version.
3153 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3154 error = cd6byteworkaround(ccb);
3155 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3156 CAM_SCSI_STATUS_ERROR)
3157 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3158 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3159 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3160 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3161 int sense_key, error_code, asc, ascq;
3163 scsi_extract_sense_len(&ccb->csio.sense_data,
3164 ccb->csio.sense_len - ccb->csio.sense_resid, &error_code,
3165 &sense_key, &asc, &ascq, /*show_errors*/ 1);
3166 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3167 error = cd6byteworkaround(ccb);
3170 if (error == ERESTART)
3175 * Until we have a better way of doing pack validation,
3176 * don't treat UAs as errors.
3178 sense_flags |= SF_RETRY_UA;
3179 return (cam_periph_error(ccb, cam_flags, sense_flags,
3180 &softc->saved_ccb));
3184 * Read table of contents
3187 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3188 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3190 struct scsi_read_toc *scsi_cmd;
3192 struct ccb_scsiio *csio;
3199 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3204 /* retries */ cd_retry_count,
3205 /* cbfcnp */ cddone,
3206 /* flags */ CAM_DIR_IN,
3207 /* tag_action */ MSG_SIMPLE_Q_TAG,
3208 /* data_ptr */ data,
3209 /* dxfer_len */ len,
3210 /* sense_len */ SSD_FULL_SIZE,
3211 sizeof(struct scsi_read_toc),
3212 /* timeout */ 50000);
3214 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3215 bzero (scsi_cmd, sizeof(*scsi_cmd));
3217 if (mode == CD_MSF_FORMAT)
3218 scsi_cmd->byte2 |= CD_MSF;
3219 scsi_cmd->from_track = start;
3220 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3221 scsi_cmd->data_len[0] = (ntoc) >> 8;
3222 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3224 scsi_cmd->op_code = READ_TOC;
3226 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3227 /*sense_flags*/SF_RETRY_UA | sense_flags);
3229 xpt_release_ccb(ccb);
3235 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3236 u_int32_t format, int track,
3237 struct cd_sub_channel_info *data, u_int32_t len)
3239 struct scsi_read_subchannel *scsi_cmd;
3240 struct ccb_scsiio *csio;
3246 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3251 /* retries */ cd_retry_count,
3252 /* cbfcnp */ cddone,
3253 /* flags */ CAM_DIR_IN,
3254 /* tag_action */ MSG_SIMPLE_Q_TAG,
3255 /* data_ptr */ (u_int8_t *)data,
3256 /* dxfer_len */ len,
3257 /* sense_len */ SSD_FULL_SIZE,
3258 sizeof(struct scsi_read_subchannel),
3259 /* timeout */ 50000);
3261 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3262 bzero (scsi_cmd, sizeof(*scsi_cmd));
3264 scsi_cmd->op_code = READ_SUBCHANNEL;
3265 if (mode == CD_MSF_FORMAT)
3266 scsi_cmd->byte1 |= CD_MSF;
3267 scsi_cmd->byte2 = SRS_SUBQ;
3268 scsi_cmd->subchan_format = format;
3269 scsi_cmd->track = track;
3270 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3271 scsi_cmd->control = 0;
3273 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3274 /*sense_flags*/SF_RETRY_UA);
3276 xpt_release_ccb(ccb);
3283 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3284 * routine. See comments in cd6byteworkaround() for details.
3287 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3290 struct ccb_scsiio *csio;
3291 struct cd_softc *softc;
3296 softc = (struct cd_softc *)periph->softc;
3298 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3302 data->cdb_size = softc->minimum_command_size;
3303 if (data->cdb_size < 10)
3304 param_len = sizeof(struct cd_mode_data);
3306 param_len = sizeof(struct cd_mode_data_10);
3308 /* Don't say we've got more room than we actually allocated */
3309 param_len = min(param_len, data->alloc_len);
3311 scsi_mode_sense_len(csio,
3312 /* retries */ cd_retry_count,
3313 /* cbfcnp */ cddone,
3314 /* tag_action */ MSG_SIMPLE_Q_TAG,
3316 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3318 /* param_buf */ data->mode_buf,
3319 /* param_len */ param_len,
3320 /* minimum_cmd_size */ softc->minimum_command_size,
3321 /* sense_len */ SSD_FULL_SIZE,
3322 /* timeout */ 50000);
3325 * It would be nice not to have to do this, but there's no
3326 * available pointer in the CCB that would allow us to stuff the
3327 * mode params structure in there and retrieve it in
3328 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3329 * lets the caller know what CDB size we ended up using, so they
3330 * can find the actual mode page offset.
3332 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3334 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3335 /*sense_flags*/SF_RETRY_UA);
3337 xpt_release_ccb(ccb);
3339 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3342 * This is a bit of belt-and-suspenders checking, but if we run
3343 * into a situation where the target sends back multiple block
3344 * descriptors, we might not have enough space in the buffer to
3345 * see the whole mode page. Better to return an error than
3346 * potentially access memory beyond our malloced region.
3351 if (data->cdb_size == 10) {
3352 struct scsi_mode_header_10 *hdr10;
3354 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3355 data_len = scsi_2btoul(hdr10->data_length);
3356 data_len += sizeof(hdr10->data_length);
3358 struct scsi_mode_header_6 *hdr6;
3360 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3361 data_len = hdr6->data_length;
3362 data_len += sizeof(hdr6->data_length);
3366 * Complain if there is more mode data available than we
3367 * allocated space for. This could potentially happen if
3368 * we miscalculated the page length for some reason, if the
3369 * drive returns multiple block descriptors, or if it sets
3370 * the data length incorrectly.
3372 if (data_len > data->alloc_len) {
3373 xpt_print(periph->path, "allocated modepage %d length "
3374 "%d < returned length %d\n", page, data->alloc_len,
3383 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3384 * routine. See comments in cd6byteworkaround() for details.
3387 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3389 struct ccb_scsiio *csio;
3390 struct cd_softc *softc;
3392 int cdb_size, param_len;
3395 softc = (struct cd_softc *)periph->softc;
3397 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3404 * If the data is formatted for the 10 byte version of the mode
3405 * select parameter list, we need to use the 10 byte CDB.
3406 * Otherwise, we use whatever the stored minimum command size.
3408 if (data->cdb_size == 10)
3409 cdb_size = data->cdb_size;
3411 cdb_size = softc->minimum_command_size;
3413 if (cdb_size >= 10) {
3414 struct scsi_mode_header_10 *mode_header;
3417 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3419 data_len = scsi_2btoul(mode_header->data_length);
3421 scsi_ulto2b(0, mode_header->data_length);
3423 * SONY drives do not allow a mode select with a medium_type
3424 * value that has just been returned by a mode sense; use a
3425 * medium_type of 0 (Default) instead.
3427 mode_header->medium_type = 0;
3430 * Pass back whatever the drive passed to us, plus the size
3431 * of the data length field.
3433 param_len = data_len + sizeof(mode_header->data_length);
3436 struct scsi_mode_header_6 *mode_header;
3438 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3440 param_len = mode_header->data_length + 1;
3442 mode_header->data_length = 0;
3444 * SONY drives do not allow a mode select with a medium_type
3445 * value that has just been returned by a mode sense; use a
3446 * medium_type of 0 (Default) instead.
3448 mode_header->medium_type = 0;
3451 /* Don't say we've got more room than we actually allocated */
3452 param_len = min(param_len, data->alloc_len);
3454 scsi_mode_select_len(csio,
3455 /* retries */ cd_retry_count,
3456 /* cbfcnp */ cddone,
3457 /* tag_action */ MSG_SIMPLE_Q_TAG,
3458 /* scsi_page_fmt */ 1,
3460 /* param_buf */ data->mode_buf,
3461 /* param_len */ param_len,
3462 /* minimum_cmd_size */ cdb_size,
3463 /* sense_len */ SSD_FULL_SIZE,
3464 /* timeout */ 50000);
3466 /* See comments in cdgetmode() and cd6byteworkaround(). */
3467 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3469 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3470 /*sense_flags*/SF_RETRY_UA);
3472 xpt_release_ccb(ccb);
3474 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3481 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3483 struct ccb_scsiio *csio;
3489 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3492 * Use the smallest possible command to perform the operation.
3494 if ((len & 0xffff0000) == 0) {
3496 * We can fit in a 10 byte cdb.
3498 struct scsi_play_10 *scsi_cmd;
3500 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3501 bzero (scsi_cmd, sizeof(*scsi_cmd));
3502 scsi_cmd->op_code = PLAY_10;
3503 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3504 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3505 cdb_len = sizeof(*scsi_cmd);
3507 struct scsi_play_12 *scsi_cmd;
3509 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3510 bzero (scsi_cmd, sizeof(*scsi_cmd));
3511 scsi_cmd->op_code = PLAY_12;
3512 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3513 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3514 cdb_len = sizeof(*scsi_cmd);
3517 /*retries*/ cd_retry_count,
3519 /*flags*/CAM_DIR_NONE,
3523 /*sense_len*/SSD_FULL_SIZE,
3525 /*timeout*/50 * 1000);
3527 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3528 /*sense_flags*/SF_RETRY_UA);
3530 xpt_release_ccb(ccb);
3536 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3537 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3539 struct scsi_play_msf *scsi_cmd;
3540 struct ccb_scsiio *csio;
3546 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3551 /* retries */ cd_retry_count,
3552 /* cbfcnp */ cddone,
3553 /* flags */ CAM_DIR_NONE,
3554 /* tag_action */ MSG_SIMPLE_Q_TAG,
3555 /* data_ptr */ NULL,
3557 /* sense_len */ SSD_FULL_SIZE,
3558 sizeof(struct scsi_play_msf),
3559 /* timeout */ 50000);
3561 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3562 bzero (scsi_cmd, sizeof(*scsi_cmd));
3564 scsi_cmd->op_code = PLAY_MSF;
3565 scsi_cmd->start_m = startm;
3566 scsi_cmd->start_s = starts;
3567 scsi_cmd->start_f = startf;
3568 scsi_cmd->end_m = endm;
3569 scsi_cmd->end_s = ends;
3570 scsi_cmd->end_f = endf;
3572 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3573 /*sense_flags*/SF_RETRY_UA);
3575 xpt_release_ccb(ccb);
3582 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3583 u_int32_t etrack, u_int32_t eindex)
3585 struct scsi_play_track *scsi_cmd;
3586 struct ccb_scsiio *csio;
3592 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3597 /* retries */ cd_retry_count,
3598 /* cbfcnp */ cddone,
3599 /* flags */ CAM_DIR_NONE,
3600 /* tag_action */ MSG_SIMPLE_Q_TAG,
3601 /* data_ptr */ NULL,
3603 /* sense_len */ SSD_FULL_SIZE,
3604 sizeof(struct scsi_play_track),
3605 /* timeout */ 50000);
3607 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3608 bzero (scsi_cmd, sizeof(*scsi_cmd));
3610 scsi_cmd->op_code = PLAY_TRACK;
3611 scsi_cmd->start_track = strack;
3612 scsi_cmd->start_index = sindex;
3613 scsi_cmd->end_track = etrack;
3614 scsi_cmd->end_index = eindex;
3616 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3617 /*sense_flags*/SF_RETRY_UA);
3619 xpt_release_ccb(ccb);
3625 cdpause(struct cam_periph *periph, u_int32_t go)
3627 struct scsi_pause *scsi_cmd;
3628 struct ccb_scsiio *csio;
3634 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3639 /* retries */ cd_retry_count,
3640 /* cbfcnp */ cddone,
3641 /* flags */ CAM_DIR_NONE,
3642 /* tag_action */ MSG_SIMPLE_Q_TAG,
3643 /* data_ptr */ NULL,
3645 /* sense_len */ SSD_FULL_SIZE,
3646 sizeof(struct scsi_pause),
3647 /* timeout */ 50000);
3649 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3650 bzero (scsi_cmd, sizeof(*scsi_cmd));
3652 scsi_cmd->op_code = PAUSE;
3653 scsi_cmd->resume = go;
3655 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3656 /*sense_flags*/SF_RETRY_UA);
3658 xpt_release_ccb(ccb);
3664 cdstartunit(struct cam_periph *periph, int load)
3671 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3673 scsi_start_stop(&ccb->csio,
3674 /* retries */ cd_retry_count,
3675 /* cbfcnp */ cddone,
3676 /* tag_action */ MSG_SIMPLE_Q_TAG,
3678 /* load_eject */ load,
3679 /* immediate */ FALSE,
3680 /* sense_len */ SSD_FULL_SIZE,
3681 /* timeout */ 50000);
3683 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3684 /*sense_flags*/SF_RETRY_UA);
3686 xpt_release_ccb(ccb);
3692 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3699 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3701 scsi_start_stop(&ccb->csio,
3702 /* retries */ cd_retry_count,
3703 /* cbfcnp */ cddone,
3704 /* tag_action */ MSG_SIMPLE_Q_TAG,
3706 /* load_eject */ eject,
3707 /* immediate */ FALSE,
3708 /* sense_len */ SSD_FULL_SIZE,
3709 /* timeout */ 50000);
3711 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3712 /*sense_flags*/SF_RETRY_UA);
3714 xpt_release_ccb(ccb);
3720 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3722 struct scsi_set_speed *scsi_cmd;
3723 struct ccb_scsiio *csio;
3728 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3731 /* Preserve old behavior: units in multiples of CDROM speed */
3738 /* retries */ cd_retry_count,
3739 /* cbfcnp */ cddone,
3740 /* flags */ CAM_DIR_NONE,
3741 /* tag_action */ MSG_SIMPLE_Q_TAG,
3742 /* data_ptr */ NULL,
3744 /* sense_len */ SSD_FULL_SIZE,
3745 sizeof(struct scsi_set_speed),
3746 /* timeout */ 50000);
3748 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3749 bzero(scsi_cmd, sizeof(*scsi_cmd));
3751 scsi_cmd->opcode = SET_CD_SPEED;
3752 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3753 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3755 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3756 /*sense_flags*/SF_RETRY_UA);
3758 xpt_release_ccb(ccb);
3764 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3776 switch (authinfo->format) {
3777 case DVD_REPORT_AGID:
3778 length = sizeof(struct scsi_report_key_data_agid);
3780 case DVD_REPORT_CHALLENGE:
3781 length = sizeof(struct scsi_report_key_data_challenge);
3783 case DVD_REPORT_KEY1:
3784 length = sizeof(struct scsi_report_key_data_key1_key2);
3786 case DVD_REPORT_TITLE_KEY:
3787 length = sizeof(struct scsi_report_key_data_title);
3788 /* The lba field is only set for the title key */
3789 lba = authinfo->lba;
3791 case DVD_REPORT_ASF:
3792 length = sizeof(struct scsi_report_key_data_asf);
3794 case DVD_REPORT_RPC:
3795 length = sizeof(struct scsi_report_key_data_rpc);
3797 case DVD_INVALIDATE_AGID:
3805 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3809 cam_periph_lock(periph);
3810 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3812 scsi_report_key(&ccb->csio,
3813 /* retries */ cd_retry_count,
3814 /* cbfcnp */ cddone,
3815 /* tag_action */ MSG_SIMPLE_Q_TAG,
3817 /* agid */ authinfo->agid,
3818 /* key_format */ authinfo->format,
3819 /* data_ptr */ databuf,
3820 /* dxfer_len */ length,
3821 /* sense_len */ SSD_FULL_SIZE,
3822 /* timeout */ 50000);
3824 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3825 /*sense_flags*/SF_RETRY_UA);
3830 if (ccb->csio.resid != 0) {
3831 xpt_print(periph->path, "warning, residual for report key "
3832 "command is %d\n", ccb->csio.resid);
3835 switch(authinfo->format) {
3836 case DVD_REPORT_AGID: {
3837 struct scsi_report_key_data_agid *agid_data;
3839 agid_data = (struct scsi_report_key_data_agid *)databuf;
3841 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3845 case DVD_REPORT_CHALLENGE: {
3846 struct scsi_report_key_data_challenge *chal_data;
3848 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3850 bcopy(chal_data->challenge_key, authinfo->keychal,
3851 min(sizeof(chal_data->challenge_key),
3852 sizeof(authinfo->keychal)));
3855 case DVD_REPORT_KEY1: {
3856 struct scsi_report_key_data_key1_key2 *key1_data;
3858 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3860 bcopy(key1_data->key1, authinfo->keychal,
3861 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3864 case DVD_REPORT_TITLE_KEY: {
3865 struct scsi_report_key_data_title *title_data;
3867 title_data = (struct scsi_report_key_data_title *)databuf;
3869 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3870 RKD_TITLE_CPM_SHIFT;
3871 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3872 RKD_TITLE_CP_SEC_SHIFT;
3873 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3874 RKD_TITLE_CMGS_SHIFT;
3875 bcopy(title_data->title_key, authinfo->keychal,
3876 min(sizeof(title_data->title_key),
3877 sizeof(authinfo->keychal)));
3880 case DVD_REPORT_ASF: {
3881 struct scsi_report_key_data_asf *asf_data;
3883 asf_data = (struct scsi_report_key_data_asf *)databuf;
3885 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3888 case DVD_REPORT_RPC: {
3889 struct scsi_report_key_data_rpc *rpc_data;
3891 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3893 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3895 authinfo->vend_rsts =
3896 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3897 RKD_RPC_VENDOR_RESET_SHIFT;
3898 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3899 authinfo->region = rpc_data->region_mask;
3900 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3903 case DVD_INVALIDATE_AGID:
3906 /* This should be impossible, since we checked above */
3909 break; /* NOTREACHED */
3913 xpt_release_ccb(ccb);
3914 cam_periph_unlock(periph);
3916 if (databuf != NULL)
3917 free(databuf, M_DEVBUF);
3923 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3933 switch(authinfo->format) {
3934 case DVD_SEND_CHALLENGE: {
3935 struct scsi_report_key_data_challenge *challenge_data;
3937 length = sizeof(*challenge_data);
3939 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3941 databuf = (u_int8_t *)challenge_data;
3943 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3944 challenge_data->data_len);
3946 bcopy(authinfo->keychal, challenge_data->challenge_key,
3947 min(sizeof(authinfo->keychal),
3948 sizeof(challenge_data->challenge_key)));
3951 case DVD_SEND_KEY2: {
3952 struct scsi_report_key_data_key1_key2 *key2_data;
3954 length = sizeof(*key2_data);
3956 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3958 databuf = (u_int8_t *)key2_data;
3960 scsi_ulto2b(length - sizeof(key2_data->data_len),
3961 key2_data->data_len);
3963 bcopy(authinfo->keychal, key2_data->key1,
3964 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3968 case DVD_SEND_RPC: {
3969 struct scsi_send_key_data_rpc *rpc_data;
3971 length = sizeof(*rpc_data);
3973 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3975 databuf = (u_int8_t *)rpc_data;
3977 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3978 rpc_data->data_len);
3980 rpc_data->region_code = authinfo->region;
3987 cam_periph_lock(periph);
3988 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3990 scsi_send_key(&ccb->csio,
3991 /* retries */ cd_retry_count,
3992 /* cbfcnp */ cddone,
3993 /* tag_action */ MSG_SIMPLE_Q_TAG,
3994 /* agid */ authinfo->agid,
3995 /* key_format */ authinfo->format,
3996 /* data_ptr */ databuf,
3997 /* dxfer_len */ length,
3998 /* sense_len */ SSD_FULL_SIZE,
3999 /* timeout */ 50000);
4001 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4002 /*sense_flags*/SF_RETRY_UA);
4004 xpt_release_ccb(ccb);
4005 cam_periph_unlock(periph);
4007 if (databuf != NULL)
4008 free(databuf, M_DEVBUF);
4014 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4024 /* The address is reserved for many of the formats */
4027 switch(dvdstruct->format) {
4028 case DVD_STRUCT_PHYSICAL:
4029 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4031 case DVD_STRUCT_COPYRIGHT:
4032 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4034 case DVD_STRUCT_DISCKEY:
4035 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4037 case DVD_STRUCT_BCA:
4038 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4040 case DVD_STRUCT_MANUFACT:
4041 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4043 case DVD_STRUCT_CMI:
4045 case DVD_STRUCT_PROTDISCID:
4046 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4048 case DVD_STRUCT_DISCKEYBLOCK:
4049 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4051 case DVD_STRUCT_DDS:
4052 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4054 case DVD_STRUCT_MEDIUM_STAT:
4055 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4057 case DVD_STRUCT_SPARE_AREA:
4058 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4060 case DVD_STRUCT_RMD_LAST:
4062 case DVD_STRUCT_RMD_RMA:
4064 case DVD_STRUCT_PRERECORDED:
4065 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4067 case DVD_STRUCT_UNIQUEID:
4068 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4070 case DVD_STRUCT_DCB:
4072 case DVD_STRUCT_LIST:
4074 * This is the maximum allocation length for the READ DVD
4075 * STRUCTURE command. There's nothing in the MMC3 spec
4076 * that indicates a limit in the amount of data that can
4077 * be returned from this call, other than the limits
4078 * imposed by the 2-byte length variables.
4087 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4091 cam_periph_lock(periph);
4092 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4094 scsi_read_dvd_structure(&ccb->csio,
4095 /* retries */ cd_retry_count,
4096 /* cbfcnp */ cddone,
4097 /* tag_action */ MSG_SIMPLE_Q_TAG,
4099 /* layer_number */ dvdstruct->layer_num,
4100 /* key_format */ dvdstruct->format,
4101 /* agid */ dvdstruct->agid,
4102 /* data_ptr */ databuf,
4103 /* dxfer_len */ length,
4104 /* sense_len */ SSD_FULL_SIZE,
4105 /* timeout */ 50000);
4107 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4108 /*sense_flags*/SF_RETRY_UA);
4113 switch(dvdstruct->format) {
4114 case DVD_STRUCT_PHYSICAL: {
4115 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4116 struct dvd_layer *outlayer;
4117 struct scsi_read_dvd_struct_data_physical *phys_data;
4120 (struct scsi_read_dvd_struct_data_physical *)databuf;
4121 inlayer = &phys_data->layer_desc;
4122 outlayer = (struct dvd_layer *)&dvdstruct->data;
4124 dvdstruct->length = sizeof(*inlayer);
4126 outlayer->book_type = (inlayer->book_type_version &
4127 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4128 outlayer->book_version = (inlayer->book_type_version &
4129 RDSD_BOOK_VERSION_MASK);
4130 outlayer->disc_size = (inlayer->disc_size_max_rate &
4131 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4132 outlayer->max_rate = (inlayer->disc_size_max_rate &
4133 RDSD_MAX_RATE_MASK);
4134 outlayer->nlayers = (inlayer->layer_info &
4135 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4136 outlayer->track_path = (inlayer->layer_info &
4137 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4138 outlayer->layer_type = (inlayer->layer_info &
4139 RDSD_LAYER_TYPE_MASK);
4140 outlayer->linear_density = (inlayer->density &
4141 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4142 outlayer->track_density = (inlayer->density &
4143 RDSD_TRACK_DENSITY_MASK);
4144 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4146 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4147 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4148 outlayer->end_sector_l0 =
4149 scsi_3btoul(inlayer->end_sector_layer0);
4152 case DVD_STRUCT_COPYRIGHT: {
4153 struct scsi_read_dvd_struct_data_copyright *copy_data;
4155 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4158 dvdstruct->cpst = copy_data->cps_type;
4159 dvdstruct->rmi = copy_data->region_info;
4160 dvdstruct->length = 0;
4166 * Tell the user what the overall length is, no matter
4167 * what we can actually fit in the data buffer.
4169 dvdstruct->length = length - ccb->csio.resid -
4170 sizeof(struct scsi_read_dvd_struct_data_header);
4173 * But only actually copy out the smaller of what we read
4174 * in or what the structure can take.
4176 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4178 min(sizeof(dvdstruct->data), dvdstruct->length));
4183 xpt_release_ccb(ccb);
4184 cam_periph_unlock(periph);
4186 if (databuf != NULL)
4187 free(databuf, M_DEVBUF);
4193 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4194 void (*cbfcnp)(struct cam_periph *, union ccb *),
4195 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4196 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4197 u_int8_t sense_len, u_int32_t timeout)
4199 struct scsi_report_key *scsi_cmd;
4201 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4202 bzero(scsi_cmd, sizeof(*scsi_cmd));
4203 scsi_cmd->opcode = REPORT_KEY;
4204 scsi_ulto4b(lba, scsi_cmd->lba);
4205 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4206 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4207 (key_format & RK_KF_KEYFORMAT_MASK);
4212 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4214 /*data_ptr*/ data_ptr,
4215 /*dxfer_len*/ dxfer_len,
4222 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4223 void (*cbfcnp)(struct cam_periph *, union ccb *),
4224 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4225 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4228 struct scsi_send_key *scsi_cmd;
4230 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4231 bzero(scsi_cmd, sizeof(*scsi_cmd));
4232 scsi_cmd->opcode = SEND_KEY;
4234 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4235 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4236 (key_format & RK_KF_KEYFORMAT_MASK);
4241 /*flags*/ CAM_DIR_OUT,
4243 /*data_ptr*/ data_ptr,
4244 /*dxfer_len*/ dxfer_len,
4252 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4253 void (*cbfcnp)(struct cam_periph *, union ccb *),
4254 u_int8_t tag_action, u_int32_t address,
4255 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4256 u_int8_t *data_ptr, u_int32_t dxfer_len,
4257 u_int8_t sense_len, u_int32_t timeout)
4259 struct scsi_read_dvd_structure *scsi_cmd;
4261 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4262 bzero(scsi_cmd, sizeof(*scsi_cmd));
4263 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4265 scsi_ulto4b(address, scsi_cmd->address);
4266 scsi_cmd->layer_number = layer_number;
4267 scsi_cmd->format = format;
4268 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4269 /* The AGID is the top two bits of this byte */
4270 scsi_cmd->agid = agid << 6;
4275 /*flags*/ CAM_DIR_IN,
4277 /*data_ptr*/ data_ptr,
4278 /*dxfer_len*/ dxfer_len,