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 callout_func_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;
705 snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
706 "%s%d", cpi.dev_name, cpi.unit_number);
709 * Acquire a reference to the periph before we register with GEOM.
710 * We'll release this reference once GEOM calls us back (via
711 * dadiskgonecb()) telling us that our provider has been freed.
713 if (cam_periph_acquire(periph) != 0) {
714 xpt_print(periph->path, "%s: lost periph during "
715 "registration!\n", __func__);
716 cam_periph_lock(periph);
717 return (CAM_REQ_CMP_ERR);
720 disk_create(softc->disk, DISK_VERSION);
721 cam_periph_lock(periph);
724 * Add an async callback so that we get
725 * notified if this device goes away.
727 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
728 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
731 * Schedule a periodic media polling events.
733 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
734 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
735 (cgd->inq_flags & SID_AEN) == 0 &&
737 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
738 cdmediapoll, periph);
740 xpt_schedule(periph, CAM_PRIORITY_DEV);
745 cdopen(struct disk *dp)
747 struct cam_periph *periph;
748 struct cd_softc *softc;
751 periph = (struct cam_periph *)dp->d_drv1;
752 softc = (struct cd_softc *)periph->softc;
754 if (cam_periph_acquire(periph) != 0)
757 cam_periph_lock(periph);
759 if (softc->flags & CD_FLAG_INVALID) {
760 cam_periph_release_locked(periph);
761 cam_periph_unlock(periph);
765 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
766 cam_periph_release_locked(periph);
767 cam_periph_unlock(periph);
771 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
775 * Check for media, and set the appropriate flags. We don't bail
776 * if we don't have media, but then we don't allow anything but the
777 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
779 cdcheckmedia(periph, /*do_wait*/ 1);
781 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
782 cam_periph_unhold(periph);
784 cam_periph_unlock(periph);
790 cdclose(struct disk *dp)
792 struct cam_periph *periph;
793 struct cd_softc *softc;
795 periph = (struct cam_periph *)dp->d_drv1;
796 softc = (struct cd_softc *)periph->softc;
798 cam_periph_lock(periph);
799 if (cam_periph_hold(periph, PRIBIO) != 0) {
800 cam_periph_unlock(periph);
801 cam_periph_release(periph);
805 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
808 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
809 cdprevent(periph, PR_ALLOW);
812 * Since we're closing this CD, mark the blocksize as unavailable.
813 * It will be marked as available when the CD is opened again.
815 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
818 * We'll check the media and toc again at the next open().
820 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
822 cam_periph_unhold(periph);
823 cam_periph_release_locked(periph);
824 cam_periph_unlock(periph);
830 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
832 u_int32_t sense_flags),
833 u_int32_t cam_flags, u_int32_t sense_flags)
835 struct cd_softc *softc;
836 struct cam_periph *periph;
839 periph = xpt_path_periph(ccb->ccb_h.path);
840 softc = (struct cd_softc *)periph->softc;
842 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
843 softc->disk->d_devstat);
849 * Actually translate the requested transfer into one the physical driver
850 * can understand. The transfer is described by a buf and will include
851 * only one physical transfer.
854 cdstrategy(struct bio *bp)
856 struct cam_periph *periph;
857 struct cd_softc *softc;
859 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
860 cam_periph_lock(periph);
861 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
862 ("cdstrategy(%p)\n", bp));
864 softc = (struct cd_softc *)periph->softc;
867 * If the device has been made invalid, error out
869 if ((softc->flags & CD_FLAG_INVALID)) {
870 cam_periph_unlock(periph);
871 biofinish(bp, NULL, ENXIO);
876 * Place it in the queue of disk activities for this disk
878 bioq_disksort(&softc->bio_queue, bp);
881 * If we don't know that we have valid media, schedule the media
882 * check first. The I/O will get executed after the media check.
884 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
885 cdcheckmedia(periph, /*do_wait*/ 0);
887 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
889 cam_periph_unlock(periph);
894 cdstart(struct cam_periph *periph, union ccb *start_ccb)
896 struct cd_softc *softc;
898 struct ccb_scsiio *csio;
900 softc = (struct cd_softc *)periph->softc;
902 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
904 switch (softc->state) {
905 case CD_STATE_NORMAL:
907 bp = bioq_first(&softc->bio_queue);
911 csio = &start_ccb->csio;
912 scsi_test_unit_ready(csio,
913 /*retries*/ cd_retry_count,
918 start_ccb->ccb_h.ccb_bp = NULL;
919 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
920 xpt_action(start_ccb);
922 xpt_release_ccb(start_ccb);
926 cam_periph_release_locked(periph);
928 bioq_remove(&softc->bio_queue, bp);
930 if ((bp->bio_cmd != BIO_READ) &&
931 (bp->bio_cmd != BIO_WRITE)) {
932 biofinish(bp, NULL, EOPNOTSUPP);
933 xpt_release_ccb(start_ccb);
937 scsi_read_write(&start_ccb->csio,
938 /*retries*/ cd_retry_count,
941 /* read */bp->bio_cmd == BIO_READ ?
942 SCSI_RW_READ : SCSI_RW_WRITE,
944 /* minimum_cmd_size */ 10,
945 /* lba */ bp->bio_offset /
946 softc->params.blksize,
947 bp->bio_bcount / softc->params.blksize,
948 /* data_ptr */ bp->bio_data,
949 /* dxfer_len */ bp->bio_bcount,
950 /* sense_len */ cd_retry_count ?
951 SSD_FULL_SIZE : SF_NO_PRINT,
952 /* timeout */ cd_timeout);
953 /* Use READ CD command for audio tracks. */
954 if (softc->params.blksize == 2352) {
955 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
956 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
957 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
958 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
959 start_ccb->csio.cdb_len = 12;
961 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
963 LIST_INSERT_HEAD(&softc->pending_ccbs,
964 &start_ccb->ccb_h, periph_links.le);
965 softc->outstanding_cmds++;
967 /* We expect a unit attention from this device */
968 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
969 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
970 softc->flags &= ~CD_FLAG_RETRY_UA;
973 start_ccb->ccb_h.ccb_bp = bp;
974 bp = bioq_first(&softc->bio_queue);
976 xpt_action(start_ccb);
978 if (bp != NULL || softc->tur) {
979 /* Have more work to do, so ensure we stay scheduled */
980 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
985 case CD_STATE_MEDIA_SIZE:
987 struct scsi_read_capacity_data *rcap;
989 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
990 M_SCSICD, M_NOWAIT | M_ZERO);
992 xpt_print(periph->path,
993 "%s: Couldn't malloc read_capacity data\n",
995 xpt_release_ccb(start_ccb);
997 * We can't probe because we can't allocate memory,
998 * so invalidate the peripheral. The system probably
999 * has larger problems at this stage. If we've
1000 * already probed (and are re-probing capacity), we
1001 * don't need to invalidate.
1003 * XXX KDM need to reset probe state and kick out
1006 if (softc->state == CD_STATE_PROBE)
1007 cam_periph_invalidate(periph);
1012 * Set the default capacity and sector size to something that
1013 * GEOM can handle. This will get reset when a read capacity
1014 * completes successfully.
1016 softc->disk->d_sectorsize = 2048;
1017 softc->disk->d_mediasize = 0;
1019 csio = &start_ccb->csio;
1020 scsi_read_capacity(csio,
1021 /*retries*/ cd_retry_count,
1027 start_ccb->ccb_h.ccb_bp = NULL;
1028 if (softc->state == CD_STATE_PROBE)
1029 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1031 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_SIZE;
1032 xpt_action(start_ccb);
1035 case CD_STATE_MEDIA_ALLOW:
1036 case CD_STATE_MEDIA_PREVENT:
1039 * If the CD is already locked, we don't need to do this.
1040 * Move on to the capacity check.
1042 if (softc->state == CD_STATE_MEDIA_PREVENT
1043 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0) {
1044 softc->state = CD_STATE_MEDIA_SIZE;
1045 xpt_release_ccb(start_ccb);
1046 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1050 scsi_prevent(&start_ccb->csio,
1051 /*retries*/ cd_retry_count,
1053 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1054 /*action*/ (softc->state == CD_STATE_MEDIA_ALLOW) ?
1055 PR_ALLOW : PR_PREVENT,
1056 /*sense_len*/ SSD_FULL_SIZE,
1059 start_ccb->ccb_h.ccb_bp = NULL;
1060 if (softc->state == CD_STATE_MEDIA_ALLOW)
1061 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_ALLOW;
1063 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_PREVENT;
1064 xpt_action(start_ccb);
1067 case CD_STATE_MEDIA_TOC_HDR: {
1068 struct ioc_toc_header *toch;
1070 bzero(&softc->toc, sizeof(softc->toc));
1072 toch = &softc->toc.header;
1074 scsi_read_toc(&start_ccb->csio,
1075 /*retries*/ cd_retry_count,
1077 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1079 /*format*/ SRTOC_FORMAT_TOC,
1081 /*data_ptr*/ (uint8_t *)toch,
1082 /*dxfer_len*/ sizeof(*toch),
1083 /*sense_len*/ SSD_FULL_SIZE,
1085 start_ccb->ccb_h.ccb_bp = NULL;
1086 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_HDR;
1087 xpt_action(start_ccb);
1090 case CD_STATE_MEDIA_TOC_FULL: {
1092 bzero(&softc->toc, sizeof(softc->toc));
1094 scsi_read_toc(&start_ccb->csio,
1095 /*retries*/ cd_retry_count,
1097 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1099 /*format*/ SRTOC_FORMAT_TOC,
1101 /*data_ptr*/ (uint8_t *)&softc->toc,
1102 /*dxfer_len*/ softc->toc_read_len ?
1103 softc->toc_read_len :
1105 /*sense_len*/ SSD_FULL_SIZE,
1107 start_ccb->ccb_h.ccb_bp = NULL;
1108 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_FULL;
1109 xpt_action(start_ccb);
1112 case CD_STATE_MEDIA_TOC_LEAD: {
1113 struct cd_toc_single *leadout;
1115 leadout = &softc->leadout;
1116 bzero(leadout, sizeof(*leadout));
1118 scsi_read_toc(&start_ccb->csio,
1119 /*retries*/ cd_retry_count,
1121 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1122 /*byte1_flags*/ CD_MSF,
1123 /*format*/ SRTOC_FORMAT_TOC,
1125 /*data_ptr*/ (uint8_t *)leadout,
1126 /*dxfer_len*/ sizeof(*leadout),
1127 /*sense_len*/ SSD_FULL_SIZE,
1129 start_ccb->ccb_h.ccb_bp = NULL;
1130 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_LEAD;
1131 xpt_action(start_ccb);
1138 cddone(struct cam_periph *periph, union ccb *done_ccb)
1140 struct cd_softc *softc;
1141 struct ccb_scsiio *csio;
1143 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1145 softc = (struct cd_softc *)periph->softc;
1146 csio = &done_ccb->csio;
1148 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1149 case CD_CCB_BUFFER_IO:
1154 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1157 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1160 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1165 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1166 if (error == ERESTART) {
1168 * A retry was scheuled, so
1176 xpt_print(periph->path,
1177 "cddone: got error %#x back\n", error);
1178 bioq_flush(&softc->bio_queue, NULL, EIO);
1179 bp->bio_resid = bp->bio_bcount;
1180 bp->bio_error = error;
1181 bp->bio_flags |= BIO_ERROR;
1182 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1183 cam_release_devq(done_ccb->ccb_h.path,
1187 /*getcount_only*/0);
1190 bp->bio_resid = csio->resid;
1192 if (bp->bio_resid != 0) {
1194 * Short transfer ???
1195 * XXX: not sure this is correct for partial
1198 bp->bio_flags |= BIO_ERROR;
1202 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1203 softc->outstanding_cmds--;
1205 biofinish(bp, NULL, 0);
1210 struct scsi_read_capacity_data *rdcap;
1212 struct cd_params *cdp;
1215 cdp = &softc->params;
1216 announce_buf = softc->announce_temp;
1217 bzero(announce_buf, CD_ANNOUNCETMP_SZ);
1219 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1221 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1222 cdp->blksize = scsi_4btoul (rdcap->length);
1225 * Retry any UNIT ATTENTION type errors. They
1226 * are expected at boot.
1228 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1229 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1230 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1231 snprintf(announce_buf, CD_ANNOUNCETMP_SZ,
1232 "%juMB (%ju %u byte sectors)",
1233 ((uintmax_t)cdp->disksize * cdp->blksize) /
1235 (uintmax_t)cdp->disksize, cdp->blksize);
1237 if (error == ERESTART) {
1239 * A retry was scheuled, so
1245 int sense_key, error_code;
1248 struct ccb_getdev cgd;
1250 /* Don't wedge this device's queue */
1251 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1252 cam_release_devq(done_ccb->ccb_h.path,
1256 /*getcount_only*/0);
1258 status = done_ccb->ccb_h.status;
1260 xpt_setup_ccb(&cgd.ccb_h,
1261 done_ccb->ccb_h.path,
1262 CAM_PRIORITY_NORMAL);
1263 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1264 xpt_action((union ccb *)&cgd);
1266 if (scsi_extract_sense_ccb(done_ccb,
1267 &error_code, &sense_key, &asc, &ascq))
1273 * Attach to anything that claims to be a
1274 * CDROM or WORM device, as long as it
1275 * doesn't return a "Logical unit not
1276 * supported" (0x25) error.
1278 if ((have_sense) && (asc != 0x25)
1279 && (error_code == SSD_CURRENT_ERROR
1280 || error_code == SSD_DESC_CURRENT_ERROR)) {
1281 const char *sense_key_desc;
1282 const char *asc_desc;
1284 scsi_sense_desc(sense_key, asc, ascq,
1288 snprintf(announce_buf,
1290 "Attempt to query device "
1291 "size failed: %s, %s",
1294 } else if ((have_sense == 0)
1295 && ((status & CAM_STATUS_MASK) ==
1296 CAM_SCSI_STATUS_ERROR)
1297 && (csio->scsi_status ==
1298 SCSI_STATUS_BUSY)) {
1299 snprintf(announce_buf,
1301 "Attempt to query device "
1302 "size failed: SCSI Status: %s",
1303 scsi_status_string(csio));
1304 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1306 * We only print out an error for
1307 * CDROM type devices. For WORM
1308 * devices, we don't print out an
1309 * error since a few WORM devices
1310 * don't support CDROM commands.
1311 * If we have sense information, go
1312 * ahead and print it out.
1313 * Otherwise, just say that we
1318 * Just print out the error, not
1319 * the full probe message, when we
1326 xpt_print(periph->path,
1327 "got CAM status %#x\n",
1328 done_ccb->ccb_h.status);
1330 xpt_print(periph->path, "fatal error, "
1331 "failed to attach to device\n");
1333 * Invalidate this peripheral.
1335 cam_periph_invalidate(periph);
1337 announce_buf = NULL;
1341 * Invalidate this peripheral.
1343 cam_periph_invalidate(periph);
1344 announce_buf = NULL;
1348 free(rdcap, M_SCSICD);
1349 if (announce_buf != NULL) {
1352 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ,
1354 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1355 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
1361 * Create our sysctl variables, now that we know
1362 * we have successfully attached.
1364 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1366 softc->state = CD_STATE_NORMAL;
1368 * Since our peripheral may be invalidated by an error
1369 * above or an external event, we must release our CCB
1370 * before releasing the probe lock on the peripheral.
1371 * The peripheral will only go away once the last lock
1372 * is removed, and we need it around for the CCB release
1375 xpt_release_ccb(done_ccb);
1376 cam_periph_unhold(periph);
1381 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1383 if (cderror(done_ccb, CAM_RETRY_SELTO,
1384 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1387 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1388 cam_release_devq(done_ccb->ccb_h.path,
1392 /*getcount_only*/0);
1394 xpt_release_ccb(done_ccb);
1395 cam_periph_release_locked(periph);
1398 case CD_CCB_MEDIA_ALLOW:
1399 case CD_CCB_MEDIA_PREVENT:
1406 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1407 error = cderror(done_ccb, CAM_RETRY_SELTO,
1408 SF_RETRY_UA | SF_NO_PRINT);
1410 if (error == ERESTART)
1412 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1413 cam_release_devq(done_ccb->ccb_h.path,
1417 /*getcount_only*/0);
1420 * Note that just like the original cdcheckmedia(), we do
1421 * a prevent without failing the whole operation if the
1422 * prevent fails. We try, but keep going if it doesn't
1426 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1427 CD_CCB_MEDIA_PREVENT)
1432 xpt_release_ccb(done_ccb);
1434 if (is_prevent != 0) {
1436 softc->flags |= CD_FLAG_DISC_LOCKED;
1438 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1439 softc->state = CD_STATE_MEDIA_SIZE;
1440 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1443 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1444 softc->state = CD_STATE_NORMAL;
1445 if (bioq_first(&softc->bio_queue) != NULL)
1446 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1450 case CD_CCB_MEDIA_SIZE:
1452 struct scsi_read_capacity_data *rdcap;
1456 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1457 error = cderror(done_ccb, CAM_RETRY_SELTO,
1458 SF_RETRY_UA | SF_NO_PRINT);
1460 if (error == ERESTART)
1462 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1463 cam_release_devq(done_ccb->ccb_h.path,
1467 /*getcount_only*/0);
1468 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1471 softc->params.disksize =scsi_4btoul(rdcap->addr) + 1;
1472 softc->params.blksize = scsi_4btoul(rdcap->length);
1474 /* Make sure we got at least some block size. */
1475 if (softc->params.blksize == 0)
1478 * SCSI-3 mandates that the reported blocksize shall be
1479 * 2048. Older drives sometimes report funny values,
1480 * trim it down to 2048, or other parts of the kernel
1481 * will get confused.
1483 * XXX we leave drives alone that might report 512
1484 * bytes, as well as drives reporting more weird
1485 * sizes like perhaps 4K.
1487 if (softc->params.blksize > 2048
1488 && softc->params.blksize <= 2352)
1489 softc->params.blksize = 2048;
1491 free(rdcap, M_SCSICD);
1494 softc->disk->d_sectorsize = softc->params.blksize;
1495 softc->disk->d_mediasize =
1496 (off_t)softc->params.blksize *
1497 softc->params.disksize;
1498 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
1499 softc->state = CD_STATE_MEDIA_TOC_HDR;
1501 softc->flags &= ~(CD_FLAG_VALID_MEDIA |
1503 bioq_flush(&softc->bio_queue, NULL, EINVAL);
1504 softc->state = CD_STATE_MEDIA_ALLOW;
1505 cdmediaprobedone(periph);
1507 xpt_release_ccb(done_ccb);
1508 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1511 case CD_CCB_MEDIA_TOC_HDR:
1512 case CD_CCB_MEDIA_TOC_FULL:
1513 case CD_CCB_MEDIA_TOC_LEAD:
1516 struct ioc_toc_header *toch;
1522 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1523 error = cderror(done_ccb, CAM_RETRY_SELTO,
1524 SF_RETRY_UA | SF_NO_PRINT);
1526 if (error == ERESTART)
1529 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1530 cam_release_devq(done_ccb->ccb_h.path,
1534 /*getcount_only*/0);
1537 * We will get errors here for media that doesn't have a table
1538 * of contents. According to the MMC-3 spec: "When a Read
1539 * TOC/PMA/ATIP command is presented for a DDCD/CD-R/RW media,
1540 * where the first TOC has not been recorded (no complete
1541 * session) and the Format codes 0000b, 0001b, or 0010b are
1542 * specified, this command shall be rejected with an INVALID
1543 * FIELD IN CDB. Devices that are not capable of reading an
1544 * incomplete session on DDC/CD-R/RW media shall report
1545 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
1547 * So this isn't fatal if we can't read the table of contents,
1548 * it just means that the user won't be able to issue the
1549 * play tracks ioctl, and likely lots of other stuff won't
1550 * work either. They need to burn the CD before we can do
1551 * a whole lot with it. So we don't print anything here if
1552 * we get an error back.
1554 * We also bail out if the drive doesn't at least give us
1555 * the full TOC header.
1558 || ((csio->dxfer_len - csio->resid) <
1559 sizeof(struct ioc_toc_header))) {
1560 softc->flags &= ~CD_FLAG_VALID_TOC;
1561 bzero(&softc->toc, sizeof(softc->toc));
1563 * Failing the TOC read is not an error.
1565 softc->state = CD_STATE_NORMAL;
1566 xpt_release_ccb(done_ccb);
1568 cdmediaprobedone(periph);
1571 * Go ahead and schedule I/O execution if there is
1572 * anything in the queue. It'll probably get
1573 * kicked out with an error.
1575 if (bioq_first(&softc->bio_queue) != NULL)
1576 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1581 * Note that this is NOT the storage location used for the
1584 toch = &softc->toc.header;
1586 if (softc->quirks & CD_Q_BCD_TRACKS) {
1587 toch->starting_track = bcd2bin(toch->starting_track);
1588 toch->ending_track = bcd2bin(toch->ending_track);
1591 /* Number of TOC entries, plus leadout */
1592 num_entries = (toch->ending_track - toch->starting_track) + 2;
1593 cdindex = toch->starting_track + num_entries -1;
1595 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1596 CD_CCB_MEDIA_TOC_HDR) {
1597 if (num_entries <= 0) {
1598 softc->flags &= ~CD_FLAG_VALID_TOC;
1599 bzero(&softc->toc, sizeof(softc->toc));
1601 * Failing the TOC read is not an error.
1603 softc->state = CD_STATE_NORMAL;
1604 xpt_release_ccb(done_ccb);
1606 cdmediaprobedone(periph);
1609 * Go ahead and schedule I/O execution if
1610 * there is anything in the queue. It'll
1611 * probably get kicked out with an error.
1613 if (bioq_first(&softc->bio_queue) != NULL)
1614 xpt_schedule(periph,
1615 CAM_PRIORITY_NORMAL);
1617 softc->toc_read_len = num_entries *
1618 sizeof(struct cd_toc_entry);
1619 softc->toc_read_len += sizeof(*toch);
1621 softc->state = CD_STATE_MEDIA_TOC_FULL;
1622 xpt_release_ccb(done_ccb);
1623 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1627 } else if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1628 CD_CCB_MEDIA_TOC_LEAD) {
1629 struct cd_toc_single *leadout;
1631 leadout = (struct cd_toc_single *)csio->data_ptr;
1632 softc->toc.entries[cdindex - toch->starting_track] =
1634 } else if (((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1635 CD_CCB_MEDIA_TOC_FULL)
1636 && (cdindex == toch->ending_track + 1)) {
1638 * XXX KDM is this necessary? Probably only if the
1639 * drive doesn't return leadout information with the
1640 * table of contents.
1642 softc->state = CD_STATE_MEDIA_TOC_LEAD;
1643 xpt_release_ccb(done_ccb);
1644 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1648 if (softc->quirks & CD_Q_BCD_TRACKS) {
1649 for (cdindex = 0; cdindex < num_entries - 1; cdindex++){
1650 softc->toc.entries[cdindex].track =
1651 bcd2bin(softc->toc.entries[cdindex].track);
1655 softc->flags |= CD_FLAG_VALID_TOC;
1656 /* If the first track is audio, correct sector size. */
1657 if ((softc->toc.entries[0].control & 4) == 0) {
1658 softc->disk->d_sectorsize =softc->params.blksize = 2352;
1659 softc->disk->d_mediasize =
1660 (off_t)softc->params.blksize *
1661 softc->params.disksize;
1663 softc->state = CD_STATE_NORMAL;
1666 * We unconditionally (re)set the blocksize each time the
1667 * CD device is opened. This is because the CD can change,
1668 * and therefore the blocksize might change.
1669 * XXX problems here if some slice or partition is still
1670 * open with the old size?
1672 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE)!=0)
1673 softc->disk->d_devstat->flags &=
1674 ~DEVSTAT_BS_UNAVAILABLE;
1675 softc->disk->d_devstat->block_size = softc->params.blksize;
1677 xpt_release_ccb(done_ccb);
1679 cdmediaprobedone(periph);
1681 if (bioq_first(&softc->bio_queue) != NULL)
1682 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1688 xpt_release_ccb(done_ccb);
1691 static union cd_pages *
1692 cdgetpage(struct cd_mode_params *mode_params)
1694 union cd_pages *page;
1696 if (mode_params->cdb_size == 10)
1697 page = (union cd_pages *)find_mode_page_10(
1698 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1700 page = (union cd_pages *)find_mode_page_6(
1701 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1707 cdgetpagesize(int page_num)
1711 for (i = 0; i < nitems(cd_page_size_table); i++) {
1712 if (cd_page_size_table[i].page == page_num)
1713 return (cd_page_size_table[i].page_size);
1719 static struct cd_toc_entry *
1720 te_data_get_ptr(void *irtep, u_long cmd)
1723 struct ioc_read_toc_entry irte;
1724 #ifdef COMPAT_FREEBSD32
1725 struct ioc_read_toc_entry32 irte32;
1730 switch (IOCPARM_LEN(cmd)) {
1731 case sizeof(irteup->irte):
1732 return (irteup->irte.data);
1733 #ifdef COMPAT_FREEBSD32
1734 case sizeof(irteup->irte32):
1735 return ((struct cd_toc_entry *)(uintptr_t)irteup->irte32.data);
1738 panic("Unhandled ioctl command %ld", cmd);
1743 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1746 struct cam_periph *periph;
1747 struct cd_softc *softc;
1750 periph = (struct cam_periph *)dp->d_drv1;
1751 cam_periph_lock(periph);
1753 softc = (struct cd_softc *)periph->softc;
1755 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1756 ("cdioctl(%#lx)\n", cmd));
1758 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1759 cam_periph_unlock(periph);
1760 cam_periph_release(periph);
1765 * If we don't have media loaded, check for it. If still don't
1766 * have media loaded, we can only do a load or eject.
1768 * We only care whether media is loaded if this is a cd-specific ioctl
1769 * (thus the IOCGROUP check below). Note that this will break if
1770 * anyone adds any ioctls into the switch statement below that don't
1771 * have their ioctl group set to 'c'.
1773 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1774 && ((cmd != CDIOCCLOSE)
1775 && (cmd != CDIOCEJECT))
1776 && (IOCGROUP(cmd) == 'c')) {
1777 error = cdcheckmedia(periph, /*do_wait*/ 1);
1779 cam_periph_unhold(periph);
1780 cam_periph_unlock(periph);
1785 * Drop the lock here so later mallocs can use WAITOK. The periph
1786 * is essentially locked still with the cam_periph_hold call above.
1788 cam_periph_unlock(periph);
1792 case CDIOCPLAYTRACKS:
1794 struct ioc_play_track *args
1795 = (struct ioc_play_track *) addr;
1796 struct cd_mode_params params;
1797 union cd_pages *page;
1799 params.alloc_len = sizeof(union cd_mode_data_6_10);
1800 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1803 cam_periph_lock(periph);
1804 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1805 ("trying to do CDIOCPLAYTRACKS\n"));
1807 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1809 free(params.mode_buf, M_SCSICD);
1810 cam_periph_unlock(periph);
1813 page = cdgetpage(¶ms);
1815 page->audio.flags &= ~CD_PA_SOTC;
1816 page->audio.flags |= CD_PA_IMMED;
1817 error = cdsetmode(periph, ¶ms);
1818 free(params.mode_buf, M_SCSICD);
1820 cam_periph_unlock(periph);
1825 * This was originally implemented with the PLAY
1826 * AUDIO TRACK INDEX command, but that command was
1827 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1828 * drives support it but ATAPI and ATAPI-derivative
1829 * drives don't seem to support it. So we keep a
1830 * cache of the table of contents and translate
1831 * track numbers to MSF format.
1833 if (softc->flags & CD_FLAG_VALID_TOC) {
1834 union msf_lba *sentry, *eentry;
1837 if (args->end_track <
1838 softc->toc.header.ending_track + 1)
1840 if (args->end_track >
1841 softc->toc.header.ending_track + 1)
1843 softc->toc.header.ending_track + 1;
1844 st = args->start_track -
1845 softc->toc.header.starting_track;
1846 et = args->end_track -
1847 softc->toc.header.starting_track;
1850 || (st > (softc->toc.header.ending_track -
1851 softc->toc.header.starting_track))) {
1853 cam_periph_unlock(periph);
1856 sentry = &softc->toc.entries[st].addr;
1857 eentry = &softc->toc.entries[et].addr;
1858 error = cdplaymsf(periph,
1867 * If we don't have a valid TOC, try the
1868 * play track index command. It is part of
1869 * the SCSI-2 spec, but was removed in the
1870 * MMC specs. ATAPI and ATAPI-derived
1871 * drives don't support it.
1873 if (softc->quirks & CD_Q_BCD_TRACKS) {
1875 bin2bcd(args->start_track);
1877 bin2bcd(args->end_track);
1879 error = cdplaytracks(periph,
1885 cam_periph_unlock(periph);
1890 struct ioc_play_msf *args
1891 = (struct ioc_play_msf *) addr;
1892 struct cd_mode_params params;
1893 union cd_pages *page;
1895 params.alloc_len = sizeof(union cd_mode_data_6_10);
1896 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1899 cam_periph_lock(periph);
1900 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1901 ("trying to do CDIOCPLAYMSF\n"));
1903 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1905 free(params.mode_buf, M_SCSICD);
1906 cam_periph_unlock(periph);
1909 page = cdgetpage(¶ms);
1911 page->audio.flags &= ~CD_PA_SOTC;
1912 page->audio.flags |= CD_PA_IMMED;
1913 error = cdsetmode(periph, ¶ms);
1914 free(params.mode_buf, M_SCSICD);
1916 cam_periph_unlock(periph);
1919 error = cdplaymsf(periph,
1926 cam_periph_unlock(periph);
1929 case CDIOCPLAYBLOCKS:
1931 struct ioc_play_blocks *args
1932 = (struct ioc_play_blocks *) addr;
1933 struct cd_mode_params params;
1934 union cd_pages *page;
1936 params.alloc_len = sizeof(union cd_mode_data_6_10);
1937 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1940 cam_periph_lock(periph);
1941 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1942 ("trying to do CDIOCPLAYBLOCKS\n"));
1945 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1947 free(params.mode_buf, M_SCSICD);
1948 cam_periph_unlock(periph);
1951 page = cdgetpage(¶ms);
1953 page->audio.flags &= ~CD_PA_SOTC;
1954 page->audio.flags |= CD_PA_IMMED;
1955 error = cdsetmode(periph, ¶ms);
1956 free(params.mode_buf, M_SCSICD);
1958 cam_periph_unlock(periph);
1961 error = cdplay(periph, args->blk, args->len);
1962 cam_periph_unlock(periph);
1965 case CDIOCREADSUBCHANNEL:
1967 struct ioc_read_subchannel *args
1968 = (struct ioc_read_subchannel *) addr;
1969 struct cd_sub_channel_info *data;
1970 u_int32_t len = args->data_len;
1972 data = malloc(sizeof(struct cd_sub_channel_info),
1973 M_SCSICD, M_WAITOK | M_ZERO);
1975 cam_periph_lock(periph);
1976 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1977 ("trying to do CDIOCREADSUBCHANNEL\n"));
1979 if ((len > sizeof(struct cd_sub_channel_info)) ||
1980 (len < sizeof(struct cd_sub_channel_header))) {
1982 "scsi_cd: cdioctl: "
1983 "cdioreadsubchannel: error, len=%d\n",
1986 free(data, M_SCSICD);
1987 cam_periph_unlock(periph);
1991 if (softc->quirks & CD_Q_BCD_TRACKS)
1992 args->track = bin2bcd(args->track);
1994 error = cdreadsubchannel(periph, args->address_format,
1995 args->data_format, args->track, data, len);
1998 free(data, M_SCSICD);
1999 cam_periph_unlock(periph);
2002 if (softc->quirks & CD_Q_BCD_TRACKS)
2003 data->what.track_info.track_number =
2004 bcd2bin(data->what.track_info.track_number);
2005 len = min(len, ((data->header.data_len[0] << 8) +
2006 data->header.data_len[1] +
2007 sizeof(struct cd_sub_channel_header)));
2008 cam_periph_unlock(periph);
2009 error = copyout(data, args->data, len);
2010 free(data, M_SCSICD);
2014 case CDIOREADTOCHEADER:
2016 struct ioc_toc_header *th;
2018 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2021 cam_periph_lock(periph);
2022 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2023 ("trying to do CDIOREADTOCHEADER\n"));
2025 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2026 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2029 cam_periph_unlock(periph);
2032 if (softc->quirks & CD_Q_BCD_TRACKS) {
2033 /* we are going to have to convert the BCD
2034 * encoding on the cd to what is expected
2036 th->starting_track =
2037 bcd2bin(th->starting_track);
2038 th->ending_track = bcd2bin(th->ending_track);
2040 th->len = ntohs(th->len);
2041 bcopy(th, addr, sizeof(*th));
2043 cam_periph_unlock(periph);
2046 case CDIOREADTOCENTRYS:
2047 #ifdef COMPAT_FREEBSD32
2048 case CDIOREADTOCENTRYS_32:
2051 struct cd_tocdata *data;
2052 struct cd_toc_single *lead;
2053 struct ioc_read_toc_entry *te =
2054 (struct ioc_read_toc_entry *) addr;
2055 struct ioc_toc_header *th;
2056 u_int32_t len, readlen, idx, num;
2057 u_int32_t starting_track = te->starting_track;
2059 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2060 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2062 cam_periph_lock(periph);
2063 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2064 ("trying to do CDIOREADTOCENTRYS\n"));
2066 if (te->data_len < sizeof(struct cd_toc_entry)
2067 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2068 || (te->address_format != CD_MSF_FORMAT
2069 && te->address_format != CD_LBA_FORMAT)) {
2071 printf("scsi_cd: error in readtocentries, "
2072 "returning EINVAL\n");
2073 free(data, M_SCSICD);
2074 free(lead, M_SCSICD);
2075 cam_periph_unlock(periph);
2080 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2081 sizeof (*th), /*sense_flags*/0);
2083 free(data, M_SCSICD);
2084 free(lead, M_SCSICD);
2085 cam_periph_unlock(periph);
2089 if (softc->quirks & CD_Q_BCD_TRACKS) {
2090 /* we are going to have to convert the BCD
2091 * encoding on the cd to what is expected
2093 th->starting_track =
2094 bcd2bin(th->starting_track);
2095 th->ending_track = bcd2bin(th->ending_track);
2098 if (starting_track == 0)
2099 starting_track = th->starting_track;
2100 else if (starting_track == LEADOUT)
2101 starting_track = th->ending_track + 1;
2102 else if (starting_track < th->starting_track ||
2103 starting_track > th->ending_track + 1) {
2104 printf("scsi_cd: error in readtocentries, "
2105 "returning EINVAL\n");
2106 free(data, M_SCSICD);
2107 free(lead, M_SCSICD);
2108 cam_periph_unlock(periph);
2113 /* calculate reading length without leadout entry */
2114 readlen = (th->ending_track - starting_track + 1) *
2115 sizeof(struct cd_toc_entry);
2117 /* and with leadout entry */
2118 len = readlen + sizeof(struct cd_toc_entry);
2119 if (te->data_len < len) {
2124 if (len > sizeof(data->entries)) {
2125 printf("scsi_cd: error in readtocentries, "
2126 "returning EINVAL\n");
2128 free(data, M_SCSICD);
2129 free(lead, M_SCSICD);
2130 cam_periph_unlock(periph);
2133 num = len / sizeof(struct cd_toc_entry);
2136 error = cdreadtoc(periph, te->address_format,
2139 readlen + sizeof (*th),
2142 free(data, M_SCSICD);
2143 free(lead, M_SCSICD);
2144 cam_periph_unlock(periph);
2149 /* make leadout entry if needed */
2150 idx = starting_track + num - 1;
2151 if (softc->quirks & CD_Q_BCD_TRACKS)
2152 th->ending_track = bcd2bin(th->ending_track);
2153 if (idx == th->ending_track + 1) {
2154 error = cdreadtoc(periph, te->address_format,
2155 LEADOUT, (u_int8_t *)lead,
2159 free(data, M_SCSICD);
2160 free(lead, M_SCSICD);
2161 cam_periph_unlock(periph);
2164 data->entries[idx - starting_track] =
2167 if (softc->quirks & CD_Q_BCD_TRACKS) {
2168 for (idx = 0; idx < num - 1; idx++) {
2169 data->entries[idx].track =
2170 bcd2bin(data->entries[idx].track);
2174 cam_periph_unlock(periph);
2175 error = copyout(data->entries, te_data_get_ptr(te, cmd),
2177 free(data, M_SCSICD);
2178 free(lead, M_SCSICD);
2181 case CDIOREADTOCENTRY:
2183 struct cd_toc_single *data;
2184 struct ioc_read_toc_single_entry *te =
2185 (struct ioc_read_toc_single_entry *) addr;
2186 struct ioc_toc_header *th;
2189 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2191 cam_periph_lock(periph);
2192 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2193 ("trying to do CDIOREADTOCENTRY\n"));
2195 if (te->address_format != CD_MSF_FORMAT
2196 && te->address_format != CD_LBA_FORMAT) {
2197 printf("error in readtocentry, "
2198 " returning EINVAL\n");
2199 free(data, M_SCSICD);
2201 cam_periph_unlock(periph);
2206 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2207 sizeof (*th), /*sense_flags*/0);
2209 free(data, M_SCSICD);
2210 cam_periph_unlock(periph);
2214 if (softc->quirks & CD_Q_BCD_TRACKS) {
2215 /* we are going to have to convert the BCD
2216 * encoding on the cd to what is expected
2218 th->starting_track =
2219 bcd2bin(th->starting_track);
2220 th->ending_track = bcd2bin(th->ending_track);
2224 track = th->starting_track;
2225 else if (track == LEADOUT)
2227 else if (track < th->starting_track ||
2228 track > th->ending_track + 1) {
2229 printf("error in readtocentry, "
2230 " returning EINVAL\n");
2231 free(data, M_SCSICD);
2233 cam_periph_unlock(periph);
2237 error = cdreadtoc(periph, te->address_format, track,
2238 (u_int8_t *)data, sizeof(*data),
2241 free(data, M_SCSICD);
2242 cam_periph_unlock(periph);
2246 if (softc->quirks & CD_Q_BCD_TRACKS)
2247 data->entry.track = bcd2bin(data->entry.track);
2248 bcopy(&data->entry, &te->entry,
2249 sizeof(struct cd_toc_entry));
2250 free(data, M_SCSICD);
2251 cam_periph_unlock(periph);
2256 struct ioc_patch *arg = (struct ioc_patch *)addr;
2257 struct cd_mode_params params;
2258 union cd_pages *page;
2260 params.alloc_len = sizeof(union cd_mode_data_6_10);
2261 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2264 cam_periph_lock(periph);
2265 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2266 ("trying to do CDIOCSETPATCH\n"));
2268 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2270 free(params.mode_buf, M_SCSICD);
2271 cam_periph_unlock(periph);
2274 page = cdgetpage(¶ms);
2276 page->audio.port[LEFT_PORT].channels =
2278 page->audio.port[RIGHT_PORT].channels =
2280 page->audio.port[2].channels = arg->patch[2];
2281 page->audio.port[3].channels = arg->patch[3];
2282 error = cdsetmode(periph, ¶ms);
2283 free(params.mode_buf, M_SCSICD);
2284 cam_periph_unlock(periph);
2289 struct ioc_vol *arg = (struct ioc_vol *) addr;
2290 struct cd_mode_params params;
2291 union cd_pages *page;
2293 params.alloc_len = sizeof(union cd_mode_data_6_10);
2294 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2297 cam_periph_lock(periph);
2298 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2299 ("trying to do CDIOCGETVOL\n"));
2301 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2303 free(params.mode_buf, M_SCSICD);
2304 cam_periph_unlock(periph);
2307 page = cdgetpage(¶ms);
2309 arg->vol[LEFT_PORT] =
2310 page->audio.port[LEFT_PORT].volume;
2311 arg->vol[RIGHT_PORT] =
2312 page->audio.port[RIGHT_PORT].volume;
2313 arg->vol[2] = page->audio.port[2].volume;
2314 arg->vol[3] = page->audio.port[3].volume;
2315 free(params.mode_buf, M_SCSICD);
2316 cam_periph_unlock(periph);
2321 struct ioc_vol *arg = (struct ioc_vol *) addr;
2322 struct cd_mode_params params;
2323 union cd_pages *page;
2325 params.alloc_len = sizeof(union cd_mode_data_6_10);
2326 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2329 cam_periph_lock(periph);
2330 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2331 ("trying to do CDIOCSETVOL\n"));
2333 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2335 free(params.mode_buf, M_SCSICD);
2336 cam_periph_unlock(periph);
2339 page = cdgetpage(¶ms);
2341 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2342 page->audio.port[LEFT_PORT].volume =
2343 arg->vol[LEFT_PORT];
2344 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2345 page->audio.port[RIGHT_PORT].volume =
2346 arg->vol[RIGHT_PORT];
2347 page->audio.port[2].volume = arg->vol[2];
2348 page->audio.port[3].volume = arg->vol[3];
2349 error = cdsetmode(periph, ¶ms);
2350 cam_periph_unlock(periph);
2351 free(params.mode_buf, M_SCSICD);
2356 struct cd_mode_params params;
2357 union cd_pages *page;
2359 params.alloc_len = sizeof(union cd_mode_data_6_10);
2360 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2363 cam_periph_lock(periph);
2364 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2365 ("trying to do CDIOCSETMONO\n"));
2367 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2369 free(params.mode_buf, M_SCSICD);
2370 cam_periph_unlock(periph);
2373 page = cdgetpage(¶ms);
2375 page->audio.port[LEFT_PORT].channels =
2376 LEFT_CHANNEL | RIGHT_CHANNEL;
2377 page->audio.port[RIGHT_PORT].channels =
2378 LEFT_CHANNEL | RIGHT_CHANNEL;
2379 page->audio.port[2].channels = 0;
2380 page->audio.port[3].channels = 0;
2381 error = cdsetmode(periph, ¶ms);
2382 cam_periph_unlock(periph);
2383 free(params.mode_buf, M_SCSICD);
2386 case CDIOCSETSTEREO:
2388 struct cd_mode_params params;
2389 union cd_pages *page;
2391 params.alloc_len = sizeof(union cd_mode_data_6_10);
2392 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2395 cam_periph_lock(periph);
2396 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2397 ("trying to do CDIOCSETSTEREO\n"));
2399 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2401 free(params.mode_buf, M_SCSICD);
2402 cam_periph_unlock(periph);
2405 page = cdgetpage(¶ms);
2407 page->audio.port[LEFT_PORT].channels =
2409 page->audio.port[RIGHT_PORT].channels =
2411 page->audio.port[2].channels = 0;
2412 page->audio.port[3].channels = 0;
2413 error = cdsetmode(periph, ¶ms);
2414 free(params.mode_buf, M_SCSICD);
2415 cam_periph_unlock(periph);
2420 struct cd_mode_params params;
2421 union cd_pages *page;
2423 params.alloc_len = sizeof(union cd_mode_data_6_10);
2424 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2427 cam_periph_lock(periph);
2428 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2429 ("trying to do CDIOCSETMUTE\n"));
2431 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2433 free(params.mode_buf, M_SCSICD);
2434 cam_periph_unlock(periph);
2437 page = cdgetpage(¶ms);
2439 page->audio.port[LEFT_PORT].channels = 0;
2440 page->audio.port[RIGHT_PORT].channels = 0;
2441 page->audio.port[2].channels = 0;
2442 page->audio.port[3].channels = 0;
2443 error = cdsetmode(periph, ¶ms);
2444 free(params.mode_buf, M_SCSICD);
2445 cam_periph_unlock(periph);
2450 struct cd_mode_params params;
2451 union cd_pages *page;
2453 params.alloc_len = sizeof(union cd_mode_data_6_10);
2454 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2457 cam_periph_lock(periph);
2458 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2459 ("trying to do CDIOCSETLEFT\n"));
2461 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2463 free(params.mode_buf, M_SCSICD);
2464 cam_periph_unlock(periph);
2467 page = cdgetpage(¶ms);
2469 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2470 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2471 page->audio.port[2].channels = 0;
2472 page->audio.port[3].channels = 0;
2473 error = cdsetmode(periph, ¶ms);
2474 free(params.mode_buf, M_SCSICD);
2475 cam_periph_unlock(periph);
2480 struct cd_mode_params params;
2481 union cd_pages *page;
2483 params.alloc_len = sizeof(union cd_mode_data_6_10);
2484 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2487 cam_periph_lock(periph);
2488 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2489 ("trying to do CDIOCSETRIGHT\n"));
2491 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2493 free(params.mode_buf, M_SCSICD);
2494 cam_periph_unlock(periph);
2497 page = cdgetpage(¶ms);
2499 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2500 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2501 page->audio.port[2].channels = 0;
2502 page->audio.port[3].channels = 0;
2503 error = cdsetmode(periph, ¶ms);
2504 free(params.mode_buf, M_SCSICD);
2505 cam_periph_unlock(periph);
2509 cam_periph_lock(periph);
2510 error = cdpause(periph, 1);
2511 cam_periph_unlock(periph);
2514 cam_periph_lock(periph);
2515 error = cdpause(periph, 0);
2516 cam_periph_unlock(periph);
2519 cam_periph_lock(periph);
2520 error = cdstartunit(periph, 0);
2521 cam_periph_unlock(periph);
2524 cam_periph_lock(periph);
2525 error = cdstartunit(periph, 1);
2526 cam_periph_unlock(periph);
2529 cam_periph_lock(periph);
2530 error = cdstopunit(periph, 0);
2531 cam_periph_unlock(periph);
2534 cam_periph_lock(periph);
2535 error = cdstopunit(periph, 1);
2536 cam_periph_unlock(periph);
2539 cam_periph_lock(periph);
2540 cdprevent(periph, PR_ALLOW);
2541 cam_periph_unlock(periph);
2544 cam_periph_lock(periph);
2545 cdprevent(periph, PR_PREVENT);
2546 cam_periph_unlock(periph);
2549 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2553 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2557 /* return (cd_reset(periph)); */
2560 case CDRIOCREADSPEED:
2561 cam_periph_lock(periph);
2562 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2563 cam_periph_unlock(periph);
2565 case CDRIOCWRITESPEED:
2566 cam_periph_lock(periph);
2567 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2568 cam_periph_unlock(periph);
2570 case CDRIOCGETBLOCKSIZE:
2571 *(int *)addr = softc->params.blksize;
2573 case CDRIOCSETBLOCKSIZE:
2574 if (*(int *)addr <= 0) {
2578 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2581 case DVDIOCREPORTKEY: {
2582 struct dvd_authinfo *authinfo;
2584 authinfo = (struct dvd_authinfo *)addr;
2586 if (cmd == DVDIOCREPORTKEY)
2587 error = cdreportkey(periph, authinfo);
2589 error = cdsendkey(periph, authinfo);
2592 case DVDIOCREADSTRUCTURE: {
2593 struct dvd_struct *dvdstruct;
2595 dvdstruct = (struct dvd_struct *)addr;
2597 error = cdreaddvdstructure(periph, dvdstruct);
2602 cam_periph_lock(periph);
2603 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2604 cam_periph_unlock(periph);
2608 cam_periph_lock(periph);
2609 cam_periph_unhold(periph);
2611 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2612 if (error && bootverbose) {
2613 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2615 cam_periph_unlock(periph);
2621 cdprevent(struct cam_periph *periph, int action)
2624 struct cd_softc *softc;
2627 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2629 softc = (struct cd_softc *)periph->softc;
2631 if (((action == PR_ALLOW)
2632 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2633 || ((action == PR_PREVENT)
2634 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2638 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2640 scsi_prevent(&ccb->csio,
2641 /*retries*/ cd_retry_count,
2646 /* timeout */60000);
2648 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2649 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2651 xpt_release_ccb(ccb);
2654 if (action == PR_ALLOW)
2655 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2657 softc->flags |= CD_FLAG_DISC_LOCKED;
2662 cdmediaprobedone(struct cam_periph *periph)
2664 struct cd_softc *softc;
2666 softc = (struct cd_softc *)periph->softc;
2668 softc->flags &= ~CD_FLAG_MEDIA_SCAN_ACT;
2670 if ((softc->flags & CD_FLAG_MEDIA_WAIT) != 0) {
2671 softc->flags &= ~CD_FLAG_MEDIA_WAIT;
2672 wakeup(&softc->toc);
2677 * XXX: the disk media and sector size is only really able to change
2678 * XXX: while the device is closed.
2682 cdcheckmedia(struct cam_periph *periph, int do_wait)
2684 struct cd_softc *softc;
2687 softc = (struct cd_softc *)periph->softc;
2691 && ((softc->flags & CD_FLAG_MEDIA_WAIT) == 0)) {
2692 softc->flags |= CD_FLAG_MEDIA_WAIT;
2694 if ((softc->flags & CD_FLAG_MEDIA_SCAN_ACT) == 0) {
2695 softc->state = CD_STATE_MEDIA_PREVENT;
2696 softc->flags |= CD_FLAG_MEDIA_SCAN_ACT;
2697 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2703 error = msleep(&softc->toc, cam_periph_mtx(periph), PRIBIO,"cdmedia",0);
2709 * Check to see whether we have a valid size from the media. We
2710 * may or may not have a valid TOC.
2712 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
2721 cdcheckmedia(struct cam_periph *periph)
2723 struct cd_softc *softc;
2724 struct ioc_toc_header *toch;
2725 struct cd_toc_single leadout;
2726 u_int32_t size, toclen;
2727 int error, num_entries, cdindex;
2729 softc = (struct cd_softc *)periph->softc;
2731 cdprevent(periph, PR_PREVENT);
2732 softc->disk->d_sectorsize = 2048;
2733 softc->disk->d_mediasize = 0;
2736 * Get the disc size and block size. If we can't get it, we don't
2737 * have media, most likely.
2739 if ((error = cdsize(periph, &size)) != 0) {
2740 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2741 cdprevent(periph, PR_ALLOW);
2744 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2745 softc->disk->d_sectorsize = softc->params.blksize;
2746 softc->disk->d_mediasize =
2747 (off_t)softc->params.blksize * softc->params.disksize;
2751 * Now we check the table of contents. This (currently) is only
2752 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2753 * things like present a separate entry in /dev for each track,
2754 * like that acd(4) driver does.
2756 bzero(&softc->toc, sizeof(softc->toc));
2757 toch = &softc->toc.header;
2759 * We will get errors here for media that doesn't have a table of
2760 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2761 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2762 * has not been recorded (no complete session) and the Format codes
2763 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2764 * with an INVALID FIELD IN CDB. Devices that are not capable of
2765 * reading an incomplete session on DDC/CD-R/RW media shall report
2766 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2768 * So this isn't fatal if we can't read the table of contents, it
2769 * just means that the user won't be able to issue the play tracks
2770 * ioctl, and likely lots of other stuff won't work either. They
2771 * need to burn the CD before we can do a whole lot with it. So
2772 * we don't print anything here if we get an error back.
2774 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2777 * Errors in reading the table of contents aren't fatal, we just
2778 * won't have a valid table of contents cached.
2782 bzero(&softc->toc, sizeof(softc->toc));
2786 if (softc->quirks & CD_Q_BCD_TRACKS) {
2787 toch->starting_track = bcd2bin(toch->starting_track);
2788 toch->ending_track = bcd2bin(toch->ending_track);
2791 /* Number of TOC entries, plus leadout */
2792 num_entries = (toch->ending_track - toch->starting_track) + 2;
2794 if (num_entries <= 0)
2797 toclen = num_entries * sizeof(struct cd_toc_entry);
2799 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2800 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2804 bzero(&softc->toc, sizeof(softc->toc));
2808 if (softc->quirks & CD_Q_BCD_TRACKS) {
2809 toch->starting_track = bcd2bin(toch->starting_track);
2810 toch->ending_track = bcd2bin(toch->ending_track);
2813 * XXX KDM is this necessary? Probably only if the drive doesn't
2814 * return leadout information with the table of contents.
2816 cdindex = toch->starting_track + num_entries -1;
2817 if (cdindex == toch->ending_track + 1) {
2819 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2820 (u_int8_t *)&leadout, sizeof(leadout),
2826 softc->toc.entries[cdindex - toch->starting_track] =
2829 if (softc->quirks & CD_Q_BCD_TRACKS) {
2830 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2831 softc->toc.entries[cdindex].track =
2832 bcd2bin(softc->toc.entries[cdindex].track);
2836 softc->flags |= CD_FLAG_VALID_TOC;
2838 /* If the first track is audio, correct sector size. */
2839 if ((softc->toc.entries[0].control & 4) == 0) {
2840 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2841 softc->disk->d_mediasize =
2842 (off_t)softc->params.blksize * softc->params.disksize;
2848 * We unconditionally (re)set the blocksize each time the
2849 * CD device is opened. This is because the CD can change,
2850 * and therefore the blocksize might change.
2851 * XXX problems here if some slice or partition is still
2852 * open with the old size?
2854 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2855 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2856 softc->disk->d_devstat->block_size = softc->params.blksize;
2862 cdsize(struct cam_periph *periph, u_int32_t *size)
2864 struct cd_softc *softc;
2866 struct scsi_read_capacity_data *rcap_buf;
2869 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2871 softc = (struct cd_softc *)periph->softc;
2873 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2875 /* XXX Should be M_WAITOK */
2876 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2877 M_SCSICD, M_NOWAIT | M_ZERO);
2878 if (rcap_buf == NULL)
2881 scsi_read_capacity(&ccb->csio,
2882 /*retries*/ cd_retry_count,
2887 /* timeout */20000);
2889 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2890 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2892 xpt_release_ccb(ccb);
2894 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2895 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2896 /* Make sure we got at least some block size. */
2897 if (error == 0 && softc->params.blksize == 0)
2900 * SCSI-3 mandates that the reported blocksize shall be 2048.
2901 * Older drives sometimes report funny values, trim it down to
2902 * 2048, or other parts of the kernel will get confused.
2904 * XXX we leave drives alone that might report 512 bytes, as
2905 * well as drives reporting more weird sizes like perhaps 4K.
2907 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2908 softc->params.blksize = 2048;
2910 free(rcap_buf, M_SCSICD);
2911 *size = softc->params.disksize;
2919 cd6byteworkaround(union ccb *ccb)
2922 struct cam_periph *periph;
2923 struct cd_softc *softc;
2924 struct cd_mode_params *params;
2927 periph = xpt_path_periph(ccb->ccb_h.path);
2928 softc = (struct cd_softc *)periph->softc;
2930 cdb = ccb->csio.cdb_io.cdb_bytes;
2932 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2933 || ((cdb[0] != MODE_SENSE_6)
2934 && (cdb[0] != MODE_SELECT_6)))
2938 * Because there is no convenient place to stash the overall
2939 * cd_mode_params structure pointer, we have to grab it like this.
2940 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2941 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2943 * XXX It would be nice if, at some point, we could increase the
2944 * number of available peripheral private pointers. Both pointers
2945 * are currently used in most every peripheral driver.
2949 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2950 if (params->mode_buf == ccb->csio.data_ptr) {
2957 * This shouldn't happen. All mode sense and mode select
2958 * operations in the cd(4) driver MUST go through cdgetmode() and
2962 xpt_print(periph->path,
2963 "mode buffer not found in mode queue!\n");
2967 params->cdb_size = 10;
2968 softc->minimum_command_size = 10;
2969 xpt_print(ccb->ccb_h.path,
2970 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2971 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2973 if (cdb[0] == MODE_SENSE_6) {
2974 struct scsi_mode_sense_10 ms10;
2975 struct scsi_mode_sense_6 *ms6;
2978 ms6 = (struct scsi_mode_sense_6 *)cdb;
2980 bzero(&ms10, sizeof(ms10));
2981 ms10.opcode = MODE_SENSE_10;
2982 ms10.byte2 = ms6->byte2;
2983 ms10.page = ms6->page;
2986 * 10 byte mode header, block descriptor,
2987 * sizeof(union cd_pages)
2989 len = sizeof(struct cd_mode_data_10);
2990 ccb->csio.dxfer_len = len;
2992 scsi_ulto2b(len, ms10.length);
2993 ms10.control = ms6->control;
2994 bcopy(&ms10, cdb, 10);
2995 ccb->csio.cdb_len = 10;
2997 struct scsi_mode_select_10 ms10;
2998 struct scsi_mode_select_6 *ms6;
2999 struct scsi_mode_header_6 *header6;
3000 struct scsi_mode_header_10 *header10;
3001 struct scsi_mode_page_header *page_header;
3002 int blk_desc_len, page_num, page_size, len;
3004 ms6 = (struct scsi_mode_select_6 *)cdb;
3006 bzero(&ms10, sizeof(ms10));
3007 ms10.opcode = MODE_SELECT_10;
3008 ms10.byte2 = ms6->byte2;
3010 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3011 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3013 page_header = find_mode_page_6(header6);
3014 page_num = page_header->page_code;
3016 blk_desc_len = header6->blk_desc_len;
3018 page_size = cdgetpagesize(page_num);
3020 if (page_size != (page_header->page_length +
3021 sizeof(*page_header)))
3022 page_size = page_header->page_length +
3023 sizeof(*page_header);
3025 len = sizeof(*header10) + blk_desc_len + page_size;
3027 len = min(params->alloc_len, len);
3030 * Since the 6 byte parameter header is shorter than the 10
3031 * byte parameter header, we need to copy the actual mode
3032 * page data, and the block descriptor, if any, so things wind
3033 * up in the right place. The regions will overlap, but
3034 * bcopy() does the right thing.
3036 bcopy(params->mode_buf + sizeof(*header6),
3037 params->mode_buf + sizeof(*header10),
3038 len - sizeof(*header10));
3040 /* Make sure these fields are set correctly. */
3041 scsi_ulto2b(0, header10->data_length);
3042 header10->medium_type = 0;
3043 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3045 ccb->csio.dxfer_len = len;
3047 scsi_ulto2b(len, ms10.length);
3048 ms10.control = ms6->control;
3049 bcopy(&ms10, cdb, 10);
3050 ccb->csio.cdb_len = 10;
3053 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3054 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3057 cam_release_devq(ccb->ccb_h.path,
3061 /*getcount_only*/0);
3068 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3070 struct cd_softc *softc;
3071 struct cam_periph *periph;
3072 int error, error_code, sense_key, asc, ascq;
3074 periph = xpt_path_periph(ccb->ccb_h.path);
3075 softc = (struct cd_softc *)periph->softc;
3080 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3081 * CDB comes back with this particular error, try transforming it
3082 * into the 10 byte version.
3084 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3085 error = cd6byteworkaround(ccb);
3086 } else if (scsi_extract_sense_ccb(ccb,
3087 &error_code, &sense_key, &asc, &ascq)) {
3088 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3089 error = cd6byteworkaround(ccb);
3090 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3091 asc == 0x28 && ascq == 0x00)
3092 disk_media_changed(softc->disk, M_NOWAIT);
3093 else if (sense_key == SSD_KEY_NOT_READY &&
3094 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3095 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3096 disk_media_gone(softc->disk, M_NOWAIT);
3100 if (error == ERESTART)
3105 * Until we have a better way of doing pack validation,
3106 * don't treat UAs as errors.
3108 sense_flags |= SF_RETRY_UA;
3110 if (softc->quirks & CD_Q_RETRY_BUSY)
3111 sense_flags |= SF_RETRY_BUSY;
3112 return (cam_periph_error(ccb, cam_flags, sense_flags));
3116 cdmediapoll(void *arg)
3118 struct cam_periph *periph = arg;
3119 struct cd_softc *softc = periph->softc;
3121 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3122 softc->outstanding_cmds == 0) {
3123 if (cam_periph_acquire(periph) == 0) {
3125 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3128 /* Queue us up again */
3129 if (cd_poll_period != 0)
3130 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3134 * Read table of contents
3137 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3138 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3141 struct ccb_scsiio *csio;
3148 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3153 /* retries */ cd_retry_count,
3155 /* tag_action */ MSG_SIMPLE_Q_TAG,
3156 /* byte1_flags */ (mode == CD_MSF_FORMAT) ? CD_MSF : 0,
3157 /* format */ SRTOC_FORMAT_TOC,
3159 /* data_ptr */ data,
3160 /* dxfer_len */ len,
3161 /* sense_len */ SSD_FULL_SIZE,
3162 /* timeout */ 50000);
3164 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3165 /*sense_flags*/SF_RETRY_UA | sense_flags);
3167 xpt_release_ccb(ccb);
3173 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3174 u_int32_t format, int track,
3175 struct cd_sub_channel_info *data, u_int32_t len)
3177 struct scsi_read_subchannel *scsi_cmd;
3178 struct ccb_scsiio *csio;
3184 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3189 /* retries */ cd_retry_count,
3191 /* flags */ CAM_DIR_IN,
3192 /* tag_action */ MSG_SIMPLE_Q_TAG,
3193 /* data_ptr */ (u_int8_t *)data,
3194 /* dxfer_len */ len,
3195 /* sense_len */ SSD_FULL_SIZE,
3196 sizeof(struct scsi_read_subchannel),
3197 /* timeout */ 50000);
3199 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3200 bzero (scsi_cmd, sizeof(*scsi_cmd));
3202 scsi_cmd->op_code = READ_SUBCHANNEL;
3203 if (mode == CD_MSF_FORMAT)
3204 scsi_cmd->byte1 |= CD_MSF;
3205 scsi_cmd->byte2 = SRS_SUBQ;
3206 scsi_cmd->subchan_format = format;
3207 scsi_cmd->track = track;
3208 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3209 scsi_cmd->control = 0;
3211 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3212 /*sense_flags*/SF_RETRY_UA);
3214 xpt_release_ccb(ccb);
3221 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3222 * routine. See comments in cd6byteworkaround() for details.
3225 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3228 struct ccb_scsiio *csio;
3229 struct cd_softc *softc;
3234 softc = (struct cd_softc *)periph->softc;
3236 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3240 data->cdb_size = softc->minimum_command_size;
3241 if (data->cdb_size < 10)
3242 param_len = sizeof(struct cd_mode_data);
3244 param_len = sizeof(struct cd_mode_data_10);
3246 /* Don't say we've got more room than we actually allocated */
3247 param_len = min(param_len, data->alloc_len);
3249 scsi_mode_sense_len(csio,
3250 /* retries */ cd_retry_count,
3252 /* tag_action */ MSG_SIMPLE_Q_TAG,
3254 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3256 /* param_buf */ data->mode_buf,
3257 /* param_len */ param_len,
3258 /* minimum_cmd_size */ softc->minimum_command_size,
3259 /* sense_len */ SSD_FULL_SIZE,
3260 /* timeout */ 50000);
3263 * It would be nice not to have to do this, but there's no
3264 * available pointer in the CCB that would allow us to stuff the
3265 * mode params structure in there and retrieve it in
3266 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3267 * lets the caller know what CDB size we ended up using, so they
3268 * can find the actual mode page offset.
3270 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3272 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3273 /*sense_flags*/SF_RETRY_UA);
3275 xpt_release_ccb(ccb);
3277 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3280 * This is a bit of belt-and-suspenders checking, but if we run
3281 * into a situation where the target sends back multiple block
3282 * descriptors, we might not have enough space in the buffer to
3283 * see the whole mode page. Better to return an error than
3284 * potentially access memory beyond our malloced region.
3289 if (data->cdb_size == 10) {
3290 struct scsi_mode_header_10 *hdr10;
3292 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3293 data_len = scsi_2btoul(hdr10->data_length);
3294 data_len += sizeof(hdr10->data_length);
3296 struct scsi_mode_header_6 *hdr6;
3298 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3299 data_len = hdr6->data_length;
3300 data_len += sizeof(hdr6->data_length);
3304 * Complain if there is more mode data available than we
3305 * allocated space for. This could potentially happen if
3306 * we miscalculated the page length for some reason, if the
3307 * drive returns multiple block descriptors, or if it sets
3308 * the data length incorrectly.
3310 if (data_len > data->alloc_len) {
3311 xpt_print(periph->path, "allocated modepage %d length "
3312 "%d < returned length %d\n", page, data->alloc_len,
3321 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3322 * routine. See comments in cd6byteworkaround() for details.
3325 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3327 struct ccb_scsiio *csio;
3328 struct cd_softc *softc;
3330 int cdb_size, param_len;
3333 softc = (struct cd_softc *)periph->softc;
3335 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3342 * If the data is formatted for the 10 byte version of the mode
3343 * select parameter list, we need to use the 10 byte CDB.
3344 * Otherwise, we use whatever the stored minimum command size.
3346 if (data->cdb_size == 10)
3347 cdb_size = data->cdb_size;
3349 cdb_size = softc->minimum_command_size;
3351 if (cdb_size >= 10) {
3352 struct scsi_mode_header_10 *mode_header;
3355 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3357 data_len = scsi_2btoul(mode_header->data_length);
3359 scsi_ulto2b(0, mode_header->data_length);
3361 * SONY drives do not allow a mode select with a medium_type
3362 * value that has just been returned by a mode sense; use a
3363 * medium_type of 0 (Default) instead.
3365 mode_header->medium_type = 0;
3368 * Pass back whatever the drive passed to us, plus the size
3369 * of the data length field.
3371 param_len = data_len + sizeof(mode_header->data_length);
3374 struct scsi_mode_header_6 *mode_header;
3376 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3378 param_len = mode_header->data_length + 1;
3380 mode_header->data_length = 0;
3382 * SONY drives do not allow a mode select with a medium_type
3383 * value that has just been returned by a mode sense; use a
3384 * medium_type of 0 (Default) instead.
3386 mode_header->medium_type = 0;
3389 /* Don't say we've got more room than we actually allocated */
3390 param_len = min(param_len, data->alloc_len);
3392 scsi_mode_select_len(csio,
3393 /* retries */ cd_retry_count,
3395 /* tag_action */ MSG_SIMPLE_Q_TAG,
3396 /* scsi_page_fmt */ 1,
3398 /* param_buf */ data->mode_buf,
3399 /* param_len */ param_len,
3400 /* minimum_cmd_size */ cdb_size,
3401 /* sense_len */ SSD_FULL_SIZE,
3402 /* timeout */ 50000);
3404 /* See comments in cdgetmode() and cd6byteworkaround(). */
3405 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3407 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3408 /*sense_flags*/SF_RETRY_UA);
3410 xpt_release_ccb(ccb);
3412 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3419 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3421 struct ccb_scsiio *csio;
3427 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3430 * Use the smallest possible command to perform the operation.
3432 if ((len & 0xffff0000) == 0) {
3434 * We can fit in a 10 byte cdb.
3436 struct scsi_play_10 *scsi_cmd;
3438 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3439 bzero (scsi_cmd, sizeof(*scsi_cmd));
3440 scsi_cmd->op_code = PLAY_10;
3441 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3442 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3443 cdb_len = sizeof(*scsi_cmd);
3445 struct scsi_play_12 *scsi_cmd;
3447 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3448 bzero (scsi_cmd, sizeof(*scsi_cmd));
3449 scsi_cmd->op_code = PLAY_12;
3450 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3451 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3452 cdb_len = sizeof(*scsi_cmd);
3455 /*retries*/ cd_retry_count,
3457 /*flags*/CAM_DIR_NONE,
3461 /*sense_len*/SSD_FULL_SIZE,
3463 /*timeout*/50 * 1000);
3465 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3466 /*sense_flags*/SF_RETRY_UA);
3468 xpt_release_ccb(ccb);
3474 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3475 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3477 struct scsi_play_msf *scsi_cmd;
3478 struct ccb_scsiio *csio;
3484 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3489 /* retries */ cd_retry_count,
3491 /* flags */ CAM_DIR_NONE,
3492 /* tag_action */ MSG_SIMPLE_Q_TAG,
3493 /* data_ptr */ NULL,
3495 /* sense_len */ SSD_FULL_SIZE,
3496 sizeof(struct scsi_play_msf),
3497 /* timeout */ 50000);
3499 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3500 bzero (scsi_cmd, sizeof(*scsi_cmd));
3502 scsi_cmd->op_code = PLAY_MSF;
3503 scsi_cmd->start_m = startm;
3504 scsi_cmd->start_s = starts;
3505 scsi_cmd->start_f = startf;
3506 scsi_cmd->end_m = endm;
3507 scsi_cmd->end_s = ends;
3508 scsi_cmd->end_f = endf;
3510 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3511 /*sense_flags*/SF_RETRY_UA);
3513 xpt_release_ccb(ccb);
3520 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3521 u_int32_t etrack, u_int32_t eindex)
3523 struct scsi_play_track *scsi_cmd;
3524 struct ccb_scsiio *csio;
3530 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3535 /* retries */ cd_retry_count,
3537 /* flags */ CAM_DIR_NONE,
3538 /* tag_action */ MSG_SIMPLE_Q_TAG,
3539 /* data_ptr */ NULL,
3541 /* sense_len */ SSD_FULL_SIZE,
3542 sizeof(struct scsi_play_track),
3543 /* timeout */ 50000);
3545 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3546 bzero (scsi_cmd, sizeof(*scsi_cmd));
3548 scsi_cmd->op_code = PLAY_TRACK;
3549 scsi_cmd->start_track = strack;
3550 scsi_cmd->start_index = sindex;
3551 scsi_cmd->end_track = etrack;
3552 scsi_cmd->end_index = eindex;
3554 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3555 /*sense_flags*/SF_RETRY_UA);
3557 xpt_release_ccb(ccb);
3563 cdpause(struct cam_periph *periph, u_int32_t go)
3565 struct scsi_pause *scsi_cmd;
3566 struct ccb_scsiio *csio;
3572 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3577 /* retries */ cd_retry_count,
3579 /* flags */ CAM_DIR_NONE,
3580 /* tag_action */ MSG_SIMPLE_Q_TAG,
3581 /* data_ptr */ NULL,
3583 /* sense_len */ SSD_FULL_SIZE,
3584 sizeof(struct scsi_pause),
3585 /* timeout */ 50000);
3587 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3588 bzero (scsi_cmd, sizeof(*scsi_cmd));
3590 scsi_cmd->op_code = PAUSE;
3591 scsi_cmd->resume = go;
3593 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3594 /*sense_flags*/SF_RETRY_UA);
3596 xpt_release_ccb(ccb);
3602 cdstartunit(struct cam_periph *periph, int load)
3609 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3611 scsi_start_stop(&ccb->csio,
3612 /* retries */ cd_retry_count,
3614 /* tag_action */ MSG_SIMPLE_Q_TAG,
3616 /* load_eject */ load,
3617 /* immediate */ FALSE,
3618 /* sense_len */ SSD_FULL_SIZE,
3619 /* timeout */ 50000);
3621 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3622 /*sense_flags*/SF_RETRY_UA);
3624 xpt_release_ccb(ccb);
3630 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3637 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3639 scsi_start_stop(&ccb->csio,
3640 /* retries */ cd_retry_count,
3642 /* tag_action */ MSG_SIMPLE_Q_TAG,
3644 /* load_eject */ eject,
3645 /* immediate */ FALSE,
3646 /* sense_len */ SSD_FULL_SIZE,
3647 /* timeout */ 50000);
3649 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3650 /*sense_flags*/SF_RETRY_UA);
3652 xpt_release_ccb(ccb);
3658 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3660 struct scsi_set_speed *scsi_cmd;
3661 struct ccb_scsiio *csio;
3666 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3669 /* Preserve old behavior: units in multiples of CDROM speed */
3676 /* retries */ cd_retry_count,
3678 /* flags */ CAM_DIR_NONE,
3679 /* tag_action */ MSG_SIMPLE_Q_TAG,
3680 /* data_ptr */ NULL,
3682 /* sense_len */ SSD_FULL_SIZE,
3683 sizeof(struct scsi_set_speed),
3684 /* timeout */ 50000);
3686 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3687 bzero(scsi_cmd, sizeof(*scsi_cmd));
3689 scsi_cmd->opcode = SET_CD_SPEED;
3690 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3691 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3693 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3694 /*sense_flags*/SF_RETRY_UA);
3696 xpt_release_ccb(ccb);
3702 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3714 switch (authinfo->format) {
3715 case DVD_REPORT_AGID:
3716 length = sizeof(struct scsi_report_key_data_agid);
3718 case DVD_REPORT_CHALLENGE:
3719 length = sizeof(struct scsi_report_key_data_challenge);
3721 case DVD_REPORT_KEY1:
3722 length = sizeof(struct scsi_report_key_data_key1_key2);
3724 case DVD_REPORT_TITLE_KEY:
3725 length = sizeof(struct scsi_report_key_data_title);
3726 /* The lba field is only set for the title key */
3727 lba = authinfo->lba;
3729 case DVD_REPORT_ASF:
3730 length = sizeof(struct scsi_report_key_data_asf);
3732 case DVD_REPORT_RPC:
3733 length = sizeof(struct scsi_report_key_data_rpc);
3735 case DVD_INVALIDATE_AGID:
3743 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3747 cam_periph_lock(periph);
3748 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3750 scsi_report_key(&ccb->csio,
3751 /* retries */ cd_retry_count,
3753 /* tag_action */ MSG_SIMPLE_Q_TAG,
3755 /* agid */ authinfo->agid,
3756 /* key_format */ authinfo->format,
3757 /* data_ptr */ databuf,
3758 /* dxfer_len */ length,
3759 /* sense_len */ SSD_FULL_SIZE,
3760 /* timeout */ 50000);
3762 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3763 /*sense_flags*/SF_RETRY_UA);
3768 if (ccb->csio.resid != 0) {
3769 xpt_print(periph->path, "warning, residual for report key "
3770 "command is %d\n", ccb->csio.resid);
3773 switch(authinfo->format) {
3774 case DVD_REPORT_AGID: {
3775 struct scsi_report_key_data_agid *agid_data;
3777 agid_data = (struct scsi_report_key_data_agid *)databuf;
3779 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3783 case DVD_REPORT_CHALLENGE: {
3784 struct scsi_report_key_data_challenge *chal_data;
3786 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3788 bcopy(chal_data->challenge_key, authinfo->keychal,
3789 min(sizeof(chal_data->challenge_key),
3790 sizeof(authinfo->keychal)));
3793 case DVD_REPORT_KEY1: {
3794 struct scsi_report_key_data_key1_key2 *key1_data;
3796 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3798 bcopy(key1_data->key1, authinfo->keychal,
3799 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3802 case DVD_REPORT_TITLE_KEY: {
3803 struct scsi_report_key_data_title *title_data;
3805 title_data = (struct scsi_report_key_data_title *)databuf;
3807 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3808 RKD_TITLE_CPM_SHIFT;
3809 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3810 RKD_TITLE_CP_SEC_SHIFT;
3811 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3812 RKD_TITLE_CMGS_SHIFT;
3813 bcopy(title_data->title_key, authinfo->keychal,
3814 min(sizeof(title_data->title_key),
3815 sizeof(authinfo->keychal)));
3818 case DVD_REPORT_ASF: {
3819 struct scsi_report_key_data_asf *asf_data;
3821 asf_data = (struct scsi_report_key_data_asf *)databuf;
3823 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3826 case DVD_REPORT_RPC: {
3827 struct scsi_report_key_data_rpc *rpc_data;
3829 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3831 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3833 authinfo->vend_rsts =
3834 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3835 RKD_RPC_VENDOR_RESET_SHIFT;
3836 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3837 authinfo->region = rpc_data->region_mask;
3838 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3841 case DVD_INVALIDATE_AGID:
3844 /* This should be impossible, since we checked above */
3847 break; /* NOTREACHED */
3851 xpt_release_ccb(ccb);
3852 cam_periph_unlock(periph);
3854 if (databuf != NULL)
3855 free(databuf, M_DEVBUF);
3861 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3871 switch(authinfo->format) {
3872 case DVD_SEND_CHALLENGE: {
3873 struct scsi_report_key_data_challenge *challenge_data;
3875 length = sizeof(*challenge_data);
3877 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3879 databuf = (u_int8_t *)challenge_data;
3881 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3882 challenge_data->data_len);
3884 bcopy(authinfo->keychal, challenge_data->challenge_key,
3885 min(sizeof(authinfo->keychal),
3886 sizeof(challenge_data->challenge_key)));
3889 case DVD_SEND_KEY2: {
3890 struct scsi_report_key_data_key1_key2 *key2_data;
3892 length = sizeof(*key2_data);
3894 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3896 databuf = (u_int8_t *)key2_data;
3898 scsi_ulto2b(length - sizeof(key2_data->data_len),
3899 key2_data->data_len);
3901 bcopy(authinfo->keychal, key2_data->key1,
3902 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3906 case DVD_SEND_RPC: {
3907 struct scsi_send_key_data_rpc *rpc_data;
3909 length = sizeof(*rpc_data);
3911 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3913 databuf = (u_int8_t *)rpc_data;
3915 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3916 rpc_data->data_len);
3918 rpc_data->region_code = authinfo->region;
3925 cam_periph_lock(periph);
3926 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3928 scsi_send_key(&ccb->csio,
3929 /* retries */ cd_retry_count,
3931 /* tag_action */ MSG_SIMPLE_Q_TAG,
3932 /* agid */ authinfo->agid,
3933 /* key_format */ authinfo->format,
3934 /* data_ptr */ databuf,
3935 /* dxfer_len */ length,
3936 /* sense_len */ SSD_FULL_SIZE,
3937 /* timeout */ 50000);
3939 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3940 /*sense_flags*/SF_RETRY_UA);
3942 xpt_release_ccb(ccb);
3943 cam_periph_unlock(periph);
3945 if (databuf != NULL)
3946 free(databuf, M_DEVBUF);
3952 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3962 /* The address is reserved for many of the formats */
3965 switch(dvdstruct->format) {
3966 case DVD_STRUCT_PHYSICAL:
3967 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3969 case DVD_STRUCT_COPYRIGHT:
3970 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3972 case DVD_STRUCT_DISCKEY:
3973 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3975 case DVD_STRUCT_BCA:
3976 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3978 case DVD_STRUCT_MANUFACT:
3979 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3981 case DVD_STRUCT_CMI:
3983 case DVD_STRUCT_PROTDISCID:
3984 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3986 case DVD_STRUCT_DISCKEYBLOCK:
3987 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3989 case DVD_STRUCT_DDS:
3990 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3992 case DVD_STRUCT_MEDIUM_STAT:
3993 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3995 case DVD_STRUCT_SPARE_AREA:
3996 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3998 case DVD_STRUCT_RMD_LAST:
4000 case DVD_STRUCT_RMD_RMA:
4002 case DVD_STRUCT_PRERECORDED:
4003 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4005 case DVD_STRUCT_UNIQUEID:
4006 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4008 case DVD_STRUCT_DCB:
4010 case DVD_STRUCT_LIST:
4012 * This is the maximum allocation length for the READ DVD
4013 * STRUCTURE command. There's nothing in the MMC3 spec
4014 * that indicates a limit in the amount of data that can
4015 * be returned from this call, other than the limits
4016 * imposed by the 2-byte length variables.
4025 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4029 cam_periph_lock(periph);
4030 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4032 scsi_read_dvd_structure(&ccb->csio,
4033 /* retries */ cd_retry_count,
4035 /* tag_action */ MSG_SIMPLE_Q_TAG,
4037 /* layer_number */ dvdstruct->layer_num,
4038 /* key_format */ dvdstruct->format,
4039 /* agid */ dvdstruct->agid,
4040 /* data_ptr */ databuf,
4041 /* dxfer_len */ length,
4042 /* sense_len */ SSD_FULL_SIZE,
4043 /* timeout */ 50000);
4045 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4046 /*sense_flags*/SF_RETRY_UA);
4051 switch(dvdstruct->format) {
4052 case DVD_STRUCT_PHYSICAL: {
4053 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4054 struct dvd_layer *outlayer;
4055 struct scsi_read_dvd_struct_data_physical *phys_data;
4058 (struct scsi_read_dvd_struct_data_physical *)databuf;
4059 inlayer = &phys_data->layer_desc;
4060 outlayer = (struct dvd_layer *)&dvdstruct->data;
4062 dvdstruct->length = sizeof(*inlayer);
4064 outlayer->book_type = (inlayer->book_type_version &
4065 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4066 outlayer->book_version = (inlayer->book_type_version &
4067 RDSD_BOOK_VERSION_MASK);
4068 outlayer->disc_size = (inlayer->disc_size_max_rate &
4069 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4070 outlayer->max_rate = (inlayer->disc_size_max_rate &
4071 RDSD_MAX_RATE_MASK);
4072 outlayer->nlayers = (inlayer->layer_info &
4073 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4074 outlayer->track_path = (inlayer->layer_info &
4075 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4076 outlayer->layer_type = (inlayer->layer_info &
4077 RDSD_LAYER_TYPE_MASK);
4078 outlayer->linear_density = (inlayer->density &
4079 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4080 outlayer->track_density = (inlayer->density &
4081 RDSD_TRACK_DENSITY_MASK);
4082 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4084 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4085 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4086 outlayer->end_sector_l0 =
4087 scsi_3btoul(inlayer->end_sector_layer0);
4090 case DVD_STRUCT_COPYRIGHT: {
4091 struct scsi_read_dvd_struct_data_copyright *copy_data;
4093 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4096 dvdstruct->cpst = copy_data->cps_type;
4097 dvdstruct->rmi = copy_data->region_info;
4098 dvdstruct->length = 0;
4104 * Tell the user what the overall length is, no matter
4105 * what we can actually fit in the data buffer.
4107 dvdstruct->length = length - ccb->csio.resid -
4108 sizeof(struct scsi_read_dvd_struct_data_header);
4111 * But only actually copy out the smaller of what we read
4112 * in or what the structure can take.
4114 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4116 min(sizeof(dvdstruct->data), dvdstruct->length));
4121 xpt_release_ccb(ccb);
4122 cam_periph_unlock(periph);
4124 if (databuf != NULL)
4125 free(databuf, M_DEVBUF);
4131 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4132 void (*cbfcnp)(struct cam_periph *, union ccb *),
4133 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4134 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4135 u_int8_t sense_len, u_int32_t timeout)
4137 struct scsi_report_key *scsi_cmd;
4139 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4140 bzero(scsi_cmd, sizeof(*scsi_cmd));
4141 scsi_cmd->opcode = REPORT_KEY;
4142 scsi_ulto4b(lba, scsi_cmd->lba);
4143 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4144 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4145 (key_format & RK_KF_KEYFORMAT_MASK);
4150 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4152 /*data_ptr*/ data_ptr,
4153 /*dxfer_len*/ dxfer_len,
4160 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4161 void (*cbfcnp)(struct cam_periph *, union ccb *),
4162 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4163 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4166 struct scsi_send_key *scsi_cmd;
4168 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4169 bzero(scsi_cmd, sizeof(*scsi_cmd));
4170 scsi_cmd->opcode = SEND_KEY;
4172 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4173 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4174 (key_format & RK_KF_KEYFORMAT_MASK);
4179 /*flags*/ CAM_DIR_OUT,
4181 /*data_ptr*/ data_ptr,
4182 /*dxfer_len*/ dxfer_len,
4190 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4191 void (*cbfcnp)(struct cam_periph *, union ccb *),
4192 u_int8_t tag_action, u_int32_t address,
4193 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4194 u_int8_t *data_ptr, u_int32_t dxfer_len,
4195 u_int8_t sense_len, u_int32_t timeout)
4197 struct scsi_read_dvd_structure *scsi_cmd;
4199 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4200 bzero(scsi_cmd, sizeof(*scsi_cmd));
4201 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4203 scsi_ulto4b(address, scsi_cmd->address);
4204 scsi_cmd->layer_number = layer_number;
4205 scsi_cmd->format = format;
4206 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4207 /* The AGID is the top two bits of this byte */
4208 scsi_cmd->agid = agid << 6;
4213 /*flags*/ CAM_DIR_IN,
4215 /*data_ptr*/ data_ptr,
4216 /*dxfer_len*/ dxfer_len,
4223 scsi_read_toc(struct ccb_scsiio *csio, uint32_t retries,
4224 void (*cbfcnp)(struct cam_periph *, union ccb *),
4225 uint8_t tag_action, uint8_t byte1_flags, uint8_t format,
4226 uint8_t track, uint8_t *data_ptr, uint32_t dxfer_len,
4227 int sense_len, int timeout)
4229 struct scsi_read_toc *scsi_cmd;
4231 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
4232 bzero(scsi_cmd, sizeof(*scsi_cmd));
4233 scsi_cmd->op_code = READ_TOC;
4236 * The structure is counting from 1, the function counting from 0.
4237 * The spec counts from 0. In MMC-6, there is only one flag, the
4238 * MSF flag. But we put the whole byte in for a bit a future-proofing.
4240 scsi_cmd->byte2 = byte1_flags;
4241 scsi_cmd->format = format;
4242 scsi_cmd->from_track = track;
4243 scsi_ulto2b(dxfer_len, scsi_cmd->data_len);
4246 /* retries */ retries,
4247 /* cbfcnp */ cbfcnp,
4248 /* flags */ CAM_DIR_IN,
4249 /* tag_action */ tag_action,
4250 /* data_ptr */ data_ptr,
4251 /* dxfer_len */ dxfer_len,
4252 /* sense_len */ sense_len,
4254 /* timeout */ timeout);