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 disk_create(softc->disk, DISK_VERSION);
737 cam_periph_lock(periph);
740 * Add an async callback so that we get
741 * notified if this device goes away.
743 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
744 cdasync, periph, periph->path);
747 * If the target lun is greater than 0, we most likely have a CD
748 * changer device. Check the quirk entries as well, though, just
749 * in case someone has a CD tower with one lun per drive or
750 * something like that. Also, if we know up front that a
751 * particular device is a changer, we can mark it as such starting
752 * with lun 0, instead of lun 1. It shouldn't be necessary to have
753 * a quirk entry to define something as a changer, however.
755 if (((cgd->ccb_h.target_lun > 0)
756 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
757 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
758 struct cdchanger *nchanger;
759 struct cam_periph *nperiph;
760 struct cam_path *path;
764 /* Set the changer flag in the current device's softc */
765 softc->flags |= CD_FLAG_CHANGER;
768 * Now, look around for an existing changer device with the
769 * same path and target ID as the current device.
771 mtx_lock(&changerq_mtx);
773 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
775 nchanger = STAILQ_NEXT(nchanger, changer_links)){
776 if ((nchanger->path_id == cgd->ccb_h.path_id)
777 && (nchanger->target_id == cgd->ccb_h.target_id)) {
782 mtx_unlock(&changerq_mtx);
785 * If we found a matching entry, just add this device to
786 * the list of devices on this changer.
789 struct chdevlist *chlunhead;
791 chlunhead = &nchanger->chluns;
794 * XXX KDM look at consolidating this code with the
795 * code below in a separate function.
799 * Create a path with lun id 0, and see if we can
800 * find a matching device
802 status = xpt_create_path(&path, /*periph*/ periph,
804 cgd->ccb_h.target_id, 0);
806 if ((status == CAM_REQ_CMP)
807 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
808 struct cd_softc *nsoftc;
810 nsoftc = (struct cd_softc *)nperiph->softc;
812 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
813 nsoftc->flags |= CD_FLAG_CHANGER;
814 nchanger->num_devices++;
815 if (camq_resize(&nchanger->devq,
816 nchanger->num_devices)!=CAM_REQ_CMP){
817 printf("cdregister: "
823 nsoftc->changer = nchanger;
824 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
826 STAILQ_INSERT_TAIL(&nchanger->chluns,
827 nsoftc,changer_links);
830 } else if (status == CAM_REQ_CMP)
833 printf("cdregister: unable to allocate path\n"
834 "cdregister: changer support may be "
838 nchanger->num_devices++;
840 softc->changer = nchanger;
841 softc->pinfo.index = CAM_UNQUEUED_INDEX;
843 if (camq_resize(&nchanger->devq,
844 nchanger->num_devices) != CAM_REQ_CMP) {
845 printf("cdregister: camq_resize "
846 "failed, changer support may "
850 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
853 * In this case, we don't already have an entry for this
854 * particular changer, so we need to create one, add it to
855 * the queue, and queue this device on the list for this
856 * changer. Before we queue this device, however, we need
857 * to search for lun id 0 on this target, and add it to the
858 * queue first, if it exists. (and if it hasn't already
859 * been marked as part of the changer.)
862 nchanger = malloc(sizeof(struct cdchanger),
863 M_DEVBUF, M_NOWAIT | M_ZERO);
864 if (nchanger == NULL) {
865 softc->flags &= ~CD_FLAG_CHANGER;
866 printf("cdregister: unable to malloc "
867 "changer structure\ncdregister: "
868 "changer support disabled\n");
871 * Yes, gotos can be gross but in this case
872 * I think it's justified..
876 if (camq_init(&nchanger->devq, 1) != 0) {
877 softc->flags &= ~CD_FLAG_CHANGER;
878 printf("cdregister: changer support "
883 nchanger->path_id = cgd->ccb_h.path_id;
884 nchanger->target_id = cgd->ccb_h.target_id;
886 /* this is superfluous, but it makes things clearer */
887 nchanger->num_devices = 0;
889 STAILQ_INIT(&nchanger->chluns);
891 callout_init_mtx(&nchanger->long_handle,
892 periph->sim->mtx, 0);
893 callout_init_mtx(&nchanger->short_handle,
894 periph->sim->mtx, 0);
896 mtx_lock(&changerq_mtx);
898 STAILQ_INSERT_TAIL(&changerq, nchanger,
900 mtx_unlock(&changerq_mtx);
903 * Create a path with lun id 0, and see if we can
904 * find a matching device
906 status = xpt_create_path(&path, /*periph*/ periph,
908 cgd->ccb_h.target_id, 0);
911 * If we were able to allocate the path, and if we
912 * find a matching device and it isn't already
913 * marked as part of a changer, then we add it to
914 * the current changer.
916 if ((status == CAM_REQ_CMP)
917 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
918 && ((((struct cd_softc *)periph->softc)->flags &
919 CD_FLAG_CHANGER) == 0)) {
920 struct cd_softc *nsoftc;
922 nsoftc = (struct cd_softc *)nperiph->softc;
924 nsoftc->flags |= CD_FLAG_CHANGER;
925 nchanger->num_devices++;
926 if (camq_resize(&nchanger->devq,
927 nchanger->num_devices) != CAM_REQ_CMP) {
928 printf("cdregister: camq_resize "
929 "failed, changer support may "
932 nsoftc->changer = nchanger;
933 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
935 STAILQ_INSERT_TAIL(&nchanger->chluns,
936 nsoftc, changer_links);
938 } else if (status == CAM_REQ_CMP)
941 printf("cdregister: unable to allocate path\n"
942 "cdregister: changer support may be "
946 softc->changer = nchanger;
947 softc->pinfo.index = CAM_UNQUEUED_INDEX;
948 nchanger->num_devices++;
949 if (camq_resize(&nchanger->devq,
950 nchanger->num_devices) != CAM_REQ_CMP) {
951 printf("cdregister: camq_resize "
952 "failed, changer support may "
955 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
963 * Refcount and block open attempts until we are setup
966 (void)cam_periph_hold(periph, PRIBIO);
968 if ((softc->flags & CD_FLAG_CHANGER) == 0)
969 xpt_schedule(periph, CAM_PRIORITY_DEV);
971 cdschedule(periph, CAM_PRIORITY_DEV);
977 cdopen(struct disk *dp)
979 struct cam_periph *periph;
980 struct cd_softc *softc;
983 periph = (struct cam_periph *)dp->d_drv1;
987 softc = (struct cd_softc *)periph->softc;
989 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
992 cam_periph_lock(periph);
994 if (softc->flags & CD_FLAG_INVALID) {
995 cam_periph_unlock(periph);
996 cam_periph_release(periph);
1000 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1001 cam_periph_unlock(periph);
1002 cam_periph_release(periph);
1007 * Check for media, and set the appropriate flags. We don't bail
1008 * if we don't have media, but then we don't allow anything but the
1009 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1011 cdcheckmedia(periph);
1013 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1014 cam_periph_unhold(periph);
1016 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1017 if ((softc->flags & CD_FLAG_OPEN) == 0) {
1018 softc->flags |= CD_FLAG_OPEN;
1019 cam_periph_unlock(periph);
1021 cam_periph_unlock(periph);
1022 cam_periph_release(periph);
1029 cdclose(struct disk *dp)
1031 struct cam_periph *periph;
1032 struct cd_softc *softc;
1034 periph = (struct cam_periph *)dp->d_drv1;
1038 softc = (struct cd_softc *)periph->softc;
1040 cam_periph_lock(periph);
1041 cam_periph_hold(periph, PRIBIO);
1043 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1044 cdprevent(periph, PR_ALLOW);
1047 * Since we're closing this CD, mark the blocksize as unavailable.
1048 * It will be marked as available when the CD is opened again.
1050 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1053 * We'll check the media and toc again at the next open().
1055 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1057 cam_periph_unhold(periph);
1058 cam_periph_unlock(periph);
1059 cam_periph_release(periph);
1065 cdshorttimeout(void *arg)
1067 struct cdchanger *changer;
1069 changer = (struct cdchanger *)arg;
1071 /* Always clear the short timeout flag, since that's what we're in */
1072 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1075 * Check to see if there is any more pending or outstanding I/O for
1076 * this device. If not, move it out of the active slot.
1078 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1079 && (changer->cur_device->outstanding_cmds == 0)) {
1080 changer->flags |= CHANGER_MANUAL_CALL;
1081 cdrunchangerqueue(changer);
1086 * This is a wrapper for xpt_schedule. It only applies to changers.
1089 cdschedule(struct cam_periph *periph, int priority)
1091 struct cd_softc *softc;
1093 softc = (struct cd_softc *)periph->softc;
1096 * If this device isn't currently queued, and if it isn't
1097 * the active device, then we queue this device and run the
1098 * changer queue if there is no timeout scheduled to do it.
1099 * If this device is the active device, just schedule it
1100 * to run again. If this device is queued, there should be
1101 * a timeout in place already that will make sure it runs.
1103 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1104 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1106 * We don't do anything with the priority here.
1107 * This is strictly a fifo queue.
1109 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1110 softc->pinfo.generation = ++softc->changer->devq.generation;
1111 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1114 * Since we just put a device in the changer queue,
1115 * check and see if there is a timeout scheduled for
1116 * this changer. If so, let the timeout handle
1117 * switching this device into the active slot. If
1118 * not, manually call the timeout routine to
1121 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1122 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1123 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1124 softc->changer->flags |= CHANGER_MANUAL_CALL;
1125 cdrunchangerqueue(softc->changer);
1127 } else if ((softc->flags & CD_FLAG_ACTIVE)
1128 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1129 xpt_schedule(periph, priority);
1133 cdrunchangerqueue(void *arg)
1135 struct cd_softc *softc;
1136 struct cdchanger *changer;
1137 int called_from_timeout;
1139 changer = (struct cdchanger *)arg;
1142 * If we have NOT been called from cdstrategy() or cddone(), and
1143 * instead from a timeout routine, go ahead and clear the
1146 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1147 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1148 called_from_timeout = 1;
1150 called_from_timeout = 0;
1152 /* Always clear the manual call flag */
1153 changer->flags &= ~CHANGER_MANUAL_CALL;
1155 /* nothing to do if the queue is empty */
1156 if (changer->devq.entries <= 0) {
1161 * If the changer queue is frozen, that means we have an active
1164 if (changer->devq.qfrozen_cnt[0] > 0) {
1167 * We always need to reset the frozen count and clear the
1170 changer->devq.qfrozen_cnt[0]--;
1171 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1172 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1174 if (changer->cur_device->outstanding_cmds > 0) {
1175 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1176 changer->cur_device->bufs_left =
1177 changer->cur_device->outstanding_cmds;
1178 if (called_from_timeout) {
1179 callout_reset(&changer->long_handle,
1180 changer_max_busy_seconds * hz,
1181 cdrunchangerqueue, changer);
1182 changer->flags |= CHANGER_TIMEOUT_SCHED;
1188 * Check to see whether the current device has any I/O left
1189 * to do. If so, requeue it at the end of the queue. If
1190 * not, there is no need to requeue it.
1192 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1194 changer->cur_device->pinfo.generation =
1195 ++changer->devq.generation;
1196 camq_insert(&changer->devq,
1197 (cam_pinfo *)changer->cur_device);
1201 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1203 changer->cur_device = softc;
1205 changer->devq.qfrozen_cnt[0]++;
1206 softc->flags |= CD_FLAG_ACTIVE;
1208 /* Just in case this device is waiting */
1209 wakeup(&softc->changer);
1210 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1213 * Get rid of any pending timeouts, and set a flag to schedule new
1214 * ones so this device gets its full time quantum.
1216 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1217 callout_stop(&changer->long_handle);
1218 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1221 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1222 callout_stop(&changer->short_handle);
1223 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1227 * We need to schedule timeouts, but we only do this after the
1228 * first transaction has completed. This eliminates the changer
1231 changer->flags |= CHANGER_NEED_TIMEOUT;
1235 cdchangerschedule(struct cd_softc *softc)
1237 struct cdchanger *changer;
1239 changer = softc->changer;
1242 * If this is a changer, and this is the current device,
1243 * and this device has at least the minimum time quantum to
1244 * run, see if we can switch it out.
1246 if ((softc->flags & CD_FLAG_ACTIVE)
1247 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1248 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1250 * We try three things here. The first is that we
1251 * check to see whether the schedule on completion
1252 * flag is set. If it is, we decrement the number
1253 * of buffers left, and if it's zero, we reschedule.
1254 * Next, we check to see whether the pending buffer
1255 * queue is empty and whether there are no
1256 * outstanding transactions. If so, we reschedule.
1257 * Next, we see if the pending buffer queue is empty.
1258 * If it is, we set the number of buffers left to
1259 * the current active buffer count and set the
1260 * schedule on complete flag.
1262 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1263 if (--softc->bufs_left == 0) {
1264 softc->changer->flags |=
1265 CHANGER_MANUAL_CALL;
1266 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1267 cdrunchangerqueue(softc->changer);
1269 } else if ((bioq_first(&softc->bio_queue) == NULL)
1270 && (softc->outstanding_cmds == 0)) {
1271 softc->changer->flags |= CHANGER_MANUAL_CALL;
1272 cdrunchangerqueue(softc->changer);
1274 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1275 && (softc->flags & CD_FLAG_ACTIVE)) {
1278 * Now that the first transaction to this
1279 * particular device has completed, we can go ahead
1280 * and schedule our timeouts.
1282 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1283 callout_reset(&changer->long_handle,
1284 changer_max_busy_seconds * hz,
1285 cdrunchangerqueue, changer);
1286 changer->flags |= CHANGER_TIMEOUT_SCHED;
1288 printf("cdchangerschedule: already have a long"
1291 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1292 callout_reset(&changer->short_handle,
1293 changer_min_busy_seconds * hz,
1294 cdshorttimeout, changer);
1295 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1297 printf("cdchangerschedule: already have a short "
1301 * We just scheduled timeouts, no need to schedule
1304 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1310 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1311 u_int32_t cam_flags,
1312 u_int32_t sense_flags),
1313 u_int32_t cam_flags, u_int32_t sense_flags)
1315 struct cd_softc *softc;
1316 struct cam_periph *periph;
1319 periph = xpt_path_periph(ccb->ccb_h.path);
1320 softc = (struct cd_softc *)periph->softc;
1322 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1323 softc->disk->d_devstat);
1325 if (softc->flags & CD_FLAG_CHANGER)
1326 cdchangerschedule(softc);
1332 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1334 struct cd_softc *softc;
1336 softc = (struct cd_softc *)periph->softc;
1338 if (softc->flags & CD_FLAG_CHANGER) {
1340 * This should work the first time this device is woken up,
1341 * but just in case it doesn't, we use a while loop.
1343 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1345 * If this changer isn't already queued, queue it up.
1347 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1348 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1349 softc->pinfo.generation =
1350 ++softc->changer->devq.generation;
1351 camq_insert(&softc->changer->devq,
1352 (cam_pinfo *)softc);
1354 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1355 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1356 && ((softc->changer->flags
1357 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1358 softc->changer->flags |= CHANGER_MANUAL_CALL;
1359 cdrunchangerqueue(softc->changer);
1361 msleep(&softc->changer, periph->sim->mtx,
1362 PRIBIO, "cgticb", 0);
1365 return(cam_periph_getccb(periph, priority));
1370 * Actually translate the requested transfer into one the physical driver
1371 * can understand. The transfer is described by a buf and will include
1372 * only one physical transfer.
1375 cdstrategy(struct bio *bp)
1377 struct cam_periph *periph;
1378 struct cd_softc *softc;
1380 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1381 if (periph == NULL) {
1382 biofinish(bp, NULL, ENXIO);
1386 cam_periph_lock(periph);
1387 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1389 softc = (struct cd_softc *)periph->softc;
1392 * If the device has been made invalid, error out
1394 if ((softc->flags & CD_FLAG_INVALID)) {
1395 cam_periph_unlock(periph);
1396 biofinish(bp, NULL, ENXIO);
1401 * If we don't have valid media, look for it before trying to
1404 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1407 error = cdcheckmedia(periph);
1409 cam_periph_unlock(periph);
1410 biofinish(bp, NULL, error);
1416 * Place it in the queue of disk activities for this disk
1418 bioq_disksort(&softc->bio_queue, bp);
1421 * Schedule ourselves for performing the work. We do things
1422 * differently for changers.
1424 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1425 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1427 cdschedule(periph, CAM_PRIORITY_NORMAL);
1429 cam_periph_unlock(periph);
1434 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1436 struct cd_softc *softc;
1438 struct ccb_scsiio *csio;
1439 struct scsi_read_capacity_data *rcap;
1441 softc = (struct cd_softc *)periph->softc;
1443 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1445 switch (softc->state) {
1446 case CD_STATE_NORMAL:
1448 bp = bioq_first(&softc->bio_queue);
1449 if (periph->immediate_priority <= periph->pinfo.priority) {
1450 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1452 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1454 periph->immediate_priority = CAM_PRIORITY_NONE;
1455 wakeup(&periph->ccb_list);
1456 } else if (bp == NULL) {
1457 xpt_release_ccb(start_ccb);
1459 bioq_remove(&softc->bio_queue, bp);
1461 scsi_read_write(&start_ccb->csio,
1462 /*retries*/ cd_retry_count,
1463 /* cbfcnp */ cddone,
1465 /* read */bp->bio_cmd == BIO_READ,
1467 /* minimum_cmd_size */ 10,
1468 /* lba */ bp->bio_offset /
1469 softc->params.blksize,
1470 bp->bio_bcount / softc->params.blksize,
1471 /* data_ptr */ bp->bio_data,
1472 /* dxfer_len */ bp->bio_bcount,
1473 /* sense_len */ SSD_FULL_SIZE,
1474 /* timeout */ 30000);
1475 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1478 LIST_INSERT_HEAD(&softc->pending_ccbs,
1479 &start_ccb->ccb_h, periph_links.le);
1480 softc->outstanding_cmds++;
1482 /* We expect a unit attention from this device */
1483 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1484 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1485 softc->flags &= ~CD_FLAG_RETRY_UA;
1488 start_ccb->ccb_h.ccb_bp = bp;
1489 bp = bioq_first(&softc->bio_queue);
1491 xpt_action(start_ccb);
1494 /* Have more work to do, so ensure we stay scheduled */
1495 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1499 case CD_STATE_PROBE:
1502 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1503 M_SCSICD, M_NOWAIT | M_ZERO);
1505 xpt_print(periph->path,
1506 "cdstart: Couldn't malloc read_capacity data\n");
1507 /* cd_free_periph??? */
1510 csio = &start_ccb->csio;
1511 scsi_read_capacity(csio,
1512 /*retries*/ cd_retry_count,
1518 start_ccb->ccb_h.ccb_bp = NULL;
1519 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1520 xpt_action(start_ccb);
1527 cddone(struct cam_periph *periph, union ccb *done_ccb)
1529 struct cd_softc *softc;
1530 struct ccb_scsiio *csio;
1532 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1534 softc = (struct cd_softc *)periph->softc;
1535 csio = &done_ccb->csio;
1537 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1538 case CD_CCB_BUFFER_IO:
1543 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1546 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1549 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1554 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1555 if (error == ERESTART) {
1557 * A retry was scheuled, so
1565 xpt_print(periph->path,
1566 "cddone: got error %#x back\n", error);
1567 bioq_flush(&softc->bio_queue, NULL, EIO);
1568 bp->bio_resid = bp->bio_bcount;
1569 bp->bio_error = error;
1570 bp->bio_flags |= BIO_ERROR;
1571 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1572 cam_release_devq(done_ccb->ccb_h.path,
1576 /*getcount_only*/0);
1579 bp->bio_resid = csio->resid;
1581 if (bp->bio_resid != 0) {
1583 * Short transfer ???
1584 * XXX: not sure this is correct for partial
1587 bp->bio_flags |= BIO_ERROR;
1591 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1592 softc->outstanding_cmds--;
1594 if (softc->flags & CD_FLAG_CHANGER)
1595 cdchangerschedule(softc);
1597 biofinish(bp, NULL, 0);
1602 struct scsi_read_capacity_data *rdcap;
1603 char announce_buf[120]; /*
1604 * Currently (9/30/97) the
1605 * longest possible announce
1606 * buffer is 108 bytes, for the
1607 * first error case below.
1608 * That is 39 bytes for the
1609 * basic string, 16 bytes for the
1610 * biggest sense key (hardware
1611 * error), 52 bytes for the
1612 * text of the largest sense
1613 * qualifier valid for a CDROM,
1614 * (0x72, 0x03 or 0x04,
1615 * 0x03), and one byte for the
1616 * null terminating character.
1617 * To allow for longer strings,
1618 * the announce buffer is 120
1621 struct cd_params *cdp;
1623 cdp = &softc->params;
1625 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1627 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1628 cdp->blksize = scsi_4btoul (rdcap->length);
1630 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1632 snprintf(announce_buf, sizeof(announce_buf),
1633 "cd present [%lu x %lu byte records]",
1634 cdp->disksize, (u_long)cdp->blksize);
1639 * Retry any UNIT ATTENTION type errors. They
1640 * are expected at boot.
1642 error = cderror(done_ccb, CAM_RETRY_SELTO,
1643 SF_RETRY_UA | SF_NO_PRINT);
1644 if (error == ERESTART) {
1646 * A retry was scheuled, so
1650 } else if (error != 0) {
1652 struct scsi_sense_data *sense;
1654 int sense_key, error_code;
1657 struct ccb_getdev cgd;
1659 /* Don't wedge this device's queue */
1660 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1661 cam_release_devq(done_ccb->ccb_h.path,
1665 /*getcount_only*/0);
1667 status = done_ccb->ccb_h.status;
1669 xpt_setup_ccb(&cgd.ccb_h,
1670 done_ccb->ccb_h.path,
1671 CAM_PRIORITY_NORMAL);
1672 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1673 xpt_action((union ccb *)&cgd);
1675 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1676 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1677 || ((status & CAM_AUTOSNS_VALID) == 0))
1683 sense = &csio->sense_data;
1684 scsi_extract_sense(sense, &error_code,
1689 * Attach to anything that claims to be a
1690 * CDROM or WORM device, as long as it
1691 * doesn't return a "Logical unit not
1692 * supported" (0x25) error.
1694 if ((have_sense) && (asc != 0x25)
1695 && (error_code == SSD_CURRENT_ERROR)) {
1696 const char *sense_key_desc;
1697 const char *asc_desc;
1699 scsi_sense_desc(sense_key, asc, ascq,
1703 snprintf(announce_buf,
1704 sizeof(announce_buf),
1705 "Attempt to query device "
1706 "size failed: %s, %s",
1709 } else if ((have_sense == 0)
1710 && ((status & CAM_STATUS_MASK) ==
1711 CAM_SCSI_STATUS_ERROR)
1712 && (csio->scsi_status ==
1713 SCSI_STATUS_BUSY)) {
1714 snprintf(announce_buf,
1715 sizeof(announce_buf),
1716 "Attempt to query device "
1717 "size failed: SCSI Status: %s",
1718 scsi_status_string(csio));
1719 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1721 * We only print out an error for
1722 * CDROM type devices. For WORM
1723 * devices, we don't print out an
1724 * error since a few WORM devices
1725 * don't support CDROM commands.
1726 * If we have sense information, go
1727 * ahead and print it out.
1728 * Otherwise, just say that we
1733 * Just print out the error, not
1734 * the full probe message, when we
1741 xpt_print(periph->path,
1742 "got CAM status %#x\n",
1743 done_ccb->ccb_h.status);
1745 xpt_print(periph->path, "fatal error, "
1746 "failed to attach to device\n");
1748 * Invalidate this peripheral.
1750 cam_periph_invalidate(periph);
1752 announce_buf[0] = '\0';
1756 * Invalidate this peripheral.
1758 cam_periph_invalidate(periph);
1759 announce_buf[0] = '\0';
1763 free(rdcap, M_SCSICD);
1764 if (announce_buf[0] != '\0') {
1765 xpt_announce_periph(periph, announce_buf);
1766 if (softc->flags & CD_FLAG_CHANGER)
1767 cdchangerschedule(softc);
1769 * Create our sysctl variables, now that we know
1770 * we have successfully attached.
1772 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1774 softc->state = CD_STATE_NORMAL;
1776 * Since our peripheral may be invalidated by an error
1777 * above or an external event, we must release our CCB
1778 * before releasing the probe lock on the peripheral.
1779 * The peripheral will only go away once the last lock
1780 * is removed, and we need it around for the CCB release
1783 xpt_release_ccb(done_ccb);
1784 cam_periph_unhold(periph);
1787 case CD_CCB_WAITING:
1789 /* Caller will release the CCB */
1790 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1791 ("trying to wakeup ccbwait\n"));
1793 wakeup(&done_ccb->ccb_h.cbfcnp);
1799 xpt_release_ccb(done_ccb);
1802 static union cd_pages *
1803 cdgetpage(struct cd_mode_params *mode_params)
1805 union cd_pages *page;
1807 if (mode_params->cdb_size == 10)
1808 page = (union cd_pages *)find_mode_page_10(
1809 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1811 page = (union cd_pages *)find_mode_page_6(
1812 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1818 cdgetpagesize(int page_num)
1822 for (i = 0; i < (sizeof(cd_page_size_table)/
1823 sizeof(cd_page_size_table[0])); i++) {
1824 if (cd_page_size_table[i].page == page_num)
1825 return (cd_page_size_table[i].page_size);
1832 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1835 struct cam_periph *periph;
1836 struct cd_softc *softc;
1837 int nocopyout, error = 0;
1839 periph = (struct cam_periph *)dp->d_drv1;
1843 cam_periph_lock(periph);
1844 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1846 softc = (struct cd_softc *)periph->softc;
1848 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1849 ("trying to do ioctl %#lx\n", cmd));
1851 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1852 cam_periph_unlock(periph);
1853 cam_periph_release(periph);
1858 * If we don't have media loaded, check for it. If still don't
1859 * have media loaded, we can only do a load or eject.
1861 * We only care whether media is loaded if this is a cd-specific ioctl
1862 * (thus the IOCGROUP check below). Note that this will break if
1863 * anyone adds any ioctls into the switch statement below that don't
1864 * have their ioctl group set to 'c'.
1866 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1867 && ((cmd != CDIOCCLOSE)
1868 && (cmd != CDIOCEJECT))
1869 && (IOCGROUP(cmd) == 'c')) {
1870 error = cdcheckmedia(periph);
1872 cam_periph_unhold(periph);
1873 cam_periph_unlock(periph);
1878 * Drop the lock here so later mallocs can use WAITOK. The periph
1879 * is essentially locked still with the cam_periph_hold call above.
1881 cam_periph_unlock(periph);
1886 case CDIOCPLAYTRACKS:
1888 struct ioc_play_track *args
1889 = (struct ioc_play_track *) addr;
1890 struct cd_mode_params params;
1891 union cd_pages *page;
1893 params.alloc_len = sizeof(union cd_mode_data_6_10);
1894 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1897 cam_periph_lock(periph);
1898 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1899 ("trying to do CDIOCPLAYTRACKS\n"));
1901 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1903 free(params.mode_buf, M_SCSICD);
1904 cam_periph_unlock(periph);
1907 page = cdgetpage(¶ms);
1909 page->audio.flags &= ~CD_PA_SOTC;
1910 page->audio.flags |= CD_PA_IMMED;
1911 error = cdsetmode(periph, ¶ms);
1912 free(params.mode_buf, M_SCSICD);
1914 cam_periph_unlock(periph);
1919 * This was originally implemented with the PLAY
1920 * AUDIO TRACK INDEX command, but that command was
1921 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1922 * drives support it but ATAPI and ATAPI-derivative
1923 * drives don't seem to support it. So we keep a
1924 * cache of the table of contents and translate
1925 * track numbers to MSF format.
1927 if (softc->flags & CD_FLAG_VALID_TOC) {
1928 union msf_lba *sentry, *eentry;
1931 if (args->end_track <
1932 softc->toc.header.ending_track + 1)
1934 if (args->end_track >
1935 softc->toc.header.ending_track + 1)
1937 softc->toc.header.ending_track + 1;
1938 st = args->start_track -
1939 softc->toc.header.starting_track;
1940 et = args->end_track -
1941 softc->toc.header.starting_track;
1944 || (st > (softc->toc.header.ending_track -
1945 softc->toc.header.starting_track))) {
1949 sentry = &softc->toc.entries[st].addr;
1950 eentry = &softc->toc.entries[et].addr;
1951 error = cdplaymsf(periph,
1960 * If we don't have a valid TOC, try the
1961 * play track index command. It is part of
1962 * the SCSI-2 spec, but was removed in the
1963 * MMC specs. ATAPI and ATAPI-derived
1964 * drives don't support it.
1966 if (softc->quirks & CD_Q_BCD_TRACKS) {
1968 bin2bcd(args->start_track);
1970 bin2bcd(args->end_track);
1972 error = cdplaytracks(periph,
1978 cam_periph_unlock(periph);
1983 struct ioc_play_msf *args
1984 = (struct ioc_play_msf *) addr;
1985 struct cd_mode_params params;
1986 union cd_pages *page;
1988 params.alloc_len = sizeof(union cd_mode_data_6_10);
1989 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1992 cam_periph_lock(periph);
1993 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1994 ("trying to do CDIOCPLAYMSF\n"));
1996 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1998 free(params.mode_buf, M_SCSICD);
1999 cam_periph_unlock(periph);
2002 page = cdgetpage(¶ms);
2004 page->audio.flags &= ~CD_PA_SOTC;
2005 page->audio.flags |= CD_PA_IMMED;
2006 error = cdsetmode(periph, ¶ms);
2007 free(params.mode_buf, M_SCSICD);
2009 cam_periph_unlock(periph);
2012 error = cdplaymsf(periph,
2019 cam_periph_unlock(periph);
2022 case CDIOCPLAYBLOCKS:
2024 struct ioc_play_blocks *args
2025 = (struct ioc_play_blocks *) addr;
2026 struct cd_mode_params params;
2027 union cd_pages *page;
2029 params.alloc_len = sizeof(union cd_mode_data_6_10);
2030 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2033 cam_periph_lock(periph);
2034 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2035 ("trying to do CDIOCPLAYBLOCKS\n"));
2038 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2040 free(params.mode_buf, M_SCSICD);
2041 cam_periph_unlock(periph);
2044 page = cdgetpage(¶ms);
2046 page->audio.flags &= ~CD_PA_SOTC;
2047 page->audio.flags |= CD_PA_IMMED;
2048 error = cdsetmode(periph, ¶ms);
2049 free(params.mode_buf, M_SCSICD);
2051 cam_periph_unlock(periph);
2054 error = cdplay(periph, args->blk, args->len);
2055 cam_periph_unlock(periph);
2058 case CDIOCREADSUBCHANNEL_SYSSPACE:
2061 case CDIOCREADSUBCHANNEL:
2063 struct ioc_read_subchannel *args
2064 = (struct ioc_read_subchannel *) addr;
2065 struct cd_sub_channel_info *data;
2066 u_int32_t len = args->data_len;
2068 data = malloc(sizeof(struct cd_sub_channel_info),
2069 M_SCSICD, M_WAITOK | M_ZERO);
2071 cam_periph_lock(periph);
2072 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2073 ("trying to do CDIOCREADSUBCHANNEL\n"));
2075 if ((len > sizeof(struct cd_sub_channel_info)) ||
2076 (len < sizeof(struct cd_sub_channel_header))) {
2078 "scsi_cd: cdioctl: "
2079 "cdioreadsubchannel: error, len=%d\n",
2082 free(data, M_SCSICD);
2083 cam_periph_unlock(periph);
2087 if (softc->quirks & CD_Q_BCD_TRACKS)
2088 args->track = bin2bcd(args->track);
2090 error = cdreadsubchannel(periph, args->address_format,
2091 args->data_format, args->track, data, len);
2094 free(data, M_SCSICD);
2095 cam_periph_unlock(periph);
2098 if (softc->quirks & CD_Q_BCD_TRACKS)
2099 data->what.track_info.track_number =
2100 bcd2bin(data->what.track_info.track_number);
2101 len = min(len, ((data->header.data_len[0] << 8) +
2102 data->header.data_len[1] +
2103 sizeof(struct cd_sub_channel_header)));
2104 cam_periph_unlock(periph);
2105 if (nocopyout == 0) {
2106 if (copyout(data, args->data, len) != 0) {
2110 bcopy(data, args->data, len);
2112 free(data, M_SCSICD);
2116 case CDIOREADTOCHEADER:
2118 struct ioc_toc_header *th;
2120 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2123 cam_periph_lock(periph);
2124 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2125 ("trying to do CDIOREADTOCHEADER\n"));
2127 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2128 sizeof (*th), /*sense_flags*/0);
2131 cam_periph_unlock(periph);
2134 if (softc->quirks & CD_Q_BCD_TRACKS) {
2135 /* we are going to have to convert the BCD
2136 * encoding on the cd to what is expected
2138 th->starting_track =
2139 bcd2bin(th->starting_track);
2140 th->ending_track = bcd2bin(th->ending_track);
2142 th->len = ntohs(th->len);
2143 bcopy(th, addr, sizeof(*th));
2145 cam_periph_unlock(periph);
2148 case CDIOREADTOCENTRYS:
2150 struct cd_tocdata *data;
2151 struct cd_toc_single *lead;
2152 struct ioc_read_toc_entry *te =
2153 (struct ioc_read_toc_entry *) addr;
2154 struct ioc_toc_header *th;
2155 u_int32_t len, readlen, idx, num;
2156 u_int32_t starting_track = te->starting_track;
2158 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2159 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2161 cam_periph_lock(periph);
2162 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2163 ("trying to do CDIOREADTOCENTRYS\n"));
2165 if (te->data_len < sizeof(struct cd_toc_entry)
2166 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2167 || (te->address_format != CD_MSF_FORMAT
2168 && te->address_format != CD_LBA_FORMAT)) {
2170 printf("scsi_cd: error in readtocentries, "
2171 "returning EINVAL\n");
2172 free(data, M_SCSICD);
2173 free(lead, M_SCSICD);
2174 cam_periph_unlock(periph);
2179 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2180 sizeof (*th), /*sense_flags*/0);
2182 free(data, M_SCSICD);
2183 free(lead, M_SCSICD);
2184 cam_periph_unlock(periph);
2188 if (softc->quirks & CD_Q_BCD_TRACKS) {
2189 /* we are going to have to convert the BCD
2190 * encoding on the cd to what is expected
2192 th->starting_track =
2193 bcd2bin(th->starting_track);
2194 th->ending_track = bcd2bin(th->ending_track);
2197 if (starting_track == 0)
2198 starting_track = th->starting_track;
2199 else if (starting_track == LEADOUT)
2200 starting_track = th->ending_track + 1;
2201 else if (starting_track < th->starting_track ||
2202 starting_track > th->ending_track + 1) {
2203 printf("scsi_cd: error in readtocentries, "
2204 "returning EINVAL\n");
2205 free(data, M_SCSICD);
2206 free(lead, M_SCSICD);
2207 cam_periph_unlock(periph);
2212 /* calculate reading length without leadout entry */
2213 readlen = (th->ending_track - starting_track + 1) *
2214 sizeof(struct cd_toc_entry);
2216 /* and with leadout entry */
2217 len = readlen + sizeof(struct cd_toc_entry);
2218 if (te->data_len < len) {
2223 if (len > sizeof(data->entries)) {
2224 printf("scsi_cd: error in readtocentries, "
2225 "returning EINVAL\n");
2227 free(data, M_SCSICD);
2228 free(lead, M_SCSICD);
2229 cam_periph_unlock(periph);
2232 num = len / sizeof(struct cd_toc_entry);
2235 error = cdreadtoc(periph, te->address_format,
2238 readlen + sizeof (*th),
2241 free(data, M_SCSICD);
2242 free(lead, M_SCSICD);
2243 cam_periph_unlock(periph);
2248 /* make leadout entry if needed */
2249 idx = starting_track + num - 1;
2250 if (softc->quirks & CD_Q_BCD_TRACKS)
2251 th->ending_track = bcd2bin(th->ending_track);
2252 if (idx == th->ending_track + 1) {
2253 error = cdreadtoc(periph, te->address_format,
2254 LEADOUT, (u_int8_t *)lead,
2258 free(data, M_SCSICD);
2259 free(lead, M_SCSICD);
2260 cam_periph_unlock(periph);
2263 data->entries[idx - starting_track] =
2266 if (softc->quirks & CD_Q_BCD_TRACKS) {
2267 for (idx = 0; idx < num - 1; idx++) {
2268 data->entries[idx].track =
2269 bcd2bin(data->entries[idx].track);
2273 cam_periph_unlock(periph);
2274 error = copyout(data->entries, te->data, len);
2275 free(data, M_SCSICD);
2276 free(lead, M_SCSICD);
2279 case CDIOREADTOCENTRY:
2281 struct cd_toc_single *data;
2282 struct ioc_read_toc_single_entry *te =
2283 (struct ioc_read_toc_single_entry *) addr;
2284 struct ioc_toc_header *th;
2287 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2289 cam_periph_lock(periph);
2290 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2291 ("trying to do CDIOREADTOCENTRY\n"));
2293 if (te->address_format != CD_MSF_FORMAT
2294 && te->address_format != CD_LBA_FORMAT) {
2295 printf("error in readtocentry, "
2296 " returning EINVAL\n");
2297 free(data, M_SCSICD);
2299 cam_periph_unlock(periph);
2304 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2305 sizeof (*th), /*sense_flags*/0);
2307 free(data, M_SCSICD);
2308 cam_periph_unlock(periph);
2312 if (softc->quirks & CD_Q_BCD_TRACKS) {
2313 /* we are going to have to convert the BCD
2314 * encoding on the cd to what is expected
2316 th->starting_track =
2317 bcd2bin(th->starting_track);
2318 th->ending_track = bcd2bin(th->ending_track);
2322 track = th->starting_track;
2323 else if (track == LEADOUT)
2325 else if (track < th->starting_track ||
2326 track > th->ending_track + 1) {
2327 printf("error in readtocentry, "
2328 " returning EINVAL\n");
2329 free(data, M_SCSICD);
2331 cam_periph_unlock(periph);
2335 error = cdreadtoc(periph, te->address_format, track,
2336 (u_int8_t *)data, sizeof(*data),
2339 free(data, M_SCSICD);
2340 cam_periph_unlock(periph);
2344 if (softc->quirks & CD_Q_BCD_TRACKS)
2345 data->entry.track = bcd2bin(data->entry.track);
2346 bcopy(&data->entry, &te->entry,
2347 sizeof(struct cd_toc_entry));
2348 free(data, M_SCSICD);
2349 cam_periph_unlock(periph);
2354 struct ioc_patch *arg = (struct ioc_patch *)addr;
2355 struct cd_mode_params params;
2356 union cd_pages *page;
2358 params.alloc_len = sizeof(union cd_mode_data_6_10);
2359 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2362 cam_periph_lock(periph);
2363 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2364 ("trying to do CDIOCSETPATCH\n"));
2366 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2368 free(params.mode_buf, M_SCSICD);
2369 cam_periph_unlock(periph);
2372 page = cdgetpage(¶ms);
2374 page->audio.port[LEFT_PORT].channels =
2376 page->audio.port[RIGHT_PORT].channels =
2378 page->audio.port[2].channels = arg->patch[2];
2379 page->audio.port[3].channels = arg->patch[3];
2380 error = cdsetmode(periph, ¶ms);
2381 free(params.mode_buf, M_SCSICD);
2382 cam_periph_unlock(periph);
2387 struct ioc_vol *arg = (struct ioc_vol *) addr;
2388 struct cd_mode_params params;
2389 union cd_pages *page;
2391 params.alloc_len = sizeof(union cd_mode_data_6_10);
2392 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2395 cam_periph_lock(periph);
2396 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2397 ("trying to do CDIOCGETVOL\n"));
2399 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2401 free(params.mode_buf, M_SCSICD);
2402 cam_periph_unlock(periph);
2405 page = cdgetpage(¶ms);
2407 arg->vol[LEFT_PORT] =
2408 page->audio.port[LEFT_PORT].volume;
2409 arg->vol[RIGHT_PORT] =
2410 page->audio.port[RIGHT_PORT].volume;
2411 arg->vol[2] = page->audio.port[2].volume;
2412 arg->vol[3] = page->audio.port[3].volume;
2413 free(params.mode_buf, M_SCSICD);
2414 cam_periph_unlock(periph);
2419 struct ioc_vol *arg = (struct ioc_vol *) addr;
2420 struct cd_mode_params params;
2421 union cd_pages *page;
2423 params.alloc_len = sizeof(union cd_mode_data_6_10);
2424 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2427 cam_periph_lock(periph);
2428 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2429 ("trying to do CDIOCSETVOL\n"));
2431 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2433 free(params.mode_buf, M_SCSICD);
2434 cam_periph_unlock(periph);
2437 page = cdgetpage(¶ms);
2439 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2440 page->audio.port[LEFT_PORT].volume =
2441 arg->vol[LEFT_PORT];
2442 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2443 page->audio.port[RIGHT_PORT].volume =
2444 arg->vol[RIGHT_PORT];
2445 page->audio.port[2].volume = arg->vol[2];
2446 page->audio.port[3].volume = arg->vol[3];
2447 error = cdsetmode(periph, ¶ms);
2448 cam_periph_unlock(periph);
2449 free(params.mode_buf, M_SCSICD);
2454 struct cd_mode_params params;
2455 union cd_pages *page;
2457 params.alloc_len = sizeof(union cd_mode_data_6_10);
2458 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2461 cam_periph_lock(periph);
2462 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2463 ("trying to do CDIOCSETMONO\n"));
2465 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2467 free(params.mode_buf, M_SCSICD);
2468 cam_periph_unlock(periph);
2471 page = cdgetpage(¶ms);
2473 page->audio.port[LEFT_PORT].channels =
2474 LEFT_CHANNEL | RIGHT_CHANNEL;
2475 page->audio.port[RIGHT_PORT].channels =
2476 LEFT_CHANNEL | RIGHT_CHANNEL;
2477 page->audio.port[2].channels = 0;
2478 page->audio.port[3].channels = 0;
2479 error = cdsetmode(periph, ¶ms);
2480 cam_periph_unlock(periph);
2481 free(params.mode_buf, M_SCSICD);
2484 case CDIOCSETSTEREO:
2486 struct cd_mode_params params;
2487 union cd_pages *page;
2489 params.alloc_len = sizeof(union cd_mode_data_6_10);
2490 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2493 cam_periph_lock(periph);
2494 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2495 ("trying to do CDIOCSETSTEREO\n"));
2497 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2499 free(params.mode_buf, M_SCSICD);
2500 cam_periph_unlock(periph);
2503 page = cdgetpage(¶ms);
2505 page->audio.port[LEFT_PORT].channels =
2507 page->audio.port[RIGHT_PORT].channels =
2509 page->audio.port[2].channels = 0;
2510 page->audio.port[3].channels = 0;
2511 error = cdsetmode(periph, ¶ms);
2512 free(params.mode_buf, M_SCSICD);
2513 cam_periph_unlock(periph);
2518 struct cd_mode_params params;
2519 union cd_pages *page;
2521 params.alloc_len = sizeof(union cd_mode_data_6_10);
2522 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2525 cam_periph_lock(periph);
2526 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2527 ("trying to do CDIOCSETMUTE\n"));
2529 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2531 free(params.mode_buf, M_SCSICD);
2532 cam_periph_unlock(periph);
2535 page = cdgetpage(¶ms);
2537 page->audio.port[LEFT_PORT].channels = 0;
2538 page->audio.port[RIGHT_PORT].channels = 0;
2539 page->audio.port[2].channels = 0;
2540 page->audio.port[3].channels = 0;
2541 error = cdsetmode(periph, ¶ms);
2542 free(params.mode_buf, M_SCSICD);
2543 cam_periph_unlock(periph);
2548 struct cd_mode_params params;
2549 union cd_pages *page;
2551 params.alloc_len = sizeof(union cd_mode_data_6_10);
2552 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2555 cam_periph_lock(periph);
2556 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2557 ("trying to do CDIOCSETLEFT\n"));
2559 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2561 free(params.mode_buf, M_SCSICD);
2562 cam_periph_unlock(periph);
2565 page = cdgetpage(¶ms);
2567 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2568 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2569 page->audio.port[2].channels = 0;
2570 page->audio.port[3].channels = 0;
2571 error = cdsetmode(periph, ¶ms);
2572 free(params.mode_buf, M_SCSICD);
2573 cam_periph_unlock(periph);
2578 struct cd_mode_params params;
2579 union cd_pages *page;
2581 params.alloc_len = sizeof(union cd_mode_data_6_10);
2582 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2585 cam_periph_lock(periph);
2586 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2587 ("trying to do CDIOCSETRIGHT\n"));
2589 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2591 free(params.mode_buf, M_SCSICD);
2592 cam_periph_unlock(periph);
2595 page = cdgetpage(¶ms);
2597 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2598 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2599 page->audio.port[2].channels = 0;
2600 page->audio.port[3].channels = 0;
2601 error = cdsetmode(periph, ¶ms);
2602 free(params.mode_buf, M_SCSICD);
2603 cam_periph_unlock(periph);
2607 cam_periph_lock(periph);
2608 error = cdpause(periph, 1);
2609 cam_periph_unlock(periph);
2612 cam_periph_lock(periph);
2613 error = cdpause(periph, 0);
2614 cam_periph_unlock(periph);
2617 cam_periph_lock(periph);
2618 error = cdstartunit(periph, 0);
2619 cam_periph_unlock(periph);
2622 cam_periph_lock(periph);
2623 error = cdstartunit(periph, 1);
2624 cam_periph_unlock(periph);
2627 cam_periph_lock(periph);
2628 error = cdstopunit(periph, 0);
2629 cam_periph_unlock(periph);
2632 cam_periph_lock(periph);
2633 error = cdstopunit(periph, 1);
2634 cam_periph_unlock(periph);
2637 cam_periph_lock(periph);
2638 cdprevent(periph, PR_ALLOW);
2639 cam_periph_unlock(periph);
2642 cam_periph_lock(periph);
2643 cdprevent(periph, PR_PREVENT);
2644 cam_periph_unlock(periph);
2647 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2651 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2655 /* return (cd_reset(periph)); */
2658 case CDRIOCREADSPEED:
2659 cam_periph_lock(periph);
2660 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2661 cam_periph_unlock(periph);
2663 case CDRIOCWRITESPEED:
2664 cam_periph_lock(periph);
2665 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2666 cam_periph_unlock(periph);
2669 case DVDIOCREPORTKEY: {
2670 struct dvd_authinfo *authinfo;
2672 authinfo = (struct dvd_authinfo *)addr;
2674 if (cmd == DVDIOCREPORTKEY)
2675 error = cdreportkey(periph, authinfo);
2677 error = cdsendkey(periph, authinfo);
2680 case DVDIOCREADSTRUCTURE: {
2681 struct dvd_struct *dvdstruct;
2683 dvdstruct = (struct dvd_struct *)addr;
2685 error = cdreaddvdstructure(periph, dvdstruct);
2690 cam_periph_lock(periph);
2691 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2692 cam_periph_unlock(periph);
2696 cam_periph_lock(periph);
2697 cam_periph_unhold(periph);
2699 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2700 if (error && bootverbose) {
2701 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2703 cam_periph_unlock(periph);
2709 cdprevent(struct cam_periph *periph, int action)
2712 struct cd_softc *softc;
2715 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2717 softc = (struct cd_softc *)periph->softc;
2719 if (((action == PR_ALLOW)
2720 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2721 || ((action == PR_PREVENT)
2722 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2726 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2728 scsi_prevent(&ccb->csio,
2729 /*retries*/ cd_retry_count,
2734 /* timeout */60000);
2736 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2737 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2739 xpt_release_ccb(ccb);
2742 if (action == PR_ALLOW)
2743 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2745 softc->flags |= CD_FLAG_DISC_LOCKED;
2750 * XXX: the disk media and sector size is only really able to change
2751 * XXX: while the device is closed.
2754 cdcheckmedia(struct cam_periph *periph)
2756 struct cd_softc *softc;
2757 struct ioc_toc_header *toch;
2758 struct cd_toc_single leadout;
2759 u_int32_t size, toclen;
2760 int error, num_entries, cdindex;
2762 softc = (struct cd_softc *)periph->softc;
2764 cdprevent(periph, PR_PREVENT);
2765 softc->disk->d_sectorsize = 2048;
2766 softc->disk->d_mediasize = 0;
2769 * Get the disc size and block size. If we can't get it, we don't
2770 * have media, most likely.
2772 if ((error = cdsize(periph, &size)) != 0) {
2773 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2774 cdprevent(periph, PR_ALLOW);
2777 softc->flags |= CD_FLAG_VALID_MEDIA;
2778 softc->disk->d_sectorsize = softc->params.blksize;
2779 softc->disk->d_mediasize =
2780 (off_t)softc->params.blksize * softc->params.disksize;
2784 * Now we check the table of contents. This (currently) is only
2785 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2786 * things like present a separate entry in /dev for each track,
2787 * like that acd(4) driver does.
2789 bzero(&softc->toc, sizeof(softc->toc));
2790 toch = &softc->toc.header;
2792 * We will get errors here for media that doesn't have a table of
2793 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2794 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2795 * has not been recorded (no complete session) and the Format codes
2796 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2797 * with an INVALID FIELD IN CDB. Devices that are not capable of
2798 * reading an incomplete session on DDC/CD-R/RW media shall report
2799 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2801 * So this isn't fatal if we can't read the table of contents, it
2802 * just means that the user won't be able to issue the play tracks
2803 * ioctl, and likely lots of other stuff won't work either. They
2804 * need to burn the CD before we can do a whole lot with it. So
2805 * we don't print anything here if we get an error back.
2807 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2810 * Errors in reading the table of contents aren't fatal, we just
2811 * won't have a valid table of contents cached.
2815 bzero(&softc->toc, sizeof(softc->toc));
2819 if (softc->quirks & CD_Q_BCD_TRACKS) {
2820 toch->starting_track = bcd2bin(toch->starting_track);
2821 toch->ending_track = bcd2bin(toch->ending_track);
2824 /* Number of TOC entries, plus leadout */
2825 num_entries = (toch->ending_track - toch->starting_track) + 2;
2827 if (num_entries <= 0)
2830 toclen = num_entries * sizeof(struct cd_toc_entry);
2832 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2833 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2837 bzero(&softc->toc, sizeof(softc->toc));
2841 if (softc->quirks & CD_Q_BCD_TRACKS) {
2842 toch->starting_track = bcd2bin(toch->starting_track);
2843 toch->ending_track = bcd2bin(toch->ending_track);
2846 * XXX KDM is this necessary? Probably only if the drive doesn't
2847 * return leadout information with the table of contents.
2849 cdindex = toch->starting_track + num_entries -1;
2850 if (cdindex == toch->ending_track + 1) {
2852 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2853 (u_int8_t *)&leadout, sizeof(leadout),
2859 softc->toc.entries[cdindex - toch->starting_track] =
2862 if (softc->quirks & CD_Q_BCD_TRACKS) {
2863 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2864 softc->toc.entries[cdindex].track =
2865 bcd2bin(softc->toc.entries[cdindex].track);
2869 softc->flags |= CD_FLAG_VALID_TOC;
2874 * We unconditionally (re)set the blocksize each time the
2875 * CD device is opened. This is because the CD can change,
2876 * and therefore the blocksize might change.
2877 * XXX problems here if some slice or partition is still
2878 * open with the old size?
2880 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2881 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2882 softc->disk->d_devstat->block_size = softc->params.blksize;
2888 cdsize(struct cam_periph *periph, u_int32_t *size)
2890 struct cd_softc *softc;
2892 struct scsi_read_capacity_data *rcap_buf;
2895 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2897 softc = (struct cd_softc *)periph->softc;
2899 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2901 /* XXX Should be M_WAITOK */
2902 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2903 M_SCSICD, M_NOWAIT | M_ZERO);
2904 if (rcap_buf == NULL)
2907 scsi_read_capacity(&ccb->csio,
2908 /*retries*/ cd_retry_count,
2913 /* timeout */20000);
2915 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2916 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2918 xpt_release_ccb(ccb);
2920 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
2921 softc->params.blksize = scsi_4btoul(rcap_buf->length);
2922 /* Make sure we got at least some block size. */
2923 if (error == 0 && softc->params.blksize == 0)
2926 * SCSI-3 mandates that the reported blocksize shall be 2048.
2927 * Older drives sometimes report funny values, trim it down to
2928 * 2048, or other parts of the kernel will get confused.
2930 * XXX we leave drives alone that might report 512 bytes, as
2931 * well as drives reporting more weird sizes like perhaps 4K.
2933 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
2934 softc->params.blksize = 2048;
2936 free(rcap_buf, M_SCSICD);
2937 *size = softc->params.disksize;
2944 cd6byteworkaround(union ccb *ccb)
2947 struct cam_periph *periph;
2948 struct cd_softc *softc;
2949 struct cd_mode_params *params;
2952 periph = xpt_path_periph(ccb->ccb_h.path);
2953 softc = (struct cd_softc *)periph->softc;
2955 cdb = ccb->csio.cdb_io.cdb_bytes;
2957 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2958 || ((cdb[0] != MODE_SENSE_6)
2959 && (cdb[0] != MODE_SELECT_6)))
2963 * Because there is no convenient place to stash the overall
2964 * cd_mode_params structure pointer, we have to grab it like this.
2965 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2966 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2968 * XXX It would be nice if, at some point, we could increase the
2969 * number of available peripheral private pointers. Both pointers
2970 * are currently used in most every peripheral driver.
2974 STAILQ_FOREACH(params, &softc->mode_queue, links) {
2975 if (params->mode_buf == ccb->csio.data_ptr) {
2982 * This shouldn't happen. All mode sense and mode select
2983 * operations in the cd(4) driver MUST go through cdgetmode() and
2987 xpt_print(periph->path,
2988 "mode buffer not found in mode queue!\n");
2992 params->cdb_size = 10;
2993 softc->minimum_command_size = 10;
2994 xpt_print(ccb->ccb_h.path,
2995 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2996 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2998 if (cdb[0] == MODE_SENSE_6) {
2999 struct scsi_mode_sense_10 ms10;
3000 struct scsi_mode_sense_6 *ms6;
3003 ms6 = (struct scsi_mode_sense_6 *)cdb;
3005 bzero(&ms10, sizeof(ms10));
3006 ms10.opcode = MODE_SENSE_10;
3007 ms10.byte2 = ms6->byte2;
3008 ms10.page = ms6->page;
3011 * 10 byte mode header, block descriptor,
3012 * sizeof(union cd_pages)
3014 len = sizeof(struct cd_mode_data_10);
3015 ccb->csio.dxfer_len = len;
3017 scsi_ulto2b(len, ms10.length);
3018 ms10.control = ms6->control;
3019 bcopy(&ms10, cdb, 10);
3020 ccb->csio.cdb_len = 10;
3022 struct scsi_mode_select_10 ms10;
3023 struct scsi_mode_select_6 *ms6;
3024 struct scsi_mode_header_6 *header6;
3025 struct scsi_mode_header_10 *header10;
3026 struct scsi_mode_page_header *page_header;
3027 int blk_desc_len, page_num, page_size, len;
3029 ms6 = (struct scsi_mode_select_6 *)cdb;
3031 bzero(&ms10, sizeof(ms10));
3032 ms10.opcode = MODE_SELECT_10;
3033 ms10.byte2 = ms6->byte2;
3035 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3036 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3038 page_header = find_mode_page_6(header6);
3039 page_num = page_header->page_code;
3041 blk_desc_len = header6->blk_desc_len;
3043 page_size = cdgetpagesize(page_num);
3045 if (page_size != (page_header->page_length +
3046 sizeof(*page_header)))
3047 page_size = page_header->page_length +
3048 sizeof(*page_header);
3050 len = sizeof(*header10) + blk_desc_len + page_size;
3052 len = min(params->alloc_len, len);
3055 * Since the 6 byte parameter header is shorter than the 10
3056 * byte parameter header, we need to copy the actual mode
3057 * page data, and the block descriptor, if any, so things wind
3058 * up in the right place. The regions will overlap, but
3059 * bcopy() does the right thing.
3061 bcopy(params->mode_buf + sizeof(*header6),
3062 params->mode_buf + sizeof(*header10),
3063 len - sizeof(*header10));
3065 /* Make sure these fields are set correctly. */
3066 scsi_ulto2b(0, header10->data_length);
3067 header10->medium_type = 0;
3068 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3070 ccb->csio.dxfer_len = len;
3072 scsi_ulto2b(len, ms10.length);
3073 ms10.control = ms6->control;
3074 bcopy(&ms10, cdb, 10);
3075 ccb->csio.cdb_len = 10;
3078 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3079 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3082 cam_release_devq(ccb->ccb_h.path,
3086 /*getcount_only*/0);
3093 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3095 struct cd_softc *softc;
3096 struct cam_periph *periph;
3099 periph = xpt_path_periph(ccb->ccb_h.path);
3100 softc = (struct cd_softc *)periph->softc;
3105 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3106 * CDB comes back with this particular error, try transforming it
3107 * into the 10 byte version.
3109 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3110 error = cd6byteworkaround(ccb);
3111 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3112 CAM_SCSI_STATUS_ERROR)
3113 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3114 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3115 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3116 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3117 int sense_key, error_code, asc, ascq;
3119 scsi_extract_sense(&ccb->csio.sense_data,
3120 &error_code, &sense_key, &asc, &ascq);
3121 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3122 error = cd6byteworkaround(ccb);
3125 if (error == ERESTART)
3130 * Until we have a better way of doing pack validation,
3131 * don't treat UAs as errors.
3133 sense_flags |= SF_RETRY_UA;
3134 return (cam_periph_error(ccb, cam_flags, sense_flags,
3135 &softc->saved_ccb));
3139 * Read table of contents
3142 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3143 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3145 struct scsi_read_toc *scsi_cmd;
3147 struct ccb_scsiio *csio;
3154 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3159 /* retries */ cd_retry_count,
3160 /* cbfcnp */ cddone,
3161 /* flags */ CAM_DIR_IN,
3162 /* tag_action */ MSG_SIMPLE_Q_TAG,
3163 /* data_ptr */ data,
3164 /* dxfer_len */ len,
3165 /* sense_len */ SSD_FULL_SIZE,
3166 sizeof(struct scsi_read_toc),
3167 /* timeout */ 50000);
3169 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3170 bzero (scsi_cmd, sizeof(*scsi_cmd));
3172 if (mode == CD_MSF_FORMAT)
3173 scsi_cmd->byte2 |= CD_MSF;
3174 scsi_cmd->from_track = start;
3175 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3176 scsi_cmd->data_len[0] = (ntoc) >> 8;
3177 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3179 scsi_cmd->op_code = READ_TOC;
3181 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3182 /*sense_flags*/SF_RETRY_UA | sense_flags);
3184 xpt_release_ccb(ccb);
3190 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3191 u_int32_t format, int track,
3192 struct cd_sub_channel_info *data, u_int32_t len)
3194 struct scsi_read_subchannel *scsi_cmd;
3195 struct ccb_scsiio *csio;
3201 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3206 /* retries */ cd_retry_count,
3207 /* cbfcnp */ cddone,
3208 /* flags */ CAM_DIR_IN,
3209 /* tag_action */ MSG_SIMPLE_Q_TAG,
3210 /* data_ptr */ (u_int8_t *)data,
3211 /* dxfer_len */ len,
3212 /* sense_len */ SSD_FULL_SIZE,
3213 sizeof(struct scsi_read_subchannel),
3214 /* timeout */ 50000);
3216 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3217 bzero (scsi_cmd, sizeof(*scsi_cmd));
3219 scsi_cmd->op_code = READ_SUBCHANNEL;
3220 if (mode == CD_MSF_FORMAT)
3221 scsi_cmd->byte1 |= CD_MSF;
3222 scsi_cmd->byte2 = SRS_SUBQ;
3223 scsi_cmd->subchan_format = format;
3224 scsi_cmd->track = track;
3225 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3226 scsi_cmd->control = 0;
3228 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3229 /*sense_flags*/SF_RETRY_UA);
3231 xpt_release_ccb(ccb);
3238 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3239 * routine. See comments in cd6byteworkaround() for details.
3242 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3245 struct ccb_scsiio *csio;
3246 struct cd_softc *softc;
3251 softc = (struct cd_softc *)periph->softc;
3253 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3257 data->cdb_size = softc->minimum_command_size;
3258 if (data->cdb_size < 10)
3259 param_len = sizeof(struct cd_mode_data);
3261 param_len = sizeof(struct cd_mode_data_10);
3263 /* Don't say we've got more room than we actually allocated */
3264 param_len = min(param_len, data->alloc_len);
3266 scsi_mode_sense_len(csio,
3267 /* retries */ cd_retry_count,
3268 /* cbfcnp */ cddone,
3269 /* tag_action */ MSG_SIMPLE_Q_TAG,
3271 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3273 /* param_buf */ data->mode_buf,
3274 /* param_len */ param_len,
3275 /* minimum_cmd_size */ softc->minimum_command_size,
3276 /* sense_len */ SSD_FULL_SIZE,
3277 /* timeout */ 50000);
3280 * It would be nice not to have to do this, but there's no
3281 * available pointer in the CCB that would allow us to stuff the
3282 * mode params structure in there and retrieve it in
3283 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3284 * lets the caller know what CDB size we ended up using, so they
3285 * can find the actual mode page offset.
3287 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3289 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3290 /*sense_flags*/SF_RETRY_UA);
3292 xpt_release_ccb(ccb);
3294 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3297 * This is a bit of belt-and-suspenders checking, but if we run
3298 * into a situation where the target sends back multiple block
3299 * descriptors, we might not have enough space in the buffer to
3300 * see the whole mode page. Better to return an error than
3301 * potentially access memory beyond our malloced region.
3306 if (data->cdb_size == 10) {
3307 struct scsi_mode_header_10 *hdr10;
3309 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3310 data_len = scsi_2btoul(hdr10->data_length);
3311 data_len += sizeof(hdr10->data_length);
3313 struct scsi_mode_header_6 *hdr6;
3315 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3316 data_len = hdr6->data_length;
3317 data_len += sizeof(hdr6->data_length);
3321 * Complain if there is more mode data available than we
3322 * allocated space for. This could potentially happen if
3323 * we miscalculated the page length for some reason, if the
3324 * drive returns multiple block descriptors, or if it sets
3325 * the data length incorrectly.
3327 if (data_len > data->alloc_len) {
3328 xpt_print(periph->path, "allocated modepage %d length "
3329 "%d < returned length %d\n", page, data->alloc_len,
3338 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3339 * routine. See comments in cd6byteworkaround() for details.
3342 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3344 struct ccb_scsiio *csio;
3345 struct cd_softc *softc;
3347 int cdb_size, param_len;
3350 softc = (struct cd_softc *)periph->softc;
3352 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3359 * If the data is formatted for the 10 byte version of the mode
3360 * select parameter list, we need to use the 10 byte CDB.
3361 * Otherwise, we use whatever the stored minimum command size.
3363 if (data->cdb_size == 10)
3364 cdb_size = data->cdb_size;
3366 cdb_size = softc->minimum_command_size;
3368 if (cdb_size >= 10) {
3369 struct scsi_mode_header_10 *mode_header;
3372 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3374 data_len = scsi_2btoul(mode_header->data_length);
3376 scsi_ulto2b(0, mode_header->data_length);
3378 * SONY drives do not allow a mode select with a medium_type
3379 * value that has just been returned by a mode sense; use a
3380 * medium_type of 0 (Default) instead.
3382 mode_header->medium_type = 0;
3385 * Pass back whatever the drive passed to us, plus the size
3386 * of the data length field.
3388 param_len = data_len + sizeof(mode_header->data_length);
3391 struct scsi_mode_header_6 *mode_header;
3393 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3395 param_len = mode_header->data_length + 1;
3397 mode_header->data_length = 0;
3399 * SONY drives do not allow a mode select with a medium_type
3400 * value that has just been returned by a mode sense; use a
3401 * medium_type of 0 (Default) instead.
3403 mode_header->medium_type = 0;
3406 /* Don't say we've got more room than we actually allocated */
3407 param_len = min(param_len, data->alloc_len);
3409 scsi_mode_select_len(csio,
3410 /* retries */ cd_retry_count,
3411 /* cbfcnp */ cddone,
3412 /* tag_action */ MSG_SIMPLE_Q_TAG,
3413 /* scsi_page_fmt */ 1,
3415 /* param_buf */ data->mode_buf,
3416 /* param_len */ param_len,
3417 /* minimum_cmd_size */ cdb_size,
3418 /* sense_len */ SSD_FULL_SIZE,
3419 /* timeout */ 50000);
3421 /* See comments in cdgetmode() and cd6byteworkaround(). */
3422 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3424 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3425 /*sense_flags*/SF_RETRY_UA);
3427 xpt_release_ccb(ccb);
3429 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3436 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3438 struct ccb_scsiio *csio;
3444 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3447 * Use the smallest possible command to perform the operation.
3449 if ((len & 0xffff0000) == 0) {
3451 * We can fit in a 10 byte cdb.
3453 struct scsi_play_10 *scsi_cmd;
3455 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3456 bzero (scsi_cmd, sizeof(*scsi_cmd));
3457 scsi_cmd->op_code = PLAY_10;
3458 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3459 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3460 cdb_len = sizeof(*scsi_cmd);
3462 struct scsi_play_12 *scsi_cmd;
3464 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3465 bzero (scsi_cmd, sizeof(*scsi_cmd));
3466 scsi_cmd->op_code = PLAY_12;
3467 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3468 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3469 cdb_len = sizeof(*scsi_cmd);
3472 /*retries*/ cd_retry_count,
3474 /*flags*/CAM_DIR_NONE,
3478 /*sense_len*/SSD_FULL_SIZE,
3480 /*timeout*/50 * 1000);
3482 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3483 /*sense_flags*/SF_RETRY_UA);
3485 xpt_release_ccb(ccb);
3491 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3492 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3494 struct scsi_play_msf *scsi_cmd;
3495 struct ccb_scsiio *csio;
3501 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3506 /* retries */ cd_retry_count,
3507 /* cbfcnp */ cddone,
3508 /* flags */ CAM_DIR_NONE,
3509 /* tag_action */ MSG_SIMPLE_Q_TAG,
3510 /* data_ptr */ NULL,
3512 /* sense_len */ SSD_FULL_SIZE,
3513 sizeof(struct scsi_play_msf),
3514 /* timeout */ 50000);
3516 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3517 bzero (scsi_cmd, sizeof(*scsi_cmd));
3519 scsi_cmd->op_code = PLAY_MSF;
3520 scsi_cmd->start_m = startm;
3521 scsi_cmd->start_s = starts;
3522 scsi_cmd->start_f = startf;
3523 scsi_cmd->end_m = endm;
3524 scsi_cmd->end_s = ends;
3525 scsi_cmd->end_f = endf;
3527 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3528 /*sense_flags*/SF_RETRY_UA);
3530 xpt_release_ccb(ccb);
3537 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3538 u_int32_t etrack, u_int32_t eindex)
3540 struct scsi_play_track *scsi_cmd;
3541 struct ccb_scsiio *csio;
3547 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3552 /* retries */ cd_retry_count,
3553 /* cbfcnp */ cddone,
3554 /* flags */ CAM_DIR_NONE,
3555 /* tag_action */ MSG_SIMPLE_Q_TAG,
3556 /* data_ptr */ NULL,
3558 /* sense_len */ SSD_FULL_SIZE,
3559 sizeof(struct scsi_play_track),
3560 /* timeout */ 50000);
3562 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3563 bzero (scsi_cmd, sizeof(*scsi_cmd));
3565 scsi_cmd->op_code = PLAY_TRACK;
3566 scsi_cmd->start_track = strack;
3567 scsi_cmd->start_index = sindex;
3568 scsi_cmd->end_track = etrack;
3569 scsi_cmd->end_index = eindex;
3571 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3572 /*sense_flags*/SF_RETRY_UA);
3574 xpt_release_ccb(ccb);
3580 cdpause(struct cam_periph *periph, u_int32_t go)
3582 struct scsi_pause *scsi_cmd;
3583 struct ccb_scsiio *csio;
3589 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3594 /* retries */ cd_retry_count,
3595 /* cbfcnp */ cddone,
3596 /* flags */ CAM_DIR_NONE,
3597 /* tag_action */ MSG_SIMPLE_Q_TAG,
3598 /* data_ptr */ NULL,
3600 /* sense_len */ SSD_FULL_SIZE,
3601 sizeof(struct scsi_pause),
3602 /* timeout */ 50000);
3604 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3605 bzero (scsi_cmd, sizeof(*scsi_cmd));
3607 scsi_cmd->op_code = PAUSE;
3608 scsi_cmd->resume = go;
3610 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3611 /*sense_flags*/SF_RETRY_UA);
3613 xpt_release_ccb(ccb);
3619 cdstartunit(struct cam_periph *periph, int load)
3626 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3628 scsi_start_stop(&ccb->csio,
3629 /* retries */ cd_retry_count,
3630 /* cbfcnp */ cddone,
3631 /* tag_action */ MSG_SIMPLE_Q_TAG,
3633 /* load_eject */ load,
3634 /* immediate */ FALSE,
3635 /* sense_len */ SSD_FULL_SIZE,
3636 /* timeout */ 50000);
3638 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3639 /*sense_flags*/SF_RETRY_UA);
3641 xpt_release_ccb(ccb);
3647 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3654 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3656 scsi_start_stop(&ccb->csio,
3657 /* retries */ cd_retry_count,
3658 /* cbfcnp */ cddone,
3659 /* tag_action */ MSG_SIMPLE_Q_TAG,
3661 /* load_eject */ eject,
3662 /* immediate */ FALSE,
3663 /* sense_len */ SSD_FULL_SIZE,
3664 /* timeout */ 50000);
3666 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3667 /*sense_flags*/SF_RETRY_UA);
3669 xpt_release_ccb(ccb);
3675 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3677 struct scsi_set_speed *scsi_cmd;
3678 struct ccb_scsiio *csio;
3683 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3686 /* Preserve old behavior: units in multiples of CDROM speed */
3693 /* retries */ cd_retry_count,
3694 /* cbfcnp */ cddone,
3695 /* flags */ CAM_DIR_NONE,
3696 /* tag_action */ MSG_SIMPLE_Q_TAG,
3697 /* data_ptr */ NULL,
3699 /* sense_len */ SSD_FULL_SIZE,
3700 sizeof(struct scsi_set_speed),
3701 /* timeout */ 50000);
3703 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3704 bzero(scsi_cmd, sizeof(*scsi_cmd));
3706 scsi_cmd->opcode = SET_CD_SPEED;
3707 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3708 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3710 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3711 /*sense_flags*/SF_RETRY_UA);
3713 xpt_release_ccb(ccb);
3719 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3731 switch (authinfo->format) {
3732 case DVD_REPORT_AGID:
3733 length = sizeof(struct scsi_report_key_data_agid);
3735 case DVD_REPORT_CHALLENGE:
3736 length = sizeof(struct scsi_report_key_data_challenge);
3738 case DVD_REPORT_KEY1:
3739 length = sizeof(struct scsi_report_key_data_key1_key2);
3741 case DVD_REPORT_TITLE_KEY:
3742 length = sizeof(struct scsi_report_key_data_title);
3743 /* The lba field is only set for the title key */
3744 lba = authinfo->lba;
3746 case DVD_REPORT_ASF:
3747 length = sizeof(struct scsi_report_key_data_asf);
3749 case DVD_REPORT_RPC:
3750 length = sizeof(struct scsi_report_key_data_rpc);
3752 case DVD_INVALIDATE_AGID:
3760 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3764 cam_periph_lock(periph);
3765 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3767 scsi_report_key(&ccb->csio,
3768 /* retries */ cd_retry_count,
3769 /* cbfcnp */ cddone,
3770 /* tag_action */ MSG_SIMPLE_Q_TAG,
3772 /* agid */ authinfo->agid,
3773 /* key_format */ authinfo->format,
3774 /* data_ptr */ databuf,
3775 /* dxfer_len */ length,
3776 /* sense_len */ SSD_FULL_SIZE,
3777 /* timeout */ 50000);
3779 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3780 /*sense_flags*/SF_RETRY_UA);
3785 if (ccb->csio.resid != 0) {
3786 xpt_print(periph->path, "warning, residual for report key "
3787 "command is %d\n", ccb->csio.resid);
3790 switch(authinfo->format) {
3791 case DVD_REPORT_AGID: {
3792 struct scsi_report_key_data_agid *agid_data;
3794 agid_data = (struct scsi_report_key_data_agid *)databuf;
3796 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3800 case DVD_REPORT_CHALLENGE: {
3801 struct scsi_report_key_data_challenge *chal_data;
3803 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3805 bcopy(chal_data->challenge_key, authinfo->keychal,
3806 min(sizeof(chal_data->challenge_key),
3807 sizeof(authinfo->keychal)));
3810 case DVD_REPORT_KEY1: {
3811 struct scsi_report_key_data_key1_key2 *key1_data;
3813 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3815 bcopy(key1_data->key1, authinfo->keychal,
3816 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3819 case DVD_REPORT_TITLE_KEY: {
3820 struct scsi_report_key_data_title *title_data;
3822 title_data = (struct scsi_report_key_data_title *)databuf;
3824 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3825 RKD_TITLE_CPM_SHIFT;
3826 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3827 RKD_TITLE_CP_SEC_SHIFT;
3828 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3829 RKD_TITLE_CMGS_SHIFT;
3830 bcopy(title_data->title_key, authinfo->keychal,
3831 min(sizeof(title_data->title_key),
3832 sizeof(authinfo->keychal)));
3835 case DVD_REPORT_ASF: {
3836 struct scsi_report_key_data_asf *asf_data;
3838 asf_data = (struct scsi_report_key_data_asf *)databuf;
3840 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3843 case DVD_REPORT_RPC: {
3844 struct scsi_report_key_data_rpc *rpc_data;
3846 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3848 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3850 authinfo->vend_rsts =
3851 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3852 RKD_RPC_VENDOR_RESET_SHIFT;
3853 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3854 authinfo->region = rpc_data->region_mask;
3855 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3858 case DVD_INVALIDATE_AGID:
3861 /* This should be impossible, since we checked above */
3864 break; /* NOTREACHED */
3868 xpt_release_ccb(ccb);
3869 cam_periph_unlock(periph);
3871 if (databuf != NULL)
3872 free(databuf, M_DEVBUF);
3878 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3888 switch(authinfo->format) {
3889 case DVD_SEND_CHALLENGE: {
3890 struct scsi_report_key_data_challenge *challenge_data;
3892 length = sizeof(*challenge_data);
3894 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3896 databuf = (u_int8_t *)challenge_data;
3898 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3899 challenge_data->data_len);
3901 bcopy(authinfo->keychal, challenge_data->challenge_key,
3902 min(sizeof(authinfo->keychal),
3903 sizeof(challenge_data->challenge_key)));
3906 case DVD_SEND_KEY2: {
3907 struct scsi_report_key_data_key1_key2 *key2_data;
3909 length = sizeof(*key2_data);
3911 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3913 databuf = (u_int8_t *)key2_data;
3915 scsi_ulto2b(length - sizeof(key2_data->data_len),
3916 key2_data->data_len);
3918 bcopy(authinfo->keychal, key2_data->key1,
3919 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
3923 case DVD_SEND_RPC: {
3924 struct scsi_send_key_data_rpc *rpc_data;
3926 length = sizeof(*rpc_data);
3928 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3930 databuf = (u_int8_t *)rpc_data;
3932 scsi_ulto2b(length - sizeof(rpc_data->data_len),
3933 rpc_data->data_len);
3935 rpc_data->region_code = authinfo->region;
3942 cam_periph_lock(periph);
3943 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3945 scsi_send_key(&ccb->csio,
3946 /* retries */ cd_retry_count,
3947 /* cbfcnp */ cddone,
3948 /* tag_action */ MSG_SIMPLE_Q_TAG,
3949 /* agid */ authinfo->agid,
3950 /* key_format */ authinfo->format,
3951 /* data_ptr */ databuf,
3952 /* dxfer_len */ length,
3953 /* sense_len */ SSD_FULL_SIZE,
3954 /* timeout */ 50000);
3956 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3957 /*sense_flags*/SF_RETRY_UA);
3959 xpt_release_ccb(ccb);
3960 cam_periph_unlock(periph);
3962 if (databuf != NULL)
3963 free(databuf, M_DEVBUF);
3969 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
3979 /* The address is reserved for many of the formats */
3982 switch(dvdstruct->format) {
3983 case DVD_STRUCT_PHYSICAL:
3984 length = sizeof(struct scsi_read_dvd_struct_data_physical);
3986 case DVD_STRUCT_COPYRIGHT:
3987 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
3989 case DVD_STRUCT_DISCKEY:
3990 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
3992 case DVD_STRUCT_BCA:
3993 length = sizeof(struct scsi_read_dvd_struct_data_bca);
3995 case DVD_STRUCT_MANUFACT:
3996 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
3998 case DVD_STRUCT_CMI:
4000 case DVD_STRUCT_PROTDISCID:
4001 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4003 case DVD_STRUCT_DISCKEYBLOCK:
4004 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4006 case DVD_STRUCT_DDS:
4007 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4009 case DVD_STRUCT_MEDIUM_STAT:
4010 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4012 case DVD_STRUCT_SPARE_AREA:
4013 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4015 case DVD_STRUCT_RMD_LAST:
4017 case DVD_STRUCT_RMD_RMA:
4019 case DVD_STRUCT_PRERECORDED:
4020 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4022 case DVD_STRUCT_UNIQUEID:
4023 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4025 case DVD_STRUCT_DCB:
4027 case DVD_STRUCT_LIST:
4029 * This is the maximum allocation length for the READ DVD
4030 * STRUCTURE command. There's nothing in the MMC3 spec
4031 * that indicates a limit in the amount of data that can
4032 * be returned from this call, other than the limits
4033 * imposed by the 2-byte length variables.
4042 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4046 cam_periph_lock(periph);
4047 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4049 scsi_read_dvd_structure(&ccb->csio,
4050 /* retries */ cd_retry_count,
4051 /* cbfcnp */ cddone,
4052 /* tag_action */ MSG_SIMPLE_Q_TAG,
4054 /* layer_number */ dvdstruct->layer_num,
4055 /* key_format */ dvdstruct->format,
4056 /* agid */ dvdstruct->agid,
4057 /* data_ptr */ databuf,
4058 /* dxfer_len */ length,
4059 /* sense_len */ SSD_FULL_SIZE,
4060 /* timeout */ 50000);
4062 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4063 /*sense_flags*/SF_RETRY_UA);
4068 switch(dvdstruct->format) {
4069 case DVD_STRUCT_PHYSICAL: {
4070 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4071 struct dvd_layer *outlayer;
4072 struct scsi_read_dvd_struct_data_physical *phys_data;
4075 (struct scsi_read_dvd_struct_data_physical *)databuf;
4076 inlayer = &phys_data->layer_desc;
4077 outlayer = (struct dvd_layer *)&dvdstruct->data;
4079 dvdstruct->length = sizeof(*inlayer);
4081 outlayer->book_type = (inlayer->book_type_version &
4082 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4083 outlayer->book_version = (inlayer->book_type_version &
4084 RDSD_BOOK_VERSION_MASK);
4085 outlayer->disc_size = (inlayer->disc_size_max_rate &
4086 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4087 outlayer->max_rate = (inlayer->disc_size_max_rate &
4088 RDSD_MAX_RATE_MASK);
4089 outlayer->nlayers = (inlayer->layer_info &
4090 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4091 outlayer->track_path = (inlayer->layer_info &
4092 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4093 outlayer->layer_type = (inlayer->layer_info &
4094 RDSD_LAYER_TYPE_MASK);
4095 outlayer->linear_density = (inlayer->density &
4096 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4097 outlayer->track_density = (inlayer->density &
4098 RDSD_TRACK_DENSITY_MASK);
4099 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4101 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4102 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4103 outlayer->end_sector_l0 =
4104 scsi_3btoul(inlayer->end_sector_layer0);
4107 case DVD_STRUCT_COPYRIGHT: {
4108 struct scsi_read_dvd_struct_data_copyright *copy_data;
4110 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4113 dvdstruct->cpst = copy_data->cps_type;
4114 dvdstruct->rmi = copy_data->region_info;
4115 dvdstruct->length = 0;
4121 * Tell the user what the overall length is, no matter
4122 * what we can actually fit in the data buffer.
4124 dvdstruct->length = length - ccb->csio.resid -
4125 sizeof(struct scsi_read_dvd_struct_data_header);
4128 * But only actually copy out the smaller of what we read
4129 * in or what the structure can take.
4131 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4133 min(sizeof(dvdstruct->data), dvdstruct->length));
4138 xpt_release_ccb(ccb);
4139 cam_periph_unlock(periph);
4141 if (databuf != NULL)
4142 free(databuf, M_DEVBUF);
4148 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4149 void (*cbfcnp)(struct cam_periph *, union ccb *),
4150 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4151 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4152 u_int8_t sense_len, u_int32_t timeout)
4154 struct scsi_report_key *scsi_cmd;
4156 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4157 bzero(scsi_cmd, sizeof(*scsi_cmd));
4158 scsi_cmd->opcode = REPORT_KEY;
4159 scsi_ulto4b(lba, scsi_cmd->lba);
4160 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4161 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4162 (key_format & RK_KF_KEYFORMAT_MASK);
4167 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4169 /*data_ptr*/ data_ptr,
4170 /*dxfer_len*/ dxfer_len,
4177 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4178 void (*cbfcnp)(struct cam_periph *, union ccb *),
4179 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4180 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4183 struct scsi_send_key *scsi_cmd;
4185 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4186 bzero(scsi_cmd, sizeof(*scsi_cmd));
4187 scsi_cmd->opcode = SEND_KEY;
4189 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4190 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4191 (key_format & RK_KF_KEYFORMAT_MASK);
4196 /*flags*/ CAM_DIR_OUT,
4198 /*data_ptr*/ data_ptr,
4199 /*dxfer_len*/ dxfer_len,
4207 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4208 void (*cbfcnp)(struct cam_periph *, union ccb *),
4209 u_int8_t tag_action, u_int32_t address,
4210 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4211 u_int8_t *data_ptr, u_int32_t dxfer_len,
4212 u_int8_t sense_len, u_int32_t timeout)
4214 struct scsi_read_dvd_structure *scsi_cmd;
4216 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4217 bzero(scsi_cmd, sizeof(*scsi_cmd));
4218 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4220 scsi_ulto4b(address, scsi_cmd->address);
4221 scsi_cmd->layer_number = layer_number;
4222 scsi_cmd->format = format;
4223 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4224 /* The AGID is the top two bits of this byte */
4225 scsi_cmd->agid = agid << 6;
4230 /*flags*/ CAM_DIR_IN,
4232 /*data_ptr*/ data_ptr,
4233 /*dxfer_len*/ dxfer_len,