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 | CTLFLAG_MPSAFE, 0,
331 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RWTUN,
332 &cd_poll_period, 0, "Media polling period in seconds");
333 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RWTUN,
334 &cd_retry_count, 0, "Normal I/O retry count");
335 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RWTUN,
336 &cd_timeout, 0, "Timeout, in us, for read operations");
338 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
346 * Install a global async callback. This callback will
347 * receive async callbacks like "new device found".
349 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
351 if (status != CAM_REQ_CMP) {
352 printf("cd: Failed to attach master async callback "
353 "due to status 0x%x!\n", status);
358 * Callback from GEOM, called when it has finished cleaning up its
362 cddiskgonecb(struct disk *dp)
364 struct cam_periph *periph;
366 periph = (struct cam_periph *)dp->d_drv1;
367 cam_periph_release(periph);
371 cdoninvalidate(struct cam_periph *periph)
373 struct cd_softc *softc;
375 softc = (struct cd_softc *)periph->softc;
378 * De-register any async callbacks.
380 xpt_register_async(0, cdasync, periph, periph->path);
382 softc->flags |= CD_FLAG_INVALID;
385 * Return all queued I/O with ENXIO.
386 * XXX Handle any transactions queued to the card
387 * with XPT_ABORT_CCB.
389 bioq_flush(&softc->bio_queue, NULL, ENXIO);
391 disk_gone(softc->disk);
395 cdcleanup(struct cam_periph *periph)
397 struct cd_softc *softc;
399 softc = (struct cd_softc *)periph->softc;
401 cam_periph_unlock(periph);
402 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
403 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
404 xpt_print(periph->path, "can't remove sysctl context\n");
407 callout_drain(&softc->mediapoll_c);
408 disk_destroy(softc->disk);
409 free(softc, M_DEVBUF);
410 cam_periph_lock(periph);
414 cdasync(void *callback_arg, u_int32_t code,
415 struct cam_path *path, void *arg)
417 struct cam_periph *periph;
418 struct cd_softc *softc;
420 periph = (struct cam_periph *)callback_arg;
422 case AC_FOUND_DEVICE:
424 struct ccb_getdev *cgd;
427 cgd = (struct ccb_getdev *)arg;
431 if (cgd->protocol != PROTO_SCSI)
433 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
435 if (SID_TYPE(&cgd->inq_data) != T_CDROM
436 && SID_TYPE(&cgd->inq_data) != T_WORM)
440 * Allocate a peripheral instance for
441 * this device and start the probe
444 status = cam_periph_alloc(cdregister, cdoninvalidate,
446 "cd", CAM_PERIPH_BIO,
448 AC_FOUND_DEVICE, cgd);
450 if (status != CAM_REQ_CMP
451 && status != CAM_REQ_INPROG)
452 printf("cdasync: Unable to attach new device "
453 "due to status 0x%x\n", status);
457 case AC_UNIT_ATTENTION:
460 int error_code, sense_key, asc, ascq;
462 softc = (struct cd_softc *)periph->softc;
463 ccb = (union ccb *)arg;
466 * Handle all media change UNIT ATTENTIONs except
467 * our own, as they will be handled by cderror().
469 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
470 scsi_extract_sense_ccb(ccb,
471 &error_code, &sense_key, &asc, &ascq)) {
472 if (asc == 0x28 && ascq == 0x00)
473 disk_media_changed(softc->disk, M_NOWAIT);
475 cam_periph_async(periph, code, path, arg);
479 softc = (struct cd_softc *)periph->softc;
480 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
481 if (cam_periph_acquire(periph) == 0) {
483 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
490 struct ccb_hdr *ccbh;
492 softc = (struct cd_softc *)periph->softc;
494 * Don't fail on the expected unit attention
497 softc->flags |= CD_FLAG_RETRY_UA;
498 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
499 ccbh->ccb_state |= CD_CCB_RETRY_UA;
503 cam_periph_async(periph, code, path, arg);
509 cdsysctlinit(void *context, int pending)
511 struct cam_periph *periph;
512 struct cd_softc *softc;
513 char tmpstr[32], tmpstr2[16];
515 periph = (struct cam_periph *)context;
516 if (cam_periph_acquire(periph) != 0)
519 softc = (struct cd_softc *)periph->softc;
520 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
521 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
523 sysctl_ctx_init(&softc->sysctl_ctx);
524 softc->flags |= CD_FLAG_SCTX_INIT;
525 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
526 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
527 tmpstr2, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr,
530 if (softc->sysctl_tree == NULL) {
531 printf("cdsysctlinit: unable to allocate sysctl tree\n");
532 cam_periph_release(periph);
537 * Now register the sysctl handler, so the user can the value on
540 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
541 OID_AUTO, "minimum_cmd_size",
542 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
543 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
546 cam_periph_release(periph);
550 * We have a handler function for this so we can check the values when the
551 * user sets them, instead of every time we look at them.
554 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
558 value = *(int *)arg1;
560 error = sysctl_handle_int(oidp, &value, 0, req);
563 || (req->newptr == NULL))
567 * The only real values we can have here are 6 or 10. I don't
568 * really forsee having 12 be an option at any time in the future.
569 * So if the user sets something less than or equal to 6, we'll set
570 * it to 6. If he sets something greater than 6, we'll set it to 10.
572 * I suppose we could just return an error here for the wrong values,
573 * but I don't think it's necessary to do so, as long as we can
574 * determine the user's intent without too much trouble.
581 *(int *)arg1 = value;
587 cdregister(struct cam_periph *periph, void *arg)
589 struct cd_softc *softc;
590 struct ccb_pathinq cpi;
591 struct ccb_getdev *cgd;
595 cgd = (struct ccb_getdev *)arg;
597 printf("cdregister: no getdev CCB, can't register device\n");
598 return(CAM_REQ_CMP_ERR);
601 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
604 printf("cdregister: Unable to probe new device. "
605 "Unable to allocate softc\n");
606 return(CAM_REQ_CMP_ERR);
609 LIST_INIT(&softc->pending_ccbs);
610 STAILQ_INIT(&softc->mode_queue);
611 softc->state = CD_STATE_PROBE;
612 bioq_init(&softc->bio_queue);
613 if (SID_IS_REMOVABLE(&cgd->inq_data))
614 softc->flags |= CD_FLAG_DISC_REMOVABLE;
616 periph->softc = softc;
617 softc->periph = periph;
620 * See if this device has any quirks.
622 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
623 (caddr_t)cd_quirk_table,
624 nitems(cd_quirk_table),
625 sizeof(*cd_quirk_table), scsi_inquiry_match);
628 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
630 softc->quirks = CD_Q_NONE;
632 /* Check if the SIM does not want 6 byte commands */
633 xpt_path_inq(&cpi, periph->path);
634 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
635 softc->quirks |= CD_Q_10_BYTE_ONLY;
637 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
639 /* The default is 6 byte commands, unless quirked otherwise */
640 if (softc->quirks & CD_Q_10_BYTE_ONLY)
641 softc->minimum_command_size = 10;
643 softc->minimum_command_size = 6;
646 * Refcount and block open attempts until we are setup
649 (void)cam_periph_hold(periph, PRIBIO);
650 cam_periph_unlock(periph);
652 * Load the user's default, if any.
654 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
655 periph->unit_number);
656 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
658 /* 6 and 10 are the only permissible values here. */
659 if (softc->minimum_command_size < 6)
660 softc->minimum_command_size = 6;
661 else if (softc->minimum_command_size > 6)
662 softc->minimum_command_size = 10;
665 * We need to register the statistics structure for this device,
666 * but we don't have the blocksize yet for it. So, we register
667 * the structure and indicate that we don't have the blocksize
668 * yet. Unlike other SCSI peripheral drivers, we explicitly set
669 * the device type here to be CDROM, rather than just ORing in
670 * the device type. This is because this driver can attach to either
671 * CDROM or WORM devices, and we want this peripheral driver to
672 * show up in the devstat list as a CD peripheral driver, not a
673 * WORM peripheral driver. WORM drives will also have the WORM
674 * driver attached to them.
676 softc->disk = disk_alloc();
677 softc->disk->d_devstat = devstat_new_entry("cd",
678 periph->unit_number, 0,
679 DEVSTAT_BS_UNAVAILABLE,
681 XPORT_DEVSTAT_TYPE(cpi.transport),
682 DEVSTAT_PRIORITY_CD);
683 softc->disk->d_open = cdopen;
684 softc->disk->d_close = cdclose;
685 softc->disk->d_strategy = cdstrategy;
686 softc->disk->d_gone = cddiskgonecb;
687 softc->disk->d_ioctl = cdioctl;
688 softc->disk->d_name = "cd";
689 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
690 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
691 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
692 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
693 cgd->inq_data.product, sizeof(cgd->inq_data.product),
694 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
695 softc->disk->d_unit = periph->unit_number;
696 softc->disk->d_drv1 = periph;
698 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
699 else if (cpi.maxio > maxphys)
700 softc->disk->d_maxsize = maxphys; /* for safety */
702 softc->disk->d_maxsize = cpi.maxio;
703 softc->disk->d_flags = 0;
704 softc->disk->d_hba_vendor = cpi.hba_vendor;
705 softc->disk->d_hba_device = cpi.hba_device;
706 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
707 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
708 snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
709 "%s%d", cpi.dev_name, cpi.unit_number);
712 * Acquire a reference to the periph before we register with GEOM.
713 * We'll release this reference once GEOM calls us back (via
714 * dadiskgonecb()) telling us that our provider has been freed.
716 if (cam_periph_acquire(periph) != 0) {
717 xpt_print(periph->path, "%s: lost periph during "
718 "registration!\n", __func__);
719 cam_periph_lock(periph);
720 return (CAM_REQ_CMP_ERR);
723 disk_create(softc->disk, DISK_VERSION);
724 cam_periph_lock(periph);
727 * Add an async callback so that we get
728 * notified if this device goes away.
730 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
731 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
734 * Schedule a periodic media polling events.
736 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
737 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
738 (cgd->inq_flags & SID_AEN) == 0 &&
740 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
741 cdmediapoll, periph);
743 xpt_schedule(periph, CAM_PRIORITY_DEV);
748 cdopen(struct disk *dp)
750 struct cam_periph *periph;
751 struct cd_softc *softc;
754 periph = (struct cam_periph *)dp->d_drv1;
755 softc = (struct cd_softc *)periph->softc;
757 if (cam_periph_acquire(periph) != 0)
760 cam_periph_lock(periph);
762 if (softc->flags & CD_FLAG_INVALID) {
763 cam_periph_release_locked(periph);
764 cam_periph_unlock(periph);
768 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
769 cam_periph_release_locked(periph);
770 cam_periph_unlock(periph);
774 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
778 * Check for media, and set the appropriate flags. We don't bail
779 * if we don't have media, but then we don't allow anything but the
780 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
782 cdcheckmedia(periph, /*do_wait*/ 1);
784 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
785 cam_periph_unhold(periph);
787 cam_periph_unlock(periph);
793 cdclose(struct disk *dp)
795 struct cam_periph *periph;
796 struct cd_softc *softc;
798 periph = (struct cam_periph *)dp->d_drv1;
799 softc = (struct cd_softc *)periph->softc;
801 cam_periph_lock(periph);
802 if (cam_periph_hold(periph, PRIBIO) != 0) {
803 cam_periph_unlock(periph);
804 cam_periph_release(periph);
808 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
811 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
812 cdprevent(periph, PR_ALLOW);
815 * Since we're closing this CD, mark the blocksize as unavailable.
816 * It will be marked as available when the CD is opened again.
818 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
821 * We'll check the media and toc again at the next open().
823 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
825 cam_periph_unhold(periph);
826 cam_periph_release_locked(periph);
827 cam_periph_unlock(periph);
833 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
835 u_int32_t sense_flags),
836 u_int32_t cam_flags, u_int32_t sense_flags)
838 struct cd_softc *softc;
839 struct cam_periph *periph;
842 periph = xpt_path_periph(ccb->ccb_h.path);
843 softc = (struct cd_softc *)periph->softc;
845 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
846 softc->disk->d_devstat);
852 * Actually translate the requested transfer into one the physical driver
853 * can understand. The transfer is described by a buf and will include
854 * only one physical transfer.
857 cdstrategy(struct bio *bp)
859 struct cam_periph *periph;
860 struct cd_softc *softc;
862 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
863 cam_periph_lock(periph);
864 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
865 ("cdstrategy(%p)\n", bp));
867 softc = (struct cd_softc *)periph->softc;
870 * If the device has been made invalid, error out
872 if ((softc->flags & CD_FLAG_INVALID)) {
873 cam_periph_unlock(periph);
874 biofinish(bp, NULL, ENXIO);
879 * Place it in the queue of disk activities for this disk
881 bioq_disksort(&softc->bio_queue, bp);
884 * If we don't know that we have valid media, schedule the media
885 * check first. The I/O will get executed after the media check.
887 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
888 cdcheckmedia(periph, /*do_wait*/ 0);
890 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
892 cam_periph_unlock(periph);
897 cdstart(struct cam_periph *periph, union ccb *start_ccb)
899 struct cd_softc *softc;
901 struct ccb_scsiio *csio;
903 softc = (struct cd_softc *)periph->softc;
905 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
907 switch (softc->state) {
908 case CD_STATE_NORMAL:
910 bp = bioq_first(&softc->bio_queue);
914 csio = &start_ccb->csio;
915 scsi_test_unit_ready(csio,
916 /*retries*/ cd_retry_count,
921 start_ccb->ccb_h.ccb_bp = NULL;
922 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
923 xpt_action(start_ccb);
925 xpt_release_ccb(start_ccb);
929 cam_periph_release_locked(periph);
931 bioq_remove(&softc->bio_queue, bp);
933 if ((bp->bio_cmd != BIO_READ) &&
934 (bp->bio_cmd != BIO_WRITE)) {
935 biofinish(bp, NULL, EOPNOTSUPP);
936 xpt_release_ccb(start_ccb);
940 scsi_read_write(&start_ccb->csio,
941 /*retries*/ cd_retry_count,
944 /* read */bp->bio_cmd == BIO_READ ?
945 SCSI_RW_READ : SCSI_RW_WRITE,
947 /* minimum_cmd_size */ 10,
948 /* lba */ bp->bio_offset /
949 softc->params.blksize,
950 bp->bio_bcount / softc->params.blksize,
951 /* data_ptr */ bp->bio_data,
952 /* dxfer_len */ bp->bio_bcount,
953 /* sense_len */ cd_retry_count ?
954 SSD_FULL_SIZE : SF_NO_PRINT,
955 /* timeout */ cd_timeout);
956 /* Use READ CD command for audio tracks. */
957 if (softc->params.blksize == 2352) {
958 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
959 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
960 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
961 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
962 start_ccb->csio.cdb_len = 12;
964 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
966 LIST_INSERT_HEAD(&softc->pending_ccbs,
967 &start_ccb->ccb_h, periph_links.le);
968 softc->outstanding_cmds++;
970 /* We expect a unit attention from this device */
971 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
972 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
973 softc->flags &= ~CD_FLAG_RETRY_UA;
976 start_ccb->ccb_h.ccb_bp = bp;
977 bp = bioq_first(&softc->bio_queue);
979 xpt_action(start_ccb);
981 if (bp != NULL || softc->tur) {
982 /* Have more work to do, so ensure we stay scheduled */
983 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
988 case CD_STATE_MEDIA_SIZE:
990 struct scsi_read_capacity_data *rcap;
992 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
993 M_SCSICD, M_NOWAIT | M_ZERO);
995 xpt_print(periph->path,
996 "%s: Couldn't malloc read_capacity data\n",
998 xpt_release_ccb(start_ccb);
1000 * We can't probe because we can't allocate memory,
1001 * so invalidate the peripheral. The system probably
1002 * has larger problems at this stage. If we've
1003 * already probed (and are re-probing capacity), we
1004 * don't need to invalidate.
1006 * XXX KDM need to reset probe state and kick out
1009 if (softc->state == CD_STATE_PROBE)
1010 cam_periph_invalidate(periph);
1015 * Set the default capacity and sector size to something that
1016 * GEOM can handle. This will get reset when a read capacity
1017 * completes successfully.
1019 softc->disk->d_sectorsize = 2048;
1020 softc->disk->d_mediasize = 0;
1022 csio = &start_ccb->csio;
1023 scsi_read_capacity(csio,
1024 /*retries*/ cd_retry_count,
1030 start_ccb->ccb_h.ccb_bp = NULL;
1031 if (softc->state == CD_STATE_PROBE)
1032 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1034 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_SIZE;
1035 xpt_action(start_ccb);
1038 case CD_STATE_MEDIA_ALLOW:
1039 case CD_STATE_MEDIA_PREVENT:
1042 * If the CD is already locked, we don't need to do this.
1043 * Move on to the capacity check.
1045 if (softc->state == CD_STATE_MEDIA_PREVENT
1046 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0) {
1047 softc->state = CD_STATE_MEDIA_SIZE;
1048 xpt_release_ccb(start_ccb);
1049 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1053 scsi_prevent(&start_ccb->csio,
1054 /*retries*/ cd_retry_count,
1056 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1057 /*action*/ (softc->state == CD_STATE_MEDIA_ALLOW) ?
1058 PR_ALLOW : PR_PREVENT,
1059 /*sense_len*/ SSD_FULL_SIZE,
1062 start_ccb->ccb_h.ccb_bp = NULL;
1063 if (softc->state == CD_STATE_MEDIA_ALLOW)
1064 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_ALLOW;
1066 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_PREVENT;
1067 xpt_action(start_ccb);
1070 case CD_STATE_MEDIA_TOC_HDR: {
1071 struct ioc_toc_header *toch;
1073 bzero(&softc->toc, sizeof(softc->toc));
1075 toch = &softc->toc.header;
1077 scsi_read_toc(&start_ccb->csio,
1078 /*retries*/ cd_retry_count,
1080 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1082 /*format*/ SRTOC_FORMAT_TOC,
1084 /*data_ptr*/ (uint8_t *)toch,
1085 /*dxfer_len*/ sizeof(*toch),
1086 /*sense_len*/ SSD_FULL_SIZE,
1088 start_ccb->ccb_h.ccb_bp = NULL;
1089 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_HDR;
1090 xpt_action(start_ccb);
1093 case CD_STATE_MEDIA_TOC_FULL: {
1094 bzero(&softc->toc, sizeof(softc->toc));
1096 scsi_read_toc(&start_ccb->csio,
1097 /*retries*/ cd_retry_count,
1099 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1101 /*format*/ SRTOC_FORMAT_TOC,
1103 /*data_ptr*/ (uint8_t *)&softc->toc,
1104 /*dxfer_len*/ softc->toc_read_len ?
1105 softc->toc_read_len :
1107 /*sense_len*/ SSD_FULL_SIZE,
1109 start_ccb->ccb_h.ccb_bp = NULL;
1110 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_FULL;
1111 xpt_action(start_ccb);
1114 case CD_STATE_MEDIA_TOC_LEAD: {
1115 struct cd_toc_single *leadout;
1117 leadout = &softc->leadout;
1118 bzero(leadout, sizeof(*leadout));
1120 scsi_read_toc(&start_ccb->csio,
1121 /*retries*/ cd_retry_count,
1123 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1124 /*byte1_flags*/ CD_MSF,
1125 /*format*/ SRTOC_FORMAT_TOC,
1127 /*data_ptr*/ (uint8_t *)leadout,
1128 /*dxfer_len*/ sizeof(*leadout),
1129 /*sense_len*/ SSD_FULL_SIZE,
1131 start_ccb->ccb_h.ccb_bp = NULL;
1132 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_LEAD;
1133 xpt_action(start_ccb);
1140 cddone(struct cam_periph *periph, union ccb *done_ccb)
1142 struct cd_softc *softc;
1143 struct ccb_scsiio *csio;
1145 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1147 softc = (struct cd_softc *)periph->softc;
1148 csio = &done_ccb->csio;
1150 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1151 case CD_CCB_BUFFER_IO:
1156 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1159 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1162 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1167 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1168 if (error == ERESTART) {
1170 * A retry was scheuled, so
1178 xpt_print(periph->path,
1179 "cddone: got error %#x back\n", error);
1180 bioq_flush(&softc->bio_queue, NULL, EIO);
1181 bp->bio_resid = bp->bio_bcount;
1182 bp->bio_error = error;
1183 bp->bio_flags |= BIO_ERROR;
1184 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1185 cam_release_devq(done_ccb->ccb_h.path,
1189 /*getcount_only*/0);
1192 bp->bio_resid = csio->resid;
1194 if (bp->bio_resid != 0) {
1196 * Short transfer ???
1197 * XXX: not sure this is correct for partial
1200 bp->bio_flags |= BIO_ERROR;
1204 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1205 softc->outstanding_cmds--;
1207 biofinish(bp, NULL, 0);
1212 struct scsi_read_capacity_data *rdcap;
1214 struct cd_params *cdp;
1217 cdp = &softc->params;
1218 announce_buf = softc->announce_temp;
1219 bzero(announce_buf, CD_ANNOUNCETMP_SZ);
1221 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1223 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1224 cdp->blksize = scsi_4btoul (rdcap->length);
1227 * Retry any UNIT ATTENTION type errors. They
1228 * are expected at boot.
1230 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1231 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1232 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1233 snprintf(announce_buf, CD_ANNOUNCETMP_SZ,
1234 "%juMB (%ju %u byte sectors)",
1235 ((uintmax_t)cdp->disksize * cdp->blksize) /
1237 (uintmax_t)cdp->disksize, cdp->blksize);
1239 if (error == ERESTART) {
1241 * A retry was scheuled, so
1247 int sense_key, error_code;
1250 struct ccb_getdev cgd;
1252 /* Don't wedge this device's queue */
1253 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1254 cam_release_devq(done_ccb->ccb_h.path,
1258 /*getcount_only*/0);
1260 status = done_ccb->ccb_h.status;
1262 bzero(&cgd, sizeof(cgd));
1263 xpt_setup_ccb(&cgd.ccb_h,
1264 done_ccb->ccb_h.path,
1265 CAM_PRIORITY_NORMAL);
1266 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1267 xpt_action((union ccb *)&cgd);
1269 if (scsi_extract_sense_ccb(done_ccb,
1270 &error_code, &sense_key, &asc, &ascq))
1276 * Attach to anything that claims to be a
1277 * CDROM or WORM device, as long as it
1278 * doesn't return a "Logical unit not
1279 * supported" (0x25) error.
1281 if ((have_sense) && (asc != 0x25)
1282 && (error_code == SSD_CURRENT_ERROR
1283 || error_code == SSD_DESC_CURRENT_ERROR)) {
1284 const char *sense_key_desc;
1285 const char *asc_desc;
1287 scsi_sense_desc(sense_key, asc, ascq,
1291 snprintf(announce_buf,
1293 "Attempt to query device "
1294 "size failed: %s, %s",
1297 } else if ((have_sense == 0)
1298 && ((status & CAM_STATUS_MASK) ==
1299 CAM_SCSI_STATUS_ERROR)
1300 && (csio->scsi_status ==
1301 SCSI_STATUS_BUSY)) {
1302 snprintf(announce_buf,
1304 "Attempt to query device "
1305 "size failed: SCSI Status: %s",
1306 scsi_status_string(csio));
1307 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1309 * We only print out an error for
1310 * CDROM type devices. For WORM
1311 * devices, we don't print out an
1312 * error since a few WORM devices
1313 * don't support CDROM commands.
1314 * If we have sense information, go
1315 * ahead and print it out.
1316 * Otherwise, just say that we
1321 * Just print out the error, not
1322 * the full probe message, when we
1329 xpt_print(periph->path,
1330 "got CAM status %#x\n",
1331 done_ccb->ccb_h.status);
1333 xpt_print(periph->path, "fatal error, "
1334 "failed to attach to device\n");
1336 * Invalidate this peripheral.
1338 cam_periph_invalidate(periph);
1340 announce_buf = NULL;
1343 * Invalidate this peripheral.
1345 cam_periph_invalidate(periph);
1346 announce_buf = NULL;
1350 free(rdcap, M_SCSICD);
1351 if (announce_buf != NULL) {
1354 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ,
1356 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1357 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
1363 * Create our sysctl variables, now that we know
1364 * we have successfully attached.
1366 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1368 softc->state = CD_STATE_NORMAL;
1370 * Since our peripheral may be invalidated by an error
1371 * above or an external event, we must release our CCB
1372 * before releasing the probe lock on the peripheral.
1373 * The peripheral will only go away once the last lock
1374 * is removed, and we need it around for the CCB release
1377 xpt_release_ccb(done_ccb);
1378 cam_periph_unhold(periph);
1383 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1384 if (cderror(done_ccb, CAM_RETRY_SELTO,
1385 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1388 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1389 cam_release_devq(done_ccb->ccb_h.path,
1393 /*getcount_only*/0);
1395 xpt_release_ccb(done_ccb);
1396 cam_periph_release_locked(periph);
1399 case CD_CCB_MEDIA_ALLOW:
1400 case CD_CCB_MEDIA_PREVENT:
1407 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1408 error = cderror(done_ccb, CAM_RETRY_SELTO,
1409 SF_RETRY_UA | SF_NO_PRINT);
1411 if (error == ERESTART)
1413 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1414 cam_release_devq(done_ccb->ccb_h.path,
1418 /*getcount_only*/0);
1421 * Note that just like the original cdcheckmedia(), we do
1422 * a prevent without failing the whole operation if the
1423 * prevent fails. We try, but keep going if it doesn't
1427 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1428 CD_CCB_MEDIA_PREVENT)
1433 xpt_release_ccb(done_ccb);
1435 if (is_prevent != 0) {
1437 softc->flags |= CD_FLAG_DISC_LOCKED;
1439 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1440 softc->state = CD_STATE_MEDIA_SIZE;
1441 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1444 softc->flags &= ~CD_FLAG_DISC_LOCKED;
1445 softc->state = CD_STATE_NORMAL;
1446 if (bioq_first(&softc->bio_queue) != NULL)
1447 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1451 case CD_CCB_MEDIA_SIZE:
1453 struct scsi_read_capacity_data *rdcap;
1457 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1458 error = cderror(done_ccb, CAM_RETRY_SELTO,
1459 SF_RETRY_UA | SF_NO_PRINT);
1461 if (error == ERESTART)
1463 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1464 cam_release_devq(done_ccb->ccb_h.path,
1468 /*getcount_only*/0);
1469 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1472 softc->params.disksize =scsi_4btoul(rdcap->addr) + 1;
1473 softc->params.blksize = scsi_4btoul(rdcap->length);
1475 /* Make sure we got at least some block size. */
1476 if (softc->params.blksize == 0)
1479 * SCSI-3 mandates that the reported blocksize shall be
1480 * 2048. Older drives sometimes report funny values,
1481 * trim it down to 2048, or other parts of the kernel
1482 * will get confused.
1484 * XXX we leave drives alone that might report 512
1485 * bytes, as well as drives reporting more weird
1486 * sizes like perhaps 4K.
1488 if (softc->params.blksize > 2048
1489 && softc->params.blksize <= 2352)
1490 softc->params.blksize = 2048;
1492 free(rdcap, M_SCSICD);
1495 softc->disk->d_sectorsize = softc->params.blksize;
1496 softc->disk->d_mediasize =
1497 (off_t)softc->params.blksize *
1498 softc->params.disksize;
1499 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
1500 softc->state = CD_STATE_MEDIA_TOC_HDR;
1502 softc->flags &= ~(CD_FLAG_VALID_MEDIA |
1504 bioq_flush(&softc->bio_queue, NULL, EINVAL);
1505 softc->state = CD_STATE_MEDIA_ALLOW;
1506 cdmediaprobedone(periph);
1508 xpt_release_ccb(done_ccb);
1509 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1512 case CD_CCB_MEDIA_TOC_HDR:
1513 case CD_CCB_MEDIA_TOC_FULL:
1514 case CD_CCB_MEDIA_TOC_LEAD:
1517 struct ioc_toc_header *toch;
1523 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1524 error = cderror(done_ccb, CAM_RETRY_SELTO,
1525 SF_RETRY_UA | SF_NO_PRINT);
1527 if (error == ERESTART)
1530 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1531 cam_release_devq(done_ccb->ccb_h.path,
1535 /*getcount_only*/0);
1538 * We will get errors here for media that doesn't have a table
1539 * of contents. According to the MMC-3 spec: "When a Read
1540 * TOC/PMA/ATIP command is presented for a DDCD/CD-R/RW media,
1541 * where the first TOC has not been recorded (no complete
1542 * session) and the Format codes 0000b, 0001b, or 0010b are
1543 * specified, this command shall be rejected with an INVALID
1544 * FIELD IN CDB. Devices that are not capable of reading an
1545 * incomplete session on DDC/CD-R/RW media shall report
1546 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
1548 * So this isn't fatal if we can't read the table of contents,
1549 * it just means that the user won't be able to issue the
1550 * play tracks ioctl, and likely lots of other stuff won't
1551 * work either. They need to burn the CD before we can do
1552 * a whole lot with it. So we don't print anything here if
1553 * we get an error back.
1555 * We also bail out if the drive doesn't at least give us
1556 * the full TOC header.
1559 || ((csio->dxfer_len - csio->resid) <
1560 sizeof(struct ioc_toc_header))) {
1561 softc->flags &= ~CD_FLAG_VALID_TOC;
1562 bzero(&softc->toc, sizeof(softc->toc));
1564 * Failing the TOC read is not an error.
1566 softc->state = CD_STATE_NORMAL;
1567 xpt_release_ccb(done_ccb);
1569 cdmediaprobedone(periph);
1572 * Go ahead and schedule I/O execution if there is
1573 * anything in the queue. It'll probably get
1574 * kicked out with an error.
1576 if (bioq_first(&softc->bio_queue) != NULL)
1577 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1582 * Note that this is NOT the storage location used for the
1585 toch = &softc->toc.header;
1587 if (softc->quirks & CD_Q_BCD_TRACKS) {
1588 toch->starting_track = bcd2bin(toch->starting_track);
1589 toch->ending_track = bcd2bin(toch->ending_track);
1592 /* Number of TOC entries, plus leadout */
1593 num_entries = (toch->ending_track - toch->starting_track) + 2;
1594 cdindex = toch->starting_track + num_entries -1;
1596 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1597 CD_CCB_MEDIA_TOC_HDR) {
1598 if (num_entries <= 0) {
1599 softc->flags &= ~CD_FLAG_VALID_TOC;
1600 bzero(&softc->toc, sizeof(softc->toc));
1602 * Failing the TOC read is not an error.
1604 softc->state = CD_STATE_NORMAL;
1605 xpt_release_ccb(done_ccb);
1607 cdmediaprobedone(periph);
1610 * Go ahead and schedule I/O execution if
1611 * there is anything in the queue. It'll
1612 * probably get kicked out with an error.
1614 if (bioq_first(&softc->bio_queue) != NULL)
1615 xpt_schedule(periph,
1616 CAM_PRIORITY_NORMAL);
1618 softc->toc_read_len = num_entries *
1619 sizeof(struct cd_toc_entry);
1620 softc->toc_read_len += sizeof(*toch);
1622 softc->state = CD_STATE_MEDIA_TOC_FULL;
1623 xpt_release_ccb(done_ccb);
1624 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1628 } else if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1629 CD_CCB_MEDIA_TOC_LEAD) {
1630 struct cd_toc_single *leadout;
1632 leadout = (struct cd_toc_single *)csio->data_ptr;
1633 softc->toc.entries[cdindex - toch->starting_track] =
1635 } else if (((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) ==
1636 CD_CCB_MEDIA_TOC_FULL)
1637 && (cdindex == toch->ending_track + 1)) {
1639 * XXX KDM is this necessary? Probably only if the
1640 * drive doesn't return leadout information with the
1641 * table of contents.
1643 softc->state = CD_STATE_MEDIA_TOC_LEAD;
1644 xpt_release_ccb(done_ccb);
1645 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1649 if (softc->quirks & CD_Q_BCD_TRACKS) {
1650 for (cdindex = 0; cdindex < num_entries - 1; cdindex++){
1651 softc->toc.entries[cdindex].track =
1652 bcd2bin(softc->toc.entries[cdindex].track);
1656 softc->flags |= CD_FLAG_VALID_TOC;
1657 /* If the first track is audio, correct sector size. */
1658 if ((softc->toc.entries[0].control & 4) == 0) {
1659 softc->disk->d_sectorsize =softc->params.blksize = 2352;
1660 softc->disk->d_mediasize =
1661 (off_t)softc->params.blksize *
1662 softc->params.disksize;
1664 softc->state = CD_STATE_NORMAL;
1667 * We unconditionally (re)set the blocksize each time the
1668 * CD device is opened. This is because the CD can change,
1669 * and therefore the blocksize might change.
1670 * XXX problems here if some slice or partition is still
1671 * open with the old size?
1673 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE)!=0)
1674 softc->disk->d_devstat->flags &=
1675 ~DEVSTAT_BS_UNAVAILABLE;
1676 softc->disk->d_devstat->block_size = softc->params.blksize;
1678 xpt_release_ccb(done_ccb);
1680 cdmediaprobedone(periph);
1682 if (bioq_first(&softc->bio_queue) != NULL)
1683 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1689 xpt_release_ccb(done_ccb);
1692 static union cd_pages *
1693 cdgetpage(struct cd_mode_params *mode_params)
1695 union cd_pages *page;
1697 if (mode_params->cdb_size == 10)
1698 page = (union cd_pages *)find_mode_page_10(
1699 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1701 page = (union cd_pages *)find_mode_page_6(
1702 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1708 cdgetpagesize(int page_num)
1712 for (i = 0; i < nitems(cd_page_size_table); i++) {
1713 if (cd_page_size_table[i].page == page_num)
1714 return (cd_page_size_table[i].page_size);
1720 static struct cd_toc_entry *
1721 te_data_get_ptr(void *irtep, u_long cmd)
1724 struct ioc_read_toc_entry irte;
1725 #ifdef COMPAT_FREEBSD32
1726 struct ioc_read_toc_entry32 irte32;
1731 switch (IOCPARM_LEN(cmd)) {
1732 case sizeof(irteup->irte):
1733 return (irteup->irte.data);
1734 #ifdef COMPAT_FREEBSD32
1735 case sizeof(irteup->irte32):
1736 return ((struct cd_toc_entry *)(uintptr_t)irteup->irte32.data);
1739 panic("Unhandled ioctl command %ld", cmd);
1744 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1747 struct cam_periph *periph;
1748 struct cd_softc *softc;
1751 periph = (struct cam_periph *)dp->d_drv1;
1752 cam_periph_lock(periph);
1754 softc = (struct cd_softc *)periph->softc;
1756 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1757 ("cdioctl(%#lx)\n", cmd));
1759 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1760 cam_periph_unlock(periph);
1761 cam_periph_release(periph);
1766 * If we don't have media loaded, check for it. If still don't
1767 * have media loaded, we can only do a load or eject.
1769 * We only care whether media is loaded if this is a cd-specific ioctl
1770 * (thus the IOCGROUP check below). Note that this will break if
1771 * anyone adds any ioctls into the switch statement below that don't
1772 * have their ioctl group set to 'c'.
1774 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1775 && ((cmd != CDIOCCLOSE)
1776 && (cmd != CDIOCEJECT))
1777 && (IOCGROUP(cmd) == 'c')) {
1778 error = cdcheckmedia(periph, /*do_wait*/ 1);
1780 cam_periph_unhold(periph);
1781 cam_periph_unlock(periph);
1786 * Drop the lock here so later mallocs can use WAITOK. The periph
1787 * is essentially locked still with the cam_periph_hold call above.
1789 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"));
1944 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1946 free(params.mode_buf, M_SCSICD);
1947 cam_periph_unlock(periph);
1950 page = cdgetpage(¶ms);
1952 page->audio.flags &= ~CD_PA_SOTC;
1953 page->audio.flags |= CD_PA_IMMED;
1954 error = cdsetmode(periph, ¶ms);
1955 free(params.mode_buf, M_SCSICD);
1957 cam_periph_unlock(periph);
1960 error = cdplay(periph, args->blk, args->len);
1961 cam_periph_unlock(periph);
1964 case CDIOCREADSUBCHANNEL:
1966 struct ioc_read_subchannel *args
1967 = (struct ioc_read_subchannel *) addr;
1968 struct cd_sub_channel_info *data;
1969 u_int32_t len = args->data_len;
1971 data = malloc(sizeof(struct cd_sub_channel_info),
1972 M_SCSICD, M_WAITOK | M_ZERO);
1974 cam_periph_lock(periph);
1975 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1976 ("trying to do CDIOCREADSUBCHANNEL\n"));
1978 if ((len > sizeof(struct cd_sub_channel_info)) ||
1979 (len < sizeof(struct cd_sub_channel_header))) {
1981 "scsi_cd: cdioctl: "
1982 "cdioreadsubchannel: error, len=%d\n",
1985 free(data, M_SCSICD);
1986 cam_periph_unlock(periph);
1990 if (softc->quirks & CD_Q_BCD_TRACKS)
1991 args->track = bin2bcd(args->track);
1993 error = cdreadsubchannel(periph, args->address_format,
1994 args->data_format, args->track, data, len);
1997 free(data, M_SCSICD);
1998 cam_periph_unlock(periph);
2001 if (softc->quirks & CD_Q_BCD_TRACKS)
2002 data->what.track_info.track_number =
2003 bcd2bin(data->what.track_info.track_number);
2004 len = min(len, ((data->header.data_len[0] << 8) +
2005 data->header.data_len[1] +
2006 sizeof(struct cd_sub_channel_header)));
2007 cam_periph_unlock(periph);
2008 error = copyout(data, args->data, len);
2009 free(data, M_SCSICD);
2013 case CDIOREADTOCHEADER:
2015 struct ioc_toc_header *th;
2017 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2020 cam_periph_lock(periph);
2021 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2022 ("trying to do CDIOREADTOCHEADER\n"));
2024 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2025 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2028 cam_periph_unlock(periph);
2031 if (softc->quirks & CD_Q_BCD_TRACKS) {
2032 /* we are going to have to convert the BCD
2033 * encoding on the cd to what is expected
2035 th->starting_track =
2036 bcd2bin(th->starting_track);
2037 th->ending_track = bcd2bin(th->ending_track);
2039 th->len = ntohs(th->len);
2040 bcopy(th, addr, sizeof(*th));
2042 cam_periph_unlock(periph);
2045 case CDIOREADTOCENTRYS:
2046 #ifdef COMPAT_FREEBSD32
2047 case CDIOREADTOCENTRYS_32:
2050 struct cd_tocdata *data;
2051 struct cd_toc_single *lead;
2052 struct ioc_read_toc_entry *te =
2053 (struct ioc_read_toc_entry *) addr;
2054 struct ioc_toc_header *th;
2055 u_int32_t len, readlen, idx, num;
2056 u_int32_t starting_track = te->starting_track;
2058 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2059 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2061 cam_periph_lock(periph);
2062 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2063 ("trying to do CDIOREADTOCENTRYS\n"));
2065 if (te->data_len < sizeof(struct cd_toc_entry)
2066 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2067 || (te->address_format != CD_MSF_FORMAT
2068 && te->address_format != CD_LBA_FORMAT)) {
2070 printf("scsi_cd: error in readtocentries, "
2071 "returning EINVAL\n");
2072 free(data, M_SCSICD);
2073 free(lead, M_SCSICD);
2074 cam_periph_unlock(periph);
2079 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2080 sizeof (*th), /*sense_flags*/0);
2082 free(data, M_SCSICD);
2083 free(lead, M_SCSICD);
2084 cam_periph_unlock(periph);
2088 if (softc->quirks & CD_Q_BCD_TRACKS) {
2089 /* we are going to have to convert the BCD
2090 * encoding on the cd to what is expected
2092 th->starting_track =
2093 bcd2bin(th->starting_track);
2094 th->ending_track = bcd2bin(th->ending_track);
2097 if (starting_track == 0)
2098 starting_track = th->starting_track;
2099 else if (starting_track == LEADOUT)
2100 starting_track = th->ending_track + 1;
2101 else if (starting_track < th->starting_track ||
2102 starting_track > th->ending_track + 1) {
2103 printf("scsi_cd: error in readtocentries, "
2104 "returning EINVAL\n");
2105 free(data, M_SCSICD);
2106 free(lead, M_SCSICD);
2107 cam_periph_unlock(periph);
2112 /* calculate reading length without leadout entry */
2113 readlen = (th->ending_track - starting_track + 1) *
2114 sizeof(struct cd_toc_entry);
2116 /* and with leadout entry */
2117 len = readlen + sizeof(struct cd_toc_entry);
2118 if (te->data_len < len) {
2123 if (len > sizeof(data->entries)) {
2124 printf("scsi_cd: error in readtocentries, "
2125 "returning EINVAL\n");
2127 free(data, M_SCSICD);
2128 free(lead, M_SCSICD);
2129 cam_periph_unlock(periph);
2132 num = len / sizeof(struct cd_toc_entry);
2135 error = cdreadtoc(periph, te->address_format,
2138 readlen + sizeof (*th),
2141 free(data, M_SCSICD);
2142 free(lead, M_SCSICD);
2143 cam_periph_unlock(periph);
2148 /* make leadout entry if needed */
2149 idx = starting_track + num - 1;
2150 if (softc->quirks & CD_Q_BCD_TRACKS)
2151 th->ending_track = bcd2bin(th->ending_track);
2152 if (idx == th->ending_track + 1) {
2153 error = cdreadtoc(periph, te->address_format,
2154 LEADOUT, (u_int8_t *)lead,
2158 free(data, M_SCSICD);
2159 free(lead, M_SCSICD);
2160 cam_periph_unlock(periph);
2163 data->entries[idx - starting_track] =
2166 if (softc->quirks & CD_Q_BCD_TRACKS) {
2167 for (idx = 0; idx < num - 1; idx++) {
2168 data->entries[idx].track =
2169 bcd2bin(data->entries[idx].track);
2173 cam_periph_unlock(periph);
2174 error = copyout(data->entries, te_data_get_ptr(te, cmd),
2176 free(data, M_SCSICD);
2177 free(lead, M_SCSICD);
2180 case CDIOREADTOCENTRY:
2182 struct cd_toc_single *data;
2183 struct ioc_read_toc_single_entry *te =
2184 (struct ioc_read_toc_single_entry *) addr;
2185 struct ioc_toc_header *th;
2188 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2190 cam_periph_lock(periph);
2191 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2192 ("trying to do CDIOREADTOCENTRY\n"));
2194 if (te->address_format != CD_MSF_FORMAT
2195 && te->address_format != CD_LBA_FORMAT) {
2196 printf("error in readtocentry, "
2197 " returning EINVAL\n");
2198 free(data, M_SCSICD);
2200 cam_periph_unlock(periph);
2205 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2206 sizeof (*th), /*sense_flags*/0);
2208 free(data, M_SCSICD);
2209 cam_periph_unlock(periph);
2213 if (softc->quirks & CD_Q_BCD_TRACKS) {
2214 /* we are going to have to convert the BCD
2215 * encoding on the cd to what is expected
2217 th->starting_track =
2218 bcd2bin(th->starting_track);
2219 th->ending_track = bcd2bin(th->ending_track);
2223 track = th->starting_track;
2224 else if (track == LEADOUT)
2226 else if (track < th->starting_track ||
2227 track > th->ending_track + 1) {
2228 printf("error in readtocentry, "
2229 " returning EINVAL\n");
2230 free(data, M_SCSICD);
2232 cam_periph_unlock(periph);
2236 error = cdreadtoc(periph, te->address_format, track,
2237 (u_int8_t *)data, sizeof(*data),
2240 free(data, M_SCSICD);
2241 cam_periph_unlock(periph);
2245 if (softc->quirks & CD_Q_BCD_TRACKS)
2246 data->entry.track = bcd2bin(data->entry.track);
2247 bcopy(&data->entry, &te->entry,
2248 sizeof(struct cd_toc_entry));
2249 free(data, M_SCSICD);
2250 cam_periph_unlock(periph);
2255 struct ioc_patch *arg = (struct ioc_patch *)addr;
2256 struct cd_mode_params params;
2257 union cd_pages *page;
2259 params.alloc_len = sizeof(union cd_mode_data_6_10);
2260 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2263 cam_periph_lock(periph);
2264 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2265 ("trying to do CDIOCSETPATCH\n"));
2267 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2269 free(params.mode_buf, M_SCSICD);
2270 cam_periph_unlock(periph);
2273 page = cdgetpage(¶ms);
2275 page->audio.port[LEFT_PORT].channels =
2277 page->audio.port[RIGHT_PORT].channels =
2279 page->audio.port[2].channels = arg->patch[2];
2280 page->audio.port[3].channels = arg->patch[3];
2281 error = cdsetmode(periph, ¶ms);
2282 free(params.mode_buf, M_SCSICD);
2283 cam_periph_unlock(periph);
2288 struct ioc_vol *arg = (struct ioc_vol *) addr;
2289 struct cd_mode_params params;
2290 union cd_pages *page;
2292 params.alloc_len = sizeof(union cd_mode_data_6_10);
2293 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2296 cam_periph_lock(periph);
2297 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2298 ("trying to do CDIOCGETVOL\n"));
2300 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2302 free(params.mode_buf, M_SCSICD);
2303 cam_periph_unlock(periph);
2306 page = cdgetpage(¶ms);
2308 arg->vol[LEFT_PORT] =
2309 page->audio.port[LEFT_PORT].volume;
2310 arg->vol[RIGHT_PORT] =
2311 page->audio.port[RIGHT_PORT].volume;
2312 arg->vol[2] = page->audio.port[2].volume;
2313 arg->vol[3] = page->audio.port[3].volume;
2314 free(params.mode_buf, M_SCSICD);
2315 cam_periph_unlock(periph);
2320 struct ioc_vol *arg = (struct ioc_vol *) addr;
2321 struct cd_mode_params params;
2322 union cd_pages *page;
2324 params.alloc_len = sizeof(union cd_mode_data_6_10);
2325 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2328 cam_periph_lock(periph);
2329 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2330 ("trying to do CDIOCSETVOL\n"));
2332 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2334 free(params.mode_buf, M_SCSICD);
2335 cam_periph_unlock(periph);
2338 page = cdgetpage(¶ms);
2340 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2341 page->audio.port[LEFT_PORT].volume =
2342 arg->vol[LEFT_PORT];
2343 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2344 page->audio.port[RIGHT_PORT].volume =
2345 arg->vol[RIGHT_PORT];
2346 page->audio.port[2].volume = arg->vol[2];
2347 page->audio.port[3].volume = arg->vol[3];
2348 error = cdsetmode(periph, ¶ms);
2349 cam_periph_unlock(periph);
2350 free(params.mode_buf, M_SCSICD);
2355 struct cd_mode_params params;
2356 union cd_pages *page;
2358 params.alloc_len = sizeof(union cd_mode_data_6_10);
2359 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2362 cam_periph_lock(periph);
2363 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2364 ("trying to do CDIOCSETMONO\n"));
2366 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2368 free(params.mode_buf, M_SCSICD);
2369 cam_periph_unlock(periph);
2372 page = cdgetpage(¶ms);
2374 page->audio.port[LEFT_PORT].channels =
2375 LEFT_CHANNEL | RIGHT_CHANNEL;
2376 page->audio.port[RIGHT_PORT].channels =
2377 LEFT_CHANNEL | RIGHT_CHANNEL;
2378 page->audio.port[2].channels = 0;
2379 page->audio.port[3].channels = 0;
2380 error = cdsetmode(periph, ¶ms);
2381 cam_periph_unlock(periph);
2382 free(params.mode_buf, M_SCSICD);
2385 case CDIOCSETSTEREO:
2387 struct cd_mode_params params;
2388 union cd_pages *page;
2390 params.alloc_len = sizeof(union cd_mode_data_6_10);
2391 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2394 cam_periph_lock(periph);
2395 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2396 ("trying to do CDIOCSETSTEREO\n"));
2398 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2400 free(params.mode_buf, M_SCSICD);
2401 cam_periph_unlock(periph);
2404 page = cdgetpage(¶ms);
2406 page->audio.port[LEFT_PORT].channels =
2408 page->audio.port[RIGHT_PORT].channels =
2410 page->audio.port[2].channels = 0;
2411 page->audio.port[3].channels = 0;
2412 error = cdsetmode(periph, ¶ms);
2413 free(params.mode_buf, M_SCSICD);
2414 cam_periph_unlock(periph);
2419 struct cd_mode_params params;
2420 union cd_pages *page;
2422 params.alloc_len = sizeof(union cd_mode_data_6_10);
2423 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2426 cam_periph_lock(periph);
2427 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2428 ("trying to do CDIOCSETMUTE\n"));
2430 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2432 free(params.mode_buf, M_SCSICD);
2433 cam_periph_unlock(periph);
2436 page = cdgetpage(¶ms);
2438 page->audio.port[LEFT_PORT].channels = 0;
2439 page->audio.port[RIGHT_PORT].channels = 0;
2440 page->audio.port[2].channels = 0;
2441 page->audio.port[3].channels = 0;
2442 error = cdsetmode(periph, ¶ms);
2443 free(params.mode_buf, M_SCSICD);
2444 cam_periph_unlock(periph);
2449 struct cd_mode_params params;
2450 union cd_pages *page;
2452 params.alloc_len = sizeof(union cd_mode_data_6_10);
2453 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2456 cam_periph_lock(periph);
2457 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2458 ("trying to do CDIOCSETLEFT\n"));
2460 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2462 free(params.mode_buf, M_SCSICD);
2463 cam_periph_unlock(periph);
2466 page = cdgetpage(¶ms);
2468 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2469 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2470 page->audio.port[2].channels = 0;
2471 page->audio.port[3].channels = 0;
2472 error = cdsetmode(periph, ¶ms);
2473 free(params.mode_buf, M_SCSICD);
2474 cam_periph_unlock(periph);
2479 struct cd_mode_params params;
2480 union cd_pages *page;
2482 params.alloc_len = sizeof(union cd_mode_data_6_10);
2483 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2486 cam_periph_lock(periph);
2487 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2488 ("trying to do CDIOCSETRIGHT\n"));
2490 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2492 free(params.mode_buf, M_SCSICD);
2493 cam_periph_unlock(periph);
2496 page = cdgetpage(¶ms);
2498 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2499 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2500 page->audio.port[2].channels = 0;
2501 page->audio.port[3].channels = 0;
2502 error = cdsetmode(periph, ¶ms);
2503 free(params.mode_buf, M_SCSICD);
2504 cam_periph_unlock(periph);
2508 cam_periph_lock(periph);
2509 error = cdpause(periph, 1);
2510 cam_periph_unlock(periph);
2513 cam_periph_lock(periph);
2514 error = cdpause(periph, 0);
2515 cam_periph_unlock(periph);
2518 cam_periph_lock(periph);
2519 error = cdstartunit(periph, 0);
2520 cam_periph_unlock(periph);
2523 cam_periph_lock(periph);
2524 error = cdstartunit(periph, 1);
2525 cam_periph_unlock(periph);
2528 cam_periph_lock(periph);
2529 error = cdstopunit(periph, 0);
2530 cam_periph_unlock(periph);
2533 cam_periph_lock(periph);
2534 error = cdstopunit(periph, 1);
2535 cam_periph_unlock(periph);
2538 cam_periph_lock(periph);
2539 cdprevent(periph, PR_ALLOW);
2540 cam_periph_unlock(periph);
2543 cam_periph_lock(periph);
2544 cdprevent(periph, PR_PREVENT);
2545 cam_periph_unlock(periph);
2548 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2552 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2556 /* return (cd_reset(periph)); */
2559 case CDRIOCREADSPEED:
2560 cam_periph_lock(periph);
2561 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2562 cam_periph_unlock(periph);
2564 case CDRIOCWRITESPEED:
2565 cam_periph_lock(periph);
2566 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2567 cam_periph_unlock(periph);
2569 case CDRIOCGETBLOCKSIZE:
2570 *(int *)addr = softc->params.blksize;
2572 case CDRIOCSETBLOCKSIZE:
2573 if (*(int *)addr <= 0) {
2577 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2580 case DVDIOCREPORTKEY: {
2581 struct dvd_authinfo *authinfo;
2583 authinfo = (struct dvd_authinfo *)addr;
2585 if (cmd == DVDIOCREPORTKEY)
2586 error = cdreportkey(periph, authinfo);
2588 error = cdsendkey(periph, authinfo);
2591 case DVDIOCREADSTRUCTURE: {
2592 struct dvd_struct *dvdstruct;
2594 dvdstruct = (struct dvd_struct *)addr;
2596 error = cdreaddvdstructure(periph, dvdstruct);
2601 cam_periph_lock(periph);
2602 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2603 cam_periph_unlock(periph);
2607 cam_periph_lock(periph);
2608 cam_periph_unhold(periph);
2610 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2611 if (error && bootverbose) {
2612 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2614 cam_periph_unlock(periph);
2620 cdprevent(struct cam_periph *periph, int action)
2623 struct cd_softc *softc;
2626 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2628 softc = (struct cd_softc *)periph->softc;
2630 if (((action == PR_ALLOW)
2631 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2632 || ((action == PR_PREVENT)
2633 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2637 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2639 scsi_prevent(&ccb->csio,
2640 /*retries*/ cd_retry_count,
2645 /* timeout */60000);
2647 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2648 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2650 xpt_release_ccb(ccb);
2653 if (action == PR_ALLOW)
2654 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2656 softc->flags |= CD_FLAG_DISC_LOCKED;
2661 cdmediaprobedone(struct cam_periph *periph)
2663 struct cd_softc *softc;
2665 softc = (struct cd_softc *)periph->softc;
2667 softc->flags &= ~CD_FLAG_MEDIA_SCAN_ACT;
2669 if ((softc->flags & CD_FLAG_MEDIA_WAIT) != 0) {
2670 softc->flags &= ~CD_FLAG_MEDIA_WAIT;
2671 wakeup(&softc->toc);
2676 * XXX: the disk media and sector size is only really able to change
2677 * XXX: while the device is closed.
2681 cdcheckmedia(struct cam_periph *periph, int do_wait)
2683 struct cd_softc *softc;
2686 softc = (struct cd_softc *)periph->softc;
2690 && ((softc->flags & CD_FLAG_MEDIA_WAIT) == 0)) {
2691 softc->flags |= CD_FLAG_MEDIA_WAIT;
2693 if ((softc->flags & CD_FLAG_MEDIA_SCAN_ACT) == 0) {
2694 softc->state = CD_STATE_MEDIA_PREVENT;
2695 softc->flags |= CD_FLAG_MEDIA_SCAN_ACT;
2696 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2702 error = msleep(&softc->toc, cam_periph_mtx(periph), PRIBIO,"cdmedia",0);
2708 * Check to see whether we have a valid size from the media. We
2709 * may or may not have a valid TOC.
2711 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
2720 cdcheckmedia(struct cam_periph *periph)
2722 struct cd_softc *softc;
2723 struct ioc_toc_header *toch;
2724 struct cd_toc_single leadout;
2725 u_int32_t size, toclen;
2726 int error, num_entries, cdindex;
2728 softc = (struct cd_softc *)periph->softc;
2730 cdprevent(periph, PR_PREVENT);
2731 softc->disk->d_sectorsize = 2048;
2732 softc->disk->d_mediasize = 0;
2735 * Get the disc size and block size. If we can't get it, we don't
2736 * have media, most likely.
2738 if ((error = cdsize(periph, &size)) != 0) {
2739 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2740 cdprevent(periph, PR_ALLOW);
2743 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2744 softc->disk->d_sectorsize = softc->params.blksize;
2745 softc->disk->d_mediasize =
2746 (off_t)softc->params.blksize * softc->params.disksize;
2750 * Now we check the table of contents. This (currently) is only
2751 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2752 * things like present a separate entry in /dev for each track,
2753 * like that acd(4) driver does.
2755 bzero(&softc->toc, sizeof(softc->toc));
2756 toch = &softc->toc.header;
2758 * We will get errors here for media that doesn't have a table of
2759 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2760 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2761 * has not been recorded (no complete session) and the Format codes
2762 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2763 * with an INVALID FIELD IN CDB. Devices that are not capable of
2764 * reading an incomplete session on DDC/CD-R/RW media shall report
2765 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2767 * So this isn't fatal if we can't read the table of contents, it
2768 * just means that the user won't be able to issue the play tracks
2769 * ioctl, and likely lots of other stuff won't work either. They
2770 * need to burn the CD before we can do a whole lot with it. So
2771 * we don't print anything here if we get an error back.
2773 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2776 * Errors in reading the table of contents aren't fatal, we just
2777 * won't have a valid table of contents cached.
2781 bzero(&softc->toc, sizeof(softc->toc));
2785 if (softc->quirks & CD_Q_BCD_TRACKS) {
2786 toch->starting_track = bcd2bin(toch->starting_track);
2787 toch->ending_track = bcd2bin(toch->ending_track);
2790 /* Number of TOC entries, plus leadout */
2791 num_entries = (toch->ending_track - toch->starting_track) + 2;
2793 if (num_entries <= 0)
2796 toclen = num_entries * sizeof(struct cd_toc_entry);
2798 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2799 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2803 bzero(&softc->toc, sizeof(softc->toc));
2807 if (softc->quirks & CD_Q_BCD_TRACKS) {
2808 toch->starting_track = bcd2bin(toch->starting_track);
2809 toch->ending_track = bcd2bin(toch->ending_track);
2812 * XXX KDM is this necessary? Probably only if the drive doesn't
2813 * return leadout information with the table of contents.
2815 cdindex = toch->starting_track + num_entries -1;
2816 if (cdindex == toch->ending_track + 1) {
2817 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2818 (u_int8_t *)&leadout, sizeof(leadout),
2824 softc->toc.entries[cdindex - toch->starting_track] =
2827 if (softc->quirks & CD_Q_BCD_TRACKS) {
2828 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2829 softc->toc.entries[cdindex].track =
2830 bcd2bin(softc->toc.entries[cdindex].track);
2834 softc->flags |= CD_FLAG_VALID_TOC;
2836 /* If the first track is audio, correct sector size. */
2837 if ((softc->toc.entries[0].control & 4) == 0) {
2838 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2839 softc->disk->d_mediasize =
2840 (off_t)softc->params.blksize * softc->params.disksize;
2846 * We unconditionally (re)set the blocksize each time the
2847 * CD device is opened. This is because the CD can change,
2848 * and therefore the blocksize might change.
2849 * XXX problems here if some slice or partition is still
2850 * open with the old size?
2852 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2853 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2854 softc->disk->d_devstat->block_size = softc->params.blksize;
2860 cdsize(struct cam_periph *periph, u_int32_t *size)
2862 struct cd_softc *softc;
2864 struct scsi_read_capacity_data *rcap_buf;
2867 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2869 softc = (struct cd_softc *)periph->softc;
2871 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2873 /* XXX Should be M_WAITOK */
2874 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2875 M_SCSICD, M_NOWAIT | M_ZERO);
2876 if (rcap_buf == NULL)
2879 scsi_read_capacity(&ccb->csio,
2880 /*retries*/ cd_retry_count,
2885 /* timeout */20000);
2887 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2888 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2890 xpt_release_ccb(ccb);
2892 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2893 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2894 /* Make sure we got at least some block size. */
2895 if (error == 0 && softc->params.blksize == 0)
2898 * SCSI-3 mandates that the reported blocksize shall be 2048.
2899 * Older drives sometimes report funny values, trim it down to
2900 * 2048, or other parts of the kernel will get confused.
2902 * XXX we leave drives alone that might report 512 bytes, as
2903 * well as drives reporting more weird sizes like perhaps 4K.
2905 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2906 softc->params.blksize = 2048;
2908 free(rcap_buf, M_SCSICD);
2909 *size = softc->params.disksize;
2917 cd6byteworkaround(union ccb *ccb)
2920 struct cam_periph *periph;
2921 struct cd_softc *softc;
2922 struct cd_mode_params *params;
2925 periph = xpt_path_periph(ccb->ccb_h.path);
2926 softc = (struct cd_softc *)periph->softc;
2928 cdb = ccb->csio.cdb_io.cdb_bytes;
2930 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2931 || ((cdb[0] != MODE_SENSE_6)
2932 && (cdb[0] != MODE_SELECT_6)))
2936 * Because there is no convenient place to stash the overall
2937 * cd_mode_params structure pointer, we have to grab it like this.
2938 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2939 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2941 * XXX It would be nice if, at some point, we could increase the
2942 * number of available peripheral private pointers. Both pointers
2943 * are currently used in most every peripheral driver.
2947 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2948 if (params->mode_buf == ccb->csio.data_ptr) {
2955 * This shouldn't happen. All mode sense and mode select
2956 * operations in the cd(4) driver MUST go through cdgetmode() and
2960 xpt_print(periph->path,
2961 "mode buffer not found in mode queue!\n");
2965 params->cdb_size = 10;
2966 softc->minimum_command_size = 10;
2967 xpt_print(ccb->ccb_h.path,
2968 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2969 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2971 if (cdb[0] == MODE_SENSE_6) {
2972 struct scsi_mode_sense_10 ms10;
2973 struct scsi_mode_sense_6 *ms6;
2976 ms6 = (struct scsi_mode_sense_6 *)cdb;
2978 bzero(&ms10, sizeof(ms10));
2979 ms10.opcode = MODE_SENSE_10;
2980 ms10.byte2 = ms6->byte2;
2981 ms10.page = ms6->page;
2984 * 10 byte mode header, block descriptor,
2985 * sizeof(union cd_pages)
2987 len = sizeof(struct cd_mode_data_10);
2988 ccb->csio.dxfer_len = len;
2990 scsi_ulto2b(len, ms10.length);
2991 ms10.control = ms6->control;
2992 bcopy(&ms10, cdb, 10);
2993 ccb->csio.cdb_len = 10;
2995 struct scsi_mode_select_10 ms10;
2996 struct scsi_mode_select_6 *ms6;
2997 struct scsi_mode_header_6 *header6;
2998 struct scsi_mode_header_10 *header10;
2999 struct scsi_mode_page_header *page_header;
3000 int blk_desc_len, page_num, page_size, len;
3002 ms6 = (struct scsi_mode_select_6 *)cdb;
3004 bzero(&ms10, sizeof(ms10));
3005 ms10.opcode = MODE_SELECT_10;
3006 ms10.byte2 = ms6->byte2;
3008 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3009 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3011 page_header = find_mode_page_6(header6);
3012 page_num = page_header->page_code;
3014 blk_desc_len = header6->blk_desc_len;
3016 page_size = cdgetpagesize(page_num);
3018 if (page_size != (page_header->page_length +
3019 sizeof(*page_header)))
3020 page_size = page_header->page_length +
3021 sizeof(*page_header);
3023 len = sizeof(*header10) + blk_desc_len + page_size;
3025 len = min(params->alloc_len, len);
3028 * Since the 6 byte parameter header is shorter than the 10
3029 * byte parameter header, we need to copy the actual mode
3030 * page data, and the block descriptor, if any, so things wind
3031 * up in the right place. The regions will overlap, but
3032 * bcopy() does the right thing.
3034 bcopy(params->mode_buf + sizeof(*header6),
3035 params->mode_buf + sizeof(*header10),
3036 len - sizeof(*header10));
3038 /* Make sure these fields are set correctly. */
3039 scsi_ulto2b(0, header10->data_length);
3040 header10->medium_type = 0;
3041 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3043 ccb->csio.dxfer_len = len;
3045 scsi_ulto2b(len, ms10.length);
3046 ms10.control = ms6->control;
3047 bcopy(&ms10, cdb, 10);
3048 ccb->csio.cdb_len = 10;
3051 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3052 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3055 cam_release_devq(ccb->ccb_h.path,
3059 /*getcount_only*/0);
3066 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3068 struct cd_softc *softc;
3069 struct cam_periph *periph;
3070 int error, error_code, sense_key, asc, ascq;
3072 periph = xpt_path_periph(ccb->ccb_h.path);
3073 softc = (struct cd_softc *)periph->softc;
3078 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3079 * CDB comes back with this particular error, try transforming it
3080 * into the 10 byte version.
3082 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3083 error = cd6byteworkaround(ccb);
3084 } else if (scsi_extract_sense_ccb(ccb,
3085 &error_code, &sense_key, &asc, &ascq)) {
3086 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3087 error = cd6byteworkaround(ccb);
3088 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3089 asc == 0x28 && ascq == 0x00)
3090 disk_media_changed(softc->disk, M_NOWAIT);
3091 else if (sense_key == SSD_KEY_NOT_READY &&
3092 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3093 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3094 disk_media_gone(softc->disk, M_NOWAIT);
3098 if (error == ERESTART)
3103 * Until we have a better way of doing pack validation,
3104 * don't treat UAs as errors.
3106 sense_flags |= SF_RETRY_UA;
3108 if (softc->quirks & CD_Q_RETRY_BUSY)
3109 sense_flags |= SF_RETRY_BUSY;
3110 return (cam_periph_error(ccb, cam_flags, sense_flags));
3114 cdmediapoll(void *arg)
3116 struct cam_periph *periph = arg;
3117 struct cd_softc *softc = periph->softc;
3119 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3120 softc->outstanding_cmds == 0) {
3121 if (cam_periph_acquire(periph) == 0) {
3123 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3126 /* Queue us up again */
3127 if (cd_poll_period != 0)
3128 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3132 * Read table of contents
3135 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3136 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3139 struct ccb_scsiio *csio;
3146 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3151 /* retries */ cd_retry_count,
3153 /* tag_action */ MSG_SIMPLE_Q_TAG,
3154 /* byte1_flags */ (mode == CD_MSF_FORMAT) ? CD_MSF : 0,
3155 /* format */ SRTOC_FORMAT_TOC,
3157 /* data_ptr */ data,
3158 /* dxfer_len */ len,
3159 /* sense_len */ SSD_FULL_SIZE,
3160 /* timeout */ 50000);
3162 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3163 /*sense_flags*/SF_RETRY_UA | sense_flags);
3165 xpt_release_ccb(ccb);
3171 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3172 u_int32_t format, int track,
3173 struct cd_sub_channel_info *data, u_int32_t len)
3175 struct scsi_read_subchannel *scsi_cmd;
3176 struct ccb_scsiio *csio;
3182 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3187 /* retries */ cd_retry_count,
3189 /* flags */ CAM_DIR_IN,
3190 /* tag_action */ MSG_SIMPLE_Q_TAG,
3191 /* data_ptr */ (u_int8_t *)data,
3192 /* dxfer_len */ len,
3193 /* sense_len */ SSD_FULL_SIZE,
3194 sizeof(struct scsi_read_subchannel),
3195 /* timeout */ 50000);
3197 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3198 bzero (scsi_cmd, sizeof(*scsi_cmd));
3200 scsi_cmd->op_code = READ_SUBCHANNEL;
3201 if (mode == CD_MSF_FORMAT)
3202 scsi_cmd->byte1 |= CD_MSF;
3203 scsi_cmd->byte2 = SRS_SUBQ;
3204 scsi_cmd->subchan_format = format;
3205 scsi_cmd->track = track;
3206 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3207 scsi_cmd->control = 0;
3209 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3210 /*sense_flags*/SF_RETRY_UA);
3212 xpt_release_ccb(ccb);
3218 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3219 * routine. See comments in cd6byteworkaround() for details.
3222 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3225 struct ccb_scsiio *csio;
3226 struct cd_softc *softc;
3231 softc = (struct cd_softc *)periph->softc;
3233 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3237 data->cdb_size = softc->minimum_command_size;
3238 if (data->cdb_size < 10)
3239 param_len = sizeof(struct cd_mode_data);
3241 param_len = sizeof(struct cd_mode_data_10);
3243 /* Don't say we've got more room than we actually allocated */
3244 param_len = min(param_len, data->alloc_len);
3246 scsi_mode_sense_len(csio,
3247 /* retries */ cd_retry_count,
3249 /* tag_action */ MSG_SIMPLE_Q_TAG,
3251 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3253 /* param_buf */ data->mode_buf,
3254 /* param_len */ param_len,
3255 /* minimum_cmd_size */ softc->minimum_command_size,
3256 /* sense_len */ SSD_FULL_SIZE,
3257 /* timeout */ 50000);
3260 * It would be nice not to have to do this, but there's no
3261 * available pointer in the CCB that would allow us to stuff the
3262 * mode params structure in there and retrieve it in
3263 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3264 * lets the caller know what CDB size we ended up using, so they
3265 * can find the actual mode page offset.
3267 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3269 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3270 /*sense_flags*/SF_RETRY_UA);
3272 xpt_release_ccb(ccb);
3274 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3277 * This is a bit of belt-and-suspenders checking, but if we run
3278 * into a situation where the target sends back multiple block
3279 * descriptors, we might not have enough space in the buffer to
3280 * see the whole mode page. Better to return an error than
3281 * potentially access memory beyond our malloced region.
3286 if (data->cdb_size == 10) {
3287 struct scsi_mode_header_10 *hdr10;
3289 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3290 data_len = scsi_2btoul(hdr10->data_length);
3291 data_len += sizeof(hdr10->data_length);
3293 struct scsi_mode_header_6 *hdr6;
3295 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3296 data_len = hdr6->data_length;
3297 data_len += sizeof(hdr6->data_length);
3301 * Complain if there is more mode data available than we
3302 * allocated space for. This could potentially happen if
3303 * we miscalculated the page length for some reason, if the
3304 * drive returns multiple block descriptors, or if it sets
3305 * the data length incorrectly.
3307 if (data_len > data->alloc_len) {
3308 xpt_print(periph->path, "allocated modepage %d length "
3309 "%d < returned length %d\n", page, data->alloc_len,
3318 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3319 * routine. See comments in cd6byteworkaround() for details.
3322 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3324 struct ccb_scsiio *csio;
3325 struct cd_softc *softc;
3327 int cdb_size, param_len;
3330 softc = (struct cd_softc *)periph->softc;
3332 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3339 * If the data is formatted for the 10 byte version of the mode
3340 * select parameter list, we need to use the 10 byte CDB.
3341 * Otherwise, we use whatever the stored minimum command size.
3343 if (data->cdb_size == 10)
3344 cdb_size = data->cdb_size;
3346 cdb_size = softc->minimum_command_size;
3348 if (cdb_size >= 10) {
3349 struct scsi_mode_header_10 *mode_header;
3352 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3354 data_len = scsi_2btoul(mode_header->data_length);
3356 scsi_ulto2b(0, mode_header->data_length);
3358 * SONY drives do not allow a mode select with a medium_type
3359 * value that has just been returned by a mode sense; use a
3360 * medium_type of 0 (Default) instead.
3362 mode_header->medium_type = 0;
3365 * Pass back whatever the drive passed to us, plus the size
3366 * of the data length field.
3368 param_len = data_len + sizeof(mode_header->data_length);
3371 struct scsi_mode_header_6 *mode_header;
3373 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3375 param_len = mode_header->data_length + 1;
3377 mode_header->data_length = 0;
3379 * SONY drives do not allow a mode select with a medium_type
3380 * value that has just been returned by a mode sense; use a
3381 * medium_type of 0 (Default) instead.
3383 mode_header->medium_type = 0;
3386 /* Don't say we've got more room than we actually allocated */
3387 param_len = min(param_len, data->alloc_len);
3389 scsi_mode_select_len(csio,
3390 /* retries */ cd_retry_count,
3392 /* tag_action */ MSG_SIMPLE_Q_TAG,
3393 /* scsi_page_fmt */ 1,
3395 /* param_buf */ data->mode_buf,
3396 /* param_len */ param_len,
3397 /* minimum_cmd_size */ cdb_size,
3398 /* sense_len */ SSD_FULL_SIZE,
3399 /* timeout */ 50000);
3401 /* See comments in cdgetmode() and cd6byteworkaround(). */
3402 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3404 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3405 /*sense_flags*/SF_RETRY_UA);
3407 xpt_release_ccb(ccb);
3409 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3415 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3417 struct ccb_scsiio *csio;
3423 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3426 * Use the smallest possible command to perform the operation.
3428 if ((len & 0xffff0000) == 0) {
3430 * We can fit in a 10 byte cdb.
3432 struct scsi_play_10 *scsi_cmd;
3434 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3435 bzero (scsi_cmd, sizeof(*scsi_cmd));
3436 scsi_cmd->op_code = PLAY_10;
3437 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3438 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3439 cdb_len = sizeof(*scsi_cmd);
3441 struct scsi_play_12 *scsi_cmd;
3443 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3444 bzero (scsi_cmd, sizeof(*scsi_cmd));
3445 scsi_cmd->op_code = PLAY_12;
3446 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3447 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3448 cdb_len = sizeof(*scsi_cmd);
3451 /*retries*/ cd_retry_count,
3453 /*flags*/CAM_DIR_NONE,
3457 /*sense_len*/SSD_FULL_SIZE,
3459 /*timeout*/50 * 1000);
3461 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3462 /*sense_flags*/SF_RETRY_UA);
3464 xpt_release_ccb(ccb);
3470 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3471 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3473 struct scsi_play_msf *scsi_cmd;
3474 struct ccb_scsiio *csio;
3480 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3485 /* retries */ cd_retry_count,
3487 /* flags */ CAM_DIR_NONE,
3488 /* tag_action */ MSG_SIMPLE_Q_TAG,
3489 /* data_ptr */ NULL,
3491 /* sense_len */ SSD_FULL_SIZE,
3492 sizeof(struct scsi_play_msf),
3493 /* timeout */ 50000);
3495 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3496 bzero (scsi_cmd, sizeof(*scsi_cmd));
3498 scsi_cmd->op_code = PLAY_MSF;
3499 scsi_cmd->start_m = startm;
3500 scsi_cmd->start_s = starts;
3501 scsi_cmd->start_f = startf;
3502 scsi_cmd->end_m = endm;
3503 scsi_cmd->end_s = ends;
3504 scsi_cmd->end_f = endf;
3506 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3507 /*sense_flags*/SF_RETRY_UA);
3509 xpt_release_ccb(ccb);
3515 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3516 u_int32_t etrack, u_int32_t eindex)
3518 struct scsi_play_track *scsi_cmd;
3519 struct ccb_scsiio *csio;
3525 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3530 /* retries */ cd_retry_count,
3532 /* flags */ CAM_DIR_NONE,
3533 /* tag_action */ MSG_SIMPLE_Q_TAG,
3534 /* data_ptr */ NULL,
3536 /* sense_len */ SSD_FULL_SIZE,
3537 sizeof(struct scsi_play_track),
3538 /* timeout */ 50000);
3540 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3541 bzero (scsi_cmd, sizeof(*scsi_cmd));
3543 scsi_cmd->op_code = PLAY_TRACK;
3544 scsi_cmd->start_track = strack;
3545 scsi_cmd->start_index = sindex;
3546 scsi_cmd->end_track = etrack;
3547 scsi_cmd->end_index = eindex;
3549 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3550 /*sense_flags*/SF_RETRY_UA);
3552 xpt_release_ccb(ccb);
3558 cdpause(struct cam_periph *periph, u_int32_t go)
3560 struct scsi_pause *scsi_cmd;
3561 struct ccb_scsiio *csio;
3567 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3572 /* retries */ cd_retry_count,
3574 /* flags */ CAM_DIR_NONE,
3575 /* tag_action */ MSG_SIMPLE_Q_TAG,
3576 /* data_ptr */ NULL,
3578 /* sense_len */ SSD_FULL_SIZE,
3579 sizeof(struct scsi_pause),
3580 /* timeout */ 50000);
3582 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3583 bzero (scsi_cmd, sizeof(*scsi_cmd));
3585 scsi_cmd->op_code = PAUSE;
3586 scsi_cmd->resume = go;
3588 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3589 /*sense_flags*/SF_RETRY_UA);
3591 xpt_release_ccb(ccb);
3597 cdstartunit(struct cam_periph *periph, int load)
3604 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3606 scsi_start_stop(&ccb->csio,
3607 /* retries */ cd_retry_count,
3609 /* tag_action */ MSG_SIMPLE_Q_TAG,
3611 /* load_eject */ load,
3612 /* immediate */ FALSE,
3613 /* sense_len */ SSD_FULL_SIZE,
3614 /* timeout */ 50000);
3616 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3617 /*sense_flags*/SF_RETRY_UA);
3619 xpt_release_ccb(ccb);
3625 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3632 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3634 scsi_start_stop(&ccb->csio,
3635 /* retries */ cd_retry_count,
3637 /* tag_action */ MSG_SIMPLE_Q_TAG,
3639 /* load_eject */ eject,
3640 /* immediate */ FALSE,
3641 /* sense_len */ SSD_FULL_SIZE,
3642 /* timeout */ 50000);
3644 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3645 /*sense_flags*/SF_RETRY_UA);
3647 xpt_release_ccb(ccb);
3653 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3655 struct scsi_set_speed *scsi_cmd;
3656 struct ccb_scsiio *csio;
3661 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3664 /* Preserve old behavior: units in multiples of CDROM speed */
3671 /* retries */ cd_retry_count,
3673 /* flags */ CAM_DIR_NONE,
3674 /* tag_action */ MSG_SIMPLE_Q_TAG,
3675 /* data_ptr */ NULL,
3677 /* sense_len */ SSD_FULL_SIZE,
3678 sizeof(struct scsi_set_speed),
3679 /* timeout */ 50000);
3681 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3682 bzero(scsi_cmd, sizeof(*scsi_cmd));
3684 scsi_cmd->opcode = SET_CD_SPEED;
3685 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3686 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3688 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3689 /*sense_flags*/SF_RETRY_UA);
3691 xpt_release_ccb(ccb);
3697 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3709 switch (authinfo->format) {
3710 case DVD_REPORT_AGID:
3711 length = sizeof(struct scsi_report_key_data_agid);
3713 case DVD_REPORT_CHALLENGE:
3714 length = sizeof(struct scsi_report_key_data_challenge);
3716 case DVD_REPORT_KEY1:
3717 length = sizeof(struct scsi_report_key_data_key1_key2);
3719 case DVD_REPORT_TITLE_KEY:
3720 length = sizeof(struct scsi_report_key_data_title);
3721 /* The lba field is only set for the title key */
3722 lba = authinfo->lba;
3724 case DVD_REPORT_ASF:
3725 length = sizeof(struct scsi_report_key_data_asf);
3727 case DVD_REPORT_RPC:
3728 length = sizeof(struct scsi_report_key_data_rpc);
3730 case DVD_INVALIDATE_AGID:
3738 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3742 cam_periph_lock(periph);
3743 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3745 scsi_report_key(&ccb->csio,
3746 /* retries */ cd_retry_count,
3748 /* tag_action */ MSG_SIMPLE_Q_TAG,
3750 /* agid */ authinfo->agid,
3751 /* key_format */ authinfo->format,
3752 /* data_ptr */ databuf,
3753 /* dxfer_len */ length,
3754 /* sense_len */ SSD_FULL_SIZE,
3755 /* timeout */ 50000);
3757 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3758 /*sense_flags*/SF_RETRY_UA);
3763 if (ccb->csio.resid != 0) {
3764 xpt_print(periph->path, "warning, residual for report key "
3765 "command is %d\n", ccb->csio.resid);
3768 switch(authinfo->format) {
3769 case DVD_REPORT_AGID: {
3770 struct scsi_report_key_data_agid *agid_data;
3772 agid_data = (struct scsi_report_key_data_agid *)databuf;
3774 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3778 case DVD_REPORT_CHALLENGE: {
3779 struct scsi_report_key_data_challenge *chal_data;
3781 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3783 bcopy(chal_data->challenge_key, authinfo->keychal,
3784 min(sizeof(chal_data->challenge_key),
3785 sizeof(authinfo->keychal)));
3788 case DVD_REPORT_KEY1: {
3789 struct scsi_report_key_data_key1_key2 *key1_data;
3791 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3793 bcopy(key1_data->key1, authinfo->keychal,
3794 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3797 case DVD_REPORT_TITLE_KEY: {
3798 struct scsi_report_key_data_title *title_data;
3800 title_data = (struct scsi_report_key_data_title *)databuf;
3802 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3803 RKD_TITLE_CPM_SHIFT;
3804 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3805 RKD_TITLE_CP_SEC_SHIFT;
3806 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3807 RKD_TITLE_CMGS_SHIFT;
3808 bcopy(title_data->title_key, authinfo->keychal,
3809 min(sizeof(title_data->title_key),
3810 sizeof(authinfo->keychal)));
3813 case DVD_REPORT_ASF: {
3814 struct scsi_report_key_data_asf *asf_data;
3816 asf_data = (struct scsi_report_key_data_asf *)databuf;
3818 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3821 case DVD_REPORT_RPC: {
3822 struct scsi_report_key_data_rpc *rpc_data;
3824 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3826 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3828 authinfo->vend_rsts =
3829 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3830 RKD_RPC_VENDOR_RESET_SHIFT;
3831 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3832 authinfo->region = rpc_data->region_mask;
3833 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3836 case DVD_INVALIDATE_AGID:
3839 /* This should be impossible, since we checked above */
3842 break; /* NOTREACHED */
3846 xpt_release_ccb(ccb);
3847 cam_periph_unlock(periph);
3849 if (databuf != NULL)
3850 free(databuf, M_DEVBUF);
3856 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3866 switch(authinfo->format) {
3867 case DVD_SEND_CHALLENGE: {
3868 struct scsi_report_key_data_challenge *challenge_data;
3870 length = sizeof(*challenge_data);
3872 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3874 databuf = (u_int8_t *)challenge_data;
3876 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3877 challenge_data->data_len);
3879 bcopy(authinfo->keychal, challenge_data->challenge_key,
3880 min(sizeof(authinfo->keychal),
3881 sizeof(challenge_data->challenge_key)));
3884 case DVD_SEND_KEY2: {
3885 struct scsi_report_key_data_key1_key2 *key2_data;
3887 length = sizeof(*key2_data);
3889 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3891 databuf = (u_int8_t *)key2_data;
3893 scsi_ulto2b(length - sizeof(key2_data->data_len),
3894 key2_data->data_len);
3896 bcopy(authinfo->keychal, key2_data->key1,
3897 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3901 case DVD_SEND_RPC: {
3902 struct scsi_send_key_data_rpc *rpc_data;
3904 length = sizeof(*rpc_data);
3906 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3908 databuf = (u_int8_t *)rpc_data;
3910 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3911 rpc_data->data_len);
3913 rpc_data->region_code = authinfo->region;
3920 cam_periph_lock(periph);
3921 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3923 scsi_send_key(&ccb->csio,
3924 /* retries */ cd_retry_count,
3926 /* tag_action */ MSG_SIMPLE_Q_TAG,
3927 /* agid */ authinfo->agid,
3928 /* key_format */ authinfo->format,
3929 /* data_ptr */ databuf,
3930 /* dxfer_len */ length,
3931 /* sense_len */ SSD_FULL_SIZE,
3932 /* timeout */ 50000);
3934 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3935 /*sense_flags*/SF_RETRY_UA);
3937 xpt_release_ccb(ccb);
3938 cam_periph_unlock(periph);
3940 if (databuf != NULL)
3941 free(databuf, M_DEVBUF);
3947 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3957 /* The address is reserved for many of the formats */
3960 switch(dvdstruct->format) {
3961 case DVD_STRUCT_PHYSICAL:
3962 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3964 case DVD_STRUCT_COPYRIGHT:
3965 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3967 case DVD_STRUCT_DISCKEY:
3968 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3970 case DVD_STRUCT_BCA:
3971 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3973 case DVD_STRUCT_MANUFACT:
3974 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3976 case DVD_STRUCT_CMI:
3978 case DVD_STRUCT_PROTDISCID:
3979 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3981 case DVD_STRUCT_DISCKEYBLOCK:
3982 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3984 case DVD_STRUCT_DDS:
3985 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3987 case DVD_STRUCT_MEDIUM_STAT:
3988 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3990 case DVD_STRUCT_SPARE_AREA:
3991 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3993 case DVD_STRUCT_RMD_LAST:
3995 case DVD_STRUCT_RMD_RMA:
3997 case DVD_STRUCT_PRERECORDED:
3998 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4000 case DVD_STRUCT_UNIQUEID:
4001 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4003 case DVD_STRUCT_DCB:
4005 case DVD_STRUCT_LIST:
4007 * This is the maximum allocation length for the READ DVD
4008 * STRUCTURE command. There's nothing in the MMC3 spec
4009 * that indicates a limit in the amount of data that can
4010 * be returned from this call, other than the limits
4011 * imposed by the 2-byte length variables.
4020 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4024 cam_periph_lock(periph);
4025 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4027 scsi_read_dvd_structure(&ccb->csio,
4028 /* retries */ cd_retry_count,
4030 /* tag_action */ MSG_SIMPLE_Q_TAG,
4032 /* layer_number */ dvdstruct->layer_num,
4033 /* key_format */ dvdstruct->format,
4034 /* agid */ dvdstruct->agid,
4035 /* data_ptr */ databuf,
4036 /* dxfer_len */ length,
4037 /* sense_len */ SSD_FULL_SIZE,
4038 /* timeout */ 50000);
4040 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4041 /*sense_flags*/SF_RETRY_UA);
4046 switch(dvdstruct->format) {
4047 case DVD_STRUCT_PHYSICAL: {
4048 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4049 struct dvd_layer *outlayer;
4050 struct scsi_read_dvd_struct_data_physical *phys_data;
4053 (struct scsi_read_dvd_struct_data_physical *)databuf;
4054 inlayer = &phys_data->layer_desc;
4055 outlayer = (struct dvd_layer *)&dvdstruct->data;
4057 dvdstruct->length = sizeof(*inlayer);
4059 outlayer->book_type = (inlayer->book_type_version &
4060 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4061 outlayer->book_version = (inlayer->book_type_version &
4062 RDSD_BOOK_VERSION_MASK);
4063 outlayer->disc_size = (inlayer->disc_size_max_rate &
4064 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4065 outlayer->max_rate = (inlayer->disc_size_max_rate &
4066 RDSD_MAX_RATE_MASK);
4067 outlayer->nlayers = (inlayer->layer_info &
4068 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4069 outlayer->track_path = (inlayer->layer_info &
4070 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4071 outlayer->layer_type = (inlayer->layer_info &
4072 RDSD_LAYER_TYPE_MASK);
4073 outlayer->linear_density = (inlayer->density &
4074 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4075 outlayer->track_density = (inlayer->density &
4076 RDSD_TRACK_DENSITY_MASK);
4077 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4079 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4080 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4081 outlayer->end_sector_l0 =
4082 scsi_3btoul(inlayer->end_sector_layer0);
4085 case DVD_STRUCT_COPYRIGHT: {
4086 struct scsi_read_dvd_struct_data_copyright *copy_data;
4088 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4091 dvdstruct->cpst = copy_data->cps_type;
4092 dvdstruct->rmi = copy_data->region_info;
4093 dvdstruct->length = 0;
4099 * Tell the user what the overall length is, no matter
4100 * what we can actually fit in the data buffer.
4102 dvdstruct->length = length - ccb->csio.resid -
4103 sizeof(struct scsi_read_dvd_struct_data_header);
4106 * But only actually copy out the smaller of what we read
4107 * in or what the structure can take.
4109 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4111 min(sizeof(dvdstruct->data), dvdstruct->length));
4116 xpt_release_ccb(ccb);
4117 cam_periph_unlock(periph);
4119 if (databuf != NULL)
4120 free(databuf, M_DEVBUF);
4126 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4127 void (*cbfcnp)(struct cam_periph *, union ccb *),
4128 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4129 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4130 u_int8_t sense_len, u_int32_t timeout)
4132 struct scsi_report_key *scsi_cmd;
4134 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4135 bzero(scsi_cmd, sizeof(*scsi_cmd));
4136 scsi_cmd->opcode = REPORT_KEY;
4137 scsi_ulto4b(lba, scsi_cmd->lba);
4138 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4139 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4140 (key_format & RK_KF_KEYFORMAT_MASK);
4145 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4147 /*data_ptr*/ data_ptr,
4148 /*dxfer_len*/ dxfer_len,
4155 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4156 void (*cbfcnp)(struct cam_periph *, union ccb *),
4157 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4158 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4161 struct scsi_send_key *scsi_cmd;
4163 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4164 bzero(scsi_cmd, sizeof(*scsi_cmd));
4165 scsi_cmd->opcode = SEND_KEY;
4167 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4168 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4169 (key_format & RK_KF_KEYFORMAT_MASK);
4174 /*flags*/ CAM_DIR_OUT,
4176 /*data_ptr*/ data_ptr,
4177 /*dxfer_len*/ dxfer_len,
4184 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4185 void (*cbfcnp)(struct cam_periph *, union ccb *),
4186 u_int8_t tag_action, u_int32_t address,
4187 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4188 u_int8_t *data_ptr, u_int32_t dxfer_len,
4189 u_int8_t sense_len, u_int32_t timeout)
4191 struct scsi_read_dvd_structure *scsi_cmd;
4193 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4194 bzero(scsi_cmd, sizeof(*scsi_cmd));
4195 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4197 scsi_ulto4b(address, scsi_cmd->address);
4198 scsi_cmd->layer_number = layer_number;
4199 scsi_cmd->format = format;
4200 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4201 /* The AGID is the top two bits of this byte */
4202 scsi_cmd->agid = agid << 6;
4207 /*flags*/ CAM_DIR_IN,
4209 /*data_ptr*/ data_ptr,
4210 /*dxfer_len*/ dxfer_len,
4217 scsi_read_toc(struct ccb_scsiio *csio, uint32_t retries,
4218 void (*cbfcnp)(struct cam_periph *, union ccb *),
4219 uint8_t tag_action, uint8_t byte1_flags, uint8_t format,
4220 uint8_t track, uint8_t *data_ptr, uint32_t dxfer_len,
4221 int sense_len, int timeout)
4223 struct scsi_read_toc *scsi_cmd;
4225 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
4226 bzero(scsi_cmd, sizeof(*scsi_cmd));
4227 scsi_cmd->op_code = READ_TOC;
4230 * The structure is counting from 1, the function counting from 0.
4231 * The spec counts from 0. In MMC-6, there is only one flag, the
4232 * MSF flag. But we put the whole byte in for a bit a future-proofing.
4234 scsi_cmd->byte2 = byte1_flags;
4235 scsi_cmd->format = format;
4236 scsi_cmd->from_track = track;
4237 scsi_ulto2b(dxfer_len, scsi_cmd->data_len);
4240 /* retries */ retries,
4241 /* cbfcnp */ cbfcnp,
4242 /* flags */ CAM_DIR_IN,
4243 /* tag_action */ tag_action,
4244 /* data_ptr */ data_ptr,
4245 /* dxfer_len */ dxfer_len,
4246 /* sense_len */ sense_len,
4248 /* timeout */ timeout);