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_NO_CHANGER = 0x04,
92 CD_Q_10_BYTE_ONLY = 0x10
95 #define CD_Q_BIT_STRING \
104 CD_FLAG_INVALID = 0x0001,
105 CD_FLAG_NEW_DISC = 0x0002,
106 CD_FLAG_DISC_LOCKED = 0x0004,
107 CD_FLAG_DISC_REMOVABLE = 0x0008,
108 CD_FLAG_CHANGER = 0x0040,
109 CD_FLAG_ACTIVE = 0x0080,
110 CD_FLAG_SCHED_ON_COMP = 0x0100,
111 CD_FLAG_RETRY_UA = 0x0200,
112 CD_FLAG_VALID_MEDIA = 0x0400,
113 CD_FLAG_VALID_TOC = 0x0800,
114 CD_FLAG_SCTX_INIT = 0x1000,
115 CD_FLAG_OPEN = 0x2000
120 CD_CCB_BUFFER_IO = 0x02,
121 CD_CCB_WAITING = 0x03,
122 CD_CCB_TYPE_MASK = 0x0F,
123 CD_CCB_RETRY_UA = 0x10
127 CHANGER_TIMEOUT_SCHED = 0x01,
128 CHANGER_SHORT_TMOUT_SCHED = 0x02,
129 CHANGER_MANUAL_CALL = 0x04,
130 CHANGER_NEED_TIMEOUT = 0x08
133 #define ccb_state ppriv_field0
134 #define ccb_bp ppriv_ptr1
137 struct ioc_toc_header header;
138 struct cd_toc_entry entries[100];
141 struct cd_toc_single {
142 struct ioc_toc_header header;
143 struct cd_toc_entry entry;
154 volatile cd_flags flags;
155 struct bio_queue_head bio_queue;
156 LIST_HEAD(, ccb_hdr) pending_ccbs;
157 struct cd_params params;
160 STAILQ_ENTRY(cd_softc) changer_links;
161 struct cdchanger *changer;
163 struct cam_periph *periph;
164 int minimum_command_size;
165 int outstanding_cmds;
166 struct task sysctl_task;
167 struct sysctl_ctx_list sysctl_ctx;
168 struct sysctl_oid *sysctl_tree;
169 STAILQ_HEAD(, cd_mode_params) mode_queue;
170 struct cd_tocdata toc;
174 struct cd_page_sizes {
179 static struct cd_page_sizes cd_page_size_table[] =
181 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
184 struct cd_quirk_entry {
185 struct scsi_inquiry_pattern inq_pat;
190 * The changer quirk entries aren't strictly necessary. Basically, what
191 * they do is tell cdregister() up front that a device is a changer.
192 * Otherwise, it will figure that fact out once it sees a LUN on the device
193 * that is greater than 0. If it is known up front that a device is a changer,
194 * all I/O to the device will go through the changer scheduling routines, as
195 * opposed to the "normal" CD code.
197 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
198 * your device hangs when it gets a 10 byte command. Adding a quirk just
199 * to get rid of the informative diagnostic message is not acceptable. All
200 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
201 * referenced in a comment along with the quirk) , and must be approved by
202 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
203 * be removed until the submitter can explain why they are needed.
204 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
205 * when the CAM_NEW_TRAN_CODE work is done.
207 static struct cd_quirk_entry cd_quirk_table[] =
210 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
211 /*quirks*/ CD_Q_CHANGER
214 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
215 "*"}, /* quirks */ CD_Q_CHANGER
218 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
219 /* quirks */ CD_Q_CHANGER
222 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
223 /* quirks */ CD_Q_BCD_TRACKS
227 static disk_open_t cdopen;
228 static disk_close_t cdclose;
229 static disk_ioctl_t cdioctl;
230 static disk_strategy_t cdstrategy;
232 static periph_init_t cdinit;
233 static periph_ctor_t cdregister;
234 static periph_dtor_t cdcleanup;
235 static periph_start_t cdstart;
236 static periph_oninv_t cdoninvalidate;
237 static void cdasync(void *callback_arg, u_int32_t code,
238 struct cam_path *path, void *arg);
239 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
240 static void cdshorttimeout(void *arg);
241 static void cdschedule(struct cam_periph *periph, int priority);
242 static void cdrunchangerqueue(void *arg);
243 static void cdchangerschedule(struct cd_softc *softc);
244 static int cdrunccb(union ccb *ccb,
245 int (*error_routine)(union ccb *ccb,
247 u_int32_t sense_flags),
248 u_int32_t cam_flags, u_int32_t sense_flags);
249 static union ccb *cdgetccb(struct cam_periph *periph,
251 static void cddone(struct cam_periph *periph,
252 union ccb *start_ccb);
253 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
254 static int cdgetpagesize(int page_num);
255 static void cdprevent(struct cam_periph *periph, int action);
256 static int cdcheckmedia(struct cam_periph *periph);
257 static int cdsize(struct cam_periph *periph, u_int32_t *size);
258 static int cd6byteworkaround(union ccb *ccb);
259 static int cderror(union ccb *ccb, u_int32_t cam_flags,
260 u_int32_t sense_flags);
261 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
262 u_int32_t start, u_int8_t *data,
263 u_int32_t len, u_int32_t sense_flags);
264 static int cdgetmode(struct cam_periph *periph,
265 struct cd_mode_params *data, u_int32_t page);
266 static int cdsetmode(struct cam_periph *periph,
267 struct cd_mode_params *data);
268 static int cdplay(struct cam_periph *periph, u_int32_t blk,
270 static int cdreadsubchannel(struct cam_periph *periph,
271 u_int32_t mode, u_int32_t format,
273 struct cd_sub_channel_info *data,
275 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
276 u_int32_t starts, u_int32_t startf,
277 u_int32_t endm, u_int32_t ends,
279 static int cdplaytracks(struct cam_periph *periph,
280 u_int32_t strack, u_int32_t sindex,
281 u_int32_t etrack, u_int32_t eindex);
282 static int cdpause(struct cam_periph *periph, u_int32_t go);
283 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
284 static int cdstartunit(struct cam_periph *periph, int load);
285 static int cdsetspeed(struct cam_periph *periph,
286 u_int32_t rdspeed, u_int32_t wrspeed);
287 static int cdreportkey(struct cam_periph *periph,
288 struct dvd_authinfo *authinfo);
289 static int cdsendkey(struct cam_periph *periph,
290 struct dvd_authinfo *authinfo);
291 static int cdreaddvdstructure(struct cam_periph *periph,
292 struct dvd_struct *dvdstruct);
294 static struct periph_driver cddriver =
297 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
300 PERIPHDRIVER_DECLARE(cd, cddriver);
302 #ifndef CD_DEFAULT_RETRY
303 #define CD_DEFAULT_RETRY 4
305 #ifndef CHANGER_MIN_BUSY_SECONDS
306 #define CHANGER_MIN_BUSY_SECONDS 5
308 #ifndef CHANGER_MAX_BUSY_SECONDS
309 #define CHANGER_MAX_BUSY_SECONDS 15
312 static int cd_retry_count = CD_DEFAULT_RETRY;
313 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
314 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
316 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
317 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
318 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
319 &cd_retry_count, 0, "Normal I/O retry count");
320 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
321 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
322 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
323 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
324 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
325 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
326 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
330 target_id_t target_id;
333 struct timeval start_time;
334 struct cd_softc *cur_device;
335 struct callout short_handle;
336 struct callout long_handle;
337 volatile cd_changer_flags flags;
338 STAILQ_ENTRY(cdchanger) changer_links;
339 STAILQ_HEAD(chdevlist, cd_softc) chluns;
342 static struct mtx changerq_mtx;
343 static STAILQ_HEAD(changerlist, cdchanger) changerq;
344 static int num_changers;
346 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
353 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
354 STAILQ_INIT(&changerq);
357 * Install a global async callback. This callback will
358 * receive async callbacks like "new device found".
360 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
362 if (status != CAM_REQ_CMP) {
363 printf("cd: Failed to attach master async callback "
364 "due to status 0x%x!\n", status);
369 cdoninvalidate(struct cam_periph *periph)
371 struct cd_softc *softc;
373 softc = (struct cd_softc *)periph->softc;
376 * De-register any async callbacks.
378 xpt_register_async(0, cdasync, periph, periph->path);
380 softc->flags |= CD_FLAG_INVALID;
383 * Return all queued I/O with ENXIO.
384 * XXX Handle any transactions queued to the card
385 * with XPT_ABORT_CCB.
387 bioq_flush(&softc->bio_queue, NULL, ENXIO);
390 * If this device is part of a changer, and it was scheduled
391 * to run, remove it from the run queue since we just nuked
392 * all of its scheduled I/O.
394 if ((softc->flags & CD_FLAG_CHANGER)
395 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
396 camq_remove(&softc->changer->devq, softc->pinfo.index);
398 disk_gone(softc->disk);
399 xpt_print(periph->path, "lost device\n");
403 cdcleanup(struct cam_periph *periph)
405 struct cd_softc *softc;
407 softc = (struct cd_softc *)periph->softc;
409 xpt_print(periph->path, "removing device entry\n");
412 * In the queued, non-active case, the device in question
413 * has already been removed from the changer run queue. Since this
414 * device is active, we need to de-activate it, and schedule
415 * another device to run. (if there is another one to run)
417 if ((softc->flags & CD_FLAG_CHANGER)
418 && (softc->flags & CD_FLAG_ACTIVE)) {
421 * The purpose of the short timeout is soley to determine
422 * whether the current device has finished or not. Well,
423 * since we're removing the active device, we know that it
424 * is finished. So, get rid of the short timeout.
425 * Otherwise, if we're in the time period before the short
426 * timeout fires, and there are no other devices in the
427 * queue to run, there won't be any other device put in the
428 * active slot. i.e., when we call cdrunchangerqueue()
429 * below, it won't do anything. Then, when the short
430 * timeout fires, it'll look at the "current device", which
431 * we are free below, and possibly panic the kernel on a
432 * bogus pointer reference.
434 * The long timeout doesn't really matter, since we
435 * decrement the qfrozen_cnt to indicate that there is
436 * nothing in the active slot now. Therefore, there won't
437 * be any bogus pointer references there.
439 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
440 callout_stop(&softc->changer->short_handle);
441 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
443 softc->changer->devq.qfrozen_cnt[0]--;
444 softc->changer->flags |= CHANGER_MANUAL_CALL;
445 cdrunchangerqueue(softc->changer);
449 * If we're removing the last device on the changer, go ahead and
450 * remove the changer device structure.
452 if ((softc->flags & CD_FLAG_CHANGER)
453 && (--softc->changer->num_devices == 0)) {
456 * Theoretically, there shouldn't be any timeouts left, but
457 * I'm not completely sure that that will be the case. So,
458 * it won't hurt to check and see if there are any left.
460 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
461 callout_stop(&softc->changer->long_handle);
462 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
465 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
466 callout_stop(&softc->changer->short_handle);
467 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
470 mtx_lock(&changerq_mtx);
471 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
474 mtx_unlock(&changerq_mtx);
475 xpt_print(periph->path, "removing changer entry\n");
476 free(softc->changer, M_DEVBUF);
478 cam_periph_unlock(periph);
479 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
480 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
481 xpt_print(periph->path, "can't remove sysctl context\n");
484 disk_destroy(softc->disk);
485 free(softc, M_DEVBUF);
486 cam_periph_lock(periph);
490 cdasync(void *callback_arg, u_int32_t code,
491 struct cam_path *path, void *arg)
493 struct cam_periph *periph;
495 periph = (struct cam_periph *)callback_arg;
497 case AC_FOUND_DEVICE:
499 struct ccb_getdev *cgd;
502 cgd = (struct ccb_getdev *)arg;
506 if (cgd->protocol != PROTO_SCSI)
509 if (SID_TYPE(&cgd->inq_data) != T_CDROM
510 && SID_TYPE(&cgd->inq_data) != T_WORM)
514 * Allocate a peripheral instance for
515 * this device and start the probe
518 status = cam_periph_alloc(cdregister, cdoninvalidate,
520 "cd", CAM_PERIPH_BIO,
521 cgd->ccb_h.path, cdasync,
522 AC_FOUND_DEVICE, cgd);
524 if (status != CAM_REQ_CMP
525 && status != CAM_REQ_INPROG)
526 printf("cdasync: Unable to attach new device "
527 "due to status 0x%x\n", status);
534 struct cd_softc *softc;
535 struct ccb_hdr *ccbh;
537 softc = (struct cd_softc *)periph->softc;
539 * Don't fail on the expected unit attention
542 softc->flags |= CD_FLAG_RETRY_UA;
543 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
544 ccbh->ccb_state |= CD_CCB_RETRY_UA;
548 cam_periph_async(periph, code, path, arg);
554 cdsysctlinit(void *context, int pending)
556 struct cam_periph *periph;
557 struct cd_softc *softc;
558 char tmpstr[80], tmpstr2[80];
560 periph = (struct cam_periph *)context;
561 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
564 softc = (struct cd_softc *)periph->softc;
565 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
566 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
568 sysctl_ctx_init(&softc->sysctl_ctx);
569 softc->flags |= CD_FLAG_SCTX_INIT;
570 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
571 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
572 tmpstr2, CTLFLAG_RD, 0, tmpstr);
574 if (softc->sysctl_tree == NULL) {
575 printf("cdsysctlinit: unable to allocate sysctl tree\n");
576 cam_periph_release(periph);
581 * Now register the sysctl handler, so the user can the value on
584 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
585 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
586 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
589 cam_periph_release(periph);
593 * We have a handler function for this so we can check the values when the
594 * user sets them, instead of every time we look at them.
597 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
601 value = *(int *)arg1;
603 error = sysctl_handle_int(oidp, &value, 0, req);
606 || (req->newptr == NULL))
610 * The only real values we can have here are 6 or 10. I don't
611 * really forsee having 12 be an option at any time in the future.
612 * So if the user sets something less than or equal to 6, we'll set
613 * it to 6. If he sets something greater than 6, we'll set it to 10.
615 * I suppose we could just return an error here for the wrong values,
616 * but I don't think it's necessary to do so, as long as we can
617 * determine the user's intent without too much trouble.
624 *(int *)arg1 = value;
630 cdregister(struct cam_periph *periph, void *arg)
632 struct cd_softc *softc;
633 struct ccb_pathinq cpi;
634 struct ccb_getdev *cgd;
638 cgd = (struct ccb_getdev *)arg;
639 if (periph == NULL) {
640 printf("cdregister: periph was NULL!!\n");
641 return(CAM_REQ_CMP_ERR);
644 printf("cdregister: no getdev CCB, can't register device\n");
645 return(CAM_REQ_CMP_ERR);
648 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
651 printf("cdregister: Unable to probe new device. "
652 "Unable to allocate softc\n");
653 return(CAM_REQ_CMP_ERR);
656 LIST_INIT(&softc->pending_ccbs);
657 STAILQ_INIT(&softc->mode_queue);
658 softc->state = CD_STATE_PROBE;
659 bioq_init(&softc->bio_queue);
660 if (SID_IS_REMOVABLE(&cgd->inq_data))
661 softc->flags |= CD_FLAG_DISC_REMOVABLE;
663 periph->softc = softc;
664 softc->periph = periph;
667 * See if this device has any quirks.
669 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
670 (caddr_t)cd_quirk_table,
671 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
672 sizeof(*cd_quirk_table), scsi_inquiry_match);
675 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
677 softc->quirks = CD_Q_NONE;
679 /* Check if the SIM does not want 6 byte commands */
680 bzero(&cpi, sizeof(cpi));
681 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
682 cpi.ccb_h.func_code = XPT_PATH_INQ;
683 xpt_action((union ccb *)&cpi);
684 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
685 softc->quirks |= CD_Q_10_BYTE_ONLY;
687 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
689 /* The default is 6 byte commands, unless quirked otherwise */
690 if (softc->quirks & CD_Q_10_BYTE_ONLY)
691 softc->minimum_command_size = 10;
693 softc->minimum_command_size = 6;
695 (void)cam_periph_hold(periph, PRIBIO);
696 cam_periph_unlock(periph);
698 * Load the user's default, if any.
700 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
701 periph->unit_number);
702 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
704 /* 6 and 10 are the only permissible values here. */
705 if (softc->minimum_command_size < 6)
706 softc->minimum_command_size = 6;
707 else if (softc->minimum_command_size > 6)
708 softc->minimum_command_size = 10;
711 * We need to register the statistics structure for this device,
712 * but we don't have the blocksize yet for it. So, we register
713 * the structure and indicate that we don't have the blocksize
714 * yet. Unlike other SCSI peripheral drivers, we explicitly set
715 * the device type here to be CDROM, rather than just ORing in
716 * the device type. This is because this driver can attach to either
717 * CDROM or WORM devices, and we want this peripheral driver to
718 * show up in the devstat list as a CD peripheral driver, not a
719 * WORM peripheral driver. WORM drives will also have the WORM
720 * driver attached to them.
722 softc->disk = disk_alloc();
723 softc->disk->d_devstat = devstat_new_entry("cd",
724 periph->unit_number, 0,
725 DEVSTAT_BS_UNAVAILABLE,
727 XPORT_DEVSTAT_TYPE(cpi.transport),
728 DEVSTAT_PRIORITY_CD);
729 softc->disk->d_open = cdopen;
730 softc->disk->d_close = cdclose;
731 softc->disk->d_strategy = cdstrategy;
732 softc->disk->d_ioctl = cdioctl;
733 softc->disk->d_name = "cd";
734 softc->disk->d_unit = periph->unit_number;
735 softc->disk->d_drv1 = periph;
737 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
738 else if (cpi.maxio > MAXPHYS)
739 softc->disk->d_maxsize = MAXPHYS; /* for safety */
741 softc->disk->d_maxsize = cpi.maxio;
742 softc->disk->d_flags = 0;
743 disk_create(softc->disk, DISK_VERSION);
744 cam_periph_lock(periph);
745 cam_periph_unhold(periph);
748 * Add an async callback so that we get
749 * notified if this device goes away.
751 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
752 cdasync, periph, periph->path);
755 * If the target lun is greater than 0, we most likely have a CD
756 * changer device. Check the quirk entries as well, though, just
757 * in case someone has a CD tower with one lun per drive or
758 * something like that. Also, if we know up front that a
759 * particular device is a changer, we can mark it as such starting
760 * with lun 0, instead of lun 1. It shouldn't be necessary to have
761 * a quirk entry to define something as a changer, however.
763 if (((cgd->ccb_h.target_lun > 0)
764 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
765 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
766 struct cdchanger *nchanger;
767 struct cam_periph *nperiph;
768 struct cam_path *path;
772 /* Set the changer flag in the current device's softc */
773 softc->flags |= CD_FLAG_CHANGER;
776 * Now, look around for an existing changer device with the
777 * same path and target ID as the current device.
779 mtx_lock(&changerq_mtx);
781 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
783 nchanger = STAILQ_NEXT(nchanger, changer_links)){
784 if ((nchanger->path_id == cgd->ccb_h.path_id)
785 && (nchanger->target_id == cgd->ccb_h.target_id)) {
790 mtx_unlock(&changerq_mtx);
793 * If we found a matching entry, just add this device to
794 * the list of devices on this changer.
797 struct chdevlist *chlunhead;
799 chlunhead = &nchanger->chluns;
802 * XXX KDM look at consolidating this code with the
803 * code below in a separate function.
807 * Create a path with lun id 0, and see if we can
808 * find a matching device
810 status = xpt_create_path(&path, /*periph*/ periph,
812 cgd->ccb_h.target_id, 0);
814 if ((status == CAM_REQ_CMP)
815 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
816 struct cd_softc *nsoftc;
818 nsoftc = (struct cd_softc *)nperiph->softc;
820 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
821 nsoftc->flags |= CD_FLAG_CHANGER;
822 nchanger->num_devices++;
823 if (camq_resize(&nchanger->devq,
824 nchanger->num_devices)!=CAM_REQ_CMP){
825 printf("cdregister: "
831 nsoftc->changer = nchanger;
832 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
834 STAILQ_INSERT_TAIL(&nchanger->chluns,
835 nsoftc,changer_links);
838 } else if (status == CAM_REQ_CMP)
841 printf("cdregister: unable to allocate path\n"
842 "cdregister: changer support may be "
846 nchanger->num_devices++;
848 softc->changer = nchanger;
849 softc->pinfo.index = CAM_UNQUEUED_INDEX;
851 if (camq_resize(&nchanger->devq,
852 nchanger->num_devices) != CAM_REQ_CMP) {
853 printf("cdregister: camq_resize "
854 "failed, changer support may "
858 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
861 * In this case, we don't already have an entry for this
862 * particular changer, so we need to create one, add it to
863 * the queue, and queue this device on the list for this
864 * changer. Before we queue this device, however, we need
865 * to search for lun id 0 on this target, and add it to the
866 * queue first, if it exists. (and if it hasn't already
867 * been marked as part of the changer.)
870 nchanger = malloc(sizeof(struct cdchanger),
871 M_DEVBUF, M_NOWAIT | M_ZERO);
872 if (nchanger == NULL) {
873 softc->flags &= ~CD_FLAG_CHANGER;
874 printf("cdregister: unable to malloc "
875 "changer structure\ncdregister: "
876 "changer support disabled\n");
879 * Yes, gotos can be gross but in this case
880 * I think it's justified..
884 if (camq_init(&nchanger->devq, 1) != 0) {
885 softc->flags &= ~CD_FLAG_CHANGER;
886 printf("cdregister: changer support "
891 nchanger->path_id = cgd->ccb_h.path_id;
892 nchanger->target_id = cgd->ccb_h.target_id;
894 /* this is superfluous, but it makes things clearer */
895 nchanger->num_devices = 0;
897 STAILQ_INIT(&nchanger->chluns);
899 callout_init_mtx(&nchanger->long_handle,
900 periph->sim->mtx, 0);
901 callout_init_mtx(&nchanger->short_handle,
902 periph->sim->mtx, 0);
904 mtx_lock(&changerq_mtx);
906 STAILQ_INSERT_TAIL(&changerq, nchanger,
908 mtx_unlock(&changerq_mtx);
911 * Create a path with lun id 0, and see if we can
912 * find a matching device
914 status = xpt_create_path(&path, /*periph*/ periph,
916 cgd->ccb_h.target_id, 0);
919 * If we were able to allocate the path, and if we
920 * find a matching device and it isn't already
921 * marked as part of a changer, then we add it to
922 * the current changer.
924 if ((status == CAM_REQ_CMP)
925 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
926 && ((((struct cd_softc *)periph->softc)->flags &
927 CD_FLAG_CHANGER) == 0)) {
928 struct cd_softc *nsoftc;
930 nsoftc = (struct cd_softc *)nperiph->softc;
932 nsoftc->flags |= CD_FLAG_CHANGER;
933 nchanger->num_devices++;
934 if (camq_resize(&nchanger->devq,
935 nchanger->num_devices) != CAM_REQ_CMP) {
936 printf("cdregister: camq_resize "
937 "failed, changer support may "
940 nsoftc->changer = nchanger;
941 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
943 STAILQ_INSERT_TAIL(&nchanger->chluns,
944 nsoftc, changer_links);
946 } else if (status == CAM_REQ_CMP)
949 printf("cdregister: unable to allocate path\n"
950 "cdregister: changer support may be "
954 softc->changer = nchanger;
955 softc->pinfo.index = CAM_UNQUEUED_INDEX;
956 nchanger->num_devices++;
957 if (camq_resize(&nchanger->devq,
958 nchanger->num_devices) != CAM_REQ_CMP) {
959 printf("cdregister: camq_resize "
960 "failed, changer support may "
963 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
971 * Refcount and block open attempts until we are setup
974 (void)cam_periph_hold(periph, PRIBIO);
976 if ((softc->flags & CD_FLAG_CHANGER) == 0)
977 xpt_schedule(periph, CAM_PRIORITY_DEV);
979 cdschedule(periph, CAM_PRIORITY_DEV);
985 cdopen(struct disk *dp)
987 struct cam_periph *periph;
988 struct cd_softc *softc;
991 periph = (struct cam_periph *)dp->d_drv1;
995 softc = (struct cd_softc *)periph->softc;
997 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1000 cam_periph_lock(periph);
1002 if (softc->flags & CD_FLAG_INVALID) {
1003 cam_periph_unlock(periph);
1004 cam_periph_release(periph);
1008 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1009 cam_periph_unlock(periph);
1010 cam_periph_release(periph);
1014 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1018 * Check for media, and set the appropriate flags. We don't bail
1019 * if we don't have media, but then we don't allow anything but the
1020 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1022 cdcheckmedia(periph);
1024 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1025 cam_periph_unhold(periph);
1027 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1028 if ((softc->flags & CD_FLAG_OPEN) == 0) {
1029 softc->flags |= CD_FLAG_OPEN;
1030 cam_periph_unlock(periph);
1032 cam_periph_unlock(periph);
1033 cam_periph_release(periph);
1040 cdclose(struct disk *dp)
1042 struct cam_periph *periph;
1043 struct cd_softc *softc;
1045 periph = (struct cam_periph *)dp->d_drv1;
1049 softc = (struct cd_softc *)periph->softc;
1051 cam_periph_lock(periph);
1052 if (cam_periph_hold(periph, PRIBIO) != 0) {
1053 cam_periph_unlock(periph);
1054 cam_periph_release(periph);
1058 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1061 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1062 cdprevent(periph, PR_ALLOW);
1065 * Since we're closing this CD, mark the blocksize as unavailable.
1066 * It will be marked as available when the CD is opened again.
1068 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1071 * We'll check the media and toc again at the next open().
1073 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1075 cam_periph_unhold(periph);
1076 cam_periph_unlock(periph);
1077 cam_periph_release(periph);
1083 cdshorttimeout(void *arg)
1085 struct cdchanger *changer;
1087 changer = (struct cdchanger *)arg;
1089 /* Always clear the short timeout flag, since that's what we're in */
1090 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1093 * Check to see if there is any more pending or outstanding I/O for
1094 * this device. If not, move it out of the active slot.
1096 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1097 && (changer->cur_device->outstanding_cmds == 0)) {
1098 changer->flags |= CHANGER_MANUAL_CALL;
1099 cdrunchangerqueue(changer);
1104 * This is a wrapper for xpt_schedule. It only applies to changers.
1107 cdschedule(struct cam_periph *periph, int priority)
1109 struct cd_softc *softc;
1111 softc = (struct cd_softc *)periph->softc;
1114 * If this device isn't currently queued, and if it isn't
1115 * the active device, then we queue this device and run the
1116 * changer queue if there is no timeout scheduled to do it.
1117 * If this device is the active device, just schedule it
1118 * to run again. If this device is queued, there should be
1119 * a timeout in place already that will make sure it runs.
1121 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1122 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1124 * We don't do anything with the priority here.
1125 * This is strictly a fifo queue.
1127 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1128 softc->pinfo.generation = ++softc->changer->devq.generation;
1129 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1132 * Since we just put a device in the changer queue,
1133 * check and see if there is a timeout scheduled for
1134 * this changer. If so, let the timeout handle
1135 * switching this device into the active slot. If
1136 * not, manually call the timeout routine to
1139 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1140 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1141 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1142 softc->changer->flags |= CHANGER_MANUAL_CALL;
1143 cdrunchangerqueue(softc->changer);
1145 } else if ((softc->flags & CD_FLAG_ACTIVE)
1146 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1147 xpt_schedule(periph, priority);
1151 cdrunchangerqueue(void *arg)
1153 struct cd_softc *softc;
1154 struct cdchanger *changer;
1155 int called_from_timeout;
1157 changer = (struct cdchanger *)arg;
1160 * If we have NOT been called from cdstrategy() or cddone(), and
1161 * instead from a timeout routine, go ahead and clear the
1164 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1165 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1166 called_from_timeout = 1;
1168 called_from_timeout = 0;
1170 /* Always clear the manual call flag */
1171 changer->flags &= ~CHANGER_MANUAL_CALL;
1173 /* nothing to do if the queue is empty */
1174 if (changer->devq.entries <= 0) {
1179 * If the changer queue is frozen, that means we have an active
1182 if (changer->devq.qfrozen_cnt[0] > 0) {
1185 * We always need to reset the frozen count and clear the
1188 changer->devq.qfrozen_cnt[0]--;
1189 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1190 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1192 if (changer->cur_device->outstanding_cmds > 0) {
1193 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1194 changer->cur_device->bufs_left =
1195 changer->cur_device->outstanding_cmds;
1196 if (called_from_timeout) {
1197 callout_reset(&changer->long_handle,
1198 changer_max_busy_seconds * hz,
1199 cdrunchangerqueue, changer);
1200 changer->flags |= CHANGER_TIMEOUT_SCHED;
1206 * Check to see whether the current device has any I/O left
1207 * to do. If so, requeue it at the end of the queue. If
1208 * not, there is no need to requeue it.
1210 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1212 changer->cur_device->pinfo.generation =
1213 ++changer->devq.generation;
1214 camq_insert(&changer->devq,
1215 (cam_pinfo *)changer->cur_device);
1219 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1221 changer->cur_device = softc;
1223 changer->devq.qfrozen_cnt[0]++;
1224 softc->flags |= CD_FLAG_ACTIVE;
1226 /* Just in case this device is waiting */
1227 wakeup(&softc->changer);
1228 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1231 * Get rid of any pending timeouts, and set a flag to schedule new
1232 * ones so this device gets its full time quantum.
1234 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1235 callout_stop(&changer->long_handle);
1236 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1239 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1240 callout_stop(&changer->short_handle);
1241 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1245 * We need to schedule timeouts, but we only do this after the
1246 * first transaction has completed. This eliminates the changer
1249 changer->flags |= CHANGER_NEED_TIMEOUT;
1253 cdchangerschedule(struct cd_softc *softc)
1255 struct cdchanger *changer;
1257 changer = softc->changer;
1260 * If this is a changer, and this is the current device,
1261 * and this device has at least the minimum time quantum to
1262 * run, see if we can switch it out.
1264 if ((softc->flags & CD_FLAG_ACTIVE)
1265 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1266 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1268 * We try three things here. The first is that we
1269 * check to see whether the schedule on completion
1270 * flag is set. If it is, we decrement the number
1271 * of buffers left, and if it's zero, we reschedule.
1272 * Next, we check to see whether the pending buffer
1273 * queue is empty and whether there are no
1274 * outstanding transactions. If so, we reschedule.
1275 * Next, we see if the pending buffer queue is empty.
1276 * If it is, we set the number of buffers left to
1277 * the current active buffer count and set the
1278 * schedule on complete flag.
1280 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1281 if (--softc->bufs_left == 0) {
1282 softc->changer->flags |=
1283 CHANGER_MANUAL_CALL;
1284 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1285 cdrunchangerqueue(softc->changer);
1287 } else if ((bioq_first(&softc->bio_queue) == NULL)
1288 && (softc->outstanding_cmds == 0)) {
1289 softc->changer->flags |= CHANGER_MANUAL_CALL;
1290 cdrunchangerqueue(softc->changer);
1292 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1293 && (softc->flags & CD_FLAG_ACTIVE)) {
1296 * Now that the first transaction to this
1297 * particular device has completed, we can go ahead
1298 * and schedule our timeouts.
1300 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1301 callout_reset(&changer->long_handle,
1302 changer_max_busy_seconds * hz,
1303 cdrunchangerqueue, changer);
1304 changer->flags |= CHANGER_TIMEOUT_SCHED;
1306 printf("cdchangerschedule: already have a long"
1309 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1310 callout_reset(&changer->short_handle,
1311 changer_min_busy_seconds * hz,
1312 cdshorttimeout, changer);
1313 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1315 printf("cdchangerschedule: already have a short "
1319 * We just scheduled timeouts, no need to schedule
1322 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1328 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1329 u_int32_t cam_flags,
1330 u_int32_t sense_flags),
1331 u_int32_t cam_flags, u_int32_t sense_flags)
1333 struct cd_softc *softc;
1334 struct cam_periph *periph;
1337 periph = xpt_path_periph(ccb->ccb_h.path);
1338 softc = (struct cd_softc *)periph->softc;
1340 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1341 softc->disk->d_devstat);
1343 if (softc->flags & CD_FLAG_CHANGER)
1344 cdchangerschedule(softc);
1350 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1352 struct cd_softc *softc;
1354 softc = (struct cd_softc *)periph->softc;
1356 if (softc->flags & CD_FLAG_CHANGER) {
1358 * This should work the first time this device is woken up,
1359 * but just in case it doesn't, we use a while loop.
1361 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1363 * If this changer isn't already queued, queue it up.
1365 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1366 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1367 softc->pinfo.generation =
1368 ++softc->changer->devq.generation;
1369 camq_insert(&softc->changer->devq,
1370 (cam_pinfo *)softc);
1372 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1373 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1374 && ((softc->changer->flags
1375 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1376 softc->changer->flags |= CHANGER_MANUAL_CALL;
1377 cdrunchangerqueue(softc->changer);
1379 msleep(&softc->changer, periph->sim->mtx,
1380 PRIBIO, "cgticb", 0);
1383 return(cam_periph_getccb(periph, priority));
1388 * Actually translate the requested transfer into one the physical driver
1389 * can understand. The transfer is described by a buf and will include
1390 * only one physical transfer.
1393 cdstrategy(struct bio *bp)
1395 struct cam_periph *periph;
1396 struct cd_softc *softc;
1398 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1399 if (periph == NULL) {
1400 biofinish(bp, NULL, ENXIO);
1404 cam_periph_lock(periph);
1405 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1406 ("cdstrategy(%p)\n", bp));
1408 softc = (struct cd_softc *)periph->softc;
1411 * If the device has been made invalid, error out
1413 if ((softc->flags & CD_FLAG_INVALID)) {
1414 cam_periph_unlock(periph);
1415 biofinish(bp, NULL, ENXIO);
1420 * If we don't have valid media, look for it before trying to
1423 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1426 error = cdcheckmedia(periph);
1428 cam_periph_unlock(periph);
1429 biofinish(bp, NULL, error);
1435 * Place it in the queue of disk activities for this disk
1437 bioq_disksort(&softc->bio_queue, bp);
1440 * Schedule ourselves for performing the work. We do things
1441 * differently for changers.
1443 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1444 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1446 cdschedule(periph, CAM_PRIORITY_NORMAL);
1448 cam_periph_unlock(periph);
1453 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1455 struct cd_softc *softc;
1457 struct ccb_scsiio *csio;
1458 struct scsi_read_capacity_data *rcap;
1460 softc = (struct cd_softc *)periph->softc;
1462 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1464 switch (softc->state) {
1465 case CD_STATE_NORMAL:
1467 bp = bioq_first(&softc->bio_queue);
1468 if (periph->immediate_priority <= periph->pinfo.priority) {
1469 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1471 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1473 periph->immediate_priority = CAM_PRIORITY_NONE;
1474 wakeup(&periph->ccb_list);
1475 } else if (bp == NULL) {
1476 xpt_release_ccb(start_ccb);
1478 bioq_remove(&softc->bio_queue, bp);
1480 scsi_read_write(&start_ccb->csio,
1481 /*retries*/ cd_retry_count,
1482 /* cbfcnp */ cddone,
1484 /* read */bp->bio_cmd == BIO_READ,
1486 /* minimum_cmd_size */ 10,
1487 /* lba */ bp->bio_offset /
1488 softc->params.blksize,
1489 bp->bio_bcount / softc->params.blksize,
1490 /* data_ptr */ bp->bio_data,
1491 /* dxfer_len */ bp->bio_bcount,
1492 /* sense_len */ SSD_FULL_SIZE,
1493 /* timeout */ 30000);
1494 /* Use READ CD command for audio tracks. */
1495 if (softc->params.blksize == 2352) {
1496 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1497 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1498 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1499 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1500 start_ccb->csio.cdb_len = 12;
1502 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1505 LIST_INSERT_HEAD(&softc->pending_ccbs,
1506 &start_ccb->ccb_h, periph_links.le);
1507 softc->outstanding_cmds++;
1509 /* We expect a unit attention from this device */
1510 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1511 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1512 softc->flags &= ~CD_FLAG_RETRY_UA;
1515 start_ccb->ccb_h.ccb_bp = bp;
1516 bp = bioq_first(&softc->bio_queue);
1518 xpt_action(start_ccb);
1521 /* Have more work to do, so ensure we stay scheduled */
1522 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1526 case CD_STATE_PROBE:
1529 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1530 M_SCSICD, M_NOWAIT | M_ZERO);
1532 xpt_print(periph->path,
1533 "cdstart: Couldn't malloc read_capacity data\n");
1534 /* cd_free_periph??? */
1537 csio = &start_ccb->csio;
1538 scsi_read_capacity(csio,
1539 /*retries*/ cd_retry_count,
1545 start_ccb->ccb_h.ccb_bp = NULL;
1546 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1547 xpt_action(start_ccb);
1554 cddone(struct cam_periph *periph, union ccb *done_ccb)
1556 struct cd_softc *softc;
1557 struct ccb_scsiio *csio;
1559 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1561 softc = (struct cd_softc *)periph->softc;
1562 csio = &done_ccb->csio;
1564 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1565 case CD_CCB_BUFFER_IO:
1570 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1573 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1576 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1581 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1582 if (error == ERESTART) {
1584 * A retry was scheuled, so
1592 xpt_print(periph->path,
1593 "cddone: got error %#x back\n", error);
1594 bioq_flush(&softc->bio_queue, NULL, EIO);
1595 bp->bio_resid = bp->bio_bcount;
1596 bp->bio_error = error;
1597 bp->bio_flags |= BIO_ERROR;
1598 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1599 cam_release_devq(done_ccb->ccb_h.path,
1603 /*getcount_only*/0);
1606 bp->bio_resid = csio->resid;
1608 if (bp->bio_resid != 0) {
1610 * Short transfer ???
1611 * XXX: not sure this is correct for partial
1614 bp->bio_flags |= BIO_ERROR;
1618 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1619 softc->outstanding_cmds--;
1621 if (softc->flags & CD_FLAG_CHANGER)
1622 cdchangerschedule(softc);
1624 biofinish(bp, NULL, 0);
1629 struct scsi_read_capacity_data *rdcap;
1630 char announce_buf[120]; /*
1631 * Currently (9/30/97) the
1632 * longest possible announce
1633 * buffer is 108 bytes, for the
1634 * first error case below.
1635 * That is 39 bytes for the
1636 * basic string, 16 bytes for the
1637 * biggest sense key (hardware
1638 * error), 52 bytes for the
1639 * text of the largest sense
1640 * qualifier valid for a CDROM,
1641 * (0x72, 0x03 or 0x04,
1642 * 0x03), and one byte for the
1643 * null terminating character.
1644 * To allow for longer strings,
1645 * the announce buffer is 120
1648 struct cd_params *cdp;
1650 cdp = &softc->params;
1652 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1654 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1655 cdp->blksize = scsi_4btoul (rdcap->length);
1657 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1659 snprintf(announce_buf, sizeof(announce_buf),
1660 "cd present [%lu x %lu byte records]",
1661 cdp->disksize, (u_long)cdp->blksize);
1666 * Retry any UNIT ATTENTION type errors. They
1667 * are expected at boot.
1669 error = cderror(done_ccb, CAM_RETRY_SELTO,
1670 SF_RETRY_UA | SF_NO_PRINT);
1671 if (error == ERESTART) {
1673 * A retry was scheuled, so
1677 } else if (error != 0) {
1679 struct scsi_sense_data *sense;
1681 int sense_key, error_code;
1684 struct ccb_getdev cgd;
1686 /* Don't wedge this device's queue */
1687 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1688 cam_release_devq(done_ccb->ccb_h.path,
1692 /*getcount_only*/0);
1694 status = done_ccb->ccb_h.status;
1696 xpt_setup_ccb(&cgd.ccb_h,
1697 done_ccb->ccb_h.path,
1698 CAM_PRIORITY_NORMAL);
1699 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1700 xpt_action((union ccb *)&cgd);
1702 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1703 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1704 || ((status & CAM_AUTOSNS_VALID) == 0))
1710 sense = &csio->sense_data;
1711 scsi_extract_sense(sense, &error_code,
1716 * Attach to anything that claims to be a
1717 * CDROM or WORM device, as long as it
1718 * doesn't return a "Logical unit not
1719 * supported" (0x25) error.
1721 if ((have_sense) && (asc != 0x25)
1722 && (error_code == SSD_CURRENT_ERROR)) {
1723 const char *sense_key_desc;
1724 const char *asc_desc;
1726 scsi_sense_desc(sense_key, asc, ascq,
1730 snprintf(announce_buf,
1731 sizeof(announce_buf),
1732 "Attempt to query device "
1733 "size failed: %s, %s",
1736 } else if ((have_sense == 0)
1737 && ((status & CAM_STATUS_MASK) ==
1738 CAM_SCSI_STATUS_ERROR)
1739 && (csio->scsi_status ==
1740 SCSI_STATUS_BUSY)) {
1741 snprintf(announce_buf,
1742 sizeof(announce_buf),
1743 "Attempt to query device "
1744 "size failed: SCSI Status: %s",
1745 scsi_status_string(csio));
1746 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1748 * We only print out an error for
1749 * CDROM type devices. For WORM
1750 * devices, we don't print out an
1751 * error since a few WORM devices
1752 * don't support CDROM commands.
1753 * If we have sense information, go
1754 * ahead and print it out.
1755 * Otherwise, just say that we
1760 * Just print out the error, not
1761 * the full probe message, when we
1768 xpt_print(periph->path,
1769 "got CAM status %#x\n",
1770 done_ccb->ccb_h.status);
1772 xpt_print(periph->path, "fatal error, "
1773 "failed to attach to device\n");
1775 * Invalidate this peripheral.
1777 cam_periph_invalidate(periph);
1779 announce_buf[0] = '\0';
1783 * Invalidate this peripheral.
1785 cam_periph_invalidate(periph);
1786 announce_buf[0] = '\0';
1790 free(rdcap, M_SCSICD);
1791 if (announce_buf[0] != '\0') {
1792 xpt_announce_periph(periph, announce_buf);
1793 xpt_announce_quirks(periph, softc->quirks,
1795 if (softc->flags & CD_FLAG_CHANGER)
1796 cdchangerschedule(softc);
1798 * Create our sysctl variables, now that we know
1799 * we have successfully attached.
1801 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1803 softc->state = CD_STATE_NORMAL;
1805 * Since our peripheral may be invalidated by an error
1806 * above or an external event, we must release our CCB
1807 * before releasing the probe lock on the peripheral.
1808 * The peripheral will only go away once the last lock
1809 * is removed, and we need it around for the CCB release
1812 xpt_release_ccb(done_ccb);
1813 cam_periph_unhold(periph);
1816 case CD_CCB_WAITING:
1818 /* Caller will release the CCB */
1819 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1820 ("trying to wakeup ccbwait\n"));
1822 wakeup(&done_ccb->ccb_h.cbfcnp);
1828 xpt_release_ccb(done_ccb);
1831 static union cd_pages *
1832 cdgetpage(struct cd_mode_params *mode_params)
1834 union cd_pages *page;
1836 if (mode_params->cdb_size == 10)
1837 page = (union cd_pages *)find_mode_page_10(
1838 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1840 page = (union cd_pages *)find_mode_page_6(
1841 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1847 cdgetpagesize(int page_num)
1851 for (i = 0; i < (sizeof(cd_page_size_table)/
1852 sizeof(cd_page_size_table[0])); i++) {
1853 if (cd_page_size_table[i].page == page_num)
1854 return (cd_page_size_table[i].page_size);
1861 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1864 struct cam_periph *periph;
1865 struct cd_softc *softc;
1866 int nocopyout, error = 0;
1868 periph = (struct cam_periph *)dp->d_drv1;
1872 cam_periph_lock(periph);
1874 softc = (struct cd_softc *)periph->softc;
1876 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1877 ("cdioctl(%#lx)\n", cmd));
1879 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1880 cam_periph_unlock(periph);
1881 cam_periph_release(periph);
1886 * If we don't have media loaded, check for it. If still don't
1887 * have media loaded, we can only do a load or eject.
1889 * We only care whether media is loaded if this is a cd-specific ioctl
1890 * (thus the IOCGROUP check below). Note that this will break if
1891 * anyone adds any ioctls into the switch statement below that don't
1892 * have their ioctl group set to 'c'.
1894 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1895 && ((cmd != CDIOCCLOSE)
1896 && (cmd != CDIOCEJECT))
1897 && (IOCGROUP(cmd) == 'c')) {
1898 error = cdcheckmedia(periph);
1900 cam_periph_unhold(periph);
1901 cam_periph_unlock(periph);
1906 * Drop the lock here so later mallocs can use WAITOK. The periph
1907 * is essentially locked still with the cam_periph_hold call above.
1909 cam_periph_unlock(periph);
1914 case CDIOCPLAYTRACKS:
1916 struct ioc_play_track *args
1917 = (struct ioc_play_track *) addr;
1918 struct cd_mode_params params;
1919 union cd_pages *page;
1921 params.alloc_len = sizeof(union cd_mode_data_6_10);
1922 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1925 cam_periph_lock(periph);
1926 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1927 ("trying to do CDIOCPLAYTRACKS\n"));
1929 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1931 free(params.mode_buf, M_SCSICD);
1932 cam_periph_unlock(periph);
1935 page = cdgetpage(¶ms);
1937 page->audio.flags &= ~CD_PA_SOTC;
1938 page->audio.flags |= CD_PA_IMMED;
1939 error = cdsetmode(periph, ¶ms);
1940 free(params.mode_buf, M_SCSICD);
1942 cam_periph_unlock(periph);
1947 * This was originally implemented with the PLAY
1948 * AUDIO TRACK INDEX command, but that command was
1949 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1950 * drives support it but ATAPI and ATAPI-derivative
1951 * drives don't seem to support it. So we keep a
1952 * cache of the table of contents and translate
1953 * track numbers to MSF format.
1955 if (softc->flags & CD_FLAG_VALID_TOC) {
1956 union msf_lba *sentry, *eentry;
1959 if (args->end_track <
1960 softc->toc.header.ending_track + 1)
1962 if (args->end_track >
1963 softc->toc.header.ending_track + 1)
1965 softc->toc.header.ending_track + 1;
1966 st = args->start_track -
1967 softc->toc.header.starting_track;
1968 et = args->end_track -
1969 softc->toc.header.starting_track;
1972 || (st > (softc->toc.header.ending_track -
1973 softc->toc.header.starting_track))) {
1975 cam_periph_unlock(periph);
1978 sentry = &softc->toc.entries[st].addr;
1979 eentry = &softc->toc.entries[et].addr;
1980 error = cdplaymsf(periph,
1989 * If we don't have a valid TOC, try the
1990 * play track index command. It is part of
1991 * the SCSI-2 spec, but was removed in the
1992 * MMC specs. ATAPI and ATAPI-derived
1993 * drives don't support it.
1995 if (softc->quirks & CD_Q_BCD_TRACKS) {
1997 bin2bcd(args->start_track);
1999 bin2bcd(args->end_track);
2001 error = cdplaytracks(periph,
2007 cam_periph_unlock(periph);
2012 struct ioc_play_msf *args
2013 = (struct ioc_play_msf *) addr;
2014 struct cd_mode_params params;
2015 union cd_pages *page;
2017 params.alloc_len = sizeof(union cd_mode_data_6_10);
2018 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2021 cam_periph_lock(periph);
2022 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2023 ("trying to do CDIOCPLAYMSF\n"));
2025 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2027 free(params.mode_buf, M_SCSICD);
2028 cam_periph_unlock(periph);
2031 page = cdgetpage(¶ms);
2033 page->audio.flags &= ~CD_PA_SOTC;
2034 page->audio.flags |= CD_PA_IMMED;
2035 error = cdsetmode(periph, ¶ms);
2036 free(params.mode_buf, M_SCSICD);
2038 cam_periph_unlock(periph);
2041 error = cdplaymsf(periph,
2048 cam_periph_unlock(periph);
2051 case CDIOCPLAYBLOCKS:
2053 struct ioc_play_blocks *args
2054 = (struct ioc_play_blocks *) addr;
2055 struct cd_mode_params params;
2056 union cd_pages *page;
2058 params.alloc_len = sizeof(union cd_mode_data_6_10);
2059 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2062 cam_periph_lock(periph);
2063 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2064 ("trying to do CDIOCPLAYBLOCKS\n"));
2067 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2069 free(params.mode_buf, M_SCSICD);
2070 cam_periph_unlock(periph);
2073 page = cdgetpage(¶ms);
2075 page->audio.flags &= ~CD_PA_SOTC;
2076 page->audio.flags |= CD_PA_IMMED;
2077 error = cdsetmode(periph, ¶ms);
2078 free(params.mode_buf, M_SCSICD);
2080 cam_periph_unlock(periph);
2083 error = cdplay(periph, args->blk, args->len);
2084 cam_periph_unlock(periph);
2087 case CDIOCREADSUBCHANNEL_SYSSPACE:
2090 case CDIOCREADSUBCHANNEL:
2092 struct ioc_read_subchannel *args
2093 = (struct ioc_read_subchannel *) addr;
2094 struct cd_sub_channel_info *data;
2095 u_int32_t len = args->data_len;
2097 data = malloc(sizeof(struct cd_sub_channel_info),
2098 M_SCSICD, M_WAITOK | M_ZERO);
2100 cam_periph_lock(periph);
2101 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2102 ("trying to do CDIOCREADSUBCHANNEL\n"));
2104 if ((len > sizeof(struct cd_sub_channel_info)) ||
2105 (len < sizeof(struct cd_sub_channel_header))) {
2107 "scsi_cd: cdioctl: "
2108 "cdioreadsubchannel: error, len=%d\n",
2111 free(data, M_SCSICD);
2112 cam_periph_unlock(periph);
2116 if (softc->quirks & CD_Q_BCD_TRACKS)
2117 args->track = bin2bcd(args->track);
2119 error = cdreadsubchannel(periph, args->address_format,
2120 args->data_format, args->track, data, len);
2123 free(data, M_SCSICD);
2124 cam_periph_unlock(periph);
2127 if (softc->quirks & CD_Q_BCD_TRACKS)
2128 data->what.track_info.track_number =
2129 bcd2bin(data->what.track_info.track_number);
2130 len = min(len, ((data->header.data_len[0] << 8) +
2131 data->header.data_len[1] +
2132 sizeof(struct cd_sub_channel_header)));
2133 cam_periph_unlock(periph);
2134 if (nocopyout == 0) {
2135 if (copyout(data, args->data, len) != 0) {
2139 bcopy(data, args->data, len);
2141 free(data, M_SCSICD);
2145 case CDIOREADTOCHEADER:
2147 struct ioc_toc_header *th;
2149 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2152 cam_periph_lock(periph);
2153 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2154 ("trying to do CDIOREADTOCHEADER\n"));
2156 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2157 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2160 cam_periph_unlock(periph);
2163 if (softc->quirks & CD_Q_BCD_TRACKS) {
2164 /* we are going to have to convert the BCD
2165 * encoding on the cd to what is expected
2167 th->starting_track =
2168 bcd2bin(th->starting_track);
2169 th->ending_track = bcd2bin(th->ending_track);
2171 th->len = ntohs(th->len);
2172 bcopy(th, addr, sizeof(*th));
2174 cam_periph_unlock(periph);
2177 case CDIOREADTOCENTRYS:
2179 struct cd_tocdata *data;
2180 struct cd_toc_single *lead;
2181 struct ioc_read_toc_entry *te =
2182 (struct ioc_read_toc_entry *) addr;
2183 struct ioc_toc_header *th;
2184 u_int32_t len, readlen, idx, num;
2185 u_int32_t starting_track = te->starting_track;
2187 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2188 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2190 cam_periph_lock(periph);
2191 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2192 ("trying to do CDIOREADTOCENTRYS\n"));
2194 if (te->data_len < sizeof(struct cd_toc_entry)
2195 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2196 || (te->address_format != CD_MSF_FORMAT
2197 && te->address_format != CD_LBA_FORMAT)) {
2199 printf("scsi_cd: error in readtocentries, "
2200 "returning EINVAL\n");
2201 free(data, M_SCSICD);
2202 free(lead, M_SCSICD);
2203 cam_periph_unlock(periph);
2208 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2209 sizeof (*th), /*sense_flags*/0);
2211 free(data, M_SCSICD);
2212 free(lead, M_SCSICD);
2213 cam_periph_unlock(periph);
2217 if (softc->quirks & CD_Q_BCD_TRACKS) {
2218 /* we are going to have to convert the BCD
2219 * encoding on the cd to what is expected
2221 th->starting_track =
2222 bcd2bin(th->starting_track);
2223 th->ending_track = bcd2bin(th->ending_track);
2226 if (starting_track == 0)
2227 starting_track = th->starting_track;
2228 else if (starting_track == LEADOUT)
2229 starting_track = th->ending_track + 1;
2230 else if (starting_track < th->starting_track ||
2231 starting_track > th->ending_track + 1) {
2232 printf("scsi_cd: error in readtocentries, "
2233 "returning EINVAL\n");
2234 free(data, M_SCSICD);
2235 free(lead, M_SCSICD);
2236 cam_periph_unlock(periph);
2241 /* calculate reading length without leadout entry */
2242 readlen = (th->ending_track - starting_track + 1) *
2243 sizeof(struct cd_toc_entry);
2245 /* and with leadout entry */
2246 len = readlen + sizeof(struct cd_toc_entry);
2247 if (te->data_len < len) {
2252 if (len > sizeof(data->entries)) {
2253 printf("scsi_cd: error in readtocentries, "
2254 "returning EINVAL\n");
2256 free(data, M_SCSICD);
2257 free(lead, M_SCSICD);
2258 cam_periph_unlock(periph);
2261 num = len / sizeof(struct cd_toc_entry);
2264 error = cdreadtoc(periph, te->address_format,
2267 readlen + sizeof (*th),
2270 free(data, M_SCSICD);
2271 free(lead, M_SCSICD);
2272 cam_periph_unlock(periph);
2277 /* make leadout entry if needed */
2278 idx = starting_track + num - 1;
2279 if (softc->quirks & CD_Q_BCD_TRACKS)
2280 th->ending_track = bcd2bin(th->ending_track);
2281 if (idx == th->ending_track + 1) {
2282 error = cdreadtoc(periph, te->address_format,
2283 LEADOUT, (u_int8_t *)lead,
2287 free(data, M_SCSICD);
2288 free(lead, M_SCSICD);
2289 cam_periph_unlock(periph);
2292 data->entries[idx - starting_track] =
2295 if (softc->quirks & CD_Q_BCD_TRACKS) {
2296 for (idx = 0; idx < num - 1; idx++) {
2297 data->entries[idx].track =
2298 bcd2bin(data->entries[idx].track);
2302 cam_periph_unlock(periph);
2303 error = copyout(data->entries, te->data, len);
2304 free(data, M_SCSICD);
2305 free(lead, M_SCSICD);
2308 case CDIOREADTOCENTRY:
2310 struct cd_toc_single *data;
2311 struct ioc_read_toc_single_entry *te =
2312 (struct ioc_read_toc_single_entry *) addr;
2313 struct ioc_toc_header *th;
2316 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2318 cam_periph_lock(periph);
2319 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2320 ("trying to do CDIOREADTOCENTRY\n"));
2322 if (te->address_format != CD_MSF_FORMAT
2323 && te->address_format != CD_LBA_FORMAT) {
2324 printf("error in readtocentry, "
2325 " returning EINVAL\n");
2326 free(data, M_SCSICD);
2328 cam_periph_unlock(periph);
2333 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2334 sizeof (*th), /*sense_flags*/0);
2336 free(data, M_SCSICD);
2337 cam_periph_unlock(periph);
2341 if (softc->quirks & CD_Q_BCD_TRACKS) {
2342 /* we are going to have to convert the BCD
2343 * encoding on the cd to what is expected
2345 th->starting_track =
2346 bcd2bin(th->starting_track);
2347 th->ending_track = bcd2bin(th->ending_track);
2351 track = th->starting_track;
2352 else if (track == LEADOUT)
2354 else if (track < th->starting_track ||
2355 track > th->ending_track + 1) {
2356 printf("error in readtocentry, "
2357 " returning EINVAL\n");
2358 free(data, M_SCSICD);
2360 cam_periph_unlock(periph);
2364 error = cdreadtoc(periph, te->address_format, track,
2365 (u_int8_t *)data, sizeof(*data),
2368 free(data, M_SCSICD);
2369 cam_periph_unlock(periph);
2373 if (softc->quirks & CD_Q_BCD_TRACKS)
2374 data->entry.track = bcd2bin(data->entry.track);
2375 bcopy(&data->entry, &te->entry,
2376 sizeof(struct cd_toc_entry));
2377 free(data, M_SCSICD);
2378 cam_periph_unlock(periph);
2383 struct ioc_patch *arg = (struct ioc_patch *)addr;
2384 struct cd_mode_params params;
2385 union cd_pages *page;
2387 params.alloc_len = sizeof(union cd_mode_data_6_10);
2388 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2391 cam_periph_lock(periph);
2392 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2393 ("trying to do CDIOCSETPATCH\n"));
2395 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2397 free(params.mode_buf, M_SCSICD);
2398 cam_periph_unlock(periph);
2401 page = cdgetpage(¶ms);
2403 page->audio.port[LEFT_PORT].channels =
2405 page->audio.port[RIGHT_PORT].channels =
2407 page->audio.port[2].channels = arg->patch[2];
2408 page->audio.port[3].channels = arg->patch[3];
2409 error = cdsetmode(periph, ¶ms);
2410 free(params.mode_buf, M_SCSICD);
2411 cam_periph_unlock(periph);
2416 struct ioc_vol *arg = (struct ioc_vol *) addr;
2417 struct cd_mode_params params;
2418 union cd_pages *page;
2420 params.alloc_len = sizeof(union cd_mode_data_6_10);
2421 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2424 cam_periph_lock(periph);
2425 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2426 ("trying to do CDIOCGETVOL\n"));
2428 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2430 free(params.mode_buf, M_SCSICD);
2431 cam_periph_unlock(periph);
2434 page = cdgetpage(¶ms);
2436 arg->vol[LEFT_PORT] =
2437 page->audio.port[LEFT_PORT].volume;
2438 arg->vol[RIGHT_PORT] =
2439 page->audio.port[RIGHT_PORT].volume;
2440 arg->vol[2] = page->audio.port[2].volume;
2441 arg->vol[3] = page->audio.port[3].volume;
2442 free(params.mode_buf, M_SCSICD);
2443 cam_periph_unlock(periph);
2448 struct ioc_vol *arg = (struct ioc_vol *) addr;
2449 struct cd_mode_params params;
2450 union cd_pages *page;
2452 params.alloc_len = sizeof(union cd_mode_data_6_10);
2453 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2456 cam_periph_lock(periph);
2457 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2458 ("trying to do CDIOCSETVOL\n"));
2460 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2462 free(params.mode_buf, M_SCSICD);
2463 cam_periph_unlock(periph);
2466 page = cdgetpage(¶ms);
2468 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2469 page->audio.port[LEFT_PORT].volume =
2470 arg->vol[LEFT_PORT];
2471 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2472 page->audio.port[RIGHT_PORT].volume =
2473 arg->vol[RIGHT_PORT];
2474 page->audio.port[2].volume = arg->vol[2];
2475 page->audio.port[3].volume = arg->vol[3];
2476 error = cdsetmode(periph, ¶ms);
2477 cam_periph_unlock(periph);
2478 free(params.mode_buf, M_SCSICD);
2483 struct cd_mode_params params;
2484 union cd_pages *page;
2486 params.alloc_len = sizeof(union cd_mode_data_6_10);
2487 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2490 cam_periph_lock(periph);
2491 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2492 ("trying to do CDIOCSETMONO\n"));
2494 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2496 free(params.mode_buf, M_SCSICD);
2497 cam_periph_unlock(periph);
2500 page = cdgetpage(¶ms);
2502 page->audio.port[LEFT_PORT].channels =
2503 LEFT_CHANNEL | RIGHT_CHANNEL;
2504 page->audio.port[RIGHT_PORT].channels =
2505 LEFT_CHANNEL | RIGHT_CHANNEL;
2506 page->audio.port[2].channels = 0;
2507 page->audio.port[3].channels = 0;
2508 error = cdsetmode(periph, ¶ms);
2509 cam_periph_unlock(periph);
2510 free(params.mode_buf, M_SCSICD);
2513 case CDIOCSETSTEREO:
2515 struct cd_mode_params params;
2516 union cd_pages *page;
2518 params.alloc_len = sizeof(union cd_mode_data_6_10);
2519 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2522 cam_periph_lock(periph);
2523 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2524 ("trying to do CDIOCSETSTEREO\n"));
2526 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2528 free(params.mode_buf, M_SCSICD);
2529 cam_periph_unlock(periph);
2532 page = cdgetpage(¶ms);
2534 page->audio.port[LEFT_PORT].channels =
2536 page->audio.port[RIGHT_PORT].channels =
2538 page->audio.port[2].channels = 0;
2539 page->audio.port[3].channels = 0;
2540 error = cdsetmode(periph, ¶ms);
2541 free(params.mode_buf, M_SCSICD);
2542 cam_periph_unlock(periph);
2547 struct cd_mode_params params;
2548 union cd_pages *page;
2550 params.alloc_len = sizeof(union cd_mode_data_6_10);
2551 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2554 cam_periph_lock(periph);
2555 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2556 ("trying to do CDIOCSETMUTE\n"));
2558 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2560 free(params.mode_buf, M_SCSICD);
2561 cam_periph_unlock(periph);
2564 page = cdgetpage(¶ms);
2566 page->audio.port[LEFT_PORT].channels = 0;
2567 page->audio.port[RIGHT_PORT].channels = 0;
2568 page->audio.port[2].channels = 0;
2569 page->audio.port[3].channels = 0;
2570 error = cdsetmode(periph, ¶ms);
2571 free(params.mode_buf, M_SCSICD);
2572 cam_periph_unlock(periph);
2577 struct cd_mode_params params;
2578 union cd_pages *page;
2580 params.alloc_len = sizeof(union cd_mode_data_6_10);
2581 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2584 cam_periph_lock(periph);
2585 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2586 ("trying to do CDIOCSETLEFT\n"));
2588 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2590 free(params.mode_buf, M_SCSICD);
2591 cam_periph_unlock(periph);
2594 page = cdgetpage(¶ms);
2596 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2597 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2598 page->audio.port[2].channels = 0;
2599 page->audio.port[3].channels = 0;
2600 error = cdsetmode(periph, ¶ms);
2601 free(params.mode_buf, M_SCSICD);
2602 cam_periph_unlock(periph);
2607 struct cd_mode_params params;
2608 union cd_pages *page;
2610 params.alloc_len = sizeof(union cd_mode_data_6_10);
2611 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2614 cam_periph_lock(periph);
2615 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2616 ("trying to do CDIOCSETRIGHT\n"));
2618 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2620 free(params.mode_buf, M_SCSICD);
2621 cam_periph_unlock(periph);
2624 page = cdgetpage(¶ms);
2626 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2627 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2628 page->audio.port[2].channels = 0;
2629 page->audio.port[3].channels = 0;
2630 error = cdsetmode(periph, ¶ms);
2631 free(params.mode_buf, M_SCSICD);
2632 cam_periph_unlock(periph);
2636 cam_periph_lock(periph);
2637 error = cdpause(periph, 1);
2638 cam_periph_unlock(periph);
2641 cam_periph_lock(periph);
2642 error = cdpause(periph, 0);
2643 cam_periph_unlock(periph);
2646 cam_periph_lock(periph);
2647 error = cdstartunit(periph, 0);
2648 cam_periph_unlock(periph);
2651 cam_periph_lock(periph);
2652 error = cdstartunit(periph, 1);
2653 cam_periph_unlock(periph);
2656 cam_periph_lock(periph);
2657 error = cdstopunit(periph, 0);
2658 cam_periph_unlock(periph);
2661 cam_periph_lock(periph);
2662 error = cdstopunit(periph, 1);
2663 cam_periph_unlock(periph);
2666 cam_periph_lock(periph);
2667 cdprevent(periph, PR_ALLOW);
2668 cam_periph_unlock(periph);
2671 cam_periph_lock(periph);
2672 cdprevent(periph, PR_PREVENT);
2673 cam_periph_unlock(periph);
2676 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2680 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2684 /* return (cd_reset(periph)); */
2687 case CDRIOCREADSPEED:
2688 cam_periph_lock(periph);
2689 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2690 cam_periph_unlock(periph);
2692 case CDRIOCWRITESPEED:
2693 cam_periph_lock(periph);
2694 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2695 cam_periph_unlock(periph);
2697 case CDRIOCGETBLOCKSIZE:
2698 *(int *)addr = softc->params.blksize;
2700 case CDRIOCSETBLOCKSIZE:
2701 if (*(int *)addr <= 0) {
2705 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2708 case DVDIOCREPORTKEY: {
2709 struct dvd_authinfo *authinfo;
2711 authinfo = (struct dvd_authinfo *)addr;
2713 if (cmd == DVDIOCREPORTKEY)
2714 error = cdreportkey(periph, authinfo);
2716 error = cdsendkey(periph, authinfo);
2719 case DVDIOCREADSTRUCTURE: {
2720 struct dvd_struct *dvdstruct;
2722 dvdstruct = (struct dvd_struct *)addr;
2724 error = cdreaddvdstructure(periph, dvdstruct);
2729 cam_periph_lock(periph);
2730 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2731 cam_periph_unlock(periph);
2735 cam_periph_lock(periph);
2736 cam_periph_unhold(periph);
2738 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2739 if (error && bootverbose) {
2740 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2742 cam_periph_unlock(periph);
2748 cdprevent(struct cam_periph *periph, int action)
2751 struct cd_softc *softc;
2754 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2756 softc = (struct cd_softc *)periph->softc;
2758 if (((action == PR_ALLOW)
2759 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2760 || ((action == PR_PREVENT)
2761 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2765 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2767 scsi_prevent(&ccb->csio,
2768 /*retries*/ cd_retry_count,
2773 /* timeout */60000);
2775 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2776 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2778 xpt_release_ccb(ccb);
2781 if (action == PR_ALLOW)
2782 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2784 softc->flags |= CD_FLAG_DISC_LOCKED;
2789 * XXX: the disk media and sector size is only really able to change
2790 * XXX: while the device is closed.
2793 cdcheckmedia(struct cam_periph *periph)
2795 struct cd_softc *softc;
2796 struct ioc_toc_header *toch;
2797 struct cd_toc_single leadout;
2798 u_int32_t size, toclen;
2799 int error, num_entries, cdindex;
2801 softc = (struct cd_softc *)periph->softc;
2803 cdprevent(periph, PR_PREVENT);
2804 softc->disk->d_sectorsize = 2048;
2805 softc->disk->d_mediasize = 0;
2808 * Get the disc size and block size. If we can't get it, we don't
2809 * have media, most likely.
2811 if ((error = cdsize(periph, &size)) != 0) {
2812 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2813 cdprevent(periph, PR_ALLOW);
2816 softc->flags |= CD_FLAG_VALID_MEDIA;
2817 softc->disk->d_sectorsize = softc->params.blksize;
2818 softc->disk->d_mediasize =
2819 (off_t)softc->params.blksize * softc->params.disksize;
2823 * Now we check the table of contents. This (currently) is only
2824 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2825 * things like present a separate entry in /dev for each track,
2826 * like that acd(4) driver does.
2828 bzero(&softc->toc, sizeof(softc->toc));
2829 toch = &softc->toc.header;
2831 * We will get errors here for media that doesn't have a table of
2832 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2833 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2834 * has not been recorded (no complete session) and the Format codes
2835 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2836 * with an INVALID FIELD IN CDB. Devices that are not capable of
2837 * reading an incomplete session on DDC/CD-R/RW media shall report
2838 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2840 * So this isn't fatal if we can't read the table of contents, it
2841 * just means that the user won't be able to issue the play tracks
2842 * ioctl, and likely lots of other stuff won't work either. They
2843 * need to burn the CD before we can do a whole lot with it. So
2844 * we don't print anything here if we get an error back.
2846 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2849 * Errors in reading the table of contents aren't fatal, we just
2850 * won't have a valid table of contents cached.
2854 bzero(&softc->toc, sizeof(softc->toc));
2858 if (softc->quirks & CD_Q_BCD_TRACKS) {
2859 toch->starting_track = bcd2bin(toch->starting_track);
2860 toch->ending_track = bcd2bin(toch->ending_track);
2863 /* Number of TOC entries, plus leadout */
2864 num_entries = (toch->ending_track - toch->starting_track) + 2;
2866 if (num_entries <= 0)
2869 toclen = num_entries * sizeof(struct cd_toc_entry);
2871 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2872 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2876 bzero(&softc->toc, sizeof(softc->toc));
2880 if (softc->quirks & CD_Q_BCD_TRACKS) {
2881 toch->starting_track = bcd2bin(toch->starting_track);
2882 toch->ending_track = bcd2bin(toch->ending_track);
2885 * XXX KDM is this necessary? Probably only if the drive doesn't
2886 * return leadout information with the table of contents.
2888 cdindex = toch->starting_track + num_entries -1;
2889 if (cdindex == toch->ending_track + 1) {
2891 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2892 (u_int8_t *)&leadout, sizeof(leadout),
2898 softc->toc.entries[cdindex - toch->starting_track] =
2901 if (softc->quirks & CD_Q_BCD_TRACKS) {
2902 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2903 softc->toc.entries[cdindex].track =
2904 bcd2bin(softc->toc.entries[cdindex].track);
2908 softc->flags |= CD_FLAG_VALID_TOC;
2910 /* If the first track is audio, correct sector size. */
2911 if ((softc->toc.entries[0].control & 4) == 0) {
2912 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2913 softc->disk->d_mediasize =
2914 (off_t)softc->params.blksize * softc->params.disksize;
2920 * We unconditionally (re)set the blocksize each time the
2921 * CD device is opened. This is because the CD can change,
2922 * and therefore the blocksize might change.
2923 * XXX problems here if some slice or partition is still
2924 * open with the old size?
2926 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2927 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2928 softc->disk->d_devstat->block_size = softc->params.blksize;
2934 cdsize(struct cam_periph *periph, u_int32_t *size)
2936 struct cd_softc *softc;
2938 struct scsi_read_capacity_data *rcap_buf;
2941 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2943 softc = (struct cd_softc *)periph->softc;
2945 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2947 /* XXX Should be M_WAITOK */
2948 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2949 M_SCSICD, M_NOWAIT | M_ZERO);
2950 if (rcap_buf == NULL)
2953 scsi_read_capacity(&ccb->csio,
2954 /*retries*/ cd_retry_count,
2959 /* timeout */20000);
2961 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2962 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2964 xpt_release_ccb(ccb);
2966 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2967 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2968 /* Make sure we got at least some block size. */
2969 if (error == 0 && softc->params.blksize == 0)
2972 * SCSI-3 mandates that the reported blocksize shall be 2048.
2973 * Older drives sometimes report funny values, trim it down to
2974 * 2048, or other parts of the kernel will get confused.
2976 * XXX we leave drives alone that might report 512 bytes, as
2977 * well as drives reporting more weird sizes like perhaps 4K.
2979 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2980 softc->params.blksize = 2048;
2982 free(rcap_buf, M_SCSICD);
2983 *size = softc->params.disksize;
2990 cd6byteworkaround(union ccb *ccb)
2993 struct cam_periph *periph;
2994 struct cd_softc *softc;
2995 struct cd_mode_params *params;
2998 periph = xpt_path_periph(ccb->ccb_h.path);
2999 softc = (struct cd_softc *)periph->softc;
3001 cdb = ccb->csio.cdb_io.cdb_bytes;
3003 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3004 || ((cdb[0] != MODE_SENSE_6)
3005 && (cdb[0] != MODE_SELECT_6)))
3009 * Because there is no convenient place to stash the overall
3010 * cd_mode_params structure pointer, we have to grab it like this.
3011 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3012 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3014 * XXX It would be nice if, at some point, we could increase the
3015 * number of available peripheral private pointers. Both pointers
3016 * are currently used in most every peripheral driver.
3020 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3021 if (params->mode_buf == ccb->csio.data_ptr) {
3028 * This shouldn't happen. All mode sense and mode select
3029 * operations in the cd(4) driver MUST go through cdgetmode() and
3033 xpt_print(periph->path,
3034 "mode buffer not found in mode queue!\n");
3038 params->cdb_size = 10;
3039 softc->minimum_command_size = 10;
3040 xpt_print(ccb->ccb_h.path,
3041 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3042 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3044 if (cdb[0] == MODE_SENSE_6) {
3045 struct scsi_mode_sense_10 ms10;
3046 struct scsi_mode_sense_6 *ms6;
3049 ms6 = (struct scsi_mode_sense_6 *)cdb;
3051 bzero(&ms10, sizeof(ms10));
3052 ms10.opcode = MODE_SENSE_10;
3053 ms10.byte2 = ms6->byte2;
3054 ms10.page = ms6->page;
3057 * 10 byte mode header, block descriptor,
3058 * sizeof(union cd_pages)
3060 len = sizeof(struct cd_mode_data_10);
3061 ccb->csio.dxfer_len = len;
3063 scsi_ulto2b(len, ms10.length);
3064 ms10.control = ms6->control;
3065 bcopy(&ms10, cdb, 10);
3066 ccb->csio.cdb_len = 10;
3068 struct scsi_mode_select_10 ms10;
3069 struct scsi_mode_select_6 *ms6;
3070 struct scsi_mode_header_6 *header6;
3071 struct scsi_mode_header_10 *header10;
3072 struct scsi_mode_page_header *page_header;
3073 int blk_desc_len, page_num, page_size, len;
3075 ms6 = (struct scsi_mode_select_6 *)cdb;
3077 bzero(&ms10, sizeof(ms10));
3078 ms10.opcode = MODE_SELECT_10;
3079 ms10.byte2 = ms6->byte2;
3081 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3082 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3084 page_header = find_mode_page_6(header6);
3085 page_num = page_header->page_code;
3087 blk_desc_len = header6->blk_desc_len;
3089 page_size = cdgetpagesize(page_num);
3091 if (page_size != (page_header->page_length +
3092 sizeof(*page_header)))
3093 page_size = page_header->page_length +
3094 sizeof(*page_header);
3096 len = sizeof(*header10) + blk_desc_len + page_size;
3098 len = min(params->alloc_len, len);
3101 * Since the 6 byte parameter header is shorter than the 10
3102 * byte parameter header, we need to copy the actual mode
3103 * page data, and the block descriptor, if any, so things wind
3104 * up in the right place. The regions will overlap, but
3105 * bcopy() does the right thing.
3107 bcopy(params->mode_buf + sizeof(*header6),
3108 params->mode_buf + sizeof(*header10),
3109 len - sizeof(*header10));
3111 /* Make sure these fields are set correctly. */
3112 scsi_ulto2b(0, header10->data_length);
3113 header10->medium_type = 0;
3114 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3116 ccb->csio.dxfer_len = len;
3118 scsi_ulto2b(len, ms10.length);
3119 ms10.control = ms6->control;
3120 bcopy(&ms10, cdb, 10);
3121 ccb->csio.cdb_len = 10;
3124 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3125 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3128 cam_release_devq(ccb->ccb_h.path,
3132 /*getcount_only*/0);
3139 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3141 struct cd_softc *softc;
3142 struct cam_periph *periph;
3145 periph = xpt_path_periph(ccb->ccb_h.path);
3146 softc = (struct cd_softc *)periph->softc;
3151 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3152 * CDB comes back with this particular error, try transforming it
3153 * into the 10 byte version.
3155 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3156 error = cd6byteworkaround(ccb);
3157 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3158 CAM_SCSI_STATUS_ERROR)
3159 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3160 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3161 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3162 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3163 int sense_key, error_code, asc, ascq;
3165 scsi_extract_sense(&ccb->csio.sense_data,
3166 &error_code, &sense_key, &asc, &ascq);
3167 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3168 error = cd6byteworkaround(ccb);
3171 if (error == ERESTART)
3176 * Until we have a better way of doing pack validation,
3177 * don't treat UAs as errors.
3179 sense_flags |= SF_RETRY_UA;
3180 return (cam_periph_error(ccb, cam_flags, sense_flags,
3181 &softc->saved_ccb));
3185 * Read table of contents
3188 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3189 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3191 struct scsi_read_toc *scsi_cmd;
3193 struct ccb_scsiio *csio;
3200 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3205 /* retries */ cd_retry_count,
3206 /* cbfcnp */ cddone,
3207 /* flags */ CAM_DIR_IN,
3208 /* tag_action */ MSG_SIMPLE_Q_TAG,
3209 /* data_ptr */ data,
3210 /* dxfer_len */ len,
3211 /* sense_len */ SSD_FULL_SIZE,
3212 sizeof(struct scsi_read_toc),
3213 /* timeout */ 50000);
3215 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3216 bzero (scsi_cmd, sizeof(*scsi_cmd));
3218 if (mode == CD_MSF_FORMAT)
3219 scsi_cmd->byte2 |= CD_MSF;
3220 scsi_cmd->from_track = start;
3221 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3222 scsi_cmd->data_len[0] = (ntoc) >> 8;
3223 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3225 scsi_cmd->op_code = READ_TOC;
3227 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3228 /*sense_flags*/SF_RETRY_UA | sense_flags);
3230 xpt_release_ccb(ccb);
3236 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3237 u_int32_t format, int track,
3238 struct cd_sub_channel_info *data, u_int32_t len)
3240 struct scsi_read_subchannel *scsi_cmd;
3241 struct ccb_scsiio *csio;
3247 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3252 /* retries */ cd_retry_count,
3253 /* cbfcnp */ cddone,
3254 /* flags */ CAM_DIR_IN,
3255 /* tag_action */ MSG_SIMPLE_Q_TAG,
3256 /* data_ptr */ (u_int8_t *)data,
3257 /* dxfer_len */ len,
3258 /* sense_len */ SSD_FULL_SIZE,
3259 sizeof(struct scsi_read_subchannel),
3260 /* timeout */ 50000);
3262 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3263 bzero (scsi_cmd, sizeof(*scsi_cmd));
3265 scsi_cmd->op_code = READ_SUBCHANNEL;
3266 if (mode == CD_MSF_FORMAT)
3267 scsi_cmd->byte1 |= CD_MSF;
3268 scsi_cmd->byte2 = SRS_SUBQ;
3269 scsi_cmd->subchan_format = format;
3270 scsi_cmd->track = track;
3271 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3272 scsi_cmd->control = 0;
3274 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3275 /*sense_flags*/SF_RETRY_UA);
3277 xpt_release_ccb(ccb);
3284 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3285 * routine. See comments in cd6byteworkaround() for details.
3288 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3291 struct ccb_scsiio *csio;
3292 struct cd_softc *softc;
3297 softc = (struct cd_softc *)periph->softc;
3299 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3303 data->cdb_size = softc->minimum_command_size;
3304 if (data->cdb_size < 10)
3305 param_len = sizeof(struct cd_mode_data);
3307 param_len = sizeof(struct cd_mode_data_10);
3309 /* Don't say we've got more room than we actually allocated */
3310 param_len = min(param_len, data->alloc_len);
3312 scsi_mode_sense_len(csio,
3313 /* retries */ cd_retry_count,
3314 /* cbfcnp */ cddone,
3315 /* tag_action */ MSG_SIMPLE_Q_TAG,
3317 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3319 /* param_buf */ data->mode_buf,
3320 /* param_len */ param_len,
3321 /* minimum_cmd_size */ softc->minimum_command_size,
3322 /* sense_len */ SSD_FULL_SIZE,
3323 /* timeout */ 50000);
3326 * It would be nice not to have to do this, but there's no
3327 * available pointer in the CCB that would allow us to stuff the
3328 * mode params structure in there and retrieve it in
3329 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3330 * lets the caller know what CDB size we ended up using, so they
3331 * can find the actual mode page offset.
3333 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3335 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3336 /*sense_flags*/SF_RETRY_UA);
3338 xpt_release_ccb(ccb);
3340 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3343 * This is a bit of belt-and-suspenders checking, but if we run
3344 * into a situation where the target sends back multiple block
3345 * descriptors, we might not have enough space in the buffer to
3346 * see the whole mode page. Better to return an error than
3347 * potentially access memory beyond our malloced region.
3352 if (data->cdb_size == 10) {
3353 struct scsi_mode_header_10 *hdr10;
3355 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3356 data_len = scsi_2btoul(hdr10->data_length);
3357 data_len += sizeof(hdr10->data_length);
3359 struct scsi_mode_header_6 *hdr6;
3361 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3362 data_len = hdr6->data_length;
3363 data_len += sizeof(hdr6->data_length);
3367 * Complain if there is more mode data available than we
3368 * allocated space for. This could potentially happen if
3369 * we miscalculated the page length for some reason, if the
3370 * drive returns multiple block descriptors, or if it sets
3371 * the data length incorrectly.
3373 if (data_len > data->alloc_len) {
3374 xpt_print(periph->path, "allocated modepage %d length "
3375 "%d < returned length %d\n", page, data->alloc_len,
3384 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3385 * routine. See comments in cd6byteworkaround() for details.
3388 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3390 struct ccb_scsiio *csio;
3391 struct cd_softc *softc;
3393 int cdb_size, param_len;
3396 softc = (struct cd_softc *)periph->softc;
3398 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3405 * If the data is formatted for the 10 byte version of the mode
3406 * select parameter list, we need to use the 10 byte CDB.
3407 * Otherwise, we use whatever the stored minimum command size.
3409 if (data->cdb_size == 10)
3410 cdb_size = data->cdb_size;
3412 cdb_size = softc->minimum_command_size;
3414 if (cdb_size >= 10) {
3415 struct scsi_mode_header_10 *mode_header;
3418 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3420 data_len = scsi_2btoul(mode_header->data_length);
3422 scsi_ulto2b(0, mode_header->data_length);
3424 * SONY drives do not allow a mode select with a medium_type
3425 * value that has just been returned by a mode sense; use a
3426 * medium_type of 0 (Default) instead.
3428 mode_header->medium_type = 0;
3431 * Pass back whatever the drive passed to us, plus the size
3432 * of the data length field.
3434 param_len = data_len + sizeof(mode_header->data_length);
3437 struct scsi_mode_header_6 *mode_header;
3439 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3441 param_len = mode_header->data_length + 1;
3443 mode_header->data_length = 0;
3445 * SONY drives do not allow a mode select with a medium_type
3446 * value that has just been returned by a mode sense; use a
3447 * medium_type of 0 (Default) instead.
3449 mode_header->medium_type = 0;
3452 /* Don't say we've got more room than we actually allocated */
3453 param_len = min(param_len, data->alloc_len);
3455 scsi_mode_select_len(csio,
3456 /* retries */ cd_retry_count,
3457 /* cbfcnp */ cddone,
3458 /* tag_action */ MSG_SIMPLE_Q_TAG,
3459 /* scsi_page_fmt */ 1,
3461 /* param_buf */ data->mode_buf,
3462 /* param_len */ param_len,
3463 /* minimum_cmd_size */ cdb_size,
3464 /* sense_len */ SSD_FULL_SIZE,
3465 /* timeout */ 50000);
3467 /* See comments in cdgetmode() and cd6byteworkaround(). */
3468 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3470 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3471 /*sense_flags*/SF_RETRY_UA);
3473 xpt_release_ccb(ccb);
3475 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3482 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3484 struct ccb_scsiio *csio;
3490 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3493 * Use the smallest possible command to perform the operation.
3495 if ((len & 0xffff0000) == 0) {
3497 * We can fit in a 10 byte cdb.
3499 struct scsi_play_10 *scsi_cmd;
3501 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3502 bzero (scsi_cmd, sizeof(*scsi_cmd));
3503 scsi_cmd->op_code = PLAY_10;
3504 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3505 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3506 cdb_len = sizeof(*scsi_cmd);
3508 struct scsi_play_12 *scsi_cmd;
3510 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3511 bzero (scsi_cmd, sizeof(*scsi_cmd));
3512 scsi_cmd->op_code = PLAY_12;
3513 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3514 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3515 cdb_len = sizeof(*scsi_cmd);
3518 /*retries*/ cd_retry_count,
3520 /*flags*/CAM_DIR_NONE,
3524 /*sense_len*/SSD_FULL_SIZE,
3526 /*timeout*/50 * 1000);
3528 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3529 /*sense_flags*/SF_RETRY_UA);
3531 xpt_release_ccb(ccb);
3537 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3538 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3540 struct scsi_play_msf *scsi_cmd;
3541 struct ccb_scsiio *csio;
3547 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3552 /* retries */ cd_retry_count,
3553 /* cbfcnp */ cddone,
3554 /* flags */ CAM_DIR_NONE,
3555 /* tag_action */ MSG_SIMPLE_Q_TAG,
3556 /* data_ptr */ NULL,
3558 /* sense_len */ SSD_FULL_SIZE,
3559 sizeof(struct scsi_play_msf),
3560 /* timeout */ 50000);
3562 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3563 bzero (scsi_cmd, sizeof(*scsi_cmd));
3565 scsi_cmd->op_code = PLAY_MSF;
3566 scsi_cmd->start_m = startm;
3567 scsi_cmd->start_s = starts;
3568 scsi_cmd->start_f = startf;
3569 scsi_cmd->end_m = endm;
3570 scsi_cmd->end_s = ends;
3571 scsi_cmd->end_f = endf;
3573 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3574 /*sense_flags*/SF_RETRY_UA);
3576 xpt_release_ccb(ccb);
3583 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3584 u_int32_t etrack, u_int32_t eindex)
3586 struct scsi_play_track *scsi_cmd;
3587 struct ccb_scsiio *csio;
3593 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3598 /* retries */ cd_retry_count,
3599 /* cbfcnp */ cddone,
3600 /* flags */ CAM_DIR_NONE,
3601 /* tag_action */ MSG_SIMPLE_Q_TAG,
3602 /* data_ptr */ NULL,
3604 /* sense_len */ SSD_FULL_SIZE,
3605 sizeof(struct scsi_play_track),
3606 /* timeout */ 50000);
3608 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3609 bzero (scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->op_code = PLAY_TRACK;
3612 scsi_cmd->start_track = strack;
3613 scsi_cmd->start_index = sindex;
3614 scsi_cmd->end_track = etrack;
3615 scsi_cmd->end_index = eindex;
3617 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3618 /*sense_flags*/SF_RETRY_UA);
3620 xpt_release_ccb(ccb);
3626 cdpause(struct cam_periph *periph, u_int32_t go)
3628 struct scsi_pause *scsi_cmd;
3629 struct ccb_scsiio *csio;
3635 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3640 /* retries */ cd_retry_count,
3641 /* cbfcnp */ cddone,
3642 /* flags */ CAM_DIR_NONE,
3643 /* tag_action */ MSG_SIMPLE_Q_TAG,
3644 /* data_ptr */ NULL,
3646 /* sense_len */ SSD_FULL_SIZE,
3647 sizeof(struct scsi_pause),
3648 /* timeout */ 50000);
3650 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3651 bzero (scsi_cmd, sizeof(*scsi_cmd));
3653 scsi_cmd->op_code = PAUSE;
3654 scsi_cmd->resume = go;
3656 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3657 /*sense_flags*/SF_RETRY_UA);
3659 xpt_release_ccb(ccb);
3665 cdstartunit(struct cam_periph *periph, int load)
3672 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3674 scsi_start_stop(&ccb->csio,
3675 /* retries */ cd_retry_count,
3676 /* cbfcnp */ cddone,
3677 /* tag_action */ MSG_SIMPLE_Q_TAG,
3679 /* load_eject */ load,
3680 /* immediate */ FALSE,
3681 /* sense_len */ SSD_FULL_SIZE,
3682 /* timeout */ 50000);
3684 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3685 /*sense_flags*/SF_RETRY_UA);
3687 xpt_release_ccb(ccb);
3693 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3700 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3702 scsi_start_stop(&ccb->csio,
3703 /* retries */ cd_retry_count,
3704 /* cbfcnp */ cddone,
3705 /* tag_action */ MSG_SIMPLE_Q_TAG,
3707 /* load_eject */ eject,
3708 /* immediate */ FALSE,
3709 /* sense_len */ SSD_FULL_SIZE,
3710 /* timeout */ 50000);
3712 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3713 /*sense_flags*/SF_RETRY_UA);
3715 xpt_release_ccb(ccb);
3721 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3723 struct scsi_set_speed *scsi_cmd;
3724 struct ccb_scsiio *csio;
3729 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3732 /* Preserve old behavior: units in multiples of CDROM speed */
3739 /* retries */ cd_retry_count,
3740 /* cbfcnp */ cddone,
3741 /* flags */ CAM_DIR_NONE,
3742 /* tag_action */ MSG_SIMPLE_Q_TAG,
3743 /* data_ptr */ NULL,
3745 /* sense_len */ SSD_FULL_SIZE,
3746 sizeof(struct scsi_set_speed),
3747 /* timeout */ 50000);
3749 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3750 bzero(scsi_cmd, sizeof(*scsi_cmd));
3752 scsi_cmd->opcode = SET_CD_SPEED;
3753 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3754 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3756 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3757 /*sense_flags*/SF_RETRY_UA);
3759 xpt_release_ccb(ccb);
3765 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3777 switch (authinfo->format) {
3778 case DVD_REPORT_AGID:
3779 length = sizeof(struct scsi_report_key_data_agid);
3781 case DVD_REPORT_CHALLENGE:
3782 length = sizeof(struct scsi_report_key_data_challenge);
3784 case DVD_REPORT_KEY1:
3785 length = sizeof(struct scsi_report_key_data_key1_key2);
3787 case DVD_REPORT_TITLE_KEY:
3788 length = sizeof(struct scsi_report_key_data_title);
3789 /* The lba field is only set for the title key */
3790 lba = authinfo->lba;
3792 case DVD_REPORT_ASF:
3793 length = sizeof(struct scsi_report_key_data_asf);
3795 case DVD_REPORT_RPC:
3796 length = sizeof(struct scsi_report_key_data_rpc);
3798 case DVD_INVALIDATE_AGID:
3806 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3810 cam_periph_lock(periph);
3811 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3813 scsi_report_key(&ccb->csio,
3814 /* retries */ cd_retry_count,
3815 /* cbfcnp */ cddone,
3816 /* tag_action */ MSG_SIMPLE_Q_TAG,
3818 /* agid */ authinfo->agid,
3819 /* key_format */ authinfo->format,
3820 /* data_ptr */ databuf,
3821 /* dxfer_len */ length,
3822 /* sense_len */ SSD_FULL_SIZE,
3823 /* timeout */ 50000);
3825 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3826 /*sense_flags*/SF_RETRY_UA);
3831 if (ccb->csio.resid != 0) {
3832 xpt_print(periph->path, "warning, residual for report key "
3833 "command is %d\n", ccb->csio.resid);
3836 switch(authinfo->format) {
3837 case DVD_REPORT_AGID: {
3838 struct scsi_report_key_data_agid *agid_data;
3840 agid_data = (struct scsi_report_key_data_agid *)databuf;
3842 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3846 case DVD_REPORT_CHALLENGE: {
3847 struct scsi_report_key_data_challenge *chal_data;
3849 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3851 bcopy(chal_data->challenge_key, authinfo->keychal,
3852 min(sizeof(chal_data->challenge_key),
3853 sizeof(authinfo->keychal)));
3856 case DVD_REPORT_KEY1: {
3857 struct scsi_report_key_data_key1_key2 *key1_data;
3859 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3861 bcopy(key1_data->key1, authinfo->keychal,
3862 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3865 case DVD_REPORT_TITLE_KEY: {
3866 struct scsi_report_key_data_title *title_data;
3868 title_data = (struct scsi_report_key_data_title *)databuf;
3870 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3871 RKD_TITLE_CPM_SHIFT;
3872 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3873 RKD_TITLE_CP_SEC_SHIFT;
3874 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3875 RKD_TITLE_CMGS_SHIFT;
3876 bcopy(title_data->title_key, authinfo->keychal,
3877 min(sizeof(title_data->title_key),
3878 sizeof(authinfo->keychal)));
3881 case DVD_REPORT_ASF: {
3882 struct scsi_report_key_data_asf *asf_data;
3884 asf_data = (struct scsi_report_key_data_asf *)databuf;
3886 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3889 case DVD_REPORT_RPC: {
3890 struct scsi_report_key_data_rpc *rpc_data;
3892 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3894 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3896 authinfo->vend_rsts =
3897 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3898 RKD_RPC_VENDOR_RESET_SHIFT;
3899 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3900 authinfo->region = rpc_data->region_mask;
3901 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3904 case DVD_INVALIDATE_AGID:
3907 /* This should be impossible, since we checked above */
3910 break; /* NOTREACHED */
3914 xpt_release_ccb(ccb);
3915 cam_periph_unlock(periph);
3917 if (databuf != NULL)
3918 free(databuf, M_DEVBUF);
3924 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3934 switch(authinfo->format) {
3935 case DVD_SEND_CHALLENGE: {
3936 struct scsi_report_key_data_challenge *challenge_data;
3938 length = sizeof(*challenge_data);
3940 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3942 databuf = (u_int8_t *)challenge_data;
3944 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3945 challenge_data->data_len);
3947 bcopy(authinfo->keychal, challenge_data->challenge_key,
3948 min(sizeof(authinfo->keychal),
3949 sizeof(challenge_data->challenge_key)));
3952 case DVD_SEND_KEY2: {
3953 struct scsi_report_key_data_key1_key2 *key2_data;
3955 length = sizeof(*key2_data);
3957 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3959 databuf = (u_int8_t *)key2_data;
3961 scsi_ulto2b(length - sizeof(key2_data->data_len),
3962 key2_data->data_len);
3964 bcopy(authinfo->keychal, key2_data->key1,
3965 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3969 case DVD_SEND_RPC: {
3970 struct scsi_send_key_data_rpc *rpc_data;
3972 length = sizeof(*rpc_data);
3974 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3976 databuf = (u_int8_t *)rpc_data;
3978 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3979 rpc_data->data_len);
3981 rpc_data->region_code = authinfo->region;
3988 cam_periph_lock(periph);
3989 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3991 scsi_send_key(&ccb->csio,
3992 /* retries */ cd_retry_count,
3993 /* cbfcnp */ cddone,
3994 /* tag_action */ MSG_SIMPLE_Q_TAG,
3995 /* agid */ authinfo->agid,
3996 /* key_format */ authinfo->format,
3997 /* data_ptr */ databuf,
3998 /* dxfer_len */ length,
3999 /* sense_len */ SSD_FULL_SIZE,
4000 /* timeout */ 50000);
4002 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4003 /*sense_flags*/SF_RETRY_UA);
4005 xpt_release_ccb(ccb);
4006 cam_periph_unlock(periph);
4008 if (databuf != NULL)
4009 free(databuf, M_DEVBUF);
4015 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4025 /* The address is reserved for many of the formats */
4028 switch(dvdstruct->format) {
4029 case DVD_STRUCT_PHYSICAL:
4030 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4032 case DVD_STRUCT_COPYRIGHT:
4033 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4035 case DVD_STRUCT_DISCKEY:
4036 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4038 case DVD_STRUCT_BCA:
4039 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4041 case DVD_STRUCT_MANUFACT:
4042 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4044 case DVD_STRUCT_CMI:
4046 case DVD_STRUCT_PROTDISCID:
4047 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4049 case DVD_STRUCT_DISCKEYBLOCK:
4050 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4052 case DVD_STRUCT_DDS:
4053 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4055 case DVD_STRUCT_MEDIUM_STAT:
4056 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4058 case DVD_STRUCT_SPARE_AREA:
4059 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4061 case DVD_STRUCT_RMD_LAST:
4063 case DVD_STRUCT_RMD_RMA:
4065 case DVD_STRUCT_PRERECORDED:
4066 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4068 case DVD_STRUCT_UNIQUEID:
4069 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4071 case DVD_STRUCT_DCB:
4073 case DVD_STRUCT_LIST:
4075 * This is the maximum allocation length for the READ DVD
4076 * STRUCTURE command. There's nothing in the MMC3 spec
4077 * that indicates a limit in the amount of data that can
4078 * be returned from this call, other than the limits
4079 * imposed by the 2-byte length variables.
4088 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4092 cam_periph_lock(periph);
4093 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4095 scsi_read_dvd_structure(&ccb->csio,
4096 /* retries */ cd_retry_count,
4097 /* cbfcnp */ cddone,
4098 /* tag_action */ MSG_SIMPLE_Q_TAG,
4100 /* layer_number */ dvdstruct->layer_num,
4101 /* key_format */ dvdstruct->format,
4102 /* agid */ dvdstruct->agid,
4103 /* data_ptr */ databuf,
4104 /* dxfer_len */ length,
4105 /* sense_len */ SSD_FULL_SIZE,
4106 /* timeout */ 50000);
4108 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4109 /*sense_flags*/SF_RETRY_UA);
4114 switch(dvdstruct->format) {
4115 case DVD_STRUCT_PHYSICAL: {
4116 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4117 struct dvd_layer *outlayer;
4118 struct scsi_read_dvd_struct_data_physical *phys_data;
4121 (struct scsi_read_dvd_struct_data_physical *)databuf;
4122 inlayer = &phys_data->layer_desc;
4123 outlayer = (struct dvd_layer *)&dvdstruct->data;
4125 dvdstruct->length = sizeof(*inlayer);
4127 outlayer->book_type = (inlayer->book_type_version &
4128 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4129 outlayer->book_version = (inlayer->book_type_version &
4130 RDSD_BOOK_VERSION_MASK);
4131 outlayer->disc_size = (inlayer->disc_size_max_rate &
4132 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4133 outlayer->max_rate = (inlayer->disc_size_max_rate &
4134 RDSD_MAX_RATE_MASK);
4135 outlayer->nlayers = (inlayer->layer_info &
4136 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4137 outlayer->track_path = (inlayer->layer_info &
4138 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4139 outlayer->layer_type = (inlayer->layer_info &
4140 RDSD_LAYER_TYPE_MASK);
4141 outlayer->linear_density = (inlayer->density &
4142 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4143 outlayer->track_density = (inlayer->density &
4144 RDSD_TRACK_DENSITY_MASK);
4145 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4147 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4148 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4149 outlayer->end_sector_l0 =
4150 scsi_3btoul(inlayer->end_sector_layer0);
4153 case DVD_STRUCT_COPYRIGHT: {
4154 struct scsi_read_dvd_struct_data_copyright *copy_data;
4156 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4159 dvdstruct->cpst = copy_data->cps_type;
4160 dvdstruct->rmi = copy_data->region_info;
4161 dvdstruct->length = 0;
4167 * Tell the user what the overall length is, no matter
4168 * what we can actually fit in the data buffer.
4170 dvdstruct->length = length - ccb->csio.resid -
4171 sizeof(struct scsi_read_dvd_struct_data_header);
4174 * But only actually copy out the smaller of what we read
4175 * in or what the structure can take.
4177 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4179 min(sizeof(dvdstruct->data), dvdstruct->length));
4184 xpt_release_ccb(ccb);
4185 cam_periph_unlock(periph);
4187 if (databuf != NULL)
4188 free(databuf, M_DEVBUF);
4194 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4195 void (*cbfcnp)(struct cam_periph *, union ccb *),
4196 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4197 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4198 u_int8_t sense_len, u_int32_t timeout)
4200 struct scsi_report_key *scsi_cmd;
4202 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4203 bzero(scsi_cmd, sizeof(*scsi_cmd));
4204 scsi_cmd->opcode = REPORT_KEY;
4205 scsi_ulto4b(lba, scsi_cmd->lba);
4206 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4207 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4208 (key_format & RK_KF_KEYFORMAT_MASK);
4213 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4215 /*data_ptr*/ data_ptr,
4216 /*dxfer_len*/ dxfer_len,
4223 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4224 void (*cbfcnp)(struct cam_periph *, union ccb *),
4225 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4226 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4229 struct scsi_send_key *scsi_cmd;
4231 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4232 bzero(scsi_cmd, sizeof(*scsi_cmd));
4233 scsi_cmd->opcode = SEND_KEY;
4235 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4236 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4237 (key_format & RK_KF_KEYFORMAT_MASK);
4242 /*flags*/ CAM_DIR_OUT,
4244 /*data_ptr*/ data_ptr,
4245 /*dxfer_len*/ dxfer_len,
4253 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4254 void (*cbfcnp)(struct cam_periph *, union ccb *),
4255 u_int8_t tag_action, u_int32_t address,
4256 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4257 u_int8_t *data_ptr, u_int32_t dxfer_len,
4258 u_int8_t sense_len, u_int32_t timeout)
4260 struct scsi_read_dvd_structure *scsi_cmd;
4262 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4263 bzero(scsi_cmd, sizeof(*scsi_cmd));
4264 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4266 scsi_ulto4b(address, scsi_cmd->address);
4267 scsi_cmd->layer_number = layer_number;
4268 scsi_cmd->format = format;
4269 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4270 /* The AGID is the top two bits of this byte */
4271 scsi_cmd->agid = agid << 6;
4276 /*flags*/ CAM_DIR_IN,
4278 /*data_ptr*/ data_ptr,
4279 /*dxfer_len*/ dxfer_len,