2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 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
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_cd.c,v 1.19 2005/09/21 18:58:55 hsu Exp $
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
52 #include <sys/param.h>
53 #include <sys/bootmaj.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>
68 #include <sys/thread2.h>
71 #include "../cam_ccb.h"
72 #include "../cam_extend.h"
73 #include "../cam_periph.h"
74 #include "../cam_xpt_periph.h"
75 #include "../cam_queue.h"
77 #include "scsi_message.h"
81 #define LEADOUT 0xaa /* leadout toc entry */
91 CD_Q_BCD_TRACKS = 0x02,
92 CD_Q_NO_CHANGER = 0x04,
94 CD_Q_10_BYTE_ONLY = 0x10
98 CD_FLAG_INVALID = 0x0001,
99 CD_FLAG_NEW_DISC = 0x0002,
100 CD_FLAG_DISC_LOCKED = 0x0004,
101 CD_FLAG_DISC_REMOVABLE = 0x0008,
102 CD_FLAG_TAGGED_QUEUING = 0x0010,
103 CD_FLAG_CHANGER = 0x0040,
104 CD_FLAG_ACTIVE = 0x0080,
105 CD_FLAG_SCHED_ON_COMP = 0x0100,
106 CD_FLAG_RETRY_UA = 0x0200,
107 CD_FLAG_VALID_MEDIA = 0x0400,
108 CD_FLAG_VALID_TOC = 0x0800,
109 CD_FLAG_OPEN = 0x1000,
110 CD_FLAG_SCTX_INIT = 0x2000
115 CD_CCB_BUFFER_IO = 0x02,
116 CD_CCB_WAITING = 0x03,
117 CD_CCB_TYPE_MASK = 0x0F,
118 CD_CCB_RETRY_UA = 0x10
122 CHANGER_TIMEOUT_SCHED = 0x01,
123 CHANGER_SHORT_TMOUT_SCHED = 0x02,
124 CHANGER_MANUAL_CALL = 0x04,
125 CHANGER_NEED_TIMEOUT = 0x08
128 #define ccb_state ppriv_field0
129 #define ccb_bp ppriv_ptr1
132 struct ioc_toc_header header;
133 struct cd_toc_entry entries[100];
136 struct cd_toc_single {
137 struct ioc_toc_header header;
138 struct cd_toc_entry entry;
149 volatile cd_flags flags;
150 struct buf_queue_head buf_queue;
151 LIST_HEAD(, ccb_hdr) pending_ccbs;
152 struct cd_params params;
156 struct devstat device_stats;
157 STAILQ_ENTRY(cd_softc) changer_links;
158 struct cdchanger *changer;
160 struct cam_periph *periph;
161 int minimum_command_size;
162 struct task sysctl_task;
163 struct sysctl_ctx_list sysctl_ctx;
164 struct sysctl_oid *sysctl_tree;
165 STAILQ_HEAD(, cd_mode_params) mode_queue;
166 struct cd_tocdata toc;
169 struct cd_page_sizes {
174 static struct cd_page_sizes cd_page_size_table[] =
176 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
179 struct cd_quirk_entry {
180 struct scsi_inquiry_pattern inq_pat;
185 * The changer quirk entries aren't strictly necessary. Basically, what
186 * they do is tell cdregister() up front that a device is a changer.
187 * Otherwise, it will figure that fact out once it sees a LUN on the device
188 * that is greater than 0. If it is known up front that a device is a changer,
189 * all I/O to the device will go through the changer scheduling routines, as
190 * opposed to the "normal" CD code.
192 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
193 * your device hangs when it gets a 10 byte command. Adding a quirk just
194 * to get rid of the informative diagnostic message is not acceptable. All
195 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
196 * referenced in a comment along with the quirk) , and must be approved by
197 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
198 * be removed until the submitter can explain why they are needed.
199 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
200 * when the CAM_NEW_TRAN_CODE work is done.
202 static struct cd_quirk_entry cd_quirk_table[] =
205 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
206 /*quirks*/ CD_Q_CHANGER
209 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
210 "*"}, /* quirks */ CD_Q_CHANGER
213 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
214 /* quirks */ CD_Q_CHANGER
217 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
218 /* quirks */ CD_Q_BCD_TRACKS
222 static d_open_t cdopen;
223 static d_close_t cdclose;
224 static d_ioctl_t cdioctl;
225 static d_strategy_t cdstrategy;
227 static periph_init_t cdinit;
228 static periph_ctor_t cdregister;
229 static periph_dtor_t cdcleanup;
230 static periph_start_t cdstart;
231 static periph_oninv_t cdoninvalidate;
232 static void cdasync(void *callback_arg, u_int32_t code,
233 struct cam_path *path, void *arg);
234 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
235 static void cdshorttimeout(void *arg);
236 static void cdschedule(struct cam_periph *periph, int priority);
237 static void cdrunchangerqueue(void *arg);
238 static void cdchangerschedule(struct cd_softc *softc);
239 static int cdrunccb(union ccb *ccb,
240 int (*error_routine)(union ccb *ccb,
242 u_int32_t sense_flags),
243 u_int32_t cam_flags, u_int32_t sense_flags);
244 static union ccb *cdgetccb(struct cam_periph *periph,
246 static void cddone(struct cam_periph *periph,
247 union ccb *start_ccb);
248 static int cderror(union ccb *ccb, u_int32_t cam_flags,
249 u_int32_t sense_flags);
250 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
251 static int cdgetpagesize(int page_num);
252 static void cdprevent(struct cam_periph *periph, int action);
253 static int cdcheckmedia(struct cam_periph *periph);
254 static int cdsize(struct cam_periph *periph, u_int32_t *size);
255 static int cd6byteworkaround(union ccb *ccb);
256 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
257 u_int32_t start, u_int8_t *data,
258 u_int32_t len, u_int32_t sense_flags);
259 static int cdgetmode(struct cam_periph *periph,
260 struct cd_mode_params *data, u_int32_t page);
261 static int cdsetmode(struct cam_periph *periph,
262 struct cd_mode_params *data);
263 static int cdplay(struct cam_periph *periph, u_int32_t blk,
265 static int cdreadsubchannel(struct cam_periph *periph,
266 u_int32_t mode, u_int32_t format,
268 struct cd_sub_channel_info *data,
270 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
271 u_int32_t starts, u_int32_t startf,
272 u_int32_t endm, u_int32_t ends,
274 static int cdplaytracks(struct cam_periph *periph,
275 u_int32_t strack, u_int32_t sindex,
276 u_int32_t etrack, u_int32_t eindex);
277 static int cdpause(struct cam_periph *periph, u_int32_t go);
278 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
279 static int cdstartunit(struct cam_periph *periph, int load);
280 static int cdsetspeed(struct cam_periph *periph,
281 u_int32_t rdspeed, u_int32_t wrspeed);
282 static int cdreportkey(struct cam_periph *periph,
283 struct dvd_authinfo *authinfo);
284 static int cdsendkey(struct cam_periph *periph,
285 struct dvd_authinfo *authinfo);
286 static int cdreaddvdstructure(struct cam_periph *periph,
287 struct dvd_struct *dvdstruct);
289 static struct periph_driver cddriver =
292 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
295 DATA_SET(periphdriver_set, cddriver);
297 /* For 2.2-stable support */
301 static struct cdevsw cd_cdevsw = {
303 /* maj */ SCSICD_CDEV_MAJOR,
311 /* write */ physwrite,
315 /* strategy */ cdstrategy,
320 static struct extend_array *cdperiphs;
321 static int num_changers;
323 #ifndef CHANGER_MIN_BUSY_SECONDS
324 #define CHANGER_MIN_BUSY_SECONDS 5
326 #ifndef CHANGER_MAX_BUSY_SECONDS
327 #define CHANGER_MAX_BUSY_SECONDS 15
330 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
331 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
334 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl
337 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
338 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
339 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
340 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
341 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
342 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
343 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
344 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
348 target_id_t target_id;
351 struct timeval start_time;
352 struct cd_softc *cur_device;
353 struct callout short_handle;
354 struct callout long_handle;
355 volatile cd_changer_flags flags;
356 STAILQ_ENTRY(cdchanger) changer_links;
357 STAILQ_HEAD(chdevlist, cd_softc) chluns;
360 static STAILQ_HEAD(changerlist, cdchanger) changerq;
366 struct cam_path *path;
369 * Create our extend array for storing the devices we attach to.
371 cdperiphs = cam_extend_new();
372 if (cdperiphs == NULL) {
373 printf("cd: Failed to alloc extend array!\n");
378 * Install a global async callback. This callback will
379 * receive async callbacks like "new device found".
381 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
382 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
384 if (status == CAM_REQ_CMP) {
385 struct ccb_setasync csa;
387 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
388 csa.ccb_h.func_code = XPT_SASYNC_CB;
389 csa.event_enable = AC_FOUND_DEVICE;
390 csa.callback = cdasync;
391 csa.callback_arg = NULL;
392 xpt_action((union ccb *)&csa);
393 status = csa.ccb_h.status;
397 if (status != CAM_REQ_CMP) {
398 printf("cd: Failed to attach master async callback "
399 "due to status 0x%x!\n", status);
404 cdoninvalidate(struct cam_periph *periph)
406 struct cd_softc *softc;
408 struct ccb_setasync csa;
410 softc = (struct cd_softc *)periph->softc;
413 * De-register any async callbacks.
415 xpt_setup_ccb(&csa.ccb_h, periph->path,
417 csa.ccb_h.func_code = XPT_SASYNC_CB;
418 csa.event_enable = 0;
419 csa.callback = cdasync;
420 csa.callback_arg = periph;
421 xpt_action((union ccb *)&csa);
423 softc->flags |= CD_FLAG_INVALID;
426 * Although the oninvalidate() routines are always while in a
427 * critical section, we need to be in a critical section here to
428 * keep the buffer queue from being modified while we traverse it.
433 * Return all queued I/O with ENXIO.
434 * XXX Handle any transactions queued to the card
435 * with XPT_ABORT_CCB.
437 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
438 bufq_remove(&softc->buf_queue, q_bp);
439 q_bp->b_resid = q_bp->b_bcount;
440 q_bp->b_error = ENXIO;
441 q_bp->b_flags |= B_ERROR;
447 * If this device is part of a changer, and it was scheduled
448 * to run, remove it from the run queue since we just nuked
449 * all of its scheduled I/O.
451 if ((softc->flags & CD_FLAG_CHANGER)
452 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
453 camq_remove(&softc->changer->devq, softc->pinfo.index);
455 xpt_print_path(periph->path);
456 printf("lost device\n");
460 cdcleanup(struct cam_periph *periph)
462 struct cd_softc *softc;
464 softc = (struct cd_softc *)periph->softc;
466 xpt_print_path(periph->path);
467 printf("removing device entry\n");
469 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
470 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
471 xpt_print_path(periph->path);
472 printf("can't remove sysctl context\n");
477 * In the queued, non-active case, the device in question
478 * has already been removed from the changer run queue. Since this
479 * device is active, we need to de-activate it, and schedule
480 * another device to run. (if there is another one to run)
482 if ((softc->flags & CD_FLAG_CHANGER)
483 && (softc->flags & CD_FLAG_ACTIVE)) {
486 * The purpose of the short timeout is soley to determine
487 * whether the current device has finished or not. Well,
488 * since we're removing the active device, we know that it
489 * is finished. So, get rid of the short timeout.
490 * Otherwise, if we're in the time period before the short
491 * timeout fires, and there are no other devices in the
492 * queue to run, there won't be any other device put in the
493 * active slot. i.e., when we call cdrunchangerqueue()
494 * below, it won't do anything. Then, when the short
495 * timeout fires, it'll look at the "current device", which
496 * we are free below, and possibly panic the kernel on a
497 * bogus pointer reference.
499 * The long timeout doesn't really matter, since we
500 * decrement the qfrozen_cnt to indicate that there is
501 * nothing in the active slot now. Therefore, there won't
502 * be any bogus pointer references there.
504 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
505 callout_stop(&softc->changer->short_handle);
506 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
508 softc->changer->devq.qfrozen_cnt--;
509 softc->changer->flags |= CHANGER_MANUAL_CALL;
510 cdrunchangerqueue(softc->changer);
514 * If we're removing the last device on the changer, go ahead and
515 * remove the changer device structure.
517 if ((softc->flags & CD_FLAG_CHANGER)
518 && (--softc->changer->num_devices == 0)) {
521 * Theoretically, there shouldn't be any timeouts left, but
522 * I'm not completely sure that that will be the case. So,
523 * it won't hurt to check and see if there are any left.
525 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
526 callout_stop(&softc->changer->long_handle);
527 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
530 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
531 callout_stop(&softc->changer->short_handle);
532 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
535 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
537 xpt_print_path(periph->path);
538 printf("removing changer entry\n");
539 free(softc->changer, M_DEVBUF);
542 devstat_remove_entry(&softc->device_stats);
543 cam_extend_release(cdperiphs, periph->unit_number);
544 if (softc->disk.d_rawdev) {
545 disk_destroy(&softc->disk);
547 free(softc, M_DEVBUF);
552 cdasync(void *callback_arg, u_int32_t code,
553 struct cam_path *path, void *arg)
555 struct cam_periph *periph;
557 periph = (struct cam_periph *)callback_arg;
559 case AC_FOUND_DEVICE:
561 struct ccb_getdev *cgd;
564 cgd = (struct ccb_getdev *)arg;
566 if (SID_TYPE(&cgd->inq_data) != T_CDROM
567 && SID_TYPE(&cgd->inq_data) != T_WORM)
571 * Allocate a peripheral instance for
572 * this device and start the probe
575 status = cam_periph_alloc(cdregister, cdoninvalidate,
577 "cd", CAM_PERIPH_BIO,
578 cgd->ccb_h.path, cdasync,
579 AC_FOUND_DEVICE, cgd);
581 if (status != CAM_REQ_CMP
582 && status != CAM_REQ_INPROG)
583 printf("cdasync: Unable to attach new device "
584 "due to status 0x%x\n", status);
591 struct cd_softc *softc;
592 struct ccb_hdr *ccbh;
594 softc = (struct cd_softc *)periph->softc;
597 * Don't fail on the expected unit attention
600 softc->flags |= CD_FLAG_RETRY_UA;
601 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
602 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
603 ccbh->ccb_state |= CD_CCB_RETRY_UA;
608 cam_periph_async(periph, code, path, arg);
614 cdsysctlinit(void *context, int pending)
616 struct cam_periph *periph;
617 struct cd_softc *softc;
618 char tmpstr[80], tmpstr2[80];
620 periph = (struct cam_periph *)context;
621 softc = (struct cd_softc *)periph->softc;
623 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
624 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
626 sysctl_ctx_init(&softc->sysctl_ctx);
627 softc->flags |= CD_FLAG_SCTX_INIT;
628 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
629 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
630 tmpstr2, CTLFLAG_RD, 0, tmpstr);
632 if (softc->sysctl_tree == NULL) {
633 printf("cdsysctlinit: unable to allocate sysctl tree\n");
638 * Now register the sysctl handler, so the user can the value on
641 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
642 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
643 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
648 * We have a handler function for this so we can check the values when the
649 * user sets them, instead of every time we look at them.
652 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
656 value = *(int *)arg1;
658 error = sysctl_handle_int(oidp, &value, 0, req);
660 if ((error != 0) || (req->newptr == NULL))
664 * The only real values we can have here are 6 or 10. I don't
665 * really forsee having 12 be an option at any time in the future.
666 * So if the user sets something less than or equal to 6, we'll set
667 * it to 6. If he sets something greater than 6, we'll set it to 10.
669 * I suppose we could just return an error here for the wrong values,
670 * but I don't think it's necessary to do so, as long as we can
671 * determine the user's intent without too much trouble.
678 *(int *)arg1 = value;
685 cdregister(struct cam_periph *periph, void *arg)
687 struct cd_softc *softc;
688 struct ccb_setasync csa;
689 struct ccb_pathinq cpi;
690 struct ccb_getdev *cgd;
694 cgd = (struct ccb_getdev *)arg;
695 if (periph == NULL) {
696 printf("cdregister: periph was NULL!!\n");
697 return(CAM_REQ_CMP_ERR);
700 printf("cdregister: no getdev CCB, can't register device\n");
701 return(CAM_REQ_CMP_ERR);
704 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
705 LIST_INIT(&softc->pending_ccbs);
706 STAILQ_INIT(&softc->mode_queue);
707 softc->state = CD_STATE_PROBE;
708 bufq_init(&softc->buf_queue);
709 if (SID_IS_REMOVABLE(&cgd->inq_data))
710 softc->flags |= CD_FLAG_DISC_REMOVABLE;
711 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
712 softc->flags |= CD_FLAG_TAGGED_QUEUING;
714 periph->softc = softc;
715 softc->periph = periph;
717 cam_extend_set(cdperiphs, periph->unit_number, periph);
720 * See if this device has any quirks.
722 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
723 (caddr_t)cd_quirk_table,
724 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
725 sizeof(*cd_quirk_table), scsi_inquiry_match);
728 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
730 softc->quirks = CD_Q_NONE;
732 /* Check if the SIM does not want 6 byte commands */
733 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
734 cpi.ccb_h.func_code = XPT_PATH_INQ;
735 xpt_action((union ccb *)&cpi);
736 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
737 softc->quirks |= CD_Q_10_BYTE_ONLY;
739 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
741 /* The default is 6 byte commands, unless quirked otherwise */
742 if (softc->quirks & CD_Q_10_BYTE_ONLY)
743 softc->minimum_command_size = 10;
745 softc->minimum_command_size = 6;
748 * Load the user's default, if any.
750 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
751 periph->unit_number);
752 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
754 /* 6 and 10 are the only permissible values here. */
755 if (softc->minimum_command_size < 6)
756 softc->minimum_command_size = 6;
757 else if (softc->minimum_command_size > 6)
758 softc->minimum_command_size = 10;
761 * We need to register the statistics structure for this device,
762 * but we don't have the blocksize yet for it. So, we register
763 * the structure and indicate that we don't have the blocksize
764 * yet. Unlike other SCSI peripheral drivers, we explicitly set
765 * the device type here to be CDROM, rather than just ORing in
766 * the device type. This is because this driver can attach to either
767 * CDROM or WORM devices, and we want this peripheral driver to
768 * show up in the devstat list as a CD peripheral driver, not a
769 * WORM peripheral driver. WORM drives will also have the WORM
770 * driver attached to them.
772 devstat_add_entry(&softc->device_stats, "cd",
773 periph->unit_number, 0,
774 DEVSTAT_BS_UNAVAILABLE,
775 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
776 DEVSTAT_PRIORITY_CD);
777 disk_create(periph->unit_number, &softc->disk,
778 DSO_ONESLICE | DSO_COMPATLABEL, &cd_cdevsw);
781 * Add an async callback so that we get
782 * notified if this device goes away.
784 xpt_setup_ccb(&csa.ccb_h, periph->path,
786 csa.ccb_h.func_code = XPT_SASYNC_CB;
787 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
788 csa.callback = cdasync;
789 csa.callback_arg = periph;
790 xpt_action((union ccb *)&csa);
793 * If the target lun is greater than 0, we most likely have a CD
794 * changer device. Check the quirk entries as well, though, just
795 * in case someone has a CD tower with one lun per drive or
796 * something like that. Also, if we know up front that a
797 * particular device is a changer, we can mark it as such starting
798 * with lun 0, instead of lun 1. It shouldn't be necessary to have
799 * a quirk entry to define something as a changer, however.
801 if (((cgd->ccb_h.target_lun > 0)
802 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
803 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
804 struct cdchanger *nchanger;
805 struct cam_periph *nperiph;
806 struct cam_path *path;
810 /* Set the changer flag in the current device's softc */
811 softc->flags |= CD_FLAG_CHANGER;
813 if (num_changers == 0)
814 STAILQ_INIT(&changerq);
817 * Now, look around for an existing changer device with the
818 * same path and target ID as the current device.
821 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
823 nchanger = STAILQ_NEXT(nchanger, changer_links)){
824 if ((nchanger->path_id == cgd->ccb_h.path_id)
825 && (nchanger->target_id == cgd->ccb_h.target_id)) {
832 * If we found a matching entry, just add this device to
833 * the list of devices on this changer.
836 struct chdevlist *chlunhead;
838 chlunhead = &nchanger->chluns;
841 * XXX KDM look at consolidating this code with the
842 * code below in a separate function.
846 * Create a path with lun id 0, and see if we can
847 * find a matching device
849 status = xpt_create_path(&path, /*periph*/ periph,
851 cgd->ccb_h.target_id, 0);
853 if ((status == CAM_REQ_CMP)
854 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
855 struct cd_softc *nsoftc;
857 nsoftc = (struct cd_softc *)nperiph->softc;
859 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
860 nsoftc->flags |= CD_FLAG_CHANGER;
861 nchanger->num_devices++;
862 if (camq_resize(&nchanger->devq,
863 nchanger->num_devices)!=CAM_REQ_CMP){
864 printf("cdregister: "
870 nsoftc->changer = nchanger;
871 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
873 STAILQ_INSERT_TAIL(&nchanger->chluns,
874 nsoftc,changer_links);
877 } else if (status == CAM_REQ_CMP)
880 printf("cdregister: unable to allocate path\n"
881 "cdregister: changer support may be "
885 nchanger->num_devices++;
887 softc->changer = nchanger;
888 softc->pinfo.index = CAM_UNQUEUED_INDEX;
890 if (camq_resize(&nchanger->devq,
891 nchanger->num_devices) != CAM_REQ_CMP) {
892 printf("cdregister: camq_resize "
893 "failed, changer support may "
897 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
900 * In this case, we don't already have an entry for this
901 * particular changer, so we need to create one, add it to
902 * the queue, and queue this device on the list for this
903 * changer. Before we queue this device, however, we need
904 * to search for lun id 0 on this target, and add it to the
905 * queue first, if it exists. (and if it hasn't already
906 * been marked as part of the changer.)
909 nchanger = malloc(sizeof(struct cdchanger),
910 M_DEVBUF, M_INTWAIT | M_ZERO);
911 callout_init(&nchanger->short_handle);
912 callout_init(&nchanger->long_handle);
913 if (camq_init(&nchanger->devq, 1) != 0) {
914 softc->flags &= ~CD_FLAG_CHANGER;
915 printf("cdregister: changer support "
922 nchanger->path_id = cgd->ccb_h.path_id;
923 nchanger->target_id = cgd->ccb_h.target_id;
925 /* this is superfluous, but it makes things clearer */
926 nchanger->num_devices = 0;
928 STAILQ_INIT(&nchanger->chluns);
930 STAILQ_INSERT_TAIL(&changerq, nchanger,
934 * Create a path with lun id 0, and see if we can
935 * find a matching device
937 status = xpt_create_path(&path, /*periph*/ periph,
939 cgd->ccb_h.target_id, 0);
942 * If we were able to allocate the path, and if we
943 * find a matching device and it isn't already
944 * marked as part of a changer, then we add it to
945 * the current changer.
947 if ((status == CAM_REQ_CMP)
948 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
949 && ((((struct cd_softc *)periph->softc)->flags &
950 CD_FLAG_CHANGER) == 0)) {
951 struct cd_softc *nsoftc;
953 nsoftc = (struct cd_softc *)nperiph->softc;
955 nsoftc->flags |= CD_FLAG_CHANGER;
956 nchanger->num_devices++;
957 if (camq_resize(&nchanger->devq,
958 nchanger->num_devices) != CAM_REQ_CMP) {
959 printf("cdregister: camq_resize "
960 "failed, changer support may "
963 nsoftc->changer = nchanger;
964 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
966 STAILQ_INSERT_TAIL(&nchanger->chluns,
967 nsoftc, changer_links);
969 } else if (status == CAM_REQ_CMP)
972 printf("cdregister: unable to allocate path\n"
973 "cdregister: changer support may be "
977 softc->changer = nchanger;
978 softc->pinfo.index = CAM_UNQUEUED_INDEX;
979 nchanger->num_devices++;
980 if (camq_resize(&nchanger->devq,
981 nchanger->num_devices) != CAM_REQ_CMP) {
982 printf("cdregister: camq_resize "
983 "failed, changer support may "
986 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
993 /* Lock this peripheral until we are setup */
995 cam_periph_lock(periph, 0);
997 if ((softc->flags & CD_FLAG_CHANGER) == 0)
998 xpt_schedule(periph, /*priority*/5);
1000 cdschedule(periph, /*priority*/ 5);
1002 return(CAM_REQ_CMP);
1006 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
1008 struct cam_periph *periph;
1009 struct cd_softc *softc;
1013 periph = cam_extend_get(cdperiphs, unit);
1018 softc = (struct cd_softc *)periph->softc;
1021 * Grab a critical section and hold it until we lock the peripheral.
1024 if (softc->flags & CD_FLAG_INVALID) {
1029 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
1036 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1040 * Check for media, and set the appropriate flags. We don't bail
1041 * if we don't have media, but then we don't allow anything but the
1042 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1044 * XXX KDM for now, we do fail the open if we don't have media. We
1045 * can change this once we've figured out how to make the slice
1046 * code work well with media changing underneath it.
1048 error = cdcheckmedia(periph);
1051 softc->flags |= CD_FLAG_OPEN;
1053 cam_periph_unlock(periph);
1055 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1061 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
1063 struct cam_periph *periph;
1064 struct cd_softc *softc;
1068 periph = cam_extend_get(cdperiphs, unit);
1072 softc = (struct cd_softc *)periph->softc;
1074 if ((error = cam_periph_lock(periph, 0)) != 0)
1077 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1078 cdprevent(periph, PR_ALLOW);
1081 * Unconditionally set the dsopen() flags back to their default
1084 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1085 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1088 * Since we're closing this CD, mark the blocksize as unavailable.
1089 * It will be marked as available when the CD is opened again.
1091 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1094 * We'll check the media and toc again at the next open().
1096 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1098 softc->flags &= ~CD_FLAG_OPEN;
1100 cam_periph_unlock(periph);
1101 cam_periph_release(periph);
1107 cdshorttimeout(void *arg)
1109 struct cdchanger *changer;
1113 changer = (struct cdchanger *)arg;
1115 /* Always clear the short timeout flag, since that's what we're in */
1116 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1119 * Check to see if there is any more pending or outstanding I/O for
1120 * this device. If not, move it out of the active slot.
1122 if ((bufq_first(&changer->cur_device->buf_queue) == NULL)
1123 && (changer->cur_device->device_stats.busy_count == 0)) {
1124 changer->flags |= CHANGER_MANUAL_CALL;
1125 cdrunchangerqueue(changer);
1131 * This is a wrapper for xpt_schedule. It only applies to changers.
1134 cdschedule(struct cam_periph *periph, int priority)
1136 struct cd_softc *softc;
1140 softc = (struct cd_softc *)periph->softc;
1143 * If this device isn't currently queued, and if it isn't
1144 * the active device, then we queue this device and run the
1145 * changer queue if there is no timeout scheduled to do it.
1146 * If this device is the active device, just schedule it
1147 * to run again. If this device is queued, there should be
1148 * a timeout in place already that will make sure it runs.
1150 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1151 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1153 * We don't do anything with the priority here.
1154 * This is strictly a fifo queue.
1156 softc->pinfo.priority = 1;
1157 softc->pinfo.generation = ++softc->changer->devq.generation;
1158 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1161 * Since we just put a device in the changer queue,
1162 * check and see if there is a timeout scheduled for
1163 * this changer. If so, let the timeout handle
1164 * switching this device into the active slot. If
1165 * not, manually call the timeout routine to
1168 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1169 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1170 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1171 softc->changer->flags |= CHANGER_MANUAL_CALL;
1172 cdrunchangerqueue(softc->changer);
1174 } else if ((softc->flags & CD_FLAG_ACTIVE)
1175 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1176 xpt_schedule(periph, priority);
1182 cdrunchangerqueue(void *arg)
1184 struct cd_softc *softc;
1185 struct cdchanger *changer;
1186 int called_from_timeout;
1190 changer = (struct cdchanger *)arg;
1193 * If we have NOT been called from cdstrategy() or cddone(), and
1194 * instead from a timeout routine, go ahead and clear the
1197 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1198 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1199 called_from_timeout = 1;
1201 called_from_timeout = 0;
1203 /* Always clear the manual call flag */
1204 changer->flags &= ~CHANGER_MANUAL_CALL;
1206 /* nothing to do if the queue is empty */
1207 if (changer->devq.entries <= 0) {
1213 * If the changer queue is frozen, that means we have an active
1216 if (changer->devq.qfrozen_cnt > 0) {
1218 if (changer->cur_device->device_stats.busy_count > 0) {
1219 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1220 changer->cur_device->bufs_left =
1221 changer->cur_device->device_stats.busy_count;
1222 if (called_from_timeout) {
1223 callout_reset(&changer->long_handle,
1224 changer_max_busy_seconds * hz,
1225 cdrunchangerqueue, changer);
1226 changer->flags |= CHANGER_TIMEOUT_SCHED;
1233 * We always need to reset the frozen count and clear the
1236 changer->devq.qfrozen_cnt--;
1237 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1238 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1241 * Check to see whether the current device has any I/O left
1242 * to do. If so, requeue it at the end of the queue. If
1243 * not, there is no need to requeue it.
1245 if (bufq_first(&changer->cur_device->buf_queue) != NULL) {
1247 changer->cur_device->pinfo.generation =
1248 ++changer->devq.generation;
1249 camq_insert(&changer->devq,
1250 (cam_pinfo *)changer->cur_device);
1254 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1256 changer->cur_device = softc;
1258 changer->devq.qfrozen_cnt++;
1259 softc->flags |= CD_FLAG_ACTIVE;
1261 /* Just in case this device is waiting */
1262 wakeup(&softc->changer);
1263 xpt_schedule(softc->periph, /*priority*/ 1);
1266 * Get rid of any pending timeouts, and set a flag to schedule new
1267 * ones so this device gets its full time quantum.
1269 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1270 callout_stop(&changer->long_handle);
1271 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1274 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1275 callout_stop(&changer->short_handle);
1276 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1280 * We need to schedule timeouts, but we only do this after the
1281 * first transaction has completed. This eliminates the changer
1284 changer->flags |= CHANGER_NEED_TIMEOUT;
1290 cdchangerschedule(struct cd_softc *softc)
1292 struct cdchanger *changer;
1296 changer = softc->changer;
1299 * If this is a changer, and this is the current device,
1300 * and this device has at least the minimum time quantum to
1301 * run, see if we can switch it out.
1303 if ((softc->flags & CD_FLAG_ACTIVE)
1304 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1305 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1307 * We try three things here. The first is that we
1308 * check to see whether the schedule on completion
1309 * flag is set. If it is, we decrement the number
1310 * of buffers left, and if it's zero, we reschedule.
1311 * Next, we check to see whether the pending buffer
1312 * queue is empty and whether there are no
1313 * outstanding transactions. If so, we reschedule.
1314 * Next, we see if the pending buffer queue is empty.
1315 * If it is, we set the number of buffers left to
1316 * the current active buffer count and set the
1317 * schedule on complete flag.
1319 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1320 if (--softc->bufs_left == 0) {
1321 softc->changer->flags |=
1322 CHANGER_MANUAL_CALL;
1323 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1324 cdrunchangerqueue(softc->changer);
1326 } else if ((bufq_first(&softc->buf_queue) == NULL)
1327 && (softc->device_stats.busy_count == 0)) {
1328 softc->changer->flags |= CHANGER_MANUAL_CALL;
1329 cdrunchangerqueue(softc->changer);
1331 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1332 && (softc->flags & CD_FLAG_ACTIVE)) {
1335 * Now that the first transaction to this
1336 * particular device has completed, we can go ahead
1337 * and schedule our timeouts.
1339 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1340 callout_reset(&changer->long_handle,
1341 changer_max_busy_seconds * hz,
1342 cdrunchangerqueue, changer);
1343 changer->flags |= CHANGER_TIMEOUT_SCHED;
1345 printf("cdchangerschedule: already have a long"
1348 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1349 callout_reset(&changer->short_handle,
1350 changer_min_busy_seconds * hz,
1351 cdshorttimeout, changer);
1352 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1354 printf("cdchangerschedule: already have a short "
1358 * We just scheduled timeouts, no need to schedule
1361 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1368 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1369 u_int32_t cam_flags,
1370 u_int32_t sense_flags),
1371 u_int32_t cam_flags, u_int32_t sense_flags)
1373 struct cd_softc *softc;
1374 struct cam_periph *periph;
1377 periph = xpt_path_periph(ccb->ccb_h.path);
1378 softc = (struct cd_softc *)periph->softc;
1380 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1381 &softc->device_stats);
1383 if (softc->flags & CD_FLAG_CHANGER)
1384 cdchangerschedule(softc);
1390 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1392 struct cd_softc *softc;
1394 softc = (struct cd_softc *)periph->softc;
1396 if (softc->flags & CD_FLAG_CHANGER) {
1400 * This should work the first time this device is woken up,
1401 * but just in case it doesn't, we use a while loop.
1403 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1405 * If this changer isn't already queued, queue it up.
1407 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1408 softc->pinfo.priority = 1;
1409 softc->pinfo.generation =
1410 ++softc->changer->devq.generation;
1411 camq_insert(&softc->changer->devq,
1412 (cam_pinfo *)softc);
1414 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1415 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1416 && ((softc->changer->flags
1417 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1418 softc->changer->flags |= CHANGER_MANUAL_CALL;
1419 cdrunchangerqueue(softc->changer);
1421 tsleep(&softc->changer, 0, "cgticb", 0);
1425 return(cam_periph_getccb(periph, priority));
1429 * Actually translate the requested transfer into one the physical driver
1430 * can understand. The transfer is described by a buf and will include
1431 * only one physical transfer.
1434 cdstrategy(struct buf *bp)
1436 struct cam_periph *periph;
1437 struct cd_softc *softc;
1440 unit = dkunit(bp->b_dev);
1441 part = dkpart(bp->b_dev);
1442 periph = cam_extend_get(cdperiphs, unit);
1443 if (periph == NULL) {
1444 bp->b_error = ENXIO;
1448 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1450 softc = (struct cd_softc *)periph->softc;
1453 * Mask interrupts so that the pack cannot be invalidated until
1454 * after we are in the queue. Otherwise, we might not properly
1455 * clean up one of the buffers.
1460 * If the device has been made invalid, error out
1462 if ((softc->flags & CD_FLAG_INVALID)) {
1464 bp->b_error = ENXIO;
1469 * If we don't have valid media, look for it before trying to
1472 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1475 error = cdcheckmedia(periph);
1478 bp->b_error = error;
1484 * Place it in the queue of disk activities for this disk
1486 bufqdisksort(&softc->buf_queue, bp);
1491 * Schedule ourselves for performing the work. We do things
1492 * differently for changers.
1494 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1495 xpt_schedule(periph, /* XXX priority */1);
1497 cdschedule(periph, /* priority */ 1);
1501 bp->b_flags |= B_ERROR;
1503 * Correctly set the buf to indicate a completed xfer
1505 bp->b_resid = bp->b_bcount;
1511 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1513 struct cd_softc *softc;
1515 struct ccb_scsiio *csio;
1516 struct scsi_read_capacity_data *rcap;
1518 softc = (struct cd_softc *)periph->softc;
1520 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1522 switch (softc->state) {
1523 case CD_STATE_NORMAL:
1526 bp = bufq_first(&softc->buf_queue);
1527 if (periph->immediate_priority <= periph->pinfo.priority) {
1528 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1530 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1532 periph->immediate_priority = CAM_PRIORITY_NONE;
1534 wakeup(&periph->ccb_list);
1535 } else if (bp == NULL) {
1537 xpt_release_ccb(start_ccb);
1539 bufq_remove(&softc->buf_queue, bp);
1541 devstat_start_transaction(&softc->device_stats);
1543 scsi_read_write(&start_ccb->csio,
1545 /* cbfcnp */ cddone,
1546 (bp->b_flags & B_ORDERED) != 0 ?
1549 /* read */bp->b_flags & B_READ,
1551 /* minimum_cmd_size */ 10,
1552 /* lba */ bp->b_pblkno,
1553 bp->b_bcount / softc->params.blksize,
1554 /* data_ptr */ bp->b_data,
1555 /* dxfer_len */ bp->b_bcount,
1556 /* sense_len */ SSD_FULL_SIZE,
1557 /* timeout */ 30000);
1558 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1562 * Block out any asyncronous callbacks
1563 * while we touch the pending ccb list.
1565 LIST_INSERT_HEAD(&softc->pending_ccbs,
1566 &start_ccb->ccb_h, periph_links.le);
1568 /* We expect a unit attention from this device */
1569 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1570 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1571 softc->flags &= ~CD_FLAG_RETRY_UA;
1574 start_ccb->ccb_h.ccb_bp = bp;
1575 bp = bufq_first(&softc->buf_queue);
1578 xpt_action(start_ccb);
1581 /* Have more work to do, so ensure we stay scheduled */
1582 xpt_schedule(periph, /* XXX priority */1);
1586 case CD_STATE_PROBE:
1589 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT);
1590 csio = &start_ccb->csio;
1591 scsi_read_capacity(csio,
1598 start_ccb->ccb_h.ccb_bp = NULL;
1599 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1600 xpt_action(start_ccb);
1607 cddone(struct cam_periph *periph, union ccb *done_ccb)
1609 struct cd_softc *softc;
1610 struct ccb_scsiio *csio;
1612 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1614 softc = (struct cd_softc *)periph->softc;
1615 csio = &done_ccb->csio;
1617 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1618 case CD_CCB_BUFFER_IO:
1623 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1626 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1629 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1634 /* Retry selection timeouts */
1635 sf |= SF_RETRY_SELTO;
1637 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1639 * A retry was scheuled, so
1649 xpt_print_path(periph->path);
1650 printf("cddone: got error %#x back\n", error);
1652 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1653 bufq_remove(&softc->buf_queue, q_bp);
1654 q_bp->b_resid = q_bp->b_bcount;
1655 q_bp->b_error = EIO;
1656 q_bp->b_flags |= B_ERROR;
1660 bp->b_resid = bp->b_bcount;
1661 bp->b_error = error;
1662 bp->b_flags |= B_ERROR;
1663 cam_release_devq(done_ccb->ccb_h.path,
1667 /*getcount_only*/0);
1670 bp->b_resid = csio->resid;
1672 if (bp->b_resid != 0) {
1673 /* Short transfer ??? */
1674 bp->b_flags |= B_ERROR;
1679 * Block out any asyncronous callbacks
1680 * while we touch the pending ccb list.
1683 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1686 if (softc->flags & CD_FLAG_CHANGER)
1687 cdchangerschedule(softc);
1689 devstat_end_transaction_buf(&softc->device_stats, bp);
1695 struct scsi_read_capacity_data *rdcap;
1696 char announce_buf[120]; /*
1697 * Currently (9/30/97) the
1698 * longest possible announce
1699 * buffer is 108 bytes, for the
1700 * first error case below.
1701 * That is 39 bytes for the
1702 * basic string, 16 bytes for the
1703 * biggest sense key (hardware
1704 * error), 52 bytes for the
1705 * text of the largest sense
1706 * qualifier valid for a CDROM,
1707 * (0x72, 0x03 or 0x04,
1708 * 0x03), and one byte for the
1709 * null terminating character.
1710 * To allow for longer strings,
1711 * the announce buffer is 120
1714 struct cd_params *cdp;
1716 cdp = &softc->params;
1718 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1720 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1721 cdp->blksize = scsi_4btoul (rdcap->length);
1723 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1725 snprintf(announce_buf, sizeof(announce_buf),
1726 "cd present [%lu x %lu byte records]",
1727 cdp->disksize, (u_long)cdp->blksize);
1732 * Retry any UNIT ATTENTION type errors. They
1733 * are expected at boot.
1735 error = cderror(done_ccb, 0, SF_RETRY_UA |
1736 SF_NO_PRINT | SF_RETRY_SELTO);
1737 if (error == ERESTART) {
1739 * A retry was scheuled, so
1743 } else if (error != 0) {
1745 struct scsi_sense_data *sense;
1747 int sense_key, error_code;
1750 struct ccb_getdev cgd;
1752 /* Don't wedge this device's queue */
1753 cam_release_devq(done_ccb->ccb_h.path,
1757 /*getcount_only*/0);
1759 status = done_ccb->ccb_h.status;
1761 xpt_setup_ccb(&cgd.ccb_h,
1762 done_ccb->ccb_h.path,
1764 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1765 xpt_action((union ccb *)&cgd);
1767 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1768 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1769 || ((status & CAM_AUTOSNS_VALID) == 0))
1775 sense = &csio->sense_data;
1776 scsi_extract_sense(sense, &error_code,
1781 * Attach to anything that claims to be a
1782 * CDROM or WORM device, as long as it
1783 * doesn't return a "Logical unit not
1784 * supported" (0x25) error.
1786 if ((have_sense) && (asc != 0x25)
1787 && (error_code == SSD_CURRENT_ERROR))
1788 snprintf(announce_buf,
1789 sizeof(announce_buf),
1790 "Attempt to query device "
1791 "size failed: %s, %s",
1792 scsi_sense_key_text[sense_key],
1793 scsi_sense_desc(asc,ascq,
1795 else if ((have_sense == 0)
1796 && ((status & CAM_STATUS_MASK) ==
1797 CAM_SCSI_STATUS_ERROR)
1798 && (csio->scsi_status ==
1799 SCSI_STATUS_BUSY)) {
1800 snprintf(announce_buf,
1801 sizeof(announce_buf),
1802 "Attempt to query device "
1803 "size failed: SCSI status: BUSY");
1804 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1806 * We only print out an error for
1807 * CDROM type devices. For WORM
1808 * devices, we don't print out an
1809 * error since a few WORM devices
1810 * don't support CDROM commands.
1811 * If we have sense information, go
1812 * ahead and print it out.
1813 * Otherwise, just say that we
1818 * Just print out the error, not
1819 * the full probe message, when we
1826 xpt_print_path(periph->path);
1827 printf("got CAM status %#x\n",
1828 done_ccb->ccb_h.status);
1830 xpt_print_path(periph->path);
1831 printf("fatal error, failed"
1832 " to attach to device\n");
1835 * Invalidate this peripheral.
1837 cam_periph_invalidate(periph);
1839 announce_buf[0] = '\0';
1843 * Invalidate this peripheral.
1845 cam_periph_invalidate(periph);
1846 announce_buf[0] = '\0';
1850 free(rdcap, M_TEMP);
1851 if (announce_buf[0] != '\0') {
1852 xpt_announce_periph(periph, announce_buf);
1853 if (softc->flags & CD_FLAG_CHANGER)
1854 cdchangerschedule(softc);
1856 * Create our sysctl variables, now that we know
1857 * we have successfully attached.
1859 taskqueue_enqueue(taskqueue_thread[mycpuid],
1860 &softc->sysctl_task);
1862 softc->state = CD_STATE_NORMAL;
1864 * Since our peripheral may be invalidated by an error
1865 * above or an external event, we must release our CCB
1866 * before releasing the probe lock on the peripheral.
1867 * The peripheral will only go away once the last lock
1868 * is removed, and we need it around for the CCB release
1871 xpt_release_ccb(done_ccb);
1872 cam_periph_unlock(periph);
1875 case CD_CCB_WAITING:
1877 /* Caller will release the CCB */
1878 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1879 ("trying to wakeup ccbwait\n"));
1881 wakeup(&done_ccb->ccb_h.cbfcnp);
1887 xpt_release_ccb(done_ccb);
1890 static union cd_pages *
1891 cdgetpage(struct cd_mode_params *mode_params)
1893 union cd_pages *page;
1895 if (mode_params->cdb_size == 10)
1896 page = (union cd_pages *)find_mode_page_10(
1897 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1899 page = (union cd_pages *)find_mode_page_6(
1900 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1906 cdgetpagesize(int page_num)
1910 for (i = 0; i < (sizeof(cd_page_size_table)/
1911 sizeof(cd_page_size_table[0])); i++) {
1912 if (cd_page_size_table[i].page == page_num)
1913 return (cd_page_size_table[i].page_size);
1919 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1922 struct cam_periph *periph;
1923 struct cd_softc *softc;
1928 periph = cam_extend_get(cdperiphs, unit);
1932 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1934 softc = (struct cd_softc *)periph->softc;
1936 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1937 ("trying to do ioctl %#lx\n", cmd));
1939 error = cam_periph_lock(periph, PCATCH);
1945 * If we don't have media loaded, check for it. If still don't
1946 * have media loaded, we can only do a load or eject.
1948 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1949 && ((cmd != CDIOCCLOSE)
1950 && (cmd != CDIOCEJECT))) {
1951 error = cdcheckmedia(periph);
1953 cam_periph_unlock(periph);
1960 case CDIOCPLAYTRACKS:
1962 struct ioc_play_track *args
1963 = (struct ioc_play_track *) addr;
1964 struct cd_mode_params params;
1965 union cd_pages *page;
1967 params.alloc_len = sizeof(union cd_mode_data_6_10);
1968 params.mode_buf = malloc(params.alloc_len, M_TEMP,
1971 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1972 ("trying to do CDIOCPLAYTRACKS\n"));
1974 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1976 free(params.mode_buf, M_TEMP);
1979 page = cdgetpage(¶ms);
1981 page->audio.flags &= ~CD_PA_SOTC;
1982 page->audio.flags |= CD_PA_IMMED;
1983 error = cdsetmode(periph, ¶ms);
1984 free(params.mode_buf, M_TEMP);
1989 * This was originally implemented with the PLAY
1990 * AUDIO TRACK INDEX command, but that command was
1991 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1992 * drives support it but ATAPI and ATAPI-derivative
1993 * drives don't seem to support it. So we keep a
1994 * cache of the table of contents and translate
1995 * track numbers to MSF format.
1997 if (softc->flags & CD_FLAG_VALID_TOC) {
1998 union msf_lba *sentry, *eentry;
2001 if (args->end_track <
2002 softc->toc.header.ending_track + 1)
2004 if (args->end_track >
2005 softc->toc.header.ending_track + 1)
2007 softc->toc.header.ending_track + 1;
2008 st = args->start_track -
2009 softc->toc.header.starting_track;
2010 et = args->end_track -
2011 softc->toc.header.starting_track;
2014 || (st > (softc->toc.header.ending_track -
2015 softc->toc.header.starting_track))) {
2019 sentry = &softc->toc.entries[st].addr;
2020 eentry = &softc->toc.entries[et].addr;
2021 error = cdplaymsf(periph,
2030 * If we don't have a valid TOC, try the
2031 * play track index command. It is part of
2032 * the SCSI-2 spec, but was removed in the
2033 * MMC specs. ATAPI and ATAPI-derived
2034 * drives don't support it.
2036 if (softc->quirks & CD_Q_BCD_TRACKS) {
2038 bin2bcd(args->start_track);
2040 bin2bcd(args->end_track);
2042 error = cdplaytracks(periph,
2052 struct ioc_play_msf *args
2053 = (struct ioc_play_msf *) addr;
2054 struct cd_mode_params params;
2055 union cd_pages *page;
2057 params.alloc_len = sizeof(union cd_mode_data_6_10);
2058 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2061 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2062 ("trying to do CDIOCPLAYMSF\n"));
2064 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2066 free(params.mode_buf, M_TEMP);
2069 page = cdgetpage(¶ms);
2071 page->audio.flags &= ~CD_PA_SOTC;
2072 page->audio.flags |= CD_PA_IMMED;
2073 error = cdsetmode(periph, ¶ms);
2074 free(params.mode_buf, M_TEMP);
2077 error = cdplaymsf(periph,
2086 case CDIOCPLAYBLOCKS:
2088 struct ioc_play_blocks *args
2089 = (struct ioc_play_blocks *) addr;
2090 struct cd_mode_params params;
2091 union cd_pages *page;
2093 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2094 ("trying to do CDIOCPLAYBLOCKS\n"));
2096 params.alloc_len = sizeof(union cd_mode_data_6_10);
2097 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2100 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2102 free(params.mode_buf, M_TEMP);
2105 page = cdgetpage(¶ms);
2107 page->audio.flags &= ~CD_PA_SOTC;
2108 page->audio.flags |= CD_PA_IMMED;
2109 error = cdsetmode(periph, ¶ms);
2110 free(params.mode_buf, M_TEMP);
2113 error = cdplay(periph, args->blk, args->len);
2116 case CDIOCREADSUBCHANNEL:
2118 struct ioc_read_subchannel *args
2119 = (struct ioc_read_subchannel *) addr;
2120 struct cd_sub_channel_info *data;
2121 u_int32_t len = args->data_len;
2123 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2124 ("trying to do CDIOCREADSUBCHANNEL\n"));
2126 data = malloc(sizeof(struct cd_sub_channel_info),
2129 if ((len > sizeof(struct cd_sub_channel_info)) ||
2130 (len < sizeof(struct cd_sub_channel_header))) {
2132 "scsi_cd: cdioctl: "
2133 "cdioreadsubchannel: error, len=%d\n",
2140 if (softc->quirks & CD_Q_BCD_TRACKS)
2141 args->track = bin2bcd(args->track);
2143 error = cdreadsubchannel(periph, args->address_format,
2144 args->data_format, args->track, data, len);
2150 if (softc->quirks & CD_Q_BCD_TRACKS)
2151 data->what.track_info.track_number =
2152 bcd2bin(data->what.track_info.track_number);
2153 len = min(len, ((data->header.data_len[0] << 8) +
2154 data->header.data_len[1] +
2155 sizeof(struct cd_sub_channel_header)));
2156 if (copyout(data, args->data, len) != 0) {
2163 case CDIOREADTOCHEADER:
2165 struct ioc_toc_header *th;
2167 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2168 ("trying to do CDIOREADTOCHEADER\n"));
2170 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
2172 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2173 sizeof (*th), /*sense_flags*/0);
2178 if (softc->quirks & CD_Q_BCD_TRACKS) {
2179 /* we are going to have to convert the BCD
2180 * encoding on the cd to what is expected
2182 th->starting_track =
2183 bcd2bin(th->starting_track);
2184 th->ending_track = bcd2bin(th->ending_track);
2186 th->len = ntohs(th->len);
2187 bcopy(th, addr, sizeof(*th));
2191 case CDIOREADTOCENTRYS:
2193 struct cd_tocdata *data;
2194 struct cd_toc_single *lead;
2195 struct ioc_read_toc_entry *te =
2196 (struct ioc_read_toc_entry *) addr;
2197 struct ioc_toc_header *th;
2198 u_int32_t len, readlen, idx, num;
2199 u_int32_t starting_track = te->starting_track;
2201 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2202 ("trying to do CDIOREADTOCENTRYS\n"));
2204 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2205 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK);
2207 if (te->data_len < sizeof(struct cd_toc_entry)
2208 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2209 || (te->address_format != CD_MSF_FORMAT
2210 && te->address_format != CD_LBA_FORMAT)) {
2212 printf("scsi_cd: error in readtocentries, "
2213 "returning EINVAL\n");
2220 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2221 sizeof (*th), /*sense_flags*/0);
2228 if (softc->quirks & CD_Q_BCD_TRACKS) {
2229 /* we are going to have to convert the BCD
2230 * encoding on the cd to what is expected
2232 th->starting_track =
2233 bcd2bin(th->starting_track);
2234 th->ending_track = bcd2bin(th->ending_track);
2237 if (starting_track == 0)
2238 starting_track = th->starting_track;
2239 else if (starting_track == LEADOUT)
2240 starting_track = th->ending_track + 1;
2241 else if (starting_track < th->starting_track ||
2242 starting_track > th->ending_track + 1) {
2243 printf("scsi_cd: error in readtocentries, "
2244 "returning EINVAL\n");
2251 /* calculate reading length without leadout entry */
2252 readlen = (th->ending_track - starting_track + 1) *
2253 sizeof(struct cd_toc_entry);
2255 /* and with leadout entry */
2256 len = readlen + sizeof(struct cd_toc_entry);
2257 if (te->data_len < len) {
2262 if (len > sizeof(data->entries)) {
2263 printf("scsi_cd: error in readtocentries, "
2264 "returning EINVAL\n");
2270 num = len / sizeof(struct cd_toc_entry);
2273 error = cdreadtoc(periph, te->address_format,
2276 readlen + sizeof (*th),
2285 /* make leadout entry if needed */
2286 idx = starting_track + num - 1;
2287 if (softc->quirks & CD_Q_BCD_TRACKS)
2288 th->ending_track = bcd2bin(th->ending_track);
2289 if (idx == th->ending_track + 1) {
2290 error = cdreadtoc(periph, te->address_format,
2291 LEADOUT, (u_int8_t *)lead,
2299 data->entries[idx - starting_track] =
2302 if (softc->quirks & CD_Q_BCD_TRACKS) {
2303 for (idx = 0; idx < num - 1; idx++) {
2304 data->entries[idx].track =
2305 bcd2bin(data->entries[idx].track);
2309 error = copyout(data->entries, te->data, len);
2314 case CDIOREADTOCENTRY:
2316 struct cd_toc_single *data;
2317 struct ioc_read_toc_single_entry *te =
2318 (struct ioc_read_toc_single_entry *) addr;
2319 struct ioc_toc_header *th;
2322 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2323 ("trying to do CDIOREADTOCENTRY\n"));
2325 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2327 if (te->address_format != CD_MSF_FORMAT
2328 && te->address_format != CD_LBA_FORMAT) {
2329 printf("error in readtocentry, "
2330 " returning EINVAL\n");
2337 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2338 sizeof (*th), /*sense_flags*/0);
2344 if (softc->quirks & CD_Q_BCD_TRACKS) {
2345 /* we are going to have to convert the BCD
2346 * encoding on the cd to what is expected
2348 th->starting_track =
2349 bcd2bin(th->starting_track);
2350 th->ending_track = bcd2bin(th->ending_track);
2354 track = th->starting_track;
2355 else if (track == LEADOUT)
2357 else if (track < th->starting_track ||
2358 track > th->ending_track + 1) {
2359 printf("error in readtocentry, "
2360 " returning EINVAL\n");
2366 error = cdreadtoc(periph, te->address_format, track,
2367 (u_int8_t *)data, sizeof(*data),
2374 if (softc->quirks & CD_Q_BCD_TRACKS)
2375 data->entry.track = bcd2bin(data->entry.track);
2376 bcopy(&data->entry, &te->entry,
2377 sizeof(struct cd_toc_entry));
2383 struct ioc_patch *arg = (struct ioc_patch *)addr;
2384 struct cd_mode_params params;
2385 union cd_pages *page;
2387 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2388 ("trying to do CDIOCSETPATCH\n"));
2390 params.alloc_len = sizeof(union cd_mode_data_6_10);
2391 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2393 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2395 free(params.mode_buf, M_TEMP);
2398 page = cdgetpage(¶ms);
2400 page->audio.port[LEFT_PORT].channels =
2402 page->audio.port[RIGHT_PORT].channels =
2404 page->audio.port[2].channels = arg->patch[2];
2405 page->audio.port[3].channels = arg->patch[3];
2406 error = cdsetmode(periph, ¶ms);
2407 free(params.mode_buf, M_TEMP);
2412 struct ioc_vol *arg = (struct ioc_vol *) addr;
2413 struct cd_mode_params params;
2414 union cd_pages *page;
2416 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2417 ("trying to do CDIOCGETVOL\n"));
2419 params.alloc_len = sizeof(union cd_mode_data_6_10);
2420 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2422 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2424 free(params.mode_buf, M_TEMP);
2427 page = cdgetpage(¶ms);
2429 arg->vol[LEFT_PORT] =
2430 page->audio.port[LEFT_PORT].volume;
2431 arg->vol[RIGHT_PORT] =
2432 page->audio.port[RIGHT_PORT].volume;
2433 arg->vol[2] = page->audio.port[2].volume;
2434 arg->vol[3] = page->audio.port[3].volume;
2435 free(params.mode_buf, M_TEMP);
2440 struct ioc_vol *arg = (struct ioc_vol *) addr;
2441 struct cd_mode_params params;
2442 union cd_pages *page;
2444 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2445 ("trying to do CDIOCSETVOL\n"));
2447 params.alloc_len = sizeof(union cd_mode_data_6_10);
2448 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2450 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2452 free(params.mode_buf, M_TEMP);
2455 page = cdgetpage(¶ms);
2457 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2458 page->audio.port[LEFT_PORT].volume =
2459 arg->vol[LEFT_PORT];
2460 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2461 page->audio.port[RIGHT_PORT].volume =
2462 arg->vol[RIGHT_PORT];
2463 page->audio.port[2].volume = arg->vol[2];
2464 page->audio.port[3].volume = arg->vol[3];
2465 error = cdsetmode(periph, ¶ms);
2466 free(params.mode_buf, M_TEMP);
2471 struct cd_mode_params params;
2472 union cd_pages *page;
2474 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2475 ("trying to do CDIOCSETMONO\n"));
2477 params.alloc_len = sizeof(union cd_mode_data_6_10);
2478 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2480 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2482 free(params.mode_buf, M_TEMP);
2485 page = cdgetpage(¶ms);
2487 page->audio.port[LEFT_PORT].channels =
2488 LEFT_CHANNEL | RIGHT_CHANNEL;
2489 page->audio.port[RIGHT_PORT].channels =
2490 LEFT_CHANNEL | RIGHT_CHANNEL;
2491 page->audio.port[2].channels = 0;
2492 page->audio.port[3].channels = 0;
2493 error = cdsetmode(periph, ¶ms);
2494 free(params.mode_buf, M_TEMP);
2497 case CDIOCSETSTEREO:
2499 struct cd_mode_params params;
2500 union cd_pages *page;
2502 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2503 ("trying to do CDIOCSETSTEREO\n"));
2505 params.alloc_len = sizeof(union cd_mode_data_6_10);
2506 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2508 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2510 free(params.mode_buf, M_TEMP);
2513 page = cdgetpage(¶ms);
2515 page->audio.port[LEFT_PORT].channels =
2517 page->audio.port[RIGHT_PORT].channels =
2519 page->audio.port[2].channels = 0;
2520 page->audio.port[3].channels = 0;
2521 error = cdsetmode(periph, ¶ms);
2522 free(params.mode_buf, M_TEMP);
2527 struct cd_mode_params params;
2528 union cd_pages *page;
2530 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2531 ("trying to do CDIOCSETMUTE\n"));
2533 params.alloc_len = sizeof(union cd_mode_data_6_10);
2534 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2536 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2538 free(¶ms, M_TEMP);
2541 page = cdgetpage(¶ms);
2543 page->audio.port[LEFT_PORT].channels = 0;
2544 page->audio.port[RIGHT_PORT].channels = 0;
2545 page->audio.port[2].channels = 0;
2546 page->audio.port[3].channels = 0;
2547 error = cdsetmode(periph, ¶ms);
2548 free(params.mode_buf, M_TEMP);
2553 struct cd_mode_params params;
2554 union cd_pages *page;
2556 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2557 ("trying to do CDIOCSETLEFT\n"));
2559 params.alloc_len = sizeof(union cd_mode_data_6_10);
2560 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2563 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2565 free(params.mode_buf, M_TEMP);
2568 page = cdgetpage(¶ms);
2570 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2571 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2572 page->audio.port[2].channels = 0;
2573 page->audio.port[3].channels = 0;
2574 error = cdsetmode(periph, ¶ms);
2575 free(params.mode_buf, M_TEMP);
2580 struct cd_mode_params params;
2581 union cd_pages *page;
2583 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2584 ("trying to do CDIOCSETRIGHT\n"));
2586 params.alloc_len = sizeof(union cd_mode_data_6_10);
2587 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2590 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2592 free(params.mode_buf, M_TEMP);
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_TEMP);
2606 error = cdpause(periph, 1);
2609 error = cdpause(periph, 0);
2612 error = cdstartunit(periph, 0);
2615 error = cdstartunit(periph, 1);
2622 * The user successfully closed the tray, run
2623 * cdcheckmedia() again so we can re-sync the disklabel
2626 cdcheckmedia(periph);
2630 error = cdstopunit(periph, 0);
2633 error = cdstopunit(periph, 1);
2640 * Since we've successfully ejected the media, run
2641 * cdcheckmedia() again so we re-sync the disklabel
2644 cdcheckmedia(periph);
2648 cdprevent(periph, PR_ALLOW);
2651 cdprevent(periph, PR_PREVENT);
2654 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2658 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2662 /* return (cd_reset(periph)); */
2665 case CDRIOCREADSPEED:
2666 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2668 case CDRIOCWRITESPEED:
2669 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2672 case DVDIOCREPORTKEY: {
2673 struct dvd_authinfo *authinfo;
2675 authinfo = (struct dvd_authinfo *)addr;
2677 if (cmd == DVDIOCREPORTKEY)
2678 error = cdreportkey(periph, authinfo);
2680 error = cdsendkey(periph, authinfo);
2683 case DVDIOCREADSTRUCTURE: {
2684 struct dvd_struct *dvdstruct;
2686 dvdstruct = (struct dvd_struct *)addr;
2688 error = cdreaddvdstructure(periph, dvdstruct);
2693 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2697 cam_periph_unlock(periph);
2699 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2705 cdprevent(struct cam_periph *periph, int action)
2708 struct cd_softc *softc;
2711 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2713 softc = (struct cd_softc *)periph->softc;
2715 if (((action == PR_ALLOW)
2716 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2717 || ((action == PR_PREVENT)
2718 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2722 ccb = cdgetccb(periph, /* priority */ 1);
2724 scsi_prevent(&ccb->csio,
2730 /* timeout */60000);
2732 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2733 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2735 xpt_release_ccb(ccb);
2738 if (action == PR_ALLOW)
2739 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2741 softc->flags |= CD_FLAG_DISC_LOCKED;
2746 cdcheckmedia(struct cam_periph *periph)
2748 struct cd_softc *softc;
2749 struct ioc_toc_header *toch;
2750 struct cd_toc_single leadout;
2751 struct ccb_getdev cgd;
2752 u_int32_t size, toclen;
2753 int error, num_entries, cdindex;
2754 int first_track_audio;
2755 struct disklabel *label;
2757 softc = (struct cd_softc *)periph->softc;
2759 first_track_audio = -1;
2762 cdprevent(periph, PR_PREVENT);
2765 * Build prototype label for whole disk.
2766 * Should take information about different data tracks from the
2767 * TOC and put it in the partition table.
2769 label = &softc->disk.d_label;
2770 bzero(label, sizeof(*label));
2771 label->d_type = DTYPE_SCSI;
2774 * Grab the inquiry data to get the vendor and product names.
2775 * Put them in the typename and packname for the label.
2777 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2778 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2779 xpt_action((union ccb *)&cgd);
2781 strncpy(label->d_typename, cgd.inq_data.vendor,
2782 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2783 strncpy(label->d_packname, cgd.inq_data.product,
2784 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2786 label->d_flags = D_REMOVABLE;
2788 * Make partition 'a' cover the whole disk. This is a temporary
2789 * compatibility hack. The 'a' partition should not exist, so
2790 * the slice code won't create it. The slice code will make
2791 * partition (RAW_PART + 'a') cover the whole disk and fill in
2792 * some more defaults.
2794 label->d_partitions[0].p_size = label->d_secperunit;
2795 label->d_partitions[0].p_fstype = FS_OTHER;
2798 * Default to not reading the disklabel off the disk, until we can
2799 * verify that we have media, that we have a table of contents, and
2800 * that the first track is a data track (which could theoretically
2801 * contain a disklabel).
2803 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
2804 softc->disk.d_dsflags |= DSO_NOLABELS;
2807 * Clear the valid media and TOC flags until we've verified that we
2810 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2813 * Get the disc size and block size. If we can't get it, we don't
2814 * have media, most likely.
2816 if ((error = cdsize(periph, &size)) != 0) {
2818 * Set a bogus sector size, so the slice code won't try to
2819 * divide by 0 and panic the kernel.
2821 label->d_secsize = 2048;
2823 label->d_secperunit = 0;
2826 * XXX KDM is this a good idea? Seems to cause more
2829 if (softc->flags & CD_FLAG_OPEN) {
2835 * We don't bother checking the return value here,
2836 * since we already have an error...
2838 dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2839 /*data*/(caddr_t)&force, /*flags*/ 0,
2840 &softc->disk.d_slice);
2844 * Tell devstat(9) we don't have a blocksize.
2846 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2848 cdprevent(periph, PR_ALLOW);
2853 label->d_secsize = softc->params.blksize;
2854 label->d_secperunit = softc->params.disksize;
2857 * Force a re-sync of slice information, like the blocksize,
2858 * now that we know it. It isn't pretty...but according to
2859 * Bruce Evans, this is probably the best way to do it in
2860 * -stable. We only do this if we're already open, and
2861 * therefore dsopen() has already run. If CD_FLAG_OPEN
2862 * isn't set, this isn't necessary.
2864 if (softc->flags & CD_FLAG_OPEN) {
2869 error = dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2870 /*data*/(caddr_t)&force, /*flags*/ 0,
2871 &softc->disk.d_slice);
2874 * Set a bogus sector size, so the slice code
2875 * won't try to divide by 0 and panic the
2878 label->d_secsize = 2048;
2880 label->d_secperunit = 0;
2883 * Tell devstat(9) we don't have a blocksize.
2885 softc->device_stats.flags |=
2886 DEVSTAT_BS_UNAVAILABLE;
2888 cdprevent(periph, PR_ALLOW);
2893 * We unconditionally (re)set the blocksize each time the
2894 * CD device is opened. This is because the CD can change,
2895 * and therefore the blocksize might change.
2896 * XXX problems here if some slice or partition is still
2897 * open with the old size?
2899 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2900 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2901 softc->device_stats.block_size = softc->params.blksize;
2903 softc->flags |= CD_FLAG_VALID_MEDIA;
2907 * Now we check the table of contents. This (currently) is only
2908 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2909 * things like present a separate entry in /dev for each track,
2910 * like that acd(4) driver does.
2912 bzero(&softc->toc, sizeof(softc->toc));
2913 toch = &softc->toc.header;
2916 * We will get errors here for media that doesn't have a table of
2917 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2918 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2919 * has not been recorded (no complete session) and the Format codes
2920 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2921 * with an INVALID FIELD IN CDB. Devices that are not capable of
2922 * reading an incomplete session on DDC/CD-R/RW media shall report
2923 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2925 * So this isn't fatal if we can't read the table of contents, it
2926 * just means that the user won't be able to issue the play tracks
2927 * ioctl, and likely lots of other stuff won't work either. They
2928 * need to burn the CD before we can do a whole lot with it. So
2929 * we don't print anything here if we get an error back.
2931 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2934 * Errors in reading the table of contents aren't fatal, we just
2935 * won't have a valid table of contents cached.
2939 bzero(&softc->toc, sizeof(softc->toc));
2943 if (softc->quirks & CD_Q_BCD_TRACKS) {
2944 toch->starting_track = bcd2bin(toch->starting_track);
2945 toch->ending_track = bcd2bin(toch->ending_track);
2948 /* Number of TOC entries, plus leadout */
2949 num_entries = (toch->ending_track - toch->starting_track) + 2;
2951 if (num_entries <= 0)
2954 toclen = num_entries * sizeof(struct cd_toc_entry);
2956 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2957 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2961 bzero(&softc->toc, sizeof(softc->toc));
2965 if (softc->quirks & CD_Q_BCD_TRACKS) {
2966 toch->starting_track = bcd2bin(toch->starting_track);
2967 toch->ending_track = bcd2bin(toch->ending_track);
2969 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2972 * XXX KDM is this necessary? Probably only if the drive doesn't
2973 * return leadout information with the table of contents.
2975 cdindex = toch->starting_track + num_entries -1;
2976 if (cdindex == toch->ending_track + 1) {
2978 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2979 (u_int8_t *)&leadout, sizeof(leadout),
2985 softc->toc.entries[cdindex - toch->starting_track] =
2988 if (softc->quirks & CD_Q_BCD_TRACKS) {
2989 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2990 softc->toc.entries[cdindex].track =
2991 bcd2bin(softc->toc.entries[cdindex].track);
2996 * Run through the TOC entries, find the first entry and determine
2997 * whether it is an audio or data track.
2999 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3000 if (softc->toc.entries[cdindex].track == toch->starting_track) {
3001 if (softc->toc.entries[cdindex].control & 0x04)
3002 first_track_audio = 0;
3004 first_track_audio = 1;
3010 * If first_track_audio is non-zero, we either have an error (e.g.
3011 * couldn't find the starting track) or the first track is an audio
3012 * track. If first_track_audio is 0, the first track is a data
3013 * track that could have a disklabel. Attempt to read the
3014 * disklabel off the media, just in case the user put one there.
3016 if (first_track_audio == 0) {
3017 softc->disk.d_dsflags |= DSO_COMPATLABEL;
3018 softc->disk.d_dsflags &= ~DSO_NOLABELS;
3020 softc->flags |= CD_FLAG_VALID_TOC;
3027 cdsize(struct cam_periph *periph, u_int32_t *size)
3029 struct cd_softc *softc;
3031 struct scsi_read_capacity_data *rcap_buf;
3034 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3036 softc = (struct cd_softc *)periph->softc;
3038 ccb = cdgetccb(periph, /* priority */ 1);
3040 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3041 M_TEMP, M_INTWAIT | M_ZERO);
3043 scsi_read_capacity(&ccb->csio,
3049 /* timeout */20000);
3051 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3052 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
3054 xpt_release_ccb(ccb);
3056 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3057 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3059 * SCSI-3 mandates that the reported blocksize shall be 2048.
3060 * Older drives sometimes report funny values, trim it down to
3061 * 2048, or other parts of the kernel will get confused.
3063 * XXX we leave drives alone that might report 512 bytes, as
3064 * well as drives reporting more weird sizes like perhaps 4K.
3066 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3067 softc->params.blksize = 2048;
3069 free(rcap_buf, M_TEMP);
3070 *size = softc->params.disksize;
3077 cd6byteworkaround(union ccb *ccb)
3080 struct cam_periph *periph;
3081 struct cd_softc *softc;
3082 struct cd_mode_params *params;
3085 periph = xpt_path_periph(ccb->ccb_h.path);
3086 softc = (struct cd_softc *)periph->softc;
3088 cdb = ccb->csio.cdb_io.cdb_bytes;
3090 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3091 || ((cdb[0] != MODE_SENSE_6)
3092 && (cdb[0] != MODE_SELECT_6)))
3096 * Because there is no convenient place to stash the overall
3097 * cd_mode_params structure pointer, we have to grab it like this.
3098 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3099 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3101 * XXX It would be nice if, at some point, we could increase the
3102 * number of available peripheral private pointers. Both pointers
3103 * are currently used in most every peripheral driver.
3107 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3108 if (params->mode_buf == ccb->csio.data_ptr) {
3115 * This shouldn't happen. All mode sense and mode select
3116 * operations in the cd(4) driver MUST go through cdgetmode() and
3120 xpt_print_path(periph->path);
3121 printf("mode buffer not found in mode queue!\n");
3125 params->cdb_size = 10;
3126 softc->minimum_command_size = 10;
3127 xpt_print_path(ccb->ccb_h.path);
3128 printf("%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3129 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3131 if (cdb[0] == MODE_SENSE_6) {
3132 struct scsi_mode_sense_10 ms10;
3133 struct scsi_mode_sense_6 *ms6;
3136 ms6 = (struct scsi_mode_sense_6 *)cdb;
3138 bzero(&ms10, sizeof(ms10));
3139 ms10.opcode = MODE_SENSE_10;
3140 ms10.byte2 = ms6->byte2;
3141 ms10.page = ms6->page;
3144 * 10 byte mode header, block descriptor,
3145 * sizeof(union cd_pages)
3147 len = sizeof(struct cd_mode_data_10);
3148 ccb->csio.dxfer_len = len;
3150 scsi_ulto2b(len, ms10.length);
3151 ms10.control = ms6->control;
3152 bcopy(&ms10, cdb, 10);
3153 ccb->csio.cdb_len = 10;
3155 struct scsi_mode_select_10 ms10;
3156 struct scsi_mode_select_6 *ms6;
3157 struct scsi_mode_header_6 *header6;
3158 struct scsi_mode_header_10 *header10;
3159 struct scsi_mode_page_header *page_header;
3160 int blk_desc_len, page_num, page_size, len;
3162 ms6 = (struct scsi_mode_select_6 *)cdb;
3164 bzero(&ms10, sizeof(ms10));
3165 ms10.opcode = MODE_SELECT_10;
3166 ms10.byte2 = ms6->byte2;
3168 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3169 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3171 page_header = find_mode_page_6(header6);
3172 page_num = page_header->page_code;
3174 blk_desc_len = header6->blk_desc_len;
3176 page_size = cdgetpagesize(page_num);
3178 if (page_size != (page_header->page_length +
3179 sizeof(*page_header)))
3180 page_size = page_header->page_length +
3181 sizeof(*page_header);
3183 len = sizeof(*header10) + blk_desc_len + page_size;
3185 len = min(params->alloc_len, len);
3188 * Since the 6 byte parameter header is shorter than the 10
3189 * byte parameter header, we need to copy the actual mode
3190 * page data, and the block descriptor, if any, so things wind
3191 * up in the right place. The regions will overlap, but
3192 * bcopy() does the right thing.
3194 bcopy(params->mode_buf + sizeof(*header6),
3195 params->mode_buf + sizeof(*header10),
3196 len - sizeof(*header10));
3198 /* Make sure these fields are set correctly. */
3199 scsi_ulto2b(0, header10->data_length);
3200 header10->medium_type = 0;
3201 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3203 ccb->csio.dxfer_len = len;
3205 scsi_ulto2b(len, ms10.length);
3206 ms10.control = ms6->control;
3207 bcopy(&ms10, cdb, 10);
3208 ccb->csio.cdb_len = 10;
3211 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3212 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3215 cam_release_devq(ccb->ccb_h.path,
3219 /*getcount_only*/0);
3226 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3228 struct cd_softc *softc;
3229 struct cam_periph *periph;
3232 periph = xpt_path_periph(ccb->ccb_h.path);
3233 softc = (struct cd_softc *)periph->softc;
3238 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3239 * CDB comes back with this particular error, try transforming it
3240 * into the 10 byte version.
3242 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3243 error = cd6byteworkaround(ccb);
3244 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3245 CAM_SCSI_STATUS_ERROR)
3246 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3247 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3248 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3249 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3250 int sense_key, error_code, asc, ascq;
3252 scsi_extract_sense(&ccb->csio.sense_data,
3253 &error_code, &sense_key, &asc, &ascq);
3254 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3255 error = cd6byteworkaround(ccb);
3258 if (error == ERESTART)
3263 * Until we have a better way of doing pack validation,
3264 * don't treat UAs as errors.
3266 sense_flags |= SF_RETRY_UA;
3267 return (cam_periph_error(ccb, cam_flags, sense_flags,
3268 &softc->saved_ccb));
3272 * Read table of contents
3275 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3276 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3278 struct scsi_read_toc *scsi_cmd;
3280 struct ccb_scsiio *csio;
3287 ccb = cdgetccb(periph, /* priority */ 1);
3293 /* cbfcnp */ cddone,
3294 /* flags */ CAM_DIR_IN,
3295 /* tag_action */ MSG_SIMPLE_Q_TAG,
3296 /* data_ptr */ data,
3297 /* dxfer_len */ len,
3298 /* sense_len */ SSD_FULL_SIZE,
3299 sizeof(struct scsi_read_toc),
3300 /* timeout */ 50000);
3302 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3303 bzero (scsi_cmd, sizeof(*scsi_cmd));
3305 if (mode == CD_MSF_FORMAT)
3306 scsi_cmd->byte2 |= CD_MSF;
3307 scsi_cmd->from_track = start;
3308 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3309 scsi_cmd->data_len[0] = (ntoc) >> 8;
3310 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3312 scsi_cmd->op_code = READ_TOC;
3314 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3315 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO |
3318 xpt_release_ccb(ccb);
3324 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3325 u_int32_t format, int track,
3326 struct cd_sub_channel_info *data, u_int32_t len)
3328 struct scsi_read_subchannel *scsi_cmd;
3329 struct ccb_scsiio *csio;
3335 ccb = cdgetccb(periph, /* priority */ 1);
3341 /* cbfcnp */ cddone,
3342 /* flags */ CAM_DIR_IN,
3343 /* tag_action */ MSG_SIMPLE_Q_TAG,
3344 /* data_ptr */ (u_int8_t *)data,
3345 /* dxfer_len */ len,
3346 /* sense_len */ SSD_FULL_SIZE,
3347 sizeof(struct scsi_read_subchannel),
3348 /* timeout */ 50000);
3350 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3351 bzero (scsi_cmd, sizeof(*scsi_cmd));
3353 scsi_cmd->op_code = READ_SUBCHANNEL;
3354 if (mode == CD_MSF_FORMAT)
3355 scsi_cmd->byte1 |= CD_MSF;
3356 scsi_cmd->byte2 = SRS_SUBQ;
3357 scsi_cmd->subchan_format = format;
3358 scsi_cmd->track = track;
3359 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3360 scsi_cmd->control = 0;
3362 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3363 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3365 xpt_release_ccb(ccb);
3371 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3372 * routine. See comments in cd6byteworkaround() for details.
3375 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3378 struct ccb_scsiio *csio;
3379 struct cd_softc *softc;
3384 softc = (struct cd_softc *)periph->softc;
3386 ccb = cdgetccb(periph, /* priority */ 1);
3390 data->cdb_size = softc->minimum_command_size;
3391 if (data->cdb_size < 10)
3392 param_len = sizeof(struct cd_mode_data);
3394 param_len = sizeof(struct cd_mode_data_10);
3396 /* Don't say we've got more room than we actually allocated */
3397 param_len = min(param_len, data->alloc_len);
3399 scsi_mode_sense_len(csio,
3401 /* cbfcnp */ cddone,
3402 /* tag_action */ MSG_SIMPLE_Q_TAG,
3404 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3406 /* param_buf */ data->mode_buf,
3407 /* param_len */ param_len,
3408 /* minimum_cmd_size */ softc->minimum_command_size,
3409 /* sense_len */ SSD_FULL_SIZE,
3410 /* timeout */ 50000);
3413 * It would be nice not to have to do this, but there's no
3414 * available pointer in the CCB that would allow us to stuff the
3415 * mode params structure in there and retrieve it in
3416 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3417 * lets the caller know what CDB size we ended up using, so they
3418 * can find the actual mode page offset.
3420 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3422 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3423 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3425 xpt_release_ccb(ccb);
3427 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3430 * This is a bit of belt-and-suspenders checking, but if we run
3431 * into a situation where the target sends back multiple block
3432 * descriptors, we might not have enough space in the buffer to
3433 * see the whole mode page. Better to return an error than
3434 * potentially access memory beyond our malloced region.
3439 if (data->cdb_size == 10) {
3440 struct scsi_mode_header_10 *hdr10;
3442 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3443 data_len = scsi_2btoul(hdr10->data_length);
3444 data_len += sizeof(hdr10->data_length);
3446 struct scsi_mode_header_6 *hdr6;
3448 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3449 data_len = hdr6->data_length;
3450 data_len += sizeof(hdr6->data_length);
3454 * Complain if there is more mode data available than we
3455 * allocated space for. This could potentially happen if
3456 * we miscalculated the page length for some reason, if the
3457 * drive returns multiple block descriptors, or if it sets
3458 * the data length incorrectly.
3460 if (data_len > data->alloc_len) {
3461 xpt_print_path(periph->path);
3462 printf("allocated modepage %d length %d < returned "
3463 "length %d\n", page, data->alloc_len, data_len);
3472 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3473 * routine. See comments in cd6byteworkaround() for details.
3476 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3478 struct ccb_scsiio *csio;
3479 struct cd_softc *softc;
3481 int cdb_size, param_len;
3484 softc = (struct cd_softc *)periph->softc;
3486 ccb = cdgetccb(periph, /* priority */ 1);
3493 * If the data is formatted for the 10 byte version of the mode
3494 * select parameter list, we need to use the 10 byte CDB.
3495 * Otherwise, we use whatever the stored minimum command size.
3497 if (data->cdb_size == 10)
3498 cdb_size = data->cdb_size;
3500 cdb_size = softc->minimum_command_size;
3502 if (cdb_size >= 10) {
3503 struct scsi_mode_header_10 *mode_header;
3506 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3508 data_len = scsi_2btoul(mode_header->data_length);
3510 scsi_ulto2b(0, mode_header->data_length);
3512 * SONY drives do not allow a mode select with a medium_type
3513 * value that has just been returned by a mode sense; use a
3514 * medium_type of 0 (Default) instead.
3516 mode_header->medium_type = 0;
3519 * Pass back whatever the drive passed to us, plus the size
3520 * of the data length field.
3522 param_len = data_len + sizeof(mode_header->data_length);
3525 struct scsi_mode_header_6 *mode_header;
3527 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3529 param_len = mode_header->data_length + 1;
3531 mode_header->data_length = 0;
3533 * SONY drives do not allow a mode select with a medium_type
3534 * value that has just been returned by a mode sense; use a
3535 * medium_type of 0 (Default) instead.
3537 mode_header->medium_type = 0;
3540 /* Don't say we've got more room than we actually allocated */
3541 param_len = min(param_len, data->alloc_len);
3543 scsi_mode_select_len(csio,
3545 /* cbfcnp */ cddone,
3546 /* tag_action */ MSG_SIMPLE_Q_TAG,
3547 /* scsi_page_fmt */ 1,
3549 /* param_buf */ data->mode_buf,
3550 /* param_len */ param_len,
3551 /* minimum_cmd_size */ cdb_size,
3552 /* sense_len */ SSD_FULL_SIZE,
3553 /* timeout */ 50000);
3555 /* See comments in cdgetmode() and cd6byteworkaround(). */
3556 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3558 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3559 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3561 xpt_release_ccb(ccb);
3563 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3571 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3573 struct ccb_scsiio *csio;
3579 ccb = cdgetccb(periph, /* priority */ 1);
3582 * Use the smallest possible command to perform the operation.
3584 if ((len & 0xffff0000) == 0) {
3586 * We can fit in a 10 byte cdb.
3588 struct scsi_play_10 *scsi_cmd;
3590 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3591 bzero (scsi_cmd, sizeof(*scsi_cmd));
3592 scsi_cmd->op_code = PLAY_10;
3593 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3594 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3595 cdb_len = sizeof(*scsi_cmd);
3597 struct scsi_play_12 *scsi_cmd;
3599 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3600 bzero (scsi_cmd, sizeof(*scsi_cmd));
3601 scsi_cmd->op_code = PLAY_12;
3602 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3603 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3604 cdb_len = sizeof(*scsi_cmd);
3609 /*flags*/CAM_DIR_NONE,
3613 /*sense_len*/SSD_FULL_SIZE,
3615 /*timeout*/50 * 1000);
3617 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3618 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3620 xpt_release_ccb(ccb);
3626 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3627 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3629 struct scsi_play_msf *scsi_cmd;
3630 struct ccb_scsiio *csio;
3636 ccb = cdgetccb(periph, /* priority */ 1);
3642 /* cbfcnp */ cddone,
3643 /* flags */ CAM_DIR_NONE,
3644 /* tag_action */ MSG_SIMPLE_Q_TAG,
3645 /* data_ptr */ NULL,
3647 /* sense_len */ SSD_FULL_SIZE,
3648 sizeof(struct scsi_play_msf),
3649 /* timeout */ 50000);
3651 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3652 bzero (scsi_cmd, sizeof(*scsi_cmd));
3654 scsi_cmd->op_code = PLAY_MSF;
3655 scsi_cmd->start_m = startm;
3656 scsi_cmd->start_s = starts;
3657 scsi_cmd->start_f = startf;
3658 scsi_cmd->end_m = endm;
3659 scsi_cmd->end_s = ends;
3660 scsi_cmd->end_f = endf;
3662 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3663 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3665 xpt_release_ccb(ccb);
3672 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3673 u_int32_t etrack, u_int32_t eindex)
3675 struct scsi_play_track *scsi_cmd;
3676 struct ccb_scsiio *csio;
3682 ccb = cdgetccb(periph, /* priority */ 1);
3688 /* cbfcnp */ cddone,
3689 /* flags */ CAM_DIR_NONE,
3690 /* tag_action */ MSG_SIMPLE_Q_TAG,
3691 /* data_ptr */ NULL,
3693 /* sense_len */ SSD_FULL_SIZE,
3694 sizeof(struct scsi_play_track),
3695 /* timeout */ 50000);
3697 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3698 bzero (scsi_cmd, sizeof(*scsi_cmd));
3700 scsi_cmd->op_code = PLAY_TRACK;
3701 scsi_cmd->start_track = strack;
3702 scsi_cmd->start_index = sindex;
3703 scsi_cmd->end_track = etrack;
3704 scsi_cmd->end_index = eindex;
3706 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3707 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3709 xpt_release_ccb(ccb);
3715 cdpause(struct cam_periph *periph, u_int32_t go)
3717 struct scsi_pause *scsi_cmd;
3718 struct ccb_scsiio *csio;
3724 ccb = cdgetccb(periph, /* priority */ 1);
3730 /* cbfcnp */ cddone,
3731 /* flags */ CAM_DIR_NONE,
3732 /* tag_action */ MSG_SIMPLE_Q_TAG,
3733 /* data_ptr */ NULL,
3735 /* sense_len */ SSD_FULL_SIZE,
3736 sizeof(struct scsi_pause),
3737 /* timeout */ 50000);
3739 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3740 bzero (scsi_cmd, sizeof(*scsi_cmd));
3742 scsi_cmd->op_code = PAUSE;
3743 scsi_cmd->resume = go;
3745 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3746 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3748 xpt_release_ccb(ccb);
3754 cdstartunit(struct cam_periph *periph, int load)
3761 ccb = cdgetccb(periph, /* priority */ 1);
3763 scsi_start_stop(&ccb->csio,
3765 /* cbfcnp */ cddone,
3766 /* tag_action */ MSG_SIMPLE_Q_TAG,
3768 /* load_eject */ load,
3769 /* immediate */ FALSE,
3770 /* sense_len */ SSD_FULL_SIZE,
3771 /* timeout */ 50000);
3773 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3774 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3776 xpt_release_ccb(ccb);
3782 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3789 ccb = cdgetccb(periph, /* priority */ 1);
3791 scsi_start_stop(&ccb->csio,
3793 /* cbfcnp */ cddone,
3794 /* tag_action */ MSG_SIMPLE_Q_TAG,
3796 /* load_eject */ eject,
3797 /* immediate */ FALSE,
3798 /* sense_len */ SSD_FULL_SIZE,
3799 /* timeout */ 50000);
3801 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3802 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3804 xpt_release_ccb(ccb);
3810 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3812 struct scsi_set_speed *scsi_cmd;
3813 struct ccb_scsiio *csio;
3818 ccb = cdgetccb(periph, /* priority */ 1);
3821 /* Preserve old behavior: units in multiples of CDROM speed */
3829 /* cbfcnp */ cddone,
3830 /* flags */ CAM_DIR_NONE,
3831 /* tag_action */ MSG_SIMPLE_Q_TAG,
3832 /* data_ptr */ NULL,
3834 /* sense_len */ SSD_FULL_SIZE,
3835 sizeof(struct scsi_set_speed),
3836 /* timeout */ 50000);
3838 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3839 bzero(scsi_cmd, sizeof(*scsi_cmd));
3841 scsi_cmd->opcode = SET_CD_SPEED;
3842 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3843 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3845 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3846 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3848 xpt_release_ccb(ccb);
3854 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3866 ccb = cdgetccb(periph, /* priority */ 1);
3868 switch (authinfo->format) {
3869 case DVD_REPORT_AGID:
3870 length = sizeof(struct scsi_report_key_data_agid);
3872 case DVD_REPORT_CHALLENGE:
3873 length = sizeof(struct scsi_report_key_data_challenge);
3875 case DVD_REPORT_KEY1:
3876 length = sizeof(struct scsi_report_key_data_key1_key2);
3878 case DVD_REPORT_TITLE_KEY:
3879 length = sizeof(struct scsi_report_key_data_title);
3880 /* The lba field is only set for the title key */
3881 lba = authinfo->lba;
3883 case DVD_REPORT_ASF:
3884 length = sizeof(struct scsi_report_key_data_asf);
3886 case DVD_REPORT_RPC:
3887 length = sizeof(struct scsi_report_key_data_rpc);
3889 case DVD_INVALIDATE_AGID:
3895 break; /* NOTREACHED */
3899 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3905 scsi_report_key(&ccb->csio,
3907 /* cbfcnp */ cddone,
3908 /* tag_action */ MSG_SIMPLE_Q_TAG,
3910 /* agid */ authinfo->agid,
3911 /* key_format */ authinfo->format,
3912 /* data_ptr */ databuf,
3913 /* dxfer_len */ length,
3914 /* sense_len */ SSD_FULL_SIZE,
3915 /* timeout */ 50000);
3917 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3918 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3923 if (ccb->csio.resid != 0) {
3924 xpt_print_path(periph->path);
3925 printf("warning, residual for report key command is %d\n",
3929 switch(authinfo->format) {
3930 case DVD_REPORT_AGID: {
3931 struct scsi_report_key_data_agid *agid_data;
3933 agid_data = (struct scsi_report_key_data_agid *)databuf;
3935 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3939 case DVD_REPORT_CHALLENGE: {
3940 struct scsi_report_key_data_challenge *chal_data;
3942 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3944 bcopy(chal_data->challenge_key, authinfo->keychal,
3945 min(sizeof(chal_data->challenge_key),
3946 sizeof(authinfo->keychal)));
3949 case DVD_REPORT_KEY1: {
3950 struct scsi_report_key_data_key1_key2 *key1_data;
3952 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3954 bcopy(key1_data->key1, authinfo->keychal,
3955 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3958 case DVD_REPORT_TITLE_KEY: {
3959 struct scsi_report_key_data_title *title_data;
3961 title_data = (struct scsi_report_key_data_title *)databuf;
3963 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3964 RKD_TITLE_CPM_SHIFT;
3965 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3966 RKD_TITLE_CP_SEC_SHIFT;
3967 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3968 RKD_TITLE_CMGS_SHIFT;
3969 bcopy(title_data->title_key, authinfo->keychal,
3970 min(sizeof(title_data->title_key),
3971 sizeof(authinfo->keychal)));
3974 case DVD_REPORT_ASF: {
3975 struct scsi_report_key_data_asf *asf_data;
3977 asf_data = (struct scsi_report_key_data_asf *)databuf;
3979 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3982 case DVD_REPORT_RPC: {
3983 struct scsi_report_key_data_rpc *rpc_data;
3985 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3987 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3989 authinfo->vend_rsts =
3990 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3991 RKD_RPC_VENDOR_RESET_SHIFT;
3992 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3993 authinfo->region = rpc_data->region_mask;
3994 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3997 case DVD_INVALIDATE_AGID:
4000 /* This should be impossible, since we checked above */
4003 break; /* NOTREACHED */
4006 if (databuf != NULL)
4007 free(databuf, M_DEVBUF);
4009 xpt_release_ccb(ccb);
4015 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4025 ccb = cdgetccb(periph, /* priority */ 1);
4027 switch(authinfo->format) {
4028 case DVD_SEND_CHALLENGE: {
4029 struct scsi_report_key_data_challenge *challenge_data;
4031 length = sizeof(*challenge_data);
4033 challenge_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4035 databuf = (u_int8_t *)challenge_data;
4037 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4038 challenge_data->data_len);
4040 bcopy(authinfo->keychal, challenge_data->challenge_key,
4041 min(sizeof(authinfo->keychal),
4042 sizeof(challenge_data->challenge_key)));
4045 case DVD_SEND_KEY2: {
4046 struct scsi_report_key_data_key1_key2 *key2_data;
4048 length = sizeof(*key2_data);
4050 key2_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4052 databuf = (u_int8_t *)key2_data;
4054 scsi_ulto2b(length - sizeof(key2_data->data_len),
4055 key2_data->data_len);
4057 bcopy(authinfo->keychal, key2_data->key1,
4058 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4062 case DVD_SEND_RPC: {
4063 struct scsi_send_key_data_rpc *rpc_data;
4065 length = sizeof(*rpc_data);
4067 rpc_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4069 databuf = (u_int8_t *)rpc_data;
4071 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4072 rpc_data->data_len);
4074 rpc_data->region_code = authinfo->region;
4080 break; /* NOTREACHED */
4083 scsi_send_key(&ccb->csio,
4085 /* cbfcnp */ cddone,
4086 /* tag_action */ MSG_SIMPLE_Q_TAG,
4087 /* agid */ authinfo->agid,
4088 /* key_format */ authinfo->format,
4089 /* data_ptr */ databuf,
4090 /* dxfer_len */ length,
4091 /* sense_len */ SSD_FULL_SIZE,
4092 /* timeout */ 50000);
4094 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4095 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4099 if (databuf != NULL)
4100 free(databuf, M_DEVBUF);
4102 xpt_release_ccb(ccb);
4108 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4118 /* The address is reserved for many of the formats */
4121 ccb = cdgetccb(periph, /* priority */ 1);
4123 switch(dvdstruct->format) {
4124 case DVD_STRUCT_PHYSICAL:
4125 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4127 case DVD_STRUCT_COPYRIGHT:
4128 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4130 case DVD_STRUCT_DISCKEY:
4131 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4133 case DVD_STRUCT_BCA:
4134 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4136 case DVD_STRUCT_MANUFACT:
4137 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4139 case DVD_STRUCT_CMI:
4143 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4144 address = dvdstruct->address;
4146 break; /* NOTREACHED */
4147 case DVD_STRUCT_PROTDISCID:
4148 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4150 case DVD_STRUCT_DISCKEYBLOCK:
4151 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4153 case DVD_STRUCT_DDS:
4154 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4156 case DVD_STRUCT_MEDIUM_STAT:
4157 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4159 case DVD_STRUCT_SPARE_AREA:
4160 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4162 case DVD_STRUCT_RMD_LAST:
4166 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4167 address = dvdstruct->address;
4169 break; /* NOTREACHED */
4170 case DVD_STRUCT_RMD_RMA:
4174 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4175 address = dvdstruct->address;
4177 break; /* NOTREACHED */
4178 case DVD_STRUCT_PRERECORDED:
4179 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4181 case DVD_STRUCT_UNIQUEID:
4182 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4184 case DVD_STRUCT_DCB:
4188 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4189 address = dvdstruct->address;
4191 break; /* NOTREACHED */
4192 case DVD_STRUCT_LIST:
4194 * This is the maximum allocation length for the READ DVD
4195 * STRUCTURE command. There's nothing in the MMC3 spec
4196 * that indicates a limit in the amount of data that can
4197 * be returned from this call, other than the limits
4198 * imposed by the 2-byte length variables.
4205 break; /* NOTREACHED */
4209 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4214 scsi_read_dvd_structure(&ccb->csio,
4216 /* cbfcnp */ cddone,
4217 /* tag_action */ MSG_SIMPLE_Q_TAG,
4219 /* layer_number */ dvdstruct->layer_num,
4220 /* key_format */ dvdstruct->format,
4221 /* agid */ dvdstruct->agid,
4222 /* data_ptr */ databuf,
4223 /* dxfer_len */ length,
4224 /* sense_len */ SSD_FULL_SIZE,
4225 /* timeout */ 50000);
4227 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4228 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4233 switch(dvdstruct->format) {
4234 case DVD_STRUCT_PHYSICAL: {
4235 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4236 struct dvd_layer *outlayer;
4237 struct scsi_read_dvd_struct_data_physical *phys_data;
4240 (struct scsi_read_dvd_struct_data_physical *)databuf;
4241 inlayer = &phys_data->layer_desc;
4242 outlayer = (struct dvd_layer *)&dvdstruct->data;
4244 dvdstruct->length = sizeof(*inlayer);
4246 outlayer->book_type = (inlayer->book_type_version &
4247 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4248 outlayer->book_version = (inlayer->book_type_version &
4249 RDSD_BOOK_VERSION_MASK);
4250 outlayer->disc_size = (inlayer->disc_size_max_rate &
4251 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4252 outlayer->max_rate = (inlayer->disc_size_max_rate &
4253 RDSD_MAX_RATE_MASK);
4254 outlayer->nlayers = (inlayer->layer_info &
4255 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4256 outlayer->track_path = (inlayer->layer_info &
4257 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4258 outlayer->layer_type = (inlayer->layer_info &
4259 RDSD_LAYER_TYPE_MASK);
4260 outlayer->linear_density = (inlayer->density &
4261 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4262 outlayer->track_density = (inlayer->density &
4263 RDSD_TRACK_DENSITY_MASK);
4264 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4266 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4267 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4268 outlayer->end_sector_l0 =
4269 scsi_3btoul(inlayer->end_sector_layer0);
4272 case DVD_STRUCT_COPYRIGHT: {
4273 struct scsi_read_dvd_struct_data_copyright *copy_data;
4275 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4278 dvdstruct->cpst = copy_data->cps_type;
4279 dvdstruct->rmi = copy_data->region_info;
4280 dvdstruct->length = 0;
4286 * Tell the user what the overall length is, no matter
4287 * what we can actually fit in the data buffer.
4289 dvdstruct->length = length - ccb->csio.resid -
4290 sizeof(struct scsi_read_dvd_struct_data_header);
4293 * But only actually copy out the smaller of what we read
4294 * in or what the structure can take.
4296 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4298 min(sizeof(dvdstruct->data), dvdstruct->length));
4303 if (databuf != NULL)
4304 free(databuf, M_DEVBUF);
4306 xpt_release_ccb(ccb);
4312 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4313 void (*cbfcnp)(struct cam_periph *, union ccb *),
4314 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4315 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4316 u_int8_t sense_len, u_int32_t timeout)
4318 struct scsi_report_key *scsi_cmd;
4320 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4321 bzero(scsi_cmd, sizeof(*scsi_cmd));
4322 scsi_cmd->opcode = REPORT_KEY;
4323 scsi_ulto4b(lba, scsi_cmd->lba);
4324 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4325 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4326 (key_format & RK_KF_KEYFORMAT_MASK);
4331 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4333 /*data_ptr*/ data_ptr,
4334 /*dxfer_len*/ dxfer_len,
4341 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4342 void (*cbfcnp)(struct cam_periph *, union ccb *),
4343 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4344 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4347 struct scsi_send_key *scsi_cmd;
4349 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4350 bzero(scsi_cmd, sizeof(*scsi_cmd));
4351 scsi_cmd->opcode = SEND_KEY;
4353 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4354 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4355 (key_format & RK_KF_KEYFORMAT_MASK);
4360 /*flags*/ CAM_DIR_OUT,
4362 /*data_ptr*/ data_ptr,
4363 /*dxfer_len*/ dxfer_len,
4371 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4372 void (*cbfcnp)(struct cam_periph *, union ccb *),
4373 u_int8_t tag_action, u_int32_t address,
4374 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4375 u_int8_t *data_ptr, u_int32_t dxfer_len,
4376 u_int8_t sense_len, u_int32_t timeout)
4378 struct scsi_read_dvd_structure *scsi_cmd;
4380 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4381 bzero(scsi_cmd, sizeof(*scsi_cmd));
4382 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4384 scsi_ulto4b(address, scsi_cmd->address);
4385 scsi_cmd->layer_number = layer_number;
4386 scsi_cmd->format = format;
4387 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4388 /* The AGID is the top two bits of this byte */
4389 scsi_cmd->agid = agid << 6;
4394 /*flags*/ CAM_DIR_IN,
4396 /*data_ptr*/ data_ptr,
4397 /*dxfer_len*/ dxfer_len,