2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
66 #include <geom/geom_disk.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
73 #include <cam/cam_sim.h>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_NO_CHANGER = 0x04,
92 CD_Q_10_BYTE_ONLY = 0x10
96 CD_FLAG_INVALID = 0x0001,
97 CD_FLAG_NEW_DISC = 0x0002,
98 CD_FLAG_DISC_LOCKED = 0x0004,
99 CD_FLAG_DISC_REMOVABLE = 0x0008,
100 CD_FLAG_SAW_MEDIA = 0x0010,
101 CD_FLAG_CHANGER = 0x0040,
102 CD_FLAG_ACTIVE = 0x0080,
103 CD_FLAG_SCHED_ON_COMP = 0x0100,
104 CD_FLAG_RETRY_UA = 0x0200,
105 CD_FLAG_VALID_MEDIA = 0x0400,
106 CD_FLAG_VALID_TOC = 0x0800,
107 CD_FLAG_SCTX_INIT = 0x1000
112 CD_CCB_BUFFER_IO = 0x02,
113 CD_CCB_WAITING = 0x03,
115 CD_CCB_TYPE_MASK = 0x0F,
116 CD_CCB_RETRY_UA = 0x10
120 CHANGER_TIMEOUT_SCHED = 0x01,
121 CHANGER_SHORT_TMOUT_SCHED = 0x02,
122 CHANGER_MANUAL_CALL = 0x04,
123 CHANGER_NEED_TIMEOUT = 0x08
126 #define ccb_state ppriv_field0
127 #define ccb_bp ppriv_ptr1
130 struct ioc_toc_header header;
131 struct cd_toc_entry entries[100];
134 struct cd_toc_single {
135 struct ioc_toc_header header;
136 struct cd_toc_entry entry;
147 volatile cd_flags flags;
148 struct bio_queue_head bio_queue;
149 LIST_HEAD(, ccb_hdr) pending_ccbs;
150 struct cd_params params;
153 STAILQ_ENTRY(cd_softc) changer_links;
154 struct cdchanger *changer;
156 struct cam_periph *periph;
157 int minimum_command_size;
158 int outstanding_cmds;
160 struct task sysctl_task;
161 struct sysctl_ctx_list sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
163 STAILQ_HEAD(, cd_mode_params) mode_queue;
164 struct cd_tocdata toc;
166 struct callout mediapoll_c;
169 struct cd_page_sizes {
174 static struct cd_page_sizes cd_page_size_table[] =
176 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
179 struct cd_quirk_entry {
180 struct scsi_inquiry_pattern inq_pat;
185 * The changer quirk entries aren't strictly necessary. Basically, what
186 * they do is tell cdregister() up front that a device is a changer.
187 * Otherwise, it will figure that fact out once it sees a LUN on the device
188 * that is greater than 0. If it is known up front that a device is a changer,
189 * all I/O to the device will go through the changer scheduling routines, as
190 * opposed to the "normal" CD code.
192 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
193 * your device hangs when it gets a 10 byte command. Adding a quirk just
194 * to get rid of the informative diagnostic message is not acceptable. All
195 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
196 * referenced in a comment along with the quirk) , and must be approved by
197 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
198 * be removed until the submitter can explain why they are needed.
199 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
200 * when the CAM_NEW_TRAN_CODE work is done.
202 static struct cd_quirk_entry cd_quirk_table[] =
205 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
206 /*quirks*/ CD_Q_CHANGER
209 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
210 "*"}, /* quirks */ CD_Q_CHANGER
213 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
214 /* quirks */ CD_Q_CHANGER
217 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
218 /* quirks */ CD_Q_BCD_TRACKS
222 static disk_open_t cdopen;
223 static disk_close_t cdclose;
224 static disk_ioctl_t cdioctl;
225 static disk_strategy_t cdstrategy;
227 static periph_init_t cdinit;
228 static periph_ctor_t cdregister;
229 static periph_dtor_t cdcleanup;
230 static periph_start_t cdstart;
231 static periph_oninv_t cdoninvalidate;
232 static void cdasync(void *callback_arg, u_int32_t code,
233 struct cam_path *path, void *arg);
234 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
235 static void cdshorttimeout(void *arg);
236 static void cdschedule(struct cam_periph *periph, int priority);
237 static void cdrunchangerqueue(void *arg);
238 static void cdchangerschedule(struct cd_softc *softc);
239 static int cdrunccb(union ccb *ccb,
240 int (*error_routine)(union ccb *ccb,
242 u_int32_t sense_flags),
243 u_int32_t cam_flags, u_int32_t sense_flags);
244 static union ccb *cdgetccb(struct cam_periph *periph,
246 static void cddone(struct cam_periph *periph,
247 union ccb *start_ccb);
248 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
249 static int cdgetpagesize(int page_num);
250 static void cdprevent(struct cam_periph *periph, int action);
251 static int cdcheckmedia(struct cam_periph *periph);
252 static int cdsize(struct cam_periph *periph, u_int32_t *size);
253 static int cd6byteworkaround(union ccb *ccb);
254 static int cderror(union ccb *ccb, u_int32_t cam_flags,
255 u_int32_t sense_flags);
256 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
257 u_int32_t start, u_int8_t *data,
258 u_int32_t len, u_int32_t sense_flags);
259 static int cdgetmode(struct cam_periph *periph,
260 struct cd_mode_params *data, u_int32_t page);
261 static int cdsetmode(struct cam_periph *periph,
262 struct cd_mode_params *data);
263 static int cdplay(struct cam_periph *periph, u_int32_t blk,
265 static int cdreadsubchannel(struct cam_periph *periph,
266 u_int32_t mode, u_int32_t format,
268 struct cd_sub_channel_info *data,
270 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
271 u_int32_t starts, u_int32_t startf,
272 u_int32_t endm, u_int32_t ends,
274 static int cdplaytracks(struct cam_periph *periph,
275 u_int32_t strack, u_int32_t sindex,
276 u_int32_t etrack, u_int32_t eindex);
277 static int cdpause(struct cam_periph *periph, u_int32_t go);
278 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
279 static int cdstartunit(struct cam_periph *periph, int load);
280 static int cdsetspeed(struct cam_periph *periph,
281 u_int32_t rdspeed, u_int32_t wrspeed);
282 static int cdreportkey(struct cam_periph *periph,
283 struct dvd_authinfo *authinfo);
284 static int cdsendkey(struct cam_periph *periph,
285 struct dvd_authinfo *authinfo);
286 static int cdreaddvdstructure(struct cam_periph *periph,
287 struct dvd_struct *dvdstruct);
288 static timeout_t cdmediapoll;
290 static struct periph_driver cddriver =
293 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
296 PERIPHDRIVER_DECLARE(cd, cddriver);
298 #ifndef CD_DEFAULT_POLL_PERIOD
299 #define CD_DEFAULT_POLL_PERIOD 3
301 #ifndef CD_DEFAULT_RETRY
302 #define CD_DEFAULT_RETRY 4
304 #ifndef CD_DEFAULT_TIMEOUT
305 #define CD_DEFAULT_TIMEOUT 30000
307 #ifndef CHANGER_MIN_BUSY_SECONDS
308 #define CHANGER_MIN_BUSY_SECONDS 5
310 #ifndef CHANGER_MAX_BUSY_SECONDS
311 #define CHANGER_MAX_BUSY_SECONDS 15
314 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
315 static int cd_retry_count = CD_DEFAULT_RETRY;
316 static int cd_timeout = CD_DEFAULT_TIMEOUT;
317 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
318 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
320 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
321 static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
323 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RW,
324 &cd_poll_period, 0, "Media polling period in seconds");
325 TUNABLE_INT("kern.cam.cd.poll_period", &cd_poll_period);
326 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
327 &cd_retry_count, 0, "Normal I/O retry count");
328 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
329 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RW,
330 &cd_timeout, 0, "Timeout, in us, for read operations");
331 TUNABLE_INT("kern.cam.cd.timeout", &cd_timeout);
332 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
333 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
334 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
335 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
336 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
337 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
341 target_id_t target_id;
344 struct timeval start_time;
345 struct cd_softc *cur_device;
346 struct callout short_handle;
347 struct callout long_handle;
348 volatile cd_changer_flags flags;
349 STAILQ_ENTRY(cdchanger) changer_links;
350 STAILQ_HEAD(chdevlist, cd_softc) chluns;
353 static struct mtx changerq_mtx;
354 static STAILQ_HEAD(changerlist, cdchanger) changerq;
355 static int num_changers;
357 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
364 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
365 STAILQ_INIT(&changerq);
368 * Install a global async callback. This callback will
369 * receive async callbacks like "new device found".
371 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
373 if (status != CAM_REQ_CMP) {
374 printf("cd: Failed to attach master async callback "
375 "due to status 0x%x!\n", status);
380 * Callback from GEOM, called when it has finished cleaning up its
384 cddiskgonecb(struct disk *dp)
386 struct cam_periph *periph;
388 periph = (struct cam_periph *)dp->d_drv1;
390 cam_periph_release(periph);
394 cdoninvalidate(struct cam_periph *periph)
396 struct cd_softc *softc;
398 softc = (struct cd_softc *)periph->softc;
401 * De-register any async callbacks.
403 xpt_register_async(0, cdasync, periph, periph->path);
405 softc->flags |= CD_FLAG_INVALID;
408 * Return all queued I/O with ENXIO.
409 * XXX Handle any transactions queued to the card
410 * with XPT_ABORT_CCB.
412 bioq_flush(&softc->bio_queue, NULL, ENXIO);
415 * If this device is part of a changer, and it was scheduled
416 * to run, remove it from the run queue since we just nuked
417 * all of its scheduled I/O.
419 if ((softc->flags & CD_FLAG_CHANGER)
420 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
421 camq_remove(&softc->changer->devq, softc->pinfo.index);
423 disk_gone(softc->disk);
424 xpt_print(periph->path, "lost device, %d refs\n", periph->refcount);
428 cdcleanup(struct cam_periph *periph)
430 struct cd_softc *softc;
432 softc = (struct cd_softc *)periph->softc;
434 xpt_print(periph->path, "removing device entry\n");
437 * In the queued, non-active case, the device in question
438 * has already been removed from the changer run queue. Since this
439 * device is active, we need to de-activate it, and schedule
440 * another device to run. (if there is another one to run)
442 if ((softc->flags & CD_FLAG_CHANGER)
443 && (softc->flags & CD_FLAG_ACTIVE)) {
446 * The purpose of the short timeout is soley to determine
447 * whether the current device has finished or not. Well,
448 * since we're removing the active device, we know that it
449 * is finished. So, get rid of the short timeout.
450 * Otherwise, if we're in the time period before the short
451 * timeout fires, and there are no other devices in the
452 * queue to run, there won't be any other device put in the
453 * active slot. i.e., when we call cdrunchangerqueue()
454 * below, it won't do anything. Then, when the short
455 * timeout fires, it'll look at the "current device", which
456 * we are free below, and possibly panic the kernel on a
457 * bogus pointer reference.
459 * The long timeout doesn't really matter, since we
460 * decrement the qfrozen_cnt to indicate that there is
461 * nothing in the active slot now. Therefore, there won't
462 * be any bogus pointer references there.
464 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
465 callout_stop(&softc->changer->short_handle);
466 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
468 softc->changer->devq.qfrozen_cnt[0]--;
469 softc->changer->flags |= CHANGER_MANUAL_CALL;
470 cdrunchangerqueue(softc->changer);
474 * If we're removing the last device on the changer, go ahead and
475 * remove the changer device structure.
477 if ((softc->flags & CD_FLAG_CHANGER)
478 && (--softc->changer->num_devices == 0)) {
481 * Theoretically, there shouldn't be any timeouts left, but
482 * I'm not completely sure that that will be the case. So,
483 * it won't hurt to check and see if there are any left.
485 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
486 callout_stop(&softc->changer->long_handle);
487 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
490 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
491 callout_stop(&softc->changer->short_handle);
492 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
495 mtx_lock(&changerq_mtx);
496 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
499 mtx_unlock(&changerq_mtx);
500 xpt_print(periph->path, "removing changer entry\n");
501 free(softc->changer, M_DEVBUF);
503 cam_periph_unlock(periph);
504 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
505 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
506 xpt_print(periph->path, "can't remove sysctl context\n");
509 callout_drain(&softc->mediapoll_c);
510 disk_destroy(softc->disk);
511 free(softc, M_DEVBUF);
512 cam_periph_lock(periph);
516 cdasync(void *callback_arg, u_int32_t code,
517 struct cam_path *path, void *arg)
519 struct cam_periph *periph;
520 struct cd_softc *softc;
522 periph = (struct cam_periph *)callback_arg;
524 case AC_FOUND_DEVICE:
526 struct ccb_getdev *cgd;
529 cgd = (struct ccb_getdev *)arg;
533 if (cgd->protocol != PROTO_SCSI)
536 if (SID_TYPE(&cgd->inq_data) != T_CDROM
537 && SID_TYPE(&cgd->inq_data) != T_WORM)
541 * Allocate a peripheral instance for
542 * this device and start the probe
545 status = cam_periph_alloc(cdregister, cdoninvalidate,
547 "cd", CAM_PERIPH_BIO,
548 cgd->ccb_h.path, cdasync,
549 AC_FOUND_DEVICE, cgd);
551 if (status != CAM_REQ_CMP
552 && status != CAM_REQ_INPROG)
553 printf("cdasync: Unable to attach new device "
554 "due to status 0x%x\n", status);
558 case AC_UNIT_ATTENTION:
561 int error_code, sense_key, asc, ascq;
563 softc = (struct cd_softc *)periph->softc;
564 ccb = (union ccb *)arg;
567 * Handle all media change UNIT ATTENTIONs except
568 * our own, as they will be handled by cderror().
570 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
571 scsi_extract_sense_ccb(ccb,
572 &error_code, &sense_key, &asc, &ascq)) {
573 if (asc == 0x28 && ascq == 0x00)
574 disk_media_changed(softc->disk, M_NOWAIT);
576 cam_periph_async(periph, code, path, arg);
580 softc = (struct cd_softc *)periph->softc;
581 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
582 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
584 xpt_schedule(periph, CAM_PRIORITY_DEV);
591 struct ccb_hdr *ccbh;
593 softc = (struct cd_softc *)periph->softc;
595 * Don't fail on the expected unit attention
598 softc->flags |= CD_FLAG_RETRY_UA;
599 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
600 ccbh->ccb_state |= CD_CCB_RETRY_UA;
604 cam_periph_async(periph, code, path, arg);
610 cdsysctlinit(void *context, int pending)
612 struct cam_periph *periph;
613 struct cd_softc *softc;
614 char tmpstr[80], tmpstr2[80];
616 periph = (struct cam_periph *)context;
617 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
620 softc = (struct cd_softc *)periph->softc;
621 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
622 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
624 sysctl_ctx_init(&softc->sysctl_ctx);
625 softc->flags |= CD_FLAG_SCTX_INIT;
626 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
627 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
628 tmpstr2, CTLFLAG_RD, 0, tmpstr);
630 if (softc->sysctl_tree == NULL) {
631 printf("cdsysctlinit: unable to allocate sysctl tree\n");
632 cam_periph_release(periph);
637 * Now register the sysctl handler, so the user can the value on
640 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
641 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
642 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
645 cam_periph_release(periph);
649 * We have a handler function for this so we can check the values when the
650 * user sets them, instead of every time we look at them.
653 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
657 value = *(int *)arg1;
659 error = sysctl_handle_int(oidp, &value, 0, req);
662 || (req->newptr == NULL))
666 * The only real values we can have here are 6 or 10. I don't
667 * really forsee having 12 be an option at any time in the future.
668 * So if the user sets something less than or equal to 6, we'll set
669 * it to 6. If he sets something greater than 6, we'll set it to 10.
671 * I suppose we could just return an error here for the wrong values,
672 * but I don't think it's necessary to do so, as long as we can
673 * determine the user's intent without too much trouble.
680 *(int *)arg1 = value;
686 cdregister(struct cam_periph *periph, void *arg)
688 struct cd_softc *softc;
689 struct ccb_pathinq cpi;
690 struct ccb_getdev *cgd;
694 cgd = (struct ccb_getdev *)arg;
696 printf("cdregister: no getdev CCB, can't register device\n");
697 return(CAM_REQ_CMP_ERR);
700 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
703 printf("cdregister: Unable to probe new device. "
704 "Unable to allocate softc\n");
705 return(CAM_REQ_CMP_ERR);
708 LIST_INIT(&softc->pending_ccbs);
709 STAILQ_INIT(&softc->mode_queue);
710 softc->state = CD_STATE_PROBE;
711 bioq_init(&softc->bio_queue);
712 if (SID_IS_REMOVABLE(&cgd->inq_data))
713 softc->flags |= CD_FLAG_DISC_REMOVABLE;
715 periph->softc = softc;
716 softc->periph = periph;
719 * See if this device has any quirks.
721 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
722 (caddr_t)cd_quirk_table,
723 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
724 sizeof(*cd_quirk_table), scsi_inquiry_match);
727 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
729 softc->quirks = CD_Q_NONE;
731 /* Check if the SIM does not want 6 byte commands */
732 bzero(&cpi, sizeof(cpi));
733 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
734 cpi.ccb_h.func_code = XPT_PATH_INQ;
735 xpt_action((union ccb *)&cpi);
736 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
737 softc->quirks |= CD_Q_10_BYTE_ONLY;
739 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
741 /* The default is 6 byte commands, unless quirked otherwise */
742 if (softc->quirks & CD_Q_10_BYTE_ONLY)
743 softc->minimum_command_size = 10;
745 softc->minimum_command_size = 6;
748 * Refcount and block open attempts until we are setup
751 (void)cam_periph_hold(periph, PRIBIO);
752 cam_periph_unlock(periph);
754 * Load the user's default, if any.
756 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
757 periph->unit_number);
758 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
760 /* 6 and 10 are the only permissible values here. */
761 if (softc->minimum_command_size < 6)
762 softc->minimum_command_size = 6;
763 else if (softc->minimum_command_size > 6)
764 softc->minimum_command_size = 10;
767 * We need to register the statistics structure for this device,
768 * but we don't have the blocksize yet for it. So, we register
769 * the structure and indicate that we don't have the blocksize
770 * yet. Unlike other SCSI peripheral drivers, we explicitly set
771 * the device type here to be CDROM, rather than just ORing in
772 * the device type. This is because this driver can attach to either
773 * CDROM or WORM devices, and we want this peripheral driver to
774 * show up in the devstat list as a CD peripheral driver, not a
775 * WORM peripheral driver. WORM drives will also have the WORM
776 * driver attached to them.
778 softc->disk = disk_alloc();
779 softc->disk->d_devstat = devstat_new_entry("cd",
780 periph->unit_number, 0,
781 DEVSTAT_BS_UNAVAILABLE,
783 XPORT_DEVSTAT_TYPE(cpi.transport),
784 DEVSTAT_PRIORITY_CD);
785 softc->disk->d_open = cdopen;
786 softc->disk->d_close = cdclose;
787 softc->disk->d_strategy = cdstrategy;
788 softc->disk->d_gone = cddiskgonecb;
789 softc->disk->d_ioctl = cdioctl;
790 softc->disk->d_name = "cd";
791 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
792 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
793 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
794 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
795 cgd->inq_data.product, sizeof(cgd->inq_data.product),
796 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
797 softc->disk->d_unit = periph->unit_number;
798 softc->disk->d_drv1 = periph;
800 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
801 else if (cpi.maxio > MAXPHYS)
802 softc->disk->d_maxsize = MAXPHYS; /* for safety */
804 softc->disk->d_maxsize = cpi.maxio;
805 softc->disk->d_flags = 0;
806 softc->disk->d_hba_vendor = cpi.hba_vendor;
807 softc->disk->d_hba_device = cpi.hba_device;
808 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
809 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
812 * Acquire a reference to the periph before we register with GEOM.
813 * We'll release this reference once GEOM calls us back (via
814 * dadiskgonecb()) telling us that our provider has been freed.
816 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
817 xpt_print(periph->path, "%s: lost periph during "
818 "registration!\n", __func__);
819 cam_periph_lock(periph);
820 return (CAM_REQ_CMP_ERR);
823 disk_create(softc->disk, DISK_VERSION);
824 cam_periph_lock(periph);
827 * Add an async callback so that we get
828 * notified if this device goes away.
830 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
831 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
834 * If the target lun is greater than 0, we most likely have a CD
835 * changer device. Check the quirk entries as well, though, just
836 * in case someone has a CD tower with one lun per drive or
837 * something like that. Also, if we know up front that a
838 * particular device is a changer, we can mark it as such starting
839 * with lun 0, instead of lun 1. It shouldn't be necessary to have
840 * a quirk entry to define something as a changer, however.
842 if (((cgd->ccb_h.target_lun > 0)
843 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
844 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
845 struct cdchanger *nchanger;
846 struct cam_periph *nperiph;
847 struct cam_path *path;
851 /* Set the changer flag in the current device's softc */
852 softc->flags |= CD_FLAG_CHANGER;
855 * Now, look around for an existing changer device with the
856 * same path and target ID as the current device.
858 mtx_lock(&changerq_mtx);
860 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
862 nchanger = STAILQ_NEXT(nchanger, changer_links)){
863 if ((nchanger->path_id == cgd->ccb_h.path_id)
864 && (nchanger->target_id == cgd->ccb_h.target_id)) {
869 mtx_unlock(&changerq_mtx);
872 * If we found a matching entry, just add this device to
873 * the list of devices on this changer.
876 struct chdevlist *chlunhead;
878 chlunhead = &nchanger->chluns;
881 * XXX KDM look at consolidating this code with the
882 * code below in a separate function.
886 * Create a path with lun id 0, and see if we can
887 * find a matching device
889 status = xpt_create_path(&path, /*periph*/ periph,
891 cgd->ccb_h.target_id, 0);
893 if ((status == CAM_REQ_CMP)
894 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
895 struct cd_softc *nsoftc;
897 nsoftc = (struct cd_softc *)nperiph->softc;
899 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
900 nsoftc->flags |= CD_FLAG_CHANGER;
901 nchanger->num_devices++;
902 if (camq_resize(&nchanger->devq,
903 nchanger->num_devices)!=CAM_REQ_CMP){
904 printf("cdregister: "
910 nsoftc->changer = nchanger;
911 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
913 STAILQ_INSERT_TAIL(&nchanger->chluns,
914 nsoftc,changer_links);
917 } else if (status == CAM_REQ_CMP)
920 printf("cdregister: unable to allocate path\n"
921 "cdregister: changer support may be "
925 nchanger->num_devices++;
927 softc->changer = nchanger;
928 softc->pinfo.index = CAM_UNQUEUED_INDEX;
930 if (camq_resize(&nchanger->devq,
931 nchanger->num_devices) != CAM_REQ_CMP) {
932 printf("cdregister: camq_resize "
933 "failed, changer support may "
937 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
940 * In this case, we don't already have an entry for this
941 * particular changer, so we need to create one, add it to
942 * the queue, and queue this device on the list for this
943 * changer. Before we queue this device, however, we need
944 * to search for lun id 0 on this target, and add it to the
945 * queue first, if it exists. (and if it hasn't already
946 * been marked as part of the changer.)
949 nchanger = malloc(sizeof(struct cdchanger),
950 M_DEVBUF, M_NOWAIT | M_ZERO);
951 if (nchanger == NULL) {
952 softc->flags &= ~CD_FLAG_CHANGER;
953 printf("cdregister: unable to malloc "
954 "changer structure\ncdregister: "
955 "changer support disabled\n");
958 * Yes, gotos can be gross but in this case
959 * I think it's justified..
963 if (camq_init(&nchanger->devq, 1) != 0) {
964 softc->flags &= ~CD_FLAG_CHANGER;
965 printf("cdregister: changer support "
970 nchanger->path_id = cgd->ccb_h.path_id;
971 nchanger->target_id = cgd->ccb_h.target_id;
973 /* this is superfluous, but it makes things clearer */
974 nchanger->num_devices = 0;
976 STAILQ_INIT(&nchanger->chluns);
978 callout_init_mtx(&nchanger->long_handle,
979 periph->sim->mtx, 0);
980 callout_init_mtx(&nchanger->short_handle,
981 periph->sim->mtx, 0);
983 mtx_lock(&changerq_mtx);
985 STAILQ_INSERT_TAIL(&changerq, nchanger,
987 mtx_unlock(&changerq_mtx);
990 * Create a path with lun id 0, and see if we can
991 * find a matching device
993 status = xpt_create_path(&path, /*periph*/ periph,
995 cgd->ccb_h.target_id, 0);
998 * If we were able to allocate the path, and if we
999 * find a matching device and it isn't already
1000 * marked as part of a changer, then we add it to
1001 * the current changer.
1003 if ((status == CAM_REQ_CMP)
1004 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
1005 && ((((struct cd_softc *)periph->softc)->flags &
1006 CD_FLAG_CHANGER) == 0)) {
1007 struct cd_softc *nsoftc;
1009 nsoftc = (struct cd_softc *)nperiph->softc;
1011 nsoftc->flags |= CD_FLAG_CHANGER;
1012 nchanger->num_devices++;
1013 if (camq_resize(&nchanger->devq,
1014 nchanger->num_devices) != CAM_REQ_CMP) {
1015 printf("cdregister: camq_resize "
1016 "failed, changer support may "
1019 nsoftc->changer = nchanger;
1020 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
1022 STAILQ_INSERT_TAIL(&nchanger->chluns,
1023 nsoftc, changer_links);
1024 xpt_free_path(path);
1025 } else if (status == CAM_REQ_CMP)
1026 xpt_free_path(path);
1028 printf("cdregister: unable to allocate path\n"
1029 "cdregister: changer support may be "
1033 softc->changer = nchanger;
1034 softc->pinfo.index = CAM_UNQUEUED_INDEX;
1035 nchanger->num_devices++;
1036 if (camq_resize(&nchanger->devq,
1037 nchanger->num_devices) != CAM_REQ_CMP) {
1038 printf("cdregister: camq_resize "
1039 "failed, changer support may "
1042 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
1048 * Schedule a periodic media polling events.
1050 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1051 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
1052 (softc->flags & CD_FLAG_CHANGER) == 0 &&
1053 (cgd->inq_flags & SID_AEN) == 0 &&
1054 cd_poll_period != 0)
1055 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
1056 cdmediapoll, periph);
1060 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1061 xpt_schedule(periph, CAM_PRIORITY_DEV);
1063 cdschedule(periph, CAM_PRIORITY_DEV);
1065 return(CAM_REQ_CMP);
1069 cdopen(struct disk *dp)
1071 struct cam_periph *periph;
1072 struct cd_softc *softc;
1075 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;
1126 softc = (struct cd_softc *)periph->softc;
1128 cam_periph_lock(periph);
1129 if (cam_periph_hold(periph, PRIBIO) != 0) {
1130 cam_periph_unlock(periph);
1131 cam_periph_release(periph);
1135 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1138 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1139 cdprevent(periph, PR_ALLOW);
1142 * Since we're closing this CD, mark the blocksize as unavailable.
1143 * It will be marked as available when the CD is opened again.
1145 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1148 * We'll check the media and toc again at the next open().
1150 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1152 cam_periph_unhold(periph);
1153 cam_periph_release_locked(periph);
1154 cam_periph_unlock(periph);
1160 cdshorttimeout(void *arg)
1162 struct cdchanger *changer;
1164 changer = (struct cdchanger *)arg;
1166 /* Always clear the short timeout flag, since that's what we're in */
1167 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1170 * Check to see if there is any more pending or outstanding I/O for
1171 * this device. If not, move it out of the active slot.
1173 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1174 && (changer->cur_device->outstanding_cmds == 0)) {
1175 changer->flags |= CHANGER_MANUAL_CALL;
1176 cdrunchangerqueue(changer);
1181 * This is a wrapper for xpt_schedule. It only applies to changers.
1184 cdschedule(struct cam_periph *periph, int priority)
1186 struct cd_softc *softc;
1188 softc = (struct cd_softc *)periph->softc;
1191 * If this device isn't currently queued, and if it isn't
1192 * the active device, then we queue this device and run the
1193 * changer queue if there is no timeout scheduled to do it.
1194 * If this device is the active device, just schedule it
1195 * to run again. If this device is queued, there should be
1196 * a timeout in place already that will make sure it runs.
1198 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1199 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1201 * We don't do anything with the priority here.
1202 * This is strictly a fifo queue.
1204 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1205 softc->pinfo.generation = ++softc->changer->devq.generation;
1206 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1209 * Since we just put a device in the changer queue,
1210 * check and see if there is a timeout scheduled for
1211 * this changer. If so, let the timeout handle
1212 * switching this device into the active slot. If
1213 * not, manually call the timeout routine to
1216 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1217 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1218 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1219 softc->changer->flags |= CHANGER_MANUAL_CALL;
1220 cdrunchangerqueue(softc->changer);
1222 } else if ((softc->flags & CD_FLAG_ACTIVE)
1223 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1224 xpt_schedule(periph, priority);
1228 cdrunchangerqueue(void *arg)
1230 struct cd_softc *softc;
1231 struct cdchanger *changer;
1232 int called_from_timeout;
1234 changer = (struct cdchanger *)arg;
1237 * If we have NOT been called from cdstrategy() or cddone(), and
1238 * instead from a timeout routine, go ahead and clear the
1241 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1242 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1243 called_from_timeout = 1;
1245 called_from_timeout = 0;
1247 /* Always clear the manual call flag */
1248 changer->flags &= ~CHANGER_MANUAL_CALL;
1250 /* nothing to do if the queue is empty */
1251 if (changer->devq.entries <= 0) {
1256 * If the changer queue is frozen, that means we have an active
1259 if (changer->devq.qfrozen_cnt[0] > 0) {
1262 * We always need to reset the frozen count and clear the
1265 changer->devq.qfrozen_cnt[0]--;
1266 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1267 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1269 if (changer->cur_device->outstanding_cmds > 0) {
1270 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1271 changer->cur_device->bufs_left =
1272 changer->cur_device->outstanding_cmds;
1273 if (called_from_timeout) {
1274 callout_reset(&changer->long_handle,
1275 changer_max_busy_seconds * hz,
1276 cdrunchangerqueue, changer);
1277 changer->flags |= CHANGER_TIMEOUT_SCHED;
1283 * Check to see whether the current device has any I/O left
1284 * to do. If so, requeue it at the end of the queue. If
1285 * not, there is no need to requeue it.
1287 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1289 changer->cur_device->pinfo.generation =
1290 ++changer->devq.generation;
1291 camq_insert(&changer->devq,
1292 (cam_pinfo *)changer->cur_device);
1296 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1298 changer->cur_device = softc;
1300 changer->devq.qfrozen_cnt[0]++;
1301 softc->flags |= CD_FLAG_ACTIVE;
1303 /* Just in case this device is waiting */
1304 wakeup(&softc->changer);
1305 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1308 * Get rid of any pending timeouts, and set a flag to schedule new
1309 * ones so this device gets its full time quantum.
1311 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1312 callout_stop(&changer->long_handle);
1313 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1316 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1317 callout_stop(&changer->short_handle);
1318 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1322 * We need to schedule timeouts, but we only do this after the
1323 * first transaction has completed. This eliminates the changer
1326 changer->flags |= CHANGER_NEED_TIMEOUT;
1330 cdchangerschedule(struct cd_softc *softc)
1332 struct cdchanger *changer;
1334 changer = softc->changer;
1337 * If this is a changer, and this is the current device,
1338 * and this device has at least the minimum time quantum to
1339 * run, see if we can switch it out.
1341 if ((softc->flags & CD_FLAG_ACTIVE)
1342 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1343 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1345 * We try three things here. The first is that we
1346 * check to see whether the schedule on completion
1347 * flag is set. If it is, we decrement the number
1348 * of buffers left, and if it's zero, we reschedule.
1349 * Next, we check to see whether the pending buffer
1350 * queue is empty and whether there are no
1351 * outstanding transactions. If so, we reschedule.
1352 * Next, we see if the pending buffer queue is empty.
1353 * If it is, we set the number of buffers left to
1354 * the current active buffer count and set the
1355 * schedule on complete flag.
1357 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1358 if (--softc->bufs_left == 0) {
1359 softc->changer->flags |=
1360 CHANGER_MANUAL_CALL;
1361 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1362 cdrunchangerqueue(softc->changer);
1364 } else if ((bioq_first(&softc->bio_queue) == NULL)
1365 && (softc->outstanding_cmds == 0)) {
1366 softc->changer->flags |= CHANGER_MANUAL_CALL;
1367 cdrunchangerqueue(softc->changer);
1369 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1370 && (softc->flags & CD_FLAG_ACTIVE)) {
1373 * Now that the first transaction to this
1374 * particular device has completed, we can go ahead
1375 * and schedule our timeouts.
1377 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1378 callout_reset(&changer->long_handle,
1379 changer_max_busy_seconds * hz,
1380 cdrunchangerqueue, changer);
1381 changer->flags |= CHANGER_TIMEOUT_SCHED;
1383 printf("cdchangerschedule: already have a long"
1386 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1387 callout_reset(&changer->short_handle,
1388 changer_min_busy_seconds * hz,
1389 cdshorttimeout, changer);
1390 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1392 printf("cdchangerschedule: already have a short "
1396 * We just scheduled timeouts, no need to schedule
1399 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1405 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1406 u_int32_t cam_flags,
1407 u_int32_t sense_flags),
1408 u_int32_t cam_flags, u_int32_t sense_flags)
1410 struct cd_softc *softc;
1411 struct cam_periph *periph;
1414 periph = xpt_path_periph(ccb->ccb_h.path);
1415 softc = (struct cd_softc *)periph->softc;
1417 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1418 softc->disk->d_devstat);
1420 if (softc->flags & CD_FLAG_CHANGER)
1421 cdchangerschedule(softc);
1427 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1429 struct cd_softc *softc;
1431 softc = (struct cd_softc *)periph->softc;
1433 if (softc->flags & CD_FLAG_CHANGER) {
1435 * This should work the first time this device is woken up,
1436 * but just in case it doesn't, we use a while loop.
1438 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1440 * If this changer isn't already queued, queue it up.
1442 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1443 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1444 softc->pinfo.generation =
1445 ++softc->changer->devq.generation;
1446 camq_insert(&softc->changer->devq,
1447 (cam_pinfo *)softc);
1449 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1450 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1451 && ((softc->changer->flags
1452 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1453 softc->changer->flags |= CHANGER_MANUAL_CALL;
1454 cdrunchangerqueue(softc->changer);
1456 msleep(&softc->changer, periph->sim->mtx,
1457 PRIBIO, "cgticb", 0);
1460 return(cam_periph_getccb(periph, priority));
1465 * Actually translate the requested transfer into one the physical driver
1466 * can understand. The transfer is described by a buf and will include
1467 * only one physical transfer.
1470 cdstrategy(struct bio *bp)
1472 struct cam_periph *periph;
1473 struct cd_softc *softc;
1475 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1476 if (periph == NULL) {
1477 biofinish(bp, NULL, ENXIO);
1481 cam_periph_lock(periph);
1482 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1483 ("cdstrategy(%p)\n", bp));
1485 softc = (struct cd_softc *)periph->softc;
1488 * If the device has been made invalid, error out
1490 if ((softc->flags & CD_FLAG_INVALID)) {
1491 cam_periph_unlock(periph);
1492 biofinish(bp, NULL, ENXIO);
1497 * If we don't have valid media, look for it before trying to
1500 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1503 error = cdcheckmedia(periph);
1505 cam_periph_unlock(periph);
1506 biofinish(bp, NULL, error);
1512 * Place it in the queue of disk activities for this disk
1514 bioq_disksort(&softc->bio_queue, bp);
1517 * Schedule ourselves for performing the work. We do things
1518 * differently for changers.
1520 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1521 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1523 cdschedule(periph, CAM_PRIORITY_NORMAL);
1525 cam_periph_unlock(periph);
1530 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1532 struct cd_softc *softc;
1534 struct ccb_scsiio *csio;
1535 struct scsi_read_capacity_data *rcap;
1537 softc = (struct cd_softc *)periph->softc;
1539 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1541 switch (softc->state) {
1542 case CD_STATE_NORMAL:
1544 bp = bioq_first(&softc->bio_queue);
1545 if (periph->immediate_priority <= periph->pinfo.priority) {
1546 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1548 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1550 periph->immediate_priority = CAM_PRIORITY_NONE;
1551 wakeup(&periph->ccb_list);
1552 } else if (bp == NULL) {
1555 csio = &start_ccb->csio;
1556 scsi_test_unit_ready(csio,
1557 /*retries*/ cd_retry_count,
1562 start_ccb->ccb_h.ccb_bp = NULL;
1563 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
1564 xpt_action(start_ccb);
1566 xpt_release_ccb(start_ccb);
1570 cam_periph_release_locked(periph);
1572 bioq_remove(&softc->bio_queue, bp);
1574 scsi_read_write(&start_ccb->csio,
1575 /*retries*/ cd_retry_count,
1576 /* cbfcnp */ cddone,
1578 /* read */bp->bio_cmd == BIO_READ,
1580 /* minimum_cmd_size */ 10,
1581 /* lba */ bp->bio_offset /
1582 softc->params.blksize,
1583 bp->bio_bcount / softc->params.blksize,
1584 /* data_ptr */ bp->bio_data,
1585 /* dxfer_len */ bp->bio_bcount,
1586 /* sense_len */ cd_retry_count ?
1587 SSD_FULL_SIZE : SF_NO_PRINT,
1588 /* timeout */ cd_timeout);
1589 /* Use READ CD command for audio tracks. */
1590 if (softc->params.blksize == 2352) {
1591 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1592 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1593 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1594 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1595 start_ccb->csio.cdb_len = 12;
1597 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1600 LIST_INSERT_HEAD(&softc->pending_ccbs,
1601 &start_ccb->ccb_h, periph_links.le);
1602 softc->outstanding_cmds++;
1604 /* We expect a unit attention from this device */
1605 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1606 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1607 softc->flags &= ~CD_FLAG_RETRY_UA;
1610 start_ccb->ccb_h.ccb_bp = bp;
1611 bp = bioq_first(&softc->bio_queue);
1613 xpt_action(start_ccb);
1615 if (bp != NULL || softc->tur) {
1616 /* Have more work to do, so ensure we stay scheduled */
1617 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1621 case CD_STATE_PROBE:
1624 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1625 M_SCSICD, M_NOWAIT | M_ZERO);
1627 xpt_print(periph->path,
1628 "cdstart: Couldn't malloc read_capacity data\n");
1629 /* cd_free_periph??? */
1632 csio = &start_ccb->csio;
1633 scsi_read_capacity(csio,
1634 /*retries*/ cd_retry_count,
1640 start_ccb->ccb_h.ccb_bp = NULL;
1641 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1642 xpt_action(start_ccb);
1649 cddone(struct cam_periph *periph, union ccb *done_ccb)
1651 struct cd_softc *softc;
1652 struct ccb_scsiio *csio;
1654 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1656 softc = (struct cd_softc *)periph->softc;
1657 csio = &done_ccb->csio;
1659 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1660 case CD_CCB_BUFFER_IO:
1665 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1668 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1671 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1676 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1677 if (error == ERESTART) {
1679 * A retry was scheuled, so
1687 xpt_print(periph->path,
1688 "cddone: got error %#x back\n", error);
1689 bioq_flush(&softc->bio_queue, NULL, EIO);
1690 bp->bio_resid = bp->bio_bcount;
1691 bp->bio_error = error;
1692 bp->bio_flags |= BIO_ERROR;
1693 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1694 cam_release_devq(done_ccb->ccb_h.path,
1698 /*getcount_only*/0);
1701 bp->bio_resid = csio->resid;
1703 if (bp->bio_resid != 0) {
1705 * Short transfer ???
1706 * XXX: not sure this is correct for partial
1709 bp->bio_flags |= BIO_ERROR;
1713 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1714 softc->outstanding_cmds--;
1716 if (softc->flags & CD_FLAG_CHANGER)
1717 cdchangerschedule(softc);
1719 biofinish(bp, NULL, 0);
1724 struct scsi_read_capacity_data *rdcap;
1725 char announce_buf[120]; /*
1726 * Currently (9/30/97) the
1727 * longest possible announce
1728 * buffer is 108 bytes, for the
1729 * first error case below.
1730 * That is 39 bytes for the
1731 * basic string, 16 bytes for the
1732 * biggest sense key (hardware
1733 * error), 52 bytes for the
1734 * text of the largest sense
1735 * qualifier valid for a CDROM,
1736 * (0x72, 0x03 or 0x04,
1737 * 0x03), and one byte for the
1738 * null terminating character.
1739 * To allow for longer strings,
1740 * the announce buffer is 120
1743 struct cd_params *cdp;
1746 cdp = &softc->params;
1748 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1750 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1751 cdp->blksize = scsi_4btoul (rdcap->length);
1754 * Retry any UNIT ATTENTION type errors. They
1755 * are expected at boot.
1757 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1758 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1759 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1761 snprintf(announce_buf, sizeof(announce_buf),
1762 "cd present [%lu x %lu byte records]",
1763 cdp->disksize, (u_long)cdp->blksize);
1766 if (error == ERESTART) {
1768 * A retry was scheuled, so
1774 int sense_key, error_code;
1777 struct ccb_getdev cgd;
1779 /* Don't wedge this device's queue */
1780 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1781 cam_release_devq(done_ccb->ccb_h.path,
1785 /*getcount_only*/0);
1787 status = done_ccb->ccb_h.status;
1789 xpt_setup_ccb(&cgd.ccb_h,
1790 done_ccb->ccb_h.path,
1791 CAM_PRIORITY_NORMAL);
1792 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1793 xpt_action((union ccb *)&cgd);
1795 if (scsi_extract_sense_ccb(done_ccb,
1796 &error_code, &sense_key, &asc, &ascq))
1802 * Attach to anything that claims to be a
1803 * CDROM or WORM device, as long as it
1804 * doesn't return a "Logical unit not
1805 * supported" (0x25) error.
1807 if ((have_sense) && (asc != 0x25)
1808 && (error_code == SSD_CURRENT_ERROR)) {
1809 const char *sense_key_desc;
1810 const char *asc_desc;
1812 scsi_sense_desc(sense_key, asc, ascq,
1816 snprintf(announce_buf,
1817 sizeof(announce_buf),
1818 "Attempt to query device "
1819 "size failed: %s, %s",
1822 } else if ((have_sense == 0)
1823 && ((status & CAM_STATUS_MASK) ==
1824 CAM_SCSI_STATUS_ERROR)
1825 && (csio->scsi_status ==
1826 SCSI_STATUS_BUSY)) {
1827 snprintf(announce_buf,
1828 sizeof(announce_buf),
1829 "Attempt to query device "
1830 "size failed: SCSI Status: %s",
1831 scsi_status_string(csio));
1832 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1834 * We only print out an error for
1835 * CDROM type devices. For WORM
1836 * devices, we don't print out an
1837 * error since a few WORM devices
1838 * don't support CDROM commands.
1839 * If we have sense information, go
1840 * ahead and print it out.
1841 * Otherwise, just say that we
1846 * Just print out the error, not
1847 * the full probe message, when we
1854 xpt_print(periph->path,
1855 "got CAM status %#x\n",
1856 done_ccb->ccb_h.status);
1858 xpt_print(periph->path, "fatal error, "
1859 "failed to attach to device\n");
1861 * Invalidate this peripheral.
1863 cam_periph_invalidate(periph);
1865 announce_buf[0] = '\0';
1869 * Invalidate this peripheral.
1871 cam_periph_invalidate(periph);
1872 announce_buf[0] = '\0';
1876 free(rdcap, M_SCSICD);
1877 if (announce_buf[0] != '\0') {
1878 xpt_announce_periph(periph, announce_buf);
1879 if (softc->flags & CD_FLAG_CHANGER)
1880 cdchangerschedule(softc);
1882 * Create our sysctl variables, now that we know
1883 * we have successfully attached.
1885 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1887 softc->state = CD_STATE_NORMAL;
1889 * Since our peripheral may be invalidated by an error
1890 * above or an external event, we must release our CCB
1891 * before releasing the probe lock on the peripheral.
1892 * The peripheral will only go away once the last lock
1893 * is removed, and we need it around for the CCB release
1896 xpt_release_ccb(done_ccb);
1897 cam_periph_unhold(periph);
1900 case CD_CCB_WAITING:
1902 /* Caller will release the CCB */
1903 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1904 ("trying to wakeup ccbwait\n"));
1906 wakeup(&done_ccb->ccb_h.cbfcnp);
1911 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1913 if (cderror(done_ccb, CAM_RETRY_SELTO,
1914 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1917 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1918 cam_release_devq(done_ccb->ccb_h.path,
1922 /*getcount_only*/0);
1924 xpt_release_ccb(done_ccb);
1925 cam_periph_release_locked(periph);
1931 xpt_release_ccb(done_ccb);
1934 static union cd_pages *
1935 cdgetpage(struct cd_mode_params *mode_params)
1937 union cd_pages *page;
1939 if (mode_params->cdb_size == 10)
1940 page = (union cd_pages *)find_mode_page_10(
1941 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1943 page = (union cd_pages *)find_mode_page_6(
1944 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1950 cdgetpagesize(int page_num)
1954 for (i = 0; i < (sizeof(cd_page_size_table)/
1955 sizeof(cd_page_size_table[0])); i++) {
1956 if (cd_page_size_table[i].page == page_num)
1957 return (cd_page_size_table[i].page_size);
1964 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1967 struct cam_periph *periph;
1968 struct cd_softc *softc;
1969 int nocopyout, error = 0;
1971 periph = (struct cam_periph *)dp->d_drv1;
1975 cam_periph_lock(periph);
1977 softc = (struct cd_softc *)periph->softc;
1979 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1980 ("cdioctl(%#lx)\n", cmd));
1982 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1983 cam_periph_unlock(periph);
1984 cam_periph_release(periph);
1989 * If we don't have media loaded, check for it. If still don't
1990 * have media loaded, we can only do a load or eject.
1992 * We only care whether media is loaded if this is a cd-specific ioctl
1993 * (thus the IOCGROUP check below). Note that this will break if
1994 * anyone adds any ioctls into the switch statement below that don't
1995 * have their ioctl group set to 'c'.
1997 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1998 && ((cmd != CDIOCCLOSE)
1999 && (cmd != CDIOCEJECT))
2000 && (IOCGROUP(cmd) == 'c')) {
2001 error = cdcheckmedia(periph);
2003 cam_periph_unhold(periph);
2004 cam_periph_unlock(periph);
2009 * Drop the lock here so later mallocs can use WAITOK. The periph
2010 * is essentially locked still with the cam_periph_hold call above.
2012 cam_periph_unlock(periph);
2017 case CDIOCPLAYTRACKS:
2019 struct ioc_play_track *args
2020 = (struct ioc_play_track *) addr;
2021 struct cd_mode_params params;
2022 union cd_pages *page;
2024 params.alloc_len = sizeof(union cd_mode_data_6_10);
2025 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2028 cam_periph_lock(periph);
2029 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2030 ("trying to do CDIOCPLAYTRACKS\n"));
2032 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2034 free(params.mode_buf, M_SCSICD);
2035 cam_periph_unlock(periph);
2038 page = cdgetpage(¶ms);
2040 page->audio.flags &= ~CD_PA_SOTC;
2041 page->audio.flags |= CD_PA_IMMED;
2042 error = cdsetmode(periph, ¶ms);
2043 free(params.mode_buf, M_SCSICD);
2045 cam_periph_unlock(periph);
2050 * This was originally implemented with the PLAY
2051 * AUDIO TRACK INDEX command, but that command was
2052 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2053 * drives support it but ATAPI and ATAPI-derivative
2054 * drives don't seem to support it. So we keep a
2055 * cache of the table of contents and translate
2056 * track numbers to MSF format.
2058 if (softc->flags & CD_FLAG_VALID_TOC) {
2059 union msf_lba *sentry, *eentry;
2062 if (args->end_track <
2063 softc->toc.header.ending_track + 1)
2065 if (args->end_track >
2066 softc->toc.header.ending_track + 1)
2068 softc->toc.header.ending_track + 1;
2069 st = args->start_track -
2070 softc->toc.header.starting_track;
2071 et = args->end_track -
2072 softc->toc.header.starting_track;
2075 || (st > (softc->toc.header.ending_track -
2076 softc->toc.header.starting_track))) {
2078 cam_periph_unlock(periph);
2081 sentry = &softc->toc.entries[st].addr;
2082 eentry = &softc->toc.entries[et].addr;
2083 error = cdplaymsf(periph,
2092 * If we don't have a valid TOC, try the
2093 * play track index command. It is part of
2094 * the SCSI-2 spec, but was removed in the
2095 * MMC specs. ATAPI and ATAPI-derived
2096 * drives don't support it.
2098 if (softc->quirks & CD_Q_BCD_TRACKS) {
2100 bin2bcd(args->start_track);
2102 bin2bcd(args->end_track);
2104 error = cdplaytracks(periph,
2110 cam_periph_unlock(periph);
2115 struct ioc_play_msf *args
2116 = (struct ioc_play_msf *) addr;
2117 struct cd_mode_params params;
2118 union cd_pages *page;
2120 params.alloc_len = sizeof(union cd_mode_data_6_10);
2121 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2124 cam_periph_lock(periph);
2125 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2126 ("trying to do CDIOCPLAYMSF\n"));
2128 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2130 free(params.mode_buf, M_SCSICD);
2131 cam_periph_unlock(periph);
2134 page = cdgetpage(¶ms);
2136 page->audio.flags &= ~CD_PA_SOTC;
2137 page->audio.flags |= CD_PA_IMMED;
2138 error = cdsetmode(periph, ¶ms);
2139 free(params.mode_buf, M_SCSICD);
2141 cam_periph_unlock(periph);
2144 error = cdplaymsf(periph,
2151 cam_periph_unlock(periph);
2154 case CDIOCPLAYBLOCKS:
2156 struct ioc_play_blocks *args
2157 = (struct ioc_play_blocks *) addr;
2158 struct cd_mode_params params;
2159 union cd_pages *page;
2161 params.alloc_len = sizeof(union cd_mode_data_6_10);
2162 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2165 cam_periph_lock(periph);
2166 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2167 ("trying to do CDIOCPLAYBLOCKS\n"));
2170 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2172 free(params.mode_buf, M_SCSICD);
2173 cam_periph_unlock(periph);
2176 page = cdgetpage(¶ms);
2178 page->audio.flags &= ~CD_PA_SOTC;
2179 page->audio.flags |= CD_PA_IMMED;
2180 error = cdsetmode(periph, ¶ms);
2181 free(params.mode_buf, M_SCSICD);
2183 cam_periph_unlock(periph);
2186 error = cdplay(periph, args->blk, args->len);
2187 cam_periph_unlock(periph);
2190 case CDIOCREADSUBCHANNEL_SYSSPACE:
2193 case CDIOCREADSUBCHANNEL:
2195 struct ioc_read_subchannel *args
2196 = (struct ioc_read_subchannel *) addr;
2197 struct cd_sub_channel_info *data;
2198 u_int32_t len = args->data_len;
2200 data = malloc(sizeof(struct cd_sub_channel_info),
2201 M_SCSICD, M_WAITOK | M_ZERO);
2203 cam_periph_lock(periph);
2204 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2205 ("trying to do CDIOCREADSUBCHANNEL\n"));
2207 if ((len > sizeof(struct cd_sub_channel_info)) ||
2208 (len < sizeof(struct cd_sub_channel_header))) {
2210 "scsi_cd: cdioctl: "
2211 "cdioreadsubchannel: error, len=%d\n",
2214 free(data, M_SCSICD);
2215 cam_periph_unlock(periph);
2219 if (softc->quirks & CD_Q_BCD_TRACKS)
2220 args->track = bin2bcd(args->track);
2222 error = cdreadsubchannel(periph, args->address_format,
2223 args->data_format, args->track, data, len);
2226 free(data, M_SCSICD);
2227 cam_periph_unlock(periph);
2230 if (softc->quirks & CD_Q_BCD_TRACKS)
2231 data->what.track_info.track_number =
2232 bcd2bin(data->what.track_info.track_number);
2233 len = min(len, ((data->header.data_len[0] << 8) +
2234 data->header.data_len[1] +
2235 sizeof(struct cd_sub_channel_header)));
2236 cam_periph_unlock(periph);
2237 if (nocopyout == 0) {
2238 if (copyout(data, args->data, len) != 0) {
2242 bcopy(data, args->data, len);
2244 free(data, M_SCSICD);
2248 case CDIOREADTOCHEADER:
2250 struct ioc_toc_header *th;
2252 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2255 cam_periph_lock(periph);
2256 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2257 ("trying to do CDIOREADTOCHEADER\n"));
2259 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2260 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2263 cam_periph_unlock(periph);
2266 if (softc->quirks & CD_Q_BCD_TRACKS) {
2267 /* we are going to have to convert the BCD
2268 * encoding on the cd to what is expected
2270 th->starting_track =
2271 bcd2bin(th->starting_track);
2272 th->ending_track = bcd2bin(th->ending_track);
2274 th->len = ntohs(th->len);
2275 bcopy(th, addr, sizeof(*th));
2277 cam_periph_unlock(periph);
2280 case CDIOREADTOCENTRYS:
2282 struct cd_tocdata *data;
2283 struct cd_toc_single *lead;
2284 struct ioc_read_toc_entry *te =
2285 (struct ioc_read_toc_entry *) addr;
2286 struct ioc_toc_header *th;
2287 u_int32_t len, readlen, idx, num;
2288 u_int32_t starting_track = te->starting_track;
2290 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2291 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2293 cam_periph_lock(periph);
2294 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2295 ("trying to do CDIOREADTOCENTRYS\n"));
2297 if (te->data_len < sizeof(struct cd_toc_entry)
2298 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2299 || (te->address_format != CD_MSF_FORMAT
2300 && te->address_format != CD_LBA_FORMAT)) {
2302 printf("scsi_cd: error in readtocentries, "
2303 "returning EINVAL\n");
2304 free(data, M_SCSICD);
2305 free(lead, M_SCSICD);
2306 cam_periph_unlock(periph);
2311 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2312 sizeof (*th), /*sense_flags*/0);
2314 free(data, M_SCSICD);
2315 free(lead, M_SCSICD);
2316 cam_periph_unlock(periph);
2320 if (softc->quirks & CD_Q_BCD_TRACKS) {
2321 /* we are going to have to convert the BCD
2322 * encoding on the cd to what is expected
2324 th->starting_track =
2325 bcd2bin(th->starting_track);
2326 th->ending_track = bcd2bin(th->ending_track);
2329 if (starting_track == 0)
2330 starting_track = th->starting_track;
2331 else if (starting_track == LEADOUT)
2332 starting_track = th->ending_track + 1;
2333 else if (starting_track < th->starting_track ||
2334 starting_track > th->ending_track + 1) {
2335 printf("scsi_cd: error in readtocentries, "
2336 "returning EINVAL\n");
2337 free(data, M_SCSICD);
2338 free(lead, M_SCSICD);
2339 cam_periph_unlock(periph);
2344 /* calculate reading length without leadout entry */
2345 readlen = (th->ending_track - starting_track + 1) *
2346 sizeof(struct cd_toc_entry);
2348 /* and with leadout entry */
2349 len = readlen + sizeof(struct cd_toc_entry);
2350 if (te->data_len < len) {
2355 if (len > sizeof(data->entries)) {
2356 printf("scsi_cd: error in readtocentries, "
2357 "returning EINVAL\n");
2359 free(data, M_SCSICD);
2360 free(lead, M_SCSICD);
2361 cam_periph_unlock(periph);
2364 num = len / sizeof(struct cd_toc_entry);
2367 error = cdreadtoc(periph, te->address_format,
2370 readlen + sizeof (*th),
2373 free(data, M_SCSICD);
2374 free(lead, M_SCSICD);
2375 cam_periph_unlock(periph);
2380 /* make leadout entry if needed */
2381 idx = starting_track + num - 1;
2382 if (softc->quirks & CD_Q_BCD_TRACKS)
2383 th->ending_track = bcd2bin(th->ending_track);
2384 if (idx == th->ending_track + 1) {
2385 error = cdreadtoc(periph, te->address_format,
2386 LEADOUT, (u_int8_t *)lead,
2390 free(data, M_SCSICD);
2391 free(lead, M_SCSICD);
2392 cam_periph_unlock(periph);
2395 data->entries[idx - starting_track] =
2398 if (softc->quirks & CD_Q_BCD_TRACKS) {
2399 for (idx = 0; idx < num - 1; idx++) {
2400 data->entries[idx].track =
2401 bcd2bin(data->entries[idx].track);
2405 cam_periph_unlock(periph);
2406 error = copyout(data->entries, te->data, len);
2407 free(data, M_SCSICD);
2408 free(lead, M_SCSICD);
2411 case CDIOREADTOCENTRY:
2413 struct cd_toc_single *data;
2414 struct ioc_read_toc_single_entry *te =
2415 (struct ioc_read_toc_single_entry *) addr;
2416 struct ioc_toc_header *th;
2419 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2421 cam_periph_lock(periph);
2422 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2423 ("trying to do CDIOREADTOCENTRY\n"));
2425 if (te->address_format != CD_MSF_FORMAT
2426 && te->address_format != CD_LBA_FORMAT) {
2427 printf("error in readtocentry, "
2428 " returning EINVAL\n");
2429 free(data, M_SCSICD);
2431 cam_periph_unlock(periph);
2436 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2437 sizeof (*th), /*sense_flags*/0);
2439 free(data, M_SCSICD);
2440 cam_periph_unlock(periph);
2444 if (softc->quirks & CD_Q_BCD_TRACKS) {
2445 /* we are going to have to convert the BCD
2446 * encoding on the cd to what is expected
2448 th->starting_track =
2449 bcd2bin(th->starting_track);
2450 th->ending_track = bcd2bin(th->ending_track);
2454 track = th->starting_track;
2455 else if (track == LEADOUT)
2457 else if (track < th->starting_track ||
2458 track > th->ending_track + 1) {
2459 printf("error in readtocentry, "
2460 " returning EINVAL\n");
2461 free(data, M_SCSICD);
2463 cam_periph_unlock(periph);
2467 error = cdreadtoc(periph, te->address_format, track,
2468 (u_int8_t *)data, sizeof(*data),
2471 free(data, M_SCSICD);
2472 cam_periph_unlock(periph);
2476 if (softc->quirks & CD_Q_BCD_TRACKS)
2477 data->entry.track = bcd2bin(data->entry.track);
2478 bcopy(&data->entry, &te->entry,
2479 sizeof(struct cd_toc_entry));
2480 free(data, M_SCSICD);
2481 cam_periph_unlock(periph);
2486 struct ioc_patch *arg = (struct ioc_patch *)addr;
2487 struct cd_mode_params params;
2488 union cd_pages *page;
2490 params.alloc_len = sizeof(union cd_mode_data_6_10);
2491 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2494 cam_periph_lock(periph);
2495 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2496 ("trying to do CDIOCSETPATCH\n"));
2498 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2500 free(params.mode_buf, M_SCSICD);
2501 cam_periph_unlock(periph);
2504 page = cdgetpage(¶ms);
2506 page->audio.port[LEFT_PORT].channels =
2508 page->audio.port[RIGHT_PORT].channels =
2510 page->audio.port[2].channels = arg->patch[2];
2511 page->audio.port[3].channels = arg->patch[3];
2512 error = cdsetmode(periph, ¶ms);
2513 free(params.mode_buf, M_SCSICD);
2514 cam_periph_unlock(periph);
2519 struct ioc_vol *arg = (struct ioc_vol *) addr;
2520 struct cd_mode_params params;
2521 union cd_pages *page;
2523 params.alloc_len = sizeof(union cd_mode_data_6_10);
2524 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2527 cam_periph_lock(periph);
2528 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2529 ("trying to do CDIOCGETVOL\n"));
2531 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2533 free(params.mode_buf, M_SCSICD);
2534 cam_periph_unlock(periph);
2537 page = cdgetpage(¶ms);
2539 arg->vol[LEFT_PORT] =
2540 page->audio.port[LEFT_PORT].volume;
2541 arg->vol[RIGHT_PORT] =
2542 page->audio.port[RIGHT_PORT].volume;
2543 arg->vol[2] = page->audio.port[2].volume;
2544 arg->vol[3] = page->audio.port[3].volume;
2545 free(params.mode_buf, M_SCSICD);
2546 cam_periph_unlock(periph);
2551 struct ioc_vol *arg = (struct ioc_vol *) addr;
2552 struct cd_mode_params params;
2553 union cd_pages *page;
2555 params.alloc_len = sizeof(union cd_mode_data_6_10);
2556 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2559 cam_periph_lock(periph);
2560 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2561 ("trying to do CDIOCSETVOL\n"));
2563 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2565 free(params.mode_buf, M_SCSICD);
2566 cam_periph_unlock(periph);
2569 page = cdgetpage(¶ms);
2571 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2572 page->audio.port[LEFT_PORT].volume =
2573 arg->vol[LEFT_PORT];
2574 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2575 page->audio.port[RIGHT_PORT].volume =
2576 arg->vol[RIGHT_PORT];
2577 page->audio.port[2].volume = arg->vol[2];
2578 page->audio.port[3].volume = arg->vol[3];
2579 error = cdsetmode(periph, ¶ms);
2580 cam_periph_unlock(periph);
2581 free(params.mode_buf, M_SCSICD);
2586 struct cd_mode_params params;
2587 union cd_pages *page;
2589 params.alloc_len = sizeof(union cd_mode_data_6_10);
2590 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2593 cam_periph_lock(periph);
2594 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2595 ("trying to do CDIOCSETMONO\n"));
2597 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2599 free(params.mode_buf, M_SCSICD);
2600 cam_periph_unlock(periph);
2603 page = cdgetpage(¶ms);
2605 page->audio.port[LEFT_PORT].channels =
2606 LEFT_CHANNEL | RIGHT_CHANNEL;
2607 page->audio.port[RIGHT_PORT].channels =
2608 LEFT_CHANNEL | RIGHT_CHANNEL;
2609 page->audio.port[2].channels = 0;
2610 page->audio.port[3].channels = 0;
2611 error = cdsetmode(periph, ¶ms);
2612 cam_periph_unlock(periph);
2613 free(params.mode_buf, M_SCSICD);
2616 case CDIOCSETSTEREO:
2618 struct cd_mode_params params;
2619 union cd_pages *page;
2621 params.alloc_len = sizeof(union cd_mode_data_6_10);
2622 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2625 cam_periph_lock(periph);
2626 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2627 ("trying to do CDIOCSETSTEREO\n"));
2629 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2631 free(params.mode_buf, M_SCSICD);
2632 cam_periph_unlock(periph);
2635 page = cdgetpage(¶ms);
2637 page->audio.port[LEFT_PORT].channels =
2639 page->audio.port[RIGHT_PORT].channels =
2641 page->audio.port[2].channels = 0;
2642 page->audio.port[3].channels = 0;
2643 error = cdsetmode(periph, ¶ms);
2644 free(params.mode_buf, M_SCSICD);
2645 cam_periph_unlock(periph);
2650 struct cd_mode_params params;
2651 union cd_pages *page;
2653 params.alloc_len = sizeof(union cd_mode_data_6_10);
2654 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2657 cam_periph_lock(periph);
2658 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2659 ("trying to do CDIOCSETMUTE\n"));
2661 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2663 free(params.mode_buf, M_SCSICD);
2664 cam_periph_unlock(periph);
2667 page = cdgetpage(¶ms);
2669 page->audio.port[LEFT_PORT].channels = 0;
2670 page->audio.port[RIGHT_PORT].channels = 0;
2671 page->audio.port[2].channels = 0;
2672 page->audio.port[3].channels = 0;
2673 error = cdsetmode(periph, ¶ms);
2674 free(params.mode_buf, M_SCSICD);
2675 cam_periph_unlock(periph);
2680 struct cd_mode_params params;
2681 union cd_pages *page;
2683 params.alloc_len = sizeof(union cd_mode_data_6_10);
2684 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2687 cam_periph_lock(periph);
2688 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2689 ("trying to do CDIOCSETLEFT\n"));
2691 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2693 free(params.mode_buf, M_SCSICD);
2694 cam_periph_unlock(periph);
2697 page = cdgetpage(¶ms);
2699 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2700 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2701 page->audio.port[2].channels = 0;
2702 page->audio.port[3].channels = 0;
2703 error = cdsetmode(periph, ¶ms);
2704 free(params.mode_buf, M_SCSICD);
2705 cam_periph_unlock(periph);
2710 struct cd_mode_params params;
2711 union cd_pages *page;
2713 params.alloc_len = sizeof(union cd_mode_data_6_10);
2714 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2717 cam_periph_lock(periph);
2718 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2719 ("trying to do CDIOCSETRIGHT\n"));
2721 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2723 free(params.mode_buf, M_SCSICD);
2724 cam_periph_unlock(periph);
2727 page = cdgetpage(¶ms);
2729 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2730 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2731 page->audio.port[2].channels = 0;
2732 page->audio.port[3].channels = 0;
2733 error = cdsetmode(periph, ¶ms);
2734 free(params.mode_buf, M_SCSICD);
2735 cam_periph_unlock(periph);
2739 cam_periph_lock(periph);
2740 error = cdpause(periph, 1);
2741 cam_periph_unlock(periph);
2744 cam_periph_lock(periph);
2745 error = cdpause(periph, 0);
2746 cam_periph_unlock(periph);
2749 cam_periph_lock(periph);
2750 error = cdstartunit(periph, 0);
2751 cam_periph_unlock(periph);
2754 cam_periph_lock(periph);
2755 error = cdstartunit(periph, 1);
2756 cam_periph_unlock(periph);
2759 cam_periph_lock(periph);
2760 error = cdstopunit(periph, 0);
2761 cam_periph_unlock(periph);
2764 cam_periph_lock(periph);
2765 error = cdstopunit(periph, 1);
2766 cam_periph_unlock(periph);
2769 cam_periph_lock(periph);
2770 cdprevent(periph, PR_ALLOW);
2771 cam_periph_unlock(periph);
2774 cam_periph_lock(periph);
2775 cdprevent(periph, PR_PREVENT);
2776 cam_periph_unlock(periph);
2779 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2783 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2787 /* return (cd_reset(periph)); */
2790 case CDRIOCREADSPEED:
2791 cam_periph_lock(periph);
2792 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2793 cam_periph_unlock(periph);
2795 case CDRIOCWRITESPEED:
2796 cam_periph_lock(periph);
2797 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2798 cam_periph_unlock(periph);
2800 case CDRIOCGETBLOCKSIZE:
2801 *(int *)addr = softc->params.blksize;
2803 case CDRIOCSETBLOCKSIZE:
2804 if (*(int *)addr <= 0) {
2808 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2811 case DVDIOCREPORTKEY: {
2812 struct dvd_authinfo *authinfo;
2814 authinfo = (struct dvd_authinfo *)addr;
2816 if (cmd == DVDIOCREPORTKEY)
2817 error = cdreportkey(periph, authinfo);
2819 error = cdsendkey(periph, authinfo);
2822 case DVDIOCREADSTRUCTURE: {
2823 struct dvd_struct *dvdstruct;
2825 dvdstruct = (struct dvd_struct *)addr;
2827 error = cdreaddvdstructure(periph, dvdstruct);
2832 cam_periph_lock(periph);
2833 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2834 cam_periph_unlock(periph);
2838 cam_periph_lock(periph);
2839 cam_periph_unhold(periph);
2841 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2842 if (error && bootverbose) {
2843 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2845 cam_periph_unlock(periph);
2851 cdprevent(struct cam_periph *periph, int action)
2854 struct cd_softc *softc;
2857 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2859 softc = (struct cd_softc *)periph->softc;
2861 if (((action == PR_ALLOW)
2862 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2863 || ((action == PR_PREVENT)
2864 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2868 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2870 scsi_prevent(&ccb->csio,
2871 /*retries*/ cd_retry_count,
2876 /* timeout */60000);
2878 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2879 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2881 xpt_release_ccb(ccb);
2884 if (action == PR_ALLOW)
2885 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2887 softc->flags |= CD_FLAG_DISC_LOCKED;
2892 * XXX: the disk media and sector size is only really able to change
2893 * XXX: while the device is closed.
2896 cdcheckmedia(struct cam_periph *periph)
2898 struct cd_softc *softc;
2899 struct ioc_toc_header *toch;
2900 struct cd_toc_single leadout;
2901 u_int32_t size, toclen;
2902 int error, num_entries, cdindex;
2904 softc = (struct cd_softc *)periph->softc;
2906 cdprevent(periph, PR_PREVENT);
2907 softc->disk->d_sectorsize = 2048;
2908 softc->disk->d_mediasize = 0;
2911 * Get the disc size and block size. If we can't get it, we don't
2912 * have media, most likely.
2914 if ((error = cdsize(periph, &size)) != 0) {
2915 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2916 cdprevent(periph, PR_ALLOW);
2919 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2920 softc->disk->d_sectorsize = softc->params.blksize;
2921 softc->disk->d_mediasize =
2922 (off_t)softc->params.blksize * softc->params.disksize;
2926 * Now we check the table of contents. This (currently) is only
2927 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2928 * things like present a separate entry in /dev for each track,
2929 * like that acd(4) driver does.
2931 bzero(&softc->toc, sizeof(softc->toc));
2932 toch = &softc->toc.header;
2934 * We will get errors here for media that doesn't have a table of
2935 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2936 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2937 * has not been recorded (no complete session) and the Format codes
2938 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2939 * with an INVALID FIELD IN CDB. Devices that are not capable of
2940 * reading an incomplete session on DDC/CD-R/RW media shall report
2941 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2943 * So this isn't fatal if we can't read the table of contents, it
2944 * just means that the user won't be able to issue the play tracks
2945 * ioctl, and likely lots of other stuff won't work either. They
2946 * need to burn the CD before we can do a whole lot with it. So
2947 * we don't print anything here if we get an error back.
2949 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2952 * Errors in reading the table of contents aren't fatal, we just
2953 * won't have a valid table of contents cached.
2957 bzero(&softc->toc, sizeof(softc->toc));
2961 if (softc->quirks & CD_Q_BCD_TRACKS) {
2962 toch->starting_track = bcd2bin(toch->starting_track);
2963 toch->ending_track = bcd2bin(toch->ending_track);
2966 /* Number of TOC entries, plus leadout */
2967 num_entries = (toch->ending_track - toch->starting_track) + 2;
2969 if (num_entries <= 0)
2972 toclen = num_entries * sizeof(struct cd_toc_entry);
2974 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2975 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2979 bzero(&softc->toc, sizeof(softc->toc));
2983 if (softc->quirks & CD_Q_BCD_TRACKS) {
2984 toch->starting_track = bcd2bin(toch->starting_track);
2985 toch->ending_track = bcd2bin(toch->ending_track);
2988 * XXX KDM is this necessary? Probably only if the drive doesn't
2989 * return leadout information with the table of contents.
2991 cdindex = toch->starting_track + num_entries -1;
2992 if (cdindex == toch->ending_track + 1) {
2994 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2995 (u_int8_t *)&leadout, sizeof(leadout),
3001 softc->toc.entries[cdindex - toch->starting_track] =
3004 if (softc->quirks & CD_Q_BCD_TRACKS) {
3005 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
3006 softc->toc.entries[cdindex].track =
3007 bcd2bin(softc->toc.entries[cdindex].track);
3011 softc->flags |= CD_FLAG_VALID_TOC;
3013 /* If the first track is audio, correct sector size. */
3014 if ((softc->toc.entries[0].control & 4) == 0) {
3015 softc->disk->d_sectorsize = softc->params.blksize = 2352;
3016 softc->disk->d_mediasize =
3017 (off_t)softc->params.blksize * softc->params.disksize;
3023 * We unconditionally (re)set the blocksize each time the
3024 * CD device is opened. This is because the CD can change,
3025 * and therefore the blocksize might change.
3026 * XXX problems here if some slice or partition is still
3027 * open with the old size?
3029 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
3030 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3031 softc->disk->d_devstat->block_size = softc->params.blksize;
3037 cdsize(struct cam_periph *periph, u_int32_t *size)
3039 struct cd_softc *softc;
3041 struct scsi_read_capacity_data *rcap_buf;
3044 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3046 softc = (struct cd_softc *)periph->softc;
3048 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3050 /* XXX Should be M_WAITOK */
3051 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3052 M_SCSICD, M_NOWAIT | M_ZERO);
3053 if (rcap_buf == NULL)
3056 scsi_read_capacity(&ccb->csio,
3057 /*retries*/ cd_retry_count,
3062 /* timeout */20000);
3064 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3065 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3067 xpt_release_ccb(ccb);
3069 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3070 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3071 /* Make sure we got at least some block size. */
3072 if (error == 0 && softc->params.blksize == 0)
3075 * SCSI-3 mandates that the reported blocksize shall be 2048.
3076 * Older drives sometimes report funny values, trim it down to
3077 * 2048, or other parts of the kernel will get confused.
3079 * XXX we leave drives alone that might report 512 bytes, as
3080 * well as drives reporting more weird sizes like perhaps 4K.
3082 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3083 softc->params.blksize = 2048;
3085 free(rcap_buf, M_SCSICD);
3086 *size = softc->params.disksize;
3093 cd6byteworkaround(union ccb *ccb)
3096 struct cam_periph *periph;
3097 struct cd_softc *softc;
3098 struct cd_mode_params *params;
3101 periph = xpt_path_periph(ccb->ccb_h.path);
3102 softc = (struct cd_softc *)periph->softc;
3104 cdb = ccb->csio.cdb_io.cdb_bytes;
3106 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3107 || ((cdb[0] != MODE_SENSE_6)
3108 && (cdb[0] != MODE_SELECT_6)))
3112 * Because there is no convenient place to stash the overall
3113 * cd_mode_params structure pointer, we have to grab it like this.
3114 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3115 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3117 * XXX It would be nice if, at some point, we could increase the
3118 * number of available peripheral private pointers. Both pointers
3119 * are currently used in most every peripheral driver.
3123 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3124 if (params->mode_buf == ccb->csio.data_ptr) {
3131 * This shouldn't happen. All mode sense and mode select
3132 * operations in the cd(4) driver MUST go through cdgetmode() and
3136 xpt_print(periph->path,
3137 "mode buffer not found in mode queue!\n");
3141 params->cdb_size = 10;
3142 softc->minimum_command_size = 10;
3143 xpt_print(ccb->ccb_h.path,
3144 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3145 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3147 if (cdb[0] == MODE_SENSE_6) {
3148 struct scsi_mode_sense_10 ms10;
3149 struct scsi_mode_sense_6 *ms6;
3152 ms6 = (struct scsi_mode_sense_6 *)cdb;
3154 bzero(&ms10, sizeof(ms10));
3155 ms10.opcode = MODE_SENSE_10;
3156 ms10.byte2 = ms6->byte2;
3157 ms10.page = ms6->page;
3160 * 10 byte mode header, block descriptor,
3161 * sizeof(union cd_pages)
3163 len = sizeof(struct cd_mode_data_10);
3164 ccb->csio.dxfer_len = len;
3166 scsi_ulto2b(len, ms10.length);
3167 ms10.control = ms6->control;
3168 bcopy(&ms10, cdb, 10);
3169 ccb->csio.cdb_len = 10;
3171 struct scsi_mode_select_10 ms10;
3172 struct scsi_mode_select_6 *ms6;
3173 struct scsi_mode_header_6 *header6;
3174 struct scsi_mode_header_10 *header10;
3175 struct scsi_mode_page_header *page_header;
3176 int blk_desc_len, page_num, page_size, len;
3178 ms6 = (struct scsi_mode_select_6 *)cdb;
3180 bzero(&ms10, sizeof(ms10));
3181 ms10.opcode = MODE_SELECT_10;
3182 ms10.byte2 = ms6->byte2;
3184 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3185 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3187 page_header = find_mode_page_6(header6);
3188 page_num = page_header->page_code;
3190 blk_desc_len = header6->blk_desc_len;
3192 page_size = cdgetpagesize(page_num);
3194 if (page_size != (page_header->page_length +
3195 sizeof(*page_header)))
3196 page_size = page_header->page_length +
3197 sizeof(*page_header);
3199 len = sizeof(*header10) + blk_desc_len + page_size;
3201 len = min(params->alloc_len, len);
3204 * Since the 6 byte parameter header is shorter than the 10
3205 * byte parameter header, we need to copy the actual mode
3206 * page data, and the block descriptor, if any, so things wind
3207 * up in the right place. The regions will overlap, but
3208 * bcopy() does the right thing.
3210 bcopy(params->mode_buf + sizeof(*header6),
3211 params->mode_buf + sizeof(*header10),
3212 len - sizeof(*header10));
3214 /* Make sure these fields are set correctly. */
3215 scsi_ulto2b(0, header10->data_length);
3216 header10->medium_type = 0;
3217 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3219 ccb->csio.dxfer_len = len;
3221 scsi_ulto2b(len, ms10.length);
3222 ms10.control = ms6->control;
3223 bcopy(&ms10, cdb, 10);
3224 ccb->csio.cdb_len = 10;
3227 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3228 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3231 cam_release_devq(ccb->ccb_h.path,
3235 /*getcount_only*/0);
3242 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3244 struct cd_softc *softc;
3245 struct cam_periph *periph;
3246 int error, error_code, sense_key, asc, ascq;
3248 periph = xpt_path_periph(ccb->ccb_h.path);
3249 softc = (struct cd_softc *)periph->softc;
3254 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3255 * CDB comes back with this particular error, try transforming it
3256 * into the 10 byte version.
3258 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3259 error = cd6byteworkaround(ccb);
3260 } else if (scsi_extract_sense_ccb(ccb,
3261 &error_code, &sense_key, &asc, &ascq)) {
3262 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3263 error = cd6byteworkaround(ccb);
3264 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3265 asc == 0x28 && ascq == 0x00)
3266 disk_media_changed(softc->disk, M_NOWAIT);
3267 else if (sense_key == SSD_KEY_NOT_READY &&
3268 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3269 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3270 disk_media_gone(softc->disk, M_NOWAIT);
3274 if (error == ERESTART)
3279 * Until we have a better way of doing pack validation,
3280 * don't treat UAs as errors.
3282 sense_flags |= SF_RETRY_UA;
3283 return (cam_periph_error(ccb, cam_flags, sense_flags,
3284 &softc->saved_ccb));
3288 cdmediapoll(void *arg)
3290 struct cam_periph *periph = arg;
3291 struct cd_softc *softc = periph->softc;
3293 if (softc->flags & CD_FLAG_CHANGER)
3296 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
3297 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3299 xpt_schedule(periph, CAM_PRIORITY_DEV);
3302 /* Queue us up again */
3303 if (cd_poll_period != 0)
3304 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3308 * Read table of contents
3311 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3312 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3314 struct scsi_read_toc *scsi_cmd;
3316 struct ccb_scsiio *csio;
3323 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3328 /* retries */ cd_retry_count,
3329 /* cbfcnp */ cddone,
3330 /* flags */ CAM_DIR_IN,
3331 /* tag_action */ MSG_SIMPLE_Q_TAG,
3332 /* data_ptr */ data,
3333 /* dxfer_len */ len,
3334 /* sense_len */ SSD_FULL_SIZE,
3335 sizeof(struct scsi_read_toc),
3336 /* timeout */ 50000);
3338 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3339 bzero (scsi_cmd, sizeof(*scsi_cmd));
3341 if (mode == CD_MSF_FORMAT)
3342 scsi_cmd->byte2 |= CD_MSF;
3343 scsi_cmd->from_track = start;
3344 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3345 scsi_cmd->data_len[0] = (ntoc) >> 8;
3346 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3348 scsi_cmd->op_code = READ_TOC;
3350 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3351 /*sense_flags*/SF_RETRY_UA | sense_flags);
3353 xpt_release_ccb(ccb);
3359 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3360 u_int32_t format, int track,
3361 struct cd_sub_channel_info *data, u_int32_t len)
3363 struct scsi_read_subchannel *scsi_cmd;
3364 struct ccb_scsiio *csio;
3370 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3375 /* retries */ cd_retry_count,
3376 /* cbfcnp */ cddone,
3377 /* flags */ CAM_DIR_IN,
3378 /* tag_action */ MSG_SIMPLE_Q_TAG,
3379 /* data_ptr */ (u_int8_t *)data,
3380 /* dxfer_len */ len,
3381 /* sense_len */ SSD_FULL_SIZE,
3382 sizeof(struct scsi_read_subchannel),
3383 /* timeout */ 50000);
3385 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3386 bzero (scsi_cmd, sizeof(*scsi_cmd));
3388 scsi_cmd->op_code = READ_SUBCHANNEL;
3389 if (mode == CD_MSF_FORMAT)
3390 scsi_cmd->byte1 |= CD_MSF;
3391 scsi_cmd->byte2 = SRS_SUBQ;
3392 scsi_cmd->subchan_format = format;
3393 scsi_cmd->track = track;
3394 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3395 scsi_cmd->control = 0;
3397 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3398 /*sense_flags*/SF_RETRY_UA);
3400 xpt_release_ccb(ccb);
3407 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3408 * routine. See comments in cd6byteworkaround() for details.
3411 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3414 struct ccb_scsiio *csio;
3415 struct cd_softc *softc;
3420 softc = (struct cd_softc *)periph->softc;
3422 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3426 data->cdb_size = softc->minimum_command_size;
3427 if (data->cdb_size < 10)
3428 param_len = sizeof(struct cd_mode_data);
3430 param_len = sizeof(struct cd_mode_data_10);
3432 /* Don't say we've got more room than we actually allocated */
3433 param_len = min(param_len, data->alloc_len);
3435 scsi_mode_sense_len(csio,
3436 /* retries */ cd_retry_count,
3437 /* cbfcnp */ cddone,
3438 /* tag_action */ MSG_SIMPLE_Q_TAG,
3440 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3442 /* param_buf */ data->mode_buf,
3443 /* param_len */ param_len,
3444 /* minimum_cmd_size */ softc->minimum_command_size,
3445 /* sense_len */ SSD_FULL_SIZE,
3446 /* timeout */ 50000);
3449 * It would be nice not to have to do this, but there's no
3450 * available pointer in the CCB that would allow us to stuff the
3451 * mode params structure in there and retrieve it in
3452 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3453 * lets the caller know what CDB size we ended up using, so they
3454 * can find the actual mode page offset.
3456 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3458 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3459 /*sense_flags*/SF_RETRY_UA);
3461 xpt_release_ccb(ccb);
3463 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3466 * This is a bit of belt-and-suspenders checking, but if we run
3467 * into a situation where the target sends back multiple block
3468 * descriptors, we might not have enough space in the buffer to
3469 * see the whole mode page. Better to return an error than
3470 * potentially access memory beyond our malloced region.
3475 if (data->cdb_size == 10) {
3476 struct scsi_mode_header_10 *hdr10;
3478 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3479 data_len = scsi_2btoul(hdr10->data_length);
3480 data_len += sizeof(hdr10->data_length);
3482 struct scsi_mode_header_6 *hdr6;
3484 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3485 data_len = hdr6->data_length;
3486 data_len += sizeof(hdr6->data_length);
3490 * Complain if there is more mode data available than we
3491 * allocated space for. This could potentially happen if
3492 * we miscalculated the page length for some reason, if the
3493 * drive returns multiple block descriptors, or if it sets
3494 * the data length incorrectly.
3496 if (data_len > data->alloc_len) {
3497 xpt_print(periph->path, "allocated modepage %d length "
3498 "%d < returned length %d\n", page, data->alloc_len,
3507 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3508 * routine. See comments in cd6byteworkaround() for details.
3511 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3513 struct ccb_scsiio *csio;
3514 struct cd_softc *softc;
3516 int cdb_size, param_len;
3519 softc = (struct cd_softc *)periph->softc;
3521 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3528 * If the data is formatted for the 10 byte version of the mode
3529 * select parameter list, we need to use the 10 byte CDB.
3530 * Otherwise, we use whatever the stored minimum command size.
3532 if (data->cdb_size == 10)
3533 cdb_size = data->cdb_size;
3535 cdb_size = softc->minimum_command_size;
3537 if (cdb_size >= 10) {
3538 struct scsi_mode_header_10 *mode_header;
3541 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3543 data_len = scsi_2btoul(mode_header->data_length);
3545 scsi_ulto2b(0, mode_header->data_length);
3547 * SONY drives do not allow a mode select with a medium_type
3548 * value that has just been returned by a mode sense; use a
3549 * medium_type of 0 (Default) instead.
3551 mode_header->medium_type = 0;
3554 * Pass back whatever the drive passed to us, plus the size
3555 * of the data length field.
3557 param_len = data_len + sizeof(mode_header->data_length);
3560 struct scsi_mode_header_6 *mode_header;
3562 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3564 param_len = mode_header->data_length + 1;
3566 mode_header->data_length = 0;
3568 * SONY drives do not allow a mode select with a medium_type
3569 * value that has just been returned by a mode sense; use a
3570 * medium_type of 0 (Default) instead.
3572 mode_header->medium_type = 0;
3575 /* Don't say we've got more room than we actually allocated */
3576 param_len = min(param_len, data->alloc_len);
3578 scsi_mode_select_len(csio,
3579 /* retries */ cd_retry_count,
3580 /* cbfcnp */ cddone,
3581 /* tag_action */ MSG_SIMPLE_Q_TAG,
3582 /* scsi_page_fmt */ 1,
3584 /* param_buf */ data->mode_buf,
3585 /* param_len */ param_len,
3586 /* minimum_cmd_size */ cdb_size,
3587 /* sense_len */ SSD_FULL_SIZE,
3588 /* timeout */ 50000);
3590 /* See comments in cdgetmode() and cd6byteworkaround(). */
3591 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3593 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3594 /*sense_flags*/SF_RETRY_UA);
3596 xpt_release_ccb(ccb);
3598 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3605 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3607 struct ccb_scsiio *csio;
3613 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3616 * Use the smallest possible command to perform the operation.
3618 if ((len & 0xffff0000) == 0) {
3620 * We can fit in a 10 byte cdb.
3622 struct scsi_play_10 *scsi_cmd;
3624 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3625 bzero (scsi_cmd, sizeof(*scsi_cmd));
3626 scsi_cmd->op_code = PLAY_10;
3627 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3628 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3629 cdb_len = sizeof(*scsi_cmd);
3631 struct scsi_play_12 *scsi_cmd;
3633 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3634 bzero (scsi_cmd, sizeof(*scsi_cmd));
3635 scsi_cmd->op_code = PLAY_12;
3636 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3637 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3638 cdb_len = sizeof(*scsi_cmd);
3641 /*retries*/ cd_retry_count,
3643 /*flags*/CAM_DIR_NONE,
3647 /*sense_len*/SSD_FULL_SIZE,
3649 /*timeout*/50 * 1000);
3651 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3652 /*sense_flags*/SF_RETRY_UA);
3654 xpt_release_ccb(ccb);
3660 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3661 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3663 struct scsi_play_msf *scsi_cmd;
3664 struct ccb_scsiio *csio;
3670 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3675 /* retries */ cd_retry_count,
3676 /* cbfcnp */ cddone,
3677 /* flags */ CAM_DIR_NONE,
3678 /* tag_action */ MSG_SIMPLE_Q_TAG,
3679 /* data_ptr */ NULL,
3681 /* sense_len */ SSD_FULL_SIZE,
3682 sizeof(struct scsi_play_msf),
3683 /* timeout */ 50000);
3685 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3686 bzero (scsi_cmd, sizeof(*scsi_cmd));
3688 scsi_cmd->op_code = PLAY_MSF;
3689 scsi_cmd->start_m = startm;
3690 scsi_cmd->start_s = starts;
3691 scsi_cmd->start_f = startf;
3692 scsi_cmd->end_m = endm;
3693 scsi_cmd->end_s = ends;
3694 scsi_cmd->end_f = endf;
3696 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3697 /*sense_flags*/SF_RETRY_UA);
3699 xpt_release_ccb(ccb);
3706 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3707 u_int32_t etrack, u_int32_t eindex)
3709 struct scsi_play_track *scsi_cmd;
3710 struct ccb_scsiio *csio;
3716 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3721 /* retries */ cd_retry_count,
3722 /* cbfcnp */ cddone,
3723 /* flags */ CAM_DIR_NONE,
3724 /* tag_action */ MSG_SIMPLE_Q_TAG,
3725 /* data_ptr */ NULL,
3727 /* sense_len */ SSD_FULL_SIZE,
3728 sizeof(struct scsi_play_track),
3729 /* timeout */ 50000);
3731 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3732 bzero (scsi_cmd, sizeof(*scsi_cmd));
3734 scsi_cmd->op_code = PLAY_TRACK;
3735 scsi_cmd->start_track = strack;
3736 scsi_cmd->start_index = sindex;
3737 scsi_cmd->end_track = etrack;
3738 scsi_cmd->end_index = eindex;
3740 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3741 /*sense_flags*/SF_RETRY_UA);
3743 xpt_release_ccb(ccb);
3749 cdpause(struct cam_periph *periph, u_int32_t go)
3751 struct scsi_pause *scsi_cmd;
3752 struct ccb_scsiio *csio;
3758 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3763 /* retries */ cd_retry_count,
3764 /* cbfcnp */ cddone,
3765 /* flags */ CAM_DIR_NONE,
3766 /* tag_action */ MSG_SIMPLE_Q_TAG,
3767 /* data_ptr */ NULL,
3769 /* sense_len */ SSD_FULL_SIZE,
3770 sizeof(struct scsi_pause),
3771 /* timeout */ 50000);
3773 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3774 bzero (scsi_cmd, sizeof(*scsi_cmd));
3776 scsi_cmd->op_code = PAUSE;
3777 scsi_cmd->resume = go;
3779 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3780 /*sense_flags*/SF_RETRY_UA);
3782 xpt_release_ccb(ccb);
3788 cdstartunit(struct cam_periph *periph, int load)
3795 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3797 scsi_start_stop(&ccb->csio,
3798 /* retries */ cd_retry_count,
3799 /* cbfcnp */ cddone,
3800 /* tag_action */ MSG_SIMPLE_Q_TAG,
3802 /* load_eject */ load,
3803 /* immediate */ FALSE,
3804 /* sense_len */ SSD_FULL_SIZE,
3805 /* timeout */ 50000);
3807 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3808 /*sense_flags*/SF_RETRY_UA);
3810 xpt_release_ccb(ccb);
3816 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3823 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3825 scsi_start_stop(&ccb->csio,
3826 /* retries */ cd_retry_count,
3827 /* cbfcnp */ cddone,
3828 /* tag_action */ MSG_SIMPLE_Q_TAG,
3830 /* load_eject */ eject,
3831 /* immediate */ FALSE,
3832 /* sense_len */ SSD_FULL_SIZE,
3833 /* timeout */ 50000);
3835 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3836 /*sense_flags*/SF_RETRY_UA);
3838 xpt_release_ccb(ccb);
3844 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3846 struct scsi_set_speed *scsi_cmd;
3847 struct ccb_scsiio *csio;
3852 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3855 /* Preserve old behavior: units in multiples of CDROM speed */
3862 /* retries */ cd_retry_count,
3863 /* cbfcnp */ cddone,
3864 /* flags */ CAM_DIR_NONE,
3865 /* tag_action */ MSG_SIMPLE_Q_TAG,
3866 /* data_ptr */ NULL,
3868 /* sense_len */ SSD_FULL_SIZE,
3869 sizeof(struct scsi_set_speed),
3870 /* timeout */ 50000);
3872 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3873 bzero(scsi_cmd, sizeof(*scsi_cmd));
3875 scsi_cmd->opcode = SET_CD_SPEED;
3876 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3877 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3879 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3880 /*sense_flags*/SF_RETRY_UA);
3882 xpt_release_ccb(ccb);
3888 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3900 switch (authinfo->format) {
3901 case DVD_REPORT_AGID:
3902 length = sizeof(struct scsi_report_key_data_agid);
3904 case DVD_REPORT_CHALLENGE:
3905 length = sizeof(struct scsi_report_key_data_challenge);
3907 case DVD_REPORT_KEY1:
3908 length = sizeof(struct scsi_report_key_data_key1_key2);
3910 case DVD_REPORT_TITLE_KEY:
3911 length = sizeof(struct scsi_report_key_data_title);
3912 /* The lba field is only set for the title key */
3913 lba = authinfo->lba;
3915 case DVD_REPORT_ASF:
3916 length = sizeof(struct scsi_report_key_data_asf);
3918 case DVD_REPORT_RPC:
3919 length = sizeof(struct scsi_report_key_data_rpc);
3921 case DVD_INVALIDATE_AGID:
3929 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3933 cam_periph_lock(periph);
3934 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3936 scsi_report_key(&ccb->csio,
3937 /* retries */ cd_retry_count,
3938 /* cbfcnp */ cddone,
3939 /* tag_action */ MSG_SIMPLE_Q_TAG,
3941 /* agid */ authinfo->agid,
3942 /* key_format */ authinfo->format,
3943 /* data_ptr */ databuf,
3944 /* dxfer_len */ length,
3945 /* sense_len */ SSD_FULL_SIZE,
3946 /* timeout */ 50000);
3948 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3949 /*sense_flags*/SF_RETRY_UA);
3954 if (ccb->csio.resid != 0) {
3955 xpt_print(periph->path, "warning, residual for report key "
3956 "command is %d\n", ccb->csio.resid);
3959 switch(authinfo->format) {
3960 case DVD_REPORT_AGID: {
3961 struct scsi_report_key_data_agid *agid_data;
3963 agid_data = (struct scsi_report_key_data_agid *)databuf;
3965 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3969 case DVD_REPORT_CHALLENGE: {
3970 struct scsi_report_key_data_challenge *chal_data;
3972 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3974 bcopy(chal_data->challenge_key, authinfo->keychal,
3975 min(sizeof(chal_data->challenge_key),
3976 sizeof(authinfo->keychal)));
3979 case DVD_REPORT_KEY1: {
3980 struct scsi_report_key_data_key1_key2 *key1_data;
3982 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3984 bcopy(key1_data->key1, authinfo->keychal,
3985 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3988 case DVD_REPORT_TITLE_KEY: {
3989 struct scsi_report_key_data_title *title_data;
3991 title_data = (struct scsi_report_key_data_title *)databuf;
3993 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3994 RKD_TITLE_CPM_SHIFT;
3995 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3996 RKD_TITLE_CP_SEC_SHIFT;
3997 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3998 RKD_TITLE_CMGS_SHIFT;
3999 bcopy(title_data->title_key, authinfo->keychal,
4000 min(sizeof(title_data->title_key),
4001 sizeof(authinfo->keychal)));
4004 case DVD_REPORT_ASF: {
4005 struct scsi_report_key_data_asf *asf_data;
4007 asf_data = (struct scsi_report_key_data_asf *)databuf;
4009 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
4012 case DVD_REPORT_RPC: {
4013 struct scsi_report_key_data_rpc *rpc_data;
4015 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
4017 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
4019 authinfo->vend_rsts =
4020 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
4021 RKD_RPC_VENDOR_RESET_SHIFT;
4022 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4023 authinfo->region = rpc_data->region_mask;
4024 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4027 case DVD_INVALIDATE_AGID:
4030 /* This should be impossible, since we checked above */
4033 break; /* NOTREACHED */
4037 xpt_release_ccb(ccb);
4038 cam_periph_unlock(periph);
4040 if (databuf != NULL)
4041 free(databuf, M_DEVBUF);
4047 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4057 switch(authinfo->format) {
4058 case DVD_SEND_CHALLENGE: {
4059 struct scsi_report_key_data_challenge *challenge_data;
4061 length = sizeof(*challenge_data);
4063 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4065 databuf = (u_int8_t *)challenge_data;
4067 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4068 challenge_data->data_len);
4070 bcopy(authinfo->keychal, challenge_data->challenge_key,
4071 min(sizeof(authinfo->keychal),
4072 sizeof(challenge_data->challenge_key)));
4075 case DVD_SEND_KEY2: {
4076 struct scsi_report_key_data_key1_key2 *key2_data;
4078 length = sizeof(*key2_data);
4080 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4082 databuf = (u_int8_t *)key2_data;
4084 scsi_ulto2b(length - sizeof(key2_data->data_len),
4085 key2_data->data_len);
4087 bcopy(authinfo->keychal, key2_data->key1,
4088 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4092 case DVD_SEND_RPC: {
4093 struct scsi_send_key_data_rpc *rpc_data;
4095 length = sizeof(*rpc_data);
4097 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4099 databuf = (u_int8_t *)rpc_data;
4101 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4102 rpc_data->data_len);
4104 rpc_data->region_code = authinfo->region;
4111 cam_periph_lock(periph);
4112 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4114 scsi_send_key(&ccb->csio,
4115 /* retries */ cd_retry_count,
4116 /* cbfcnp */ cddone,
4117 /* tag_action */ MSG_SIMPLE_Q_TAG,
4118 /* agid */ authinfo->agid,
4119 /* key_format */ authinfo->format,
4120 /* data_ptr */ databuf,
4121 /* dxfer_len */ length,
4122 /* sense_len */ SSD_FULL_SIZE,
4123 /* timeout */ 50000);
4125 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4126 /*sense_flags*/SF_RETRY_UA);
4128 xpt_release_ccb(ccb);
4129 cam_periph_unlock(periph);
4131 if (databuf != NULL)
4132 free(databuf, M_DEVBUF);
4138 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4148 /* The address is reserved for many of the formats */
4151 switch(dvdstruct->format) {
4152 case DVD_STRUCT_PHYSICAL:
4153 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4155 case DVD_STRUCT_COPYRIGHT:
4156 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4158 case DVD_STRUCT_DISCKEY:
4159 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4161 case DVD_STRUCT_BCA:
4162 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4164 case DVD_STRUCT_MANUFACT:
4165 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4167 case DVD_STRUCT_CMI:
4169 case DVD_STRUCT_PROTDISCID:
4170 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4172 case DVD_STRUCT_DISCKEYBLOCK:
4173 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4175 case DVD_STRUCT_DDS:
4176 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4178 case DVD_STRUCT_MEDIUM_STAT:
4179 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4181 case DVD_STRUCT_SPARE_AREA:
4182 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4184 case DVD_STRUCT_RMD_LAST:
4186 case DVD_STRUCT_RMD_RMA:
4188 case DVD_STRUCT_PRERECORDED:
4189 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4191 case DVD_STRUCT_UNIQUEID:
4192 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4194 case DVD_STRUCT_DCB:
4196 case DVD_STRUCT_LIST:
4198 * This is the maximum allocation length for the READ DVD
4199 * STRUCTURE command. There's nothing in the MMC3 spec
4200 * that indicates a limit in the amount of data that can
4201 * be returned from this call, other than the limits
4202 * imposed by the 2-byte length variables.
4211 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4215 cam_periph_lock(periph);
4216 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4218 scsi_read_dvd_structure(&ccb->csio,
4219 /* retries */ cd_retry_count,
4220 /* cbfcnp */ cddone,
4221 /* tag_action */ MSG_SIMPLE_Q_TAG,
4223 /* layer_number */ dvdstruct->layer_num,
4224 /* key_format */ dvdstruct->format,
4225 /* agid */ dvdstruct->agid,
4226 /* data_ptr */ databuf,
4227 /* dxfer_len */ length,
4228 /* sense_len */ SSD_FULL_SIZE,
4229 /* timeout */ 50000);
4231 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4232 /*sense_flags*/SF_RETRY_UA);
4237 switch(dvdstruct->format) {
4238 case DVD_STRUCT_PHYSICAL: {
4239 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4240 struct dvd_layer *outlayer;
4241 struct scsi_read_dvd_struct_data_physical *phys_data;
4244 (struct scsi_read_dvd_struct_data_physical *)databuf;
4245 inlayer = &phys_data->layer_desc;
4246 outlayer = (struct dvd_layer *)&dvdstruct->data;
4248 dvdstruct->length = sizeof(*inlayer);
4250 outlayer->book_type = (inlayer->book_type_version &
4251 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4252 outlayer->book_version = (inlayer->book_type_version &
4253 RDSD_BOOK_VERSION_MASK);
4254 outlayer->disc_size = (inlayer->disc_size_max_rate &
4255 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4256 outlayer->max_rate = (inlayer->disc_size_max_rate &
4257 RDSD_MAX_RATE_MASK);
4258 outlayer->nlayers = (inlayer->layer_info &
4259 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4260 outlayer->track_path = (inlayer->layer_info &
4261 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4262 outlayer->layer_type = (inlayer->layer_info &
4263 RDSD_LAYER_TYPE_MASK);
4264 outlayer->linear_density = (inlayer->density &
4265 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4266 outlayer->track_density = (inlayer->density &
4267 RDSD_TRACK_DENSITY_MASK);
4268 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4270 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4271 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4272 outlayer->end_sector_l0 =
4273 scsi_3btoul(inlayer->end_sector_layer0);
4276 case DVD_STRUCT_COPYRIGHT: {
4277 struct scsi_read_dvd_struct_data_copyright *copy_data;
4279 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4282 dvdstruct->cpst = copy_data->cps_type;
4283 dvdstruct->rmi = copy_data->region_info;
4284 dvdstruct->length = 0;
4290 * Tell the user what the overall length is, no matter
4291 * what we can actually fit in the data buffer.
4293 dvdstruct->length = length - ccb->csio.resid -
4294 sizeof(struct scsi_read_dvd_struct_data_header);
4297 * But only actually copy out the smaller of what we read
4298 * in or what the structure can take.
4300 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4302 min(sizeof(dvdstruct->data), dvdstruct->length));
4307 xpt_release_ccb(ccb);
4308 cam_periph_unlock(periph);
4310 if (databuf != NULL)
4311 free(databuf, M_DEVBUF);
4317 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4318 void (*cbfcnp)(struct cam_periph *, union ccb *),
4319 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4320 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4321 u_int8_t sense_len, u_int32_t timeout)
4323 struct scsi_report_key *scsi_cmd;
4325 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4326 bzero(scsi_cmd, sizeof(*scsi_cmd));
4327 scsi_cmd->opcode = REPORT_KEY;
4328 scsi_ulto4b(lba, scsi_cmd->lba);
4329 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4330 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4331 (key_format & RK_KF_KEYFORMAT_MASK);
4336 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4338 /*data_ptr*/ data_ptr,
4339 /*dxfer_len*/ dxfer_len,
4346 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4347 void (*cbfcnp)(struct cam_periph *, union ccb *),
4348 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4349 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4352 struct scsi_send_key *scsi_cmd;
4354 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4355 bzero(scsi_cmd, sizeof(*scsi_cmd));
4356 scsi_cmd->opcode = SEND_KEY;
4358 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4359 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4360 (key_format & RK_KF_KEYFORMAT_MASK);
4365 /*flags*/ CAM_DIR_OUT,
4367 /*data_ptr*/ data_ptr,
4368 /*dxfer_len*/ dxfer_len,
4376 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4377 void (*cbfcnp)(struct cam_periph *, union ccb *),
4378 u_int8_t tag_action, u_int32_t address,
4379 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4380 u_int8_t *data_ptr, u_int32_t dxfer_len,
4381 u_int8_t sense_len, u_int32_t timeout)
4383 struct scsi_read_dvd_structure *scsi_cmd;
4385 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4386 bzero(scsi_cmd, sizeof(*scsi_cmd));
4387 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4389 scsi_ulto4b(address, scsi_cmd->address);
4390 scsi_cmd->layer_number = layer_number;
4391 scsi_cmd->format = format;
4392 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4393 /* The AGID is the top two bits of this byte */
4394 scsi_cmd->agid = agid << 6;
4399 /*flags*/ CAM_DIR_IN,
4401 /*data_ptr*/ data_ptr,
4402 /*dxfer_len*/ dxfer_len,