2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
65 #include <sys/sysctl.h>
66 #include <sys/taskqueue.h>
67 #include <geom/geom_disk.h>
70 #include <cam/cam_ccb.h>
71 #include <cam/cam_periph.h>
72 #include <cam/cam_xpt_periph.h>
73 #include <cam/cam_queue.h>
74 #include <cam/cam_sim.h>
76 #include <cam/scsi/scsi_message.h>
77 #include <cam/scsi/scsi_da.h>
78 #include <cam/scsi/scsi_cd.h>
80 #define LEADOUT 0xaa /* leadout toc entry */
90 CD_Q_BCD_TRACKS = 0x02,
91 CD_Q_10_BYTE_ONLY = 0x10,
92 CD_Q_RETRY_BUSY = 0x40
95 #define CD_Q_BIT_STRING \
103 CD_FLAG_INVALID = 0x0001,
104 CD_FLAG_NEW_DISC = 0x0002,
105 CD_FLAG_DISC_LOCKED = 0x0004,
106 CD_FLAG_DISC_REMOVABLE = 0x0008,
107 CD_FLAG_SAW_MEDIA = 0x0010,
108 CD_FLAG_ACTIVE = 0x0080,
109 CD_FLAG_SCHED_ON_COMP = 0x0100,
110 CD_FLAG_RETRY_UA = 0x0200,
111 CD_FLAG_VALID_MEDIA = 0x0400,
112 CD_FLAG_VALID_TOC = 0x0800,
113 CD_FLAG_SCTX_INIT = 0x1000
118 CD_CCB_BUFFER_IO = 0x02,
120 CD_CCB_TYPE_MASK = 0x0F,
121 CD_CCB_RETRY_UA = 0x10
124 #define ccb_state ppriv_field0
125 #define ccb_bp ppriv_ptr1
128 struct ioc_toc_header header;
129 struct cd_toc_entry entries[100];
132 struct cd_toc_single {
133 struct ioc_toc_header header;
134 struct cd_toc_entry entry;
145 volatile cd_flags flags;
146 struct bio_queue_head bio_queue;
147 LIST_HEAD(, ccb_hdr) pending_ccbs;
148 struct cd_params params;
151 struct cam_periph *periph;
152 int minimum_command_size;
153 int outstanding_cmds;
155 struct task sysctl_task;
156 struct sysctl_ctx_list sysctl_ctx;
157 struct sysctl_oid *sysctl_tree;
158 STAILQ_HEAD(, cd_mode_params) mode_queue;
159 struct cd_tocdata toc;
161 struct callout mediapoll_c;
163 #define CD_ANNOUNCETMP_SZ 120
164 char announce_temp[CD_ANNOUNCETMP_SZ];
165 #define CD_ANNOUNCE_SZ 400
166 char announce_buf[CD_ANNOUNCE_SZ];
169 struct cd_page_sizes {
174 static struct cd_page_sizes cd_page_size_table[] =
176 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
179 struct cd_quirk_entry {
180 struct scsi_inquiry_pattern inq_pat;
185 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
186 * your device hangs when it gets a 10 byte command. Adding a quirk just
187 * to get rid of the informative diagnostic message is not acceptable. All
188 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
189 * referenced in a comment along with the quirk) , and must be approved by
190 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
191 * be removed until the submitter can explain why they are needed.
192 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
193 * when the CAM_NEW_TRAN_CODE work is done.
195 static struct cd_quirk_entry cd_quirk_table[] =
198 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
199 /* quirks */ CD_Q_BCD_TRACKS
203 * VMware returns BUSY status when storage has transient
204 * connectivity problems, so better wait.
206 {T_CDROM, SIP_MEDIA_REMOVABLE, "NECVMWar", "VMware IDE CDR10", "*"},
207 /*quirks*/ CD_Q_RETRY_BUSY
211 static disk_open_t cdopen;
212 static disk_close_t cdclose;
213 static disk_ioctl_t cdioctl;
214 static disk_strategy_t cdstrategy;
216 static periph_init_t cdinit;
217 static periph_ctor_t cdregister;
218 static periph_dtor_t cdcleanup;
219 static periph_start_t cdstart;
220 static periph_oninv_t cdoninvalidate;
221 static void cdasync(void *callback_arg, u_int32_t code,
222 struct cam_path *path, void *arg);
223 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
224 static int cdrunccb(union ccb *ccb,
225 int (*error_routine)(union ccb *ccb,
227 u_int32_t sense_flags),
228 u_int32_t cam_flags, u_int32_t sense_flags);
229 static void cddone(struct cam_periph *periph,
230 union ccb *start_ccb);
231 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
232 static int cdgetpagesize(int page_num);
233 static void cdprevent(struct cam_periph *periph, int action);
234 static int cdcheckmedia(struct cam_periph *periph);
235 static int cdsize(struct cam_periph *periph, u_int32_t *size);
236 static int cd6byteworkaround(union ccb *ccb);
237 static int cderror(union ccb *ccb, u_int32_t cam_flags,
238 u_int32_t sense_flags);
239 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
240 u_int32_t start, u_int8_t *data,
241 u_int32_t len, u_int32_t sense_flags);
242 static int cdgetmode(struct cam_periph *periph,
243 struct cd_mode_params *data, u_int32_t page);
244 static int cdsetmode(struct cam_periph *periph,
245 struct cd_mode_params *data);
246 static int cdplay(struct cam_periph *periph, u_int32_t blk,
248 static int cdreadsubchannel(struct cam_periph *periph,
249 u_int32_t mode, u_int32_t format,
251 struct cd_sub_channel_info *data,
253 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
254 u_int32_t starts, u_int32_t startf,
255 u_int32_t endm, u_int32_t ends,
257 static int cdplaytracks(struct cam_periph *periph,
258 u_int32_t strack, u_int32_t sindex,
259 u_int32_t etrack, u_int32_t eindex);
260 static int cdpause(struct cam_periph *periph, u_int32_t go);
261 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
262 static int cdstartunit(struct cam_periph *periph, int load);
263 static int cdsetspeed(struct cam_periph *periph,
264 u_int32_t rdspeed, u_int32_t wrspeed);
265 static int cdreportkey(struct cam_periph *periph,
266 struct dvd_authinfo *authinfo);
267 static int cdsendkey(struct cam_periph *periph,
268 struct dvd_authinfo *authinfo);
269 static int cdreaddvdstructure(struct cam_periph *periph,
270 struct dvd_struct *dvdstruct);
271 static timeout_t cdmediapoll;
273 static struct periph_driver cddriver =
276 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
279 PERIPHDRIVER_DECLARE(cd, cddriver);
281 #ifndef CD_DEFAULT_POLL_PERIOD
282 #define CD_DEFAULT_POLL_PERIOD 3
284 #ifndef CD_DEFAULT_RETRY
285 #define CD_DEFAULT_RETRY 4
287 #ifndef CD_DEFAULT_TIMEOUT
288 #define CD_DEFAULT_TIMEOUT 30000
291 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
292 static int cd_retry_count = CD_DEFAULT_RETRY;
293 static int cd_timeout = CD_DEFAULT_TIMEOUT;
295 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
296 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RWTUN,
297 &cd_poll_period, 0, "Media polling period in seconds");
298 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RWTUN,
299 &cd_retry_count, 0, "Normal I/O retry count");
300 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RWTUN,
301 &cd_timeout, 0, "Timeout, in us, for read operations");
303 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
311 * Install a global async callback. This callback will
312 * receive async callbacks like "new device found".
314 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
316 if (status != CAM_REQ_CMP) {
317 printf("cd: Failed to attach master async callback "
318 "due to status 0x%x!\n", status);
323 * Callback from GEOM, called when it has finished cleaning up its
327 cddiskgonecb(struct disk *dp)
329 struct cam_periph *periph;
331 periph = (struct cam_periph *)dp->d_drv1;
332 cam_periph_release(periph);
336 cdoninvalidate(struct cam_periph *periph)
338 struct cd_softc *softc;
340 softc = (struct cd_softc *)periph->softc;
343 * De-register any async callbacks.
345 xpt_register_async(0, cdasync, periph, periph->path);
347 softc->flags |= CD_FLAG_INVALID;
350 * Return all queued I/O with ENXIO.
351 * XXX Handle any transactions queued to the card
352 * with XPT_ABORT_CCB.
354 bioq_flush(&softc->bio_queue, NULL, ENXIO);
356 disk_gone(softc->disk);
360 cdcleanup(struct cam_periph *periph)
362 struct cd_softc *softc;
364 softc = (struct cd_softc *)periph->softc;
366 cam_periph_unlock(periph);
367 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
368 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
369 xpt_print(periph->path, "can't remove sysctl context\n");
372 callout_drain(&softc->mediapoll_c);
373 disk_destroy(softc->disk);
374 free(softc, M_DEVBUF);
375 cam_periph_lock(periph);
379 cdasync(void *callback_arg, u_int32_t code,
380 struct cam_path *path, void *arg)
382 struct cam_periph *periph;
383 struct cd_softc *softc;
385 periph = (struct cam_periph *)callback_arg;
387 case AC_FOUND_DEVICE:
389 struct ccb_getdev *cgd;
392 cgd = (struct ccb_getdev *)arg;
396 if (cgd->protocol != PROTO_SCSI)
398 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
400 if (SID_TYPE(&cgd->inq_data) != T_CDROM
401 && SID_TYPE(&cgd->inq_data) != T_WORM)
405 * Allocate a peripheral instance for
406 * this device and start the probe
409 status = cam_periph_alloc(cdregister, cdoninvalidate,
411 "cd", CAM_PERIPH_BIO,
413 AC_FOUND_DEVICE, cgd);
415 if (status != CAM_REQ_CMP
416 && status != CAM_REQ_INPROG)
417 printf("cdasync: Unable to attach new device "
418 "due to status 0x%x\n", status);
422 case AC_UNIT_ATTENTION:
425 int error_code, sense_key, asc, ascq;
427 softc = (struct cd_softc *)periph->softc;
428 ccb = (union ccb *)arg;
431 * Handle all media change UNIT ATTENTIONs except
432 * our own, as they will be handled by cderror().
434 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
435 scsi_extract_sense_ccb(ccb,
436 &error_code, &sense_key, &asc, &ascq)) {
437 if (asc == 0x28 && ascq == 0x00)
438 disk_media_changed(softc->disk, M_NOWAIT);
440 cam_periph_async(periph, code, path, arg);
444 softc = (struct cd_softc *)periph->softc;
445 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
446 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
448 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
455 struct ccb_hdr *ccbh;
457 softc = (struct cd_softc *)periph->softc;
459 * Don't fail on the expected unit attention
462 softc->flags |= CD_FLAG_RETRY_UA;
463 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
464 ccbh->ccb_state |= CD_CCB_RETRY_UA;
468 cam_periph_async(periph, code, path, arg);
474 cdsysctlinit(void *context, int pending)
476 struct cam_periph *periph;
477 struct cd_softc *softc;
478 char tmpstr[80], tmpstr2[80];
480 periph = (struct cam_periph *)context;
481 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
484 softc = (struct cd_softc *)periph->softc;
485 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
486 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
488 sysctl_ctx_init(&softc->sysctl_ctx);
489 softc->flags |= CD_FLAG_SCTX_INIT;
490 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
491 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
492 tmpstr2, CTLFLAG_RD, 0, tmpstr, "device_index");
494 if (softc->sysctl_tree == NULL) {
495 printf("cdsysctlinit: unable to allocate sysctl tree\n");
496 cam_periph_release(periph);
501 * Now register the sysctl handler, so the user can the value on
504 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
505 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
506 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
509 cam_periph_release(periph);
513 * We have a handler function for this so we can check the values when the
514 * user sets them, instead of every time we look at them.
517 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
521 value = *(int *)arg1;
523 error = sysctl_handle_int(oidp, &value, 0, req);
526 || (req->newptr == NULL))
530 * The only real values we can have here are 6 or 10. I don't
531 * really forsee having 12 be an option at any time in the future.
532 * So if the user sets something less than or equal to 6, we'll set
533 * it to 6. If he sets something greater than 6, we'll set it to 10.
535 * I suppose we could just return an error here for the wrong values,
536 * but I don't think it's necessary to do so, as long as we can
537 * determine the user's intent without too much trouble.
544 *(int *)arg1 = value;
550 cdregister(struct cam_periph *periph, void *arg)
552 struct cd_softc *softc;
553 struct ccb_pathinq cpi;
554 struct ccb_getdev *cgd;
558 cgd = (struct ccb_getdev *)arg;
560 printf("cdregister: no getdev CCB, can't register device\n");
561 return(CAM_REQ_CMP_ERR);
564 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
567 printf("cdregister: Unable to probe new device. "
568 "Unable to allocate softc\n");
569 return(CAM_REQ_CMP_ERR);
572 LIST_INIT(&softc->pending_ccbs);
573 STAILQ_INIT(&softc->mode_queue);
574 softc->state = CD_STATE_PROBE;
575 bioq_init(&softc->bio_queue);
576 if (SID_IS_REMOVABLE(&cgd->inq_data))
577 softc->flags |= CD_FLAG_DISC_REMOVABLE;
579 periph->softc = softc;
580 softc->periph = periph;
583 * See if this device has any quirks.
585 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
586 (caddr_t)cd_quirk_table,
587 nitems(cd_quirk_table),
588 sizeof(*cd_quirk_table), scsi_inquiry_match);
591 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
593 softc->quirks = CD_Q_NONE;
595 /* Check if the SIM does not want 6 byte commands */
596 bzero(&cpi, sizeof(cpi));
597 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
598 cpi.ccb_h.func_code = XPT_PATH_INQ;
599 xpt_action((union ccb *)&cpi);
600 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
601 softc->quirks |= CD_Q_10_BYTE_ONLY;
603 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
605 /* The default is 6 byte commands, unless quirked otherwise */
606 if (softc->quirks & CD_Q_10_BYTE_ONLY)
607 softc->minimum_command_size = 10;
609 softc->minimum_command_size = 6;
612 * Refcount and block open attempts until we are setup
615 (void)cam_periph_hold(periph, PRIBIO);
616 cam_periph_unlock(periph);
618 * Load the user's default, if any.
620 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
621 periph->unit_number);
622 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
624 /* 6 and 10 are the only permissible values here. */
625 if (softc->minimum_command_size < 6)
626 softc->minimum_command_size = 6;
627 else if (softc->minimum_command_size > 6)
628 softc->minimum_command_size = 10;
631 * We need to register the statistics structure for this device,
632 * but we don't have the blocksize yet for it. So, we register
633 * the structure and indicate that we don't have the blocksize
634 * yet. Unlike other SCSI peripheral drivers, we explicitly set
635 * the device type here to be CDROM, rather than just ORing in
636 * the device type. This is because this driver can attach to either
637 * CDROM or WORM devices, and we want this peripheral driver to
638 * show up in the devstat list as a CD peripheral driver, not a
639 * WORM peripheral driver. WORM drives will also have the WORM
640 * driver attached to them.
642 softc->disk = disk_alloc();
643 softc->disk->d_devstat = devstat_new_entry("cd",
644 periph->unit_number, 0,
645 DEVSTAT_BS_UNAVAILABLE,
647 XPORT_DEVSTAT_TYPE(cpi.transport),
648 DEVSTAT_PRIORITY_CD);
649 softc->disk->d_open = cdopen;
650 softc->disk->d_close = cdclose;
651 softc->disk->d_strategy = cdstrategy;
652 softc->disk->d_gone = cddiskgonecb;
653 softc->disk->d_ioctl = cdioctl;
654 softc->disk->d_name = "cd";
655 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
656 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
657 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
658 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
659 cgd->inq_data.product, sizeof(cgd->inq_data.product),
660 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
661 softc->disk->d_unit = periph->unit_number;
662 softc->disk->d_drv1 = periph;
664 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
665 else if (cpi.maxio > MAXPHYS)
666 softc->disk->d_maxsize = MAXPHYS; /* for safety */
668 softc->disk->d_maxsize = cpi.maxio;
669 softc->disk->d_flags = 0;
670 softc->disk->d_hba_vendor = cpi.hba_vendor;
671 softc->disk->d_hba_device = cpi.hba_device;
672 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
673 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
676 * Acquire a reference to the periph before we register with GEOM.
677 * We'll release this reference once GEOM calls us back (via
678 * dadiskgonecb()) telling us that our provider has been freed.
680 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
681 xpt_print(periph->path, "%s: lost periph during "
682 "registration!\n", __func__);
683 cam_periph_lock(periph);
684 return (CAM_REQ_CMP_ERR);
687 disk_create(softc->disk, DISK_VERSION);
688 cam_periph_lock(periph);
691 * Add an async callback so that we get
692 * notified if this device goes away.
694 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
695 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
698 * Schedule a periodic media polling events.
700 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
701 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
702 (cgd->inq_flags & SID_AEN) == 0 &&
704 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
705 cdmediapoll, periph);
707 xpt_schedule(periph, CAM_PRIORITY_DEV);
712 cdopen(struct disk *dp)
714 struct cam_periph *periph;
715 struct cd_softc *softc;
718 periph = (struct cam_periph *)dp->d_drv1;
719 softc = (struct cd_softc *)periph->softc;
721 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
724 cam_periph_lock(periph);
726 if (softc->flags & CD_FLAG_INVALID) {
727 cam_periph_release_locked(periph);
728 cam_periph_unlock(periph);
732 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
733 cam_periph_release_locked(periph);
734 cam_periph_unlock(periph);
738 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
742 * Check for media, and set the appropriate flags. We don't bail
743 * if we don't have media, but then we don't allow anything but the
744 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
746 cdcheckmedia(periph);
748 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
749 cam_periph_unhold(periph);
751 cam_periph_unlock(periph);
757 cdclose(struct disk *dp)
759 struct cam_periph *periph;
760 struct cd_softc *softc;
762 periph = (struct cam_periph *)dp->d_drv1;
763 softc = (struct cd_softc *)periph->softc;
765 cam_periph_lock(periph);
766 if (cam_periph_hold(periph, PRIBIO) != 0) {
767 cam_periph_unlock(periph);
768 cam_periph_release(periph);
772 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
775 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
776 cdprevent(periph, PR_ALLOW);
779 * Since we're closing this CD, mark the blocksize as unavailable.
780 * It will be marked as available when the CD is opened again.
782 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
785 * We'll check the media and toc again at the next open().
787 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
789 cam_periph_unhold(periph);
790 cam_periph_release_locked(periph);
791 cam_periph_unlock(periph);
797 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
799 u_int32_t sense_flags),
800 u_int32_t cam_flags, u_int32_t sense_flags)
802 struct cd_softc *softc;
803 struct cam_periph *periph;
806 periph = xpt_path_periph(ccb->ccb_h.path);
807 softc = (struct cd_softc *)periph->softc;
809 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
810 softc->disk->d_devstat);
816 * Actually translate the requested transfer into one the physical driver
817 * can understand. The transfer is described by a buf and will include
818 * only one physical transfer.
821 cdstrategy(struct bio *bp)
823 struct cam_periph *periph;
824 struct cd_softc *softc;
826 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
827 cam_periph_lock(periph);
828 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
829 ("cdstrategy(%p)\n", bp));
831 softc = (struct cd_softc *)periph->softc;
834 * If the device has been made invalid, error out
836 if ((softc->flags & CD_FLAG_INVALID)) {
837 cam_periph_unlock(periph);
838 biofinish(bp, NULL, ENXIO);
843 * If we don't have valid media, look for it before trying to
846 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
849 error = cdcheckmedia(periph);
851 cam_periph_unlock(periph);
852 biofinish(bp, NULL, error);
858 * Place it in the queue of disk activities for this disk
860 bioq_disksort(&softc->bio_queue, bp);
862 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
864 cam_periph_unlock(periph);
869 cdstart(struct cam_periph *periph, union ccb *start_ccb)
871 struct cd_softc *softc;
873 struct ccb_scsiio *csio;
874 struct scsi_read_capacity_data *rcap;
876 softc = (struct cd_softc *)periph->softc;
878 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
880 switch (softc->state) {
881 case CD_STATE_NORMAL:
883 bp = bioq_first(&softc->bio_queue);
887 csio = &start_ccb->csio;
888 scsi_test_unit_ready(csio,
889 /*retries*/ cd_retry_count,
894 start_ccb->ccb_h.ccb_bp = NULL;
895 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
896 xpt_action(start_ccb);
898 xpt_release_ccb(start_ccb);
902 cam_periph_release_locked(periph);
904 bioq_remove(&softc->bio_queue, bp);
906 scsi_read_write(&start_ccb->csio,
907 /*retries*/ cd_retry_count,
910 /* read */bp->bio_cmd == BIO_READ ?
911 SCSI_RW_READ : SCSI_RW_WRITE,
913 /* minimum_cmd_size */ 10,
914 /* lba */ bp->bio_offset /
915 softc->params.blksize,
916 bp->bio_bcount / softc->params.blksize,
917 /* data_ptr */ bp->bio_data,
918 /* dxfer_len */ bp->bio_bcount,
919 /* sense_len */ cd_retry_count ?
920 SSD_FULL_SIZE : SF_NO_PRINT,
921 /* timeout */ cd_timeout);
922 /* Use READ CD command for audio tracks. */
923 if (softc->params.blksize == 2352) {
924 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
925 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
926 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
927 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
928 start_ccb->csio.cdb_len = 12;
930 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
933 LIST_INSERT_HEAD(&softc->pending_ccbs,
934 &start_ccb->ccb_h, periph_links.le);
935 softc->outstanding_cmds++;
937 /* We expect a unit attention from this device */
938 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
939 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
940 softc->flags &= ~CD_FLAG_RETRY_UA;
943 start_ccb->ccb_h.ccb_bp = bp;
944 bp = bioq_first(&softc->bio_queue);
946 xpt_action(start_ccb);
948 if (bp != NULL || softc->tur) {
949 /* Have more work to do, so ensure we stay scheduled */
950 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
957 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
958 M_SCSICD, M_NOWAIT | M_ZERO);
960 xpt_print(periph->path,
961 "cdstart: Couldn't malloc read_capacity data\n");
962 /* cd_free_periph??? */
965 csio = &start_ccb->csio;
966 scsi_read_capacity(csio,
967 /*retries*/ cd_retry_count,
973 start_ccb->ccb_h.ccb_bp = NULL;
974 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
975 xpt_action(start_ccb);
982 cddone(struct cam_periph *periph, union ccb *done_ccb)
984 struct cd_softc *softc;
985 struct ccb_scsiio *csio;
987 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
989 softc = (struct cd_softc *)periph->softc;
990 csio = &done_ccb->csio;
992 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
993 case CD_CCB_BUFFER_IO:
998 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1001 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1004 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1009 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1010 if (error == ERESTART) {
1012 * A retry was scheuled, so
1020 xpt_print(periph->path,
1021 "cddone: got error %#x back\n", error);
1022 bioq_flush(&softc->bio_queue, NULL, EIO);
1023 bp->bio_resid = bp->bio_bcount;
1024 bp->bio_error = error;
1025 bp->bio_flags |= BIO_ERROR;
1026 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1027 cam_release_devq(done_ccb->ccb_h.path,
1031 /*getcount_only*/0);
1034 bp->bio_resid = csio->resid;
1036 if (bp->bio_resid != 0) {
1038 * Short transfer ???
1039 * XXX: not sure this is correct for partial
1042 bp->bio_flags |= BIO_ERROR;
1046 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1047 softc->outstanding_cmds--;
1049 biofinish(bp, NULL, 0);
1054 struct scsi_read_capacity_data *rdcap;
1056 struct cd_params *cdp;
1059 cdp = &softc->params;
1060 announce_buf = softc->announce_temp;
1062 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1064 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1065 cdp->blksize = scsi_4btoul (rdcap->length);
1068 * Retry any UNIT ATTENTION type errors. They
1069 * are expected at boot.
1071 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1072 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1073 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1074 snprintf(announce_buf, CD_ANNOUNCETMP_SZ,
1075 "%juMB (%ju %u byte sectors)",
1076 ((uintmax_t)cdp->disksize * cdp->blksize) /
1078 (uintmax_t)cdp->disksize, cdp->blksize);
1080 if (error == ERESTART) {
1082 * A retry was scheuled, so
1088 int sense_key, error_code;
1091 struct ccb_getdev cgd;
1093 /* Don't wedge this device's queue */
1094 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1095 cam_release_devq(done_ccb->ccb_h.path,
1099 /*getcount_only*/0);
1101 status = done_ccb->ccb_h.status;
1103 xpt_setup_ccb(&cgd.ccb_h,
1104 done_ccb->ccb_h.path,
1105 CAM_PRIORITY_NORMAL);
1106 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1107 xpt_action((union ccb *)&cgd);
1109 if (scsi_extract_sense_ccb(done_ccb,
1110 &error_code, &sense_key, &asc, &ascq))
1116 * Attach to anything that claims to be a
1117 * CDROM or WORM device, as long as it
1118 * doesn't return a "Logical unit not
1119 * supported" (0x25) error.
1121 if ((have_sense) && (asc != 0x25)
1122 && (error_code == SSD_CURRENT_ERROR)) {
1123 const char *sense_key_desc;
1124 const char *asc_desc;
1126 scsi_sense_desc(sense_key, asc, ascq,
1130 snprintf(announce_buf,
1131 sizeof(announce_buf),
1132 "Attempt to query device "
1133 "size failed: %s, %s",
1136 } else if ((have_sense == 0)
1137 && ((status & CAM_STATUS_MASK) ==
1138 CAM_SCSI_STATUS_ERROR)
1139 && (csio->scsi_status ==
1140 SCSI_STATUS_BUSY)) {
1141 snprintf(announce_buf,
1142 sizeof(announce_buf),
1143 "Attempt to query device "
1144 "size failed: SCSI Status: %s",
1145 scsi_status_string(csio));
1146 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1148 * We only print out an error for
1149 * CDROM type devices. For WORM
1150 * devices, we don't print out an
1151 * error since a few WORM devices
1152 * don't support CDROM commands.
1153 * If we have sense information, go
1154 * ahead and print it out.
1155 * Otherwise, just say that we
1160 * Just print out the error, not
1161 * the full probe message, when we
1168 xpt_print(periph->path,
1169 "got CAM status %#x\n",
1170 done_ccb->ccb_h.status);
1172 xpt_print(periph->path, "fatal error, "
1173 "failed to attach to device\n");
1175 * Invalidate this peripheral.
1177 cam_periph_invalidate(periph);
1179 announce_buf = NULL;
1183 * Invalidate this peripheral.
1185 cam_periph_invalidate(periph);
1186 announce_buf = NULL;
1190 free(rdcap, M_SCSICD);
1191 if (announce_buf != NULL) {
1194 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ,
1196 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1197 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
1203 * Create our sysctl variables, now that we know
1204 * we have successfully attached.
1206 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1208 softc->state = CD_STATE_NORMAL;
1210 * Since our peripheral may be invalidated by an error
1211 * above or an external event, we must release our CCB
1212 * before releasing the probe lock on the peripheral.
1213 * The peripheral will only go away once the last lock
1214 * is removed, and we need it around for the CCB release
1217 xpt_release_ccb(done_ccb);
1218 cam_periph_unhold(periph);
1223 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1225 if (cderror(done_ccb, CAM_RETRY_SELTO,
1226 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1229 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1230 cam_release_devq(done_ccb->ccb_h.path,
1234 /*getcount_only*/0);
1236 xpt_release_ccb(done_ccb);
1237 cam_periph_release_locked(periph);
1243 xpt_release_ccb(done_ccb);
1246 static union cd_pages *
1247 cdgetpage(struct cd_mode_params *mode_params)
1249 union cd_pages *page;
1251 if (mode_params->cdb_size == 10)
1252 page = (union cd_pages *)find_mode_page_10(
1253 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1255 page = (union cd_pages *)find_mode_page_6(
1256 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1262 cdgetpagesize(int page_num)
1266 for (i = 0; i < nitems(cd_page_size_table); i++) {
1267 if (cd_page_size_table[i].page == page_num)
1268 return (cd_page_size_table[i].page_size);
1275 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1278 struct cam_periph *periph;
1279 struct cd_softc *softc;
1280 int nocopyout, error = 0;
1282 periph = (struct cam_periph *)dp->d_drv1;
1283 cam_periph_lock(periph);
1285 softc = (struct cd_softc *)periph->softc;
1287 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1288 ("cdioctl(%#lx)\n", cmd));
1290 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1291 cam_periph_unlock(periph);
1292 cam_periph_release(periph);
1297 * If we don't have media loaded, check for it. If still don't
1298 * have media loaded, we can only do a load or eject.
1300 * We only care whether media is loaded if this is a cd-specific ioctl
1301 * (thus the IOCGROUP check below). Note that this will break if
1302 * anyone adds any ioctls into the switch statement below that don't
1303 * have their ioctl group set to 'c'.
1305 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1306 && ((cmd != CDIOCCLOSE)
1307 && (cmd != CDIOCEJECT))
1308 && (IOCGROUP(cmd) == 'c')) {
1309 error = cdcheckmedia(periph);
1311 cam_periph_unhold(periph);
1312 cam_periph_unlock(periph);
1317 * Drop the lock here so later mallocs can use WAITOK. The periph
1318 * is essentially locked still with the cam_periph_hold call above.
1320 cam_periph_unlock(periph);
1325 case CDIOCPLAYTRACKS:
1327 struct ioc_play_track *args
1328 = (struct ioc_play_track *) addr;
1329 struct cd_mode_params params;
1330 union cd_pages *page;
1332 params.alloc_len = sizeof(union cd_mode_data_6_10);
1333 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1336 cam_periph_lock(periph);
1337 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1338 ("trying to do CDIOCPLAYTRACKS\n"));
1340 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1342 free(params.mode_buf, M_SCSICD);
1343 cam_periph_unlock(periph);
1346 page = cdgetpage(¶ms);
1348 page->audio.flags &= ~CD_PA_SOTC;
1349 page->audio.flags |= CD_PA_IMMED;
1350 error = cdsetmode(periph, ¶ms);
1351 free(params.mode_buf, M_SCSICD);
1353 cam_periph_unlock(periph);
1358 * This was originally implemented with the PLAY
1359 * AUDIO TRACK INDEX command, but that command was
1360 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1361 * drives support it but ATAPI and ATAPI-derivative
1362 * drives don't seem to support it. So we keep a
1363 * cache of the table of contents and translate
1364 * track numbers to MSF format.
1366 if (softc->flags & CD_FLAG_VALID_TOC) {
1367 union msf_lba *sentry, *eentry;
1370 if (args->end_track <
1371 softc->toc.header.ending_track + 1)
1373 if (args->end_track >
1374 softc->toc.header.ending_track + 1)
1376 softc->toc.header.ending_track + 1;
1377 st = args->start_track -
1378 softc->toc.header.starting_track;
1379 et = args->end_track -
1380 softc->toc.header.starting_track;
1383 || (st > (softc->toc.header.ending_track -
1384 softc->toc.header.starting_track))) {
1386 cam_periph_unlock(periph);
1389 sentry = &softc->toc.entries[st].addr;
1390 eentry = &softc->toc.entries[et].addr;
1391 error = cdplaymsf(periph,
1400 * If we don't have a valid TOC, try the
1401 * play track index command. It is part of
1402 * the SCSI-2 spec, but was removed in the
1403 * MMC specs. ATAPI and ATAPI-derived
1404 * drives don't support it.
1406 if (softc->quirks & CD_Q_BCD_TRACKS) {
1408 bin2bcd(args->start_track);
1410 bin2bcd(args->end_track);
1412 error = cdplaytracks(periph,
1418 cam_periph_unlock(periph);
1423 struct ioc_play_msf *args
1424 = (struct ioc_play_msf *) addr;
1425 struct cd_mode_params params;
1426 union cd_pages *page;
1428 params.alloc_len = sizeof(union cd_mode_data_6_10);
1429 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1432 cam_periph_lock(periph);
1433 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1434 ("trying to do CDIOCPLAYMSF\n"));
1436 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1438 free(params.mode_buf, M_SCSICD);
1439 cam_periph_unlock(periph);
1442 page = cdgetpage(¶ms);
1444 page->audio.flags &= ~CD_PA_SOTC;
1445 page->audio.flags |= CD_PA_IMMED;
1446 error = cdsetmode(periph, ¶ms);
1447 free(params.mode_buf, M_SCSICD);
1449 cam_periph_unlock(periph);
1452 error = cdplaymsf(periph,
1459 cam_periph_unlock(periph);
1462 case CDIOCPLAYBLOCKS:
1464 struct ioc_play_blocks *args
1465 = (struct ioc_play_blocks *) addr;
1466 struct cd_mode_params params;
1467 union cd_pages *page;
1469 params.alloc_len = sizeof(union cd_mode_data_6_10);
1470 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1473 cam_periph_lock(periph);
1474 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1475 ("trying to do CDIOCPLAYBLOCKS\n"));
1478 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1480 free(params.mode_buf, M_SCSICD);
1481 cam_periph_unlock(periph);
1484 page = cdgetpage(¶ms);
1486 page->audio.flags &= ~CD_PA_SOTC;
1487 page->audio.flags |= CD_PA_IMMED;
1488 error = cdsetmode(periph, ¶ms);
1489 free(params.mode_buf, M_SCSICD);
1491 cam_periph_unlock(periph);
1494 error = cdplay(periph, args->blk, args->len);
1495 cam_periph_unlock(periph);
1498 case CDIOCREADSUBCHANNEL_SYSSPACE:
1501 case CDIOCREADSUBCHANNEL:
1503 struct ioc_read_subchannel *args
1504 = (struct ioc_read_subchannel *) addr;
1505 struct cd_sub_channel_info *data;
1506 u_int32_t len = args->data_len;
1508 data = malloc(sizeof(struct cd_sub_channel_info),
1509 M_SCSICD, M_WAITOK | M_ZERO);
1511 cam_periph_lock(periph);
1512 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1513 ("trying to do CDIOCREADSUBCHANNEL\n"));
1515 if ((len > sizeof(struct cd_sub_channel_info)) ||
1516 (len < sizeof(struct cd_sub_channel_header))) {
1518 "scsi_cd: cdioctl: "
1519 "cdioreadsubchannel: error, len=%d\n",
1522 free(data, M_SCSICD);
1523 cam_periph_unlock(periph);
1527 if (softc->quirks & CD_Q_BCD_TRACKS)
1528 args->track = bin2bcd(args->track);
1530 error = cdreadsubchannel(periph, args->address_format,
1531 args->data_format, args->track, data, len);
1534 free(data, M_SCSICD);
1535 cam_periph_unlock(periph);
1538 if (softc->quirks & CD_Q_BCD_TRACKS)
1539 data->what.track_info.track_number =
1540 bcd2bin(data->what.track_info.track_number);
1541 len = min(len, ((data->header.data_len[0] << 8) +
1542 data->header.data_len[1] +
1543 sizeof(struct cd_sub_channel_header)));
1544 cam_periph_unlock(periph);
1545 if (nocopyout == 0) {
1546 if (copyout(data, args->data, len) != 0) {
1550 bcopy(data, args->data, len);
1552 free(data, M_SCSICD);
1556 case CDIOREADTOCHEADER:
1558 struct ioc_toc_header *th;
1560 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
1563 cam_periph_lock(periph);
1564 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1565 ("trying to do CDIOREADTOCHEADER\n"));
1567 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1568 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
1571 cam_periph_unlock(periph);
1574 if (softc->quirks & CD_Q_BCD_TRACKS) {
1575 /* we are going to have to convert the BCD
1576 * encoding on the cd to what is expected
1578 th->starting_track =
1579 bcd2bin(th->starting_track);
1580 th->ending_track = bcd2bin(th->ending_track);
1582 th->len = ntohs(th->len);
1583 bcopy(th, addr, sizeof(*th));
1585 cam_periph_unlock(periph);
1588 case CDIOREADTOCENTRYS:
1590 struct cd_tocdata *data;
1591 struct cd_toc_single *lead;
1592 struct ioc_read_toc_entry *te =
1593 (struct ioc_read_toc_entry *) addr;
1594 struct ioc_toc_header *th;
1595 u_int32_t len, readlen, idx, num;
1596 u_int32_t starting_track = te->starting_track;
1598 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
1599 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
1601 cam_periph_lock(periph);
1602 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1603 ("trying to do CDIOREADTOCENTRYS\n"));
1605 if (te->data_len < sizeof(struct cd_toc_entry)
1606 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
1607 || (te->address_format != CD_MSF_FORMAT
1608 && te->address_format != CD_LBA_FORMAT)) {
1610 printf("scsi_cd: error in readtocentries, "
1611 "returning EINVAL\n");
1612 free(data, M_SCSICD);
1613 free(lead, M_SCSICD);
1614 cam_periph_unlock(periph);
1619 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1620 sizeof (*th), /*sense_flags*/0);
1622 free(data, M_SCSICD);
1623 free(lead, M_SCSICD);
1624 cam_periph_unlock(periph);
1628 if (softc->quirks & CD_Q_BCD_TRACKS) {
1629 /* we are going to have to convert the BCD
1630 * encoding on the cd to what is expected
1632 th->starting_track =
1633 bcd2bin(th->starting_track);
1634 th->ending_track = bcd2bin(th->ending_track);
1637 if (starting_track == 0)
1638 starting_track = th->starting_track;
1639 else if (starting_track == LEADOUT)
1640 starting_track = th->ending_track + 1;
1641 else if (starting_track < th->starting_track ||
1642 starting_track > th->ending_track + 1) {
1643 printf("scsi_cd: error in readtocentries, "
1644 "returning EINVAL\n");
1645 free(data, M_SCSICD);
1646 free(lead, M_SCSICD);
1647 cam_periph_unlock(periph);
1652 /* calculate reading length without leadout entry */
1653 readlen = (th->ending_track - starting_track + 1) *
1654 sizeof(struct cd_toc_entry);
1656 /* and with leadout entry */
1657 len = readlen + sizeof(struct cd_toc_entry);
1658 if (te->data_len < len) {
1663 if (len > sizeof(data->entries)) {
1664 printf("scsi_cd: error in readtocentries, "
1665 "returning EINVAL\n");
1667 free(data, M_SCSICD);
1668 free(lead, M_SCSICD);
1669 cam_periph_unlock(periph);
1672 num = len / sizeof(struct cd_toc_entry);
1675 error = cdreadtoc(periph, te->address_format,
1678 readlen + sizeof (*th),
1681 free(data, M_SCSICD);
1682 free(lead, M_SCSICD);
1683 cam_periph_unlock(periph);
1688 /* make leadout entry if needed */
1689 idx = starting_track + num - 1;
1690 if (softc->quirks & CD_Q_BCD_TRACKS)
1691 th->ending_track = bcd2bin(th->ending_track);
1692 if (idx == th->ending_track + 1) {
1693 error = cdreadtoc(periph, te->address_format,
1694 LEADOUT, (u_int8_t *)lead,
1698 free(data, M_SCSICD);
1699 free(lead, M_SCSICD);
1700 cam_periph_unlock(periph);
1703 data->entries[idx - starting_track] =
1706 if (softc->quirks & CD_Q_BCD_TRACKS) {
1707 for (idx = 0; idx < num - 1; idx++) {
1708 data->entries[idx].track =
1709 bcd2bin(data->entries[idx].track);
1713 cam_periph_unlock(periph);
1714 error = copyout(data->entries, te->data, len);
1715 free(data, M_SCSICD);
1716 free(lead, M_SCSICD);
1719 case CDIOREADTOCENTRY:
1721 struct cd_toc_single *data;
1722 struct ioc_read_toc_single_entry *te =
1723 (struct ioc_read_toc_single_entry *) addr;
1724 struct ioc_toc_header *th;
1727 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
1729 cam_periph_lock(periph);
1730 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1731 ("trying to do CDIOREADTOCENTRY\n"));
1733 if (te->address_format != CD_MSF_FORMAT
1734 && te->address_format != CD_LBA_FORMAT) {
1735 printf("error in readtocentry, "
1736 " returning EINVAL\n");
1737 free(data, M_SCSICD);
1739 cam_periph_unlock(periph);
1744 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1745 sizeof (*th), /*sense_flags*/0);
1747 free(data, M_SCSICD);
1748 cam_periph_unlock(periph);
1752 if (softc->quirks & CD_Q_BCD_TRACKS) {
1753 /* we are going to have to convert the BCD
1754 * encoding on the cd to what is expected
1756 th->starting_track =
1757 bcd2bin(th->starting_track);
1758 th->ending_track = bcd2bin(th->ending_track);
1762 track = th->starting_track;
1763 else if (track == LEADOUT)
1765 else if (track < th->starting_track ||
1766 track > th->ending_track + 1) {
1767 printf("error in readtocentry, "
1768 " returning EINVAL\n");
1769 free(data, M_SCSICD);
1771 cam_periph_unlock(periph);
1775 error = cdreadtoc(periph, te->address_format, track,
1776 (u_int8_t *)data, sizeof(*data),
1779 free(data, M_SCSICD);
1780 cam_periph_unlock(periph);
1784 if (softc->quirks & CD_Q_BCD_TRACKS)
1785 data->entry.track = bcd2bin(data->entry.track);
1786 bcopy(&data->entry, &te->entry,
1787 sizeof(struct cd_toc_entry));
1788 free(data, M_SCSICD);
1789 cam_periph_unlock(periph);
1794 struct ioc_patch *arg = (struct ioc_patch *)addr;
1795 struct cd_mode_params params;
1796 union cd_pages *page;
1798 params.alloc_len = sizeof(union cd_mode_data_6_10);
1799 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1802 cam_periph_lock(periph);
1803 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1804 ("trying to do CDIOCSETPATCH\n"));
1806 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1808 free(params.mode_buf, M_SCSICD);
1809 cam_periph_unlock(periph);
1812 page = cdgetpage(¶ms);
1814 page->audio.port[LEFT_PORT].channels =
1816 page->audio.port[RIGHT_PORT].channels =
1818 page->audio.port[2].channels = arg->patch[2];
1819 page->audio.port[3].channels = arg->patch[3];
1820 error = cdsetmode(periph, ¶ms);
1821 free(params.mode_buf, M_SCSICD);
1822 cam_periph_unlock(periph);
1827 struct ioc_vol *arg = (struct ioc_vol *) addr;
1828 struct cd_mode_params params;
1829 union cd_pages *page;
1831 params.alloc_len = sizeof(union cd_mode_data_6_10);
1832 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1835 cam_periph_lock(periph);
1836 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1837 ("trying to do CDIOCGETVOL\n"));
1839 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1841 free(params.mode_buf, M_SCSICD);
1842 cam_periph_unlock(periph);
1845 page = cdgetpage(¶ms);
1847 arg->vol[LEFT_PORT] =
1848 page->audio.port[LEFT_PORT].volume;
1849 arg->vol[RIGHT_PORT] =
1850 page->audio.port[RIGHT_PORT].volume;
1851 arg->vol[2] = page->audio.port[2].volume;
1852 arg->vol[3] = page->audio.port[3].volume;
1853 free(params.mode_buf, M_SCSICD);
1854 cam_periph_unlock(periph);
1859 struct ioc_vol *arg = (struct ioc_vol *) addr;
1860 struct cd_mode_params params;
1861 union cd_pages *page;
1863 params.alloc_len = sizeof(union cd_mode_data_6_10);
1864 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1867 cam_periph_lock(periph);
1868 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1869 ("trying to do CDIOCSETVOL\n"));
1871 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1873 free(params.mode_buf, M_SCSICD);
1874 cam_periph_unlock(periph);
1877 page = cdgetpage(¶ms);
1879 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
1880 page->audio.port[LEFT_PORT].volume =
1881 arg->vol[LEFT_PORT];
1882 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
1883 page->audio.port[RIGHT_PORT].volume =
1884 arg->vol[RIGHT_PORT];
1885 page->audio.port[2].volume = arg->vol[2];
1886 page->audio.port[3].volume = arg->vol[3];
1887 error = cdsetmode(periph, ¶ms);
1888 cam_periph_unlock(periph);
1889 free(params.mode_buf, M_SCSICD);
1894 struct cd_mode_params params;
1895 union cd_pages *page;
1897 params.alloc_len = sizeof(union cd_mode_data_6_10);
1898 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1901 cam_periph_lock(periph);
1902 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1903 ("trying to do CDIOCSETMONO\n"));
1905 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1907 free(params.mode_buf, M_SCSICD);
1908 cam_periph_unlock(periph);
1911 page = cdgetpage(¶ms);
1913 page->audio.port[LEFT_PORT].channels =
1914 LEFT_CHANNEL | RIGHT_CHANNEL;
1915 page->audio.port[RIGHT_PORT].channels =
1916 LEFT_CHANNEL | RIGHT_CHANNEL;
1917 page->audio.port[2].channels = 0;
1918 page->audio.port[3].channels = 0;
1919 error = cdsetmode(periph, ¶ms);
1920 cam_periph_unlock(periph);
1921 free(params.mode_buf, M_SCSICD);
1924 case CDIOCSETSTEREO:
1926 struct cd_mode_params params;
1927 union cd_pages *page;
1929 params.alloc_len = sizeof(union cd_mode_data_6_10);
1930 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1933 cam_periph_lock(periph);
1934 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1935 ("trying to do CDIOCSETSTEREO\n"));
1937 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1939 free(params.mode_buf, M_SCSICD);
1940 cam_periph_unlock(periph);
1943 page = cdgetpage(¶ms);
1945 page->audio.port[LEFT_PORT].channels =
1947 page->audio.port[RIGHT_PORT].channels =
1949 page->audio.port[2].channels = 0;
1950 page->audio.port[3].channels = 0;
1951 error = cdsetmode(periph, ¶ms);
1952 free(params.mode_buf, M_SCSICD);
1953 cam_periph_unlock(periph);
1958 struct cd_mode_params params;
1959 union cd_pages *page;
1961 params.alloc_len = sizeof(union cd_mode_data_6_10);
1962 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1965 cam_periph_lock(periph);
1966 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1967 ("trying to do CDIOCSETMUTE\n"));
1969 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1971 free(params.mode_buf, M_SCSICD);
1972 cam_periph_unlock(periph);
1975 page = cdgetpage(¶ms);
1977 page->audio.port[LEFT_PORT].channels = 0;
1978 page->audio.port[RIGHT_PORT].channels = 0;
1979 page->audio.port[2].channels = 0;
1980 page->audio.port[3].channels = 0;
1981 error = cdsetmode(periph, ¶ms);
1982 free(params.mode_buf, M_SCSICD);
1983 cam_periph_unlock(periph);
1988 struct cd_mode_params params;
1989 union cd_pages *page;
1991 params.alloc_len = sizeof(union cd_mode_data_6_10);
1992 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1995 cam_periph_lock(periph);
1996 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1997 ("trying to do CDIOCSETLEFT\n"));
1999 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2001 free(params.mode_buf, M_SCSICD);
2002 cam_periph_unlock(periph);
2005 page = cdgetpage(¶ms);
2007 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2008 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2009 page->audio.port[2].channels = 0;
2010 page->audio.port[3].channels = 0;
2011 error = cdsetmode(periph, ¶ms);
2012 free(params.mode_buf, M_SCSICD);
2013 cam_periph_unlock(periph);
2018 struct cd_mode_params params;
2019 union cd_pages *page;
2021 params.alloc_len = sizeof(union cd_mode_data_6_10);
2022 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2025 cam_periph_lock(periph);
2026 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2027 ("trying to do CDIOCSETRIGHT\n"));
2029 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2031 free(params.mode_buf, M_SCSICD);
2032 cam_periph_unlock(periph);
2035 page = cdgetpage(¶ms);
2037 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2038 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2039 page->audio.port[2].channels = 0;
2040 page->audio.port[3].channels = 0;
2041 error = cdsetmode(periph, ¶ms);
2042 free(params.mode_buf, M_SCSICD);
2043 cam_periph_unlock(periph);
2047 cam_periph_lock(periph);
2048 error = cdpause(periph, 1);
2049 cam_periph_unlock(periph);
2052 cam_periph_lock(periph);
2053 error = cdpause(periph, 0);
2054 cam_periph_unlock(periph);
2057 cam_periph_lock(periph);
2058 error = cdstartunit(periph, 0);
2059 cam_periph_unlock(periph);
2062 cam_periph_lock(periph);
2063 error = cdstartunit(periph, 1);
2064 cam_periph_unlock(periph);
2067 cam_periph_lock(periph);
2068 error = cdstopunit(periph, 0);
2069 cam_periph_unlock(periph);
2072 cam_periph_lock(periph);
2073 error = cdstopunit(periph, 1);
2074 cam_periph_unlock(periph);
2077 cam_periph_lock(periph);
2078 cdprevent(periph, PR_ALLOW);
2079 cam_periph_unlock(periph);
2082 cam_periph_lock(periph);
2083 cdprevent(periph, PR_PREVENT);
2084 cam_periph_unlock(periph);
2087 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2091 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2095 /* return (cd_reset(periph)); */
2098 case CDRIOCREADSPEED:
2099 cam_periph_lock(periph);
2100 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2101 cam_periph_unlock(periph);
2103 case CDRIOCWRITESPEED:
2104 cam_periph_lock(periph);
2105 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2106 cam_periph_unlock(periph);
2108 case CDRIOCGETBLOCKSIZE:
2109 *(int *)addr = softc->params.blksize;
2111 case CDRIOCSETBLOCKSIZE:
2112 if (*(int *)addr <= 0) {
2116 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2119 case DVDIOCREPORTKEY: {
2120 struct dvd_authinfo *authinfo;
2122 authinfo = (struct dvd_authinfo *)addr;
2124 if (cmd == DVDIOCREPORTKEY)
2125 error = cdreportkey(periph, authinfo);
2127 error = cdsendkey(periph, authinfo);
2130 case DVDIOCREADSTRUCTURE: {
2131 struct dvd_struct *dvdstruct;
2133 dvdstruct = (struct dvd_struct *)addr;
2135 error = cdreaddvdstructure(periph, dvdstruct);
2140 cam_periph_lock(periph);
2141 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2142 cam_periph_unlock(periph);
2146 cam_periph_lock(periph);
2147 cam_periph_unhold(periph);
2149 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2150 if (error && bootverbose) {
2151 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2153 cam_periph_unlock(periph);
2159 cdprevent(struct cam_periph *periph, int action)
2162 struct cd_softc *softc;
2165 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2167 softc = (struct cd_softc *)periph->softc;
2169 if (((action == PR_ALLOW)
2170 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2171 || ((action == PR_PREVENT)
2172 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2176 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2178 scsi_prevent(&ccb->csio,
2179 /*retries*/ cd_retry_count,
2184 /* timeout */60000);
2186 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2187 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2189 xpt_release_ccb(ccb);
2192 if (action == PR_ALLOW)
2193 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2195 softc->flags |= CD_FLAG_DISC_LOCKED;
2200 * XXX: the disk media and sector size is only really able to change
2201 * XXX: while the device is closed.
2204 cdcheckmedia(struct cam_periph *periph)
2206 struct cd_softc *softc;
2207 struct ioc_toc_header *toch;
2208 struct cd_toc_single leadout;
2209 u_int32_t size, toclen;
2210 int error, num_entries, cdindex;
2212 softc = (struct cd_softc *)periph->softc;
2214 cdprevent(periph, PR_PREVENT);
2215 softc->disk->d_sectorsize = 2048;
2216 softc->disk->d_mediasize = 0;
2219 * Get the disc size and block size. If we can't get it, we don't
2220 * have media, most likely.
2222 if ((error = cdsize(periph, &size)) != 0) {
2223 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2224 cdprevent(periph, PR_ALLOW);
2227 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2228 softc->disk->d_sectorsize = softc->params.blksize;
2229 softc->disk->d_mediasize =
2230 (off_t)softc->params.blksize * softc->params.disksize;
2234 * Now we check the table of contents. This (currently) is only
2235 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2236 * things like present a separate entry in /dev for each track,
2237 * like that acd(4) driver does.
2239 bzero(&softc->toc, sizeof(softc->toc));
2240 toch = &softc->toc.header;
2242 * We will get errors here for media that doesn't have a table of
2243 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2244 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2245 * has not been recorded (no complete session) and the Format codes
2246 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2247 * with an INVALID FIELD IN CDB. Devices that are not capable of
2248 * reading an incomplete session on DDC/CD-R/RW media shall report
2249 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2251 * So this isn't fatal if we can't read the table of contents, it
2252 * just means that the user won't be able to issue the play tracks
2253 * ioctl, and likely lots of other stuff won't work either. They
2254 * need to burn the CD before we can do a whole lot with it. So
2255 * we don't print anything here if we get an error back.
2257 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2260 * Errors in reading the table of contents aren't fatal, we just
2261 * won't have a valid table of contents cached.
2265 bzero(&softc->toc, sizeof(softc->toc));
2269 if (softc->quirks & CD_Q_BCD_TRACKS) {
2270 toch->starting_track = bcd2bin(toch->starting_track);
2271 toch->ending_track = bcd2bin(toch->ending_track);
2274 /* Number of TOC entries, plus leadout */
2275 num_entries = (toch->ending_track - toch->starting_track) + 2;
2277 if (num_entries <= 0)
2280 toclen = num_entries * sizeof(struct cd_toc_entry);
2282 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2283 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2287 bzero(&softc->toc, sizeof(softc->toc));
2291 if (softc->quirks & CD_Q_BCD_TRACKS) {
2292 toch->starting_track = bcd2bin(toch->starting_track);
2293 toch->ending_track = bcd2bin(toch->ending_track);
2296 * XXX KDM is this necessary? Probably only if the drive doesn't
2297 * return leadout information with the table of contents.
2299 cdindex = toch->starting_track + num_entries -1;
2300 if (cdindex == toch->ending_track + 1) {
2302 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2303 (u_int8_t *)&leadout, sizeof(leadout),
2309 softc->toc.entries[cdindex - toch->starting_track] =
2312 if (softc->quirks & CD_Q_BCD_TRACKS) {
2313 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2314 softc->toc.entries[cdindex].track =
2315 bcd2bin(softc->toc.entries[cdindex].track);
2319 softc->flags |= CD_FLAG_VALID_TOC;
2321 /* If the first track is audio, correct sector size. */
2322 if ((softc->toc.entries[0].control & 4) == 0) {
2323 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2324 softc->disk->d_mediasize =
2325 (off_t)softc->params.blksize * softc->params.disksize;
2331 * We unconditionally (re)set the blocksize each time the
2332 * CD device is opened. This is because the CD can change,
2333 * and therefore the blocksize might change.
2334 * XXX problems here if some slice or partition is still
2335 * open with the old size?
2337 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2338 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2339 softc->disk->d_devstat->block_size = softc->params.blksize;
2345 cdsize(struct cam_periph *periph, u_int32_t *size)
2347 struct cd_softc *softc;
2349 struct scsi_read_capacity_data *rcap_buf;
2352 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2354 softc = (struct cd_softc *)periph->softc;
2356 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2358 /* XXX Should be M_WAITOK */
2359 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2360 M_SCSICD, M_NOWAIT | M_ZERO);
2361 if (rcap_buf == NULL)
2364 scsi_read_capacity(&ccb->csio,
2365 /*retries*/ cd_retry_count,
2370 /* timeout */20000);
2372 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2373 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2375 xpt_release_ccb(ccb);
2377 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2378 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2379 /* Make sure we got at least some block size. */
2380 if (error == 0 && softc->params.blksize == 0)
2383 * SCSI-3 mandates that the reported blocksize shall be 2048.
2384 * Older drives sometimes report funny values, trim it down to
2385 * 2048, or other parts of the kernel will get confused.
2387 * XXX we leave drives alone that might report 512 bytes, as
2388 * well as drives reporting more weird sizes like perhaps 4K.
2390 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2391 softc->params.blksize = 2048;
2393 free(rcap_buf, M_SCSICD);
2394 *size = softc->params.disksize;
2401 cd6byteworkaround(union ccb *ccb)
2404 struct cam_periph *periph;
2405 struct cd_softc *softc;
2406 struct cd_mode_params *params;
2409 periph = xpt_path_periph(ccb->ccb_h.path);
2410 softc = (struct cd_softc *)periph->softc;
2412 cdb = ccb->csio.cdb_io.cdb_bytes;
2414 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2415 || ((cdb[0] != MODE_SENSE_6)
2416 && (cdb[0] != MODE_SELECT_6)))
2420 * Because there is no convenient place to stash the overall
2421 * cd_mode_params structure pointer, we have to grab it like this.
2422 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2423 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2425 * XXX It would be nice if, at some point, we could increase the
2426 * number of available peripheral private pointers. Both pointers
2427 * are currently used in most every peripheral driver.
2431 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2432 if (params->mode_buf == ccb->csio.data_ptr) {
2439 * This shouldn't happen. All mode sense and mode select
2440 * operations in the cd(4) driver MUST go through cdgetmode() and
2444 xpt_print(periph->path,
2445 "mode buffer not found in mode queue!\n");
2449 params->cdb_size = 10;
2450 softc->minimum_command_size = 10;
2451 xpt_print(ccb->ccb_h.path,
2452 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2453 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2455 if (cdb[0] == MODE_SENSE_6) {
2456 struct scsi_mode_sense_10 ms10;
2457 struct scsi_mode_sense_6 *ms6;
2460 ms6 = (struct scsi_mode_sense_6 *)cdb;
2462 bzero(&ms10, sizeof(ms10));
2463 ms10.opcode = MODE_SENSE_10;
2464 ms10.byte2 = ms6->byte2;
2465 ms10.page = ms6->page;
2468 * 10 byte mode header, block descriptor,
2469 * sizeof(union cd_pages)
2471 len = sizeof(struct cd_mode_data_10);
2472 ccb->csio.dxfer_len = len;
2474 scsi_ulto2b(len, ms10.length);
2475 ms10.control = ms6->control;
2476 bcopy(&ms10, cdb, 10);
2477 ccb->csio.cdb_len = 10;
2479 struct scsi_mode_select_10 ms10;
2480 struct scsi_mode_select_6 *ms6;
2481 struct scsi_mode_header_6 *header6;
2482 struct scsi_mode_header_10 *header10;
2483 struct scsi_mode_page_header *page_header;
2484 int blk_desc_len, page_num, page_size, len;
2486 ms6 = (struct scsi_mode_select_6 *)cdb;
2488 bzero(&ms10, sizeof(ms10));
2489 ms10.opcode = MODE_SELECT_10;
2490 ms10.byte2 = ms6->byte2;
2492 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
2493 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
2495 page_header = find_mode_page_6(header6);
2496 page_num = page_header->page_code;
2498 blk_desc_len = header6->blk_desc_len;
2500 page_size = cdgetpagesize(page_num);
2502 if (page_size != (page_header->page_length +
2503 sizeof(*page_header)))
2504 page_size = page_header->page_length +
2505 sizeof(*page_header);
2507 len = sizeof(*header10) + blk_desc_len + page_size;
2509 len = min(params->alloc_len, len);
2512 * Since the 6 byte parameter header is shorter than the 10
2513 * byte parameter header, we need to copy the actual mode
2514 * page data, and the block descriptor, if any, so things wind
2515 * up in the right place. The regions will overlap, but
2516 * bcopy() does the right thing.
2518 bcopy(params->mode_buf + sizeof(*header6),
2519 params->mode_buf + sizeof(*header10),
2520 len - sizeof(*header10));
2522 /* Make sure these fields are set correctly. */
2523 scsi_ulto2b(0, header10->data_length);
2524 header10->medium_type = 0;
2525 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
2527 ccb->csio.dxfer_len = len;
2529 scsi_ulto2b(len, ms10.length);
2530 ms10.control = ms6->control;
2531 bcopy(&ms10, cdb, 10);
2532 ccb->csio.cdb_len = 10;
2535 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2536 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2539 cam_release_devq(ccb->ccb_h.path,
2543 /*getcount_only*/0);
2550 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2552 struct cd_softc *softc;
2553 struct cam_periph *periph;
2554 int error, error_code, sense_key, asc, ascq;
2556 periph = xpt_path_periph(ccb->ccb_h.path);
2557 softc = (struct cd_softc *)periph->softc;
2562 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
2563 * CDB comes back with this particular error, try transforming it
2564 * into the 10 byte version.
2566 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2567 error = cd6byteworkaround(ccb);
2568 } else if (scsi_extract_sense_ccb(ccb,
2569 &error_code, &sense_key, &asc, &ascq)) {
2570 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2571 error = cd6byteworkaround(ccb);
2572 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2573 asc == 0x28 && ascq == 0x00)
2574 disk_media_changed(softc->disk, M_NOWAIT);
2575 else if (sense_key == SSD_KEY_NOT_READY &&
2576 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
2577 softc->flags &= ~CD_FLAG_SAW_MEDIA;
2578 disk_media_gone(softc->disk, M_NOWAIT);
2582 if (error == ERESTART)
2587 * Until we have a better way of doing pack validation,
2588 * don't treat UAs as errors.
2590 sense_flags |= SF_RETRY_UA;
2592 if (softc->quirks & CD_Q_RETRY_BUSY)
2593 sense_flags |= SF_RETRY_BUSY;
2594 return (cam_periph_error(ccb, cam_flags, sense_flags,
2595 &softc->saved_ccb));
2599 cdmediapoll(void *arg)
2601 struct cam_periph *periph = arg;
2602 struct cd_softc *softc = periph->softc;
2604 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
2605 softc->outstanding_cmds == 0) {
2606 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2608 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2611 /* Queue us up again */
2612 if (cd_poll_period != 0)
2613 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
2617 * Read table of contents
2620 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2621 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
2623 struct scsi_read_toc *scsi_cmd;
2625 struct ccb_scsiio *csio;
2632 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2637 /* retries */ cd_retry_count,
2638 /* cbfcnp */ cddone,
2639 /* flags */ CAM_DIR_IN,
2640 /* tag_action */ MSG_SIMPLE_Q_TAG,
2641 /* data_ptr */ data,
2642 /* dxfer_len */ len,
2643 /* sense_len */ SSD_FULL_SIZE,
2644 sizeof(struct scsi_read_toc),
2645 /* timeout */ 50000);
2647 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2648 bzero (scsi_cmd, sizeof(*scsi_cmd));
2650 if (mode == CD_MSF_FORMAT)
2651 scsi_cmd->byte2 |= CD_MSF;
2652 scsi_cmd->from_track = start;
2653 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
2654 scsi_cmd->data_len[0] = (ntoc) >> 8;
2655 scsi_cmd->data_len[1] = (ntoc) & 0xff;
2657 scsi_cmd->op_code = READ_TOC;
2659 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2660 /*sense_flags*/SF_RETRY_UA | sense_flags);
2662 xpt_release_ccb(ccb);
2668 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
2669 u_int32_t format, int track,
2670 struct cd_sub_channel_info *data, u_int32_t len)
2672 struct scsi_read_subchannel *scsi_cmd;
2673 struct ccb_scsiio *csio;
2679 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2684 /* retries */ cd_retry_count,
2685 /* cbfcnp */ cddone,
2686 /* flags */ CAM_DIR_IN,
2687 /* tag_action */ MSG_SIMPLE_Q_TAG,
2688 /* data_ptr */ (u_int8_t *)data,
2689 /* dxfer_len */ len,
2690 /* sense_len */ SSD_FULL_SIZE,
2691 sizeof(struct scsi_read_subchannel),
2692 /* timeout */ 50000);
2694 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2695 bzero (scsi_cmd, sizeof(*scsi_cmd));
2697 scsi_cmd->op_code = READ_SUBCHANNEL;
2698 if (mode == CD_MSF_FORMAT)
2699 scsi_cmd->byte1 |= CD_MSF;
2700 scsi_cmd->byte2 = SRS_SUBQ;
2701 scsi_cmd->subchan_format = format;
2702 scsi_cmd->track = track;
2703 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
2704 scsi_cmd->control = 0;
2706 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2707 /*sense_flags*/SF_RETRY_UA);
2709 xpt_release_ccb(ccb);
2716 * All MODE_SENSE requests in the cd(4) driver MUST go through this
2717 * routine. See comments in cd6byteworkaround() for details.
2720 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
2723 struct ccb_scsiio *csio;
2724 struct cd_softc *softc;
2729 softc = (struct cd_softc *)periph->softc;
2731 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2735 data->cdb_size = softc->minimum_command_size;
2736 if (data->cdb_size < 10)
2737 param_len = sizeof(struct cd_mode_data);
2739 param_len = sizeof(struct cd_mode_data_10);
2741 /* Don't say we've got more room than we actually allocated */
2742 param_len = min(param_len, data->alloc_len);
2744 scsi_mode_sense_len(csio,
2745 /* retries */ cd_retry_count,
2746 /* cbfcnp */ cddone,
2747 /* tag_action */ MSG_SIMPLE_Q_TAG,
2749 /* page_code */ SMS_PAGE_CTRL_CURRENT,
2751 /* param_buf */ data->mode_buf,
2752 /* param_len */ param_len,
2753 /* minimum_cmd_size */ softc->minimum_command_size,
2754 /* sense_len */ SSD_FULL_SIZE,
2755 /* timeout */ 50000);
2758 * It would be nice not to have to do this, but there's no
2759 * available pointer in the CCB that would allow us to stuff the
2760 * mode params structure in there and retrieve it in
2761 * cd6byteworkaround(), so we can set the cdb size. The cdb size
2762 * lets the caller know what CDB size we ended up using, so they
2763 * can find the actual mode page offset.
2765 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
2767 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2768 /*sense_flags*/SF_RETRY_UA);
2770 xpt_release_ccb(ccb);
2772 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
2775 * This is a bit of belt-and-suspenders checking, but if we run
2776 * into a situation where the target sends back multiple block
2777 * descriptors, we might not have enough space in the buffer to
2778 * see the whole mode page. Better to return an error than
2779 * potentially access memory beyond our malloced region.
2784 if (data->cdb_size == 10) {
2785 struct scsi_mode_header_10 *hdr10;
2787 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
2788 data_len = scsi_2btoul(hdr10->data_length);
2789 data_len += sizeof(hdr10->data_length);
2791 struct scsi_mode_header_6 *hdr6;
2793 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
2794 data_len = hdr6->data_length;
2795 data_len += sizeof(hdr6->data_length);
2799 * Complain if there is more mode data available than we
2800 * allocated space for. This could potentially happen if
2801 * we miscalculated the page length for some reason, if the
2802 * drive returns multiple block descriptors, or if it sets
2803 * the data length incorrectly.
2805 if (data_len > data->alloc_len) {
2806 xpt_print(periph->path, "allocated modepage %d length "
2807 "%d < returned length %d\n", page, data->alloc_len,
2816 * All MODE_SELECT requests in the cd(4) driver MUST go through this
2817 * routine. See comments in cd6byteworkaround() for details.
2820 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
2822 struct ccb_scsiio *csio;
2823 struct cd_softc *softc;
2825 int cdb_size, param_len;
2828 softc = (struct cd_softc *)periph->softc;
2830 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2837 * If the data is formatted for the 10 byte version of the mode
2838 * select parameter list, we need to use the 10 byte CDB.
2839 * Otherwise, we use whatever the stored minimum command size.
2841 if (data->cdb_size == 10)
2842 cdb_size = data->cdb_size;
2844 cdb_size = softc->minimum_command_size;
2846 if (cdb_size >= 10) {
2847 struct scsi_mode_header_10 *mode_header;
2850 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
2852 data_len = scsi_2btoul(mode_header->data_length);
2854 scsi_ulto2b(0, mode_header->data_length);
2856 * SONY drives do not allow a mode select with a medium_type
2857 * value that has just been returned by a mode sense; use a
2858 * medium_type of 0 (Default) instead.
2860 mode_header->medium_type = 0;
2863 * Pass back whatever the drive passed to us, plus the size
2864 * of the data length field.
2866 param_len = data_len + sizeof(mode_header->data_length);
2869 struct scsi_mode_header_6 *mode_header;
2871 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
2873 param_len = mode_header->data_length + 1;
2875 mode_header->data_length = 0;
2877 * SONY drives do not allow a mode select with a medium_type
2878 * value that has just been returned by a mode sense; use a
2879 * medium_type of 0 (Default) instead.
2881 mode_header->medium_type = 0;
2884 /* Don't say we've got more room than we actually allocated */
2885 param_len = min(param_len, data->alloc_len);
2887 scsi_mode_select_len(csio,
2888 /* retries */ cd_retry_count,
2889 /* cbfcnp */ cddone,
2890 /* tag_action */ MSG_SIMPLE_Q_TAG,
2891 /* scsi_page_fmt */ 1,
2893 /* param_buf */ data->mode_buf,
2894 /* param_len */ param_len,
2895 /* minimum_cmd_size */ cdb_size,
2896 /* sense_len */ SSD_FULL_SIZE,
2897 /* timeout */ 50000);
2899 /* See comments in cdgetmode() and cd6byteworkaround(). */
2900 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
2902 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2903 /*sense_flags*/SF_RETRY_UA);
2905 xpt_release_ccb(ccb);
2907 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
2914 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2916 struct ccb_scsiio *csio;
2922 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2925 * Use the smallest possible command to perform the operation.
2927 if ((len & 0xffff0000) == 0) {
2929 * We can fit in a 10 byte cdb.
2931 struct scsi_play_10 *scsi_cmd;
2933 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
2934 bzero (scsi_cmd, sizeof(*scsi_cmd));
2935 scsi_cmd->op_code = PLAY_10;
2936 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2937 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
2938 cdb_len = sizeof(*scsi_cmd);
2940 struct scsi_play_12 *scsi_cmd;
2942 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
2943 bzero (scsi_cmd, sizeof(*scsi_cmd));
2944 scsi_cmd->op_code = PLAY_12;
2945 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2946 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
2947 cdb_len = sizeof(*scsi_cmd);
2950 /*retries*/ cd_retry_count,
2952 /*flags*/CAM_DIR_NONE,
2956 /*sense_len*/SSD_FULL_SIZE,
2958 /*timeout*/50 * 1000);
2960 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2961 /*sense_flags*/SF_RETRY_UA);
2963 xpt_release_ccb(ccb);
2969 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
2970 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
2972 struct scsi_play_msf *scsi_cmd;
2973 struct ccb_scsiio *csio;
2979 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2984 /* retries */ cd_retry_count,
2985 /* cbfcnp */ cddone,
2986 /* flags */ CAM_DIR_NONE,
2987 /* tag_action */ MSG_SIMPLE_Q_TAG,
2988 /* data_ptr */ NULL,
2990 /* sense_len */ SSD_FULL_SIZE,
2991 sizeof(struct scsi_play_msf),
2992 /* timeout */ 50000);
2994 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2995 bzero (scsi_cmd, sizeof(*scsi_cmd));
2997 scsi_cmd->op_code = PLAY_MSF;
2998 scsi_cmd->start_m = startm;
2999 scsi_cmd->start_s = starts;
3000 scsi_cmd->start_f = startf;
3001 scsi_cmd->end_m = endm;
3002 scsi_cmd->end_s = ends;
3003 scsi_cmd->end_f = endf;
3005 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3006 /*sense_flags*/SF_RETRY_UA);
3008 xpt_release_ccb(ccb);
3015 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3016 u_int32_t etrack, u_int32_t eindex)
3018 struct scsi_play_track *scsi_cmd;
3019 struct ccb_scsiio *csio;
3025 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3030 /* retries */ cd_retry_count,
3031 /* cbfcnp */ cddone,
3032 /* flags */ CAM_DIR_NONE,
3033 /* tag_action */ MSG_SIMPLE_Q_TAG,
3034 /* data_ptr */ NULL,
3036 /* sense_len */ SSD_FULL_SIZE,
3037 sizeof(struct scsi_play_track),
3038 /* timeout */ 50000);
3040 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3041 bzero (scsi_cmd, sizeof(*scsi_cmd));
3043 scsi_cmd->op_code = PLAY_TRACK;
3044 scsi_cmd->start_track = strack;
3045 scsi_cmd->start_index = sindex;
3046 scsi_cmd->end_track = etrack;
3047 scsi_cmd->end_index = eindex;
3049 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3050 /*sense_flags*/SF_RETRY_UA);
3052 xpt_release_ccb(ccb);
3058 cdpause(struct cam_periph *periph, u_int32_t go)
3060 struct scsi_pause *scsi_cmd;
3061 struct ccb_scsiio *csio;
3067 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3072 /* retries */ cd_retry_count,
3073 /* cbfcnp */ cddone,
3074 /* flags */ CAM_DIR_NONE,
3075 /* tag_action */ MSG_SIMPLE_Q_TAG,
3076 /* data_ptr */ NULL,
3078 /* sense_len */ SSD_FULL_SIZE,
3079 sizeof(struct scsi_pause),
3080 /* timeout */ 50000);
3082 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3083 bzero (scsi_cmd, sizeof(*scsi_cmd));
3085 scsi_cmd->op_code = PAUSE;
3086 scsi_cmd->resume = go;
3088 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3089 /*sense_flags*/SF_RETRY_UA);
3091 xpt_release_ccb(ccb);
3097 cdstartunit(struct cam_periph *periph, int load)
3104 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3106 scsi_start_stop(&ccb->csio,
3107 /* retries */ cd_retry_count,
3108 /* cbfcnp */ cddone,
3109 /* tag_action */ MSG_SIMPLE_Q_TAG,
3111 /* load_eject */ load,
3112 /* immediate */ FALSE,
3113 /* sense_len */ SSD_FULL_SIZE,
3114 /* timeout */ 50000);
3116 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3117 /*sense_flags*/SF_RETRY_UA);
3119 xpt_release_ccb(ccb);
3125 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3132 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3134 scsi_start_stop(&ccb->csio,
3135 /* retries */ cd_retry_count,
3136 /* cbfcnp */ cddone,
3137 /* tag_action */ MSG_SIMPLE_Q_TAG,
3139 /* load_eject */ eject,
3140 /* immediate */ FALSE,
3141 /* sense_len */ SSD_FULL_SIZE,
3142 /* timeout */ 50000);
3144 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3145 /*sense_flags*/SF_RETRY_UA);
3147 xpt_release_ccb(ccb);
3153 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3155 struct scsi_set_speed *scsi_cmd;
3156 struct ccb_scsiio *csio;
3161 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3164 /* Preserve old behavior: units in multiples of CDROM speed */
3171 /* retries */ cd_retry_count,
3172 /* cbfcnp */ cddone,
3173 /* flags */ CAM_DIR_NONE,
3174 /* tag_action */ MSG_SIMPLE_Q_TAG,
3175 /* data_ptr */ NULL,
3177 /* sense_len */ SSD_FULL_SIZE,
3178 sizeof(struct scsi_set_speed),
3179 /* timeout */ 50000);
3181 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3182 bzero(scsi_cmd, sizeof(*scsi_cmd));
3184 scsi_cmd->opcode = SET_CD_SPEED;
3185 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3186 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3188 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3189 /*sense_flags*/SF_RETRY_UA);
3191 xpt_release_ccb(ccb);
3197 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3209 switch (authinfo->format) {
3210 case DVD_REPORT_AGID:
3211 length = sizeof(struct scsi_report_key_data_agid);
3213 case DVD_REPORT_CHALLENGE:
3214 length = sizeof(struct scsi_report_key_data_challenge);
3216 case DVD_REPORT_KEY1:
3217 length = sizeof(struct scsi_report_key_data_key1_key2);
3219 case DVD_REPORT_TITLE_KEY:
3220 length = sizeof(struct scsi_report_key_data_title);
3221 /* The lba field is only set for the title key */
3222 lba = authinfo->lba;
3224 case DVD_REPORT_ASF:
3225 length = sizeof(struct scsi_report_key_data_asf);
3227 case DVD_REPORT_RPC:
3228 length = sizeof(struct scsi_report_key_data_rpc);
3230 case DVD_INVALIDATE_AGID:
3238 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3242 cam_periph_lock(periph);
3243 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3245 scsi_report_key(&ccb->csio,
3246 /* retries */ cd_retry_count,
3247 /* cbfcnp */ cddone,
3248 /* tag_action */ MSG_SIMPLE_Q_TAG,
3250 /* agid */ authinfo->agid,
3251 /* key_format */ authinfo->format,
3252 /* data_ptr */ databuf,
3253 /* dxfer_len */ length,
3254 /* sense_len */ SSD_FULL_SIZE,
3255 /* timeout */ 50000);
3257 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3258 /*sense_flags*/SF_RETRY_UA);
3263 if (ccb->csio.resid != 0) {
3264 xpt_print(periph->path, "warning, residual for report key "
3265 "command is %d\n", ccb->csio.resid);
3268 switch(authinfo->format) {
3269 case DVD_REPORT_AGID: {
3270 struct scsi_report_key_data_agid *agid_data;
3272 agid_data = (struct scsi_report_key_data_agid *)databuf;
3274 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3278 case DVD_REPORT_CHALLENGE: {
3279 struct scsi_report_key_data_challenge *chal_data;
3281 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3283 bcopy(chal_data->challenge_key, authinfo->keychal,
3284 min(sizeof(chal_data->challenge_key),
3285 sizeof(authinfo->keychal)));
3288 case DVD_REPORT_KEY1: {
3289 struct scsi_report_key_data_key1_key2 *key1_data;
3291 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3293 bcopy(key1_data->key1, authinfo->keychal,
3294 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3297 case DVD_REPORT_TITLE_KEY: {
3298 struct scsi_report_key_data_title *title_data;
3300 title_data = (struct scsi_report_key_data_title *)databuf;
3302 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3303 RKD_TITLE_CPM_SHIFT;
3304 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3305 RKD_TITLE_CP_SEC_SHIFT;
3306 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3307 RKD_TITLE_CMGS_SHIFT;
3308 bcopy(title_data->title_key, authinfo->keychal,
3309 min(sizeof(title_data->title_key),
3310 sizeof(authinfo->keychal)));
3313 case DVD_REPORT_ASF: {
3314 struct scsi_report_key_data_asf *asf_data;
3316 asf_data = (struct scsi_report_key_data_asf *)databuf;
3318 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3321 case DVD_REPORT_RPC: {
3322 struct scsi_report_key_data_rpc *rpc_data;
3324 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3326 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3328 authinfo->vend_rsts =
3329 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3330 RKD_RPC_VENDOR_RESET_SHIFT;
3331 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3332 authinfo->region = rpc_data->region_mask;
3333 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3336 case DVD_INVALIDATE_AGID:
3339 /* This should be impossible, since we checked above */
3342 break; /* NOTREACHED */
3346 xpt_release_ccb(ccb);
3347 cam_periph_unlock(periph);
3349 if (databuf != NULL)
3350 free(databuf, M_DEVBUF);
3356 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3366 switch(authinfo->format) {
3367 case DVD_SEND_CHALLENGE: {
3368 struct scsi_report_key_data_challenge *challenge_data;
3370 length = sizeof(*challenge_data);
3372 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3374 databuf = (u_int8_t *)challenge_data;
3376 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3377 challenge_data->data_len);
3379 bcopy(authinfo->keychal, challenge_data->challenge_key,
3380 min(sizeof(authinfo->keychal),
3381 sizeof(challenge_data->challenge_key)));
3384 case DVD_SEND_KEY2: {
3385 struct scsi_report_key_data_key1_key2 *key2_data;
3387 length = sizeof(*key2_data);
3389 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3391 databuf = (u_int8_t *)key2_data;
3393 scsi_ulto2b(length - sizeof(key2_data->data_len),
3394 key2_data->data_len);
3396 bcopy(authinfo->keychal, key2_data->key1,
3397 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3401 case DVD_SEND_RPC: {
3402 struct scsi_send_key_data_rpc *rpc_data;
3404 length = sizeof(*rpc_data);
3406 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3408 databuf = (u_int8_t *)rpc_data;
3410 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3411 rpc_data->data_len);
3413 rpc_data->region_code = authinfo->region;
3420 cam_periph_lock(periph);
3421 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3423 scsi_send_key(&ccb->csio,
3424 /* retries */ cd_retry_count,
3425 /* cbfcnp */ cddone,
3426 /* tag_action */ MSG_SIMPLE_Q_TAG,
3427 /* agid */ authinfo->agid,
3428 /* key_format */ authinfo->format,
3429 /* data_ptr */ databuf,
3430 /* dxfer_len */ length,
3431 /* sense_len */ SSD_FULL_SIZE,
3432 /* timeout */ 50000);
3434 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3435 /*sense_flags*/SF_RETRY_UA);
3437 xpt_release_ccb(ccb);
3438 cam_periph_unlock(periph);
3440 if (databuf != NULL)
3441 free(databuf, M_DEVBUF);
3447 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3457 /* The address is reserved for many of the formats */
3460 switch(dvdstruct->format) {
3461 case DVD_STRUCT_PHYSICAL:
3462 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3464 case DVD_STRUCT_COPYRIGHT:
3465 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3467 case DVD_STRUCT_DISCKEY:
3468 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3470 case DVD_STRUCT_BCA:
3471 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3473 case DVD_STRUCT_MANUFACT:
3474 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3476 case DVD_STRUCT_CMI:
3478 case DVD_STRUCT_PROTDISCID:
3479 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3481 case DVD_STRUCT_DISCKEYBLOCK:
3482 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3484 case DVD_STRUCT_DDS:
3485 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3487 case DVD_STRUCT_MEDIUM_STAT:
3488 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3490 case DVD_STRUCT_SPARE_AREA:
3491 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3493 case DVD_STRUCT_RMD_LAST:
3495 case DVD_STRUCT_RMD_RMA:
3497 case DVD_STRUCT_PRERECORDED:
3498 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3500 case DVD_STRUCT_UNIQUEID:
3501 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3503 case DVD_STRUCT_DCB:
3505 case DVD_STRUCT_LIST:
3507 * This is the maximum allocation length for the READ DVD
3508 * STRUCTURE command. There's nothing in the MMC3 spec
3509 * that indicates a limit in the amount of data that can
3510 * be returned from this call, other than the limits
3511 * imposed by the 2-byte length variables.
3520 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3524 cam_periph_lock(periph);
3525 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3527 scsi_read_dvd_structure(&ccb->csio,
3528 /* retries */ cd_retry_count,
3529 /* cbfcnp */ cddone,
3530 /* tag_action */ MSG_SIMPLE_Q_TAG,
3532 /* layer_number */ dvdstruct->layer_num,
3533 /* key_format */ dvdstruct->format,
3534 /* agid */ dvdstruct->agid,
3535 /* data_ptr */ databuf,
3536 /* dxfer_len */ length,
3537 /* sense_len */ SSD_FULL_SIZE,
3538 /* timeout */ 50000);
3540 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3541 /*sense_flags*/SF_RETRY_UA);
3546 switch(dvdstruct->format) {
3547 case DVD_STRUCT_PHYSICAL: {
3548 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
3549 struct dvd_layer *outlayer;
3550 struct scsi_read_dvd_struct_data_physical *phys_data;
3553 (struct scsi_read_dvd_struct_data_physical *)databuf;
3554 inlayer = &phys_data->layer_desc;
3555 outlayer = (struct dvd_layer *)&dvdstruct->data;
3557 dvdstruct->length = sizeof(*inlayer);
3559 outlayer->book_type = (inlayer->book_type_version &
3560 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
3561 outlayer->book_version = (inlayer->book_type_version &
3562 RDSD_BOOK_VERSION_MASK);
3563 outlayer->disc_size = (inlayer->disc_size_max_rate &
3564 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
3565 outlayer->max_rate = (inlayer->disc_size_max_rate &
3566 RDSD_MAX_RATE_MASK);
3567 outlayer->nlayers = (inlayer->layer_info &
3568 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
3569 outlayer->track_path = (inlayer->layer_info &
3570 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
3571 outlayer->layer_type = (inlayer->layer_info &
3572 RDSD_LAYER_TYPE_MASK);
3573 outlayer->linear_density = (inlayer->density &
3574 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
3575 outlayer->track_density = (inlayer->density &
3576 RDSD_TRACK_DENSITY_MASK);
3577 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
3579 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
3580 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
3581 outlayer->end_sector_l0 =
3582 scsi_3btoul(inlayer->end_sector_layer0);
3585 case DVD_STRUCT_COPYRIGHT: {
3586 struct scsi_read_dvd_struct_data_copyright *copy_data;
3588 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3591 dvdstruct->cpst = copy_data->cps_type;
3592 dvdstruct->rmi = copy_data->region_info;
3593 dvdstruct->length = 0;
3599 * Tell the user what the overall length is, no matter
3600 * what we can actually fit in the data buffer.
3602 dvdstruct->length = length - ccb->csio.resid -
3603 sizeof(struct scsi_read_dvd_struct_data_header);
3606 * But only actually copy out the smaller of what we read
3607 * in or what the structure can take.
3609 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3611 min(sizeof(dvdstruct->data), dvdstruct->length));
3616 xpt_release_ccb(ccb);
3617 cam_periph_unlock(periph);
3619 if (databuf != NULL)
3620 free(databuf, M_DEVBUF);
3626 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
3627 void (*cbfcnp)(struct cam_periph *, union ccb *),
3628 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
3629 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
3630 u_int8_t sense_len, u_int32_t timeout)
3632 struct scsi_report_key *scsi_cmd;
3634 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
3635 bzero(scsi_cmd, sizeof(*scsi_cmd));
3636 scsi_cmd->opcode = REPORT_KEY;
3637 scsi_ulto4b(lba, scsi_cmd->lba);
3638 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3639 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3640 (key_format & RK_KF_KEYFORMAT_MASK);
3645 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3647 /*data_ptr*/ data_ptr,
3648 /*dxfer_len*/ dxfer_len,
3655 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
3656 void (*cbfcnp)(struct cam_periph *, union ccb *),
3657 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
3658 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3661 struct scsi_send_key *scsi_cmd;
3663 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
3664 bzero(scsi_cmd, sizeof(*scsi_cmd));
3665 scsi_cmd->opcode = SEND_KEY;
3667 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
3668 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3669 (key_format & RK_KF_KEYFORMAT_MASK);
3674 /*flags*/ CAM_DIR_OUT,
3676 /*data_ptr*/ data_ptr,
3677 /*dxfer_len*/ dxfer_len,
3685 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
3686 void (*cbfcnp)(struct cam_periph *, union ccb *),
3687 u_int8_t tag_action, u_int32_t address,
3688 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
3689 u_int8_t *data_ptr, u_int32_t dxfer_len,
3690 u_int8_t sense_len, u_int32_t timeout)
3692 struct scsi_read_dvd_structure *scsi_cmd;
3694 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
3695 bzero(scsi_cmd, sizeof(*scsi_cmd));
3696 scsi_cmd->opcode = READ_DVD_STRUCTURE;
3698 scsi_ulto4b(address, scsi_cmd->address);
3699 scsi_cmd->layer_number = layer_number;
3700 scsi_cmd->format = format;
3701 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3702 /* The AGID is the top two bits of this byte */
3703 scsi_cmd->agid = agid << 6;
3708 /*flags*/ CAM_DIR_IN,
3710 /*data_ptr*/ data_ptr,
3711 /*dxfer_len*/ dxfer_len,