2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
45 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/cdrio.h>
62 #include <sys/dvdio.h>
63 #include <sys/devicestat.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
66 #include <geom/geom_disk.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_queue.h>
73 #include <cam/cam_sim.h>
75 #include <cam/scsi/scsi_message.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/scsi/scsi_cd.h>
79 #define LEADOUT 0xaa /* leadout toc entry */
89 CD_Q_BCD_TRACKS = 0x02,
90 CD_Q_NO_CHANGER = 0x04,
92 CD_Q_10_BYTE_ONLY = 0x10
95 #define CD_Q_BIT_STRING \
104 CD_FLAG_INVALID = 0x0001,
105 CD_FLAG_NEW_DISC = 0x0002,
106 CD_FLAG_DISC_LOCKED = 0x0004,
107 CD_FLAG_DISC_REMOVABLE = 0x0008,
108 CD_FLAG_SAW_MEDIA = 0x0010,
109 CD_FLAG_CHANGER = 0x0040,
110 CD_FLAG_ACTIVE = 0x0080,
111 CD_FLAG_SCHED_ON_COMP = 0x0100,
112 CD_FLAG_RETRY_UA = 0x0200,
113 CD_FLAG_VALID_MEDIA = 0x0400,
114 CD_FLAG_VALID_TOC = 0x0800,
115 CD_FLAG_SCTX_INIT = 0x1000
120 CD_CCB_BUFFER_IO = 0x02,
121 CD_CCB_WAITING = 0x03,
123 CD_CCB_TYPE_MASK = 0x0F,
124 CD_CCB_RETRY_UA = 0x10
128 CHANGER_TIMEOUT_SCHED = 0x01,
129 CHANGER_SHORT_TMOUT_SCHED = 0x02,
130 CHANGER_MANUAL_CALL = 0x04,
131 CHANGER_NEED_TIMEOUT = 0x08
134 #define ccb_state ppriv_field0
135 #define ccb_bp ppriv_ptr1
138 struct ioc_toc_header header;
139 struct cd_toc_entry entries[100];
142 struct cd_toc_single {
143 struct ioc_toc_header header;
144 struct cd_toc_entry entry;
155 volatile cd_flags flags;
156 struct bio_queue_head bio_queue;
157 LIST_HEAD(, ccb_hdr) pending_ccbs;
158 struct cd_params params;
161 STAILQ_ENTRY(cd_softc) changer_links;
162 struct cdchanger *changer;
164 struct cam_periph *periph;
165 int minimum_command_size;
166 int outstanding_cmds;
168 struct task sysctl_task;
169 struct sysctl_ctx_list sysctl_ctx;
170 struct sysctl_oid *sysctl_tree;
171 STAILQ_HEAD(, cd_mode_params) mode_queue;
172 struct cd_tocdata toc;
174 struct callout mediapoll_c;
177 struct cd_page_sizes {
182 static struct cd_page_sizes cd_page_size_table[] =
184 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
187 struct cd_quirk_entry {
188 struct scsi_inquiry_pattern inq_pat;
193 * The changer quirk entries aren't strictly necessary. Basically, what
194 * they do is tell cdregister() up front that a device is a changer.
195 * Otherwise, it will figure that fact out once it sees a LUN on the device
196 * that is greater than 0. If it is known up front that a device is a changer,
197 * all I/O to the device will go through the changer scheduling routines, as
198 * opposed to the "normal" CD code.
200 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
201 * your device hangs when it gets a 10 byte command. Adding a quirk just
202 * to get rid of the informative diagnostic message is not acceptable. All
203 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
204 * referenced in a comment along with the quirk) , and must be approved by
205 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
206 * be removed until the submitter can explain why they are needed.
207 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
208 * when the CAM_NEW_TRAN_CODE work is done.
210 static struct cd_quirk_entry cd_quirk_table[] =
213 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
214 /*quirks*/ CD_Q_CHANGER
217 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
218 "*"}, /* quirks */ CD_Q_CHANGER
221 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
222 /* quirks */ CD_Q_CHANGER
225 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
226 /* quirks */ CD_Q_BCD_TRACKS
230 static disk_open_t cdopen;
231 static disk_close_t cdclose;
232 static disk_ioctl_t cdioctl;
233 static disk_strategy_t cdstrategy;
235 static periph_init_t cdinit;
236 static periph_ctor_t cdregister;
237 static periph_dtor_t cdcleanup;
238 static periph_start_t cdstart;
239 static periph_oninv_t cdoninvalidate;
240 static void cdasync(void *callback_arg, u_int32_t code,
241 struct cam_path *path, void *arg);
242 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
243 static void cdshorttimeout(void *arg);
244 static void cdschedule(struct cam_periph *periph, int priority);
245 static void cdrunchangerqueue(void *arg);
246 static void cdchangerschedule(struct cd_softc *softc);
247 static int cdrunccb(union ccb *ccb,
248 int (*error_routine)(union ccb *ccb,
250 u_int32_t sense_flags),
251 u_int32_t cam_flags, u_int32_t sense_flags);
252 static union ccb *cdgetccb(struct cam_periph *periph,
254 static void cddone(struct cam_periph *periph,
255 union ccb *start_ccb);
256 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
257 static int cdgetpagesize(int page_num);
258 static void cdprevent(struct cam_periph *periph, int action);
259 static int cdcheckmedia(struct cam_periph *periph);
260 static int cdsize(struct cam_periph *periph, u_int32_t *size);
261 static int cd6byteworkaround(union ccb *ccb);
262 static int cderror(union ccb *ccb, u_int32_t cam_flags,
263 u_int32_t sense_flags);
264 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
265 u_int32_t start, u_int8_t *data,
266 u_int32_t len, u_int32_t sense_flags);
267 static int cdgetmode(struct cam_periph *periph,
268 struct cd_mode_params *data, u_int32_t page);
269 static int cdsetmode(struct cam_periph *periph,
270 struct cd_mode_params *data);
271 static int cdplay(struct cam_periph *periph, u_int32_t blk,
273 static int cdreadsubchannel(struct cam_periph *periph,
274 u_int32_t mode, u_int32_t format,
276 struct cd_sub_channel_info *data,
278 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
279 u_int32_t starts, u_int32_t startf,
280 u_int32_t endm, u_int32_t ends,
282 static int cdplaytracks(struct cam_periph *periph,
283 u_int32_t strack, u_int32_t sindex,
284 u_int32_t etrack, u_int32_t eindex);
285 static int cdpause(struct cam_periph *periph, u_int32_t go);
286 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
287 static int cdstartunit(struct cam_periph *periph, int load);
288 static int cdsetspeed(struct cam_periph *periph,
289 u_int32_t rdspeed, u_int32_t wrspeed);
290 static int cdreportkey(struct cam_periph *periph,
291 struct dvd_authinfo *authinfo);
292 static int cdsendkey(struct cam_periph *periph,
293 struct dvd_authinfo *authinfo);
294 static int cdreaddvdstructure(struct cam_periph *periph,
295 struct dvd_struct *dvdstruct);
296 static timeout_t cdmediapoll;
298 static struct periph_driver cddriver =
301 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
304 PERIPHDRIVER_DECLARE(cd, cddriver);
306 #ifndef CD_DEFAULT_POLL_PERIOD
307 #define CD_DEFAULT_POLL_PERIOD 3
309 #ifndef CD_DEFAULT_RETRY
310 #define CD_DEFAULT_RETRY 4
312 #ifndef CD_DEFAULT_TIMEOUT
313 #define CD_DEFAULT_TIMEOUT 30000
315 #ifndef CHANGER_MIN_BUSY_SECONDS
316 #define CHANGER_MIN_BUSY_SECONDS 5
318 #ifndef CHANGER_MAX_BUSY_SECONDS
319 #define CHANGER_MAX_BUSY_SECONDS 15
322 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
323 static int cd_retry_count = CD_DEFAULT_RETRY;
324 static int cd_timeout = CD_DEFAULT_TIMEOUT;
325 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
326 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
328 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
329 static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
331 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RW,
332 &cd_poll_period, 0, "Media polling period in seconds");
333 TUNABLE_INT("kern.cam.cd.poll_period", &cd_poll_period);
334 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
335 &cd_retry_count, 0, "Normal I/O retry count");
336 TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
337 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RW,
338 &cd_timeout, 0, "Timeout, in us, for read operations");
339 TUNABLE_INT("kern.cam.cd.timeout", &cd_timeout);
340 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
341 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
342 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
343 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
344 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
345 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
349 target_id_t target_id;
352 struct timeval start_time;
353 struct cd_softc *cur_device;
354 struct callout short_handle;
355 struct callout long_handle;
356 volatile cd_changer_flags flags;
357 STAILQ_ENTRY(cdchanger) changer_links;
358 STAILQ_HEAD(chdevlist, cd_softc) chluns;
361 static struct mtx changerq_mtx;
362 static STAILQ_HEAD(changerlist, cdchanger) changerq;
363 static int num_changers;
365 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
372 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
373 STAILQ_INIT(&changerq);
376 * Install a global async callback. This callback will
377 * receive async callbacks like "new device found".
379 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
381 if (status != CAM_REQ_CMP) {
382 printf("cd: Failed to attach master async callback "
383 "due to status 0x%x!\n", status);
388 * Callback from GEOM, called when it has finished cleaning up its
392 cddiskgonecb(struct disk *dp)
394 struct cam_periph *periph;
396 periph = (struct cam_periph *)dp->d_drv1;
397 cam_periph_release(periph);
401 cdoninvalidate(struct cam_periph *periph)
403 struct cd_softc *softc;
405 softc = (struct cd_softc *)periph->softc;
408 * De-register any async callbacks.
410 xpt_register_async(0, cdasync, periph, periph->path);
412 softc->flags |= CD_FLAG_INVALID;
415 * Return all queued I/O with ENXIO.
416 * XXX Handle any transactions queued to the card
417 * with XPT_ABORT_CCB.
419 bioq_flush(&softc->bio_queue, NULL, ENXIO);
422 * If this device is part of a changer, and it was scheduled
423 * to run, remove it from the run queue since we just nuked
424 * all of its scheduled I/O.
426 if ((softc->flags & CD_FLAG_CHANGER)
427 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
428 camq_remove(&softc->changer->devq, softc->pinfo.index);
430 disk_gone(softc->disk);
434 cdcleanup(struct cam_periph *periph)
436 struct cd_softc *softc;
438 softc = (struct cd_softc *)periph->softc;
441 * In the queued, non-active case, the device in question
442 * has already been removed from the changer run queue. Since this
443 * device is active, we need to de-activate it, and schedule
444 * another device to run. (if there is another one to run)
446 if ((softc->flags & CD_FLAG_CHANGER)
447 && (softc->flags & CD_FLAG_ACTIVE)) {
450 * The purpose of the short timeout is soley to determine
451 * whether the current device has finished or not. Well,
452 * since we're removing the active device, we know that it
453 * is finished. So, get rid of the short timeout.
454 * Otherwise, if we're in the time period before the short
455 * timeout fires, and there are no other devices in the
456 * queue to run, there won't be any other device put in the
457 * active slot. i.e., when we call cdrunchangerqueue()
458 * below, it won't do anything. Then, when the short
459 * timeout fires, it'll look at the "current device", which
460 * we are free below, and possibly panic the kernel on a
461 * bogus pointer reference.
463 * The long timeout doesn't really matter, since we
464 * decrement the qfrozen_cnt to indicate that there is
465 * nothing in the active slot now. Therefore, there won't
466 * be any bogus pointer references there.
468 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
469 callout_stop(&softc->changer->short_handle);
470 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
472 softc->changer->devq.qfrozen_cnt--;
473 softc->changer->flags |= CHANGER_MANUAL_CALL;
474 cdrunchangerqueue(softc->changer);
478 * If we're removing the last device on the changer, go ahead and
479 * remove the changer device structure.
481 if ((softc->flags & CD_FLAG_CHANGER)
482 && (--softc->changer->num_devices == 0)) {
485 * Theoretically, there shouldn't be any timeouts left, but
486 * I'm not completely sure that that will be the case. So,
487 * it won't hurt to check and see if there are any left.
489 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
490 callout_stop(&softc->changer->long_handle);
491 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
494 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
495 callout_stop(&softc->changer->short_handle);
496 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
499 mtx_lock(&changerq_mtx);
500 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
503 mtx_unlock(&changerq_mtx);
504 xpt_print(periph->path, "removing changer entry\n");
505 free(softc->changer, M_DEVBUF);
507 cam_periph_unlock(periph);
508 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
509 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
510 xpt_print(periph->path, "can't remove sysctl context\n");
513 callout_drain(&softc->mediapoll_c);
514 disk_destroy(softc->disk);
515 free(softc, M_DEVBUF);
516 cam_periph_lock(periph);
520 cdasync(void *callback_arg, u_int32_t code,
521 struct cam_path *path, void *arg)
523 struct cam_periph *periph;
524 struct cd_softc *softc;
526 periph = (struct cam_periph *)callback_arg;
528 case AC_FOUND_DEVICE:
530 struct ccb_getdev *cgd;
533 cgd = (struct ccb_getdev *)arg;
537 if (cgd->protocol != PROTO_SCSI)
540 if (SID_TYPE(&cgd->inq_data) != T_CDROM
541 && SID_TYPE(&cgd->inq_data) != T_WORM)
545 * Allocate a peripheral instance for
546 * this device and start the probe
549 status = cam_periph_alloc(cdregister, cdoninvalidate,
551 "cd", CAM_PERIPH_BIO,
552 cgd->ccb_h.path, cdasync,
553 AC_FOUND_DEVICE, cgd);
555 if (status != CAM_REQ_CMP
556 && status != CAM_REQ_INPROG)
557 printf("cdasync: Unable to attach new device "
558 "due to status 0x%x\n", status);
562 case AC_UNIT_ATTENTION:
565 int error_code, sense_key, asc, ascq;
567 softc = (struct cd_softc *)periph->softc;
568 ccb = (union ccb *)arg;
571 * Handle all media change UNIT ATTENTIONs except
572 * our own, as they will be handled by cderror().
574 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
575 scsi_extract_sense_ccb(ccb,
576 &error_code, &sense_key, &asc, &ascq)) {
577 if (asc == 0x28 && ascq == 0x00)
578 disk_media_changed(softc->disk, M_NOWAIT);
580 cam_periph_async(periph, code, path, arg);
584 softc = (struct cd_softc *)periph->softc;
585 if (softc->state == CD_STATE_NORMAL && !softc->tur) {
586 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
588 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
595 struct ccb_hdr *ccbh;
597 softc = (struct cd_softc *)periph->softc;
599 * Don't fail on the expected unit attention
602 softc->flags |= CD_FLAG_RETRY_UA;
603 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
604 ccbh->ccb_state |= CD_CCB_RETRY_UA;
608 cam_periph_async(periph, code, path, arg);
614 cdsysctlinit(void *context, int pending)
616 struct cam_periph *periph;
617 struct cd_softc *softc;
618 char tmpstr[80], tmpstr2[80];
620 periph = (struct cam_periph *)context;
621 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
624 softc = (struct cd_softc *)periph->softc;
625 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
626 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
628 sysctl_ctx_init(&softc->sysctl_ctx);
629 softc->flags |= CD_FLAG_SCTX_INIT;
630 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
631 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
632 tmpstr2, CTLFLAG_RD, 0, tmpstr);
634 if (softc->sysctl_tree == NULL) {
635 printf("cdsysctlinit: unable to allocate sysctl tree\n");
636 cam_periph_release(periph);
641 * Now register the sysctl handler, so the user can the value on
644 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
645 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
646 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
649 cam_periph_release(periph);
653 * We have a handler function for this so we can check the values when the
654 * user sets them, instead of every time we look at them.
657 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
661 value = *(int *)arg1;
663 error = sysctl_handle_int(oidp, &value, 0, req);
666 || (req->newptr == NULL))
670 * The only real values we can have here are 6 or 10. I don't
671 * really forsee having 12 be an option at any time in the future.
672 * So if the user sets something less than or equal to 6, we'll set
673 * it to 6. If he sets something greater than 6, we'll set it to 10.
675 * I suppose we could just return an error here for the wrong values,
676 * but I don't think it's necessary to do so, as long as we can
677 * determine the user's intent without too much trouble.
684 *(int *)arg1 = value;
690 cdregister(struct cam_periph *periph, void *arg)
692 struct cd_softc *softc;
693 struct ccb_pathinq cpi;
694 struct ccb_getdev *cgd;
698 cgd = (struct ccb_getdev *)arg;
700 printf("cdregister: no getdev CCB, can't register device\n");
701 return(CAM_REQ_CMP_ERR);
704 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
707 printf("cdregister: Unable to probe new device. "
708 "Unable to allocate softc\n");
709 return(CAM_REQ_CMP_ERR);
712 LIST_INIT(&softc->pending_ccbs);
713 STAILQ_INIT(&softc->mode_queue);
714 softc->state = CD_STATE_PROBE;
715 bioq_init(&softc->bio_queue);
716 if (SID_IS_REMOVABLE(&cgd->inq_data))
717 softc->flags |= CD_FLAG_DISC_REMOVABLE;
719 periph->softc = softc;
720 softc->periph = periph;
723 * See if this device has any quirks.
725 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
726 (caddr_t)cd_quirk_table,
727 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
728 sizeof(*cd_quirk_table), scsi_inquiry_match);
731 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
733 softc->quirks = CD_Q_NONE;
735 /* Check if the SIM does not want 6 byte commands */
736 bzero(&cpi, sizeof(cpi));
737 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
738 cpi.ccb_h.func_code = XPT_PATH_INQ;
739 xpt_action((union ccb *)&cpi);
740 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
741 softc->quirks |= CD_Q_10_BYTE_ONLY;
743 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
745 /* The default is 6 byte commands, unless quirked otherwise */
746 if (softc->quirks & CD_Q_10_BYTE_ONLY)
747 softc->minimum_command_size = 10;
749 softc->minimum_command_size = 6;
752 * Refcount and block open attempts until we are setup
755 (void)cam_periph_hold(periph, PRIBIO);
756 cam_periph_unlock(periph);
758 * Load the user's default, if any.
760 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
761 periph->unit_number);
762 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
764 /* 6 and 10 are the only permissible values here. */
765 if (softc->minimum_command_size < 6)
766 softc->minimum_command_size = 6;
767 else if (softc->minimum_command_size > 6)
768 softc->minimum_command_size = 10;
771 * We need to register the statistics structure for this device,
772 * but we don't have the blocksize yet for it. So, we register
773 * the structure and indicate that we don't have the blocksize
774 * yet. Unlike other SCSI peripheral drivers, we explicitly set
775 * the device type here to be CDROM, rather than just ORing in
776 * the device type. This is because this driver can attach to either
777 * CDROM or WORM devices, and we want this peripheral driver to
778 * show up in the devstat list as a CD peripheral driver, not a
779 * WORM peripheral driver. WORM drives will also have the WORM
780 * driver attached to them.
782 softc->disk = disk_alloc();
783 softc->disk->d_devstat = devstat_new_entry("cd",
784 periph->unit_number, 0,
785 DEVSTAT_BS_UNAVAILABLE,
787 XPORT_DEVSTAT_TYPE(cpi.transport),
788 DEVSTAT_PRIORITY_CD);
789 softc->disk->d_open = cdopen;
790 softc->disk->d_close = cdclose;
791 softc->disk->d_strategy = cdstrategy;
792 softc->disk->d_gone = cddiskgonecb;
793 softc->disk->d_ioctl = cdioctl;
794 softc->disk->d_name = "cd";
795 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
796 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
797 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
798 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
799 cgd->inq_data.product, sizeof(cgd->inq_data.product),
800 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
801 softc->disk->d_unit = periph->unit_number;
802 softc->disk->d_drv1 = periph;
804 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
805 else if (cpi.maxio > MAXPHYS)
806 softc->disk->d_maxsize = MAXPHYS; /* for safety */
808 softc->disk->d_maxsize = cpi.maxio;
809 softc->disk->d_flags = 0;
810 softc->disk->d_hba_vendor = cpi.hba_vendor;
811 softc->disk->d_hba_device = cpi.hba_device;
812 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
813 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
816 * Acquire a reference to the periph before we register with GEOM.
817 * We'll release this reference once GEOM calls us back (via
818 * dadiskgonecb()) telling us that our provider has been freed.
820 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
821 xpt_print(periph->path, "%s: lost periph during "
822 "registration!\n", __func__);
823 cam_periph_lock(periph);
824 return (CAM_REQ_CMP_ERR);
827 disk_create(softc->disk, DISK_VERSION);
828 cam_periph_lock(periph);
831 * Add an async callback so that we get
832 * notified if this device goes away.
834 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
835 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
838 * If the target lun is greater than 0, we most likely have a CD
839 * changer device. Check the quirk entries as well, though, just
840 * in case someone has a CD tower with one lun per drive or
841 * something like that. Also, if we know up front that a
842 * particular device is a changer, we can mark it as such starting
843 * with lun 0, instead of lun 1. It shouldn't be necessary to have
844 * a quirk entry to define something as a changer, however.
846 if (((cgd->ccb_h.target_lun > 0)
847 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
848 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
849 struct cdchanger *nchanger;
850 struct cam_periph *nperiph;
851 struct cam_path *path;
855 /* Set the changer flag in the current device's softc */
856 softc->flags |= CD_FLAG_CHANGER;
859 * Now, look around for an existing changer device with the
860 * same path and target ID as the current device.
862 mtx_lock(&changerq_mtx);
864 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
866 nchanger = STAILQ_NEXT(nchanger, changer_links)){
867 if ((nchanger->path_id == cgd->ccb_h.path_id)
868 && (nchanger->target_id == cgd->ccb_h.target_id)) {
873 mtx_unlock(&changerq_mtx);
876 * If we found a matching entry, just add this device to
877 * the list of devices on this changer.
880 struct chdevlist *chlunhead;
882 chlunhead = &nchanger->chluns;
885 * XXX KDM look at consolidating this code with the
886 * code below in a separate function.
890 * Create a path with lun id 0, and see if we can
891 * find a matching device
893 status = xpt_create_path(&path, /*periph*/ periph,
895 cgd->ccb_h.target_id, 0);
897 if ((status == CAM_REQ_CMP)
898 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
899 struct cd_softc *nsoftc;
901 nsoftc = (struct cd_softc *)nperiph->softc;
903 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
904 nsoftc->flags |= CD_FLAG_CHANGER;
905 nchanger->num_devices++;
906 if (camq_resize(&nchanger->devq,
907 nchanger->num_devices)!=CAM_REQ_CMP){
908 printf("cdregister: "
914 nsoftc->changer = nchanger;
915 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
917 STAILQ_INSERT_TAIL(&nchanger->chluns,
918 nsoftc,changer_links);
921 } else if (status == CAM_REQ_CMP)
924 printf("cdregister: unable to allocate path\n"
925 "cdregister: changer support may be "
929 nchanger->num_devices++;
931 softc->changer = nchanger;
932 softc->pinfo.index = CAM_UNQUEUED_INDEX;
934 if (camq_resize(&nchanger->devq,
935 nchanger->num_devices) != CAM_REQ_CMP) {
936 printf("cdregister: camq_resize "
937 "failed, changer support may "
941 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
944 * In this case, we don't already have an entry for this
945 * particular changer, so we need to create one, add it to
946 * the queue, and queue this device on the list for this
947 * changer. Before we queue this device, however, we need
948 * to search for lun id 0 on this target, and add it to the
949 * queue first, if it exists. (and if it hasn't already
950 * been marked as part of the changer.)
953 nchanger = malloc(sizeof(struct cdchanger),
954 M_DEVBUF, M_NOWAIT | M_ZERO);
955 if (nchanger == NULL) {
956 softc->flags &= ~CD_FLAG_CHANGER;
957 printf("cdregister: unable to malloc "
958 "changer structure\ncdregister: "
959 "changer support disabled\n");
962 * Yes, gotos can be gross but in this case
963 * I think it's justified..
967 if (camq_init(&nchanger->devq, 1) != 0) {
968 softc->flags &= ~CD_FLAG_CHANGER;
969 printf("cdregister: changer support "
974 nchanger->path_id = cgd->ccb_h.path_id;
975 nchanger->target_id = cgd->ccb_h.target_id;
977 /* this is superfluous, but it makes things clearer */
978 nchanger->num_devices = 0;
980 STAILQ_INIT(&nchanger->chluns);
982 callout_init_mtx(&nchanger->long_handle,
983 periph->sim->mtx, 0);
984 callout_init_mtx(&nchanger->short_handle,
985 periph->sim->mtx, 0);
987 mtx_lock(&changerq_mtx);
989 STAILQ_INSERT_TAIL(&changerq, nchanger,
991 mtx_unlock(&changerq_mtx);
994 * Create a path with lun id 0, and see if we can
995 * find a matching device
997 status = xpt_create_path(&path, /*periph*/ periph,
999 cgd->ccb_h.target_id, 0);
1002 * If we were able to allocate the path, and if we
1003 * find a matching device and it isn't already
1004 * marked as part of a changer, then we add it to
1005 * the current changer.
1007 if ((status == CAM_REQ_CMP)
1008 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
1009 && ((((struct cd_softc *)periph->softc)->flags &
1010 CD_FLAG_CHANGER) == 0)) {
1011 struct cd_softc *nsoftc;
1013 nsoftc = (struct cd_softc *)nperiph->softc;
1015 nsoftc->flags |= CD_FLAG_CHANGER;
1016 nchanger->num_devices++;
1017 if (camq_resize(&nchanger->devq,
1018 nchanger->num_devices) != CAM_REQ_CMP) {
1019 printf("cdregister: camq_resize "
1020 "failed, changer support may "
1023 nsoftc->changer = nchanger;
1024 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
1026 STAILQ_INSERT_TAIL(&nchanger->chluns,
1027 nsoftc, changer_links);
1028 xpt_free_path(path);
1029 } else if (status == CAM_REQ_CMP)
1030 xpt_free_path(path);
1032 printf("cdregister: unable to allocate path\n"
1033 "cdregister: changer support may be "
1037 softc->changer = nchanger;
1038 softc->pinfo.index = CAM_UNQUEUED_INDEX;
1039 nchanger->num_devices++;
1040 if (camq_resize(&nchanger->devq,
1041 nchanger->num_devices) != CAM_REQ_CMP) {
1042 printf("cdregister: camq_resize "
1043 "failed, changer support may "
1046 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
1052 * Schedule a periodic media polling events.
1054 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1055 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
1056 (softc->flags & CD_FLAG_CHANGER) == 0 &&
1057 (cgd->inq_flags & SID_AEN) == 0 &&
1058 cd_poll_period != 0)
1059 callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
1060 cdmediapoll, periph);
1064 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1065 xpt_schedule(periph, CAM_PRIORITY_DEV);
1067 cdschedule(periph, CAM_PRIORITY_DEV);
1069 return(CAM_REQ_CMP);
1073 cdopen(struct disk *dp)
1075 struct cam_periph *periph;
1076 struct cd_softc *softc;
1079 periph = (struct cam_periph *)dp->d_drv1;
1080 softc = (struct cd_softc *)periph->softc;
1082 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1085 cam_periph_lock(periph);
1087 if (softc->flags & CD_FLAG_INVALID) {
1088 cam_periph_release_locked(periph);
1089 cam_periph_unlock(periph);
1093 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1094 cam_periph_release_locked(periph);
1095 cam_periph_unlock(periph);
1099 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1103 * Check for media, and set the appropriate flags. We don't bail
1104 * if we don't have media, but then we don't allow anything but the
1105 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1107 cdcheckmedia(periph);
1109 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1110 cam_periph_unhold(periph);
1112 cam_periph_unlock(periph);
1118 cdclose(struct disk *dp)
1120 struct cam_periph *periph;
1121 struct cd_softc *softc;
1123 periph = (struct cam_periph *)dp->d_drv1;
1124 softc = (struct cd_softc *)periph->softc;
1126 cam_periph_lock(periph);
1127 if (cam_periph_hold(periph, PRIBIO) != 0) {
1128 cam_periph_unlock(periph);
1129 cam_periph_release(periph);
1133 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1136 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1137 cdprevent(periph, PR_ALLOW);
1140 * Since we're closing this CD, mark the blocksize as unavailable.
1141 * It will be marked as available when the CD is opened again.
1143 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1146 * We'll check the media and toc again at the next open().
1148 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1150 cam_periph_unhold(periph);
1151 cam_periph_release_locked(periph);
1152 cam_periph_unlock(periph);
1158 cdshorttimeout(void *arg)
1160 struct cdchanger *changer;
1162 changer = (struct cdchanger *)arg;
1164 /* Always clear the short timeout flag, since that's what we're in */
1165 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1168 * Check to see if there is any more pending or outstanding I/O for
1169 * this device. If not, move it out of the active slot.
1171 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1172 && (changer->cur_device->outstanding_cmds == 0)) {
1173 changer->flags |= CHANGER_MANUAL_CALL;
1174 cdrunchangerqueue(changer);
1179 * This is a wrapper for xpt_schedule. It only applies to changers.
1182 cdschedule(struct cam_periph *periph, int priority)
1184 struct cd_softc *softc;
1186 softc = (struct cd_softc *)periph->softc;
1189 * If this device isn't currently queued, and if it isn't
1190 * the active device, then we queue this device and run the
1191 * changer queue if there is no timeout scheduled to do it.
1192 * If this device is the active device, just schedule it
1193 * to run again. If this device is queued, there should be
1194 * a timeout in place already that will make sure it runs.
1196 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1197 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1199 * We don't do anything with the priority here.
1200 * This is strictly a fifo queue.
1202 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1203 softc->pinfo.generation = ++softc->changer->devq.generation;
1204 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1207 * Since we just put a device in the changer queue,
1208 * check and see if there is a timeout scheduled for
1209 * this changer. If so, let the timeout handle
1210 * switching this device into the active slot. If
1211 * not, manually call the timeout routine to
1214 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1215 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1216 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1217 softc->changer->flags |= CHANGER_MANUAL_CALL;
1218 cdrunchangerqueue(softc->changer);
1220 } else if ((softc->flags & CD_FLAG_ACTIVE)
1221 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1222 xpt_schedule(periph, priority);
1226 cdrunchangerqueue(void *arg)
1228 struct cd_softc *softc;
1229 struct cdchanger *changer;
1230 int called_from_timeout;
1232 changer = (struct cdchanger *)arg;
1235 * If we have NOT been called from cdstrategy() or cddone(), and
1236 * instead from a timeout routine, go ahead and clear the
1239 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1240 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1241 called_from_timeout = 1;
1243 called_from_timeout = 0;
1245 /* Always clear the manual call flag */
1246 changer->flags &= ~CHANGER_MANUAL_CALL;
1248 /* nothing to do if the queue is empty */
1249 if (changer->devq.entries <= 0) {
1254 * If the changer queue is frozen, that means we have an active
1257 if (changer->devq.qfrozen_cnt > 0) {
1260 * We always need to reset the frozen count and clear the
1263 changer->devq.qfrozen_cnt--;
1264 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1265 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1267 if (changer->cur_device->outstanding_cmds > 0) {
1268 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1269 changer->cur_device->bufs_left =
1270 changer->cur_device->outstanding_cmds;
1271 if (called_from_timeout) {
1272 callout_reset(&changer->long_handle,
1273 changer_max_busy_seconds * hz,
1274 cdrunchangerqueue, changer);
1275 changer->flags |= CHANGER_TIMEOUT_SCHED;
1281 * Check to see whether the current device has any I/O left
1282 * to do. If so, requeue it at the end of the queue. If
1283 * not, there is no need to requeue it.
1285 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1287 changer->cur_device->pinfo.generation =
1288 ++changer->devq.generation;
1289 camq_insert(&changer->devq,
1290 (cam_pinfo *)changer->cur_device);
1294 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1296 changer->cur_device = softc;
1298 changer->devq.qfrozen_cnt++;
1299 softc->flags |= CD_FLAG_ACTIVE;
1301 /* Just in case this device is waiting */
1302 wakeup(&softc->changer);
1303 xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1306 * Get rid of any pending timeouts, and set a flag to schedule new
1307 * ones so this device gets its full time quantum.
1309 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1310 callout_stop(&changer->long_handle);
1311 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1314 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1315 callout_stop(&changer->short_handle);
1316 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1320 * We need to schedule timeouts, but we only do this after the
1321 * first transaction has completed. This eliminates the changer
1324 changer->flags |= CHANGER_NEED_TIMEOUT;
1328 cdchangerschedule(struct cd_softc *softc)
1330 struct cdchanger *changer;
1332 changer = softc->changer;
1335 * If this is a changer, and this is the current device,
1336 * and this device has at least the minimum time quantum to
1337 * run, see if we can switch it out.
1339 if ((softc->flags & CD_FLAG_ACTIVE)
1340 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1341 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1343 * We try three things here. The first is that we
1344 * check to see whether the schedule on completion
1345 * flag is set. If it is, we decrement the number
1346 * of buffers left, and if it's zero, we reschedule.
1347 * Next, we check to see whether the pending buffer
1348 * queue is empty and whether there are no
1349 * outstanding transactions. If so, we reschedule.
1350 * Next, we see if the pending buffer queue is empty.
1351 * If it is, we set the number of buffers left to
1352 * the current active buffer count and set the
1353 * schedule on complete flag.
1355 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1356 if (--softc->bufs_left == 0) {
1357 softc->changer->flags |=
1358 CHANGER_MANUAL_CALL;
1359 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1360 cdrunchangerqueue(softc->changer);
1362 } else if ((bioq_first(&softc->bio_queue) == NULL)
1363 && (softc->outstanding_cmds == 0)) {
1364 softc->changer->flags |= CHANGER_MANUAL_CALL;
1365 cdrunchangerqueue(softc->changer);
1367 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1368 && (softc->flags & CD_FLAG_ACTIVE)) {
1371 * Now that the first transaction to this
1372 * particular device has completed, we can go ahead
1373 * and schedule our timeouts.
1375 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1376 callout_reset(&changer->long_handle,
1377 changer_max_busy_seconds * hz,
1378 cdrunchangerqueue, changer);
1379 changer->flags |= CHANGER_TIMEOUT_SCHED;
1381 printf("cdchangerschedule: already have a long"
1384 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1385 callout_reset(&changer->short_handle,
1386 changer_min_busy_seconds * hz,
1387 cdshorttimeout, changer);
1388 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1390 printf("cdchangerschedule: already have a short "
1394 * We just scheduled timeouts, no need to schedule
1397 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1403 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1404 u_int32_t cam_flags,
1405 u_int32_t sense_flags),
1406 u_int32_t cam_flags, u_int32_t sense_flags)
1408 struct cd_softc *softc;
1409 struct cam_periph *periph;
1412 periph = xpt_path_periph(ccb->ccb_h.path);
1413 softc = (struct cd_softc *)periph->softc;
1415 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1416 softc->disk->d_devstat);
1418 if (softc->flags & CD_FLAG_CHANGER)
1419 cdchangerschedule(softc);
1425 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1427 struct cd_softc *softc;
1429 softc = (struct cd_softc *)periph->softc;
1431 if (softc->flags & CD_FLAG_CHANGER) {
1433 * This should work the first time this device is woken up,
1434 * but just in case it doesn't, we use a while loop.
1436 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1438 * If this changer isn't already queued, queue it up.
1440 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1441 softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1442 softc->pinfo.generation =
1443 ++softc->changer->devq.generation;
1444 camq_insert(&softc->changer->devq,
1445 (cam_pinfo *)softc);
1447 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1448 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1449 && ((softc->changer->flags
1450 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1451 softc->changer->flags |= CHANGER_MANUAL_CALL;
1452 cdrunchangerqueue(softc->changer);
1454 cam_periph_sleep(periph, &softc->changer,
1455 PRIBIO, "cgticb", 0);
1458 return(cam_periph_getccb(periph, priority));
1463 * Actually translate the requested transfer into one the physical driver
1464 * can understand. The transfer is described by a buf and will include
1465 * only one physical transfer.
1468 cdstrategy(struct bio *bp)
1470 struct cam_periph *periph;
1471 struct cd_softc *softc;
1473 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1474 cam_periph_lock(periph);
1475 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1476 ("cdstrategy(%p)\n", bp));
1478 softc = (struct cd_softc *)periph->softc;
1481 * If the device has been made invalid, error out
1483 if ((softc->flags & CD_FLAG_INVALID)) {
1484 cam_periph_unlock(periph);
1485 biofinish(bp, NULL, ENXIO);
1490 * If we don't have valid media, look for it before trying to
1493 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1496 error = cdcheckmedia(periph);
1498 cam_periph_unlock(periph);
1499 biofinish(bp, NULL, error);
1505 * Place it in the queue of disk activities for this disk
1507 bioq_disksort(&softc->bio_queue, bp);
1510 * Schedule ourselves for performing the work. We do things
1511 * differently for changers.
1513 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1514 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1516 cdschedule(periph, CAM_PRIORITY_NORMAL);
1518 cam_periph_unlock(periph);
1523 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1525 struct cd_softc *softc;
1527 struct ccb_scsiio *csio;
1528 struct scsi_read_capacity_data *rcap;
1530 softc = (struct cd_softc *)periph->softc;
1532 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1534 switch (softc->state) {
1535 case CD_STATE_NORMAL:
1537 bp = bioq_first(&softc->bio_queue);
1538 if (periph->immediate_priority <= periph->pinfo.priority) {
1539 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1541 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1543 periph->immediate_priority = CAM_PRIORITY_NONE;
1544 wakeup(&periph->ccb_list);
1545 } else if (bp == NULL) {
1548 csio = &start_ccb->csio;
1549 scsi_test_unit_ready(csio,
1550 /*retries*/ cd_retry_count,
1555 start_ccb->ccb_h.ccb_bp = NULL;
1556 start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
1557 xpt_action(start_ccb);
1559 xpt_release_ccb(start_ccb);
1563 cam_periph_release_locked(periph);
1565 bioq_remove(&softc->bio_queue, bp);
1567 scsi_read_write(&start_ccb->csio,
1568 /*retries*/ cd_retry_count,
1569 /* cbfcnp */ cddone,
1571 /* read */bp->bio_cmd == BIO_READ ?
1572 SCSI_RW_READ : SCSI_RW_WRITE,
1574 /* minimum_cmd_size */ 10,
1575 /* lba */ bp->bio_offset /
1576 softc->params.blksize,
1577 bp->bio_bcount / softc->params.blksize,
1578 /* data_ptr */ bp->bio_data,
1579 /* dxfer_len */ bp->bio_bcount,
1580 /* sense_len */ cd_retry_count ?
1581 SSD_FULL_SIZE : SF_NO_PRINT,
1582 /* timeout */ cd_timeout);
1583 /* Use READ CD command for audio tracks. */
1584 if (softc->params.blksize == 2352) {
1585 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1586 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1587 start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1588 start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1589 start_ccb->csio.cdb_len = 12;
1591 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1594 LIST_INSERT_HEAD(&softc->pending_ccbs,
1595 &start_ccb->ccb_h, periph_links.le);
1596 softc->outstanding_cmds++;
1598 /* We expect a unit attention from this device */
1599 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1600 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1601 softc->flags &= ~CD_FLAG_RETRY_UA;
1604 start_ccb->ccb_h.ccb_bp = bp;
1605 bp = bioq_first(&softc->bio_queue);
1607 xpt_action(start_ccb);
1609 if (bp != NULL || softc->tur ||
1610 periph->immediate_priority != CAM_PRIORITY_NONE) {
1611 /* Have more work to do, so ensure we stay scheduled */
1612 xpt_schedule(periph, min(CAM_PRIORITY_NORMAL,
1613 periph->immediate_priority));
1617 case CD_STATE_PROBE:
1620 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1621 M_SCSICD, M_NOWAIT | M_ZERO);
1623 xpt_print(periph->path,
1624 "cdstart: Couldn't malloc read_capacity data\n");
1625 /* cd_free_periph??? */
1628 csio = &start_ccb->csio;
1629 scsi_read_capacity(csio,
1630 /*retries*/ cd_retry_count,
1636 start_ccb->ccb_h.ccb_bp = NULL;
1637 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1638 xpt_action(start_ccb);
1645 cddone(struct cam_periph *periph, union ccb *done_ccb)
1647 struct cd_softc *softc;
1648 struct ccb_scsiio *csio;
1650 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1652 softc = (struct cd_softc *)periph->softc;
1653 csio = &done_ccb->csio;
1655 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1656 case CD_CCB_BUFFER_IO:
1661 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1664 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1667 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1672 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1673 if (error == ERESTART) {
1675 * A retry was scheuled, so
1683 xpt_print(periph->path,
1684 "cddone: got error %#x back\n", error);
1685 bioq_flush(&softc->bio_queue, NULL, EIO);
1686 bp->bio_resid = bp->bio_bcount;
1687 bp->bio_error = error;
1688 bp->bio_flags |= BIO_ERROR;
1689 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1690 cam_release_devq(done_ccb->ccb_h.path,
1694 /*getcount_only*/0);
1697 bp->bio_resid = csio->resid;
1699 if (bp->bio_resid != 0) {
1701 * Short transfer ???
1702 * XXX: not sure this is correct for partial
1705 bp->bio_flags |= BIO_ERROR;
1709 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1710 softc->outstanding_cmds--;
1712 if (softc->flags & CD_FLAG_CHANGER)
1713 cdchangerschedule(softc);
1715 biofinish(bp, NULL, 0);
1720 struct scsi_read_capacity_data *rdcap;
1721 char announce_buf[120]; /*
1722 * Currently (9/30/97) the
1723 * longest possible announce
1724 * buffer is 108 bytes, for the
1725 * first error case below.
1726 * That is 39 bytes for the
1727 * basic string, 16 bytes for the
1728 * biggest sense key (hardware
1729 * error), 52 bytes for the
1730 * text of the largest sense
1731 * qualifier valid for a CDROM,
1732 * (0x72, 0x03 or 0x04,
1733 * 0x03), and one byte for the
1734 * null terminating character.
1735 * To allow for longer strings,
1736 * the announce buffer is 120
1739 struct cd_params *cdp;
1742 cdp = &softc->params;
1744 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1746 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1747 cdp->blksize = scsi_4btoul (rdcap->length);
1750 * Retry any UNIT ATTENTION type errors. They
1751 * are expected at boot.
1753 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1754 (error = cderror(done_ccb, CAM_RETRY_SELTO,
1755 SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1757 snprintf(announce_buf, sizeof(announce_buf),
1758 "cd present [%lu x %lu byte records]",
1759 cdp->disksize, (u_long)cdp->blksize);
1762 if (error == ERESTART) {
1764 * A retry was scheuled, so
1770 int sense_key, error_code;
1773 struct ccb_getdev cgd;
1775 /* Don't wedge this device's queue */
1776 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1777 cam_release_devq(done_ccb->ccb_h.path,
1781 /*getcount_only*/0);
1783 status = done_ccb->ccb_h.status;
1785 xpt_setup_ccb(&cgd.ccb_h,
1786 done_ccb->ccb_h.path,
1787 CAM_PRIORITY_NORMAL);
1788 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1789 xpt_action((union ccb *)&cgd);
1791 if (scsi_extract_sense_ccb(done_ccb,
1792 &error_code, &sense_key, &asc, &ascq))
1798 * Attach to anything that claims to be a
1799 * CDROM or WORM device, as long as it
1800 * doesn't return a "Logical unit not
1801 * supported" (0x25) error.
1803 if ((have_sense) && (asc != 0x25)
1804 && (error_code == SSD_CURRENT_ERROR)) {
1805 const char *sense_key_desc;
1806 const char *asc_desc;
1808 scsi_sense_desc(sense_key, asc, ascq,
1812 snprintf(announce_buf,
1813 sizeof(announce_buf),
1814 "Attempt to query device "
1815 "size failed: %s, %s",
1818 } else if ((have_sense == 0)
1819 && ((status & CAM_STATUS_MASK) ==
1820 CAM_SCSI_STATUS_ERROR)
1821 && (csio->scsi_status ==
1822 SCSI_STATUS_BUSY)) {
1823 snprintf(announce_buf,
1824 sizeof(announce_buf),
1825 "Attempt to query device "
1826 "size failed: SCSI Status: %s",
1827 scsi_status_string(csio));
1828 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1830 * We only print out an error for
1831 * CDROM type devices. For WORM
1832 * devices, we don't print out an
1833 * error since a few WORM devices
1834 * don't support CDROM commands.
1835 * If we have sense information, go
1836 * ahead and print it out.
1837 * Otherwise, just say that we
1842 * Just print out the error, not
1843 * the full probe message, when we
1850 xpt_print(periph->path,
1851 "got CAM status %#x\n",
1852 done_ccb->ccb_h.status);
1854 xpt_print(periph->path, "fatal error, "
1855 "failed to attach to device\n");
1857 * Invalidate this peripheral.
1859 cam_periph_invalidate(periph);
1861 announce_buf[0] = '\0';
1865 * Invalidate this peripheral.
1867 cam_periph_invalidate(periph);
1868 announce_buf[0] = '\0';
1872 free(rdcap, M_SCSICD);
1873 if (announce_buf[0] != '\0') {
1874 xpt_announce_periph(periph, announce_buf);
1875 xpt_announce_quirks(periph, softc->quirks,
1877 if (softc->flags & CD_FLAG_CHANGER)
1878 cdchangerschedule(softc);
1880 * Create our sysctl variables, now that we know
1881 * we have successfully attached.
1883 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1885 softc->state = CD_STATE_NORMAL;
1887 * Since our peripheral may be invalidated by an error
1888 * above or an external event, we must release our CCB
1889 * before releasing the probe lock on the peripheral.
1890 * The peripheral will only go away once the last lock
1891 * is removed, and we need it around for the CCB release
1894 xpt_release_ccb(done_ccb);
1895 cam_periph_unhold(periph);
1898 case CD_CCB_WAITING:
1900 /* Caller will release the CCB */
1901 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1902 ("trying to wakeup ccbwait\n"));
1904 wakeup(&done_ccb->ccb_h.cbfcnp);
1909 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1911 if (cderror(done_ccb, CAM_RETRY_SELTO,
1912 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1915 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1916 cam_release_devq(done_ccb->ccb_h.path,
1920 /*getcount_only*/0);
1922 xpt_release_ccb(done_ccb);
1923 cam_periph_release_locked(periph);
1929 xpt_release_ccb(done_ccb);
1932 static union cd_pages *
1933 cdgetpage(struct cd_mode_params *mode_params)
1935 union cd_pages *page;
1937 if (mode_params->cdb_size == 10)
1938 page = (union cd_pages *)find_mode_page_10(
1939 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1941 page = (union cd_pages *)find_mode_page_6(
1942 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1948 cdgetpagesize(int page_num)
1952 for (i = 0; i < (sizeof(cd_page_size_table)/
1953 sizeof(cd_page_size_table[0])); i++) {
1954 if (cd_page_size_table[i].page == page_num)
1955 return (cd_page_size_table[i].page_size);
1962 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1965 struct cam_periph *periph;
1966 struct cd_softc *softc;
1967 int nocopyout, error = 0;
1969 periph = (struct cam_periph *)dp->d_drv1;
1970 cam_periph_lock(periph);
1972 softc = (struct cd_softc *)periph->softc;
1974 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1975 ("cdioctl(%#lx)\n", cmd));
1977 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1978 cam_periph_unlock(periph);
1979 cam_periph_release(periph);
1984 * If we don't have media loaded, check for it. If still don't
1985 * have media loaded, we can only do a load or eject.
1987 * We only care whether media is loaded if this is a cd-specific ioctl
1988 * (thus the IOCGROUP check below). Note that this will break if
1989 * anyone adds any ioctls into the switch statement below that don't
1990 * have their ioctl group set to 'c'.
1992 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1993 && ((cmd != CDIOCCLOSE)
1994 && (cmd != CDIOCEJECT))
1995 && (IOCGROUP(cmd) == 'c')) {
1996 error = cdcheckmedia(periph);
1998 cam_periph_unhold(periph);
1999 cam_periph_unlock(periph);
2004 * Drop the lock here so later mallocs can use WAITOK. The periph
2005 * is essentially locked still with the cam_periph_hold call above.
2007 cam_periph_unlock(periph);
2012 case CDIOCPLAYTRACKS:
2014 struct ioc_play_track *args
2015 = (struct ioc_play_track *) addr;
2016 struct cd_mode_params params;
2017 union cd_pages *page;
2019 params.alloc_len = sizeof(union cd_mode_data_6_10);
2020 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2023 cam_periph_lock(periph);
2024 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2025 ("trying to do CDIOCPLAYTRACKS\n"));
2027 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2029 free(params.mode_buf, M_SCSICD);
2030 cam_periph_unlock(periph);
2033 page = cdgetpage(¶ms);
2035 page->audio.flags &= ~CD_PA_SOTC;
2036 page->audio.flags |= CD_PA_IMMED;
2037 error = cdsetmode(periph, ¶ms);
2038 free(params.mode_buf, M_SCSICD);
2040 cam_periph_unlock(periph);
2045 * This was originally implemented with the PLAY
2046 * AUDIO TRACK INDEX command, but that command was
2047 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2048 * drives support it but ATAPI and ATAPI-derivative
2049 * drives don't seem to support it. So we keep a
2050 * cache of the table of contents and translate
2051 * track numbers to MSF format.
2053 if (softc->flags & CD_FLAG_VALID_TOC) {
2054 union msf_lba *sentry, *eentry;
2057 if (args->end_track <
2058 softc->toc.header.ending_track + 1)
2060 if (args->end_track >
2061 softc->toc.header.ending_track + 1)
2063 softc->toc.header.ending_track + 1;
2064 st = args->start_track -
2065 softc->toc.header.starting_track;
2066 et = args->end_track -
2067 softc->toc.header.starting_track;
2070 || (st > (softc->toc.header.ending_track -
2071 softc->toc.header.starting_track))) {
2073 cam_periph_unlock(periph);
2076 sentry = &softc->toc.entries[st].addr;
2077 eentry = &softc->toc.entries[et].addr;
2078 error = cdplaymsf(periph,
2087 * If we don't have a valid TOC, try the
2088 * play track index command. It is part of
2089 * the SCSI-2 spec, but was removed in the
2090 * MMC specs. ATAPI and ATAPI-derived
2091 * drives don't support it.
2093 if (softc->quirks & CD_Q_BCD_TRACKS) {
2095 bin2bcd(args->start_track);
2097 bin2bcd(args->end_track);
2099 error = cdplaytracks(periph,
2105 cam_periph_unlock(periph);
2110 struct ioc_play_msf *args
2111 = (struct ioc_play_msf *) addr;
2112 struct cd_mode_params params;
2113 union cd_pages *page;
2115 params.alloc_len = sizeof(union cd_mode_data_6_10);
2116 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2119 cam_periph_lock(periph);
2120 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2121 ("trying to do CDIOCPLAYMSF\n"));
2123 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2125 free(params.mode_buf, M_SCSICD);
2126 cam_periph_unlock(periph);
2129 page = cdgetpage(¶ms);
2131 page->audio.flags &= ~CD_PA_SOTC;
2132 page->audio.flags |= CD_PA_IMMED;
2133 error = cdsetmode(periph, ¶ms);
2134 free(params.mode_buf, M_SCSICD);
2136 cam_periph_unlock(periph);
2139 error = cdplaymsf(periph,
2146 cam_periph_unlock(periph);
2149 case CDIOCPLAYBLOCKS:
2151 struct ioc_play_blocks *args
2152 = (struct ioc_play_blocks *) addr;
2153 struct cd_mode_params params;
2154 union cd_pages *page;
2156 params.alloc_len = sizeof(union cd_mode_data_6_10);
2157 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2160 cam_periph_lock(periph);
2161 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2162 ("trying to do CDIOCPLAYBLOCKS\n"));
2165 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2167 free(params.mode_buf, M_SCSICD);
2168 cam_periph_unlock(periph);
2171 page = cdgetpage(¶ms);
2173 page->audio.flags &= ~CD_PA_SOTC;
2174 page->audio.flags |= CD_PA_IMMED;
2175 error = cdsetmode(periph, ¶ms);
2176 free(params.mode_buf, M_SCSICD);
2178 cam_periph_unlock(periph);
2181 error = cdplay(periph, args->blk, args->len);
2182 cam_periph_unlock(periph);
2185 case CDIOCREADSUBCHANNEL_SYSSPACE:
2188 case CDIOCREADSUBCHANNEL:
2190 struct ioc_read_subchannel *args
2191 = (struct ioc_read_subchannel *) addr;
2192 struct cd_sub_channel_info *data;
2193 u_int32_t len = args->data_len;
2195 data = malloc(sizeof(struct cd_sub_channel_info),
2196 M_SCSICD, M_WAITOK | M_ZERO);
2198 cam_periph_lock(periph);
2199 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2200 ("trying to do CDIOCREADSUBCHANNEL\n"));
2202 if ((len > sizeof(struct cd_sub_channel_info)) ||
2203 (len < sizeof(struct cd_sub_channel_header))) {
2205 "scsi_cd: cdioctl: "
2206 "cdioreadsubchannel: error, len=%d\n",
2209 free(data, M_SCSICD);
2210 cam_periph_unlock(periph);
2214 if (softc->quirks & CD_Q_BCD_TRACKS)
2215 args->track = bin2bcd(args->track);
2217 error = cdreadsubchannel(periph, args->address_format,
2218 args->data_format, args->track, data, len);
2221 free(data, M_SCSICD);
2222 cam_periph_unlock(periph);
2225 if (softc->quirks & CD_Q_BCD_TRACKS)
2226 data->what.track_info.track_number =
2227 bcd2bin(data->what.track_info.track_number);
2228 len = min(len, ((data->header.data_len[0] << 8) +
2229 data->header.data_len[1] +
2230 sizeof(struct cd_sub_channel_header)));
2231 cam_periph_unlock(periph);
2232 if (nocopyout == 0) {
2233 if (copyout(data, args->data, len) != 0) {
2237 bcopy(data, args->data, len);
2239 free(data, M_SCSICD);
2243 case CDIOREADTOCHEADER:
2245 struct ioc_toc_header *th;
2247 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2250 cam_periph_lock(periph);
2251 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2252 ("trying to do CDIOREADTOCHEADER\n"));
2254 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2255 sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2258 cam_periph_unlock(periph);
2261 if (softc->quirks & CD_Q_BCD_TRACKS) {
2262 /* we are going to have to convert the BCD
2263 * encoding on the cd to what is expected
2265 th->starting_track =
2266 bcd2bin(th->starting_track);
2267 th->ending_track = bcd2bin(th->ending_track);
2269 th->len = ntohs(th->len);
2270 bcopy(th, addr, sizeof(*th));
2272 cam_periph_unlock(periph);
2275 case CDIOREADTOCENTRYS:
2277 struct cd_tocdata *data;
2278 struct cd_toc_single *lead;
2279 struct ioc_read_toc_entry *te =
2280 (struct ioc_read_toc_entry *) addr;
2281 struct ioc_toc_header *th;
2282 u_int32_t len, readlen, idx, num;
2283 u_int32_t starting_track = te->starting_track;
2285 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2286 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2288 cam_periph_lock(periph);
2289 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2290 ("trying to do CDIOREADTOCENTRYS\n"));
2292 if (te->data_len < sizeof(struct cd_toc_entry)
2293 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2294 || (te->address_format != CD_MSF_FORMAT
2295 && te->address_format != CD_LBA_FORMAT)) {
2297 printf("scsi_cd: error in readtocentries, "
2298 "returning EINVAL\n");
2299 free(data, M_SCSICD);
2300 free(lead, M_SCSICD);
2301 cam_periph_unlock(periph);
2306 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2307 sizeof (*th), /*sense_flags*/0);
2309 free(data, M_SCSICD);
2310 free(lead, M_SCSICD);
2311 cam_periph_unlock(periph);
2315 if (softc->quirks & CD_Q_BCD_TRACKS) {
2316 /* we are going to have to convert the BCD
2317 * encoding on the cd to what is expected
2319 th->starting_track =
2320 bcd2bin(th->starting_track);
2321 th->ending_track = bcd2bin(th->ending_track);
2324 if (starting_track == 0)
2325 starting_track = th->starting_track;
2326 else if (starting_track == LEADOUT)
2327 starting_track = th->ending_track + 1;
2328 else if (starting_track < th->starting_track ||
2329 starting_track > th->ending_track + 1) {
2330 printf("scsi_cd: error in readtocentries, "
2331 "returning EINVAL\n");
2332 free(data, M_SCSICD);
2333 free(lead, M_SCSICD);
2334 cam_periph_unlock(periph);
2339 /* calculate reading length without leadout entry */
2340 readlen = (th->ending_track - starting_track + 1) *
2341 sizeof(struct cd_toc_entry);
2343 /* and with leadout entry */
2344 len = readlen + sizeof(struct cd_toc_entry);
2345 if (te->data_len < len) {
2350 if (len > sizeof(data->entries)) {
2351 printf("scsi_cd: error in readtocentries, "
2352 "returning EINVAL\n");
2354 free(data, M_SCSICD);
2355 free(lead, M_SCSICD);
2356 cam_periph_unlock(periph);
2359 num = len / sizeof(struct cd_toc_entry);
2362 error = cdreadtoc(periph, te->address_format,
2365 readlen + sizeof (*th),
2368 free(data, M_SCSICD);
2369 free(lead, M_SCSICD);
2370 cam_periph_unlock(periph);
2375 /* make leadout entry if needed */
2376 idx = starting_track + num - 1;
2377 if (softc->quirks & CD_Q_BCD_TRACKS)
2378 th->ending_track = bcd2bin(th->ending_track);
2379 if (idx == th->ending_track + 1) {
2380 error = cdreadtoc(periph, te->address_format,
2381 LEADOUT, (u_int8_t *)lead,
2385 free(data, M_SCSICD);
2386 free(lead, M_SCSICD);
2387 cam_periph_unlock(periph);
2390 data->entries[idx - starting_track] =
2393 if (softc->quirks & CD_Q_BCD_TRACKS) {
2394 for (idx = 0; idx < num - 1; idx++) {
2395 data->entries[idx].track =
2396 bcd2bin(data->entries[idx].track);
2400 cam_periph_unlock(periph);
2401 error = copyout(data->entries, te->data, len);
2402 free(data, M_SCSICD);
2403 free(lead, M_SCSICD);
2406 case CDIOREADTOCENTRY:
2408 struct cd_toc_single *data;
2409 struct ioc_read_toc_single_entry *te =
2410 (struct ioc_read_toc_single_entry *) addr;
2411 struct ioc_toc_header *th;
2414 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2416 cam_periph_lock(periph);
2417 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2418 ("trying to do CDIOREADTOCENTRY\n"));
2420 if (te->address_format != CD_MSF_FORMAT
2421 && te->address_format != CD_LBA_FORMAT) {
2422 printf("error in readtocentry, "
2423 " returning EINVAL\n");
2424 free(data, M_SCSICD);
2426 cam_periph_unlock(periph);
2431 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2432 sizeof (*th), /*sense_flags*/0);
2434 free(data, M_SCSICD);
2435 cam_periph_unlock(periph);
2439 if (softc->quirks & CD_Q_BCD_TRACKS) {
2440 /* we are going to have to convert the BCD
2441 * encoding on the cd to what is expected
2443 th->starting_track =
2444 bcd2bin(th->starting_track);
2445 th->ending_track = bcd2bin(th->ending_track);
2449 track = th->starting_track;
2450 else if (track == LEADOUT)
2452 else if (track < th->starting_track ||
2453 track > th->ending_track + 1) {
2454 printf("error in readtocentry, "
2455 " returning EINVAL\n");
2456 free(data, M_SCSICD);
2458 cam_periph_unlock(periph);
2462 error = cdreadtoc(periph, te->address_format, track,
2463 (u_int8_t *)data, sizeof(*data),
2466 free(data, M_SCSICD);
2467 cam_periph_unlock(periph);
2471 if (softc->quirks & CD_Q_BCD_TRACKS)
2472 data->entry.track = bcd2bin(data->entry.track);
2473 bcopy(&data->entry, &te->entry,
2474 sizeof(struct cd_toc_entry));
2475 free(data, M_SCSICD);
2476 cam_periph_unlock(periph);
2481 struct ioc_patch *arg = (struct ioc_patch *)addr;
2482 struct cd_mode_params params;
2483 union cd_pages *page;
2485 params.alloc_len = sizeof(union cd_mode_data_6_10);
2486 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2489 cam_periph_lock(periph);
2490 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2491 ("trying to do CDIOCSETPATCH\n"));
2493 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2495 free(params.mode_buf, M_SCSICD);
2496 cam_periph_unlock(periph);
2499 page = cdgetpage(¶ms);
2501 page->audio.port[LEFT_PORT].channels =
2503 page->audio.port[RIGHT_PORT].channels =
2505 page->audio.port[2].channels = arg->patch[2];
2506 page->audio.port[3].channels = arg->patch[3];
2507 error = cdsetmode(periph, ¶ms);
2508 free(params.mode_buf, M_SCSICD);
2509 cam_periph_unlock(periph);
2514 struct ioc_vol *arg = (struct ioc_vol *) addr;
2515 struct cd_mode_params params;
2516 union cd_pages *page;
2518 params.alloc_len = sizeof(union cd_mode_data_6_10);
2519 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2522 cam_periph_lock(periph);
2523 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2524 ("trying to do CDIOCGETVOL\n"));
2526 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2528 free(params.mode_buf, M_SCSICD);
2529 cam_periph_unlock(periph);
2532 page = cdgetpage(¶ms);
2534 arg->vol[LEFT_PORT] =
2535 page->audio.port[LEFT_PORT].volume;
2536 arg->vol[RIGHT_PORT] =
2537 page->audio.port[RIGHT_PORT].volume;
2538 arg->vol[2] = page->audio.port[2].volume;
2539 arg->vol[3] = page->audio.port[3].volume;
2540 free(params.mode_buf, M_SCSICD);
2541 cam_periph_unlock(periph);
2546 struct ioc_vol *arg = (struct ioc_vol *) addr;
2547 struct cd_mode_params params;
2548 union cd_pages *page;
2550 params.alloc_len = sizeof(union cd_mode_data_6_10);
2551 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2554 cam_periph_lock(periph);
2555 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2556 ("trying to do CDIOCSETVOL\n"));
2558 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2560 free(params.mode_buf, M_SCSICD);
2561 cam_periph_unlock(periph);
2564 page = cdgetpage(¶ms);
2566 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2567 page->audio.port[LEFT_PORT].volume =
2568 arg->vol[LEFT_PORT];
2569 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2570 page->audio.port[RIGHT_PORT].volume =
2571 arg->vol[RIGHT_PORT];
2572 page->audio.port[2].volume = arg->vol[2];
2573 page->audio.port[3].volume = arg->vol[3];
2574 error = cdsetmode(periph, ¶ms);
2575 cam_periph_unlock(periph);
2576 free(params.mode_buf, M_SCSICD);
2581 struct cd_mode_params params;
2582 union cd_pages *page;
2584 params.alloc_len = sizeof(union cd_mode_data_6_10);
2585 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2588 cam_periph_lock(periph);
2589 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2590 ("trying to do CDIOCSETMONO\n"));
2592 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2594 free(params.mode_buf, M_SCSICD);
2595 cam_periph_unlock(periph);
2598 page = cdgetpage(¶ms);
2600 page->audio.port[LEFT_PORT].channels =
2601 LEFT_CHANNEL | RIGHT_CHANNEL;
2602 page->audio.port[RIGHT_PORT].channels =
2603 LEFT_CHANNEL | RIGHT_CHANNEL;
2604 page->audio.port[2].channels = 0;
2605 page->audio.port[3].channels = 0;
2606 error = cdsetmode(periph, ¶ms);
2607 cam_periph_unlock(periph);
2608 free(params.mode_buf, M_SCSICD);
2611 case CDIOCSETSTEREO:
2613 struct cd_mode_params params;
2614 union cd_pages *page;
2616 params.alloc_len = sizeof(union cd_mode_data_6_10);
2617 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2620 cam_periph_lock(periph);
2621 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2622 ("trying to do CDIOCSETSTEREO\n"));
2624 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2626 free(params.mode_buf, M_SCSICD);
2627 cam_periph_unlock(periph);
2630 page = cdgetpage(¶ms);
2632 page->audio.port[LEFT_PORT].channels =
2634 page->audio.port[RIGHT_PORT].channels =
2636 page->audio.port[2].channels = 0;
2637 page->audio.port[3].channels = 0;
2638 error = cdsetmode(periph, ¶ms);
2639 free(params.mode_buf, M_SCSICD);
2640 cam_periph_unlock(periph);
2645 struct cd_mode_params params;
2646 union cd_pages *page;
2648 params.alloc_len = sizeof(union cd_mode_data_6_10);
2649 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2652 cam_periph_lock(periph);
2653 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2654 ("trying to do CDIOCSETMUTE\n"));
2656 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2658 free(params.mode_buf, M_SCSICD);
2659 cam_periph_unlock(periph);
2662 page = cdgetpage(¶ms);
2664 page->audio.port[LEFT_PORT].channels = 0;
2665 page->audio.port[RIGHT_PORT].channels = 0;
2666 page->audio.port[2].channels = 0;
2667 page->audio.port[3].channels = 0;
2668 error = cdsetmode(periph, ¶ms);
2669 free(params.mode_buf, M_SCSICD);
2670 cam_periph_unlock(periph);
2675 struct cd_mode_params params;
2676 union cd_pages *page;
2678 params.alloc_len = sizeof(union cd_mode_data_6_10);
2679 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2682 cam_periph_lock(periph);
2683 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2684 ("trying to do CDIOCSETLEFT\n"));
2686 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2688 free(params.mode_buf, M_SCSICD);
2689 cam_periph_unlock(periph);
2692 page = cdgetpage(¶ms);
2694 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2695 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2696 page->audio.port[2].channels = 0;
2697 page->audio.port[3].channels = 0;
2698 error = cdsetmode(periph, ¶ms);
2699 free(params.mode_buf, M_SCSICD);
2700 cam_periph_unlock(periph);
2705 struct cd_mode_params params;
2706 union cd_pages *page;
2708 params.alloc_len = sizeof(union cd_mode_data_6_10);
2709 params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2712 cam_periph_lock(periph);
2713 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2714 ("trying to do CDIOCSETRIGHT\n"));
2716 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2718 free(params.mode_buf, M_SCSICD);
2719 cam_periph_unlock(periph);
2722 page = cdgetpage(¶ms);
2724 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2725 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2726 page->audio.port[2].channels = 0;
2727 page->audio.port[3].channels = 0;
2728 error = cdsetmode(periph, ¶ms);
2729 free(params.mode_buf, M_SCSICD);
2730 cam_periph_unlock(periph);
2734 cam_periph_lock(periph);
2735 error = cdpause(periph, 1);
2736 cam_periph_unlock(periph);
2739 cam_periph_lock(periph);
2740 error = cdpause(periph, 0);
2741 cam_periph_unlock(periph);
2744 cam_periph_lock(periph);
2745 error = cdstartunit(periph, 0);
2746 cam_periph_unlock(periph);
2749 cam_periph_lock(periph);
2750 error = cdstartunit(periph, 1);
2751 cam_periph_unlock(periph);
2754 cam_periph_lock(periph);
2755 error = cdstopunit(periph, 0);
2756 cam_periph_unlock(periph);
2759 cam_periph_lock(periph);
2760 error = cdstopunit(periph, 1);
2761 cam_periph_unlock(periph);
2764 cam_periph_lock(periph);
2765 cdprevent(periph, PR_ALLOW);
2766 cam_periph_unlock(periph);
2769 cam_periph_lock(periph);
2770 cdprevent(periph, PR_PREVENT);
2771 cam_periph_unlock(periph);
2774 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2778 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2782 /* return (cd_reset(periph)); */
2785 case CDRIOCREADSPEED:
2786 cam_periph_lock(periph);
2787 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2788 cam_periph_unlock(periph);
2790 case CDRIOCWRITESPEED:
2791 cam_periph_lock(periph);
2792 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2793 cam_periph_unlock(periph);
2795 case CDRIOCGETBLOCKSIZE:
2796 *(int *)addr = softc->params.blksize;
2798 case CDRIOCSETBLOCKSIZE:
2799 if (*(int *)addr <= 0) {
2803 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2806 case DVDIOCREPORTKEY: {
2807 struct dvd_authinfo *authinfo;
2809 authinfo = (struct dvd_authinfo *)addr;
2811 if (cmd == DVDIOCREPORTKEY)
2812 error = cdreportkey(periph, authinfo);
2814 error = cdsendkey(periph, authinfo);
2817 case DVDIOCREADSTRUCTURE: {
2818 struct dvd_struct *dvdstruct;
2820 dvdstruct = (struct dvd_struct *)addr;
2822 error = cdreaddvdstructure(periph, dvdstruct);
2827 cam_periph_lock(periph);
2828 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2829 cam_periph_unlock(periph);
2833 cam_periph_lock(periph);
2834 cam_periph_unhold(periph);
2836 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2837 if (error && bootverbose) {
2838 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2840 cam_periph_unlock(periph);
2846 cdprevent(struct cam_periph *periph, int action)
2849 struct cd_softc *softc;
2852 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2854 softc = (struct cd_softc *)periph->softc;
2856 if (((action == PR_ALLOW)
2857 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2858 || ((action == PR_PREVENT)
2859 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2863 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2865 scsi_prevent(&ccb->csio,
2866 /*retries*/ cd_retry_count,
2871 /* timeout */60000);
2873 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2874 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2876 xpt_release_ccb(ccb);
2879 if (action == PR_ALLOW)
2880 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2882 softc->flags |= CD_FLAG_DISC_LOCKED;
2887 * XXX: the disk media and sector size is only really able to change
2888 * XXX: while the device is closed.
2891 cdcheckmedia(struct cam_periph *periph)
2893 struct cd_softc *softc;
2894 struct ioc_toc_header *toch;
2895 struct cd_toc_single leadout;
2896 u_int32_t size, toclen;
2897 int error, num_entries, cdindex;
2899 softc = (struct cd_softc *)periph->softc;
2901 cdprevent(periph, PR_PREVENT);
2902 softc->disk->d_sectorsize = 2048;
2903 softc->disk->d_mediasize = 0;
2906 * Get the disc size and block size. If we can't get it, we don't
2907 * have media, most likely.
2909 if ((error = cdsize(periph, &size)) != 0) {
2910 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2911 cdprevent(periph, PR_ALLOW);
2914 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2915 softc->disk->d_sectorsize = softc->params.blksize;
2916 softc->disk->d_mediasize =
2917 (off_t)softc->params.blksize * softc->params.disksize;
2921 * Now we check the table of contents. This (currently) is only
2922 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2923 * things like present a separate entry in /dev for each track,
2924 * like that acd(4) driver does.
2926 bzero(&softc->toc, sizeof(softc->toc));
2927 toch = &softc->toc.header;
2929 * We will get errors here for media that doesn't have a table of
2930 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2931 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2932 * has not been recorded (no complete session) and the Format codes
2933 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2934 * with an INVALID FIELD IN CDB. Devices that are not capable of
2935 * reading an incomplete session on DDC/CD-R/RW media shall report
2936 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2938 * So this isn't fatal if we can't read the table of contents, it
2939 * just means that the user won't be able to issue the play tracks
2940 * ioctl, and likely lots of other stuff won't work either. They
2941 * need to burn the CD before we can do a whole lot with it. So
2942 * we don't print anything here if we get an error back.
2944 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2947 * Errors in reading the table of contents aren't fatal, we just
2948 * won't have a valid table of contents cached.
2952 bzero(&softc->toc, sizeof(softc->toc));
2956 if (softc->quirks & CD_Q_BCD_TRACKS) {
2957 toch->starting_track = bcd2bin(toch->starting_track);
2958 toch->ending_track = bcd2bin(toch->ending_track);
2961 /* Number of TOC entries, plus leadout */
2962 num_entries = (toch->ending_track - toch->starting_track) + 2;
2964 if (num_entries <= 0)
2967 toclen = num_entries * sizeof(struct cd_toc_entry);
2969 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2970 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2974 bzero(&softc->toc, sizeof(softc->toc));
2978 if (softc->quirks & CD_Q_BCD_TRACKS) {
2979 toch->starting_track = bcd2bin(toch->starting_track);
2980 toch->ending_track = bcd2bin(toch->ending_track);
2983 * XXX KDM is this necessary? Probably only if the drive doesn't
2984 * return leadout information with the table of contents.
2986 cdindex = toch->starting_track + num_entries -1;
2987 if (cdindex == toch->ending_track + 1) {
2989 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2990 (u_int8_t *)&leadout, sizeof(leadout),
2996 softc->toc.entries[cdindex - toch->starting_track] =
2999 if (softc->quirks & CD_Q_BCD_TRACKS) {
3000 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
3001 softc->toc.entries[cdindex].track =
3002 bcd2bin(softc->toc.entries[cdindex].track);
3006 softc->flags |= CD_FLAG_VALID_TOC;
3008 /* If the first track is audio, correct sector size. */
3009 if ((softc->toc.entries[0].control & 4) == 0) {
3010 softc->disk->d_sectorsize = softc->params.blksize = 2352;
3011 softc->disk->d_mediasize =
3012 (off_t)softc->params.blksize * softc->params.disksize;
3018 * We unconditionally (re)set the blocksize each time the
3019 * CD device is opened. This is because the CD can change,
3020 * and therefore the blocksize might change.
3021 * XXX problems here if some slice or partition is still
3022 * open with the old size?
3024 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
3025 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3026 softc->disk->d_devstat->block_size = softc->params.blksize;
3032 cdsize(struct cam_periph *periph, u_int32_t *size)
3034 struct cd_softc *softc;
3036 struct scsi_read_capacity_data *rcap_buf;
3039 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3041 softc = (struct cd_softc *)periph->softc;
3043 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3045 /* XXX Should be M_WAITOK */
3046 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3047 M_SCSICD, M_NOWAIT | M_ZERO);
3048 if (rcap_buf == NULL)
3051 scsi_read_capacity(&ccb->csio,
3052 /*retries*/ cd_retry_count,
3057 /* timeout */20000);
3059 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3060 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3062 xpt_release_ccb(ccb);
3064 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3065 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3066 /* Make sure we got at least some block size. */
3067 if (error == 0 && softc->params.blksize == 0)
3070 * SCSI-3 mandates that the reported blocksize shall be 2048.
3071 * Older drives sometimes report funny values, trim it down to
3072 * 2048, or other parts of the kernel will get confused.
3074 * XXX we leave drives alone that might report 512 bytes, as
3075 * well as drives reporting more weird sizes like perhaps 4K.
3077 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3078 softc->params.blksize = 2048;
3080 free(rcap_buf, M_SCSICD);
3081 *size = softc->params.disksize;
3088 cd6byteworkaround(union ccb *ccb)
3091 struct cam_periph *periph;
3092 struct cd_softc *softc;
3093 struct cd_mode_params *params;
3096 periph = xpt_path_periph(ccb->ccb_h.path);
3097 softc = (struct cd_softc *)periph->softc;
3099 cdb = ccb->csio.cdb_io.cdb_bytes;
3101 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3102 || ((cdb[0] != MODE_SENSE_6)
3103 && (cdb[0] != MODE_SELECT_6)))
3107 * Because there is no convenient place to stash the overall
3108 * cd_mode_params structure pointer, we have to grab it like this.
3109 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3110 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3112 * XXX It would be nice if, at some point, we could increase the
3113 * number of available peripheral private pointers. Both pointers
3114 * are currently used in most every peripheral driver.
3118 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3119 if (params->mode_buf == ccb->csio.data_ptr) {
3126 * This shouldn't happen. All mode sense and mode select
3127 * operations in the cd(4) driver MUST go through cdgetmode() and
3131 xpt_print(periph->path,
3132 "mode buffer not found in mode queue!\n");
3136 params->cdb_size = 10;
3137 softc->minimum_command_size = 10;
3138 xpt_print(ccb->ccb_h.path,
3139 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3140 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3142 if (cdb[0] == MODE_SENSE_6) {
3143 struct scsi_mode_sense_10 ms10;
3144 struct scsi_mode_sense_6 *ms6;
3147 ms6 = (struct scsi_mode_sense_6 *)cdb;
3149 bzero(&ms10, sizeof(ms10));
3150 ms10.opcode = MODE_SENSE_10;
3151 ms10.byte2 = ms6->byte2;
3152 ms10.page = ms6->page;
3155 * 10 byte mode header, block descriptor,
3156 * sizeof(union cd_pages)
3158 len = sizeof(struct cd_mode_data_10);
3159 ccb->csio.dxfer_len = len;
3161 scsi_ulto2b(len, ms10.length);
3162 ms10.control = ms6->control;
3163 bcopy(&ms10, cdb, 10);
3164 ccb->csio.cdb_len = 10;
3166 struct scsi_mode_select_10 ms10;
3167 struct scsi_mode_select_6 *ms6;
3168 struct scsi_mode_header_6 *header6;
3169 struct scsi_mode_header_10 *header10;
3170 struct scsi_mode_page_header *page_header;
3171 int blk_desc_len, page_num, page_size, len;
3173 ms6 = (struct scsi_mode_select_6 *)cdb;
3175 bzero(&ms10, sizeof(ms10));
3176 ms10.opcode = MODE_SELECT_10;
3177 ms10.byte2 = ms6->byte2;
3179 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3180 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3182 page_header = find_mode_page_6(header6);
3183 page_num = page_header->page_code;
3185 blk_desc_len = header6->blk_desc_len;
3187 page_size = cdgetpagesize(page_num);
3189 if (page_size != (page_header->page_length +
3190 sizeof(*page_header)))
3191 page_size = page_header->page_length +
3192 sizeof(*page_header);
3194 len = sizeof(*header10) + blk_desc_len + page_size;
3196 len = min(params->alloc_len, len);
3199 * Since the 6 byte parameter header is shorter than the 10
3200 * byte parameter header, we need to copy the actual mode
3201 * page data, and the block descriptor, if any, so things wind
3202 * up in the right place. The regions will overlap, but
3203 * bcopy() does the right thing.
3205 bcopy(params->mode_buf + sizeof(*header6),
3206 params->mode_buf + sizeof(*header10),
3207 len - sizeof(*header10));
3209 /* Make sure these fields are set correctly. */
3210 scsi_ulto2b(0, header10->data_length);
3211 header10->medium_type = 0;
3212 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3214 ccb->csio.dxfer_len = len;
3216 scsi_ulto2b(len, ms10.length);
3217 ms10.control = ms6->control;
3218 bcopy(&ms10, cdb, 10);
3219 ccb->csio.cdb_len = 10;
3222 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3223 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3226 cam_release_devq(ccb->ccb_h.path,
3230 /*getcount_only*/0);
3237 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3239 struct cd_softc *softc;
3240 struct cam_periph *periph;
3241 int error, error_code, sense_key, asc, ascq;
3243 periph = xpt_path_periph(ccb->ccb_h.path);
3244 softc = (struct cd_softc *)periph->softc;
3249 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3250 * CDB comes back with this particular error, try transforming it
3251 * into the 10 byte version.
3253 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3254 error = cd6byteworkaround(ccb);
3255 } else if (scsi_extract_sense_ccb(ccb,
3256 &error_code, &sense_key, &asc, &ascq)) {
3257 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3258 error = cd6byteworkaround(ccb);
3259 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3260 asc == 0x28 && ascq == 0x00)
3261 disk_media_changed(softc->disk, M_NOWAIT);
3262 else if (sense_key == SSD_KEY_NOT_READY &&
3263 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3264 softc->flags &= ~CD_FLAG_SAW_MEDIA;
3265 disk_media_gone(softc->disk, M_NOWAIT);
3269 if (error == ERESTART)
3274 * Until we have a better way of doing pack validation,
3275 * don't treat UAs as errors.
3277 sense_flags |= SF_RETRY_UA;
3278 return (cam_periph_error(ccb, cam_flags, sense_flags,
3279 &softc->saved_ccb));
3283 cdmediapoll(void *arg)
3285 struct cam_periph *periph = arg;
3286 struct cd_softc *softc = periph->softc;
3288 if (softc->flags & CD_FLAG_CHANGER)
3291 if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3292 softc->outstanding_cmds == 0) {
3293 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3295 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3298 /* Queue us up again */
3299 if (cd_poll_period != 0)
3300 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3304 * Read table of contents
3307 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3308 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3310 struct scsi_read_toc *scsi_cmd;
3312 struct ccb_scsiio *csio;
3319 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3324 /* retries */ cd_retry_count,
3325 /* cbfcnp */ cddone,
3326 /* flags */ CAM_DIR_IN,
3327 /* tag_action */ MSG_SIMPLE_Q_TAG,
3328 /* data_ptr */ data,
3329 /* dxfer_len */ len,
3330 /* sense_len */ SSD_FULL_SIZE,
3331 sizeof(struct scsi_read_toc),
3332 /* timeout */ 50000);
3334 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3335 bzero (scsi_cmd, sizeof(*scsi_cmd));
3337 if (mode == CD_MSF_FORMAT)
3338 scsi_cmd->byte2 |= CD_MSF;
3339 scsi_cmd->from_track = start;
3340 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3341 scsi_cmd->data_len[0] = (ntoc) >> 8;
3342 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3344 scsi_cmd->op_code = READ_TOC;
3346 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3347 /*sense_flags*/SF_RETRY_UA | sense_flags);
3349 xpt_release_ccb(ccb);
3355 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3356 u_int32_t format, int track,
3357 struct cd_sub_channel_info *data, u_int32_t len)
3359 struct scsi_read_subchannel *scsi_cmd;
3360 struct ccb_scsiio *csio;
3366 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3371 /* retries */ cd_retry_count,
3372 /* cbfcnp */ cddone,
3373 /* flags */ CAM_DIR_IN,
3374 /* tag_action */ MSG_SIMPLE_Q_TAG,
3375 /* data_ptr */ (u_int8_t *)data,
3376 /* dxfer_len */ len,
3377 /* sense_len */ SSD_FULL_SIZE,
3378 sizeof(struct scsi_read_subchannel),
3379 /* timeout */ 50000);
3381 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3382 bzero (scsi_cmd, sizeof(*scsi_cmd));
3384 scsi_cmd->op_code = READ_SUBCHANNEL;
3385 if (mode == CD_MSF_FORMAT)
3386 scsi_cmd->byte1 |= CD_MSF;
3387 scsi_cmd->byte2 = SRS_SUBQ;
3388 scsi_cmd->subchan_format = format;
3389 scsi_cmd->track = track;
3390 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3391 scsi_cmd->control = 0;
3393 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3394 /*sense_flags*/SF_RETRY_UA);
3396 xpt_release_ccb(ccb);
3403 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3404 * routine. See comments in cd6byteworkaround() for details.
3407 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3410 struct ccb_scsiio *csio;
3411 struct cd_softc *softc;
3416 softc = (struct cd_softc *)periph->softc;
3418 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3422 data->cdb_size = softc->minimum_command_size;
3423 if (data->cdb_size < 10)
3424 param_len = sizeof(struct cd_mode_data);
3426 param_len = sizeof(struct cd_mode_data_10);
3428 /* Don't say we've got more room than we actually allocated */
3429 param_len = min(param_len, data->alloc_len);
3431 scsi_mode_sense_len(csio,
3432 /* retries */ cd_retry_count,
3433 /* cbfcnp */ cddone,
3434 /* tag_action */ MSG_SIMPLE_Q_TAG,
3436 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3438 /* param_buf */ data->mode_buf,
3439 /* param_len */ param_len,
3440 /* minimum_cmd_size */ softc->minimum_command_size,
3441 /* sense_len */ SSD_FULL_SIZE,
3442 /* timeout */ 50000);
3445 * It would be nice not to have to do this, but there's no
3446 * available pointer in the CCB that would allow us to stuff the
3447 * mode params structure in there and retrieve it in
3448 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3449 * lets the caller know what CDB size we ended up using, so they
3450 * can find the actual mode page offset.
3452 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3454 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3455 /*sense_flags*/SF_RETRY_UA);
3457 xpt_release_ccb(ccb);
3459 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3462 * This is a bit of belt-and-suspenders checking, but if we run
3463 * into a situation where the target sends back multiple block
3464 * descriptors, we might not have enough space in the buffer to
3465 * see the whole mode page. Better to return an error than
3466 * potentially access memory beyond our malloced region.
3471 if (data->cdb_size == 10) {
3472 struct scsi_mode_header_10 *hdr10;
3474 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3475 data_len = scsi_2btoul(hdr10->data_length);
3476 data_len += sizeof(hdr10->data_length);
3478 struct scsi_mode_header_6 *hdr6;
3480 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3481 data_len = hdr6->data_length;
3482 data_len += sizeof(hdr6->data_length);
3486 * Complain if there is more mode data available than we
3487 * allocated space for. This could potentially happen if
3488 * we miscalculated the page length for some reason, if the
3489 * drive returns multiple block descriptors, or if it sets
3490 * the data length incorrectly.
3492 if (data_len > data->alloc_len) {
3493 xpt_print(periph->path, "allocated modepage %d length "
3494 "%d < returned length %d\n", page, data->alloc_len,
3503 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3504 * routine. See comments in cd6byteworkaround() for details.
3507 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3509 struct ccb_scsiio *csio;
3510 struct cd_softc *softc;
3512 int cdb_size, param_len;
3515 softc = (struct cd_softc *)periph->softc;
3517 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3524 * If the data is formatted for the 10 byte version of the mode
3525 * select parameter list, we need to use the 10 byte CDB.
3526 * Otherwise, we use whatever the stored minimum command size.
3528 if (data->cdb_size == 10)
3529 cdb_size = data->cdb_size;
3531 cdb_size = softc->minimum_command_size;
3533 if (cdb_size >= 10) {
3534 struct scsi_mode_header_10 *mode_header;
3537 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3539 data_len = scsi_2btoul(mode_header->data_length);
3541 scsi_ulto2b(0, mode_header->data_length);
3543 * SONY drives do not allow a mode select with a medium_type
3544 * value that has just been returned by a mode sense; use a
3545 * medium_type of 0 (Default) instead.
3547 mode_header->medium_type = 0;
3550 * Pass back whatever the drive passed to us, plus the size
3551 * of the data length field.
3553 param_len = data_len + sizeof(mode_header->data_length);
3556 struct scsi_mode_header_6 *mode_header;
3558 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3560 param_len = mode_header->data_length + 1;
3562 mode_header->data_length = 0;
3564 * SONY drives do not allow a mode select with a medium_type
3565 * value that has just been returned by a mode sense; use a
3566 * medium_type of 0 (Default) instead.
3568 mode_header->medium_type = 0;
3571 /* Don't say we've got more room than we actually allocated */
3572 param_len = min(param_len, data->alloc_len);
3574 scsi_mode_select_len(csio,
3575 /* retries */ cd_retry_count,
3576 /* cbfcnp */ cddone,
3577 /* tag_action */ MSG_SIMPLE_Q_TAG,
3578 /* scsi_page_fmt */ 1,
3580 /* param_buf */ data->mode_buf,
3581 /* param_len */ param_len,
3582 /* minimum_cmd_size */ cdb_size,
3583 /* sense_len */ SSD_FULL_SIZE,
3584 /* timeout */ 50000);
3586 /* See comments in cdgetmode() and cd6byteworkaround(). */
3587 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3589 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3590 /*sense_flags*/SF_RETRY_UA);
3592 xpt_release_ccb(ccb);
3594 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3601 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3603 struct ccb_scsiio *csio;
3609 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3612 * Use the smallest possible command to perform the operation.
3614 if ((len & 0xffff0000) == 0) {
3616 * We can fit in a 10 byte cdb.
3618 struct scsi_play_10 *scsi_cmd;
3620 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3621 bzero (scsi_cmd, sizeof(*scsi_cmd));
3622 scsi_cmd->op_code = PLAY_10;
3623 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3624 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3625 cdb_len = sizeof(*scsi_cmd);
3627 struct scsi_play_12 *scsi_cmd;
3629 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3630 bzero (scsi_cmd, sizeof(*scsi_cmd));
3631 scsi_cmd->op_code = PLAY_12;
3632 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3633 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3634 cdb_len = sizeof(*scsi_cmd);
3637 /*retries*/ cd_retry_count,
3639 /*flags*/CAM_DIR_NONE,
3643 /*sense_len*/SSD_FULL_SIZE,
3645 /*timeout*/50 * 1000);
3647 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3648 /*sense_flags*/SF_RETRY_UA);
3650 xpt_release_ccb(ccb);
3656 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3657 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3659 struct scsi_play_msf *scsi_cmd;
3660 struct ccb_scsiio *csio;
3666 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3671 /* retries */ cd_retry_count,
3672 /* cbfcnp */ cddone,
3673 /* flags */ CAM_DIR_NONE,
3674 /* tag_action */ MSG_SIMPLE_Q_TAG,
3675 /* data_ptr */ NULL,
3677 /* sense_len */ SSD_FULL_SIZE,
3678 sizeof(struct scsi_play_msf),
3679 /* timeout */ 50000);
3681 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3682 bzero (scsi_cmd, sizeof(*scsi_cmd));
3684 scsi_cmd->op_code = PLAY_MSF;
3685 scsi_cmd->start_m = startm;
3686 scsi_cmd->start_s = starts;
3687 scsi_cmd->start_f = startf;
3688 scsi_cmd->end_m = endm;
3689 scsi_cmd->end_s = ends;
3690 scsi_cmd->end_f = endf;
3692 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3693 /*sense_flags*/SF_RETRY_UA);
3695 xpt_release_ccb(ccb);
3702 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3703 u_int32_t etrack, u_int32_t eindex)
3705 struct scsi_play_track *scsi_cmd;
3706 struct ccb_scsiio *csio;
3712 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3717 /* retries */ cd_retry_count,
3718 /* cbfcnp */ cddone,
3719 /* flags */ CAM_DIR_NONE,
3720 /* tag_action */ MSG_SIMPLE_Q_TAG,
3721 /* data_ptr */ NULL,
3723 /* sense_len */ SSD_FULL_SIZE,
3724 sizeof(struct scsi_play_track),
3725 /* timeout */ 50000);
3727 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3728 bzero (scsi_cmd, sizeof(*scsi_cmd));
3730 scsi_cmd->op_code = PLAY_TRACK;
3731 scsi_cmd->start_track = strack;
3732 scsi_cmd->start_index = sindex;
3733 scsi_cmd->end_track = etrack;
3734 scsi_cmd->end_index = eindex;
3736 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3737 /*sense_flags*/SF_RETRY_UA);
3739 xpt_release_ccb(ccb);
3745 cdpause(struct cam_periph *periph, u_int32_t go)
3747 struct scsi_pause *scsi_cmd;
3748 struct ccb_scsiio *csio;
3754 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3759 /* retries */ cd_retry_count,
3760 /* cbfcnp */ cddone,
3761 /* flags */ CAM_DIR_NONE,
3762 /* tag_action */ MSG_SIMPLE_Q_TAG,
3763 /* data_ptr */ NULL,
3765 /* sense_len */ SSD_FULL_SIZE,
3766 sizeof(struct scsi_pause),
3767 /* timeout */ 50000);
3769 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3770 bzero (scsi_cmd, sizeof(*scsi_cmd));
3772 scsi_cmd->op_code = PAUSE;
3773 scsi_cmd->resume = go;
3775 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3776 /*sense_flags*/SF_RETRY_UA);
3778 xpt_release_ccb(ccb);
3784 cdstartunit(struct cam_periph *periph, int load)
3791 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3793 scsi_start_stop(&ccb->csio,
3794 /* retries */ cd_retry_count,
3795 /* cbfcnp */ cddone,
3796 /* tag_action */ MSG_SIMPLE_Q_TAG,
3798 /* load_eject */ load,
3799 /* immediate */ FALSE,
3800 /* sense_len */ SSD_FULL_SIZE,
3801 /* timeout */ 50000);
3803 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3804 /*sense_flags*/SF_RETRY_UA);
3806 xpt_release_ccb(ccb);
3812 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3819 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3821 scsi_start_stop(&ccb->csio,
3822 /* retries */ cd_retry_count,
3823 /* cbfcnp */ cddone,
3824 /* tag_action */ MSG_SIMPLE_Q_TAG,
3826 /* load_eject */ eject,
3827 /* immediate */ FALSE,
3828 /* sense_len */ SSD_FULL_SIZE,
3829 /* timeout */ 50000);
3831 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3832 /*sense_flags*/SF_RETRY_UA);
3834 xpt_release_ccb(ccb);
3840 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3842 struct scsi_set_speed *scsi_cmd;
3843 struct ccb_scsiio *csio;
3848 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3851 /* Preserve old behavior: units in multiples of CDROM speed */
3858 /* retries */ cd_retry_count,
3859 /* cbfcnp */ cddone,
3860 /* flags */ CAM_DIR_NONE,
3861 /* tag_action */ MSG_SIMPLE_Q_TAG,
3862 /* data_ptr */ NULL,
3864 /* sense_len */ SSD_FULL_SIZE,
3865 sizeof(struct scsi_set_speed),
3866 /* timeout */ 50000);
3868 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3869 bzero(scsi_cmd, sizeof(*scsi_cmd));
3871 scsi_cmd->opcode = SET_CD_SPEED;
3872 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3873 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3875 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3876 /*sense_flags*/SF_RETRY_UA);
3878 xpt_release_ccb(ccb);
3884 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3896 switch (authinfo->format) {
3897 case DVD_REPORT_AGID:
3898 length = sizeof(struct scsi_report_key_data_agid);
3900 case DVD_REPORT_CHALLENGE:
3901 length = sizeof(struct scsi_report_key_data_challenge);
3903 case DVD_REPORT_KEY1:
3904 length = sizeof(struct scsi_report_key_data_key1_key2);
3906 case DVD_REPORT_TITLE_KEY:
3907 length = sizeof(struct scsi_report_key_data_title);
3908 /* The lba field is only set for the title key */
3909 lba = authinfo->lba;
3911 case DVD_REPORT_ASF:
3912 length = sizeof(struct scsi_report_key_data_asf);
3914 case DVD_REPORT_RPC:
3915 length = sizeof(struct scsi_report_key_data_rpc);
3917 case DVD_INVALIDATE_AGID:
3925 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3929 cam_periph_lock(periph);
3930 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3932 scsi_report_key(&ccb->csio,
3933 /* retries */ cd_retry_count,
3934 /* cbfcnp */ cddone,
3935 /* tag_action */ MSG_SIMPLE_Q_TAG,
3937 /* agid */ authinfo->agid,
3938 /* key_format */ authinfo->format,
3939 /* data_ptr */ databuf,
3940 /* dxfer_len */ length,
3941 /* sense_len */ SSD_FULL_SIZE,
3942 /* timeout */ 50000);
3944 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3945 /*sense_flags*/SF_RETRY_UA);
3950 if (ccb->csio.resid != 0) {
3951 xpt_print(periph->path, "warning, residual for report key "
3952 "command is %d\n", ccb->csio.resid);
3955 switch(authinfo->format) {
3956 case DVD_REPORT_AGID: {
3957 struct scsi_report_key_data_agid *agid_data;
3959 agid_data = (struct scsi_report_key_data_agid *)databuf;
3961 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3965 case DVD_REPORT_CHALLENGE: {
3966 struct scsi_report_key_data_challenge *chal_data;
3968 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3970 bcopy(chal_data->challenge_key, authinfo->keychal,
3971 min(sizeof(chal_data->challenge_key),
3972 sizeof(authinfo->keychal)));
3975 case DVD_REPORT_KEY1: {
3976 struct scsi_report_key_data_key1_key2 *key1_data;
3978 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3980 bcopy(key1_data->key1, authinfo->keychal,
3981 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3984 case DVD_REPORT_TITLE_KEY: {
3985 struct scsi_report_key_data_title *title_data;
3987 title_data = (struct scsi_report_key_data_title *)databuf;
3989 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3990 RKD_TITLE_CPM_SHIFT;
3991 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3992 RKD_TITLE_CP_SEC_SHIFT;
3993 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3994 RKD_TITLE_CMGS_SHIFT;
3995 bcopy(title_data->title_key, authinfo->keychal,
3996 min(sizeof(title_data->title_key),
3997 sizeof(authinfo->keychal)));
4000 case DVD_REPORT_ASF: {
4001 struct scsi_report_key_data_asf *asf_data;
4003 asf_data = (struct scsi_report_key_data_asf *)databuf;
4005 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
4008 case DVD_REPORT_RPC: {
4009 struct scsi_report_key_data_rpc *rpc_data;
4011 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
4013 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
4015 authinfo->vend_rsts =
4016 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
4017 RKD_RPC_VENDOR_RESET_SHIFT;
4018 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4019 authinfo->region = rpc_data->region_mask;
4020 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4023 case DVD_INVALIDATE_AGID:
4026 /* This should be impossible, since we checked above */
4029 break; /* NOTREACHED */
4033 xpt_release_ccb(ccb);
4034 cam_periph_unlock(periph);
4036 if (databuf != NULL)
4037 free(databuf, M_DEVBUF);
4043 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4053 switch(authinfo->format) {
4054 case DVD_SEND_CHALLENGE: {
4055 struct scsi_report_key_data_challenge *challenge_data;
4057 length = sizeof(*challenge_data);
4059 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4061 databuf = (u_int8_t *)challenge_data;
4063 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4064 challenge_data->data_len);
4066 bcopy(authinfo->keychal, challenge_data->challenge_key,
4067 min(sizeof(authinfo->keychal),
4068 sizeof(challenge_data->challenge_key)));
4071 case DVD_SEND_KEY2: {
4072 struct scsi_report_key_data_key1_key2 *key2_data;
4074 length = sizeof(*key2_data);
4076 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4078 databuf = (u_int8_t *)key2_data;
4080 scsi_ulto2b(length - sizeof(key2_data->data_len),
4081 key2_data->data_len);
4083 bcopy(authinfo->keychal, key2_data->key1,
4084 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4088 case DVD_SEND_RPC: {
4089 struct scsi_send_key_data_rpc *rpc_data;
4091 length = sizeof(*rpc_data);
4093 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4095 databuf = (u_int8_t *)rpc_data;
4097 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4098 rpc_data->data_len);
4100 rpc_data->region_code = authinfo->region;
4107 cam_periph_lock(periph);
4108 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4110 scsi_send_key(&ccb->csio,
4111 /* retries */ cd_retry_count,
4112 /* cbfcnp */ cddone,
4113 /* tag_action */ MSG_SIMPLE_Q_TAG,
4114 /* agid */ authinfo->agid,
4115 /* key_format */ authinfo->format,
4116 /* data_ptr */ databuf,
4117 /* dxfer_len */ length,
4118 /* sense_len */ SSD_FULL_SIZE,
4119 /* timeout */ 50000);
4121 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4122 /*sense_flags*/SF_RETRY_UA);
4124 xpt_release_ccb(ccb);
4125 cam_periph_unlock(periph);
4127 if (databuf != NULL)
4128 free(databuf, M_DEVBUF);
4134 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4144 /* The address is reserved for many of the formats */
4147 switch(dvdstruct->format) {
4148 case DVD_STRUCT_PHYSICAL:
4149 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4151 case DVD_STRUCT_COPYRIGHT:
4152 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4154 case DVD_STRUCT_DISCKEY:
4155 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4157 case DVD_STRUCT_BCA:
4158 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4160 case DVD_STRUCT_MANUFACT:
4161 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4163 case DVD_STRUCT_CMI:
4165 case DVD_STRUCT_PROTDISCID:
4166 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4168 case DVD_STRUCT_DISCKEYBLOCK:
4169 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4171 case DVD_STRUCT_DDS:
4172 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4174 case DVD_STRUCT_MEDIUM_STAT:
4175 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4177 case DVD_STRUCT_SPARE_AREA:
4178 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4180 case DVD_STRUCT_RMD_LAST:
4182 case DVD_STRUCT_RMD_RMA:
4184 case DVD_STRUCT_PRERECORDED:
4185 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4187 case DVD_STRUCT_UNIQUEID:
4188 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4190 case DVD_STRUCT_DCB:
4192 case DVD_STRUCT_LIST:
4194 * This is the maximum allocation length for the READ DVD
4195 * STRUCTURE command. There's nothing in the MMC3 spec
4196 * that indicates a limit in the amount of data that can
4197 * be returned from this call, other than the limits
4198 * imposed by the 2-byte length variables.
4207 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4211 cam_periph_lock(periph);
4212 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4214 scsi_read_dvd_structure(&ccb->csio,
4215 /* retries */ cd_retry_count,
4216 /* cbfcnp */ cddone,
4217 /* tag_action */ MSG_SIMPLE_Q_TAG,
4219 /* layer_number */ dvdstruct->layer_num,
4220 /* key_format */ dvdstruct->format,
4221 /* agid */ dvdstruct->agid,
4222 /* data_ptr */ databuf,
4223 /* dxfer_len */ length,
4224 /* sense_len */ SSD_FULL_SIZE,
4225 /* timeout */ 50000);
4227 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4228 /*sense_flags*/SF_RETRY_UA);
4233 switch(dvdstruct->format) {
4234 case DVD_STRUCT_PHYSICAL: {
4235 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4236 struct dvd_layer *outlayer;
4237 struct scsi_read_dvd_struct_data_physical *phys_data;
4240 (struct scsi_read_dvd_struct_data_physical *)databuf;
4241 inlayer = &phys_data->layer_desc;
4242 outlayer = (struct dvd_layer *)&dvdstruct->data;
4244 dvdstruct->length = sizeof(*inlayer);
4246 outlayer->book_type = (inlayer->book_type_version &
4247 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4248 outlayer->book_version = (inlayer->book_type_version &
4249 RDSD_BOOK_VERSION_MASK);
4250 outlayer->disc_size = (inlayer->disc_size_max_rate &
4251 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4252 outlayer->max_rate = (inlayer->disc_size_max_rate &
4253 RDSD_MAX_RATE_MASK);
4254 outlayer->nlayers = (inlayer->layer_info &
4255 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4256 outlayer->track_path = (inlayer->layer_info &
4257 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4258 outlayer->layer_type = (inlayer->layer_info &
4259 RDSD_LAYER_TYPE_MASK);
4260 outlayer->linear_density = (inlayer->density &
4261 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4262 outlayer->track_density = (inlayer->density &
4263 RDSD_TRACK_DENSITY_MASK);
4264 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4266 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4267 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4268 outlayer->end_sector_l0 =
4269 scsi_3btoul(inlayer->end_sector_layer0);
4272 case DVD_STRUCT_COPYRIGHT: {
4273 struct scsi_read_dvd_struct_data_copyright *copy_data;
4275 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4278 dvdstruct->cpst = copy_data->cps_type;
4279 dvdstruct->rmi = copy_data->region_info;
4280 dvdstruct->length = 0;
4286 * Tell the user what the overall length is, no matter
4287 * what we can actually fit in the data buffer.
4289 dvdstruct->length = length - ccb->csio.resid -
4290 sizeof(struct scsi_read_dvd_struct_data_header);
4293 * But only actually copy out the smaller of what we read
4294 * in or what the structure can take.
4296 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4298 min(sizeof(dvdstruct->data), dvdstruct->length));
4303 xpt_release_ccb(ccb);
4304 cam_periph_unlock(periph);
4306 if (databuf != NULL)
4307 free(databuf, M_DEVBUF);
4313 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4314 void (*cbfcnp)(struct cam_periph *, union ccb *),
4315 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4316 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4317 u_int8_t sense_len, u_int32_t timeout)
4319 struct scsi_report_key *scsi_cmd;
4321 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4322 bzero(scsi_cmd, sizeof(*scsi_cmd));
4323 scsi_cmd->opcode = REPORT_KEY;
4324 scsi_ulto4b(lba, scsi_cmd->lba);
4325 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4326 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4327 (key_format & RK_KF_KEYFORMAT_MASK);
4332 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4334 /*data_ptr*/ data_ptr,
4335 /*dxfer_len*/ dxfer_len,
4342 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4343 void (*cbfcnp)(struct cam_periph *, union ccb *),
4344 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4345 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4348 struct scsi_send_key *scsi_cmd;
4350 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4351 bzero(scsi_cmd, sizeof(*scsi_cmd));
4352 scsi_cmd->opcode = SEND_KEY;
4354 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4355 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4356 (key_format & RK_KF_KEYFORMAT_MASK);
4361 /*flags*/ CAM_DIR_OUT,
4363 /*data_ptr*/ data_ptr,
4364 /*dxfer_len*/ dxfer_len,
4372 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4373 void (*cbfcnp)(struct cam_periph *, union ccb *),
4374 u_int8_t tag_action, u_int32_t address,
4375 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4376 u_int8_t *data_ptr, u_int32_t dxfer_len,
4377 u_int8_t sense_len, u_int32_t timeout)
4379 struct scsi_read_dvd_structure *scsi_cmd;
4381 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4382 bzero(scsi_cmd, sizeof(*scsi_cmd));
4383 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4385 scsi_ulto4b(address, scsi_cmd->address);
4386 scsi_cmd->layer_number = layer_number;
4387 scsi_cmd->format = format;
4388 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4389 /* The AGID is the top two bits of this byte */
4390 scsi_cmd->agid = agid << 6;
4395 /*flags*/ CAM_DIR_IN,
4397 /*data_ptr*/ data_ptr,
4398 /*dxfer_len*/ dxfer_len,