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>
69 #include <sys/thread2.h>
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
76 #include "../cam_queue.h"
77 #include "../cam_sim.h"
79 #include "scsi_message.h"
83 #define LEADOUT 0xaa /* leadout toc entry */
93 CD_Q_BCD_TRACKS = 0x02,
94 CD_Q_NO_CHANGER = 0x04,
96 CD_Q_10_BYTE_ONLY = 0x10
100 CD_FLAG_INVALID = 0x0001,
101 CD_FLAG_NEW_DISC = 0x0002,
102 CD_FLAG_DISC_LOCKED = 0x0004,
103 CD_FLAG_DISC_REMOVABLE = 0x0008,
104 CD_FLAG_TAGGED_QUEUING = 0x0010,
105 CD_FLAG_CHANGER = 0x0040,
106 CD_FLAG_ACTIVE = 0x0080,
107 CD_FLAG_SCHED_ON_COMP = 0x0100,
108 CD_FLAG_RETRY_UA = 0x0200,
109 CD_FLAG_VALID_MEDIA = 0x0400,
110 CD_FLAG_VALID_TOC = 0x0800,
111 CD_FLAG_SCTX_INIT = 0x1000,
112 CD_FLAG_OPEN = 0x2000
117 CD_CCB_BUFFER_IO = 0x02,
118 CD_CCB_WAITING = 0x03,
119 CD_CCB_TYPE_MASK = 0x0F,
120 CD_CCB_RETRY_UA = 0x10
124 CHANGER_TIMEOUT_SCHED = 0x01,
125 CHANGER_SHORT_TMOUT_SCHED = 0x02,
126 CHANGER_MANUAL_CALL = 0x04,
127 CHANGER_NEED_TIMEOUT = 0x08
130 #define ccb_state ppriv_field0
131 #define ccb_bio ppriv_ptr1
134 struct ioc_toc_header header;
135 struct cd_toc_entry entries[100];
138 struct cd_toc_single {
139 struct ioc_toc_header header;
140 struct cd_toc_entry entry;
151 volatile cd_flags flags;
152 struct bio_queue_head bio_queue;
153 LIST_HEAD(, ccb_hdr) pending_ccbs;
154 struct cd_params params;
158 struct devstat device_stats;
159 STAILQ_ENTRY(cd_softc) changer_links;
160 struct cdchanger *changer;
162 struct cam_periph *periph;
163 int minimum_command_size;
164 int outstanding_cmds;
165 struct task sysctl_task;
166 struct sysctl_ctx_list sysctl_ctx;
167 struct sysctl_oid *sysctl_tree;
168 STAILQ_HEAD(, cd_mode_params) mode_queue;
169 struct cd_tocdata toc;
172 struct cd_page_sizes {
177 static struct cd_page_sizes cd_page_size_table[] =
179 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
182 struct cd_quirk_entry {
183 struct scsi_inquiry_pattern inq_pat;
188 * The changer quirk entries aren't strictly necessary. Basically, what
189 * they do is tell cdregister() up front that a device is a changer.
190 * Otherwise, it will figure that fact out once it sees a LUN on the device
191 * that is greater than 0. If it is known up front that a device is a changer,
192 * all I/O to the device will go through the changer scheduling routines, as
193 * opposed to the "normal" CD code.
195 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
196 * your device hangs when it gets a 10 byte command. Adding a quirk just
197 * to get rid of the informative diagnostic message is not acceptable. All
198 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
199 * referenced in a comment along with the quirk) , and must be approved by
200 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
201 * be removed until the submitter can explain why they are needed.
202 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
203 * when the CAM_NEW_TRAN_CODE work is done.
205 static struct cd_quirk_entry cd_quirk_table[] =
208 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
209 /*quirks*/ CD_Q_CHANGER
212 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
213 "*"}, /* quirks */ CD_Q_CHANGER
216 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
217 /* quirks */ CD_Q_CHANGER
220 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
221 /* quirks */ CD_Q_BCD_TRACKS
225 static d_open_t cdopen;
226 static d_close_t cdclose;
227 static d_ioctl_t cdioctl;
228 static d_strategy_t cdstrategy;
230 static periph_init_t cdinit;
231 static periph_ctor_t cdregister;
232 static periph_dtor_t cdcleanup;
233 static periph_start_t cdstart;
234 static periph_oninv_t cdoninvalidate;
235 static void cdasync(void *callback_arg, u_int32_t code,
236 struct cam_path *path, void *arg);
237 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
238 static void cdshorttimeout(void *arg);
239 static void cdschedule(struct cam_periph *periph, int priority);
240 static void cdrunchangerqueue(void *arg);
241 static void cdchangerschedule(struct cd_softc *softc);
242 static int cdrunccb(union ccb *ccb,
243 int (*error_routine)(union ccb *ccb,
245 u_int32_t sense_flags),
246 u_int32_t cam_flags, u_int32_t sense_flags);
247 static union ccb *cdgetccb(struct cam_periph *periph,
249 static void cddone(struct cam_periph *periph,
250 union ccb *start_ccb);
251 static int cderror(union ccb *ccb, u_int32_t cam_flags,
252 u_int32_t sense_flags);
253 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
254 static int cdgetpagesize(int page_num);
255 static void cdprevent(struct cam_periph *periph, int action);
256 static int cdcheckmedia(struct cam_periph *periph);
257 static int cdsize(struct cam_periph *periph, u_int32_t *size);
258 static int cd6byteworkaround(union ccb *ccb);
259 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
260 u_int32_t start, u_int8_t *data,
261 u_int32_t len, u_int32_t sense_flags);
262 static int cdgetmode(struct cam_periph *periph,
263 struct cd_mode_params *data, u_int32_t page);
264 static int cdsetmode(struct cam_periph *periph,
265 struct cd_mode_params *data);
266 static int cdplay(struct cam_periph *periph, u_int32_t blk,
268 static int cdreadsubchannel(struct cam_periph *periph,
269 u_int32_t mode, u_int32_t format,
271 struct cd_sub_channel_info *data,
273 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
274 u_int32_t starts, u_int32_t startf,
275 u_int32_t endm, u_int32_t ends,
277 static int cdplaytracks(struct cam_periph *periph,
278 u_int32_t strack, u_int32_t sindex,
279 u_int32_t etrack, u_int32_t eindex);
280 static int cdpause(struct cam_periph *periph, u_int32_t go);
281 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
282 static int cdstartunit(struct cam_periph *periph, int load);
283 static int cdsetspeed(struct cam_periph *periph,
284 u_int32_t rdspeed, u_int32_t wrspeed);
285 static int cdreportkey(struct cam_periph *periph,
286 struct dvd_authinfo *authinfo);
287 static int cdsendkey(struct cam_periph *periph,
288 struct dvd_authinfo *authinfo);
289 static int cdreaddvdstructure(struct cam_periph *periph,
290 struct dvd_struct *dvdstruct);
292 static struct periph_driver cddriver =
295 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
298 PERIPHDRIVER_DECLARE(cd, cddriver);
300 static struct dev_ops cd_ops = {
301 { "cd", SCSICD_CDEV_MAJOR, D_DISK },
305 .d_write = physwrite,
307 .d_strategy = cdstrategy
310 static struct extend_array *cdperiphs;
312 #ifndef CHANGER_MIN_BUSY_SECONDS
313 #define CHANGER_MIN_BUSY_SECONDS 5
315 #ifndef CHANGER_MAX_BUSY_SECONDS
316 #define CHANGER_MAX_BUSY_SECONDS 15
319 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
320 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
322 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
323 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
324 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
325 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
326 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
327 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
328 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
329 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
333 target_id_t target_id;
336 struct timeval start_time;
337 struct cd_softc *cur_device;
338 struct callout short_handle;
339 struct callout long_handle;
340 volatile cd_changer_flags flags;
341 STAILQ_ENTRY(cdchanger) changer_links;
342 STAILQ_HEAD(chdevlist, cd_softc) chluns;
345 static struct lock changerq_lock;
346 static STAILQ_HEAD(changerlist, cdchanger) changerq;
347 static int num_changers;
349 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
356 lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
357 STAILQ_INIT(&changerq);
360 * Create our extend array for storing the devices we attach to.
362 cdperiphs = cam_extend_new();
363 if (cdperiphs == NULL) {
364 kprintf("cd: Failed to alloc extend array!\n");
369 * Install a global async callback. This callback will
370 * receive async callbacks like "new device found".
372 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
374 if (status != CAM_REQ_CMP) {
375 kprintf("cd: Failed to attach master async callback "
376 "due to status 0x%x!\n", status);
381 cdoninvalidate(struct cam_periph *periph)
383 struct cd_softc *softc;
387 softc = (struct cd_softc *)periph->softc;
390 * De-register any async callbacks.
392 xpt_register_async(0, cdasync, periph, periph->path);
394 softc->flags |= CD_FLAG_INVALID;
397 * Return all queued I/O with ENXIO.
398 * XXX Handle any transactions queued to the card
399 * with XPT_ABORT_CCB.
401 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
402 bioq_remove(&softc->bio_queue, q_bio);
403 q_bp = q_bio->bio_buf;
404 q_bp->b_resid = q_bp->b_bcount;
405 q_bp->b_error = ENXIO;
406 q_bp->b_flags |= B_ERROR;
411 * If this device is part of a changer, and it was scheduled
412 * to run, remove it from the run queue since we just nuked
413 * all of its scheduled I/O.
415 if ((softc->flags & CD_FLAG_CHANGER)
416 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
417 camq_remove(&softc->changer->devq, softc->pinfo.index);
419 xpt_print(periph->path, "lost device\n");
423 cdcleanup(struct cam_periph *periph)
425 struct cd_softc *softc;
427 softc = (struct cd_softc *)periph->softc;
429 xpt_print(periph->path, "removing device entry\n");
431 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
432 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
433 xpt_print(periph->path, "can't remove sysctl context\n");
437 * In the queued, non-active case, the device in question
438 * has already been removed from the changer run queue. Since this
439 * device is active, we need to de-activate it, and schedule
440 * another device to run. (if there is another one to run)
442 if ((softc->flags & CD_FLAG_CHANGER)
443 && (softc->flags & CD_FLAG_ACTIVE)) {
446 * The purpose of the short timeout is soley to determine
447 * whether the current device has finished or not. Well,
448 * since we're removing the active device, we know that it
449 * is finished. So, get rid of the short timeout.
450 * Otherwise, if we're in the time period before the short
451 * timeout fires, and there are no other devices in the
452 * queue to run, there won't be any other device put in the
453 * active slot. i.e., when we call cdrunchangerqueue()
454 * below, it won't do anything. Then, when the short
455 * timeout fires, it'll look at the "current device", which
456 * we are free below, and possibly panic the kernel on a
457 * bogus pointer reference.
459 * The long timeout doesn't really matter, since we
460 * decrement the qfrozen_cnt to indicate that there is
461 * nothing in the active slot now. Therefore, there won't
462 * be any bogus pointer references there.
464 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
465 callout_stop(&softc->changer->short_handle);
466 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
468 softc->changer->devq.qfrozen_cnt--;
469 softc->changer->flags |= CHANGER_MANUAL_CALL;
470 cdrunchangerqueue(softc->changer);
474 * If we're removing the last device on the changer, go ahead and
475 * remove the changer device structure.
477 if ((softc->flags & CD_FLAG_CHANGER)
478 && (--softc->changer->num_devices == 0)) {
481 * Theoretically, there shouldn't be any timeouts left, but
482 * I'm not completely sure that that will be the case. So,
483 * it won't hurt to check and see if there are any left.
485 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
486 callout_stop(&softc->changer->long_handle);
487 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
490 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
491 callout_stop(&softc->changer->short_handle);
492 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
495 lockmgr(&changerq_lock, LK_EXCLUSIVE);
496 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
499 lockmgr(&changerq_lock, LK_RELEASE);
500 xpt_print(periph->path, "removing changer entry\n");
501 kfree(softc->changer, M_DEVBUF);
503 devstat_remove_entry(&softc->device_stats);
504 cam_extend_release(cdperiphs, periph->unit_number);
505 if (softc->disk.d_rawdev) {
506 cam_periph_unlock(periph);
507 disk_destroy(&softc->disk);
508 cam_periph_lock(periph);
510 kfree(softc, M_DEVBUF);
514 cdasync(void *callback_arg, u_int32_t code,
515 struct cam_path *path, void *arg)
517 struct cam_periph *periph;
519 periph = (struct cam_periph *)callback_arg;
522 case AC_FOUND_DEVICE:
524 struct ccb_getdev *cgd;
527 cgd = (struct ccb_getdev *)arg;
531 if (SID_TYPE(&cgd->inq_data) != T_CDROM
532 && SID_TYPE(&cgd->inq_data) != T_WORM)
536 * Don't complain if a valid peripheral is already attached.
538 periph = cam_periph_find(cgd->ccb_h.path, "cd");
539 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
543 * Allocate a peripheral instance for
544 * this device and start the probe
547 status = cam_periph_alloc(cdregister, cdoninvalidate,
549 "cd", CAM_PERIPH_BIO,
550 cgd->ccb_h.path, cdasync,
551 AC_FOUND_DEVICE, cgd);
553 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
554 kprintf("cdasync: Unable to attach new device "
555 "due to status 0x%x\n", status);
562 struct cd_softc *softc;
563 struct ccb_hdr *ccbh;
565 softc = (struct cd_softc *)periph->softc;
567 * Don't fail on the expected unit attention
570 softc->flags |= CD_FLAG_RETRY_UA;
571 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
572 ccbh->ccb_state |= CD_CCB_RETRY_UA;
576 cam_periph_async(periph, code, path, arg);
582 cdsysctlinit(void *context, int pending)
584 struct cam_periph *periph;
585 struct cd_softc *softc;
586 char tmpstr[80], tmpstr2[80];
588 periph = (struct cam_periph *)context;
589 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
592 softc = (struct cd_softc *)periph->softc;
593 ksnprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
594 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
596 sysctl_ctx_init(&softc->sysctl_ctx);
597 softc->flags |= CD_FLAG_SCTX_INIT;
598 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
599 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
600 tmpstr2, CTLFLAG_RD, 0, tmpstr);
602 if (softc->sysctl_tree == NULL) {
603 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
604 cam_periph_release(periph);
609 * Now register the sysctl handler, so the user can the value on
612 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
613 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
614 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
617 cam_periph_release(periph);
621 * We have a handler function for this so we can check the values when the
622 * user sets them, instead of every time we look at them.
625 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
629 value = *(int *)arg1;
631 error = sysctl_handle_int(oidp, &value, 0, req);
633 if ((error != 0) || (req->newptr == NULL))
637 * The only real values we can have here are 6 or 10. I don't
638 * really forsee having 12 be an option at any time in the future.
639 * So if the user sets something less than or equal to 6, we'll set
640 * it to 6. If he sets something greater than 6, we'll set it to 10.
642 * I suppose we could just return an error here for the wrong values,
643 * but I don't think it's necessary to do so, as long as we can
644 * determine the user's intent without too much trouble.
651 *(int *)arg1 = value;
658 cdregister(struct cam_periph *periph, void *arg)
660 struct cd_softc *softc;
661 struct ccb_pathinq cpi;
662 struct ccb_getdev *cgd;
666 cgd = (struct ccb_getdev *)arg;
667 if (periph == NULL) {
668 kprintf("cdregister: periph was NULL!!\n");
669 return(CAM_REQ_CMP_ERR);
672 kprintf("cdregister: no getdev CCB, can't register device\n");
673 return(CAM_REQ_CMP_ERR);
676 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
677 LIST_INIT(&softc->pending_ccbs);
678 STAILQ_INIT(&softc->mode_queue);
679 softc->state = CD_STATE_PROBE;
680 bioq_init(&softc->bio_queue);
681 if (SID_IS_REMOVABLE(&cgd->inq_data))
682 softc->flags |= CD_FLAG_DISC_REMOVABLE;
683 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
684 softc->flags |= CD_FLAG_TAGGED_QUEUING;
686 periph->softc = softc;
687 softc->periph = periph;
689 cam_extend_set(cdperiphs, periph->unit_number, periph);
692 * See if this device has any quirks.
694 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
695 (caddr_t)cd_quirk_table,
696 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
697 sizeof(*cd_quirk_table), scsi_inquiry_match);
700 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
702 softc->quirks = CD_Q_NONE;
704 /* Check if the SIM does not want 6 byte commands */
705 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
706 cpi.ccb_h.func_code = XPT_PATH_INQ;
707 xpt_action((union ccb *)&cpi);
708 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
709 softc->quirks |= CD_Q_10_BYTE_ONLY;
711 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
713 /* The default is 6 byte commands, unless quirked otherwise */
714 if (softc->quirks & CD_Q_10_BYTE_ONLY)
715 softc->minimum_command_size = 10;
717 softc->minimum_command_size = 6;
720 * Load the user's default, if any.
722 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
723 periph->unit_number);
724 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
726 /* 6 and 10 are the only permissible values here. */
727 if (softc->minimum_command_size < 6)
728 softc->minimum_command_size = 6;
729 else if (softc->minimum_command_size > 6)
730 softc->minimum_command_size = 10;
733 * We need to register the statistics structure for this device,
734 * but we don't have the blocksize yet for it. So, we register
735 * the structure and indicate that we don't have the blocksize
736 * yet. Unlike other SCSI peripheral drivers, we explicitly set
737 * the device type here to be CDROM, rather than just ORing in
738 * the device type. This is because this driver can attach to either
739 * CDROM or WORM devices, and we want this peripheral driver to
740 * show up in the devstat list as a CD peripheral driver, not a
741 * WORM peripheral driver. WORM drives will also have the WORM
742 * driver attached to them.
744 cam_periph_unlock(periph);
745 devstat_add_entry(&softc->device_stats, "cd",
746 periph->unit_number, 0,
747 DEVSTAT_BS_UNAVAILABLE,
748 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
749 DEVSTAT_PRIORITY_CD);
750 disk_create(periph->unit_number, &softc->disk, &cd_ops);
751 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
752 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
754 cam_periph_lock(periph);
757 * Add an async callback so that we get
758 * notified if this device goes away.
760 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
761 cdasync, periph, periph->path);
764 * If the target lun is greater than 0, we most likely have a CD
765 * changer device. Check the quirk entries as well, though, just
766 * in case someone has a CD tower with one lun per drive or
767 * something like that. Also, if we know up front that a
768 * particular device is a changer, we can mark it as such starting
769 * with lun 0, instead of lun 1. It shouldn't be necessary to have
770 * a quirk entry to define something as a changer, however.
772 if (((cgd->ccb_h.target_lun > 0)
773 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
774 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
775 struct cdchanger *nchanger;
776 struct cam_periph *nperiph;
777 struct cam_path *path;
781 /* Set the changer flag in the current device's softc */
782 softc->flags |= CD_FLAG_CHANGER;
785 * Now, look around for an existing changer device with the
786 * same path and target ID as the current device.
788 lockmgr(&changerq_lock, LK_EXCLUSIVE);
790 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
792 nchanger = STAILQ_NEXT(nchanger, changer_links)){
793 if ((nchanger->path_id == cgd->ccb_h.path_id)
794 && (nchanger->target_id == cgd->ccb_h.target_id)) {
799 lockmgr(&changerq_lock, LK_RELEASE);
802 * If we found a matching entry, just add this device to
803 * the list of devices on this changer.
806 struct chdevlist *chlunhead;
808 chlunhead = &nchanger->chluns;
811 * XXX KDM look at consolidating this code with the
812 * code below in a separate function.
816 * Create a path with lun id 0, and see if we can
817 * find a matching device
819 status = xpt_create_path(&path, /*periph*/ periph,
821 cgd->ccb_h.target_id, 0);
823 if ((status == CAM_REQ_CMP)
824 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
825 struct cd_softc *nsoftc;
827 nsoftc = (struct cd_softc *)nperiph->softc;
829 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
830 nsoftc->flags |= CD_FLAG_CHANGER;
831 nchanger->num_devices++;
832 if (camq_resize(&nchanger->devq,
833 nchanger->num_devices)!=CAM_REQ_CMP){
834 kprintf("cdregister: "
840 nsoftc->changer = nchanger;
841 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
843 STAILQ_INSERT_TAIL(&nchanger->chluns,
844 nsoftc,changer_links);
847 } else if (status == CAM_REQ_CMP)
850 kprintf("cdregister: unable to allocate path\n"
851 "cdregister: changer support may be "
855 nchanger->num_devices++;
857 softc->changer = nchanger;
858 softc->pinfo.index = CAM_UNQUEUED_INDEX;
860 if (camq_resize(&nchanger->devq,
861 nchanger->num_devices) != CAM_REQ_CMP) {
862 kprintf("cdregister: camq_resize "
863 "failed, changer support may "
867 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
870 * In this case, we don't already have an entry for this
871 * particular changer, so we need to create one, add it to
872 * the queue, and queue this device on the list for this
873 * changer. Before we queue this device, however, we need
874 * to search for lun id 0 on this target, and add it to the
875 * queue first, if it exists. (and if it hasn't already
876 * been marked as part of the changer.)
879 nchanger = kmalloc(sizeof(struct cdchanger),
880 M_DEVBUF, M_INTWAIT | M_ZERO);
881 callout_init(&nchanger->short_handle);
882 callout_init(&nchanger->long_handle);
883 if (camq_init(&nchanger->devq, 1) != 0) {
884 softc->flags &= ~CD_FLAG_CHANGER;
885 kprintf("cdregister: changer support "
890 nchanger->path_id = cgd->ccb_h.path_id;
891 nchanger->target_id = cgd->ccb_h.target_id;
893 /* this is superfluous, but it makes things clearer */
894 nchanger->num_devices = 0;
896 STAILQ_INIT(&nchanger->chluns);
898 callout_init(&nchanger->long_handle);
899 callout_init(&nchanger->short_handle);
901 lockmgr(&changerq_lock, LK_EXCLUSIVE);
903 STAILQ_INSERT_TAIL(&changerq, nchanger,
905 lockmgr(&changerq_lock, LK_RELEASE);
908 * Create a path with lun id 0, and see if we can
909 * find a matching device
911 status = xpt_create_path(&path, /*periph*/ periph,
913 cgd->ccb_h.target_id, 0);
916 * If we were able to allocate the path, and if we
917 * find a matching device and it isn't already
918 * marked as part of a changer, then we add it to
919 * the current changer.
921 if ((status == CAM_REQ_CMP)
922 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
923 && ((((struct cd_softc *)periph->softc)->flags &
924 CD_FLAG_CHANGER) == 0)) {
925 struct cd_softc *nsoftc;
927 nsoftc = (struct cd_softc *)nperiph->softc;
929 nsoftc->flags |= CD_FLAG_CHANGER;
930 nchanger->num_devices++;
931 if (camq_resize(&nchanger->devq,
932 nchanger->num_devices) != CAM_REQ_CMP) {
933 kprintf("cdregister: camq_resize "
934 "failed, changer support may "
937 nsoftc->changer = nchanger;
938 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
940 STAILQ_INSERT_TAIL(&nchanger->chluns,
941 nsoftc, changer_links);
943 } else if (status == CAM_REQ_CMP)
946 kprintf("cdregister: unable to allocate path\n"
947 "cdregister: changer support may be "
951 softc->changer = nchanger;
952 softc->pinfo.index = CAM_UNQUEUED_INDEX;
953 nchanger->num_devices++;
954 if (camq_resize(&nchanger->devq,
955 nchanger->num_devices) != CAM_REQ_CMP) {
956 kprintf("cdregister: camq_resize "
957 "failed, changer support may "
960 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
968 * Refcount and block open attempts until we are setup
971 cam_periph_hold(periph, 0);
973 if ((softc->flags & CD_FLAG_CHANGER) == 0)
974 xpt_schedule(periph, /*priority*/5);
976 cdschedule(periph, /*priority*/ 5);
982 cdopen(struct dev_open_args *ap)
984 cdev_t dev = ap->a_head.a_dev;
985 struct cam_periph *periph;
986 struct cd_softc *softc;
990 periph = cam_extend_get(cdperiphs, unit);
995 softc = (struct cd_softc *)periph->softc;
997 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1000 cam_periph_lock(periph);
1002 if (softc->flags & CD_FLAG_INVALID) {
1003 cam_periph_unlock(periph);
1004 cam_periph_release(periph);
1008 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1009 cam_periph_unlock(periph);
1010 cam_periph_release(periph);
1014 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1015 if (softc->flags & CD_FLAG_OPEN)
1016 cam_periph_release(periph);
1018 softc->flags |= CD_FLAG_OPEN;
1021 * Check for media, and set the appropriate flags. We don't bail
1022 * if we don't have media, but then we don't allow anything but the
1023 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1025 * XXX KDM for now, we do fail the open if we don't have media. We
1026 * can change this once we've figured out how to make the slice
1027 * code work well with media changing underneath it.
1029 error = cdcheckmedia(periph);
1031 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1032 cam_periph_unhold(periph, 1);
1033 /* stays acquired */
1039 cdclose(struct dev_close_args *ap)
1041 cdev_t dev = ap->a_head.a_dev;
1042 struct cam_periph *periph;
1043 struct cd_softc *softc;
1044 struct disk_info *info;
1048 periph = cam_extend_get(cdperiphs, unit);
1052 softc = (struct cd_softc *)periph->softc;
1054 cam_periph_lock(periph);
1055 cam_periph_hold(periph, 0);
1057 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1058 cdprevent(periph, PR_ALLOW);
1061 * Unconditionally set the dsopen() flags back to their default
1064 info = &softc->disk.d_info;
1065 info->d_dsflags &= ~DSO_NOLABELS;
1066 info->d_dsflags |= DSO_COMPATLABEL;
1069 * Since we're closing this CD, mark the blocksize as unavailable.
1070 * It will be marked as available when the CD is opened again.
1072 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1075 * We'll check the media and toc again at the next open().
1077 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1079 cam_periph_unhold(periph, 1);
1080 cam_periph_release(periph);
1086 cdshorttimeout(void *arg)
1088 struct cdchanger *changer;
1090 changer = (struct cdchanger *)arg;
1092 /* Always clear the short timeout flag, since that's what we're in */
1093 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1096 * Check to see if there is any more pending or outstanding I/O for
1097 * this device. If not, move it out of the active slot.
1099 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1100 && (changer->cur_device->outstanding_cmds == 0)) {
1101 changer->flags |= CHANGER_MANUAL_CALL;
1102 cdrunchangerqueue(changer);
1107 * This is a wrapper for xpt_schedule. It only applies to changers.
1110 cdschedule(struct cam_periph *periph, int priority)
1112 struct cd_softc *softc;
1114 softc = (struct cd_softc *)periph->softc;
1117 * If this device isn't currently queued, and if it isn't
1118 * the active device, then we queue this device and run the
1119 * changer queue if there is no timeout scheduled to do it.
1120 * If this device is the active device, just schedule it
1121 * to run again. If this device is queued, there should be
1122 * a timeout in place already that will make sure it runs.
1124 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1125 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1127 * We don't do anything with the priority here.
1128 * This is strictly a fifo queue.
1130 softc->pinfo.priority = 1;
1131 softc->pinfo.generation = ++softc->changer->devq.generation;
1132 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1135 * Since we just put a device in the changer queue,
1136 * check and see if there is a timeout scheduled for
1137 * this changer. If so, let the timeout handle
1138 * switching this device into the active slot. If
1139 * not, manually call the timeout routine to
1142 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1143 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1144 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1145 softc->changer->flags |= CHANGER_MANUAL_CALL;
1146 cdrunchangerqueue(softc->changer);
1148 } else if ((softc->flags & CD_FLAG_ACTIVE)
1149 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1150 xpt_schedule(periph, priority);
1155 cdrunchangerqueue(void *arg)
1157 struct cd_softc *softc;
1158 struct cdchanger *changer;
1159 int called_from_timeout;
1161 changer = (struct cdchanger *)arg;
1164 * If we have NOT been called from cdstrategy() or cddone(), and
1165 * instead from a timeout routine, go ahead and clear the
1168 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1169 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1170 called_from_timeout = 1;
1172 called_from_timeout = 0;
1174 /* Always clear the manual call flag */
1175 changer->flags &= ~CHANGER_MANUAL_CALL;
1177 /* nothing to do if the queue is empty */
1178 if (changer->devq.entries <= 0) {
1183 * If the changer queue is frozen, that means we have an active
1186 if (changer->devq.qfrozen_cnt > 0) {
1189 * We always need to reset the frozen count and clear the
1192 changer->devq.qfrozen_cnt--;
1193 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1194 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1196 if (changer->cur_device->outstanding_cmds > 0) {
1197 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1198 changer->cur_device->bufs_left =
1199 changer->cur_device->outstanding_cmds;
1200 if (called_from_timeout) {
1201 callout_reset(&changer->long_handle,
1202 changer_max_busy_seconds * hz,
1203 cdrunchangerqueue, changer);
1204 changer->flags |= CHANGER_TIMEOUT_SCHED;
1210 * Check to see whether the current device has any I/O left
1211 * to do. If so, requeue it at the end of the queue. If
1212 * not, there is no need to requeue it.
1214 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1216 changer->cur_device->pinfo.generation =
1217 ++changer->devq.generation;
1218 camq_insert(&changer->devq,
1219 (cam_pinfo *)changer->cur_device);
1223 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1225 changer->cur_device = softc;
1227 changer->devq.qfrozen_cnt++;
1228 softc->flags |= CD_FLAG_ACTIVE;
1230 /* Just in case this device is waiting */
1231 wakeup(&softc->changer);
1232 xpt_schedule(softc->periph, /*priority*/ 1);
1235 * Get rid of any pending timeouts, and set a flag to schedule new
1236 * ones so this device gets its full time quantum.
1238 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1239 callout_stop(&changer->long_handle);
1240 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1243 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1244 callout_stop(&changer->short_handle);
1245 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1249 * We need to schedule timeouts, but we only do this after the
1250 * first transaction has completed. This eliminates the changer
1253 changer->flags |= CHANGER_NEED_TIMEOUT;
1257 cdchangerschedule(struct cd_softc *softc)
1259 struct cdchanger *changer;
1261 changer = softc->changer;
1264 * If this is a changer, and this is the current device,
1265 * and this device has at least the minimum time quantum to
1266 * run, see if we can switch it out.
1268 if ((softc->flags & CD_FLAG_ACTIVE)
1269 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1270 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1272 * We try three things here. The first is that we
1273 * check to see whether the schedule on completion
1274 * flag is set. If it is, we decrement the number
1275 * of buffers left, and if it's zero, we reschedule.
1276 * Next, we check to see whether the pending buffer
1277 * queue is empty and whether there are no
1278 * outstanding transactions. If so, we reschedule.
1279 * Next, we see if the pending buffer queue is empty.
1280 * If it is, we set the number of buffers left to
1281 * the current active buffer count and set the
1282 * schedule on complete flag.
1284 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1285 if (--softc->bufs_left == 0) {
1286 softc->changer->flags |=
1287 CHANGER_MANUAL_CALL;
1288 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1289 cdrunchangerqueue(softc->changer);
1291 } else if ((bioq_first(&softc->bio_queue) == NULL)
1292 && (softc->outstanding_cmds == 0)) {
1293 softc->changer->flags |= CHANGER_MANUAL_CALL;
1294 cdrunchangerqueue(softc->changer);
1296 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1297 && (softc->flags & CD_FLAG_ACTIVE)) {
1300 * Now that the first transaction to this
1301 * particular device has completed, we can go ahead
1302 * and schedule our timeouts.
1304 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1305 callout_reset(&changer->long_handle,
1306 changer_max_busy_seconds * hz,
1307 cdrunchangerqueue, changer);
1308 changer->flags |= CHANGER_TIMEOUT_SCHED;
1310 kprintf("cdchangerschedule: already have a long"
1313 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1314 callout_reset(&changer->short_handle,
1315 changer_min_busy_seconds * hz,
1316 cdshorttimeout, changer);
1317 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1319 kprintf("cdchangerschedule: already have a short "
1323 * We just scheduled timeouts, no need to schedule
1326 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1332 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1333 u_int32_t cam_flags,
1334 u_int32_t sense_flags),
1335 u_int32_t cam_flags, u_int32_t sense_flags)
1337 struct cd_softc *softc;
1338 struct cam_periph *periph;
1341 periph = xpt_path_periph(ccb->ccb_h.path);
1342 softc = (struct cd_softc *)periph->softc;
1344 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1345 &softc->device_stats);
1347 if (softc->flags & CD_FLAG_CHANGER)
1348 cdchangerschedule(softc);
1354 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1356 struct cd_softc *softc;
1358 softc = (struct cd_softc *)periph->softc;
1360 if (softc->flags & CD_FLAG_CHANGER) {
1362 * This should work the first time this device is woken up,
1363 * but just in case it doesn't, we use a while loop.
1365 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1367 * If this changer isn't already queued, queue it up.
1369 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1370 softc->pinfo.priority = 1;
1371 softc->pinfo.generation =
1372 ++softc->changer->devq.generation;
1373 camq_insert(&softc->changer->devq,
1374 (cam_pinfo *)softc);
1376 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1377 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1378 && ((softc->changer->flags
1379 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1380 softc->changer->flags |= CHANGER_MANUAL_CALL;
1381 cdrunchangerqueue(softc->changer);
1383 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1387 return(cam_periph_getccb(periph, priority));
1391 * Actually translate the requested transfer into one the physical driver
1392 * can understand. The transfer is described by a buf and will include
1393 * only one physical transfer.
1396 cdstrategy(struct dev_strategy_args *ap)
1398 cdev_t dev = ap->a_head.a_dev;
1399 struct bio *bio = ap->a_bio;
1400 struct buf *bp = bio->bio_buf;
1401 struct cam_periph *periph;
1402 struct cd_softc *softc;
1406 periph = cam_extend_get(cdperiphs, unit);
1407 if (periph == NULL) {
1408 bp->b_error = ENXIO;
1412 cam_periph_lock(periph);
1413 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1415 softc = (struct cd_softc *)periph->softc;
1418 * If the device has been made invalid, error out
1420 if ((softc->flags & CD_FLAG_INVALID)) {
1421 cam_periph_unlock(periph);
1422 bp->b_error = ENXIO;
1427 * If we don't have valid media, look for it before trying to
1430 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1433 error = cdcheckmedia(periph);
1435 cam_periph_unlock(periph);
1436 bp->b_error = error;
1442 * Place it in the queue of disk activities for this disk
1444 bioqdisksort(&softc->bio_queue, bio);
1447 * Schedule ourselves for performing the work. We do things
1448 * differently for changers.
1450 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1451 xpt_schedule(periph, /* XXX priority */1);
1453 cdschedule(periph, /* priority */ 1);
1455 cam_periph_unlock(periph);
1458 bp->b_flags |= B_ERROR;
1460 * Correctly set the buf to indicate a completed xfer
1462 bp->b_resid = bp->b_bcount;
1468 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1470 struct cd_softc *softc;
1473 struct ccb_scsiio *csio;
1474 struct scsi_read_capacity_data *rcap;
1476 softc = (struct cd_softc *)periph->softc;
1478 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1480 switch (softc->state) {
1481 case CD_STATE_NORMAL:
1483 bio = bioq_first(&softc->bio_queue);
1484 if (periph->immediate_priority <= periph->pinfo.priority) {
1485 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1487 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1489 periph->immediate_priority = CAM_PRIORITY_NONE;
1490 wakeup(&periph->ccb_list);
1491 } else if (bio == NULL) {
1492 xpt_release_ccb(start_ccb);
1495 bioq_remove(&softc->bio_queue, bio);
1497 devstat_start_transaction(&softc->device_stats);
1499 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1501 scsi_read_write(&start_ccb->csio,
1503 /* cbfcnp */ cddone,
1504 (bp->b_flags & B_ORDERED) != 0 ?
1507 /* read */(bp->b_cmd == BUF_CMD_READ),
1509 /* minimum_cmd_size */ 10,
1511 bio->bio_offset / softc->params.blksize,
1512 bp->b_bcount / softc->params.blksize,
1513 /* data_ptr */ bp->b_data,
1514 /* dxfer_len */ bp->b_bcount,
1515 /* sense_len */ SSD_FULL_SIZE,
1516 /* timeout */ 30000);
1517 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1520 LIST_INSERT_HEAD(&softc->pending_ccbs,
1521 &start_ccb->ccb_h, periph_links.le);
1523 softc->outstanding_cmds++;
1524 /* We expect a unit attention from this device */
1525 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1526 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1527 softc->flags &= ~CD_FLAG_RETRY_UA;
1530 start_ccb->ccb_h.ccb_bio = bio;
1531 bio = bioq_first(&softc->bio_queue);
1533 xpt_action(start_ccb);
1536 /* Have more work to do, so ensure we stay scheduled */
1537 xpt_schedule(periph, /* XXX priority */1);
1541 case CD_STATE_PROBE:
1544 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1545 csio = &start_ccb->csio;
1546 scsi_read_capacity(csio,
1553 start_ccb->ccb_h.ccb_bio = NULL;
1554 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1555 xpt_action(start_ccb);
1562 cddone(struct cam_periph *periph, union ccb *done_ccb)
1564 struct cd_softc *softc;
1565 struct ccb_scsiio *csio;
1567 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1569 softc = (struct cd_softc *)periph->softc;
1570 csio = &done_ccb->csio;
1572 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1573 case CD_CCB_BUFFER_IO:
1579 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1583 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1586 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1591 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1592 if (error == ERESTART) {
1594 * A retry was scheuled, so
1605 xpt_print(periph->path,
1606 "cddone: got error %#x back\n", error);
1607 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1608 bioq_remove(&softc->bio_queue, q_bio);
1609 q_bp = q_bio->bio_buf;
1610 q_bp->b_resid = q_bp->b_bcount;
1611 q_bp->b_error = EIO;
1612 q_bp->b_flags |= B_ERROR;
1615 bp->b_resid = bp->b_bcount;
1616 bp->b_error = error;
1617 bp->b_flags |= B_ERROR;
1618 cam_release_devq(done_ccb->ccb_h.path,
1622 /*getcount_only*/0);
1625 bp->b_resid = csio->resid;
1627 if (bp->b_resid != 0) {
1628 /* Short transfer ??? */
1629 bp->b_flags |= B_ERROR;
1633 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1634 softc->outstanding_cmds--;
1636 if (softc->flags & CD_FLAG_CHANGER)
1637 cdchangerschedule(softc);
1639 devstat_end_transaction_buf(&softc->device_stats, bp);
1645 struct scsi_read_capacity_data *rdcap;
1646 char announce_buf[120]; /*
1647 * Currently (9/30/97) the
1648 * longest possible announce
1649 * buffer is 108 bytes, for the
1650 * first error case below.
1651 * That is 39 bytes for the
1652 * basic string, 16 bytes for the
1653 * biggest sense key (hardware
1654 * error), 52 bytes for the
1655 * text of the largest sense
1656 * qualifier valid for a CDROM,
1657 * (0x72, 0x03 or 0x04,
1658 * 0x03), and one byte for the
1659 * null terminating character.
1660 * To allow for longer strings,
1661 * the announce buffer is 120
1664 struct cd_params *cdp;
1666 cdp = &softc->params;
1668 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1670 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1671 cdp->blksize = scsi_4btoul (rdcap->length);
1673 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1675 ksnprintf(announce_buf, sizeof(announce_buf),
1676 "cd present [%lu x %lu byte records]",
1677 cdp->disksize, (u_long)cdp->blksize);
1682 * Retry any UNIT ATTENTION type errors. They
1683 * are expected at boot.
1685 error = cderror(done_ccb, CAM_RETRY_SELTO,
1686 SF_RETRY_UA | SF_NO_PRINT);
1687 if (error == ERESTART) {
1689 * A retry was scheuled, so
1693 } else if (error != 0) {
1695 struct scsi_sense_data *sense;
1697 int sense_key, error_code;
1700 struct ccb_getdev cgd;
1702 /* Don't wedge this device's queue */
1703 cam_release_devq(done_ccb->ccb_h.path,
1707 /*getcount_only*/0);
1709 status = done_ccb->ccb_h.status;
1711 xpt_setup_ccb(&cgd.ccb_h,
1712 done_ccb->ccb_h.path,
1714 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1715 xpt_action((union ccb *)&cgd);
1717 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1718 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1719 || ((status & CAM_AUTOSNS_VALID) == 0))
1725 sense = &csio->sense_data;
1726 scsi_extract_sense(sense, &error_code,
1731 * Attach to anything that claims to be a
1732 * CDROM or WORM device, as long as it
1733 * doesn't return a "Logical unit not
1734 * supported" (0x25) error.
1736 if ((have_sense) && (asc != 0x25)
1737 && (error_code == SSD_CURRENT_ERROR)) {
1738 const char *sense_key_desc;
1739 const char *asc_desc;
1741 scsi_sense_desc(sense_key, asc, ascq,
1745 ksnprintf(announce_buf,
1746 sizeof(announce_buf),
1747 "Attempt to query device "
1748 "size failed: %s, %s",
1751 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1753 * We only print out an error for
1754 * CDROM type devices. For WORM
1755 * devices, we don't print out an
1756 * error since a few WORM devices
1757 * don't support CDROM commands.
1758 * If we have sense information, go
1759 * ahead and print it out.
1760 * Otherwise, just say that we
1765 * Just print out the error, not
1766 * the full probe message, when we
1773 xpt_print(periph->path,
1774 "got CAM status %#x\n",
1775 done_ccb->ccb_h.status);
1777 xpt_print(periph->path, "fatal error, "
1778 "failed to attach to device\n");
1780 * Invalidate this peripheral.
1782 cam_periph_invalidate(periph);
1784 announce_buf[0] = '\0';
1788 * Invalidate this peripheral.
1790 cam_periph_invalidate(periph);
1791 announce_buf[0] = '\0';
1795 kfree(rdcap, M_SCSICD);
1796 if (announce_buf[0] != '\0') {
1797 xpt_announce_periph(periph, announce_buf);
1798 if (softc->flags & CD_FLAG_CHANGER)
1799 cdchangerschedule(softc);
1801 * Create our sysctl variables, now that we know
1802 * we have successfully attached.
1804 taskqueue_enqueue(taskqueue_thread[mycpuid],
1805 &softc->sysctl_task);
1807 softc->state = CD_STATE_NORMAL;
1809 * Since our peripheral may be invalidated by an error
1810 * above or an external event, we must release our CCB
1811 * before releasing the probe lock on the peripheral.
1812 * The peripheral will only go away once the last lock
1813 * is removed, and we need it around for the CCB release
1816 xpt_release_ccb(done_ccb);
1817 cam_periph_unhold(periph, 0);
1820 case CD_CCB_WAITING:
1822 /* Caller will release the CCB */
1823 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1824 ("trying to wakeup ccbwait\n"));
1826 wakeup(&done_ccb->ccb_h.cbfcnp);
1832 xpt_release_ccb(done_ccb);
1835 static union cd_pages *
1836 cdgetpage(struct cd_mode_params *mode_params)
1838 union cd_pages *page;
1840 if (mode_params->cdb_size == 10)
1841 page = (union cd_pages *)find_mode_page_10(
1842 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1844 page = (union cd_pages *)find_mode_page_6(
1845 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1851 cdgetpagesize(int page_num)
1855 for (i = 0; i < (sizeof(cd_page_size_table)/
1856 sizeof(cd_page_size_table[0])); i++) {
1857 if (cd_page_size_table[i].page == page_num)
1858 return (cd_page_size_table[i].page_size);
1864 cdioctl(struct dev_ioctl_args *ap)
1866 cdev_t dev = ap->a_head.a_dev;
1867 caddr_t addr = ap->a_data;
1868 struct cam_periph *periph;
1869 struct cd_softc *softc;
1870 int unit, error = 0;
1874 periph = cam_extend_get(cdperiphs, unit);
1878 cam_periph_lock(periph);
1879 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1881 softc = (struct cd_softc *)periph->softc;
1883 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1884 ("trying to do ioctl %#lx\n", ap->a_cmd));
1886 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1887 cam_periph_unlock(periph);
1888 cam_periph_release(periph);
1893 * If we don't have media loaded, check for it. If still don't
1894 * have media loaded, we can only do a load or eject.
1896 * We only care whether media is loaded if this is a cd-specific ioctl
1897 * (thus the IOCGROUP check below). Note that this will break if
1898 * anyone adds any ioctls into the switch statement below that don't
1899 * have their ioctl group set to 'c'.
1901 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1902 && ((ap->a_cmd != CDIOCCLOSE)
1903 && (ap->a_cmd != CDIOCEJECT))
1904 && (IOCGROUP(ap->a_cmd) == 'c')) {
1905 error = cdcheckmedia(periph);
1907 cam_periph_unhold(periph, 1);
1912 * Drop the lock here so later mallocs can use WAITOK. The periph
1913 * is essentially locked still with the cam_periph_hold call above.
1915 cam_periph_unlock(periph);
1917 switch (ap->a_cmd) {
1919 case CDIOCPLAYTRACKS:
1921 struct ioc_play_track *args
1922 = (struct ioc_play_track *)addr;
1923 struct cd_mode_params params;
1924 union cd_pages *page;
1926 params.alloc_len = sizeof(union cd_mode_data_6_10);
1927 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1930 cam_periph_lock(periph);
1931 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1932 ("trying to do CDIOCPLAYTRACKS\n"));
1934 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1936 kfree(params.mode_buf, M_SCSICD);
1937 cam_periph_unlock(periph);
1940 page = cdgetpage(¶ms);
1942 page->audio.flags &= ~CD_PA_SOTC;
1943 page->audio.flags |= CD_PA_IMMED;
1944 error = cdsetmode(periph, ¶ms);
1945 kfree(params.mode_buf, M_SCSICD);
1947 cam_periph_unlock(periph);
1952 * This was originally implemented with the PLAY
1953 * AUDIO TRACK INDEX command, but that command was
1954 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1955 * drives support it but ATAPI and ATAPI-derivative
1956 * drives don't seem to support it. So we keep a
1957 * cache of the table of contents and translate
1958 * track numbers to MSF format.
1960 if (softc->flags & CD_FLAG_VALID_TOC) {
1961 union msf_lba *sentry, *eentry;
1964 if (args->end_track <
1965 softc->toc.header.ending_track + 1)
1967 if (args->end_track >
1968 softc->toc.header.ending_track + 1)
1970 softc->toc.header.ending_track + 1;
1971 st = args->start_track -
1972 softc->toc.header.starting_track;
1973 et = args->end_track -
1974 softc->toc.header.starting_track;
1977 || (st > (softc->toc.header.ending_track -
1978 softc->toc.header.starting_track))) {
1982 sentry = &softc->toc.entries[st].addr;
1983 eentry = &softc->toc.entries[et].addr;
1984 error = cdplaymsf(periph,
1993 * If we don't have a valid TOC, try the
1994 * play track index command. It is part of
1995 * the SCSI-2 spec, but was removed in the
1996 * MMC specs. ATAPI and ATAPI-derived
1997 * drives don't support it.
1999 if (softc->quirks & CD_Q_BCD_TRACKS) {
2001 bin2bcd(args->start_track);
2003 bin2bcd(args->end_track);
2005 error = cdplaytracks(periph,
2011 cam_periph_unlock(periph);
2016 struct ioc_play_msf *args
2017 = (struct ioc_play_msf *) addr;
2018 struct cd_mode_params params;
2019 union cd_pages *page;
2021 params.alloc_len = sizeof(union cd_mode_data_6_10);
2022 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2025 cam_periph_lock(periph);
2026 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2027 ("trying to do CDIOCPLAYMSF\n"));
2029 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2031 kfree(params.mode_buf, M_SCSICD);
2032 cam_periph_unlock(periph);
2035 page = cdgetpage(¶ms);
2037 page->audio.flags &= ~CD_PA_SOTC;
2038 page->audio.flags |= CD_PA_IMMED;
2039 error = cdsetmode(periph, ¶ms);
2040 kfree(params.mode_buf, M_SCSICD);
2042 cam_periph_unlock(periph);
2045 error = cdplaymsf(periph,
2052 cam_periph_unlock(periph);
2055 case CDIOCPLAYBLOCKS:
2057 struct ioc_play_blocks *args
2058 = (struct ioc_play_blocks *) addr;
2059 struct cd_mode_params params;
2060 union cd_pages *page;
2062 params.alloc_len = sizeof(union cd_mode_data_6_10);
2063 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2065 cam_periph_lock(periph);
2066 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2067 ("trying to do CDIOCPLAYBLOCKS\n"));
2070 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2072 kfree(params.mode_buf, M_SCSICD);
2073 cam_periph_unlock(periph);
2076 page = cdgetpage(¶ms);
2078 page->audio.flags &= ~CD_PA_SOTC;
2079 page->audio.flags |= CD_PA_IMMED;
2080 error = cdsetmode(periph, ¶ms);
2081 kfree(params.mode_buf, M_SCSICD);
2083 cam_periph_unlock(periph);
2086 error = cdplay(periph, args->blk, args->len);
2087 cam_periph_unlock(periph);
2090 case CDIOCREADSUBCHANNEL:
2092 struct ioc_read_subchannel *args
2093 = (struct ioc_read_subchannel *) addr;
2094 struct cd_sub_channel_info *data;
2095 u_int32_t len = args->data_len;
2097 data = kmalloc(sizeof(struct cd_sub_channel_info),
2098 M_SCSICD, M_WAITOK);
2100 cam_periph_lock(periph);
2101 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2102 ("trying to do CDIOCREADSUBCHANNEL\n"));
2104 if ((len > sizeof(struct cd_sub_channel_info)) ||
2105 (len < sizeof(struct cd_sub_channel_header))) {
2107 "scsi_cd: cdioctl: "
2108 "cdioreadsubchannel: error, len=%d\n",
2111 kfree(data, M_SCSICD);
2112 cam_periph_unlock(periph);
2116 if (softc->quirks & CD_Q_BCD_TRACKS)
2117 args->track = bin2bcd(args->track);
2119 error = cdreadsubchannel(periph, args->address_format,
2120 args->data_format, args->track, data, len);
2123 kfree(data, M_SCSICD);
2124 cam_periph_unlock(periph);
2127 if (softc->quirks & CD_Q_BCD_TRACKS)
2128 data->what.track_info.track_number =
2129 bcd2bin(data->what.track_info.track_number);
2130 len = min(len, ((data->header.data_len[0] << 8) +
2131 data->header.data_len[1] +
2132 sizeof(struct cd_sub_channel_header)));
2133 cam_periph_unlock(periph);
2134 if (copyout(data, args->data, len) != 0) {
2137 kfree(data, M_SCSICD);
2141 case CDIOREADTOCHEADER:
2143 struct ioc_toc_header *th;
2145 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2148 cam_periph_lock(periph);
2149 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2150 ("trying to do CDIOREADTOCHEADER\n"));
2152 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2153 sizeof (*th), /*sense_flags*/0);
2155 kfree(th, M_SCSICD);
2156 cam_periph_unlock(periph);
2159 if (softc->quirks & CD_Q_BCD_TRACKS) {
2160 /* we are going to have to convert the BCD
2161 * encoding on the cd to what is expected
2163 th->starting_track =
2164 bcd2bin(th->starting_track);
2165 th->ending_track = bcd2bin(th->ending_track);
2167 th->len = ntohs(th->len);
2168 bcopy(th, addr, sizeof(*th));
2169 kfree(th, M_SCSICD);
2170 cam_periph_unlock(periph);
2173 case CDIOREADTOCENTRYS:
2175 struct cd_tocdata *data;
2176 struct cd_toc_single *lead;
2177 struct ioc_read_toc_entry *te =
2178 (struct ioc_read_toc_entry *) addr;
2179 struct ioc_toc_header *th;
2180 u_int32_t len, readlen, idx, num;
2181 u_int32_t starting_track = te->starting_track;
2183 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2184 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2186 cam_periph_lock(periph);
2187 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2188 ("trying to do CDIOREADTOCENTRYS\n"));
2190 if (te->data_len < sizeof(struct cd_toc_entry)
2191 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2192 || (te->address_format != CD_MSF_FORMAT
2193 && te->address_format != CD_LBA_FORMAT)) {
2195 kprintf("scsi_cd: error in readtocentries, "
2196 "returning EINVAL\n");
2197 kfree(data, M_SCSICD);
2198 kfree(lead, M_SCSICD);
2199 cam_periph_unlock(periph);
2204 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2205 sizeof (*th), /*sense_flags*/0);
2207 kfree(data, M_SCSICD);
2208 kfree(lead, M_SCSICD);
2209 cam_periph_unlock(periph);
2213 if (softc->quirks & CD_Q_BCD_TRACKS) {
2214 /* we are going to have to convert the BCD
2215 * encoding on the cd to what is expected
2217 th->starting_track =
2218 bcd2bin(th->starting_track);
2219 th->ending_track = bcd2bin(th->ending_track);
2222 if (starting_track == 0)
2223 starting_track = th->starting_track;
2224 else if (starting_track == LEADOUT)
2225 starting_track = th->ending_track + 1;
2226 else if (starting_track < th->starting_track ||
2227 starting_track > th->ending_track + 1) {
2228 kprintf("scsi_cd: error in readtocentries, "
2229 "returning EINVAL\n");
2230 kfree(data, M_SCSICD);
2231 kfree(lead, M_SCSICD);
2232 cam_periph_unlock(periph);
2237 /* calculate reading length without leadout entry */
2238 readlen = (th->ending_track - starting_track + 1) *
2239 sizeof(struct cd_toc_entry);
2241 /* and with leadout entry */
2242 len = readlen + sizeof(struct cd_toc_entry);
2243 if (te->data_len < len) {
2248 if (len > sizeof(data->entries)) {
2249 kprintf("scsi_cd: error in readtocentries, "
2250 "returning EINVAL\n");
2252 kfree(data, M_SCSICD);
2253 kfree(lead, M_SCSICD);
2254 cam_periph_unlock(periph);
2257 num = len / sizeof(struct cd_toc_entry);
2260 error = cdreadtoc(periph, te->address_format,
2263 readlen + sizeof (*th),
2266 kfree(data, M_SCSICD);
2267 kfree(lead, M_SCSICD);
2268 cam_periph_unlock(periph);
2273 /* make leadout entry if needed */
2274 idx = starting_track + num - 1;
2275 if (softc->quirks & CD_Q_BCD_TRACKS)
2276 th->ending_track = bcd2bin(th->ending_track);
2277 if (idx == th->ending_track + 1) {
2278 error = cdreadtoc(periph, te->address_format,
2279 LEADOUT, (u_int8_t *)lead,
2283 kfree(data, M_SCSICD);
2284 kfree(lead, M_SCSICD);
2285 cam_periph_unlock(periph);
2288 data->entries[idx - starting_track] =
2291 if (softc->quirks & CD_Q_BCD_TRACKS) {
2292 for (idx = 0; idx < num - 1; idx++) {
2293 data->entries[idx].track =
2294 bcd2bin(data->entries[idx].track);
2298 cam_periph_unlock(periph);
2299 error = copyout(data->entries, te->data, len);
2300 kfree(data, M_SCSICD);
2301 kfree(lead, M_SCSICD);
2304 case CDIOREADTOCENTRY:
2306 struct cd_toc_single *data;
2307 struct ioc_read_toc_single_entry *te =
2308 (struct ioc_read_toc_single_entry *) addr;
2309 struct ioc_toc_header *th;
2312 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2314 cam_periph_lock(periph);
2315 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2316 ("trying to do CDIOREADTOCENTRY\n"));
2318 if (te->address_format != CD_MSF_FORMAT
2319 && te->address_format != CD_LBA_FORMAT) {
2320 kprintf("error in readtocentry, "
2321 " returning EINVAL\n");
2322 kfree(data, M_SCSICD);
2324 cam_periph_unlock(periph);
2329 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2330 sizeof (*th), /*sense_flags*/0);
2332 kfree(data, M_SCSICD);
2333 cam_periph_unlock(periph);
2337 if (softc->quirks & CD_Q_BCD_TRACKS) {
2338 /* we are going to have to convert the BCD
2339 * encoding on the cd to what is expected
2341 th->starting_track =
2342 bcd2bin(th->starting_track);
2343 th->ending_track = bcd2bin(th->ending_track);
2347 track = th->starting_track;
2348 else if (track == LEADOUT)
2350 else if (track < th->starting_track ||
2351 track > th->ending_track + 1) {
2352 kprintf("error in readtocentry, "
2353 " returning EINVAL\n");
2354 kfree(data, M_SCSICD);
2356 cam_periph_unlock(periph);
2360 error = cdreadtoc(periph, te->address_format, track,
2361 (u_int8_t *)data, sizeof(*data),
2364 kfree(data, M_SCSICD);
2365 cam_periph_unlock(periph);
2369 if (softc->quirks & CD_Q_BCD_TRACKS)
2370 data->entry.track = bcd2bin(data->entry.track);
2371 bcopy(&data->entry, &te->entry,
2372 sizeof(struct cd_toc_entry));
2373 kfree(data, M_SCSICD);
2374 cam_periph_unlock(periph);
2379 struct ioc_patch *arg = (struct ioc_patch *)addr;
2380 struct cd_mode_params params;
2381 union cd_pages *page;
2383 params.alloc_len = sizeof(union cd_mode_data_6_10);
2384 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2387 cam_periph_lock(periph);
2388 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2389 ("trying to do CDIOCSETPATCH\n"));
2391 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2393 kfree(params.mode_buf, M_SCSICD);
2394 cam_periph_unlock(periph);
2397 page = cdgetpage(¶ms);
2399 page->audio.port[LEFT_PORT].channels =
2401 page->audio.port[RIGHT_PORT].channels =
2403 page->audio.port[2].channels = arg->patch[2];
2404 page->audio.port[3].channels = arg->patch[3];
2405 error = cdsetmode(periph, ¶ms);
2406 kfree(params.mode_buf, M_SCSICD);
2407 cam_periph_unlock(periph);
2412 struct ioc_vol *arg = (struct ioc_vol *) addr;
2413 struct cd_mode_params params;
2414 union cd_pages *page;
2416 params.alloc_len = sizeof(union cd_mode_data_6_10);
2417 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2420 cam_periph_lock(periph);
2421 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2422 ("trying to do CDIOCGETVOL\n"));
2424 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2426 kfree(params.mode_buf, M_SCSICD);
2427 cam_periph_unlock(periph);
2430 page = cdgetpage(¶ms);
2432 arg->vol[LEFT_PORT] =
2433 page->audio.port[LEFT_PORT].volume;
2434 arg->vol[RIGHT_PORT] =
2435 page->audio.port[RIGHT_PORT].volume;
2436 arg->vol[2] = page->audio.port[2].volume;
2437 arg->vol[3] = page->audio.port[3].volume;
2438 kfree(params.mode_buf, M_SCSICD);
2439 cam_periph_unlock(periph);
2444 struct ioc_vol *arg = (struct ioc_vol *) addr;
2445 struct cd_mode_params params;
2446 union cd_pages *page;
2448 params.alloc_len = sizeof(union cd_mode_data_6_10);
2449 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2452 cam_periph_lock(periph);
2453 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2454 ("trying to do CDIOCSETVOL\n"));
2456 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2458 kfree(params.mode_buf, M_SCSICD);
2459 cam_periph_unlock(periph);
2462 page = cdgetpage(¶ms);
2464 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2465 page->audio.port[LEFT_PORT].volume =
2466 arg->vol[LEFT_PORT];
2467 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2468 page->audio.port[RIGHT_PORT].volume =
2469 arg->vol[RIGHT_PORT];
2470 page->audio.port[2].volume = arg->vol[2];
2471 page->audio.port[3].volume = arg->vol[3];
2472 error = cdsetmode(periph, ¶ms);
2473 cam_periph_unlock(periph);
2474 kfree(params.mode_buf, M_SCSICD);
2479 struct cd_mode_params params;
2480 union cd_pages *page;
2482 params.alloc_len = sizeof(union cd_mode_data_6_10);
2483 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2486 cam_periph_lock(periph);
2487 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2488 ("trying to do CDIOCSETMONO\n"));
2490 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2492 kfree(params.mode_buf, M_SCSICD);
2493 cam_periph_unlock(periph);
2496 page = cdgetpage(¶ms);
2498 page->audio.port[LEFT_PORT].channels =
2499 LEFT_CHANNEL | RIGHT_CHANNEL;
2500 page->audio.port[RIGHT_PORT].channels =
2501 LEFT_CHANNEL | RIGHT_CHANNEL;
2502 page->audio.port[2].channels = 0;
2503 page->audio.port[3].channels = 0;
2504 error = cdsetmode(periph, ¶ms);
2505 cam_periph_unlock(periph);
2506 kfree(params.mode_buf, M_SCSICD);
2509 case CDIOCSETSTEREO:
2511 struct cd_mode_params params;
2512 union cd_pages *page;
2514 params.alloc_len = sizeof(union cd_mode_data_6_10);
2515 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2518 cam_periph_lock(periph);
2519 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2520 ("trying to do CDIOCSETSTEREO\n"));
2522 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2524 kfree(params.mode_buf, M_SCSICD);
2525 cam_periph_unlock(periph);
2528 page = cdgetpage(¶ms);
2530 page->audio.port[LEFT_PORT].channels =
2532 page->audio.port[RIGHT_PORT].channels =
2534 page->audio.port[2].channels = 0;
2535 page->audio.port[3].channels = 0;
2536 error = cdsetmode(periph, ¶ms);
2537 kfree(params.mode_buf, M_SCSICD);
2538 cam_periph_unlock(periph);
2543 struct cd_mode_params params;
2544 union cd_pages *page;
2546 params.alloc_len = sizeof(union cd_mode_data_6_10);
2547 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2550 cam_periph_lock(periph);
2551 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2552 ("trying to do CDIOCSETMUTE\n"));
2554 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2556 kfree(¶ms, M_SCSICD);
2557 cam_periph_unlock(periph);
2560 page = cdgetpage(¶ms);
2562 page->audio.port[LEFT_PORT].channels = 0;
2563 page->audio.port[RIGHT_PORT].channels = 0;
2564 page->audio.port[2].channels = 0;
2565 page->audio.port[3].channels = 0;
2566 error = cdsetmode(periph, ¶ms);
2567 kfree(params.mode_buf, M_SCSICD);
2568 cam_periph_unlock(periph);
2573 struct cd_mode_params params;
2574 union cd_pages *page;
2576 params.alloc_len = sizeof(union cd_mode_data_6_10);
2577 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2580 cam_periph_lock(periph);
2581 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2582 ("trying to do CDIOCSETLEFT\n"));
2584 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2586 kfree(params.mode_buf, M_SCSICD);
2587 cam_periph_unlock(periph);
2590 page = cdgetpage(¶ms);
2592 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2593 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2594 page->audio.port[2].channels = 0;
2595 page->audio.port[3].channels = 0;
2596 error = cdsetmode(periph, ¶ms);
2597 kfree(params.mode_buf, M_SCSICD);
2598 cam_periph_unlock(periph);
2603 struct cd_mode_params params;
2604 union cd_pages *page;
2606 params.alloc_len = sizeof(union cd_mode_data_6_10);
2607 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2610 cam_periph_lock(periph);
2611 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2612 ("trying to do CDIOCSETRIGHT\n"));
2614 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2616 kfree(params.mode_buf, M_SCSICD);
2617 cam_periph_unlock(periph);
2620 page = cdgetpage(¶ms);
2622 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2623 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2624 page->audio.port[2].channels = 0;
2625 page->audio.port[3].channels = 0;
2626 error = cdsetmode(periph, ¶ms);
2627 kfree(params.mode_buf, M_SCSICD);
2628 cam_periph_unlock(periph);
2632 cam_periph_lock(periph);
2633 error = cdpause(periph, 1);
2634 cam_periph_unlock(periph);
2637 cam_periph_lock(periph);
2638 error = cdpause(periph, 0);
2639 cam_periph_unlock(periph);
2642 cam_periph_lock(periph);
2643 error = cdstartunit(periph, 0);
2644 cam_periph_unlock(periph);
2647 cam_periph_lock(periph);
2648 error = cdstartunit(periph, 1);
2649 cam_periph_unlock(periph);
2652 cam_periph_lock(periph);
2653 error = cdstopunit(periph, 0);
2654 cam_periph_unlock(periph);
2657 cam_periph_lock(periph);
2658 error = cdstopunit(periph, 1);
2659 cam_periph_unlock(periph);
2662 cam_periph_lock(periph);
2663 cdprevent(periph, PR_ALLOW);
2664 cam_periph_unlock(periph);
2667 cam_periph_lock(periph);
2668 cdprevent(periph, PR_PREVENT);
2669 cam_periph_unlock(periph);
2672 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2676 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2680 /* return (cd_reset(periph)); */
2683 case CDRIOCREADSPEED:
2684 cam_periph_lock(periph);
2685 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2686 cam_periph_unlock(periph);
2688 case CDRIOCWRITESPEED:
2689 cam_periph_lock(periph);
2690 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2691 cam_periph_unlock(periph);
2694 case DVDIOCREPORTKEY: {
2695 struct dvd_authinfo *authinfo;
2697 authinfo = (struct dvd_authinfo *)addr;
2699 cam_periph_lock(periph);
2700 if (ap->a_cmd == DVDIOCREPORTKEY)
2701 error = cdreportkey(periph, authinfo);
2703 error = cdsendkey(periph, authinfo);
2704 cam_periph_unlock(periph);
2707 case DVDIOCREADSTRUCTURE: {
2708 struct dvd_struct *dvdstruct;
2710 dvdstruct = (struct dvd_struct *)addr;
2712 cam_periph_lock(periph);
2713 error = cdreaddvdstructure(periph, dvdstruct);
2714 cam_periph_unlock(periph);
2719 cam_periph_lock(periph);
2720 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2721 cam_periph_unlock(periph);
2725 cam_periph_lock(periph);
2726 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2727 cam_periph_unhold(periph, 1);
2733 cdprevent(struct cam_periph *periph, int action)
2736 struct cd_softc *softc;
2739 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2741 softc = (struct cd_softc *)periph->softc;
2743 if (((action == PR_ALLOW)
2744 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2745 || ((action == PR_PREVENT)
2746 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2750 ccb = cdgetccb(periph, /* priority */ 1);
2752 scsi_prevent(&ccb->csio,
2758 /* timeout */60000);
2760 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2761 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2763 xpt_release_ccb(ccb);
2766 if (action == PR_ALLOW)
2767 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2769 softc->flags |= CD_FLAG_DISC_LOCKED;
2774 cdcheckmedia(struct cam_periph *periph)
2776 struct cd_softc *softc;
2777 struct ioc_toc_header *toch;
2778 struct cd_toc_single leadout;
2779 struct ccb_getdev cgd;
2780 u_int32_t size, toclen;
2781 int error, num_entries, cdindex;
2782 int first_track_audio;
2783 struct disk_info info;
2785 softc = (struct cd_softc *)periph->softc;
2787 first_track_audio = -1;
2790 cdprevent(periph, PR_PREVENT);
2793 * Set up disk info fields and tell the disk subsystem to reset
2794 * its internal copy of the label.
2796 bzero(&info, sizeof(info));
2797 info.d_type = DTYPE_SCSI;
2798 info.d_dsflags &= ~DSO_COMPATLABEL;
2799 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2802 * Grab the inquiry data to get the vendor and product names.
2803 * Put them in the typename and packname for the label.
2805 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2806 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2807 xpt_action((union ccb *)&cgd);
2810 strncpy(label->d_typename, cgd.inq_data.vendor,
2811 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2812 strncpy(label->d_packname, cgd.inq_data.product,
2813 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2816 * Clear the valid media and TOC flags until we've verified that we
2819 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2822 * Get the disc size and block size. If we can't get it, we don't
2823 * have media, most likely.
2825 if ((error = cdsize(periph, &size)) != 0) {
2827 * Set a bogus sector size, so the slice code won't try to
2828 * divide by 0 and panic the kernel.
2830 info.d_media_blksize = 2048;
2831 disk_setdiskinfo(&softc->disk, &info);
2834 * Tell devstat(9) we don't have a blocksize.
2836 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2838 cdprevent(periph, PR_ALLOW);
2842 info.d_media_blksize = softc->params.blksize;
2843 info.d_media_blocks = softc->params.disksize;
2844 disk_setdiskinfo(&softc->disk, &info);
2847 * We unconditionally (re)set the blocksize each time the
2848 * CD device is opened. This is because the CD can change,
2849 * and therefore the blocksize might change.
2850 * XXX problems here if some slice or partition is still
2851 * open with the old size?
2853 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2854 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2855 softc->device_stats.block_size = softc->params.blksize;
2857 softc->flags |= CD_FLAG_VALID_MEDIA;
2861 * Now we check the table of contents. This (currently) is only
2862 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2863 * things like present a separate entry in /dev for each track,
2864 * like that acd(4) driver does.
2866 bzero(&softc->toc, sizeof(softc->toc));
2867 toch = &softc->toc.header;
2870 * We will get errors here for media that doesn't have a table of
2871 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2872 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2873 * has not been recorded (no complete session) and the Format codes
2874 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2875 * with an INVALID FIELD IN CDB. Devices that are not capable of
2876 * reading an incomplete session on DDC/CD-R/RW media shall report
2877 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2879 * So this isn't fatal if we can't read the table of contents, it
2880 * just means that the user won't be able to issue the play tracks
2881 * ioctl, and likely lots of other stuff won't work either. They
2882 * need to burn the CD before we can do a whole lot with it. So
2883 * we don't print anything here if we get an error back.
2885 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2888 * Errors in reading the table of contents aren't fatal, we just
2889 * won't have a valid table of contents cached.
2893 bzero(&softc->toc, sizeof(softc->toc));
2897 if (softc->quirks & CD_Q_BCD_TRACKS) {
2898 toch->starting_track = bcd2bin(toch->starting_track);
2899 toch->ending_track = bcd2bin(toch->ending_track);
2902 /* Number of TOC entries, plus leadout */
2903 num_entries = (toch->ending_track - toch->starting_track) + 2;
2905 if (num_entries <= 0)
2908 toclen = num_entries * sizeof(struct cd_toc_entry);
2910 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2911 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2915 bzero(&softc->toc, sizeof(softc->toc));
2919 if (softc->quirks & CD_Q_BCD_TRACKS) {
2920 toch->starting_track = bcd2bin(toch->starting_track);
2921 toch->ending_track = bcd2bin(toch->ending_track);
2923 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2926 * XXX KDM is this necessary? Probably only if the drive doesn't
2927 * return leadout information with the table of contents.
2929 cdindex = toch->starting_track + num_entries -1;
2930 if (cdindex == toch->ending_track + 1) {
2932 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2933 (u_int8_t *)&leadout, sizeof(leadout),
2939 softc->toc.entries[cdindex - toch->starting_track] =
2942 if (softc->quirks & CD_Q_BCD_TRACKS) {
2943 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2944 softc->toc.entries[cdindex].track =
2945 bcd2bin(softc->toc.entries[cdindex].track);
2950 * Run through the TOC entries, find the first entry and determine
2951 * whether it is an audio or data track.
2953 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2954 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2955 if (softc->toc.entries[cdindex].control & 0x04)
2956 first_track_audio = 0;
2958 first_track_audio = 1;
2964 * If first_track_audio is non-zero, we either have an error (e.g.
2965 * couldn't find the starting track) or the first track is an audio
2966 * track. If first_track_audio is 0, the first track is a data
2967 * track that could have a disklabel. Attempt to read the
2968 * disklabel off the media, just in case the user put one there.
2970 if (first_track_audio == 0) {
2971 info.d_dsflags |= DSO_COMPATLABEL;
2972 info.d_dsflags &= ~DSO_NOLABELS;
2973 disk_setdiskinfo(&softc->disk, &info);
2975 softc->flags |= CD_FLAG_VALID_TOC;
2982 cdsize(struct cam_periph *periph, u_int32_t *size)
2984 struct cd_softc *softc;
2986 struct scsi_read_capacity_data *rcap_buf;
2989 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2991 softc = (struct cd_softc *)periph->softc;
2993 ccb = cdgetccb(periph, /* priority */ 1);
2995 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
2996 M_SCSICD, M_INTWAIT | M_ZERO);
2998 scsi_read_capacity(&ccb->csio,
3004 /* timeout */20000);
3006 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3007 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3009 xpt_release_ccb(ccb);
3011 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3012 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3014 * SCSI-3 mandates that the reported blocksize shall be 2048.
3015 * Older drives sometimes report funny values, trim it down to
3016 * 2048, or other parts of the kernel will get confused.
3018 * XXX we leave drives alone that might report 512 bytes, as
3019 * well as drives reporting more weird sizes like perhaps 4K.
3021 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3022 softc->params.blksize = 2048;
3024 kfree(rcap_buf, M_SCSICD);
3025 *size = softc->params.disksize;
3032 cd6byteworkaround(union ccb *ccb)
3035 struct cam_periph *periph;
3036 struct cd_softc *softc;
3037 struct cd_mode_params *params;
3040 periph = xpt_path_periph(ccb->ccb_h.path);
3041 softc = (struct cd_softc *)periph->softc;
3043 cdb = ccb->csio.cdb_io.cdb_bytes;
3045 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3046 || ((cdb[0] != MODE_SENSE_6)
3047 && (cdb[0] != MODE_SELECT_6)))
3051 * Because there is no convenient place to stash the overall
3052 * cd_mode_params structure pointer, we have to grab it like this.
3053 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3054 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3056 * XXX It would be nice if, at some point, we could increase the
3057 * number of available peripheral private pointers. Both pointers
3058 * are currently used in most every peripheral driver.
3062 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3063 if (params->mode_buf == ccb->csio.data_ptr) {
3070 * This shouldn't happen. All mode sense and mode select
3071 * operations in the cd(4) driver MUST go through cdgetmode() and
3075 xpt_print(periph->path,
3076 "mode buffer not found in mode queue!\n");
3080 params->cdb_size = 10;
3081 softc->minimum_command_size = 10;
3082 xpt_print(ccb->ccb_h.path,
3083 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3084 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3086 if (cdb[0] == MODE_SENSE_6) {
3087 struct scsi_mode_sense_10 ms10;
3088 struct scsi_mode_sense_6 *ms6;
3091 ms6 = (struct scsi_mode_sense_6 *)cdb;
3093 bzero(&ms10, sizeof(ms10));
3094 ms10.opcode = MODE_SENSE_10;
3095 ms10.byte2 = ms6->byte2;
3096 ms10.page = ms6->page;
3099 * 10 byte mode header, block descriptor,
3100 * sizeof(union cd_pages)
3102 len = sizeof(struct cd_mode_data_10);
3103 ccb->csio.dxfer_len = len;
3105 scsi_ulto2b(len, ms10.length);
3106 ms10.control = ms6->control;
3107 bcopy(&ms10, cdb, 10);
3108 ccb->csio.cdb_len = 10;
3110 struct scsi_mode_select_10 ms10;
3111 struct scsi_mode_select_6 *ms6;
3112 struct scsi_mode_header_6 *header6;
3113 struct scsi_mode_header_10 *header10;
3114 struct scsi_mode_page_header *page_header;
3115 int blk_desc_len, page_num, page_size, len;
3117 ms6 = (struct scsi_mode_select_6 *)cdb;
3119 bzero(&ms10, sizeof(ms10));
3120 ms10.opcode = MODE_SELECT_10;
3121 ms10.byte2 = ms6->byte2;
3123 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3124 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3126 page_header = find_mode_page_6(header6);
3127 page_num = page_header->page_code;
3129 blk_desc_len = header6->blk_desc_len;
3131 page_size = cdgetpagesize(page_num);
3133 if (page_size != (page_header->page_length +
3134 sizeof(*page_header)))
3135 page_size = page_header->page_length +
3136 sizeof(*page_header);
3138 len = sizeof(*header10) + blk_desc_len + page_size;
3140 len = min(params->alloc_len, len);
3143 * Since the 6 byte parameter header is shorter than the 10
3144 * byte parameter header, we need to copy the actual mode
3145 * page data, and the block descriptor, if any, so things wind
3146 * up in the right place. The regions will overlap, but
3147 * bcopy() does the right thing.
3149 bcopy(params->mode_buf + sizeof(*header6),
3150 params->mode_buf + sizeof(*header10),
3151 len - sizeof(*header10));
3153 /* Make sure these fields are set correctly. */
3154 scsi_ulto2b(0, header10->data_length);
3155 header10->medium_type = 0;
3156 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3158 ccb->csio.dxfer_len = len;
3160 scsi_ulto2b(len, ms10.length);
3161 ms10.control = ms6->control;
3162 bcopy(&ms10, cdb, 10);
3163 ccb->csio.cdb_len = 10;
3166 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3167 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3170 cam_release_devq(ccb->ccb_h.path,
3174 /*getcount_only*/0);
3181 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3183 struct cd_softc *softc;
3184 struct cam_periph *periph;
3187 periph = xpt_path_periph(ccb->ccb_h.path);
3188 softc = (struct cd_softc *)periph->softc;
3193 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3194 * CDB comes back with this particular error, try transforming it
3195 * into the 10 byte version.
3197 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3198 error = cd6byteworkaround(ccb);
3199 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3200 CAM_SCSI_STATUS_ERROR)
3201 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3202 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3203 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3204 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3205 int sense_key, error_code, asc, ascq;
3207 scsi_extract_sense(&ccb->csio.sense_data,
3208 &error_code, &sense_key, &asc, &ascq);
3209 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3210 error = cd6byteworkaround(ccb);
3213 if (error == ERESTART)
3218 * Until we have a better way of doing pack validation,
3219 * don't treat UAs as errors.
3221 sense_flags |= SF_RETRY_UA;
3222 return (cam_periph_error(ccb, cam_flags, sense_flags,
3223 &softc->saved_ccb));
3227 * Read table of contents
3230 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3231 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3233 struct scsi_read_toc *scsi_cmd;
3235 struct ccb_scsiio *csio;
3242 ccb = cdgetccb(periph, /* priority */ 1);
3248 /* cbfcnp */ cddone,
3249 /* flags */ CAM_DIR_IN,
3250 /* tag_action */ MSG_SIMPLE_Q_TAG,
3251 /* data_ptr */ data,
3252 /* dxfer_len */ len,
3253 /* sense_len */ SSD_FULL_SIZE,
3254 sizeof(struct scsi_read_toc),
3255 /* timeout */ 50000);
3257 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3258 bzero (scsi_cmd, sizeof(*scsi_cmd));
3260 if (mode == CD_MSF_FORMAT)
3261 scsi_cmd->byte2 |= CD_MSF;
3262 scsi_cmd->from_track = start;
3263 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3264 scsi_cmd->data_len[0] = (ntoc) >> 8;
3265 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3267 scsi_cmd->op_code = READ_TOC;
3269 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3270 /*sense_flags*/SF_RETRY_UA | sense_flags);
3272 xpt_release_ccb(ccb);
3278 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3279 u_int32_t format, int track,
3280 struct cd_sub_channel_info *data, u_int32_t len)
3282 struct scsi_read_subchannel *scsi_cmd;
3283 struct ccb_scsiio *csio;
3289 ccb = cdgetccb(periph, /* priority */ 1);
3295 /* cbfcnp */ cddone,
3296 /* flags */ CAM_DIR_IN,
3297 /* tag_action */ MSG_SIMPLE_Q_TAG,
3298 /* data_ptr */ (u_int8_t *)data,
3299 /* dxfer_len */ len,
3300 /* sense_len */ SSD_FULL_SIZE,
3301 sizeof(struct scsi_read_subchannel),
3302 /* timeout */ 50000);
3304 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3305 bzero (scsi_cmd, sizeof(*scsi_cmd));
3307 scsi_cmd->op_code = READ_SUBCHANNEL;
3308 if (mode == CD_MSF_FORMAT)
3309 scsi_cmd->byte1 |= CD_MSF;
3310 scsi_cmd->byte2 = SRS_SUBQ;
3311 scsi_cmd->subchan_format = format;
3312 scsi_cmd->track = track;
3313 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3314 scsi_cmd->control = 0;
3316 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3317 /*sense_flags*/SF_RETRY_UA);
3319 xpt_release_ccb(ccb);
3325 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3326 * routine. See comments in cd6byteworkaround() for details.
3329 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3332 struct ccb_scsiio *csio;
3333 struct cd_softc *softc;
3338 softc = (struct cd_softc *)periph->softc;
3340 ccb = cdgetccb(periph, /* priority */ 1);
3344 data->cdb_size = softc->minimum_command_size;
3345 if (data->cdb_size < 10)
3346 param_len = sizeof(struct cd_mode_data);
3348 param_len = sizeof(struct cd_mode_data_10);
3350 /* Don't say we've got more room than we actually allocated */
3351 param_len = min(param_len, data->alloc_len);
3353 scsi_mode_sense_len(csio,
3355 /* cbfcnp */ cddone,
3356 /* tag_action */ MSG_SIMPLE_Q_TAG,
3358 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3360 /* param_buf */ data->mode_buf,
3361 /* param_len */ param_len,
3362 /* minimum_cmd_size */ softc->minimum_command_size,
3363 /* sense_len */ SSD_FULL_SIZE,
3364 /* timeout */ 50000);
3367 * It would be nice not to have to do this, but there's no
3368 * available pointer in the CCB that would allow us to stuff the
3369 * mode params structure in there and retrieve it in
3370 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3371 * lets the caller know what CDB size we ended up using, so they
3372 * can find the actual mode page offset.
3374 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3376 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3377 /*sense_flags*/SF_RETRY_UA);
3379 xpt_release_ccb(ccb);
3381 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3384 * This is a bit of belt-and-suspenders checking, but if we run
3385 * into a situation where the target sends back multiple block
3386 * descriptors, we might not have enough space in the buffer to
3387 * see the whole mode page. Better to return an error than
3388 * potentially access memory beyond our malloced region.
3393 if (data->cdb_size == 10) {
3394 struct scsi_mode_header_10 *hdr10;
3396 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3397 data_len = scsi_2btoul(hdr10->data_length);
3398 data_len += sizeof(hdr10->data_length);
3400 struct scsi_mode_header_6 *hdr6;
3402 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3403 data_len = hdr6->data_length;
3404 data_len += sizeof(hdr6->data_length);
3408 * Complain if there is more mode data available than we
3409 * allocated space for. This could potentially happen if
3410 * we miscalculated the page length for some reason, if the
3411 * drive returns multiple block descriptors, or if it sets
3412 * the data length incorrectly.
3414 if (data_len > data->alloc_len) {
3415 xpt_print(periph->path, "allocated modepage %d length "
3416 "%d < returned length %d\n", page, data->alloc_len,
3425 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3426 * routine. See comments in cd6byteworkaround() for details.
3429 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3431 struct ccb_scsiio *csio;
3432 struct cd_softc *softc;
3434 int cdb_size, param_len;
3437 softc = (struct cd_softc *)periph->softc;
3439 ccb = cdgetccb(periph, /* priority */ 1);
3446 * If the data is formatted for the 10 byte version of the mode
3447 * select parameter list, we need to use the 10 byte CDB.
3448 * Otherwise, we use whatever the stored minimum command size.
3450 if (data->cdb_size == 10)
3451 cdb_size = data->cdb_size;
3453 cdb_size = softc->minimum_command_size;
3455 if (cdb_size >= 10) {
3456 struct scsi_mode_header_10 *mode_header;
3459 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3461 data_len = scsi_2btoul(mode_header->data_length);
3463 scsi_ulto2b(0, mode_header->data_length);
3465 * SONY drives do not allow a mode select with a medium_type
3466 * value that has just been returned by a mode sense; use a
3467 * medium_type of 0 (Default) instead.
3469 mode_header->medium_type = 0;
3472 * Pass back whatever the drive passed to us, plus the size
3473 * of the data length field.
3475 param_len = data_len + sizeof(mode_header->data_length);
3478 struct scsi_mode_header_6 *mode_header;
3480 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3482 param_len = mode_header->data_length + 1;
3484 mode_header->data_length = 0;
3486 * SONY drives do not allow a mode select with a medium_type
3487 * value that has just been returned by a mode sense; use a
3488 * medium_type of 0 (Default) instead.
3490 mode_header->medium_type = 0;
3493 /* Don't say we've got more room than we actually allocated */
3494 param_len = min(param_len, data->alloc_len);
3496 scsi_mode_select_len(csio,
3498 /* cbfcnp */ cddone,
3499 /* tag_action */ MSG_SIMPLE_Q_TAG,
3500 /* scsi_page_fmt */ 1,
3502 /* param_buf */ data->mode_buf,
3503 /* param_len */ param_len,
3504 /* minimum_cmd_size */ cdb_size,
3505 /* sense_len */ SSD_FULL_SIZE,
3506 /* timeout */ 50000);
3508 /* See comments in cdgetmode() and cd6byteworkaround(). */
3509 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3511 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3512 /*sense_flags*/SF_RETRY_UA);
3514 xpt_release_ccb(ccb);
3516 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3524 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3526 struct ccb_scsiio *csio;
3532 ccb = cdgetccb(periph, /* priority */ 1);
3535 * Use the smallest possible command to perform the operation.
3537 if ((len & 0xffff0000) == 0) {
3539 * We can fit in a 10 byte cdb.
3541 struct scsi_play_10 *scsi_cmd;
3543 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3544 bzero (scsi_cmd, sizeof(*scsi_cmd));
3545 scsi_cmd->op_code = PLAY_10;
3546 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3547 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3548 cdb_len = sizeof(*scsi_cmd);
3550 struct scsi_play_12 *scsi_cmd;
3552 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3553 bzero (scsi_cmd, sizeof(*scsi_cmd));
3554 scsi_cmd->op_code = PLAY_12;
3555 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3556 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3557 cdb_len = sizeof(*scsi_cmd);
3562 /*flags*/CAM_DIR_NONE,
3566 /*sense_len*/SSD_FULL_SIZE,
3568 /*timeout*/50 * 1000);
3570 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3571 /*sense_flags*/SF_RETRY_UA);
3573 xpt_release_ccb(ccb);
3579 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3580 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3582 struct scsi_play_msf *scsi_cmd;
3583 struct ccb_scsiio *csio;
3589 ccb = cdgetccb(periph, /* priority */ 1);
3595 /* cbfcnp */ cddone,
3596 /* flags */ CAM_DIR_NONE,
3597 /* tag_action */ MSG_SIMPLE_Q_TAG,
3598 /* data_ptr */ NULL,
3600 /* sense_len */ SSD_FULL_SIZE,
3601 sizeof(struct scsi_play_msf),
3602 /* timeout */ 50000);
3604 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3605 bzero (scsi_cmd, sizeof(*scsi_cmd));
3607 scsi_cmd->op_code = PLAY_MSF;
3608 scsi_cmd->start_m = startm;
3609 scsi_cmd->start_s = starts;
3610 scsi_cmd->start_f = startf;
3611 scsi_cmd->end_m = endm;
3612 scsi_cmd->end_s = ends;
3613 scsi_cmd->end_f = endf;
3615 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3616 /*sense_flags*/SF_RETRY_UA);
3618 xpt_release_ccb(ccb);
3625 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3626 u_int32_t etrack, u_int32_t eindex)
3628 struct scsi_play_track *scsi_cmd;
3629 struct ccb_scsiio *csio;
3635 ccb = cdgetccb(periph, /* priority */ 1);
3641 /* cbfcnp */ cddone,
3642 /* flags */ CAM_DIR_NONE,
3643 /* tag_action */ MSG_SIMPLE_Q_TAG,
3644 /* data_ptr */ NULL,
3646 /* sense_len */ SSD_FULL_SIZE,
3647 sizeof(struct scsi_play_track),
3648 /* timeout */ 50000);
3650 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3651 bzero (scsi_cmd, sizeof(*scsi_cmd));
3653 scsi_cmd->op_code = PLAY_TRACK;
3654 scsi_cmd->start_track = strack;
3655 scsi_cmd->start_index = sindex;
3656 scsi_cmd->end_track = etrack;
3657 scsi_cmd->end_index = eindex;
3659 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3660 /*sense_flags*/SF_RETRY_UA);
3662 xpt_release_ccb(ccb);
3668 cdpause(struct cam_periph *periph, u_int32_t go)
3670 struct scsi_pause *scsi_cmd;
3671 struct ccb_scsiio *csio;
3677 ccb = cdgetccb(periph, /* priority */ 1);
3683 /* cbfcnp */ cddone,
3684 /* flags */ CAM_DIR_NONE,
3685 /* tag_action */ MSG_SIMPLE_Q_TAG,
3686 /* data_ptr */ NULL,
3688 /* sense_len */ SSD_FULL_SIZE,
3689 sizeof(struct scsi_pause),
3690 /* timeout */ 50000);
3692 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3693 bzero (scsi_cmd, sizeof(*scsi_cmd));
3695 scsi_cmd->op_code = PAUSE;
3696 scsi_cmd->resume = go;
3698 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3699 /*sense_flags*/SF_RETRY_UA);
3701 xpt_release_ccb(ccb);
3707 cdstartunit(struct cam_periph *periph, int load)
3714 ccb = cdgetccb(periph, /* priority */ 1);
3716 scsi_start_stop(&ccb->csio,
3718 /* cbfcnp */ cddone,
3719 /* tag_action */ MSG_SIMPLE_Q_TAG,
3721 /* load_eject */ load,
3722 /* immediate */ FALSE,
3723 /* sense_len */ SSD_FULL_SIZE,
3724 /* timeout */ 50000);
3726 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3727 /*sense_flags*/SF_RETRY_UA);
3729 xpt_release_ccb(ccb);
3735 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3742 ccb = cdgetccb(periph, /* priority */ 1);
3744 scsi_start_stop(&ccb->csio,
3746 /* cbfcnp */ cddone,
3747 /* tag_action */ MSG_SIMPLE_Q_TAG,
3749 /* load_eject */ eject,
3750 /* immediate */ FALSE,
3751 /* sense_len */ SSD_FULL_SIZE,
3752 /* timeout */ 50000);
3754 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3755 /*sense_flags*/SF_RETRY_UA);
3757 xpt_release_ccb(ccb);
3763 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3765 struct scsi_set_speed *scsi_cmd;
3766 struct ccb_scsiio *csio;
3771 ccb = cdgetccb(periph, /* priority */ 1);
3774 /* Preserve old behavior: units in multiples of CDROM speed */
3782 /* cbfcnp */ cddone,
3783 /* flags */ CAM_DIR_NONE,
3784 /* tag_action */ MSG_SIMPLE_Q_TAG,
3785 /* data_ptr */ NULL,
3787 /* sense_len */ SSD_FULL_SIZE,
3788 sizeof(struct scsi_set_speed),
3789 /* timeout */ 50000);
3791 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3792 bzero(scsi_cmd, sizeof(*scsi_cmd));
3794 scsi_cmd->opcode = SET_CD_SPEED;
3795 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3796 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3798 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3799 /*sense_flags*/SF_RETRY_UA);
3801 xpt_release_ccb(ccb);
3807 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3819 ccb = cdgetccb(periph, /* priority */ 1);
3821 switch (authinfo->format) {
3822 case DVD_REPORT_AGID:
3823 length = sizeof(struct scsi_report_key_data_agid);
3825 case DVD_REPORT_CHALLENGE:
3826 length = sizeof(struct scsi_report_key_data_challenge);
3828 case DVD_REPORT_KEY1:
3829 length = sizeof(struct scsi_report_key_data_key1_key2);
3831 case DVD_REPORT_TITLE_KEY:
3832 length = sizeof(struct scsi_report_key_data_title);
3833 /* The lba field is only set for the title key */
3834 lba = authinfo->lba;
3836 case DVD_REPORT_ASF:
3837 length = sizeof(struct scsi_report_key_data_asf);
3839 case DVD_REPORT_RPC:
3840 length = sizeof(struct scsi_report_key_data_rpc);
3842 case DVD_INVALIDATE_AGID:
3848 break; /* NOTREACHED */
3852 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3858 scsi_report_key(&ccb->csio,
3860 /* cbfcnp */ cddone,
3861 /* tag_action */ MSG_SIMPLE_Q_TAG,
3863 /* agid */ authinfo->agid,
3864 /* key_format */ authinfo->format,
3865 /* data_ptr */ databuf,
3866 /* dxfer_len */ length,
3867 /* sense_len */ SSD_FULL_SIZE,
3868 /* timeout */ 50000);
3870 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3871 /*sense_flags*/SF_RETRY_UA);
3876 if (ccb->csio.resid != 0) {
3877 xpt_print(periph->path, "warning, residual for report key "
3878 "command is %d\n", ccb->csio.resid);
3881 switch(authinfo->format) {
3882 case DVD_REPORT_AGID: {
3883 struct scsi_report_key_data_agid *agid_data;
3885 agid_data = (struct scsi_report_key_data_agid *)databuf;
3887 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3891 case DVD_REPORT_CHALLENGE: {
3892 struct scsi_report_key_data_challenge *chal_data;
3894 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3896 bcopy(chal_data->challenge_key, authinfo->keychal,
3897 min(sizeof(chal_data->challenge_key),
3898 sizeof(authinfo->keychal)));
3901 case DVD_REPORT_KEY1: {
3902 struct scsi_report_key_data_key1_key2 *key1_data;
3904 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3906 bcopy(key1_data->key1, authinfo->keychal,
3907 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3910 case DVD_REPORT_TITLE_KEY: {
3911 struct scsi_report_key_data_title *title_data;
3913 title_data = (struct scsi_report_key_data_title *)databuf;
3915 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3916 RKD_TITLE_CPM_SHIFT;
3917 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3918 RKD_TITLE_CP_SEC_SHIFT;
3919 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3920 RKD_TITLE_CMGS_SHIFT;
3921 bcopy(title_data->title_key, authinfo->keychal,
3922 min(sizeof(title_data->title_key),
3923 sizeof(authinfo->keychal)));
3926 case DVD_REPORT_ASF: {
3927 struct scsi_report_key_data_asf *asf_data;
3929 asf_data = (struct scsi_report_key_data_asf *)databuf;
3931 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3934 case DVD_REPORT_RPC: {
3935 struct scsi_report_key_data_rpc *rpc_data;
3937 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3939 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3941 authinfo->vend_rsts =
3942 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3943 RKD_RPC_VENDOR_RESET_SHIFT;
3944 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3945 authinfo->region = rpc_data->region_mask;
3946 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3949 case DVD_INVALIDATE_AGID:
3952 /* This should be impossible, since we checked above */
3955 break; /* NOTREACHED */
3958 if (databuf != NULL)
3959 kfree(databuf, M_DEVBUF);
3961 xpt_release_ccb(ccb);
3967 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3977 ccb = cdgetccb(periph, /* priority */ 1);
3979 switch(authinfo->format) {
3980 case DVD_SEND_CHALLENGE: {
3981 struct scsi_report_key_data_challenge *challenge_data;
3983 length = sizeof(*challenge_data);
3985 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3987 databuf = (u_int8_t *)challenge_data;
3989 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3990 challenge_data->data_len);
3992 bcopy(authinfo->keychal, challenge_data->challenge_key,
3993 min(sizeof(authinfo->keychal),
3994 sizeof(challenge_data->challenge_key)));
3997 case DVD_SEND_KEY2: {
3998 struct scsi_report_key_data_key1_key2 *key2_data;
4000 length = sizeof(*key2_data);
4002 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4004 databuf = (u_int8_t *)key2_data;
4006 scsi_ulto2b(length - sizeof(key2_data->data_len),
4007 key2_data->data_len);
4009 bcopy(authinfo->keychal, key2_data->key1,
4010 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4014 case DVD_SEND_RPC: {
4015 struct scsi_send_key_data_rpc *rpc_data;
4017 length = sizeof(*rpc_data);
4019 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4021 databuf = (u_int8_t *)rpc_data;
4023 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4024 rpc_data->data_len);
4026 rpc_data->region_code = authinfo->region;
4032 break; /* NOTREACHED */
4035 scsi_send_key(&ccb->csio,
4037 /* cbfcnp */ cddone,
4038 /* tag_action */ MSG_SIMPLE_Q_TAG,
4039 /* agid */ authinfo->agid,
4040 /* key_format */ authinfo->format,
4041 /* data_ptr */ databuf,
4042 /* dxfer_len */ length,
4043 /* sense_len */ SSD_FULL_SIZE,
4044 /* timeout */ 50000);
4046 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4047 /*sense_flags*/SF_RETRY_UA);
4051 if (databuf != NULL)
4052 kfree(databuf, M_DEVBUF);
4054 xpt_release_ccb(ccb);
4060 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4070 /* The address is reserved for many of the formats */
4073 ccb = cdgetccb(periph, /* priority */ 1);
4075 switch(dvdstruct->format) {
4076 case DVD_STRUCT_PHYSICAL:
4077 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4079 case DVD_STRUCT_COPYRIGHT:
4080 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4082 case DVD_STRUCT_DISCKEY:
4083 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4085 case DVD_STRUCT_BCA:
4086 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4088 case DVD_STRUCT_MANUFACT:
4089 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4091 case DVD_STRUCT_CMI:
4095 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4096 address = dvdstruct->address;
4098 break; /* NOTREACHED */
4099 case DVD_STRUCT_PROTDISCID:
4100 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4102 case DVD_STRUCT_DISCKEYBLOCK:
4103 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4105 case DVD_STRUCT_DDS:
4106 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4108 case DVD_STRUCT_MEDIUM_STAT:
4109 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4111 case DVD_STRUCT_SPARE_AREA:
4112 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4114 case DVD_STRUCT_RMD_LAST:
4118 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4119 address = dvdstruct->address;
4121 break; /* NOTREACHED */
4122 case DVD_STRUCT_RMD_RMA:
4126 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4127 address = dvdstruct->address;
4129 break; /* NOTREACHED */
4130 case DVD_STRUCT_PRERECORDED:
4131 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4133 case DVD_STRUCT_UNIQUEID:
4134 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4136 case DVD_STRUCT_DCB:
4140 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4141 address = dvdstruct->address;
4143 break; /* NOTREACHED */
4144 case DVD_STRUCT_LIST:
4146 * This is the maximum allocation length for the READ DVD
4147 * STRUCTURE command. There's nothing in the MMC3 spec
4148 * that indicates a limit in the amount of data that can
4149 * be returned from this call, other than the limits
4150 * imposed by the 2-byte length variables.
4157 break; /* NOTREACHED */
4161 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4166 scsi_read_dvd_structure(&ccb->csio,
4168 /* cbfcnp */ cddone,
4169 /* tag_action */ MSG_SIMPLE_Q_TAG,
4171 /* layer_number */ dvdstruct->layer_num,
4172 /* key_format */ dvdstruct->format,
4173 /* agid */ dvdstruct->agid,
4174 /* data_ptr */ databuf,
4175 /* dxfer_len */ length,
4176 /* sense_len */ SSD_FULL_SIZE,
4177 /* timeout */ 50000);
4179 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4180 /*sense_flags*/SF_RETRY_UA);
4185 switch(dvdstruct->format) {
4186 case DVD_STRUCT_PHYSICAL: {
4187 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4188 struct dvd_layer *outlayer;
4189 struct scsi_read_dvd_struct_data_physical *phys_data;
4192 (struct scsi_read_dvd_struct_data_physical *)databuf;
4193 inlayer = &phys_data->layer_desc;
4194 outlayer = (struct dvd_layer *)&dvdstruct->data;
4196 dvdstruct->length = sizeof(*inlayer);
4198 outlayer->book_type = (inlayer->book_type_version &
4199 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4200 outlayer->book_version = (inlayer->book_type_version &
4201 RDSD_BOOK_VERSION_MASK);
4202 outlayer->disc_size = (inlayer->disc_size_max_rate &
4203 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4204 outlayer->max_rate = (inlayer->disc_size_max_rate &
4205 RDSD_MAX_RATE_MASK);
4206 outlayer->nlayers = (inlayer->layer_info &
4207 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4208 outlayer->track_path = (inlayer->layer_info &
4209 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4210 outlayer->layer_type = (inlayer->layer_info &
4211 RDSD_LAYER_TYPE_MASK);
4212 outlayer->linear_density = (inlayer->density &
4213 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4214 outlayer->track_density = (inlayer->density &
4215 RDSD_TRACK_DENSITY_MASK);
4216 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4218 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4219 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4220 outlayer->end_sector_l0 =
4221 scsi_3btoul(inlayer->end_sector_layer0);
4224 case DVD_STRUCT_COPYRIGHT: {
4225 struct scsi_read_dvd_struct_data_copyright *copy_data;
4227 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4230 dvdstruct->cpst = copy_data->cps_type;
4231 dvdstruct->rmi = copy_data->region_info;
4232 dvdstruct->length = 0;
4238 * Tell the user what the overall length is, no matter
4239 * what we can actually fit in the data buffer.
4241 dvdstruct->length = length - ccb->csio.resid -
4242 sizeof(struct scsi_read_dvd_struct_data_header);
4245 * But only actually copy out the smaller of what we read
4246 * in or what the structure can take.
4248 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4250 min(sizeof(dvdstruct->data), dvdstruct->length));
4255 if (databuf != NULL)
4256 kfree(databuf, M_DEVBUF);
4258 xpt_release_ccb(ccb);
4264 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4265 void (*cbfcnp)(struct cam_periph *, union ccb *),
4266 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4267 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4268 u_int8_t sense_len, u_int32_t timeout)
4270 struct scsi_report_key *scsi_cmd;
4272 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4273 bzero(scsi_cmd, sizeof(*scsi_cmd));
4274 scsi_cmd->opcode = REPORT_KEY;
4275 scsi_ulto4b(lba, scsi_cmd->lba);
4276 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4277 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4278 (key_format & RK_KF_KEYFORMAT_MASK);
4283 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4285 /*data_ptr*/ data_ptr,
4286 /*dxfer_len*/ dxfer_len,
4293 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4294 void (*cbfcnp)(struct cam_periph *, union ccb *),
4295 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4296 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4299 struct scsi_send_key *scsi_cmd;
4301 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4302 bzero(scsi_cmd, sizeof(*scsi_cmd));
4303 scsi_cmd->opcode = SEND_KEY;
4305 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4306 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4307 (key_format & RK_KF_KEYFORMAT_MASK);
4312 /*flags*/ CAM_DIR_OUT,
4314 /*data_ptr*/ data_ptr,
4315 /*dxfer_len*/ dxfer_len,
4323 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4324 void (*cbfcnp)(struct cam_periph *, union ccb *),
4325 u_int8_t tag_action, u_int32_t address,
4326 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4327 u_int8_t *data_ptr, u_int32_t dxfer_len,
4328 u_int8_t sense_len, u_int32_t timeout)
4330 struct scsi_read_dvd_structure *scsi_cmd;
4332 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4333 bzero(scsi_cmd, sizeof(*scsi_cmd));
4334 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4336 scsi_ulto4b(address, scsi_cmd->address);
4337 scsi_cmd->layer_number = layer_number;
4338 scsi_cmd->format = format;
4339 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4340 /* The AGID is the top two bits of this byte */
4341 scsi_cmd->agid = agid << 6;
4346 /*flags*/ CAM_DIR_IN,
4348 /*data_ptr*/ data_ptr,
4349 /*dxfer_len*/ dxfer_len,