2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
30 * Portions of this driver taken from the original FreeBSD cd driver.
31 * Written by Julian Elischer (julian@tfs.com)
32 * for TRW Financial Systems for use under the MACH(2.5) operating system.
34 * TRW Financial Systems, in accordance with their agreement with Carnegie
35 * Mellon University, makes this software available to CMU to distribute
36 * or use in any manner that they see fit as long as this message is kept with
37 * the software. For this reason TFS also grants any other persons or
38 * organisations permission to use or modify this software.
40 * TFS supplies this software to be publicly redistributed
41 * on the understanding that TFS is not responsible for the correct
42 * functioning of this software in any circumstances.
44 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
46 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
57 #include <sys/dtype.h>
58 #include <sys/malloc.h>
60 #include <sys/cdrio.h>
61 #include <sys/dvdio.h>
62 #include <sys/devicestat.h>
63 #include <sys/sysctl.h>
64 #include <sys/taskqueue.h>
66 #include <sys/camlib.h>
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
76 #include "../cam_queue.h"
77 #include "../cam_sim.h"
79 #include "scsi_message.h"
83 #define LEADOUT 0xaa /* leadout toc entry */
93 CD_Q_BCD_TRACKS = 0x02,
94 CD_Q_NO_CHANGER = 0x04,
96 CD_Q_10_BYTE_ONLY = 0x10
100 CD_FLAG_INVALID = 0x0001,
101 CD_FLAG_NEW_DISC = 0x0002,
102 CD_FLAG_DISC_LOCKED = 0x0004,
103 CD_FLAG_DISC_REMOVABLE = 0x0008,
104 CD_FLAG_TAGGED_QUEUING = 0x0010,
105 CD_FLAG_CHANGER = 0x0040,
106 CD_FLAG_ACTIVE = 0x0080,
107 CD_FLAG_SCHED_ON_COMP = 0x0100,
108 CD_FLAG_RETRY_UA = 0x0200,
109 CD_FLAG_VALID_MEDIA = 0x0400,
110 CD_FLAG_VALID_TOC = 0x0800,
111 CD_FLAG_SCTX_INIT = 0x1000,
112 CD_FLAG_OPEN = 0x2000,
113 CD_FLAG_CAP_MUTE = 0x4000
117 CD_CCB_POLLED = 0x00,
119 CD_CCB_BUFFER_IO = 0x02,
120 CD_CCB_WAITING = 0x03,
121 CD_CCB_TYPE_MASK = 0x0F,
122 CD_CCB_RETRY_UA = 0x10
126 CHANGER_TIMEOUT_SCHED = 0x01,
127 CHANGER_SHORT_TMOUT_SCHED = 0x02,
128 CHANGER_MANUAL_CALL = 0x04,
129 CHANGER_NEED_TIMEOUT = 0x08
132 #define ccb_state ppriv_field0
133 #define ccb_bio ppriv_ptr1
136 struct ioc_toc_header header;
137 struct cd_toc_entry entries[100];
140 struct cd_toc_single {
141 struct ioc_toc_header header;
142 struct cd_toc_entry entry;
153 volatile cd_flags flags;
154 struct bio_queue_head bio_queue;
155 LIST_HEAD(, ccb_hdr) pending_ccbs;
156 struct cd_params params;
160 struct devstat device_stats;
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;
167 struct task sysctl_task;
168 struct sysctl_ctx_list sysctl_ctx;
169 struct sysctl_oid *sysctl_tree;
170 STAILQ_HEAD(, cd_mode_params) mode_queue;
171 struct cd_tocdata toc;
174 struct cd_page_sizes {
179 static struct cd_page_sizes cd_page_size_table[] =
181 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
184 struct cd_quirk_entry {
185 struct scsi_inquiry_pattern inq_pat;
190 * The changer quirk entries aren't strictly necessary. Basically, what
191 * they do is tell cdregister() up front that a device is a changer.
192 * Otherwise, it will figure that fact out once it sees a LUN on the device
193 * that is greater than 0. If it is known up front that a device is a changer,
194 * all I/O to the device will go through the changer scheduling routines, as
195 * opposed to the "normal" CD code.
197 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
198 * your device hangs when it gets a 10 byte command. Adding a quirk just
199 * to get rid of the informative diagnostic message is not acceptable. All
200 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
201 * referenced in a comment along with the quirk) , and must be approved by
202 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
203 * be removed until the submitter can explain why they are needed.
204 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
205 * when the CAM_NEW_TRAN_CODE work is done.
207 static struct cd_quirk_entry cd_quirk_table[] =
210 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
211 /*quirks*/ CD_Q_CHANGER
214 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
215 "*"}, /* quirks */ CD_Q_CHANGER
218 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
219 /* quirks */ CD_Q_CHANGER
222 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
223 /* quirks */ CD_Q_BCD_TRACKS
227 static d_open_t cdopen;
228 static d_close_t cdclose;
229 static d_ioctl_t cdioctl;
230 static d_strategy_t cdstrategy;
232 static periph_init_t cdinit;
233 static periph_ctor_t cdregister;
234 static periph_dtor_t cdcleanup;
235 static periph_start_t cdstart;
236 static periph_oninv_t cdoninvalidate;
237 static void cdasync(void *callback_arg, u_int32_t code,
238 struct cam_path *path, void *arg);
239 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
240 static void cdshorttimeout(void *arg);
241 static void cdschedule(struct cam_periph *periph, int priority);
242 static void cdrunchangerqueue(void *arg);
243 static void cdchangerschedule(struct cd_softc *softc);
244 static int cdrunccb(union ccb *ccb,
245 int (*error_routine)(union ccb *ccb,
247 u_int32_t sense_flags),
248 u_int32_t cam_flags, u_int32_t sense_flags);
249 static union ccb *cdgetccb(struct cam_periph *periph,
251 static void cddone(struct cam_periph *periph,
252 union ccb *start_ccb);
253 static int cderror(union ccb *ccb, u_int32_t cam_flags,
254 u_int32_t sense_flags);
255 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
256 static int cdgetpagesize(int page_num);
257 static void cdprevent(struct cam_periph *periph, int action);
258 static int cdcheckmedia(struct cam_periph *periph);
259 static int cdsize(struct cam_periph *periph, u_int32_t *size);
260 static int cd6byteworkaround(union ccb *ccb);
261 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
262 u_int32_t start, u_int8_t *data,
263 u_int32_t len, u_int32_t sense_flags);
264 static int cdgetmode(struct cam_periph *periph,
265 struct cd_mode_params *data, u_int32_t page);
266 static int cdsetmode(struct cam_periph *periph,
267 struct cd_mode_params *data);
268 static int cdplay(struct cam_periph *periph, u_int32_t blk,
270 static int cdreadsubchannel(struct cam_periph *periph,
271 u_int32_t mode, u_int32_t format,
273 struct cd_sub_channel_info *data,
275 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
276 u_int32_t starts, u_int32_t startf,
277 u_int32_t endm, u_int32_t ends,
279 static int cdplaytracks(struct cam_periph *periph,
280 u_int32_t strack, u_int32_t sindex,
281 u_int32_t etrack, u_int32_t eindex);
282 static int cdpause(struct cam_periph *periph, u_int32_t go);
283 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
284 static int cdstartunit(struct cam_periph *periph, int load);
285 static int cdsetspeed(struct cam_periph *periph,
286 u_int32_t rdspeed, u_int32_t wrspeed);
287 static int cdreportkey(struct cam_periph *periph,
288 struct dvd_authinfo *authinfo);
289 static int cdsendkey(struct cam_periph *periph,
290 struct dvd_authinfo *authinfo);
291 static int cdreaddvdstructure(struct cam_periph *periph,
292 struct dvd_struct *dvdstruct);
294 static struct periph_driver cddriver =
297 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
300 PERIPHDRIVER_DECLARE(cd, cddriver);
302 static struct dev_ops cd_ops = {
303 { "cd", 0, D_DISK | D_MPSAFE },
307 .d_write = physwrite,
309 .d_strategy = cdstrategy
312 static struct extend_array *cdperiphs;
314 #ifndef CHANGER_MIN_BUSY_SECONDS
315 #define CHANGER_MIN_BUSY_SECONDS 5
317 #ifndef CHANGER_MAX_BUSY_SECONDS
318 #define CHANGER_MAX_BUSY_SECONDS 15
321 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
322 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
324 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
325 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
326 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
327 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
328 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
329 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
330 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
331 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
335 target_id_t target_id;
338 struct timeval start_time;
339 struct cd_softc *cur_device;
340 struct callout short_handle;
341 struct callout long_handle;
342 volatile cd_changer_flags flags;
343 STAILQ_ENTRY(cdchanger) changer_links;
344 STAILQ_HEAD(chdevlist, cd_softc) chluns;
347 static struct lock changerq_lock;
348 static STAILQ_HEAD(changerlist, cdchanger) changerq;
349 static int num_changers;
351 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
358 lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
359 STAILQ_INIT(&changerq);
362 * Create our extend array for storing the devices we attach to.
364 cdperiphs = cam_extend_new();
365 if (cdperiphs == NULL) {
366 kprintf("cd: Failed to alloc extend array!\n");
371 * Install a global async callback. This callback will
372 * receive async callbacks like "new device found".
374 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
376 if (status != CAM_REQ_CMP) {
377 kprintf("cd: Failed to attach master async callback "
378 "due to status 0x%x!\n", status);
383 cdoninvalidate(struct cam_periph *periph)
385 struct cd_softc *softc;
389 softc = (struct cd_softc *)periph->softc;
392 * De-register any async callbacks.
394 xpt_register_async(0, cdasync, periph, periph->path);
396 softc->flags |= CD_FLAG_INVALID;
399 * Return all queued I/O with ENXIO.
400 * XXX Handle any transactions queued to the card
401 * with XPT_ABORT_CCB.
403 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
404 q_bp = q_bio->bio_buf;
405 q_bp->b_resid = q_bp->b_bcount;
406 q_bp->b_error = ENXIO;
407 q_bp->b_flags |= B_ERROR;
412 * If this device is part of a changer, and it was scheduled
413 * to run, remove it from the run queue since we just nuked
414 * all of its scheduled I/O.
416 if ((softc->flags & CD_FLAG_CHANGER)
417 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
418 camq_remove(&softc->changer->devq, softc->pinfo.index);
420 xpt_print(periph->path, "lost device\n");
424 cdcleanup(struct cam_periph *periph)
426 struct cd_softc *softc;
428 softc = (struct cd_softc *)periph->softc;
430 xpt_print(periph->path, "removing device entry\n");
432 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
433 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
434 xpt_print(periph->path, "can't remove sysctl context\n");
438 * In the queued, non-active case, the device in question
439 * has already been removed from the changer run queue. Since this
440 * device is active, we need to de-activate it, and schedule
441 * another device to run. (if there is another one to run)
443 if ((softc->flags & CD_FLAG_CHANGER)
444 && (softc->flags & CD_FLAG_ACTIVE)) {
447 * The purpose of the short timeout is soley to determine
448 * whether the current device has finished or not. Well,
449 * since we're removing the active device, we know that it
450 * is finished. So, get rid of the short timeout.
451 * Otherwise, if we're in the time period before the short
452 * timeout fires, and there are no other devices in the
453 * queue to run, there won't be any other device put in the
454 * active slot. i.e., when we call cdrunchangerqueue()
455 * below, it won't do anything. Then, when the short
456 * timeout fires, it'll look at the "current device", which
457 * we are free below, and possibly panic the kernel on a
458 * bogus pointer reference.
460 * The long timeout doesn't really matter, since we
461 * decrement the qfrozen_cnt to indicate that there is
462 * nothing in the active slot now. Therefore, there won't
463 * be any bogus pointer references there.
465 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
466 callout_stop(&softc->changer->short_handle);
467 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
469 softc->changer->devq.qfrozen_cnt--;
470 softc->changer->flags |= CHANGER_MANUAL_CALL;
471 cdrunchangerqueue(softc->changer);
475 * If we're removing the last device on the changer, go ahead and
476 * remove the changer device structure.
478 if ((softc->flags & CD_FLAG_CHANGER)
479 && (--softc->changer->num_devices == 0)) {
482 * Theoretically, there shouldn't be any timeouts left, but
483 * I'm not completely sure that that will be the case. So,
484 * it won't hurt to check and see if there are any left.
486 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
487 callout_stop(&softc->changer->long_handle);
488 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
491 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
492 callout_stop(&softc->changer->short_handle);
493 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
496 lockmgr(&changerq_lock, LK_EXCLUSIVE);
497 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
500 lockmgr(&changerq_lock, LK_RELEASE);
501 xpt_print(periph->path, "removing changer entry\n");
502 kfree(softc->changer, M_DEVBUF);
504 devstat_remove_entry(&softc->device_stats);
505 cam_extend_release(cdperiphs, periph->unit_number);
506 if (softc->disk.d_rawdev) {
507 cam_periph_unlock(periph);
508 disk_destroy(&softc->disk);
509 cam_periph_lock(periph);
511 kfree(softc, M_DEVBUF);
515 cdasync(void *callback_arg, u_int32_t code,
516 struct cam_path *path, void *arg)
518 struct cam_periph *periph;
520 periph = (struct cam_periph *)callback_arg;
523 case AC_FOUND_DEVICE:
525 struct ccb_getdev *cgd;
528 cgd = (struct ccb_getdev *)arg;
532 if (SID_TYPE(&cgd->inq_data) != T_CDROM
533 && SID_TYPE(&cgd->inq_data) != T_WORM)
537 * Don't complain if a valid peripheral is already attached.
539 periph = cam_periph_find(cgd->ccb_h.path, "cd");
540 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
544 * Allocate a peripheral instance for
545 * this device and start the probe
548 status = cam_periph_alloc(cdregister, cdoninvalidate,
550 "cd", CAM_PERIPH_BIO,
551 cgd->ccb_h.path, cdasync,
552 AC_FOUND_DEVICE, cgd);
554 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
555 kprintf("cdasync: Unable to attach new device "
556 "due to status 0x%x\n", status);
563 struct cd_softc *softc;
564 struct ccb_hdr *ccbh;
566 softc = (struct cd_softc *)periph->softc;
568 * Don't fail on the expected unit attention
571 softc->flags |= CD_FLAG_RETRY_UA;
572 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
573 ccbh->ccb_state |= CD_CCB_RETRY_UA;
577 cam_periph_async(periph, code, path, arg);
583 cdsysctlinit(void *context, int pending)
585 struct cam_periph *periph;
586 struct cd_softc *softc;
587 char tmpstr[80], tmpstr2[80];
589 periph = (struct cam_periph *)context;
590 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
594 softc = (struct cd_softc *)periph->softc;
595 ksnprintf(tmpstr, sizeof(tmpstr),
596 "CAM CD unit %d", periph->unit_number);
597 ksnprintf(tmpstr2, sizeof(tmpstr2),
598 "%d", periph->unit_number);
600 sysctl_ctx_free(&softc->sysctl_ctx);
601 sysctl_ctx_init(&softc->sysctl_ctx);
602 softc->flags |= CD_FLAG_SCTX_INIT;
603 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
604 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
605 tmpstr2, CTLFLAG_RD, 0, tmpstr);
607 if (softc->sysctl_tree == NULL) {
608 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
609 cam_periph_release(periph);
614 * Now register the sysctl handler, so the user can the value on
617 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
618 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
619 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
622 cam_periph_release(periph);
626 * We have a handler function for this so we can check the values when the
627 * user sets them, instead of every time we look at them.
630 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
634 value = *(int *)arg1;
636 error = sysctl_handle_int(oidp, &value, 0, req);
638 if ((error != 0) || (req->newptr == NULL))
642 * The only real values we can have here are 6 or 10. I don't
643 * really forsee having 12 be an option at any time in the future.
644 * So if the user sets something less than or equal to 6, we'll set
645 * it to 6. If he sets something greater than 6, we'll set it to 10.
647 * I suppose we could just return an error here for the wrong values,
648 * but I don't think it's necessary to do so, as long as we can
649 * determine the user's intent without too much trouble.
656 *(int *)arg1 = value;
663 cdregister(struct cam_periph *periph, void *arg)
665 struct cd_softc *softc;
666 struct ccb_pathinq *cpi;
667 struct ccb_getdev *cgd;
671 cgd = (struct ccb_getdev *)arg;
672 if (periph == NULL) {
673 kprintf("cdregister: periph was NULL!!\n");
674 return(CAM_REQ_CMP_ERR);
677 kprintf("cdregister: no getdev CCB, can't register device\n");
678 return(CAM_REQ_CMP_ERR);
681 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
682 sysctl_ctx_init(&softc->sysctl_ctx);
683 LIST_INIT(&softc->pending_ccbs);
684 STAILQ_INIT(&softc->mode_queue);
685 softc->state = CD_STATE_PROBE;
686 bioq_init(&softc->bio_queue);
687 if (SID_IS_REMOVABLE(&cgd->inq_data))
688 softc->flags |= CD_FLAG_DISC_REMOVABLE;
689 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
690 softc->flags |= CD_FLAG_TAGGED_QUEUING;
692 periph->softc = softc;
693 softc->periph = periph;
695 cam_extend_set(cdperiphs, periph->unit_number, periph);
698 * See if this device has any quirks.
700 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
701 (caddr_t)cd_quirk_table,
702 NELEM(cd_quirk_table),
703 sizeof(*cd_quirk_table), scsi_inquiry_match);
706 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
708 softc->quirks = CD_Q_NONE;
710 /* Check if the SIM does not want 6 byte commands */
711 cpi = &xpt_alloc_ccb()->cpi;
712 xpt_setup_ccb(&cpi->ccb_h, periph->path, /*priority*/1);
713 cpi->ccb_h.func_code = XPT_PATH_INQ;
714 xpt_action((union ccb *)cpi);
715 if (cpi->ccb_h.status == CAM_REQ_CMP && (cpi->hba_misc & PIM_NO_6_BYTE))
716 softc->quirks |= CD_Q_10_BYTE_ONLY;
717 xpt_free_ccb(&cpi->ccb_h);
719 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
721 /* The default is 6 byte commands, unless quirked otherwise */
722 if (softc->quirks & CD_Q_10_BYTE_ONLY)
723 softc->minimum_command_size = 10;
725 softc->minimum_command_size = 6;
728 * Load the user's default, if any.
730 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
731 periph->unit_number);
732 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
734 /* 6 and 10 are the only permissible values here. */
735 if (softc->minimum_command_size < 6)
736 softc->minimum_command_size = 6;
737 else if (softc->minimum_command_size > 6)
738 softc->minimum_command_size = 10;
741 * We need to register the statistics structure for this device,
742 * but we don't have the blocksize yet for it. So, we register
743 * the structure and indicate that we don't have the blocksize
744 * yet. Unlike other SCSI peripheral drivers, we explicitly set
745 * the device type here to be CDROM, rather than just ORing in
746 * the device type. This is because this driver can attach to either
747 * CDROM or WORM devices, and we want this peripheral driver to
748 * show up in the devstat list as a CD peripheral driver, not a
749 * WORM peripheral driver. WORM drives will also have the WORM
750 * driver attached to them.
752 cam_periph_unlock(periph);
753 devstat_add_entry(&softc->device_stats, "cd",
754 periph->unit_number, 0,
755 DEVSTAT_BS_UNAVAILABLE,
756 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
757 DEVSTAT_PRIORITY_CD);
758 disk_create(periph->unit_number, &softc->disk, &cd_ops);
759 disk_setdisktype(&softc->disk, "optical");
760 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
761 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
763 cam_periph_lock(periph);
766 * Add an async callback so that we get
767 * notified if this device goes away.
769 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
770 cdasync, periph, periph->path);
773 * If the target lun is greater than 0, we most likely have a CD
774 * changer device. Check the quirk entries as well, though, just
775 * in case someone has a CD tower with one lun per drive or
776 * something like that. Also, if we know up front that a
777 * particular device is a changer, we can mark it as such starting
778 * with lun 0, instead of lun 1. It shouldn't be necessary to have
779 * a quirk entry to define something as a changer, however.
781 if (((cgd->ccb_h.target_lun > 0)
782 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
783 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
784 struct cdchanger *nchanger;
785 struct cam_periph *nperiph;
786 struct cam_path *path;
790 /* Set the changer flag in the current device's softc */
791 softc->flags |= CD_FLAG_CHANGER;
794 * Now, look around for an existing changer device with the
795 * same path and target ID as the current device.
797 lockmgr(&changerq_lock, LK_EXCLUSIVE);
799 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
801 nchanger = STAILQ_NEXT(nchanger, changer_links)){
802 if ((nchanger->path_id == cgd->ccb_h.path_id)
803 && (nchanger->target_id == cgd->ccb_h.target_id)) {
808 lockmgr(&changerq_lock, LK_RELEASE);
811 * If we found a matching entry, just add this device to
812 * the list of devices on this changer.
815 struct chdevlist *chlunhead;
817 chlunhead = &nchanger->chluns;
820 * XXX KDM look at consolidating this code with the
821 * code below in a separate function.
825 * Create a path with lun id 0, and see if we can
826 * find a matching device
828 status = xpt_create_path(&path, /*periph*/ periph,
830 cgd->ccb_h.target_id, 0);
832 if ((status == CAM_REQ_CMP)
833 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
834 struct cd_softc *nsoftc;
836 nsoftc = (struct cd_softc *)nperiph->softc;
838 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
839 nsoftc->flags |= CD_FLAG_CHANGER;
840 nchanger->num_devices++;
841 if (camq_resize(&nchanger->devq,
842 nchanger->num_devices)!=CAM_REQ_CMP){
843 kprintf("cdregister: "
849 nsoftc->changer = nchanger;
850 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
852 STAILQ_INSERT_TAIL(&nchanger->chluns,
853 nsoftc,changer_links);
856 } else if (status == CAM_REQ_CMP)
859 kprintf("cdregister: unable to allocate path\n"
860 "cdregister: changer support may be "
864 nchanger->num_devices++;
866 softc->changer = nchanger;
867 softc->pinfo.index = CAM_UNQUEUED_INDEX;
869 if (camq_resize(&nchanger->devq,
870 nchanger->num_devices) != CAM_REQ_CMP) {
871 kprintf("cdregister: camq_resize "
872 "failed, changer support may "
876 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
879 * In this case, we don't already have an entry for this
880 * particular changer, so we need to create one, add it to
881 * the queue, and queue this device on the list for this
882 * changer. Before we queue this device, however, we need
883 * to search for lun id 0 on this target, and add it to the
884 * queue first, if it exists. (and if it hasn't already
885 * been marked as part of the changer.)
888 nchanger = kmalloc(sizeof(struct cdchanger),
889 M_DEVBUF, M_INTWAIT | M_ZERO);
890 callout_init_lk(&nchanger->short_handle,
892 callout_init_lk(&nchanger->long_handle,
894 if (camq_init(&nchanger->devq, 1) != 0) {
895 softc->flags &= ~CD_FLAG_CHANGER;
896 kprintf("cdregister: changer support "
901 nchanger->path_id = cgd->ccb_h.path_id;
902 nchanger->target_id = cgd->ccb_h.target_id;
904 /* this is superfluous, but it makes things clearer */
905 nchanger->num_devices = 0;
907 STAILQ_INIT(&nchanger->chluns);
909 callout_init_lk(&nchanger->long_handle,
911 callout_init_lk(&nchanger->short_handle,
914 lockmgr(&changerq_lock, LK_EXCLUSIVE);
916 STAILQ_INSERT_TAIL(&changerq, nchanger,
918 lockmgr(&changerq_lock, LK_RELEASE);
921 * Create a path with lun id 0, and see if we can
922 * find a matching device
924 status = xpt_create_path(&path, /*periph*/ periph,
926 cgd->ccb_h.target_id, 0);
929 * If we were able to allocate the path, and if we
930 * find a matching device and it isn't already
931 * marked as part of a changer, then we add it to
932 * the current changer.
934 if ((status == CAM_REQ_CMP)
935 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
936 && ((((struct cd_softc *)periph->softc)->flags &
937 CD_FLAG_CHANGER) == 0)) {
938 struct cd_softc *nsoftc;
940 nsoftc = (struct cd_softc *)nperiph->softc;
942 nsoftc->flags |= CD_FLAG_CHANGER;
943 nchanger->num_devices++;
944 if (camq_resize(&nchanger->devq,
945 nchanger->num_devices) != CAM_REQ_CMP) {
946 kprintf("cdregister: camq_resize "
947 "failed, changer support may "
950 nsoftc->changer = nchanger;
951 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
953 STAILQ_INSERT_TAIL(&nchanger->chluns,
954 nsoftc, changer_links);
956 } else if (status == CAM_REQ_CMP)
959 kprintf("cdregister: unable to allocate path\n"
960 "cdregister: changer support may be "
964 softc->changer = nchanger;
965 softc->pinfo.index = CAM_UNQUEUED_INDEX;
966 nchanger->num_devices++;
967 if (camq_resize(&nchanger->devq,
968 nchanger->num_devices) != CAM_REQ_CMP) {
969 kprintf("cdregister: camq_resize "
970 "failed, changer support may "
973 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
981 * Refcount and block open attempts until we are setup
984 cam_periph_hold(periph, 0);
986 if ((softc->flags & CD_FLAG_CHANGER) == 0)
987 xpt_schedule(periph, /*priority*/5);
989 cdschedule(periph, /*priority*/ 5);
995 cdopen(struct dev_open_args *ap)
997 cdev_t dev = ap->a_head.a_dev;
998 struct cam_periph *periph;
999 struct cd_softc *softc;
1003 periph = cam_extend_get(cdperiphs, unit);
1008 softc = (struct cd_softc *)periph->softc;
1010 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1013 cam_periph_lock(periph);
1015 if (softc->flags & CD_FLAG_INVALID) {
1016 cam_periph_unlock(periph);
1017 cam_periph_release(periph);
1021 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1022 cam_periph_unlock(periph);
1023 cam_periph_release(periph);
1027 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1028 if (softc->flags & CD_FLAG_OPEN)
1029 cam_periph_release(periph);
1031 softc->flags |= CD_FLAG_OPEN;
1034 * Check for media, and set the appropriate flags. We don't bail
1035 * if we don't have media, but then we don't allow anything but the
1036 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1038 * XXX KDM for now, we do fail the open if we don't have media. We
1039 * can change this once we've figured out how to make the slice
1040 * code work well with media changing underneath it.
1042 error = cdcheckmedia(periph);
1045 softc->flags |= CD_FLAG_CAP_MUTE;
1047 softc->flags &= ~CD_FLAG_CAP_MUTE;
1050 cdprevent(periph, PR_PREVENT);
1051 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1052 cam_periph_unhold(periph, 1);
1053 /* stays acquired */
1059 cdclose(struct dev_close_args *ap)
1061 cdev_t dev = ap->a_head.a_dev;
1062 struct cam_periph *periph;
1063 struct cd_softc *softc;
1064 struct disk_info *info;
1068 periph = cam_extend_get(cdperiphs, unit);
1072 softc = (struct cd_softc *)periph->softc;
1074 cam_periph_lock(periph);
1075 cam_periph_hold(periph, 0);
1077 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1078 cdprevent(periph, PR_ALLOW);
1081 * Unconditionally set the dsopen() flags back to their default
1084 info = &softc->disk.d_info;
1085 info->d_dsflags &= ~DSO_NOLABELS;
1086 info->d_dsflags |= DSO_COMPATLABEL;
1089 * Since we're closing this CD, mark the blocksize as unavailable.
1090 * It will be marked as available when the CD is opened again.
1092 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1095 * We'll check the media and toc again at the next open().
1097 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1099 cam_periph_unhold(periph, 1);
1100 cam_periph_release(periph);
1106 cdshorttimeout(void *arg)
1108 struct cdchanger *changer;
1110 changer = (struct cdchanger *)arg;
1112 /* Always clear the short timeout flag, since that's what we're in */
1113 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1116 * Check to see if there is any more pending or outstanding I/O for
1117 * this device. If not, move it out of the active slot.
1119 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1120 && (changer->cur_device->outstanding_cmds == 0)) {
1121 changer->flags |= CHANGER_MANUAL_CALL;
1122 cdrunchangerqueue(changer);
1127 * This is a wrapper for xpt_schedule. It only applies to changers.
1130 cdschedule(struct cam_periph *periph, int priority)
1132 struct cd_softc *softc;
1134 softc = (struct cd_softc *)periph->softc;
1137 * If this device isn't currently queued, and if it isn't
1138 * the active device, then we queue this device and run the
1139 * changer queue if there is no timeout scheduled to do it.
1140 * If this device is the active device, just schedule it
1141 * to run again. If this device is queued, there should be
1142 * a timeout in place already that will make sure it runs.
1144 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1145 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1147 * We don't do anything with the priority here.
1148 * This is strictly a fifo queue.
1150 softc->pinfo.priority = 1;
1151 softc->pinfo.generation = ++softc->changer->devq.generation;
1152 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1155 * Since we just put a device in the changer queue,
1156 * check and see if there is a timeout scheduled for
1157 * this changer. If so, let the timeout handle
1158 * switching this device into the active slot. If
1159 * not, manually call the timeout routine to
1162 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1163 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1164 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1165 softc->changer->flags |= CHANGER_MANUAL_CALL;
1166 cdrunchangerqueue(softc->changer);
1168 } else if ((softc->flags & CD_FLAG_ACTIVE)
1169 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1170 xpt_schedule(periph, priority);
1175 cdrunchangerqueue(void *arg)
1177 struct cd_softc *softc;
1178 struct cdchanger *changer;
1179 int called_from_timeout;
1181 changer = (struct cdchanger *)arg;
1184 * If we have NOT been called from cdstrategy() or cddone(), and
1185 * instead from a timeout routine, go ahead and clear the
1188 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1189 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1190 called_from_timeout = 1;
1192 called_from_timeout = 0;
1194 /* Always clear the manual call flag */
1195 changer->flags &= ~CHANGER_MANUAL_CALL;
1197 /* nothing to do if the queue is empty */
1198 if (changer->devq.entries <= 0) {
1203 * If the changer queue is frozen, that means we have an active
1206 if (changer->devq.qfrozen_cnt > 0) {
1209 * We always need to reset the frozen count and clear the
1212 changer->devq.qfrozen_cnt--;
1213 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1214 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1216 if (changer->cur_device->outstanding_cmds > 0) {
1217 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1218 changer->cur_device->bufs_left =
1219 changer->cur_device->outstanding_cmds;
1220 if (called_from_timeout) {
1221 callout_reset(&changer->long_handle,
1222 changer_max_busy_seconds * hz,
1223 cdrunchangerqueue, changer);
1224 changer->flags |= CHANGER_TIMEOUT_SCHED;
1230 * Check to see whether the current device has any I/O left
1231 * to do. If so, requeue it at the end of the queue. If
1232 * not, there is no need to requeue it.
1234 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1236 changer->cur_device->pinfo.generation =
1237 ++changer->devq.generation;
1238 camq_insert(&changer->devq,
1239 (cam_pinfo *)changer->cur_device);
1243 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1245 changer->cur_device = softc;
1247 changer->devq.qfrozen_cnt++;
1248 softc->flags |= CD_FLAG_ACTIVE;
1250 /* Just in case this device is waiting */
1251 wakeup(&softc->changer);
1252 xpt_schedule(softc->periph, /*priority*/ 1);
1255 * Get rid of any pending timeouts, and set a flag to schedule new
1256 * ones so this device gets its full time quantum.
1258 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1259 callout_stop(&changer->long_handle);
1260 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1263 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1264 callout_stop(&changer->short_handle);
1265 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1269 * We need to schedule timeouts, but we only do this after the
1270 * first transaction has completed. This eliminates the changer
1273 changer->flags |= CHANGER_NEED_TIMEOUT;
1277 cdchangerschedule(struct cd_softc *softc)
1279 struct cdchanger *changer;
1281 changer = softc->changer;
1284 * If this is a changer, and this is the current device,
1285 * and this device has at least the minimum time quantum to
1286 * run, see if we can switch it out.
1288 if ((softc->flags & CD_FLAG_ACTIVE)
1289 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1290 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1292 * We try three things here. The first is that we
1293 * check to see whether the schedule on completion
1294 * flag is set. If it is, we decrement the number
1295 * of buffers left, and if it's zero, we reschedule.
1296 * Next, we check to see whether the pending buffer
1297 * queue is empty and whether there are no
1298 * outstanding transactions. If so, we reschedule.
1299 * Next, we see if the pending buffer queue is empty.
1300 * If it is, we set the number of buffers left to
1301 * the current active buffer count and set the
1302 * schedule on complete flag.
1304 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1305 if (--softc->bufs_left == 0) {
1306 softc->changer->flags |=
1307 CHANGER_MANUAL_CALL;
1308 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1309 cdrunchangerqueue(softc->changer);
1311 } else if ((bioq_first(&softc->bio_queue) == NULL)
1312 && (softc->outstanding_cmds == 0)) {
1313 softc->changer->flags |= CHANGER_MANUAL_CALL;
1314 cdrunchangerqueue(softc->changer);
1316 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1317 && (softc->flags & CD_FLAG_ACTIVE)) {
1320 * Now that the first transaction to this
1321 * particular device has completed, we can go ahead
1322 * and schedule our timeouts.
1324 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1325 callout_reset(&changer->long_handle,
1326 changer_max_busy_seconds * hz,
1327 cdrunchangerqueue, changer);
1328 changer->flags |= CHANGER_TIMEOUT_SCHED;
1330 kprintf("cdchangerschedule: already have a long"
1333 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1334 callout_reset(&changer->short_handle,
1335 changer_min_busy_seconds * hz,
1336 cdshorttimeout, changer);
1337 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1339 kprintf("cdchangerschedule: already have a short "
1343 * We just scheduled timeouts, no need to schedule
1346 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1352 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1353 u_int32_t cam_flags,
1354 u_int32_t sense_flags),
1355 u_int32_t cam_flags, u_int32_t sense_flags)
1357 struct cd_softc *softc;
1358 struct cam_periph *periph;
1361 periph = xpt_path_periph(ccb->ccb_h.path);
1362 softc = (struct cd_softc *)periph->softc;
1364 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1365 &softc->device_stats);
1367 if (softc->flags & CD_FLAG_CHANGER)
1368 cdchangerschedule(softc);
1374 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1376 struct cd_softc *softc;
1377 static union ccb *ccb;
1379 softc = (struct cd_softc *)periph->softc;
1381 if (softc->flags & CD_FLAG_CHANGER) {
1383 * This should work the first time this device is woken up,
1384 * but just in case it doesn't, we use a while loop.
1386 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1388 * If this changer isn't already queued, queue it up.
1390 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1391 softc->pinfo.priority = 1;
1392 softc->pinfo.generation =
1393 ++softc->changer->devq.generation;
1394 camq_insert(&softc->changer->devq,
1395 (cam_pinfo *)softc);
1397 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1398 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1399 && ((softc->changer->flags
1400 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1401 softc->changer->flags |= CHANGER_MANUAL_CALL;
1402 cdrunchangerqueue(softc->changer);
1404 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1408 ccb = cam_periph_getccb(periph, priority);
1409 ccb->ccb_h.ccb_state = CD_CCB_POLLED;
1415 * Actually translate the requested transfer into one the physical driver
1416 * can understand. The transfer is described by a buf and will include
1417 * only one physical transfer.
1420 cdstrategy(struct dev_strategy_args *ap)
1422 cdev_t dev = ap->a_head.a_dev;
1423 struct bio *bio = ap->a_bio;
1424 struct buf *bp = bio->bio_buf;
1425 struct cam_periph *periph;
1426 struct cd_softc *softc;
1430 periph = cam_extend_get(cdperiphs, unit);
1431 if (periph == NULL) {
1432 bp->b_error = ENXIO;
1436 cam_periph_lock(periph);
1437 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1439 softc = (struct cd_softc *)periph->softc;
1442 * If the device has been made invalid, error out
1444 if ((softc->flags & CD_FLAG_INVALID)) {
1445 cam_periph_unlock(periph);
1446 bp->b_error = ENXIO;
1451 * If we don't have valid media, look for it before trying to
1454 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1457 error = cdcheckmedia(periph);
1459 cam_periph_unlock(periph);
1460 bp->b_error = error;
1466 * Place it in the queue of disk activities for this disk
1468 bioqdisksort(&softc->bio_queue, bio);
1471 * Schedule ourselves for performing the work. We do things
1472 * differently for changers.
1474 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1475 xpt_schedule(periph, /* XXX priority */1);
1477 cdschedule(periph, /* priority */ 1);
1479 cam_periph_unlock(periph);
1482 bp->b_flags |= B_ERROR;
1484 * Correctly set the buf to indicate a completed xfer
1486 bp->b_resid = bp->b_bcount;
1492 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1494 struct cd_softc *softc;
1497 struct ccb_scsiio *csio;
1498 struct scsi_read_capacity_data *rcap;
1500 softc = (struct cd_softc *)periph->softc;
1502 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1504 switch (softc->state) {
1505 case CD_STATE_NORMAL:
1507 bio = bioq_first(&softc->bio_queue);
1508 if (periph->immediate_priority <= periph->pinfo.priority) {
1509 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1511 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1513 periph->immediate_priority = CAM_PRIORITY_NONE;
1514 wakeup(&periph->ccb_list);
1515 } else if (bio == NULL) {
1516 xpt_release_ccb(start_ccb);
1519 bioq_remove(&softc->bio_queue, bio);
1521 devstat_start_transaction(&softc->device_stats);
1523 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1525 scsi_read_write(&start_ccb->csio,
1527 /* cbfcnp */ cddone,
1529 /* read */(bp->b_cmd == BUF_CMD_READ),
1531 /* minimum_cmd_size */ 10,
1533 bio->bio_offset / softc->params.blksize,
1534 bp->b_bcount / softc->params.blksize,
1535 /* data_ptr */ bp->b_data,
1536 /* dxfer_len */ bp->b_bcount,
1537 /* sense_len */ SSD_FULL_SIZE,
1538 /* timeout */ 30000);
1539 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1542 LIST_INSERT_HEAD(&softc->pending_ccbs,
1543 &start_ccb->ccb_h, periph_links.le);
1545 softc->outstanding_cmds++;
1546 /* We expect a unit attention from this device */
1547 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1548 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1549 softc->flags &= ~CD_FLAG_RETRY_UA;
1552 start_ccb->ccb_h.ccb_bio = bio;
1553 bio = bioq_first(&softc->bio_queue);
1555 xpt_action(start_ccb);
1558 /* Have more work to do, so ensure we stay scheduled */
1559 xpt_schedule(periph, /* XXX priority */1);
1563 case CD_STATE_PROBE:
1566 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1567 csio = &start_ccb->csio;
1568 scsi_read_capacity(csio,
1575 start_ccb->ccb_h.ccb_bio = NULL;
1576 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1577 xpt_action(start_ccb);
1584 cddone(struct cam_periph *periph, union ccb *done_ccb)
1586 struct cd_softc *softc;
1587 struct ccb_scsiio *csio;
1589 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1591 softc = (struct cd_softc *)periph->softc;
1592 csio = &done_ccb->csio;
1594 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1595 case CD_CCB_BUFFER_IO:
1601 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1605 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1608 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1613 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1614 if (error == ERESTART) {
1616 * A retry was scheuled, so
1627 xpt_print(periph->path,
1628 "cddone: got error %#x back\n", error);
1629 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
1630 q_bp = q_bio->bio_buf;
1631 q_bp->b_resid = q_bp->b_bcount;
1632 q_bp->b_error = EIO;
1633 q_bp->b_flags |= B_ERROR;
1636 bp->b_resid = bp->b_bcount;
1637 bp->b_error = error;
1638 bp->b_flags |= B_ERROR;
1639 cam_release_devq(done_ccb->ccb_h.path,
1643 /*getcount_only*/0);
1646 bp->b_resid = csio->resid;
1648 if (bp->b_resid != 0) {
1649 /* Short transfer ??? */
1650 bp->b_flags |= B_ERROR;
1654 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1655 softc->outstanding_cmds--;
1657 if (softc->flags & CD_FLAG_CHANGER)
1658 cdchangerschedule(softc);
1660 devstat_end_transaction_buf(&softc->device_stats, bp);
1667 * Currently (9/30/97) the longest possible announce
1668 * buffer is 108 bytes, for the first error case below.
1669 * That is 39 bytes for the basic string, 16 bytes for the
1670 * biggest sense key (hardware error), 52 bytes for the
1671 * text of the largest sense qualifier valid for a CDROM,
1672 * (0x72, 0x03 or 0x04, 0x03), and one byte for the
1673 * null terminating character. To allow for longer strings,
1674 * the announce buffer is 120 bytes.
1676 * We need to call disk_setdiskinfo() before the disk
1677 * subsystem will allow any opens. Because additional
1678 * probe code is in cdopen() we do this now whether a CD
1679 * is present or not.
1681 struct scsi_read_capacity_data *rdcap;
1682 struct disk_info info;
1683 char announce_buf[120];
1684 struct cd_params *cdp;
1687 cdp = &softc->params;
1688 bzero(&info, sizeof(info));
1689 info.d_type = DTYPE_SCSI;
1690 info.d_dsflags &= ~DSO_COMPATLABEL;
1691 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
1692 info.d_serialno = xpt_path_serialno(periph->path);
1694 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1696 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1697 cdp->blksize = scsi_4btoul (rdcap->length);
1699 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1700 ksnprintf(announce_buf, sizeof(announce_buf),
1701 "cd present [%lu x %lu byte records]",
1702 cdp->disksize, (u_long)cdp->blksize);
1703 info.d_media_blksize = cdp->blksize;
1704 info.d_media_blocks = cdp->disksize;
1710 * Retry any UNIT ATTENTION type errors. They
1711 * are expected at boot.
1713 error = cderror(done_ccb, CAM_RETRY_SELTO,
1714 SF_RETRY_UA | SF_NO_PRINT);
1715 if (error == ERESTART) {
1717 * A retry was scheuled, so
1721 } else if (error != 0) {
1722 struct scsi_sense_data *sense;
1724 int sense_key, error_code;
1727 struct ccb_getdev *cgd;
1729 /* Don't wedge this device's queue */
1730 cam_release_devq(done_ccb->ccb_h.path,
1734 /*getcount_only*/0);
1736 status = done_ccb->ccb_h.status;
1738 cgd = &xpt_alloc_ccb()->cgd;
1739 xpt_setup_ccb(&cgd->ccb_h,
1740 done_ccb->ccb_h.path,
1742 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1743 xpt_action((union ccb *)cgd);
1745 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1746 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1747 || ((status & CAM_AUTOSNS_VALID) == 0))
1753 sense = &csio->sense_data;
1754 scsi_extract_sense(sense, &error_code,
1759 * Attach to anything that claims to be a
1760 * CDROM or WORM device, as long as it
1761 * doesn't return a "Logical unit not
1762 * supported" (0x25) error.
1764 if ((have_sense) && (asc != 0x25)
1765 && (error_code == SSD_CURRENT_ERROR)) {
1766 const char *sense_key_desc;
1767 const char *asc_desc;
1769 scsi_sense_desc(sense_key, asc, ascq,
1773 ksnprintf(announce_buf,
1774 sizeof(announce_buf),
1775 "Attempt to query device "
1776 "size failed: %s, %s",
1779 info.d_media_blksize = 2048;
1781 } else if (SID_TYPE(&cgd->inq_data) == T_CDROM) {
1783 * We only print out an error for
1784 * CDROM type devices. For WORM
1785 * devices, we don't print out an
1786 * error since a few WORM devices
1787 * don't support CDROM commands.
1788 * If we have sense information, go
1789 * ahead and print it out.
1790 * Otherwise, just say that we
1795 * Just print out the error, not
1796 * the full probe message, when we
1803 xpt_print(periph->path,
1804 "got CAM status %#x\n",
1805 done_ccb->ccb_h.status);
1807 xpt_print(periph->path, "fatal error, "
1808 "failed to attach to device\n");
1810 * Invalidate this peripheral.
1812 cam_periph_invalidate(periph);
1814 announce_buf[0] = '\0';
1818 * Invalidate this peripheral.
1820 cam_periph_invalidate(periph);
1821 announce_buf[0] = '\0';
1823 xpt_free_ccb(&cgd->ccb_h);
1826 kfree(rdcap, M_SCSICD);
1827 if (announce_buf[0] != '\0') {
1828 xpt_announce_periph(periph, announce_buf);
1829 if (softc->flags & CD_FLAG_CHANGER)
1830 cdchangerschedule(softc);
1832 * Create our sysctl variables, now that we know
1833 * we have successfully attached.
1835 taskqueue_enqueue(taskqueue_thread[mycpuid],
1836 &softc->sysctl_task);
1838 softc->state = CD_STATE_NORMAL;
1840 * Since our peripheral may be invalidated by an error
1841 * above or an external event, we must release our CCB
1842 * before releasing the probe lock on the peripheral.
1843 * The peripheral will only go away once the last lock
1844 * is removed, and we need it around for the CCB release
1847 xpt_release_ccb(done_ccb);
1848 cam_periph_unhold(periph, 0);
1850 disk_setdiskinfo(&softc->disk, &info);
1854 case CD_CCB_WAITING:
1856 /* Caller will release the CCB */
1857 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1858 ("trying to wakeup ccbwait\n"));
1860 wakeup(&done_ccb->ccb_h.cbfcnp);
1864 /* Caller releases CCB */
1865 wakeup(&done_ccb->ccb_h.cbfcnp);
1870 xpt_release_ccb(done_ccb);
1873 static union cd_pages *
1874 cdgetpage(struct cd_mode_params *mode_params)
1876 union cd_pages *page;
1878 if (mode_params->cdb_size == 10)
1879 page = (union cd_pages *)find_mode_page_10(
1880 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1882 page = (union cd_pages *)find_mode_page_6(
1883 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1889 cdgetpagesize(int page_num)
1893 for (i = 0; i < NELEM(cd_page_size_table); i++) {
1894 if (cd_page_size_table[i].page == page_num)
1895 return (cd_page_size_table[i].page_size);
1901 cdioctl(struct dev_ioctl_args *ap)
1903 cdev_t dev = ap->a_head.a_dev;
1904 caddr_t addr = ap->a_data;
1905 struct cam_periph *periph;
1906 struct cd_softc *softc;
1907 int unit, error = 0;
1911 periph = cam_extend_get(cdperiphs, unit);
1915 cam_periph_lock(periph);
1916 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1918 softc = (struct cd_softc *)periph->softc;
1920 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1921 ("trying to do ioctl %#lx\n", ap->a_cmd));
1923 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1924 cam_periph_unlock(periph);
1925 cam_periph_release(periph);
1930 * If we don't have media loaded, check for it. If still don't
1931 * have media loaded, we can only do a load or eject.
1933 * We only care whether media is loaded if this is a cd-specific ioctl
1934 * (thus the IOCGROUP check below). Note that this will break if
1935 * anyone adds any ioctls into the switch statement below that don't
1936 * have their ioctl group set to 'c'.
1938 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1939 && ((ap->a_cmd != CDIOCCLOSE)
1940 && (ap->a_cmd != CDIOCEJECT))
1941 && (IOCGROUP(ap->a_cmd) == 'c')) {
1942 error = cdcheckmedia(periph);
1944 cam_periph_unhold(periph, 1);
1949 * Drop the lock here so later mallocs can use WAITOK. The periph
1950 * is essentially locked still with the cam_periph_hold call above.
1952 cam_periph_unlock(periph);
1954 switch (ap->a_cmd) {
1956 case CDIOCPLAYTRACKS:
1958 struct ioc_play_track *args
1959 = (struct ioc_play_track *)addr;
1960 struct cd_mode_params params;
1961 union cd_pages *page;
1963 params.alloc_len = sizeof(union cd_mode_data_6_10);
1964 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1967 cam_periph_lock(periph);
1968 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1969 ("trying to do CDIOCPLAYTRACKS\n"));
1971 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1973 kfree(params.mode_buf, M_SCSICD);
1974 cam_periph_unlock(periph);
1977 page = cdgetpage(¶ms);
1979 page->audio.flags &= ~CD_PA_SOTC;
1980 page->audio.flags |= CD_PA_IMMED;
1981 error = cdsetmode(periph, ¶ms);
1982 kfree(params.mode_buf, M_SCSICD);
1984 cam_periph_unlock(periph);
1989 * This was originally implemented with the PLAY
1990 * AUDIO TRACK INDEX command, but that command was
1991 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1992 * drives support it but ATAPI and ATAPI-derivative
1993 * drives don't seem to support it. So we keep a
1994 * cache of the table of contents and translate
1995 * track numbers to MSF format.
1997 if (softc->flags & CD_FLAG_VALID_TOC) {
1998 union msf_lba *sentry, *eentry;
2001 if (args->end_track <
2002 softc->toc.header.ending_track + 1)
2004 if (args->end_track >
2005 softc->toc.header.ending_track + 1)
2007 softc->toc.header.ending_track + 1;
2008 st = args->start_track -
2009 softc->toc.header.starting_track;
2010 et = args->end_track -
2011 softc->toc.header.starting_track;
2014 || (st > (softc->toc.header.ending_track -
2015 softc->toc.header.starting_track))) {
2019 sentry = &softc->toc.entries[st].addr;
2020 eentry = &softc->toc.entries[et].addr;
2021 error = cdplaymsf(periph,
2030 * If we don't have a valid TOC, try the
2031 * play track index command. It is part of
2032 * the SCSI-2 spec, but was removed in the
2033 * MMC specs. ATAPI and ATAPI-derived
2034 * drives don't support it.
2036 if (softc->quirks & CD_Q_BCD_TRACKS) {
2038 bin2bcd(args->start_track);
2040 bin2bcd(args->end_track);
2042 error = cdplaytracks(periph,
2048 cam_periph_unlock(periph);
2053 struct ioc_play_msf *args
2054 = (struct ioc_play_msf *) addr;
2055 struct cd_mode_params params;
2056 union cd_pages *page;
2058 params.alloc_len = sizeof(union cd_mode_data_6_10);
2059 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2062 cam_periph_lock(periph);
2063 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2064 ("trying to do CDIOCPLAYMSF\n"));
2066 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2068 kfree(params.mode_buf, M_SCSICD);
2069 cam_periph_unlock(periph);
2072 page = cdgetpage(¶ms);
2074 page->audio.flags &= ~CD_PA_SOTC;
2075 page->audio.flags |= CD_PA_IMMED;
2076 error = cdsetmode(periph, ¶ms);
2077 kfree(params.mode_buf, M_SCSICD);
2079 cam_periph_unlock(periph);
2082 error = cdplaymsf(periph,
2089 cam_periph_unlock(periph);
2092 case CDIOCPLAYBLOCKS:
2094 struct ioc_play_blocks *args
2095 = (struct ioc_play_blocks *) addr;
2096 struct cd_mode_params params;
2097 union cd_pages *page;
2099 params.alloc_len = sizeof(union cd_mode_data_6_10);
2100 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2102 cam_periph_lock(periph);
2103 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2104 ("trying to do CDIOCPLAYBLOCKS\n"));
2107 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2109 kfree(params.mode_buf, M_SCSICD);
2110 cam_periph_unlock(periph);
2113 page = cdgetpage(¶ms);
2115 page->audio.flags &= ~CD_PA_SOTC;
2116 page->audio.flags |= CD_PA_IMMED;
2117 error = cdsetmode(periph, ¶ms);
2118 kfree(params.mode_buf, M_SCSICD);
2120 cam_periph_unlock(periph);
2123 error = cdplay(periph, args->blk, args->len);
2124 cam_periph_unlock(periph);
2127 case CDIOCREADSUBCHANNEL:
2129 struct ioc_read_subchannel *args
2130 = (struct ioc_read_subchannel *) addr;
2131 struct cd_sub_channel_info *data;
2132 u_int32_t len = args->data_len;
2134 data = kmalloc(sizeof(struct cd_sub_channel_info),
2135 M_SCSICD, M_WAITOK);
2137 cam_periph_lock(periph);
2138 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2139 ("trying to do CDIOCREADSUBCHANNEL\n"));
2141 if ((len > sizeof(struct cd_sub_channel_info)) ||
2142 (len < sizeof(struct cd_sub_channel_header))) {
2144 "scsi_cd: cdioctl: "
2145 "cdioreadsubchannel: error, len=%d\n",
2148 kfree(data, M_SCSICD);
2149 cam_periph_unlock(periph);
2153 if (softc->quirks & CD_Q_BCD_TRACKS)
2154 args->track = bin2bcd(args->track);
2156 error = cdreadsubchannel(periph, args->address_format,
2157 args->data_format, args->track, data, len);
2160 kfree(data, M_SCSICD);
2161 cam_periph_unlock(periph);
2164 if (softc->quirks & CD_Q_BCD_TRACKS)
2165 data->what.track_info.track_number =
2166 bcd2bin(data->what.track_info.track_number);
2167 len = min(len, ((data->header.data_len[0] << 8) +
2168 data->header.data_len[1] +
2169 sizeof(struct cd_sub_channel_header)));
2170 cam_periph_unlock(periph);
2171 if (copyout(data, args->data, len) != 0) {
2174 kfree(data, M_SCSICD);
2178 case CDIOREADTOCHEADER:
2180 struct ioc_toc_header *th;
2182 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2185 cam_periph_lock(periph);
2186 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2187 ("trying to do CDIOREADTOCHEADER\n"));
2189 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2190 sizeof (*th), /*sense_flags*/0);
2192 kfree(th, M_SCSICD);
2193 cam_periph_unlock(periph);
2196 if (softc->quirks & CD_Q_BCD_TRACKS) {
2197 /* we are going to have to convert the BCD
2198 * encoding on the cd to what is expected
2200 th->starting_track =
2201 bcd2bin(th->starting_track);
2202 th->ending_track = bcd2bin(th->ending_track);
2204 th->len = ntohs(th->len);
2205 bcopy(th, addr, sizeof(*th));
2206 kfree(th, M_SCSICD);
2207 cam_periph_unlock(periph);
2210 case CDIOREADTOCENTRYS:
2212 struct cd_tocdata *data;
2213 struct cd_toc_single *lead;
2214 struct ioc_read_toc_entry *te =
2215 (struct ioc_read_toc_entry *) addr;
2216 struct ioc_toc_header *th;
2217 u_int32_t len, readlen, idx, num;
2218 u_int32_t starting_track = te->starting_track;
2220 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2221 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2223 cam_periph_lock(periph);
2224 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2225 ("trying to do CDIOREADTOCENTRYS\n"));
2227 if (te->data_len < sizeof(struct cd_toc_entry)
2228 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2229 || (te->address_format != CD_MSF_FORMAT
2230 && te->address_format != CD_LBA_FORMAT)) {
2232 kprintf("scsi_cd: error in readtocentries, "
2233 "returning EINVAL\n");
2234 kfree(data, M_SCSICD);
2235 kfree(lead, M_SCSICD);
2236 cam_periph_unlock(periph);
2241 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2242 sizeof (*th), /*sense_flags*/0);
2244 kfree(data, M_SCSICD);
2245 kfree(lead, M_SCSICD);
2246 cam_periph_unlock(periph);
2250 if (softc->quirks & CD_Q_BCD_TRACKS) {
2251 /* we are going to have to convert the BCD
2252 * encoding on the cd to what is expected
2254 th->starting_track =
2255 bcd2bin(th->starting_track);
2256 th->ending_track = bcd2bin(th->ending_track);
2259 if (starting_track == 0)
2260 starting_track = th->starting_track;
2261 else if (starting_track == LEADOUT)
2262 starting_track = th->ending_track + 1;
2263 else if (starting_track < th->starting_track ||
2264 starting_track > th->ending_track + 1) {
2265 kprintf("scsi_cd: error in readtocentries, "
2266 "returning EINVAL\n");
2267 kfree(data, M_SCSICD);
2268 kfree(lead, M_SCSICD);
2269 cam_periph_unlock(periph);
2274 /* calculate reading length without leadout entry */
2275 readlen = (th->ending_track - starting_track + 1) *
2276 sizeof(struct cd_toc_entry);
2278 /* and with leadout entry */
2279 len = readlen + sizeof(struct cd_toc_entry);
2280 if (te->data_len < len) {
2285 if (len > sizeof(data->entries)) {
2286 kprintf("scsi_cd: error in readtocentries, "
2287 "returning EINVAL\n");
2289 kfree(data, M_SCSICD);
2290 kfree(lead, M_SCSICD);
2291 cam_periph_unlock(periph);
2294 num = len / sizeof(struct cd_toc_entry);
2297 error = cdreadtoc(periph, te->address_format,
2300 readlen + sizeof (*th),
2303 kfree(data, M_SCSICD);
2304 kfree(lead, M_SCSICD);
2305 cam_periph_unlock(periph);
2310 /* make leadout entry if needed */
2311 idx = starting_track + num - 1;
2312 if (softc->quirks & CD_Q_BCD_TRACKS)
2313 th->ending_track = bcd2bin(th->ending_track);
2314 if (idx == th->ending_track + 1) {
2315 error = cdreadtoc(periph, te->address_format,
2316 LEADOUT, (u_int8_t *)lead,
2320 kfree(data, M_SCSICD);
2321 kfree(lead, M_SCSICD);
2322 cam_periph_unlock(periph);
2325 data->entries[idx - starting_track] =
2328 if (softc->quirks & CD_Q_BCD_TRACKS) {
2329 for (idx = 0; idx < num - 1; idx++) {
2330 data->entries[idx].track =
2331 bcd2bin(data->entries[idx].track);
2335 cam_periph_unlock(periph);
2336 error = copyout(data->entries, te->data, len);
2337 kfree(data, M_SCSICD);
2338 kfree(lead, M_SCSICD);
2341 case CDIOREADTOCENTRY:
2343 struct cd_toc_single *data;
2344 struct ioc_read_toc_single_entry *te =
2345 (struct ioc_read_toc_single_entry *) addr;
2346 struct ioc_toc_header *th;
2349 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2351 cam_periph_lock(periph);
2352 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2353 ("trying to do CDIOREADTOCENTRY\n"));
2355 if (te->address_format != CD_MSF_FORMAT
2356 && te->address_format != CD_LBA_FORMAT) {
2357 kprintf("error in readtocentry, "
2358 " returning EINVAL\n");
2359 kfree(data, M_SCSICD);
2361 cam_periph_unlock(periph);
2366 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2367 sizeof (*th), /*sense_flags*/0);
2369 kfree(data, M_SCSICD);
2370 cam_periph_unlock(periph);
2374 if (softc->quirks & CD_Q_BCD_TRACKS) {
2375 /* we are going to have to convert the BCD
2376 * encoding on the cd to what is expected
2378 th->starting_track =
2379 bcd2bin(th->starting_track);
2380 th->ending_track = bcd2bin(th->ending_track);
2384 track = th->starting_track;
2385 else if (track == LEADOUT)
2387 else if (track < th->starting_track ||
2388 track > th->ending_track + 1) {
2389 kprintf("error in readtocentry, "
2390 " returning EINVAL\n");
2391 kfree(data, M_SCSICD);
2393 cam_periph_unlock(periph);
2397 error = cdreadtoc(periph, te->address_format, track,
2398 (u_int8_t *)data, sizeof(*data),
2401 kfree(data, M_SCSICD);
2402 cam_periph_unlock(periph);
2406 if (softc->quirks & CD_Q_BCD_TRACKS)
2407 data->entry.track = bcd2bin(data->entry.track);
2408 bcopy(&data->entry, &te->entry,
2409 sizeof(struct cd_toc_entry));
2410 kfree(data, M_SCSICD);
2411 cam_periph_unlock(periph);
2416 struct ioc_patch *arg = (struct ioc_patch *)addr;
2417 struct cd_mode_params params;
2418 union cd_pages *page;
2420 params.alloc_len = sizeof(union cd_mode_data_6_10);
2421 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2424 cam_periph_lock(periph);
2425 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2426 ("trying to do CDIOCSETPATCH\n"));
2428 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2430 kfree(params.mode_buf, M_SCSICD);
2431 cam_periph_unlock(periph);
2434 page = cdgetpage(¶ms);
2436 page->audio.port[LEFT_PORT].channels =
2438 page->audio.port[RIGHT_PORT].channels =
2440 page->audio.port[2].channels = arg->patch[2];
2441 page->audio.port[3].channels = arg->patch[3];
2442 error = cdsetmode(periph, ¶ms);
2443 kfree(params.mode_buf, M_SCSICD);
2444 cam_periph_unlock(periph);
2449 struct ioc_vol *arg = (struct ioc_vol *) addr;
2450 struct cd_mode_params params;
2451 union cd_pages *page;
2453 params.alloc_len = sizeof(union cd_mode_data_6_10);
2454 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2457 cam_periph_lock(periph);
2458 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2459 ("trying to do CDIOCGETVOL\n"));
2461 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2463 kfree(params.mode_buf, M_SCSICD);
2464 cam_periph_unlock(periph);
2467 page = cdgetpage(¶ms);
2469 arg->vol[LEFT_PORT] =
2470 page->audio.port[LEFT_PORT].volume;
2471 arg->vol[RIGHT_PORT] =
2472 page->audio.port[RIGHT_PORT].volume;
2473 arg->vol[2] = page->audio.port[2].volume;
2474 arg->vol[3] = page->audio.port[3].volume;
2475 kfree(params.mode_buf, M_SCSICD);
2476 cam_periph_unlock(periph);
2481 struct ioc_vol *arg = (struct ioc_vol *) 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 = kmalloc(params.alloc_len, M_SCSICD,
2489 cam_periph_lock(periph);
2490 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2491 ("trying to do CDIOCSETVOL\n"));
2493 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2495 kfree(params.mode_buf, M_SCSICD);
2496 cam_periph_unlock(periph);
2499 page = cdgetpage(¶ms);
2501 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2502 page->audio.port[LEFT_PORT].volume =
2503 arg->vol[LEFT_PORT];
2504 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2505 page->audio.port[RIGHT_PORT].volume =
2506 arg->vol[RIGHT_PORT];
2507 page->audio.port[2].volume = arg->vol[2];
2508 page->audio.port[3].volume = arg->vol[3];
2509 error = cdsetmode(periph, ¶ms);
2510 cam_periph_unlock(periph);
2511 kfree(params.mode_buf, M_SCSICD);
2516 struct cd_mode_params params;
2517 union cd_pages *page;
2519 params.alloc_len = sizeof(union cd_mode_data_6_10);
2520 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2523 cam_periph_lock(periph);
2524 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2525 ("trying to do CDIOCSETMONO\n"));
2527 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2529 kfree(params.mode_buf, M_SCSICD);
2530 cam_periph_unlock(periph);
2533 page = cdgetpage(¶ms);
2535 page->audio.port[LEFT_PORT].channels =
2536 LEFT_CHANNEL | RIGHT_CHANNEL;
2537 page->audio.port[RIGHT_PORT].channels =
2538 LEFT_CHANNEL | RIGHT_CHANNEL;
2539 page->audio.port[2].channels = 0;
2540 page->audio.port[3].channels = 0;
2541 error = cdsetmode(periph, ¶ms);
2542 cam_periph_unlock(periph);
2543 kfree(params.mode_buf, M_SCSICD);
2546 case CDIOCSETSTEREO:
2548 struct cd_mode_params params;
2549 union cd_pages *page;
2551 params.alloc_len = sizeof(union cd_mode_data_6_10);
2552 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2555 cam_periph_lock(periph);
2556 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2557 ("trying to do CDIOCSETSTEREO\n"));
2559 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2561 kfree(params.mode_buf, M_SCSICD);
2562 cam_periph_unlock(periph);
2565 page = cdgetpage(¶ms);
2567 page->audio.port[LEFT_PORT].channels =
2569 page->audio.port[RIGHT_PORT].channels =
2571 page->audio.port[2].channels = 0;
2572 page->audio.port[3].channels = 0;
2573 error = cdsetmode(periph, ¶ms);
2574 kfree(params.mode_buf, M_SCSICD);
2575 cam_periph_unlock(periph);
2580 struct cd_mode_params params;
2581 union cd_pages *page;
2583 params.alloc_len = sizeof(union cd_mode_data_6_10);
2584 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2587 cam_periph_lock(periph);
2588 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2589 ("trying to do CDIOCSETMUTE\n"));
2591 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2593 kfree(¶ms, M_SCSICD);
2594 cam_periph_unlock(periph);
2597 page = cdgetpage(¶ms);
2599 page->audio.port[LEFT_PORT].channels = 0;
2600 page->audio.port[RIGHT_PORT].channels = 0;
2601 page->audio.port[2].channels = 0;
2602 page->audio.port[3].channels = 0;
2603 error = cdsetmode(periph, ¶ms);
2604 kfree(params.mode_buf, M_SCSICD);
2605 cam_periph_unlock(periph);
2610 struct cd_mode_params params;
2611 union cd_pages *page;
2613 params.alloc_len = sizeof(union cd_mode_data_6_10);
2614 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2617 cam_periph_lock(periph);
2618 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2619 ("trying to do CDIOCSETLEFT\n"));
2621 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2623 kfree(params.mode_buf, M_SCSICD);
2624 cam_periph_unlock(periph);
2627 page = cdgetpage(¶ms);
2629 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2630 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2631 page->audio.port[2].channels = 0;
2632 page->audio.port[3].channels = 0;
2633 error = cdsetmode(periph, ¶ms);
2634 kfree(params.mode_buf, M_SCSICD);
2635 cam_periph_unlock(periph);
2640 struct cd_mode_params params;
2641 union cd_pages *page;
2643 params.alloc_len = sizeof(union cd_mode_data_6_10);
2644 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2647 cam_periph_lock(periph);
2648 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2649 ("trying to do CDIOCSETRIGHT\n"));
2651 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2653 kfree(params.mode_buf, M_SCSICD);
2654 cam_periph_unlock(periph);
2657 page = cdgetpage(¶ms);
2659 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2660 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2661 page->audio.port[2].channels = 0;
2662 page->audio.port[3].channels = 0;
2663 error = cdsetmode(periph, ¶ms);
2664 kfree(params.mode_buf, M_SCSICD);
2665 cam_periph_unlock(periph);
2669 cam_periph_lock(periph);
2670 error = cdpause(periph, 1);
2671 cam_periph_unlock(periph);
2674 cam_periph_lock(periph);
2675 error = cdpause(periph, 0);
2676 cam_periph_unlock(periph);
2679 cam_periph_lock(periph);
2680 error = cdstartunit(periph, 0);
2681 cam_periph_unlock(periph);
2684 cam_periph_lock(periph);
2685 error = cdstartunit(periph, 1);
2686 cam_periph_unlock(periph);
2689 cam_periph_lock(periph);
2690 error = cdstopunit(periph, 0);
2691 cam_periph_unlock(periph);
2694 cam_periph_lock(periph);
2695 error = cdstopunit(periph, 1);
2696 cam_periph_unlock(periph);
2699 cam_periph_lock(periph);
2700 cdprevent(periph, PR_ALLOW);
2701 cam_periph_unlock(periph);
2704 cam_periph_lock(periph);
2705 cdprevent(periph, PR_PREVENT);
2706 cam_periph_unlock(periph);
2709 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2713 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2717 /* return (cd_reset(periph)); */
2720 case CDRIOCREADSPEED:
2721 cam_periph_lock(periph);
2722 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2723 cam_periph_unlock(periph);
2725 case CDRIOCWRITESPEED:
2726 cam_periph_lock(periph);
2727 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2728 cam_periph_unlock(periph);
2731 case DVDIOCREPORTKEY: {
2732 struct dvd_authinfo *authinfo;
2734 authinfo = (struct dvd_authinfo *)addr;
2736 cam_periph_lock(periph);
2737 if (ap->a_cmd == DVDIOCREPORTKEY)
2738 error = cdreportkey(periph, authinfo);
2740 error = cdsendkey(periph, authinfo);
2741 cam_periph_unlock(periph);
2744 case DVDIOCREADSTRUCTURE: {
2745 struct dvd_struct *dvdstruct;
2747 dvdstruct = (struct dvd_struct *)addr;
2749 cam_periph_lock(periph);
2750 error = cdreaddvdstructure(periph, dvdstruct);
2751 cam_periph_unlock(periph);
2756 cam_periph_lock(periph);
2757 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2758 cam_periph_unlock(periph);
2762 cam_periph_lock(periph);
2763 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2764 cam_periph_unhold(periph, 1);
2770 cdprevent(struct cam_periph *periph, int action)
2773 struct cd_softc *softc;
2776 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2778 softc = (struct cd_softc *)periph->softc;
2780 if (((action == PR_ALLOW)
2781 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2782 || ((action == PR_PREVENT)
2783 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2787 ccb = cdgetccb(periph, /* priority */ 1);
2789 scsi_prevent(&ccb->csio,
2795 /* timeout */60000);
2797 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2798 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2800 xpt_release_ccb(ccb);
2803 if (action == PR_ALLOW)
2804 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2806 softc->flags |= CD_FLAG_DISC_LOCKED;
2811 cdcheckmedia(struct cam_periph *periph)
2813 struct cd_softc *softc;
2814 struct ioc_toc_header *toch;
2815 struct cd_toc_single leadout;
2816 struct ccb_getdev *cgd;
2817 u_int32_t size, toclen;
2818 int error, num_entries, cdindex;
2819 int first_track_audio;
2820 struct disk_info info;
2822 softc = (struct cd_softc *)periph->softc;
2824 first_track_audio = -1;
2828 * Set up disk info fields and tell the disk subsystem to reset
2829 * its internal copy of the label.
2831 bzero(&info, sizeof(info));
2832 info.d_type = DTYPE_SCSI;
2833 info.d_dsflags &= ~DSO_COMPATLABEL;
2834 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2835 info.d_serialno = xpt_path_serialno(periph->path);
2838 * Grab the inquiry data to get the vendor and product names.
2839 * Put them in the typename and packname for the label.
2841 cgd = &xpt_alloc_ccb()->cgd;
2842 xpt_setup_ccb(&cgd->ccb_h, periph->path, /*priority*/ 1);
2843 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2844 xpt_action((union ccb *)cgd);
2847 strncpy(label->d_typename, cgd->inq_data.vendor,
2848 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2849 strncpy(label->d_packname, cgd->inq_data.product,
2850 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2852 xpt_free_ccb(&cgd->ccb_h);
2855 * Clear the valid media and TOC flags until we've verified that we
2858 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2861 * Get the disc size and block size. If we can't get it, we don't
2862 * have media, most likely.
2864 if ((error = cdsize(periph, &size)) != 0) {
2866 * Set a bogus sector size, so the slice code won't try to
2867 * divide by 0 and panic the kernel.
2869 info.d_media_blksize = 2048;
2870 disk_setdiskinfo(&softc->disk, &info);
2873 * Tell devstat(9) we don't have a blocksize.
2875 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2879 info.d_media_blksize = softc->params.blksize;
2880 info.d_media_blocks = softc->params.disksize;
2881 disk_setdiskinfo(&softc->disk, &info);
2884 * We unconditionally (re)set the blocksize each time the
2885 * CD device is opened. This is because the CD can change,
2886 * and therefore the blocksize might change.
2887 * XXX problems here if some slice or partition is still
2888 * open with the old size?
2890 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2891 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2892 softc->device_stats.block_size = softc->params.blksize;
2894 softc->flags |= CD_FLAG_VALID_MEDIA;
2898 * Now we check the table of contents. This (currently) is only
2899 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2900 * things like present a separate entry in /dev for each track,
2901 * like that acd(4) driver does.
2903 bzero(&softc->toc, sizeof(softc->toc));
2904 toch = &softc->toc.header;
2907 * We will get errors here for media that doesn't have a table of
2908 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2909 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2910 * has not been recorded (no complete session) and the Format codes
2911 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2912 * with an INVALID FIELD IN CDB. Devices that are not capable of
2913 * reading an incomplete session on DDC/CD-R/RW media shall report
2914 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2916 * So this isn't fatal if we can't read the table of contents, it
2917 * just means that the user won't be able to issue the play tracks
2918 * ioctl, and likely lots of other stuff won't work either. They
2919 * need to burn the CD before we can do a whole lot with it. So
2920 * we don't print anything here if we get an error back.
2922 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2925 * Errors in reading the table of contents aren't fatal, we just
2926 * won't have a valid table of contents cached.
2930 bzero(&softc->toc, sizeof(softc->toc));
2934 if (softc->quirks & CD_Q_BCD_TRACKS) {
2935 toch->starting_track = bcd2bin(toch->starting_track);
2936 toch->ending_track = bcd2bin(toch->ending_track);
2939 /* Number of TOC entries, plus leadout */
2940 num_entries = (toch->ending_track - toch->starting_track) + 2;
2942 if (num_entries <= 0)
2945 toclen = num_entries * sizeof(struct cd_toc_entry);
2947 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2948 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
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);
2960 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2963 * XXX KDM is this necessary? Probably only if the drive doesn't
2964 * return leadout information with the table of contents.
2966 cdindex = toch->starting_track + num_entries -1;
2967 if (cdindex == toch->ending_track + 1) {
2969 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2970 (u_int8_t *)&leadout, sizeof(leadout),
2976 softc->toc.entries[cdindex - toch->starting_track] =
2979 if (softc->quirks & CD_Q_BCD_TRACKS) {
2980 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2981 softc->toc.entries[cdindex].track =
2982 bcd2bin(softc->toc.entries[cdindex].track);
2987 * Run through the TOC entries, find the first entry and determine
2988 * whether it is an audio or data track.
2990 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2991 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2992 if (softc->toc.entries[cdindex].control & 0x04)
2993 first_track_audio = 0;
2995 first_track_audio = 1;
3001 * If first_track_audio is non-zero, we either have an error (e.g.
3002 * couldn't find the starting track) or the first track is an audio
3003 * track. If first_track_audio is 0, the first track is a data
3004 * track that could have a disklabel. Attempt to read the
3005 * disklabel off the media, just in case the user put one there.
3007 if (first_track_audio == 0) {
3008 info.d_dsflags |= DSO_COMPATLABEL;
3009 info.d_dsflags &= ~DSO_NOLABELS;
3010 disk_setdiskinfo(&softc->disk, &info);
3012 softc->flags |= CD_FLAG_VALID_TOC;
3019 cdsize(struct cam_periph *periph, u_int32_t *size)
3021 struct cd_softc *softc;
3023 struct scsi_read_capacity_data *rcap_buf;
3026 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3028 softc = (struct cd_softc *)periph->softc;
3030 ccb = cdgetccb(periph, /* priority */ 1);
3032 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
3033 M_SCSICD, M_INTWAIT | M_ZERO);
3035 scsi_read_capacity(&ccb->csio,
3041 /* timeout */20000);
3043 if (softc->flags & CD_FLAG_CAP_MUTE)
3044 ccb->ccb_h.flags |= CAM_QUIET;
3046 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3047 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3049 xpt_release_ccb(ccb);
3051 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3052 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3054 * SCSI-3 mandates that the reported blocksize shall be 2048.
3055 * Older drives sometimes report funny values, trim it down to
3056 * 2048, or other parts of the kernel will get confused.
3058 * XXX we leave drives alone that might report 512 bytes, as
3059 * well as drives reporting more weird sizes like perhaps 4K.
3061 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3062 softc->params.blksize = 2048;
3064 kfree(rcap_buf, M_SCSICD);
3065 *size = softc->params.disksize;
3072 cd6byteworkaround(union ccb *ccb)
3075 struct cam_periph *periph;
3076 struct cd_softc *softc;
3077 struct cd_mode_params *params;
3080 periph = xpt_path_periph(ccb->ccb_h.path);
3081 softc = (struct cd_softc *)periph->softc;
3083 cdb = ccb->csio.cdb_io.cdb_bytes;
3085 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3086 || ((cdb[0] != MODE_SENSE_6)
3087 && (cdb[0] != MODE_SELECT_6)))
3091 * Because there is no convenient place to stash the overall
3092 * cd_mode_params structure pointer, we have to grab it like this.
3093 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3094 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3096 * XXX It would be nice if, at some point, we could increase the
3097 * number of available peripheral private pointers. Both pointers
3098 * are currently used in most every peripheral driver.
3102 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3103 if (params->mode_buf == ccb->csio.data_ptr) {
3110 * This shouldn't happen. All mode sense and mode select
3111 * operations in the cd(4) driver MUST go through cdgetmode() and
3115 xpt_print(periph->path,
3116 "mode buffer not found in mode queue!\n");
3120 params->cdb_size = 10;
3121 softc->minimum_command_size = 10;
3122 xpt_print(ccb->ccb_h.path,
3123 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3124 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3126 if (cdb[0] == MODE_SENSE_6) {
3127 struct scsi_mode_sense_10 ms10;
3128 struct scsi_mode_sense_6 *ms6;
3131 ms6 = (struct scsi_mode_sense_6 *)cdb;
3133 bzero(&ms10, sizeof(ms10));
3134 ms10.opcode = MODE_SENSE_10;
3135 ms10.byte2 = ms6->byte2;
3136 ms10.page = ms6->page;
3139 * 10 byte mode header, block descriptor,
3140 * sizeof(union cd_pages)
3142 len = sizeof(struct cd_mode_data_10);
3143 ccb->csio.dxfer_len = len;
3145 scsi_ulto2b(len, ms10.length);
3146 ms10.control = ms6->control;
3147 bcopy(&ms10, cdb, 10);
3148 ccb->csio.cdb_len = 10;
3150 struct scsi_mode_select_10 ms10;
3151 struct scsi_mode_select_6 *ms6;
3152 struct scsi_mode_header_6 *header6;
3153 struct scsi_mode_header_10 *header10;
3154 struct scsi_mode_page_header *page_header;
3155 int blk_desc_len, page_num, page_size, len;
3157 ms6 = (struct scsi_mode_select_6 *)cdb;
3159 bzero(&ms10, sizeof(ms10));
3160 ms10.opcode = MODE_SELECT_10;
3161 ms10.byte2 = ms6->byte2;
3163 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3164 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3166 page_header = find_mode_page_6(header6);
3167 page_num = page_header->page_code;
3169 blk_desc_len = header6->blk_desc_len;
3171 page_size = cdgetpagesize(page_num);
3173 if (page_size != (page_header->page_length +
3174 sizeof(*page_header)))
3175 page_size = page_header->page_length +
3176 sizeof(*page_header);
3178 len = sizeof(*header10) + blk_desc_len + page_size;
3180 len = min(params->alloc_len, len);
3183 * Since the 6 byte parameter header is shorter than the 10
3184 * byte parameter header, we need to copy the actual mode
3185 * page data, and the block descriptor, if any, so things wind
3186 * up in the right place. The regions will overlap, but
3187 * bcopy() does the right thing.
3189 bcopy(params->mode_buf + sizeof(*header6),
3190 params->mode_buf + sizeof(*header10),
3191 len - sizeof(*header10));
3193 /* Make sure these fields are set correctly. */
3194 scsi_ulto2b(0, header10->data_length);
3195 header10->medium_type = 0;
3196 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3198 ccb->csio.dxfer_len = len;
3200 scsi_ulto2b(len, ms10.length);
3201 ms10.control = ms6->control;
3202 bcopy(&ms10, cdb, 10);
3203 ccb->csio.cdb_len = 10;
3206 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3207 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3210 cam_release_devq(ccb->ccb_h.path,
3214 /*getcount_only*/0);
3221 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3223 struct cd_softc *softc;
3224 struct cam_periph *periph;
3227 periph = xpt_path_periph(ccb->ccb_h.path);
3228 softc = (struct cd_softc *)periph->softc;
3233 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3234 * CDB comes back with this particular error, try transforming it
3235 * into the 10 byte version.
3237 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3238 error = cd6byteworkaround(ccb);
3239 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3240 CAM_SCSI_STATUS_ERROR)
3241 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3242 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3243 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3244 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3245 int sense_key, error_code, asc, ascq;
3247 scsi_extract_sense(&ccb->csio.sense_data,
3248 &error_code, &sense_key, &asc, &ascq);
3249 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3250 error = cd6byteworkaround(ccb);
3253 if (error == ERESTART)
3258 * Until we have a better way of doing pack validation,
3259 * don't treat UAs as errors.
3261 sense_flags |= SF_RETRY_UA;
3262 return (cam_periph_error(ccb, cam_flags, sense_flags,
3263 &softc->saved_ccb));
3267 * Read table of contents
3270 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3271 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3273 struct scsi_read_toc *scsi_cmd;
3275 struct ccb_scsiio *csio;
3282 ccb = cdgetccb(periph, /* priority */ 1);
3288 /* cbfcnp */ cddone,
3289 /* flags */ CAM_DIR_IN,
3290 /* tag_action */ MSG_SIMPLE_Q_TAG,
3291 /* data_ptr */ data,
3292 /* dxfer_len */ len,
3293 /* sense_len */ SSD_FULL_SIZE,
3294 sizeof(struct scsi_read_toc),
3295 /* timeout */ 50000);
3297 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3298 bzero (scsi_cmd, sizeof(*scsi_cmd));
3300 if (mode == CD_MSF_FORMAT)
3301 scsi_cmd->byte2 |= CD_MSF;
3302 scsi_cmd->from_track = start;
3303 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3304 scsi_cmd->data_len[0] = (ntoc) >> 8;
3305 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3307 scsi_cmd->op_code = READ_TOC;
3309 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3310 /*sense_flags*/SF_RETRY_UA | sense_flags);
3312 xpt_release_ccb(ccb);
3318 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3319 u_int32_t format, int track,
3320 struct cd_sub_channel_info *data, u_int32_t len)
3322 struct scsi_read_subchannel *scsi_cmd;
3323 struct ccb_scsiio *csio;
3329 ccb = cdgetccb(periph, /* priority */ 1);
3335 /* cbfcnp */ cddone,
3336 /* flags */ CAM_DIR_IN,
3337 /* tag_action */ MSG_SIMPLE_Q_TAG,
3338 /* data_ptr */ (u_int8_t *)data,
3339 /* dxfer_len */ len,
3340 /* sense_len */ SSD_FULL_SIZE,
3341 sizeof(struct scsi_read_subchannel),
3342 /* timeout */ 50000);
3344 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3345 bzero (scsi_cmd, sizeof(*scsi_cmd));
3347 scsi_cmd->op_code = READ_SUBCHANNEL;
3348 if (mode == CD_MSF_FORMAT)
3349 scsi_cmd->byte1 |= CD_MSF;
3350 scsi_cmd->byte2 = SRS_SUBQ;
3351 scsi_cmd->subchan_format = format;
3352 scsi_cmd->track = track;
3353 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3354 scsi_cmd->control = 0;
3356 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3357 /*sense_flags*/SF_RETRY_UA);
3359 xpt_release_ccb(ccb);
3365 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3366 * routine. See comments in cd6byteworkaround() for details.
3369 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3372 struct ccb_scsiio *csio;
3373 struct cd_softc *softc;
3378 softc = (struct cd_softc *)periph->softc;
3380 ccb = cdgetccb(periph, /* priority */ 1);
3384 data->cdb_size = softc->minimum_command_size;
3385 if (data->cdb_size < 10)
3386 param_len = sizeof(struct cd_mode_data);
3388 param_len = sizeof(struct cd_mode_data_10);
3390 /* Don't say we've got more room than we actually allocated */
3391 param_len = min(param_len, data->alloc_len);
3393 scsi_mode_sense_len(csio,
3395 /* cbfcnp */ cddone,
3396 /* tag_action */ MSG_SIMPLE_Q_TAG,
3398 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3400 /* param_buf */ data->mode_buf,
3401 /* param_len */ param_len,
3402 /* minimum_cmd_size */ softc->minimum_command_size,
3403 /* sense_len */ SSD_FULL_SIZE,
3404 /* timeout */ 50000);
3407 * It would be nice not to have to do this, but there's no
3408 * available pointer in the CCB that would allow us to stuff the
3409 * mode params structure in there and retrieve it in
3410 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3411 * lets the caller know what CDB size we ended up using, so they
3412 * can find the actual mode page offset.
3414 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3416 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3417 /*sense_flags*/SF_RETRY_UA);
3419 xpt_release_ccb(ccb);
3421 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3424 * This is a bit of belt-and-suspenders checking, but if we run
3425 * into a situation where the target sends back multiple block
3426 * descriptors, we might not have enough space in the buffer to
3427 * see the whole mode page. Better to return an error than
3428 * potentially access memory beyond our malloced region.
3433 if (data->cdb_size == 10) {
3434 struct scsi_mode_header_10 *hdr10;
3436 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3437 data_len = scsi_2btoul(hdr10->data_length);
3438 data_len += sizeof(hdr10->data_length);
3440 struct scsi_mode_header_6 *hdr6;
3442 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3443 data_len = hdr6->data_length;
3444 data_len += sizeof(hdr6->data_length);
3448 * Complain if there is more mode data available than we
3449 * allocated space for. This could potentially happen if
3450 * we miscalculated the page length for some reason, if the
3451 * drive returns multiple block descriptors, or if it sets
3452 * the data length incorrectly.
3454 if (data_len > data->alloc_len) {
3455 xpt_print(periph->path, "allocated modepage %d length "
3456 "%d < returned length %d\n", page, data->alloc_len,
3465 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3466 * routine. See comments in cd6byteworkaround() for details.
3469 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3471 struct ccb_scsiio *csio;
3472 struct cd_softc *softc;
3474 int cdb_size, param_len;
3477 softc = (struct cd_softc *)periph->softc;
3479 ccb = cdgetccb(periph, /* priority */ 1);
3486 * If the data is formatted for the 10 byte version of the mode
3487 * select parameter list, we need to use the 10 byte CDB.
3488 * Otherwise, we use whatever the stored minimum command size.
3490 if (data->cdb_size == 10)
3491 cdb_size = data->cdb_size;
3493 cdb_size = softc->minimum_command_size;
3495 if (cdb_size >= 10) {
3496 struct scsi_mode_header_10 *mode_header;
3499 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3501 data_len = scsi_2btoul(mode_header->data_length);
3503 scsi_ulto2b(0, mode_header->data_length);
3505 * SONY drives do not allow a mode select with a medium_type
3506 * value that has just been returned by a mode sense; use a
3507 * medium_type of 0 (Default) instead.
3509 mode_header->medium_type = 0;
3512 * Pass back whatever the drive passed to us, plus the size
3513 * of the data length field.
3515 param_len = data_len + sizeof(mode_header->data_length);
3518 struct scsi_mode_header_6 *mode_header;
3520 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3522 param_len = mode_header->data_length + 1;
3524 mode_header->data_length = 0;
3526 * SONY drives do not allow a mode select with a medium_type
3527 * value that has just been returned by a mode sense; use a
3528 * medium_type of 0 (Default) instead.
3530 mode_header->medium_type = 0;
3533 /* Don't say we've got more room than we actually allocated */
3534 param_len = min(param_len, data->alloc_len);
3536 scsi_mode_select_len(csio,
3538 /* cbfcnp */ cddone,
3539 /* tag_action */ MSG_SIMPLE_Q_TAG,
3540 /* scsi_page_fmt */ 1,
3542 /* param_buf */ data->mode_buf,
3543 /* param_len */ param_len,
3544 /* minimum_cmd_size */ cdb_size,
3545 /* sense_len */ SSD_FULL_SIZE,
3546 /* timeout */ 50000);
3548 /* See comments in cdgetmode() and cd6byteworkaround(). */
3549 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3551 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3552 /*sense_flags*/SF_RETRY_UA);
3554 xpt_release_ccb(ccb);
3556 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3564 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3566 struct ccb_scsiio *csio;
3572 ccb = cdgetccb(periph, /* priority */ 1);
3575 * Use the smallest possible command to perform the operation.
3577 if ((len & 0xffff0000) == 0) {
3579 * We can fit in a 10 byte cdb.
3581 struct scsi_play_10 *scsi_cmd;
3583 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3584 bzero (scsi_cmd, sizeof(*scsi_cmd));
3585 scsi_cmd->op_code = PLAY_10;
3586 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3587 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3588 cdb_len = sizeof(*scsi_cmd);
3590 struct scsi_play_12 *scsi_cmd;
3592 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3593 bzero (scsi_cmd, sizeof(*scsi_cmd));
3594 scsi_cmd->op_code = PLAY_12;
3595 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3596 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3597 cdb_len = sizeof(*scsi_cmd);
3602 /*flags*/CAM_DIR_NONE,
3606 /*sense_len*/SSD_FULL_SIZE,
3608 /*timeout*/50 * 1000);
3610 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3611 /*sense_flags*/SF_RETRY_UA);
3613 xpt_release_ccb(ccb);
3619 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3620 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3622 struct scsi_play_msf *scsi_cmd;
3623 struct ccb_scsiio *csio;
3629 ccb = cdgetccb(periph, /* priority */ 1);
3635 /* cbfcnp */ cddone,
3636 /* flags */ CAM_DIR_NONE,
3637 /* tag_action */ MSG_SIMPLE_Q_TAG,
3638 /* data_ptr */ NULL,
3640 /* sense_len */ SSD_FULL_SIZE,
3641 sizeof(struct scsi_play_msf),
3642 /* timeout */ 50000);
3644 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3645 bzero (scsi_cmd, sizeof(*scsi_cmd));
3647 scsi_cmd->op_code = PLAY_MSF;
3648 scsi_cmd->start_m = startm;
3649 scsi_cmd->start_s = starts;
3650 scsi_cmd->start_f = startf;
3651 scsi_cmd->end_m = endm;
3652 scsi_cmd->end_s = ends;
3653 scsi_cmd->end_f = endf;
3655 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3656 /*sense_flags*/SF_RETRY_UA);
3658 xpt_release_ccb(ccb);
3665 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3666 u_int32_t etrack, u_int32_t eindex)
3668 struct scsi_play_track *scsi_cmd;
3669 struct ccb_scsiio *csio;
3675 ccb = cdgetccb(periph, /* priority */ 1);
3681 /* cbfcnp */ cddone,
3682 /* flags */ CAM_DIR_NONE,
3683 /* tag_action */ MSG_SIMPLE_Q_TAG,
3684 /* data_ptr */ NULL,
3686 /* sense_len */ SSD_FULL_SIZE,
3687 sizeof(struct scsi_play_track),
3688 /* timeout */ 50000);
3690 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3691 bzero (scsi_cmd, sizeof(*scsi_cmd));
3693 scsi_cmd->op_code = PLAY_TRACK;
3694 scsi_cmd->start_track = strack;
3695 scsi_cmd->start_index = sindex;
3696 scsi_cmd->end_track = etrack;
3697 scsi_cmd->end_index = eindex;
3699 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3700 /*sense_flags*/SF_RETRY_UA);
3702 xpt_release_ccb(ccb);
3708 cdpause(struct cam_periph *periph, u_int32_t go)
3710 struct scsi_pause *scsi_cmd;
3711 struct ccb_scsiio *csio;
3717 ccb = cdgetccb(periph, /* priority */ 1);
3723 /* cbfcnp */ cddone,
3724 /* flags */ CAM_DIR_NONE,
3725 /* tag_action */ MSG_SIMPLE_Q_TAG,
3726 /* data_ptr */ NULL,
3728 /* sense_len */ SSD_FULL_SIZE,
3729 sizeof(struct scsi_pause),
3730 /* timeout */ 50000);
3732 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3733 bzero (scsi_cmd, sizeof(*scsi_cmd));
3735 scsi_cmd->op_code = PAUSE;
3736 scsi_cmd->resume = go;
3738 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3739 /*sense_flags*/SF_RETRY_UA);
3741 xpt_release_ccb(ccb);
3747 cdstartunit(struct cam_periph *periph, int load)
3754 ccb = cdgetccb(periph, /* priority */ 1);
3756 scsi_start_stop(&ccb->csio,
3758 /* cbfcnp */ cddone,
3759 /* tag_action */ MSG_SIMPLE_Q_TAG,
3761 /* load_eject */ load,
3762 /* immediate */ FALSE,
3763 /* sense_len */ SSD_FULL_SIZE,
3764 /* timeout */ 50000);
3766 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3767 /*sense_flags*/SF_RETRY_UA);
3769 xpt_release_ccb(ccb);
3775 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3782 ccb = cdgetccb(periph, /* priority */ 1);
3784 scsi_start_stop(&ccb->csio,
3786 /* cbfcnp */ cddone,
3787 /* tag_action */ MSG_SIMPLE_Q_TAG,
3789 /* load_eject */ eject,
3790 /* immediate */ FALSE,
3791 /* sense_len */ SSD_FULL_SIZE,
3792 /* timeout */ 50000);
3794 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3795 /*sense_flags*/SF_RETRY_UA);
3797 xpt_release_ccb(ccb);
3803 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3805 struct scsi_set_speed *scsi_cmd;
3806 struct ccb_scsiio *csio;
3811 ccb = cdgetccb(periph, /* priority */ 1);
3814 /* Preserve old behavior: units in multiples of CDROM speed */
3822 /* cbfcnp */ cddone,
3823 /* flags */ CAM_DIR_NONE,
3824 /* tag_action */ MSG_SIMPLE_Q_TAG,
3825 /* data_ptr */ NULL,
3827 /* sense_len */ SSD_FULL_SIZE,
3828 sizeof(struct scsi_set_speed),
3829 /* timeout */ 50000);
3831 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3832 bzero(scsi_cmd, sizeof(*scsi_cmd));
3834 scsi_cmd->opcode = SET_CD_SPEED;
3835 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3836 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3838 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3839 /*sense_flags*/SF_RETRY_UA);
3841 xpt_release_ccb(ccb);
3847 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3859 ccb = cdgetccb(periph, /* priority */ 1);
3861 switch (authinfo->format) {
3862 case DVD_REPORT_AGID:
3863 length = sizeof(struct scsi_report_key_data_agid);
3865 case DVD_REPORT_CHALLENGE:
3866 length = sizeof(struct scsi_report_key_data_challenge);
3868 case DVD_REPORT_KEY1:
3869 length = sizeof(struct scsi_report_key_data_key1_key2);
3871 case DVD_REPORT_TITLE_KEY:
3872 length = sizeof(struct scsi_report_key_data_title);
3873 /* The lba field is only set for the title key */
3874 lba = authinfo->lba;
3876 case DVD_REPORT_ASF:
3877 length = sizeof(struct scsi_report_key_data_asf);
3879 case DVD_REPORT_RPC:
3880 length = sizeof(struct scsi_report_key_data_rpc);
3882 case DVD_INVALIDATE_AGID:
3888 break; /* NOTREACHED */
3892 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3898 scsi_report_key(&ccb->csio,
3900 /* cbfcnp */ cddone,
3901 /* tag_action */ MSG_SIMPLE_Q_TAG,
3903 /* agid */ authinfo->agid,
3904 /* key_format */ authinfo->format,
3905 /* data_ptr */ databuf,
3906 /* dxfer_len */ length,
3907 /* sense_len */ SSD_FULL_SIZE,
3908 /* timeout */ 50000);
3910 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3911 /*sense_flags*/SF_RETRY_UA);
3916 if (ccb->csio.resid != 0) {
3917 xpt_print(periph->path, "warning, residual for report key "
3918 "command is %d\n", ccb->csio.resid);
3921 switch(authinfo->format) {
3922 case DVD_REPORT_AGID: {
3923 struct scsi_report_key_data_agid *agid_data;
3925 agid_data = (struct scsi_report_key_data_agid *)databuf;
3927 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3931 case DVD_REPORT_CHALLENGE: {
3932 struct scsi_report_key_data_challenge *chal_data;
3934 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3936 bcopy(chal_data->challenge_key, authinfo->keychal,
3937 min(sizeof(chal_data->challenge_key),
3938 sizeof(authinfo->keychal)));
3941 case DVD_REPORT_KEY1: {
3942 struct scsi_report_key_data_key1_key2 *key1_data;
3944 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3946 bcopy(key1_data->key1, authinfo->keychal,
3947 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3950 case DVD_REPORT_TITLE_KEY: {
3951 struct scsi_report_key_data_title *title_data;
3953 title_data = (struct scsi_report_key_data_title *)databuf;
3955 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3956 RKD_TITLE_CPM_SHIFT;
3957 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3958 RKD_TITLE_CP_SEC_SHIFT;
3959 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3960 RKD_TITLE_CMGS_SHIFT;
3961 bcopy(title_data->title_key, authinfo->keychal,
3962 min(sizeof(title_data->title_key),
3963 sizeof(authinfo->keychal)));
3966 case DVD_REPORT_ASF: {
3967 struct scsi_report_key_data_asf *asf_data;
3969 asf_data = (struct scsi_report_key_data_asf *)databuf;
3971 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3974 case DVD_REPORT_RPC: {
3975 struct scsi_report_key_data_rpc *rpc_data;
3977 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3979 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3981 authinfo->vend_rsts =
3982 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3983 RKD_RPC_VENDOR_RESET_SHIFT;
3984 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3985 authinfo->region = rpc_data->region_mask;
3986 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3989 case DVD_INVALIDATE_AGID:
3992 /* This should be impossible, since we checked above */
3995 break; /* NOTREACHED */
3998 if (databuf != NULL)
3999 kfree(databuf, M_DEVBUF);
4001 xpt_release_ccb(ccb);
4007 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4017 ccb = cdgetccb(periph, /* priority */ 1);
4019 switch(authinfo->format) {
4020 case DVD_SEND_CHALLENGE: {
4021 struct scsi_report_key_data_challenge *challenge_data;
4023 length = sizeof(*challenge_data);
4025 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4027 databuf = (u_int8_t *)challenge_data;
4029 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4030 challenge_data->data_len);
4032 bcopy(authinfo->keychal, challenge_data->challenge_key,
4033 min(sizeof(authinfo->keychal),
4034 sizeof(challenge_data->challenge_key)));
4037 case DVD_SEND_KEY2: {
4038 struct scsi_report_key_data_key1_key2 *key2_data;
4040 length = sizeof(*key2_data);
4042 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4044 databuf = (u_int8_t *)key2_data;
4046 scsi_ulto2b(length - sizeof(key2_data->data_len),
4047 key2_data->data_len);
4049 bcopy(authinfo->keychal, key2_data->key1,
4050 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4054 case DVD_SEND_RPC: {
4055 struct scsi_send_key_data_rpc *rpc_data;
4057 length = sizeof(*rpc_data);
4059 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4061 databuf = (u_int8_t *)rpc_data;
4063 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4064 rpc_data->data_len);
4066 rpc_data->region_code = authinfo->region;
4072 break; /* NOTREACHED */
4075 scsi_send_key(&ccb->csio,
4077 /* cbfcnp */ cddone,
4078 /* tag_action */ MSG_SIMPLE_Q_TAG,
4079 /* agid */ authinfo->agid,
4080 /* key_format */ authinfo->format,
4081 /* data_ptr */ databuf,
4082 /* dxfer_len */ length,
4083 /* sense_len */ SSD_FULL_SIZE,
4084 /* timeout */ 50000);
4086 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4087 /*sense_flags*/SF_RETRY_UA);
4091 if (databuf != NULL)
4092 kfree(databuf, M_DEVBUF);
4094 xpt_release_ccb(ccb);
4100 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4110 /* The address is reserved for many of the formats */
4113 ccb = cdgetccb(periph, /* priority */ 1);
4115 switch(dvdstruct->format) {
4116 case DVD_STRUCT_PHYSICAL:
4117 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4119 case DVD_STRUCT_COPYRIGHT:
4120 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4122 case DVD_STRUCT_DISCKEY:
4123 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4125 case DVD_STRUCT_BCA:
4126 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4128 case DVD_STRUCT_MANUFACT:
4129 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4131 case DVD_STRUCT_CMI:
4135 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4136 address = dvdstruct->address;
4138 break; /* NOTREACHED */
4139 case DVD_STRUCT_PROTDISCID:
4140 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4142 case DVD_STRUCT_DISCKEYBLOCK:
4143 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4145 case DVD_STRUCT_DDS:
4146 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4148 case DVD_STRUCT_MEDIUM_STAT:
4149 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4151 case DVD_STRUCT_SPARE_AREA:
4152 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4154 case DVD_STRUCT_RMD_LAST:
4158 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4159 address = dvdstruct->address;
4161 break; /* NOTREACHED */
4162 case DVD_STRUCT_RMD_RMA:
4166 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4167 address = dvdstruct->address;
4169 break; /* NOTREACHED */
4170 case DVD_STRUCT_PRERECORDED:
4171 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4173 case DVD_STRUCT_UNIQUEID:
4174 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4176 case DVD_STRUCT_DCB:
4180 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4181 address = dvdstruct->address;
4183 break; /* NOTREACHED */
4184 case DVD_STRUCT_LIST:
4186 * This is the maximum allocation length for the READ DVD
4187 * STRUCTURE command. There's nothing in the MMC3 spec
4188 * that indicates a limit in the amount of data that can
4189 * be returned from this call, other than the limits
4190 * imposed by the 2-byte length variables.
4197 break; /* NOTREACHED */
4201 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4206 scsi_read_dvd_structure(&ccb->csio,
4208 /* cbfcnp */ cddone,
4209 /* tag_action */ MSG_SIMPLE_Q_TAG,
4211 /* layer_number */ dvdstruct->layer_num,
4212 /* key_format */ dvdstruct->format,
4213 /* agid */ dvdstruct->agid,
4214 /* data_ptr */ databuf,
4215 /* dxfer_len */ length,
4216 /* sense_len */ SSD_FULL_SIZE,
4217 /* timeout */ 50000);
4219 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4220 /*sense_flags*/SF_RETRY_UA);
4225 switch(dvdstruct->format) {
4226 case DVD_STRUCT_PHYSICAL: {
4227 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4228 struct dvd_layer *outlayer;
4229 struct scsi_read_dvd_struct_data_physical *phys_data;
4232 (struct scsi_read_dvd_struct_data_physical *)databuf;
4233 inlayer = &phys_data->layer_desc;
4234 outlayer = (struct dvd_layer *)&dvdstruct->data;
4236 dvdstruct->length = sizeof(*inlayer);
4238 outlayer->book_type = (inlayer->book_type_version &
4239 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4240 outlayer->book_version = (inlayer->book_type_version &
4241 RDSD_BOOK_VERSION_MASK);
4242 outlayer->disc_size = (inlayer->disc_size_max_rate &
4243 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4244 outlayer->max_rate = (inlayer->disc_size_max_rate &
4245 RDSD_MAX_RATE_MASK);
4246 outlayer->nlayers = (inlayer->layer_info &
4247 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4248 outlayer->track_path = (inlayer->layer_info &
4249 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4250 outlayer->layer_type = (inlayer->layer_info &
4251 RDSD_LAYER_TYPE_MASK);
4252 outlayer->linear_density = (inlayer->density &
4253 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4254 outlayer->track_density = (inlayer->density &
4255 RDSD_TRACK_DENSITY_MASK);
4256 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4258 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4259 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4260 outlayer->end_sector_l0 =
4261 scsi_3btoul(inlayer->end_sector_layer0);
4264 case DVD_STRUCT_COPYRIGHT: {
4265 struct scsi_read_dvd_struct_data_copyright *copy_data;
4267 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4270 dvdstruct->cpst = copy_data->cps_type;
4271 dvdstruct->rmi = copy_data->region_info;
4272 dvdstruct->length = 0;
4278 * Tell the user what the overall length is, no matter
4279 * what we can actually fit in the data buffer.
4281 dvdstruct->length = length - ccb->csio.resid -
4282 sizeof(struct scsi_read_dvd_struct_data_header);
4285 * But only actually copy out the smaller of what we read
4286 * in or what the structure can take.
4288 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4290 min(sizeof(dvdstruct->data), dvdstruct->length));
4295 if (databuf != NULL)
4296 kfree(databuf, M_DEVBUF);
4298 xpt_release_ccb(ccb);
4304 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4305 void (*cbfcnp)(struct cam_periph *, union ccb *),
4306 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4307 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4308 u_int8_t sense_len, u_int32_t timeout)
4310 struct scsi_report_key *scsi_cmd;
4312 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4313 bzero(scsi_cmd, sizeof(*scsi_cmd));
4314 scsi_cmd->opcode = REPORT_KEY;
4315 scsi_ulto4b(lba, scsi_cmd->lba);
4316 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4317 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4318 (key_format & RK_KF_KEYFORMAT_MASK);
4323 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4325 /*data_ptr*/ data_ptr,
4326 /*dxfer_len*/ dxfer_len,
4333 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4334 void (*cbfcnp)(struct cam_periph *, union ccb *),
4335 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4336 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4339 struct scsi_send_key *scsi_cmd;
4341 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4342 bzero(scsi_cmd, sizeof(*scsi_cmd));
4343 scsi_cmd->opcode = SEND_KEY;
4345 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4346 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4347 (key_format & RK_KF_KEYFORMAT_MASK);
4352 /*flags*/ CAM_DIR_OUT,
4354 /*data_ptr*/ data_ptr,
4355 /*dxfer_len*/ dxfer_len,
4363 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4364 void (*cbfcnp)(struct cam_periph *, union ccb *),
4365 u_int8_t tag_action, u_int32_t address,
4366 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4367 u_int8_t *data_ptr, u_int32_t dxfer_len,
4368 u_int8_t sense_len, u_int32_t timeout)
4370 struct scsi_read_dvd_structure *scsi_cmd;
4372 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4373 bzero(scsi_cmd, sizeof(*scsi_cmd));
4374 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4376 scsi_ulto4b(address, scsi_cmd->address);
4377 scsi_cmd->layer_number = layer_number;
4378 scsi_cmd->format = format;
4379 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4380 /* The AGID is the top two bits of this byte */
4381 scsi_cmd->agid = agid << 6;
4386 /*flags*/ CAM_DIR_IN,
4388 /*data_ptr*/ data_ptr,
4389 /*dxfer_len*/ dxfer_len,