2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
66 #include <geom/geom_disk.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
73 #include <cam/cam_sim.h>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_NO_CHANGER = 0x04,
92 CD_Q_10_BYTE_ONLY = 0x10
95 #define CD_Q_BIT_STRING \
104 CD_FLAG_INVALID = 0x0001,
105 CD_FLAG_NEW_DISC = 0x0002,
106 CD_FLAG_DISC_LOCKED = 0x0004,
107 CD_FLAG_DISC_REMOVABLE = 0x0008,
108 CD_FLAG_SAW_MEDIA = 0x0010,
109 CD_FLAG_CHANGER = 0x0040,
110 CD_FLAG_ACTIVE = 0x0080,
111 CD_FLAG_SCHED_ON_COMP = 0x0100,
112 CD_FLAG_RETRY_UA = 0x0200,
113 CD_FLAG_VALID_MEDIA = 0x0400,
114 CD_FLAG_VALID_TOC = 0x0800,
115 CD_FLAG_SCTX_INIT = 0x1000
120 CD_CCB_BUFFER_IO = 0x02,
122 CD_CCB_TYPE_MASK = 0x0F,
123 CD_CCB_RETRY_UA = 0x10
127 CHANGER_TIMEOUT_SCHED = 0x01,
128 CHANGER_SHORT_TMOUT_SCHED = 0x02,
129 CHANGER_MANUAL_CALL = 0x04,
130 CHANGER_NEED_TIMEOUT = 0x08
133 #define ccb_state ppriv_field0
134 #define ccb_bp ppriv_ptr1
137 struct ioc_toc_header header;
138 struct cd_toc_entry entries[100];
141 struct cd_toc_single {
142 struct ioc_toc_header header;
143 struct cd_toc_entry entry;
154 volatile cd_flags flags;
155 struct bio_queue_head bio_queue;
156 LIST_HEAD(, ccb_hdr) pending_ccbs;
157 struct cd_params params;
160 STAILQ_ENTRY(cd_softc) changer_links;
161 struct cdchanger *changer;
163 struct cam_periph *periph;
164 int minimum_command_size;
165 int outstanding_cmds;
167 struct task sysctl_task;
168 struct sysctl_ctx_list sysctl_ctx;
169 struct sysctl_oid *sysctl_tree;
170 STAILQ_HEAD(, cd_mode_params) mode_queue;
171 struct cd_tocdata toc;
173 struct callout mediapoll_c;
176 struct cd_page_sizes {
181 static struct cd_page_sizes cd_page_size_table[] =
183 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
186 struct cd_quirk_entry {
187 struct scsi_inquiry_pattern inq_pat;
192 * The changer quirk entries aren't strictly necessary. Basically, what
193 * they do is tell cdregister() up front that a device is a changer.
194 * Otherwise, it will figure that fact out once it sees a LUN on the device
195 * that is greater than 0. If it is known up front that a device is a changer,
196 * all I/O to the device will go through the changer scheduling routines, as
197 * opposed to the "normal" CD code.
199 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
200 * your device hangs when it gets a 10 byte command. Adding a quirk just
201 * to get rid of the informative diagnostic message is not acceptable. All
202 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
203 * referenced in a comment along with the quirk) , and must be approved by
204 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
205 * be removed until the submitter can explain why they are needed.
206 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
207 * when the CAM_NEW_TRAN_CODE work is done.
209 static struct cd_quirk_entry cd_quirk_table[] =
212 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
213 /*quirks*/ CD_Q_CHANGER
216 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
217 "*"}, /* quirks */ CD_Q_CHANGER
220 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
221 /* quirks */ CD_Q_CHANGER
224 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
225 /* quirks */ CD_Q_BCD_TRACKS
229 static disk_open_t cdopen;
230 static disk_close_t cdclose;
231 static disk_ioctl_t cdioctl;
232 static disk_strategy_t cdstrategy;
234 static periph_init_t cdinit;
235 static periph_ctor_t cdregister;
236 static periph_dtor_t cdcleanup;
237 static periph_start_t cdstart;
238 static periph_oninv_t cdoninvalidate;
239 static void cdasync(void *callback_arg, u_int32_t code,
240 struct cam_path *path, void *arg);
241 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
242 static void cdshorttimeout(void *arg);
243 static void cdschedule(struct cam_periph *periph, int priority);
244 static void cdrunchangerqueue(void *arg);
245 static void cdchangerschedule(struct cd_softc *softc);
246 static int cdrunccb(union ccb *ccb,
247 int (*error_routine)(union ccb *ccb,
249 u_int32_t sense_flags),
250 u_int32_t cam_flags, u_int32_t sense_flags);
251 static union ccb *cdgetccb(struct cam_periph *periph,
253 static void cddone(struct cam_periph *periph,
254 union ccb *start_ccb);
255 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
256 static int cdgetpagesize(int page_num);
257 static void cdprevent(struct cam_periph *periph, int action);
258 static int cdcheckmedia(struct cam_periph *periph);
259 static int cdsize(struct cam_periph *periph, u_int32_t *size);
260 static int cd6byteworkaround(union ccb *ccb);
261 static int cderror(union ccb *ccb, u_int32_t cam_flags,
262 u_int32_t sense_flags);
263 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
264 u_int32_t start, u_int8_t *data,
265 u_int32_t len, u_int32_t sense_flags);
266 static int cdgetmode(struct cam_periph *periph,
267 struct cd_mode_params *data, u_int32_t page);
268 static int cdsetmode(struct cam_periph *periph,
269 struct cd_mode_params *data);
270 static int cdplay(struct cam_periph *periph, u_int32_t blk,
272 static int cdreadsubchannel(struct cam_periph *periph,
273 u_int32_t mode, u_int32_t format,
275 struct cd_sub_channel_info *data,
277 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
278 u_int32_t starts, u_int32_t startf,
279 u_int32_t endm, u_int32_t ends,
281 static int cdplaytracks(struct cam_periph *periph,
282 u_int32_t strack, u_int32_t sindex,
283 u_int32_t etrack, u_int32_t eindex);
284 static int cdpause(struct cam_periph *periph, u_int32_t go);
285 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
286 static int cdstartunit(struct cam_periph *periph, int load);
287 static int cdsetspeed(struct cam_periph *periph,
288 u_int32_t rdspeed, u_int32_t wrspeed);
289 static int cdreportkey(struct cam_periph *periph,
290 struct dvd_authinfo *authinfo);
291 static int cdsendkey(struct cam_periph *periph,
292 struct dvd_authinfo *authinfo);
293 static int cdreaddvdstructure(struct cam_periph *periph,
294 struct dvd_struct *dvdstruct);
295 static timeout_t cdmediapoll;
297 static struct periph_driver cddriver =
300 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
303 PERIPHDRIVER_DECLARE(cd, cddriver);
305 #ifndef CD_DEFAULT_POLL_PERIOD
306 #define CD_DEFAULT_POLL_PERIOD 3
308 #ifndef CD_DEFAULT_RETRY
309 #define CD_DEFAULT_RETRY 4
311 #ifndef CD_DEFAULT_TIMEOUT
312 #define CD_DEFAULT_TIMEOUT 30000
314 #ifndef CHANGER_MIN_BUSY_SECONDS
315 #define CHANGER_MIN_BUSY_SECONDS 5
317 #ifndef CHANGER_MAX_BUSY_SECONDS
318 #define CHANGER_MAX_BUSY_SECONDS 15
321 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
322 static int cd_retry_count = CD_DEFAULT_RETRY;
323 static int cd_timeout = CD_DEFAULT_TIMEOUT;
324 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
325 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
327 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
328 static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
330 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RW,
331 &cd_poll_period, 0, "Media polling period in seconds");
332 TUNABLE_INT("kern.cam.cd.poll_period", &cd_poll_period);
333 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
334 &cd_retry_count, 0, "Normal I/O retry count");
335 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
336 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RW,
337 &cd_timeout, 0, "Timeout, in us, for read operations");
338 TUNABLE_INT("kern.cam.cd.timeout", &cd_timeout);
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 struct mtx changerq_mtx;
361 static STAILQ_HEAD(changerlist, cdchanger) changerq;
362 static int num_changers;
364 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
371 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
372 STAILQ_INIT(&changerq);
375 * Install a global async callback. This callback will
376 * receive async callbacks like "new device found".
378 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
380 if (status != CAM_REQ_CMP) {
381 printf("cd: Failed to attach master async callback "
382 "due to status 0x%x!\n", status);
387 * Callback from GEOM, called when it has finished cleaning up its
391 cddiskgonecb(struct disk *dp)
393 struct cam_periph *periph;
395 periph = (struct cam_periph *)dp->d_drv1;
396 cam_periph_release(periph);
400 cdoninvalidate(struct cam_periph *periph)
402 struct cd_softc *softc;
404 softc = (struct cd_softc *)periph->softc;
407 * De-register any async callbacks.
409 xpt_register_async(0, cdasync, periph, periph->path);
411 softc->flags |= CD_FLAG_INVALID;
414 * Return all queued I/O with ENXIO.
415 * XXX Handle any transactions queued to the card
416 * with XPT_ABORT_CCB.
418 bioq_flush(&softc->bio_queue, NULL, ENXIO);
421 * If this device is part of a changer, and it was scheduled
422 * to run, remove it from the run queue since we just nuked
423 * all of its scheduled I/O.
425 if ((softc->flags & CD_FLAG_CHANGER)
426 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
427 camq_remove(&softc->changer->devq, softc->pinfo.index);
429 disk_gone(softc->disk);
433 cdcleanup(struct cam_periph *periph)
435 struct cd_softc *softc;
437 softc = (struct cd_softc *)periph->softc;
440 * In the queued, non-active case, the device in question
441 * has already been removed from the changer run queue. Since this
442 * device is active, we need to de-activate it, and schedule
443 * another device to run. (if there is another one to run)
445 if ((softc->flags & CD_FLAG_CHANGER)
446 && (softc->flags & CD_FLAG_ACTIVE)) {
449 * The purpose of the short timeout is soley to determine
450 * whether the current device has finished or not. Well,
451 * since we're removing the active device, we know that it
452 * is finished. So, get rid of the short timeout.
453 * Otherwise, if we're in the time period before the short
454 * timeout fires, and there are no other devices in the
455 * queue to run, there won't be any other device put in the
456 * active slot. i.e., when we call cdrunchangerqueue()
457 * below, it won't do anything. Then, when the short
458 * timeout fires, it'll look at the "current device", which
459 * we are free below, and possibly panic the kernel on a
460 * bogus pointer reference.
462 * The long timeout doesn't really matter, since we
463 * decrement the qfrozen_cnt to indicate that there is
464 * nothing in the active slot now. Therefore, there won't
465 * be any bogus pointer references there.
467 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
468 callout_stop(&softc->changer->short_handle);
469 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
471 softc->changer->devq.qfrozen_cnt--;
472 softc->changer->flags |= CHANGER_MANUAL_CALL;
473 cdrunchangerqueue(softc->changer);
477 * If we're removing the last device on the changer, go ahead and
478 * remove the changer device structure.
480 if ((softc->flags & CD_FLAG_CHANGER)
481 && (--softc->changer->num_devices == 0)) {
484 * Theoretically, there shouldn't be any timeouts left, but
485 * I'm not completely sure that that will be the case. So,
486 * it won't hurt to check and see if there are any left.
488 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
489 callout_stop(&softc->changer->long_handle);
490 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
493 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
494 callout_stop(&softc->changer->short_handle);
495 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
498 mtx_lock(&changerq_mtx);
499 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
502 mtx_unlock(&changerq_mtx);
503 xpt_print(periph->path, "removing changer entry\n");
504 free(softc->changer, M_DEVBUF);
506 cam_periph_unlock(periph);
507 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
508 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
509 xpt_print(periph->path, "can't remove sysctl context\n");
512 callout_drain(&softc->mediapoll_c);
513 disk_destroy(softc->disk);
514 free(softc, M_DEVBUF);
515 cam_periph_lock(periph);
519 cdasync(void *callback_arg, u_int32_t code,
520 struct cam_path *path, void *arg)
522 struct cam_periph *periph;
523 struct cd_softc *softc;
525 periph = (struct cam_periph *)callback_arg;
527 case AC_FOUND_DEVICE:
529 struct ccb_getdev *cgd;
532 cgd = (struct ccb_getdev *)arg;
536 if (cgd->protocol != PROTO_SCSI)
539 if (SID_TYPE(&cgd->inq_data) != T_CDROM
540 && SID_TYPE(&cgd->inq_data) != T_WORM)
544 * Allocate a peripheral instance for
545 * this device and start the probe
548 status = cam_periph_alloc(cdregister, cdoninvalidate,
550 "cd", CAM_PERIPH_BIO,
552 AC_FOUND_DEVICE, cgd);
554 if (status != CAM_REQ_CMP
555 && status != CAM_REQ_INPROG)
556 printf("cdasync: Unable to attach new device "
557 "due to status 0x%x\n", status);
561 case AC_UNIT_ATTENTION:
564 int error_code, sense_key, asc, ascq;
566 softc = (struct cd_softc *)periph->softc;
567 ccb = (union ccb *)arg;
570 * Handle all media change UNIT ATTENTIONs except
571 * our own, as they will be handled by cderror().
573 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
574 scsi_extract_sense_ccb(ccb,
575 &error_code, &sense_key, &asc, &ascq)) {
576 if (asc == 0x28 && ascq == 0x00)
577 disk_media_changed(softc->disk, M_NOWAIT);
579 cam_periph_async(periph, code, path, arg);
583 softc = (struct cd_softc *)periph->softc;
584 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
585 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
587 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
594 struct ccb_hdr *ccbh;
596 softc = (struct cd_softc *)periph->softc;
598 * Don't fail on the expected unit attention
601 softc->flags |= CD_FLAG_RETRY_UA;
602 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
603 ccbh->ccb_state |= CD_CCB_RETRY_UA;
607 cam_periph_async(periph, code, path, arg);
613 cdsysctlinit(void *context, int pending)
615 struct cam_periph *periph;
616 struct cd_softc *softc;
617 char tmpstr[80], tmpstr2[80];
619 periph = (struct cam_periph *)context;
620 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
623 softc = (struct cd_softc *)periph->softc;
624 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
625 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
627 sysctl_ctx_init(&softc->sysctl_ctx);
628 softc->flags |= CD_FLAG_SCTX_INIT;
629 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
630 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
631 tmpstr2, CTLFLAG_RD, 0, tmpstr);
633 if (softc->sysctl_tree == NULL) {
634 printf("cdsysctlinit: unable to allocate sysctl tree\n");
635 cam_periph_release(periph);
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",
648 cam_periph_release(periph);
652 * We have a handler function for this so we can check the values when the
653 * user sets them, instead of every time we look at them.
656 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
660 value = *(int *)arg1;
662 error = sysctl_handle_int(oidp, &value, 0, req);
665 || (req->newptr == NULL))
669 * The only real values we can have here are 6 or 10. I don't
670 * really forsee having 12 be an option at any time in the future.
671 * So if the user sets something less than or equal to 6, we'll set
672 * it to 6. If he sets something greater than 6, we'll set it to 10.
674 * I suppose we could just return an error here for the wrong values,
675 * but I don't think it's necessary to do so, as long as we can
676 * determine the user's intent without too much trouble.
683 *(int *)arg1 = value;
689 cdregister(struct cam_periph *periph, void *arg)
691 struct cd_softc *softc;
692 struct ccb_pathinq cpi;
693 struct ccb_getdev *cgd;
697 cgd = (struct ccb_getdev *)arg;
699 printf("cdregister: no getdev CCB, can't register device\n");
700 return(CAM_REQ_CMP_ERR);
703 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
706 printf("cdregister: Unable to probe new device. "
707 "Unable to allocate softc\n");
708 return(CAM_REQ_CMP_ERR);
711 LIST_INIT(&softc->pending_ccbs);
712 STAILQ_INIT(&softc->mode_queue);
713 softc->state = CD_STATE_PROBE;
714 bioq_init(&softc->bio_queue);
715 if (SID_IS_REMOVABLE(&cgd->inq_data))
716 softc->flags |= CD_FLAG_DISC_REMOVABLE;
718 periph->softc = softc;
719 softc->periph = 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 bzero(&cpi, sizeof(cpi));
736 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
737 cpi.ccb_h.func_code = XPT_PATH_INQ;
738 xpt_action((union ccb *)&cpi);
739 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
740 softc->quirks |= CD_Q_10_BYTE_ONLY;
742 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
744 /* The default is 6 byte commands, unless quirked otherwise */
745 if (softc->quirks & CD_Q_10_BYTE_ONLY)
746 softc->minimum_command_size = 10;
748 softc->minimum_command_size = 6;
751 * Refcount and block open attempts until we are setup
754 (void)cam_periph_hold(periph, PRIBIO);
755 cam_periph_unlock(periph);
757 * Load the user's default, if any.
759 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
760 periph->unit_number);
761 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
763 /* 6 and 10 are the only permissible values here. */
764 if (softc->minimum_command_size < 6)
765 softc->minimum_command_size = 6;
766 else if (softc->minimum_command_size > 6)
767 softc->minimum_command_size = 10;
770 * We need to register the statistics structure for this device,
771 * but we don't have the blocksize yet for it. So, we register
772 * the structure and indicate that we don't have the blocksize
773 * yet. Unlike other SCSI peripheral drivers, we explicitly set
774 * the device type here to be CDROM, rather than just ORing in
775 * the device type. This is because this driver can attach to either
776 * CDROM or WORM devices, and we want this peripheral driver to
777 * show up in the devstat list as a CD peripheral driver, not a
778 * WORM peripheral driver. WORM drives will also have the WORM
779 * driver attached to them.
781 softc->disk = disk_alloc();
782 softc->disk->d_devstat = devstat_new_entry("cd",
783 periph->unit_number, 0,
784 DEVSTAT_BS_UNAVAILABLE,
786 XPORT_DEVSTAT_TYPE(cpi.transport),
787 DEVSTAT_PRIORITY_CD);
788 softc->disk->d_open = cdopen;
789 softc->disk->d_close = cdclose;
790 softc->disk->d_strategy = cdstrategy;
791 softc->disk->d_gone = cddiskgonecb;
792 softc->disk->d_ioctl = cdioctl;
793 softc->disk->d_name = "cd";
794 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
795 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
796 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
797 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
798 cgd->inq_data.product, sizeof(cgd->inq_data.product),
799 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
800 softc->disk->d_unit = periph->unit_number;
801 softc->disk->d_drv1 = periph;
803 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
804 else if (cpi.maxio > MAXPHYS)
805 softc->disk->d_maxsize = MAXPHYS; /* for safety */
807 softc->disk->d_maxsize = cpi.maxio;
808 softc->disk->d_flags = 0;
809 softc->disk->d_hba_vendor = cpi.hba_vendor;
810 softc->disk->d_hba_device = cpi.hba_device;
811 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
812 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
815 * Acquire a reference to the periph before we register with GEOM.
816 * We'll release this reference once GEOM calls us back (via
817 * dadiskgonecb()) telling us that our provider has been freed.
819 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
820 xpt_print(periph->path, "%s: lost periph during "
821 "registration!\n", __func__);
822 cam_periph_lock(periph);
823 return (CAM_REQ_CMP_ERR);
826 disk_create(softc->disk, DISK_VERSION);
827 cam_periph_lock(periph);
830 * Add an async callback so that we get
831 * notified if this device goes away.
833 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
834 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
837 * If the target lun is greater than 0, we most likely have a CD
838 * changer device. Check the quirk entries as well, though, just
839 * in case someone has a CD tower with one lun per drive or
840 * something like that. Also, if we know up front that a
841 * particular device is a changer, we can mark it as such starting
842 * with lun 0, instead of lun 1. It shouldn't be necessary to have
843 * a quirk entry to define something as a changer, however.
845 if (((cgd->ccb_h.target_lun > 0)
846 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
847 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
848 struct cdchanger *nchanger;
849 struct cam_periph *nperiph;
850 struct cam_path *path;
854 /* Set the changer flag in the current device's softc */
855 softc->flags |= CD_FLAG_CHANGER;
858 * Now, look around for an existing changer device with the
859 * same path and target ID as the current device.
861 mtx_lock(&changerq_mtx);
863 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
865 nchanger = STAILQ_NEXT(nchanger, changer_links)){
866 if ((nchanger->path_id == cgd->ccb_h.path_id)
867 && (nchanger->target_id == cgd->ccb_h.target_id)) {
872 mtx_unlock(&changerq_mtx);
875 * If we found a matching entry, just add this device to
876 * the list of devices on this changer.
879 struct chdevlist *chlunhead;
881 chlunhead = &nchanger->chluns;
884 * XXX KDM look at consolidating this code with the
885 * code below in a separate function.
889 * Create a path with lun id 0, and see if we can
890 * find a matching device
892 status = xpt_create_path(&path, /*periph*/ periph,
894 cgd->ccb_h.target_id, 0);
896 if ((status == CAM_REQ_CMP)
897 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
898 struct cd_softc *nsoftc;
900 nsoftc = (struct cd_softc *)nperiph->softc;
902 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
903 nsoftc->flags |= CD_FLAG_CHANGER;
904 nchanger->num_devices++;
905 if (camq_resize(&nchanger->devq,
906 nchanger->num_devices)!=CAM_REQ_CMP){
907 printf("cdregister: "
913 nsoftc->changer = nchanger;
914 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
916 STAILQ_INSERT_TAIL(&nchanger->chluns,
917 nsoftc,changer_links);
920 } else if (status == CAM_REQ_CMP)
923 printf("cdregister: unable to allocate path\n"
924 "cdregister: changer support may be "
928 nchanger->num_devices++;
930 softc->changer = nchanger;
931 softc->pinfo.index = CAM_UNQUEUED_INDEX;
933 if (camq_resize(&nchanger->devq,
934 nchanger->num_devices) != CAM_REQ_CMP) {
935 printf("cdregister: camq_resize "
936 "failed, changer support may "
940 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
943 * In this case, we don't already have an entry for this
944 * particular changer, so we need to create one, add it to
945 * the queue, and queue this device on the list for this
946 * changer. Before we queue this device, however, we need
947 * to search for lun id 0 on this target, and add it to the
948 * queue first, if it exists. (and if it hasn't already
949 * been marked as part of the changer.)
952 nchanger = malloc(sizeof(struct cdchanger),
953 M_DEVBUF, M_NOWAIT | M_ZERO);
954 if (nchanger == NULL) {
955 softc->flags &= ~CD_FLAG_CHANGER;
956 printf("cdregister: unable to malloc "
957 "changer structure\ncdregister: "
958 "changer support disabled\n");
961 * Yes, gotos can be gross but in this case
962 * I think it's justified..
966 if (camq_init(&nchanger->devq, 1) != 0) {
967 softc->flags &= ~CD_FLAG_CHANGER;
968 printf("cdregister: changer support "
973 nchanger->path_id = cgd->ccb_h.path_id;
974 nchanger->target_id = cgd->ccb_h.target_id;
976 /* this is superfluous, but it makes things clearer */
977 nchanger->num_devices = 0;
979 STAILQ_INIT(&nchanger->chluns);
981 callout_init_mtx(&nchanger->long_handle,
982 cam_periph_mtx(periph), 0);
983 callout_init_mtx(&nchanger->short_handle,
984 cam_periph_mtx(periph), 0);
986 mtx_lock(&changerq_mtx);
988 STAILQ_INSERT_TAIL(&changerq, nchanger,
990 mtx_unlock(&changerq_mtx);
993 * Create a path with lun id 0, and see if we can
994 * find a matching device
996 status = xpt_create_path(&path, /*periph*/ periph,
998 cgd->ccb_h.target_id, 0);
1001 * If we were able to allocate the path, and if we
1002 * find a matching device and it isn't already
1003 * marked as part of a changer, then we add it to
1004 * the current changer.
1006 if ((status == CAM_REQ_CMP)
1007 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
1008 && ((((struct cd_softc *)periph->softc)->flags &
1009 CD_FLAG_CHANGER) == 0)) {
1010 struct cd_softc *nsoftc;
1012 nsoftc = (struct cd_softc *)nperiph->softc;
1014 nsoftc->flags |= CD_FLAG_CHANGER;
1015 nchanger->num_devices++;
1016 if (camq_resize(&nchanger->devq,
1017 nchanger->num_devices) != CAM_REQ_CMP) {
1018 printf("cdregister: camq_resize "
1019 "failed, changer support may "
1022 nsoftc->changer = nchanger;
1023 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
1025 STAILQ_INSERT_TAIL(&nchanger->chluns,
1026 nsoftc, changer_links);
1027 xpt_free_path(path);
1028 } else if (status == CAM_REQ_CMP)
1029 xpt_free_path(path);
1031 printf("cdregister: unable to allocate path\n"
1032 "cdregister: changer support may be "
1036 softc->changer = nchanger;
1037 softc->pinfo.index = CAM_UNQUEUED_INDEX;
1038 nchanger->num_devices++;
1039 if (camq_resize(&nchanger->devq,
1040 nchanger->num_devices) != CAM_REQ_CMP) {
1041 printf("cdregister: camq_resize "
1042 "failed, changer support may "
1045 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
1051 * Schedule a periodic media polling events.
1053 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
1054 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
1055 (softc->flags & CD_FLAG_CHANGER) == 0 &&
1056 (cgd->inq_flags & SID_AEN) == 0 &&
1057 cd_poll_period != 0)
1058 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
1059 cdmediapoll, periph);
1063 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1064 xpt_schedule(periph, CAM_PRIORITY_DEV);
1066 cdschedule(periph, CAM_PRIORITY_DEV);
1068 return(CAM_REQ_CMP);
1072 cdopen(struct disk *dp)
1074 struct cam_periph *periph;
1075 struct cd_softc *softc;
1078 periph = (struct cam_periph *)dp->d_drv1;
1079 softc = (struct cd_softc *)periph->softc;
1081 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1084 cam_periph_lock(periph);
1086 if (softc->flags & CD_FLAG_INVALID) {
1087 cam_periph_release_locked(periph);
1088 cam_periph_unlock(periph);
1092 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1093 cam_periph_release_locked(periph);
1094 cam_periph_unlock(periph);
1098 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1102 * Check for media, and set the appropriate flags. We don't bail
1103 * if we don't have media, but then we don't allow anything but the
1104 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1106 cdcheckmedia(periph);
1108 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1109 cam_periph_unhold(periph);
1111 cam_periph_unlock(periph);
1117 cdclose(struct disk *dp)
1119 struct cam_periph *periph;
1120 struct cd_softc *softc;
1122 periph = (struct cam_periph *)dp->d_drv1;
1123 softc = (struct cd_softc *)periph->softc;
1125 cam_periph_lock(periph);
1126 if (cam_periph_hold(periph, PRIBIO) != 0) {
1127 cam_periph_unlock(periph);
1128 cam_periph_release(periph);
1132 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1135 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1136 cdprevent(periph, PR_ALLOW);
1139 * Since we're closing this CD, mark the blocksize as unavailable.
1140 * It will be marked as available when the CD is opened again.
1142 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1145 * We'll check the media and toc again at the next open().
1147 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1149 cam_periph_unhold(periph);
1150 cam_periph_release_locked(periph);
1151 cam_periph_unlock(periph);
1157 cdshorttimeout(void *arg)
1159 struct cdchanger *changer;
1161 changer = (struct cdchanger *)arg;
1163 /* Always clear the short timeout flag, since that's what we're in */
1164 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1167 * Check to see if there is any more pending or outstanding I/O for
1168 * this device. If not, move it out of the active slot.
1170 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1171 && (changer->cur_device->outstanding_cmds == 0)) {
1172 changer->flags |= CHANGER_MANUAL_CALL;
1173 cdrunchangerqueue(changer);
1178 * This is a wrapper for xpt_schedule. It only applies to changers.
1181 cdschedule(struct cam_periph *periph, int priority)
1183 struct cd_softc *softc;
1185 softc = (struct cd_softc *)periph->softc;
1188 * If this device isn't currently queued, and if it isn't
1189 * the active device, then we queue this device and run the
1190 * changer queue if there is no timeout scheduled to do it.
1191 * If this device is the active device, just schedule it
1192 * to run again. If this device is queued, there should be
1193 * a timeout in place already that will make sure it runs.
1195 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1196 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1198 * We don't do anything with the priority here.
1199 * This is strictly a fifo queue.
1201 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1202 softc->pinfo.generation = ++softc->changer->devq.generation;
1203 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1206 * Since we just put a device in the changer queue,
1207 * check and see if there is a timeout scheduled for
1208 * this changer. If so, let the timeout handle
1209 * switching this device into the active slot. If
1210 * not, manually call the timeout routine to
1213 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1214 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1215 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1216 softc->changer->flags |= CHANGER_MANUAL_CALL;
1217 cdrunchangerqueue(softc->changer);
1219 } else if ((softc->flags & CD_FLAG_ACTIVE)
1220 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1221 xpt_schedule(periph, priority);
1225 cdrunchangerqueue(void *arg)
1227 struct cd_softc *softc;
1228 struct cdchanger *changer;
1229 int called_from_timeout;
1231 changer = (struct cdchanger *)arg;
1234 * If we have NOT been called from cdstrategy() or cddone(), and
1235 * instead from a timeout routine, go ahead and clear the
1238 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1239 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1240 called_from_timeout = 1;
1242 called_from_timeout = 0;
1244 /* Always clear the manual call flag */
1245 changer->flags &= ~CHANGER_MANUAL_CALL;
1247 /* nothing to do if the queue is empty */
1248 if (changer->devq.entries <= 0) {
1253 * If the changer queue is frozen, that means we have an active
1256 if (changer->devq.qfrozen_cnt > 0) {
1259 * We always need to reset the frozen count and clear the
1262 changer->devq.qfrozen_cnt--;
1263 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1264 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1266 if (changer->cur_device->outstanding_cmds > 0) {
1267 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1268 changer->cur_device->bufs_left =
1269 changer->cur_device->outstanding_cmds;
1270 if (called_from_timeout) {
1271 callout_reset(&changer->long_handle,
1272 changer_max_busy_seconds * hz,
1273 cdrunchangerqueue, changer);
1274 changer->flags |= CHANGER_TIMEOUT_SCHED;
1280 * Check to see whether the current device has any I/O left
1281 * to do. If so, requeue it at the end of the queue. If
1282 * not, there is no need to requeue it.
1284 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1286 changer->cur_device->pinfo.generation =
1287 ++changer->devq.generation;
1288 camq_insert(&changer->devq,
1289 (cam_pinfo *)changer->cur_device);
1293 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1295 changer->cur_device = softc;
1297 changer->devq.qfrozen_cnt++;
1298 softc->flags |= CD_FLAG_ACTIVE;
1300 /* Just in case this device is waiting */
1301 wakeup(&softc->changer);
1302 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1305 * Get rid of any pending timeouts, and set a flag to schedule new
1306 * ones so this device gets its full time quantum.
1308 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1309 callout_stop(&changer->long_handle);
1310 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1313 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1314 callout_stop(&changer->short_handle);
1315 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1319 * We need to schedule timeouts, but we only do this after the
1320 * first transaction has completed. This eliminates the changer
1323 changer->flags |= CHANGER_NEED_TIMEOUT;
1327 cdchangerschedule(struct cd_softc *softc)
1329 struct cdchanger *changer;
1331 changer = softc->changer;
1334 * If this is a changer, and this is the current device,
1335 * and this device has at least the minimum time quantum to
1336 * run, see if we can switch it out.
1338 if ((softc->flags & CD_FLAG_ACTIVE)
1339 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1340 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1342 * We try three things here. The first is that we
1343 * check to see whether the schedule on completion
1344 * flag is set. If it is, we decrement the number
1345 * of buffers left, and if it's zero, we reschedule.
1346 * Next, we check to see whether the pending buffer
1347 * queue is empty and whether there are no
1348 * outstanding transactions. If so, we reschedule.
1349 * Next, we see if the pending buffer queue is empty.
1350 * If it is, we set the number of buffers left to
1351 * the current active buffer count and set the
1352 * schedule on complete flag.
1354 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1355 if (--softc->bufs_left == 0) {
1356 softc->changer->flags |=
1357 CHANGER_MANUAL_CALL;
1358 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1359 cdrunchangerqueue(softc->changer);
1361 } else if ((bioq_first(&softc->bio_queue) == NULL)
1362 && (softc->outstanding_cmds == 0)) {
1363 softc->changer->flags |= CHANGER_MANUAL_CALL;
1364 cdrunchangerqueue(softc->changer);
1366 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1367 && (softc->flags & CD_FLAG_ACTIVE)) {
1370 * Now that the first transaction to this
1371 * particular device has completed, we can go ahead
1372 * and schedule our timeouts.
1374 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1375 callout_reset(&changer->long_handle,
1376 changer_max_busy_seconds * hz,
1377 cdrunchangerqueue, changer);
1378 changer->flags |= CHANGER_TIMEOUT_SCHED;
1380 printf("cdchangerschedule: already have a long"
1383 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1384 callout_reset(&changer->short_handle,
1385 changer_min_busy_seconds * hz,
1386 cdshorttimeout, changer);
1387 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1389 printf("cdchangerschedule: already have a short "
1393 * We just scheduled timeouts, no need to schedule
1396 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1402 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1403 u_int32_t cam_flags,
1404 u_int32_t sense_flags),
1405 u_int32_t cam_flags, u_int32_t sense_flags)
1407 struct cd_softc *softc;
1408 struct cam_periph *periph;
1411 periph = xpt_path_periph(ccb->ccb_h.path);
1412 softc = (struct cd_softc *)periph->softc;
1414 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1415 softc->disk->d_devstat);
1417 if (softc->flags & CD_FLAG_CHANGER)
1418 cdchangerschedule(softc);
1424 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1426 struct cd_softc *softc;
1428 softc = (struct cd_softc *)periph->softc;
1430 if (softc->flags & CD_FLAG_CHANGER) {
1432 * This should work the first time this device is woken up,
1433 * but just in case it doesn't, we use a while loop.
1435 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1437 * If this changer isn't already queued, queue it up.
1439 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1440 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1441 softc->pinfo.generation =
1442 ++softc->changer->devq.generation;
1443 camq_insert(&softc->changer->devq,
1444 (cam_pinfo *)softc);
1446 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1447 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1448 && ((softc->changer->flags
1449 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1450 softc->changer->flags |= CHANGER_MANUAL_CALL;
1451 cdrunchangerqueue(softc->changer);
1453 cam_periph_sleep(periph, &softc->changer,
1454 PRIBIO, "cgticb", 0);
1457 return(cam_periph_getccb(periph, priority));
1462 * Actually translate the requested transfer into one the physical driver
1463 * can understand. The transfer is described by a buf and will include
1464 * only one physical transfer.
1467 cdstrategy(struct bio *bp)
1469 struct cam_periph *periph;
1470 struct cd_softc *softc;
1472 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1473 cam_periph_lock(periph);
1474 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1475 ("cdstrategy(%p)\n", bp));
1477 softc = (struct cd_softc *)periph->softc;
1480 * If the device has been made invalid, error out
1482 if ((softc->flags & CD_FLAG_INVALID)) {
1483 cam_periph_unlock(periph);
1484 biofinish(bp, NULL, ENXIO);
1489 * If we don't have valid media, look for it before trying to
1492 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1495 error = cdcheckmedia(periph);
1497 cam_periph_unlock(periph);
1498 biofinish(bp, NULL, error);
1504 * Place it in the queue of disk activities for this disk
1506 bioq_disksort(&softc->bio_queue, bp);
1509 * Schedule ourselves for performing the work. We do things
1510 * differently for changers.
1512 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1513 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1515 cdschedule(periph, CAM_PRIORITY_NORMAL);
1517 cam_periph_unlock(periph);
1522 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1524 struct cd_softc *softc;
1526 struct ccb_scsiio *csio;
1527 struct scsi_read_capacity_data *rcap;
1529 softc = (struct cd_softc *)periph->softc;
1531 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1533 switch (softc->state) {
1534 case CD_STATE_NORMAL:
1536 bp = bioq_first(&softc->bio_queue);
1540 csio = &start_ccb->csio;
1541 scsi_test_unit_ready(csio,
1542 /*retries*/ cd_retry_count,
1547 start_ccb->ccb_h.ccb_bp = NULL;
1548 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
1549 xpt_action(start_ccb);
1551 xpt_release_ccb(start_ccb);
1555 cam_periph_release_locked(periph);
1557 bioq_remove(&softc->bio_queue, bp);
1559 scsi_read_write(&start_ccb->csio,
1560 /*retries*/ cd_retry_count,
1561 /* cbfcnp */ cddone,
1563 /* read */bp->bio_cmd == BIO_READ ?
1564 SCSI_RW_READ : SCSI_RW_WRITE,
1566 /* minimum_cmd_size */ 10,
1567 /* lba */ bp->bio_offset /
1568 softc->params.blksize,
1569 bp->bio_bcount / softc->params.blksize,
1570 /* data_ptr */ bp->bio_data,
1571 /* dxfer_len */ bp->bio_bcount,
1572 /* sense_len */ cd_retry_count ?
1573 SSD_FULL_SIZE : SF_NO_PRINT,
1574 /* timeout */ cd_timeout);
1575 /* Use READ CD command for audio tracks. */
1576 if (softc->params.blksize == 2352) {
1577 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1578 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1579 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1580 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1581 start_ccb->csio.cdb_len = 12;
1583 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1586 LIST_INSERT_HEAD(&softc->pending_ccbs,
1587 &start_ccb->ccb_h, periph_links.le);
1588 softc->outstanding_cmds++;
1590 /* We expect a unit attention from this device */
1591 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1592 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1593 softc->flags &= ~CD_FLAG_RETRY_UA;
1596 start_ccb->ccb_h.ccb_bp = bp;
1597 bp = bioq_first(&softc->bio_queue);
1599 xpt_action(start_ccb);
1601 if (bp != NULL || softc->tur) {
1602 /* Have more work to do, so ensure we stay scheduled */
1603 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1607 case CD_STATE_PROBE:
1610 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1611 M_SCSICD, M_NOWAIT | M_ZERO);
1613 xpt_print(periph->path,
1614 "cdstart: Couldn't malloc read_capacity data\n");
1615 /* cd_free_periph??? */
1618 csio = &start_ccb->csio;
1619 scsi_read_capacity(csio,
1620 /*retries*/ cd_retry_count,
1626 start_ccb->ccb_h.ccb_bp = NULL;
1627 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1628 xpt_action(start_ccb);
1635 cddone(struct cam_periph *periph, union ccb *done_ccb)
1637 struct cd_softc *softc;
1638 struct ccb_scsiio *csio;
1640 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1642 softc = (struct cd_softc *)periph->softc;
1643 csio = &done_ccb->csio;
1645 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1646 case CD_CCB_BUFFER_IO:
1651 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1654 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1657 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1662 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1663 if (error == ERESTART) {
1665 * A retry was scheuled, so
1673 xpt_print(periph->path,
1674 "cddone: got error %#x back\n", error);
1675 bioq_flush(&softc->bio_queue, NULL, EIO);
1676 bp->bio_resid = bp->bio_bcount;
1677 bp->bio_error = error;
1678 bp->bio_flags |= BIO_ERROR;
1679 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1680 cam_release_devq(done_ccb->ccb_h.path,
1684 /*getcount_only*/0);
1687 bp->bio_resid = csio->resid;
1689 if (bp->bio_resid != 0) {
1691 * Short transfer ???
1692 * XXX: not sure this is correct for partial
1695 bp->bio_flags |= BIO_ERROR;
1699 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1700 softc->outstanding_cmds--;
1702 if (softc->flags & CD_FLAG_CHANGER)
1703 cdchangerschedule(softc);
1705 biofinish(bp, NULL, 0);
1710 struct scsi_read_capacity_data *rdcap;
1711 char announce_buf[120]; /*
1712 * Currently (9/30/97) the
1713 * longest possible announce
1714 * buffer is 108 bytes, for the
1715 * first error case below.
1716 * That is 39 bytes for the
1717 * basic string, 16 bytes for the
1718 * biggest sense key (hardware
1719 * error), 52 bytes for the
1720 * text of the largest sense
1721 * qualifier valid for a CDROM,
1722 * (0x72, 0x03 or 0x04,
1723 * 0x03), and one byte for the
1724 * null terminating character.
1725 * To allow for longer strings,
1726 * the announce buffer is 120
1729 struct cd_params *cdp;
1732 cdp = &softc->params;
1734 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1736 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1737 cdp->blksize = scsi_4btoul (rdcap->length);
1740 * Retry any UNIT ATTENTION type errors. They
1741 * are expected at boot.
1743 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1744 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1745 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1747 snprintf(announce_buf, sizeof(announce_buf),
1748 "cd present [%lu x %lu byte records]",
1749 cdp->disksize, (u_long)cdp->blksize);
1752 if (error == ERESTART) {
1754 * A retry was scheuled, so
1760 int sense_key, error_code;
1763 struct ccb_getdev cgd;
1765 /* Don't wedge this device's queue */
1766 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1767 cam_release_devq(done_ccb->ccb_h.path,
1771 /*getcount_only*/0);
1773 status = done_ccb->ccb_h.status;
1775 xpt_setup_ccb(&cgd.ccb_h,
1776 done_ccb->ccb_h.path,
1777 CAM_PRIORITY_NORMAL);
1778 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1779 xpt_action((union ccb *)&cgd);
1781 if (scsi_extract_sense_ccb(done_ccb,
1782 &error_code, &sense_key, &asc, &ascq))
1788 * Attach to anything that claims to be a
1789 * CDROM or WORM device, as long as it
1790 * doesn't return a "Logical unit not
1791 * supported" (0x25) error.
1793 if ((have_sense) && (asc != 0x25)
1794 && (error_code == SSD_CURRENT_ERROR)) {
1795 const char *sense_key_desc;
1796 const char *asc_desc;
1798 scsi_sense_desc(sense_key, asc, ascq,
1802 snprintf(announce_buf,
1803 sizeof(announce_buf),
1804 "Attempt to query device "
1805 "size failed: %s, %s",
1808 } else if ((have_sense == 0)
1809 && ((status & CAM_STATUS_MASK) ==
1810 CAM_SCSI_STATUS_ERROR)
1811 && (csio->scsi_status ==
1812 SCSI_STATUS_BUSY)) {
1813 snprintf(announce_buf,
1814 sizeof(announce_buf),
1815 "Attempt to query device "
1816 "size failed: SCSI Status: %s",
1817 scsi_status_string(csio));
1818 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1820 * We only print out an error for
1821 * CDROM type devices. For WORM
1822 * devices, we don't print out an
1823 * error since a few WORM devices
1824 * don't support CDROM commands.
1825 * If we have sense information, go
1826 * ahead and print it out.
1827 * Otherwise, just say that we
1832 * Just print out the error, not
1833 * the full probe message, when we
1840 xpt_print(periph->path,
1841 "got CAM status %#x\n",
1842 done_ccb->ccb_h.status);
1844 xpt_print(periph->path, "fatal error, "
1845 "failed to attach to device\n");
1847 * Invalidate this peripheral.
1849 cam_periph_invalidate(periph);
1851 announce_buf[0] = '\0';
1855 * Invalidate this peripheral.
1857 cam_periph_invalidate(periph);
1858 announce_buf[0] = '\0';
1862 free(rdcap, M_SCSICD);
1863 if (announce_buf[0] != '\0') {
1864 xpt_announce_periph(periph, announce_buf);
1865 xpt_announce_quirks(periph, softc->quirks,
1867 if (softc->flags & CD_FLAG_CHANGER)
1868 cdchangerschedule(softc);
1870 * Create our sysctl variables, now that we know
1871 * we have successfully attached.
1873 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1875 softc->state = CD_STATE_NORMAL;
1877 * Since our peripheral may be invalidated by an error
1878 * above or an external event, we must release our CCB
1879 * before releasing the probe lock on the peripheral.
1880 * The peripheral will only go away once the last lock
1881 * is removed, and we need it around for the CCB release
1884 xpt_release_ccb(done_ccb);
1885 cam_periph_unhold(periph);
1890 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1892 if (cderror(done_ccb, CAM_RETRY_SELTO,
1893 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1896 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1897 cam_release_devq(done_ccb->ccb_h.path,
1901 /*getcount_only*/0);
1903 xpt_release_ccb(done_ccb);
1904 cam_periph_release_locked(periph);
1910 xpt_release_ccb(done_ccb);
1913 static union cd_pages *
1914 cdgetpage(struct cd_mode_params *mode_params)
1916 union cd_pages *page;
1918 if (mode_params->cdb_size == 10)
1919 page = (union cd_pages *)find_mode_page_10(
1920 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1922 page = (union cd_pages *)find_mode_page_6(
1923 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1929 cdgetpagesize(int page_num)
1933 for (i = 0; i < (sizeof(cd_page_size_table)/
1934 sizeof(cd_page_size_table[0])); i++) {
1935 if (cd_page_size_table[i].page == page_num)
1936 return (cd_page_size_table[i].page_size);
1943 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1946 struct cam_periph *periph;
1947 struct cd_softc *softc;
1948 int nocopyout, error = 0;
1950 periph = (struct cam_periph *)dp->d_drv1;
1951 cam_periph_lock(periph);
1953 softc = (struct cd_softc *)periph->softc;
1955 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1956 ("cdioctl(%#lx)\n", cmd));
1958 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1959 cam_periph_unlock(periph);
1960 cam_periph_release(periph);
1965 * If we don't have media loaded, check for it. If still don't
1966 * have media loaded, we can only do a load or eject.
1968 * We only care whether media is loaded if this is a cd-specific ioctl
1969 * (thus the IOCGROUP check below). Note that this will break if
1970 * anyone adds any ioctls into the switch statement below that don't
1971 * have their ioctl group set to 'c'.
1973 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1974 && ((cmd != CDIOCCLOSE)
1975 && (cmd != CDIOCEJECT))
1976 && (IOCGROUP(cmd) == 'c')) {
1977 error = cdcheckmedia(periph);
1979 cam_periph_unhold(periph);
1980 cam_periph_unlock(periph);
1985 * Drop the lock here so later mallocs can use WAITOK. The periph
1986 * is essentially locked still with the cam_periph_hold call above.
1988 cam_periph_unlock(periph);
1993 case CDIOCPLAYTRACKS:
1995 struct ioc_play_track *args
1996 = (struct ioc_play_track *) addr;
1997 struct cd_mode_params params;
1998 union cd_pages *page;
2000 params.alloc_len = sizeof(union cd_mode_data_6_10);
2001 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2004 cam_periph_lock(periph);
2005 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2006 ("trying to do CDIOCPLAYTRACKS\n"));
2008 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2010 free(params.mode_buf, M_SCSICD);
2011 cam_periph_unlock(periph);
2014 page = cdgetpage(¶ms);
2016 page->audio.flags &= ~CD_PA_SOTC;
2017 page->audio.flags |= CD_PA_IMMED;
2018 error = cdsetmode(periph, ¶ms);
2019 free(params.mode_buf, M_SCSICD);
2021 cam_periph_unlock(periph);
2026 * This was originally implemented with the PLAY
2027 * AUDIO TRACK INDEX command, but that command was
2028 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2029 * drives support it but ATAPI and ATAPI-derivative
2030 * drives don't seem to support it. So we keep a
2031 * cache of the table of contents and translate
2032 * track numbers to MSF format.
2034 if (softc->flags & CD_FLAG_VALID_TOC) {
2035 union msf_lba *sentry, *eentry;
2038 if (args->end_track <
2039 softc->toc.header.ending_track + 1)
2041 if (args->end_track >
2042 softc->toc.header.ending_track + 1)
2044 softc->toc.header.ending_track + 1;
2045 st = args->start_track -
2046 softc->toc.header.starting_track;
2047 et = args->end_track -
2048 softc->toc.header.starting_track;
2051 || (st > (softc->toc.header.ending_track -
2052 softc->toc.header.starting_track))) {
2054 cam_periph_unlock(periph);
2057 sentry = &softc->toc.entries[st].addr;
2058 eentry = &softc->toc.entries[et].addr;
2059 error = cdplaymsf(periph,
2068 * If we don't have a valid TOC, try the
2069 * play track index command. It is part of
2070 * the SCSI-2 spec, but was removed in the
2071 * MMC specs. ATAPI and ATAPI-derived
2072 * drives don't support it.
2074 if (softc->quirks & CD_Q_BCD_TRACKS) {
2076 bin2bcd(args->start_track);
2078 bin2bcd(args->end_track);
2080 error = cdplaytracks(periph,
2086 cam_periph_unlock(periph);
2091 struct ioc_play_msf *args
2092 = (struct ioc_play_msf *) addr;
2093 struct cd_mode_params params;
2094 union cd_pages *page;
2096 params.alloc_len = sizeof(union cd_mode_data_6_10);
2097 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2100 cam_periph_lock(periph);
2101 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2102 ("trying to do CDIOCPLAYMSF\n"));
2104 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2106 free(params.mode_buf, M_SCSICD);
2107 cam_periph_unlock(periph);
2110 page = cdgetpage(¶ms);
2112 page->audio.flags &= ~CD_PA_SOTC;
2113 page->audio.flags |= CD_PA_IMMED;
2114 error = cdsetmode(periph, ¶ms);
2115 free(params.mode_buf, M_SCSICD);
2117 cam_periph_unlock(periph);
2120 error = cdplaymsf(periph,
2127 cam_periph_unlock(periph);
2130 case CDIOCPLAYBLOCKS:
2132 struct ioc_play_blocks *args
2133 = (struct ioc_play_blocks *) addr;
2134 struct cd_mode_params params;
2135 union cd_pages *page;
2137 params.alloc_len = sizeof(union cd_mode_data_6_10);
2138 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2141 cam_periph_lock(periph);
2142 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2143 ("trying to do CDIOCPLAYBLOCKS\n"));
2146 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2148 free(params.mode_buf, M_SCSICD);
2149 cam_periph_unlock(periph);
2152 page = cdgetpage(¶ms);
2154 page->audio.flags &= ~CD_PA_SOTC;
2155 page->audio.flags |= CD_PA_IMMED;
2156 error = cdsetmode(periph, ¶ms);
2157 free(params.mode_buf, M_SCSICD);
2159 cam_periph_unlock(periph);
2162 error = cdplay(periph, args->blk, args->len);
2163 cam_periph_unlock(periph);
2166 case CDIOCREADSUBCHANNEL_SYSSPACE:
2169 case CDIOCREADSUBCHANNEL:
2171 struct ioc_read_subchannel *args
2172 = (struct ioc_read_subchannel *) addr;
2173 struct cd_sub_channel_info *data;
2174 u_int32_t len = args->data_len;
2176 data = malloc(sizeof(struct cd_sub_channel_info),
2177 M_SCSICD, M_WAITOK | M_ZERO);
2179 cam_periph_lock(periph);
2180 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2181 ("trying to do CDIOCREADSUBCHANNEL\n"));
2183 if ((len > sizeof(struct cd_sub_channel_info)) ||
2184 (len < sizeof(struct cd_sub_channel_header))) {
2186 "scsi_cd: cdioctl: "
2187 "cdioreadsubchannel: error, len=%d\n",
2190 free(data, M_SCSICD);
2191 cam_periph_unlock(periph);
2195 if (softc->quirks & CD_Q_BCD_TRACKS)
2196 args->track = bin2bcd(args->track);
2198 error = cdreadsubchannel(periph, args->address_format,
2199 args->data_format, args->track, data, len);
2202 free(data, M_SCSICD);
2203 cam_periph_unlock(periph);
2206 if (softc->quirks & CD_Q_BCD_TRACKS)
2207 data->what.track_info.track_number =
2208 bcd2bin(data->what.track_info.track_number);
2209 len = min(len, ((data->header.data_len[0] << 8) +
2210 data->header.data_len[1] +
2211 sizeof(struct cd_sub_channel_header)));
2212 cam_periph_unlock(periph);
2213 if (nocopyout == 0) {
2214 if (copyout(data, args->data, len) != 0) {
2218 bcopy(data, args->data, len);
2220 free(data, M_SCSICD);
2224 case CDIOREADTOCHEADER:
2226 struct ioc_toc_header *th;
2228 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2231 cam_periph_lock(periph);
2232 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2233 ("trying to do CDIOREADTOCHEADER\n"));
2235 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2236 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2239 cam_periph_unlock(periph);
2242 if (softc->quirks & CD_Q_BCD_TRACKS) {
2243 /* we are going to have to convert the BCD
2244 * encoding on the cd to what is expected
2246 th->starting_track =
2247 bcd2bin(th->starting_track);
2248 th->ending_track = bcd2bin(th->ending_track);
2250 th->len = ntohs(th->len);
2251 bcopy(th, addr, sizeof(*th));
2253 cam_periph_unlock(periph);
2256 case CDIOREADTOCENTRYS:
2258 struct cd_tocdata *data;
2259 struct cd_toc_single *lead;
2260 struct ioc_read_toc_entry *te =
2261 (struct ioc_read_toc_entry *) addr;
2262 struct ioc_toc_header *th;
2263 u_int32_t len, readlen, idx, num;
2264 u_int32_t starting_track = te->starting_track;
2266 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2267 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2269 cam_periph_lock(periph);
2270 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2271 ("trying to do CDIOREADTOCENTRYS\n"));
2273 if (te->data_len < sizeof(struct cd_toc_entry)
2274 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2275 || (te->address_format != CD_MSF_FORMAT
2276 && te->address_format != CD_LBA_FORMAT)) {
2278 printf("scsi_cd: error in readtocentries, "
2279 "returning EINVAL\n");
2280 free(data, M_SCSICD);
2281 free(lead, M_SCSICD);
2282 cam_periph_unlock(periph);
2287 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2288 sizeof (*th), /*sense_flags*/0);
2290 free(data, M_SCSICD);
2291 free(lead, M_SCSICD);
2292 cam_periph_unlock(periph);
2296 if (softc->quirks & CD_Q_BCD_TRACKS) {
2297 /* we are going to have to convert the BCD
2298 * encoding on the cd to what is expected
2300 th->starting_track =
2301 bcd2bin(th->starting_track);
2302 th->ending_track = bcd2bin(th->ending_track);
2305 if (starting_track == 0)
2306 starting_track = th->starting_track;
2307 else if (starting_track == LEADOUT)
2308 starting_track = th->ending_track + 1;
2309 else if (starting_track < th->starting_track ||
2310 starting_track > th->ending_track + 1) {
2311 printf("scsi_cd: error in readtocentries, "
2312 "returning EINVAL\n");
2313 free(data, M_SCSICD);
2314 free(lead, M_SCSICD);
2315 cam_periph_unlock(periph);
2320 /* calculate reading length without leadout entry */
2321 readlen = (th->ending_track - starting_track + 1) *
2322 sizeof(struct cd_toc_entry);
2324 /* and with leadout entry */
2325 len = readlen + sizeof(struct cd_toc_entry);
2326 if (te->data_len < len) {
2331 if (len > sizeof(data->entries)) {
2332 printf("scsi_cd: error in readtocentries, "
2333 "returning EINVAL\n");
2335 free(data, M_SCSICD);
2336 free(lead, M_SCSICD);
2337 cam_periph_unlock(periph);
2340 num = len / sizeof(struct cd_toc_entry);
2343 error = cdreadtoc(periph, te->address_format,
2346 readlen + sizeof (*th),
2349 free(data, M_SCSICD);
2350 free(lead, M_SCSICD);
2351 cam_periph_unlock(periph);
2356 /* make leadout entry if needed */
2357 idx = starting_track + num - 1;
2358 if (softc->quirks & CD_Q_BCD_TRACKS)
2359 th->ending_track = bcd2bin(th->ending_track);
2360 if (idx == th->ending_track + 1) {
2361 error = cdreadtoc(periph, te->address_format,
2362 LEADOUT, (u_int8_t *)lead,
2366 free(data, M_SCSICD);
2367 free(lead, M_SCSICD);
2368 cam_periph_unlock(periph);
2371 data->entries[idx - starting_track] =
2374 if (softc->quirks & CD_Q_BCD_TRACKS) {
2375 for (idx = 0; idx < num - 1; idx++) {
2376 data->entries[idx].track =
2377 bcd2bin(data->entries[idx].track);
2381 cam_periph_unlock(periph);
2382 error = copyout(data->entries, te->data, len);
2383 free(data, M_SCSICD);
2384 free(lead, M_SCSICD);
2387 case CDIOREADTOCENTRY:
2389 struct cd_toc_single *data;
2390 struct ioc_read_toc_single_entry *te =
2391 (struct ioc_read_toc_single_entry *) addr;
2392 struct ioc_toc_header *th;
2395 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2397 cam_periph_lock(periph);
2398 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2399 ("trying to do CDIOREADTOCENTRY\n"));
2401 if (te->address_format != CD_MSF_FORMAT
2402 && te->address_format != CD_LBA_FORMAT) {
2403 printf("error in readtocentry, "
2404 " returning EINVAL\n");
2405 free(data, M_SCSICD);
2407 cam_periph_unlock(periph);
2412 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2413 sizeof (*th), /*sense_flags*/0);
2415 free(data, M_SCSICD);
2416 cam_periph_unlock(periph);
2420 if (softc->quirks & CD_Q_BCD_TRACKS) {
2421 /* we are going to have to convert the BCD
2422 * encoding on the cd to what is expected
2424 th->starting_track =
2425 bcd2bin(th->starting_track);
2426 th->ending_track = bcd2bin(th->ending_track);
2430 track = th->starting_track;
2431 else if (track == LEADOUT)
2433 else if (track < th->starting_track ||
2434 track > th->ending_track + 1) {
2435 printf("error in readtocentry, "
2436 " returning EINVAL\n");
2437 free(data, M_SCSICD);
2439 cam_periph_unlock(periph);
2443 error = cdreadtoc(periph, te->address_format, track,
2444 (u_int8_t *)data, sizeof(*data),
2447 free(data, M_SCSICD);
2448 cam_periph_unlock(periph);
2452 if (softc->quirks & CD_Q_BCD_TRACKS)
2453 data->entry.track = bcd2bin(data->entry.track);
2454 bcopy(&data->entry, &te->entry,
2455 sizeof(struct cd_toc_entry));
2456 free(data, M_SCSICD);
2457 cam_periph_unlock(periph);
2462 struct ioc_patch *arg = (struct ioc_patch *)addr;
2463 struct cd_mode_params params;
2464 union cd_pages *page;
2466 params.alloc_len = sizeof(union cd_mode_data_6_10);
2467 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2470 cam_periph_lock(periph);
2471 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2472 ("trying to do CDIOCSETPATCH\n"));
2474 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2476 free(params.mode_buf, M_SCSICD);
2477 cam_periph_unlock(periph);
2480 page = cdgetpage(¶ms);
2482 page->audio.port[LEFT_PORT].channels =
2484 page->audio.port[RIGHT_PORT].channels =
2486 page->audio.port[2].channels = arg->patch[2];
2487 page->audio.port[3].channels = arg->patch[3];
2488 error = cdsetmode(periph, ¶ms);
2489 free(params.mode_buf, M_SCSICD);
2490 cam_periph_unlock(periph);
2495 struct ioc_vol *arg = (struct ioc_vol *) addr;
2496 struct cd_mode_params params;
2497 union cd_pages *page;
2499 params.alloc_len = sizeof(union cd_mode_data_6_10);
2500 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2503 cam_periph_lock(periph);
2504 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2505 ("trying to do CDIOCGETVOL\n"));
2507 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2509 free(params.mode_buf, M_SCSICD);
2510 cam_periph_unlock(periph);
2513 page = cdgetpage(¶ms);
2515 arg->vol[LEFT_PORT] =
2516 page->audio.port[LEFT_PORT].volume;
2517 arg->vol[RIGHT_PORT] =
2518 page->audio.port[RIGHT_PORT].volume;
2519 arg->vol[2] = page->audio.port[2].volume;
2520 arg->vol[3] = page->audio.port[3].volume;
2521 free(params.mode_buf, M_SCSICD);
2522 cam_periph_unlock(periph);
2527 struct ioc_vol *arg = (struct ioc_vol *) addr;
2528 struct cd_mode_params params;
2529 union cd_pages *page;
2531 params.alloc_len = sizeof(union cd_mode_data_6_10);
2532 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2535 cam_periph_lock(periph);
2536 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2537 ("trying to do CDIOCSETVOL\n"));
2539 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2541 free(params.mode_buf, M_SCSICD);
2542 cam_periph_unlock(periph);
2545 page = cdgetpage(¶ms);
2547 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2548 page->audio.port[LEFT_PORT].volume =
2549 arg->vol[LEFT_PORT];
2550 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2551 page->audio.port[RIGHT_PORT].volume =
2552 arg->vol[RIGHT_PORT];
2553 page->audio.port[2].volume = arg->vol[2];
2554 page->audio.port[3].volume = arg->vol[3];
2555 error = cdsetmode(periph, ¶ms);
2556 cam_periph_unlock(periph);
2557 free(params.mode_buf, M_SCSICD);
2562 struct cd_mode_params params;
2563 union cd_pages *page;
2565 params.alloc_len = sizeof(union cd_mode_data_6_10);
2566 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2569 cam_periph_lock(periph);
2570 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2571 ("trying to do CDIOCSETMONO\n"));
2573 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2575 free(params.mode_buf, M_SCSICD);
2576 cam_periph_unlock(periph);
2579 page = cdgetpage(¶ms);
2581 page->audio.port[LEFT_PORT].channels =
2582 LEFT_CHANNEL | RIGHT_CHANNEL;
2583 page->audio.port[RIGHT_PORT].channels =
2584 LEFT_CHANNEL | RIGHT_CHANNEL;
2585 page->audio.port[2].channels = 0;
2586 page->audio.port[3].channels = 0;
2587 error = cdsetmode(periph, ¶ms);
2588 cam_periph_unlock(periph);
2589 free(params.mode_buf, M_SCSICD);
2592 case CDIOCSETSTEREO:
2594 struct cd_mode_params params;
2595 union cd_pages *page;
2597 params.alloc_len = sizeof(union cd_mode_data_6_10);
2598 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2601 cam_periph_lock(periph);
2602 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2603 ("trying to do CDIOCSETSTEREO\n"));
2605 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2607 free(params.mode_buf, M_SCSICD);
2608 cam_periph_unlock(periph);
2611 page = cdgetpage(¶ms);
2613 page->audio.port[LEFT_PORT].channels =
2615 page->audio.port[RIGHT_PORT].channels =
2617 page->audio.port[2].channels = 0;
2618 page->audio.port[3].channels = 0;
2619 error = cdsetmode(periph, ¶ms);
2620 free(params.mode_buf, M_SCSICD);
2621 cam_periph_unlock(periph);
2626 struct cd_mode_params params;
2627 union cd_pages *page;
2629 params.alloc_len = sizeof(union cd_mode_data_6_10);
2630 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2633 cam_periph_lock(periph);
2634 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2635 ("trying to do CDIOCSETMUTE\n"));
2637 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2639 free(params.mode_buf, M_SCSICD);
2640 cam_periph_unlock(periph);
2643 page = cdgetpage(¶ms);
2645 page->audio.port[LEFT_PORT].channels = 0;
2646 page->audio.port[RIGHT_PORT].channels = 0;
2647 page->audio.port[2].channels = 0;
2648 page->audio.port[3].channels = 0;
2649 error = cdsetmode(periph, ¶ms);
2650 free(params.mode_buf, M_SCSICD);
2651 cam_periph_unlock(periph);
2656 struct cd_mode_params params;
2657 union cd_pages *page;
2659 params.alloc_len = sizeof(union cd_mode_data_6_10);
2660 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2663 cam_periph_lock(periph);
2664 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2665 ("trying to do CDIOCSETLEFT\n"));
2667 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2669 free(params.mode_buf, M_SCSICD);
2670 cam_periph_unlock(periph);
2673 page = cdgetpage(¶ms);
2675 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2676 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2677 page->audio.port[2].channels = 0;
2678 page->audio.port[3].channels = 0;
2679 error = cdsetmode(periph, ¶ms);
2680 free(params.mode_buf, M_SCSICD);
2681 cam_periph_unlock(periph);
2686 struct cd_mode_params params;
2687 union cd_pages *page;
2689 params.alloc_len = sizeof(union cd_mode_data_6_10);
2690 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2693 cam_periph_lock(periph);
2694 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2695 ("trying to do CDIOCSETRIGHT\n"));
2697 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2699 free(params.mode_buf, M_SCSICD);
2700 cam_periph_unlock(periph);
2703 page = cdgetpage(¶ms);
2705 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2706 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2707 page->audio.port[2].channels = 0;
2708 page->audio.port[3].channels = 0;
2709 error = cdsetmode(periph, ¶ms);
2710 free(params.mode_buf, M_SCSICD);
2711 cam_periph_unlock(periph);
2715 cam_periph_lock(periph);
2716 error = cdpause(periph, 1);
2717 cam_periph_unlock(periph);
2720 cam_periph_lock(periph);
2721 error = cdpause(periph, 0);
2722 cam_periph_unlock(periph);
2725 cam_periph_lock(periph);
2726 error = cdstartunit(periph, 0);
2727 cam_periph_unlock(periph);
2730 cam_periph_lock(periph);
2731 error = cdstartunit(periph, 1);
2732 cam_periph_unlock(periph);
2735 cam_periph_lock(periph);
2736 error = cdstopunit(periph, 0);
2737 cam_periph_unlock(periph);
2740 cam_periph_lock(periph);
2741 error = cdstopunit(periph, 1);
2742 cam_periph_unlock(periph);
2745 cam_periph_lock(periph);
2746 cdprevent(periph, PR_ALLOW);
2747 cam_periph_unlock(periph);
2750 cam_periph_lock(periph);
2751 cdprevent(periph, PR_PREVENT);
2752 cam_periph_unlock(periph);
2755 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2759 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2763 /* return (cd_reset(periph)); */
2766 case CDRIOCREADSPEED:
2767 cam_periph_lock(periph);
2768 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2769 cam_periph_unlock(periph);
2771 case CDRIOCWRITESPEED:
2772 cam_periph_lock(periph);
2773 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2774 cam_periph_unlock(periph);
2776 case CDRIOCGETBLOCKSIZE:
2777 *(int *)addr = softc->params.blksize;
2779 case CDRIOCSETBLOCKSIZE:
2780 if (*(int *)addr <= 0) {
2784 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2787 case DVDIOCREPORTKEY: {
2788 struct dvd_authinfo *authinfo;
2790 authinfo = (struct dvd_authinfo *)addr;
2792 if (cmd == DVDIOCREPORTKEY)
2793 error = cdreportkey(periph, authinfo);
2795 error = cdsendkey(periph, authinfo);
2798 case DVDIOCREADSTRUCTURE: {
2799 struct dvd_struct *dvdstruct;
2801 dvdstruct = (struct dvd_struct *)addr;
2803 error = cdreaddvdstructure(periph, dvdstruct);
2808 cam_periph_lock(periph);
2809 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2810 cam_periph_unlock(periph);
2814 cam_periph_lock(periph);
2815 cam_periph_unhold(periph);
2817 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2818 if (error && bootverbose) {
2819 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2821 cam_periph_unlock(periph);
2827 cdprevent(struct cam_periph *periph, int action)
2830 struct cd_softc *softc;
2833 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2835 softc = (struct cd_softc *)periph->softc;
2837 if (((action == PR_ALLOW)
2838 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2839 || ((action == PR_PREVENT)
2840 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2844 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2846 scsi_prevent(&ccb->csio,
2847 /*retries*/ cd_retry_count,
2852 /* timeout */60000);
2854 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2855 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2857 xpt_release_ccb(ccb);
2860 if (action == PR_ALLOW)
2861 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2863 softc->flags |= CD_FLAG_DISC_LOCKED;
2868 * XXX: the disk media and sector size is only really able to change
2869 * XXX: while the device is closed.
2872 cdcheckmedia(struct cam_periph *periph)
2874 struct cd_softc *softc;
2875 struct ioc_toc_header *toch;
2876 struct cd_toc_single leadout;
2877 u_int32_t size, toclen;
2878 int error, num_entries, cdindex;
2880 softc = (struct cd_softc *)periph->softc;
2882 cdprevent(periph, PR_PREVENT);
2883 softc->disk->d_sectorsize = 2048;
2884 softc->disk->d_mediasize = 0;
2887 * Get the disc size and block size. If we can't get it, we don't
2888 * have media, most likely.
2890 if ((error = cdsize(periph, &size)) != 0) {
2891 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2892 cdprevent(periph, PR_ALLOW);
2895 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2896 softc->disk->d_sectorsize = softc->params.blksize;
2897 softc->disk->d_mediasize =
2898 (off_t)softc->params.blksize * softc->params.disksize;
2902 * Now we check the table of contents. This (currently) is only
2903 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2904 * things like present a separate entry in /dev for each track,
2905 * like that acd(4) driver does.
2907 bzero(&softc->toc, sizeof(softc->toc));
2908 toch = &softc->toc.header;
2910 * We will get errors here for media that doesn't have a table of
2911 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2912 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2913 * has not been recorded (no complete session) and the Format codes
2914 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2915 * with an INVALID FIELD IN CDB. Devices that are not capable of
2916 * reading an incomplete session on DDC/CD-R/RW media shall report
2917 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2919 * So this isn't fatal if we can't read the table of contents, it
2920 * just means that the user won't be able to issue the play tracks
2921 * ioctl, and likely lots of other stuff won't work either. They
2922 * need to burn the CD before we can do a whole lot with it. So
2923 * we don't print anything here if we get an error back.
2925 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2928 * Errors in reading the table of contents aren't fatal, we just
2929 * won't have a valid table of contents cached.
2933 bzero(&softc->toc, sizeof(softc->toc));
2937 if (softc->quirks & CD_Q_BCD_TRACKS) {
2938 toch->starting_track = bcd2bin(toch->starting_track);
2939 toch->ending_track = bcd2bin(toch->ending_track);
2942 /* Number of TOC entries, plus leadout */
2943 num_entries = (toch->ending_track - toch->starting_track) + 2;
2945 if (num_entries <= 0)
2948 toclen = num_entries * sizeof(struct cd_toc_entry);
2950 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2951 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2955 bzero(&softc->toc, sizeof(softc->toc));
2959 if (softc->quirks & CD_Q_BCD_TRACKS) {
2960 toch->starting_track = bcd2bin(toch->starting_track);
2961 toch->ending_track = bcd2bin(toch->ending_track);
2964 * XXX KDM is this necessary? Probably only if the drive doesn't
2965 * return leadout information with the table of contents.
2967 cdindex = toch->starting_track + num_entries -1;
2968 if (cdindex == toch->ending_track + 1) {
2970 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2971 (u_int8_t *)&leadout, sizeof(leadout),
2977 softc->toc.entries[cdindex - toch->starting_track] =
2980 if (softc->quirks & CD_Q_BCD_TRACKS) {
2981 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2982 softc->toc.entries[cdindex].track =
2983 bcd2bin(softc->toc.entries[cdindex].track);
2987 softc->flags |= CD_FLAG_VALID_TOC;
2989 /* If the first track is audio, correct sector size. */
2990 if ((softc->toc.entries[0].control & 4) == 0) {
2991 softc->disk->d_sectorsize = softc->params.blksize = 2352;
2992 softc->disk->d_mediasize =
2993 (off_t)softc->params.blksize * softc->params.disksize;
2999 * We unconditionally (re)set the blocksize each time the
3000 * CD device is opened. This is because the CD can change,
3001 * and therefore the blocksize might change.
3002 * XXX problems here if some slice or partition is still
3003 * open with the old size?
3005 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
3006 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3007 softc->disk->d_devstat->block_size = softc->params.blksize;
3013 cdsize(struct cam_periph *periph, u_int32_t *size)
3015 struct cd_softc *softc;
3017 struct scsi_read_capacity_data *rcap_buf;
3020 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3022 softc = (struct cd_softc *)periph->softc;
3024 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3026 /* XXX Should be M_WAITOK */
3027 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3028 M_SCSICD, M_NOWAIT | M_ZERO);
3029 if (rcap_buf == NULL)
3032 scsi_read_capacity(&ccb->csio,
3033 /*retries*/ cd_retry_count,
3038 /* timeout */20000);
3040 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3041 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3043 xpt_release_ccb(ccb);
3045 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3046 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3047 /* Make sure we got at least some block size. */
3048 if (error == 0 && softc->params.blksize == 0)
3051 * SCSI-3 mandates that the reported blocksize shall be 2048.
3052 * Older drives sometimes report funny values, trim it down to
3053 * 2048, or other parts of the kernel will get confused.
3055 * XXX we leave drives alone that might report 512 bytes, as
3056 * well as drives reporting more weird sizes like perhaps 4K.
3058 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3059 softc->params.blksize = 2048;
3061 free(rcap_buf, M_SCSICD);
3062 *size = softc->params.disksize;
3069 cd6byteworkaround(union ccb *ccb)
3072 struct cam_periph *periph;
3073 struct cd_softc *softc;
3074 struct cd_mode_params *params;
3077 periph = xpt_path_periph(ccb->ccb_h.path);
3078 softc = (struct cd_softc *)periph->softc;
3080 cdb = ccb->csio.cdb_io.cdb_bytes;
3082 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3083 || ((cdb[0] != MODE_SENSE_6)
3084 && (cdb[0] != MODE_SELECT_6)))
3088 * Because there is no convenient place to stash the overall
3089 * cd_mode_params structure pointer, we have to grab it like this.
3090 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3091 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3093 * XXX It would be nice if, at some point, we could increase the
3094 * number of available peripheral private pointers. Both pointers
3095 * are currently used in most every peripheral driver.
3099 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3100 if (params->mode_buf == ccb->csio.data_ptr) {
3107 * This shouldn't happen. All mode sense and mode select
3108 * operations in the cd(4) driver MUST go through cdgetmode() and
3112 xpt_print(periph->path,
3113 "mode buffer not found in mode queue!\n");
3117 params->cdb_size = 10;
3118 softc->minimum_command_size = 10;
3119 xpt_print(ccb->ccb_h.path,
3120 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3121 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3123 if (cdb[0] == MODE_SENSE_6) {
3124 struct scsi_mode_sense_10 ms10;
3125 struct scsi_mode_sense_6 *ms6;
3128 ms6 = (struct scsi_mode_sense_6 *)cdb;
3130 bzero(&ms10, sizeof(ms10));
3131 ms10.opcode = MODE_SENSE_10;
3132 ms10.byte2 = ms6->byte2;
3133 ms10.page = ms6->page;
3136 * 10 byte mode header, block descriptor,
3137 * sizeof(union cd_pages)
3139 len = sizeof(struct cd_mode_data_10);
3140 ccb->csio.dxfer_len = len;
3142 scsi_ulto2b(len, ms10.length);
3143 ms10.control = ms6->control;
3144 bcopy(&ms10, cdb, 10);
3145 ccb->csio.cdb_len = 10;
3147 struct scsi_mode_select_10 ms10;
3148 struct scsi_mode_select_6 *ms6;
3149 struct scsi_mode_header_6 *header6;
3150 struct scsi_mode_header_10 *header10;
3151 struct scsi_mode_page_header *page_header;
3152 int blk_desc_len, page_num, page_size, len;
3154 ms6 = (struct scsi_mode_select_6 *)cdb;
3156 bzero(&ms10, sizeof(ms10));
3157 ms10.opcode = MODE_SELECT_10;
3158 ms10.byte2 = ms6->byte2;
3160 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3161 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3163 page_header = find_mode_page_6(header6);
3164 page_num = page_header->page_code;
3166 blk_desc_len = header6->blk_desc_len;
3168 page_size = cdgetpagesize(page_num);
3170 if (page_size != (page_header->page_length +
3171 sizeof(*page_header)))
3172 page_size = page_header->page_length +
3173 sizeof(*page_header);
3175 len = sizeof(*header10) + blk_desc_len + page_size;
3177 len = min(params->alloc_len, len);
3180 * Since the 6 byte parameter header is shorter than the 10
3181 * byte parameter header, we need to copy the actual mode
3182 * page data, and the block descriptor, if any, so things wind
3183 * up in the right place. The regions will overlap, but
3184 * bcopy() does the right thing.
3186 bcopy(params->mode_buf + sizeof(*header6),
3187 params->mode_buf + sizeof(*header10),
3188 len - sizeof(*header10));
3190 /* Make sure these fields are set correctly. */
3191 scsi_ulto2b(0, header10->data_length);
3192 header10->medium_type = 0;
3193 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3195 ccb->csio.dxfer_len = len;
3197 scsi_ulto2b(len, ms10.length);
3198 ms10.control = ms6->control;
3199 bcopy(&ms10, cdb, 10);
3200 ccb->csio.cdb_len = 10;
3203 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3204 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3207 cam_release_devq(ccb->ccb_h.path,
3211 /*getcount_only*/0);
3218 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3220 struct cd_softc *softc;
3221 struct cam_periph *periph;
3222 int error, error_code, sense_key, asc, ascq;
3224 periph = xpt_path_periph(ccb->ccb_h.path);
3225 softc = (struct cd_softc *)periph->softc;
3230 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3231 * CDB comes back with this particular error, try transforming it
3232 * into the 10 byte version.
3234 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3235 error = cd6byteworkaround(ccb);
3236 } else if (scsi_extract_sense_ccb(ccb,
3237 &error_code, &sense_key, &asc, &ascq)) {
3238 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3239 error = cd6byteworkaround(ccb);
3240 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3241 asc == 0x28 && ascq == 0x00)
3242 disk_media_changed(softc->disk, M_NOWAIT);
3243 else if (sense_key == SSD_KEY_NOT_READY &&
3244 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3245 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3246 disk_media_gone(softc->disk, M_NOWAIT);
3250 if (error == ERESTART)
3255 * Until we have a better way of doing pack validation,
3256 * don't treat UAs as errors.
3258 sense_flags |= SF_RETRY_UA;
3259 return (cam_periph_error(ccb, cam_flags, sense_flags,
3260 &softc->saved_ccb));
3264 cdmediapoll(void *arg)
3266 struct cam_periph *periph = arg;
3267 struct cd_softc *softc = periph->softc;
3269 if (softc->flags & CD_FLAG_CHANGER)
3272 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3273 softc->outstanding_cmds == 0) {
3274 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3276 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3279 /* Queue us up again */
3280 if (cd_poll_period != 0)
3281 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3285 * Read table of contents
3288 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3289 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3291 struct scsi_read_toc *scsi_cmd;
3293 struct ccb_scsiio *csio;
3300 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3305 /* retries */ cd_retry_count,
3306 /* cbfcnp */ cddone,
3307 /* flags */ CAM_DIR_IN,
3308 /* tag_action */ MSG_SIMPLE_Q_TAG,
3309 /* data_ptr */ data,
3310 /* dxfer_len */ len,
3311 /* sense_len */ SSD_FULL_SIZE,
3312 sizeof(struct scsi_read_toc),
3313 /* timeout */ 50000);
3315 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3316 bzero (scsi_cmd, sizeof(*scsi_cmd));
3318 if (mode == CD_MSF_FORMAT)
3319 scsi_cmd->byte2 |= CD_MSF;
3320 scsi_cmd->from_track = start;
3321 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3322 scsi_cmd->data_len[0] = (ntoc) >> 8;
3323 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3325 scsi_cmd->op_code = READ_TOC;
3327 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3328 /*sense_flags*/SF_RETRY_UA | sense_flags);
3330 xpt_release_ccb(ccb);
3336 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3337 u_int32_t format, int track,
3338 struct cd_sub_channel_info *data, u_int32_t len)
3340 struct scsi_read_subchannel *scsi_cmd;
3341 struct ccb_scsiio *csio;
3347 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3352 /* retries */ cd_retry_count,
3353 /* cbfcnp */ cddone,
3354 /* flags */ CAM_DIR_IN,
3355 /* tag_action */ MSG_SIMPLE_Q_TAG,
3356 /* data_ptr */ (u_int8_t *)data,
3357 /* dxfer_len */ len,
3358 /* sense_len */ SSD_FULL_SIZE,
3359 sizeof(struct scsi_read_subchannel),
3360 /* timeout */ 50000);
3362 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3363 bzero (scsi_cmd, sizeof(*scsi_cmd));
3365 scsi_cmd->op_code = READ_SUBCHANNEL;
3366 if (mode == CD_MSF_FORMAT)
3367 scsi_cmd->byte1 |= CD_MSF;
3368 scsi_cmd->byte2 = SRS_SUBQ;
3369 scsi_cmd->subchan_format = format;
3370 scsi_cmd->track = track;
3371 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3372 scsi_cmd->control = 0;
3374 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3375 /*sense_flags*/SF_RETRY_UA);
3377 xpt_release_ccb(ccb);
3384 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3385 * routine. See comments in cd6byteworkaround() for details.
3388 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3391 struct ccb_scsiio *csio;
3392 struct cd_softc *softc;
3397 softc = (struct cd_softc *)periph->softc;
3399 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3403 data->cdb_size = softc->minimum_command_size;
3404 if (data->cdb_size < 10)
3405 param_len = sizeof(struct cd_mode_data);
3407 param_len = sizeof(struct cd_mode_data_10);
3409 /* Don't say we've got more room than we actually allocated */
3410 param_len = min(param_len, data->alloc_len);
3412 scsi_mode_sense_len(csio,
3413 /* retries */ cd_retry_count,
3414 /* cbfcnp */ cddone,
3415 /* tag_action */ MSG_SIMPLE_Q_TAG,
3417 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3419 /* param_buf */ data->mode_buf,
3420 /* param_len */ param_len,
3421 /* minimum_cmd_size */ softc->minimum_command_size,
3422 /* sense_len */ SSD_FULL_SIZE,
3423 /* timeout */ 50000);
3426 * It would be nice not to have to do this, but there's no
3427 * available pointer in the CCB that would allow us to stuff the
3428 * mode params structure in there and retrieve it in
3429 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3430 * lets the caller know what CDB size we ended up using, so they
3431 * can find the actual mode page offset.
3433 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3435 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3436 /*sense_flags*/SF_RETRY_UA);
3438 xpt_release_ccb(ccb);
3440 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3443 * This is a bit of belt-and-suspenders checking, but if we run
3444 * into a situation where the target sends back multiple block
3445 * descriptors, we might not have enough space in the buffer to
3446 * see the whole mode page. Better to return an error than
3447 * potentially access memory beyond our malloced region.
3452 if (data->cdb_size == 10) {
3453 struct scsi_mode_header_10 *hdr10;
3455 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3456 data_len = scsi_2btoul(hdr10->data_length);
3457 data_len += sizeof(hdr10->data_length);
3459 struct scsi_mode_header_6 *hdr6;
3461 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3462 data_len = hdr6->data_length;
3463 data_len += sizeof(hdr6->data_length);
3467 * Complain if there is more mode data available than we
3468 * allocated space for. This could potentially happen if
3469 * we miscalculated the page length for some reason, if the
3470 * drive returns multiple block descriptors, or if it sets
3471 * the data length incorrectly.
3473 if (data_len > data->alloc_len) {
3474 xpt_print(periph->path, "allocated modepage %d length "
3475 "%d < returned length %d\n", page, data->alloc_len,
3484 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3485 * routine. See comments in cd6byteworkaround() for details.
3488 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3490 struct ccb_scsiio *csio;
3491 struct cd_softc *softc;
3493 int cdb_size, param_len;
3496 softc = (struct cd_softc *)periph->softc;
3498 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3505 * If the data is formatted for the 10 byte version of the mode
3506 * select parameter list, we need to use the 10 byte CDB.
3507 * Otherwise, we use whatever the stored minimum command size.
3509 if (data->cdb_size == 10)
3510 cdb_size = data->cdb_size;
3512 cdb_size = softc->minimum_command_size;
3514 if (cdb_size >= 10) {
3515 struct scsi_mode_header_10 *mode_header;
3518 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3520 data_len = scsi_2btoul(mode_header->data_length);
3522 scsi_ulto2b(0, mode_header->data_length);
3524 * SONY drives do not allow a mode select with a medium_type
3525 * value that has just been returned by a mode sense; use a
3526 * medium_type of 0 (Default) instead.
3528 mode_header->medium_type = 0;
3531 * Pass back whatever the drive passed to us, plus the size
3532 * of the data length field.
3534 param_len = data_len + sizeof(mode_header->data_length);
3537 struct scsi_mode_header_6 *mode_header;
3539 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3541 param_len = mode_header->data_length + 1;
3543 mode_header->data_length = 0;
3545 * SONY drives do not allow a mode select with a medium_type
3546 * value that has just been returned by a mode sense; use a
3547 * medium_type of 0 (Default) instead.
3549 mode_header->medium_type = 0;
3552 /* Don't say we've got more room than we actually allocated */
3553 param_len = min(param_len, data->alloc_len);
3555 scsi_mode_select_len(csio,
3556 /* retries */ cd_retry_count,
3557 /* cbfcnp */ cddone,
3558 /* tag_action */ MSG_SIMPLE_Q_TAG,
3559 /* scsi_page_fmt */ 1,
3561 /* param_buf */ data->mode_buf,
3562 /* param_len */ param_len,
3563 /* minimum_cmd_size */ cdb_size,
3564 /* sense_len */ SSD_FULL_SIZE,
3565 /* timeout */ 50000);
3567 /* See comments in cdgetmode() and cd6byteworkaround(). */
3568 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3570 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3571 /*sense_flags*/SF_RETRY_UA);
3573 xpt_release_ccb(ccb);
3575 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3582 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3584 struct ccb_scsiio *csio;
3590 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3593 * Use the smallest possible command to perform the operation.
3595 if ((len & 0xffff0000) == 0) {
3597 * We can fit in a 10 byte cdb.
3599 struct scsi_play_10 *scsi_cmd;
3601 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3602 bzero (scsi_cmd, sizeof(*scsi_cmd));
3603 scsi_cmd->op_code = PLAY_10;
3604 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3605 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3606 cdb_len = sizeof(*scsi_cmd);
3608 struct scsi_play_12 *scsi_cmd;
3610 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3611 bzero (scsi_cmd, sizeof(*scsi_cmd));
3612 scsi_cmd->op_code = PLAY_12;
3613 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3614 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3615 cdb_len = sizeof(*scsi_cmd);
3618 /*retries*/ cd_retry_count,
3620 /*flags*/CAM_DIR_NONE,
3624 /*sense_len*/SSD_FULL_SIZE,
3626 /*timeout*/50 * 1000);
3628 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3629 /*sense_flags*/SF_RETRY_UA);
3631 xpt_release_ccb(ccb);
3637 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3638 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3640 struct scsi_play_msf *scsi_cmd;
3641 struct ccb_scsiio *csio;
3647 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3652 /* retries */ cd_retry_count,
3653 /* cbfcnp */ cddone,
3654 /* flags */ CAM_DIR_NONE,
3655 /* tag_action */ MSG_SIMPLE_Q_TAG,
3656 /* data_ptr */ NULL,
3658 /* sense_len */ SSD_FULL_SIZE,
3659 sizeof(struct scsi_play_msf),
3660 /* timeout */ 50000);
3662 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3663 bzero (scsi_cmd, sizeof(*scsi_cmd));
3665 scsi_cmd->op_code = PLAY_MSF;
3666 scsi_cmd->start_m = startm;
3667 scsi_cmd->start_s = starts;
3668 scsi_cmd->start_f = startf;
3669 scsi_cmd->end_m = endm;
3670 scsi_cmd->end_s = ends;
3671 scsi_cmd->end_f = endf;
3673 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3674 /*sense_flags*/SF_RETRY_UA);
3676 xpt_release_ccb(ccb);
3683 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3684 u_int32_t etrack, u_int32_t eindex)
3686 struct scsi_play_track *scsi_cmd;
3687 struct ccb_scsiio *csio;
3693 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3698 /* retries */ cd_retry_count,
3699 /* cbfcnp */ cddone,
3700 /* flags */ CAM_DIR_NONE,
3701 /* tag_action */ MSG_SIMPLE_Q_TAG,
3702 /* data_ptr */ NULL,
3704 /* sense_len */ SSD_FULL_SIZE,
3705 sizeof(struct scsi_play_track),
3706 /* timeout */ 50000);
3708 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3709 bzero (scsi_cmd, sizeof(*scsi_cmd));
3711 scsi_cmd->op_code = PLAY_TRACK;
3712 scsi_cmd->start_track = strack;
3713 scsi_cmd->start_index = sindex;
3714 scsi_cmd->end_track = etrack;
3715 scsi_cmd->end_index = eindex;
3717 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3718 /*sense_flags*/SF_RETRY_UA);
3720 xpt_release_ccb(ccb);
3726 cdpause(struct cam_periph *periph, u_int32_t go)
3728 struct scsi_pause *scsi_cmd;
3729 struct ccb_scsiio *csio;
3735 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3740 /* retries */ cd_retry_count,
3741 /* cbfcnp */ cddone,
3742 /* flags */ CAM_DIR_NONE,
3743 /* tag_action */ MSG_SIMPLE_Q_TAG,
3744 /* data_ptr */ NULL,
3746 /* sense_len */ SSD_FULL_SIZE,
3747 sizeof(struct scsi_pause),
3748 /* timeout */ 50000);
3750 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3751 bzero (scsi_cmd, sizeof(*scsi_cmd));
3753 scsi_cmd->op_code = PAUSE;
3754 scsi_cmd->resume = go;
3756 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3757 /*sense_flags*/SF_RETRY_UA);
3759 xpt_release_ccb(ccb);
3765 cdstartunit(struct cam_periph *periph, int load)
3772 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3774 scsi_start_stop(&ccb->csio,
3775 /* retries */ cd_retry_count,
3776 /* cbfcnp */ cddone,
3777 /* tag_action */ MSG_SIMPLE_Q_TAG,
3779 /* load_eject */ load,
3780 /* immediate */ FALSE,
3781 /* sense_len */ SSD_FULL_SIZE,
3782 /* timeout */ 50000);
3784 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3785 /*sense_flags*/SF_RETRY_UA);
3787 xpt_release_ccb(ccb);
3793 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3800 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3802 scsi_start_stop(&ccb->csio,
3803 /* retries */ cd_retry_count,
3804 /* cbfcnp */ cddone,
3805 /* tag_action */ MSG_SIMPLE_Q_TAG,
3807 /* load_eject */ eject,
3808 /* immediate */ FALSE,
3809 /* sense_len */ SSD_FULL_SIZE,
3810 /* timeout */ 50000);
3812 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3813 /*sense_flags*/SF_RETRY_UA);
3815 xpt_release_ccb(ccb);
3821 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3823 struct scsi_set_speed *scsi_cmd;
3824 struct ccb_scsiio *csio;
3829 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3832 /* Preserve old behavior: units in multiples of CDROM speed */
3839 /* retries */ cd_retry_count,
3840 /* cbfcnp */ cddone,
3841 /* flags */ CAM_DIR_NONE,
3842 /* tag_action */ MSG_SIMPLE_Q_TAG,
3843 /* data_ptr */ NULL,
3845 /* sense_len */ SSD_FULL_SIZE,
3846 sizeof(struct scsi_set_speed),
3847 /* timeout */ 50000);
3849 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3850 bzero(scsi_cmd, sizeof(*scsi_cmd));
3852 scsi_cmd->opcode = SET_CD_SPEED;
3853 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3854 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3856 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3857 /*sense_flags*/SF_RETRY_UA);
3859 xpt_release_ccb(ccb);
3865 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3877 switch (authinfo->format) {
3878 case DVD_REPORT_AGID:
3879 length = sizeof(struct scsi_report_key_data_agid);
3881 case DVD_REPORT_CHALLENGE:
3882 length = sizeof(struct scsi_report_key_data_challenge);
3884 case DVD_REPORT_KEY1:
3885 length = sizeof(struct scsi_report_key_data_key1_key2);
3887 case DVD_REPORT_TITLE_KEY:
3888 length = sizeof(struct scsi_report_key_data_title);
3889 /* The lba field is only set for the title key */
3890 lba = authinfo->lba;
3892 case DVD_REPORT_ASF:
3893 length = sizeof(struct scsi_report_key_data_asf);
3895 case DVD_REPORT_RPC:
3896 length = sizeof(struct scsi_report_key_data_rpc);
3898 case DVD_INVALIDATE_AGID:
3906 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3910 cam_periph_lock(periph);
3911 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3913 scsi_report_key(&ccb->csio,
3914 /* retries */ cd_retry_count,
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*/CAM_RETRY_SELTO,
3926 /*sense_flags*/SF_RETRY_UA);
3931 if (ccb->csio.resid != 0) {
3932 xpt_print(periph->path, "warning, residual for report key "
3933 "command is %d\n", ccb->csio.resid);
3936 switch(authinfo->format) {
3937 case DVD_REPORT_AGID: {
3938 struct scsi_report_key_data_agid *agid_data;
3940 agid_data = (struct scsi_report_key_data_agid *)databuf;
3942 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3946 case DVD_REPORT_CHALLENGE: {
3947 struct scsi_report_key_data_challenge *chal_data;
3949 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3951 bcopy(chal_data->challenge_key, authinfo->keychal,
3952 min(sizeof(chal_data->challenge_key),
3953 sizeof(authinfo->keychal)));
3956 case DVD_REPORT_KEY1: {
3957 struct scsi_report_key_data_key1_key2 *key1_data;
3959 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3961 bcopy(key1_data->key1, authinfo->keychal,
3962 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3965 case DVD_REPORT_TITLE_KEY: {
3966 struct scsi_report_key_data_title *title_data;
3968 title_data = (struct scsi_report_key_data_title *)databuf;
3970 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3971 RKD_TITLE_CPM_SHIFT;
3972 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3973 RKD_TITLE_CP_SEC_SHIFT;
3974 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3975 RKD_TITLE_CMGS_SHIFT;
3976 bcopy(title_data->title_key, authinfo->keychal,
3977 min(sizeof(title_data->title_key),
3978 sizeof(authinfo->keychal)));
3981 case DVD_REPORT_ASF: {
3982 struct scsi_report_key_data_asf *asf_data;
3984 asf_data = (struct scsi_report_key_data_asf *)databuf;
3986 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3989 case DVD_REPORT_RPC: {
3990 struct scsi_report_key_data_rpc *rpc_data;
3992 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3994 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3996 authinfo->vend_rsts =
3997 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3998 RKD_RPC_VENDOR_RESET_SHIFT;
3999 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4000 authinfo->region = rpc_data->region_mask;
4001 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4004 case DVD_INVALIDATE_AGID:
4007 /* This should be impossible, since we checked above */
4010 break; /* NOTREACHED */
4014 xpt_release_ccb(ccb);
4015 cam_periph_unlock(periph);
4017 if (databuf != NULL)
4018 free(databuf, M_DEVBUF);
4024 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4034 switch(authinfo->format) {
4035 case DVD_SEND_CHALLENGE: {
4036 struct scsi_report_key_data_challenge *challenge_data;
4038 length = sizeof(*challenge_data);
4040 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4042 databuf = (u_int8_t *)challenge_data;
4044 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4045 challenge_data->data_len);
4047 bcopy(authinfo->keychal, challenge_data->challenge_key,
4048 min(sizeof(authinfo->keychal),
4049 sizeof(challenge_data->challenge_key)));
4052 case DVD_SEND_KEY2: {
4053 struct scsi_report_key_data_key1_key2 *key2_data;
4055 length = sizeof(*key2_data);
4057 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4059 databuf = (u_int8_t *)key2_data;
4061 scsi_ulto2b(length - sizeof(key2_data->data_len),
4062 key2_data->data_len);
4064 bcopy(authinfo->keychal, key2_data->key1,
4065 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4069 case DVD_SEND_RPC: {
4070 struct scsi_send_key_data_rpc *rpc_data;
4072 length = sizeof(*rpc_data);
4074 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4076 databuf = (u_int8_t *)rpc_data;
4078 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4079 rpc_data->data_len);
4081 rpc_data->region_code = authinfo->region;
4088 cam_periph_lock(periph);
4089 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4091 scsi_send_key(&ccb->csio,
4092 /* retries */ cd_retry_count,
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*/CAM_RETRY_SELTO,
4103 /*sense_flags*/SF_RETRY_UA);
4105 xpt_release_ccb(ccb);
4106 cam_periph_unlock(periph);
4108 if (databuf != NULL)
4109 free(databuf, M_DEVBUF);
4115 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4125 /* The address is reserved for many of the formats */
4128 switch(dvdstruct->format) {
4129 case DVD_STRUCT_PHYSICAL:
4130 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4132 case DVD_STRUCT_COPYRIGHT:
4133 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4135 case DVD_STRUCT_DISCKEY:
4136 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4138 case DVD_STRUCT_BCA:
4139 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4141 case DVD_STRUCT_MANUFACT:
4142 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4144 case DVD_STRUCT_CMI:
4146 case DVD_STRUCT_PROTDISCID:
4147 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4149 case DVD_STRUCT_DISCKEYBLOCK:
4150 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4152 case DVD_STRUCT_DDS:
4153 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4155 case DVD_STRUCT_MEDIUM_STAT:
4156 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4158 case DVD_STRUCT_SPARE_AREA:
4159 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4161 case DVD_STRUCT_RMD_LAST:
4163 case DVD_STRUCT_RMD_RMA:
4165 case DVD_STRUCT_PRERECORDED:
4166 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4168 case DVD_STRUCT_UNIQUEID:
4169 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4171 case DVD_STRUCT_DCB:
4173 case DVD_STRUCT_LIST:
4175 * This is the maximum allocation length for the READ DVD
4176 * STRUCTURE command. There's nothing in the MMC3 spec
4177 * that indicates a limit in the amount of data that can
4178 * be returned from this call, other than the limits
4179 * imposed by the 2-byte length variables.
4188 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4192 cam_periph_lock(periph);
4193 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4195 scsi_read_dvd_structure(&ccb->csio,
4196 /* retries */ cd_retry_count,
4197 /* cbfcnp */ cddone,
4198 /* tag_action */ MSG_SIMPLE_Q_TAG,
4200 /* layer_number */ dvdstruct->layer_num,
4201 /* key_format */ dvdstruct->format,
4202 /* agid */ dvdstruct->agid,
4203 /* data_ptr */ databuf,
4204 /* dxfer_len */ length,
4205 /* sense_len */ SSD_FULL_SIZE,
4206 /* timeout */ 50000);
4208 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4209 /*sense_flags*/SF_RETRY_UA);
4214 switch(dvdstruct->format) {
4215 case DVD_STRUCT_PHYSICAL: {
4216 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4217 struct dvd_layer *outlayer;
4218 struct scsi_read_dvd_struct_data_physical *phys_data;
4221 (struct scsi_read_dvd_struct_data_physical *)databuf;
4222 inlayer = &phys_data->layer_desc;
4223 outlayer = (struct dvd_layer *)&dvdstruct->data;
4225 dvdstruct->length = sizeof(*inlayer);
4227 outlayer->book_type = (inlayer->book_type_version &
4228 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4229 outlayer->book_version = (inlayer->book_type_version &
4230 RDSD_BOOK_VERSION_MASK);
4231 outlayer->disc_size = (inlayer->disc_size_max_rate &
4232 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4233 outlayer->max_rate = (inlayer->disc_size_max_rate &
4234 RDSD_MAX_RATE_MASK);
4235 outlayer->nlayers = (inlayer->layer_info &
4236 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4237 outlayer->track_path = (inlayer->layer_info &
4238 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4239 outlayer->layer_type = (inlayer->layer_info &
4240 RDSD_LAYER_TYPE_MASK);
4241 outlayer->linear_density = (inlayer->density &
4242 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4243 outlayer->track_density = (inlayer->density &
4244 RDSD_TRACK_DENSITY_MASK);
4245 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4247 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4248 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4249 outlayer->end_sector_l0 =
4250 scsi_3btoul(inlayer->end_sector_layer0);
4253 case DVD_STRUCT_COPYRIGHT: {
4254 struct scsi_read_dvd_struct_data_copyright *copy_data;
4256 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4259 dvdstruct->cpst = copy_data->cps_type;
4260 dvdstruct->rmi = copy_data->region_info;
4261 dvdstruct->length = 0;
4267 * Tell the user what the overall length is, no matter
4268 * what we can actually fit in the data buffer.
4270 dvdstruct->length = length - ccb->csio.resid -
4271 sizeof(struct scsi_read_dvd_struct_data_header);
4274 * But only actually copy out the smaller of what we read
4275 * in or what the structure can take.
4277 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4279 min(sizeof(dvdstruct->data), dvdstruct->length));
4284 xpt_release_ccb(ccb);
4285 cam_periph_unlock(periph);
4287 if (databuf != NULL)
4288 free(databuf, M_DEVBUF);
4294 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4295 void (*cbfcnp)(struct cam_periph *, union ccb *),
4296 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4297 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4298 u_int8_t sense_len, u_int32_t timeout)
4300 struct scsi_report_key *scsi_cmd;
4302 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4303 bzero(scsi_cmd, sizeof(*scsi_cmd));
4304 scsi_cmd->opcode = REPORT_KEY;
4305 scsi_ulto4b(lba, scsi_cmd->lba);
4306 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4307 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4308 (key_format & RK_KF_KEYFORMAT_MASK);
4313 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4315 /*data_ptr*/ data_ptr,
4316 /*dxfer_len*/ dxfer_len,
4323 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4324 void (*cbfcnp)(struct cam_periph *, union ccb *),
4325 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4326 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4329 struct scsi_send_key *scsi_cmd;
4331 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4332 bzero(scsi_cmd, sizeof(*scsi_cmd));
4333 scsi_cmd->opcode = SEND_KEY;
4335 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4336 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4337 (key_format & RK_KF_KEYFORMAT_MASK);
4342 /*flags*/ CAM_DIR_OUT,
4344 /*data_ptr*/ data_ptr,
4345 /*dxfer_len*/ dxfer_len,
4353 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4354 void (*cbfcnp)(struct cam_periph *, union ccb *),
4355 u_int8_t tag_action, u_int32_t address,
4356 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4357 u_int8_t *data_ptr, u_int32_t dxfer_len,
4358 u_int8_t sense_len, u_int32_t timeout)
4360 struct scsi_read_dvd_structure *scsi_cmd;
4362 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4363 bzero(scsi_cmd, sizeof(*scsi_cmd));
4364 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4366 scsi_ulto4b(address, scsi_cmd->address);
4367 scsi_cmd->layer_number = layer_number;
4368 scsi_cmd->format = format;
4369 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4370 /* The AGID is the top two bits of this byte */
4371 scsi_cmd->agid = agid << 6;
4376 /*flags*/ CAM_DIR_IN,
4378 /*data_ptr*/ data_ptr,
4379 /*dxfer_len*/ dxfer_len,