2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/cdrio.h>
64 #include <sys/dvdio.h>
65 #include <sys/devicestat.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
70 #include <sys/taskqueue.h>
71 #include <geom/geom_disk.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_periph.h>
76 #include <cam/cam_xpt_periph.h>
77 #include <cam/cam_queue.h>
78 #include <cam/cam_sim.h>
80 #include <cam/scsi/scsi_message.h>
81 #include <cam/scsi/scsi_da.h>
82 #include <cam/scsi/scsi_cd.h>
84 #define LEADOUT 0xaa /* leadout toc entry */
94 CD_Q_BCD_TRACKS = 0x02,
95 CD_Q_10_BYTE_ONLY = 0x10,
96 CD_Q_RETRY_BUSY = 0x40
99 #define CD_Q_BIT_STRING \
107 CD_FLAG_INVALID = 0x0001,
108 CD_FLAG_NEW_DISC = 0x0002,
109 CD_FLAG_DISC_LOCKED = 0x0004,
110 CD_FLAG_DISC_REMOVABLE = 0x0008,
111 CD_FLAG_SAW_MEDIA = 0x0010,
112 CD_FLAG_ACTIVE = 0x0080,
113 CD_FLAG_SCHED_ON_COMP = 0x0100,
114 CD_FLAG_RETRY_UA = 0x0200,
115 CD_FLAG_VALID_MEDIA = 0x0400,
116 CD_FLAG_VALID_TOC = 0x0800,
117 CD_FLAG_SCTX_INIT = 0x1000,
118 CD_FLAG_MEDIA_WAIT = 0x2000,
119 CD_FLAG_MEDIA_SCAN_ACT = 0x4000
124 CD_CCB_BUFFER_IO = 0x02,
126 CD_CCB_MEDIA_PREVENT = 0x04,
127 CD_CCB_MEDIA_ALLOW = 0x05,
128 CD_CCB_MEDIA_SIZE = 0x06,
129 CD_CCB_MEDIA_TOC_HDR = 0x07,
130 CD_CCB_MEDIA_TOC_FULL = 0x08,
131 CD_CCB_MEDIA_TOC_LEAD = 0x09,
132 CD_CCB_TYPE_MASK = 0x0F,
133 CD_CCB_RETRY_UA = 0x10
136 #define ccb_state ppriv_field0
137 #define ccb_bp ppriv_ptr1
140 struct ioc_toc_header header;
141 struct cd_toc_entry entries[100];
144 struct cd_toc_single {
145 struct ioc_toc_header header;
146 struct cd_toc_entry entry;
152 CD_STATE_MEDIA_PREVENT,
153 CD_STATE_MEDIA_ALLOW,
155 CD_STATE_MEDIA_TOC_HDR,
156 CD_STATE_MEDIA_TOC_FULL,
157 CD_STATE_MEDIA_TOC_LEAD
163 volatile cd_flags flags;
164 struct bio_queue_head bio_queue;
165 LIST_HEAD(, ccb_hdr) pending_ccbs;
166 struct cd_params params;
169 struct cam_periph *periph;
170 int minimum_command_size;
171 int outstanding_cmds;
173 struct task sysctl_task;
174 struct sysctl_ctx_list sysctl_ctx;
175 struct sysctl_oid *sysctl_tree;
176 STAILQ_HEAD(, cd_mode_params) mode_queue;
177 struct cd_tocdata toc;
179 struct cd_toc_single leadout;
181 struct callout mediapoll_c;
183 #define CD_ANNOUNCETMP_SZ 120
184 char announce_temp[CD_ANNOUNCETMP_SZ];
185 #define CD_ANNOUNCE_SZ 400
186 char announce_buf[CD_ANNOUNCE_SZ];
189 struct cd_page_sizes {
194 static struct cd_page_sizes cd_page_size_table[] =
196 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
199 struct cd_quirk_entry {
200 struct scsi_inquiry_pattern inq_pat;
205 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
206 * your device hangs when it gets a 10 byte command. Adding a quirk just
207 * to get rid of the informative diagnostic message is not acceptable. All
208 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
209 * referenced in a comment along with the quirk) , and must be approved by
210 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
211 * be removed until the submitter can explain why they are needed.
212 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
213 * when the CAM_NEW_TRAN_CODE work is done.
215 static struct cd_quirk_entry cd_quirk_table[] =
218 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
219 /* quirks */ CD_Q_BCD_TRACKS
223 * VMware returns BUSY status when storage has transient
224 * connectivity problems, so better wait.
226 {T_CDROM, SIP_MEDIA_REMOVABLE, "NECVMWar", "VMware IDE CDR10", "*"},
227 /*quirks*/ CD_Q_RETRY_BUSY
231 #ifdef COMPAT_FREEBSD32
232 struct ioc_read_toc_entry32 {
233 u_char address_format;
234 u_char starting_track;
236 uint32_t data; /* (struct cd_toc_entry *) */
238 #define CDIOREADTOCENTRYS_32 \
239 _IOC_NEWTYPE(CDIOREADTOCENTRYS, struct ioc_read_toc_entry32)
242 static disk_open_t cdopen;
243 static disk_close_t cdclose;
244 static disk_ioctl_t cdioctl;
245 static disk_strategy_t cdstrategy;
247 static periph_init_t cdinit;
248 static periph_ctor_t cdregister;
249 static periph_dtor_t cdcleanup;
250 static periph_start_t cdstart;
251 static periph_oninv_t cdoninvalidate;
252 static void cdasync(void *callback_arg, u_int32_t code,
253 struct cam_path *path, void *arg);
254 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
255 static int cdrunccb(union ccb *ccb,
256 int (*error_routine)(union ccb *ccb,
258 u_int32_t sense_flags),
259 u_int32_t cam_flags, u_int32_t sense_flags);
260 static void cddone(struct cam_periph *periph,
261 union ccb *start_ccb);
262 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
263 static int cdgetpagesize(int page_num);
264 static void cdprevent(struct cam_periph *periph, int action);
265 static void cdmediaprobedone(struct cam_periph *periph);
266 static int cdcheckmedia(struct cam_periph *periph, int do_wait);
268 static int cdsize(struct cam_periph *periph, u_int32_t *size);
270 static int cd6byteworkaround(union ccb *ccb);
271 static int cderror(union ccb *ccb, u_int32_t cam_flags,
272 u_int32_t sense_flags);
273 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
274 u_int32_t start, u_int8_t *data,
275 u_int32_t len, u_int32_t sense_flags);
276 static int cdgetmode(struct cam_periph *periph,
277 struct cd_mode_params *data, u_int32_t page);
278 static int cdsetmode(struct cam_periph *periph,
279 struct cd_mode_params *data);
280 static int cdplay(struct cam_periph *periph, u_int32_t blk,
282 static int cdreadsubchannel(struct cam_periph *periph,
283 u_int32_t mode, u_int32_t format,
285 struct cd_sub_channel_info *data,
287 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
288 u_int32_t starts, u_int32_t startf,
289 u_int32_t endm, u_int32_t ends,
291 static int cdplaytracks(struct cam_periph *periph,
292 u_int32_t strack, u_int32_t sindex,
293 u_int32_t etrack, u_int32_t eindex);
294 static int cdpause(struct cam_periph *periph, u_int32_t go);
295 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
296 static int cdstartunit(struct cam_periph *periph, int load);
297 static int cdsetspeed(struct cam_periph *periph,
298 u_int32_t rdspeed, u_int32_t wrspeed);
299 static int cdreportkey(struct cam_periph *periph,
300 struct dvd_authinfo *authinfo);
301 static int cdsendkey(struct cam_periph *periph,
302 struct dvd_authinfo *authinfo);
303 static int cdreaddvdstructure(struct cam_periph *periph,
304 struct dvd_struct *dvdstruct);
305 static timeout_t cdmediapoll;
307 static struct periph_driver cddriver =
310 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
313 PERIPHDRIVER_DECLARE(cd, cddriver);
315 #ifndef CD_DEFAULT_POLL_PERIOD
316 #define CD_DEFAULT_POLL_PERIOD 3
318 #ifndef CD_DEFAULT_RETRY
319 #define CD_DEFAULT_RETRY 4
321 #ifndef CD_DEFAULT_TIMEOUT
322 #define CD_DEFAULT_TIMEOUT 30000
325 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
326 static int cd_retry_count = CD_DEFAULT_RETRY;
327 static int cd_timeout = CD_DEFAULT_TIMEOUT;
329 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
330 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RWTUN,
331 &cd_poll_period, 0, "Media polling period in seconds");
332 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RWTUN,
333 &cd_retry_count, 0, "Normal I/O retry count");
334 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RWTUN,
335 &cd_timeout, 0, "Timeout, in us, for read operations");
337 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
345 * Install a global async callback. This callback will
346 * receive async callbacks like "new device found".
348 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
350 if (status != CAM_REQ_CMP) {
351 printf("cd: Failed to attach master async callback "
352 "due to status 0x%x!\n", status);
357 * Callback from GEOM, called when it has finished cleaning up its
361 cddiskgonecb(struct disk *dp)
363 struct cam_periph *periph;
365 periph = (struct cam_periph *)dp->d_drv1;
366 cam_periph_release(periph);
370 cdoninvalidate(struct cam_periph *periph)
372 struct cd_softc *softc;
374 softc = (struct cd_softc *)periph->softc;
377 * De-register any async callbacks.
379 xpt_register_async(0, cdasync, periph, periph->path);
381 softc->flags |= CD_FLAG_INVALID;
384 * Return all queued I/O with ENXIO.
385 * XXX Handle any transactions queued to the card
386 * with XPT_ABORT_CCB.
388 bioq_flush(&softc->bio_queue, NULL, ENXIO);
390 disk_gone(softc->disk);
394 cdcleanup(struct cam_periph *periph)
396 struct cd_softc *softc;
398 softc = (struct cd_softc *)periph->softc;
400 cam_periph_unlock(periph);
401 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
402 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
403 xpt_print(periph->path, "can't remove sysctl context\n");
406 callout_drain(&softc->mediapoll_c);
407 disk_destroy(softc->disk);
408 free(softc, M_DEVBUF);
409 cam_periph_lock(periph);
413 cdasync(void *callback_arg, u_int32_t code,
414 struct cam_path *path, void *arg)
416 struct cam_periph *periph;
417 struct cd_softc *softc;
419 periph = (struct cam_periph *)callback_arg;
421 case AC_FOUND_DEVICE:
423 struct ccb_getdev *cgd;
426 cgd = (struct ccb_getdev *)arg;
430 if (cgd->protocol != PROTO_SCSI)
432 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
434 if (SID_TYPE(&cgd->inq_data) != T_CDROM
435 && SID_TYPE(&cgd->inq_data) != T_WORM)
439 * Allocate a peripheral instance for
440 * this device and start the probe
443 status = cam_periph_alloc(cdregister, cdoninvalidate,
445 "cd", CAM_PERIPH_BIO,
447 AC_FOUND_DEVICE, cgd);
449 if (status != CAM_REQ_CMP
450 && status != CAM_REQ_INPROG)
451 printf("cdasync: Unable to attach new device "
452 "due to status 0x%x\n", status);
456 case AC_UNIT_ATTENTION:
459 int error_code, sense_key, asc, ascq;
461 softc = (struct cd_softc *)periph->softc;
462 ccb = (union ccb *)arg;
465 * Handle all media change UNIT ATTENTIONs except
466 * our own, as they will be handled by cderror().
468 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
469 scsi_extract_sense_ccb(ccb,
470 &error_code, &sense_key, &asc, &ascq)) {
471 if (asc == 0x28 && ascq == 0x00)
472 disk_media_changed(softc->disk, M_NOWAIT);
474 cam_periph_async(periph, code, path, arg);
478 softc = (struct cd_softc *)periph->softc;
479 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
480 if (cam_periph_acquire(periph) == 0) {
482 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
489 struct ccb_hdr *ccbh;
491 softc = (struct cd_softc *)periph->softc;
493 * Don't fail on the expected unit attention
496 softc->flags |= CD_FLAG_RETRY_UA;
497 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
498 ccbh->ccb_state |= CD_CCB_RETRY_UA;
502 cam_periph_async(periph, code, path, arg);
508 cdsysctlinit(void *context, int pending)
510 struct cam_periph *periph;
511 struct cd_softc *softc;
512 char tmpstr[32], tmpstr2[16];
514 periph = (struct cam_periph *)context;
515 if (cam_periph_acquire(periph) != 0)
518 softc = (struct cd_softc *)periph->softc;
519 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
520 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
522 sysctl_ctx_init(&softc->sysctl_ctx);
523 softc->flags |= CD_FLAG_SCTX_INIT;
524 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
525 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
526 tmpstr2, CTLFLAG_RD, 0, tmpstr, "device_index");
528 if (softc->sysctl_tree == NULL) {
529 printf("cdsysctlinit: unable to allocate sysctl tree\n");
530 cam_periph_release(periph);
535 * Now register the sysctl handler, so the user can the value on
538 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
539 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
540 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
543 cam_periph_release(periph);
547 * We have a handler function for this so we can check the values when the
548 * user sets them, instead of every time we look at them.
551 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
555 value = *(int *)arg1;
557 error = sysctl_handle_int(oidp, &value, 0, req);
560 || (req->newptr == NULL))
564 * The only real values we can have here are 6 or 10. I don't
565 * really forsee having 12 be an option at any time in the future.
566 * So if the user sets something less than or equal to 6, we'll set
567 * it to 6. If he sets something greater than 6, we'll set it to 10.
569 * I suppose we could just return an error here for the wrong values,
570 * but I don't think it's necessary to do so, as long as we can
571 * determine the user's intent without too much trouble.
578 *(int *)arg1 = value;
584 cdregister(struct cam_periph *periph, void *arg)
586 struct cd_softc *softc;
587 struct ccb_pathinq cpi;
588 struct ccb_getdev *cgd;
592 cgd = (struct ccb_getdev *)arg;
594 printf("cdregister: no getdev CCB, can't register device\n");
595 return(CAM_REQ_CMP_ERR);
598 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
601 printf("cdregister: Unable to probe new device. "
602 "Unable to allocate softc\n");
603 return(CAM_REQ_CMP_ERR);
606 LIST_INIT(&softc->pending_ccbs);
607 STAILQ_INIT(&softc->mode_queue);
608 softc->state = CD_STATE_PROBE;
609 bioq_init(&softc->bio_queue);
610 if (SID_IS_REMOVABLE(&cgd->inq_data))
611 softc->flags |= CD_FLAG_DISC_REMOVABLE;
613 periph->softc = softc;
614 softc->periph = periph;
617 * See if this device has any quirks.
619 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
620 (caddr_t)cd_quirk_table,
621 nitems(cd_quirk_table),
622 sizeof(*cd_quirk_table), scsi_inquiry_match);
625 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
627 softc->quirks = CD_Q_NONE;
629 /* Check if the SIM does not want 6 byte commands */
630 xpt_path_inq(&cpi, periph->path);
631 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
632 softc->quirks |= CD_Q_10_BYTE_ONLY;
634 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
636 /* The default is 6 byte commands, unless quirked otherwise */
637 if (softc->quirks & CD_Q_10_BYTE_ONLY)
638 softc->minimum_command_size = 10;
640 softc->minimum_command_size = 6;
643 * Refcount and block open attempts until we are setup
646 (void)cam_periph_hold(periph, PRIBIO);
647 cam_periph_unlock(periph);
649 * Load the user's default, if any.
651 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
652 periph->unit_number);
653 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
655 /* 6 and 10 are the only permissible values here. */
656 if (softc->minimum_command_size < 6)
657 softc->minimum_command_size = 6;
658 else if (softc->minimum_command_size > 6)
659 softc->minimum_command_size = 10;
662 * We need to register the statistics structure for this device,
663 * but we don't have the blocksize yet for it. So, we register
664 * the structure and indicate that we don't have the blocksize
665 * yet. Unlike other SCSI peripheral drivers, we explicitly set
666 * the device type here to be CDROM, rather than just ORing in
667 * the device type. This is because this driver can attach to either
668 * CDROM or WORM devices, and we want this peripheral driver to
669 * show up in the devstat list as a CD peripheral driver, not a
670 * WORM peripheral driver. WORM drives will also have the WORM
671 * driver attached to them.
673 softc->disk = disk_alloc();
674 softc->disk->d_devstat = devstat_new_entry("cd",
675 periph->unit_number, 0,
676 DEVSTAT_BS_UNAVAILABLE,
678 XPORT_DEVSTAT_TYPE(cpi.transport),
679 DEVSTAT_PRIORITY_CD);
680 softc->disk->d_open = cdopen;
681 softc->disk->d_close = cdclose;
682 softc->disk->d_strategy = cdstrategy;
683 softc->disk->d_gone = cddiskgonecb;
684 softc->disk->d_ioctl = cdioctl;
685 softc->disk->d_name = "cd";
686 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
687 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
688 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
689 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
690 cgd->inq_data.product, sizeof(cgd->inq_data.product),
691 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
692 softc->disk->d_unit = periph->unit_number;
693 softc->disk->d_drv1 = periph;
695 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
696 else if (cpi.maxio > MAXPHYS)
697 softc->disk->d_maxsize = MAXPHYS; /* for safety */
699 softc->disk->d_maxsize = cpi.maxio;
700 softc->disk->d_flags = 0;
701 softc->disk->d_hba_vendor = cpi.hba_vendor;
702 softc->disk->d_hba_device = cpi.hba_device;
703 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
704 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
707 * Acquire a reference to the periph before we register with GEOM.
708 * We'll release this reference once GEOM calls us back (via
709 * dadiskgonecb()) telling us that our provider has been freed.
711 if (cam_periph_acquire(periph) != 0) {
712 xpt_print(periph->path, "%s: lost periph during "
713 "registration!\n", __func__);
714 cam_periph_lock(periph);
715 return (CAM_REQ_CMP_ERR);
718 disk_create(softc->disk, DISK_VERSION);
719 cam_periph_lock(periph);
722 * Add an async callback so that we get
723 * notified if this device goes away.
725 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
726 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
729 * Schedule a periodic media polling events.
731 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
732 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
733 (cgd->inq_flags & SID_AEN) == 0 &&
735 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
736 cdmediapoll, periph);
738 xpt_schedule(periph, CAM_PRIORITY_DEV);
743 cdopen(struct disk *dp)
745 struct cam_periph *periph;
746 struct cd_softc *softc;
749 periph = (struct cam_periph *)dp->d_drv1;
750 softc = (struct cd_softc *)periph->softc;
752 if (cam_periph_acquire(periph) != 0)
755 cam_periph_lock(periph);
757 if (softc->flags & CD_FLAG_INVALID) {
758 cam_periph_release_locked(periph);
759 cam_periph_unlock(periph);
763 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
764 cam_periph_release_locked(periph);
765 cam_periph_unlock(periph);
769 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
773 * Check for media, and set the appropriate flags. We don't bail
774 * if we don't have media, but then we don't allow anything but the
775 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
777 cdcheckmedia(periph, /*do_wait*/ 1);
779 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
780 cam_periph_unhold(periph);
782 cam_periph_unlock(periph);
788 cdclose(struct disk *dp)
790 struct cam_periph *periph;
791 struct cd_softc *softc;
793 periph = (struct cam_periph *)dp->d_drv1;
794 softc = (struct cd_softc *)periph->softc;
796 cam_periph_lock(periph);
797 if (cam_periph_hold(periph, PRIBIO) != 0) {
798 cam_periph_unlock(periph);
799 cam_periph_release(periph);
803 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
806 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
807 cdprevent(periph, PR_ALLOW);
810 * Since we're closing this CD, mark the blocksize as unavailable.
811 * It will be marked as available when the CD is opened again.
813 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
816 * We'll check the media and toc again at the next open().
818 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
820 cam_periph_unhold(periph);
821 cam_periph_release_locked(periph);
822 cam_periph_unlock(periph);
828 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
830 u_int32_t sense_flags),
831 u_int32_t cam_flags, u_int32_t sense_flags)
833 struct cd_softc *softc;
834 struct cam_periph *periph;
837 periph = xpt_path_periph(ccb->ccb_h.path);
838 softc = (struct cd_softc *)periph->softc;
840 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
841 softc->disk->d_devstat);
847 * Actually translate the requested transfer into one the physical driver
848 * can understand. The transfer is described by a buf and will include
849 * only one physical transfer.
852 cdstrategy(struct bio *bp)
854 struct cam_periph *periph;
855 struct cd_softc *softc;
857 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
858 cam_periph_lock(periph);
859 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
860 ("cdstrategy(%p)\n", bp));
862 softc = (struct cd_softc *)periph->softc;
865 * If the device has been made invalid, error out
867 if ((softc->flags & CD_FLAG_INVALID)) {
868 cam_periph_unlock(periph);
869 biofinish(bp, NULL, ENXIO);
874 * Place it in the queue of disk activities for this disk
876 bioq_disksort(&softc->bio_queue, bp);
879 * If we don't know that we have valid media, schedule the media
880 * check first. The I/O will get executed after the media check.
882 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
883 cdcheckmedia(periph, /*do_wait*/ 0);
885 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
887 cam_periph_unlock(periph);
892 cdstart(struct cam_periph *periph, union ccb *start_ccb)
894 struct cd_softc *softc;
896 struct ccb_scsiio *csio;
898 softc = (struct cd_softc *)periph->softc;
900 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
902 switch (softc->state) {
903 case CD_STATE_NORMAL:
905 bp = bioq_first(&softc->bio_queue);
909 csio = &start_ccb->csio;
910 scsi_test_unit_ready(csio,
911 /*retries*/ cd_retry_count,
916 start_ccb->ccb_h.ccb_bp = NULL;
917 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
918 xpt_action(start_ccb);
920 xpt_release_ccb(start_ccb);
924 cam_periph_release_locked(periph);
926 bioq_remove(&softc->bio_queue, bp);
928 scsi_read_write(&start_ccb->csio,
929 /*retries*/ cd_retry_count,
932 /* read */bp->bio_cmd == BIO_READ ?
933 SCSI_RW_READ : SCSI_RW_WRITE,
935 /* minimum_cmd_size */ 10,
936 /* lba */ bp->bio_offset /
937 softc->params.blksize,
938 bp->bio_bcount / softc->params.blksize,
939 /* data_ptr */ bp->bio_data,
940 /* dxfer_len */ bp->bio_bcount,
941 /* sense_len */ cd_retry_count ?
942 SSD_FULL_SIZE : SF_NO_PRINT,
943 /* timeout */ cd_timeout);
944 /* Use READ CD command for audio tracks. */
945 if (softc->params.blksize == 2352) {
946 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
947 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
948 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
949 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
950 start_ccb->csio.cdb_len = 12;
952 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
954 LIST_INSERT_HEAD(&softc->pending_ccbs,
955 &start_ccb->ccb_h, periph_links.le);
956 softc->outstanding_cmds++;
958 /* We expect a unit attention from this device */
959 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
960 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
961 softc->flags &= ~CD_FLAG_RETRY_UA;
964 start_ccb->ccb_h.ccb_bp = bp;
965 bp = bioq_first(&softc->bio_queue);
967 xpt_action(start_ccb);
969 if (bp != NULL || softc->tur) {
970 /* Have more work to do, so ensure we stay scheduled */
971 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
976 case CD_STATE_MEDIA_SIZE:
978 struct scsi_read_capacity_data *rcap;
980 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
981 M_SCSICD, M_NOWAIT | M_ZERO);
983 xpt_print(periph->path,
984 "%s: Couldn't malloc read_capacity data\n",
986 xpt_release_ccb(start_ccb);
988 * We can't probe because we can't allocate memory,
989 * so invalidate the peripheral. The system probably
990 * has larger problems at this stage. If we've
991 * already probed (and are re-probing capacity), we
992 * don't need to invalidate.
994 * XXX KDM need to reset probe state and kick out
997 if (softc->state == CD_STATE_PROBE)
998 cam_periph_invalidate(periph);
1003 * Set the default capacity and sector size to something that
1004 * GEOM can handle. This will get reset when a read capacity
1005 * completes successfully.
1007 softc->disk->d_sectorsize = 2048;
1008 softc->disk->d_mediasize = 0;
1010 csio = &start_ccb->csio;
1011 scsi_read_capacity(csio,
1012 /*retries*/ cd_retry_count,
1018 start_ccb->ccb_h.ccb_bp = NULL;
1019 if (softc->state == CD_STATE_PROBE)
1020 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1022 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_SIZE;
1023 xpt_action(start_ccb);
1026 case CD_STATE_MEDIA_ALLOW:
1027 case CD_STATE_MEDIA_PREVENT:
1030 * If the CD is already locked, we don't need to do this.
1031 * Move on to the capacity check.
1033 if ((softc->flags & CD_FLAG_DISC_LOCKED) != 0) {
1034 softc->state = CD_STATE_MEDIA_SIZE;
1035 xpt_release_ccb(start_ccb);
1036 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1040 scsi_prevent(&start_ccb->csio,
1041 /*retries*/ cd_retry_count,
1043 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1044 /*action*/ (softc->state == CD_STATE_MEDIA_ALLOW) ?
1045 PR_ALLOW : PR_PREVENT,
1046 /*sense_len*/ SSD_FULL_SIZE,
1049 start_ccb->ccb_h.ccb_bp = NULL;
1050 if (softc->state == CD_STATE_MEDIA_ALLOW)
1051 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_ALLOW;
1053 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_PREVENT;
1054 xpt_action(start_ccb);
1057 case CD_STATE_MEDIA_TOC_HDR: {
1058 struct ioc_toc_header *toch;
1060 bzero(&softc->toc, sizeof(softc->toc));
1062 toch = &softc->toc.header;
1064 scsi_read_toc(&start_ccb->csio,
1065 /*retries*/ cd_retry_count,
1067 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1069 /*format*/ SRTOC_FORMAT_TOC,
1071 /*data_ptr*/ (uint8_t *)toch,
1072 /*dxfer_len*/ sizeof(*toch),
1073 /*sense_len*/ SSD_FULL_SIZE,
1075 start_ccb->ccb_h.ccb_bp = NULL;
1076 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_HDR;
1077 xpt_action(start_ccb);
1080 case CD_STATE_MEDIA_TOC_FULL: {
1082 bzero(&softc->toc, sizeof(softc->toc));
1084 scsi_read_toc(&start_ccb->csio,
1085 /*retries*/ cd_retry_count,
1087 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1089 /*format*/ SRTOC_FORMAT_TOC,
1091 /*data_ptr*/ (uint8_t *)&softc->toc,
1092 /*dxfer_len*/ softc->toc_read_len ?
1093 softc->toc_read_len :
1095 /*sense_len*/ SSD_FULL_SIZE,
1097 start_ccb->ccb_h.ccb_bp = NULL;
1098 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_FULL;
1099 xpt_action(start_ccb);
1102 case CD_STATE_MEDIA_TOC_LEAD: {
1103 struct cd_toc_single *leadout;
1105 leadout = &softc->leadout;
1106 bzero(leadout, sizeof(*leadout));
1108 scsi_read_toc(&start_ccb->csio,
1109 /*retries*/ cd_retry_count,
1111 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1112 /*byte1_flags*/ CD_MSF,
1113 /*format*/ SRTOC_FORMAT_TOC,
1115 /*data_ptr*/ (uint8_t *)leadout,
1116 /*dxfer_len*/ sizeof(*leadout),
1117 /*sense_len*/ SSD_FULL_SIZE,
1119 start_ccb->ccb_h.ccb_bp = NULL;
1120 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_LEAD;
1121 xpt_action(start_ccb);
1128 cddone(struct cam_periph *periph, union ccb *done_ccb)
1130 struct cd_softc *softc;
1131 struct ccb_scsiio *csio;
1133 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1135 softc = (struct cd_softc *)periph->softc;
1136 csio = &done_ccb->csio;
1138 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1139 case CD_CCB_BUFFER_IO:
1144 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1147 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1150 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1155 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1156 if (error == ERESTART) {
1158 * A retry was scheuled, so
1166 xpt_print(periph->path,
1167 "cddone: got error %#x back\n", error);
1168 bioq_flush(&softc->bio_queue, NULL, EIO);
1169 bp->bio_resid = bp->bio_bcount;
1170 bp->bio_error = error;
1171 bp->bio_flags |= BIO_ERROR;
1172 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1173 cam_release_devq(done_ccb->ccb_h.path,
1177 /*getcount_only*/0);
1180 bp->bio_resid = csio->resid;
1182 if (bp->bio_resid != 0) {
1184 * Short transfer ???
1185 * XXX: not sure this is correct for partial
1188 bp->bio_flags |= BIO_ERROR;
1192 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1193 softc->outstanding_cmds--;
1195 biofinish(bp, NULL, 0);
1200 struct scsi_read_capacity_data *rdcap;
1202 struct cd_params *cdp;
1205 cdp = &softc->params;
1206 announce_buf = softc->announce_temp;
1207 bzero(announce_buf, CD_ANNOUNCETMP_SZ);
1209 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1211 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1212 cdp->blksize = scsi_4btoul (rdcap->length);
1215 * Retry any UNIT ATTENTION type errors. They
1216 * are expected at boot.
1218 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1219 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1220 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1221 snprintf(announce_buf, CD_ANNOUNCETMP_SZ,
1222 "%juMB (%ju %u byte sectors)",
1223 ((uintmax_t)cdp->disksize * cdp->blksize) /
1225 (uintmax_t)cdp->disksize, cdp->blksize);
1227 if (error == ERESTART) {
1229 * A retry was scheuled, so
1235 int sense_key, error_code;
1238 struct ccb_getdev cgd;
1240 /* Don't wedge this device's queue */
1241 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1242 cam_release_devq(done_ccb->ccb_h.path,
1246 /*getcount_only*/0);
1248 status = done_ccb->ccb_h.status;
1250 xpt_setup_ccb(&cgd.ccb_h,
1251 done_ccb->ccb_h.path,
1252 CAM_PRIORITY_NORMAL);
1253 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1254 xpt_action((union ccb *)&cgd);
1256 if (scsi_extract_sense_ccb(done_ccb,
1257 &error_code, &sense_key, &asc, &ascq))
1263 * Attach to anything that claims to be a
1264 * CDROM or WORM device, as long as it
1265 * doesn't return a "Logical unit not
1266 * supported" (0x25) error.
1268 if ((have_sense) && (asc != 0x25)
1269 && (error_code == SSD_CURRENT_ERROR
1270 || error_code == SSD_DESC_CURRENT_ERROR)) {
1271 const char *sense_key_desc;
1272 const char *asc_desc;
1274 scsi_sense_desc(sense_key, asc, ascq,
1278 snprintf(announce_buf,
1280 "Attempt to query device "
1281 "size failed: %s, %s",
1284 } else if ((have_sense == 0)
1285 && ((status & CAM_STATUS_MASK) ==
1286 CAM_SCSI_STATUS_ERROR)
1287 && (csio->scsi_status ==
1288 SCSI_STATUS_BUSY)) {
1289 snprintf(announce_buf,
1291 "Attempt to query device "
1292 "size failed: SCSI Status: %s",
1293 scsi_status_string(csio));
1294 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1296 * We only print out an error for
1297 * CDROM type devices. For WORM
1298 * devices, we don't print out an
1299 * error since a few WORM devices
1300 * don't support CDROM commands.
1301 * If we have sense information, go
1302 * ahead and print it out.
1303 * Otherwise, just say that we
1308 * Just print out the error, not
1309 * the full probe message, when we
1316 xpt_print(periph->path,
1317 "got CAM status %#x\n",
1318 done_ccb->ccb_h.status);
1320 xpt_print(periph->path, "fatal error, "
1321 "failed to attach to device\n");
1323 * Invalidate this peripheral.
1325 cam_periph_invalidate(periph);
1327 announce_buf = NULL;
1331 * Invalidate this peripheral.
1333 cam_periph_invalidate(periph);
1334 announce_buf = NULL;
1338 free(rdcap, M_SCSICD);
1339 if (announce_buf != NULL) {
1342 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ,
1344 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1345 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
1351 * Create our sysctl variables, now that we know
1352 * we have successfully attached.
1354 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1356 softc->state = CD_STATE_NORMAL;
1358 * Since our peripheral may be invalidated by an error
1359 * above or an external event, we must release our CCB
1360 * before releasing the probe lock on the peripheral.
1361 * The peripheral will only go away once the last lock
1362 * is removed, and we need it around for the CCB release
1365 xpt_release_ccb(done_ccb);
1366 cam_periph_unhold(periph);
1371 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1373 if (cderror(done_ccb, CAM_RETRY_SELTO,
1374 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1377 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1378 cam_release_devq(done_ccb->ccb_h.path,
1382 /*getcount_only*/0);
1384 xpt_release_ccb(done_ccb);
1385 cam_periph_release_locked(periph);
1388 case CD_CCB_MEDIA_ALLOW:
1389 case CD_CCB_MEDIA_PREVENT:
1396 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1397 error = cderror(done_ccb, CAM_RETRY_SELTO,
1398 SF_RETRY_UA | SF_NO_PRINT);
1400 if (error == ERESTART)
1402 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1403 cam_release_devq(done_ccb->ccb_h.path,
1407 /*getcount_only*/0);
1410 * Note that just like the original cdcheckmedia(), we do
1411 * a prevent without failing the whole operation if the
1412 * prevent fails. We try, but keep going if it doesn't
1416 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1417 CD_CCB_MEDIA_PREVENT)
1422 xpt_release_ccb(done_ccb);
1424 if (is_prevent != 0) {
1426 softc->flags |= CD_FLAG_DISC_LOCKED;
1428 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1429 softc->state = CD_STATE_MEDIA_SIZE;
1430 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1433 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1434 softc->state = CD_STATE_NORMAL;
1435 if (bioq_first(&softc->bio_queue) != NULL)
1436 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1440 case CD_CCB_MEDIA_SIZE:
1442 struct scsi_read_capacity_data *rdcap;
1446 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1447 error = cderror(done_ccb, CAM_RETRY_SELTO,
1448 SF_RETRY_UA | SF_NO_PRINT);
1450 if (error == ERESTART)
1452 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1453 cam_release_devq(done_ccb->ccb_h.path,
1457 /*getcount_only*/0);
1458 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1461 softc->params.disksize =scsi_4btoul(rdcap->addr) + 1;
1462 softc->params.blksize = scsi_4btoul(rdcap->length);
1464 /* Make sure we got at least some block size. */
1465 if (softc->params.blksize == 0)
1468 * SCSI-3 mandates that the reported blocksize shall be
1469 * 2048. Older drives sometimes report funny values,
1470 * trim it down to 2048, or other parts of the kernel
1471 * will get confused.
1473 * XXX we leave drives alone that might report 512
1474 * bytes, as well as drives reporting more weird
1475 * sizes like perhaps 4K.
1477 if (softc->params.blksize > 2048
1478 && softc->params.blksize <= 2352)
1479 softc->params.blksize = 2048;
1481 free(rdcap, M_SCSICD);
1484 softc->disk->d_sectorsize = softc->params.blksize;
1485 softc->disk->d_mediasize =
1486 (off_t)softc->params.blksize *
1487 softc->params.disksize;
1488 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
1489 softc->state = CD_STATE_MEDIA_TOC_HDR;
1491 softc->flags &= ~(CD_FLAG_VALID_MEDIA |
1493 bioq_flush(&softc->bio_queue, NULL, EINVAL);
1494 softc->state = CD_STATE_MEDIA_ALLOW;
1495 cdmediaprobedone(periph);
1497 xpt_release_ccb(done_ccb);
1498 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1501 case CD_CCB_MEDIA_TOC_HDR:
1502 case CD_CCB_MEDIA_TOC_FULL:
1503 case CD_CCB_MEDIA_TOC_LEAD:
1506 struct ioc_toc_header *toch;
1512 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1513 error = cderror(done_ccb, CAM_RETRY_SELTO,
1514 SF_RETRY_UA | SF_NO_PRINT);
1516 if (error == ERESTART)
1519 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1520 cam_release_devq(done_ccb->ccb_h.path,
1524 /*getcount_only*/0);
1527 * We will get errors here for media that doesn't have a table
1528 * of contents. According to the MMC-3 spec: "When a Read
1529 * TOC/PMA/ATIP command is presented for a DDCD/CD-R/RW media,
1530 * where the first TOC has not been recorded (no complete
1531 * session) and the Format codes 0000b, 0001b, or 0010b are
1532 * specified, this command shall be rejected with an INVALID
1533 * FIELD IN CDB. Devices that are not capable of reading an
1534 * incomplete session on DDC/CD-R/RW media shall report
1535 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
1537 * So this isn't fatal if we can't read the table of contents,
1538 * it just means that the user won't be able to issue the
1539 * play tracks ioctl, and likely lots of other stuff won't
1540 * work either. They need to burn the CD before we can do
1541 * a whole lot with it. So we don't print anything here if
1542 * we get an error back.
1544 * We also bail out if the drive doesn't at least give us
1545 * the full TOC header.
1548 || ((csio->dxfer_len - csio->resid) <
1549 sizeof(struct ioc_toc_header))) {
1550 softc->flags &= ~CD_FLAG_VALID_TOC;
1551 bzero(&softc->toc, sizeof(softc->toc));
1553 * Failing the TOC read is not an error.
1555 softc->state = CD_STATE_NORMAL;
1556 xpt_release_ccb(done_ccb);
1558 cdmediaprobedone(periph);
1561 * Go ahead and schedule I/O execution if there is
1562 * anything in the queue. It'll probably get
1563 * kicked out with an error.
1565 if (bioq_first(&softc->bio_queue) != NULL)
1566 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1571 * Note that this is NOT the storage location used for the
1574 toch = &softc->toc.header;
1576 if (softc->quirks & CD_Q_BCD_TRACKS) {
1577 toch->starting_track = bcd2bin(toch->starting_track);
1578 toch->ending_track = bcd2bin(toch->ending_track);
1581 /* Number of TOC entries, plus leadout */
1582 num_entries = (toch->ending_track - toch->starting_track) + 2;
1583 cdindex = toch->starting_track + num_entries -1;
1585 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1586 CD_CCB_MEDIA_TOC_HDR) {
1587 if (num_entries <= 0) {
1588 softc->flags &= ~CD_FLAG_VALID_TOC;
1589 bzero(&softc->toc, sizeof(softc->toc));
1591 * Failing the TOC read is not an error.
1593 softc->state = CD_STATE_NORMAL;
1594 xpt_release_ccb(done_ccb);
1596 cdmediaprobedone(periph);
1599 * Go ahead and schedule I/O execution if
1600 * there is anything in the queue. It'll
1601 * probably get kicked out with an error.
1603 if (bioq_first(&softc->bio_queue) != NULL)
1604 xpt_schedule(periph,
1605 CAM_PRIORITY_NORMAL);
1607 softc->toc_read_len = num_entries *
1608 sizeof(struct cd_toc_entry);
1609 softc->toc_read_len += sizeof(*toch);
1611 softc->state = CD_STATE_MEDIA_TOC_FULL;
1612 xpt_release_ccb(done_ccb);
1613 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1617 } else if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1618 CD_CCB_MEDIA_TOC_LEAD) {
1619 struct cd_toc_single *leadout;
1621 leadout = (struct cd_toc_single *)csio->data_ptr;
1622 softc->toc.entries[cdindex - toch->starting_track] =
1624 } else if (((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1625 CD_CCB_MEDIA_TOC_FULL)
1626 && (cdindex == toch->ending_track + 1)) {
1628 * XXX KDM is this necessary? Probably only if the
1629 * drive doesn't return leadout information with the
1630 * table of contents.
1632 softc->state = CD_STATE_MEDIA_TOC_LEAD;
1633 xpt_release_ccb(done_ccb);
1634 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1638 if (softc->quirks & CD_Q_BCD_TRACKS) {
1639 for (cdindex = 0; cdindex < num_entries - 1; cdindex++){
1640 softc->toc.entries[cdindex].track =
1641 bcd2bin(softc->toc.entries[cdindex].track);
1645 softc->flags |= CD_FLAG_VALID_TOC;
1646 /* If the first track is audio, correct sector size. */
1647 if ((softc->toc.entries[0].control & 4) == 0) {
1648 softc->disk->d_sectorsize =softc->params.blksize = 2352;
1649 softc->disk->d_mediasize =
1650 (off_t)softc->params.blksize *
1651 softc->params.disksize;
1653 softc->state = CD_STATE_NORMAL;
1656 * We unconditionally (re)set the blocksize each time the
1657 * CD device is opened. This is because the CD can change,
1658 * and therefore the blocksize might change.
1659 * XXX problems here if some slice or partition is still
1660 * open with the old size?
1662 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE)!=0)
1663 softc->disk->d_devstat->flags &=
1664 ~DEVSTAT_BS_UNAVAILABLE;
1665 softc->disk->d_devstat->block_size = softc->params.blksize;
1667 xpt_release_ccb(done_ccb);
1669 cdmediaprobedone(periph);
1671 if (bioq_first(&softc->bio_queue) != NULL)
1672 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1678 xpt_release_ccb(done_ccb);
1681 static union cd_pages *
1682 cdgetpage(struct cd_mode_params *mode_params)
1684 union cd_pages *page;
1686 if (mode_params->cdb_size == 10)
1687 page = (union cd_pages *)find_mode_page_10(
1688 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1690 page = (union cd_pages *)find_mode_page_6(
1691 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1697 cdgetpagesize(int page_num)
1701 for (i = 0; i < nitems(cd_page_size_table); i++) {
1702 if (cd_page_size_table[i].page == page_num)
1703 return (cd_page_size_table[i].page_size);
1709 static struct cd_toc_entry *
1710 te_data_get_ptr(void *irtep, u_long cmd)
1713 struct ioc_read_toc_entry irte;
1714 #ifdef COMPAT_FREEBSD32
1715 struct ioc_read_toc_entry32 irte32;
1720 switch (IOCPARM_LEN(cmd)) {
1721 case sizeof(irteup->irte):
1722 return (irteup->irte.data);
1723 #ifdef COMPAT_FREEBSD32
1724 case sizeof(irteup->irte32):
1725 return ((struct cd_toc_entry *)(uintptr_t)irteup->irte32.data);
1728 panic("Unhandled ioctl command %ld", cmd);
1733 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1736 struct cam_periph *periph;
1737 struct cd_softc *softc;
1740 periph = (struct cam_periph *)dp->d_drv1;
1741 cam_periph_lock(periph);
1743 softc = (struct cd_softc *)periph->softc;
1745 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1746 ("cdioctl(%#lx)\n", cmd));
1748 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1749 cam_periph_unlock(periph);
1750 cam_periph_release(periph);
1755 * If we don't have media loaded, check for it. If still don't
1756 * have media loaded, we can only do a load or eject.
1758 * We only care whether media is loaded if this is a cd-specific ioctl
1759 * (thus the IOCGROUP check below). Note that this will break if
1760 * anyone adds any ioctls into the switch statement below that don't
1761 * have their ioctl group set to 'c'.
1763 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1764 && ((cmd != CDIOCCLOSE)
1765 && (cmd != CDIOCEJECT))
1766 && (IOCGROUP(cmd) == 'c')) {
1767 error = cdcheckmedia(periph, /*do_wait*/ 1);
1769 cam_periph_unhold(periph);
1770 cam_periph_unlock(periph);
1775 * Drop the lock here so later mallocs can use WAITOK. The periph
1776 * is essentially locked still with the cam_periph_hold call above.
1778 cam_periph_unlock(periph);
1782 case CDIOCPLAYTRACKS:
1784 struct ioc_play_track *args
1785 = (struct ioc_play_track *) addr;
1786 struct cd_mode_params params;
1787 union cd_pages *page;
1789 params.alloc_len = sizeof(union cd_mode_data_6_10);
1790 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1793 cam_periph_lock(periph);
1794 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1795 ("trying to do CDIOCPLAYTRACKS\n"));
1797 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1799 free(params.mode_buf, M_SCSICD);
1800 cam_periph_unlock(periph);
1803 page = cdgetpage(¶ms);
1805 page->audio.flags &= ~CD_PA_SOTC;
1806 page->audio.flags |= CD_PA_IMMED;
1807 error = cdsetmode(periph, ¶ms);
1808 free(params.mode_buf, M_SCSICD);
1810 cam_periph_unlock(periph);
1815 * This was originally implemented with the PLAY
1816 * AUDIO TRACK INDEX command, but that command was
1817 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1818 * drives support it but ATAPI and ATAPI-derivative
1819 * drives don't seem to support it. So we keep a
1820 * cache of the table of contents and translate
1821 * track numbers to MSF format.
1823 if (softc->flags & CD_FLAG_VALID_TOC) {
1824 union msf_lba *sentry, *eentry;
1827 if (args->end_track <
1828 softc->toc.header.ending_track + 1)
1830 if (args->end_track >
1831 softc->toc.header.ending_track + 1)
1833 softc->toc.header.ending_track + 1;
1834 st = args->start_track -
1835 softc->toc.header.starting_track;
1836 et = args->end_track -
1837 softc->toc.header.starting_track;
1840 || (st > (softc->toc.header.ending_track -
1841 softc->toc.header.starting_track))) {
1843 cam_periph_unlock(periph);
1846 sentry = &softc->toc.entries[st].addr;
1847 eentry = &softc->toc.entries[et].addr;
1848 error = cdplaymsf(periph,
1857 * If we don't have a valid TOC, try the
1858 * play track index command. It is part of
1859 * the SCSI-2 spec, but was removed in the
1860 * MMC specs. ATAPI and ATAPI-derived
1861 * drives don't support it.
1863 if (softc->quirks & CD_Q_BCD_TRACKS) {
1865 bin2bcd(args->start_track);
1867 bin2bcd(args->end_track);
1869 error = cdplaytracks(periph,
1875 cam_periph_unlock(periph);
1880 struct ioc_play_msf *args
1881 = (struct ioc_play_msf *) addr;
1882 struct cd_mode_params params;
1883 union cd_pages *page;
1885 params.alloc_len = sizeof(union cd_mode_data_6_10);
1886 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1889 cam_periph_lock(periph);
1890 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1891 ("trying to do CDIOCPLAYMSF\n"));
1893 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1895 free(params.mode_buf, M_SCSICD);
1896 cam_periph_unlock(periph);
1899 page = cdgetpage(¶ms);
1901 page->audio.flags &= ~CD_PA_SOTC;
1902 page->audio.flags |= CD_PA_IMMED;
1903 error = cdsetmode(periph, ¶ms);
1904 free(params.mode_buf, M_SCSICD);
1906 cam_periph_unlock(periph);
1909 error = cdplaymsf(periph,
1916 cam_periph_unlock(periph);
1919 case CDIOCPLAYBLOCKS:
1921 struct ioc_play_blocks *args
1922 = (struct ioc_play_blocks *) addr;
1923 struct cd_mode_params params;
1924 union cd_pages *page;
1926 params.alloc_len = sizeof(union cd_mode_data_6_10);
1927 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1930 cam_periph_lock(periph);
1931 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1932 ("trying to do CDIOCPLAYBLOCKS\n"));
1935 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1937 free(params.mode_buf, M_SCSICD);
1938 cam_periph_unlock(periph);
1941 page = cdgetpage(¶ms);
1943 page->audio.flags &= ~CD_PA_SOTC;
1944 page->audio.flags |= CD_PA_IMMED;
1945 error = cdsetmode(periph, ¶ms);
1946 free(params.mode_buf, M_SCSICD);
1948 cam_periph_unlock(periph);
1951 error = cdplay(periph, args->blk, args->len);
1952 cam_periph_unlock(periph);
1955 case CDIOCREADSUBCHANNEL:
1957 struct ioc_read_subchannel *args
1958 = (struct ioc_read_subchannel *) addr;
1959 struct cd_sub_channel_info *data;
1960 u_int32_t len = args->data_len;
1962 data = malloc(sizeof(struct cd_sub_channel_info),
1963 M_SCSICD, M_WAITOK | M_ZERO);
1965 cam_periph_lock(periph);
1966 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1967 ("trying to do CDIOCREADSUBCHANNEL\n"));
1969 if ((len > sizeof(struct cd_sub_channel_info)) ||
1970 (len < sizeof(struct cd_sub_channel_header))) {
1972 "scsi_cd: cdioctl: "
1973 "cdioreadsubchannel: error, len=%d\n",
1976 free(data, M_SCSICD);
1977 cam_periph_unlock(periph);
1981 if (softc->quirks & CD_Q_BCD_TRACKS)
1982 args->track = bin2bcd(args->track);
1984 error = cdreadsubchannel(periph, args->address_format,
1985 args->data_format, args->track, data, len);
1988 free(data, M_SCSICD);
1989 cam_periph_unlock(periph);
1992 if (softc->quirks & CD_Q_BCD_TRACKS)
1993 data->what.track_info.track_number =
1994 bcd2bin(data->what.track_info.track_number);
1995 len = min(len, ((data->header.data_len[0] << 8) +
1996 data->header.data_len[1] +
1997 sizeof(struct cd_sub_channel_header)));
1998 cam_periph_unlock(periph);
1999 error = copyout(data, args->data, len);
2000 free(data, M_SCSICD);
2004 case CDIOREADTOCHEADER:
2006 struct ioc_toc_header *th;
2008 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2011 cam_periph_lock(periph);
2012 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2013 ("trying to do CDIOREADTOCHEADER\n"));
2015 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2016 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2019 cam_periph_unlock(periph);
2022 if (softc->quirks & CD_Q_BCD_TRACKS) {
2023 /* we are going to have to convert the BCD
2024 * encoding on the cd to what is expected
2026 th->starting_track =
2027 bcd2bin(th->starting_track);
2028 th->ending_track = bcd2bin(th->ending_track);
2030 th->len = ntohs(th->len);
2031 bcopy(th, addr, sizeof(*th));
2033 cam_periph_unlock(periph);
2036 case CDIOREADTOCENTRYS:
2037 #ifdef COMPAT_FREEBSD32
2038 case CDIOREADTOCENTRYS_32:
2041 struct cd_tocdata *data;
2042 struct cd_toc_single *lead;
2043 struct ioc_read_toc_entry *te =
2044 (struct ioc_read_toc_entry *) addr;
2045 struct ioc_toc_header *th;
2046 u_int32_t len, readlen, idx, num;
2047 u_int32_t starting_track = te->starting_track;
2049 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2050 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2052 cam_periph_lock(periph);
2053 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2054 ("trying to do CDIOREADTOCENTRYS\n"));
2056 if (te->data_len < sizeof(struct cd_toc_entry)
2057 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2058 || (te->address_format != CD_MSF_FORMAT
2059 && te->address_format != CD_LBA_FORMAT)) {
2061 printf("scsi_cd: error in readtocentries, "
2062 "returning EINVAL\n");
2063 free(data, M_SCSICD);
2064 free(lead, M_SCSICD);
2065 cam_periph_unlock(periph);
2070 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2071 sizeof (*th), /*sense_flags*/0);
2073 free(data, M_SCSICD);
2074 free(lead, M_SCSICD);
2075 cam_periph_unlock(periph);
2079 if (softc->quirks & CD_Q_BCD_TRACKS) {
2080 /* we are going to have to convert the BCD
2081 * encoding on the cd to what is expected
2083 th->starting_track =
2084 bcd2bin(th->starting_track);
2085 th->ending_track = bcd2bin(th->ending_track);
2088 if (starting_track == 0)
2089 starting_track = th->starting_track;
2090 else if (starting_track == LEADOUT)
2091 starting_track = th->ending_track + 1;
2092 else if (starting_track < th->starting_track ||
2093 starting_track > th->ending_track + 1) {
2094 printf("scsi_cd: error in readtocentries, "
2095 "returning EINVAL\n");
2096 free(data, M_SCSICD);
2097 free(lead, M_SCSICD);
2098 cam_periph_unlock(periph);
2103 /* calculate reading length without leadout entry */
2104 readlen = (th->ending_track - starting_track + 1) *
2105 sizeof(struct cd_toc_entry);
2107 /* and with leadout entry */
2108 len = readlen + sizeof(struct cd_toc_entry);
2109 if (te->data_len < len) {
2114 if (len > sizeof(data->entries)) {
2115 printf("scsi_cd: error in readtocentries, "
2116 "returning EINVAL\n");
2118 free(data, M_SCSICD);
2119 free(lead, M_SCSICD);
2120 cam_periph_unlock(periph);
2123 num = len / sizeof(struct cd_toc_entry);
2126 error = cdreadtoc(periph, te->address_format,
2129 readlen + sizeof (*th),
2132 free(data, M_SCSICD);
2133 free(lead, M_SCSICD);
2134 cam_periph_unlock(periph);
2139 /* make leadout entry if needed */
2140 idx = starting_track + num - 1;
2141 if (softc->quirks & CD_Q_BCD_TRACKS)
2142 th->ending_track = bcd2bin(th->ending_track);
2143 if (idx == th->ending_track + 1) {
2144 error = cdreadtoc(periph, te->address_format,
2145 LEADOUT, (u_int8_t *)lead,
2149 free(data, M_SCSICD);
2150 free(lead, M_SCSICD);
2151 cam_periph_unlock(periph);
2154 data->entries[idx - starting_track] =
2157 if (softc->quirks & CD_Q_BCD_TRACKS) {
2158 for (idx = 0; idx < num - 1; idx++) {
2159 data->entries[idx].track =
2160 bcd2bin(data->entries[idx].track);
2164 cam_periph_unlock(periph);
2165 error = copyout(data->entries, te_data_get_ptr(te, cmd),
2167 free(data, M_SCSICD);
2168 free(lead, M_SCSICD);
2171 case CDIOREADTOCENTRY:
2173 struct cd_toc_single *data;
2174 struct ioc_read_toc_single_entry *te =
2175 (struct ioc_read_toc_single_entry *) addr;
2176 struct ioc_toc_header *th;
2179 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2181 cam_periph_lock(periph);
2182 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2183 ("trying to do CDIOREADTOCENTRY\n"));
2185 if (te->address_format != CD_MSF_FORMAT
2186 && te->address_format != CD_LBA_FORMAT) {
2187 printf("error in readtocentry, "
2188 " returning EINVAL\n");
2189 free(data, M_SCSICD);
2191 cam_periph_unlock(periph);
2196 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2197 sizeof (*th), /*sense_flags*/0);
2199 free(data, M_SCSICD);
2200 cam_periph_unlock(periph);
2204 if (softc->quirks & CD_Q_BCD_TRACKS) {
2205 /* we are going to have to convert the BCD
2206 * encoding on the cd to what is expected
2208 th->starting_track =
2209 bcd2bin(th->starting_track);
2210 th->ending_track = bcd2bin(th->ending_track);
2214 track = th->starting_track;
2215 else if (track == LEADOUT)
2217 else if (track < th->starting_track ||
2218 track > th->ending_track + 1) {
2219 printf("error in readtocentry, "
2220 " returning EINVAL\n");
2221 free(data, M_SCSICD);
2223 cam_periph_unlock(periph);
2227 error = cdreadtoc(periph, te->address_format, track,
2228 (u_int8_t *)data, sizeof(*data),
2231 free(data, M_SCSICD);
2232 cam_periph_unlock(periph);
2236 if (softc->quirks & CD_Q_BCD_TRACKS)
2237 data->entry.track = bcd2bin(data->entry.track);
2238 bcopy(&data->entry, &te->entry,
2239 sizeof(struct cd_toc_entry));
2240 free(data, M_SCSICD);
2241 cam_periph_unlock(periph);
2246 struct ioc_patch *arg = (struct ioc_patch *)addr;
2247 struct cd_mode_params params;
2248 union cd_pages *page;
2250 params.alloc_len = sizeof(union cd_mode_data_6_10);
2251 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2254 cam_periph_lock(periph);
2255 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2256 ("trying to do CDIOCSETPATCH\n"));
2258 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2260 free(params.mode_buf, M_SCSICD);
2261 cam_periph_unlock(periph);
2264 page = cdgetpage(¶ms);
2266 page->audio.port[LEFT_PORT].channels =
2268 page->audio.port[RIGHT_PORT].channels =
2270 page->audio.port[2].channels = arg->patch[2];
2271 page->audio.port[3].channels = arg->patch[3];
2272 error = cdsetmode(periph, ¶ms);
2273 free(params.mode_buf, M_SCSICD);
2274 cam_periph_unlock(periph);
2279 struct ioc_vol *arg = (struct ioc_vol *) addr;
2280 struct cd_mode_params params;
2281 union cd_pages *page;
2283 params.alloc_len = sizeof(union cd_mode_data_6_10);
2284 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2287 cam_periph_lock(periph);
2288 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2289 ("trying to do CDIOCGETVOL\n"));
2291 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2293 free(params.mode_buf, M_SCSICD);
2294 cam_periph_unlock(periph);
2297 page = cdgetpage(¶ms);
2299 arg->vol[LEFT_PORT] =
2300 page->audio.port[LEFT_PORT].volume;
2301 arg->vol[RIGHT_PORT] =
2302 page->audio.port[RIGHT_PORT].volume;
2303 arg->vol[2] = page->audio.port[2].volume;
2304 arg->vol[3] = page->audio.port[3].volume;
2305 free(params.mode_buf, M_SCSICD);
2306 cam_periph_unlock(periph);
2311 struct ioc_vol *arg = (struct ioc_vol *) addr;
2312 struct cd_mode_params params;
2313 union cd_pages *page;
2315 params.alloc_len = sizeof(union cd_mode_data_6_10);
2316 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2319 cam_periph_lock(periph);
2320 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2321 ("trying to do CDIOCSETVOL\n"));
2323 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2325 free(params.mode_buf, M_SCSICD);
2326 cam_periph_unlock(periph);
2329 page = cdgetpage(¶ms);
2331 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2332 page->audio.port[LEFT_PORT].volume =
2333 arg->vol[LEFT_PORT];
2334 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2335 page->audio.port[RIGHT_PORT].volume =
2336 arg->vol[RIGHT_PORT];
2337 page->audio.port[2].volume = arg->vol[2];
2338 page->audio.port[3].volume = arg->vol[3];
2339 error = cdsetmode(periph, ¶ms);
2340 cam_periph_unlock(periph);
2341 free(params.mode_buf, M_SCSICD);
2346 struct cd_mode_params params;
2347 union cd_pages *page;
2349 params.alloc_len = sizeof(union cd_mode_data_6_10);
2350 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2353 cam_periph_lock(periph);
2354 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2355 ("trying to do CDIOCSETMONO\n"));
2357 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2359 free(params.mode_buf, M_SCSICD);
2360 cam_periph_unlock(periph);
2363 page = cdgetpage(¶ms);
2365 page->audio.port[LEFT_PORT].channels =
2366 LEFT_CHANNEL | RIGHT_CHANNEL;
2367 page->audio.port[RIGHT_PORT].channels =
2368 LEFT_CHANNEL | RIGHT_CHANNEL;
2369 page->audio.port[2].channels = 0;
2370 page->audio.port[3].channels = 0;
2371 error = cdsetmode(periph, ¶ms);
2372 cam_periph_unlock(periph);
2373 free(params.mode_buf, M_SCSICD);
2376 case CDIOCSETSTEREO:
2378 struct cd_mode_params params;
2379 union cd_pages *page;
2381 params.alloc_len = sizeof(union cd_mode_data_6_10);
2382 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2385 cam_periph_lock(periph);
2386 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2387 ("trying to do CDIOCSETSTEREO\n"));
2389 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2391 free(params.mode_buf, M_SCSICD);
2392 cam_periph_unlock(periph);
2395 page = cdgetpage(¶ms);
2397 page->audio.port[LEFT_PORT].channels =
2399 page->audio.port[RIGHT_PORT].channels =
2401 page->audio.port[2].channels = 0;
2402 page->audio.port[3].channels = 0;
2403 error = cdsetmode(periph, ¶ms);
2404 free(params.mode_buf, M_SCSICD);
2405 cam_periph_unlock(periph);
2410 struct cd_mode_params params;
2411 union cd_pages *page;
2413 params.alloc_len = sizeof(union cd_mode_data_6_10);
2414 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2417 cam_periph_lock(periph);
2418 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2419 ("trying to do CDIOCSETMUTE\n"));
2421 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2423 free(params.mode_buf, M_SCSICD);
2424 cam_periph_unlock(periph);
2427 page = cdgetpage(¶ms);
2429 page->audio.port[LEFT_PORT].channels = 0;
2430 page->audio.port[RIGHT_PORT].channels = 0;
2431 page->audio.port[2].channels = 0;
2432 page->audio.port[3].channels = 0;
2433 error = cdsetmode(periph, ¶ms);
2434 free(params.mode_buf, M_SCSICD);
2435 cam_periph_unlock(periph);
2440 struct cd_mode_params params;
2441 union cd_pages *page;
2443 params.alloc_len = sizeof(union cd_mode_data_6_10);
2444 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2447 cam_periph_lock(periph);
2448 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2449 ("trying to do CDIOCSETLEFT\n"));
2451 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2453 free(params.mode_buf, M_SCSICD);
2454 cam_periph_unlock(periph);
2457 page = cdgetpage(¶ms);
2459 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2460 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2461 page->audio.port[2].channels = 0;
2462 page->audio.port[3].channels = 0;
2463 error = cdsetmode(periph, ¶ms);
2464 free(params.mode_buf, M_SCSICD);
2465 cam_periph_unlock(periph);
2470 struct cd_mode_params params;
2471 union cd_pages *page;
2473 params.alloc_len = sizeof(union cd_mode_data_6_10);
2474 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2477 cam_periph_lock(periph);
2478 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2479 ("trying to do CDIOCSETRIGHT\n"));
2481 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2483 free(params.mode_buf, M_SCSICD);
2484 cam_periph_unlock(periph);
2487 page = cdgetpage(¶ms);
2489 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2490 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2491 page->audio.port[2].channels = 0;
2492 page->audio.port[3].channels = 0;
2493 error = cdsetmode(periph, ¶ms);
2494 free(params.mode_buf, M_SCSICD);
2495 cam_periph_unlock(periph);
2499 cam_periph_lock(periph);
2500 error = cdpause(periph, 1);
2501 cam_periph_unlock(periph);
2504 cam_periph_lock(periph);
2505 error = cdpause(periph, 0);
2506 cam_periph_unlock(periph);
2509 cam_periph_lock(periph);
2510 error = cdstartunit(periph, 0);
2511 cam_periph_unlock(periph);
2514 cam_periph_lock(periph);
2515 error = cdstartunit(periph, 1);
2516 cam_periph_unlock(periph);
2519 cam_periph_lock(periph);
2520 error = cdstopunit(periph, 0);
2521 cam_periph_unlock(periph);
2524 cam_periph_lock(periph);
2525 error = cdstopunit(periph, 1);
2526 cam_periph_unlock(periph);
2529 cam_periph_lock(periph);
2530 cdprevent(periph, PR_ALLOW);
2531 cam_periph_unlock(periph);
2534 cam_periph_lock(periph);
2535 cdprevent(periph, PR_PREVENT);
2536 cam_periph_unlock(periph);
2539 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2543 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2547 /* return (cd_reset(periph)); */
2550 case CDRIOCREADSPEED:
2551 cam_periph_lock(periph);
2552 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2553 cam_periph_unlock(periph);
2555 case CDRIOCWRITESPEED:
2556 cam_periph_lock(periph);
2557 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2558 cam_periph_unlock(periph);
2560 case CDRIOCGETBLOCKSIZE:
2561 *(int *)addr = softc->params.blksize;
2563 case CDRIOCSETBLOCKSIZE:
2564 if (*(int *)addr <= 0) {
2568 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2571 case DVDIOCREPORTKEY: {
2572 struct dvd_authinfo *authinfo;
2574 authinfo = (struct dvd_authinfo *)addr;
2576 if (cmd == DVDIOCREPORTKEY)
2577 error = cdreportkey(periph, authinfo);
2579 error = cdsendkey(periph, authinfo);
2582 case DVDIOCREADSTRUCTURE: {
2583 struct dvd_struct *dvdstruct;
2585 dvdstruct = (struct dvd_struct *)addr;
2587 error = cdreaddvdstructure(periph, dvdstruct);
2592 cam_periph_lock(periph);
2593 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2594 cam_periph_unlock(periph);
2598 cam_periph_lock(periph);
2599 cam_periph_unhold(periph);
2601 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2602 if (error && bootverbose) {
2603 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2605 cam_periph_unlock(periph);
2611 cdprevent(struct cam_periph *periph, int action)
2614 struct cd_softc *softc;
2617 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2619 softc = (struct cd_softc *)periph->softc;
2621 if (((action == PR_ALLOW)
2622 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2623 || ((action == PR_PREVENT)
2624 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2628 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2630 scsi_prevent(&ccb->csio,
2631 /*retries*/ cd_retry_count,
2636 /* timeout */60000);
2638 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2639 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2641 xpt_release_ccb(ccb);
2644 if (action == PR_ALLOW)
2645 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2647 softc->flags |= CD_FLAG_DISC_LOCKED;
2652 cdmediaprobedone(struct cam_periph *periph)
2654 struct cd_softc *softc;
2656 softc = (struct cd_softc *)periph->softc;
2658 softc->flags &= ~CD_FLAG_MEDIA_SCAN_ACT;
2660 if ((softc->flags & CD_FLAG_MEDIA_WAIT) != 0) {
2661 softc->flags &= ~CD_FLAG_MEDIA_WAIT;
2662 wakeup(&softc->toc);
2667 * XXX: the disk media and sector size is only really able to change
2668 * XXX: while the device is closed.
2672 cdcheckmedia(struct cam_periph *periph, int do_wait)
2674 struct cd_softc *softc;
2677 softc = (struct cd_softc *)periph->softc;
2681 && ((softc->flags & CD_FLAG_MEDIA_WAIT) == 0)) {
2682 softc->flags |= CD_FLAG_MEDIA_WAIT;
2684 if ((softc->flags & CD_FLAG_MEDIA_SCAN_ACT) == 0) {
2685 softc->state = CD_STATE_MEDIA_PREVENT;
2686 softc->flags |= CD_FLAG_MEDIA_SCAN_ACT;
2687 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2693 error = msleep(&softc->toc, cam_periph_mtx(periph), PRIBIO,"cdmedia",0);
2699 * Check to see whether we have a valid size from the media. We
2700 * may or may not have a valid TOC.
2702 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
2711 cdcheckmedia(struct cam_periph *periph)
2713 struct cd_softc *softc;
2714 struct ioc_toc_header *toch;
2715 struct cd_toc_single leadout;
2716 u_int32_t size, toclen;
2717 int error, num_entries, cdindex;
2719 softc = (struct cd_softc *)periph->softc;
2721 cdprevent(periph, PR_PREVENT);
2722 softc->disk->d_sectorsize = 2048;
2723 softc->disk->d_mediasize = 0;
2726 * Get the disc size and block size. If we can't get it, we don't
2727 * have media, most likely.
2729 if ((error = cdsize(periph, &size)) != 0) {
2730 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2731 cdprevent(periph, PR_ALLOW);
2734 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2735 softc->disk->d_sectorsize = softc->params.blksize;
2736 softc->disk->d_mediasize =
2737 (off_t)softc->params.blksize * softc->params.disksize;
2741 * Now we check the table of contents. This (currently) is only
2742 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2743 * things like present a separate entry in /dev for each track,
2744 * like that acd(4) driver does.
2746 bzero(&softc->toc, sizeof(softc->toc));
2747 toch = &softc->toc.header;
2749 * We will get errors here for media that doesn't have a table of
2750 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2751 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2752 * has not been recorded (no complete session) and the Format codes
2753 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2754 * with an INVALID FIELD IN CDB. Devices that are not capable of
2755 * reading an incomplete session on DDC/CD-R/RW media shall report
2756 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2758 * So this isn't fatal if we can't read the table of contents, it
2759 * just means that the user won't be able to issue the play tracks
2760 * ioctl, and likely lots of other stuff won't work either. They
2761 * need to burn the CD before we can do a whole lot with it. So
2762 * we don't print anything here if we get an error back.
2764 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2767 * Errors in reading the table of contents aren't fatal, we just
2768 * won't have a valid table of contents cached.
2772 bzero(&softc->toc, sizeof(softc->toc));
2776 if (softc->quirks & CD_Q_BCD_TRACKS) {
2777 toch->starting_track = bcd2bin(toch->starting_track);
2778 toch->ending_track = bcd2bin(toch->ending_track);
2781 /* Number of TOC entries, plus leadout */
2782 num_entries = (toch->ending_track - toch->starting_track) + 2;
2784 if (num_entries <= 0)
2787 toclen = num_entries * sizeof(struct cd_toc_entry);
2789 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2790 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2794 bzero(&softc->toc, sizeof(softc->toc));
2798 if (softc->quirks & CD_Q_BCD_TRACKS) {
2799 toch->starting_track = bcd2bin(toch->starting_track);
2800 toch->ending_track = bcd2bin(toch->ending_track);
2803 * XXX KDM is this necessary? Probably only if the drive doesn't
2804 * return leadout information with the table of contents.
2806 cdindex = toch->starting_track + num_entries -1;
2807 if (cdindex == toch->ending_track + 1) {
2809 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2810 (u_int8_t *)&leadout, sizeof(leadout),
2816 softc->toc.entries[cdindex - toch->starting_track] =
2819 if (softc->quirks & CD_Q_BCD_TRACKS) {
2820 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2821 softc->toc.entries[cdindex].track =
2822 bcd2bin(softc->toc.entries[cdindex].track);
2826 softc->flags |= CD_FLAG_VALID_TOC;
2828 /* If the first track is audio, correct sector size. */
2829 if ((softc->toc.entries[0].control & 4) == 0) {
2830 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2831 softc->disk->d_mediasize =
2832 (off_t)softc->params.blksize * softc->params.disksize;
2838 * We unconditionally (re)set the blocksize each time the
2839 * CD device is opened. This is because the CD can change,
2840 * and therefore the blocksize might change.
2841 * XXX problems here if some slice or partition is still
2842 * open with the old size?
2844 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2845 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2846 softc->disk->d_devstat->block_size = softc->params.blksize;
2852 cdsize(struct cam_periph *periph, u_int32_t *size)
2854 struct cd_softc *softc;
2856 struct scsi_read_capacity_data *rcap_buf;
2859 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2861 softc = (struct cd_softc *)periph->softc;
2863 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2865 /* XXX Should be M_WAITOK */
2866 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2867 M_SCSICD, M_NOWAIT | M_ZERO);
2868 if (rcap_buf == NULL)
2871 scsi_read_capacity(&ccb->csio,
2872 /*retries*/ cd_retry_count,
2877 /* timeout */20000);
2879 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2880 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2882 xpt_release_ccb(ccb);
2884 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2885 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2886 /* Make sure we got at least some block size. */
2887 if (error == 0 && softc->params.blksize == 0)
2890 * SCSI-3 mandates that the reported blocksize shall be 2048.
2891 * Older drives sometimes report funny values, trim it down to
2892 * 2048, or other parts of the kernel will get confused.
2894 * XXX we leave drives alone that might report 512 bytes, as
2895 * well as drives reporting more weird sizes like perhaps 4K.
2897 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2898 softc->params.blksize = 2048;
2900 free(rcap_buf, M_SCSICD);
2901 *size = softc->params.disksize;
2909 cd6byteworkaround(union ccb *ccb)
2912 struct cam_periph *periph;
2913 struct cd_softc *softc;
2914 struct cd_mode_params *params;
2917 periph = xpt_path_periph(ccb->ccb_h.path);
2918 softc = (struct cd_softc *)periph->softc;
2920 cdb = ccb->csio.cdb_io.cdb_bytes;
2922 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2923 || ((cdb[0] != MODE_SENSE_6)
2924 && (cdb[0] != MODE_SELECT_6)))
2928 * Because there is no convenient place to stash the overall
2929 * cd_mode_params structure pointer, we have to grab it like this.
2930 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2931 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2933 * XXX It would be nice if, at some point, we could increase the
2934 * number of available peripheral private pointers. Both pointers
2935 * are currently used in most every peripheral driver.
2939 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2940 if (params->mode_buf == ccb->csio.data_ptr) {
2947 * This shouldn't happen. All mode sense and mode select
2948 * operations in the cd(4) driver MUST go through cdgetmode() and
2952 xpt_print(periph->path,
2953 "mode buffer not found in mode queue!\n");
2957 params->cdb_size = 10;
2958 softc->minimum_command_size = 10;
2959 xpt_print(ccb->ccb_h.path,
2960 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2961 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2963 if (cdb[0] == MODE_SENSE_6) {
2964 struct scsi_mode_sense_10 ms10;
2965 struct scsi_mode_sense_6 *ms6;
2968 ms6 = (struct scsi_mode_sense_6 *)cdb;
2970 bzero(&ms10, sizeof(ms10));
2971 ms10.opcode = MODE_SENSE_10;
2972 ms10.byte2 = ms6->byte2;
2973 ms10.page = ms6->page;
2976 * 10 byte mode header, block descriptor,
2977 * sizeof(union cd_pages)
2979 len = sizeof(struct cd_mode_data_10);
2980 ccb->csio.dxfer_len = len;
2982 scsi_ulto2b(len, ms10.length);
2983 ms10.control = ms6->control;
2984 bcopy(&ms10, cdb, 10);
2985 ccb->csio.cdb_len = 10;
2987 struct scsi_mode_select_10 ms10;
2988 struct scsi_mode_select_6 *ms6;
2989 struct scsi_mode_header_6 *header6;
2990 struct scsi_mode_header_10 *header10;
2991 struct scsi_mode_page_header *page_header;
2992 int blk_desc_len, page_num, page_size, len;
2994 ms6 = (struct scsi_mode_select_6 *)cdb;
2996 bzero(&ms10, sizeof(ms10));
2997 ms10.opcode = MODE_SELECT_10;
2998 ms10.byte2 = ms6->byte2;
3000 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3001 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3003 page_header = find_mode_page_6(header6);
3004 page_num = page_header->page_code;
3006 blk_desc_len = header6->blk_desc_len;
3008 page_size = cdgetpagesize(page_num);
3010 if (page_size != (page_header->page_length +
3011 sizeof(*page_header)))
3012 page_size = page_header->page_length +
3013 sizeof(*page_header);
3015 len = sizeof(*header10) + blk_desc_len + page_size;
3017 len = min(params->alloc_len, len);
3020 * Since the 6 byte parameter header is shorter than the 10
3021 * byte parameter header, we need to copy the actual mode
3022 * page data, and the block descriptor, if any, so things wind
3023 * up in the right place. The regions will overlap, but
3024 * bcopy() does the right thing.
3026 bcopy(params->mode_buf + sizeof(*header6),
3027 params->mode_buf + sizeof(*header10),
3028 len - sizeof(*header10));
3030 /* Make sure these fields are set correctly. */
3031 scsi_ulto2b(0, header10->data_length);
3032 header10->medium_type = 0;
3033 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3035 ccb->csio.dxfer_len = len;
3037 scsi_ulto2b(len, ms10.length);
3038 ms10.control = ms6->control;
3039 bcopy(&ms10, cdb, 10);
3040 ccb->csio.cdb_len = 10;
3043 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3044 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3047 cam_release_devq(ccb->ccb_h.path,
3051 /*getcount_only*/0);
3058 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3060 struct cd_softc *softc;
3061 struct cam_periph *periph;
3062 int error, error_code, sense_key, asc, ascq;
3064 periph = xpt_path_periph(ccb->ccb_h.path);
3065 softc = (struct cd_softc *)periph->softc;
3070 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3071 * CDB comes back with this particular error, try transforming it
3072 * into the 10 byte version.
3074 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3075 error = cd6byteworkaround(ccb);
3076 } else if (scsi_extract_sense_ccb(ccb,
3077 &error_code, &sense_key, &asc, &ascq)) {
3078 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3079 error = cd6byteworkaround(ccb);
3080 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3081 asc == 0x28 && ascq == 0x00)
3082 disk_media_changed(softc->disk, M_NOWAIT);
3083 else if (sense_key == SSD_KEY_NOT_READY &&
3084 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3085 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3086 disk_media_gone(softc->disk, M_NOWAIT);
3090 if (error == ERESTART)
3095 * Until we have a better way of doing pack validation,
3096 * don't treat UAs as errors.
3098 sense_flags |= SF_RETRY_UA;
3100 if (softc->quirks & CD_Q_RETRY_BUSY)
3101 sense_flags |= SF_RETRY_BUSY;
3102 return (cam_periph_error(ccb, cam_flags, sense_flags));
3106 cdmediapoll(void *arg)
3108 struct cam_periph *periph = arg;
3109 struct cd_softc *softc = periph->softc;
3111 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3112 softc->outstanding_cmds == 0) {
3113 if (cam_periph_acquire(periph) == 0) {
3115 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3118 /* Queue us up again */
3119 if (cd_poll_period != 0)
3120 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3124 * Read table of contents
3127 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3128 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3131 struct ccb_scsiio *csio;
3138 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3143 /* retries */ cd_retry_count,
3145 /* tag_action */ MSG_SIMPLE_Q_TAG,
3146 /* byte1_flags */ (mode == CD_MSF_FORMAT) ? CD_MSF : 0,
3147 /* format */ SRTOC_FORMAT_TOC,
3149 /* data_ptr */ data,
3150 /* dxfer_len */ len,
3151 /* sense_len */ SSD_FULL_SIZE,
3152 /* timeout */ 50000);
3154 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3155 /*sense_flags*/SF_RETRY_UA | sense_flags);
3157 xpt_release_ccb(ccb);
3163 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3164 u_int32_t format, int track,
3165 struct cd_sub_channel_info *data, u_int32_t len)
3167 struct scsi_read_subchannel *scsi_cmd;
3168 struct ccb_scsiio *csio;
3174 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3179 /* retries */ cd_retry_count,
3181 /* flags */ CAM_DIR_IN,
3182 /* tag_action */ MSG_SIMPLE_Q_TAG,
3183 /* data_ptr */ (u_int8_t *)data,
3184 /* dxfer_len */ len,
3185 /* sense_len */ SSD_FULL_SIZE,
3186 sizeof(struct scsi_read_subchannel),
3187 /* timeout */ 50000);
3189 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3190 bzero (scsi_cmd, sizeof(*scsi_cmd));
3192 scsi_cmd->op_code = READ_SUBCHANNEL;
3193 if (mode == CD_MSF_FORMAT)
3194 scsi_cmd->byte1 |= CD_MSF;
3195 scsi_cmd->byte2 = SRS_SUBQ;
3196 scsi_cmd->subchan_format = format;
3197 scsi_cmd->track = track;
3198 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3199 scsi_cmd->control = 0;
3201 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3202 /*sense_flags*/SF_RETRY_UA);
3204 xpt_release_ccb(ccb);
3211 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3212 * routine. See comments in cd6byteworkaround() for details.
3215 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3218 struct ccb_scsiio *csio;
3219 struct cd_softc *softc;
3224 softc = (struct cd_softc *)periph->softc;
3226 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3230 data->cdb_size = softc->minimum_command_size;
3231 if (data->cdb_size < 10)
3232 param_len = sizeof(struct cd_mode_data);
3234 param_len = sizeof(struct cd_mode_data_10);
3236 /* Don't say we've got more room than we actually allocated */
3237 param_len = min(param_len, data->alloc_len);
3239 scsi_mode_sense_len(csio,
3240 /* retries */ cd_retry_count,
3242 /* tag_action */ MSG_SIMPLE_Q_TAG,
3244 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3246 /* param_buf */ data->mode_buf,
3247 /* param_len */ param_len,
3248 /* minimum_cmd_size */ softc->minimum_command_size,
3249 /* sense_len */ SSD_FULL_SIZE,
3250 /* timeout */ 50000);
3253 * It would be nice not to have to do this, but there's no
3254 * available pointer in the CCB that would allow us to stuff the
3255 * mode params structure in there and retrieve it in
3256 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3257 * lets the caller know what CDB size we ended up using, so they
3258 * can find the actual mode page offset.
3260 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3262 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3263 /*sense_flags*/SF_RETRY_UA);
3265 xpt_release_ccb(ccb);
3267 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3270 * This is a bit of belt-and-suspenders checking, but if we run
3271 * into a situation where the target sends back multiple block
3272 * descriptors, we might not have enough space in the buffer to
3273 * see the whole mode page. Better to return an error than
3274 * potentially access memory beyond our malloced region.
3279 if (data->cdb_size == 10) {
3280 struct scsi_mode_header_10 *hdr10;
3282 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3283 data_len = scsi_2btoul(hdr10->data_length);
3284 data_len += sizeof(hdr10->data_length);
3286 struct scsi_mode_header_6 *hdr6;
3288 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3289 data_len = hdr6->data_length;
3290 data_len += sizeof(hdr6->data_length);
3294 * Complain if there is more mode data available than we
3295 * allocated space for. This could potentially happen if
3296 * we miscalculated the page length for some reason, if the
3297 * drive returns multiple block descriptors, or if it sets
3298 * the data length incorrectly.
3300 if (data_len > data->alloc_len) {
3301 xpt_print(periph->path, "allocated modepage %d length "
3302 "%d < returned length %d\n", page, data->alloc_len,
3311 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3312 * routine. See comments in cd6byteworkaround() for details.
3315 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3317 struct ccb_scsiio *csio;
3318 struct cd_softc *softc;
3320 int cdb_size, param_len;
3323 softc = (struct cd_softc *)periph->softc;
3325 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3332 * If the data is formatted for the 10 byte version of the mode
3333 * select parameter list, we need to use the 10 byte CDB.
3334 * Otherwise, we use whatever the stored minimum command size.
3336 if (data->cdb_size == 10)
3337 cdb_size = data->cdb_size;
3339 cdb_size = softc->minimum_command_size;
3341 if (cdb_size >= 10) {
3342 struct scsi_mode_header_10 *mode_header;
3345 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3347 data_len = scsi_2btoul(mode_header->data_length);
3349 scsi_ulto2b(0, mode_header->data_length);
3351 * SONY drives do not allow a mode select with a medium_type
3352 * value that has just been returned by a mode sense; use a
3353 * medium_type of 0 (Default) instead.
3355 mode_header->medium_type = 0;
3358 * Pass back whatever the drive passed to us, plus the size
3359 * of the data length field.
3361 param_len = data_len + sizeof(mode_header->data_length);
3364 struct scsi_mode_header_6 *mode_header;
3366 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3368 param_len = mode_header->data_length + 1;
3370 mode_header->data_length = 0;
3372 * SONY drives do not allow a mode select with a medium_type
3373 * value that has just been returned by a mode sense; use a
3374 * medium_type of 0 (Default) instead.
3376 mode_header->medium_type = 0;
3379 /* Don't say we've got more room than we actually allocated */
3380 param_len = min(param_len, data->alloc_len);
3382 scsi_mode_select_len(csio,
3383 /* retries */ cd_retry_count,
3385 /* tag_action */ MSG_SIMPLE_Q_TAG,
3386 /* scsi_page_fmt */ 1,
3388 /* param_buf */ data->mode_buf,
3389 /* param_len */ param_len,
3390 /* minimum_cmd_size */ cdb_size,
3391 /* sense_len */ SSD_FULL_SIZE,
3392 /* timeout */ 50000);
3394 /* See comments in cdgetmode() and cd6byteworkaround(). */
3395 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3397 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3398 /*sense_flags*/SF_RETRY_UA);
3400 xpt_release_ccb(ccb);
3402 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3409 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3411 struct ccb_scsiio *csio;
3417 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3420 * Use the smallest possible command to perform the operation.
3422 if ((len & 0xffff0000) == 0) {
3424 * We can fit in a 10 byte cdb.
3426 struct scsi_play_10 *scsi_cmd;
3428 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3429 bzero (scsi_cmd, sizeof(*scsi_cmd));
3430 scsi_cmd->op_code = PLAY_10;
3431 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3432 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3433 cdb_len = sizeof(*scsi_cmd);
3435 struct scsi_play_12 *scsi_cmd;
3437 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3438 bzero (scsi_cmd, sizeof(*scsi_cmd));
3439 scsi_cmd->op_code = PLAY_12;
3440 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3441 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3442 cdb_len = sizeof(*scsi_cmd);
3445 /*retries*/ cd_retry_count,
3447 /*flags*/CAM_DIR_NONE,
3451 /*sense_len*/SSD_FULL_SIZE,
3453 /*timeout*/50 * 1000);
3455 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3456 /*sense_flags*/SF_RETRY_UA);
3458 xpt_release_ccb(ccb);
3464 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3465 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3467 struct scsi_play_msf *scsi_cmd;
3468 struct ccb_scsiio *csio;
3474 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3479 /* retries */ cd_retry_count,
3481 /* flags */ CAM_DIR_NONE,
3482 /* tag_action */ MSG_SIMPLE_Q_TAG,
3483 /* data_ptr */ NULL,
3485 /* sense_len */ SSD_FULL_SIZE,
3486 sizeof(struct scsi_play_msf),
3487 /* timeout */ 50000);
3489 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3490 bzero (scsi_cmd, sizeof(*scsi_cmd));
3492 scsi_cmd->op_code = PLAY_MSF;
3493 scsi_cmd->start_m = startm;
3494 scsi_cmd->start_s = starts;
3495 scsi_cmd->start_f = startf;
3496 scsi_cmd->end_m = endm;
3497 scsi_cmd->end_s = ends;
3498 scsi_cmd->end_f = endf;
3500 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3501 /*sense_flags*/SF_RETRY_UA);
3503 xpt_release_ccb(ccb);
3510 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3511 u_int32_t etrack, u_int32_t eindex)
3513 struct scsi_play_track *scsi_cmd;
3514 struct ccb_scsiio *csio;
3520 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3525 /* retries */ cd_retry_count,
3527 /* flags */ CAM_DIR_NONE,
3528 /* tag_action */ MSG_SIMPLE_Q_TAG,
3529 /* data_ptr */ NULL,
3531 /* sense_len */ SSD_FULL_SIZE,
3532 sizeof(struct scsi_play_track),
3533 /* timeout */ 50000);
3535 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3536 bzero (scsi_cmd, sizeof(*scsi_cmd));
3538 scsi_cmd->op_code = PLAY_TRACK;
3539 scsi_cmd->start_track = strack;
3540 scsi_cmd->start_index = sindex;
3541 scsi_cmd->end_track = etrack;
3542 scsi_cmd->end_index = eindex;
3544 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3545 /*sense_flags*/SF_RETRY_UA);
3547 xpt_release_ccb(ccb);
3553 cdpause(struct cam_periph *periph, u_int32_t go)
3555 struct scsi_pause *scsi_cmd;
3556 struct ccb_scsiio *csio;
3562 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3567 /* retries */ cd_retry_count,
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_pause),
3575 /* timeout */ 50000);
3577 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3578 bzero (scsi_cmd, sizeof(*scsi_cmd));
3580 scsi_cmd->op_code = PAUSE;
3581 scsi_cmd->resume = go;
3583 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3584 /*sense_flags*/SF_RETRY_UA);
3586 xpt_release_ccb(ccb);
3592 cdstartunit(struct cam_periph *periph, int load)
3599 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3601 scsi_start_stop(&ccb->csio,
3602 /* retries */ cd_retry_count,
3604 /* tag_action */ MSG_SIMPLE_Q_TAG,
3606 /* load_eject */ load,
3607 /* immediate */ FALSE,
3608 /* sense_len */ SSD_FULL_SIZE,
3609 /* timeout */ 50000);
3611 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3612 /*sense_flags*/SF_RETRY_UA);
3614 xpt_release_ccb(ccb);
3620 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3627 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3629 scsi_start_stop(&ccb->csio,
3630 /* retries */ cd_retry_count,
3632 /* tag_action */ MSG_SIMPLE_Q_TAG,
3634 /* load_eject */ eject,
3635 /* immediate */ FALSE,
3636 /* sense_len */ SSD_FULL_SIZE,
3637 /* timeout */ 50000);
3639 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3640 /*sense_flags*/SF_RETRY_UA);
3642 xpt_release_ccb(ccb);
3648 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3650 struct scsi_set_speed *scsi_cmd;
3651 struct ccb_scsiio *csio;
3656 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3659 /* Preserve old behavior: units in multiples of CDROM speed */
3666 /* retries */ cd_retry_count,
3668 /* flags */ CAM_DIR_NONE,
3669 /* tag_action */ MSG_SIMPLE_Q_TAG,
3670 /* data_ptr */ NULL,
3672 /* sense_len */ SSD_FULL_SIZE,
3673 sizeof(struct scsi_set_speed),
3674 /* timeout */ 50000);
3676 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3677 bzero(scsi_cmd, sizeof(*scsi_cmd));
3679 scsi_cmd->opcode = SET_CD_SPEED;
3680 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3681 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3683 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3684 /*sense_flags*/SF_RETRY_UA);
3686 xpt_release_ccb(ccb);
3692 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3704 switch (authinfo->format) {
3705 case DVD_REPORT_AGID:
3706 length = sizeof(struct scsi_report_key_data_agid);
3708 case DVD_REPORT_CHALLENGE:
3709 length = sizeof(struct scsi_report_key_data_challenge);
3711 case DVD_REPORT_KEY1:
3712 length = sizeof(struct scsi_report_key_data_key1_key2);
3714 case DVD_REPORT_TITLE_KEY:
3715 length = sizeof(struct scsi_report_key_data_title);
3716 /* The lba field is only set for the title key */
3717 lba = authinfo->lba;
3719 case DVD_REPORT_ASF:
3720 length = sizeof(struct scsi_report_key_data_asf);
3722 case DVD_REPORT_RPC:
3723 length = sizeof(struct scsi_report_key_data_rpc);
3725 case DVD_INVALIDATE_AGID:
3733 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3737 cam_periph_lock(periph);
3738 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3740 scsi_report_key(&ccb->csio,
3741 /* retries */ cd_retry_count,
3743 /* tag_action */ MSG_SIMPLE_Q_TAG,
3745 /* agid */ authinfo->agid,
3746 /* key_format */ authinfo->format,
3747 /* data_ptr */ databuf,
3748 /* dxfer_len */ length,
3749 /* sense_len */ SSD_FULL_SIZE,
3750 /* timeout */ 50000);
3752 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3753 /*sense_flags*/SF_RETRY_UA);
3758 if (ccb->csio.resid != 0) {
3759 xpt_print(periph->path, "warning, residual for report key "
3760 "command is %d\n", ccb->csio.resid);
3763 switch(authinfo->format) {
3764 case DVD_REPORT_AGID: {
3765 struct scsi_report_key_data_agid *agid_data;
3767 agid_data = (struct scsi_report_key_data_agid *)databuf;
3769 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3773 case DVD_REPORT_CHALLENGE: {
3774 struct scsi_report_key_data_challenge *chal_data;
3776 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3778 bcopy(chal_data->challenge_key, authinfo->keychal,
3779 min(sizeof(chal_data->challenge_key),
3780 sizeof(authinfo->keychal)));
3783 case DVD_REPORT_KEY1: {
3784 struct scsi_report_key_data_key1_key2 *key1_data;
3786 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3788 bcopy(key1_data->key1, authinfo->keychal,
3789 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3792 case DVD_REPORT_TITLE_KEY: {
3793 struct scsi_report_key_data_title *title_data;
3795 title_data = (struct scsi_report_key_data_title *)databuf;
3797 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3798 RKD_TITLE_CPM_SHIFT;
3799 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3800 RKD_TITLE_CP_SEC_SHIFT;
3801 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3802 RKD_TITLE_CMGS_SHIFT;
3803 bcopy(title_data->title_key, authinfo->keychal,
3804 min(sizeof(title_data->title_key),
3805 sizeof(authinfo->keychal)));
3808 case DVD_REPORT_ASF: {
3809 struct scsi_report_key_data_asf *asf_data;
3811 asf_data = (struct scsi_report_key_data_asf *)databuf;
3813 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3816 case DVD_REPORT_RPC: {
3817 struct scsi_report_key_data_rpc *rpc_data;
3819 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3821 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3823 authinfo->vend_rsts =
3824 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3825 RKD_RPC_VENDOR_RESET_SHIFT;
3826 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3827 authinfo->region = rpc_data->region_mask;
3828 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3831 case DVD_INVALIDATE_AGID:
3834 /* This should be impossible, since we checked above */
3837 break; /* NOTREACHED */
3841 xpt_release_ccb(ccb);
3842 cam_periph_unlock(periph);
3844 if (databuf != NULL)
3845 free(databuf, M_DEVBUF);
3851 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3861 switch(authinfo->format) {
3862 case DVD_SEND_CHALLENGE: {
3863 struct scsi_report_key_data_challenge *challenge_data;
3865 length = sizeof(*challenge_data);
3867 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3869 databuf = (u_int8_t *)challenge_data;
3871 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3872 challenge_data->data_len);
3874 bcopy(authinfo->keychal, challenge_data->challenge_key,
3875 min(sizeof(authinfo->keychal),
3876 sizeof(challenge_data->challenge_key)));
3879 case DVD_SEND_KEY2: {
3880 struct scsi_report_key_data_key1_key2 *key2_data;
3882 length = sizeof(*key2_data);
3884 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3886 databuf = (u_int8_t *)key2_data;
3888 scsi_ulto2b(length - sizeof(key2_data->data_len),
3889 key2_data->data_len);
3891 bcopy(authinfo->keychal, key2_data->key1,
3892 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3896 case DVD_SEND_RPC: {
3897 struct scsi_send_key_data_rpc *rpc_data;
3899 length = sizeof(*rpc_data);
3901 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3903 databuf = (u_int8_t *)rpc_data;
3905 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3906 rpc_data->data_len);
3908 rpc_data->region_code = authinfo->region;
3915 cam_periph_lock(periph);
3916 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3918 scsi_send_key(&ccb->csio,
3919 /* retries */ cd_retry_count,
3921 /* tag_action */ MSG_SIMPLE_Q_TAG,
3922 /* agid */ authinfo->agid,
3923 /* key_format */ authinfo->format,
3924 /* data_ptr */ databuf,
3925 /* dxfer_len */ length,
3926 /* sense_len */ SSD_FULL_SIZE,
3927 /* timeout */ 50000);
3929 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3930 /*sense_flags*/SF_RETRY_UA);
3932 xpt_release_ccb(ccb);
3933 cam_periph_unlock(periph);
3935 if (databuf != NULL)
3936 free(databuf, M_DEVBUF);
3942 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3952 /* The address is reserved for many of the formats */
3955 switch(dvdstruct->format) {
3956 case DVD_STRUCT_PHYSICAL:
3957 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3959 case DVD_STRUCT_COPYRIGHT:
3960 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3962 case DVD_STRUCT_DISCKEY:
3963 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3965 case DVD_STRUCT_BCA:
3966 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3968 case DVD_STRUCT_MANUFACT:
3969 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3971 case DVD_STRUCT_CMI:
3973 case DVD_STRUCT_PROTDISCID:
3974 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3976 case DVD_STRUCT_DISCKEYBLOCK:
3977 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3979 case DVD_STRUCT_DDS:
3980 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3982 case DVD_STRUCT_MEDIUM_STAT:
3983 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3985 case DVD_STRUCT_SPARE_AREA:
3986 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3988 case DVD_STRUCT_RMD_LAST:
3990 case DVD_STRUCT_RMD_RMA:
3992 case DVD_STRUCT_PRERECORDED:
3993 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3995 case DVD_STRUCT_UNIQUEID:
3996 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3998 case DVD_STRUCT_DCB:
4000 case DVD_STRUCT_LIST:
4002 * This is the maximum allocation length for the READ DVD
4003 * STRUCTURE command. There's nothing in the MMC3 spec
4004 * that indicates a limit in the amount of data that can
4005 * be returned from this call, other than the limits
4006 * imposed by the 2-byte length variables.
4015 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4019 cam_periph_lock(periph);
4020 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4022 scsi_read_dvd_structure(&ccb->csio,
4023 /* retries */ cd_retry_count,
4025 /* tag_action */ MSG_SIMPLE_Q_TAG,
4027 /* layer_number */ dvdstruct->layer_num,
4028 /* key_format */ dvdstruct->format,
4029 /* agid */ dvdstruct->agid,
4030 /* data_ptr */ databuf,
4031 /* dxfer_len */ length,
4032 /* sense_len */ SSD_FULL_SIZE,
4033 /* timeout */ 50000);
4035 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4036 /*sense_flags*/SF_RETRY_UA);
4041 switch(dvdstruct->format) {
4042 case DVD_STRUCT_PHYSICAL: {
4043 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4044 struct dvd_layer *outlayer;
4045 struct scsi_read_dvd_struct_data_physical *phys_data;
4048 (struct scsi_read_dvd_struct_data_physical *)databuf;
4049 inlayer = &phys_data->layer_desc;
4050 outlayer = (struct dvd_layer *)&dvdstruct->data;
4052 dvdstruct->length = sizeof(*inlayer);
4054 outlayer->book_type = (inlayer->book_type_version &
4055 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4056 outlayer->book_version = (inlayer->book_type_version &
4057 RDSD_BOOK_VERSION_MASK);
4058 outlayer->disc_size = (inlayer->disc_size_max_rate &
4059 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4060 outlayer->max_rate = (inlayer->disc_size_max_rate &
4061 RDSD_MAX_RATE_MASK);
4062 outlayer->nlayers = (inlayer->layer_info &
4063 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4064 outlayer->track_path = (inlayer->layer_info &
4065 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4066 outlayer->layer_type = (inlayer->layer_info &
4067 RDSD_LAYER_TYPE_MASK);
4068 outlayer->linear_density = (inlayer->density &
4069 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4070 outlayer->track_density = (inlayer->density &
4071 RDSD_TRACK_DENSITY_MASK);
4072 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4074 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4075 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4076 outlayer->end_sector_l0 =
4077 scsi_3btoul(inlayer->end_sector_layer0);
4080 case DVD_STRUCT_COPYRIGHT: {
4081 struct scsi_read_dvd_struct_data_copyright *copy_data;
4083 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4086 dvdstruct->cpst = copy_data->cps_type;
4087 dvdstruct->rmi = copy_data->region_info;
4088 dvdstruct->length = 0;
4094 * Tell the user what the overall length is, no matter
4095 * what we can actually fit in the data buffer.
4097 dvdstruct->length = length - ccb->csio.resid -
4098 sizeof(struct scsi_read_dvd_struct_data_header);
4101 * But only actually copy out the smaller of what we read
4102 * in or what the structure can take.
4104 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4106 min(sizeof(dvdstruct->data), dvdstruct->length));
4111 xpt_release_ccb(ccb);
4112 cam_periph_unlock(periph);
4114 if (databuf != NULL)
4115 free(databuf, M_DEVBUF);
4121 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4122 void (*cbfcnp)(struct cam_periph *, union ccb *),
4123 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4124 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4125 u_int8_t sense_len, u_int32_t timeout)
4127 struct scsi_report_key *scsi_cmd;
4129 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4130 bzero(scsi_cmd, sizeof(*scsi_cmd));
4131 scsi_cmd->opcode = REPORT_KEY;
4132 scsi_ulto4b(lba, scsi_cmd->lba);
4133 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4134 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4135 (key_format & RK_KF_KEYFORMAT_MASK);
4140 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4142 /*data_ptr*/ data_ptr,
4143 /*dxfer_len*/ dxfer_len,
4150 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4151 void (*cbfcnp)(struct cam_periph *, union ccb *),
4152 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4153 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4156 struct scsi_send_key *scsi_cmd;
4158 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4159 bzero(scsi_cmd, sizeof(*scsi_cmd));
4160 scsi_cmd->opcode = SEND_KEY;
4162 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4163 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4164 (key_format & RK_KF_KEYFORMAT_MASK);
4169 /*flags*/ CAM_DIR_OUT,
4171 /*data_ptr*/ data_ptr,
4172 /*dxfer_len*/ dxfer_len,
4180 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4181 void (*cbfcnp)(struct cam_periph *, union ccb *),
4182 u_int8_t tag_action, u_int32_t address,
4183 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4184 u_int8_t *data_ptr, u_int32_t dxfer_len,
4185 u_int8_t sense_len, u_int32_t timeout)
4187 struct scsi_read_dvd_structure *scsi_cmd;
4189 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4190 bzero(scsi_cmd, sizeof(*scsi_cmd));
4191 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4193 scsi_ulto4b(address, scsi_cmd->address);
4194 scsi_cmd->layer_number = layer_number;
4195 scsi_cmd->format = format;
4196 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4197 /* The AGID is the top two bits of this byte */
4198 scsi_cmd->agid = agid << 6;
4203 /*flags*/ CAM_DIR_IN,
4205 /*data_ptr*/ data_ptr,
4206 /*dxfer_len*/ dxfer_len,
4213 scsi_read_toc(struct ccb_scsiio *csio, uint32_t retries,
4214 void (*cbfcnp)(struct cam_periph *, union ccb *),
4215 uint8_t tag_action, uint8_t byte1_flags, uint8_t format,
4216 uint8_t track, uint8_t *data_ptr, uint32_t dxfer_len,
4217 int sense_len, int timeout)
4219 struct scsi_read_toc *scsi_cmd;
4221 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
4222 bzero(scsi_cmd, sizeof(*scsi_cmd));
4223 scsi_cmd->op_code = READ_TOC;
4226 * The structure is counting from 1, the function counting from 0.
4227 * The spec counts from 0. In MMC-6, there is only one flag, the
4228 * MSF flag. But we put the whole byte in for a bit a future-proofing.
4230 scsi_cmd->byte2 = byte1_flags;
4231 scsi_cmd->format = format;
4232 scsi_cmd->from_track = track;
4233 scsi_ulto2b(dxfer_len, scsi_cmd->data_len);
4236 /* retries */ retries,
4237 /* cbfcnp */ cbfcnp,
4238 /* flags */ CAM_DIR_IN,
4239 /* tag_action */ tag_action,
4240 /* data_ptr */ data_ptr,
4241 /* dxfer_len */ dxfer_len,
4242 /* sense_len */ sense_len,
4244 /* timeout */ timeout);