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
111 CD_CCB_BUFFER_IO = 0x02,
112 CD_CCB_WAITING = 0x03,
113 CD_CCB_TYPE_MASK = 0x0F,
114 CD_CCB_RETRY_UA = 0x10
118 CHANGER_TIMEOUT_SCHED = 0x01,
119 CHANGER_SHORT_TMOUT_SCHED = 0x02,
120 CHANGER_MANUAL_CALL = 0x04,
121 CHANGER_NEED_TIMEOUT = 0x08
124 #define ccb_state ppriv_field0
125 #define ccb_bp ppriv_ptr1
128 struct ioc_toc_header header;
129 struct cd_toc_entry entries[100];
132 struct cd_toc_single {
133 struct ioc_toc_header header;
134 struct cd_toc_entry entry;
145 volatile cd_flags flags;
146 struct bio_queue_head bio_queue;
147 LIST_HEAD(, ccb_hdr) pending_ccbs;
148 struct cd_params params;
151 STAILQ_ENTRY(cd_softc) changer_links;
152 struct cdchanger *changer;
154 struct cam_periph *periph;
155 int minimum_command_size;
156 int outstanding_cmds;
157 struct task sysctl_task;
158 struct sysctl_ctx_list sysctl_ctx;
159 struct sysctl_oid *sysctl_tree;
160 STAILQ_HEAD(, cd_mode_params) mode_queue;
161 struct cd_tocdata toc;
165 struct cd_page_sizes {
170 static struct cd_page_sizes cd_page_size_table[] =
172 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
175 struct cd_quirk_entry {
176 struct scsi_inquiry_pattern inq_pat;
181 * The changer quirk entries aren't strictly necessary. Basically, what
182 * they do is tell cdregister() up front that a device is a changer.
183 * Otherwise, it will figure that fact out once it sees a LUN on the device
184 * that is greater than 0. If it is known up front that a device is a changer,
185 * all I/O to the device will go through the changer scheduling routines, as
186 * opposed to the "normal" CD code.
188 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
189 * your device hangs when it gets a 10 byte command. Adding a quirk just
190 * to get rid of the informative diagnostic message is not acceptable. All
191 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
192 * referenced in a comment along with the quirk) , and must be approved by
193 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
194 * be removed until the submitter can explain why they are needed.
195 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
196 * when the CAM_NEW_TRAN_CODE work is done.
198 static struct cd_quirk_entry cd_quirk_table[] =
201 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
202 /*quirks*/ CD_Q_CHANGER
205 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
206 "*"}, /* quirks */ CD_Q_CHANGER
209 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
210 /* quirks */ CD_Q_CHANGER
213 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
214 /* quirks */ CD_Q_BCD_TRACKS
218 static disk_open_t cdopen;
219 static disk_close_t cdclose;
220 static disk_ioctl_t cdioctl;
221 static disk_strategy_t cdstrategy;
223 static periph_init_t cdinit;
224 static periph_ctor_t cdregister;
225 static periph_dtor_t cdcleanup;
226 static periph_start_t cdstart;
227 static periph_oninv_t cdoninvalidate;
228 static void cdasync(void *callback_arg, u_int32_t code,
229 struct cam_path *path, void *arg);
230 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
231 static void cdshorttimeout(void *arg);
232 static void cdschedule(struct cam_periph *periph, int priority);
233 static void cdrunchangerqueue(void *arg);
234 static void cdchangerschedule(struct cd_softc *softc);
235 static int cdrunccb(union ccb *ccb,
236 int (*error_routine)(union ccb *ccb,
238 u_int32_t sense_flags),
239 u_int32_t cam_flags, u_int32_t sense_flags);
240 static union ccb *cdgetccb(struct cam_periph *periph,
242 static void cddone(struct cam_periph *periph,
243 union ccb *start_ccb);
244 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
245 static int cdgetpagesize(int page_num);
246 static void cdprevent(struct cam_periph *periph, int action);
247 static int cdcheckmedia(struct cam_periph *periph);
248 static int cdsize(struct cam_periph *periph, u_int32_t *size);
249 static int cd6byteworkaround(union ccb *ccb);
250 static int cderror(union ccb *ccb, u_int32_t cam_flags,
251 u_int32_t sense_flags);
252 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
253 u_int32_t start, u_int8_t *data,
254 u_int32_t len, u_int32_t sense_flags);
255 static int cdgetmode(struct cam_periph *periph,
256 struct cd_mode_params *data, u_int32_t page);
257 static int cdsetmode(struct cam_periph *periph,
258 struct cd_mode_params *data);
259 static int cdplay(struct cam_periph *periph, u_int32_t blk,
261 static int cdreadsubchannel(struct cam_periph *periph,
262 u_int32_t mode, u_int32_t format,
264 struct cd_sub_channel_info *data,
266 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
267 u_int32_t starts, u_int32_t startf,
268 u_int32_t endm, u_int32_t ends,
270 static int cdplaytracks(struct cam_periph *periph,
271 u_int32_t strack, u_int32_t sindex,
272 u_int32_t etrack, u_int32_t eindex);
273 static int cdpause(struct cam_periph *periph, u_int32_t go);
274 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
275 static int cdstartunit(struct cam_periph *periph, int load);
276 static int cdsetspeed(struct cam_periph *periph,
277 u_int32_t rdspeed, u_int32_t wrspeed);
278 static int cdreportkey(struct cam_periph *periph,
279 struct dvd_authinfo *authinfo);
280 static int cdsendkey(struct cam_periph *periph,
281 struct dvd_authinfo *authinfo);
282 static int cdreaddvdstructure(struct cam_periph *periph,
283 struct dvd_struct *dvdstruct);
285 static struct periph_driver cddriver =
288 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
291 PERIPHDRIVER_DECLARE(cd, cddriver);
293 #ifndef CD_DEFAULT_RETRY
294 #define CD_DEFAULT_RETRY 4
296 #ifndef CHANGER_MIN_BUSY_SECONDS
297 #define CHANGER_MIN_BUSY_SECONDS 5
299 #ifndef CHANGER_MAX_BUSY_SECONDS
300 #define CHANGER_MAX_BUSY_SECONDS 15
303 static int cd_retry_count = CD_DEFAULT_RETRY;
304 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
305 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
307 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
308 static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
310 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
311 &cd_retry_count, 0, "Normal I/O retry count");
312 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
313 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
314 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
315 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
316 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
317 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
318 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
322 target_id_t target_id;
325 struct timeval start_time;
326 struct cd_softc *cur_device;
327 struct callout short_handle;
328 struct callout long_handle;
329 volatile cd_changer_flags flags;
330 STAILQ_ENTRY(cdchanger) changer_links;
331 STAILQ_HEAD(chdevlist, cd_softc) chluns;
334 static struct mtx changerq_mtx;
335 static STAILQ_HEAD(changerlist, cdchanger) changerq;
336 static int num_changers;
338 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
345 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
346 STAILQ_INIT(&changerq);
349 * Install a global async callback. This callback will
350 * receive async callbacks like "new device found".
352 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
354 if (status != CAM_REQ_CMP) {
355 printf("cd: Failed to attach master async callback "
356 "due to status 0x%x!\n", status);
361 * Callback from GEOM, called when it has finished cleaning up its
365 cddiskgonecb(struct disk *dp)
367 struct cam_periph *periph;
369 periph = (struct cam_periph *)dp->d_drv1;
371 cam_periph_release(periph);
375 cdoninvalidate(struct cam_periph *periph)
377 struct cd_softc *softc;
379 softc = (struct cd_softc *)periph->softc;
382 * De-register any async callbacks.
384 xpt_register_async(0, cdasync, periph, periph->path);
386 softc->flags |= CD_FLAG_INVALID;
389 * Return all queued I/O with ENXIO.
390 * XXX Handle any transactions queued to the card
391 * with XPT_ABORT_CCB.
393 bioq_flush(&softc->bio_queue, NULL, ENXIO);
396 * If this device is part of a changer, and it was scheduled
397 * to run, remove it from the run queue since we just nuked
398 * all of its scheduled I/O.
400 if ((softc->flags & CD_FLAG_CHANGER)
401 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
402 camq_remove(&softc->changer->devq, softc->pinfo.index);
404 disk_gone(softc->disk);
405 xpt_print(periph->path, "lost device, %d refs\n", periph->refcount);
409 cdcleanup(struct cam_periph *periph)
411 struct cd_softc *softc;
413 softc = (struct cd_softc *)periph->softc;
415 xpt_print(periph->path, "removing device entry\n");
418 * In the queued, non-active case, the device in question
419 * has already been removed from the changer run queue. Since this
420 * device is active, we need to de-activate it, and schedule
421 * another device to run. (if there is another one to run)
423 if ((softc->flags & CD_FLAG_CHANGER)
424 && (softc->flags & CD_FLAG_ACTIVE)) {
427 * The purpose of the short timeout is soley to determine
428 * whether the current device has finished or not. Well,
429 * since we're removing the active device, we know that it
430 * is finished. So, get rid of the short timeout.
431 * Otherwise, if we're in the time period before the short
432 * timeout fires, and there are no other devices in the
433 * queue to run, there won't be any other device put in the
434 * active slot. i.e., when we call cdrunchangerqueue()
435 * below, it won't do anything. Then, when the short
436 * timeout fires, it'll look at the "current device", which
437 * we are free below, and possibly panic the kernel on a
438 * bogus pointer reference.
440 * The long timeout doesn't really matter, since we
441 * decrement the qfrozen_cnt to indicate that there is
442 * nothing in the active slot now. Therefore, there won't
443 * be any bogus pointer references there.
445 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
446 callout_stop(&softc->changer->short_handle);
447 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
449 softc->changer->devq.qfrozen_cnt[0]--;
450 softc->changer->flags |= CHANGER_MANUAL_CALL;
451 cdrunchangerqueue(softc->changer);
455 * If we're removing the last device on the changer, go ahead and
456 * remove the changer device structure.
458 if ((softc->flags & CD_FLAG_CHANGER)
459 && (--softc->changer->num_devices == 0)) {
462 * Theoretically, there shouldn't be any timeouts left, but
463 * I'm not completely sure that that will be the case. So,
464 * it won't hurt to check and see if there are any left.
466 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
467 callout_stop(&softc->changer->long_handle);
468 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
471 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
472 callout_stop(&softc->changer->short_handle);
473 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
476 mtx_lock(&changerq_mtx);
477 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
480 mtx_unlock(&changerq_mtx);
481 xpt_print(periph->path, "removing changer entry\n");
482 free(softc->changer, M_DEVBUF);
484 cam_periph_unlock(periph);
485 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
486 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
487 xpt_print(periph->path, "can't remove sysctl context\n");
490 disk_destroy(softc->disk);
491 free(softc, M_DEVBUF);
492 cam_periph_lock(periph);
496 cdasync(void *callback_arg, u_int32_t code,
497 struct cam_path *path, void *arg)
499 struct cam_periph *periph;
501 periph = (struct cam_periph *)callback_arg;
503 case AC_FOUND_DEVICE:
505 struct ccb_getdev *cgd;
508 cgd = (struct ccb_getdev *)arg;
512 if (cgd->protocol != PROTO_SCSI)
515 if (SID_TYPE(&cgd->inq_data) != T_CDROM
516 && SID_TYPE(&cgd->inq_data) != T_WORM)
520 * Allocate a peripheral instance for
521 * this device and start the probe
524 status = cam_periph_alloc(cdregister, cdoninvalidate,
526 "cd", CAM_PERIPH_BIO,
527 cgd->ccb_h.path, cdasync,
528 AC_FOUND_DEVICE, cgd);
530 if (status != CAM_REQ_CMP
531 && status != CAM_REQ_INPROG)
532 printf("cdasync: Unable to attach new device "
533 "due to status 0x%x\n", status);
540 struct cd_softc *softc;
541 struct ccb_hdr *ccbh;
543 softc = (struct cd_softc *)periph->softc;
545 * Don't fail on the expected unit attention
548 softc->flags |= CD_FLAG_RETRY_UA;
549 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
550 ccbh->ccb_state |= CD_CCB_RETRY_UA;
554 cam_periph_async(periph, code, path, arg);
560 cdsysctlinit(void *context, int pending)
562 struct cam_periph *periph;
563 struct cd_softc *softc;
564 char tmpstr[80], tmpstr2[80];
566 periph = (struct cam_periph *)context;
567 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
570 softc = (struct cd_softc *)periph->softc;
571 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
572 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
574 sysctl_ctx_init(&softc->sysctl_ctx);
575 softc->flags |= CD_FLAG_SCTX_INIT;
576 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
577 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
578 tmpstr2, CTLFLAG_RD, 0, tmpstr);
580 if (softc->sysctl_tree == NULL) {
581 printf("cdsysctlinit: unable to allocate sysctl tree\n");
582 cam_periph_release(periph);
587 * Now register the sysctl handler, so the user can the value on
590 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
591 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
592 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
595 cam_periph_release(periph);
599 * We have a handler function for this so we can check the values when the
600 * user sets them, instead of every time we look at them.
603 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
607 value = *(int *)arg1;
609 error = sysctl_handle_int(oidp, &value, 0, req);
612 || (req->newptr == NULL))
616 * The only real values we can have here are 6 or 10. I don't
617 * really forsee having 12 be an option at any time in the future.
618 * So if the user sets something less than or equal to 6, we'll set
619 * it to 6. If he sets something greater than 6, we'll set it to 10.
621 * I suppose we could just return an error here for the wrong values,
622 * but I don't think it's necessary to do so, as long as we can
623 * determine the user's intent without too much trouble.
630 *(int *)arg1 = value;
636 cdregister(struct cam_periph *periph, void *arg)
638 struct cd_softc *softc;
639 struct ccb_pathinq cpi;
640 struct ccb_getdev *cgd;
644 cgd = (struct ccb_getdev *)arg;
646 printf("cdregister: no getdev CCB, can't register device\n");
647 return(CAM_REQ_CMP_ERR);
650 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
653 printf("cdregister: Unable to probe new device. "
654 "Unable to allocate softc\n");
655 return(CAM_REQ_CMP_ERR);
658 LIST_INIT(&softc->pending_ccbs);
659 STAILQ_INIT(&softc->mode_queue);
660 softc->state = CD_STATE_PROBE;
661 bioq_init(&softc->bio_queue);
662 if (SID_IS_REMOVABLE(&cgd->inq_data))
663 softc->flags |= CD_FLAG_DISC_REMOVABLE;
665 periph->softc = softc;
666 softc->periph = periph;
669 * See if this device has any quirks.
671 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
672 (caddr_t)cd_quirk_table,
673 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
674 sizeof(*cd_quirk_table), scsi_inquiry_match);
677 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
679 softc->quirks = CD_Q_NONE;
681 /* Check if the SIM does not want 6 byte commands */
682 bzero(&cpi, sizeof(cpi));
683 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
684 cpi.ccb_h.func_code = XPT_PATH_INQ;
685 xpt_action((union ccb *)&cpi);
686 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
687 softc->quirks |= CD_Q_10_BYTE_ONLY;
689 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
691 /* The default is 6 byte commands, unless quirked otherwise */
692 if (softc->quirks & CD_Q_10_BYTE_ONLY)
693 softc->minimum_command_size = 10;
695 softc->minimum_command_size = 6;
698 * Refcount and block open attempts until we are setup
701 (void)cam_periph_hold(periph, PRIBIO);
702 cam_periph_unlock(periph);
704 * Load the user's default, if any.
706 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
707 periph->unit_number);
708 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
710 /* 6 and 10 are the only permissible values here. */
711 if (softc->minimum_command_size < 6)
712 softc->minimum_command_size = 6;
713 else if (softc->minimum_command_size > 6)
714 softc->minimum_command_size = 10;
717 * We need to register the statistics structure for this device,
718 * but we don't have the blocksize yet for it. So, we register
719 * the structure and indicate that we don't have the blocksize
720 * yet. Unlike other SCSI peripheral drivers, we explicitly set
721 * the device type here to be CDROM, rather than just ORing in
722 * the device type. This is because this driver can attach to either
723 * CDROM or WORM devices, and we want this peripheral driver to
724 * show up in the devstat list as a CD peripheral driver, not a
725 * WORM peripheral driver. WORM drives will also have the WORM
726 * driver attached to them.
728 softc->disk = disk_alloc();
729 softc->disk->d_devstat = devstat_new_entry("cd",
730 periph->unit_number, 0,
731 DEVSTAT_BS_UNAVAILABLE,
733 XPORT_DEVSTAT_TYPE(cpi.transport),
734 DEVSTAT_PRIORITY_CD);
735 softc->disk->d_open = cdopen;
736 softc->disk->d_close = cdclose;
737 softc->disk->d_strategy = cdstrategy;
738 softc->disk->d_gone = cddiskgonecb;
739 softc->disk->d_ioctl = cdioctl;
740 softc->disk->d_name = "cd";
741 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
742 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
743 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
744 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
745 cgd->inq_data.product, sizeof(cgd->inq_data.product),
746 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
747 softc->disk->d_unit = periph->unit_number;
748 softc->disk->d_drv1 = periph;
750 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
751 else if (cpi.maxio > MAXPHYS)
752 softc->disk->d_maxsize = MAXPHYS; /* for safety */
754 softc->disk->d_maxsize = cpi.maxio;
755 softc->disk->d_flags = 0;
756 softc->disk->d_hba_vendor = cpi.hba_vendor;
757 softc->disk->d_hba_device = cpi.hba_device;
758 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
759 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
762 * Acquire a reference to the periph before we register with GEOM.
763 * We'll release this reference once GEOM calls us back (via
764 * dadiskgonecb()) telling us that our provider has been freed.
766 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
767 xpt_print(periph->path, "%s: lost periph during "
768 "registration!\n", __func__);
769 cam_periph_lock(periph);
770 return (CAM_REQ_CMP_ERR);
773 disk_create(softc->disk, DISK_VERSION);
774 cam_periph_lock(periph);
777 * Add an async callback so that we get
778 * notified if this device goes away.
780 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
781 cdasync, periph, periph->path);
784 * If the target lun is greater than 0, we most likely have a CD
785 * changer device. Check the quirk entries as well, though, just
786 * in case someone has a CD tower with one lun per drive or
787 * something like that. Also, if we know up front that a
788 * particular device is a changer, we can mark it as such starting
789 * with lun 0, instead of lun 1. It shouldn't be necessary to have
790 * a quirk entry to define something as a changer, however.
792 if (((cgd->ccb_h.target_lun > 0)
793 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
794 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
795 struct cdchanger *nchanger;
796 struct cam_periph *nperiph;
797 struct cam_path *path;
801 /* Set the changer flag in the current device's softc */
802 softc->flags |= CD_FLAG_CHANGER;
805 * Now, look around for an existing changer device with the
806 * same path and target ID as the current device.
808 mtx_lock(&changerq_mtx);
810 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
812 nchanger = STAILQ_NEXT(nchanger, changer_links)){
813 if ((nchanger->path_id == cgd->ccb_h.path_id)
814 && (nchanger->target_id == cgd->ccb_h.target_id)) {
819 mtx_unlock(&changerq_mtx);
822 * If we found a matching entry, just add this device to
823 * the list of devices on this changer.
826 struct chdevlist *chlunhead;
828 chlunhead = &nchanger->chluns;
831 * XXX KDM look at consolidating this code with the
832 * code below in a separate function.
836 * Create a path with lun id 0, and see if we can
837 * find a matching device
839 status = xpt_create_path(&path, /*periph*/ periph,
841 cgd->ccb_h.target_id, 0);
843 if ((status == CAM_REQ_CMP)
844 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
845 struct cd_softc *nsoftc;
847 nsoftc = (struct cd_softc *)nperiph->softc;
849 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
850 nsoftc->flags |= CD_FLAG_CHANGER;
851 nchanger->num_devices++;
852 if (camq_resize(&nchanger->devq,
853 nchanger->num_devices)!=CAM_REQ_CMP){
854 printf("cdregister: "
860 nsoftc->changer = nchanger;
861 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
863 STAILQ_INSERT_TAIL(&nchanger->chluns,
864 nsoftc,changer_links);
867 } else if (status == CAM_REQ_CMP)
870 printf("cdregister: unable to allocate path\n"
871 "cdregister: changer support may be "
875 nchanger->num_devices++;
877 softc->changer = nchanger;
878 softc->pinfo.index = CAM_UNQUEUED_INDEX;
880 if (camq_resize(&nchanger->devq,
881 nchanger->num_devices) != CAM_REQ_CMP) {
882 printf("cdregister: camq_resize "
883 "failed, changer support may "
887 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
890 * In this case, we don't already have an entry for this
891 * particular changer, so we need to create one, add it to
892 * the queue, and queue this device on the list for this
893 * changer. Before we queue this device, however, we need
894 * to search for lun id 0 on this target, and add it to the
895 * queue first, if it exists. (and if it hasn't already
896 * been marked as part of the changer.)
899 nchanger = malloc(sizeof(struct cdchanger),
900 M_DEVBUF, M_NOWAIT | M_ZERO);
901 if (nchanger == NULL) {
902 softc->flags &= ~CD_FLAG_CHANGER;
903 printf("cdregister: unable to malloc "
904 "changer structure\ncdregister: "
905 "changer support disabled\n");
908 * Yes, gotos can be gross but in this case
909 * I think it's justified..
913 if (camq_init(&nchanger->devq, 1) != 0) {
914 softc->flags &= ~CD_FLAG_CHANGER;
915 printf("cdregister: changer support "
920 nchanger->path_id = cgd->ccb_h.path_id;
921 nchanger->target_id = cgd->ccb_h.target_id;
923 /* this is superfluous, but it makes things clearer */
924 nchanger->num_devices = 0;
926 STAILQ_INIT(&nchanger->chluns);
928 callout_init_mtx(&nchanger->long_handle,
929 periph->sim->mtx, 0);
930 callout_init_mtx(&nchanger->short_handle,
931 periph->sim->mtx, 0);
933 mtx_lock(&changerq_mtx);
935 STAILQ_INSERT_TAIL(&changerq, nchanger,
937 mtx_unlock(&changerq_mtx);
940 * Create a path with lun id 0, and see if we can
941 * find a matching device
943 status = xpt_create_path(&path, /*periph*/ periph,
945 cgd->ccb_h.target_id, 0);
948 * If we were able to allocate the path, and if we
949 * find a matching device and it isn't already
950 * marked as part of a changer, then we add it to
951 * the current changer.
953 if ((status == CAM_REQ_CMP)
954 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
955 && ((((struct cd_softc *)periph->softc)->flags &
956 CD_FLAG_CHANGER) == 0)) {
957 struct cd_softc *nsoftc;
959 nsoftc = (struct cd_softc *)nperiph->softc;
961 nsoftc->flags |= CD_FLAG_CHANGER;
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 nsoftc->changer = nchanger;
970 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
972 STAILQ_INSERT_TAIL(&nchanger->chluns,
973 nsoftc, changer_links);
975 } else if (status == CAM_REQ_CMP)
978 printf("cdregister: unable to allocate path\n"
979 "cdregister: changer support may be "
983 softc->changer = nchanger;
984 softc->pinfo.index = CAM_UNQUEUED_INDEX;
985 nchanger->num_devices++;
986 if (camq_resize(&nchanger->devq,
987 nchanger->num_devices) != CAM_REQ_CMP) {
988 printf("cdregister: camq_resize "
989 "failed, changer support may "
992 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
999 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1000 xpt_schedule(periph, CAM_PRIORITY_DEV);
1002 cdschedule(periph, CAM_PRIORITY_DEV);
1004 return(CAM_REQ_CMP);
1008 cdopen(struct disk *dp)
1010 struct cam_periph *periph;
1011 struct cd_softc *softc;
1014 periph = (struct cam_periph *)dp->d_drv1;
1018 softc = (struct cd_softc *)periph->softc;
1020 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1023 cam_periph_lock(periph);
1025 if (softc->flags & CD_FLAG_INVALID) {
1026 cam_periph_release_locked(periph);
1027 cam_periph_unlock(periph);
1031 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1032 cam_periph_release_locked(periph);
1033 cam_periph_unlock(periph);
1037 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1041 * Check for media, and set the appropriate flags. We don't bail
1042 * if we don't have media, but then we don't allow anything but the
1043 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1045 cdcheckmedia(periph);
1047 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1048 cam_periph_unhold(periph);
1050 cam_periph_unlock(periph);
1056 cdclose(struct disk *dp)
1058 struct cam_periph *periph;
1059 struct cd_softc *softc;
1061 periph = (struct cam_periph *)dp->d_drv1;
1065 softc = (struct cd_softc *)periph->softc;
1067 cam_periph_lock(periph);
1068 if (cam_periph_hold(periph, PRIBIO) != 0) {
1069 cam_periph_unlock(periph);
1070 cam_periph_release(periph);
1074 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1077 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1078 cdprevent(periph, PR_ALLOW);
1081 * Since we're closing this CD, mark the blocksize as unavailable.
1082 * It will be marked as available when the CD is opened again.
1084 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1087 * We'll check the media and toc again at the next open().
1089 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1091 cam_periph_unhold(periph);
1092 cam_periph_release_locked(periph);
1093 cam_periph_unlock(periph);
1099 cdshorttimeout(void *arg)
1101 struct cdchanger *changer;
1103 changer = (struct cdchanger *)arg;
1105 /* Always clear the short timeout flag, since that's what we're in */
1106 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1109 * Check to see if there is any more pending or outstanding I/O for
1110 * this device. If not, move it out of the active slot.
1112 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1113 && (changer->cur_device->outstanding_cmds == 0)) {
1114 changer->flags |= CHANGER_MANUAL_CALL;
1115 cdrunchangerqueue(changer);
1120 * This is a wrapper for xpt_schedule. It only applies to changers.
1123 cdschedule(struct cam_periph *periph, int priority)
1125 struct cd_softc *softc;
1127 softc = (struct cd_softc *)periph->softc;
1130 * If this device isn't currently queued, and if it isn't
1131 * the active device, then we queue this device and run the
1132 * changer queue if there is no timeout scheduled to do it.
1133 * If this device is the active device, just schedule it
1134 * to run again. If this device is queued, there should be
1135 * a timeout in place already that will make sure it runs.
1137 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1138 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1140 * We don't do anything with the priority here.
1141 * This is strictly a fifo queue.
1143 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1144 softc->pinfo.generation = ++softc->changer->devq.generation;
1145 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1148 * Since we just put a device in the changer queue,
1149 * check and see if there is a timeout scheduled for
1150 * this changer. If so, let the timeout handle
1151 * switching this device into the active slot. If
1152 * not, manually call the timeout routine to
1155 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1156 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1157 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1158 softc->changer->flags |= CHANGER_MANUAL_CALL;
1159 cdrunchangerqueue(softc->changer);
1161 } else if ((softc->flags & CD_FLAG_ACTIVE)
1162 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1163 xpt_schedule(periph, priority);
1167 cdrunchangerqueue(void *arg)
1169 struct cd_softc *softc;
1170 struct cdchanger *changer;
1171 int called_from_timeout;
1173 changer = (struct cdchanger *)arg;
1176 * If we have NOT been called from cdstrategy() or cddone(), and
1177 * instead from a timeout routine, go ahead and clear the
1180 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1181 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1182 called_from_timeout = 1;
1184 called_from_timeout = 0;
1186 /* Always clear the manual call flag */
1187 changer->flags &= ~CHANGER_MANUAL_CALL;
1189 /* nothing to do if the queue is empty */
1190 if (changer->devq.entries <= 0) {
1195 * If the changer queue is frozen, that means we have an active
1198 if (changer->devq.qfrozen_cnt[0] > 0) {
1201 * We always need to reset the frozen count and clear the
1204 changer->devq.qfrozen_cnt[0]--;
1205 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1206 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1208 if (changer->cur_device->outstanding_cmds > 0) {
1209 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1210 changer->cur_device->bufs_left =
1211 changer->cur_device->outstanding_cmds;
1212 if (called_from_timeout) {
1213 callout_reset(&changer->long_handle,
1214 changer_max_busy_seconds * hz,
1215 cdrunchangerqueue, changer);
1216 changer->flags |= CHANGER_TIMEOUT_SCHED;
1222 * Check to see whether the current device has any I/O left
1223 * to do. If so, requeue it at the end of the queue. If
1224 * not, there is no need to requeue it.
1226 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1228 changer->cur_device->pinfo.generation =
1229 ++changer->devq.generation;
1230 camq_insert(&changer->devq,
1231 (cam_pinfo *)changer->cur_device);
1235 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1237 changer->cur_device = softc;
1239 changer->devq.qfrozen_cnt[0]++;
1240 softc->flags |= CD_FLAG_ACTIVE;
1242 /* Just in case this device is waiting */
1243 wakeup(&softc->changer);
1244 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1247 * Get rid of any pending timeouts, and set a flag to schedule new
1248 * ones so this device gets its full time quantum.
1250 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1251 callout_stop(&changer->long_handle);
1252 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1255 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1256 callout_stop(&changer->short_handle);
1257 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1261 * We need to schedule timeouts, but we only do this after the
1262 * first transaction has completed. This eliminates the changer
1265 changer->flags |= CHANGER_NEED_TIMEOUT;
1269 cdchangerschedule(struct cd_softc *softc)
1271 struct cdchanger *changer;
1273 changer = softc->changer;
1276 * If this is a changer, and this is the current device,
1277 * and this device has at least the minimum time quantum to
1278 * run, see if we can switch it out.
1280 if ((softc->flags & CD_FLAG_ACTIVE)
1281 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1282 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1284 * We try three things here. The first is that we
1285 * check to see whether the schedule on completion
1286 * flag is set. If it is, we decrement the number
1287 * of buffers left, and if it's zero, we reschedule.
1288 * Next, we check to see whether the pending buffer
1289 * queue is empty and whether there are no
1290 * outstanding transactions. If so, we reschedule.
1291 * Next, we see if the pending buffer queue is empty.
1292 * If it is, we set the number of buffers left to
1293 * the current active buffer count and set the
1294 * schedule on complete flag.
1296 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1297 if (--softc->bufs_left == 0) {
1298 softc->changer->flags |=
1299 CHANGER_MANUAL_CALL;
1300 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1301 cdrunchangerqueue(softc->changer);
1303 } else if ((bioq_first(&softc->bio_queue) == NULL)
1304 && (softc->outstanding_cmds == 0)) {
1305 softc->changer->flags |= CHANGER_MANUAL_CALL;
1306 cdrunchangerqueue(softc->changer);
1308 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1309 && (softc->flags & CD_FLAG_ACTIVE)) {
1312 * Now that the first transaction to this
1313 * particular device has completed, we can go ahead
1314 * and schedule our timeouts.
1316 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1317 callout_reset(&changer->long_handle,
1318 changer_max_busy_seconds * hz,
1319 cdrunchangerqueue, changer);
1320 changer->flags |= CHANGER_TIMEOUT_SCHED;
1322 printf("cdchangerschedule: already have a long"
1325 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1326 callout_reset(&changer->short_handle,
1327 changer_min_busy_seconds * hz,
1328 cdshorttimeout, changer);
1329 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1331 printf("cdchangerschedule: already have a short "
1335 * We just scheduled timeouts, no need to schedule
1338 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1344 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1345 u_int32_t cam_flags,
1346 u_int32_t sense_flags),
1347 u_int32_t cam_flags, u_int32_t sense_flags)
1349 struct cd_softc *softc;
1350 struct cam_periph *periph;
1353 periph = xpt_path_periph(ccb->ccb_h.path);
1354 softc = (struct cd_softc *)periph->softc;
1356 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1357 softc->disk->d_devstat);
1359 if (softc->flags & CD_FLAG_CHANGER)
1360 cdchangerschedule(softc);
1366 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1368 struct cd_softc *softc;
1370 softc = (struct cd_softc *)periph->softc;
1372 if (softc->flags & CD_FLAG_CHANGER) {
1374 * This should work the first time this device is woken up,
1375 * but just in case it doesn't, we use a while loop.
1377 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1379 * If this changer isn't already queued, queue it up.
1381 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1382 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1383 softc->pinfo.generation =
1384 ++softc->changer->devq.generation;
1385 camq_insert(&softc->changer->devq,
1386 (cam_pinfo *)softc);
1388 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1389 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1390 && ((softc->changer->flags
1391 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1392 softc->changer->flags |= CHANGER_MANUAL_CALL;
1393 cdrunchangerqueue(softc->changer);
1395 msleep(&softc->changer, periph->sim->mtx,
1396 PRIBIO, "cgticb", 0);
1399 return(cam_periph_getccb(periph, priority));
1404 * Actually translate the requested transfer into one the physical driver
1405 * can understand. The transfer is described by a buf and will include
1406 * only one physical transfer.
1409 cdstrategy(struct bio *bp)
1411 struct cam_periph *periph;
1412 struct cd_softc *softc;
1414 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1415 if (periph == NULL) {
1416 biofinish(bp, NULL, ENXIO);
1420 cam_periph_lock(periph);
1421 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1422 ("cdstrategy(%p)\n", bp));
1424 softc = (struct cd_softc *)periph->softc;
1427 * If the device has been made invalid, error out
1429 if ((softc->flags & CD_FLAG_INVALID)) {
1430 cam_periph_unlock(periph);
1431 biofinish(bp, NULL, ENXIO);
1436 * If we don't have valid media, look for it before trying to
1439 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1442 error = cdcheckmedia(periph);
1444 cam_periph_unlock(periph);
1445 biofinish(bp, NULL, error);
1451 * Place it in the queue of disk activities for this disk
1453 bioq_disksort(&softc->bio_queue, bp);
1456 * Schedule ourselves for performing the work. We do things
1457 * differently for changers.
1459 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1460 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1462 cdschedule(periph, CAM_PRIORITY_NORMAL);
1464 cam_periph_unlock(periph);
1469 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1471 struct cd_softc *softc;
1473 struct ccb_scsiio *csio;
1474 struct scsi_read_capacity_data *rcap;
1476 softc = (struct cd_softc *)periph->softc;
1478 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1480 switch (softc->state) {
1481 case CD_STATE_NORMAL:
1483 bp = bioq_first(&softc->bio_queue);
1484 if (periph->immediate_priority <= periph->pinfo.priority) {
1485 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1487 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1489 periph->immediate_priority = CAM_PRIORITY_NONE;
1490 wakeup(&periph->ccb_list);
1491 } else if (bp == NULL) {
1492 xpt_release_ccb(start_ccb);
1494 bioq_remove(&softc->bio_queue, bp);
1496 scsi_read_write(&start_ccb->csio,
1497 /*retries*/ cd_retry_count,
1498 /* cbfcnp */ cddone,
1500 /* read */bp->bio_cmd == BIO_READ,
1502 /* minimum_cmd_size */ 10,
1503 /* lba */ bp->bio_offset /
1504 softc->params.blksize,
1505 bp->bio_bcount / softc->params.blksize,
1506 /* data_ptr */ bp->bio_data,
1507 /* dxfer_len */ bp->bio_bcount,
1508 /* sense_len */ SSD_FULL_SIZE,
1509 /* timeout */ 30000);
1510 /* Use READ CD command for audio tracks. */
1511 if (softc->params.blksize == 2352) {
1512 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1513 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1514 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1515 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1516 start_ccb->csio.cdb_len = 12;
1518 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1521 LIST_INSERT_HEAD(&softc->pending_ccbs,
1522 &start_ccb->ccb_h, periph_links.le);
1523 softc->outstanding_cmds++;
1525 /* We expect a unit attention from this device */
1526 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1527 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1528 softc->flags &= ~CD_FLAG_RETRY_UA;
1531 start_ccb->ccb_h.ccb_bp = bp;
1532 bp = bioq_first(&softc->bio_queue);
1534 xpt_action(start_ccb);
1537 /* Have more work to do, so ensure we stay scheduled */
1538 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1542 case CD_STATE_PROBE:
1545 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1546 M_SCSICD, M_NOWAIT | M_ZERO);
1548 xpt_print(periph->path,
1549 "cdstart: Couldn't malloc read_capacity data\n");
1550 /* cd_free_periph??? */
1553 csio = &start_ccb->csio;
1554 scsi_read_capacity(csio,
1555 /*retries*/ cd_retry_count,
1561 start_ccb->ccb_h.ccb_bp = NULL;
1562 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1563 xpt_action(start_ccb);
1570 cddone(struct cam_periph *periph, union ccb *done_ccb)
1572 struct cd_softc *softc;
1573 struct ccb_scsiio *csio;
1575 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1577 softc = (struct cd_softc *)periph->softc;
1578 csio = &done_ccb->csio;
1580 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1581 case CD_CCB_BUFFER_IO:
1586 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1589 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1592 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1597 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1598 if (error == ERESTART) {
1600 * A retry was scheuled, so
1608 xpt_print(periph->path,
1609 "cddone: got error %#x back\n", error);
1610 bioq_flush(&softc->bio_queue, NULL, EIO);
1611 bp->bio_resid = bp->bio_bcount;
1612 bp->bio_error = error;
1613 bp->bio_flags |= BIO_ERROR;
1614 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1615 cam_release_devq(done_ccb->ccb_h.path,
1619 /*getcount_only*/0);
1622 bp->bio_resid = csio->resid;
1624 if (bp->bio_resid != 0) {
1626 * Short transfer ???
1627 * XXX: not sure this is correct for partial
1630 bp->bio_flags |= BIO_ERROR;
1634 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1635 softc->outstanding_cmds--;
1637 if (softc->flags & CD_FLAG_CHANGER)
1638 cdchangerschedule(softc);
1640 biofinish(bp, NULL, 0);
1645 struct scsi_read_capacity_data *rdcap;
1646 char announce_buf[120]; /*
1647 * Currently (9/30/97) the
1648 * longest possible announce
1649 * buffer is 108 bytes, for the
1650 * first error case below.
1651 * That is 39 bytes for the
1652 * basic string, 16 bytes for the
1653 * biggest sense key (hardware
1654 * error), 52 bytes for the
1655 * text of the largest sense
1656 * qualifier valid for a CDROM,
1657 * (0x72, 0x03 or 0x04,
1658 * 0x03), and one byte for the
1659 * null terminating character.
1660 * To allow for longer strings,
1661 * the announce buffer is 120
1664 struct cd_params *cdp;
1667 cdp = &softc->params;
1669 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1671 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1672 cdp->blksize = scsi_4btoul (rdcap->length);
1675 * Retry any UNIT ATTENTION type errors. They
1676 * are expected at boot.
1678 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1679 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1680 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1682 snprintf(announce_buf, sizeof(announce_buf),
1683 "cd present [%lu x %lu byte records]",
1684 cdp->disksize, (u_long)cdp->blksize);
1687 if (error == ERESTART) {
1689 * A retry was scheuled, so
1694 struct scsi_sense_data *sense;
1696 int sense_key, error_code;
1699 struct ccb_getdev cgd;
1701 /* Don't wedge this device's queue */
1702 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1703 cam_release_devq(done_ccb->ccb_h.path,
1707 /*getcount_only*/0);
1709 status = done_ccb->ccb_h.status;
1711 xpt_setup_ccb(&cgd.ccb_h,
1712 done_ccb->ccb_h.path,
1713 CAM_PRIORITY_NORMAL);
1714 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1715 xpt_action((union ccb *)&cgd);
1717 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1718 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1719 || ((status & CAM_AUTOSNS_VALID) == 0))
1725 sense = &csio->sense_data;
1726 scsi_extract_sense_len(sense,
1727 csio->sense_len - csio->sense_resid,
1728 &error_code, &sense_key, &asc,
1729 &ascq, /*show_errors*/ 1);
1732 * Attach to anything that claims to be a
1733 * CDROM or WORM device, as long as it
1734 * doesn't return a "Logical unit not
1735 * supported" (0x25) error.
1737 if ((have_sense) && (asc != 0x25)
1738 && (error_code == SSD_CURRENT_ERROR)) {
1739 const char *sense_key_desc;
1740 const char *asc_desc;
1742 scsi_sense_desc(sense_key, asc, ascq,
1746 snprintf(announce_buf,
1747 sizeof(announce_buf),
1748 "Attempt to query device "
1749 "size failed: %s, %s",
1752 } else if ((have_sense == 0)
1753 && ((status & CAM_STATUS_MASK) ==
1754 CAM_SCSI_STATUS_ERROR)
1755 && (csio->scsi_status ==
1756 SCSI_STATUS_BUSY)) {
1757 snprintf(announce_buf,
1758 sizeof(announce_buf),
1759 "Attempt to query device "
1760 "size failed: SCSI Status: %s",
1761 scsi_status_string(csio));
1762 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1764 * We only print out an error for
1765 * CDROM type devices. For WORM
1766 * devices, we don't print out an
1767 * error since a few WORM devices
1768 * don't support CDROM commands.
1769 * If we have sense information, go
1770 * ahead and print it out.
1771 * Otherwise, just say that we
1776 * Just print out the error, not
1777 * the full probe message, when we
1784 xpt_print(periph->path,
1785 "got CAM status %#x\n",
1786 done_ccb->ccb_h.status);
1788 xpt_print(periph->path, "fatal error, "
1789 "failed to attach to device\n");
1791 * Invalidate this peripheral.
1793 cam_periph_invalidate(periph);
1795 announce_buf[0] = '\0';
1799 * Invalidate this peripheral.
1801 cam_periph_invalidate(periph);
1802 announce_buf[0] = '\0';
1806 free(rdcap, M_SCSICD);
1807 if (announce_buf[0] != '\0') {
1808 xpt_announce_periph(periph, announce_buf);
1809 if (softc->flags & CD_FLAG_CHANGER)
1810 cdchangerschedule(softc);
1812 * Create our sysctl variables, now that we know
1813 * we have successfully attached.
1815 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1817 softc->state = CD_STATE_NORMAL;
1819 * Since our peripheral may be invalidated by an error
1820 * above or an external event, we must release our CCB
1821 * before releasing the probe lock on the peripheral.
1822 * The peripheral will only go away once the last lock
1823 * is removed, and we need it around for the CCB release
1826 xpt_release_ccb(done_ccb);
1827 cam_periph_unhold(periph);
1830 case CD_CCB_WAITING:
1832 /* Caller will release the CCB */
1833 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1834 ("trying to wakeup ccbwait\n"));
1836 wakeup(&done_ccb->ccb_h.cbfcnp);
1842 xpt_release_ccb(done_ccb);
1845 static union cd_pages *
1846 cdgetpage(struct cd_mode_params *mode_params)
1848 union cd_pages *page;
1850 if (mode_params->cdb_size == 10)
1851 page = (union cd_pages *)find_mode_page_10(
1852 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1854 page = (union cd_pages *)find_mode_page_6(
1855 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1861 cdgetpagesize(int page_num)
1865 for (i = 0; i < (sizeof(cd_page_size_table)/
1866 sizeof(cd_page_size_table[0])); i++) {
1867 if (cd_page_size_table[i].page == page_num)
1868 return (cd_page_size_table[i].page_size);
1875 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1878 struct cam_periph *periph;
1879 struct cd_softc *softc;
1880 int nocopyout, error = 0;
1882 periph = (struct cam_periph *)dp->d_drv1;
1886 cam_periph_lock(periph);
1888 softc = (struct cd_softc *)periph->softc;
1890 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1891 ("cdioctl(%#lx)\n", cmd));
1893 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1894 cam_periph_unlock(periph);
1895 cam_periph_release(periph);
1900 * If we don't have media loaded, check for it. If still don't
1901 * have media loaded, we can only do a load or eject.
1903 * We only care whether media is loaded if this is a cd-specific ioctl
1904 * (thus the IOCGROUP check below). Note that this will break if
1905 * anyone adds any ioctls into the switch statement below that don't
1906 * have their ioctl group set to 'c'.
1908 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1909 && ((cmd != CDIOCCLOSE)
1910 && (cmd != CDIOCEJECT))
1911 && (IOCGROUP(cmd) == 'c')) {
1912 error = cdcheckmedia(periph);
1914 cam_periph_unhold(periph);
1915 cam_periph_unlock(periph);
1920 * Drop the lock here so later mallocs can use WAITOK. The periph
1921 * is essentially locked still with the cam_periph_hold call above.
1923 cam_periph_unlock(periph);
1928 case CDIOCPLAYTRACKS:
1930 struct ioc_play_track *args
1931 = (struct ioc_play_track *) addr;
1932 struct cd_mode_params params;
1933 union cd_pages *page;
1935 params.alloc_len = sizeof(union cd_mode_data_6_10);
1936 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1939 cam_periph_lock(periph);
1940 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1941 ("trying to do CDIOCPLAYTRACKS\n"));
1943 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1945 free(params.mode_buf, M_SCSICD);
1946 cam_periph_unlock(periph);
1949 page = cdgetpage(¶ms);
1951 page->audio.flags &= ~CD_PA_SOTC;
1952 page->audio.flags |= CD_PA_IMMED;
1953 error = cdsetmode(periph, ¶ms);
1954 free(params.mode_buf, M_SCSICD);
1956 cam_periph_unlock(periph);
1961 * This was originally implemented with the PLAY
1962 * AUDIO TRACK INDEX command, but that command was
1963 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1964 * drives support it but ATAPI and ATAPI-derivative
1965 * drives don't seem to support it. So we keep a
1966 * cache of the table of contents and translate
1967 * track numbers to MSF format.
1969 if (softc->flags & CD_FLAG_VALID_TOC) {
1970 union msf_lba *sentry, *eentry;
1973 if (args->end_track <
1974 softc->toc.header.ending_track + 1)
1976 if (args->end_track >
1977 softc->toc.header.ending_track + 1)
1979 softc->toc.header.ending_track + 1;
1980 st = args->start_track -
1981 softc->toc.header.starting_track;
1982 et = args->end_track -
1983 softc->toc.header.starting_track;
1986 || (st > (softc->toc.header.ending_track -
1987 softc->toc.header.starting_track))) {
1989 cam_periph_unlock(periph);
1992 sentry = &softc->toc.entries[st].addr;
1993 eentry = &softc->toc.entries[et].addr;
1994 error = cdplaymsf(periph,
2003 * If we don't have a valid TOC, try the
2004 * play track index command. It is part of
2005 * the SCSI-2 spec, but was removed in the
2006 * MMC specs. ATAPI and ATAPI-derived
2007 * drives don't support it.
2009 if (softc->quirks & CD_Q_BCD_TRACKS) {
2011 bin2bcd(args->start_track);
2013 bin2bcd(args->end_track);
2015 error = cdplaytracks(periph,
2021 cam_periph_unlock(periph);
2026 struct ioc_play_msf *args
2027 = (struct ioc_play_msf *) addr;
2028 struct cd_mode_params params;
2029 union cd_pages *page;
2031 params.alloc_len = sizeof(union cd_mode_data_6_10);
2032 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2035 cam_periph_lock(periph);
2036 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2037 ("trying to do CDIOCPLAYMSF\n"));
2039 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2041 free(params.mode_buf, M_SCSICD);
2042 cam_periph_unlock(periph);
2045 page = cdgetpage(¶ms);
2047 page->audio.flags &= ~CD_PA_SOTC;
2048 page->audio.flags |= CD_PA_IMMED;
2049 error = cdsetmode(periph, ¶ms);
2050 free(params.mode_buf, M_SCSICD);
2052 cam_periph_unlock(periph);
2055 error = cdplaymsf(periph,
2062 cam_periph_unlock(periph);
2065 case CDIOCPLAYBLOCKS:
2067 struct ioc_play_blocks *args
2068 = (struct ioc_play_blocks *) addr;
2069 struct cd_mode_params params;
2070 union cd_pages *page;
2072 params.alloc_len = sizeof(union cd_mode_data_6_10);
2073 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2076 cam_periph_lock(periph);
2077 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2078 ("trying to do CDIOCPLAYBLOCKS\n"));
2081 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2083 free(params.mode_buf, M_SCSICD);
2084 cam_periph_unlock(periph);
2087 page = cdgetpage(¶ms);
2089 page->audio.flags &= ~CD_PA_SOTC;
2090 page->audio.flags |= CD_PA_IMMED;
2091 error = cdsetmode(periph, ¶ms);
2092 free(params.mode_buf, M_SCSICD);
2094 cam_periph_unlock(periph);
2097 error = cdplay(periph, args->blk, args->len);
2098 cam_periph_unlock(periph);
2101 case CDIOCREADSUBCHANNEL_SYSSPACE:
2104 case CDIOCREADSUBCHANNEL:
2106 struct ioc_read_subchannel *args
2107 = (struct ioc_read_subchannel *) addr;
2108 struct cd_sub_channel_info *data;
2109 u_int32_t len = args->data_len;
2111 data = malloc(sizeof(struct cd_sub_channel_info),
2112 M_SCSICD, M_WAITOK | M_ZERO);
2114 cam_periph_lock(periph);
2115 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2116 ("trying to do CDIOCREADSUBCHANNEL\n"));
2118 if ((len > sizeof(struct cd_sub_channel_info)) ||
2119 (len < sizeof(struct cd_sub_channel_header))) {
2121 "scsi_cd: cdioctl: "
2122 "cdioreadsubchannel: error, len=%d\n",
2125 free(data, M_SCSICD);
2126 cam_periph_unlock(periph);
2130 if (softc->quirks & CD_Q_BCD_TRACKS)
2131 args->track = bin2bcd(args->track);
2133 error = cdreadsubchannel(periph, args->address_format,
2134 args->data_format, args->track, data, len);
2137 free(data, M_SCSICD);
2138 cam_periph_unlock(periph);
2141 if (softc->quirks & CD_Q_BCD_TRACKS)
2142 data->what.track_info.track_number =
2143 bcd2bin(data->what.track_info.track_number);
2144 len = min(len, ((data->header.data_len[0] << 8) +
2145 data->header.data_len[1] +
2146 sizeof(struct cd_sub_channel_header)));
2147 cam_periph_unlock(periph);
2148 if (nocopyout == 0) {
2149 if (copyout(data, args->data, len) != 0) {
2153 bcopy(data, args->data, len);
2155 free(data, M_SCSICD);
2159 case CDIOREADTOCHEADER:
2161 struct ioc_toc_header *th;
2163 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2166 cam_periph_lock(periph);
2167 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2168 ("trying to do CDIOREADTOCHEADER\n"));
2170 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2171 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2174 cam_periph_unlock(periph);
2177 if (softc->quirks & CD_Q_BCD_TRACKS) {
2178 /* we are going to have to convert the BCD
2179 * encoding on the cd to what is expected
2181 th->starting_track =
2182 bcd2bin(th->starting_track);
2183 th->ending_track = bcd2bin(th->ending_track);
2185 th->len = ntohs(th->len);
2186 bcopy(th, addr, sizeof(*th));
2188 cam_periph_unlock(periph);
2191 case CDIOREADTOCENTRYS:
2193 struct cd_tocdata *data;
2194 struct cd_toc_single *lead;
2195 struct ioc_read_toc_entry *te =
2196 (struct ioc_read_toc_entry *) addr;
2197 struct ioc_toc_header *th;
2198 u_int32_t len, readlen, idx, num;
2199 u_int32_t starting_track = te->starting_track;
2201 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2202 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2204 cam_periph_lock(periph);
2205 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2206 ("trying to do CDIOREADTOCENTRYS\n"));
2208 if (te->data_len < sizeof(struct cd_toc_entry)
2209 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2210 || (te->address_format != CD_MSF_FORMAT
2211 && te->address_format != CD_LBA_FORMAT)) {
2213 printf("scsi_cd: error in readtocentries, "
2214 "returning EINVAL\n");
2215 free(data, M_SCSICD);
2216 free(lead, M_SCSICD);
2217 cam_periph_unlock(periph);
2222 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2223 sizeof (*th), /*sense_flags*/0);
2225 free(data, M_SCSICD);
2226 free(lead, M_SCSICD);
2227 cam_periph_unlock(periph);
2231 if (softc->quirks & CD_Q_BCD_TRACKS) {
2232 /* we are going to have to convert the BCD
2233 * encoding on the cd to what is expected
2235 th->starting_track =
2236 bcd2bin(th->starting_track);
2237 th->ending_track = bcd2bin(th->ending_track);
2240 if (starting_track == 0)
2241 starting_track = th->starting_track;
2242 else if (starting_track == LEADOUT)
2243 starting_track = th->ending_track + 1;
2244 else if (starting_track < th->starting_track ||
2245 starting_track > th->ending_track + 1) {
2246 printf("scsi_cd: error in readtocentries, "
2247 "returning EINVAL\n");
2248 free(data, M_SCSICD);
2249 free(lead, M_SCSICD);
2250 cam_periph_unlock(periph);
2255 /* calculate reading length without leadout entry */
2256 readlen = (th->ending_track - starting_track + 1) *
2257 sizeof(struct cd_toc_entry);
2259 /* and with leadout entry */
2260 len = readlen + sizeof(struct cd_toc_entry);
2261 if (te->data_len < len) {
2266 if (len > sizeof(data->entries)) {
2267 printf("scsi_cd: error in readtocentries, "
2268 "returning EINVAL\n");
2270 free(data, M_SCSICD);
2271 free(lead, M_SCSICD);
2272 cam_periph_unlock(periph);
2275 num = len / sizeof(struct cd_toc_entry);
2278 error = cdreadtoc(periph, te->address_format,
2281 readlen + sizeof (*th),
2284 free(data, M_SCSICD);
2285 free(lead, M_SCSICD);
2286 cam_periph_unlock(periph);
2291 /* make leadout entry if needed */
2292 idx = starting_track + num - 1;
2293 if (softc->quirks & CD_Q_BCD_TRACKS)
2294 th->ending_track = bcd2bin(th->ending_track);
2295 if (idx == th->ending_track + 1) {
2296 error = cdreadtoc(periph, te->address_format,
2297 LEADOUT, (u_int8_t *)lead,
2301 free(data, M_SCSICD);
2302 free(lead, M_SCSICD);
2303 cam_periph_unlock(periph);
2306 data->entries[idx - starting_track] =
2309 if (softc->quirks & CD_Q_BCD_TRACKS) {
2310 for (idx = 0; idx < num - 1; idx++) {
2311 data->entries[idx].track =
2312 bcd2bin(data->entries[idx].track);
2316 cam_periph_unlock(periph);
2317 error = copyout(data->entries, te->data, len);
2318 free(data, M_SCSICD);
2319 free(lead, M_SCSICD);
2322 case CDIOREADTOCENTRY:
2324 struct cd_toc_single *data;
2325 struct ioc_read_toc_single_entry *te =
2326 (struct ioc_read_toc_single_entry *) addr;
2327 struct ioc_toc_header *th;
2330 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2332 cam_periph_lock(periph);
2333 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2334 ("trying to do CDIOREADTOCENTRY\n"));
2336 if (te->address_format != CD_MSF_FORMAT
2337 && te->address_format != CD_LBA_FORMAT) {
2338 printf("error in readtocentry, "
2339 " returning EINVAL\n");
2340 free(data, M_SCSICD);
2342 cam_periph_unlock(periph);
2347 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2348 sizeof (*th), /*sense_flags*/0);
2350 free(data, M_SCSICD);
2351 cam_periph_unlock(periph);
2355 if (softc->quirks & CD_Q_BCD_TRACKS) {
2356 /* we are going to have to convert the BCD
2357 * encoding on the cd to what is expected
2359 th->starting_track =
2360 bcd2bin(th->starting_track);
2361 th->ending_track = bcd2bin(th->ending_track);
2365 track = th->starting_track;
2366 else if (track == LEADOUT)
2368 else if (track < th->starting_track ||
2369 track > th->ending_track + 1) {
2370 printf("error in readtocentry, "
2371 " returning EINVAL\n");
2372 free(data, M_SCSICD);
2374 cam_periph_unlock(periph);
2378 error = cdreadtoc(periph, te->address_format, track,
2379 (u_int8_t *)data, sizeof(*data),
2382 free(data, M_SCSICD);
2383 cam_periph_unlock(periph);
2387 if (softc->quirks & CD_Q_BCD_TRACKS)
2388 data->entry.track = bcd2bin(data->entry.track);
2389 bcopy(&data->entry, &te->entry,
2390 sizeof(struct cd_toc_entry));
2391 free(data, M_SCSICD);
2392 cam_periph_unlock(periph);
2397 struct ioc_patch *arg = (struct ioc_patch *)addr;
2398 struct cd_mode_params params;
2399 union cd_pages *page;
2401 params.alloc_len = sizeof(union cd_mode_data_6_10);
2402 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2405 cam_periph_lock(periph);
2406 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2407 ("trying to do CDIOCSETPATCH\n"));
2409 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2411 free(params.mode_buf, M_SCSICD);
2412 cam_periph_unlock(periph);
2415 page = cdgetpage(¶ms);
2417 page->audio.port[LEFT_PORT].channels =
2419 page->audio.port[RIGHT_PORT].channels =
2421 page->audio.port[2].channels = arg->patch[2];
2422 page->audio.port[3].channels = arg->patch[3];
2423 error = cdsetmode(periph, ¶ms);
2424 free(params.mode_buf, M_SCSICD);
2425 cam_periph_unlock(periph);
2430 struct ioc_vol *arg = (struct ioc_vol *) addr;
2431 struct cd_mode_params params;
2432 union cd_pages *page;
2434 params.alloc_len = sizeof(union cd_mode_data_6_10);
2435 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2438 cam_periph_lock(periph);
2439 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2440 ("trying to do CDIOCGETVOL\n"));
2442 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2444 free(params.mode_buf, M_SCSICD);
2445 cam_periph_unlock(periph);
2448 page = cdgetpage(¶ms);
2450 arg->vol[LEFT_PORT] =
2451 page->audio.port[LEFT_PORT].volume;
2452 arg->vol[RIGHT_PORT] =
2453 page->audio.port[RIGHT_PORT].volume;
2454 arg->vol[2] = page->audio.port[2].volume;
2455 arg->vol[3] = page->audio.port[3].volume;
2456 free(params.mode_buf, M_SCSICD);
2457 cam_periph_unlock(periph);
2462 struct ioc_vol *arg = (struct ioc_vol *) addr;
2463 struct cd_mode_params params;
2464 union cd_pages *page;
2466 params.alloc_len = sizeof(union cd_mode_data_6_10);
2467 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2470 cam_periph_lock(periph);
2471 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2472 ("trying to do CDIOCSETVOL\n"));
2474 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2476 free(params.mode_buf, M_SCSICD);
2477 cam_periph_unlock(periph);
2480 page = cdgetpage(¶ms);
2482 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2483 page->audio.port[LEFT_PORT].volume =
2484 arg->vol[LEFT_PORT];
2485 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2486 page->audio.port[RIGHT_PORT].volume =
2487 arg->vol[RIGHT_PORT];
2488 page->audio.port[2].volume = arg->vol[2];
2489 page->audio.port[3].volume = arg->vol[3];
2490 error = cdsetmode(periph, ¶ms);
2491 cam_periph_unlock(periph);
2492 free(params.mode_buf, M_SCSICD);
2497 struct cd_mode_params params;
2498 union cd_pages *page;
2500 params.alloc_len = sizeof(union cd_mode_data_6_10);
2501 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2504 cam_periph_lock(periph);
2505 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2506 ("trying to do CDIOCSETMONO\n"));
2508 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2510 free(params.mode_buf, M_SCSICD);
2511 cam_periph_unlock(periph);
2514 page = cdgetpage(¶ms);
2516 page->audio.port[LEFT_PORT].channels =
2517 LEFT_CHANNEL | RIGHT_CHANNEL;
2518 page->audio.port[RIGHT_PORT].channels =
2519 LEFT_CHANNEL | RIGHT_CHANNEL;
2520 page->audio.port[2].channels = 0;
2521 page->audio.port[3].channels = 0;
2522 error = cdsetmode(periph, ¶ms);
2523 cam_periph_unlock(periph);
2524 free(params.mode_buf, M_SCSICD);
2527 case CDIOCSETSTEREO:
2529 struct cd_mode_params params;
2530 union cd_pages *page;
2532 params.alloc_len = sizeof(union cd_mode_data_6_10);
2533 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2536 cam_periph_lock(periph);
2537 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2538 ("trying to do CDIOCSETSTEREO\n"));
2540 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2542 free(params.mode_buf, M_SCSICD);
2543 cam_periph_unlock(periph);
2546 page = cdgetpage(¶ms);
2548 page->audio.port[LEFT_PORT].channels =
2550 page->audio.port[RIGHT_PORT].channels =
2552 page->audio.port[2].channels = 0;
2553 page->audio.port[3].channels = 0;
2554 error = cdsetmode(periph, ¶ms);
2555 free(params.mode_buf, M_SCSICD);
2556 cam_periph_unlock(periph);
2561 struct cd_mode_params params;
2562 union cd_pages *page;
2564 params.alloc_len = sizeof(union cd_mode_data_6_10);
2565 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2568 cam_periph_lock(periph);
2569 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2570 ("trying to do CDIOCSETMUTE\n"));
2572 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2574 free(params.mode_buf, M_SCSICD);
2575 cam_periph_unlock(periph);
2578 page = cdgetpage(¶ms);
2580 page->audio.port[LEFT_PORT].channels = 0;
2581 page->audio.port[RIGHT_PORT].channels = 0;
2582 page->audio.port[2].channels = 0;
2583 page->audio.port[3].channels = 0;
2584 error = cdsetmode(periph, ¶ms);
2585 free(params.mode_buf, M_SCSICD);
2586 cam_periph_unlock(periph);
2591 struct cd_mode_params params;
2592 union cd_pages *page;
2594 params.alloc_len = sizeof(union cd_mode_data_6_10);
2595 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2598 cam_periph_lock(periph);
2599 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2600 ("trying to do CDIOCSETLEFT\n"));
2602 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2604 free(params.mode_buf, M_SCSICD);
2605 cam_periph_unlock(periph);
2608 page = cdgetpage(¶ms);
2610 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2611 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2612 page->audio.port[2].channels = 0;
2613 page->audio.port[3].channels = 0;
2614 error = cdsetmode(periph, ¶ms);
2615 free(params.mode_buf, M_SCSICD);
2616 cam_periph_unlock(periph);
2621 struct cd_mode_params params;
2622 union cd_pages *page;
2624 params.alloc_len = sizeof(union cd_mode_data_6_10);
2625 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2628 cam_periph_lock(periph);
2629 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2630 ("trying to do CDIOCSETRIGHT\n"));
2632 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2634 free(params.mode_buf, M_SCSICD);
2635 cam_periph_unlock(periph);
2638 page = cdgetpage(¶ms);
2640 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2641 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2642 page->audio.port[2].channels = 0;
2643 page->audio.port[3].channels = 0;
2644 error = cdsetmode(periph, ¶ms);
2645 free(params.mode_buf, M_SCSICD);
2646 cam_periph_unlock(periph);
2650 cam_periph_lock(periph);
2651 error = cdpause(periph, 1);
2652 cam_periph_unlock(periph);
2655 cam_periph_lock(periph);
2656 error = cdpause(periph, 0);
2657 cam_periph_unlock(periph);
2660 cam_periph_lock(periph);
2661 error = cdstartunit(periph, 0);
2662 cam_periph_unlock(periph);
2665 cam_periph_lock(periph);
2666 error = cdstartunit(periph, 1);
2667 cam_periph_unlock(periph);
2670 cam_periph_lock(periph);
2671 error = cdstopunit(periph, 0);
2672 cam_periph_unlock(periph);
2675 cam_periph_lock(periph);
2676 error = cdstopunit(periph, 1);
2677 cam_periph_unlock(periph);
2680 cam_periph_lock(periph);
2681 cdprevent(periph, PR_ALLOW);
2682 cam_periph_unlock(periph);
2685 cam_periph_lock(periph);
2686 cdprevent(periph, PR_PREVENT);
2687 cam_periph_unlock(periph);
2690 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2694 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2698 /* return (cd_reset(periph)); */
2701 case CDRIOCREADSPEED:
2702 cam_periph_lock(periph);
2703 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2704 cam_periph_unlock(periph);
2706 case CDRIOCWRITESPEED:
2707 cam_periph_lock(periph);
2708 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2709 cam_periph_unlock(periph);
2711 case CDRIOCGETBLOCKSIZE:
2712 *(int *)addr = softc->params.blksize;
2714 case CDRIOCSETBLOCKSIZE:
2715 if (*(int *)addr <= 0) {
2719 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2722 case DVDIOCREPORTKEY: {
2723 struct dvd_authinfo *authinfo;
2725 authinfo = (struct dvd_authinfo *)addr;
2727 if (cmd == DVDIOCREPORTKEY)
2728 error = cdreportkey(periph, authinfo);
2730 error = cdsendkey(periph, authinfo);
2733 case DVDIOCREADSTRUCTURE: {
2734 struct dvd_struct *dvdstruct;
2736 dvdstruct = (struct dvd_struct *)addr;
2738 error = cdreaddvdstructure(periph, dvdstruct);
2743 cam_periph_lock(periph);
2744 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2745 cam_periph_unlock(periph);
2749 cam_periph_lock(periph);
2750 cam_periph_unhold(periph);
2752 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2753 if (error && bootverbose) {
2754 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2756 cam_periph_unlock(periph);
2762 cdprevent(struct cam_periph *periph, int action)
2765 struct cd_softc *softc;
2768 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2770 softc = (struct cd_softc *)periph->softc;
2772 if (((action == PR_ALLOW)
2773 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2774 || ((action == PR_PREVENT)
2775 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2779 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2781 scsi_prevent(&ccb->csio,
2782 /*retries*/ cd_retry_count,
2787 /* timeout */60000);
2789 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2790 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2792 xpt_release_ccb(ccb);
2795 if (action == PR_ALLOW)
2796 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2798 softc->flags |= CD_FLAG_DISC_LOCKED;
2803 * XXX: the disk media and sector size is only really able to change
2804 * XXX: while the device is closed.
2807 cdcheckmedia(struct cam_periph *periph)
2809 struct cd_softc *softc;
2810 struct ioc_toc_header *toch;
2811 struct cd_toc_single leadout;
2812 u_int32_t size, toclen;
2813 int error, num_entries, cdindex;
2815 softc = (struct cd_softc *)periph->softc;
2817 cdprevent(periph, PR_PREVENT);
2818 softc->disk->d_sectorsize = 2048;
2819 softc->disk->d_mediasize = 0;
2822 * Get the disc size and block size. If we can't get it, we don't
2823 * have media, most likely.
2825 if ((error = cdsize(periph, &size)) != 0) {
2826 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2827 cdprevent(periph, PR_ALLOW);
2830 softc->flags |= CD_FLAG_VALID_MEDIA;
2831 softc->disk->d_sectorsize = softc->params.blksize;
2832 softc->disk->d_mediasize =
2833 (off_t)softc->params.blksize * softc->params.disksize;
2837 * Now we check the table of contents. This (currently) is only
2838 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2839 * things like present a separate entry in /dev for each track,
2840 * like that acd(4) driver does.
2842 bzero(&softc->toc, sizeof(softc->toc));
2843 toch = &softc->toc.header;
2845 * We will get errors here for media that doesn't have a table of
2846 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2847 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2848 * has not been recorded (no complete session) and the Format codes
2849 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2850 * with an INVALID FIELD IN CDB. Devices that are not capable of
2851 * reading an incomplete session on DDC/CD-R/RW media shall report
2852 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2854 * So this isn't fatal if we can't read the table of contents, it
2855 * just means that the user won't be able to issue the play tracks
2856 * ioctl, and likely lots of other stuff won't work either. They
2857 * need to burn the CD before we can do a whole lot with it. So
2858 * we don't print anything here if we get an error back.
2860 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2863 * Errors in reading the table of contents aren't fatal, we just
2864 * won't have a valid table of contents cached.
2868 bzero(&softc->toc, sizeof(softc->toc));
2872 if (softc->quirks & CD_Q_BCD_TRACKS) {
2873 toch->starting_track = bcd2bin(toch->starting_track);
2874 toch->ending_track = bcd2bin(toch->ending_track);
2877 /* Number of TOC entries, plus leadout */
2878 num_entries = (toch->ending_track - toch->starting_track) + 2;
2880 if (num_entries <= 0)
2883 toclen = num_entries * sizeof(struct cd_toc_entry);
2885 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2886 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2890 bzero(&softc->toc, sizeof(softc->toc));
2894 if (softc->quirks & CD_Q_BCD_TRACKS) {
2895 toch->starting_track = bcd2bin(toch->starting_track);
2896 toch->ending_track = bcd2bin(toch->ending_track);
2899 * XXX KDM is this necessary? Probably only if the drive doesn't
2900 * return leadout information with the table of contents.
2902 cdindex = toch->starting_track + num_entries -1;
2903 if (cdindex == toch->ending_track + 1) {
2905 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2906 (u_int8_t *)&leadout, sizeof(leadout),
2912 softc->toc.entries[cdindex - toch->starting_track] =
2915 if (softc->quirks & CD_Q_BCD_TRACKS) {
2916 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2917 softc->toc.entries[cdindex].track =
2918 bcd2bin(softc->toc.entries[cdindex].track);
2922 softc->flags |= CD_FLAG_VALID_TOC;
2924 /* If the first track is audio, correct sector size. */
2925 if ((softc->toc.entries[0].control & 4) == 0) {
2926 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2927 softc->disk->d_mediasize =
2928 (off_t)softc->params.blksize * softc->params.disksize;
2934 * We unconditionally (re)set the blocksize each time the
2935 * CD device is opened. This is because the CD can change,
2936 * and therefore the blocksize might change.
2937 * XXX problems here if some slice or partition is still
2938 * open with the old size?
2940 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2941 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2942 softc->disk->d_devstat->block_size = softc->params.blksize;
2948 cdsize(struct cam_periph *periph, u_int32_t *size)
2950 struct cd_softc *softc;
2952 struct scsi_read_capacity_data *rcap_buf;
2955 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2957 softc = (struct cd_softc *)periph->softc;
2959 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2961 /* XXX Should be M_WAITOK */
2962 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2963 M_SCSICD, M_NOWAIT | M_ZERO);
2964 if (rcap_buf == NULL)
2967 scsi_read_capacity(&ccb->csio,
2968 /*retries*/ cd_retry_count,
2973 /* timeout */20000);
2975 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2976 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2978 xpt_release_ccb(ccb);
2980 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2981 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2982 /* Make sure we got at least some block size. */
2983 if (error == 0 && softc->params.blksize == 0)
2986 * SCSI-3 mandates that the reported blocksize shall be 2048.
2987 * Older drives sometimes report funny values, trim it down to
2988 * 2048, or other parts of the kernel will get confused.
2990 * XXX we leave drives alone that might report 512 bytes, as
2991 * well as drives reporting more weird sizes like perhaps 4K.
2993 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2994 softc->params.blksize = 2048;
2996 free(rcap_buf, M_SCSICD);
2997 *size = softc->params.disksize;
3004 cd6byteworkaround(union ccb *ccb)
3007 struct cam_periph *periph;
3008 struct cd_softc *softc;
3009 struct cd_mode_params *params;
3012 periph = xpt_path_periph(ccb->ccb_h.path);
3013 softc = (struct cd_softc *)periph->softc;
3015 cdb = ccb->csio.cdb_io.cdb_bytes;
3017 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3018 || ((cdb[0] != MODE_SENSE_6)
3019 && (cdb[0] != MODE_SELECT_6)))
3023 * Because there is no convenient place to stash the overall
3024 * cd_mode_params structure pointer, we have to grab it like this.
3025 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3026 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3028 * XXX It would be nice if, at some point, we could increase the
3029 * number of available peripheral private pointers. Both pointers
3030 * are currently used in most every peripheral driver.
3034 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3035 if (params->mode_buf == ccb->csio.data_ptr) {
3042 * This shouldn't happen. All mode sense and mode select
3043 * operations in the cd(4) driver MUST go through cdgetmode() and
3047 xpt_print(periph->path,
3048 "mode buffer not found in mode queue!\n");
3052 params->cdb_size = 10;
3053 softc->minimum_command_size = 10;
3054 xpt_print(ccb->ccb_h.path,
3055 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3056 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3058 if (cdb[0] == MODE_SENSE_6) {
3059 struct scsi_mode_sense_10 ms10;
3060 struct scsi_mode_sense_6 *ms6;
3063 ms6 = (struct scsi_mode_sense_6 *)cdb;
3065 bzero(&ms10, sizeof(ms10));
3066 ms10.opcode = MODE_SENSE_10;
3067 ms10.byte2 = ms6->byte2;
3068 ms10.page = ms6->page;
3071 * 10 byte mode header, block descriptor,
3072 * sizeof(union cd_pages)
3074 len = sizeof(struct cd_mode_data_10);
3075 ccb->csio.dxfer_len = len;
3077 scsi_ulto2b(len, ms10.length);
3078 ms10.control = ms6->control;
3079 bcopy(&ms10, cdb, 10);
3080 ccb->csio.cdb_len = 10;
3082 struct scsi_mode_select_10 ms10;
3083 struct scsi_mode_select_6 *ms6;
3084 struct scsi_mode_header_6 *header6;
3085 struct scsi_mode_header_10 *header10;
3086 struct scsi_mode_page_header *page_header;
3087 int blk_desc_len, page_num, page_size, len;
3089 ms6 = (struct scsi_mode_select_6 *)cdb;
3091 bzero(&ms10, sizeof(ms10));
3092 ms10.opcode = MODE_SELECT_10;
3093 ms10.byte2 = ms6->byte2;
3095 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3096 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3098 page_header = find_mode_page_6(header6);
3099 page_num = page_header->page_code;
3101 blk_desc_len = header6->blk_desc_len;
3103 page_size = cdgetpagesize(page_num);
3105 if (page_size != (page_header->page_length +
3106 sizeof(*page_header)))
3107 page_size = page_header->page_length +
3108 sizeof(*page_header);
3110 len = sizeof(*header10) + blk_desc_len + page_size;
3112 len = min(params->alloc_len, len);
3115 * Since the 6 byte parameter header is shorter than the 10
3116 * byte parameter header, we need to copy the actual mode
3117 * page data, and the block descriptor, if any, so things wind
3118 * up in the right place. The regions will overlap, but
3119 * bcopy() does the right thing.
3121 bcopy(params->mode_buf + sizeof(*header6),
3122 params->mode_buf + sizeof(*header10),
3123 len - sizeof(*header10));
3125 /* Make sure these fields are set correctly. */
3126 scsi_ulto2b(0, header10->data_length);
3127 header10->medium_type = 0;
3128 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3130 ccb->csio.dxfer_len = len;
3132 scsi_ulto2b(len, ms10.length);
3133 ms10.control = ms6->control;
3134 bcopy(&ms10, cdb, 10);
3135 ccb->csio.cdb_len = 10;
3138 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3139 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3142 cam_release_devq(ccb->ccb_h.path,
3146 /*getcount_only*/0);
3153 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3155 struct cd_softc *softc;
3156 struct cam_periph *periph;
3159 periph = xpt_path_periph(ccb->ccb_h.path);
3160 softc = (struct cd_softc *)periph->softc;
3165 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3166 * CDB comes back with this particular error, try transforming it
3167 * into the 10 byte version.
3169 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3170 error = cd6byteworkaround(ccb);
3171 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3172 CAM_SCSI_STATUS_ERROR)
3173 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3174 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3175 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3176 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3177 int sense_key, error_code, asc, ascq;
3179 scsi_extract_sense_len(&ccb->csio.sense_data,
3180 ccb->csio.sense_len - ccb->csio.sense_resid, &error_code,
3181 &sense_key, &asc, &ascq, /*show_errors*/ 1);
3182 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3183 error = cd6byteworkaround(ccb);
3186 if (error == ERESTART)
3191 * Until we have a better way of doing pack validation,
3192 * don't treat UAs as errors.
3194 sense_flags |= SF_RETRY_UA;
3195 return (cam_periph_error(ccb, cam_flags, sense_flags,
3196 &softc->saved_ccb));
3200 * Read table of contents
3203 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3204 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3206 struct scsi_read_toc *scsi_cmd;
3208 struct ccb_scsiio *csio;
3215 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3220 /* retries */ cd_retry_count,
3221 /* cbfcnp */ cddone,
3222 /* flags */ CAM_DIR_IN,
3223 /* tag_action */ MSG_SIMPLE_Q_TAG,
3224 /* data_ptr */ data,
3225 /* dxfer_len */ len,
3226 /* sense_len */ SSD_FULL_SIZE,
3227 sizeof(struct scsi_read_toc),
3228 /* timeout */ 50000);
3230 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3231 bzero (scsi_cmd, sizeof(*scsi_cmd));
3233 if (mode == CD_MSF_FORMAT)
3234 scsi_cmd->byte2 |= CD_MSF;
3235 scsi_cmd->from_track = start;
3236 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3237 scsi_cmd->data_len[0] = (ntoc) >> 8;
3238 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3240 scsi_cmd->op_code = READ_TOC;
3242 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3243 /*sense_flags*/SF_RETRY_UA | sense_flags);
3245 xpt_release_ccb(ccb);
3251 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3252 u_int32_t format, int track,
3253 struct cd_sub_channel_info *data, u_int32_t len)
3255 struct scsi_read_subchannel *scsi_cmd;
3256 struct ccb_scsiio *csio;
3262 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3267 /* retries */ cd_retry_count,
3268 /* cbfcnp */ cddone,
3269 /* flags */ CAM_DIR_IN,
3270 /* tag_action */ MSG_SIMPLE_Q_TAG,
3271 /* data_ptr */ (u_int8_t *)data,
3272 /* dxfer_len */ len,
3273 /* sense_len */ SSD_FULL_SIZE,
3274 sizeof(struct scsi_read_subchannel),
3275 /* timeout */ 50000);
3277 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3278 bzero (scsi_cmd, sizeof(*scsi_cmd));
3280 scsi_cmd->op_code = READ_SUBCHANNEL;
3281 if (mode == CD_MSF_FORMAT)
3282 scsi_cmd->byte1 |= CD_MSF;
3283 scsi_cmd->byte2 = SRS_SUBQ;
3284 scsi_cmd->subchan_format = format;
3285 scsi_cmd->track = track;
3286 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3287 scsi_cmd->control = 0;
3289 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3290 /*sense_flags*/SF_RETRY_UA);
3292 xpt_release_ccb(ccb);
3299 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3300 * routine. See comments in cd6byteworkaround() for details.
3303 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3306 struct ccb_scsiio *csio;
3307 struct cd_softc *softc;
3312 softc = (struct cd_softc *)periph->softc;
3314 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3318 data->cdb_size = softc->minimum_command_size;
3319 if (data->cdb_size < 10)
3320 param_len = sizeof(struct cd_mode_data);
3322 param_len = sizeof(struct cd_mode_data_10);
3324 /* Don't say we've got more room than we actually allocated */
3325 param_len = min(param_len, data->alloc_len);
3327 scsi_mode_sense_len(csio,
3328 /* retries */ cd_retry_count,
3329 /* cbfcnp */ cddone,
3330 /* tag_action */ MSG_SIMPLE_Q_TAG,
3332 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3334 /* param_buf */ data->mode_buf,
3335 /* param_len */ param_len,
3336 /* minimum_cmd_size */ softc->minimum_command_size,
3337 /* sense_len */ SSD_FULL_SIZE,
3338 /* timeout */ 50000);
3341 * It would be nice not to have to do this, but there's no
3342 * available pointer in the CCB that would allow us to stuff the
3343 * mode params structure in there and retrieve it in
3344 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3345 * lets the caller know what CDB size we ended up using, so they
3346 * can find the actual mode page offset.
3348 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3350 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3351 /*sense_flags*/SF_RETRY_UA);
3353 xpt_release_ccb(ccb);
3355 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3358 * This is a bit of belt-and-suspenders checking, but if we run
3359 * into a situation where the target sends back multiple block
3360 * descriptors, we might not have enough space in the buffer to
3361 * see the whole mode page. Better to return an error than
3362 * potentially access memory beyond our malloced region.
3367 if (data->cdb_size == 10) {
3368 struct scsi_mode_header_10 *hdr10;
3370 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3371 data_len = scsi_2btoul(hdr10->data_length);
3372 data_len += sizeof(hdr10->data_length);
3374 struct scsi_mode_header_6 *hdr6;
3376 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3377 data_len = hdr6->data_length;
3378 data_len += sizeof(hdr6->data_length);
3382 * Complain if there is more mode data available than we
3383 * allocated space for. This could potentially happen if
3384 * we miscalculated the page length for some reason, if the
3385 * drive returns multiple block descriptors, or if it sets
3386 * the data length incorrectly.
3388 if (data_len > data->alloc_len) {
3389 xpt_print(periph->path, "allocated modepage %d length "
3390 "%d < returned length %d\n", page, data->alloc_len,
3399 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3400 * routine. See comments in cd6byteworkaround() for details.
3403 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3405 struct ccb_scsiio *csio;
3406 struct cd_softc *softc;
3408 int cdb_size, param_len;
3411 softc = (struct cd_softc *)periph->softc;
3413 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3420 * If the data is formatted for the 10 byte version of the mode
3421 * select parameter list, we need to use the 10 byte CDB.
3422 * Otherwise, we use whatever the stored minimum command size.
3424 if (data->cdb_size == 10)
3425 cdb_size = data->cdb_size;
3427 cdb_size = softc->minimum_command_size;
3429 if (cdb_size >= 10) {
3430 struct scsi_mode_header_10 *mode_header;
3433 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3435 data_len = scsi_2btoul(mode_header->data_length);
3437 scsi_ulto2b(0, mode_header->data_length);
3439 * SONY drives do not allow a mode select with a medium_type
3440 * value that has just been returned by a mode sense; use a
3441 * medium_type of 0 (Default) instead.
3443 mode_header->medium_type = 0;
3446 * Pass back whatever the drive passed to us, plus the size
3447 * of the data length field.
3449 param_len = data_len + sizeof(mode_header->data_length);
3452 struct scsi_mode_header_6 *mode_header;
3454 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3456 param_len = mode_header->data_length + 1;
3458 mode_header->data_length = 0;
3460 * SONY drives do not allow a mode select with a medium_type
3461 * value that has just been returned by a mode sense; use a
3462 * medium_type of 0 (Default) instead.
3464 mode_header->medium_type = 0;
3467 /* Don't say we've got more room than we actually allocated */
3468 param_len = min(param_len, data->alloc_len);
3470 scsi_mode_select_len(csio,
3471 /* retries */ cd_retry_count,
3472 /* cbfcnp */ cddone,
3473 /* tag_action */ MSG_SIMPLE_Q_TAG,
3474 /* scsi_page_fmt */ 1,
3476 /* param_buf */ data->mode_buf,
3477 /* param_len */ param_len,
3478 /* minimum_cmd_size */ cdb_size,
3479 /* sense_len */ SSD_FULL_SIZE,
3480 /* timeout */ 50000);
3482 /* See comments in cdgetmode() and cd6byteworkaround(). */
3483 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3485 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3486 /*sense_flags*/SF_RETRY_UA);
3488 xpt_release_ccb(ccb);
3490 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3497 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3499 struct ccb_scsiio *csio;
3505 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3508 * Use the smallest possible command to perform the operation.
3510 if ((len & 0xffff0000) == 0) {
3512 * We can fit in a 10 byte cdb.
3514 struct scsi_play_10 *scsi_cmd;
3516 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3517 bzero (scsi_cmd, sizeof(*scsi_cmd));
3518 scsi_cmd->op_code = PLAY_10;
3519 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3520 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3521 cdb_len = sizeof(*scsi_cmd);
3523 struct scsi_play_12 *scsi_cmd;
3525 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3526 bzero (scsi_cmd, sizeof(*scsi_cmd));
3527 scsi_cmd->op_code = PLAY_12;
3528 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3529 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3530 cdb_len = sizeof(*scsi_cmd);
3533 /*retries*/ cd_retry_count,
3535 /*flags*/CAM_DIR_NONE,
3539 /*sense_len*/SSD_FULL_SIZE,
3541 /*timeout*/50 * 1000);
3543 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3544 /*sense_flags*/SF_RETRY_UA);
3546 xpt_release_ccb(ccb);
3552 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3553 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3555 struct scsi_play_msf *scsi_cmd;
3556 struct ccb_scsiio *csio;
3562 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3567 /* retries */ cd_retry_count,
3568 /* cbfcnp */ cddone,
3569 /* flags */ CAM_DIR_NONE,
3570 /* tag_action */ MSG_SIMPLE_Q_TAG,
3571 /* data_ptr */ NULL,
3573 /* sense_len */ SSD_FULL_SIZE,
3574 sizeof(struct scsi_play_msf),
3575 /* timeout */ 50000);
3577 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3578 bzero (scsi_cmd, sizeof(*scsi_cmd));
3580 scsi_cmd->op_code = PLAY_MSF;
3581 scsi_cmd->start_m = startm;
3582 scsi_cmd->start_s = starts;
3583 scsi_cmd->start_f = startf;
3584 scsi_cmd->end_m = endm;
3585 scsi_cmd->end_s = ends;
3586 scsi_cmd->end_f = endf;
3588 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3589 /*sense_flags*/SF_RETRY_UA);
3591 xpt_release_ccb(ccb);
3598 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3599 u_int32_t etrack, u_int32_t eindex)
3601 struct scsi_play_track *scsi_cmd;
3602 struct ccb_scsiio *csio;
3608 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3613 /* retries */ cd_retry_count,
3614 /* cbfcnp */ cddone,
3615 /* flags */ CAM_DIR_NONE,
3616 /* tag_action */ MSG_SIMPLE_Q_TAG,
3617 /* data_ptr */ NULL,
3619 /* sense_len */ SSD_FULL_SIZE,
3620 sizeof(struct scsi_play_track),
3621 /* timeout */ 50000);
3623 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3624 bzero (scsi_cmd, sizeof(*scsi_cmd));
3626 scsi_cmd->op_code = PLAY_TRACK;
3627 scsi_cmd->start_track = strack;
3628 scsi_cmd->start_index = sindex;
3629 scsi_cmd->end_track = etrack;
3630 scsi_cmd->end_index = eindex;
3632 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3633 /*sense_flags*/SF_RETRY_UA);
3635 xpt_release_ccb(ccb);
3641 cdpause(struct cam_periph *periph, u_int32_t go)
3643 struct scsi_pause *scsi_cmd;
3644 struct ccb_scsiio *csio;
3650 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3655 /* retries */ cd_retry_count,
3656 /* cbfcnp */ cddone,
3657 /* flags */ CAM_DIR_NONE,
3658 /* tag_action */ MSG_SIMPLE_Q_TAG,
3659 /* data_ptr */ NULL,
3661 /* sense_len */ SSD_FULL_SIZE,
3662 sizeof(struct scsi_pause),
3663 /* timeout */ 50000);
3665 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3666 bzero (scsi_cmd, sizeof(*scsi_cmd));
3668 scsi_cmd->op_code = PAUSE;
3669 scsi_cmd->resume = go;
3671 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3672 /*sense_flags*/SF_RETRY_UA);
3674 xpt_release_ccb(ccb);
3680 cdstartunit(struct cam_periph *periph, int load)
3687 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3689 scsi_start_stop(&ccb->csio,
3690 /* retries */ cd_retry_count,
3691 /* cbfcnp */ cddone,
3692 /* tag_action */ MSG_SIMPLE_Q_TAG,
3694 /* load_eject */ load,
3695 /* immediate */ FALSE,
3696 /* sense_len */ SSD_FULL_SIZE,
3697 /* timeout */ 50000);
3699 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3700 /*sense_flags*/SF_RETRY_UA);
3702 xpt_release_ccb(ccb);
3708 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3715 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3717 scsi_start_stop(&ccb->csio,
3718 /* retries */ cd_retry_count,
3719 /* cbfcnp */ cddone,
3720 /* tag_action */ MSG_SIMPLE_Q_TAG,
3722 /* load_eject */ eject,
3723 /* immediate */ FALSE,
3724 /* sense_len */ SSD_FULL_SIZE,
3725 /* timeout */ 50000);
3727 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3728 /*sense_flags*/SF_RETRY_UA);
3730 xpt_release_ccb(ccb);
3736 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3738 struct scsi_set_speed *scsi_cmd;
3739 struct ccb_scsiio *csio;
3744 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3747 /* Preserve old behavior: units in multiples of CDROM speed */
3754 /* retries */ cd_retry_count,
3755 /* cbfcnp */ cddone,
3756 /* flags */ CAM_DIR_NONE,
3757 /* tag_action */ MSG_SIMPLE_Q_TAG,
3758 /* data_ptr */ NULL,
3760 /* sense_len */ SSD_FULL_SIZE,
3761 sizeof(struct scsi_set_speed),
3762 /* timeout */ 50000);
3764 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3765 bzero(scsi_cmd, sizeof(*scsi_cmd));
3767 scsi_cmd->opcode = SET_CD_SPEED;
3768 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3769 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3771 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3772 /*sense_flags*/SF_RETRY_UA);
3774 xpt_release_ccb(ccb);
3780 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3792 switch (authinfo->format) {
3793 case DVD_REPORT_AGID:
3794 length = sizeof(struct scsi_report_key_data_agid);
3796 case DVD_REPORT_CHALLENGE:
3797 length = sizeof(struct scsi_report_key_data_challenge);
3799 case DVD_REPORT_KEY1:
3800 length = sizeof(struct scsi_report_key_data_key1_key2);
3802 case DVD_REPORT_TITLE_KEY:
3803 length = sizeof(struct scsi_report_key_data_title);
3804 /* The lba field is only set for the title key */
3805 lba = authinfo->lba;
3807 case DVD_REPORT_ASF:
3808 length = sizeof(struct scsi_report_key_data_asf);
3810 case DVD_REPORT_RPC:
3811 length = sizeof(struct scsi_report_key_data_rpc);
3813 case DVD_INVALIDATE_AGID:
3821 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3825 cam_periph_lock(periph);
3826 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3828 scsi_report_key(&ccb->csio,
3829 /* retries */ cd_retry_count,
3830 /* cbfcnp */ cddone,
3831 /* tag_action */ MSG_SIMPLE_Q_TAG,
3833 /* agid */ authinfo->agid,
3834 /* key_format */ authinfo->format,
3835 /* data_ptr */ databuf,
3836 /* dxfer_len */ length,
3837 /* sense_len */ SSD_FULL_SIZE,
3838 /* timeout */ 50000);
3840 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3841 /*sense_flags*/SF_RETRY_UA);
3846 if (ccb->csio.resid != 0) {
3847 xpt_print(periph->path, "warning, residual for report key "
3848 "command is %d\n", ccb->csio.resid);
3851 switch(authinfo->format) {
3852 case DVD_REPORT_AGID: {
3853 struct scsi_report_key_data_agid *agid_data;
3855 agid_data = (struct scsi_report_key_data_agid *)databuf;
3857 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3861 case DVD_REPORT_CHALLENGE: {
3862 struct scsi_report_key_data_challenge *chal_data;
3864 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3866 bcopy(chal_data->challenge_key, authinfo->keychal,
3867 min(sizeof(chal_data->challenge_key),
3868 sizeof(authinfo->keychal)));
3871 case DVD_REPORT_KEY1: {
3872 struct scsi_report_key_data_key1_key2 *key1_data;
3874 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3876 bcopy(key1_data->key1, authinfo->keychal,
3877 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3880 case DVD_REPORT_TITLE_KEY: {
3881 struct scsi_report_key_data_title *title_data;
3883 title_data = (struct scsi_report_key_data_title *)databuf;
3885 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3886 RKD_TITLE_CPM_SHIFT;
3887 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3888 RKD_TITLE_CP_SEC_SHIFT;
3889 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3890 RKD_TITLE_CMGS_SHIFT;
3891 bcopy(title_data->title_key, authinfo->keychal,
3892 min(sizeof(title_data->title_key),
3893 sizeof(authinfo->keychal)));
3896 case DVD_REPORT_ASF: {
3897 struct scsi_report_key_data_asf *asf_data;
3899 asf_data = (struct scsi_report_key_data_asf *)databuf;
3901 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3904 case DVD_REPORT_RPC: {
3905 struct scsi_report_key_data_rpc *rpc_data;
3907 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3909 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3911 authinfo->vend_rsts =
3912 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3913 RKD_RPC_VENDOR_RESET_SHIFT;
3914 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3915 authinfo->region = rpc_data->region_mask;
3916 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3919 case DVD_INVALIDATE_AGID:
3922 /* This should be impossible, since we checked above */
3925 break; /* NOTREACHED */
3929 xpt_release_ccb(ccb);
3930 cam_periph_unlock(periph);
3932 if (databuf != NULL)
3933 free(databuf, M_DEVBUF);
3939 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3949 switch(authinfo->format) {
3950 case DVD_SEND_CHALLENGE: {
3951 struct scsi_report_key_data_challenge *challenge_data;
3953 length = sizeof(*challenge_data);
3955 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3957 databuf = (u_int8_t *)challenge_data;
3959 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3960 challenge_data->data_len);
3962 bcopy(authinfo->keychal, challenge_data->challenge_key,
3963 min(sizeof(authinfo->keychal),
3964 sizeof(challenge_data->challenge_key)));
3967 case DVD_SEND_KEY2: {
3968 struct scsi_report_key_data_key1_key2 *key2_data;
3970 length = sizeof(*key2_data);
3972 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3974 databuf = (u_int8_t *)key2_data;
3976 scsi_ulto2b(length - sizeof(key2_data->data_len),
3977 key2_data->data_len);
3979 bcopy(authinfo->keychal, key2_data->key1,
3980 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3984 case DVD_SEND_RPC: {
3985 struct scsi_send_key_data_rpc *rpc_data;
3987 length = sizeof(*rpc_data);
3989 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3991 databuf = (u_int8_t *)rpc_data;
3993 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3994 rpc_data->data_len);
3996 rpc_data->region_code = authinfo->region;
4003 cam_periph_lock(periph);
4004 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4006 scsi_send_key(&ccb->csio,
4007 /* retries */ cd_retry_count,
4008 /* cbfcnp */ cddone,
4009 /* tag_action */ MSG_SIMPLE_Q_TAG,
4010 /* agid */ authinfo->agid,
4011 /* key_format */ authinfo->format,
4012 /* data_ptr */ databuf,
4013 /* dxfer_len */ length,
4014 /* sense_len */ SSD_FULL_SIZE,
4015 /* timeout */ 50000);
4017 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4018 /*sense_flags*/SF_RETRY_UA);
4020 xpt_release_ccb(ccb);
4021 cam_periph_unlock(periph);
4023 if (databuf != NULL)
4024 free(databuf, M_DEVBUF);
4030 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4040 /* The address is reserved for many of the formats */
4043 switch(dvdstruct->format) {
4044 case DVD_STRUCT_PHYSICAL:
4045 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4047 case DVD_STRUCT_COPYRIGHT:
4048 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4050 case DVD_STRUCT_DISCKEY:
4051 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4053 case DVD_STRUCT_BCA:
4054 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4056 case DVD_STRUCT_MANUFACT:
4057 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4059 case DVD_STRUCT_CMI:
4061 case DVD_STRUCT_PROTDISCID:
4062 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4064 case DVD_STRUCT_DISCKEYBLOCK:
4065 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4067 case DVD_STRUCT_DDS:
4068 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4070 case DVD_STRUCT_MEDIUM_STAT:
4071 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4073 case DVD_STRUCT_SPARE_AREA:
4074 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4076 case DVD_STRUCT_RMD_LAST:
4078 case DVD_STRUCT_RMD_RMA:
4080 case DVD_STRUCT_PRERECORDED:
4081 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4083 case DVD_STRUCT_UNIQUEID:
4084 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4086 case DVD_STRUCT_DCB:
4088 case DVD_STRUCT_LIST:
4090 * This is the maximum allocation length for the READ DVD
4091 * STRUCTURE command. There's nothing in the MMC3 spec
4092 * that indicates a limit in the amount of data that can
4093 * be returned from this call, other than the limits
4094 * imposed by the 2-byte length variables.
4103 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4107 cam_periph_lock(periph);
4108 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4110 scsi_read_dvd_structure(&ccb->csio,
4111 /* retries */ cd_retry_count,
4112 /* cbfcnp */ cddone,
4113 /* tag_action */ MSG_SIMPLE_Q_TAG,
4115 /* layer_number */ dvdstruct->layer_num,
4116 /* key_format */ dvdstruct->format,
4117 /* agid */ dvdstruct->agid,
4118 /* data_ptr */ databuf,
4119 /* dxfer_len */ length,
4120 /* sense_len */ SSD_FULL_SIZE,
4121 /* timeout */ 50000);
4123 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4124 /*sense_flags*/SF_RETRY_UA);
4129 switch(dvdstruct->format) {
4130 case DVD_STRUCT_PHYSICAL: {
4131 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4132 struct dvd_layer *outlayer;
4133 struct scsi_read_dvd_struct_data_physical *phys_data;
4136 (struct scsi_read_dvd_struct_data_physical *)databuf;
4137 inlayer = &phys_data->layer_desc;
4138 outlayer = (struct dvd_layer *)&dvdstruct->data;
4140 dvdstruct->length = sizeof(*inlayer);
4142 outlayer->book_type = (inlayer->book_type_version &
4143 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4144 outlayer->book_version = (inlayer->book_type_version &
4145 RDSD_BOOK_VERSION_MASK);
4146 outlayer->disc_size = (inlayer->disc_size_max_rate &
4147 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4148 outlayer->max_rate = (inlayer->disc_size_max_rate &
4149 RDSD_MAX_RATE_MASK);
4150 outlayer->nlayers = (inlayer->layer_info &
4151 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4152 outlayer->track_path = (inlayer->layer_info &
4153 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4154 outlayer->layer_type = (inlayer->layer_info &
4155 RDSD_LAYER_TYPE_MASK);
4156 outlayer->linear_density = (inlayer->density &
4157 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4158 outlayer->track_density = (inlayer->density &
4159 RDSD_TRACK_DENSITY_MASK);
4160 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4162 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4163 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4164 outlayer->end_sector_l0 =
4165 scsi_3btoul(inlayer->end_sector_layer0);
4168 case DVD_STRUCT_COPYRIGHT: {
4169 struct scsi_read_dvd_struct_data_copyright *copy_data;
4171 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4174 dvdstruct->cpst = copy_data->cps_type;
4175 dvdstruct->rmi = copy_data->region_info;
4176 dvdstruct->length = 0;
4182 * Tell the user what the overall length is, no matter
4183 * what we can actually fit in the data buffer.
4185 dvdstruct->length = length - ccb->csio.resid -
4186 sizeof(struct scsi_read_dvd_struct_data_header);
4189 * But only actually copy out the smaller of what we read
4190 * in or what the structure can take.
4192 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4194 min(sizeof(dvdstruct->data), dvdstruct->length));
4199 xpt_release_ccb(ccb);
4200 cam_periph_unlock(periph);
4202 if (databuf != NULL)
4203 free(databuf, M_DEVBUF);
4209 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4210 void (*cbfcnp)(struct cam_periph *, union ccb *),
4211 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4212 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4213 u_int8_t sense_len, u_int32_t timeout)
4215 struct scsi_report_key *scsi_cmd;
4217 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4218 bzero(scsi_cmd, sizeof(*scsi_cmd));
4219 scsi_cmd->opcode = REPORT_KEY;
4220 scsi_ulto4b(lba, scsi_cmd->lba);
4221 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4222 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4223 (key_format & RK_KF_KEYFORMAT_MASK);
4228 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4230 /*data_ptr*/ data_ptr,
4231 /*dxfer_len*/ dxfer_len,
4238 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4239 void (*cbfcnp)(struct cam_periph *, union ccb *),
4240 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4241 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4244 struct scsi_send_key *scsi_cmd;
4246 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4247 bzero(scsi_cmd, sizeof(*scsi_cmd));
4248 scsi_cmd->opcode = SEND_KEY;
4250 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4251 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4252 (key_format & RK_KF_KEYFORMAT_MASK);
4257 /*flags*/ CAM_DIR_OUT,
4259 /*data_ptr*/ data_ptr,
4260 /*dxfer_len*/ dxfer_len,
4268 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4269 void (*cbfcnp)(struct cam_periph *, union ccb *),
4270 u_int8_t tag_action, u_int32_t address,
4271 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4272 u_int8_t *data_ptr, u_int32_t dxfer_len,
4273 u_int8_t sense_len, u_int32_t timeout)
4275 struct scsi_read_dvd_structure *scsi_cmd;
4277 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4278 bzero(scsi_cmd, sizeof(*scsi_cmd));
4279 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4281 scsi_ulto4b(address, scsi_cmd->address);
4282 scsi_cmd->layer_number = layer_number;
4283 scsi_cmd->format = format;
4284 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4285 /* The AGID is the top two bits of this byte */
4286 scsi_cmd->agid = agid << 6;
4291 /*flags*/ CAM_DIR_IN,
4293 /*data_ptr*/ data_ptr,
4294 /*dxfer_len*/ dxfer_len,