2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
66 #include <geom/geom_disk.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
73 #include <cam/cam_sim.h>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_10_BYTE_ONLY = 0x10,
91 CD_Q_RETRY_BUSY = 0x40
94 #define CD_Q_BIT_STRING \
102 CD_FLAG_INVALID = 0x0001,
103 CD_FLAG_NEW_DISC = 0x0002,
104 CD_FLAG_DISC_LOCKED = 0x0004,
105 CD_FLAG_DISC_REMOVABLE = 0x0008,
106 CD_FLAG_SAW_MEDIA = 0x0010,
107 CD_FLAG_ACTIVE = 0x0080,
108 CD_FLAG_SCHED_ON_COMP = 0x0100,
109 CD_FLAG_RETRY_UA = 0x0200,
110 CD_FLAG_VALID_MEDIA = 0x0400,
111 CD_FLAG_VALID_TOC = 0x0800,
112 CD_FLAG_SCTX_INIT = 0x1000
117 CD_CCB_BUFFER_IO = 0x02,
119 CD_CCB_TYPE_MASK = 0x0F,
120 CD_CCB_RETRY_UA = 0x10
123 #define ccb_state ppriv_field0
124 #define ccb_bp ppriv_ptr1
127 struct ioc_toc_header header;
128 struct cd_toc_entry entries[100];
131 struct cd_toc_single {
132 struct ioc_toc_header header;
133 struct cd_toc_entry entry;
144 volatile cd_flags flags;
145 struct bio_queue_head bio_queue;
146 LIST_HEAD(, ccb_hdr) pending_ccbs;
147 struct cd_params params;
150 struct cam_periph *periph;
151 int minimum_command_size;
152 int outstanding_cmds;
154 struct task sysctl_task;
155 struct sysctl_ctx_list sysctl_ctx;
156 struct sysctl_oid *sysctl_tree;
157 STAILQ_HEAD(, cd_mode_params) mode_queue;
158 struct cd_tocdata toc;
160 struct callout mediapoll_c;
163 struct cd_page_sizes {
168 static struct cd_page_sizes cd_page_size_table[] =
170 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
173 struct cd_quirk_entry {
174 struct scsi_inquiry_pattern inq_pat;
179 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
180 * your device hangs when it gets a 10 byte command. Adding a quirk just
181 * to get rid of the informative diagnostic message is not acceptable. All
182 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
183 * referenced in a comment along with the quirk) , and must be approved by
184 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
185 * be removed until the submitter can explain why they are needed.
186 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
187 * when the CAM_NEW_TRAN_CODE work is done.
189 static struct cd_quirk_entry cd_quirk_table[] =
192 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
193 /* quirks */ CD_Q_BCD_TRACKS
197 * VMware returns BUSY status when storage has transient
198 * connectivity problems, so better wait.
200 {T_CDROM, SIP_MEDIA_REMOVABLE, "NECVMWar", "VMware IDE CDR10", "*"},
201 /*quirks*/ CD_Q_RETRY_BUSY
205 static disk_open_t cdopen;
206 static disk_close_t cdclose;
207 static disk_ioctl_t cdioctl;
208 static disk_strategy_t cdstrategy;
210 static periph_init_t cdinit;
211 static periph_ctor_t cdregister;
212 static periph_dtor_t cdcleanup;
213 static periph_start_t cdstart;
214 static periph_oninv_t cdoninvalidate;
215 static void cdasync(void *callback_arg, u_int32_t code,
216 struct cam_path *path, void *arg);
217 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
218 static int cdrunccb(union ccb *ccb,
219 int (*error_routine)(union ccb *ccb,
221 u_int32_t sense_flags),
222 u_int32_t cam_flags, u_int32_t sense_flags);
223 static void cddone(struct cam_periph *periph,
224 union ccb *start_ccb);
225 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
226 static int cdgetpagesize(int page_num);
227 static void cdprevent(struct cam_periph *periph, int action);
228 static int cdcheckmedia(struct cam_periph *periph);
229 static int cdsize(struct cam_periph *periph, u_int32_t *size);
230 static int cd6byteworkaround(union ccb *ccb);
231 static int cderror(union ccb *ccb, u_int32_t cam_flags,
232 u_int32_t sense_flags);
233 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
234 u_int32_t start, u_int8_t *data,
235 u_int32_t len, u_int32_t sense_flags);
236 static int cdgetmode(struct cam_periph *periph,
237 struct cd_mode_params *data, u_int32_t page);
238 static int cdsetmode(struct cam_periph *periph,
239 struct cd_mode_params *data);
240 static int cdplay(struct cam_periph *periph, u_int32_t blk,
242 static int cdreadsubchannel(struct cam_periph *periph,
243 u_int32_t mode, u_int32_t format,
245 struct cd_sub_channel_info *data,
247 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
248 u_int32_t starts, u_int32_t startf,
249 u_int32_t endm, u_int32_t ends,
251 static int cdplaytracks(struct cam_periph *periph,
252 u_int32_t strack, u_int32_t sindex,
253 u_int32_t etrack, u_int32_t eindex);
254 static int cdpause(struct cam_periph *periph, u_int32_t go);
255 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
256 static int cdstartunit(struct cam_periph *periph, int load);
257 static int cdsetspeed(struct cam_periph *periph,
258 u_int32_t rdspeed, u_int32_t wrspeed);
259 static int cdreportkey(struct cam_periph *periph,
260 struct dvd_authinfo *authinfo);
261 static int cdsendkey(struct cam_periph *periph,
262 struct dvd_authinfo *authinfo);
263 static int cdreaddvdstructure(struct cam_periph *periph,
264 struct dvd_struct *dvdstruct);
265 static timeout_t cdmediapoll;
267 static struct periph_driver cddriver =
270 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
273 PERIPHDRIVER_DECLARE(cd, cddriver);
275 #ifndef CD_DEFAULT_POLL_PERIOD
276 #define CD_DEFAULT_POLL_PERIOD 3
278 #ifndef CD_DEFAULT_RETRY
279 #define CD_DEFAULT_RETRY 4
281 #ifndef CD_DEFAULT_TIMEOUT
282 #define CD_DEFAULT_TIMEOUT 30000
285 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
286 static int cd_retry_count = CD_DEFAULT_RETRY;
287 static int cd_timeout = CD_DEFAULT_TIMEOUT;
289 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
290 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RWTUN,
291 &cd_poll_period, 0, "Media polling period in seconds");
292 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RWTUN,
293 &cd_retry_count, 0, "Normal I/O retry count");
294 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RWTUN,
295 &cd_timeout, 0, "Timeout, in us, for read operations");
297 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
305 * Install a global async callback. This callback will
306 * receive async callbacks like "new device found".
308 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
310 if (status != CAM_REQ_CMP) {
311 printf("cd: Failed to attach master async callback "
312 "due to status 0x%x!\n", status);
317 * Callback from GEOM, called when it has finished cleaning up its
321 cddiskgonecb(struct disk *dp)
323 struct cam_periph *periph;
325 periph = (struct cam_periph *)dp->d_drv1;
326 cam_periph_release(periph);
330 cdoninvalidate(struct cam_periph *periph)
332 struct cd_softc *softc;
334 softc = (struct cd_softc *)periph->softc;
337 * De-register any async callbacks.
339 xpt_register_async(0, cdasync, periph, periph->path);
341 softc->flags |= CD_FLAG_INVALID;
344 * Return all queued I/O with ENXIO.
345 * XXX Handle any transactions queued to the card
346 * with XPT_ABORT_CCB.
348 bioq_flush(&softc->bio_queue, NULL, ENXIO);
350 disk_gone(softc->disk);
354 cdcleanup(struct cam_periph *periph)
356 struct cd_softc *softc;
358 softc = (struct cd_softc *)periph->softc;
360 cam_periph_unlock(periph);
361 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
362 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
363 xpt_print(periph->path, "can't remove sysctl context\n");
366 callout_drain(&softc->mediapoll_c);
367 disk_destroy(softc->disk);
368 free(softc, M_DEVBUF);
369 cam_periph_lock(periph);
373 cdasync(void *callback_arg, u_int32_t code,
374 struct cam_path *path, void *arg)
376 struct cam_periph *periph;
377 struct cd_softc *softc;
379 periph = (struct cam_periph *)callback_arg;
381 case AC_FOUND_DEVICE:
383 struct ccb_getdev *cgd;
386 cgd = (struct ccb_getdev *)arg;
390 if (cgd->protocol != PROTO_SCSI)
392 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
394 if (SID_TYPE(&cgd->inq_data) != T_CDROM
395 && SID_TYPE(&cgd->inq_data) != T_WORM)
399 * Allocate a peripheral instance for
400 * this device and start the probe
403 status = cam_periph_alloc(cdregister, cdoninvalidate,
405 "cd", CAM_PERIPH_BIO,
407 AC_FOUND_DEVICE, cgd);
409 if (status != CAM_REQ_CMP
410 && status != CAM_REQ_INPROG)
411 printf("cdasync: Unable to attach new device "
412 "due to status 0x%x\n", status);
416 case AC_UNIT_ATTENTION:
419 int error_code, sense_key, asc, ascq;
421 softc = (struct cd_softc *)periph->softc;
422 ccb = (union ccb *)arg;
425 * Handle all media change UNIT ATTENTIONs except
426 * our own, as they will be handled by cderror().
428 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
429 scsi_extract_sense_ccb(ccb,
430 &error_code, &sense_key, &asc, &ascq)) {
431 if (asc == 0x28 && ascq == 0x00)
432 disk_media_changed(softc->disk, M_NOWAIT);
434 cam_periph_async(periph, code, path, arg);
438 softc = (struct cd_softc *)periph->softc;
439 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
440 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
442 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
449 struct ccb_hdr *ccbh;
451 softc = (struct cd_softc *)periph->softc;
453 * Don't fail on the expected unit attention
456 softc->flags |= CD_FLAG_RETRY_UA;
457 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
458 ccbh->ccb_state |= CD_CCB_RETRY_UA;
462 cam_periph_async(periph, code, path, arg);
468 cdsysctlinit(void *context, int pending)
470 struct cam_periph *periph;
471 struct cd_softc *softc;
472 char tmpstr[80], tmpstr2[80];
474 periph = (struct cam_periph *)context;
475 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
478 softc = (struct cd_softc *)periph->softc;
479 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
480 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
482 sysctl_ctx_init(&softc->sysctl_ctx);
483 softc->flags |= CD_FLAG_SCTX_INIT;
484 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
485 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
486 tmpstr2, CTLFLAG_RD, 0, tmpstr);
488 if (softc->sysctl_tree == NULL) {
489 printf("cdsysctlinit: unable to allocate sysctl tree\n");
490 cam_periph_release(periph);
495 * Now register the sysctl handler, so the user can the value on
498 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
499 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
500 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
503 cam_periph_release(periph);
507 * We have a handler function for this so we can check the values when the
508 * user sets them, instead of every time we look at them.
511 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
515 value = *(int *)arg1;
517 error = sysctl_handle_int(oidp, &value, 0, req);
520 || (req->newptr == NULL))
524 * The only real values we can have here are 6 or 10. I don't
525 * really forsee having 12 be an option at any time in the future.
526 * So if the user sets something less than or equal to 6, we'll set
527 * it to 6. If he sets something greater than 6, we'll set it to 10.
529 * I suppose we could just return an error here for the wrong values,
530 * but I don't think it's necessary to do so, as long as we can
531 * determine the user's intent without too much trouble.
538 *(int *)arg1 = value;
544 cdregister(struct cam_periph *periph, void *arg)
546 struct cd_softc *softc;
547 struct ccb_pathinq cpi;
548 struct ccb_getdev *cgd;
552 cgd = (struct ccb_getdev *)arg;
554 printf("cdregister: no getdev CCB, can't register device\n");
555 return(CAM_REQ_CMP_ERR);
558 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
561 printf("cdregister: Unable to probe new device. "
562 "Unable to allocate softc\n");
563 return(CAM_REQ_CMP_ERR);
566 LIST_INIT(&softc->pending_ccbs);
567 STAILQ_INIT(&softc->mode_queue);
568 softc->state = CD_STATE_PROBE;
569 bioq_init(&softc->bio_queue);
570 if (SID_IS_REMOVABLE(&cgd->inq_data))
571 softc->flags |= CD_FLAG_DISC_REMOVABLE;
573 periph->softc = softc;
574 softc->periph = periph;
577 * See if this device has any quirks.
579 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
580 (caddr_t)cd_quirk_table,
581 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
582 sizeof(*cd_quirk_table), scsi_inquiry_match);
585 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
587 softc->quirks = CD_Q_NONE;
589 /* Check if the SIM does not want 6 byte commands */
590 bzero(&cpi, sizeof(cpi));
591 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
592 cpi.ccb_h.func_code = XPT_PATH_INQ;
593 xpt_action((union ccb *)&cpi);
594 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
595 softc->quirks |= CD_Q_10_BYTE_ONLY;
597 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
599 /* The default is 6 byte commands, unless quirked otherwise */
600 if (softc->quirks & CD_Q_10_BYTE_ONLY)
601 softc->minimum_command_size = 10;
603 softc->minimum_command_size = 6;
606 * Refcount and block open attempts until we are setup
609 (void)cam_periph_hold(periph, PRIBIO);
610 cam_periph_unlock(periph);
612 * Load the user's default, if any.
614 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
615 periph->unit_number);
616 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
618 /* 6 and 10 are the only permissible values here. */
619 if (softc->minimum_command_size < 6)
620 softc->minimum_command_size = 6;
621 else if (softc->minimum_command_size > 6)
622 softc->minimum_command_size = 10;
625 * We need to register the statistics structure for this device,
626 * but we don't have the blocksize yet for it. So, we register
627 * the structure and indicate that we don't have the blocksize
628 * yet. Unlike other SCSI peripheral drivers, we explicitly set
629 * the device type here to be CDROM, rather than just ORing in
630 * the device type. This is because this driver can attach to either
631 * CDROM or WORM devices, and we want this peripheral driver to
632 * show up in the devstat list as a CD peripheral driver, not a
633 * WORM peripheral driver. WORM drives will also have the WORM
634 * driver attached to them.
636 softc->disk = disk_alloc();
637 softc->disk->d_devstat = devstat_new_entry("cd",
638 periph->unit_number, 0,
639 DEVSTAT_BS_UNAVAILABLE,
641 XPORT_DEVSTAT_TYPE(cpi.transport),
642 DEVSTAT_PRIORITY_CD);
643 softc->disk->d_open = cdopen;
644 softc->disk->d_close = cdclose;
645 softc->disk->d_strategy = cdstrategy;
646 softc->disk->d_gone = cddiskgonecb;
647 softc->disk->d_ioctl = cdioctl;
648 softc->disk->d_name = "cd";
649 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
650 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
651 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
652 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
653 cgd->inq_data.product, sizeof(cgd->inq_data.product),
654 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
655 softc->disk->d_unit = periph->unit_number;
656 softc->disk->d_drv1 = periph;
658 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
659 else if (cpi.maxio > MAXPHYS)
660 softc->disk->d_maxsize = MAXPHYS; /* for safety */
662 softc->disk->d_maxsize = cpi.maxio;
663 softc->disk->d_flags = 0;
664 softc->disk->d_hba_vendor = cpi.hba_vendor;
665 softc->disk->d_hba_device = cpi.hba_device;
666 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
667 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
670 * Acquire a reference to the periph before we register with GEOM.
671 * We'll release this reference once GEOM calls us back (via
672 * dadiskgonecb()) telling us that our provider has been freed.
674 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
675 xpt_print(periph->path, "%s: lost periph during "
676 "registration!\n", __func__);
677 cam_periph_lock(periph);
678 return (CAM_REQ_CMP_ERR);
681 disk_create(softc->disk, DISK_VERSION);
682 cam_periph_lock(periph);
685 * Add an async callback so that we get
686 * notified if this device goes away.
688 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
689 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
692 * Schedule a periodic media polling events.
694 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
695 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
696 (cgd->inq_flags & SID_AEN) == 0 &&
698 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
699 cdmediapoll, periph);
701 xpt_schedule(periph, CAM_PRIORITY_DEV);
706 cdopen(struct disk *dp)
708 struct cam_periph *periph;
709 struct cd_softc *softc;
712 periph = (struct cam_periph *)dp->d_drv1;
713 softc = (struct cd_softc *)periph->softc;
715 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
718 cam_periph_lock(periph);
720 if (softc->flags & CD_FLAG_INVALID) {
721 cam_periph_release_locked(periph);
722 cam_periph_unlock(periph);
726 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
727 cam_periph_release_locked(periph);
728 cam_periph_unlock(periph);
732 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
736 * Check for media, and set the appropriate flags. We don't bail
737 * if we don't have media, but then we don't allow anything but the
738 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
740 cdcheckmedia(periph);
742 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
743 cam_periph_unhold(periph);
745 cam_periph_unlock(periph);
751 cdclose(struct disk *dp)
753 struct cam_periph *periph;
754 struct cd_softc *softc;
756 periph = (struct cam_periph *)dp->d_drv1;
757 softc = (struct cd_softc *)periph->softc;
759 cam_periph_lock(periph);
760 if (cam_periph_hold(periph, PRIBIO) != 0) {
761 cam_periph_unlock(periph);
762 cam_periph_release(periph);
766 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
769 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
770 cdprevent(periph, PR_ALLOW);
773 * Since we're closing this CD, mark the blocksize as unavailable.
774 * It will be marked as available when the CD is opened again.
776 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
779 * We'll check the media and toc again at the next open().
781 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
783 cam_periph_unhold(periph);
784 cam_periph_release_locked(periph);
785 cam_periph_unlock(periph);
791 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
793 u_int32_t sense_flags),
794 u_int32_t cam_flags, u_int32_t sense_flags)
796 struct cd_softc *softc;
797 struct cam_periph *periph;
800 periph = xpt_path_periph(ccb->ccb_h.path);
801 softc = (struct cd_softc *)periph->softc;
803 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
804 softc->disk->d_devstat);
810 * Actually translate the requested transfer into one the physical driver
811 * can understand. The transfer is described by a buf and will include
812 * only one physical transfer.
815 cdstrategy(struct bio *bp)
817 struct cam_periph *periph;
818 struct cd_softc *softc;
820 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
821 cam_periph_lock(periph);
822 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
823 ("cdstrategy(%p)\n", bp));
825 softc = (struct cd_softc *)periph->softc;
828 * If the device has been made invalid, error out
830 if ((softc->flags & CD_FLAG_INVALID)) {
831 cam_periph_unlock(periph);
832 biofinish(bp, NULL, ENXIO);
837 * If we don't have valid media, look for it before trying to
840 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
843 error = cdcheckmedia(periph);
845 cam_periph_unlock(periph);
846 biofinish(bp, NULL, error);
852 * Place it in the queue of disk activities for this disk
854 bioq_disksort(&softc->bio_queue, bp);
856 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
858 cam_periph_unlock(periph);
863 cdstart(struct cam_periph *periph, union ccb *start_ccb)
865 struct cd_softc *softc;
867 struct ccb_scsiio *csio;
868 struct scsi_read_capacity_data *rcap;
870 softc = (struct cd_softc *)periph->softc;
872 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
874 switch (softc->state) {
875 case CD_STATE_NORMAL:
877 bp = bioq_first(&softc->bio_queue);
881 csio = &start_ccb->csio;
882 scsi_test_unit_ready(csio,
883 /*retries*/ cd_retry_count,
888 start_ccb->ccb_h.ccb_bp = NULL;
889 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
890 xpt_action(start_ccb);
892 xpt_release_ccb(start_ccb);
896 cam_periph_release_locked(periph);
898 bioq_remove(&softc->bio_queue, bp);
900 scsi_read_write(&start_ccb->csio,
901 /*retries*/ cd_retry_count,
904 /* read */bp->bio_cmd == BIO_READ ?
905 SCSI_RW_READ : SCSI_RW_WRITE,
907 /* minimum_cmd_size */ 10,
908 /* lba */ bp->bio_offset /
909 softc->params.blksize,
910 bp->bio_bcount / softc->params.blksize,
911 /* data_ptr */ bp->bio_data,
912 /* dxfer_len */ bp->bio_bcount,
913 /* sense_len */ cd_retry_count ?
914 SSD_FULL_SIZE : SF_NO_PRINT,
915 /* timeout */ cd_timeout);
916 /* Use READ CD command for audio tracks. */
917 if (softc->params.blksize == 2352) {
918 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
919 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
920 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
921 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
922 start_ccb->csio.cdb_len = 12;
924 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
927 LIST_INSERT_HEAD(&softc->pending_ccbs,
928 &start_ccb->ccb_h, periph_links.le);
929 softc->outstanding_cmds++;
931 /* We expect a unit attention from this device */
932 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
933 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
934 softc->flags &= ~CD_FLAG_RETRY_UA;
937 start_ccb->ccb_h.ccb_bp = bp;
938 bp = bioq_first(&softc->bio_queue);
940 xpt_action(start_ccb);
942 if (bp != NULL || softc->tur) {
943 /* Have more work to do, so ensure we stay scheduled */
944 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
951 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
952 M_SCSICD, M_NOWAIT | M_ZERO);
954 xpt_print(periph->path,
955 "cdstart: Couldn't malloc read_capacity data\n");
956 /* cd_free_periph??? */
959 csio = &start_ccb->csio;
960 scsi_read_capacity(csio,
961 /*retries*/ cd_retry_count,
967 start_ccb->ccb_h.ccb_bp = NULL;
968 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
969 xpt_action(start_ccb);
976 cddone(struct cam_periph *periph, union ccb *done_ccb)
978 struct cd_softc *softc;
979 struct ccb_scsiio *csio;
981 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
983 softc = (struct cd_softc *)periph->softc;
984 csio = &done_ccb->csio;
986 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
987 case CD_CCB_BUFFER_IO:
992 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
995 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
998 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1003 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1004 if (error == ERESTART) {
1006 * A retry was scheuled, so
1014 xpt_print(periph->path,
1015 "cddone: got error %#x back\n", error);
1016 bioq_flush(&softc->bio_queue, NULL, EIO);
1017 bp->bio_resid = bp->bio_bcount;
1018 bp->bio_error = error;
1019 bp->bio_flags |= BIO_ERROR;
1020 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1021 cam_release_devq(done_ccb->ccb_h.path,
1025 /*getcount_only*/0);
1028 bp->bio_resid = csio->resid;
1030 if (bp->bio_resid != 0) {
1032 * Short transfer ???
1033 * XXX: not sure this is correct for partial
1036 bp->bio_flags |= BIO_ERROR;
1040 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1041 softc->outstanding_cmds--;
1043 biofinish(bp, NULL, 0);
1048 struct scsi_read_capacity_data *rdcap;
1049 char announce_buf[120]; /*
1050 * Currently (9/30/97) the
1051 * longest possible announce
1052 * buffer is 108 bytes, for the
1053 * first error case below.
1054 * That is 39 bytes for the
1055 * basic string, 16 bytes for the
1056 * biggest sense key (hardware
1057 * error), 52 bytes for the
1058 * text of the largest sense
1059 * qualifier valid for a CDROM,
1060 * (0x72, 0x03 or 0x04,
1061 * 0x03), and one byte for the
1062 * null terminating character.
1063 * To allow for longer strings,
1064 * the announce buffer is 120
1067 struct cd_params *cdp;
1070 cdp = &softc->params;
1072 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1074 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1075 cdp->blksize = scsi_4btoul (rdcap->length);
1078 * Retry any UNIT ATTENTION type errors. They
1079 * are expected at boot.
1081 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1082 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1083 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1085 snprintf(announce_buf, sizeof(announce_buf),
1086 "cd present [%lu x %lu byte records]",
1087 cdp->disksize, (u_long)cdp->blksize);
1090 if (error == ERESTART) {
1092 * A retry was scheuled, so
1098 int sense_key, error_code;
1101 struct ccb_getdev cgd;
1103 /* Don't wedge this device's queue */
1104 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1105 cam_release_devq(done_ccb->ccb_h.path,
1109 /*getcount_only*/0);
1111 status = done_ccb->ccb_h.status;
1113 xpt_setup_ccb(&cgd.ccb_h,
1114 done_ccb->ccb_h.path,
1115 CAM_PRIORITY_NORMAL);
1116 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1117 xpt_action((union ccb *)&cgd);
1119 if (scsi_extract_sense_ccb(done_ccb,
1120 &error_code, &sense_key, &asc, &ascq))
1126 * Attach to anything that claims to be a
1127 * CDROM or WORM device, as long as it
1128 * doesn't return a "Logical unit not
1129 * supported" (0x25) error.
1131 if ((have_sense) && (asc != 0x25)
1132 && (error_code == SSD_CURRENT_ERROR)) {
1133 const char *sense_key_desc;
1134 const char *asc_desc;
1136 scsi_sense_desc(sense_key, asc, ascq,
1140 snprintf(announce_buf,
1141 sizeof(announce_buf),
1142 "Attempt to query device "
1143 "size failed: %s, %s",
1146 } else if ((have_sense == 0)
1147 && ((status & CAM_STATUS_MASK) ==
1148 CAM_SCSI_STATUS_ERROR)
1149 && (csio->scsi_status ==
1150 SCSI_STATUS_BUSY)) {
1151 snprintf(announce_buf,
1152 sizeof(announce_buf),
1153 "Attempt to query device "
1154 "size failed: SCSI Status: %s",
1155 scsi_status_string(csio));
1156 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1158 * We only print out an error for
1159 * CDROM type devices. For WORM
1160 * devices, we don't print out an
1161 * error since a few WORM devices
1162 * don't support CDROM commands.
1163 * If we have sense information, go
1164 * ahead and print it out.
1165 * Otherwise, just say that we
1170 * Just print out the error, not
1171 * the full probe message, when we
1178 xpt_print(periph->path,
1179 "got CAM status %#x\n",
1180 done_ccb->ccb_h.status);
1182 xpt_print(periph->path, "fatal error, "
1183 "failed to attach to device\n");
1185 * Invalidate this peripheral.
1187 cam_periph_invalidate(periph);
1189 announce_buf[0] = '\0';
1193 * Invalidate this peripheral.
1195 cam_periph_invalidate(periph);
1196 announce_buf[0] = '\0';
1200 free(rdcap, M_SCSICD);
1201 if (announce_buf[0] != '\0') {
1202 xpt_announce_periph(periph, announce_buf);
1203 xpt_announce_quirks(periph, softc->quirks,
1206 * Create our sysctl variables, now that we know
1207 * we have successfully attached.
1209 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1211 softc->state = CD_STATE_NORMAL;
1213 * Since our peripheral may be invalidated by an error
1214 * above or an external event, we must release our CCB
1215 * before releasing the probe lock on the peripheral.
1216 * The peripheral will only go away once the last lock
1217 * is removed, and we need it around for the CCB release
1220 xpt_release_ccb(done_ccb);
1221 cam_periph_unhold(periph);
1226 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1228 if (cderror(done_ccb, CAM_RETRY_SELTO,
1229 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1232 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1233 cam_release_devq(done_ccb->ccb_h.path,
1237 /*getcount_only*/0);
1239 xpt_release_ccb(done_ccb);
1240 cam_periph_release_locked(periph);
1246 xpt_release_ccb(done_ccb);
1249 static union cd_pages *
1250 cdgetpage(struct cd_mode_params *mode_params)
1252 union cd_pages *page;
1254 if (mode_params->cdb_size == 10)
1255 page = (union cd_pages *)find_mode_page_10(
1256 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1258 page = (union cd_pages *)find_mode_page_6(
1259 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1265 cdgetpagesize(int page_num)
1269 for (i = 0; i < (sizeof(cd_page_size_table)/
1270 sizeof(cd_page_size_table[0])); i++) {
1271 if (cd_page_size_table[i].page == page_num)
1272 return (cd_page_size_table[i].page_size);
1279 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1282 struct cam_periph *periph;
1283 struct cd_softc *softc;
1284 int nocopyout, error = 0;
1286 periph = (struct cam_periph *)dp->d_drv1;
1287 cam_periph_lock(periph);
1289 softc = (struct cd_softc *)periph->softc;
1291 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1292 ("cdioctl(%#lx)\n", cmd));
1294 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1295 cam_periph_unlock(periph);
1296 cam_periph_release(periph);
1301 * If we don't have media loaded, check for it. If still don't
1302 * have media loaded, we can only do a load or eject.
1304 * We only care whether media is loaded if this is a cd-specific ioctl
1305 * (thus the IOCGROUP check below). Note that this will break if
1306 * anyone adds any ioctls into the switch statement below that don't
1307 * have their ioctl group set to 'c'.
1309 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1310 && ((cmd != CDIOCCLOSE)
1311 && (cmd != CDIOCEJECT))
1312 && (IOCGROUP(cmd) == 'c')) {
1313 error = cdcheckmedia(periph);
1315 cam_periph_unhold(periph);
1316 cam_periph_unlock(periph);
1321 * Drop the lock here so later mallocs can use WAITOK. The periph
1322 * is essentially locked still with the cam_periph_hold call above.
1324 cam_periph_unlock(periph);
1329 case CDIOCPLAYTRACKS:
1331 struct ioc_play_track *args
1332 = (struct ioc_play_track *) addr;
1333 struct cd_mode_params params;
1334 union cd_pages *page;
1336 params.alloc_len = sizeof(union cd_mode_data_6_10);
1337 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1340 cam_periph_lock(periph);
1341 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1342 ("trying to do CDIOCPLAYTRACKS\n"));
1344 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1346 free(params.mode_buf, M_SCSICD);
1347 cam_periph_unlock(periph);
1350 page = cdgetpage(¶ms);
1352 page->audio.flags &= ~CD_PA_SOTC;
1353 page->audio.flags |= CD_PA_IMMED;
1354 error = cdsetmode(periph, ¶ms);
1355 free(params.mode_buf, M_SCSICD);
1357 cam_periph_unlock(periph);
1362 * This was originally implemented with the PLAY
1363 * AUDIO TRACK INDEX command, but that command was
1364 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1365 * drives support it but ATAPI and ATAPI-derivative
1366 * drives don't seem to support it. So we keep a
1367 * cache of the table of contents and translate
1368 * track numbers to MSF format.
1370 if (softc->flags & CD_FLAG_VALID_TOC) {
1371 union msf_lba *sentry, *eentry;
1374 if (args->end_track <
1375 softc->toc.header.ending_track + 1)
1377 if (args->end_track >
1378 softc->toc.header.ending_track + 1)
1380 softc->toc.header.ending_track + 1;
1381 st = args->start_track -
1382 softc->toc.header.starting_track;
1383 et = args->end_track -
1384 softc->toc.header.starting_track;
1387 || (st > (softc->toc.header.ending_track -
1388 softc->toc.header.starting_track))) {
1390 cam_periph_unlock(periph);
1393 sentry = &softc->toc.entries[st].addr;
1394 eentry = &softc->toc.entries[et].addr;
1395 error = cdplaymsf(periph,
1404 * If we don't have a valid TOC, try the
1405 * play track index command. It is part of
1406 * the SCSI-2 spec, but was removed in the
1407 * MMC specs. ATAPI and ATAPI-derived
1408 * drives don't support it.
1410 if (softc->quirks & CD_Q_BCD_TRACKS) {
1412 bin2bcd(args->start_track);
1414 bin2bcd(args->end_track);
1416 error = cdplaytracks(periph,
1422 cam_periph_unlock(periph);
1427 struct ioc_play_msf *args
1428 = (struct ioc_play_msf *) addr;
1429 struct cd_mode_params params;
1430 union cd_pages *page;
1432 params.alloc_len = sizeof(union cd_mode_data_6_10);
1433 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1436 cam_periph_lock(periph);
1437 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1438 ("trying to do CDIOCPLAYMSF\n"));
1440 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1442 free(params.mode_buf, M_SCSICD);
1443 cam_periph_unlock(periph);
1446 page = cdgetpage(¶ms);
1448 page->audio.flags &= ~CD_PA_SOTC;
1449 page->audio.flags |= CD_PA_IMMED;
1450 error = cdsetmode(periph, ¶ms);
1451 free(params.mode_buf, M_SCSICD);
1453 cam_periph_unlock(periph);
1456 error = cdplaymsf(periph,
1463 cam_periph_unlock(periph);
1466 case CDIOCPLAYBLOCKS:
1468 struct ioc_play_blocks *args
1469 = (struct ioc_play_blocks *) addr;
1470 struct cd_mode_params params;
1471 union cd_pages *page;
1473 params.alloc_len = sizeof(union cd_mode_data_6_10);
1474 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1477 cam_periph_lock(periph);
1478 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1479 ("trying to do CDIOCPLAYBLOCKS\n"));
1482 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1484 free(params.mode_buf, M_SCSICD);
1485 cam_periph_unlock(periph);
1488 page = cdgetpage(¶ms);
1490 page->audio.flags &= ~CD_PA_SOTC;
1491 page->audio.flags |= CD_PA_IMMED;
1492 error = cdsetmode(periph, ¶ms);
1493 free(params.mode_buf, M_SCSICD);
1495 cam_periph_unlock(periph);
1498 error = cdplay(periph, args->blk, args->len);
1499 cam_periph_unlock(periph);
1502 case CDIOCREADSUBCHANNEL_SYSSPACE:
1505 case CDIOCREADSUBCHANNEL:
1507 struct ioc_read_subchannel *args
1508 = (struct ioc_read_subchannel *) addr;
1509 struct cd_sub_channel_info *data;
1510 u_int32_t len = args->data_len;
1512 data = malloc(sizeof(struct cd_sub_channel_info),
1513 M_SCSICD, M_WAITOK | M_ZERO);
1515 cam_periph_lock(periph);
1516 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1517 ("trying to do CDIOCREADSUBCHANNEL\n"));
1519 if ((len > sizeof(struct cd_sub_channel_info)) ||
1520 (len < sizeof(struct cd_sub_channel_header))) {
1522 "scsi_cd: cdioctl: "
1523 "cdioreadsubchannel: error, len=%d\n",
1526 free(data, M_SCSICD);
1527 cam_periph_unlock(periph);
1531 if (softc->quirks & CD_Q_BCD_TRACKS)
1532 args->track = bin2bcd(args->track);
1534 error = cdreadsubchannel(periph, args->address_format,
1535 args->data_format, args->track, data, len);
1538 free(data, M_SCSICD);
1539 cam_periph_unlock(periph);
1542 if (softc->quirks & CD_Q_BCD_TRACKS)
1543 data->what.track_info.track_number =
1544 bcd2bin(data->what.track_info.track_number);
1545 len = min(len, ((data->header.data_len[0] << 8) +
1546 data->header.data_len[1] +
1547 sizeof(struct cd_sub_channel_header)));
1548 cam_periph_unlock(periph);
1549 if (nocopyout == 0) {
1550 if (copyout(data, args->data, len) != 0) {
1554 bcopy(data, args->data, len);
1556 free(data, M_SCSICD);
1560 case CDIOREADTOCHEADER:
1562 struct ioc_toc_header *th;
1564 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
1567 cam_periph_lock(periph);
1568 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1569 ("trying to do CDIOREADTOCHEADER\n"));
1571 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1572 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
1575 cam_periph_unlock(periph);
1578 if (softc->quirks & CD_Q_BCD_TRACKS) {
1579 /* we are going to have to convert the BCD
1580 * encoding on the cd to what is expected
1582 th->starting_track =
1583 bcd2bin(th->starting_track);
1584 th->ending_track = bcd2bin(th->ending_track);
1586 th->len = ntohs(th->len);
1587 bcopy(th, addr, sizeof(*th));
1589 cam_periph_unlock(periph);
1592 case CDIOREADTOCENTRYS:
1594 struct cd_tocdata *data;
1595 struct cd_toc_single *lead;
1596 struct ioc_read_toc_entry *te =
1597 (struct ioc_read_toc_entry *) addr;
1598 struct ioc_toc_header *th;
1599 u_int32_t len, readlen, idx, num;
1600 u_int32_t starting_track = te->starting_track;
1602 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
1603 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
1605 cam_periph_lock(periph);
1606 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1607 ("trying to do CDIOREADTOCENTRYS\n"));
1609 if (te->data_len < sizeof(struct cd_toc_entry)
1610 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
1611 || (te->address_format != CD_MSF_FORMAT
1612 && te->address_format != CD_LBA_FORMAT)) {
1614 printf("scsi_cd: error in readtocentries, "
1615 "returning EINVAL\n");
1616 free(data, M_SCSICD);
1617 free(lead, M_SCSICD);
1618 cam_periph_unlock(periph);
1623 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1624 sizeof (*th), /*sense_flags*/0);
1626 free(data, M_SCSICD);
1627 free(lead, M_SCSICD);
1628 cam_periph_unlock(periph);
1632 if (softc->quirks & CD_Q_BCD_TRACKS) {
1633 /* we are going to have to convert the BCD
1634 * encoding on the cd to what is expected
1636 th->starting_track =
1637 bcd2bin(th->starting_track);
1638 th->ending_track = bcd2bin(th->ending_track);
1641 if (starting_track == 0)
1642 starting_track = th->starting_track;
1643 else if (starting_track == LEADOUT)
1644 starting_track = th->ending_track + 1;
1645 else if (starting_track < th->starting_track ||
1646 starting_track > th->ending_track + 1) {
1647 printf("scsi_cd: error in readtocentries, "
1648 "returning EINVAL\n");
1649 free(data, M_SCSICD);
1650 free(lead, M_SCSICD);
1651 cam_periph_unlock(periph);
1656 /* calculate reading length without leadout entry */
1657 readlen = (th->ending_track - starting_track + 1) *
1658 sizeof(struct cd_toc_entry);
1660 /* and with leadout entry */
1661 len = readlen + sizeof(struct cd_toc_entry);
1662 if (te->data_len < len) {
1667 if (len > sizeof(data->entries)) {
1668 printf("scsi_cd: error in readtocentries, "
1669 "returning EINVAL\n");
1671 free(data, M_SCSICD);
1672 free(lead, M_SCSICD);
1673 cam_periph_unlock(periph);
1676 num = len / sizeof(struct cd_toc_entry);
1679 error = cdreadtoc(periph, te->address_format,
1682 readlen + sizeof (*th),
1685 free(data, M_SCSICD);
1686 free(lead, M_SCSICD);
1687 cam_periph_unlock(periph);
1692 /* make leadout entry if needed */
1693 idx = starting_track + num - 1;
1694 if (softc->quirks & CD_Q_BCD_TRACKS)
1695 th->ending_track = bcd2bin(th->ending_track);
1696 if (idx == th->ending_track + 1) {
1697 error = cdreadtoc(periph, te->address_format,
1698 LEADOUT, (u_int8_t *)lead,
1702 free(data, M_SCSICD);
1703 free(lead, M_SCSICD);
1704 cam_periph_unlock(periph);
1707 data->entries[idx - starting_track] =
1710 if (softc->quirks & CD_Q_BCD_TRACKS) {
1711 for (idx = 0; idx < num - 1; idx++) {
1712 data->entries[idx].track =
1713 bcd2bin(data->entries[idx].track);
1717 cam_periph_unlock(periph);
1718 error = copyout(data->entries, te->data, len);
1719 free(data, M_SCSICD);
1720 free(lead, M_SCSICD);
1723 case CDIOREADTOCENTRY:
1725 struct cd_toc_single *data;
1726 struct ioc_read_toc_single_entry *te =
1727 (struct ioc_read_toc_single_entry *) addr;
1728 struct ioc_toc_header *th;
1731 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
1733 cam_periph_lock(periph);
1734 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1735 ("trying to do CDIOREADTOCENTRY\n"));
1737 if (te->address_format != CD_MSF_FORMAT
1738 && te->address_format != CD_LBA_FORMAT) {
1739 printf("error in readtocentry, "
1740 " returning EINVAL\n");
1741 free(data, M_SCSICD);
1743 cam_periph_unlock(periph);
1748 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
1749 sizeof (*th), /*sense_flags*/0);
1751 free(data, M_SCSICD);
1752 cam_periph_unlock(periph);
1756 if (softc->quirks & CD_Q_BCD_TRACKS) {
1757 /* we are going to have to convert the BCD
1758 * encoding on the cd to what is expected
1760 th->starting_track =
1761 bcd2bin(th->starting_track);
1762 th->ending_track = bcd2bin(th->ending_track);
1766 track = th->starting_track;
1767 else if (track == LEADOUT)
1769 else if (track < th->starting_track ||
1770 track > th->ending_track + 1) {
1771 printf("error in readtocentry, "
1772 " returning EINVAL\n");
1773 free(data, M_SCSICD);
1775 cam_periph_unlock(periph);
1779 error = cdreadtoc(periph, te->address_format, track,
1780 (u_int8_t *)data, sizeof(*data),
1783 free(data, M_SCSICD);
1784 cam_periph_unlock(periph);
1788 if (softc->quirks & CD_Q_BCD_TRACKS)
1789 data->entry.track = bcd2bin(data->entry.track);
1790 bcopy(&data->entry, &te->entry,
1791 sizeof(struct cd_toc_entry));
1792 free(data, M_SCSICD);
1793 cam_periph_unlock(periph);
1798 struct ioc_patch *arg = (struct ioc_patch *)addr;
1799 struct cd_mode_params params;
1800 union cd_pages *page;
1802 params.alloc_len = sizeof(union cd_mode_data_6_10);
1803 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1806 cam_periph_lock(periph);
1807 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1808 ("trying to do CDIOCSETPATCH\n"));
1810 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1812 free(params.mode_buf, M_SCSICD);
1813 cam_periph_unlock(periph);
1816 page = cdgetpage(¶ms);
1818 page->audio.port[LEFT_PORT].channels =
1820 page->audio.port[RIGHT_PORT].channels =
1822 page->audio.port[2].channels = arg->patch[2];
1823 page->audio.port[3].channels = arg->patch[3];
1824 error = cdsetmode(periph, ¶ms);
1825 free(params.mode_buf, M_SCSICD);
1826 cam_periph_unlock(periph);
1831 struct ioc_vol *arg = (struct ioc_vol *) addr;
1832 struct cd_mode_params params;
1833 union cd_pages *page;
1835 params.alloc_len = sizeof(union cd_mode_data_6_10);
1836 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1839 cam_periph_lock(periph);
1840 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1841 ("trying to do CDIOCGETVOL\n"));
1843 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1845 free(params.mode_buf, M_SCSICD);
1846 cam_periph_unlock(periph);
1849 page = cdgetpage(¶ms);
1851 arg->vol[LEFT_PORT] =
1852 page->audio.port[LEFT_PORT].volume;
1853 arg->vol[RIGHT_PORT] =
1854 page->audio.port[RIGHT_PORT].volume;
1855 arg->vol[2] = page->audio.port[2].volume;
1856 arg->vol[3] = page->audio.port[3].volume;
1857 free(params.mode_buf, M_SCSICD);
1858 cam_periph_unlock(periph);
1863 struct ioc_vol *arg = (struct ioc_vol *) addr;
1864 struct cd_mode_params params;
1865 union cd_pages *page;
1867 params.alloc_len = sizeof(union cd_mode_data_6_10);
1868 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1871 cam_periph_lock(periph);
1872 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1873 ("trying to do CDIOCSETVOL\n"));
1875 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1877 free(params.mode_buf, M_SCSICD);
1878 cam_periph_unlock(periph);
1881 page = cdgetpage(¶ms);
1883 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
1884 page->audio.port[LEFT_PORT].volume =
1885 arg->vol[LEFT_PORT];
1886 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
1887 page->audio.port[RIGHT_PORT].volume =
1888 arg->vol[RIGHT_PORT];
1889 page->audio.port[2].volume = arg->vol[2];
1890 page->audio.port[3].volume = arg->vol[3];
1891 error = cdsetmode(periph, ¶ms);
1892 cam_periph_unlock(periph);
1893 free(params.mode_buf, M_SCSICD);
1898 struct cd_mode_params params;
1899 union cd_pages *page;
1901 params.alloc_len = sizeof(union cd_mode_data_6_10);
1902 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1905 cam_periph_lock(periph);
1906 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1907 ("trying to do CDIOCSETMONO\n"));
1909 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1911 free(params.mode_buf, M_SCSICD);
1912 cam_periph_unlock(periph);
1915 page = cdgetpage(¶ms);
1917 page->audio.port[LEFT_PORT].channels =
1918 LEFT_CHANNEL | RIGHT_CHANNEL;
1919 page->audio.port[RIGHT_PORT].channels =
1920 LEFT_CHANNEL | RIGHT_CHANNEL;
1921 page->audio.port[2].channels = 0;
1922 page->audio.port[3].channels = 0;
1923 error = cdsetmode(periph, ¶ms);
1924 cam_periph_unlock(periph);
1925 free(params.mode_buf, M_SCSICD);
1928 case CDIOCSETSTEREO:
1930 struct cd_mode_params params;
1931 union cd_pages *page;
1933 params.alloc_len = sizeof(union cd_mode_data_6_10);
1934 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1937 cam_periph_lock(periph);
1938 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1939 ("trying to do CDIOCSETSTEREO\n"));
1941 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1943 free(params.mode_buf, M_SCSICD);
1944 cam_periph_unlock(periph);
1947 page = cdgetpage(¶ms);
1949 page->audio.port[LEFT_PORT].channels =
1951 page->audio.port[RIGHT_PORT].channels =
1953 page->audio.port[2].channels = 0;
1954 page->audio.port[3].channels = 0;
1955 error = cdsetmode(periph, ¶ms);
1956 free(params.mode_buf, M_SCSICD);
1957 cam_periph_unlock(periph);
1962 struct cd_mode_params params;
1963 union cd_pages *page;
1965 params.alloc_len = sizeof(union cd_mode_data_6_10);
1966 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1969 cam_periph_lock(periph);
1970 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1971 ("trying to do CDIOCSETMUTE\n"));
1973 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1975 free(params.mode_buf, M_SCSICD);
1976 cam_periph_unlock(periph);
1979 page = cdgetpage(¶ms);
1981 page->audio.port[LEFT_PORT].channels = 0;
1982 page->audio.port[RIGHT_PORT].channels = 0;
1983 page->audio.port[2].channels = 0;
1984 page->audio.port[3].channels = 0;
1985 error = cdsetmode(periph, ¶ms);
1986 free(params.mode_buf, M_SCSICD);
1987 cam_periph_unlock(periph);
1992 struct cd_mode_params params;
1993 union cd_pages *page;
1995 params.alloc_len = sizeof(union cd_mode_data_6_10);
1996 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1999 cam_periph_lock(periph);
2000 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2001 ("trying to do CDIOCSETLEFT\n"));
2003 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2005 free(params.mode_buf, M_SCSICD);
2006 cam_periph_unlock(periph);
2009 page = cdgetpage(¶ms);
2011 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2012 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2013 page->audio.port[2].channels = 0;
2014 page->audio.port[3].channels = 0;
2015 error = cdsetmode(periph, ¶ms);
2016 free(params.mode_buf, M_SCSICD);
2017 cam_periph_unlock(periph);
2022 struct cd_mode_params params;
2023 union cd_pages *page;
2025 params.alloc_len = sizeof(union cd_mode_data_6_10);
2026 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2029 cam_periph_lock(periph);
2030 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2031 ("trying to do CDIOCSETRIGHT\n"));
2033 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2035 free(params.mode_buf, M_SCSICD);
2036 cam_periph_unlock(periph);
2039 page = cdgetpage(¶ms);
2041 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2042 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2043 page->audio.port[2].channels = 0;
2044 page->audio.port[3].channels = 0;
2045 error = cdsetmode(periph, ¶ms);
2046 free(params.mode_buf, M_SCSICD);
2047 cam_periph_unlock(periph);
2051 cam_periph_lock(periph);
2052 error = cdpause(periph, 1);
2053 cam_periph_unlock(periph);
2056 cam_periph_lock(periph);
2057 error = cdpause(periph, 0);
2058 cam_periph_unlock(periph);
2061 cam_periph_lock(periph);
2062 error = cdstartunit(periph, 0);
2063 cam_periph_unlock(periph);
2066 cam_periph_lock(periph);
2067 error = cdstartunit(periph, 1);
2068 cam_periph_unlock(periph);
2071 cam_periph_lock(periph);
2072 error = cdstopunit(periph, 0);
2073 cam_periph_unlock(periph);
2076 cam_periph_lock(periph);
2077 error = cdstopunit(periph, 1);
2078 cam_periph_unlock(periph);
2081 cam_periph_lock(periph);
2082 cdprevent(periph, PR_ALLOW);
2083 cam_periph_unlock(periph);
2086 cam_periph_lock(periph);
2087 cdprevent(periph, PR_PREVENT);
2088 cam_periph_unlock(periph);
2091 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2095 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2099 /* return (cd_reset(periph)); */
2102 case CDRIOCREADSPEED:
2103 cam_periph_lock(periph);
2104 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2105 cam_periph_unlock(periph);
2107 case CDRIOCWRITESPEED:
2108 cam_periph_lock(periph);
2109 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2110 cam_periph_unlock(periph);
2112 case CDRIOCGETBLOCKSIZE:
2113 *(int *)addr = softc->params.blksize;
2115 case CDRIOCSETBLOCKSIZE:
2116 if (*(int *)addr <= 0) {
2120 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2123 case DVDIOCREPORTKEY: {
2124 struct dvd_authinfo *authinfo;
2126 authinfo = (struct dvd_authinfo *)addr;
2128 if (cmd == DVDIOCREPORTKEY)
2129 error = cdreportkey(periph, authinfo);
2131 error = cdsendkey(periph, authinfo);
2134 case DVDIOCREADSTRUCTURE: {
2135 struct dvd_struct *dvdstruct;
2137 dvdstruct = (struct dvd_struct *)addr;
2139 error = cdreaddvdstructure(periph, dvdstruct);
2144 cam_periph_lock(periph);
2145 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2146 cam_periph_unlock(periph);
2150 cam_periph_lock(periph);
2151 cam_periph_unhold(periph);
2153 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2154 if (error && bootverbose) {
2155 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2157 cam_periph_unlock(periph);
2163 cdprevent(struct cam_periph *periph, int action)
2166 struct cd_softc *softc;
2169 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2171 softc = (struct cd_softc *)periph->softc;
2173 if (((action == PR_ALLOW)
2174 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2175 || ((action == PR_PREVENT)
2176 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2180 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2182 scsi_prevent(&ccb->csio,
2183 /*retries*/ cd_retry_count,
2188 /* timeout */60000);
2190 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2191 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2193 xpt_release_ccb(ccb);
2196 if (action == PR_ALLOW)
2197 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2199 softc->flags |= CD_FLAG_DISC_LOCKED;
2204 * XXX: the disk media and sector size is only really able to change
2205 * XXX: while the device is closed.
2208 cdcheckmedia(struct cam_periph *periph)
2210 struct cd_softc *softc;
2211 struct ioc_toc_header *toch;
2212 struct cd_toc_single leadout;
2213 u_int32_t size, toclen;
2214 int error, num_entries, cdindex;
2216 softc = (struct cd_softc *)periph->softc;
2218 cdprevent(periph, PR_PREVENT);
2219 softc->disk->d_sectorsize = 2048;
2220 softc->disk->d_mediasize = 0;
2223 * Get the disc size and block size. If we can't get it, we don't
2224 * have media, most likely.
2226 if ((error = cdsize(periph, &size)) != 0) {
2227 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2228 cdprevent(periph, PR_ALLOW);
2231 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2232 softc->disk->d_sectorsize = softc->params.blksize;
2233 softc->disk->d_mediasize =
2234 (off_t)softc->params.blksize * softc->params.disksize;
2238 * Now we check the table of contents. This (currently) is only
2239 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2240 * things like present a separate entry in /dev for each track,
2241 * like that acd(4) driver does.
2243 bzero(&softc->toc, sizeof(softc->toc));
2244 toch = &softc->toc.header;
2246 * We will get errors here for media that doesn't have a table of
2247 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2248 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2249 * has not been recorded (no complete session) and the Format codes
2250 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2251 * with an INVALID FIELD IN CDB. Devices that are not capable of
2252 * reading an incomplete session on DDC/CD-R/RW media shall report
2253 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2255 * So this isn't fatal if we can't read the table of contents, it
2256 * just means that the user won't be able to issue the play tracks
2257 * ioctl, and likely lots of other stuff won't work either. They
2258 * need to burn the CD before we can do a whole lot with it. So
2259 * we don't print anything here if we get an error back.
2261 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2264 * Errors in reading the table of contents aren't fatal, we just
2265 * won't have a valid table of contents cached.
2269 bzero(&softc->toc, sizeof(softc->toc));
2273 if (softc->quirks & CD_Q_BCD_TRACKS) {
2274 toch->starting_track = bcd2bin(toch->starting_track);
2275 toch->ending_track = bcd2bin(toch->ending_track);
2278 /* Number of TOC entries, plus leadout */
2279 num_entries = (toch->ending_track - toch->starting_track) + 2;
2281 if (num_entries <= 0)
2284 toclen = num_entries * sizeof(struct cd_toc_entry);
2286 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2287 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2291 bzero(&softc->toc, sizeof(softc->toc));
2295 if (softc->quirks & CD_Q_BCD_TRACKS) {
2296 toch->starting_track = bcd2bin(toch->starting_track);
2297 toch->ending_track = bcd2bin(toch->ending_track);
2300 * XXX KDM is this necessary? Probably only if the drive doesn't
2301 * return leadout information with the table of contents.
2303 cdindex = toch->starting_track + num_entries -1;
2304 if (cdindex == toch->ending_track + 1) {
2306 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2307 (u_int8_t *)&leadout, sizeof(leadout),
2313 softc->toc.entries[cdindex - toch->starting_track] =
2316 if (softc->quirks & CD_Q_BCD_TRACKS) {
2317 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2318 softc->toc.entries[cdindex].track =
2319 bcd2bin(softc->toc.entries[cdindex].track);
2323 softc->flags |= CD_FLAG_VALID_TOC;
2325 /* If the first track is audio, correct sector size. */
2326 if ((softc->toc.entries[0].control & 4) == 0) {
2327 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2328 softc->disk->d_mediasize =
2329 (off_t)softc->params.blksize * softc->params.disksize;
2335 * We unconditionally (re)set the blocksize each time the
2336 * CD device is opened. This is because the CD can change,
2337 * and therefore the blocksize might change.
2338 * XXX problems here if some slice or partition is still
2339 * open with the old size?
2341 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2342 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2343 softc->disk->d_devstat->block_size = softc->params.blksize;
2349 cdsize(struct cam_periph *periph, u_int32_t *size)
2351 struct cd_softc *softc;
2353 struct scsi_read_capacity_data *rcap_buf;
2356 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2358 softc = (struct cd_softc *)periph->softc;
2360 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2362 /* XXX Should be M_WAITOK */
2363 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2364 M_SCSICD, M_NOWAIT | M_ZERO);
2365 if (rcap_buf == NULL)
2368 scsi_read_capacity(&ccb->csio,
2369 /*retries*/ cd_retry_count,
2374 /* timeout */20000);
2376 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2377 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2379 xpt_release_ccb(ccb);
2381 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2382 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2383 /* Make sure we got at least some block size. */
2384 if (error == 0 && softc->params.blksize == 0)
2387 * SCSI-3 mandates that the reported blocksize shall be 2048.
2388 * Older drives sometimes report funny values, trim it down to
2389 * 2048, or other parts of the kernel will get confused.
2391 * XXX we leave drives alone that might report 512 bytes, as
2392 * well as drives reporting more weird sizes like perhaps 4K.
2394 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2395 softc->params.blksize = 2048;
2397 free(rcap_buf, M_SCSICD);
2398 *size = softc->params.disksize;
2405 cd6byteworkaround(union ccb *ccb)
2408 struct cam_periph *periph;
2409 struct cd_softc *softc;
2410 struct cd_mode_params *params;
2413 periph = xpt_path_periph(ccb->ccb_h.path);
2414 softc = (struct cd_softc *)periph->softc;
2416 cdb = ccb->csio.cdb_io.cdb_bytes;
2418 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2419 || ((cdb[0] != MODE_SENSE_6)
2420 && (cdb[0] != MODE_SELECT_6)))
2424 * Because there is no convenient place to stash the overall
2425 * cd_mode_params structure pointer, we have to grab it like this.
2426 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2427 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2429 * XXX It would be nice if, at some point, we could increase the
2430 * number of available peripheral private pointers. Both pointers
2431 * are currently used in most every peripheral driver.
2435 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2436 if (params->mode_buf == ccb->csio.data_ptr) {
2443 * This shouldn't happen. All mode sense and mode select
2444 * operations in the cd(4) driver MUST go through cdgetmode() and
2448 xpt_print(periph->path,
2449 "mode buffer not found in mode queue!\n");
2453 params->cdb_size = 10;
2454 softc->minimum_command_size = 10;
2455 xpt_print(ccb->ccb_h.path,
2456 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2457 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2459 if (cdb[0] == MODE_SENSE_6) {
2460 struct scsi_mode_sense_10 ms10;
2461 struct scsi_mode_sense_6 *ms6;
2464 ms6 = (struct scsi_mode_sense_6 *)cdb;
2466 bzero(&ms10, sizeof(ms10));
2467 ms10.opcode = MODE_SENSE_10;
2468 ms10.byte2 = ms6->byte2;
2469 ms10.page = ms6->page;
2472 * 10 byte mode header, block descriptor,
2473 * sizeof(union cd_pages)
2475 len = sizeof(struct cd_mode_data_10);
2476 ccb->csio.dxfer_len = len;
2478 scsi_ulto2b(len, ms10.length);
2479 ms10.control = ms6->control;
2480 bcopy(&ms10, cdb, 10);
2481 ccb->csio.cdb_len = 10;
2483 struct scsi_mode_select_10 ms10;
2484 struct scsi_mode_select_6 *ms6;
2485 struct scsi_mode_header_6 *header6;
2486 struct scsi_mode_header_10 *header10;
2487 struct scsi_mode_page_header *page_header;
2488 int blk_desc_len, page_num, page_size, len;
2490 ms6 = (struct scsi_mode_select_6 *)cdb;
2492 bzero(&ms10, sizeof(ms10));
2493 ms10.opcode = MODE_SELECT_10;
2494 ms10.byte2 = ms6->byte2;
2496 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
2497 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
2499 page_header = find_mode_page_6(header6);
2500 page_num = page_header->page_code;
2502 blk_desc_len = header6->blk_desc_len;
2504 page_size = cdgetpagesize(page_num);
2506 if (page_size != (page_header->page_length +
2507 sizeof(*page_header)))
2508 page_size = page_header->page_length +
2509 sizeof(*page_header);
2511 len = sizeof(*header10) + blk_desc_len + page_size;
2513 len = min(params->alloc_len, len);
2516 * Since the 6 byte parameter header is shorter than the 10
2517 * byte parameter header, we need to copy the actual mode
2518 * page data, and the block descriptor, if any, so things wind
2519 * up in the right place. The regions will overlap, but
2520 * bcopy() does the right thing.
2522 bcopy(params->mode_buf + sizeof(*header6),
2523 params->mode_buf + sizeof(*header10),
2524 len - sizeof(*header10));
2526 /* Make sure these fields are set correctly. */
2527 scsi_ulto2b(0, header10->data_length);
2528 header10->medium_type = 0;
2529 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
2531 ccb->csio.dxfer_len = len;
2533 scsi_ulto2b(len, ms10.length);
2534 ms10.control = ms6->control;
2535 bcopy(&ms10, cdb, 10);
2536 ccb->csio.cdb_len = 10;
2539 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2540 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2543 cam_release_devq(ccb->ccb_h.path,
2547 /*getcount_only*/0);
2554 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2556 struct cd_softc *softc;
2557 struct cam_periph *periph;
2558 int error, error_code, sense_key, asc, ascq;
2560 periph = xpt_path_periph(ccb->ccb_h.path);
2561 softc = (struct cd_softc *)periph->softc;
2566 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
2567 * CDB comes back with this particular error, try transforming it
2568 * into the 10 byte version.
2570 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2571 error = cd6byteworkaround(ccb);
2572 } else if (scsi_extract_sense_ccb(ccb,
2573 &error_code, &sense_key, &asc, &ascq)) {
2574 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2575 error = cd6byteworkaround(ccb);
2576 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2577 asc == 0x28 && ascq == 0x00)
2578 disk_media_changed(softc->disk, M_NOWAIT);
2579 else if (sense_key == SSD_KEY_NOT_READY &&
2580 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
2581 softc->flags &= ~CD_FLAG_SAW_MEDIA;
2582 disk_media_gone(softc->disk, M_NOWAIT);
2586 if (error == ERESTART)
2591 * Until we have a better way of doing pack validation,
2592 * don't treat UAs as errors.
2594 sense_flags |= SF_RETRY_UA;
2596 if (softc->quirks & CD_Q_RETRY_BUSY)
2597 sense_flags |= SF_RETRY_BUSY;
2598 return (cam_periph_error(ccb, cam_flags, sense_flags,
2599 &softc->saved_ccb));
2603 cdmediapoll(void *arg)
2605 struct cam_periph *periph = arg;
2606 struct cd_softc *softc = periph->softc;
2608 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
2609 softc->outstanding_cmds == 0) {
2610 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2612 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2615 /* Queue us up again */
2616 if (cd_poll_period != 0)
2617 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
2621 * Read table of contents
2624 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
2625 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
2627 struct scsi_read_toc *scsi_cmd;
2629 struct ccb_scsiio *csio;
2636 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2641 /* retries */ cd_retry_count,
2642 /* cbfcnp */ cddone,
2643 /* flags */ CAM_DIR_IN,
2644 /* tag_action */ MSG_SIMPLE_Q_TAG,
2645 /* data_ptr */ data,
2646 /* dxfer_len */ len,
2647 /* sense_len */ SSD_FULL_SIZE,
2648 sizeof(struct scsi_read_toc),
2649 /* timeout */ 50000);
2651 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
2652 bzero (scsi_cmd, sizeof(*scsi_cmd));
2654 if (mode == CD_MSF_FORMAT)
2655 scsi_cmd->byte2 |= CD_MSF;
2656 scsi_cmd->from_track = start;
2657 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
2658 scsi_cmd->data_len[0] = (ntoc) >> 8;
2659 scsi_cmd->data_len[1] = (ntoc) & 0xff;
2661 scsi_cmd->op_code = READ_TOC;
2663 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2664 /*sense_flags*/SF_RETRY_UA | sense_flags);
2666 xpt_release_ccb(ccb);
2672 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
2673 u_int32_t format, int track,
2674 struct cd_sub_channel_info *data, u_int32_t len)
2676 struct scsi_read_subchannel *scsi_cmd;
2677 struct ccb_scsiio *csio;
2683 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2688 /* retries */ cd_retry_count,
2689 /* cbfcnp */ cddone,
2690 /* flags */ CAM_DIR_IN,
2691 /* tag_action */ MSG_SIMPLE_Q_TAG,
2692 /* data_ptr */ (u_int8_t *)data,
2693 /* dxfer_len */ len,
2694 /* sense_len */ SSD_FULL_SIZE,
2695 sizeof(struct scsi_read_subchannel),
2696 /* timeout */ 50000);
2698 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
2699 bzero (scsi_cmd, sizeof(*scsi_cmd));
2701 scsi_cmd->op_code = READ_SUBCHANNEL;
2702 if (mode == CD_MSF_FORMAT)
2703 scsi_cmd->byte1 |= CD_MSF;
2704 scsi_cmd->byte2 = SRS_SUBQ;
2705 scsi_cmd->subchan_format = format;
2706 scsi_cmd->track = track;
2707 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
2708 scsi_cmd->control = 0;
2710 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2711 /*sense_flags*/SF_RETRY_UA);
2713 xpt_release_ccb(ccb);
2720 * All MODE_SENSE requests in the cd(4) driver MUST go through this
2721 * routine. See comments in cd6byteworkaround() for details.
2724 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
2727 struct ccb_scsiio *csio;
2728 struct cd_softc *softc;
2733 softc = (struct cd_softc *)periph->softc;
2735 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2739 data->cdb_size = softc->minimum_command_size;
2740 if (data->cdb_size < 10)
2741 param_len = sizeof(struct cd_mode_data);
2743 param_len = sizeof(struct cd_mode_data_10);
2745 /* Don't say we've got more room than we actually allocated */
2746 param_len = min(param_len, data->alloc_len);
2748 scsi_mode_sense_len(csio,
2749 /* retries */ cd_retry_count,
2750 /* cbfcnp */ cddone,
2751 /* tag_action */ MSG_SIMPLE_Q_TAG,
2753 /* page_code */ SMS_PAGE_CTRL_CURRENT,
2755 /* param_buf */ data->mode_buf,
2756 /* param_len */ param_len,
2757 /* minimum_cmd_size */ softc->minimum_command_size,
2758 /* sense_len */ SSD_FULL_SIZE,
2759 /* timeout */ 50000);
2762 * It would be nice not to have to do this, but there's no
2763 * available pointer in the CCB that would allow us to stuff the
2764 * mode params structure in there and retrieve it in
2765 * cd6byteworkaround(), so we can set the cdb size. The cdb size
2766 * lets the caller know what CDB size we ended up using, so they
2767 * can find the actual mode page offset.
2769 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
2771 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2772 /*sense_flags*/SF_RETRY_UA);
2774 xpt_release_ccb(ccb);
2776 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
2779 * This is a bit of belt-and-suspenders checking, but if we run
2780 * into a situation where the target sends back multiple block
2781 * descriptors, we might not have enough space in the buffer to
2782 * see the whole mode page. Better to return an error than
2783 * potentially access memory beyond our malloced region.
2788 if (data->cdb_size == 10) {
2789 struct scsi_mode_header_10 *hdr10;
2791 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
2792 data_len = scsi_2btoul(hdr10->data_length);
2793 data_len += sizeof(hdr10->data_length);
2795 struct scsi_mode_header_6 *hdr6;
2797 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
2798 data_len = hdr6->data_length;
2799 data_len += sizeof(hdr6->data_length);
2803 * Complain if there is more mode data available than we
2804 * allocated space for. This could potentially happen if
2805 * we miscalculated the page length for some reason, if the
2806 * drive returns multiple block descriptors, or if it sets
2807 * the data length incorrectly.
2809 if (data_len > data->alloc_len) {
2810 xpt_print(periph->path, "allocated modepage %d length "
2811 "%d < returned length %d\n", page, data->alloc_len,
2820 * All MODE_SELECT requests in the cd(4) driver MUST go through this
2821 * routine. See comments in cd6byteworkaround() for details.
2824 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
2826 struct ccb_scsiio *csio;
2827 struct cd_softc *softc;
2829 int cdb_size, param_len;
2832 softc = (struct cd_softc *)periph->softc;
2834 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2841 * If the data is formatted for the 10 byte version of the mode
2842 * select parameter list, we need to use the 10 byte CDB.
2843 * Otherwise, we use whatever the stored minimum command size.
2845 if (data->cdb_size == 10)
2846 cdb_size = data->cdb_size;
2848 cdb_size = softc->minimum_command_size;
2850 if (cdb_size >= 10) {
2851 struct scsi_mode_header_10 *mode_header;
2854 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
2856 data_len = scsi_2btoul(mode_header->data_length);
2858 scsi_ulto2b(0, mode_header->data_length);
2860 * SONY drives do not allow a mode select with a medium_type
2861 * value that has just been returned by a mode sense; use a
2862 * medium_type of 0 (Default) instead.
2864 mode_header->medium_type = 0;
2867 * Pass back whatever the drive passed to us, plus the size
2868 * of the data length field.
2870 param_len = data_len + sizeof(mode_header->data_length);
2873 struct scsi_mode_header_6 *mode_header;
2875 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
2877 param_len = mode_header->data_length + 1;
2879 mode_header->data_length = 0;
2881 * SONY drives do not allow a mode select with a medium_type
2882 * value that has just been returned by a mode sense; use a
2883 * medium_type of 0 (Default) instead.
2885 mode_header->medium_type = 0;
2888 /* Don't say we've got more room than we actually allocated */
2889 param_len = min(param_len, data->alloc_len);
2891 scsi_mode_select_len(csio,
2892 /* retries */ cd_retry_count,
2893 /* cbfcnp */ cddone,
2894 /* tag_action */ MSG_SIMPLE_Q_TAG,
2895 /* scsi_page_fmt */ 1,
2897 /* param_buf */ data->mode_buf,
2898 /* param_len */ param_len,
2899 /* minimum_cmd_size */ cdb_size,
2900 /* sense_len */ SSD_FULL_SIZE,
2901 /* timeout */ 50000);
2903 /* See comments in cdgetmode() and cd6byteworkaround(). */
2904 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
2906 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2907 /*sense_flags*/SF_RETRY_UA);
2909 xpt_release_ccb(ccb);
2911 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
2918 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
2920 struct ccb_scsiio *csio;
2926 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2929 * Use the smallest possible command to perform the operation.
2931 if ((len & 0xffff0000) == 0) {
2933 * We can fit in a 10 byte cdb.
2935 struct scsi_play_10 *scsi_cmd;
2937 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
2938 bzero (scsi_cmd, sizeof(*scsi_cmd));
2939 scsi_cmd->op_code = PLAY_10;
2940 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2941 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
2942 cdb_len = sizeof(*scsi_cmd);
2944 struct scsi_play_12 *scsi_cmd;
2946 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
2947 bzero (scsi_cmd, sizeof(*scsi_cmd));
2948 scsi_cmd->op_code = PLAY_12;
2949 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
2950 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
2951 cdb_len = sizeof(*scsi_cmd);
2954 /*retries*/ cd_retry_count,
2956 /*flags*/CAM_DIR_NONE,
2960 /*sense_len*/SSD_FULL_SIZE,
2962 /*timeout*/50 * 1000);
2964 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2965 /*sense_flags*/SF_RETRY_UA);
2967 xpt_release_ccb(ccb);
2973 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
2974 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
2976 struct scsi_play_msf *scsi_cmd;
2977 struct ccb_scsiio *csio;
2983 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2988 /* retries */ cd_retry_count,
2989 /* cbfcnp */ cddone,
2990 /* flags */ CAM_DIR_NONE,
2991 /* tag_action */ MSG_SIMPLE_Q_TAG,
2992 /* data_ptr */ NULL,
2994 /* sense_len */ SSD_FULL_SIZE,
2995 sizeof(struct scsi_play_msf),
2996 /* timeout */ 50000);
2998 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
2999 bzero (scsi_cmd, sizeof(*scsi_cmd));
3001 scsi_cmd->op_code = PLAY_MSF;
3002 scsi_cmd->start_m = startm;
3003 scsi_cmd->start_s = starts;
3004 scsi_cmd->start_f = startf;
3005 scsi_cmd->end_m = endm;
3006 scsi_cmd->end_s = ends;
3007 scsi_cmd->end_f = endf;
3009 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3010 /*sense_flags*/SF_RETRY_UA);
3012 xpt_release_ccb(ccb);
3019 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3020 u_int32_t etrack, u_int32_t eindex)
3022 struct scsi_play_track *scsi_cmd;
3023 struct ccb_scsiio *csio;
3029 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3034 /* retries */ cd_retry_count,
3035 /* cbfcnp */ cddone,
3036 /* flags */ CAM_DIR_NONE,
3037 /* tag_action */ MSG_SIMPLE_Q_TAG,
3038 /* data_ptr */ NULL,
3040 /* sense_len */ SSD_FULL_SIZE,
3041 sizeof(struct scsi_play_track),
3042 /* timeout */ 50000);
3044 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3045 bzero (scsi_cmd, sizeof(*scsi_cmd));
3047 scsi_cmd->op_code = PLAY_TRACK;
3048 scsi_cmd->start_track = strack;
3049 scsi_cmd->start_index = sindex;
3050 scsi_cmd->end_track = etrack;
3051 scsi_cmd->end_index = eindex;
3053 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3054 /*sense_flags*/SF_RETRY_UA);
3056 xpt_release_ccb(ccb);
3062 cdpause(struct cam_periph *periph, u_int32_t go)
3064 struct scsi_pause *scsi_cmd;
3065 struct ccb_scsiio *csio;
3071 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3076 /* retries */ cd_retry_count,
3077 /* cbfcnp */ cddone,
3078 /* flags */ CAM_DIR_NONE,
3079 /* tag_action */ MSG_SIMPLE_Q_TAG,
3080 /* data_ptr */ NULL,
3082 /* sense_len */ SSD_FULL_SIZE,
3083 sizeof(struct scsi_pause),
3084 /* timeout */ 50000);
3086 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3087 bzero (scsi_cmd, sizeof(*scsi_cmd));
3089 scsi_cmd->op_code = PAUSE;
3090 scsi_cmd->resume = go;
3092 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3093 /*sense_flags*/SF_RETRY_UA);
3095 xpt_release_ccb(ccb);
3101 cdstartunit(struct cam_periph *periph, int load)
3108 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3110 scsi_start_stop(&ccb->csio,
3111 /* retries */ cd_retry_count,
3112 /* cbfcnp */ cddone,
3113 /* tag_action */ MSG_SIMPLE_Q_TAG,
3115 /* load_eject */ load,
3116 /* immediate */ FALSE,
3117 /* sense_len */ SSD_FULL_SIZE,
3118 /* timeout */ 50000);
3120 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3121 /*sense_flags*/SF_RETRY_UA);
3123 xpt_release_ccb(ccb);
3129 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3136 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3138 scsi_start_stop(&ccb->csio,
3139 /* retries */ cd_retry_count,
3140 /* cbfcnp */ cddone,
3141 /* tag_action */ MSG_SIMPLE_Q_TAG,
3143 /* load_eject */ eject,
3144 /* immediate */ FALSE,
3145 /* sense_len */ SSD_FULL_SIZE,
3146 /* timeout */ 50000);
3148 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3149 /*sense_flags*/SF_RETRY_UA);
3151 xpt_release_ccb(ccb);
3157 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3159 struct scsi_set_speed *scsi_cmd;
3160 struct ccb_scsiio *csio;
3165 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3168 /* Preserve old behavior: units in multiples of CDROM speed */
3175 /* retries */ cd_retry_count,
3176 /* cbfcnp */ cddone,
3177 /* flags */ CAM_DIR_NONE,
3178 /* tag_action */ MSG_SIMPLE_Q_TAG,
3179 /* data_ptr */ NULL,
3181 /* sense_len */ SSD_FULL_SIZE,
3182 sizeof(struct scsi_set_speed),
3183 /* timeout */ 50000);
3185 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3186 bzero(scsi_cmd, sizeof(*scsi_cmd));
3188 scsi_cmd->opcode = SET_CD_SPEED;
3189 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3190 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3192 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3193 /*sense_flags*/SF_RETRY_UA);
3195 xpt_release_ccb(ccb);
3201 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3213 switch (authinfo->format) {
3214 case DVD_REPORT_AGID:
3215 length = sizeof(struct scsi_report_key_data_agid);
3217 case DVD_REPORT_CHALLENGE:
3218 length = sizeof(struct scsi_report_key_data_challenge);
3220 case DVD_REPORT_KEY1:
3221 length = sizeof(struct scsi_report_key_data_key1_key2);
3223 case DVD_REPORT_TITLE_KEY:
3224 length = sizeof(struct scsi_report_key_data_title);
3225 /* The lba field is only set for the title key */
3226 lba = authinfo->lba;
3228 case DVD_REPORT_ASF:
3229 length = sizeof(struct scsi_report_key_data_asf);
3231 case DVD_REPORT_RPC:
3232 length = sizeof(struct scsi_report_key_data_rpc);
3234 case DVD_INVALIDATE_AGID:
3242 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3246 cam_periph_lock(periph);
3247 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3249 scsi_report_key(&ccb->csio,
3250 /* retries */ cd_retry_count,
3251 /* cbfcnp */ cddone,
3252 /* tag_action */ MSG_SIMPLE_Q_TAG,
3254 /* agid */ authinfo->agid,
3255 /* key_format */ authinfo->format,
3256 /* data_ptr */ databuf,
3257 /* dxfer_len */ length,
3258 /* sense_len */ SSD_FULL_SIZE,
3259 /* timeout */ 50000);
3261 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3262 /*sense_flags*/SF_RETRY_UA);
3267 if (ccb->csio.resid != 0) {
3268 xpt_print(periph->path, "warning, residual for report key "
3269 "command is %d\n", ccb->csio.resid);
3272 switch(authinfo->format) {
3273 case DVD_REPORT_AGID: {
3274 struct scsi_report_key_data_agid *agid_data;
3276 agid_data = (struct scsi_report_key_data_agid *)databuf;
3278 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3282 case DVD_REPORT_CHALLENGE: {
3283 struct scsi_report_key_data_challenge *chal_data;
3285 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3287 bcopy(chal_data->challenge_key, authinfo->keychal,
3288 min(sizeof(chal_data->challenge_key),
3289 sizeof(authinfo->keychal)));
3292 case DVD_REPORT_KEY1: {
3293 struct scsi_report_key_data_key1_key2 *key1_data;
3295 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3297 bcopy(key1_data->key1, authinfo->keychal,
3298 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3301 case DVD_REPORT_TITLE_KEY: {
3302 struct scsi_report_key_data_title *title_data;
3304 title_data = (struct scsi_report_key_data_title *)databuf;
3306 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3307 RKD_TITLE_CPM_SHIFT;
3308 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3309 RKD_TITLE_CP_SEC_SHIFT;
3310 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3311 RKD_TITLE_CMGS_SHIFT;
3312 bcopy(title_data->title_key, authinfo->keychal,
3313 min(sizeof(title_data->title_key),
3314 sizeof(authinfo->keychal)));
3317 case DVD_REPORT_ASF: {
3318 struct scsi_report_key_data_asf *asf_data;
3320 asf_data = (struct scsi_report_key_data_asf *)databuf;
3322 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3325 case DVD_REPORT_RPC: {
3326 struct scsi_report_key_data_rpc *rpc_data;
3328 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3330 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3332 authinfo->vend_rsts =
3333 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3334 RKD_RPC_VENDOR_RESET_SHIFT;
3335 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3336 authinfo->region = rpc_data->region_mask;
3337 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3340 case DVD_INVALIDATE_AGID:
3343 /* This should be impossible, since we checked above */
3346 break; /* NOTREACHED */
3350 xpt_release_ccb(ccb);
3351 cam_periph_unlock(periph);
3353 if (databuf != NULL)
3354 free(databuf, M_DEVBUF);
3360 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3370 switch(authinfo->format) {
3371 case DVD_SEND_CHALLENGE: {
3372 struct scsi_report_key_data_challenge *challenge_data;
3374 length = sizeof(*challenge_data);
3376 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3378 databuf = (u_int8_t *)challenge_data;
3380 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3381 challenge_data->data_len);
3383 bcopy(authinfo->keychal, challenge_data->challenge_key,
3384 min(sizeof(authinfo->keychal),
3385 sizeof(challenge_data->challenge_key)));
3388 case DVD_SEND_KEY2: {
3389 struct scsi_report_key_data_key1_key2 *key2_data;
3391 length = sizeof(*key2_data);
3393 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3395 databuf = (u_int8_t *)key2_data;
3397 scsi_ulto2b(length - sizeof(key2_data->data_len),
3398 key2_data->data_len);
3400 bcopy(authinfo->keychal, key2_data->key1,
3401 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3405 case DVD_SEND_RPC: {
3406 struct scsi_send_key_data_rpc *rpc_data;
3408 length = sizeof(*rpc_data);
3410 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3412 databuf = (u_int8_t *)rpc_data;
3414 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3415 rpc_data->data_len);
3417 rpc_data->region_code = authinfo->region;
3424 cam_periph_lock(periph);
3425 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3427 scsi_send_key(&ccb->csio,
3428 /* retries */ cd_retry_count,
3429 /* cbfcnp */ cddone,
3430 /* tag_action */ MSG_SIMPLE_Q_TAG,
3431 /* agid */ authinfo->agid,
3432 /* key_format */ authinfo->format,
3433 /* data_ptr */ databuf,
3434 /* dxfer_len */ length,
3435 /* sense_len */ SSD_FULL_SIZE,
3436 /* timeout */ 50000);
3438 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3439 /*sense_flags*/SF_RETRY_UA);
3441 xpt_release_ccb(ccb);
3442 cam_periph_unlock(periph);
3444 if (databuf != NULL)
3445 free(databuf, M_DEVBUF);
3451 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3461 /* The address is reserved for many of the formats */
3464 switch(dvdstruct->format) {
3465 case DVD_STRUCT_PHYSICAL:
3466 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3468 case DVD_STRUCT_COPYRIGHT:
3469 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3471 case DVD_STRUCT_DISCKEY:
3472 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3474 case DVD_STRUCT_BCA:
3475 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3477 case DVD_STRUCT_MANUFACT:
3478 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3480 case DVD_STRUCT_CMI:
3482 case DVD_STRUCT_PROTDISCID:
3483 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
3485 case DVD_STRUCT_DISCKEYBLOCK:
3486 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
3488 case DVD_STRUCT_DDS:
3489 length = sizeof(struct scsi_read_dvd_struct_data_dds);
3491 case DVD_STRUCT_MEDIUM_STAT:
3492 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
3494 case DVD_STRUCT_SPARE_AREA:
3495 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
3497 case DVD_STRUCT_RMD_LAST:
3499 case DVD_STRUCT_RMD_RMA:
3501 case DVD_STRUCT_PRERECORDED:
3502 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
3504 case DVD_STRUCT_UNIQUEID:
3505 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
3507 case DVD_STRUCT_DCB:
3509 case DVD_STRUCT_LIST:
3511 * This is the maximum allocation length for the READ DVD
3512 * STRUCTURE command. There's nothing in the MMC3 spec
3513 * that indicates a limit in the amount of data that can
3514 * be returned from this call, other than the limits
3515 * imposed by the 2-byte length variables.
3524 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3528 cam_periph_lock(periph);
3529 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3531 scsi_read_dvd_structure(&ccb->csio,
3532 /* retries */ cd_retry_count,
3533 /* cbfcnp */ cddone,
3534 /* tag_action */ MSG_SIMPLE_Q_TAG,
3536 /* layer_number */ dvdstruct->layer_num,
3537 /* key_format */ dvdstruct->format,
3538 /* agid */ dvdstruct->agid,
3539 /* data_ptr */ databuf,
3540 /* dxfer_len */ length,
3541 /* sense_len */ SSD_FULL_SIZE,
3542 /* timeout */ 50000);
3544 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3545 /*sense_flags*/SF_RETRY_UA);
3550 switch(dvdstruct->format) {
3551 case DVD_STRUCT_PHYSICAL: {
3552 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
3553 struct dvd_layer *outlayer;
3554 struct scsi_read_dvd_struct_data_physical *phys_data;
3557 (struct scsi_read_dvd_struct_data_physical *)databuf;
3558 inlayer = &phys_data->layer_desc;
3559 outlayer = (struct dvd_layer *)&dvdstruct->data;
3561 dvdstruct->length = sizeof(*inlayer);
3563 outlayer->book_type = (inlayer->book_type_version &
3564 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
3565 outlayer->book_version = (inlayer->book_type_version &
3566 RDSD_BOOK_VERSION_MASK);
3567 outlayer->disc_size = (inlayer->disc_size_max_rate &
3568 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
3569 outlayer->max_rate = (inlayer->disc_size_max_rate &
3570 RDSD_MAX_RATE_MASK);
3571 outlayer->nlayers = (inlayer->layer_info &
3572 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
3573 outlayer->track_path = (inlayer->layer_info &
3574 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
3575 outlayer->layer_type = (inlayer->layer_info &
3576 RDSD_LAYER_TYPE_MASK);
3577 outlayer->linear_density = (inlayer->density &
3578 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
3579 outlayer->track_density = (inlayer->density &
3580 RDSD_TRACK_DENSITY_MASK);
3581 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
3583 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
3584 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
3585 outlayer->end_sector_l0 =
3586 scsi_3btoul(inlayer->end_sector_layer0);
3589 case DVD_STRUCT_COPYRIGHT: {
3590 struct scsi_read_dvd_struct_data_copyright *copy_data;
3592 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
3595 dvdstruct->cpst = copy_data->cps_type;
3596 dvdstruct->rmi = copy_data->region_info;
3597 dvdstruct->length = 0;
3603 * Tell the user what the overall length is, no matter
3604 * what we can actually fit in the data buffer.
3606 dvdstruct->length = length - ccb->csio.resid -
3607 sizeof(struct scsi_read_dvd_struct_data_header);
3610 * But only actually copy out the smaller of what we read
3611 * in or what the structure can take.
3613 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
3615 min(sizeof(dvdstruct->data), dvdstruct->length));
3620 xpt_release_ccb(ccb);
3621 cam_periph_unlock(periph);
3623 if (databuf != NULL)
3624 free(databuf, M_DEVBUF);
3630 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
3631 void (*cbfcnp)(struct cam_periph *, union ccb *),
3632 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
3633 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
3634 u_int8_t sense_len, u_int32_t timeout)
3636 struct scsi_report_key *scsi_cmd;
3638 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
3639 bzero(scsi_cmd, sizeof(*scsi_cmd));
3640 scsi_cmd->opcode = REPORT_KEY;
3641 scsi_ulto4b(lba, scsi_cmd->lba);
3642 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3643 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3644 (key_format & RK_KF_KEYFORMAT_MASK);
3649 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
3651 /*data_ptr*/ data_ptr,
3652 /*dxfer_len*/ dxfer_len,
3659 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
3660 void (*cbfcnp)(struct cam_periph *, union ccb *),
3661 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
3662 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3665 struct scsi_send_key *scsi_cmd;
3667 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
3668 bzero(scsi_cmd, sizeof(*scsi_cmd));
3669 scsi_cmd->opcode = SEND_KEY;
3671 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
3672 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
3673 (key_format & RK_KF_KEYFORMAT_MASK);
3678 /*flags*/ CAM_DIR_OUT,
3680 /*data_ptr*/ data_ptr,
3681 /*dxfer_len*/ dxfer_len,
3689 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
3690 void (*cbfcnp)(struct cam_periph *, union ccb *),
3691 u_int8_t tag_action, u_int32_t address,
3692 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
3693 u_int8_t *data_ptr, u_int32_t dxfer_len,
3694 u_int8_t sense_len, u_int32_t timeout)
3696 struct scsi_read_dvd_structure *scsi_cmd;
3698 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
3699 bzero(scsi_cmd, sizeof(*scsi_cmd));
3700 scsi_cmd->opcode = READ_DVD_STRUCTURE;
3702 scsi_ulto4b(address, scsi_cmd->address);
3703 scsi_cmd->layer_number = layer_number;
3704 scsi_cmd->format = format;
3705 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
3706 /* The AGID is the top two bits of this byte */
3707 scsi_cmd->agid = agid << 6;
3712 /*flags*/ CAM_DIR_IN,
3714 /*data_ptr*/ data_ptr,
3715 /*dxfer_len*/ dxfer_len,