2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_cd.c,v 1.44 2008/07/18 00:07:23 dillon Exp $
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
52 #include <sys/param.h>
53 #include <sys/bootmaj.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/dtype.h>
60 #include <sys/malloc.h>
62 #include <sys/cdrio.h>
63 #include <sys/dvdio.h>
64 #include <sys/devicestat.h>
65 #include <sys/sysctl.h>
66 #include <sys/taskqueue.h>
68 #include <sys/camlib.h>
72 #include <sys/thread2.h>
73 #include <sys/mplock2.h>
76 #include "../cam_ccb.h"
77 #include "../cam_extend.h"
78 #include "../cam_periph.h"
79 #include "../cam_xpt_periph.h"
80 #include "../cam_queue.h"
81 #include "../cam_sim.h"
83 #include "scsi_message.h"
87 #define LEADOUT 0xaa /* leadout toc entry */
97 CD_Q_BCD_TRACKS = 0x02,
98 CD_Q_NO_CHANGER = 0x04,
100 CD_Q_10_BYTE_ONLY = 0x10
104 CD_FLAG_INVALID = 0x0001,
105 CD_FLAG_NEW_DISC = 0x0002,
106 CD_FLAG_DISC_LOCKED = 0x0004,
107 CD_FLAG_DISC_REMOVABLE = 0x0008,
108 CD_FLAG_TAGGED_QUEUING = 0x0010,
109 CD_FLAG_CHANGER = 0x0040,
110 CD_FLAG_ACTIVE = 0x0080,
111 CD_FLAG_SCHED_ON_COMP = 0x0100,
112 CD_FLAG_RETRY_UA = 0x0200,
113 CD_FLAG_VALID_MEDIA = 0x0400,
114 CD_FLAG_VALID_TOC = 0x0800,
115 CD_FLAG_SCTX_INIT = 0x1000,
116 CD_FLAG_OPEN = 0x2000
121 CD_CCB_BUFFER_IO = 0x02,
122 CD_CCB_WAITING = 0x03,
123 CD_CCB_TYPE_MASK = 0x0F,
124 CD_CCB_RETRY_UA = 0x10
128 CHANGER_TIMEOUT_SCHED = 0x01,
129 CHANGER_SHORT_TMOUT_SCHED = 0x02,
130 CHANGER_MANUAL_CALL = 0x04,
131 CHANGER_NEED_TIMEOUT = 0x08
134 #define ccb_state ppriv_field0
135 #define ccb_bio ppriv_ptr1
138 struct ioc_toc_header header;
139 struct cd_toc_entry entries[100];
142 struct cd_toc_single {
143 struct ioc_toc_header header;
144 struct cd_toc_entry entry;
155 volatile cd_flags flags;
156 struct bio_queue_head bio_queue;
157 LIST_HEAD(, ccb_hdr) pending_ccbs;
158 struct cd_params params;
162 struct devstat device_stats;
163 STAILQ_ENTRY(cd_softc) changer_links;
164 struct cdchanger *changer;
166 struct cam_periph *periph;
167 int minimum_command_size;
168 int outstanding_cmds;
169 struct task sysctl_task;
170 struct sysctl_ctx_list sysctl_ctx;
171 struct sysctl_oid *sysctl_tree;
172 STAILQ_HEAD(, cd_mode_params) mode_queue;
173 struct cd_tocdata toc;
176 struct cd_page_sizes {
181 static struct cd_page_sizes cd_page_size_table[] =
183 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
186 struct cd_quirk_entry {
187 struct scsi_inquiry_pattern inq_pat;
192 * The changer quirk entries aren't strictly necessary. Basically, what
193 * they do is tell cdregister() up front that a device is a changer.
194 * Otherwise, it will figure that fact out once it sees a LUN on the device
195 * that is greater than 0. If it is known up front that a device is a changer,
196 * all I/O to the device will go through the changer scheduling routines, as
197 * opposed to the "normal" CD code.
199 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
200 * your device hangs when it gets a 10 byte command. Adding a quirk just
201 * to get rid of the informative diagnostic message is not acceptable. All
202 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
203 * referenced in a comment along with the quirk) , and must be approved by
204 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
205 * be removed until the submitter can explain why they are needed.
206 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
207 * when the CAM_NEW_TRAN_CODE work is done.
209 static struct cd_quirk_entry cd_quirk_table[] =
212 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
213 /*quirks*/ CD_Q_CHANGER
216 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
217 "*"}, /* quirks */ CD_Q_CHANGER
220 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
221 /* quirks */ CD_Q_CHANGER
224 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
225 /* quirks */ CD_Q_BCD_TRACKS
229 static d_open_t cdopen;
230 static d_close_t cdclose;
231 static d_ioctl_t cdioctl;
232 static d_strategy_t cdstrategy;
234 static periph_init_t cdinit;
235 static periph_ctor_t cdregister;
236 static periph_dtor_t cdcleanup;
237 static periph_start_t cdstart;
238 static periph_oninv_t cdoninvalidate;
239 static void cdasync(void *callback_arg, u_int32_t code,
240 struct cam_path *path, void *arg);
241 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
242 static void cdshorttimeout(void *arg);
243 static void cdschedule(struct cam_periph *periph, int priority);
244 static void cdrunchangerqueue(void *arg);
245 static void cdchangerschedule(struct cd_softc *softc);
246 static int cdrunccb(union ccb *ccb,
247 int (*error_routine)(union ccb *ccb,
249 u_int32_t sense_flags),
250 u_int32_t cam_flags, u_int32_t sense_flags);
251 static union ccb *cdgetccb(struct cam_periph *periph,
253 static void cddone(struct cam_periph *periph,
254 union ccb *start_ccb);
255 static int cderror(union ccb *ccb, u_int32_t cam_flags,
256 u_int32_t sense_flags);
257 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
258 static int cdgetpagesize(int page_num);
259 static void cdprevent(struct cam_periph *periph, int action);
260 static int cdcheckmedia(struct cam_periph *periph);
261 static int cdsize(struct cam_periph *periph, u_int32_t *size);
262 static int cd6byteworkaround(union ccb *ccb);
263 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
264 u_int32_t start, u_int8_t *data,
265 u_int32_t len, u_int32_t sense_flags);
266 static int cdgetmode(struct cam_periph *periph,
267 struct cd_mode_params *data, u_int32_t page);
268 static int cdsetmode(struct cam_periph *periph,
269 struct cd_mode_params *data);
270 static int cdplay(struct cam_periph *periph, u_int32_t blk,
272 static int cdreadsubchannel(struct cam_periph *periph,
273 u_int32_t mode, u_int32_t format,
275 struct cd_sub_channel_info *data,
277 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
278 u_int32_t starts, u_int32_t startf,
279 u_int32_t endm, u_int32_t ends,
281 static int cdplaytracks(struct cam_periph *periph,
282 u_int32_t strack, u_int32_t sindex,
283 u_int32_t etrack, u_int32_t eindex);
284 static int cdpause(struct cam_periph *periph, u_int32_t go);
285 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
286 static int cdstartunit(struct cam_periph *periph, int load);
287 static int cdsetspeed(struct cam_periph *periph,
288 u_int32_t rdspeed, u_int32_t wrspeed);
289 static int cdreportkey(struct cam_periph *periph,
290 struct dvd_authinfo *authinfo);
291 static int cdsendkey(struct cam_periph *periph,
292 struct dvd_authinfo *authinfo);
293 static int cdreaddvdstructure(struct cam_periph *periph,
294 struct dvd_struct *dvdstruct);
296 static struct periph_driver cddriver =
299 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
302 PERIPHDRIVER_DECLARE(cd, cddriver);
304 static struct dev_ops cd_ops = {
305 { "cd", SCSICD_CDEV_MAJOR, D_DISK | D_MPSAFE },
309 .d_write = physwrite,
311 .d_strategy = cdstrategy
314 static struct extend_array *cdperiphs;
316 #ifndef CHANGER_MIN_BUSY_SECONDS
317 #define CHANGER_MIN_BUSY_SECONDS 5
319 #ifndef CHANGER_MAX_BUSY_SECONDS
320 #define CHANGER_MAX_BUSY_SECONDS 15
323 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
324 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
326 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
327 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
328 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
329 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
330 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
331 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
332 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
333 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
337 target_id_t target_id;
340 struct timeval start_time;
341 struct cd_softc *cur_device;
342 struct callout short_handle;
343 struct callout long_handle;
344 volatile cd_changer_flags flags;
345 STAILQ_ENTRY(cdchanger) changer_links;
346 STAILQ_HEAD(chdevlist, cd_softc) chluns;
349 static struct lock changerq_lock;
350 static STAILQ_HEAD(changerlist, cdchanger) changerq;
351 static int num_changers;
353 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
360 lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
361 STAILQ_INIT(&changerq);
364 * Create our extend array for storing the devices we attach to.
366 cdperiphs = cam_extend_new();
367 if (cdperiphs == NULL) {
368 kprintf("cd: Failed to alloc extend array!\n");
373 * Install a global async callback. This callback will
374 * receive async callbacks like "new device found".
376 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
378 if (status != CAM_REQ_CMP) {
379 kprintf("cd: Failed to attach master async callback "
380 "due to status 0x%x!\n", status);
385 cdoninvalidate(struct cam_periph *periph)
387 struct cd_softc *softc;
391 softc = (struct cd_softc *)periph->softc;
394 * De-register any async callbacks.
396 xpt_register_async(0, cdasync, periph, periph->path);
398 softc->flags |= CD_FLAG_INVALID;
401 * Return all queued I/O with ENXIO.
402 * XXX Handle any transactions queued to the card
403 * with XPT_ABORT_CCB.
405 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
406 bioq_remove(&softc->bio_queue, q_bio);
407 q_bp = q_bio->bio_buf;
408 q_bp->b_resid = q_bp->b_bcount;
409 q_bp->b_error = ENXIO;
410 q_bp->b_flags |= B_ERROR;
415 * If this device is part of a changer, and it was scheduled
416 * to run, remove it from the run queue since we just nuked
417 * all of its scheduled I/O.
419 if ((softc->flags & CD_FLAG_CHANGER)
420 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
421 camq_remove(&softc->changer->devq, softc->pinfo.index);
423 xpt_print(periph->path, "lost device\n");
427 cdcleanup(struct cam_periph *periph)
429 struct cd_softc *softc;
431 softc = (struct cd_softc *)periph->softc;
433 xpt_print(periph->path, "removing device entry\n");
435 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
436 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
437 xpt_print(periph->path, "can't remove sysctl context\n");
441 * In the queued, non-active case, the device in question
442 * has already been removed from the changer run queue. Since this
443 * device is active, we need to de-activate it, and schedule
444 * another device to run. (if there is another one to run)
446 if ((softc->flags & CD_FLAG_CHANGER)
447 && (softc->flags & CD_FLAG_ACTIVE)) {
450 * The purpose of the short timeout is soley to determine
451 * whether the current device has finished or not. Well,
452 * since we're removing the active device, we know that it
453 * is finished. So, get rid of the short timeout.
454 * Otherwise, if we're in the time period before the short
455 * timeout fires, and there are no other devices in the
456 * queue to run, there won't be any other device put in the
457 * active slot. i.e., when we call cdrunchangerqueue()
458 * below, it won't do anything. Then, when the short
459 * timeout fires, it'll look at the "current device", which
460 * we are free below, and possibly panic the kernel on a
461 * bogus pointer reference.
463 * The long timeout doesn't really matter, since we
464 * decrement the qfrozen_cnt to indicate that there is
465 * nothing in the active slot now. Therefore, there won't
466 * be any bogus pointer references there.
468 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
469 callout_stop(&softc->changer->short_handle);
470 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
472 softc->changer->devq.qfrozen_cnt--;
473 softc->changer->flags |= CHANGER_MANUAL_CALL;
474 cdrunchangerqueue(softc->changer);
478 * If we're removing the last device on the changer, go ahead and
479 * remove the changer device structure.
481 if ((softc->flags & CD_FLAG_CHANGER)
482 && (--softc->changer->num_devices == 0)) {
485 * Theoretically, there shouldn't be any timeouts left, but
486 * I'm not completely sure that that will be the case. So,
487 * it won't hurt to check and see if there are any left.
489 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
490 callout_stop(&softc->changer->long_handle);
491 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
494 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
495 callout_stop(&softc->changer->short_handle);
496 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
499 lockmgr(&changerq_lock, LK_EXCLUSIVE);
500 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
503 lockmgr(&changerq_lock, LK_RELEASE);
504 xpt_print(periph->path, "removing changer entry\n");
505 kfree(softc->changer, M_DEVBUF);
507 devstat_remove_entry(&softc->device_stats);
508 cam_extend_release(cdperiphs, periph->unit_number);
509 if (softc->disk.d_rawdev) {
510 cam_periph_unlock(periph);
511 disk_destroy(&softc->disk);
512 cam_periph_lock(periph);
514 kfree(softc, M_DEVBUF);
518 cdasync(void *callback_arg, u_int32_t code,
519 struct cam_path *path, void *arg)
521 struct cam_periph *periph;
523 periph = (struct cam_periph *)callback_arg;
526 case AC_FOUND_DEVICE:
528 struct ccb_getdev *cgd;
531 cgd = (struct ccb_getdev *)arg;
535 if (SID_TYPE(&cgd->inq_data) != T_CDROM
536 && SID_TYPE(&cgd->inq_data) != T_WORM)
540 * Don't complain if a valid peripheral is already attached.
542 periph = cam_periph_find(cgd->ccb_h.path, "cd");
543 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
547 * Allocate a peripheral instance for
548 * this device and start the probe
551 status = cam_periph_alloc(cdregister, cdoninvalidate,
553 "cd", CAM_PERIPH_BIO,
554 cgd->ccb_h.path, cdasync,
555 AC_FOUND_DEVICE, cgd);
557 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
558 kprintf("cdasync: Unable to attach new device "
559 "due to status 0x%x\n", status);
566 struct cd_softc *softc;
567 struct ccb_hdr *ccbh;
569 softc = (struct cd_softc *)periph->softc;
571 * Don't fail on the expected unit attention
574 softc->flags |= CD_FLAG_RETRY_UA;
575 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
576 ccbh->ccb_state |= CD_CCB_RETRY_UA;
580 cam_periph_async(periph, code, path, arg);
586 cdsysctlinit(void *context, int pending)
588 struct cam_periph *periph;
589 struct cd_softc *softc;
590 char tmpstr[80], tmpstr2[80];
593 periph = (struct cam_periph *)context;
594 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
599 softc = (struct cd_softc *)periph->softc;
600 ksnprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
601 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
603 sysctl_ctx_init(&softc->sysctl_ctx);
604 softc->flags |= CD_FLAG_SCTX_INIT;
605 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
606 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
607 tmpstr2, CTLFLAG_RD, 0, tmpstr);
609 if (softc->sysctl_tree == NULL) {
610 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
611 cam_periph_release(periph);
617 * Now register the sysctl handler, so the user can the value on
620 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
621 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
622 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
625 cam_periph_release(periph);
630 * We have a handler function for this so we can check the values when the
631 * user sets them, instead of every time we look at them.
634 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
638 value = *(int *)arg1;
640 error = sysctl_handle_int(oidp, &value, 0, req);
642 if ((error != 0) || (req->newptr == NULL))
646 * The only real values we can have here are 6 or 10. I don't
647 * really forsee having 12 be an option at any time in the future.
648 * So if the user sets something less than or equal to 6, we'll set
649 * it to 6. If he sets something greater than 6, we'll set it to 10.
651 * I suppose we could just return an error here for the wrong values,
652 * but I don't think it's necessary to do so, as long as we can
653 * determine the user's intent without too much trouble.
660 *(int *)arg1 = value;
667 cdregister(struct cam_periph *periph, void *arg)
669 struct cd_softc *softc;
670 struct ccb_pathinq cpi;
671 struct ccb_getdev *cgd;
675 cgd = (struct ccb_getdev *)arg;
676 if (periph == NULL) {
677 kprintf("cdregister: periph was NULL!!\n");
678 return(CAM_REQ_CMP_ERR);
681 kprintf("cdregister: no getdev CCB, can't register device\n");
682 return(CAM_REQ_CMP_ERR);
685 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
686 LIST_INIT(&softc->pending_ccbs);
687 STAILQ_INIT(&softc->mode_queue);
688 softc->state = CD_STATE_PROBE;
689 bioq_init(&softc->bio_queue);
690 if (SID_IS_REMOVABLE(&cgd->inq_data))
691 softc->flags |= CD_FLAG_DISC_REMOVABLE;
692 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
693 softc->flags |= CD_FLAG_TAGGED_QUEUING;
695 periph->softc = softc;
696 softc->periph = periph;
698 cam_extend_set(cdperiphs, periph->unit_number, periph);
701 * See if this device has any quirks.
703 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
704 (caddr_t)cd_quirk_table,
705 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
706 sizeof(*cd_quirk_table), scsi_inquiry_match);
709 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
711 softc->quirks = CD_Q_NONE;
713 /* Check if the SIM does not want 6 byte commands */
714 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
715 cpi.ccb_h.func_code = XPT_PATH_INQ;
716 xpt_action((union ccb *)&cpi);
717 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
718 softc->quirks |= CD_Q_10_BYTE_ONLY;
720 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
722 /* The default is 6 byte commands, unless quirked otherwise */
723 if (softc->quirks & CD_Q_10_BYTE_ONLY)
724 softc->minimum_command_size = 10;
726 softc->minimum_command_size = 6;
729 * Load the user's default, if any.
731 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
732 periph->unit_number);
733 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
735 /* 6 and 10 are the only permissible values here. */
736 if (softc->minimum_command_size < 6)
737 softc->minimum_command_size = 6;
738 else if (softc->minimum_command_size > 6)
739 softc->minimum_command_size = 10;
742 * We need to register the statistics structure for this device,
743 * but we don't have the blocksize yet for it. So, we register
744 * the structure and indicate that we don't have the blocksize
745 * yet. Unlike other SCSI peripheral drivers, we explicitly set
746 * the device type here to be CDROM, rather than just ORing in
747 * the device type. This is because this driver can attach to either
748 * CDROM or WORM devices, and we want this peripheral driver to
749 * show up in the devstat list as a CD peripheral driver, not a
750 * WORM peripheral driver. WORM drives will also have the WORM
751 * driver attached to them.
753 cam_periph_unlock(periph);
754 devstat_add_entry(&softc->device_stats, "cd",
755 periph->unit_number, 0,
756 DEVSTAT_BS_UNAVAILABLE,
757 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
758 DEVSTAT_PRIORITY_CD);
759 disk_create(periph->unit_number, &softc->disk, &cd_ops);
760 disk_setdisktype(&softc->disk, "optical");
761 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
762 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
764 cam_periph_lock(periph);
767 * Add an async callback so that we get
768 * notified if this device goes away.
770 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
771 cdasync, periph, periph->path);
774 * If the target lun is greater than 0, we most likely have a CD
775 * changer device. Check the quirk entries as well, though, just
776 * in case someone has a CD tower with one lun per drive or
777 * something like that. Also, if we know up front that a
778 * particular device is a changer, we can mark it as such starting
779 * with lun 0, instead of lun 1. It shouldn't be necessary to have
780 * a quirk entry to define something as a changer, however.
782 if (((cgd->ccb_h.target_lun > 0)
783 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
784 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
785 struct cdchanger *nchanger;
786 struct cam_periph *nperiph;
787 struct cam_path *path;
791 /* Set the changer flag in the current device's softc */
792 softc->flags |= CD_FLAG_CHANGER;
795 * Now, look around for an existing changer device with the
796 * same path and target ID as the current device.
798 lockmgr(&changerq_lock, LK_EXCLUSIVE);
800 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
802 nchanger = STAILQ_NEXT(nchanger, changer_links)){
803 if ((nchanger->path_id == cgd->ccb_h.path_id)
804 && (nchanger->target_id == cgd->ccb_h.target_id)) {
809 lockmgr(&changerq_lock, LK_RELEASE);
812 * If we found a matching entry, just add this device to
813 * the list of devices on this changer.
816 struct chdevlist *chlunhead;
818 chlunhead = &nchanger->chluns;
821 * XXX KDM look at consolidating this code with the
822 * code below in a separate function.
826 * Create a path with lun id 0, and see if we can
827 * find a matching device
829 status = xpt_create_path(&path, /*periph*/ periph,
831 cgd->ccb_h.target_id, 0);
833 if ((status == CAM_REQ_CMP)
834 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
835 struct cd_softc *nsoftc;
837 nsoftc = (struct cd_softc *)nperiph->softc;
839 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
840 nsoftc->flags |= CD_FLAG_CHANGER;
841 nchanger->num_devices++;
842 if (camq_resize(&nchanger->devq,
843 nchanger->num_devices)!=CAM_REQ_CMP){
844 kprintf("cdregister: "
850 nsoftc->changer = nchanger;
851 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
853 STAILQ_INSERT_TAIL(&nchanger->chluns,
854 nsoftc,changer_links);
857 } else if (status == CAM_REQ_CMP)
860 kprintf("cdregister: unable to allocate path\n"
861 "cdregister: changer support may be "
865 nchanger->num_devices++;
867 softc->changer = nchanger;
868 softc->pinfo.index = CAM_UNQUEUED_INDEX;
870 if (camq_resize(&nchanger->devq,
871 nchanger->num_devices) != CAM_REQ_CMP) {
872 kprintf("cdregister: camq_resize "
873 "failed, changer support may "
877 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
880 * In this case, we don't already have an entry for this
881 * particular changer, so we need to create one, add it to
882 * the queue, and queue this device on the list for this
883 * changer. Before we queue this device, however, we need
884 * to search for lun id 0 on this target, and add it to the
885 * queue first, if it exists. (and if it hasn't already
886 * been marked as part of the changer.)
889 nchanger = kmalloc(sizeof(struct cdchanger),
890 M_DEVBUF, M_INTWAIT | M_ZERO);
891 callout_init(&nchanger->short_handle);
892 callout_init(&nchanger->long_handle);
893 if (camq_init(&nchanger->devq, 1) != 0) {
894 softc->flags &= ~CD_FLAG_CHANGER;
895 kprintf("cdregister: changer support "
900 nchanger->path_id = cgd->ccb_h.path_id;
901 nchanger->target_id = cgd->ccb_h.target_id;
903 /* this is superfluous, but it makes things clearer */
904 nchanger->num_devices = 0;
906 STAILQ_INIT(&nchanger->chluns);
908 callout_init(&nchanger->long_handle);
909 callout_init(&nchanger->short_handle);
911 lockmgr(&changerq_lock, LK_EXCLUSIVE);
913 STAILQ_INSERT_TAIL(&changerq, nchanger,
915 lockmgr(&changerq_lock, LK_RELEASE);
918 * Create a path with lun id 0, and see if we can
919 * find a matching device
921 status = xpt_create_path(&path, /*periph*/ periph,
923 cgd->ccb_h.target_id, 0);
926 * If we were able to allocate the path, and if we
927 * find a matching device and it isn't already
928 * marked as part of a changer, then we add it to
929 * the current changer.
931 if ((status == CAM_REQ_CMP)
932 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
933 && ((((struct cd_softc *)periph->softc)->flags &
934 CD_FLAG_CHANGER) == 0)) {
935 struct cd_softc *nsoftc;
937 nsoftc = (struct cd_softc *)nperiph->softc;
939 nsoftc->flags |= CD_FLAG_CHANGER;
940 nchanger->num_devices++;
941 if (camq_resize(&nchanger->devq,
942 nchanger->num_devices) != CAM_REQ_CMP) {
943 kprintf("cdregister: camq_resize "
944 "failed, changer support may "
947 nsoftc->changer = nchanger;
948 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
950 STAILQ_INSERT_TAIL(&nchanger->chluns,
951 nsoftc, changer_links);
953 } else if (status == CAM_REQ_CMP)
956 kprintf("cdregister: unable to allocate path\n"
957 "cdregister: changer support may be "
961 softc->changer = nchanger;
962 softc->pinfo.index = CAM_UNQUEUED_INDEX;
963 nchanger->num_devices++;
964 if (camq_resize(&nchanger->devq,
965 nchanger->num_devices) != CAM_REQ_CMP) {
966 kprintf("cdregister: camq_resize "
967 "failed, changer support may "
970 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
978 * Refcount and block open attempts until we are setup
981 cam_periph_hold(periph, 0);
983 if ((softc->flags & CD_FLAG_CHANGER) == 0)
984 xpt_schedule(periph, /*priority*/5);
986 cdschedule(periph, /*priority*/ 5);
992 cdopen(struct dev_open_args *ap)
994 cdev_t dev = ap->a_head.a_dev;
995 struct cam_periph *periph;
996 struct cd_softc *softc;
1000 periph = cam_extend_get(cdperiphs, unit);
1005 softc = (struct cd_softc *)periph->softc;
1007 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1010 cam_periph_lock(periph);
1012 if (softc->flags & CD_FLAG_INVALID) {
1013 cam_periph_unlock(periph);
1014 cam_periph_release(periph);
1018 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1019 cam_periph_unlock(periph);
1020 cam_periph_release(periph);
1024 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1025 if (softc->flags & CD_FLAG_OPEN)
1026 cam_periph_release(periph);
1028 softc->flags |= CD_FLAG_OPEN;
1031 * Check for media, and set the appropriate flags. We don't bail
1032 * if we don't have media, but then we don't allow anything but the
1033 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1035 * XXX KDM for now, we do fail the open if we don't have media. We
1036 * can change this once we've figured out how to make the slice
1037 * code work well with media changing underneath it.
1039 error = cdcheckmedia(periph);
1041 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1042 cam_periph_unhold(periph, 1);
1043 /* stays acquired */
1049 cdclose(struct dev_close_args *ap)
1051 cdev_t dev = ap->a_head.a_dev;
1052 struct cam_periph *periph;
1053 struct cd_softc *softc;
1054 struct disk_info *info;
1058 periph = cam_extend_get(cdperiphs, unit);
1062 softc = (struct cd_softc *)periph->softc;
1064 cam_periph_lock(periph);
1065 cam_periph_hold(periph, 0);
1067 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1068 cdprevent(periph, PR_ALLOW);
1071 * Unconditionally set the dsopen() flags back to their default
1074 info = &softc->disk.d_info;
1075 info->d_dsflags &= ~DSO_NOLABELS;
1076 info->d_dsflags |= DSO_COMPATLABEL;
1079 * Since we're closing this CD, mark the blocksize as unavailable.
1080 * It will be marked as available when the CD is opened again.
1082 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1085 * We'll check the media and toc again at the next open().
1087 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1089 cam_periph_unhold(periph, 1);
1090 cam_periph_release(periph);
1096 cdshorttimeout(void *arg)
1098 struct cdchanger *changer;
1100 changer = (struct cdchanger *)arg;
1102 /* Always clear the short timeout flag, since that's what we're in */
1103 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1106 * Check to see if there is any more pending or outstanding I/O for
1107 * this device. If not, move it out of the active slot.
1109 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1110 && (changer->cur_device->outstanding_cmds == 0)) {
1111 changer->flags |= CHANGER_MANUAL_CALL;
1112 cdrunchangerqueue(changer);
1117 * This is a wrapper for xpt_schedule. It only applies to changers.
1120 cdschedule(struct cam_periph *periph, int priority)
1122 struct cd_softc *softc;
1124 softc = (struct cd_softc *)periph->softc;
1127 * If this device isn't currently queued, and if it isn't
1128 * the active device, then we queue this device and run the
1129 * changer queue if there is no timeout scheduled to do it.
1130 * If this device is the active device, just schedule it
1131 * to run again. If this device is queued, there should be
1132 * a timeout in place already that will make sure it runs.
1134 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1135 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1137 * We don't do anything with the priority here.
1138 * This is strictly a fifo queue.
1140 softc->pinfo.priority = 1;
1141 softc->pinfo.generation = ++softc->changer->devq.generation;
1142 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1145 * Since we just put a device in the changer queue,
1146 * check and see if there is a timeout scheduled for
1147 * this changer. If so, let the timeout handle
1148 * switching this device into the active slot. If
1149 * not, manually call the timeout routine to
1152 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1153 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1154 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1155 softc->changer->flags |= CHANGER_MANUAL_CALL;
1156 cdrunchangerqueue(softc->changer);
1158 } else if ((softc->flags & CD_FLAG_ACTIVE)
1159 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1160 xpt_schedule(periph, priority);
1165 cdrunchangerqueue(void *arg)
1167 struct cd_softc *softc;
1168 struct cdchanger *changer;
1169 int called_from_timeout;
1171 changer = (struct cdchanger *)arg;
1174 * If we have NOT been called from cdstrategy() or cddone(), and
1175 * instead from a timeout routine, go ahead and clear the
1178 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1179 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1180 called_from_timeout = 1;
1182 called_from_timeout = 0;
1184 /* Always clear the manual call flag */
1185 changer->flags &= ~CHANGER_MANUAL_CALL;
1187 /* nothing to do if the queue is empty */
1188 if (changer->devq.entries <= 0) {
1193 * If the changer queue is frozen, that means we have an active
1196 if (changer->devq.qfrozen_cnt > 0) {
1199 * We always need to reset the frozen count and clear the
1202 changer->devq.qfrozen_cnt--;
1203 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1204 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1206 if (changer->cur_device->outstanding_cmds > 0) {
1207 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1208 changer->cur_device->bufs_left =
1209 changer->cur_device->outstanding_cmds;
1210 if (called_from_timeout) {
1211 callout_reset(&changer->long_handle,
1212 changer_max_busy_seconds * hz,
1213 cdrunchangerqueue, changer);
1214 changer->flags |= CHANGER_TIMEOUT_SCHED;
1220 * Check to see whether the current device has any I/O left
1221 * to do. If so, requeue it at the end of the queue. If
1222 * not, there is no need to requeue it.
1224 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1226 changer->cur_device->pinfo.generation =
1227 ++changer->devq.generation;
1228 camq_insert(&changer->devq,
1229 (cam_pinfo *)changer->cur_device);
1233 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1235 changer->cur_device = softc;
1237 changer->devq.qfrozen_cnt++;
1238 softc->flags |= CD_FLAG_ACTIVE;
1240 /* Just in case this device is waiting */
1241 wakeup(&softc->changer);
1242 xpt_schedule(softc->periph, /*priority*/ 1);
1245 * Get rid of any pending timeouts, and set a flag to schedule new
1246 * ones so this device gets its full time quantum.
1248 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1249 callout_stop(&changer->long_handle);
1250 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1253 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1254 callout_stop(&changer->short_handle);
1255 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1259 * We need to schedule timeouts, but we only do this after the
1260 * first transaction has completed. This eliminates the changer
1263 changer->flags |= CHANGER_NEED_TIMEOUT;
1267 cdchangerschedule(struct cd_softc *softc)
1269 struct cdchanger *changer;
1271 changer = softc->changer;
1274 * If this is a changer, and this is the current device,
1275 * and this device has at least the minimum time quantum to
1276 * run, see if we can switch it out.
1278 if ((softc->flags & CD_FLAG_ACTIVE)
1279 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1280 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1282 * We try three things here. The first is that we
1283 * check to see whether the schedule on completion
1284 * flag is set. If it is, we decrement the number
1285 * of buffers left, and if it's zero, we reschedule.
1286 * Next, we check to see whether the pending buffer
1287 * queue is empty and whether there are no
1288 * outstanding transactions. If so, we reschedule.
1289 * Next, we see if the pending buffer queue is empty.
1290 * If it is, we set the number of buffers left to
1291 * the current active buffer count and set the
1292 * schedule on complete flag.
1294 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1295 if (--softc->bufs_left == 0) {
1296 softc->changer->flags |=
1297 CHANGER_MANUAL_CALL;
1298 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1299 cdrunchangerqueue(softc->changer);
1301 } else if ((bioq_first(&softc->bio_queue) == NULL)
1302 && (softc->outstanding_cmds == 0)) {
1303 softc->changer->flags |= CHANGER_MANUAL_CALL;
1304 cdrunchangerqueue(softc->changer);
1306 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1307 && (softc->flags & CD_FLAG_ACTIVE)) {
1310 * Now that the first transaction to this
1311 * particular device has completed, we can go ahead
1312 * and schedule our timeouts.
1314 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1315 callout_reset(&changer->long_handle,
1316 changer_max_busy_seconds * hz,
1317 cdrunchangerqueue, changer);
1318 changer->flags |= CHANGER_TIMEOUT_SCHED;
1320 kprintf("cdchangerschedule: already have a long"
1323 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1324 callout_reset(&changer->short_handle,
1325 changer_min_busy_seconds * hz,
1326 cdshorttimeout, changer);
1327 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1329 kprintf("cdchangerschedule: already have a short "
1333 * We just scheduled timeouts, no need to schedule
1336 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1342 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1343 u_int32_t cam_flags,
1344 u_int32_t sense_flags),
1345 u_int32_t cam_flags, u_int32_t sense_flags)
1347 struct cd_softc *softc;
1348 struct cam_periph *periph;
1351 periph = xpt_path_periph(ccb->ccb_h.path);
1352 softc = (struct cd_softc *)periph->softc;
1354 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1355 &softc->device_stats);
1357 if (softc->flags & CD_FLAG_CHANGER)
1358 cdchangerschedule(softc);
1364 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1366 struct cd_softc *softc;
1368 softc = (struct cd_softc *)periph->softc;
1370 if (softc->flags & CD_FLAG_CHANGER) {
1372 * This should work the first time this device is woken up,
1373 * but just in case it doesn't, we use a while loop.
1375 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1377 * If this changer isn't already queued, queue it up.
1379 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1380 softc->pinfo.priority = 1;
1381 softc->pinfo.generation =
1382 ++softc->changer->devq.generation;
1383 camq_insert(&softc->changer->devq,
1384 (cam_pinfo *)softc);
1386 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1387 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1388 && ((softc->changer->flags
1389 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1390 softc->changer->flags |= CHANGER_MANUAL_CALL;
1391 cdrunchangerqueue(softc->changer);
1393 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1397 return(cam_periph_getccb(periph, priority));
1401 * Actually translate the requested transfer into one the physical driver
1402 * can understand. The transfer is described by a buf and will include
1403 * only one physical transfer.
1406 cdstrategy(struct dev_strategy_args *ap)
1408 cdev_t dev = ap->a_head.a_dev;
1409 struct bio *bio = ap->a_bio;
1410 struct buf *bp = bio->bio_buf;
1411 struct cam_periph *periph;
1412 struct cd_softc *softc;
1416 periph = cam_extend_get(cdperiphs, unit);
1417 if (periph == NULL) {
1418 bp->b_error = ENXIO;
1422 cam_periph_lock(periph);
1423 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1425 softc = (struct cd_softc *)periph->softc;
1428 * If the device has been made invalid, error out
1430 if ((softc->flags & CD_FLAG_INVALID)) {
1431 cam_periph_unlock(periph);
1432 bp->b_error = ENXIO;
1437 * If we don't have valid media, look for it before trying to
1440 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1443 error = cdcheckmedia(periph);
1445 cam_periph_unlock(periph);
1446 bp->b_error = error;
1452 * Place it in the queue of disk activities for this disk
1454 bioqdisksort(&softc->bio_queue, bio);
1457 * Schedule ourselves for performing the work. We do things
1458 * differently for changers.
1460 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1461 xpt_schedule(periph, /* XXX priority */1);
1463 cdschedule(periph, /* priority */ 1);
1465 cam_periph_unlock(periph);
1468 bp->b_flags |= B_ERROR;
1470 * Correctly set the buf to indicate a completed xfer
1472 bp->b_resid = bp->b_bcount;
1478 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1480 struct cd_softc *softc;
1483 struct ccb_scsiio *csio;
1484 struct scsi_read_capacity_data *rcap;
1486 softc = (struct cd_softc *)periph->softc;
1488 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1490 switch (softc->state) {
1491 case CD_STATE_NORMAL:
1493 bio = bioq_first(&softc->bio_queue);
1494 if (periph->immediate_priority <= periph->pinfo.priority) {
1495 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1497 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1499 periph->immediate_priority = CAM_PRIORITY_NONE;
1500 wakeup(&periph->ccb_list);
1501 } else if (bio == NULL) {
1502 xpt_release_ccb(start_ccb);
1505 bioq_remove(&softc->bio_queue, bio);
1507 devstat_start_transaction(&softc->device_stats);
1509 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1511 scsi_read_write(&start_ccb->csio,
1513 /* cbfcnp */ cddone,
1514 (bp->b_flags & B_ORDERED) != 0 ?
1517 /* read */(bp->b_cmd == BUF_CMD_READ),
1519 /* minimum_cmd_size */ 10,
1521 bio->bio_offset / softc->params.blksize,
1522 bp->b_bcount / softc->params.blksize,
1523 /* data_ptr */ bp->b_data,
1524 /* dxfer_len */ bp->b_bcount,
1525 /* sense_len */ SSD_FULL_SIZE,
1526 /* timeout */ 30000);
1527 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1530 LIST_INSERT_HEAD(&softc->pending_ccbs,
1531 &start_ccb->ccb_h, periph_links.le);
1533 softc->outstanding_cmds++;
1534 /* We expect a unit attention from this device */
1535 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1536 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1537 softc->flags &= ~CD_FLAG_RETRY_UA;
1540 start_ccb->ccb_h.ccb_bio = bio;
1541 bio = bioq_first(&softc->bio_queue);
1543 xpt_action(start_ccb);
1546 /* Have more work to do, so ensure we stay scheduled */
1547 xpt_schedule(periph, /* XXX priority */1);
1551 case CD_STATE_PROBE:
1554 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1555 csio = &start_ccb->csio;
1556 scsi_read_capacity(csio,
1563 start_ccb->ccb_h.ccb_bio = NULL;
1564 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1565 xpt_action(start_ccb);
1572 cddone(struct cam_periph *periph, union ccb *done_ccb)
1574 struct cd_softc *softc;
1575 struct ccb_scsiio *csio;
1577 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1579 softc = (struct cd_softc *)periph->softc;
1580 csio = &done_ccb->csio;
1582 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1583 case CD_CCB_BUFFER_IO:
1589 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1593 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1596 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1601 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1602 if (error == ERESTART) {
1604 * A retry was scheuled, so
1615 xpt_print(periph->path,
1616 "cddone: got error %#x back\n", error);
1617 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1618 bioq_remove(&softc->bio_queue, q_bio);
1619 q_bp = q_bio->bio_buf;
1620 q_bp->b_resid = q_bp->b_bcount;
1621 q_bp->b_error = EIO;
1622 q_bp->b_flags |= B_ERROR;
1625 bp->b_resid = bp->b_bcount;
1626 bp->b_error = error;
1627 bp->b_flags |= B_ERROR;
1628 cam_release_devq(done_ccb->ccb_h.path,
1632 /*getcount_only*/0);
1635 bp->b_resid = csio->resid;
1637 if (bp->b_resid != 0) {
1638 /* Short transfer ??? */
1639 bp->b_flags |= B_ERROR;
1643 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1644 softc->outstanding_cmds--;
1646 if (softc->flags & CD_FLAG_CHANGER)
1647 cdchangerschedule(softc);
1649 devstat_end_transaction_buf(&softc->device_stats, bp);
1656 * Currently (9/30/97) the longest possible announce
1657 * buffer is 108 bytes, for the first error case below.
1658 * That is 39 bytes for the basic string, 16 bytes for the
1659 * biggest sense key (hardware error), 52 bytes for the
1660 * text of the largest sense qualifier valid for a CDROM,
1661 * (0x72, 0x03 or 0x04, 0x03), and one byte for the
1662 * null terminating character. To allow for longer strings,
1663 * the announce buffer is 120 bytes.
1665 * We need to call disk_setdiskinfo() before the disk
1666 * subsystem will allow any opens. Because additional
1667 * probe code is in cdopen() we do this now whether a CD
1668 * is present or not.
1670 struct scsi_read_capacity_data *rdcap;
1671 struct disk_info info;
1672 char announce_buf[120];
1673 struct cd_params *cdp;
1675 cdp = &softc->params;
1676 bzero(&info, sizeof(info));
1677 info.d_type = DTYPE_SCSI;
1678 info.d_dsflags &= ~DSO_COMPATLABEL;
1679 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
1680 info.d_serialno = xpt_path_serialno(periph->path);
1682 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1684 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1685 cdp->blksize = scsi_4btoul (rdcap->length);
1687 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1688 ksnprintf(announce_buf, sizeof(announce_buf),
1689 "cd present [%lu x %lu byte records]",
1690 cdp->disksize, (u_long)cdp->blksize);
1691 info.d_media_blksize = cdp->blksize;
1692 info.d_media_blocks = cdp->disksize;
1693 disk_setdiskinfo(&softc->disk, &info);
1698 * Retry any UNIT ATTENTION type errors. They
1699 * are expected at boot.
1701 error = cderror(done_ccb, CAM_RETRY_SELTO,
1702 SF_RETRY_UA | SF_NO_PRINT);
1703 if (error == ERESTART) {
1705 * A retry was scheuled, so
1709 } else if (error != 0) {
1710 struct scsi_sense_data *sense;
1712 int sense_key, error_code;
1715 struct ccb_getdev cgd;
1717 /* Don't wedge this device's queue */
1718 cam_release_devq(done_ccb->ccb_h.path,
1722 /*getcount_only*/0);
1724 status = done_ccb->ccb_h.status;
1726 xpt_setup_ccb(&cgd.ccb_h,
1727 done_ccb->ccb_h.path,
1729 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1730 xpt_action((union ccb *)&cgd);
1732 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1733 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1734 || ((status & CAM_AUTOSNS_VALID) == 0))
1740 sense = &csio->sense_data;
1741 scsi_extract_sense(sense, &error_code,
1746 * Attach to anything that claims to be a
1747 * CDROM or WORM device, as long as it
1748 * doesn't return a "Logical unit not
1749 * supported" (0x25) error.
1751 if ((have_sense) && (asc != 0x25)
1752 && (error_code == SSD_CURRENT_ERROR)) {
1753 const char *sense_key_desc;
1754 const char *asc_desc;
1756 scsi_sense_desc(sense_key, asc, ascq,
1760 ksnprintf(announce_buf,
1761 sizeof(announce_buf),
1762 "Attempt to query device "
1763 "size failed: %s, %s",
1766 info.d_media_blksize = 2048;
1767 disk_setdiskinfo(&softc->disk, &info);
1768 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1770 * We only print out an error for
1771 * CDROM type devices. For WORM
1772 * devices, we don't print out an
1773 * error since a few WORM devices
1774 * don't support CDROM commands.
1775 * If we have sense information, go
1776 * ahead and print it out.
1777 * Otherwise, just say that we
1782 * Just print out the error, not
1783 * the full probe message, when we
1790 xpt_print(periph->path,
1791 "got CAM status %#x\n",
1792 done_ccb->ccb_h.status);
1794 xpt_print(periph->path, "fatal error, "
1795 "failed to attach to device\n");
1797 * Invalidate this peripheral.
1799 cam_periph_invalidate(periph);
1801 announce_buf[0] = '\0';
1805 * Invalidate this peripheral.
1807 cam_periph_invalidate(periph);
1808 announce_buf[0] = '\0';
1812 kfree(rdcap, M_SCSICD);
1813 if (announce_buf[0] != '\0') {
1814 xpt_announce_periph(periph, announce_buf);
1815 if (softc->flags & CD_FLAG_CHANGER)
1816 cdchangerschedule(softc);
1818 * Create our sysctl variables, now that we know
1819 * we have successfully attached.
1821 taskqueue_enqueue(taskqueue_thread[mycpuid],
1822 &softc->sysctl_task);
1824 softc->state = CD_STATE_NORMAL;
1826 * Since our peripheral may be invalidated by an error
1827 * above or an external event, we must release our CCB
1828 * before releasing the probe lock on the peripheral.
1829 * The peripheral will only go away once the last lock
1830 * is removed, and we need it around for the CCB release
1833 xpt_release_ccb(done_ccb);
1834 cam_periph_unhold(periph, 0);
1837 case CD_CCB_WAITING:
1839 /* Caller will release the CCB */
1840 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1841 ("trying to wakeup ccbwait\n"));
1843 wakeup(&done_ccb->ccb_h.cbfcnp);
1849 xpt_release_ccb(done_ccb);
1852 static union cd_pages *
1853 cdgetpage(struct cd_mode_params *mode_params)
1855 union cd_pages *page;
1857 if (mode_params->cdb_size == 10)
1858 page = (union cd_pages *)find_mode_page_10(
1859 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1861 page = (union cd_pages *)find_mode_page_6(
1862 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1868 cdgetpagesize(int page_num)
1872 for (i = 0; i < (sizeof(cd_page_size_table)/
1873 sizeof(cd_page_size_table[0])); i++) {
1874 if (cd_page_size_table[i].page == page_num)
1875 return (cd_page_size_table[i].page_size);
1881 cdioctl(struct dev_ioctl_args *ap)
1883 cdev_t dev = ap->a_head.a_dev;
1884 caddr_t addr = ap->a_data;
1885 struct cam_periph *periph;
1886 struct cd_softc *softc;
1887 int unit, error = 0;
1891 periph = cam_extend_get(cdperiphs, unit);
1895 cam_periph_lock(periph);
1896 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1898 softc = (struct cd_softc *)periph->softc;
1900 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1901 ("trying to do ioctl %#lx\n", ap->a_cmd));
1903 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1904 cam_periph_unlock(periph);
1905 cam_periph_release(periph);
1910 * If we don't have media loaded, check for it. If still don't
1911 * have media loaded, we can only do a load or eject.
1913 * We only care whether media is loaded if this is a cd-specific ioctl
1914 * (thus the IOCGROUP check below). Note that this will break if
1915 * anyone adds any ioctls into the switch statement below that don't
1916 * have their ioctl group set to 'c'.
1918 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1919 && ((ap->a_cmd != CDIOCCLOSE)
1920 && (ap->a_cmd != CDIOCEJECT))
1921 && (IOCGROUP(ap->a_cmd) == 'c')) {
1922 error = cdcheckmedia(periph);
1924 cam_periph_unhold(periph, 1);
1929 * Drop the lock here so later mallocs can use WAITOK. The periph
1930 * is essentially locked still with the cam_periph_hold call above.
1932 cam_periph_unlock(periph);
1934 switch (ap->a_cmd) {
1936 case CDIOCPLAYTRACKS:
1938 struct ioc_play_track *args
1939 = (struct ioc_play_track *)addr;
1940 struct cd_mode_params params;
1941 union cd_pages *page;
1943 params.alloc_len = sizeof(union cd_mode_data_6_10);
1944 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1947 cam_periph_lock(periph);
1948 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1949 ("trying to do CDIOCPLAYTRACKS\n"));
1951 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1953 kfree(params.mode_buf, M_SCSICD);
1954 cam_periph_unlock(periph);
1957 page = cdgetpage(¶ms);
1959 page->audio.flags &= ~CD_PA_SOTC;
1960 page->audio.flags |= CD_PA_IMMED;
1961 error = cdsetmode(periph, ¶ms);
1962 kfree(params.mode_buf, M_SCSICD);
1964 cam_periph_unlock(periph);
1969 * This was originally implemented with the PLAY
1970 * AUDIO TRACK INDEX command, but that command was
1971 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1972 * drives support it but ATAPI and ATAPI-derivative
1973 * drives don't seem to support it. So we keep a
1974 * cache of the table of contents and translate
1975 * track numbers to MSF format.
1977 if (softc->flags & CD_FLAG_VALID_TOC) {
1978 union msf_lba *sentry, *eentry;
1981 if (args->end_track <
1982 softc->toc.header.ending_track + 1)
1984 if (args->end_track >
1985 softc->toc.header.ending_track + 1)
1987 softc->toc.header.ending_track + 1;
1988 st = args->start_track -
1989 softc->toc.header.starting_track;
1990 et = args->end_track -
1991 softc->toc.header.starting_track;
1994 || (st > (softc->toc.header.ending_track -
1995 softc->toc.header.starting_track))) {
1999 sentry = &softc->toc.entries[st].addr;
2000 eentry = &softc->toc.entries[et].addr;
2001 error = cdplaymsf(periph,
2010 * If we don't have a valid TOC, try the
2011 * play track index command. It is part of
2012 * the SCSI-2 spec, but was removed in the
2013 * MMC specs. ATAPI and ATAPI-derived
2014 * drives don't support it.
2016 if (softc->quirks & CD_Q_BCD_TRACKS) {
2018 bin2bcd(args->start_track);
2020 bin2bcd(args->end_track);
2022 error = cdplaytracks(periph,
2028 cam_periph_unlock(periph);
2033 struct ioc_play_msf *args
2034 = (struct ioc_play_msf *) addr;
2035 struct cd_mode_params params;
2036 union cd_pages *page;
2038 params.alloc_len = sizeof(union cd_mode_data_6_10);
2039 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2042 cam_periph_lock(periph);
2043 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2044 ("trying to do CDIOCPLAYMSF\n"));
2046 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2048 kfree(params.mode_buf, M_SCSICD);
2049 cam_periph_unlock(periph);
2052 page = cdgetpage(¶ms);
2054 page->audio.flags &= ~CD_PA_SOTC;
2055 page->audio.flags |= CD_PA_IMMED;
2056 error = cdsetmode(periph, ¶ms);
2057 kfree(params.mode_buf, M_SCSICD);
2059 cam_periph_unlock(periph);
2062 error = cdplaymsf(periph,
2069 cam_periph_unlock(periph);
2072 case CDIOCPLAYBLOCKS:
2074 struct ioc_play_blocks *args
2075 = (struct ioc_play_blocks *) addr;
2076 struct cd_mode_params params;
2077 union cd_pages *page;
2079 params.alloc_len = sizeof(union cd_mode_data_6_10);
2080 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2082 cam_periph_lock(periph);
2083 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2084 ("trying to do CDIOCPLAYBLOCKS\n"));
2087 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2089 kfree(params.mode_buf, M_SCSICD);
2090 cam_periph_unlock(periph);
2093 page = cdgetpage(¶ms);
2095 page->audio.flags &= ~CD_PA_SOTC;
2096 page->audio.flags |= CD_PA_IMMED;
2097 error = cdsetmode(periph, ¶ms);
2098 kfree(params.mode_buf, M_SCSICD);
2100 cam_periph_unlock(periph);
2103 error = cdplay(periph, args->blk, args->len);
2104 cam_periph_unlock(periph);
2107 case CDIOCREADSUBCHANNEL:
2109 struct ioc_read_subchannel *args
2110 = (struct ioc_read_subchannel *) addr;
2111 struct cd_sub_channel_info *data;
2112 u_int32_t len = args->data_len;
2114 data = kmalloc(sizeof(struct cd_sub_channel_info),
2115 M_SCSICD, M_WAITOK);
2117 cam_periph_lock(periph);
2118 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2119 ("trying to do CDIOCREADSUBCHANNEL\n"));
2121 if ((len > sizeof(struct cd_sub_channel_info)) ||
2122 (len < sizeof(struct cd_sub_channel_header))) {
2124 "scsi_cd: cdioctl: "
2125 "cdioreadsubchannel: error, len=%d\n",
2128 kfree(data, M_SCSICD);
2129 cam_periph_unlock(periph);
2133 if (softc->quirks & CD_Q_BCD_TRACKS)
2134 args->track = bin2bcd(args->track);
2136 error = cdreadsubchannel(periph, args->address_format,
2137 args->data_format, args->track, data, len);
2140 kfree(data, M_SCSICD);
2141 cam_periph_unlock(periph);
2144 if (softc->quirks & CD_Q_BCD_TRACKS)
2145 data->what.track_info.track_number =
2146 bcd2bin(data->what.track_info.track_number);
2147 len = min(len, ((data->header.data_len[0] << 8) +
2148 data->header.data_len[1] +
2149 sizeof(struct cd_sub_channel_header)));
2150 cam_periph_unlock(periph);
2151 if (copyout(data, args->data, len) != 0) {
2154 kfree(data, M_SCSICD);
2158 case CDIOREADTOCHEADER:
2160 struct ioc_toc_header *th;
2162 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2165 cam_periph_lock(periph);
2166 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2167 ("trying to do CDIOREADTOCHEADER\n"));
2169 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2170 sizeof (*th), /*sense_flags*/0);
2172 kfree(th, M_SCSICD);
2173 cam_periph_unlock(periph);
2176 if (softc->quirks & CD_Q_BCD_TRACKS) {
2177 /* we are going to have to convert the BCD
2178 * encoding on the cd to what is expected
2180 th->starting_track =
2181 bcd2bin(th->starting_track);
2182 th->ending_track = bcd2bin(th->ending_track);
2184 th->len = ntohs(th->len);
2185 bcopy(th, addr, sizeof(*th));
2186 kfree(th, M_SCSICD);
2187 cam_periph_unlock(periph);
2190 case CDIOREADTOCENTRYS:
2192 struct cd_tocdata *data;
2193 struct cd_toc_single *lead;
2194 struct ioc_read_toc_entry *te =
2195 (struct ioc_read_toc_entry *) addr;
2196 struct ioc_toc_header *th;
2197 u_int32_t len, readlen, idx, num;
2198 u_int32_t starting_track = te->starting_track;
2200 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2201 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2203 cam_periph_lock(periph);
2204 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2205 ("trying to do CDIOREADTOCENTRYS\n"));
2207 if (te->data_len < sizeof(struct cd_toc_entry)
2208 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2209 || (te->address_format != CD_MSF_FORMAT
2210 && te->address_format != CD_LBA_FORMAT)) {
2212 kprintf("scsi_cd: error in readtocentries, "
2213 "returning EINVAL\n");
2214 kfree(data, M_SCSICD);
2215 kfree(lead, M_SCSICD);
2216 cam_periph_unlock(periph);
2221 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2222 sizeof (*th), /*sense_flags*/0);
2224 kfree(data, M_SCSICD);
2225 kfree(lead, M_SCSICD);
2226 cam_periph_unlock(periph);
2230 if (softc->quirks & CD_Q_BCD_TRACKS) {
2231 /* we are going to have to convert the BCD
2232 * encoding on the cd to what is expected
2234 th->starting_track =
2235 bcd2bin(th->starting_track);
2236 th->ending_track = bcd2bin(th->ending_track);
2239 if (starting_track == 0)
2240 starting_track = th->starting_track;
2241 else if (starting_track == LEADOUT)
2242 starting_track = th->ending_track + 1;
2243 else if (starting_track < th->starting_track ||
2244 starting_track > th->ending_track + 1) {
2245 kprintf("scsi_cd: error in readtocentries, "
2246 "returning EINVAL\n");
2247 kfree(data, M_SCSICD);
2248 kfree(lead, M_SCSICD);
2249 cam_periph_unlock(periph);
2254 /* calculate reading length without leadout entry */
2255 readlen = (th->ending_track - starting_track + 1) *
2256 sizeof(struct cd_toc_entry);
2258 /* and with leadout entry */
2259 len = readlen + sizeof(struct cd_toc_entry);
2260 if (te->data_len < len) {
2265 if (len > sizeof(data->entries)) {
2266 kprintf("scsi_cd: error in readtocentries, "
2267 "returning EINVAL\n");
2269 kfree(data, M_SCSICD);
2270 kfree(lead, M_SCSICD);
2271 cam_periph_unlock(periph);
2274 num = len / sizeof(struct cd_toc_entry);
2277 error = cdreadtoc(periph, te->address_format,
2280 readlen + sizeof (*th),
2283 kfree(data, M_SCSICD);
2284 kfree(lead, M_SCSICD);
2285 cam_periph_unlock(periph);
2290 /* make leadout entry if needed */
2291 idx = starting_track + num - 1;
2292 if (softc->quirks & CD_Q_BCD_TRACKS)
2293 th->ending_track = bcd2bin(th->ending_track);
2294 if (idx == th->ending_track + 1) {
2295 error = cdreadtoc(periph, te->address_format,
2296 LEADOUT, (u_int8_t *)lead,
2300 kfree(data, M_SCSICD);
2301 kfree(lead, M_SCSICD);
2302 cam_periph_unlock(periph);
2305 data->entries[idx - starting_track] =
2308 if (softc->quirks & CD_Q_BCD_TRACKS) {
2309 for (idx = 0; idx < num - 1; idx++) {
2310 data->entries[idx].track =
2311 bcd2bin(data->entries[idx].track);
2315 cam_periph_unlock(periph);
2316 error = copyout(data->entries, te->data, len);
2317 kfree(data, M_SCSICD);
2318 kfree(lead, M_SCSICD);
2321 case CDIOREADTOCENTRY:
2323 struct cd_toc_single *data;
2324 struct ioc_read_toc_single_entry *te =
2325 (struct ioc_read_toc_single_entry *) addr;
2326 struct ioc_toc_header *th;
2329 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2331 cam_periph_lock(periph);
2332 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2333 ("trying to do CDIOREADTOCENTRY\n"));
2335 if (te->address_format != CD_MSF_FORMAT
2336 && te->address_format != CD_LBA_FORMAT) {
2337 kprintf("error in readtocentry, "
2338 " returning EINVAL\n");
2339 kfree(data, M_SCSICD);
2341 cam_periph_unlock(periph);
2346 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2347 sizeof (*th), /*sense_flags*/0);
2349 kfree(data, M_SCSICD);
2350 cam_periph_unlock(periph);
2354 if (softc->quirks & CD_Q_BCD_TRACKS) {
2355 /* we are going to have to convert the BCD
2356 * encoding on the cd to what is expected
2358 th->starting_track =
2359 bcd2bin(th->starting_track);
2360 th->ending_track = bcd2bin(th->ending_track);
2364 track = th->starting_track;
2365 else if (track == LEADOUT)
2367 else if (track < th->starting_track ||
2368 track > th->ending_track + 1) {
2369 kprintf("error in readtocentry, "
2370 " returning EINVAL\n");
2371 kfree(data, M_SCSICD);
2373 cam_periph_unlock(periph);
2377 error = cdreadtoc(periph, te->address_format, track,
2378 (u_int8_t *)data, sizeof(*data),
2381 kfree(data, M_SCSICD);
2382 cam_periph_unlock(periph);
2386 if (softc->quirks & CD_Q_BCD_TRACKS)
2387 data->entry.track = bcd2bin(data->entry.track);
2388 bcopy(&data->entry, &te->entry,
2389 sizeof(struct cd_toc_entry));
2390 kfree(data, M_SCSICD);
2391 cam_periph_unlock(periph);
2396 struct ioc_patch *arg = (struct ioc_patch *)addr;
2397 struct cd_mode_params params;
2398 union cd_pages *page;
2400 params.alloc_len = sizeof(union cd_mode_data_6_10);
2401 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2404 cam_periph_lock(periph);
2405 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2406 ("trying to do CDIOCSETPATCH\n"));
2408 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2410 kfree(params.mode_buf, M_SCSICD);
2411 cam_periph_unlock(periph);
2414 page = cdgetpage(¶ms);
2416 page->audio.port[LEFT_PORT].channels =
2418 page->audio.port[RIGHT_PORT].channels =
2420 page->audio.port[2].channels = arg->patch[2];
2421 page->audio.port[3].channels = arg->patch[3];
2422 error = cdsetmode(periph, ¶ms);
2423 kfree(params.mode_buf, M_SCSICD);
2424 cam_periph_unlock(periph);
2429 struct ioc_vol *arg = (struct ioc_vol *) addr;
2430 struct cd_mode_params params;
2431 union cd_pages *page;
2433 params.alloc_len = sizeof(union cd_mode_data_6_10);
2434 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2437 cam_periph_lock(periph);
2438 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2439 ("trying to do CDIOCGETVOL\n"));
2441 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2443 kfree(params.mode_buf, M_SCSICD);
2444 cam_periph_unlock(periph);
2447 page = cdgetpage(¶ms);
2449 arg->vol[LEFT_PORT] =
2450 page->audio.port[LEFT_PORT].volume;
2451 arg->vol[RIGHT_PORT] =
2452 page->audio.port[RIGHT_PORT].volume;
2453 arg->vol[2] = page->audio.port[2].volume;
2454 arg->vol[3] = page->audio.port[3].volume;
2455 kfree(params.mode_buf, M_SCSICD);
2456 cam_periph_unlock(periph);
2461 struct ioc_vol *arg = (struct ioc_vol *) addr;
2462 struct cd_mode_params params;
2463 union cd_pages *page;
2465 params.alloc_len = sizeof(union cd_mode_data_6_10);
2466 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2469 cam_periph_lock(periph);
2470 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2471 ("trying to do CDIOCSETVOL\n"));
2473 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2475 kfree(params.mode_buf, M_SCSICD);
2476 cam_periph_unlock(periph);
2479 page = cdgetpage(¶ms);
2481 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2482 page->audio.port[LEFT_PORT].volume =
2483 arg->vol[LEFT_PORT];
2484 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2485 page->audio.port[RIGHT_PORT].volume =
2486 arg->vol[RIGHT_PORT];
2487 page->audio.port[2].volume = arg->vol[2];
2488 page->audio.port[3].volume = arg->vol[3];
2489 error = cdsetmode(periph, ¶ms);
2490 cam_periph_unlock(periph);
2491 kfree(params.mode_buf, M_SCSICD);
2496 struct cd_mode_params params;
2497 union cd_pages *page;
2499 params.alloc_len = sizeof(union cd_mode_data_6_10);
2500 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2503 cam_periph_lock(periph);
2504 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2505 ("trying to do CDIOCSETMONO\n"));
2507 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2509 kfree(params.mode_buf, M_SCSICD);
2510 cam_periph_unlock(periph);
2513 page = cdgetpage(¶ms);
2515 page->audio.port[LEFT_PORT].channels =
2516 LEFT_CHANNEL | RIGHT_CHANNEL;
2517 page->audio.port[RIGHT_PORT].channels =
2518 LEFT_CHANNEL | RIGHT_CHANNEL;
2519 page->audio.port[2].channels = 0;
2520 page->audio.port[3].channels = 0;
2521 error = cdsetmode(periph, ¶ms);
2522 cam_periph_unlock(periph);
2523 kfree(params.mode_buf, M_SCSICD);
2526 case CDIOCSETSTEREO:
2528 struct cd_mode_params params;
2529 union cd_pages *page;
2531 params.alloc_len = sizeof(union cd_mode_data_6_10);
2532 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2535 cam_periph_lock(periph);
2536 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2537 ("trying to do CDIOCSETSTEREO\n"));
2539 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2541 kfree(params.mode_buf, M_SCSICD);
2542 cam_periph_unlock(periph);
2545 page = cdgetpage(¶ms);
2547 page->audio.port[LEFT_PORT].channels =
2549 page->audio.port[RIGHT_PORT].channels =
2551 page->audio.port[2].channels = 0;
2552 page->audio.port[3].channels = 0;
2553 error = cdsetmode(periph, ¶ms);
2554 kfree(params.mode_buf, M_SCSICD);
2555 cam_periph_unlock(periph);
2560 struct cd_mode_params params;
2561 union cd_pages *page;
2563 params.alloc_len = sizeof(union cd_mode_data_6_10);
2564 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2567 cam_periph_lock(periph);
2568 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2569 ("trying to do CDIOCSETMUTE\n"));
2571 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2573 kfree(¶ms, M_SCSICD);
2574 cam_periph_unlock(periph);
2577 page = cdgetpage(¶ms);
2579 page->audio.port[LEFT_PORT].channels = 0;
2580 page->audio.port[RIGHT_PORT].channels = 0;
2581 page->audio.port[2].channels = 0;
2582 page->audio.port[3].channels = 0;
2583 error = cdsetmode(periph, ¶ms);
2584 kfree(params.mode_buf, M_SCSICD);
2585 cam_periph_unlock(periph);
2590 struct cd_mode_params params;
2591 union cd_pages *page;
2593 params.alloc_len = sizeof(union cd_mode_data_6_10);
2594 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2597 cam_periph_lock(periph);
2598 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2599 ("trying to do CDIOCSETLEFT\n"));
2601 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2603 kfree(params.mode_buf, M_SCSICD);
2604 cam_periph_unlock(periph);
2607 page = cdgetpage(¶ms);
2609 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2610 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2611 page->audio.port[2].channels = 0;
2612 page->audio.port[3].channels = 0;
2613 error = cdsetmode(periph, ¶ms);
2614 kfree(params.mode_buf, M_SCSICD);
2615 cam_periph_unlock(periph);
2620 struct cd_mode_params params;
2621 union cd_pages *page;
2623 params.alloc_len = sizeof(union cd_mode_data_6_10);
2624 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2627 cam_periph_lock(periph);
2628 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2629 ("trying to do CDIOCSETRIGHT\n"));
2631 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2633 kfree(params.mode_buf, M_SCSICD);
2634 cam_periph_unlock(periph);
2637 page = cdgetpage(¶ms);
2639 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2640 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2641 page->audio.port[2].channels = 0;
2642 page->audio.port[3].channels = 0;
2643 error = cdsetmode(periph, ¶ms);
2644 kfree(params.mode_buf, M_SCSICD);
2645 cam_periph_unlock(periph);
2649 cam_periph_lock(periph);
2650 error = cdpause(periph, 1);
2651 cam_periph_unlock(periph);
2654 cam_periph_lock(periph);
2655 error = cdpause(periph, 0);
2656 cam_periph_unlock(periph);
2659 cam_periph_lock(periph);
2660 error = cdstartunit(periph, 0);
2661 cam_periph_unlock(periph);
2664 cam_periph_lock(periph);
2665 error = cdstartunit(periph, 1);
2666 cam_periph_unlock(periph);
2669 cam_periph_lock(periph);
2670 error = cdstopunit(periph, 0);
2671 cam_periph_unlock(periph);
2674 cam_periph_lock(periph);
2675 error = cdstopunit(periph, 1);
2676 cam_periph_unlock(periph);
2679 cam_periph_lock(periph);
2680 cdprevent(periph, PR_ALLOW);
2681 cam_periph_unlock(periph);
2684 cam_periph_lock(periph);
2685 cdprevent(periph, PR_PREVENT);
2686 cam_periph_unlock(periph);
2689 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2693 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2697 /* return (cd_reset(periph)); */
2700 case CDRIOCREADSPEED:
2701 cam_periph_lock(periph);
2702 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2703 cam_periph_unlock(periph);
2705 case CDRIOCWRITESPEED:
2706 cam_periph_lock(periph);
2707 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2708 cam_periph_unlock(periph);
2711 case DVDIOCREPORTKEY: {
2712 struct dvd_authinfo *authinfo;
2714 authinfo = (struct dvd_authinfo *)addr;
2716 cam_periph_lock(periph);
2717 if (ap->a_cmd == DVDIOCREPORTKEY)
2718 error = cdreportkey(periph, authinfo);
2720 error = cdsendkey(periph, authinfo);
2721 cam_periph_unlock(periph);
2724 case DVDIOCREADSTRUCTURE: {
2725 struct dvd_struct *dvdstruct;
2727 dvdstruct = (struct dvd_struct *)addr;
2729 cam_periph_lock(periph);
2730 error = cdreaddvdstructure(periph, dvdstruct);
2731 cam_periph_unlock(periph);
2736 cam_periph_lock(periph);
2737 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2738 cam_periph_unlock(periph);
2742 cam_periph_lock(periph);
2743 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2744 cam_periph_unhold(periph, 1);
2750 cdprevent(struct cam_periph *periph, int action)
2753 struct cd_softc *softc;
2756 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2758 softc = (struct cd_softc *)periph->softc;
2760 if (((action == PR_ALLOW)
2761 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2762 || ((action == PR_PREVENT)
2763 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2767 ccb = cdgetccb(periph, /* priority */ 1);
2769 scsi_prevent(&ccb->csio,
2775 /* timeout */60000);
2777 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2778 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2780 xpt_release_ccb(ccb);
2783 if (action == PR_ALLOW)
2784 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2786 softc->flags |= CD_FLAG_DISC_LOCKED;
2791 cdcheckmedia(struct cam_periph *periph)
2793 struct cd_softc *softc;
2794 struct ioc_toc_header *toch;
2795 struct cd_toc_single leadout;
2796 struct ccb_getdev cgd;
2797 u_int32_t size, toclen;
2798 int error, num_entries, cdindex;
2799 int first_track_audio;
2800 struct disk_info info;
2802 softc = (struct cd_softc *)periph->softc;
2804 first_track_audio = -1;
2807 cdprevent(periph, PR_PREVENT);
2810 * Set up disk info fields and tell the disk subsystem to reset
2811 * its internal copy of the label.
2813 bzero(&info, sizeof(info));
2814 info.d_type = DTYPE_SCSI;
2815 info.d_dsflags &= ~DSO_COMPATLABEL;
2816 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2817 info.d_serialno = xpt_path_serialno(periph->path);
2820 * Grab the inquiry data to get the vendor and product names.
2821 * Put them in the typename and packname for the label.
2823 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2824 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2825 xpt_action((union ccb *)&cgd);
2828 strncpy(label->d_typename, cgd.inq_data.vendor,
2829 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2830 strncpy(label->d_packname, cgd.inq_data.product,
2831 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2834 * Clear the valid media and TOC flags until we've verified that we
2837 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2840 * Get the disc size and block size. If we can't get it, we don't
2841 * have media, most likely.
2843 if ((error = cdsize(periph, &size)) != 0) {
2845 * Set a bogus sector size, so the slice code won't try to
2846 * divide by 0 and panic the kernel.
2848 info.d_media_blksize = 2048;
2849 disk_setdiskinfo(&softc->disk, &info);
2852 * Tell devstat(9) we don't have a blocksize.
2854 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2856 cdprevent(periph, PR_ALLOW);
2860 info.d_media_blksize = softc->params.blksize;
2861 info.d_media_blocks = softc->params.disksize;
2862 disk_setdiskinfo(&softc->disk, &info);
2865 * We unconditionally (re)set the blocksize each time the
2866 * CD device is opened. This is because the CD can change,
2867 * and therefore the blocksize might change.
2868 * XXX problems here if some slice or partition is still
2869 * open with the old size?
2871 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2872 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2873 softc->device_stats.block_size = softc->params.blksize;
2875 softc->flags |= CD_FLAG_VALID_MEDIA;
2879 * Now we check the table of contents. This (currently) is only
2880 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2881 * things like present a separate entry in /dev for each track,
2882 * like that acd(4) driver does.
2884 bzero(&softc->toc, sizeof(softc->toc));
2885 toch = &softc->toc.header;
2888 * We will get errors here for media that doesn't have a table of
2889 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2890 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2891 * has not been recorded (no complete session) and the Format codes
2892 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2893 * with an INVALID FIELD IN CDB. Devices that are not capable of
2894 * reading an incomplete session on DDC/CD-R/RW media shall report
2895 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2897 * So this isn't fatal if we can't read the table of contents, it
2898 * just means that the user won't be able to issue the play tracks
2899 * ioctl, and likely lots of other stuff won't work either. They
2900 * need to burn the CD before we can do a whole lot with it. So
2901 * we don't print anything here if we get an error back.
2903 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2906 * Errors in reading the table of contents aren't fatal, we just
2907 * won't have a valid table of contents cached.
2911 bzero(&softc->toc, sizeof(softc->toc));
2915 if (softc->quirks & CD_Q_BCD_TRACKS) {
2916 toch->starting_track = bcd2bin(toch->starting_track);
2917 toch->ending_track = bcd2bin(toch->ending_track);
2920 /* Number of TOC entries, plus leadout */
2921 num_entries = (toch->ending_track - toch->starting_track) + 2;
2923 if (num_entries <= 0)
2926 toclen = num_entries * sizeof(struct cd_toc_entry);
2928 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2929 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2933 bzero(&softc->toc, sizeof(softc->toc));
2937 if (softc->quirks & CD_Q_BCD_TRACKS) {
2938 toch->starting_track = bcd2bin(toch->starting_track);
2939 toch->ending_track = bcd2bin(toch->ending_track);
2941 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2944 * XXX KDM is this necessary? Probably only if the drive doesn't
2945 * return leadout information with the table of contents.
2947 cdindex = toch->starting_track + num_entries -1;
2948 if (cdindex == toch->ending_track + 1) {
2950 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2951 (u_int8_t *)&leadout, sizeof(leadout),
2957 softc->toc.entries[cdindex - toch->starting_track] =
2960 if (softc->quirks & CD_Q_BCD_TRACKS) {
2961 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2962 softc->toc.entries[cdindex].track =
2963 bcd2bin(softc->toc.entries[cdindex].track);
2968 * Run through the TOC entries, find the first entry and determine
2969 * whether it is an audio or data track.
2971 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2972 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2973 if (softc->toc.entries[cdindex].control & 0x04)
2974 first_track_audio = 0;
2976 first_track_audio = 1;
2982 * If first_track_audio is non-zero, we either have an error (e.g.
2983 * couldn't find the starting track) or the first track is an audio
2984 * track. If first_track_audio is 0, the first track is a data
2985 * track that could have a disklabel. Attempt to read the
2986 * disklabel off the media, just in case the user put one there.
2988 if (first_track_audio == 0) {
2989 info.d_dsflags |= DSO_COMPATLABEL;
2990 info.d_dsflags &= ~DSO_NOLABELS;
2991 disk_setdiskinfo(&softc->disk, &info);
2993 softc->flags |= CD_FLAG_VALID_TOC;
3000 cdsize(struct cam_periph *periph, u_int32_t *size)
3002 struct cd_softc *softc;
3004 struct scsi_read_capacity_data *rcap_buf;
3007 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3009 softc = (struct cd_softc *)periph->softc;
3011 ccb = cdgetccb(periph, /* priority */ 1);
3013 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
3014 M_SCSICD, M_INTWAIT | M_ZERO);
3016 scsi_read_capacity(&ccb->csio,
3022 /* timeout */20000);
3024 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3025 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3027 xpt_release_ccb(ccb);
3029 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3030 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3032 * SCSI-3 mandates that the reported blocksize shall be 2048.
3033 * Older drives sometimes report funny values, trim it down to
3034 * 2048, or other parts of the kernel will get confused.
3036 * XXX we leave drives alone that might report 512 bytes, as
3037 * well as drives reporting more weird sizes like perhaps 4K.
3039 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3040 softc->params.blksize = 2048;
3042 kfree(rcap_buf, M_SCSICD);
3043 *size = softc->params.disksize;
3050 cd6byteworkaround(union ccb *ccb)
3053 struct cam_periph *periph;
3054 struct cd_softc *softc;
3055 struct cd_mode_params *params;
3058 periph = xpt_path_periph(ccb->ccb_h.path);
3059 softc = (struct cd_softc *)periph->softc;
3061 cdb = ccb->csio.cdb_io.cdb_bytes;
3063 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3064 || ((cdb[0] != MODE_SENSE_6)
3065 && (cdb[0] != MODE_SELECT_6)))
3069 * Because there is no convenient place to stash the overall
3070 * cd_mode_params structure pointer, we have to grab it like this.
3071 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3072 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3074 * XXX It would be nice if, at some point, we could increase the
3075 * number of available peripheral private pointers. Both pointers
3076 * are currently used in most every peripheral driver.
3080 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3081 if (params->mode_buf == ccb->csio.data_ptr) {
3088 * This shouldn't happen. All mode sense and mode select
3089 * operations in the cd(4) driver MUST go through cdgetmode() and
3093 xpt_print(periph->path,
3094 "mode buffer not found in mode queue!\n");
3098 params->cdb_size = 10;
3099 softc->minimum_command_size = 10;
3100 xpt_print(ccb->ccb_h.path,
3101 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3102 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3104 if (cdb[0] == MODE_SENSE_6) {
3105 struct scsi_mode_sense_10 ms10;
3106 struct scsi_mode_sense_6 *ms6;
3109 ms6 = (struct scsi_mode_sense_6 *)cdb;
3111 bzero(&ms10, sizeof(ms10));
3112 ms10.opcode = MODE_SENSE_10;
3113 ms10.byte2 = ms6->byte2;
3114 ms10.page = ms6->page;
3117 * 10 byte mode header, block descriptor,
3118 * sizeof(union cd_pages)
3120 len = sizeof(struct cd_mode_data_10);
3121 ccb->csio.dxfer_len = len;
3123 scsi_ulto2b(len, ms10.length);
3124 ms10.control = ms6->control;
3125 bcopy(&ms10, cdb, 10);
3126 ccb->csio.cdb_len = 10;
3128 struct scsi_mode_select_10 ms10;
3129 struct scsi_mode_select_6 *ms6;
3130 struct scsi_mode_header_6 *header6;
3131 struct scsi_mode_header_10 *header10;
3132 struct scsi_mode_page_header *page_header;
3133 int blk_desc_len, page_num, page_size, len;
3135 ms6 = (struct scsi_mode_select_6 *)cdb;
3137 bzero(&ms10, sizeof(ms10));
3138 ms10.opcode = MODE_SELECT_10;
3139 ms10.byte2 = ms6->byte2;
3141 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3142 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3144 page_header = find_mode_page_6(header6);
3145 page_num = page_header->page_code;
3147 blk_desc_len = header6->blk_desc_len;
3149 page_size = cdgetpagesize(page_num);
3151 if (page_size != (page_header->page_length +
3152 sizeof(*page_header)))
3153 page_size = page_header->page_length +
3154 sizeof(*page_header);
3156 len = sizeof(*header10) + blk_desc_len + page_size;
3158 len = min(params->alloc_len, len);
3161 * Since the 6 byte parameter header is shorter than the 10
3162 * byte parameter header, we need to copy the actual mode
3163 * page data, and the block descriptor, if any, so things wind
3164 * up in the right place. The regions will overlap, but
3165 * bcopy() does the right thing.
3167 bcopy(params->mode_buf + sizeof(*header6),
3168 params->mode_buf + sizeof(*header10),
3169 len - sizeof(*header10));
3171 /* Make sure these fields are set correctly. */
3172 scsi_ulto2b(0, header10->data_length);
3173 header10->medium_type = 0;
3174 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3176 ccb->csio.dxfer_len = len;
3178 scsi_ulto2b(len, ms10.length);
3179 ms10.control = ms6->control;
3180 bcopy(&ms10, cdb, 10);
3181 ccb->csio.cdb_len = 10;
3184 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3185 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3188 cam_release_devq(ccb->ccb_h.path,
3192 /*getcount_only*/0);
3199 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3201 struct cd_softc *softc;
3202 struct cam_periph *periph;
3205 periph = xpt_path_periph(ccb->ccb_h.path);
3206 softc = (struct cd_softc *)periph->softc;
3211 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3212 * CDB comes back with this particular error, try transforming it
3213 * into the 10 byte version.
3215 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3216 error = cd6byteworkaround(ccb);
3217 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3218 CAM_SCSI_STATUS_ERROR)
3219 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3220 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3221 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3222 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3223 int sense_key, error_code, asc, ascq;
3225 scsi_extract_sense(&ccb->csio.sense_data,
3226 &error_code, &sense_key, &asc, &ascq);
3227 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3228 error = cd6byteworkaround(ccb);
3231 if (error == ERESTART)
3236 * Until we have a better way of doing pack validation,
3237 * don't treat UAs as errors.
3239 sense_flags |= SF_RETRY_UA;
3240 return (cam_periph_error(ccb, cam_flags, sense_flags,
3241 &softc->saved_ccb));
3245 * Read table of contents
3248 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3249 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3251 struct scsi_read_toc *scsi_cmd;
3253 struct ccb_scsiio *csio;
3260 ccb = cdgetccb(periph, /* priority */ 1);
3266 /* cbfcnp */ cddone,
3267 /* flags */ CAM_DIR_IN,
3268 /* tag_action */ MSG_SIMPLE_Q_TAG,
3269 /* data_ptr */ data,
3270 /* dxfer_len */ len,
3271 /* sense_len */ SSD_FULL_SIZE,
3272 sizeof(struct scsi_read_toc),
3273 /* timeout */ 50000);
3275 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3276 bzero (scsi_cmd, sizeof(*scsi_cmd));
3278 if (mode == CD_MSF_FORMAT)
3279 scsi_cmd->byte2 |= CD_MSF;
3280 scsi_cmd->from_track = start;
3281 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3282 scsi_cmd->data_len[0] = (ntoc) >> 8;
3283 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3285 scsi_cmd->op_code = READ_TOC;
3287 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3288 /*sense_flags*/SF_RETRY_UA | sense_flags);
3290 xpt_release_ccb(ccb);
3296 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3297 u_int32_t format, int track,
3298 struct cd_sub_channel_info *data, u_int32_t len)
3300 struct scsi_read_subchannel *scsi_cmd;
3301 struct ccb_scsiio *csio;
3307 ccb = cdgetccb(periph, /* priority */ 1);
3313 /* cbfcnp */ cddone,
3314 /* flags */ CAM_DIR_IN,
3315 /* tag_action */ MSG_SIMPLE_Q_TAG,
3316 /* data_ptr */ (u_int8_t *)data,
3317 /* dxfer_len */ len,
3318 /* sense_len */ SSD_FULL_SIZE,
3319 sizeof(struct scsi_read_subchannel),
3320 /* timeout */ 50000);
3322 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3323 bzero (scsi_cmd, sizeof(*scsi_cmd));
3325 scsi_cmd->op_code = READ_SUBCHANNEL;
3326 if (mode == CD_MSF_FORMAT)
3327 scsi_cmd->byte1 |= CD_MSF;
3328 scsi_cmd->byte2 = SRS_SUBQ;
3329 scsi_cmd->subchan_format = format;
3330 scsi_cmd->track = track;
3331 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3332 scsi_cmd->control = 0;
3334 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3335 /*sense_flags*/SF_RETRY_UA);
3337 xpt_release_ccb(ccb);
3343 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3344 * routine. See comments in cd6byteworkaround() for details.
3347 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3350 struct ccb_scsiio *csio;
3351 struct cd_softc *softc;
3356 softc = (struct cd_softc *)periph->softc;
3358 ccb = cdgetccb(periph, /* priority */ 1);
3362 data->cdb_size = softc->minimum_command_size;
3363 if (data->cdb_size < 10)
3364 param_len = sizeof(struct cd_mode_data);
3366 param_len = sizeof(struct cd_mode_data_10);
3368 /* Don't say we've got more room than we actually allocated */
3369 param_len = min(param_len, data->alloc_len);
3371 scsi_mode_sense_len(csio,
3373 /* cbfcnp */ cddone,
3374 /* tag_action */ MSG_SIMPLE_Q_TAG,
3376 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3378 /* param_buf */ data->mode_buf,
3379 /* param_len */ param_len,
3380 /* minimum_cmd_size */ softc->minimum_command_size,
3381 /* sense_len */ SSD_FULL_SIZE,
3382 /* timeout */ 50000);
3385 * It would be nice not to have to do this, but there's no
3386 * available pointer in the CCB that would allow us to stuff the
3387 * mode params structure in there and retrieve it in
3388 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3389 * lets the caller know what CDB size we ended up using, so they
3390 * can find the actual mode page offset.
3392 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3394 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3395 /*sense_flags*/SF_RETRY_UA);
3397 xpt_release_ccb(ccb);
3399 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3402 * This is a bit of belt-and-suspenders checking, but if we run
3403 * into a situation where the target sends back multiple block
3404 * descriptors, we might not have enough space in the buffer to
3405 * see the whole mode page. Better to return an error than
3406 * potentially access memory beyond our malloced region.
3411 if (data->cdb_size == 10) {
3412 struct scsi_mode_header_10 *hdr10;
3414 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3415 data_len = scsi_2btoul(hdr10->data_length);
3416 data_len += sizeof(hdr10->data_length);
3418 struct scsi_mode_header_6 *hdr6;
3420 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3421 data_len = hdr6->data_length;
3422 data_len += sizeof(hdr6->data_length);
3426 * Complain if there is more mode data available than we
3427 * allocated space for. This could potentially happen if
3428 * we miscalculated the page length for some reason, if the
3429 * drive returns multiple block descriptors, or if it sets
3430 * the data length incorrectly.
3432 if (data_len > data->alloc_len) {
3433 xpt_print(periph->path, "allocated modepage %d length "
3434 "%d < returned length %d\n", page, data->alloc_len,
3443 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3444 * routine. See comments in cd6byteworkaround() for details.
3447 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3449 struct ccb_scsiio *csio;
3450 struct cd_softc *softc;
3452 int cdb_size, param_len;
3455 softc = (struct cd_softc *)periph->softc;
3457 ccb = cdgetccb(periph, /* priority */ 1);
3464 * If the data is formatted for the 10 byte version of the mode
3465 * select parameter list, we need to use the 10 byte CDB.
3466 * Otherwise, we use whatever the stored minimum command size.
3468 if (data->cdb_size == 10)
3469 cdb_size = data->cdb_size;
3471 cdb_size = softc->minimum_command_size;
3473 if (cdb_size >= 10) {
3474 struct scsi_mode_header_10 *mode_header;
3477 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3479 data_len = scsi_2btoul(mode_header->data_length);
3481 scsi_ulto2b(0, mode_header->data_length);
3483 * SONY drives do not allow a mode select with a medium_type
3484 * value that has just been returned by a mode sense; use a
3485 * medium_type of 0 (Default) instead.
3487 mode_header->medium_type = 0;
3490 * Pass back whatever the drive passed to us, plus the size
3491 * of the data length field.
3493 param_len = data_len + sizeof(mode_header->data_length);
3496 struct scsi_mode_header_6 *mode_header;
3498 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3500 param_len = mode_header->data_length + 1;
3502 mode_header->data_length = 0;
3504 * SONY drives do not allow a mode select with a medium_type
3505 * value that has just been returned by a mode sense; use a
3506 * medium_type of 0 (Default) instead.
3508 mode_header->medium_type = 0;
3511 /* Don't say we've got more room than we actually allocated */
3512 param_len = min(param_len, data->alloc_len);
3514 scsi_mode_select_len(csio,
3516 /* cbfcnp */ cddone,
3517 /* tag_action */ MSG_SIMPLE_Q_TAG,
3518 /* scsi_page_fmt */ 1,
3520 /* param_buf */ data->mode_buf,
3521 /* param_len */ param_len,
3522 /* minimum_cmd_size */ cdb_size,
3523 /* sense_len */ SSD_FULL_SIZE,
3524 /* timeout */ 50000);
3526 /* See comments in cdgetmode() and cd6byteworkaround(). */
3527 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3529 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3530 /*sense_flags*/SF_RETRY_UA);
3532 xpt_release_ccb(ccb);
3534 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3542 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3544 struct ccb_scsiio *csio;
3550 ccb = cdgetccb(periph, /* priority */ 1);
3553 * Use the smallest possible command to perform the operation.
3555 if ((len & 0xffff0000) == 0) {
3557 * We can fit in a 10 byte cdb.
3559 struct scsi_play_10 *scsi_cmd;
3561 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3562 bzero (scsi_cmd, sizeof(*scsi_cmd));
3563 scsi_cmd->op_code = PLAY_10;
3564 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3565 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3566 cdb_len = sizeof(*scsi_cmd);
3568 struct scsi_play_12 *scsi_cmd;
3570 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3571 bzero (scsi_cmd, sizeof(*scsi_cmd));
3572 scsi_cmd->op_code = PLAY_12;
3573 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3574 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3575 cdb_len = sizeof(*scsi_cmd);
3580 /*flags*/CAM_DIR_NONE,
3584 /*sense_len*/SSD_FULL_SIZE,
3586 /*timeout*/50 * 1000);
3588 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3589 /*sense_flags*/SF_RETRY_UA);
3591 xpt_release_ccb(ccb);
3597 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3598 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3600 struct scsi_play_msf *scsi_cmd;
3601 struct ccb_scsiio *csio;
3607 ccb = cdgetccb(periph, /* priority */ 1);
3613 /* cbfcnp */ cddone,
3614 /* flags */ CAM_DIR_NONE,
3615 /* tag_action */ MSG_SIMPLE_Q_TAG,
3616 /* data_ptr */ NULL,
3618 /* sense_len */ SSD_FULL_SIZE,
3619 sizeof(struct scsi_play_msf),
3620 /* timeout */ 50000);
3622 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3623 bzero (scsi_cmd, sizeof(*scsi_cmd));
3625 scsi_cmd->op_code = PLAY_MSF;
3626 scsi_cmd->start_m = startm;
3627 scsi_cmd->start_s = starts;
3628 scsi_cmd->start_f = startf;
3629 scsi_cmd->end_m = endm;
3630 scsi_cmd->end_s = ends;
3631 scsi_cmd->end_f = endf;
3633 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3634 /*sense_flags*/SF_RETRY_UA);
3636 xpt_release_ccb(ccb);
3643 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3644 u_int32_t etrack, u_int32_t eindex)
3646 struct scsi_play_track *scsi_cmd;
3647 struct ccb_scsiio *csio;
3653 ccb = cdgetccb(periph, /* priority */ 1);
3659 /* cbfcnp */ cddone,
3660 /* flags */ CAM_DIR_NONE,
3661 /* tag_action */ MSG_SIMPLE_Q_TAG,
3662 /* data_ptr */ NULL,
3664 /* sense_len */ SSD_FULL_SIZE,
3665 sizeof(struct scsi_play_track),
3666 /* timeout */ 50000);
3668 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3669 bzero (scsi_cmd, sizeof(*scsi_cmd));
3671 scsi_cmd->op_code = PLAY_TRACK;
3672 scsi_cmd->start_track = strack;
3673 scsi_cmd->start_index = sindex;
3674 scsi_cmd->end_track = etrack;
3675 scsi_cmd->end_index = eindex;
3677 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3678 /*sense_flags*/SF_RETRY_UA);
3680 xpt_release_ccb(ccb);
3686 cdpause(struct cam_periph *periph, u_int32_t go)
3688 struct scsi_pause *scsi_cmd;
3689 struct ccb_scsiio *csio;
3695 ccb = cdgetccb(periph, /* priority */ 1);
3701 /* cbfcnp */ cddone,
3702 /* flags */ CAM_DIR_NONE,
3703 /* tag_action */ MSG_SIMPLE_Q_TAG,
3704 /* data_ptr */ NULL,
3706 /* sense_len */ SSD_FULL_SIZE,
3707 sizeof(struct scsi_pause),
3708 /* timeout */ 50000);
3710 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3711 bzero (scsi_cmd, sizeof(*scsi_cmd));
3713 scsi_cmd->op_code = PAUSE;
3714 scsi_cmd->resume = go;
3716 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3717 /*sense_flags*/SF_RETRY_UA);
3719 xpt_release_ccb(ccb);
3725 cdstartunit(struct cam_periph *periph, int load)
3732 ccb = cdgetccb(periph, /* priority */ 1);
3734 scsi_start_stop(&ccb->csio,
3736 /* cbfcnp */ cddone,
3737 /* tag_action */ MSG_SIMPLE_Q_TAG,
3739 /* load_eject */ load,
3740 /* immediate */ FALSE,
3741 /* sense_len */ SSD_FULL_SIZE,
3742 /* timeout */ 50000);
3744 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3745 /*sense_flags*/SF_RETRY_UA);
3747 xpt_release_ccb(ccb);
3753 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3760 ccb = cdgetccb(periph, /* priority */ 1);
3762 scsi_start_stop(&ccb->csio,
3764 /* cbfcnp */ cddone,
3765 /* tag_action */ MSG_SIMPLE_Q_TAG,
3767 /* load_eject */ eject,
3768 /* immediate */ FALSE,
3769 /* sense_len */ SSD_FULL_SIZE,
3770 /* timeout */ 50000);
3772 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3773 /*sense_flags*/SF_RETRY_UA);
3775 xpt_release_ccb(ccb);
3781 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3783 struct scsi_set_speed *scsi_cmd;
3784 struct ccb_scsiio *csio;
3789 ccb = cdgetccb(periph, /* priority */ 1);
3792 /* Preserve old behavior: units in multiples of CDROM speed */
3800 /* cbfcnp */ cddone,
3801 /* flags */ CAM_DIR_NONE,
3802 /* tag_action */ MSG_SIMPLE_Q_TAG,
3803 /* data_ptr */ NULL,
3805 /* sense_len */ SSD_FULL_SIZE,
3806 sizeof(struct scsi_set_speed),
3807 /* timeout */ 50000);
3809 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3810 bzero(scsi_cmd, sizeof(*scsi_cmd));
3812 scsi_cmd->opcode = SET_CD_SPEED;
3813 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3814 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3816 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3817 /*sense_flags*/SF_RETRY_UA);
3819 xpt_release_ccb(ccb);
3825 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3837 ccb = cdgetccb(periph, /* priority */ 1);
3839 switch (authinfo->format) {
3840 case DVD_REPORT_AGID:
3841 length = sizeof(struct scsi_report_key_data_agid);
3843 case DVD_REPORT_CHALLENGE:
3844 length = sizeof(struct scsi_report_key_data_challenge);
3846 case DVD_REPORT_KEY1:
3847 length = sizeof(struct scsi_report_key_data_key1_key2);
3849 case DVD_REPORT_TITLE_KEY:
3850 length = sizeof(struct scsi_report_key_data_title);
3851 /* The lba field is only set for the title key */
3852 lba = authinfo->lba;
3854 case DVD_REPORT_ASF:
3855 length = sizeof(struct scsi_report_key_data_asf);
3857 case DVD_REPORT_RPC:
3858 length = sizeof(struct scsi_report_key_data_rpc);
3860 case DVD_INVALIDATE_AGID:
3866 break; /* NOTREACHED */
3870 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3876 scsi_report_key(&ccb->csio,
3878 /* cbfcnp */ cddone,
3879 /* tag_action */ MSG_SIMPLE_Q_TAG,
3881 /* agid */ authinfo->agid,
3882 /* key_format */ authinfo->format,
3883 /* data_ptr */ databuf,
3884 /* dxfer_len */ length,
3885 /* sense_len */ SSD_FULL_SIZE,
3886 /* timeout */ 50000);
3888 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3889 /*sense_flags*/SF_RETRY_UA);
3894 if (ccb->csio.resid != 0) {
3895 xpt_print(periph->path, "warning, residual for report key "
3896 "command is %d\n", ccb->csio.resid);
3899 switch(authinfo->format) {
3900 case DVD_REPORT_AGID: {
3901 struct scsi_report_key_data_agid *agid_data;
3903 agid_data = (struct scsi_report_key_data_agid *)databuf;
3905 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3909 case DVD_REPORT_CHALLENGE: {
3910 struct scsi_report_key_data_challenge *chal_data;
3912 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3914 bcopy(chal_data->challenge_key, authinfo->keychal,
3915 min(sizeof(chal_data->challenge_key),
3916 sizeof(authinfo->keychal)));
3919 case DVD_REPORT_KEY1: {
3920 struct scsi_report_key_data_key1_key2 *key1_data;
3922 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3924 bcopy(key1_data->key1, authinfo->keychal,
3925 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3928 case DVD_REPORT_TITLE_KEY: {
3929 struct scsi_report_key_data_title *title_data;
3931 title_data = (struct scsi_report_key_data_title *)databuf;
3933 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3934 RKD_TITLE_CPM_SHIFT;
3935 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3936 RKD_TITLE_CP_SEC_SHIFT;
3937 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3938 RKD_TITLE_CMGS_SHIFT;
3939 bcopy(title_data->title_key, authinfo->keychal,
3940 min(sizeof(title_data->title_key),
3941 sizeof(authinfo->keychal)));
3944 case DVD_REPORT_ASF: {
3945 struct scsi_report_key_data_asf *asf_data;
3947 asf_data = (struct scsi_report_key_data_asf *)databuf;
3949 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3952 case DVD_REPORT_RPC: {
3953 struct scsi_report_key_data_rpc *rpc_data;
3955 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3957 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3959 authinfo->vend_rsts =
3960 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3961 RKD_RPC_VENDOR_RESET_SHIFT;
3962 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3963 authinfo->region = rpc_data->region_mask;
3964 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3967 case DVD_INVALIDATE_AGID:
3970 /* This should be impossible, since we checked above */
3973 break; /* NOTREACHED */
3976 if (databuf != NULL)
3977 kfree(databuf, M_DEVBUF);
3979 xpt_release_ccb(ccb);
3985 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3995 ccb = cdgetccb(periph, /* priority */ 1);
3997 switch(authinfo->format) {
3998 case DVD_SEND_CHALLENGE: {
3999 struct scsi_report_key_data_challenge *challenge_data;
4001 length = sizeof(*challenge_data);
4003 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4005 databuf = (u_int8_t *)challenge_data;
4007 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4008 challenge_data->data_len);
4010 bcopy(authinfo->keychal, challenge_data->challenge_key,
4011 min(sizeof(authinfo->keychal),
4012 sizeof(challenge_data->challenge_key)));
4015 case DVD_SEND_KEY2: {
4016 struct scsi_report_key_data_key1_key2 *key2_data;
4018 length = sizeof(*key2_data);
4020 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4022 databuf = (u_int8_t *)key2_data;
4024 scsi_ulto2b(length - sizeof(key2_data->data_len),
4025 key2_data->data_len);
4027 bcopy(authinfo->keychal, key2_data->key1,
4028 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4032 case DVD_SEND_RPC: {
4033 struct scsi_send_key_data_rpc *rpc_data;
4035 length = sizeof(*rpc_data);
4037 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4039 databuf = (u_int8_t *)rpc_data;
4041 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4042 rpc_data->data_len);
4044 rpc_data->region_code = authinfo->region;
4050 break; /* NOTREACHED */
4053 scsi_send_key(&ccb->csio,
4055 /* cbfcnp */ cddone,
4056 /* tag_action */ MSG_SIMPLE_Q_TAG,
4057 /* agid */ authinfo->agid,
4058 /* key_format */ authinfo->format,
4059 /* data_ptr */ databuf,
4060 /* dxfer_len */ length,
4061 /* sense_len */ SSD_FULL_SIZE,
4062 /* timeout */ 50000);
4064 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4065 /*sense_flags*/SF_RETRY_UA);
4069 if (databuf != NULL)
4070 kfree(databuf, M_DEVBUF);
4072 xpt_release_ccb(ccb);
4078 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4088 /* The address is reserved for many of the formats */
4091 ccb = cdgetccb(periph, /* priority */ 1);
4093 switch(dvdstruct->format) {
4094 case DVD_STRUCT_PHYSICAL:
4095 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4097 case DVD_STRUCT_COPYRIGHT:
4098 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4100 case DVD_STRUCT_DISCKEY:
4101 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4103 case DVD_STRUCT_BCA:
4104 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4106 case DVD_STRUCT_MANUFACT:
4107 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4109 case DVD_STRUCT_CMI:
4113 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4114 address = dvdstruct->address;
4116 break; /* NOTREACHED */
4117 case DVD_STRUCT_PROTDISCID:
4118 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4120 case DVD_STRUCT_DISCKEYBLOCK:
4121 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4123 case DVD_STRUCT_DDS:
4124 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4126 case DVD_STRUCT_MEDIUM_STAT:
4127 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4129 case DVD_STRUCT_SPARE_AREA:
4130 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4132 case DVD_STRUCT_RMD_LAST:
4136 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4137 address = dvdstruct->address;
4139 break; /* NOTREACHED */
4140 case DVD_STRUCT_RMD_RMA:
4144 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4145 address = dvdstruct->address;
4147 break; /* NOTREACHED */
4148 case DVD_STRUCT_PRERECORDED:
4149 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4151 case DVD_STRUCT_UNIQUEID:
4152 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4154 case DVD_STRUCT_DCB:
4158 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4159 address = dvdstruct->address;
4161 break; /* NOTREACHED */
4162 case DVD_STRUCT_LIST:
4164 * This is the maximum allocation length for the READ DVD
4165 * STRUCTURE command. There's nothing in the MMC3 spec
4166 * that indicates a limit in the amount of data that can
4167 * be returned from this call, other than the limits
4168 * imposed by the 2-byte length variables.
4175 break; /* NOTREACHED */
4179 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4184 scsi_read_dvd_structure(&ccb->csio,
4186 /* cbfcnp */ cddone,
4187 /* tag_action */ MSG_SIMPLE_Q_TAG,
4189 /* layer_number */ dvdstruct->layer_num,
4190 /* key_format */ dvdstruct->format,
4191 /* agid */ dvdstruct->agid,
4192 /* data_ptr */ databuf,
4193 /* dxfer_len */ length,
4194 /* sense_len */ SSD_FULL_SIZE,
4195 /* timeout */ 50000);
4197 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4198 /*sense_flags*/SF_RETRY_UA);
4203 switch(dvdstruct->format) {
4204 case DVD_STRUCT_PHYSICAL: {
4205 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4206 struct dvd_layer *outlayer;
4207 struct scsi_read_dvd_struct_data_physical *phys_data;
4210 (struct scsi_read_dvd_struct_data_physical *)databuf;
4211 inlayer = &phys_data->layer_desc;
4212 outlayer = (struct dvd_layer *)&dvdstruct->data;
4214 dvdstruct->length = sizeof(*inlayer);
4216 outlayer->book_type = (inlayer->book_type_version &
4217 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4218 outlayer->book_version = (inlayer->book_type_version &
4219 RDSD_BOOK_VERSION_MASK);
4220 outlayer->disc_size = (inlayer->disc_size_max_rate &
4221 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4222 outlayer->max_rate = (inlayer->disc_size_max_rate &
4223 RDSD_MAX_RATE_MASK);
4224 outlayer->nlayers = (inlayer->layer_info &
4225 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4226 outlayer->track_path = (inlayer->layer_info &
4227 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4228 outlayer->layer_type = (inlayer->layer_info &
4229 RDSD_LAYER_TYPE_MASK);
4230 outlayer->linear_density = (inlayer->density &
4231 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4232 outlayer->track_density = (inlayer->density &
4233 RDSD_TRACK_DENSITY_MASK);
4234 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4236 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4237 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4238 outlayer->end_sector_l0 =
4239 scsi_3btoul(inlayer->end_sector_layer0);
4242 case DVD_STRUCT_COPYRIGHT: {
4243 struct scsi_read_dvd_struct_data_copyright *copy_data;
4245 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4248 dvdstruct->cpst = copy_data->cps_type;
4249 dvdstruct->rmi = copy_data->region_info;
4250 dvdstruct->length = 0;
4256 * Tell the user what the overall length is, no matter
4257 * what we can actually fit in the data buffer.
4259 dvdstruct->length = length - ccb->csio.resid -
4260 sizeof(struct scsi_read_dvd_struct_data_header);
4263 * But only actually copy out the smaller of what we read
4264 * in or what the structure can take.
4266 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4268 min(sizeof(dvdstruct->data), dvdstruct->length));
4273 if (databuf != NULL)
4274 kfree(databuf, M_DEVBUF);
4276 xpt_release_ccb(ccb);
4282 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4283 void (*cbfcnp)(struct cam_periph *, union ccb *),
4284 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4285 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4286 u_int8_t sense_len, u_int32_t timeout)
4288 struct scsi_report_key *scsi_cmd;
4290 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4291 bzero(scsi_cmd, sizeof(*scsi_cmd));
4292 scsi_cmd->opcode = REPORT_KEY;
4293 scsi_ulto4b(lba, scsi_cmd->lba);
4294 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4295 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4296 (key_format & RK_KF_KEYFORMAT_MASK);
4301 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4303 /*data_ptr*/ data_ptr,
4304 /*dxfer_len*/ dxfer_len,
4311 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4312 void (*cbfcnp)(struct cam_periph *, union ccb *),
4313 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4314 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4317 struct scsi_send_key *scsi_cmd;
4319 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4320 bzero(scsi_cmd, sizeof(*scsi_cmd));
4321 scsi_cmd->opcode = SEND_KEY;
4323 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4324 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4325 (key_format & RK_KF_KEYFORMAT_MASK);
4330 /*flags*/ CAM_DIR_OUT,
4332 /*data_ptr*/ data_ptr,
4333 /*dxfer_len*/ dxfer_len,
4341 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4342 void (*cbfcnp)(struct cam_periph *, union ccb *),
4343 u_int8_t tag_action, u_int32_t address,
4344 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4345 u_int8_t *data_ptr, u_int32_t dxfer_len,
4346 u_int8_t sense_len, u_int32_t timeout)
4348 struct scsi_read_dvd_structure *scsi_cmd;
4350 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4351 bzero(scsi_cmd, sizeof(*scsi_cmd));
4352 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4354 scsi_ulto4b(address, scsi_cmd->address);
4355 scsi_cmd->layer_number = layer_number;
4356 scsi_cmd->format = format;
4357 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4358 /* The AGID is the top two bits of this byte */
4359 scsi_cmd->agid = agid << 6;
4364 /*flags*/ CAM_DIR_IN,
4366 /*data_ptr*/ data_ptr,
4367 /*dxfer_len*/ dxfer_len,