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
96 CD_FLAG_INVALID = 0x0001,
97 CD_FLAG_NEW_DISC = 0x0002,
98 CD_FLAG_DISC_LOCKED = 0x0004,
99 CD_FLAG_DISC_REMOVABLE = 0x0008,
100 CD_FLAG_TAGGED_QUEUING = 0x0010,
101 CD_FLAG_CHANGER = 0x0040,
102 CD_FLAG_ACTIVE = 0x0080,
103 CD_FLAG_SCHED_ON_COMP = 0x0100,
104 CD_FLAG_RETRY_UA = 0x0200,
105 CD_FLAG_VALID_MEDIA = 0x0400,
106 CD_FLAG_VALID_TOC = 0x0800,
107 CD_FLAG_SCTX_INIT = 0x1000,
108 CD_FLAG_OPEN = 0x2000
113 CD_CCB_BUFFER_IO = 0x02,
114 CD_CCB_WAITING = 0x03,
115 CD_CCB_TYPE_MASK = 0x0F,
116 CD_CCB_RETRY_UA = 0x10
120 CHANGER_TIMEOUT_SCHED = 0x01,
121 CHANGER_SHORT_TMOUT_SCHED = 0x02,
122 CHANGER_MANUAL_CALL = 0x04,
123 CHANGER_NEED_TIMEOUT = 0x08
126 #define ccb_state ppriv_field0
127 #define ccb_bp ppriv_ptr1
130 struct ioc_toc_header header;
131 struct cd_toc_entry entries[100];
134 struct cd_toc_single {
135 struct ioc_toc_header header;
136 struct cd_toc_entry entry;
147 volatile cd_flags flags;
148 struct bio_queue_head bio_queue;
149 LIST_HEAD(, ccb_hdr) pending_ccbs;
150 struct cd_params params;
153 STAILQ_ENTRY(cd_softc) changer_links;
154 struct cdchanger *changer;
156 struct cam_periph *periph;
157 int minimum_command_size;
158 int outstanding_cmds;
159 struct task sysctl_task;
160 struct sysctl_ctx_list sysctl_ctx;
161 struct sysctl_oid *sysctl_tree;
162 STAILQ_HEAD(, cd_mode_params) mode_queue;
163 struct cd_tocdata toc;
167 struct cd_page_sizes {
172 static struct cd_page_sizes cd_page_size_table[] =
174 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
177 struct cd_quirk_entry {
178 struct scsi_inquiry_pattern inq_pat;
183 * The changer quirk entries aren't strictly necessary. Basically, what
184 * they do is tell cdregister() up front that a device is a changer.
185 * Otherwise, it will figure that fact out once it sees a LUN on the device
186 * that is greater than 0. If it is known up front that a device is a changer,
187 * all I/O to the device will go through the changer scheduling routines, as
188 * opposed to the "normal" CD code.
190 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
191 * your device hangs when it gets a 10 byte command. Adding a quirk just
192 * to get rid of the informative diagnostic message is not acceptable. All
193 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
194 * referenced in a comment along with the quirk) , and must be approved by
195 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
196 * be removed until the submitter can explain why they are needed.
197 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
198 * when the CAM_NEW_TRAN_CODE work is done.
200 static struct cd_quirk_entry cd_quirk_table[] =
203 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
204 /*quirks*/ CD_Q_CHANGER
207 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
208 "*"}, /* quirks */ CD_Q_CHANGER
211 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
212 /* quirks */ CD_Q_CHANGER
215 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
216 /* quirks */ CD_Q_BCD_TRACKS
220 static disk_open_t cdopen;
221 static disk_close_t cdclose;
222 static disk_ioctl_t cdioctl;
223 static disk_strategy_t cdstrategy;
225 static periph_init_t cdinit;
226 static periph_ctor_t cdregister;
227 static periph_dtor_t cdcleanup;
228 static periph_start_t cdstart;
229 static periph_oninv_t cdoninvalidate;
230 static void cdasync(void *callback_arg, u_int32_t code,
231 struct cam_path *path, void *arg);
232 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
233 static void cdshorttimeout(void *arg);
234 static void cdschedule(struct cam_periph *periph, int priority);
235 static void cdrunchangerqueue(void *arg);
236 static void cdchangerschedule(struct cd_softc *softc);
237 static int cdrunccb(union ccb *ccb,
238 int (*error_routine)(union ccb *ccb,
240 u_int32_t sense_flags),
241 u_int32_t cam_flags, u_int32_t sense_flags);
242 static union ccb *cdgetccb(struct cam_periph *periph,
244 static void cddone(struct cam_periph *periph,
245 union ccb *start_ccb);
246 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
247 static int cdgetpagesize(int page_num);
248 static void cdprevent(struct cam_periph *periph, int action);
249 static int cdcheckmedia(struct cam_periph *periph);
250 static int cdsize(struct cam_periph *periph, u_int32_t *size);
251 static int cd6byteworkaround(union ccb *ccb);
252 static int cderror(union ccb *ccb, u_int32_t cam_flags,
253 u_int32_t sense_flags);
254 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
255 u_int32_t start, u_int8_t *data,
256 u_int32_t len, u_int32_t sense_flags);
257 static int cdgetmode(struct cam_periph *periph,
258 struct cd_mode_params *data, u_int32_t page);
259 static int cdsetmode(struct cam_periph *periph,
260 struct cd_mode_params *data);
261 static int cdplay(struct cam_periph *periph, u_int32_t blk,
263 static int cdreadsubchannel(struct cam_periph *periph,
264 u_int32_t mode, u_int32_t format,
266 struct cd_sub_channel_info *data,
268 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
269 u_int32_t starts, u_int32_t startf,
270 u_int32_t endm, u_int32_t ends,
272 static int cdplaytracks(struct cam_periph *periph,
273 u_int32_t strack, u_int32_t sindex,
274 u_int32_t etrack, u_int32_t eindex);
275 static int cdpause(struct cam_periph *periph, u_int32_t go);
276 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
277 static int cdstartunit(struct cam_periph *periph, int load);
278 static int cdsetspeed(struct cam_periph *periph,
279 u_int32_t rdspeed, u_int32_t wrspeed);
280 static int cdreportkey(struct cam_periph *periph,
281 struct dvd_authinfo *authinfo);
282 static int cdsendkey(struct cam_periph *periph,
283 struct dvd_authinfo *authinfo);
284 static int cdreaddvdstructure(struct cam_periph *periph,
285 struct dvd_struct *dvdstruct);
287 static struct periph_driver cddriver =
290 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
293 PERIPHDRIVER_DECLARE(cd, cddriver);
295 #ifndef CD_DEFAULT_RETRY
296 #define CD_DEFAULT_RETRY 4
298 #ifndef CHANGER_MIN_BUSY_SECONDS
299 #define CHANGER_MIN_BUSY_SECONDS 5
301 #ifndef CHANGER_MAX_BUSY_SECONDS
302 #define CHANGER_MAX_BUSY_SECONDS 15
305 static int cd_retry_count = CD_DEFAULT_RETRY;
306 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
307 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
309 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
310 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
311 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
312 &cd_retry_count, 0, "Normal I/O retry count");
313 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
314 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
315 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
316 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
317 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
318 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
319 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
323 target_id_t target_id;
326 struct timeval start_time;
327 struct cd_softc *cur_device;
328 struct callout short_handle;
329 struct callout long_handle;
330 volatile cd_changer_flags flags;
331 STAILQ_ENTRY(cdchanger) changer_links;
332 STAILQ_HEAD(chdevlist, cd_softc) chluns;
335 static struct mtx changerq_mtx;
336 static STAILQ_HEAD(changerlist, cdchanger) changerq;
337 static int num_changers;
339 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
346 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
347 STAILQ_INIT(&changerq);
350 * Install a global async callback. This callback will
351 * receive async callbacks like "new device found".
353 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
355 if (status != CAM_REQ_CMP) {
356 printf("cd: Failed to attach master async callback "
357 "due to status 0x%x!\n", status);
362 cdoninvalidate(struct cam_periph *periph)
364 struct cd_softc *softc;
366 softc = (struct cd_softc *)periph->softc;
369 * De-register any async callbacks.
371 xpt_register_async(0, cdasync, periph, periph->path);
373 softc->flags |= CD_FLAG_INVALID;
376 * Return all queued I/O with ENXIO.
377 * XXX Handle any transactions queued to the card
378 * with XPT_ABORT_CCB.
380 bioq_flush(&softc->bio_queue, NULL, ENXIO);
383 * If this device is part of a changer, and it was scheduled
384 * to run, remove it from the run queue since we just nuked
385 * all of its scheduled I/O.
387 if ((softc->flags & CD_FLAG_CHANGER)
388 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
389 camq_remove(&softc->changer->devq, softc->pinfo.index);
391 disk_gone(softc->disk);
392 xpt_print(periph->path, "lost device\n");
396 cdcleanup(struct cam_periph *periph)
398 struct cd_softc *softc;
400 softc = (struct cd_softc *)periph->softc;
402 xpt_print(periph->path, "removing device entry\n");
405 * In the queued, non-active case, the device in question
406 * has already been removed from the changer run queue. Since this
407 * device is active, we need to de-activate it, and schedule
408 * another device to run. (if there is another one to run)
410 if ((softc->flags & CD_FLAG_CHANGER)
411 && (softc->flags & CD_FLAG_ACTIVE)) {
414 * The purpose of the short timeout is soley to determine
415 * whether the current device has finished or not. Well,
416 * since we're removing the active device, we know that it
417 * is finished. So, get rid of the short timeout.
418 * Otherwise, if we're in the time period before the short
419 * timeout fires, and there are no other devices in the
420 * queue to run, there won't be any other device put in the
421 * active slot. i.e., when we call cdrunchangerqueue()
422 * below, it won't do anything. Then, when the short
423 * timeout fires, it'll look at the "current device", which
424 * we are free below, and possibly panic the kernel on a
425 * bogus pointer reference.
427 * The long timeout doesn't really matter, since we
428 * decrement the qfrozen_cnt to indicate that there is
429 * nothing in the active slot now. Therefore, there won't
430 * be any bogus pointer references there.
432 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
433 callout_stop(&softc->changer->short_handle);
434 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
436 softc->changer->devq.qfrozen_cnt[0]--;
437 softc->changer->flags |= CHANGER_MANUAL_CALL;
438 cdrunchangerqueue(softc->changer);
442 * If we're removing the last device on the changer, go ahead and
443 * remove the changer device structure.
445 if ((softc->flags & CD_FLAG_CHANGER)
446 && (--softc->changer->num_devices == 0)) {
449 * Theoretically, there shouldn't be any timeouts left, but
450 * I'm not completely sure that that will be the case. So,
451 * it won't hurt to check and see if there are any left.
453 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
454 callout_stop(&softc->changer->long_handle);
455 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
458 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
459 callout_stop(&softc->changer->short_handle);
460 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
463 mtx_lock(&changerq_mtx);
464 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
467 mtx_unlock(&changerq_mtx);
468 xpt_print(periph->path, "removing changer entry\n");
469 free(softc->changer, M_DEVBUF);
471 cam_periph_unlock(periph);
472 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
473 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
474 xpt_print(periph->path, "can't remove sysctl context\n");
477 disk_destroy(softc->disk);
478 free(softc, M_DEVBUF);
479 cam_periph_lock(periph);
483 cdasync(void *callback_arg, u_int32_t code,
484 struct cam_path *path, void *arg)
486 struct cam_periph *periph;
488 periph = (struct cam_periph *)callback_arg;
490 case AC_FOUND_DEVICE:
492 struct ccb_getdev *cgd;
495 cgd = (struct ccb_getdev *)arg;
499 if (cgd->protocol != PROTO_SCSI)
502 if (SID_TYPE(&cgd->inq_data) != T_CDROM
503 && SID_TYPE(&cgd->inq_data) != T_WORM)
507 * Allocate a peripheral instance for
508 * this device and start the probe
511 status = cam_periph_alloc(cdregister, cdoninvalidate,
513 "cd", CAM_PERIPH_BIO,
514 cgd->ccb_h.path, cdasync,
515 AC_FOUND_DEVICE, cgd);
517 if (status != CAM_REQ_CMP
518 && status != CAM_REQ_INPROG)
519 printf("cdasync: Unable to attach new device "
520 "due to status 0x%x\n", status);
527 struct cd_softc *softc;
528 struct ccb_hdr *ccbh;
530 softc = (struct cd_softc *)periph->softc;
532 * Don't fail on the expected unit attention
535 softc->flags |= CD_FLAG_RETRY_UA;
536 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
537 ccbh->ccb_state |= CD_CCB_RETRY_UA;
541 cam_periph_async(periph, code, path, arg);
547 cdsysctlinit(void *context, int pending)
549 struct cam_periph *periph;
550 struct cd_softc *softc;
551 char tmpstr[80], tmpstr2[80];
553 periph = (struct cam_periph *)context;
554 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
557 softc = (struct cd_softc *)periph->softc;
558 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
559 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
561 sysctl_ctx_init(&softc->sysctl_ctx);
562 softc->flags |= CD_FLAG_SCTX_INIT;
563 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
564 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
565 tmpstr2, CTLFLAG_RD, 0, tmpstr);
567 if (softc->sysctl_tree == NULL) {
568 printf("cdsysctlinit: unable to allocate sysctl tree\n");
569 cam_periph_release(periph);
574 * Now register the sysctl handler, so the user can the value on
577 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
578 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
579 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
582 cam_periph_release(periph);
586 * We have a handler function for this so we can check the values when the
587 * user sets them, instead of every time we look at them.
590 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
594 value = *(int *)arg1;
596 error = sysctl_handle_int(oidp, &value, 0, req);
599 || (req->newptr == NULL))
603 * The only real values we can have here are 6 or 10. I don't
604 * really forsee having 12 be an option at any time in the future.
605 * So if the user sets something less than or equal to 6, we'll set
606 * it to 6. If he sets something greater than 6, we'll set it to 10.
608 * I suppose we could just return an error here for the wrong values,
609 * but I don't think it's necessary to do so, as long as we can
610 * determine the user's intent without too much trouble.
617 *(int *)arg1 = value;
623 cdregister(struct cam_periph *periph, void *arg)
625 struct cd_softc *softc;
626 struct ccb_pathinq cpi;
627 struct ccb_getdev *cgd;
631 cgd = (struct ccb_getdev *)arg;
632 if (periph == NULL) {
633 printf("cdregister: periph was NULL!!\n");
634 return(CAM_REQ_CMP_ERR);
637 printf("cdregister: no getdev CCB, can't register device\n");
638 return(CAM_REQ_CMP_ERR);
641 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
644 printf("cdregister: Unable to probe new device. "
645 "Unable to allocate softc\n");
646 return(CAM_REQ_CMP_ERR);
649 LIST_INIT(&softc->pending_ccbs);
650 STAILQ_INIT(&softc->mode_queue);
651 softc->state = CD_STATE_PROBE;
652 bioq_init(&softc->bio_queue);
653 if (SID_IS_REMOVABLE(&cgd->inq_data))
654 softc->flags |= CD_FLAG_DISC_REMOVABLE;
655 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
656 softc->flags |= CD_FLAG_TAGGED_QUEUING;
658 periph->softc = softc;
659 softc->periph = periph;
662 * See if this device has any quirks.
664 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
665 (caddr_t)cd_quirk_table,
666 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
667 sizeof(*cd_quirk_table), scsi_inquiry_match);
670 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
672 softc->quirks = CD_Q_NONE;
674 /* Check if the SIM does not want 6 byte commands */
675 bzero(&cpi, sizeof(cpi));
676 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
677 cpi.ccb_h.func_code = XPT_PATH_INQ;
678 xpt_action((union ccb *)&cpi);
679 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
680 softc->quirks |= CD_Q_10_BYTE_ONLY;
682 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
684 /* The default is 6 byte commands, unless quirked otherwise */
685 if (softc->quirks & CD_Q_10_BYTE_ONLY)
686 softc->minimum_command_size = 10;
688 softc->minimum_command_size = 6;
691 * Load the user's default, if any.
693 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
694 periph->unit_number);
695 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
697 /* 6 and 10 are the only permissible values here. */
698 if (softc->minimum_command_size < 6)
699 softc->minimum_command_size = 6;
700 else if (softc->minimum_command_size > 6)
701 softc->minimum_command_size = 10;
704 * We need to register the statistics structure for this device,
705 * but we don't have the blocksize yet for it. So, we register
706 * the structure and indicate that we don't have the blocksize
707 * yet. Unlike other SCSI peripheral drivers, we explicitly set
708 * the device type here to be CDROM, rather than just ORing in
709 * the device type. This is because this driver can attach to either
710 * CDROM or WORM devices, and we want this peripheral driver to
711 * show up in the devstat list as a CD peripheral driver, not a
712 * WORM peripheral driver. WORM drives will also have the WORM
713 * driver attached to them.
715 cam_periph_unlock(periph);
716 softc->disk = disk_alloc();
717 softc->disk->d_devstat = devstat_new_entry("cd",
718 periph->unit_number, 0,
719 DEVSTAT_BS_UNAVAILABLE,
720 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
721 DEVSTAT_PRIORITY_CD);
722 softc->disk->d_open = cdopen;
723 softc->disk->d_close = cdclose;
724 softc->disk->d_strategy = cdstrategy;
725 softc->disk->d_ioctl = cdioctl;
726 softc->disk->d_name = "cd";
727 softc->disk->d_unit = periph->unit_number;
728 softc->disk->d_drv1 = periph;
730 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
731 else if (cpi.maxio > MAXPHYS)
732 softc->disk->d_maxsize = MAXPHYS; /* for safety */
734 softc->disk->d_maxsize = cpi.maxio;
735 softc->disk->d_flags = 0;
736 softc->disk->d_hba_vendor = cpi.hba_vendor;
737 softc->disk->d_hba_device = cpi.hba_device;
738 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
739 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
740 disk_create(softc->disk, DISK_VERSION);
741 cam_periph_lock(periph);
744 * Add an async callback so that we get
745 * notified if this device goes away.
747 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
748 cdasync, periph, periph->path);
751 * If the target lun is greater than 0, we most likely have a CD
752 * changer device. Check the quirk entries as well, though, just
753 * in case someone has a CD tower with one lun per drive or
754 * something like that. Also, if we know up front that a
755 * particular device is a changer, we can mark it as such starting
756 * with lun 0, instead of lun 1. It shouldn't be necessary to have
757 * a quirk entry to define something as a changer, however.
759 if (((cgd->ccb_h.target_lun > 0)
760 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
761 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
762 struct cdchanger *nchanger;
763 struct cam_periph *nperiph;
764 struct cam_path *path;
768 /* Set the changer flag in the current device's softc */
769 softc->flags |= CD_FLAG_CHANGER;
772 * Now, look around for an existing changer device with the
773 * same path and target ID as the current device.
775 mtx_lock(&changerq_mtx);
777 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
779 nchanger = STAILQ_NEXT(nchanger, changer_links)){
780 if ((nchanger->path_id == cgd->ccb_h.path_id)
781 && (nchanger->target_id == cgd->ccb_h.target_id)) {
786 mtx_unlock(&changerq_mtx);
789 * If we found a matching entry, just add this device to
790 * the list of devices on this changer.
793 struct chdevlist *chlunhead;
795 chlunhead = &nchanger->chluns;
798 * XXX KDM look at consolidating this code with the
799 * code below in a separate function.
803 * Create a path with lun id 0, and see if we can
804 * find a matching device
806 status = xpt_create_path(&path, /*periph*/ periph,
808 cgd->ccb_h.target_id, 0);
810 if ((status == CAM_REQ_CMP)
811 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
812 struct cd_softc *nsoftc;
814 nsoftc = (struct cd_softc *)nperiph->softc;
816 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
817 nsoftc->flags |= CD_FLAG_CHANGER;
818 nchanger->num_devices++;
819 if (camq_resize(&nchanger->devq,
820 nchanger->num_devices)!=CAM_REQ_CMP){
821 printf("cdregister: "
827 nsoftc->changer = nchanger;
828 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
830 STAILQ_INSERT_TAIL(&nchanger->chluns,
831 nsoftc,changer_links);
834 } else if (status == CAM_REQ_CMP)
837 printf("cdregister: unable to allocate path\n"
838 "cdregister: changer support may be "
842 nchanger->num_devices++;
844 softc->changer = nchanger;
845 softc->pinfo.index = CAM_UNQUEUED_INDEX;
847 if (camq_resize(&nchanger->devq,
848 nchanger->num_devices) != CAM_REQ_CMP) {
849 printf("cdregister: camq_resize "
850 "failed, changer support may "
854 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
857 * In this case, we don't already have an entry for this
858 * particular changer, so we need to create one, add it to
859 * the queue, and queue this device on the list for this
860 * changer. Before we queue this device, however, we need
861 * to search for lun id 0 on this target, and add it to the
862 * queue first, if it exists. (and if it hasn't already
863 * been marked as part of the changer.)
866 nchanger = malloc(sizeof(struct cdchanger),
867 M_DEVBUF, M_NOWAIT | M_ZERO);
868 if (nchanger == NULL) {
869 softc->flags &= ~CD_FLAG_CHANGER;
870 printf("cdregister: unable to malloc "
871 "changer structure\ncdregister: "
872 "changer support disabled\n");
875 * Yes, gotos can be gross but in this case
876 * I think it's justified..
880 if (camq_init(&nchanger->devq, 1) != 0) {
881 softc->flags &= ~CD_FLAG_CHANGER;
882 printf("cdregister: changer support "
887 nchanger->path_id = cgd->ccb_h.path_id;
888 nchanger->target_id = cgd->ccb_h.target_id;
890 /* this is superfluous, but it makes things clearer */
891 nchanger->num_devices = 0;
893 STAILQ_INIT(&nchanger->chluns);
895 callout_init_mtx(&nchanger->long_handle,
896 periph->sim->mtx, 0);
897 callout_init_mtx(&nchanger->short_handle,
898 periph->sim->mtx, 0);
900 mtx_lock(&changerq_mtx);
902 STAILQ_INSERT_TAIL(&changerq, nchanger,
904 mtx_unlock(&changerq_mtx);
907 * Create a path with lun id 0, and see if we can
908 * find a matching device
910 status = xpt_create_path(&path, /*periph*/ periph,
912 cgd->ccb_h.target_id, 0);
915 * If we were able to allocate the path, and if we
916 * find a matching device and it isn't already
917 * marked as part of a changer, then we add it to
918 * the current changer.
920 if ((status == CAM_REQ_CMP)
921 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
922 && ((((struct cd_softc *)periph->softc)->flags &
923 CD_FLAG_CHANGER) == 0)) {
924 struct cd_softc *nsoftc;
926 nsoftc = (struct cd_softc *)nperiph->softc;
928 nsoftc->flags |= CD_FLAG_CHANGER;
929 nchanger->num_devices++;
930 if (camq_resize(&nchanger->devq,
931 nchanger->num_devices) != CAM_REQ_CMP) {
932 printf("cdregister: camq_resize "
933 "failed, changer support may "
936 nsoftc->changer = nchanger;
937 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
939 STAILQ_INSERT_TAIL(&nchanger->chluns,
940 nsoftc, changer_links);
942 } else if (status == CAM_REQ_CMP)
945 printf("cdregister: unable to allocate path\n"
946 "cdregister: changer support may be "
950 softc->changer = nchanger;
951 softc->pinfo.index = CAM_UNQUEUED_INDEX;
952 nchanger->num_devices++;
953 if (camq_resize(&nchanger->devq,
954 nchanger->num_devices) != CAM_REQ_CMP) {
955 printf("cdregister: camq_resize "
956 "failed, changer support may "
959 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
967 * Refcount and block open attempts until we are setup
970 (void)cam_periph_hold(periph, PRIBIO);
972 if ((softc->flags & CD_FLAG_CHANGER) == 0)
973 xpt_schedule(periph, CAM_PRIORITY_DEV);
975 cdschedule(periph, CAM_PRIORITY_DEV);
981 cdopen(struct disk *dp)
983 struct cam_periph *periph;
984 struct cd_softc *softc;
987 periph = (struct cam_periph *)dp->d_drv1;
991 softc = (struct cd_softc *)periph->softc;
993 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
996 cam_periph_lock(periph);
998 if (softc->flags & CD_FLAG_INVALID) {
999 cam_periph_unlock(periph);
1000 cam_periph_release(periph);
1004 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1005 cam_periph_unlock(periph);
1006 cam_periph_release(periph);
1011 * Check for media, and set the appropriate flags. We don't bail
1012 * if we don't have media, but then we don't allow anything but the
1013 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1015 cdcheckmedia(periph);
1017 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1018 cam_periph_unhold(periph);
1020 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1021 if ((softc->flags & CD_FLAG_OPEN) == 0) {
1022 softc->flags |= CD_FLAG_OPEN;
1023 cam_periph_unlock(periph);
1025 cam_periph_unlock(periph);
1026 cam_periph_release(periph);
1033 cdclose(struct disk *dp)
1035 struct cam_periph *periph;
1036 struct cd_softc *softc;
1038 periph = (struct cam_periph *)dp->d_drv1;
1042 softc = (struct cd_softc *)periph->softc;
1044 cam_periph_lock(periph);
1045 cam_periph_hold(periph, PRIBIO);
1047 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1048 cdprevent(periph, PR_ALLOW);
1051 * Since we're closing this CD, mark the blocksize as unavailable.
1052 * It will be marked as available when the CD is opened again.
1054 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1057 * We'll check the media and toc again at the next open().
1059 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1061 cam_periph_unhold(periph);
1062 cam_periph_unlock(periph);
1063 cam_periph_release(periph);
1069 cdshorttimeout(void *arg)
1071 struct cdchanger *changer;
1073 changer = (struct cdchanger *)arg;
1075 /* Always clear the short timeout flag, since that's what we're in */
1076 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1079 * Check to see if there is any more pending or outstanding I/O for
1080 * this device. If not, move it out of the active slot.
1082 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1083 && (changer->cur_device->outstanding_cmds == 0)) {
1084 changer->flags |= CHANGER_MANUAL_CALL;
1085 cdrunchangerqueue(changer);
1090 * This is a wrapper for xpt_schedule. It only applies to changers.
1093 cdschedule(struct cam_periph *periph, int priority)
1095 struct cd_softc *softc;
1097 softc = (struct cd_softc *)periph->softc;
1100 * If this device isn't currently queued, and if it isn't
1101 * the active device, then we queue this device and run the
1102 * changer queue if there is no timeout scheduled to do it.
1103 * If this device is the active device, just schedule it
1104 * to run again. If this device is queued, there should be
1105 * a timeout in place already that will make sure it runs.
1107 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1108 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1110 * We don't do anything with the priority here.
1111 * This is strictly a fifo queue.
1113 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1114 softc->pinfo.generation = ++softc->changer->devq.generation;
1115 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1118 * Since we just put a device in the changer queue,
1119 * check and see if there is a timeout scheduled for
1120 * this changer. If so, let the timeout handle
1121 * switching this device into the active slot. If
1122 * not, manually call the timeout routine to
1125 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1126 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1127 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1128 softc->changer->flags |= CHANGER_MANUAL_CALL;
1129 cdrunchangerqueue(softc->changer);
1131 } else if ((softc->flags & CD_FLAG_ACTIVE)
1132 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1133 xpt_schedule(periph, priority);
1137 cdrunchangerqueue(void *arg)
1139 struct cd_softc *softc;
1140 struct cdchanger *changer;
1141 int called_from_timeout;
1143 changer = (struct cdchanger *)arg;
1146 * If we have NOT been called from cdstrategy() or cddone(), and
1147 * instead from a timeout routine, go ahead and clear the
1150 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1151 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1152 called_from_timeout = 1;
1154 called_from_timeout = 0;
1156 /* Always clear the manual call flag */
1157 changer->flags &= ~CHANGER_MANUAL_CALL;
1159 /* nothing to do if the queue is empty */
1160 if (changer->devq.entries <= 0) {
1165 * If the changer queue is frozen, that means we have an active
1168 if (changer->devq.qfrozen_cnt[0] > 0) {
1171 * We always need to reset the frozen count and clear the
1174 changer->devq.qfrozen_cnt[0]--;
1175 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1176 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1178 if (changer->cur_device->outstanding_cmds > 0) {
1179 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1180 changer->cur_device->bufs_left =
1181 changer->cur_device->outstanding_cmds;
1182 if (called_from_timeout) {
1183 callout_reset(&changer->long_handle,
1184 changer_max_busy_seconds * hz,
1185 cdrunchangerqueue, changer);
1186 changer->flags |= CHANGER_TIMEOUT_SCHED;
1192 * Check to see whether the current device has any I/O left
1193 * to do. If so, requeue it at the end of the queue. If
1194 * not, there is no need to requeue it.
1196 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1198 changer->cur_device->pinfo.generation =
1199 ++changer->devq.generation;
1200 camq_insert(&changer->devq,
1201 (cam_pinfo *)changer->cur_device);
1205 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1207 changer->cur_device = softc;
1209 changer->devq.qfrozen_cnt[0]++;
1210 softc->flags |= CD_FLAG_ACTIVE;
1212 /* Just in case this device is waiting */
1213 wakeup(&softc->changer);
1214 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1217 * Get rid of any pending timeouts, and set a flag to schedule new
1218 * ones so this device gets its full time quantum.
1220 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1221 callout_stop(&changer->long_handle);
1222 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1225 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1226 callout_stop(&changer->short_handle);
1227 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1231 * We need to schedule timeouts, but we only do this after the
1232 * first transaction has completed. This eliminates the changer
1235 changer->flags |= CHANGER_NEED_TIMEOUT;
1239 cdchangerschedule(struct cd_softc *softc)
1241 struct cdchanger *changer;
1243 changer = softc->changer;
1246 * If this is a changer, and this is the current device,
1247 * and this device has at least the minimum time quantum to
1248 * run, see if we can switch it out.
1250 if ((softc->flags & CD_FLAG_ACTIVE)
1251 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1252 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1254 * We try three things here. The first is that we
1255 * check to see whether the schedule on completion
1256 * flag is set. If it is, we decrement the number
1257 * of buffers left, and if it's zero, we reschedule.
1258 * Next, we check to see whether the pending buffer
1259 * queue is empty and whether there are no
1260 * outstanding transactions. If so, we reschedule.
1261 * Next, we see if the pending buffer queue is empty.
1262 * If it is, we set the number of buffers left to
1263 * the current active buffer count and set the
1264 * schedule on complete flag.
1266 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1267 if (--softc->bufs_left == 0) {
1268 softc->changer->flags |=
1269 CHANGER_MANUAL_CALL;
1270 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1271 cdrunchangerqueue(softc->changer);
1273 } else if ((bioq_first(&softc->bio_queue) == NULL)
1274 && (softc->outstanding_cmds == 0)) {
1275 softc->changer->flags |= CHANGER_MANUAL_CALL;
1276 cdrunchangerqueue(softc->changer);
1278 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1279 && (softc->flags & CD_FLAG_ACTIVE)) {
1282 * Now that the first transaction to this
1283 * particular device has completed, we can go ahead
1284 * and schedule our timeouts.
1286 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1287 callout_reset(&changer->long_handle,
1288 changer_max_busy_seconds * hz,
1289 cdrunchangerqueue, changer);
1290 changer->flags |= CHANGER_TIMEOUT_SCHED;
1292 printf("cdchangerschedule: already have a long"
1295 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1296 callout_reset(&changer->short_handle,
1297 changer_min_busy_seconds * hz,
1298 cdshorttimeout, changer);
1299 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1301 printf("cdchangerschedule: already have a short "
1305 * We just scheduled timeouts, no need to schedule
1308 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1314 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1315 u_int32_t cam_flags,
1316 u_int32_t sense_flags),
1317 u_int32_t cam_flags, u_int32_t sense_flags)
1319 struct cd_softc *softc;
1320 struct cam_periph *periph;
1323 periph = xpt_path_periph(ccb->ccb_h.path);
1324 softc = (struct cd_softc *)periph->softc;
1326 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1327 softc->disk->d_devstat);
1329 if (softc->flags & CD_FLAG_CHANGER)
1330 cdchangerschedule(softc);
1336 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1338 struct cd_softc *softc;
1340 softc = (struct cd_softc *)periph->softc;
1342 if (softc->flags & CD_FLAG_CHANGER) {
1344 * This should work the first time this device is woken up,
1345 * but just in case it doesn't, we use a while loop.
1347 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1349 * If this changer isn't already queued, queue it up.
1351 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1352 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1353 softc->pinfo.generation =
1354 ++softc->changer->devq.generation;
1355 camq_insert(&softc->changer->devq,
1356 (cam_pinfo *)softc);
1358 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1359 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1360 && ((softc->changer->flags
1361 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1362 softc->changer->flags |= CHANGER_MANUAL_CALL;
1363 cdrunchangerqueue(softc->changer);
1365 msleep(&softc->changer, periph->sim->mtx,
1366 PRIBIO, "cgticb", 0);
1369 return(cam_periph_getccb(periph, priority));
1374 * Actually translate the requested transfer into one the physical driver
1375 * can understand. The transfer is described by a buf and will include
1376 * only one physical transfer.
1379 cdstrategy(struct bio *bp)
1381 struct cam_periph *periph;
1382 struct cd_softc *softc;
1384 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1385 if (periph == NULL) {
1386 biofinish(bp, NULL, ENXIO);
1390 cam_periph_lock(periph);
1391 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1393 softc = (struct cd_softc *)periph->softc;
1396 * If the device has been made invalid, error out
1398 if ((softc->flags & CD_FLAG_INVALID)) {
1399 cam_periph_unlock(periph);
1400 biofinish(bp, NULL, ENXIO);
1405 * If we don't have valid media, look for it before trying to
1408 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1411 error = cdcheckmedia(periph);
1413 cam_periph_unlock(periph);
1414 biofinish(bp, NULL, error);
1420 * Place it in the queue of disk activities for this disk
1422 bioq_disksort(&softc->bio_queue, bp);
1425 * Schedule ourselves for performing the work. We do things
1426 * differently for changers.
1428 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1429 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1431 cdschedule(periph, CAM_PRIORITY_NORMAL);
1433 cam_periph_unlock(periph);
1438 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1440 struct cd_softc *softc;
1442 struct ccb_scsiio *csio;
1443 struct scsi_read_capacity_data *rcap;
1445 softc = (struct cd_softc *)periph->softc;
1447 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1449 switch (softc->state) {
1450 case CD_STATE_NORMAL:
1452 bp = bioq_first(&softc->bio_queue);
1453 if (periph->immediate_priority <= periph->pinfo.priority) {
1454 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1456 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1458 periph->immediate_priority = CAM_PRIORITY_NONE;
1459 wakeup(&periph->ccb_list);
1460 } else if (bp == NULL) {
1461 xpt_release_ccb(start_ccb);
1463 bioq_remove(&softc->bio_queue, bp);
1465 scsi_read_write(&start_ccb->csio,
1466 /*retries*/ cd_retry_count,
1467 /* cbfcnp */ cddone,
1469 /* read */bp->bio_cmd == BIO_READ,
1471 /* minimum_cmd_size */ 10,
1472 /* lba */ bp->bio_offset /
1473 softc->params.blksize,
1474 bp->bio_bcount / softc->params.blksize,
1475 /* data_ptr */ bp->bio_data,
1476 /* dxfer_len */ bp->bio_bcount,
1477 /* sense_len */ SSD_FULL_SIZE,
1478 /* timeout */ 30000);
1479 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1482 LIST_INSERT_HEAD(&softc->pending_ccbs,
1483 &start_ccb->ccb_h, periph_links.le);
1484 softc->outstanding_cmds++;
1486 /* We expect a unit attention from this device */
1487 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1488 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1489 softc->flags &= ~CD_FLAG_RETRY_UA;
1492 start_ccb->ccb_h.ccb_bp = bp;
1493 bp = bioq_first(&softc->bio_queue);
1495 xpt_action(start_ccb);
1498 /* Have more work to do, so ensure we stay scheduled */
1499 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1503 case CD_STATE_PROBE:
1506 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1507 M_SCSICD, M_NOWAIT | M_ZERO);
1509 xpt_print(periph->path,
1510 "cdstart: Couldn't malloc read_capacity data\n");
1511 /* cd_free_periph??? */
1514 csio = &start_ccb->csio;
1515 scsi_read_capacity(csio,
1516 /*retries*/ cd_retry_count,
1522 start_ccb->ccb_h.ccb_bp = NULL;
1523 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1524 xpt_action(start_ccb);
1531 cddone(struct cam_periph *periph, union ccb *done_ccb)
1533 struct cd_softc *softc;
1534 struct ccb_scsiio *csio;
1536 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1538 softc = (struct cd_softc *)periph->softc;
1539 csio = &done_ccb->csio;
1541 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1542 case CD_CCB_BUFFER_IO:
1547 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1550 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1553 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1558 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1559 if (error == ERESTART) {
1561 * A retry was scheuled, so
1569 xpt_print(periph->path,
1570 "cddone: got error %#x back\n", error);
1571 bioq_flush(&softc->bio_queue, NULL, EIO);
1572 bp->bio_resid = bp->bio_bcount;
1573 bp->bio_error = error;
1574 bp->bio_flags |= BIO_ERROR;
1575 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1576 cam_release_devq(done_ccb->ccb_h.path,
1580 /*getcount_only*/0);
1583 bp->bio_resid = csio->resid;
1585 if (bp->bio_resid != 0) {
1587 * Short transfer ???
1588 * XXX: not sure this is correct for partial
1591 bp->bio_flags |= BIO_ERROR;
1595 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1596 softc->outstanding_cmds--;
1598 if (softc->flags & CD_FLAG_CHANGER)
1599 cdchangerschedule(softc);
1601 biofinish(bp, NULL, 0);
1606 struct scsi_read_capacity_data *rdcap;
1607 char announce_buf[120]; /*
1608 * Currently (9/30/97) the
1609 * longest possible announce
1610 * buffer is 108 bytes, for the
1611 * first error case below.
1612 * That is 39 bytes for the
1613 * basic string, 16 bytes for the
1614 * biggest sense key (hardware
1615 * error), 52 bytes for the
1616 * text of the largest sense
1617 * qualifier valid for a CDROM,
1618 * (0x72, 0x03 or 0x04,
1619 * 0x03), and one byte for the
1620 * null terminating character.
1621 * To allow for longer strings,
1622 * the announce buffer is 120
1625 struct cd_params *cdp;
1627 cdp = &softc->params;
1629 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1631 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1632 cdp->blksize = scsi_4btoul (rdcap->length);
1634 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1636 snprintf(announce_buf, sizeof(announce_buf),
1637 "cd present [%lu x %lu byte records]",
1638 cdp->disksize, (u_long)cdp->blksize);
1643 * Retry any UNIT ATTENTION type errors. They
1644 * are expected at boot.
1646 error = cderror(done_ccb, CAM_RETRY_SELTO,
1647 SF_RETRY_UA | SF_NO_PRINT);
1648 if (error == ERESTART) {
1650 * A retry was scheuled, so
1654 } else if (error != 0) {
1656 struct scsi_sense_data *sense;
1658 int sense_key, error_code;
1661 struct ccb_getdev cgd;
1663 /* Don't wedge this device's queue */
1664 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1665 cam_release_devq(done_ccb->ccb_h.path,
1669 /*getcount_only*/0);
1671 status = done_ccb->ccb_h.status;
1673 xpt_setup_ccb(&cgd.ccb_h,
1674 done_ccb->ccb_h.path,
1675 CAM_PRIORITY_NORMAL);
1676 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1677 xpt_action((union ccb *)&cgd);
1679 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1680 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1681 || ((status & CAM_AUTOSNS_VALID) == 0))
1687 sense = &csio->sense_data;
1688 scsi_extract_sense(sense, &error_code,
1693 * Attach to anything that claims to be a
1694 * CDROM or WORM device, as long as it
1695 * doesn't return a "Logical unit not
1696 * supported" (0x25) error.
1698 if ((have_sense) && (asc != 0x25)
1699 && (error_code == SSD_CURRENT_ERROR)) {
1700 const char *sense_key_desc;
1701 const char *asc_desc;
1703 scsi_sense_desc(sense_key, asc, ascq,
1707 snprintf(announce_buf,
1708 sizeof(announce_buf),
1709 "Attempt to query device "
1710 "size failed: %s, %s",
1713 } else if ((have_sense == 0)
1714 && ((status & CAM_STATUS_MASK) ==
1715 CAM_SCSI_STATUS_ERROR)
1716 && (csio->scsi_status ==
1717 SCSI_STATUS_BUSY)) {
1718 snprintf(announce_buf,
1719 sizeof(announce_buf),
1720 "Attempt to query device "
1721 "size failed: SCSI Status: %s",
1722 scsi_status_string(csio));
1723 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1725 * We only print out an error for
1726 * CDROM type devices. For WORM
1727 * devices, we don't print out an
1728 * error since a few WORM devices
1729 * don't support CDROM commands.
1730 * If we have sense information, go
1731 * ahead and print it out.
1732 * Otherwise, just say that we
1737 * Just print out the error, not
1738 * the full probe message, when we
1745 xpt_print(periph->path,
1746 "got CAM status %#x\n",
1747 done_ccb->ccb_h.status);
1749 xpt_print(periph->path, "fatal error, "
1750 "failed to attach to device\n");
1752 * Invalidate this peripheral.
1754 cam_periph_invalidate(periph);
1756 announce_buf[0] = '\0';
1760 * Invalidate this peripheral.
1762 cam_periph_invalidate(periph);
1763 announce_buf[0] = '\0';
1767 free(rdcap, M_SCSICD);
1768 if (announce_buf[0] != '\0') {
1769 xpt_announce_periph(periph, announce_buf);
1770 if (softc->flags & CD_FLAG_CHANGER)
1771 cdchangerschedule(softc);
1773 * Create our sysctl variables, now that we know
1774 * we have successfully attached.
1776 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1778 softc->state = CD_STATE_NORMAL;
1780 * Since our peripheral may be invalidated by an error
1781 * above or an external event, we must release our CCB
1782 * before releasing the probe lock on the peripheral.
1783 * The peripheral will only go away once the last lock
1784 * is removed, and we need it around for the CCB release
1787 xpt_release_ccb(done_ccb);
1788 cam_periph_unhold(periph);
1791 case CD_CCB_WAITING:
1793 /* Caller will release the CCB */
1794 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1795 ("trying to wakeup ccbwait\n"));
1797 wakeup(&done_ccb->ccb_h.cbfcnp);
1803 xpt_release_ccb(done_ccb);
1806 static union cd_pages *
1807 cdgetpage(struct cd_mode_params *mode_params)
1809 union cd_pages *page;
1811 if (mode_params->cdb_size == 10)
1812 page = (union cd_pages *)find_mode_page_10(
1813 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1815 page = (union cd_pages *)find_mode_page_6(
1816 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1822 cdgetpagesize(int page_num)
1826 for (i = 0; i < (sizeof(cd_page_size_table)/
1827 sizeof(cd_page_size_table[0])); i++) {
1828 if (cd_page_size_table[i].page == page_num)
1829 return (cd_page_size_table[i].page_size);
1836 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1839 struct cam_periph *periph;
1840 struct cd_softc *softc;
1841 int nocopyout, error = 0;
1843 periph = (struct cam_periph *)dp->d_drv1;
1847 cam_periph_lock(periph);
1848 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1850 softc = (struct cd_softc *)periph->softc;
1852 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1853 ("trying to do ioctl %#lx\n", cmd));
1855 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1856 cam_periph_unlock(periph);
1857 cam_periph_release(periph);
1862 * If we don't have media loaded, check for it. If still don't
1863 * have media loaded, we can only do a load or eject.
1865 * We only care whether media is loaded if this is a cd-specific ioctl
1866 * (thus the IOCGROUP check below). Note that this will break if
1867 * anyone adds any ioctls into the switch statement below that don't
1868 * have their ioctl group set to 'c'.
1870 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1871 && ((cmd != CDIOCCLOSE)
1872 && (cmd != CDIOCEJECT))
1873 && (IOCGROUP(cmd) == 'c')) {
1874 error = cdcheckmedia(periph);
1876 cam_periph_unhold(periph);
1877 cam_periph_unlock(periph);
1882 * Drop the lock here so later mallocs can use WAITOK. The periph
1883 * is essentially locked still with the cam_periph_hold call above.
1885 cam_periph_unlock(periph);
1890 case CDIOCPLAYTRACKS:
1892 struct ioc_play_track *args
1893 = (struct ioc_play_track *) addr;
1894 struct cd_mode_params params;
1895 union cd_pages *page;
1897 params.alloc_len = sizeof(union cd_mode_data_6_10);
1898 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1901 cam_periph_lock(periph);
1902 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1903 ("trying to do CDIOCPLAYTRACKS\n"));
1905 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1907 free(params.mode_buf, M_SCSICD);
1908 cam_periph_unlock(periph);
1911 page = cdgetpage(¶ms);
1913 page->audio.flags &= ~CD_PA_SOTC;
1914 page->audio.flags |= CD_PA_IMMED;
1915 error = cdsetmode(periph, ¶ms);
1916 free(params.mode_buf, M_SCSICD);
1918 cam_periph_unlock(periph);
1923 * This was originally implemented with the PLAY
1924 * AUDIO TRACK INDEX command, but that command was
1925 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1926 * drives support it but ATAPI and ATAPI-derivative
1927 * drives don't seem to support it. So we keep a
1928 * cache of the table of contents and translate
1929 * track numbers to MSF format.
1931 if (softc->flags & CD_FLAG_VALID_TOC) {
1932 union msf_lba *sentry, *eentry;
1935 if (args->end_track <
1936 softc->toc.header.ending_track + 1)
1938 if (args->end_track >
1939 softc->toc.header.ending_track + 1)
1941 softc->toc.header.ending_track + 1;
1942 st = args->start_track -
1943 softc->toc.header.starting_track;
1944 et = args->end_track -
1945 softc->toc.header.starting_track;
1948 || (st > (softc->toc.header.ending_track -
1949 softc->toc.header.starting_track))) {
1953 sentry = &softc->toc.entries[st].addr;
1954 eentry = &softc->toc.entries[et].addr;
1955 error = cdplaymsf(periph,
1964 * If we don't have a valid TOC, try the
1965 * play track index command. It is part of
1966 * the SCSI-2 spec, but was removed in the
1967 * MMC specs. ATAPI and ATAPI-derived
1968 * drives don't support it.
1970 if (softc->quirks & CD_Q_BCD_TRACKS) {
1972 bin2bcd(args->start_track);
1974 bin2bcd(args->end_track);
1976 error = cdplaytracks(periph,
1982 cam_periph_unlock(periph);
1987 struct ioc_play_msf *args
1988 = (struct ioc_play_msf *) addr;
1989 struct cd_mode_params params;
1990 union cd_pages *page;
1992 params.alloc_len = sizeof(union cd_mode_data_6_10);
1993 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1996 cam_periph_lock(periph);
1997 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1998 ("trying to do CDIOCPLAYMSF\n"));
2000 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2002 free(params.mode_buf, M_SCSICD);
2003 cam_periph_unlock(periph);
2006 page = cdgetpage(¶ms);
2008 page->audio.flags &= ~CD_PA_SOTC;
2009 page->audio.flags |= CD_PA_IMMED;
2010 error = cdsetmode(periph, ¶ms);
2011 free(params.mode_buf, M_SCSICD);
2013 cam_periph_unlock(periph);
2016 error = cdplaymsf(periph,
2023 cam_periph_unlock(periph);
2026 case CDIOCPLAYBLOCKS:
2028 struct ioc_play_blocks *args
2029 = (struct ioc_play_blocks *) addr;
2030 struct cd_mode_params params;
2031 union cd_pages *page;
2033 params.alloc_len = sizeof(union cd_mode_data_6_10);
2034 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2037 cam_periph_lock(periph);
2038 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2039 ("trying to do CDIOCPLAYBLOCKS\n"));
2042 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2044 free(params.mode_buf, M_SCSICD);
2045 cam_periph_unlock(periph);
2048 page = cdgetpage(¶ms);
2050 page->audio.flags &= ~CD_PA_SOTC;
2051 page->audio.flags |= CD_PA_IMMED;
2052 error = cdsetmode(periph, ¶ms);
2053 free(params.mode_buf, M_SCSICD);
2055 cam_periph_unlock(periph);
2058 error = cdplay(periph, args->blk, args->len);
2059 cam_periph_unlock(periph);
2062 case CDIOCREADSUBCHANNEL_SYSSPACE:
2065 case CDIOCREADSUBCHANNEL:
2067 struct ioc_read_subchannel *args
2068 = (struct ioc_read_subchannel *) addr;
2069 struct cd_sub_channel_info *data;
2070 u_int32_t len = args->data_len;
2072 data = malloc(sizeof(struct cd_sub_channel_info),
2073 M_SCSICD, M_WAITOK | M_ZERO);
2075 cam_periph_lock(periph);
2076 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2077 ("trying to do CDIOCREADSUBCHANNEL\n"));
2079 if ((len > sizeof(struct cd_sub_channel_info)) ||
2080 (len < sizeof(struct cd_sub_channel_header))) {
2082 "scsi_cd: cdioctl: "
2083 "cdioreadsubchannel: error, len=%d\n",
2086 free(data, M_SCSICD);
2087 cam_periph_unlock(periph);
2091 if (softc->quirks & CD_Q_BCD_TRACKS)
2092 args->track = bin2bcd(args->track);
2094 error = cdreadsubchannel(periph, args->address_format,
2095 args->data_format, args->track, data, len);
2098 free(data, M_SCSICD);
2099 cam_periph_unlock(periph);
2102 if (softc->quirks & CD_Q_BCD_TRACKS)
2103 data->what.track_info.track_number =
2104 bcd2bin(data->what.track_info.track_number);
2105 len = min(len, ((data->header.data_len[0] << 8) +
2106 data->header.data_len[1] +
2107 sizeof(struct cd_sub_channel_header)));
2108 cam_periph_unlock(periph);
2109 if (nocopyout == 0) {
2110 if (copyout(data, args->data, len) != 0) {
2114 bcopy(data, args->data, len);
2116 free(data, M_SCSICD);
2120 case CDIOREADTOCHEADER:
2122 struct ioc_toc_header *th;
2124 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2127 cam_periph_lock(periph);
2128 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2129 ("trying to do CDIOREADTOCHEADER\n"));
2131 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2132 sizeof (*th), /*sense_flags*/0);
2135 cam_periph_unlock(periph);
2138 if (softc->quirks & CD_Q_BCD_TRACKS) {
2139 /* we are going to have to convert the BCD
2140 * encoding on the cd to what is expected
2142 th->starting_track =
2143 bcd2bin(th->starting_track);
2144 th->ending_track = bcd2bin(th->ending_track);
2146 th->len = ntohs(th->len);
2147 bcopy(th, addr, sizeof(*th));
2149 cam_periph_unlock(periph);
2152 case CDIOREADTOCENTRYS:
2154 struct cd_tocdata *data;
2155 struct cd_toc_single *lead;
2156 struct ioc_read_toc_entry *te =
2157 (struct ioc_read_toc_entry *) addr;
2158 struct ioc_toc_header *th;
2159 u_int32_t len, readlen, idx, num;
2160 u_int32_t starting_track = te->starting_track;
2162 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2163 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2165 cam_periph_lock(periph);
2166 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2167 ("trying to do CDIOREADTOCENTRYS\n"));
2169 if (te->data_len < sizeof(struct cd_toc_entry)
2170 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2171 || (te->address_format != CD_MSF_FORMAT
2172 && te->address_format != CD_LBA_FORMAT)) {
2174 printf("scsi_cd: error in readtocentries, "
2175 "returning EINVAL\n");
2176 free(data, M_SCSICD);
2177 free(lead, M_SCSICD);
2178 cam_periph_unlock(periph);
2183 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2184 sizeof (*th), /*sense_flags*/0);
2186 free(data, M_SCSICD);
2187 free(lead, M_SCSICD);
2188 cam_periph_unlock(periph);
2192 if (softc->quirks & CD_Q_BCD_TRACKS) {
2193 /* we are going to have to convert the BCD
2194 * encoding on the cd to what is expected
2196 th->starting_track =
2197 bcd2bin(th->starting_track);
2198 th->ending_track = bcd2bin(th->ending_track);
2201 if (starting_track == 0)
2202 starting_track = th->starting_track;
2203 else if (starting_track == LEADOUT)
2204 starting_track = th->ending_track + 1;
2205 else if (starting_track < th->starting_track ||
2206 starting_track > th->ending_track + 1) {
2207 printf("scsi_cd: error in readtocentries, "
2208 "returning EINVAL\n");
2209 free(data, M_SCSICD);
2210 free(lead, M_SCSICD);
2211 cam_periph_unlock(periph);
2216 /* calculate reading length without leadout entry */
2217 readlen = (th->ending_track - starting_track + 1) *
2218 sizeof(struct cd_toc_entry);
2220 /* and with leadout entry */
2221 len = readlen + sizeof(struct cd_toc_entry);
2222 if (te->data_len < len) {
2227 if (len > sizeof(data->entries)) {
2228 printf("scsi_cd: error in readtocentries, "
2229 "returning EINVAL\n");
2231 free(data, M_SCSICD);
2232 free(lead, M_SCSICD);
2233 cam_periph_unlock(periph);
2236 num = len / sizeof(struct cd_toc_entry);
2239 error = cdreadtoc(periph, te->address_format,
2242 readlen + sizeof (*th),
2245 free(data, M_SCSICD);
2246 free(lead, M_SCSICD);
2247 cam_periph_unlock(periph);
2252 /* make leadout entry if needed */
2253 idx = starting_track + num - 1;
2254 if (softc->quirks & CD_Q_BCD_TRACKS)
2255 th->ending_track = bcd2bin(th->ending_track);
2256 if (idx == th->ending_track + 1) {
2257 error = cdreadtoc(periph, te->address_format,
2258 LEADOUT, (u_int8_t *)lead,
2262 free(data, M_SCSICD);
2263 free(lead, M_SCSICD);
2264 cam_periph_unlock(periph);
2267 data->entries[idx - starting_track] =
2270 if (softc->quirks & CD_Q_BCD_TRACKS) {
2271 for (idx = 0; idx < num - 1; idx++) {
2272 data->entries[idx].track =
2273 bcd2bin(data->entries[idx].track);
2277 cam_periph_unlock(periph);
2278 error = copyout(data->entries, te->data, len);
2279 free(data, M_SCSICD);
2280 free(lead, M_SCSICD);
2283 case CDIOREADTOCENTRY:
2285 struct cd_toc_single *data;
2286 struct ioc_read_toc_single_entry *te =
2287 (struct ioc_read_toc_single_entry *) addr;
2288 struct ioc_toc_header *th;
2291 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2293 cam_periph_lock(periph);
2294 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2295 ("trying to do CDIOREADTOCENTRY\n"));
2297 if (te->address_format != CD_MSF_FORMAT
2298 && te->address_format != CD_LBA_FORMAT) {
2299 printf("error in readtocentry, "
2300 " returning EINVAL\n");
2301 free(data, M_SCSICD);
2303 cam_periph_unlock(periph);
2308 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2309 sizeof (*th), /*sense_flags*/0);
2311 free(data, M_SCSICD);
2312 cam_periph_unlock(periph);
2316 if (softc->quirks & CD_Q_BCD_TRACKS) {
2317 /* we are going to have to convert the BCD
2318 * encoding on the cd to what is expected
2320 th->starting_track =
2321 bcd2bin(th->starting_track);
2322 th->ending_track = bcd2bin(th->ending_track);
2326 track = th->starting_track;
2327 else if (track == LEADOUT)
2329 else if (track < th->starting_track ||
2330 track > th->ending_track + 1) {
2331 printf("error in readtocentry, "
2332 " returning EINVAL\n");
2333 free(data, M_SCSICD);
2335 cam_periph_unlock(periph);
2339 error = cdreadtoc(periph, te->address_format, track,
2340 (u_int8_t *)data, sizeof(*data),
2343 free(data, M_SCSICD);
2344 cam_periph_unlock(periph);
2348 if (softc->quirks & CD_Q_BCD_TRACKS)
2349 data->entry.track = bcd2bin(data->entry.track);
2350 bcopy(&data->entry, &te->entry,
2351 sizeof(struct cd_toc_entry));
2352 free(data, M_SCSICD);
2353 cam_periph_unlock(periph);
2358 struct ioc_patch *arg = (struct ioc_patch *)addr;
2359 struct cd_mode_params params;
2360 union cd_pages *page;
2362 params.alloc_len = sizeof(union cd_mode_data_6_10);
2363 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2366 cam_periph_lock(periph);
2367 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2368 ("trying to do CDIOCSETPATCH\n"));
2370 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2372 free(params.mode_buf, M_SCSICD);
2373 cam_periph_unlock(periph);
2376 page = cdgetpage(¶ms);
2378 page->audio.port[LEFT_PORT].channels =
2380 page->audio.port[RIGHT_PORT].channels =
2382 page->audio.port[2].channels = arg->patch[2];
2383 page->audio.port[3].channels = arg->patch[3];
2384 error = cdsetmode(periph, ¶ms);
2385 free(params.mode_buf, M_SCSICD);
2386 cam_periph_unlock(periph);
2391 struct ioc_vol *arg = (struct ioc_vol *) addr;
2392 struct cd_mode_params params;
2393 union cd_pages *page;
2395 params.alloc_len = sizeof(union cd_mode_data_6_10);
2396 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2399 cam_periph_lock(periph);
2400 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2401 ("trying to do CDIOCGETVOL\n"));
2403 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2405 free(params.mode_buf, M_SCSICD);
2406 cam_periph_unlock(periph);
2409 page = cdgetpage(¶ms);
2411 arg->vol[LEFT_PORT] =
2412 page->audio.port[LEFT_PORT].volume;
2413 arg->vol[RIGHT_PORT] =
2414 page->audio.port[RIGHT_PORT].volume;
2415 arg->vol[2] = page->audio.port[2].volume;
2416 arg->vol[3] = page->audio.port[3].volume;
2417 free(params.mode_buf, M_SCSICD);
2418 cam_periph_unlock(periph);
2423 struct ioc_vol *arg = (struct ioc_vol *) addr;
2424 struct cd_mode_params params;
2425 union cd_pages *page;
2427 params.alloc_len = sizeof(union cd_mode_data_6_10);
2428 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2431 cam_periph_lock(periph);
2432 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2433 ("trying to do CDIOCSETVOL\n"));
2435 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2437 free(params.mode_buf, M_SCSICD);
2438 cam_periph_unlock(periph);
2441 page = cdgetpage(¶ms);
2443 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2444 page->audio.port[LEFT_PORT].volume =
2445 arg->vol[LEFT_PORT];
2446 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2447 page->audio.port[RIGHT_PORT].volume =
2448 arg->vol[RIGHT_PORT];
2449 page->audio.port[2].volume = arg->vol[2];
2450 page->audio.port[3].volume = arg->vol[3];
2451 error = cdsetmode(periph, ¶ms);
2452 cam_periph_unlock(periph);
2453 free(params.mode_buf, M_SCSICD);
2458 struct cd_mode_params params;
2459 union cd_pages *page;
2461 params.alloc_len = sizeof(union cd_mode_data_6_10);
2462 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2465 cam_periph_lock(periph);
2466 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2467 ("trying to do CDIOCSETMONO\n"));
2469 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2471 free(params.mode_buf, M_SCSICD);
2472 cam_periph_unlock(periph);
2475 page = cdgetpage(¶ms);
2477 page->audio.port[LEFT_PORT].channels =
2478 LEFT_CHANNEL | RIGHT_CHANNEL;
2479 page->audio.port[RIGHT_PORT].channels =
2480 LEFT_CHANNEL | RIGHT_CHANNEL;
2481 page->audio.port[2].channels = 0;
2482 page->audio.port[3].channels = 0;
2483 error = cdsetmode(periph, ¶ms);
2484 cam_periph_unlock(periph);
2485 free(params.mode_buf, M_SCSICD);
2488 case CDIOCSETSTEREO:
2490 struct cd_mode_params params;
2491 union cd_pages *page;
2493 params.alloc_len = sizeof(union cd_mode_data_6_10);
2494 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2497 cam_periph_lock(periph);
2498 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2499 ("trying to do CDIOCSETSTEREO\n"));
2501 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2503 free(params.mode_buf, M_SCSICD);
2504 cam_periph_unlock(periph);
2507 page = cdgetpage(¶ms);
2509 page->audio.port[LEFT_PORT].channels =
2511 page->audio.port[RIGHT_PORT].channels =
2513 page->audio.port[2].channels = 0;
2514 page->audio.port[3].channels = 0;
2515 error = cdsetmode(periph, ¶ms);
2516 free(params.mode_buf, M_SCSICD);
2517 cam_periph_unlock(periph);
2522 struct cd_mode_params params;
2523 union cd_pages *page;
2525 params.alloc_len = sizeof(union cd_mode_data_6_10);
2526 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2529 cam_periph_lock(periph);
2530 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2531 ("trying to do CDIOCSETMUTE\n"));
2533 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2535 free(params.mode_buf, M_SCSICD);
2536 cam_periph_unlock(periph);
2539 page = cdgetpage(¶ms);
2541 page->audio.port[LEFT_PORT].channels = 0;
2542 page->audio.port[RIGHT_PORT].channels = 0;
2543 page->audio.port[2].channels = 0;
2544 page->audio.port[3].channels = 0;
2545 error = cdsetmode(periph, ¶ms);
2546 free(params.mode_buf, M_SCSICD);
2547 cam_periph_unlock(periph);
2552 struct cd_mode_params params;
2553 union cd_pages *page;
2555 params.alloc_len = sizeof(union cd_mode_data_6_10);
2556 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2559 cam_periph_lock(periph);
2560 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2561 ("trying to do CDIOCSETLEFT\n"));
2563 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2565 free(params.mode_buf, M_SCSICD);
2566 cam_periph_unlock(periph);
2569 page = cdgetpage(¶ms);
2571 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2572 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2573 page->audio.port[2].channels = 0;
2574 page->audio.port[3].channels = 0;
2575 error = cdsetmode(periph, ¶ms);
2576 free(params.mode_buf, M_SCSICD);
2577 cam_periph_unlock(periph);
2582 struct cd_mode_params params;
2583 union cd_pages *page;
2585 params.alloc_len = sizeof(union cd_mode_data_6_10);
2586 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2589 cam_periph_lock(periph);
2590 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2591 ("trying to do CDIOCSETRIGHT\n"));
2593 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2595 free(params.mode_buf, M_SCSICD);
2596 cam_periph_unlock(periph);
2599 page = cdgetpage(¶ms);
2601 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2602 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2603 page->audio.port[2].channels = 0;
2604 page->audio.port[3].channels = 0;
2605 error = cdsetmode(periph, ¶ms);
2606 free(params.mode_buf, M_SCSICD);
2607 cam_periph_unlock(periph);
2611 cam_periph_lock(periph);
2612 error = cdpause(periph, 1);
2613 cam_periph_unlock(periph);
2616 cam_periph_lock(periph);
2617 error = cdpause(periph, 0);
2618 cam_periph_unlock(periph);
2621 cam_periph_lock(periph);
2622 error = cdstartunit(periph, 0);
2623 cam_periph_unlock(periph);
2626 cam_periph_lock(periph);
2627 error = cdstartunit(periph, 1);
2628 cam_periph_unlock(periph);
2631 cam_periph_lock(periph);
2632 error = cdstopunit(periph, 0);
2633 cam_periph_unlock(periph);
2636 cam_periph_lock(periph);
2637 error = cdstopunit(periph, 1);
2638 cam_periph_unlock(periph);
2641 cam_periph_lock(periph);
2642 cdprevent(periph, PR_ALLOW);
2643 cam_periph_unlock(periph);
2646 cam_periph_lock(periph);
2647 cdprevent(periph, PR_PREVENT);
2648 cam_periph_unlock(periph);
2651 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2655 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2659 /* return (cd_reset(periph)); */
2662 case CDRIOCREADSPEED:
2663 cam_periph_lock(periph);
2664 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2665 cam_periph_unlock(periph);
2667 case CDRIOCWRITESPEED:
2668 cam_periph_lock(periph);
2669 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2670 cam_periph_unlock(periph);
2673 case DVDIOCREPORTKEY: {
2674 struct dvd_authinfo *authinfo;
2676 authinfo = (struct dvd_authinfo *)addr;
2678 if (cmd == DVDIOCREPORTKEY)
2679 error = cdreportkey(periph, authinfo);
2681 error = cdsendkey(periph, authinfo);
2684 case DVDIOCREADSTRUCTURE: {
2685 struct dvd_struct *dvdstruct;
2687 dvdstruct = (struct dvd_struct *)addr;
2689 error = cdreaddvdstructure(periph, dvdstruct);
2694 cam_periph_lock(periph);
2695 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2696 cam_periph_unlock(periph);
2700 cam_periph_lock(periph);
2701 cam_periph_unhold(periph);
2703 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2704 if (error && bootverbose) {
2705 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2707 cam_periph_unlock(periph);
2713 cdprevent(struct cam_periph *periph, int action)
2716 struct cd_softc *softc;
2719 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2721 softc = (struct cd_softc *)periph->softc;
2723 if (((action == PR_ALLOW)
2724 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2725 || ((action == PR_PREVENT)
2726 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2730 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2732 scsi_prevent(&ccb->csio,
2733 /*retries*/ cd_retry_count,
2738 /* timeout */60000);
2740 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2741 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2743 xpt_release_ccb(ccb);
2746 if (action == PR_ALLOW)
2747 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2749 softc->flags |= CD_FLAG_DISC_LOCKED;
2754 * XXX: the disk media and sector size is only really able to change
2755 * XXX: while the device is closed.
2758 cdcheckmedia(struct cam_periph *periph)
2760 struct cd_softc *softc;
2761 struct ioc_toc_header *toch;
2762 struct cd_toc_single leadout;
2763 u_int32_t size, toclen;
2764 int error, num_entries, cdindex;
2766 softc = (struct cd_softc *)periph->softc;
2768 cdprevent(periph, PR_PREVENT);
2769 softc->disk->d_sectorsize = 2048;
2770 softc->disk->d_mediasize = 0;
2773 * Get the disc size and block size. If we can't get it, we don't
2774 * have media, most likely.
2776 if ((error = cdsize(periph, &size)) != 0) {
2777 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2778 cdprevent(periph, PR_ALLOW);
2781 softc->flags |= CD_FLAG_VALID_MEDIA;
2782 softc->disk->d_sectorsize = softc->params.blksize;
2783 softc->disk->d_mediasize =
2784 (off_t)softc->params.blksize * softc->params.disksize;
2788 * Now we check the table of contents. This (currently) is only
2789 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2790 * things like present a separate entry in /dev for each track,
2791 * like that acd(4) driver does.
2793 bzero(&softc->toc, sizeof(softc->toc));
2794 toch = &softc->toc.header;
2796 * We will get errors here for media that doesn't have a table of
2797 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2798 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2799 * has not been recorded (no complete session) and the Format codes
2800 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2801 * with an INVALID FIELD IN CDB. Devices that are not capable of
2802 * reading an incomplete session on DDC/CD-R/RW media shall report
2803 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2805 * So this isn't fatal if we can't read the table of contents, it
2806 * just means that the user won't be able to issue the play tracks
2807 * ioctl, and likely lots of other stuff won't work either. They
2808 * need to burn the CD before we can do a whole lot with it. So
2809 * we don't print anything here if we get an error back.
2811 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2814 * Errors in reading the table of contents aren't fatal, we just
2815 * won't have a valid table of contents cached.
2819 bzero(&softc->toc, sizeof(softc->toc));
2823 if (softc->quirks & CD_Q_BCD_TRACKS) {
2824 toch->starting_track = bcd2bin(toch->starting_track);
2825 toch->ending_track = bcd2bin(toch->ending_track);
2828 /* Number of TOC entries, plus leadout */
2829 num_entries = (toch->ending_track - toch->starting_track) + 2;
2831 if (num_entries <= 0)
2834 toclen = num_entries * sizeof(struct cd_toc_entry);
2836 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2837 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2841 bzero(&softc->toc, sizeof(softc->toc));
2845 if (softc->quirks & CD_Q_BCD_TRACKS) {
2846 toch->starting_track = bcd2bin(toch->starting_track);
2847 toch->ending_track = bcd2bin(toch->ending_track);
2850 * XXX KDM is this necessary? Probably only if the drive doesn't
2851 * return leadout information with the table of contents.
2853 cdindex = toch->starting_track + num_entries -1;
2854 if (cdindex == toch->ending_track + 1) {
2856 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2857 (u_int8_t *)&leadout, sizeof(leadout),
2863 softc->toc.entries[cdindex - toch->starting_track] =
2866 if (softc->quirks & CD_Q_BCD_TRACKS) {
2867 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2868 softc->toc.entries[cdindex].track =
2869 bcd2bin(softc->toc.entries[cdindex].track);
2873 softc->flags |= CD_FLAG_VALID_TOC;
2878 * We unconditionally (re)set the blocksize each time the
2879 * CD device is opened. This is because the CD can change,
2880 * and therefore the blocksize might change.
2881 * XXX problems here if some slice or partition is still
2882 * open with the old size?
2884 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2885 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2886 softc->disk->d_devstat->block_size = softc->params.blksize;
2892 cdsize(struct cam_periph *periph, u_int32_t *size)
2894 struct cd_softc *softc;
2896 struct scsi_read_capacity_data *rcap_buf;
2899 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2901 softc = (struct cd_softc *)periph->softc;
2903 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2905 /* XXX Should be M_WAITOK */
2906 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2907 M_SCSICD, M_NOWAIT | M_ZERO);
2908 if (rcap_buf == NULL)
2911 scsi_read_capacity(&ccb->csio,
2912 /*retries*/ cd_retry_count,
2917 /* timeout */20000);
2919 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2920 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2922 xpt_release_ccb(ccb);
2924 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2925 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2926 /* Make sure we got at least some block size. */
2927 if (error == 0 && softc->params.blksize == 0)
2930 * SCSI-3 mandates that the reported blocksize shall be 2048.
2931 * Older drives sometimes report funny values, trim it down to
2932 * 2048, or other parts of the kernel will get confused.
2934 * XXX we leave drives alone that might report 512 bytes, as
2935 * well as drives reporting more weird sizes like perhaps 4K.
2937 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2938 softc->params.blksize = 2048;
2940 free(rcap_buf, M_SCSICD);
2941 *size = softc->params.disksize;
2948 cd6byteworkaround(union ccb *ccb)
2951 struct cam_periph *periph;
2952 struct cd_softc *softc;
2953 struct cd_mode_params *params;
2956 periph = xpt_path_periph(ccb->ccb_h.path);
2957 softc = (struct cd_softc *)periph->softc;
2959 cdb = ccb->csio.cdb_io.cdb_bytes;
2961 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2962 || ((cdb[0] != MODE_SENSE_6)
2963 && (cdb[0] != MODE_SELECT_6)))
2967 * Because there is no convenient place to stash the overall
2968 * cd_mode_params structure pointer, we have to grab it like this.
2969 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2970 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2972 * XXX It would be nice if, at some point, we could increase the
2973 * number of available peripheral private pointers. Both pointers
2974 * are currently used in most every peripheral driver.
2978 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2979 if (params->mode_buf == ccb->csio.data_ptr) {
2986 * This shouldn't happen. All mode sense and mode select
2987 * operations in the cd(4) driver MUST go through cdgetmode() and
2991 xpt_print(periph->path,
2992 "mode buffer not found in mode queue!\n");
2996 params->cdb_size = 10;
2997 softc->minimum_command_size = 10;
2998 xpt_print(ccb->ccb_h.path,
2999 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3000 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3002 if (cdb[0] == MODE_SENSE_6) {
3003 struct scsi_mode_sense_10 ms10;
3004 struct scsi_mode_sense_6 *ms6;
3007 ms6 = (struct scsi_mode_sense_6 *)cdb;
3009 bzero(&ms10, sizeof(ms10));
3010 ms10.opcode = MODE_SENSE_10;
3011 ms10.byte2 = ms6->byte2;
3012 ms10.page = ms6->page;
3015 * 10 byte mode header, block descriptor,
3016 * sizeof(union cd_pages)
3018 len = sizeof(struct cd_mode_data_10);
3019 ccb->csio.dxfer_len = len;
3021 scsi_ulto2b(len, ms10.length);
3022 ms10.control = ms6->control;
3023 bcopy(&ms10, cdb, 10);
3024 ccb->csio.cdb_len = 10;
3026 struct scsi_mode_select_10 ms10;
3027 struct scsi_mode_select_6 *ms6;
3028 struct scsi_mode_header_6 *header6;
3029 struct scsi_mode_header_10 *header10;
3030 struct scsi_mode_page_header *page_header;
3031 int blk_desc_len, page_num, page_size, len;
3033 ms6 = (struct scsi_mode_select_6 *)cdb;
3035 bzero(&ms10, sizeof(ms10));
3036 ms10.opcode = MODE_SELECT_10;
3037 ms10.byte2 = ms6->byte2;
3039 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3040 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3042 page_header = find_mode_page_6(header6);
3043 page_num = page_header->page_code;
3045 blk_desc_len = header6->blk_desc_len;
3047 page_size = cdgetpagesize(page_num);
3049 if (page_size != (page_header->page_length +
3050 sizeof(*page_header)))
3051 page_size = page_header->page_length +
3052 sizeof(*page_header);
3054 len = sizeof(*header10) + blk_desc_len + page_size;
3056 len = min(params->alloc_len, len);
3059 * Since the 6 byte parameter header is shorter than the 10
3060 * byte parameter header, we need to copy the actual mode
3061 * page data, and the block descriptor, if any, so things wind
3062 * up in the right place. The regions will overlap, but
3063 * bcopy() does the right thing.
3065 bcopy(params->mode_buf + sizeof(*header6),
3066 params->mode_buf + sizeof(*header10),
3067 len - sizeof(*header10));
3069 /* Make sure these fields are set correctly. */
3070 scsi_ulto2b(0, header10->data_length);
3071 header10->medium_type = 0;
3072 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3074 ccb->csio.dxfer_len = len;
3076 scsi_ulto2b(len, ms10.length);
3077 ms10.control = ms6->control;
3078 bcopy(&ms10, cdb, 10);
3079 ccb->csio.cdb_len = 10;
3082 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3083 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3086 cam_release_devq(ccb->ccb_h.path,
3090 /*getcount_only*/0);
3097 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3099 struct cd_softc *softc;
3100 struct cam_periph *periph;
3103 periph = xpt_path_periph(ccb->ccb_h.path);
3104 softc = (struct cd_softc *)periph->softc;
3109 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3110 * CDB comes back with this particular error, try transforming it
3111 * into the 10 byte version.
3113 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3114 error = cd6byteworkaround(ccb);
3115 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3116 CAM_SCSI_STATUS_ERROR)
3117 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3118 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3119 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3120 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3121 int sense_key, error_code, asc, ascq;
3123 scsi_extract_sense(&ccb->csio.sense_data,
3124 &error_code, &sense_key, &asc, &ascq);
3125 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3126 error = cd6byteworkaround(ccb);
3129 if (error == ERESTART)
3134 * Until we have a better way of doing pack validation,
3135 * don't treat UAs as errors.
3137 sense_flags |= SF_RETRY_UA;
3138 return (cam_periph_error(ccb, cam_flags, sense_flags,
3139 &softc->saved_ccb));
3143 * Read table of contents
3146 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3147 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3149 struct scsi_read_toc *scsi_cmd;
3151 struct ccb_scsiio *csio;
3158 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3163 /* retries */ cd_retry_count,
3164 /* cbfcnp */ cddone,
3165 /* flags */ CAM_DIR_IN,
3166 /* tag_action */ MSG_SIMPLE_Q_TAG,
3167 /* data_ptr */ data,
3168 /* dxfer_len */ len,
3169 /* sense_len */ SSD_FULL_SIZE,
3170 sizeof(struct scsi_read_toc),
3171 /* timeout */ 50000);
3173 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3174 bzero (scsi_cmd, sizeof(*scsi_cmd));
3176 if (mode == CD_MSF_FORMAT)
3177 scsi_cmd->byte2 |= CD_MSF;
3178 scsi_cmd->from_track = start;
3179 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3180 scsi_cmd->data_len[0] = (ntoc) >> 8;
3181 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3183 scsi_cmd->op_code = READ_TOC;
3185 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3186 /*sense_flags*/SF_RETRY_UA | sense_flags);
3188 xpt_release_ccb(ccb);
3194 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3195 u_int32_t format, int track,
3196 struct cd_sub_channel_info *data, u_int32_t len)
3198 struct scsi_read_subchannel *scsi_cmd;
3199 struct ccb_scsiio *csio;
3205 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3210 /* retries */ cd_retry_count,
3211 /* cbfcnp */ cddone,
3212 /* flags */ CAM_DIR_IN,
3213 /* tag_action */ MSG_SIMPLE_Q_TAG,
3214 /* data_ptr */ (u_int8_t *)data,
3215 /* dxfer_len */ len,
3216 /* sense_len */ SSD_FULL_SIZE,
3217 sizeof(struct scsi_read_subchannel),
3218 /* timeout */ 50000);
3220 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3221 bzero (scsi_cmd, sizeof(*scsi_cmd));
3223 scsi_cmd->op_code = READ_SUBCHANNEL;
3224 if (mode == CD_MSF_FORMAT)
3225 scsi_cmd->byte1 |= CD_MSF;
3226 scsi_cmd->byte2 = SRS_SUBQ;
3227 scsi_cmd->subchan_format = format;
3228 scsi_cmd->track = track;
3229 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3230 scsi_cmd->control = 0;
3232 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3233 /*sense_flags*/SF_RETRY_UA);
3235 xpt_release_ccb(ccb);
3242 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3243 * routine. See comments in cd6byteworkaround() for details.
3246 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3249 struct ccb_scsiio *csio;
3250 struct cd_softc *softc;
3255 softc = (struct cd_softc *)periph->softc;
3257 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3261 data->cdb_size = softc->minimum_command_size;
3262 if (data->cdb_size < 10)
3263 param_len = sizeof(struct cd_mode_data);
3265 param_len = sizeof(struct cd_mode_data_10);
3267 /* Don't say we've got more room than we actually allocated */
3268 param_len = min(param_len, data->alloc_len);
3270 scsi_mode_sense_len(csio,
3271 /* retries */ cd_retry_count,
3272 /* cbfcnp */ cddone,
3273 /* tag_action */ MSG_SIMPLE_Q_TAG,
3275 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3277 /* param_buf */ data->mode_buf,
3278 /* param_len */ param_len,
3279 /* minimum_cmd_size */ softc->minimum_command_size,
3280 /* sense_len */ SSD_FULL_SIZE,
3281 /* timeout */ 50000);
3284 * It would be nice not to have to do this, but there's no
3285 * available pointer in the CCB that would allow us to stuff the
3286 * mode params structure in there and retrieve it in
3287 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3288 * lets the caller know what CDB size we ended up using, so they
3289 * can find the actual mode page offset.
3291 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3293 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3294 /*sense_flags*/SF_RETRY_UA);
3296 xpt_release_ccb(ccb);
3298 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3301 * This is a bit of belt-and-suspenders checking, but if we run
3302 * into a situation where the target sends back multiple block
3303 * descriptors, we might not have enough space in the buffer to
3304 * see the whole mode page. Better to return an error than
3305 * potentially access memory beyond our malloced region.
3310 if (data->cdb_size == 10) {
3311 struct scsi_mode_header_10 *hdr10;
3313 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3314 data_len = scsi_2btoul(hdr10->data_length);
3315 data_len += sizeof(hdr10->data_length);
3317 struct scsi_mode_header_6 *hdr6;
3319 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3320 data_len = hdr6->data_length;
3321 data_len += sizeof(hdr6->data_length);
3325 * Complain if there is more mode data available than we
3326 * allocated space for. This could potentially happen if
3327 * we miscalculated the page length for some reason, if the
3328 * drive returns multiple block descriptors, or if it sets
3329 * the data length incorrectly.
3331 if (data_len > data->alloc_len) {
3332 xpt_print(periph->path, "allocated modepage %d length "
3333 "%d < returned length %d\n", page, data->alloc_len,
3342 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3343 * routine. See comments in cd6byteworkaround() for details.
3346 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3348 struct ccb_scsiio *csio;
3349 struct cd_softc *softc;
3351 int cdb_size, param_len;
3354 softc = (struct cd_softc *)periph->softc;
3356 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3363 * If the data is formatted for the 10 byte version of the mode
3364 * select parameter list, we need to use the 10 byte CDB.
3365 * Otherwise, we use whatever the stored minimum command size.
3367 if (data->cdb_size == 10)
3368 cdb_size = data->cdb_size;
3370 cdb_size = softc->minimum_command_size;
3372 if (cdb_size >= 10) {
3373 struct scsi_mode_header_10 *mode_header;
3376 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3378 data_len = scsi_2btoul(mode_header->data_length);
3380 scsi_ulto2b(0, mode_header->data_length);
3382 * SONY drives do not allow a mode select with a medium_type
3383 * value that has just been returned by a mode sense; use a
3384 * medium_type of 0 (Default) instead.
3386 mode_header->medium_type = 0;
3389 * Pass back whatever the drive passed to us, plus the size
3390 * of the data length field.
3392 param_len = data_len + sizeof(mode_header->data_length);
3395 struct scsi_mode_header_6 *mode_header;
3397 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3399 param_len = mode_header->data_length + 1;
3401 mode_header->data_length = 0;
3403 * SONY drives do not allow a mode select with a medium_type
3404 * value that has just been returned by a mode sense; use a
3405 * medium_type of 0 (Default) instead.
3407 mode_header->medium_type = 0;
3410 /* Don't say we've got more room than we actually allocated */
3411 param_len = min(param_len, data->alloc_len);
3413 scsi_mode_select_len(csio,
3414 /* retries */ cd_retry_count,
3415 /* cbfcnp */ cddone,
3416 /* tag_action */ MSG_SIMPLE_Q_TAG,
3417 /* scsi_page_fmt */ 1,
3419 /* param_buf */ data->mode_buf,
3420 /* param_len */ param_len,
3421 /* minimum_cmd_size */ cdb_size,
3422 /* sense_len */ SSD_FULL_SIZE,
3423 /* timeout */ 50000);
3425 /* See comments in cdgetmode() and cd6byteworkaround(). */
3426 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3428 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3429 /*sense_flags*/SF_RETRY_UA);
3431 xpt_release_ccb(ccb);
3433 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3440 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3442 struct ccb_scsiio *csio;
3448 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3451 * Use the smallest possible command to perform the operation.
3453 if ((len & 0xffff0000) == 0) {
3455 * We can fit in a 10 byte cdb.
3457 struct scsi_play_10 *scsi_cmd;
3459 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3460 bzero (scsi_cmd, sizeof(*scsi_cmd));
3461 scsi_cmd->op_code = PLAY_10;
3462 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3463 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3464 cdb_len = sizeof(*scsi_cmd);
3466 struct scsi_play_12 *scsi_cmd;
3468 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3469 bzero (scsi_cmd, sizeof(*scsi_cmd));
3470 scsi_cmd->op_code = PLAY_12;
3471 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3472 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3473 cdb_len = sizeof(*scsi_cmd);
3476 /*retries*/ cd_retry_count,
3478 /*flags*/CAM_DIR_NONE,
3482 /*sense_len*/SSD_FULL_SIZE,
3484 /*timeout*/50 * 1000);
3486 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3487 /*sense_flags*/SF_RETRY_UA);
3489 xpt_release_ccb(ccb);
3495 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3496 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3498 struct scsi_play_msf *scsi_cmd;
3499 struct ccb_scsiio *csio;
3505 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3510 /* retries */ cd_retry_count,
3511 /* cbfcnp */ cddone,
3512 /* flags */ CAM_DIR_NONE,
3513 /* tag_action */ MSG_SIMPLE_Q_TAG,
3514 /* data_ptr */ NULL,
3516 /* sense_len */ SSD_FULL_SIZE,
3517 sizeof(struct scsi_play_msf),
3518 /* timeout */ 50000);
3520 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3521 bzero (scsi_cmd, sizeof(*scsi_cmd));
3523 scsi_cmd->op_code = PLAY_MSF;
3524 scsi_cmd->start_m = startm;
3525 scsi_cmd->start_s = starts;
3526 scsi_cmd->start_f = startf;
3527 scsi_cmd->end_m = endm;
3528 scsi_cmd->end_s = ends;
3529 scsi_cmd->end_f = endf;
3531 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3532 /*sense_flags*/SF_RETRY_UA);
3534 xpt_release_ccb(ccb);
3541 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3542 u_int32_t etrack, u_int32_t eindex)
3544 struct scsi_play_track *scsi_cmd;
3545 struct ccb_scsiio *csio;
3551 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3556 /* retries */ cd_retry_count,
3557 /* cbfcnp */ cddone,
3558 /* flags */ CAM_DIR_NONE,
3559 /* tag_action */ MSG_SIMPLE_Q_TAG,
3560 /* data_ptr */ NULL,
3562 /* sense_len */ SSD_FULL_SIZE,
3563 sizeof(struct scsi_play_track),
3564 /* timeout */ 50000);
3566 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3567 bzero (scsi_cmd, sizeof(*scsi_cmd));
3569 scsi_cmd->op_code = PLAY_TRACK;
3570 scsi_cmd->start_track = strack;
3571 scsi_cmd->start_index = sindex;
3572 scsi_cmd->end_track = etrack;
3573 scsi_cmd->end_index = eindex;
3575 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3576 /*sense_flags*/SF_RETRY_UA);
3578 xpt_release_ccb(ccb);
3584 cdpause(struct cam_periph *periph, u_int32_t go)
3586 struct scsi_pause *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_pause),
3606 /* timeout */ 50000);
3608 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3609 bzero (scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->op_code = PAUSE;
3612 scsi_cmd->resume = go;
3614 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3615 /*sense_flags*/SF_RETRY_UA);
3617 xpt_release_ccb(ccb);
3623 cdstartunit(struct cam_periph *periph, int load)
3630 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3632 scsi_start_stop(&ccb->csio,
3633 /* retries */ cd_retry_count,
3634 /* cbfcnp */ cddone,
3635 /* tag_action */ MSG_SIMPLE_Q_TAG,
3637 /* load_eject */ load,
3638 /* immediate */ FALSE,
3639 /* sense_len */ SSD_FULL_SIZE,
3640 /* timeout */ 50000);
3642 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3643 /*sense_flags*/SF_RETRY_UA);
3645 xpt_release_ccb(ccb);
3651 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3658 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3660 scsi_start_stop(&ccb->csio,
3661 /* retries */ cd_retry_count,
3662 /* cbfcnp */ cddone,
3663 /* tag_action */ MSG_SIMPLE_Q_TAG,
3665 /* load_eject */ eject,
3666 /* immediate */ FALSE,
3667 /* sense_len */ SSD_FULL_SIZE,
3668 /* timeout */ 50000);
3670 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3671 /*sense_flags*/SF_RETRY_UA);
3673 xpt_release_ccb(ccb);
3679 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3681 struct scsi_set_speed *scsi_cmd;
3682 struct ccb_scsiio *csio;
3687 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3690 /* Preserve old behavior: units in multiples of CDROM speed */
3697 /* retries */ cd_retry_count,
3698 /* cbfcnp */ cddone,
3699 /* flags */ CAM_DIR_NONE,
3700 /* tag_action */ MSG_SIMPLE_Q_TAG,
3701 /* data_ptr */ NULL,
3703 /* sense_len */ SSD_FULL_SIZE,
3704 sizeof(struct scsi_set_speed),
3705 /* timeout */ 50000);
3707 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3708 bzero(scsi_cmd, sizeof(*scsi_cmd));
3710 scsi_cmd->opcode = SET_CD_SPEED;
3711 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3712 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3714 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3715 /*sense_flags*/SF_RETRY_UA);
3717 xpt_release_ccb(ccb);
3723 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3735 switch (authinfo->format) {
3736 case DVD_REPORT_AGID:
3737 length = sizeof(struct scsi_report_key_data_agid);
3739 case DVD_REPORT_CHALLENGE:
3740 length = sizeof(struct scsi_report_key_data_challenge);
3742 case DVD_REPORT_KEY1:
3743 length = sizeof(struct scsi_report_key_data_key1_key2);
3745 case DVD_REPORT_TITLE_KEY:
3746 length = sizeof(struct scsi_report_key_data_title);
3747 /* The lba field is only set for the title key */
3748 lba = authinfo->lba;
3750 case DVD_REPORT_ASF:
3751 length = sizeof(struct scsi_report_key_data_asf);
3753 case DVD_REPORT_RPC:
3754 length = sizeof(struct scsi_report_key_data_rpc);
3756 case DVD_INVALIDATE_AGID:
3764 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3768 cam_periph_lock(periph);
3769 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3771 scsi_report_key(&ccb->csio,
3772 /* retries */ cd_retry_count,
3773 /* cbfcnp */ cddone,
3774 /* tag_action */ MSG_SIMPLE_Q_TAG,
3776 /* agid */ authinfo->agid,
3777 /* key_format */ authinfo->format,
3778 /* data_ptr */ databuf,
3779 /* dxfer_len */ length,
3780 /* sense_len */ SSD_FULL_SIZE,
3781 /* timeout */ 50000);
3783 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3784 /*sense_flags*/SF_RETRY_UA);
3789 if (ccb->csio.resid != 0) {
3790 xpt_print(periph->path, "warning, residual for report key "
3791 "command is %d\n", ccb->csio.resid);
3794 switch(authinfo->format) {
3795 case DVD_REPORT_AGID: {
3796 struct scsi_report_key_data_agid *agid_data;
3798 agid_data = (struct scsi_report_key_data_agid *)databuf;
3800 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3804 case DVD_REPORT_CHALLENGE: {
3805 struct scsi_report_key_data_challenge *chal_data;
3807 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3809 bcopy(chal_data->challenge_key, authinfo->keychal,
3810 min(sizeof(chal_data->challenge_key),
3811 sizeof(authinfo->keychal)));
3814 case DVD_REPORT_KEY1: {
3815 struct scsi_report_key_data_key1_key2 *key1_data;
3817 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3819 bcopy(key1_data->key1, authinfo->keychal,
3820 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3823 case DVD_REPORT_TITLE_KEY: {
3824 struct scsi_report_key_data_title *title_data;
3826 title_data = (struct scsi_report_key_data_title *)databuf;
3828 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3829 RKD_TITLE_CPM_SHIFT;
3830 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3831 RKD_TITLE_CP_SEC_SHIFT;
3832 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3833 RKD_TITLE_CMGS_SHIFT;
3834 bcopy(title_data->title_key, authinfo->keychal,
3835 min(sizeof(title_data->title_key),
3836 sizeof(authinfo->keychal)));
3839 case DVD_REPORT_ASF: {
3840 struct scsi_report_key_data_asf *asf_data;
3842 asf_data = (struct scsi_report_key_data_asf *)databuf;
3844 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3847 case DVD_REPORT_RPC: {
3848 struct scsi_report_key_data_rpc *rpc_data;
3850 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3852 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3854 authinfo->vend_rsts =
3855 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3856 RKD_RPC_VENDOR_RESET_SHIFT;
3857 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3858 authinfo->region = rpc_data->region_mask;
3859 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3862 case DVD_INVALIDATE_AGID:
3865 /* This should be impossible, since we checked above */
3868 break; /* NOTREACHED */
3872 xpt_release_ccb(ccb);
3873 cam_periph_unlock(periph);
3875 if (databuf != NULL)
3876 free(databuf, M_DEVBUF);
3882 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3892 switch(authinfo->format) {
3893 case DVD_SEND_CHALLENGE: {
3894 struct scsi_report_key_data_challenge *challenge_data;
3896 length = sizeof(*challenge_data);
3898 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3900 databuf = (u_int8_t *)challenge_data;
3902 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3903 challenge_data->data_len);
3905 bcopy(authinfo->keychal, challenge_data->challenge_key,
3906 min(sizeof(authinfo->keychal),
3907 sizeof(challenge_data->challenge_key)));
3910 case DVD_SEND_KEY2: {
3911 struct scsi_report_key_data_key1_key2 *key2_data;
3913 length = sizeof(*key2_data);
3915 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3917 databuf = (u_int8_t *)key2_data;
3919 scsi_ulto2b(length - sizeof(key2_data->data_len),
3920 key2_data->data_len);
3922 bcopy(authinfo->keychal, key2_data->key1,
3923 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3927 case DVD_SEND_RPC: {
3928 struct scsi_send_key_data_rpc *rpc_data;
3930 length = sizeof(*rpc_data);
3932 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3934 databuf = (u_int8_t *)rpc_data;
3936 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3937 rpc_data->data_len);
3939 rpc_data->region_code = authinfo->region;
3946 cam_periph_lock(periph);
3947 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3949 scsi_send_key(&ccb->csio,
3950 /* retries */ cd_retry_count,
3951 /* cbfcnp */ cddone,
3952 /* tag_action */ MSG_SIMPLE_Q_TAG,
3953 /* agid */ authinfo->agid,
3954 /* key_format */ authinfo->format,
3955 /* data_ptr */ databuf,
3956 /* dxfer_len */ length,
3957 /* sense_len */ SSD_FULL_SIZE,
3958 /* timeout */ 50000);
3960 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3961 /*sense_flags*/SF_RETRY_UA);
3963 xpt_release_ccb(ccb);
3964 cam_periph_unlock(periph);
3966 if (databuf != NULL)
3967 free(databuf, M_DEVBUF);
3973 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3983 /* The address is reserved for many of the formats */
3986 switch(dvdstruct->format) {
3987 case DVD_STRUCT_PHYSICAL:
3988 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3990 case DVD_STRUCT_COPYRIGHT:
3991 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3993 case DVD_STRUCT_DISCKEY:
3994 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3996 case DVD_STRUCT_BCA:
3997 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3999 case DVD_STRUCT_MANUFACT:
4000 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4002 case DVD_STRUCT_CMI:
4004 case DVD_STRUCT_PROTDISCID:
4005 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4007 case DVD_STRUCT_DISCKEYBLOCK:
4008 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4010 case DVD_STRUCT_DDS:
4011 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4013 case DVD_STRUCT_MEDIUM_STAT:
4014 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4016 case DVD_STRUCT_SPARE_AREA:
4017 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4019 case DVD_STRUCT_RMD_LAST:
4021 case DVD_STRUCT_RMD_RMA:
4023 case DVD_STRUCT_PRERECORDED:
4024 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4026 case DVD_STRUCT_UNIQUEID:
4027 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4029 case DVD_STRUCT_DCB:
4031 case DVD_STRUCT_LIST:
4033 * This is the maximum allocation length for the READ DVD
4034 * STRUCTURE command. There's nothing in the MMC3 spec
4035 * that indicates a limit in the amount of data that can
4036 * be returned from this call, other than the limits
4037 * imposed by the 2-byte length variables.
4046 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4050 cam_periph_lock(periph);
4051 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4053 scsi_read_dvd_structure(&ccb->csio,
4054 /* retries */ cd_retry_count,
4055 /* cbfcnp */ cddone,
4056 /* tag_action */ MSG_SIMPLE_Q_TAG,
4058 /* layer_number */ dvdstruct->layer_num,
4059 /* key_format */ dvdstruct->format,
4060 /* agid */ dvdstruct->agid,
4061 /* data_ptr */ databuf,
4062 /* dxfer_len */ length,
4063 /* sense_len */ SSD_FULL_SIZE,
4064 /* timeout */ 50000);
4066 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4067 /*sense_flags*/SF_RETRY_UA);
4072 switch(dvdstruct->format) {
4073 case DVD_STRUCT_PHYSICAL: {
4074 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4075 struct dvd_layer *outlayer;
4076 struct scsi_read_dvd_struct_data_physical *phys_data;
4079 (struct scsi_read_dvd_struct_data_physical *)databuf;
4080 inlayer = &phys_data->layer_desc;
4081 outlayer = (struct dvd_layer *)&dvdstruct->data;
4083 dvdstruct->length = sizeof(*inlayer);
4085 outlayer->book_type = (inlayer->book_type_version &
4086 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4087 outlayer->book_version = (inlayer->book_type_version &
4088 RDSD_BOOK_VERSION_MASK);
4089 outlayer->disc_size = (inlayer->disc_size_max_rate &
4090 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4091 outlayer->max_rate = (inlayer->disc_size_max_rate &
4092 RDSD_MAX_RATE_MASK);
4093 outlayer->nlayers = (inlayer->layer_info &
4094 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4095 outlayer->track_path = (inlayer->layer_info &
4096 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4097 outlayer->layer_type = (inlayer->layer_info &
4098 RDSD_LAYER_TYPE_MASK);
4099 outlayer->linear_density = (inlayer->density &
4100 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4101 outlayer->track_density = (inlayer->density &
4102 RDSD_TRACK_DENSITY_MASK);
4103 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4105 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4106 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4107 outlayer->end_sector_l0 =
4108 scsi_3btoul(inlayer->end_sector_layer0);
4111 case DVD_STRUCT_COPYRIGHT: {
4112 struct scsi_read_dvd_struct_data_copyright *copy_data;
4114 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4117 dvdstruct->cpst = copy_data->cps_type;
4118 dvdstruct->rmi = copy_data->region_info;
4119 dvdstruct->length = 0;
4125 * Tell the user what the overall length is, no matter
4126 * what we can actually fit in the data buffer.
4128 dvdstruct->length = length - ccb->csio.resid -
4129 sizeof(struct scsi_read_dvd_struct_data_header);
4132 * But only actually copy out the smaller of what we read
4133 * in or what the structure can take.
4135 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4137 min(sizeof(dvdstruct->data), dvdstruct->length));
4142 xpt_release_ccb(ccb);
4143 cam_periph_unlock(periph);
4145 if (databuf != NULL)
4146 free(databuf, M_DEVBUF);
4152 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4153 void (*cbfcnp)(struct cam_periph *, union ccb *),
4154 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4155 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4156 u_int8_t sense_len, u_int32_t timeout)
4158 struct scsi_report_key *scsi_cmd;
4160 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4161 bzero(scsi_cmd, sizeof(*scsi_cmd));
4162 scsi_cmd->opcode = REPORT_KEY;
4163 scsi_ulto4b(lba, scsi_cmd->lba);
4164 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4165 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4166 (key_format & RK_KF_KEYFORMAT_MASK);
4171 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4173 /*data_ptr*/ data_ptr,
4174 /*dxfer_len*/ dxfer_len,
4181 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4182 void (*cbfcnp)(struct cam_periph *, union ccb *),
4183 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4184 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4187 struct scsi_send_key *scsi_cmd;
4189 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4190 bzero(scsi_cmd, sizeof(*scsi_cmd));
4191 scsi_cmd->opcode = SEND_KEY;
4193 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4194 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4195 (key_format & RK_KF_KEYFORMAT_MASK);
4200 /*flags*/ CAM_DIR_OUT,
4202 /*data_ptr*/ data_ptr,
4203 /*dxfer_len*/ dxfer_len,
4211 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4212 void (*cbfcnp)(struct cam_periph *, union ccb *),
4213 u_int8_t tag_action, u_int32_t address,
4214 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4215 u_int8_t *data_ptr, u_int32_t dxfer_len,
4216 u_int8_t sense_len, u_int32_t timeout)
4218 struct scsi_read_dvd_structure *scsi_cmd;
4220 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4221 bzero(scsi_cmd, sizeof(*scsi_cmd));
4222 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4224 scsi_ulto4b(address, scsi_cmd->address);
4225 scsi_cmd->layer_number = layer_number;
4226 scsi_cmd->format = format;
4227 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4228 /* The AGID is the top two bits of this byte */
4229 scsi_cmd->agid = agid << 6;
4234 /*flags*/ CAM_DIR_IN,
4236 /*data_ptr*/ data_ptr,
4237 /*dxfer_len*/ dxfer_len,