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.20 2006/02/17 19:17:42 dillon 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_bio 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 bio_queue_head bio_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;
409 struct ccb_setasync csa;
411 softc = (struct cd_softc *)periph->softc;
414 * De-register any async callbacks.
416 xpt_setup_ccb(&csa.ccb_h, periph->path,
418 csa.ccb_h.func_code = XPT_SASYNC_CB;
419 csa.event_enable = 0;
420 csa.callback = cdasync;
421 csa.callback_arg = periph;
422 xpt_action((union ccb *)&csa);
424 softc->flags |= CD_FLAG_INVALID;
427 * Although the oninvalidate() routines are always while in a
428 * critical section, we need to be in a critical section here to
429 * keep the buffer queue from being modified while we traverse it.
434 * Return all queued I/O with ENXIO.
435 * XXX Handle any transactions queued to the card
436 * with XPT_ABORT_CCB.
438 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
439 bioq_remove(&softc->bio_queue, q_bio);
440 q_bp = q_bio->bio_buf;
441 q_bp->b_resid = q_bp->b_bcount;
442 q_bp->b_error = ENXIO;
443 q_bp->b_flags |= B_ERROR;
449 * If this device is part of a changer, and it was scheduled
450 * to run, remove it from the run queue since we just nuked
451 * all of its scheduled I/O.
453 if ((softc->flags & CD_FLAG_CHANGER)
454 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
455 camq_remove(&softc->changer->devq, softc->pinfo.index);
457 xpt_print_path(periph->path);
458 printf("lost device\n");
462 cdcleanup(struct cam_periph *periph)
464 struct cd_softc *softc;
466 softc = (struct cd_softc *)periph->softc;
468 xpt_print_path(periph->path);
469 printf("removing device entry\n");
471 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
472 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
473 xpt_print_path(periph->path);
474 printf("can't remove sysctl context\n");
479 * In the queued, non-active case, the device in question
480 * has already been removed from the changer run queue. Since this
481 * device is active, we need to de-activate it, and schedule
482 * another device to run. (if there is another one to run)
484 if ((softc->flags & CD_FLAG_CHANGER)
485 && (softc->flags & CD_FLAG_ACTIVE)) {
488 * The purpose of the short timeout is soley to determine
489 * whether the current device has finished or not. Well,
490 * since we're removing the active device, we know that it
491 * is finished. So, get rid of the short timeout.
492 * Otherwise, if we're in the time period before the short
493 * timeout fires, and there are no other devices in the
494 * queue to run, there won't be any other device put in the
495 * active slot. i.e., when we call cdrunchangerqueue()
496 * below, it won't do anything. Then, when the short
497 * timeout fires, it'll look at the "current device", which
498 * we are free below, and possibly panic the kernel on a
499 * bogus pointer reference.
501 * The long timeout doesn't really matter, since we
502 * decrement the qfrozen_cnt to indicate that there is
503 * nothing in the active slot now. Therefore, there won't
504 * be any bogus pointer references there.
506 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
507 callout_stop(&softc->changer->short_handle);
508 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
510 softc->changer->devq.qfrozen_cnt--;
511 softc->changer->flags |= CHANGER_MANUAL_CALL;
512 cdrunchangerqueue(softc->changer);
516 * If we're removing the last device on the changer, go ahead and
517 * remove the changer device structure.
519 if ((softc->flags & CD_FLAG_CHANGER)
520 && (--softc->changer->num_devices == 0)) {
523 * Theoretically, there shouldn't be any timeouts left, but
524 * I'm not completely sure that that will be the case. So,
525 * it won't hurt to check and see if there are any left.
527 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
528 callout_stop(&softc->changer->long_handle);
529 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
532 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
533 callout_stop(&softc->changer->short_handle);
534 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
537 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
539 xpt_print_path(periph->path);
540 printf("removing changer entry\n");
541 free(softc->changer, M_DEVBUF);
544 devstat_remove_entry(&softc->device_stats);
545 cam_extend_release(cdperiphs, periph->unit_number);
546 if (softc->disk.d_rawdev) {
547 disk_destroy(&softc->disk);
549 free(softc, M_DEVBUF);
554 cdasync(void *callback_arg, u_int32_t code,
555 struct cam_path *path, void *arg)
557 struct cam_periph *periph;
559 periph = (struct cam_periph *)callback_arg;
561 case AC_FOUND_DEVICE:
563 struct ccb_getdev *cgd;
566 cgd = (struct ccb_getdev *)arg;
568 if (SID_TYPE(&cgd->inq_data) != T_CDROM
569 && SID_TYPE(&cgd->inq_data) != T_WORM)
573 * Allocate a peripheral instance for
574 * this device and start the probe
577 status = cam_periph_alloc(cdregister, cdoninvalidate,
579 "cd", CAM_PERIPH_BIO,
580 cgd->ccb_h.path, cdasync,
581 AC_FOUND_DEVICE, cgd);
583 if (status != CAM_REQ_CMP
584 && status != CAM_REQ_INPROG)
585 printf("cdasync: Unable to attach new device "
586 "due to status 0x%x\n", status);
593 struct cd_softc *softc;
594 struct ccb_hdr *ccbh;
596 softc = (struct cd_softc *)periph->softc;
599 * Don't fail on the expected unit attention
602 softc->flags |= CD_FLAG_RETRY_UA;
603 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
604 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
605 ccbh->ccb_state |= CD_CCB_RETRY_UA;
610 cam_periph_async(periph, code, path, arg);
616 cdsysctlinit(void *context, int pending)
618 struct cam_periph *periph;
619 struct cd_softc *softc;
620 char tmpstr[80], tmpstr2[80];
622 periph = (struct cam_periph *)context;
623 softc = (struct cd_softc *)periph->softc;
625 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
626 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
628 sysctl_ctx_init(&softc->sysctl_ctx);
629 softc->flags |= CD_FLAG_SCTX_INIT;
630 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
631 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
632 tmpstr2, CTLFLAG_RD, 0, tmpstr);
634 if (softc->sysctl_tree == NULL) {
635 printf("cdsysctlinit: unable to allocate sysctl tree\n");
640 * Now register the sysctl handler, so the user can the value on
643 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
644 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
645 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
650 * We have a handler function for this so we can check the values when the
651 * user sets them, instead of every time we look at them.
654 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
658 value = *(int *)arg1;
660 error = sysctl_handle_int(oidp, &value, 0, req);
662 if ((error != 0) || (req->newptr == NULL))
666 * The only real values we can have here are 6 or 10. I don't
667 * really forsee having 12 be an option at any time in the future.
668 * So if the user sets something less than or equal to 6, we'll set
669 * it to 6. If he sets something greater than 6, we'll set it to 10.
671 * I suppose we could just return an error here for the wrong values,
672 * but I don't think it's necessary to do so, as long as we can
673 * determine the user's intent without too much trouble.
680 *(int *)arg1 = value;
687 cdregister(struct cam_periph *periph, void *arg)
689 struct cd_softc *softc;
690 struct ccb_setasync csa;
691 struct ccb_pathinq cpi;
692 struct ccb_getdev *cgd;
696 cgd = (struct ccb_getdev *)arg;
697 if (periph == NULL) {
698 printf("cdregister: periph was NULL!!\n");
699 return(CAM_REQ_CMP_ERR);
702 printf("cdregister: no getdev CCB, can't register device\n");
703 return(CAM_REQ_CMP_ERR);
706 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
707 LIST_INIT(&softc->pending_ccbs);
708 STAILQ_INIT(&softc->mode_queue);
709 softc->state = CD_STATE_PROBE;
710 bioq_init(&softc->bio_queue);
711 if (SID_IS_REMOVABLE(&cgd->inq_data))
712 softc->flags |= CD_FLAG_DISC_REMOVABLE;
713 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
714 softc->flags |= CD_FLAG_TAGGED_QUEUING;
716 periph->softc = softc;
717 softc->periph = periph;
719 cam_extend_set(cdperiphs, periph->unit_number, periph);
722 * See if this device has any quirks.
724 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
725 (caddr_t)cd_quirk_table,
726 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
727 sizeof(*cd_quirk_table), scsi_inquiry_match);
730 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
732 softc->quirks = CD_Q_NONE;
734 /* Check if the SIM does not want 6 byte commands */
735 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
736 cpi.ccb_h.func_code = XPT_PATH_INQ;
737 xpt_action((union ccb *)&cpi);
738 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
739 softc->quirks |= CD_Q_10_BYTE_ONLY;
741 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
743 /* The default is 6 byte commands, unless quirked otherwise */
744 if (softc->quirks & CD_Q_10_BYTE_ONLY)
745 softc->minimum_command_size = 10;
747 softc->minimum_command_size = 6;
750 * Load the user's default, if any.
752 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
753 periph->unit_number);
754 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
756 /* 6 and 10 are the only permissible values here. */
757 if (softc->minimum_command_size < 6)
758 softc->minimum_command_size = 6;
759 else if (softc->minimum_command_size > 6)
760 softc->minimum_command_size = 10;
763 * We need to register the statistics structure for this device,
764 * but we don't have the blocksize yet for it. So, we register
765 * the structure and indicate that we don't have the blocksize
766 * yet. Unlike other SCSI peripheral drivers, we explicitly set
767 * the device type here to be CDROM, rather than just ORing in
768 * the device type. This is because this driver can attach to either
769 * CDROM or WORM devices, and we want this peripheral driver to
770 * show up in the devstat list as a CD peripheral driver, not a
771 * WORM peripheral driver. WORM drives will also have the WORM
772 * driver attached to them.
774 devstat_add_entry(&softc->device_stats, "cd",
775 periph->unit_number, 0,
776 DEVSTAT_BS_UNAVAILABLE,
777 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
778 DEVSTAT_PRIORITY_CD);
779 disk_create(periph->unit_number, &softc->disk,
780 DSO_ONESLICE | DSO_COMPATLABEL, &cd_cdevsw);
783 * Add an async callback so that we get
784 * notified if this device goes away.
786 xpt_setup_ccb(&csa.ccb_h, periph->path,
788 csa.ccb_h.func_code = XPT_SASYNC_CB;
789 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
790 csa.callback = cdasync;
791 csa.callback_arg = periph;
792 xpt_action((union ccb *)&csa);
795 * If the target lun is greater than 0, we most likely have a CD
796 * changer device. Check the quirk entries as well, though, just
797 * in case someone has a CD tower with one lun per drive or
798 * something like that. Also, if we know up front that a
799 * particular device is a changer, we can mark it as such starting
800 * with lun 0, instead of lun 1. It shouldn't be necessary to have
801 * a quirk entry to define something as a changer, however.
803 if (((cgd->ccb_h.target_lun > 0)
804 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
805 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
806 struct cdchanger *nchanger;
807 struct cam_periph *nperiph;
808 struct cam_path *path;
812 /* Set the changer flag in the current device's softc */
813 softc->flags |= CD_FLAG_CHANGER;
815 if (num_changers == 0)
816 STAILQ_INIT(&changerq);
819 * Now, look around for an existing changer device with the
820 * same path and target ID as the current device.
823 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
825 nchanger = STAILQ_NEXT(nchanger, changer_links)){
826 if ((nchanger->path_id == cgd->ccb_h.path_id)
827 && (nchanger->target_id == cgd->ccb_h.target_id)) {
834 * If we found a matching entry, just add this device to
835 * the list of devices on this changer.
838 struct chdevlist *chlunhead;
840 chlunhead = &nchanger->chluns;
843 * XXX KDM look at consolidating this code with the
844 * code below in a separate function.
848 * Create a path with lun id 0, and see if we can
849 * find a matching device
851 status = xpt_create_path(&path, /*periph*/ periph,
853 cgd->ccb_h.target_id, 0);
855 if ((status == CAM_REQ_CMP)
856 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
857 struct cd_softc *nsoftc;
859 nsoftc = (struct cd_softc *)nperiph->softc;
861 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
862 nsoftc->flags |= CD_FLAG_CHANGER;
863 nchanger->num_devices++;
864 if (camq_resize(&nchanger->devq,
865 nchanger->num_devices)!=CAM_REQ_CMP){
866 printf("cdregister: "
872 nsoftc->changer = nchanger;
873 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
875 STAILQ_INSERT_TAIL(&nchanger->chluns,
876 nsoftc,changer_links);
879 } else if (status == CAM_REQ_CMP)
882 printf("cdregister: unable to allocate path\n"
883 "cdregister: changer support may be "
887 nchanger->num_devices++;
889 softc->changer = nchanger;
890 softc->pinfo.index = CAM_UNQUEUED_INDEX;
892 if (camq_resize(&nchanger->devq,
893 nchanger->num_devices) != CAM_REQ_CMP) {
894 printf("cdregister: camq_resize "
895 "failed, changer support may "
899 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
902 * In this case, we don't already have an entry for this
903 * particular changer, so we need to create one, add it to
904 * the queue, and queue this device on the list for this
905 * changer. Before we queue this device, however, we need
906 * to search for lun id 0 on this target, and add it to the
907 * queue first, if it exists. (and if it hasn't already
908 * been marked as part of the changer.)
911 nchanger = malloc(sizeof(struct cdchanger),
912 M_DEVBUF, M_INTWAIT | M_ZERO);
913 callout_init(&nchanger->short_handle);
914 callout_init(&nchanger->long_handle);
915 if (camq_init(&nchanger->devq, 1) != 0) {
916 softc->flags &= ~CD_FLAG_CHANGER;
917 printf("cdregister: changer support "
924 nchanger->path_id = cgd->ccb_h.path_id;
925 nchanger->target_id = cgd->ccb_h.target_id;
927 /* this is superfluous, but it makes things clearer */
928 nchanger->num_devices = 0;
930 STAILQ_INIT(&nchanger->chluns);
932 STAILQ_INSERT_TAIL(&changerq, nchanger,
936 * Create a path with lun id 0, and see if we can
937 * find a matching device
939 status = xpt_create_path(&path, /*periph*/ periph,
941 cgd->ccb_h.target_id, 0);
944 * If we were able to allocate the path, and if we
945 * find a matching device and it isn't already
946 * marked as part of a changer, then we add it to
947 * the current changer.
949 if ((status == CAM_REQ_CMP)
950 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
951 && ((((struct cd_softc *)periph->softc)->flags &
952 CD_FLAG_CHANGER) == 0)) {
953 struct cd_softc *nsoftc;
955 nsoftc = (struct cd_softc *)nperiph->softc;
957 nsoftc->flags |= CD_FLAG_CHANGER;
958 nchanger->num_devices++;
959 if (camq_resize(&nchanger->devq,
960 nchanger->num_devices) != CAM_REQ_CMP) {
961 printf("cdregister: camq_resize "
962 "failed, changer support may "
965 nsoftc->changer = nchanger;
966 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
968 STAILQ_INSERT_TAIL(&nchanger->chluns,
969 nsoftc, changer_links);
971 } else if (status == CAM_REQ_CMP)
974 printf("cdregister: unable to allocate path\n"
975 "cdregister: changer support may be "
979 softc->changer = nchanger;
980 softc->pinfo.index = CAM_UNQUEUED_INDEX;
981 nchanger->num_devices++;
982 if (camq_resize(&nchanger->devq,
983 nchanger->num_devices) != CAM_REQ_CMP) {
984 printf("cdregister: camq_resize "
985 "failed, changer support may "
988 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
995 /* Lock this peripheral until we are setup */
997 cam_periph_lock(periph, 0);
999 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1000 xpt_schedule(periph, /*priority*/5);
1002 cdschedule(periph, /*priority*/ 5);
1004 return(CAM_REQ_CMP);
1008 cdopen(dev_t dev, int flags, int fmt, struct thread *td)
1010 struct cam_periph *periph;
1011 struct cd_softc *softc;
1015 periph = cam_extend_get(cdperiphs, unit);
1020 softc = (struct cd_softc *)periph->softc;
1023 * Grab a critical section and hold it until we lock the peripheral.
1026 if (softc->flags & CD_FLAG_INVALID) {
1031 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
1038 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1042 * Check for media, and set the appropriate flags. We don't bail
1043 * if we don't have media, but then we don't allow anything but the
1044 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1046 * XXX KDM for now, we do fail the open if we don't have media. We
1047 * can change this once we've figured out how to make the slice
1048 * code work well with media changing underneath it.
1050 error = cdcheckmedia(periph);
1053 softc->flags |= CD_FLAG_OPEN;
1055 cam_periph_unlock(periph);
1057 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1063 cdclose(dev_t dev, int flag, int fmt, struct thread *td)
1065 struct cam_periph *periph;
1066 struct cd_softc *softc;
1070 periph = cam_extend_get(cdperiphs, unit);
1074 softc = (struct cd_softc *)periph->softc;
1076 if ((error = cam_periph_lock(periph, 0)) != 0)
1079 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1080 cdprevent(periph, PR_ALLOW);
1083 * Unconditionally set the dsopen() flags back to their default
1086 softc->disk.d_dsflags &= ~DSO_NOLABELS;
1087 softc->disk.d_dsflags |= DSO_COMPATLABEL;
1090 * Since we're closing this CD, mark the blocksize as unavailable.
1091 * It will be marked as available when the CD is opened again.
1093 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1096 * We'll check the media and toc again at the next open().
1098 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1100 softc->flags &= ~CD_FLAG_OPEN;
1102 cam_periph_unlock(periph);
1103 cam_periph_release(periph);
1109 cdshorttimeout(void *arg)
1111 struct cdchanger *changer;
1115 changer = (struct cdchanger *)arg;
1117 /* Always clear the short timeout flag, since that's what we're in */
1118 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1121 * Check to see if there is any more pending or outstanding I/O for
1122 * this device. If not, move it out of the active slot.
1124 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1125 && (changer->cur_device->device_stats.busy_count == 0)) {
1126 changer->flags |= CHANGER_MANUAL_CALL;
1127 cdrunchangerqueue(changer);
1133 * This is a wrapper for xpt_schedule. It only applies to changers.
1136 cdschedule(struct cam_periph *periph, int priority)
1138 struct cd_softc *softc;
1142 softc = (struct cd_softc *)periph->softc;
1145 * If this device isn't currently queued, and if it isn't
1146 * the active device, then we queue this device and run the
1147 * changer queue if there is no timeout scheduled to do it.
1148 * If this device is the active device, just schedule it
1149 * to run again. If this device is queued, there should be
1150 * a timeout in place already that will make sure it runs.
1152 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1153 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1155 * We don't do anything with the priority here.
1156 * This is strictly a fifo queue.
1158 softc->pinfo.priority = 1;
1159 softc->pinfo.generation = ++softc->changer->devq.generation;
1160 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1163 * Since we just put a device in the changer queue,
1164 * check and see if there is a timeout scheduled for
1165 * this changer. If so, let the timeout handle
1166 * switching this device into the active slot. If
1167 * not, manually call the timeout routine to
1170 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1171 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1172 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1173 softc->changer->flags |= CHANGER_MANUAL_CALL;
1174 cdrunchangerqueue(softc->changer);
1176 } else if ((softc->flags & CD_FLAG_ACTIVE)
1177 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1178 xpt_schedule(periph, priority);
1184 cdrunchangerqueue(void *arg)
1186 struct cd_softc *softc;
1187 struct cdchanger *changer;
1188 int called_from_timeout;
1192 changer = (struct cdchanger *)arg;
1195 * If we have NOT been called from cdstrategy() or cddone(), and
1196 * instead from a timeout routine, go ahead and clear the
1199 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1200 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1201 called_from_timeout = 1;
1203 called_from_timeout = 0;
1205 /* Always clear the manual call flag */
1206 changer->flags &= ~CHANGER_MANUAL_CALL;
1208 /* nothing to do if the queue is empty */
1209 if (changer->devq.entries <= 0) {
1215 * If the changer queue is frozen, that means we have an active
1218 if (changer->devq.qfrozen_cnt > 0) {
1220 if (changer->cur_device->device_stats.busy_count > 0) {
1221 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1222 changer->cur_device->bufs_left =
1223 changer->cur_device->device_stats.busy_count;
1224 if (called_from_timeout) {
1225 callout_reset(&changer->long_handle,
1226 changer_max_busy_seconds * hz,
1227 cdrunchangerqueue, changer);
1228 changer->flags |= CHANGER_TIMEOUT_SCHED;
1235 * We always need to reset the frozen count and clear the
1238 changer->devq.qfrozen_cnt--;
1239 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1240 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1243 * Check to see whether the current device has any I/O left
1244 * to do. If so, requeue it at the end of the queue. If
1245 * not, there is no need to requeue it.
1247 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1249 changer->cur_device->pinfo.generation =
1250 ++changer->devq.generation;
1251 camq_insert(&changer->devq,
1252 (cam_pinfo *)changer->cur_device);
1256 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1258 changer->cur_device = softc;
1260 changer->devq.qfrozen_cnt++;
1261 softc->flags |= CD_FLAG_ACTIVE;
1263 /* Just in case this device is waiting */
1264 wakeup(&softc->changer);
1265 xpt_schedule(softc->periph, /*priority*/ 1);
1268 * Get rid of any pending timeouts, and set a flag to schedule new
1269 * ones so this device gets its full time quantum.
1271 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1272 callout_stop(&changer->long_handle);
1273 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1276 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1277 callout_stop(&changer->short_handle);
1278 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1282 * We need to schedule timeouts, but we only do this after the
1283 * first transaction has completed. This eliminates the changer
1286 changer->flags |= CHANGER_NEED_TIMEOUT;
1292 cdchangerschedule(struct cd_softc *softc)
1294 struct cdchanger *changer;
1298 changer = softc->changer;
1301 * If this is a changer, and this is the current device,
1302 * and this device has at least the minimum time quantum to
1303 * run, see if we can switch it out.
1305 if ((softc->flags & CD_FLAG_ACTIVE)
1306 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1307 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1309 * We try three things here. The first is that we
1310 * check to see whether the schedule on completion
1311 * flag is set. If it is, we decrement the number
1312 * of buffers left, and if it's zero, we reschedule.
1313 * Next, we check to see whether the pending buffer
1314 * queue is empty and whether there are no
1315 * outstanding transactions. If so, we reschedule.
1316 * Next, we see if the pending buffer queue is empty.
1317 * If it is, we set the number of buffers left to
1318 * the current active buffer count and set the
1319 * schedule on complete flag.
1321 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1322 if (--softc->bufs_left == 0) {
1323 softc->changer->flags |=
1324 CHANGER_MANUAL_CALL;
1325 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1326 cdrunchangerqueue(softc->changer);
1328 } else if ((bioq_first(&softc->bio_queue) == NULL)
1329 && (softc->device_stats.busy_count == 0)) {
1330 softc->changer->flags |= CHANGER_MANUAL_CALL;
1331 cdrunchangerqueue(softc->changer);
1333 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1334 && (softc->flags & CD_FLAG_ACTIVE)) {
1337 * Now that the first transaction to this
1338 * particular device has completed, we can go ahead
1339 * and schedule our timeouts.
1341 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1342 callout_reset(&changer->long_handle,
1343 changer_max_busy_seconds * hz,
1344 cdrunchangerqueue, changer);
1345 changer->flags |= CHANGER_TIMEOUT_SCHED;
1347 printf("cdchangerschedule: already have a long"
1350 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1351 callout_reset(&changer->short_handle,
1352 changer_min_busy_seconds * hz,
1353 cdshorttimeout, changer);
1354 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1356 printf("cdchangerschedule: already have a short "
1360 * We just scheduled timeouts, no need to schedule
1363 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1370 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1371 u_int32_t cam_flags,
1372 u_int32_t sense_flags),
1373 u_int32_t cam_flags, u_int32_t sense_flags)
1375 struct cd_softc *softc;
1376 struct cam_periph *periph;
1379 periph = xpt_path_periph(ccb->ccb_h.path);
1380 softc = (struct cd_softc *)periph->softc;
1382 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1383 &softc->device_stats);
1385 if (softc->flags & CD_FLAG_CHANGER)
1386 cdchangerschedule(softc);
1392 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1394 struct cd_softc *softc;
1396 softc = (struct cd_softc *)periph->softc;
1398 if (softc->flags & CD_FLAG_CHANGER) {
1402 * This should work the first time this device is woken up,
1403 * but just in case it doesn't, we use a while loop.
1405 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1407 * If this changer isn't already queued, queue it up.
1409 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1410 softc->pinfo.priority = 1;
1411 softc->pinfo.generation =
1412 ++softc->changer->devq.generation;
1413 camq_insert(&softc->changer->devq,
1414 (cam_pinfo *)softc);
1416 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1417 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1418 && ((softc->changer->flags
1419 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1420 softc->changer->flags |= CHANGER_MANUAL_CALL;
1421 cdrunchangerqueue(softc->changer);
1423 tsleep(&softc->changer, 0, "cgticb", 0);
1427 return(cam_periph_getccb(periph, priority));
1431 * Actually translate the requested transfer into one the physical driver
1432 * can understand. The transfer is described by a buf and will include
1433 * only one physical transfer.
1436 cdstrategy(dev_t dev, struct bio *bio)
1438 struct buf *bp = bio->bio_buf;
1439 struct cam_periph *periph;
1440 struct cd_softc *softc;
1445 periph = cam_extend_get(cdperiphs, unit);
1446 if (periph == NULL) {
1447 bp->b_error = ENXIO;
1451 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1453 softc = (struct cd_softc *)periph->softc;
1456 * Mask interrupts so that the pack cannot be invalidated until
1457 * after we are in the queue. Otherwise, we might not properly
1458 * clean up one of the buffers.
1463 * If the device has been made invalid, error out
1465 if ((softc->flags & CD_FLAG_INVALID)) {
1467 bp->b_error = ENXIO;
1472 * If we don't have valid media, look for it before trying to
1475 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1478 error = cdcheckmedia(periph);
1481 bp->b_error = error;
1487 * Place it in the queue of disk activities for this disk
1489 bioqdisksort(&softc->bio_queue, bio);
1494 * Schedule ourselves for performing the work. We do things
1495 * differently for changers.
1497 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1498 xpt_schedule(periph, /* XXX priority */1);
1500 cdschedule(periph, /* priority */ 1);
1504 bp->b_flags |= B_ERROR;
1506 * Correctly set the buf to indicate a completed xfer
1508 bp->b_resid = bp->b_bcount;
1513 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1515 struct cd_softc *softc;
1518 struct ccb_scsiio *csio;
1519 struct scsi_read_capacity_data *rcap;
1521 softc = (struct cd_softc *)periph->softc;
1523 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1525 switch (softc->state) {
1526 case CD_STATE_NORMAL:
1529 bio = bioq_first(&softc->bio_queue);
1530 if (periph->immediate_priority <= periph->pinfo.priority) {
1531 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1533 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1535 periph->immediate_priority = CAM_PRIORITY_NONE;
1537 wakeup(&periph->ccb_list);
1538 } else if (bio == NULL) {
1540 xpt_release_ccb(start_ccb);
1543 bioq_remove(&softc->bio_queue, bio);
1545 devstat_start_transaction(&softc->device_stats);
1547 scsi_read_write(&start_ccb->csio,
1549 /* cbfcnp */ cddone,
1550 (bp->b_flags & B_ORDERED) != 0 ?
1553 /* read */bp->b_flags & B_READ,
1555 /* minimum_cmd_size */ 10,
1556 /* lba */ bio->bio_blkno,
1557 bp->b_bcount / softc->params.blksize,
1558 /* data_ptr */ bp->b_data,
1559 /* dxfer_len */ bp->b_bcount,
1560 /* sense_len */ SSD_FULL_SIZE,
1561 /* timeout */ 30000);
1562 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1566 * Block out any asyncronous callbacks
1567 * while we touch the pending ccb list.
1569 LIST_INSERT_HEAD(&softc->pending_ccbs,
1570 &start_ccb->ccb_h, periph_links.le);
1572 /* We expect a unit attention from this device */
1573 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1574 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1575 softc->flags &= ~CD_FLAG_RETRY_UA;
1578 start_ccb->ccb_h.ccb_bio = bio;
1579 bio = bioq_first(&softc->bio_queue);
1582 xpt_action(start_ccb);
1585 /* Have more work to do, so ensure we stay scheduled */
1586 xpt_schedule(periph, /* XXX priority */1);
1590 case CD_STATE_PROBE:
1593 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT);
1594 csio = &start_ccb->csio;
1595 scsi_read_capacity(csio,
1602 start_ccb->ccb_h.ccb_bio = NULL;
1603 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1604 xpt_action(start_ccb);
1611 cddone(struct cam_periph *periph, union ccb *done_ccb)
1613 struct cd_softc *softc;
1614 struct ccb_scsiio *csio;
1616 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1618 softc = (struct cd_softc *)periph->softc;
1619 csio = &done_ccb->csio;
1621 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1622 case CD_CCB_BUFFER_IO:
1628 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1632 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1635 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1640 /* Retry selection timeouts */
1641 sf |= SF_RETRY_SELTO;
1643 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) {
1645 * A retry was scheuled, so
1656 xpt_print_path(periph->path);
1657 printf("cddone: got error %#x back\n", error);
1659 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1660 bioq_remove(&softc->bio_queue, q_bio);
1661 q_bp = q_bio->bio_buf;
1662 q_bp->b_resid = q_bp->b_bcount;
1663 q_bp->b_error = EIO;
1664 q_bp->b_flags |= B_ERROR;
1668 bp->b_resid = bp->b_bcount;
1669 bp->b_error = error;
1670 bp->b_flags |= B_ERROR;
1671 cam_release_devq(done_ccb->ccb_h.path,
1675 /*getcount_only*/0);
1678 bp->b_resid = csio->resid;
1680 if (bp->b_resid != 0) {
1681 /* Short transfer ??? */
1682 bp->b_flags |= B_ERROR;
1687 * Block out any asyncronous callbacks
1688 * while we touch the pending ccb list.
1691 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1694 if (softc->flags & CD_FLAG_CHANGER)
1695 cdchangerschedule(softc);
1697 devstat_end_transaction_buf(&softc->device_stats, bp);
1703 struct scsi_read_capacity_data *rdcap;
1704 char announce_buf[120]; /*
1705 * Currently (9/30/97) the
1706 * longest possible announce
1707 * buffer is 108 bytes, for the
1708 * first error case below.
1709 * That is 39 bytes for the
1710 * basic string, 16 bytes for the
1711 * biggest sense key (hardware
1712 * error), 52 bytes for the
1713 * text of the largest sense
1714 * qualifier valid for a CDROM,
1715 * (0x72, 0x03 or 0x04,
1716 * 0x03), and one byte for the
1717 * null terminating character.
1718 * To allow for longer strings,
1719 * the announce buffer is 120
1722 struct cd_params *cdp;
1724 cdp = &softc->params;
1726 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1728 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1729 cdp->blksize = scsi_4btoul (rdcap->length);
1731 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1733 snprintf(announce_buf, sizeof(announce_buf),
1734 "cd present [%lu x %lu byte records]",
1735 cdp->disksize, (u_long)cdp->blksize);
1740 * Retry any UNIT ATTENTION type errors. They
1741 * are expected at boot.
1743 error = cderror(done_ccb, 0, SF_RETRY_UA |
1744 SF_NO_PRINT | SF_RETRY_SELTO);
1745 if (error == ERESTART) {
1747 * A retry was scheuled, so
1751 } else if (error != 0) {
1753 struct scsi_sense_data *sense;
1755 int sense_key, error_code;
1758 struct ccb_getdev cgd;
1760 /* Don't wedge this device's queue */
1761 cam_release_devq(done_ccb->ccb_h.path,
1765 /*getcount_only*/0);
1767 status = done_ccb->ccb_h.status;
1769 xpt_setup_ccb(&cgd.ccb_h,
1770 done_ccb->ccb_h.path,
1772 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1773 xpt_action((union ccb *)&cgd);
1775 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1776 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1777 || ((status & CAM_AUTOSNS_VALID) == 0))
1783 sense = &csio->sense_data;
1784 scsi_extract_sense(sense, &error_code,
1789 * Attach to anything that claims to be a
1790 * CDROM or WORM device, as long as it
1791 * doesn't return a "Logical unit not
1792 * supported" (0x25) error.
1794 if ((have_sense) && (asc != 0x25)
1795 && (error_code == SSD_CURRENT_ERROR))
1796 snprintf(announce_buf,
1797 sizeof(announce_buf),
1798 "Attempt to query device "
1799 "size failed: %s, %s",
1800 scsi_sense_key_text[sense_key],
1801 scsi_sense_desc(asc,ascq,
1803 else if ((have_sense == 0)
1804 && ((status & CAM_STATUS_MASK) ==
1805 CAM_SCSI_STATUS_ERROR)
1806 && (csio->scsi_status ==
1807 SCSI_STATUS_BUSY)) {
1808 snprintf(announce_buf,
1809 sizeof(announce_buf),
1810 "Attempt to query device "
1811 "size failed: SCSI status: BUSY");
1812 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1814 * We only print out an error for
1815 * CDROM type devices. For WORM
1816 * devices, we don't print out an
1817 * error since a few WORM devices
1818 * don't support CDROM commands.
1819 * If we have sense information, go
1820 * ahead and print it out.
1821 * Otherwise, just say that we
1826 * Just print out the error, not
1827 * the full probe message, when we
1834 xpt_print_path(periph->path);
1835 printf("got CAM status %#x\n",
1836 done_ccb->ccb_h.status);
1838 xpt_print_path(periph->path);
1839 printf("fatal error, failed"
1840 " to attach to device\n");
1843 * Invalidate this peripheral.
1845 cam_periph_invalidate(periph);
1847 announce_buf[0] = '\0';
1851 * Invalidate this peripheral.
1853 cam_periph_invalidate(periph);
1854 announce_buf[0] = '\0';
1858 free(rdcap, M_TEMP);
1859 if (announce_buf[0] != '\0') {
1860 xpt_announce_periph(periph, announce_buf);
1861 if (softc->flags & CD_FLAG_CHANGER)
1862 cdchangerschedule(softc);
1864 * Create our sysctl variables, now that we know
1865 * we have successfully attached.
1867 taskqueue_enqueue(taskqueue_thread[mycpuid],
1868 &softc->sysctl_task);
1870 softc->state = CD_STATE_NORMAL;
1872 * Since our peripheral may be invalidated by an error
1873 * above or an external event, we must release our CCB
1874 * before releasing the probe lock on the peripheral.
1875 * The peripheral will only go away once the last lock
1876 * is removed, and we need it around for the CCB release
1879 xpt_release_ccb(done_ccb);
1880 cam_periph_unlock(periph);
1883 case CD_CCB_WAITING:
1885 /* Caller will release the CCB */
1886 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1887 ("trying to wakeup ccbwait\n"));
1889 wakeup(&done_ccb->ccb_h.cbfcnp);
1895 xpt_release_ccb(done_ccb);
1898 static union cd_pages *
1899 cdgetpage(struct cd_mode_params *mode_params)
1901 union cd_pages *page;
1903 if (mode_params->cdb_size == 10)
1904 page = (union cd_pages *)find_mode_page_10(
1905 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1907 page = (union cd_pages *)find_mode_page_6(
1908 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1914 cdgetpagesize(int page_num)
1918 for (i = 0; i < (sizeof(cd_page_size_table)/
1919 sizeof(cd_page_size_table[0])); i++) {
1920 if (cd_page_size_table[i].page == page_num)
1921 return (cd_page_size_table[i].page_size);
1927 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1930 struct cam_periph *periph;
1931 struct cd_softc *softc;
1936 periph = cam_extend_get(cdperiphs, unit);
1940 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1942 softc = (struct cd_softc *)periph->softc;
1944 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1945 ("trying to do ioctl %#lx\n", cmd));
1947 error = cam_periph_lock(periph, PCATCH);
1953 * If we don't have media loaded, check for it. If still don't
1954 * have media loaded, we can only do a load or eject.
1956 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1957 && ((cmd != CDIOCCLOSE)
1958 && (cmd != CDIOCEJECT))) {
1959 error = cdcheckmedia(periph);
1961 cam_periph_unlock(periph);
1968 case CDIOCPLAYTRACKS:
1970 struct ioc_play_track *args
1971 = (struct ioc_play_track *) addr;
1972 struct cd_mode_params params;
1973 union cd_pages *page;
1975 params.alloc_len = sizeof(union cd_mode_data_6_10);
1976 params.mode_buf = malloc(params.alloc_len, M_TEMP,
1979 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1980 ("trying to do CDIOCPLAYTRACKS\n"));
1982 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1984 free(params.mode_buf, M_TEMP);
1987 page = cdgetpage(¶ms);
1989 page->audio.flags &= ~CD_PA_SOTC;
1990 page->audio.flags |= CD_PA_IMMED;
1991 error = cdsetmode(periph, ¶ms);
1992 free(params.mode_buf, M_TEMP);
1997 * This was originally implemented with the PLAY
1998 * AUDIO TRACK INDEX command, but that command was
1999 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2000 * drives support it but ATAPI and ATAPI-derivative
2001 * drives don't seem to support it. So we keep a
2002 * cache of the table of contents and translate
2003 * track numbers to MSF format.
2005 if (softc->flags & CD_FLAG_VALID_TOC) {
2006 union msf_lba *sentry, *eentry;
2009 if (args->end_track <
2010 softc->toc.header.ending_track + 1)
2012 if (args->end_track >
2013 softc->toc.header.ending_track + 1)
2015 softc->toc.header.ending_track + 1;
2016 st = args->start_track -
2017 softc->toc.header.starting_track;
2018 et = args->end_track -
2019 softc->toc.header.starting_track;
2022 || (st > (softc->toc.header.ending_track -
2023 softc->toc.header.starting_track))) {
2027 sentry = &softc->toc.entries[st].addr;
2028 eentry = &softc->toc.entries[et].addr;
2029 error = cdplaymsf(periph,
2038 * If we don't have a valid TOC, try the
2039 * play track index command. It is part of
2040 * the SCSI-2 spec, but was removed in the
2041 * MMC specs. ATAPI and ATAPI-derived
2042 * drives don't support it.
2044 if (softc->quirks & CD_Q_BCD_TRACKS) {
2046 bin2bcd(args->start_track);
2048 bin2bcd(args->end_track);
2050 error = cdplaytracks(periph,
2060 struct ioc_play_msf *args
2061 = (struct ioc_play_msf *) addr;
2062 struct cd_mode_params params;
2063 union cd_pages *page;
2065 params.alloc_len = sizeof(union cd_mode_data_6_10);
2066 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2069 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2070 ("trying to do CDIOCPLAYMSF\n"));
2072 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2074 free(params.mode_buf, M_TEMP);
2077 page = cdgetpage(¶ms);
2079 page->audio.flags &= ~CD_PA_SOTC;
2080 page->audio.flags |= CD_PA_IMMED;
2081 error = cdsetmode(periph, ¶ms);
2082 free(params.mode_buf, M_TEMP);
2085 error = cdplaymsf(periph,
2094 case CDIOCPLAYBLOCKS:
2096 struct ioc_play_blocks *args
2097 = (struct ioc_play_blocks *) addr;
2098 struct cd_mode_params params;
2099 union cd_pages *page;
2101 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2102 ("trying to do CDIOCPLAYBLOCKS\n"));
2104 params.alloc_len = sizeof(union cd_mode_data_6_10);
2105 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2108 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2110 free(params.mode_buf, M_TEMP);
2113 page = cdgetpage(¶ms);
2115 page->audio.flags &= ~CD_PA_SOTC;
2116 page->audio.flags |= CD_PA_IMMED;
2117 error = cdsetmode(periph, ¶ms);
2118 free(params.mode_buf, M_TEMP);
2121 error = cdplay(periph, args->blk, args->len);
2124 case CDIOCREADSUBCHANNEL:
2126 struct ioc_read_subchannel *args
2127 = (struct ioc_read_subchannel *) addr;
2128 struct cd_sub_channel_info *data;
2129 u_int32_t len = args->data_len;
2131 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2132 ("trying to do CDIOCREADSUBCHANNEL\n"));
2134 data = malloc(sizeof(struct cd_sub_channel_info),
2137 if ((len > sizeof(struct cd_sub_channel_info)) ||
2138 (len < sizeof(struct cd_sub_channel_header))) {
2140 "scsi_cd: cdioctl: "
2141 "cdioreadsubchannel: error, len=%d\n",
2148 if (softc->quirks & CD_Q_BCD_TRACKS)
2149 args->track = bin2bcd(args->track);
2151 error = cdreadsubchannel(periph, args->address_format,
2152 args->data_format, args->track, data, len);
2158 if (softc->quirks & CD_Q_BCD_TRACKS)
2159 data->what.track_info.track_number =
2160 bcd2bin(data->what.track_info.track_number);
2161 len = min(len, ((data->header.data_len[0] << 8) +
2162 data->header.data_len[1] +
2163 sizeof(struct cd_sub_channel_header)));
2164 if (copyout(data, args->data, len) != 0) {
2171 case CDIOREADTOCHEADER:
2173 struct ioc_toc_header *th;
2175 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2176 ("trying to do CDIOREADTOCHEADER\n"));
2178 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
2180 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2181 sizeof (*th), /*sense_flags*/0);
2186 if (softc->quirks & CD_Q_BCD_TRACKS) {
2187 /* we are going to have to convert the BCD
2188 * encoding on the cd to what is expected
2190 th->starting_track =
2191 bcd2bin(th->starting_track);
2192 th->ending_track = bcd2bin(th->ending_track);
2194 th->len = ntohs(th->len);
2195 bcopy(th, addr, sizeof(*th));
2199 case CDIOREADTOCENTRYS:
2201 struct cd_tocdata *data;
2202 struct cd_toc_single *lead;
2203 struct ioc_read_toc_entry *te =
2204 (struct ioc_read_toc_entry *) addr;
2205 struct ioc_toc_header *th;
2206 u_int32_t len, readlen, idx, num;
2207 u_int32_t starting_track = te->starting_track;
2209 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2210 ("trying to do CDIOREADTOCENTRYS\n"));
2212 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2213 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK);
2215 if (te->data_len < sizeof(struct cd_toc_entry)
2216 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2217 || (te->address_format != CD_MSF_FORMAT
2218 && te->address_format != CD_LBA_FORMAT)) {
2220 printf("scsi_cd: error in readtocentries, "
2221 "returning EINVAL\n");
2228 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2229 sizeof (*th), /*sense_flags*/0);
2236 if (softc->quirks & CD_Q_BCD_TRACKS) {
2237 /* we are going to have to convert the BCD
2238 * encoding on the cd to what is expected
2240 th->starting_track =
2241 bcd2bin(th->starting_track);
2242 th->ending_track = bcd2bin(th->ending_track);
2245 if (starting_track == 0)
2246 starting_track = th->starting_track;
2247 else if (starting_track == LEADOUT)
2248 starting_track = th->ending_track + 1;
2249 else if (starting_track < th->starting_track ||
2250 starting_track > th->ending_track + 1) {
2251 printf("scsi_cd: error in readtocentries, "
2252 "returning EINVAL\n");
2259 /* calculate reading length without leadout entry */
2260 readlen = (th->ending_track - starting_track + 1) *
2261 sizeof(struct cd_toc_entry);
2263 /* and with leadout entry */
2264 len = readlen + sizeof(struct cd_toc_entry);
2265 if (te->data_len < len) {
2270 if (len > sizeof(data->entries)) {
2271 printf("scsi_cd: error in readtocentries, "
2272 "returning EINVAL\n");
2278 num = len / sizeof(struct cd_toc_entry);
2281 error = cdreadtoc(periph, te->address_format,
2284 readlen + sizeof (*th),
2293 /* make leadout entry if needed */
2294 idx = starting_track + num - 1;
2295 if (softc->quirks & CD_Q_BCD_TRACKS)
2296 th->ending_track = bcd2bin(th->ending_track);
2297 if (idx == th->ending_track + 1) {
2298 error = cdreadtoc(periph, te->address_format,
2299 LEADOUT, (u_int8_t *)lead,
2307 data->entries[idx - starting_track] =
2310 if (softc->quirks & CD_Q_BCD_TRACKS) {
2311 for (idx = 0; idx < num - 1; idx++) {
2312 data->entries[idx].track =
2313 bcd2bin(data->entries[idx].track);
2317 error = copyout(data->entries, te->data, len);
2322 case CDIOREADTOCENTRY:
2324 struct cd_toc_single *data;
2325 struct ioc_read_toc_single_entry *te =
2326 (struct ioc_read_toc_single_entry *) addr;
2327 struct ioc_toc_header *th;
2330 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2331 ("trying to do CDIOREADTOCENTRY\n"));
2333 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2335 if (te->address_format != CD_MSF_FORMAT
2336 && te->address_format != CD_LBA_FORMAT) {
2337 printf("error in readtocentry, "
2338 " returning EINVAL\n");
2345 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2346 sizeof (*th), /*sense_flags*/0);
2352 if (softc->quirks & CD_Q_BCD_TRACKS) {
2353 /* we are going to have to convert the BCD
2354 * encoding on the cd to what is expected
2356 th->starting_track =
2357 bcd2bin(th->starting_track);
2358 th->ending_track = bcd2bin(th->ending_track);
2362 track = th->starting_track;
2363 else if (track == LEADOUT)
2365 else if (track < th->starting_track ||
2366 track > th->ending_track + 1) {
2367 printf("error in readtocentry, "
2368 " returning EINVAL\n");
2374 error = cdreadtoc(periph, te->address_format, track,
2375 (u_int8_t *)data, sizeof(*data),
2382 if (softc->quirks & CD_Q_BCD_TRACKS)
2383 data->entry.track = bcd2bin(data->entry.track);
2384 bcopy(&data->entry, &te->entry,
2385 sizeof(struct cd_toc_entry));
2391 struct ioc_patch *arg = (struct ioc_patch *)addr;
2392 struct cd_mode_params params;
2393 union cd_pages *page;
2395 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2396 ("trying to do CDIOCSETPATCH\n"));
2398 params.alloc_len = sizeof(union cd_mode_data_6_10);
2399 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2401 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2403 free(params.mode_buf, M_TEMP);
2406 page = cdgetpage(¶ms);
2408 page->audio.port[LEFT_PORT].channels =
2410 page->audio.port[RIGHT_PORT].channels =
2412 page->audio.port[2].channels = arg->patch[2];
2413 page->audio.port[3].channels = arg->patch[3];
2414 error = cdsetmode(periph, ¶ms);
2415 free(params.mode_buf, M_TEMP);
2420 struct ioc_vol *arg = (struct ioc_vol *) addr;
2421 struct cd_mode_params params;
2422 union cd_pages *page;
2424 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2425 ("trying to do CDIOCGETVOL\n"));
2427 params.alloc_len = sizeof(union cd_mode_data_6_10);
2428 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2430 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2432 free(params.mode_buf, M_TEMP);
2435 page = cdgetpage(¶ms);
2437 arg->vol[LEFT_PORT] =
2438 page->audio.port[LEFT_PORT].volume;
2439 arg->vol[RIGHT_PORT] =
2440 page->audio.port[RIGHT_PORT].volume;
2441 arg->vol[2] = page->audio.port[2].volume;
2442 arg->vol[3] = page->audio.port[3].volume;
2443 free(params.mode_buf, M_TEMP);
2448 struct ioc_vol *arg = (struct ioc_vol *) addr;
2449 struct cd_mode_params params;
2450 union cd_pages *page;
2452 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2453 ("trying to do CDIOCSETVOL\n"));
2455 params.alloc_len = sizeof(union cd_mode_data_6_10);
2456 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2458 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2460 free(params.mode_buf, M_TEMP);
2463 page = cdgetpage(¶ms);
2465 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2466 page->audio.port[LEFT_PORT].volume =
2467 arg->vol[LEFT_PORT];
2468 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2469 page->audio.port[RIGHT_PORT].volume =
2470 arg->vol[RIGHT_PORT];
2471 page->audio.port[2].volume = arg->vol[2];
2472 page->audio.port[3].volume = arg->vol[3];
2473 error = cdsetmode(periph, ¶ms);
2474 free(params.mode_buf, M_TEMP);
2479 struct cd_mode_params params;
2480 union cd_pages *page;
2482 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2483 ("trying to do CDIOCSETMONO\n"));
2485 params.alloc_len = sizeof(union cd_mode_data_6_10);
2486 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2488 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2490 free(params.mode_buf, M_TEMP);
2493 page = cdgetpage(¶ms);
2495 page->audio.port[LEFT_PORT].channels =
2496 LEFT_CHANNEL | RIGHT_CHANNEL;
2497 page->audio.port[RIGHT_PORT].channels =
2498 LEFT_CHANNEL | RIGHT_CHANNEL;
2499 page->audio.port[2].channels = 0;
2500 page->audio.port[3].channels = 0;
2501 error = cdsetmode(periph, ¶ms);
2502 free(params.mode_buf, M_TEMP);
2505 case CDIOCSETSTEREO:
2507 struct cd_mode_params params;
2508 union cd_pages *page;
2510 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2511 ("trying to do CDIOCSETSTEREO\n"));
2513 params.alloc_len = sizeof(union cd_mode_data_6_10);
2514 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2516 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2518 free(params.mode_buf, M_TEMP);
2521 page = cdgetpage(¶ms);
2523 page->audio.port[LEFT_PORT].channels =
2525 page->audio.port[RIGHT_PORT].channels =
2527 page->audio.port[2].channels = 0;
2528 page->audio.port[3].channels = 0;
2529 error = cdsetmode(periph, ¶ms);
2530 free(params.mode_buf, M_TEMP);
2535 struct cd_mode_params params;
2536 union cd_pages *page;
2538 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2539 ("trying to do CDIOCSETMUTE\n"));
2541 params.alloc_len = sizeof(union cd_mode_data_6_10);
2542 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2544 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2546 free(¶ms, M_TEMP);
2549 page = cdgetpage(¶ms);
2551 page->audio.port[LEFT_PORT].channels = 0;
2552 page->audio.port[RIGHT_PORT].channels = 0;
2553 page->audio.port[2].channels = 0;
2554 page->audio.port[3].channels = 0;
2555 error = cdsetmode(periph, ¶ms);
2556 free(params.mode_buf, M_TEMP);
2561 struct cd_mode_params params;
2562 union cd_pages *page;
2564 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2565 ("trying to do CDIOCSETLEFT\n"));
2567 params.alloc_len = sizeof(union cd_mode_data_6_10);
2568 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2571 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2573 free(params.mode_buf, M_TEMP);
2576 page = cdgetpage(¶ms);
2578 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2579 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2580 page->audio.port[2].channels = 0;
2581 page->audio.port[3].channels = 0;
2582 error = cdsetmode(periph, ¶ms);
2583 free(params.mode_buf, M_TEMP);
2588 struct cd_mode_params params;
2589 union cd_pages *page;
2591 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2592 ("trying to do CDIOCSETRIGHT\n"));
2594 params.alloc_len = sizeof(union cd_mode_data_6_10);
2595 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2598 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2600 free(params.mode_buf, M_TEMP);
2603 page = cdgetpage(¶ms);
2605 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2606 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2607 page->audio.port[2].channels = 0;
2608 page->audio.port[3].channels = 0;
2609 error = cdsetmode(periph, ¶ms);
2610 free(params.mode_buf, M_TEMP);
2614 error = cdpause(periph, 1);
2617 error = cdpause(periph, 0);
2620 error = cdstartunit(periph, 0);
2623 error = cdstartunit(periph, 1);
2630 * The user successfully closed the tray, run
2631 * cdcheckmedia() again so we can re-sync the disklabel
2634 cdcheckmedia(periph);
2638 error = cdstopunit(periph, 0);
2641 error = cdstopunit(periph, 1);
2648 * Since we've successfully ejected the media, run
2649 * cdcheckmedia() again so we re-sync the disklabel
2652 cdcheckmedia(periph);
2656 cdprevent(periph, PR_ALLOW);
2659 cdprevent(periph, PR_PREVENT);
2662 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2666 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2670 /* return (cd_reset(periph)); */
2673 case CDRIOCREADSPEED:
2674 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2676 case CDRIOCWRITESPEED:
2677 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2680 case DVDIOCREPORTKEY: {
2681 struct dvd_authinfo *authinfo;
2683 authinfo = (struct dvd_authinfo *)addr;
2685 if (cmd == DVDIOCREPORTKEY)
2686 error = cdreportkey(periph, authinfo);
2688 error = cdsendkey(periph, authinfo);
2691 case DVDIOCREADSTRUCTURE: {
2692 struct dvd_struct *dvdstruct;
2694 dvdstruct = (struct dvd_struct *)addr;
2696 error = cdreaddvdstructure(periph, dvdstruct);
2701 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2705 cam_periph_unlock(periph);
2707 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2713 cdprevent(struct cam_periph *periph, int action)
2716 struct cd_softc *softc;
2719 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2721 softc = (struct cd_softc *)periph->softc;
2723 if (((action == PR_ALLOW)
2724 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2725 || ((action == PR_PREVENT)
2726 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2730 ccb = cdgetccb(periph, /* priority */ 1);
2732 scsi_prevent(&ccb->csio,
2738 /* timeout */60000);
2740 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
2741 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
2743 xpt_release_ccb(ccb);
2746 if (action == PR_ALLOW)
2747 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2749 softc->flags |= CD_FLAG_DISC_LOCKED;
2754 cdcheckmedia(struct cam_periph *periph)
2756 struct cd_softc *softc;
2757 struct ioc_toc_header *toch;
2758 struct cd_toc_single leadout;
2759 struct ccb_getdev cgd;
2760 u_int32_t size, toclen;
2761 int error, num_entries, cdindex;
2762 int first_track_audio;
2763 struct disklabel *label;
2765 softc = (struct cd_softc *)periph->softc;
2767 first_track_audio = -1;
2770 cdprevent(periph, PR_PREVENT);
2773 * Build prototype label for whole disk.
2774 * Should take information about different data tracks from the
2775 * TOC and put it in the partition table.
2777 label = &softc->disk.d_label;
2778 bzero(label, sizeof(*label));
2779 label->d_type = DTYPE_SCSI;
2782 * Grab the inquiry data to get the vendor and product names.
2783 * Put them in the typename and packname for the label.
2785 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2786 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2787 xpt_action((union ccb *)&cgd);
2789 strncpy(label->d_typename, cgd.inq_data.vendor,
2790 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2791 strncpy(label->d_packname, cgd.inq_data.product,
2792 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2794 label->d_flags = D_REMOVABLE;
2796 * Make partition 'a' cover the whole disk. This is a temporary
2797 * compatibility hack. The 'a' partition should not exist, so
2798 * the slice code won't create it. The slice code will make
2799 * partition (RAW_PART + 'a') cover the whole disk and fill in
2800 * some more defaults.
2802 label->d_partitions[0].p_size = label->d_secperunit;
2803 label->d_partitions[0].p_fstype = FS_OTHER;
2806 * Default to not reading the disklabel off the disk, until we can
2807 * verify that we have media, that we have a table of contents, and
2808 * that the first track is a data track (which could theoretically
2809 * contain a disklabel).
2811 softc->disk.d_dsflags &= ~DSO_COMPATLABEL;
2812 softc->disk.d_dsflags |= DSO_NOLABELS;
2815 * Clear the valid media and TOC flags until we've verified that we
2818 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2821 * Get the disc size and block size. If we can't get it, we don't
2822 * have media, most likely.
2824 if ((error = cdsize(periph, &size)) != 0) {
2826 * Set a bogus sector size, so the slice code won't try to
2827 * divide by 0 and panic the kernel.
2829 label->d_secsize = 2048;
2831 label->d_secperunit = 0;
2834 * XXX KDM is this a good idea? Seems to cause more
2837 if (softc->flags & CD_FLAG_OPEN) {
2843 * We don't bother checking the return value here,
2844 * since we already have an error...
2846 dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2847 /*data*/(caddr_t)&force, /*flags*/ 0,
2848 &softc->disk.d_slice);
2852 * Tell devstat(9) we don't have a blocksize.
2854 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2856 cdprevent(periph, PR_ALLOW);
2861 label->d_secsize = softc->params.blksize;
2862 label->d_secperunit = softc->params.disksize;
2865 * Force a re-sync of slice information, like the blocksize,
2866 * now that we know it. It isn't pretty...but according to
2867 * Bruce Evans, this is probably the best way to do it in
2868 * -stable. We only do this if we're already open, and
2869 * therefore dsopen() has already run. If CD_FLAG_OPEN
2870 * isn't set, this isn't necessary.
2872 if (softc->flags & CD_FLAG_OPEN) {
2877 error = dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2878 /*data*/(caddr_t)&force, /*flags*/ 0,
2879 &softc->disk.d_slice);
2882 * Set a bogus sector size, so the slice code
2883 * won't try to divide by 0 and panic the
2886 label->d_secsize = 2048;
2888 label->d_secperunit = 0;
2891 * Tell devstat(9) we don't have a blocksize.
2893 softc->device_stats.flags |=
2894 DEVSTAT_BS_UNAVAILABLE;
2896 cdprevent(periph, PR_ALLOW);
2901 * We unconditionally (re)set the blocksize each time the
2902 * CD device is opened. This is because the CD can change,
2903 * and therefore the blocksize might change.
2904 * XXX problems here if some slice or partition is still
2905 * open with the old size?
2907 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2908 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2909 softc->device_stats.block_size = softc->params.blksize;
2911 softc->flags |= CD_FLAG_VALID_MEDIA;
2915 * Now we check the table of contents. This (currently) is only
2916 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2917 * things like present a separate entry in /dev for each track,
2918 * like that acd(4) driver does.
2920 bzero(&softc->toc, sizeof(softc->toc));
2921 toch = &softc->toc.header;
2924 * We will get errors here for media that doesn't have a table of
2925 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2926 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2927 * has not been recorded (no complete session) and the Format codes
2928 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2929 * with an INVALID FIELD IN CDB. Devices that are not capable of
2930 * reading an incomplete session on DDC/CD-R/RW media shall report
2931 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2933 * So this isn't fatal if we can't read the table of contents, it
2934 * just means that the user won't be able to issue the play tracks
2935 * ioctl, and likely lots of other stuff won't work either. They
2936 * need to burn the CD before we can do a whole lot with it. So
2937 * we don't print anything here if we get an error back.
2939 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2942 * Errors in reading the table of contents aren't fatal, we just
2943 * won't have a valid table of contents cached.
2947 bzero(&softc->toc, sizeof(softc->toc));
2951 if (softc->quirks & CD_Q_BCD_TRACKS) {
2952 toch->starting_track = bcd2bin(toch->starting_track);
2953 toch->ending_track = bcd2bin(toch->ending_track);
2956 /* Number of TOC entries, plus leadout */
2957 num_entries = (toch->ending_track - toch->starting_track) + 2;
2959 if (num_entries <= 0)
2962 toclen = num_entries * sizeof(struct cd_toc_entry);
2964 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2965 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2969 bzero(&softc->toc, sizeof(softc->toc));
2973 if (softc->quirks & CD_Q_BCD_TRACKS) {
2974 toch->starting_track = bcd2bin(toch->starting_track);
2975 toch->ending_track = bcd2bin(toch->ending_track);
2977 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2980 * XXX KDM is this necessary? Probably only if the drive doesn't
2981 * return leadout information with the table of contents.
2983 cdindex = toch->starting_track + num_entries -1;
2984 if (cdindex == toch->ending_track + 1) {
2986 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2987 (u_int8_t *)&leadout, sizeof(leadout),
2993 softc->toc.entries[cdindex - toch->starting_track] =
2996 if (softc->quirks & CD_Q_BCD_TRACKS) {
2997 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2998 softc->toc.entries[cdindex].track =
2999 bcd2bin(softc->toc.entries[cdindex].track);
3004 * Run through the TOC entries, find the first entry and determine
3005 * whether it is an audio or data track.
3007 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3008 if (softc->toc.entries[cdindex].track == toch->starting_track) {
3009 if (softc->toc.entries[cdindex].control & 0x04)
3010 first_track_audio = 0;
3012 first_track_audio = 1;
3018 * If first_track_audio is non-zero, we either have an error (e.g.
3019 * couldn't find the starting track) or the first track is an audio
3020 * track. If first_track_audio is 0, the first track is a data
3021 * track that could have a disklabel. Attempt to read the
3022 * disklabel off the media, just in case the user put one there.
3024 if (first_track_audio == 0) {
3025 softc->disk.d_dsflags |= DSO_COMPATLABEL;
3026 softc->disk.d_dsflags &= ~DSO_NOLABELS;
3028 softc->flags |= CD_FLAG_VALID_TOC;
3035 cdsize(struct cam_periph *periph, u_int32_t *size)
3037 struct cd_softc *softc;
3039 struct scsi_read_capacity_data *rcap_buf;
3042 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3044 softc = (struct cd_softc *)periph->softc;
3046 ccb = cdgetccb(periph, /* priority */ 1);
3048 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3049 M_TEMP, M_INTWAIT | M_ZERO);
3051 scsi_read_capacity(&ccb->csio,
3057 /* timeout */20000);
3059 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3060 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT|SF_RETRY_SELTO);
3062 xpt_release_ccb(ccb);
3064 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3065 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3067 * SCSI-3 mandates that the reported blocksize shall be 2048.
3068 * Older drives sometimes report funny values, trim it down to
3069 * 2048, or other parts of the kernel will get confused.
3071 * XXX we leave drives alone that might report 512 bytes, as
3072 * well as drives reporting more weird sizes like perhaps 4K.
3074 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3075 softc->params.blksize = 2048;
3077 free(rcap_buf, M_TEMP);
3078 *size = softc->params.disksize;
3085 cd6byteworkaround(union ccb *ccb)
3088 struct cam_periph *periph;
3089 struct cd_softc *softc;
3090 struct cd_mode_params *params;
3093 periph = xpt_path_periph(ccb->ccb_h.path);
3094 softc = (struct cd_softc *)periph->softc;
3096 cdb = ccb->csio.cdb_io.cdb_bytes;
3098 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3099 || ((cdb[0] != MODE_SENSE_6)
3100 && (cdb[0] != MODE_SELECT_6)))
3104 * Because there is no convenient place to stash the overall
3105 * cd_mode_params structure pointer, we have to grab it like this.
3106 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3107 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3109 * XXX It would be nice if, at some point, we could increase the
3110 * number of available peripheral private pointers. Both pointers
3111 * are currently used in most every peripheral driver.
3115 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3116 if (params->mode_buf == ccb->csio.data_ptr) {
3123 * This shouldn't happen. All mode sense and mode select
3124 * operations in the cd(4) driver MUST go through cdgetmode() and
3128 xpt_print_path(periph->path);
3129 printf("mode buffer not found in mode queue!\n");
3133 params->cdb_size = 10;
3134 softc->minimum_command_size = 10;
3135 xpt_print_path(ccb->ccb_h.path);
3136 printf("%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3137 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3139 if (cdb[0] == MODE_SENSE_6) {
3140 struct scsi_mode_sense_10 ms10;
3141 struct scsi_mode_sense_6 *ms6;
3144 ms6 = (struct scsi_mode_sense_6 *)cdb;
3146 bzero(&ms10, sizeof(ms10));
3147 ms10.opcode = MODE_SENSE_10;
3148 ms10.byte2 = ms6->byte2;
3149 ms10.page = ms6->page;
3152 * 10 byte mode header, block descriptor,
3153 * sizeof(union cd_pages)
3155 len = sizeof(struct cd_mode_data_10);
3156 ccb->csio.dxfer_len = len;
3158 scsi_ulto2b(len, ms10.length);
3159 ms10.control = ms6->control;
3160 bcopy(&ms10, cdb, 10);
3161 ccb->csio.cdb_len = 10;
3163 struct scsi_mode_select_10 ms10;
3164 struct scsi_mode_select_6 *ms6;
3165 struct scsi_mode_header_6 *header6;
3166 struct scsi_mode_header_10 *header10;
3167 struct scsi_mode_page_header *page_header;
3168 int blk_desc_len, page_num, page_size, len;
3170 ms6 = (struct scsi_mode_select_6 *)cdb;
3172 bzero(&ms10, sizeof(ms10));
3173 ms10.opcode = MODE_SELECT_10;
3174 ms10.byte2 = ms6->byte2;
3176 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3177 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3179 page_header = find_mode_page_6(header6);
3180 page_num = page_header->page_code;
3182 blk_desc_len = header6->blk_desc_len;
3184 page_size = cdgetpagesize(page_num);
3186 if (page_size != (page_header->page_length +
3187 sizeof(*page_header)))
3188 page_size = page_header->page_length +
3189 sizeof(*page_header);
3191 len = sizeof(*header10) + blk_desc_len + page_size;
3193 len = min(params->alloc_len, len);
3196 * Since the 6 byte parameter header is shorter than the 10
3197 * byte parameter header, we need to copy the actual mode
3198 * page data, and the block descriptor, if any, so things wind
3199 * up in the right place. The regions will overlap, but
3200 * bcopy() does the right thing.
3202 bcopy(params->mode_buf + sizeof(*header6),
3203 params->mode_buf + sizeof(*header10),
3204 len - sizeof(*header10));
3206 /* Make sure these fields are set correctly. */
3207 scsi_ulto2b(0, header10->data_length);
3208 header10->medium_type = 0;
3209 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3211 ccb->csio.dxfer_len = len;
3213 scsi_ulto2b(len, ms10.length);
3214 ms10.control = ms6->control;
3215 bcopy(&ms10, cdb, 10);
3216 ccb->csio.cdb_len = 10;
3219 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3220 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3223 cam_release_devq(ccb->ccb_h.path,
3227 /*getcount_only*/0);
3234 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3236 struct cd_softc *softc;
3237 struct cam_periph *periph;
3240 periph = xpt_path_periph(ccb->ccb_h.path);
3241 softc = (struct cd_softc *)periph->softc;
3246 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3247 * CDB comes back with this particular error, try transforming it
3248 * into the 10 byte version.
3250 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3251 error = cd6byteworkaround(ccb);
3252 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3253 CAM_SCSI_STATUS_ERROR)
3254 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3255 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3256 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3257 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3258 int sense_key, error_code, asc, ascq;
3260 scsi_extract_sense(&ccb->csio.sense_data,
3261 &error_code, &sense_key, &asc, &ascq);
3262 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3263 error = cd6byteworkaround(ccb);
3266 if (error == ERESTART)
3271 * Until we have a better way of doing pack validation,
3272 * don't treat UAs as errors.
3274 sense_flags |= SF_RETRY_UA;
3275 return (cam_periph_error(ccb, cam_flags, sense_flags,
3276 &softc->saved_ccb));
3280 * Read table of contents
3283 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3284 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3286 struct scsi_read_toc *scsi_cmd;
3288 struct ccb_scsiio *csio;
3295 ccb = cdgetccb(periph, /* priority */ 1);
3301 /* cbfcnp */ cddone,
3302 /* flags */ CAM_DIR_IN,
3303 /* tag_action */ MSG_SIMPLE_Q_TAG,
3304 /* data_ptr */ data,
3305 /* dxfer_len */ len,
3306 /* sense_len */ SSD_FULL_SIZE,
3307 sizeof(struct scsi_read_toc),
3308 /* timeout */ 50000);
3310 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3311 bzero (scsi_cmd, sizeof(*scsi_cmd));
3313 if (mode == CD_MSF_FORMAT)
3314 scsi_cmd->byte2 |= CD_MSF;
3315 scsi_cmd->from_track = start;
3316 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3317 scsi_cmd->data_len[0] = (ntoc) >> 8;
3318 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3320 scsi_cmd->op_code = READ_TOC;
3322 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3323 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO |
3326 xpt_release_ccb(ccb);
3332 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3333 u_int32_t format, int track,
3334 struct cd_sub_channel_info *data, u_int32_t len)
3336 struct scsi_read_subchannel *scsi_cmd;
3337 struct ccb_scsiio *csio;
3343 ccb = cdgetccb(periph, /* priority */ 1);
3349 /* cbfcnp */ cddone,
3350 /* flags */ CAM_DIR_IN,
3351 /* tag_action */ MSG_SIMPLE_Q_TAG,
3352 /* data_ptr */ (u_int8_t *)data,
3353 /* dxfer_len */ len,
3354 /* sense_len */ SSD_FULL_SIZE,
3355 sizeof(struct scsi_read_subchannel),
3356 /* timeout */ 50000);
3358 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3359 bzero (scsi_cmd, sizeof(*scsi_cmd));
3361 scsi_cmd->op_code = READ_SUBCHANNEL;
3362 if (mode == CD_MSF_FORMAT)
3363 scsi_cmd->byte1 |= CD_MSF;
3364 scsi_cmd->byte2 = SRS_SUBQ;
3365 scsi_cmd->subchan_format = format;
3366 scsi_cmd->track = track;
3367 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3368 scsi_cmd->control = 0;
3370 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3371 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3373 xpt_release_ccb(ccb);
3379 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3380 * routine. See comments in cd6byteworkaround() for details.
3383 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3386 struct ccb_scsiio *csio;
3387 struct cd_softc *softc;
3392 softc = (struct cd_softc *)periph->softc;
3394 ccb = cdgetccb(periph, /* priority */ 1);
3398 data->cdb_size = softc->minimum_command_size;
3399 if (data->cdb_size < 10)
3400 param_len = sizeof(struct cd_mode_data);
3402 param_len = sizeof(struct cd_mode_data_10);
3404 /* Don't say we've got more room than we actually allocated */
3405 param_len = min(param_len, data->alloc_len);
3407 scsi_mode_sense_len(csio,
3409 /* cbfcnp */ cddone,
3410 /* tag_action */ MSG_SIMPLE_Q_TAG,
3412 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3414 /* param_buf */ data->mode_buf,
3415 /* param_len */ param_len,
3416 /* minimum_cmd_size */ softc->minimum_command_size,
3417 /* sense_len */ SSD_FULL_SIZE,
3418 /* timeout */ 50000);
3421 * It would be nice not to have to do this, but there's no
3422 * available pointer in the CCB that would allow us to stuff the
3423 * mode params structure in there and retrieve it in
3424 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3425 * lets the caller know what CDB size we ended up using, so they
3426 * can find the actual mode page offset.
3428 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3430 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3431 /*sense_flags*/SF_RETRY_UA|SF_RETRY_SELTO);
3433 xpt_release_ccb(ccb);
3435 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3438 * This is a bit of belt-and-suspenders checking, but if we run
3439 * into a situation where the target sends back multiple block
3440 * descriptors, we might not have enough space in the buffer to
3441 * see the whole mode page. Better to return an error than
3442 * potentially access memory beyond our malloced region.
3447 if (data->cdb_size == 10) {
3448 struct scsi_mode_header_10 *hdr10;
3450 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3451 data_len = scsi_2btoul(hdr10->data_length);
3452 data_len += sizeof(hdr10->data_length);
3454 struct scsi_mode_header_6 *hdr6;
3456 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3457 data_len = hdr6->data_length;
3458 data_len += sizeof(hdr6->data_length);
3462 * Complain if there is more mode data available than we
3463 * allocated space for. This could potentially happen if
3464 * we miscalculated the page length for some reason, if the
3465 * drive returns multiple block descriptors, or if it sets
3466 * the data length incorrectly.
3468 if (data_len > data->alloc_len) {
3469 xpt_print_path(periph->path);
3470 printf("allocated modepage %d length %d < returned "
3471 "length %d\n", page, data->alloc_len, data_len);
3480 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3481 * routine. See comments in cd6byteworkaround() for details.
3484 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3486 struct ccb_scsiio *csio;
3487 struct cd_softc *softc;
3489 int cdb_size, param_len;
3492 softc = (struct cd_softc *)periph->softc;
3494 ccb = cdgetccb(periph, /* priority */ 1);
3501 * If the data is formatted for the 10 byte version of the mode
3502 * select parameter list, we need to use the 10 byte CDB.
3503 * Otherwise, we use whatever the stored minimum command size.
3505 if (data->cdb_size == 10)
3506 cdb_size = data->cdb_size;
3508 cdb_size = softc->minimum_command_size;
3510 if (cdb_size >= 10) {
3511 struct scsi_mode_header_10 *mode_header;
3514 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3516 data_len = scsi_2btoul(mode_header->data_length);
3518 scsi_ulto2b(0, mode_header->data_length);
3520 * SONY drives do not allow a mode select with a medium_type
3521 * value that has just been returned by a mode sense; use a
3522 * medium_type of 0 (Default) instead.
3524 mode_header->medium_type = 0;
3527 * Pass back whatever the drive passed to us, plus the size
3528 * of the data length field.
3530 param_len = data_len + sizeof(mode_header->data_length);
3533 struct scsi_mode_header_6 *mode_header;
3535 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3537 param_len = mode_header->data_length + 1;
3539 mode_header->data_length = 0;
3541 * SONY drives do not allow a mode select with a medium_type
3542 * value that has just been returned by a mode sense; use a
3543 * medium_type of 0 (Default) instead.
3545 mode_header->medium_type = 0;
3548 /* Don't say we've got more room than we actually allocated */
3549 param_len = min(param_len, data->alloc_len);
3551 scsi_mode_select_len(csio,
3553 /* cbfcnp */ cddone,
3554 /* tag_action */ MSG_SIMPLE_Q_TAG,
3555 /* scsi_page_fmt */ 1,
3557 /* param_buf */ data->mode_buf,
3558 /* param_len */ param_len,
3559 /* minimum_cmd_size */ cdb_size,
3560 /* sense_len */ SSD_FULL_SIZE,
3561 /* timeout */ 50000);
3563 /* See comments in cdgetmode() and cd6byteworkaround(). */
3564 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3566 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3567 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3569 xpt_release_ccb(ccb);
3571 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3579 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3581 struct ccb_scsiio *csio;
3587 ccb = cdgetccb(periph, /* priority */ 1);
3590 * Use the smallest possible command to perform the operation.
3592 if ((len & 0xffff0000) == 0) {
3594 * We can fit in a 10 byte cdb.
3596 struct scsi_play_10 *scsi_cmd;
3598 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3599 bzero (scsi_cmd, sizeof(*scsi_cmd));
3600 scsi_cmd->op_code = PLAY_10;
3601 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3602 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3603 cdb_len = sizeof(*scsi_cmd);
3605 struct scsi_play_12 *scsi_cmd;
3607 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3608 bzero (scsi_cmd, sizeof(*scsi_cmd));
3609 scsi_cmd->op_code = PLAY_12;
3610 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3611 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3612 cdb_len = sizeof(*scsi_cmd);
3617 /*flags*/CAM_DIR_NONE,
3621 /*sense_len*/SSD_FULL_SIZE,
3623 /*timeout*/50 * 1000);
3625 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3626 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3628 xpt_release_ccb(ccb);
3634 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3635 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3637 struct scsi_play_msf *scsi_cmd;
3638 struct ccb_scsiio *csio;
3644 ccb = cdgetccb(periph, /* priority */ 1);
3650 /* cbfcnp */ cddone,
3651 /* flags */ CAM_DIR_NONE,
3652 /* tag_action */ MSG_SIMPLE_Q_TAG,
3653 /* data_ptr */ NULL,
3655 /* sense_len */ SSD_FULL_SIZE,
3656 sizeof(struct scsi_play_msf),
3657 /* timeout */ 50000);
3659 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3660 bzero (scsi_cmd, sizeof(*scsi_cmd));
3662 scsi_cmd->op_code = PLAY_MSF;
3663 scsi_cmd->start_m = startm;
3664 scsi_cmd->start_s = starts;
3665 scsi_cmd->start_f = startf;
3666 scsi_cmd->end_m = endm;
3667 scsi_cmd->end_s = ends;
3668 scsi_cmd->end_f = endf;
3670 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3671 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3673 xpt_release_ccb(ccb);
3680 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3681 u_int32_t etrack, u_int32_t eindex)
3683 struct scsi_play_track *scsi_cmd;
3684 struct ccb_scsiio *csio;
3690 ccb = cdgetccb(periph, /* priority */ 1);
3696 /* cbfcnp */ cddone,
3697 /* flags */ CAM_DIR_NONE,
3698 /* tag_action */ MSG_SIMPLE_Q_TAG,
3699 /* data_ptr */ NULL,
3701 /* sense_len */ SSD_FULL_SIZE,
3702 sizeof(struct scsi_play_track),
3703 /* timeout */ 50000);
3705 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3706 bzero (scsi_cmd, sizeof(*scsi_cmd));
3708 scsi_cmd->op_code = PLAY_TRACK;
3709 scsi_cmd->start_track = strack;
3710 scsi_cmd->start_index = sindex;
3711 scsi_cmd->end_track = etrack;
3712 scsi_cmd->end_index = eindex;
3714 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3715 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3717 xpt_release_ccb(ccb);
3723 cdpause(struct cam_periph *periph, u_int32_t go)
3725 struct scsi_pause *scsi_cmd;
3726 struct ccb_scsiio *csio;
3732 ccb = cdgetccb(periph, /* priority */ 1);
3738 /* cbfcnp */ cddone,
3739 /* flags */ CAM_DIR_NONE,
3740 /* tag_action */ MSG_SIMPLE_Q_TAG,
3741 /* data_ptr */ NULL,
3743 /* sense_len */ SSD_FULL_SIZE,
3744 sizeof(struct scsi_pause),
3745 /* timeout */ 50000);
3747 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3748 bzero (scsi_cmd, sizeof(*scsi_cmd));
3750 scsi_cmd->op_code = PAUSE;
3751 scsi_cmd->resume = go;
3753 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3754 /*sense_flags*/SF_RETRY_UA |SF_RETRY_SELTO);
3756 xpt_release_ccb(ccb);
3762 cdstartunit(struct cam_periph *periph, int load)
3769 ccb = cdgetccb(periph, /* priority */ 1);
3771 scsi_start_stop(&ccb->csio,
3773 /* cbfcnp */ cddone,
3774 /* tag_action */ MSG_SIMPLE_Q_TAG,
3776 /* load_eject */ load,
3777 /* immediate */ FALSE,
3778 /* sense_len */ SSD_FULL_SIZE,
3779 /* timeout */ 50000);
3781 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3782 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3784 xpt_release_ccb(ccb);
3790 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3797 ccb = cdgetccb(periph, /* priority */ 1);
3799 scsi_start_stop(&ccb->csio,
3801 /* cbfcnp */ cddone,
3802 /* tag_action */ MSG_SIMPLE_Q_TAG,
3804 /* load_eject */ eject,
3805 /* immediate */ FALSE,
3806 /* sense_len */ SSD_FULL_SIZE,
3807 /* timeout */ 50000);
3809 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3810 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3812 xpt_release_ccb(ccb);
3818 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3820 struct scsi_set_speed *scsi_cmd;
3821 struct ccb_scsiio *csio;
3826 ccb = cdgetccb(periph, /* priority */ 1);
3829 /* Preserve old behavior: units in multiples of CDROM speed */
3837 /* cbfcnp */ cddone,
3838 /* flags */ CAM_DIR_NONE,
3839 /* tag_action */ MSG_SIMPLE_Q_TAG,
3840 /* data_ptr */ NULL,
3842 /* sense_len */ SSD_FULL_SIZE,
3843 sizeof(struct scsi_set_speed),
3844 /* timeout */ 50000);
3846 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3847 bzero(scsi_cmd, sizeof(*scsi_cmd));
3849 scsi_cmd->opcode = SET_CD_SPEED;
3850 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3851 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3853 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3854 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3856 xpt_release_ccb(ccb);
3862 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3874 ccb = cdgetccb(periph, /* priority */ 1);
3876 switch (authinfo->format) {
3877 case DVD_REPORT_AGID:
3878 length = sizeof(struct scsi_report_key_data_agid);
3880 case DVD_REPORT_CHALLENGE:
3881 length = sizeof(struct scsi_report_key_data_challenge);
3883 case DVD_REPORT_KEY1:
3884 length = sizeof(struct scsi_report_key_data_key1_key2);
3886 case DVD_REPORT_TITLE_KEY:
3887 length = sizeof(struct scsi_report_key_data_title);
3888 /* The lba field is only set for the title key */
3889 lba = authinfo->lba;
3891 case DVD_REPORT_ASF:
3892 length = sizeof(struct scsi_report_key_data_asf);
3894 case DVD_REPORT_RPC:
3895 length = sizeof(struct scsi_report_key_data_rpc);
3897 case DVD_INVALIDATE_AGID:
3903 break; /* NOTREACHED */
3907 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3913 scsi_report_key(&ccb->csio,
3915 /* cbfcnp */ cddone,
3916 /* tag_action */ MSG_SIMPLE_Q_TAG,
3918 /* agid */ authinfo->agid,
3919 /* key_format */ authinfo->format,
3920 /* data_ptr */ databuf,
3921 /* dxfer_len */ length,
3922 /* sense_len */ SSD_FULL_SIZE,
3923 /* timeout */ 50000);
3925 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
3926 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
3931 if (ccb->csio.resid != 0) {
3932 xpt_print_path(periph->path);
3933 printf("warning, residual for report key command is %d\n",
3937 switch(authinfo->format) {
3938 case DVD_REPORT_AGID: {
3939 struct scsi_report_key_data_agid *agid_data;
3941 agid_data = (struct scsi_report_key_data_agid *)databuf;
3943 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3947 case DVD_REPORT_CHALLENGE: {
3948 struct scsi_report_key_data_challenge *chal_data;
3950 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3952 bcopy(chal_data->challenge_key, authinfo->keychal,
3953 min(sizeof(chal_data->challenge_key),
3954 sizeof(authinfo->keychal)));
3957 case DVD_REPORT_KEY1: {
3958 struct scsi_report_key_data_key1_key2 *key1_data;
3960 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3962 bcopy(key1_data->key1, authinfo->keychal,
3963 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3966 case DVD_REPORT_TITLE_KEY: {
3967 struct scsi_report_key_data_title *title_data;
3969 title_data = (struct scsi_report_key_data_title *)databuf;
3971 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3972 RKD_TITLE_CPM_SHIFT;
3973 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3974 RKD_TITLE_CP_SEC_SHIFT;
3975 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3976 RKD_TITLE_CMGS_SHIFT;
3977 bcopy(title_data->title_key, authinfo->keychal,
3978 min(sizeof(title_data->title_key),
3979 sizeof(authinfo->keychal)));
3982 case DVD_REPORT_ASF: {
3983 struct scsi_report_key_data_asf *asf_data;
3985 asf_data = (struct scsi_report_key_data_asf *)databuf;
3987 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3990 case DVD_REPORT_RPC: {
3991 struct scsi_report_key_data_rpc *rpc_data;
3993 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3995 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3997 authinfo->vend_rsts =
3998 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3999 RKD_RPC_VENDOR_RESET_SHIFT;
4000 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4001 authinfo->region = rpc_data->region_mask;
4002 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4005 case DVD_INVALIDATE_AGID:
4008 /* This should be impossible, since we checked above */
4011 break; /* NOTREACHED */
4014 if (databuf != NULL)
4015 free(databuf, M_DEVBUF);
4017 xpt_release_ccb(ccb);
4023 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4033 ccb = cdgetccb(periph, /* priority */ 1);
4035 switch(authinfo->format) {
4036 case DVD_SEND_CHALLENGE: {
4037 struct scsi_report_key_data_challenge *challenge_data;
4039 length = sizeof(*challenge_data);
4041 challenge_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4043 databuf = (u_int8_t *)challenge_data;
4045 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4046 challenge_data->data_len);
4048 bcopy(authinfo->keychal, challenge_data->challenge_key,
4049 min(sizeof(authinfo->keychal),
4050 sizeof(challenge_data->challenge_key)));
4053 case DVD_SEND_KEY2: {
4054 struct scsi_report_key_data_key1_key2 *key2_data;
4056 length = sizeof(*key2_data);
4058 key2_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4060 databuf = (u_int8_t *)key2_data;
4062 scsi_ulto2b(length - sizeof(key2_data->data_len),
4063 key2_data->data_len);
4065 bcopy(authinfo->keychal, key2_data->key1,
4066 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4070 case DVD_SEND_RPC: {
4071 struct scsi_send_key_data_rpc *rpc_data;
4073 length = sizeof(*rpc_data);
4075 rpc_data = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4077 databuf = (u_int8_t *)rpc_data;
4079 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4080 rpc_data->data_len);
4082 rpc_data->region_code = authinfo->region;
4088 break; /* NOTREACHED */
4091 scsi_send_key(&ccb->csio,
4093 /* cbfcnp */ cddone,
4094 /* tag_action */ MSG_SIMPLE_Q_TAG,
4095 /* agid */ authinfo->agid,
4096 /* key_format */ authinfo->format,
4097 /* data_ptr */ databuf,
4098 /* dxfer_len */ length,
4099 /* sense_len */ SSD_FULL_SIZE,
4100 /* timeout */ 50000);
4102 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4103 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4107 if (databuf != NULL)
4108 free(databuf, M_DEVBUF);
4110 xpt_release_ccb(ccb);
4116 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4126 /* The address is reserved for many of the formats */
4129 ccb = cdgetccb(periph, /* priority */ 1);
4131 switch(dvdstruct->format) {
4132 case DVD_STRUCT_PHYSICAL:
4133 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4135 case DVD_STRUCT_COPYRIGHT:
4136 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4138 case DVD_STRUCT_DISCKEY:
4139 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4141 case DVD_STRUCT_BCA:
4142 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4144 case DVD_STRUCT_MANUFACT:
4145 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4147 case DVD_STRUCT_CMI:
4151 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4152 address = dvdstruct->address;
4154 break; /* NOTREACHED */
4155 case DVD_STRUCT_PROTDISCID:
4156 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4158 case DVD_STRUCT_DISCKEYBLOCK:
4159 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4161 case DVD_STRUCT_DDS:
4162 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4164 case DVD_STRUCT_MEDIUM_STAT:
4165 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4167 case DVD_STRUCT_SPARE_AREA:
4168 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4170 case DVD_STRUCT_RMD_LAST:
4174 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4175 address = dvdstruct->address;
4177 break; /* NOTREACHED */
4178 case DVD_STRUCT_RMD_RMA:
4182 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4183 address = dvdstruct->address;
4185 break; /* NOTREACHED */
4186 case DVD_STRUCT_PRERECORDED:
4187 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4189 case DVD_STRUCT_UNIQUEID:
4190 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4192 case DVD_STRUCT_DCB:
4196 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4197 address = dvdstruct->address;
4199 break; /* NOTREACHED */
4200 case DVD_STRUCT_LIST:
4202 * This is the maximum allocation length for the READ DVD
4203 * STRUCTURE command. There's nothing in the MMC3 spec
4204 * that indicates a limit in the amount of data that can
4205 * be returned from this call, other than the limits
4206 * imposed by the 2-byte length variables.
4213 break; /* NOTREACHED */
4217 databuf = malloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4222 scsi_read_dvd_structure(&ccb->csio,
4224 /* cbfcnp */ cddone,
4225 /* tag_action */ MSG_SIMPLE_Q_TAG,
4227 /* layer_number */ dvdstruct->layer_num,
4228 /* key_format */ dvdstruct->format,
4229 /* agid */ dvdstruct->agid,
4230 /* data_ptr */ databuf,
4231 /* dxfer_len */ length,
4232 /* sense_len */ SSD_FULL_SIZE,
4233 /* timeout */ 50000);
4235 error = cdrunccb(ccb, cderror, /*cam_flags*/0,
4236 /*sense_flags*/SF_RETRY_UA | SF_RETRY_SELTO);
4241 switch(dvdstruct->format) {
4242 case DVD_STRUCT_PHYSICAL: {
4243 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4244 struct dvd_layer *outlayer;
4245 struct scsi_read_dvd_struct_data_physical *phys_data;
4248 (struct scsi_read_dvd_struct_data_physical *)databuf;
4249 inlayer = &phys_data->layer_desc;
4250 outlayer = (struct dvd_layer *)&dvdstruct->data;
4252 dvdstruct->length = sizeof(*inlayer);
4254 outlayer->book_type = (inlayer->book_type_version &
4255 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4256 outlayer->book_version = (inlayer->book_type_version &
4257 RDSD_BOOK_VERSION_MASK);
4258 outlayer->disc_size = (inlayer->disc_size_max_rate &
4259 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4260 outlayer->max_rate = (inlayer->disc_size_max_rate &
4261 RDSD_MAX_RATE_MASK);
4262 outlayer->nlayers = (inlayer->layer_info &
4263 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4264 outlayer->track_path = (inlayer->layer_info &
4265 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4266 outlayer->layer_type = (inlayer->layer_info &
4267 RDSD_LAYER_TYPE_MASK);
4268 outlayer->linear_density = (inlayer->density &
4269 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4270 outlayer->track_density = (inlayer->density &
4271 RDSD_TRACK_DENSITY_MASK);
4272 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4274 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4275 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4276 outlayer->end_sector_l0 =
4277 scsi_3btoul(inlayer->end_sector_layer0);
4280 case DVD_STRUCT_COPYRIGHT: {
4281 struct scsi_read_dvd_struct_data_copyright *copy_data;
4283 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4286 dvdstruct->cpst = copy_data->cps_type;
4287 dvdstruct->rmi = copy_data->region_info;
4288 dvdstruct->length = 0;
4294 * Tell the user what the overall length is, no matter
4295 * what we can actually fit in the data buffer.
4297 dvdstruct->length = length - ccb->csio.resid -
4298 sizeof(struct scsi_read_dvd_struct_data_header);
4301 * But only actually copy out the smaller of what we read
4302 * in or what the structure can take.
4304 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4306 min(sizeof(dvdstruct->data), dvdstruct->length));
4311 if (databuf != NULL)
4312 free(databuf, M_DEVBUF);
4314 xpt_release_ccb(ccb);
4320 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4321 void (*cbfcnp)(struct cam_periph *, union ccb *),
4322 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4323 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4324 u_int8_t sense_len, u_int32_t timeout)
4326 struct scsi_report_key *scsi_cmd;
4328 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4329 bzero(scsi_cmd, sizeof(*scsi_cmd));
4330 scsi_cmd->opcode = REPORT_KEY;
4331 scsi_ulto4b(lba, scsi_cmd->lba);
4332 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4333 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4334 (key_format & RK_KF_KEYFORMAT_MASK);
4339 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4341 /*data_ptr*/ data_ptr,
4342 /*dxfer_len*/ dxfer_len,
4349 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4350 void (*cbfcnp)(struct cam_periph *, union ccb *),
4351 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4352 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4355 struct scsi_send_key *scsi_cmd;
4357 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4358 bzero(scsi_cmd, sizeof(*scsi_cmd));
4359 scsi_cmd->opcode = SEND_KEY;
4361 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4362 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4363 (key_format & RK_KF_KEYFORMAT_MASK);
4368 /*flags*/ CAM_DIR_OUT,
4370 /*data_ptr*/ data_ptr,
4371 /*dxfer_len*/ dxfer_len,
4379 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4380 void (*cbfcnp)(struct cam_periph *, union ccb *),
4381 u_int8_t tag_action, u_int32_t address,
4382 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4383 u_int8_t *data_ptr, u_int32_t dxfer_len,
4384 u_int8_t sense_len, u_int32_t timeout)
4386 struct scsi_read_dvd_structure *scsi_cmd;
4388 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4389 bzero(scsi_cmd, sizeof(*scsi_cmd));
4390 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4392 scsi_ulto4b(address, scsi_cmd->address);
4393 scsi_cmd->layer_number = layer_number;
4394 scsi_cmd->format = format;
4395 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4396 /* The AGID is the top two bits of this byte */
4397 scsi_cmd->agid = agid << 6;
4402 /*flags*/ CAM_DIR_IN,
4404 /*data_ptr*/ data_ptr,
4405 /*dxfer_len*/ dxfer_len,