2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
30 * Portions of this driver taken from the original FreeBSD cd driver.
31 * Written by Julian Elischer (julian@tfs.com)
32 * for TRW Financial Systems for use under the MACH(2.5) operating system.
34 * TRW Financial Systems, in accordance with their agreement with Carnegie
35 * Mellon University, makes this software available to CMU to distribute
36 * or use in any manner that they see fit as long as this message is kept with
37 * the software. For this reason TFS also grants any other persons or
38 * organisations permission to use or modify this software.
40 * TFS supplies this software to be publicly redistributed
41 * on the understanding that TFS is not responsible for the correct
42 * functioning of this software in any circumstances.
44 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
46 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
57 #include <sys/dtype.h>
58 #include <sys/malloc.h>
60 #include <sys/cdrio.h>
61 #include <sys/dvdio.h>
62 #include <sys/devicestat.h>
63 #include <sys/sysctl.h>
64 #include <sys/taskqueue.h>
66 #include <sys/camlib.h>
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
76 #include "../cam_queue.h"
77 #include "../cam_sim.h"
79 #include "scsi_message.h"
83 #define LEADOUT 0xaa /* leadout toc entry */
93 CD_Q_BCD_TRACKS = 0x02,
94 CD_Q_NO_CHANGER = 0x04,
96 CD_Q_10_BYTE_ONLY = 0x10
100 CD_FLAG_INVALID = 0x0001,
101 CD_FLAG_NEW_DISC = 0x0002,
102 CD_FLAG_DISC_LOCKED = 0x0004,
103 CD_FLAG_DISC_REMOVABLE = 0x0008,
104 CD_FLAG_TAGGED_QUEUING = 0x0010,
105 CD_FLAG_CHANGER = 0x0040,
106 CD_FLAG_ACTIVE = 0x0080,
107 CD_FLAG_SCHED_ON_COMP = 0x0100,
108 CD_FLAG_RETRY_UA = 0x0200,
109 CD_FLAG_VALID_MEDIA = 0x0400,
110 CD_FLAG_VALID_TOC = 0x0800,
111 CD_FLAG_SCTX_INIT = 0x1000,
112 CD_FLAG_OPEN = 0x2000
116 CD_CCB_POLLED = 0x00,
118 CD_CCB_BUFFER_IO = 0x02,
119 CD_CCB_WAITING = 0x03,
120 CD_CCB_TYPE_MASK = 0x0F,
121 CD_CCB_RETRY_UA = 0x10
125 CHANGER_TIMEOUT_SCHED = 0x01,
126 CHANGER_SHORT_TMOUT_SCHED = 0x02,
127 CHANGER_MANUAL_CALL = 0x04,
128 CHANGER_NEED_TIMEOUT = 0x08
131 #define ccb_state ppriv_field0
132 #define ccb_bio ppriv_ptr1
135 struct ioc_toc_header header;
136 struct cd_toc_entry entries[100];
139 struct cd_toc_single {
140 struct ioc_toc_header header;
141 struct cd_toc_entry entry;
152 volatile cd_flags flags;
153 struct bio_queue_head bio_queue;
154 LIST_HEAD(, ccb_hdr) pending_ccbs;
155 struct cd_params params;
159 struct devstat device_stats;
160 STAILQ_ENTRY(cd_softc) changer_links;
161 struct cdchanger *changer;
163 struct cam_periph *periph;
164 int minimum_command_size;
165 int outstanding_cmds;
166 struct task sysctl_task;
167 struct sysctl_ctx_list sysctl_ctx;
168 struct sysctl_oid *sysctl_tree;
169 STAILQ_HEAD(, cd_mode_params) mode_queue;
170 struct cd_tocdata toc;
173 struct cd_page_sizes {
178 static struct cd_page_sizes cd_page_size_table[] =
180 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
183 struct cd_quirk_entry {
184 struct scsi_inquiry_pattern inq_pat;
189 * The changer quirk entries aren't strictly necessary. Basically, what
190 * they do is tell cdregister() up front that a device is a changer.
191 * Otherwise, it will figure that fact out once it sees a LUN on the device
192 * that is greater than 0. If it is known up front that a device is a changer,
193 * all I/O to the device will go through the changer scheduling routines, as
194 * opposed to the "normal" CD code.
196 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
197 * your device hangs when it gets a 10 byte command. Adding a quirk just
198 * to get rid of the informative diagnostic message is not acceptable. All
199 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
200 * referenced in a comment along with the quirk) , and must be approved by
201 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
202 * be removed until the submitter can explain why they are needed.
203 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
204 * when the CAM_NEW_TRAN_CODE work is done.
206 static struct cd_quirk_entry cd_quirk_table[] =
209 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
210 /*quirks*/ CD_Q_CHANGER
213 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
214 "*"}, /* quirks */ CD_Q_CHANGER
217 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
218 /* quirks */ CD_Q_CHANGER
221 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
222 /* quirks */ CD_Q_BCD_TRACKS
226 static d_open_t cdopen;
227 static d_close_t cdclose;
228 static d_ioctl_t cdioctl;
229 static d_strategy_t cdstrategy;
231 static periph_init_t cdinit;
232 static periph_ctor_t cdregister;
233 static periph_dtor_t cdcleanup;
234 static periph_start_t cdstart;
235 static periph_oninv_t cdoninvalidate;
236 static void cdasync(void *callback_arg, u_int32_t code,
237 struct cam_path *path, void *arg);
238 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
239 static void cdshorttimeout(void *arg);
240 static void cdschedule(struct cam_periph *periph, int priority);
241 static void cdrunchangerqueue(void *arg);
242 static void cdchangerschedule(struct cd_softc *softc);
243 static int cdrunccb(union ccb *ccb,
244 int (*error_routine)(union ccb *ccb,
246 u_int32_t sense_flags),
247 u_int32_t cam_flags, u_int32_t sense_flags);
248 static union ccb *cdgetccb(struct cam_periph *periph,
250 static void cddone(struct cam_periph *periph,
251 union ccb *start_ccb);
252 static int cderror(union ccb *ccb, u_int32_t cam_flags,
253 u_int32_t sense_flags);
254 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
255 static int cdgetpagesize(int page_num);
256 static void cdprevent(struct cam_periph *periph, int action);
257 static int cdcheckmedia(struct cam_periph *periph);
258 static int cdsize(struct cam_periph *periph, u_int32_t *size);
259 static int cd6byteworkaround(union ccb *ccb);
260 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
261 u_int32_t start, u_int8_t *data,
262 u_int32_t len, u_int32_t sense_flags);
263 static int cdgetmode(struct cam_periph *periph,
264 struct cd_mode_params *data, u_int32_t page);
265 static int cdsetmode(struct cam_periph *periph,
266 struct cd_mode_params *data);
267 static int cdplay(struct cam_periph *periph, u_int32_t blk,
269 static int cdreadsubchannel(struct cam_periph *periph,
270 u_int32_t mode, u_int32_t format,
272 struct cd_sub_channel_info *data,
274 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
275 u_int32_t starts, u_int32_t startf,
276 u_int32_t endm, u_int32_t ends,
278 static int cdplaytracks(struct cam_periph *periph,
279 u_int32_t strack, u_int32_t sindex,
280 u_int32_t etrack, u_int32_t eindex);
281 static int cdpause(struct cam_periph *periph, u_int32_t go);
282 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
283 static int cdstartunit(struct cam_periph *periph, int load);
284 static int cdsetspeed(struct cam_periph *periph,
285 u_int32_t rdspeed, u_int32_t wrspeed);
286 static int cdreportkey(struct cam_periph *periph,
287 struct dvd_authinfo *authinfo);
288 static int cdsendkey(struct cam_periph *periph,
289 struct dvd_authinfo *authinfo);
290 static int cdreaddvdstructure(struct cam_periph *periph,
291 struct dvd_struct *dvdstruct);
293 static struct periph_driver cddriver =
296 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
299 PERIPHDRIVER_DECLARE(cd, cddriver);
301 static struct dev_ops cd_ops = {
302 { "cd", 0, D_DISK | D_MPSAFE },
306 .d_write = physwrite,
308 .d_strategy = cdstrategy
311 static struct extend_array *cdperiphs;
313 #ifndef CHANGER_MIN_BUSY_SECONDS
314 #define CHANGER_MIN_BUSY_SECONDS 5
316 #ifndef CHANGER_MAX_BUSY_SECONDS
317 #define CHANGER_MAX_BUSY_SECONDS 15
320 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
321 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
323 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
324 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
325 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
326 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
327 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
328 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
329 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
330 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
334 target_id_t target_id;
337 struct timeval start_time;
338 struct cd_softc *cur_device;
339 struct callout short_handle;
340 struct callout long_handle;
341 volatile cd_changer_flags flags;
342 STAILQ_ENTRY(cdchanger) changer_links;
343 STAILQ_HEAD(chdevlist, cd_softc) chluns;
346 static struct lock changerq_lock;
347 static STAILQ_HEAD(changerlist, cdchanger) changerq;
348 static int num_changers;
350 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
357 lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
358 STAILQ_INIT(&changerq);
361 * Create our extend array for storing the devices we attach to.
363 cdperiphs = cam_extend_new();
364 if (cdperiphs == NULL) {
365 kprintf("cd: Failed to alloc extend array!\n");
370 * Install a global async callback. This callback will
371 * receive async callbacks like "new device found".
373 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
375 if (status != CAM_REQ_CMP) {
376 kprintf("cd: Failed to attach master async callback "
377 "due to status 0x%x!\n", status);
382 cdoninvalidate(struct cam_periph *periph)
384 struct cd_softc *softc;
388 softc = (struct cd_softc *)periph->softc;
391 * De-register any async callbacks.
393 xpt_register_async(0, cdasync, periph, periph->path);
395 softc->flags |= CD_FLAG_INVALID;
398 * Return all queued I/O with ENXIO.
399 * XXX Handle any transactions queued to the card
400 * with XPT_ABORT_CCB.
402 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
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) {
593 softc = (struct cd_softc *)periph->softc;
594 ksnprintf(tmpstr, sizeof(tmpstr),
595 "CAM CD unit %d", periph->unit_number);
596 ksnprintf(tmpstr2, sizeof(tmpstr2),
597 "%d", periph->unit_number);
599 sysctl_ctx_free(&softc->sysctl_ctx);
600 sysctl_ctx_init(&softc->sysctl_ctx);
601 softc->flags |= CD_FLAG_SCTX_INIT;
602 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
603 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
604 tmpstr2, CTLFLAG_RD, 0, tmpstr);
606 if (softc->sysctl_tree == NULL) {
607 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
608 cam_periph_release(periph);
613 * Now register the sysctl handler, so the user can the value on
616 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
617 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
618 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
621 cam_periph_release(periph);
625 * We have a handler function for this so we can check the values when the
626 * user sets them, instead of every time we look at them.
629 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
633 value = *(int *)arg1;
635 error = sysctl_handle_int(oidp, &value, 0, req);
637 if ((error != 0) || (req->newptr == NULL))
641 * The only real values we can have here are 6 or 10. I don't
642 * really forsee having 12 be an option at any time in the future.
643 * So if the user sets something less than or equal to 6, we'll set
644 * it to 6. If he sets something greater than 6, we'll set it to 10.
646 * I suppose we could just return an error here for the wrong values,
647 * but I don't think it's necessary to do so, as long as we can
648 * determine the user's intent without too much trouble.
655 *(int *)arg1 = value;
662 cdregister(struct cam_periph *periph, void *arg)
664 struct cd_softc *softc;
665 struct ccb_pathinq *cpi;
666 struct ccb_getdev *cgd;
670 cgd = (struct ccb_getdev *)arg;
671 if (periph == NULL) {
672 kprintf("cdregister: periph was NULL!!\n");
673 return(CAM_REQ_CMP_ERR);
676 kprintf("cdregister: no getdev CCB, can't register device\n");
677 return(CAM_REQ_CMP_ERR);
680 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
681 sysctl_ctx_init(&softc->sysctl_ctx);
682 LIST_INIT(&softc->pending_ccbs);
683 STAILQ_INIT(&softc->mode_queue);
684 softc->state = CD_STATE_PROBE;
685 bioq_init(&softc->bio_queue);
686 if (SID_IS_REMOVABLE(&cgd->inq_data))
687 softc->flags |= CD_FLAG_DISC_REMOVABLE;
688 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
689 softc->flags |= CD_FLAG_TAGGED_QUEUING;
691 periph->softc = softc;
692 softc->periph = periph;
694 cam_extend_set(cdperiphs, periph->unit_number, periph);
697 * See if this device has any quirks.
699 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
700 (caddr_t)cd_quirk_table,
701 NELEM(cd_quirk_table),
702 sizeof(*cd_quirk_table), scsi_inquiry_match);
705 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
707 softc->quirks = CD_Q_NONE;
709 /* Check if the SIM does not want 6 byte commands */
710 cpi = &xpt_alloc_ccb()->cpi;
711 xpt_setup_ccb(&cpi->ccb_h, periph->path, /*priority*/1);
712 cpi->ccb_h.func_code = XPT_PATH_INQ;
713 xpt_action((union ccb *)cpi);
714 if (cpi->ccb_h.status == CAM_REQ_CMP && (cpi->hba_misc & PIM_NO_6_BYTE))
715 softc->quirks |= CD_Q_10_BYTE_ONLY;
716 xpt_free_ccb(&cpi->ccb_h);
718 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
720 /* The default is 6 byte commands, unless quirked otherwise */
721 if (softc->quirks & CD_Q_10_BYTE_ONLY)
722 softc->minimum_command_size = 10;
724 softc->minimum_command_size = 6;
727 * Load the user's default, if any.
729 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
730 periph->unit_number);
731 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
733 /* 6 and 10 are the only permissible values here. */
734 if (softc->minimum_command_size < 6)
735 softc->minimum_command_size = 6;
736 else if (softc->minimum_command_size > 6)
737 softc->minimum_command_size = 10;
740 * We need to register the statistics structure for this device,
741 * but we don't have the blocksize yet for it. So, we register
742 * the structure and indicate that we don't have the blocksize
743 * yet. Unlike other SCSI peripheral drivers, we explicitly set
744 * the device type here to be CDROM, rather than just ORing in
745 * the device type. This is because this driver can attach to either
746 * CDROM or WORM devices, and we want this peripheral driver to
747 * show up in the devstat list as a CD peripheral driver, not a
748 * WORM peripheral driver. WORM drives will also have the WORM
749 * driver attached to them.
751 cam_periph_unlock(periph);
752 devstat_add_entry(&softc->device_stats, "cd",
753 periph->unit_number, 0,
754 DEVSTAT_BS_UNAVAILABLE,
755 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
756 DEVSTAT_PRIORITY_CD);
757 disk_create(periph->unit_number, &softc->disk, &cd_ops);
758 disk_setdisktype(&softc->disk, "optical");
759 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
760 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
762 cam_periph_lock(periph);
765 * Add an async callback so that we get
766 * notified if this device goes away.
768 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
769 cdasync, periph, periph->path);
772 * If the target lun is greater than 0, we most likely have a CD
773 * changer device. Check the quirk entries as well, though, just
774 * in case someone has a CD tower with one lun per drive or
775 * something like that. Also, if we know up front that a
776 * particular device is a changer, we can mark it as such starting
777 * with lun 0, instead of lun 1. It shouldn't be necessary to have
778 * a quirk entry to define something as a changer, however.
780 if (((cgd->ccb_h.target_lun > 0)
781 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
782 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
783 struct cdchanger *nchanger;
784 struct cam_periph *nperiph;
785 struct cam_path *path;
789 /* Set the changer flag in the current device's softc */
790 softc->flags |= CD_FLAG_CHANGER;
793 * Now, look around for an existing changer device with the
794 * same path and target ID as the current device.
796 lockmgr(&changerq_lock, LK_EXCLUSIVE);
798 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
800 nchanger = STAILQ_NEXT(nchanger, changer_links)){
801 if ((nchanger->path_id == cgd->ccb_h.path_id)
802 && (nchanger->target_id == cgd->ccb_h.target_id)) {
807 lockmgr(&changerq_lock, LK_RELEASE);
810 * If we found a matching entry, just add this device to
811 * the list of devices on this changer.
814 struct chdevlist *chlunhead;
816 chlunhead = &nchanger->chluns;
819 * XXX KDM look at consolidating this code with the
820 * code below in a separate function.
824 * Create a path with lun id 0, and see if we can
825 * find a matching device
827 status = xpt_create_path(&path, /*periph*/ periph,
829 cgd->ccb_h.target_id, 0);
831 if ((status == CAM_REQ_CMP)
832 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
833 struct cd_softc *nsoftc;
835 nsoftc = (struct cd_softc *)nperiph->softc;
837 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
838 nsoftc->flags |= CD_FLAG_CHANGER;
839 nchanger->num_devices++;
840 if (camq_resize(&nchanger->devq,
841 nchanger->num_devices)!=CAM_REQ_CMP){
842 kprintf("cdregister: "
848 nsoftc->changer = nchanger;
849 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
851 STAILQ_INSERT_TAIL(&nchanger->chluns,
852 nsoftc,changer_links);
855 } else if (status == CAM_REQ_CMP)
858 kprintf("cdregister: unable to allocate path\n"
859 "cdregister: changer support may be "
863 nchanger->num_devices++;
865 softc->changer = nchanger;
866 softc->pinfo.index = CAM_UNQUEUED_INDEX;
868 if (camq_resize(&nchanger->devq,
869 nchanger->num_devices) != CAM_REQ_CMP) {
870 kprintf("cdregister: camq_resize "
871 "failed, changer support may "
875 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
878 * In this case, we don't already have an entry for this
879 * particular changer, so we need to create one, add it to
880 * the queue, and queue this device on the list for this
881 * changer. Before we queue this device, however, we need
882 * to search for lun id 0 on this target, and add it to the
883 * queue first, if it exists. (and if it hasn't already
884 * been marked as part of the changer.)
887 nchanger = kmalloc(sizeof(struct cdchanger),
888 M_DEVBUF, M_INTWAIT | M_ZERO);
889 callout_init(&nchanger->short_handle);
890 callout_init(&nchanger->long_handle);
891 if (camq_init(&nchanger->devq, 1) != 0) {
892 softc->flags &= ~CD_FLAG_CHANGER;
893 kprintf("cdregister: changer support "
898 nchanger->path_id = cgd->ccb_h.path_id;
899 nchanger->target_id = cgd->ccb_h.target_id;
901 /* this is superfluous, but it makes things clearer */
902 nchanger->num_devices = 0;
904 STAILQ_INIT(&nchanger->chluns);
906 callout_init(&nchanger->long_handle);
907 callout_init(&nchanger->short_handle);
909 lockmgr(&changerq_lock, LK_EXCLUSIVE);
911 STAILQ_INSERT_TAIL(&changerq, nchanger,
913 lockmgr(&changerq_lock, LK_RELEASE);
916 * Create a path with lun id 0, and see if we can
917 * find a matching device
919 status = xpt_create_path(&path, /*periph*/ periph,
921 cgd->ccb_h.target_id, 0);
924 * If we were able to allocate the path, and if we
925 * find a matching device and it isn't already
926 * marked as part of a changer, then we add it to
927 * the current changer.
929 if ((status == CAM_REQ_CMP)
930 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
931 && ((((struct cd_softc *)periph->softc)->flags &
932 CD_FLAG_CHANGER) == 0)) {
933 struct cd_softc *nsoftc;
935 nsoftc = (struct cd_softc *)nperiph->softc;
937 nsoftc->flags |= CD_FLAG_CHANGER;
938 nchanger->num_devices++;
939 if (camq_resize(&nchanger->devq,
940 nchanger->num_devices) != CAM_REQ_CMP) {
941 kprintf("cdregister: camq_resize "
942 "failed, changer support may "
945 nsoftc->changer = nchanger;
946 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
948 STAILQ_INSERT_TAIL(&nchanger->chluns,
949 nsoftc, changer_links);
951 } else if (status == CAM_REQ_CMP)
954 kprintf("cdregister: unable to allocate path\n"
955 "cdregister: changer support may be "
959 softc->changer = nchanger;
960 softc->pinfo.index = CAM_UNQUEUED_INDEX;
961 nchanger->num_devices++;
962 if (camq_resize(&nchanger->devq,
963 nchanger->num_devices) != CAM_REQ_CMP) {
964 kprintf("cdregister: camq_resize "
965 "failed, changer support may "
968 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
976 * Refcount and block open attempts until we are setup
979 cam_periph_hold(periph, 0);
981 if ((softc->flags & CD_FLAG_CHANGER) == 0)
982 xpt_schedule(periph, /*priority*/5);
984 cdschedule(periph, /*priority*/ 5);
990 cdopen(struct dev_open_args *ap)
992 cdev_t dev = ap->a_head.a_dev;
993 struct cam_periph *periph;
994 struct cd_softc *softc;
998 periph = cam_extend_get(cdperiphs, unit);
1003 softc = (struct cd_softc *)periph->softc;
1005 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1008 cam_periph_lock(periph);
1010 if (softc->flags & CD_FLAG_INVALID) {
1011 cam_periph_unlock(periph);
1012 cam_periph_release(periph);
1016 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1017 cam_periph_unlock(periph);
1018 cam_periph_release(periph);
1022 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1023 if (softc->flags & CD_FLAG_OPEN)
1024 cam_periph_release(periph);
1026 softc->flags |= CD_FLAG_OPEN;
1029 * Check for media, and set the appropriate flags. We don't bail
1030 * if we don't have media, but then we don't allow anything but the
1031 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1033 * XXX KDM for now, we do fail the open if we don't have media. We
1034 * can change this once we've figured out how to make the slice
1035 * code work well with media changing underneath it.
1037 error = cdcheckmedia(periph);
1039 cdprevent(periph, PR_PREVENT);
1040 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1041 cam_periph_unhold(periph, 1);
1042 /* stays acquired */
1048 cdclose(struct dev_close_args *ap)
1050 cdev_t dev = ap->a_head.a_dev;
1051 struct cam_periph *periph;
1052 struct cd_softc *softc;
1053 struct disk_info *info;
1057 periph = cam_extend_get(cdperiphs, unit);
1061 softc = (struct cd_softc *)periph->softc;
1063 cam_periph_lock(periph);
1064 cam_periph_hold(periph, 0);
1066 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1067 cdprevent(periph, PR_ALLOW);
1070 * Unconditionally set the dsopen() flags back to their default
1073 info = &softc->disk.d_info;
1074 info->d_dsflags &= ~DSO_NOLABELS;
1075 info->d_dsflags |= DSO_COMPATLABEL;
1078 * Since we're closing this CD, mark the blocksize as unavailable.
1079 * It will be marked as available when the CD is opened again.
1081 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1084 * We'll check the media and toc again at the next open().
1086 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1088 cam_periph_unhold(periph, 1);
1089 cam_periph_release(periph);
1095 cdshorttimeout(void *arg)
1097 struct cdchanger *changer;
1099 changer = (struct cdchanger *)arg;
1101 /* Always clear the short timeout flag, since that's what we're in */
1102 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1105 * Check to see if there is any more pending or outstanding I/O for
1106 * this device. If not, move it out of the active slot.
1108 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1109 && (changer->cur_device->outstanding_cmds == 0)) {
1110 changer->flags |= CHANGER_MANUAL_CALL;
1111 cdrunchangerqueue(changer);
1116 * This is a wrapper for xpt_schedule. It only applies to changers.
1119 cdschedule(struct cam_periph *periph, int priority)
1121 struct cd_softc *softc;
1123 softc = (struct cd_softc *)periph->softc;
1126 * If this device isn't currently queued, and if it isn't
1127 * the active device, then we queue this device and run the
1128 * changer queue if there is no timeout scheduled to do it.
1129 * If this device is the active device, just schedule it
1130 * to run again. If this device is queued, there should be
1131 * a timeout in place already that will make sure it runs.
1133 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1134 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1136 * We don't do anything with the priority here.
1137 * This is strictly a fifo queue.
1139 softc->pinfo.priority = 1;
1140 softc->pinfo.generation = ++softc->changer->devq.generation;
1141 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1144 * Since we just put a device in the changer queue,
1145 * check and see if there is a timeout scheduled for
1146 * this changer. If so, let the timeout handle
1147 * switching this device into the active slot. If
1148 * not, manually call the timeout routine to
1151 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1152 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1153 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1154 softc->changer->flags |= CHANGER_MANUAL_CALL;
1155 cdrunchangerqueue(softc->changer);
1157 } else if ((softc->flags & CD_FLAG_ACTIVE)
1158 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1159 xpt_schedule(periph, priority);
1164 cdrunchangerqueue(void *arg)
1166 struct cd_softc *softc;
1167 struct cdchanger *changer;
1168 int called_from_timeout;
1170 changer = (struct cdchanger *)arg;
1173 * If we have NOT been called from cdstrategy() or cddone(), and
1174 * instead from a timeout routine, go ahead and clear the
1177 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1178 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1179 called_from_timeout = 1;
1181 called_from_timeout = 0;
1183 /* Always clear the manual call flag */
1184 changer->flags &= ~CHANGER_MANUAL_CALL;
1186 /* nothing to do if the queue is empty */
1187 if (changer->devq.entries <= 0) {
1192 * If the changer queue is frozen, that means we have an active
1195 if (changer->devq.qfrozen_cnt > 0) {
1198 * We always need to reset the frozen count and clear the
1201 changer->devq.qfrozen_cnt--;
1202 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1203 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1205 if (changer->cur_device->outstanding_cmds > 0) {
1206 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1207 changer->cur_device->bufs_left =
1208 changer->cur_device->outstanding_cmds;
1209 if (called_from_timeout) {
1210 callout_reset(&changer->long_handle,
1211 changer_max_busy_seconds * hz,
1212 cdrunchangerqueue, changer);
1213 changer->flags |= CHANGER_TIMEOUT_SCHED;
1219 * Check to see whether the current device has any I/O left
1220 * to do. If so, requeue it at the end of the queue. If
1221 * not, there is no need to requeue it.
1223 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1225 changer->cur_device->pinfo.generation =
1226 ++changer->devq.generation;
1227 camq_insert(&changer->devq,
1228 (cam_pinfo *)changer->cur_device);
1232 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1234 changer->cur_device = softc;
1236 changer->devq.qfrozen_cnt++;
1237 softc->flags |= CD_FLAG_ACTIVE;
1239 /* Just in case this device is waiting */
1240 wakeup(&softc->changer);
1241 xpt_schedule(softc->periph, /*priority*/ 1);
1244 * Get rid of any pending timeouts, and set a flag to schedule new
1245 * ones so this device gets its full time quantum.
1247 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1248 callout_stop(&changer->long_handle);
1249 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1252 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1253 callout_stop(&changer->short_handle);
1254 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1258 * We need to schedule timeouts, but we only do this after the
1259 * first transaction has completed. This eliminates the changer
1262 changer->flags |= CHANGER_NEED_TIMEOUT;
1266 cdchangerschedule(struct cd_softc *softc)
1268 struct cdchanger *changer;
1270 changer = softc->changer;
1273 * If this is a changer, and this is the current device,
1274 * and this device has at least the minimum time quantum to
1275 * run, see if we can switch it out.
1277 if ((softc->flags & CD_FLAG_ACTIVE)
1278 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1279 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1281 * We try three things here. The first is that we
1282 * check to see whether the schedule on completion
1283 * flag is set. If it is, we decrement the number
1284 * of buffers left, and if it's zero, we reschedule.
1285 * Next, we check to see whether the pending buffer
1286 * queue is empty and whether there are no
1287 * outstanding transactions. If so, we reschedule.
1288 * Next, we see if the pending buffer queue is empty.
1289 * If it is, we set the number of buffers left to
1290 * the current active buffer count and set the
1291 * schedule on complete flag.
1293 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1294 if (--softc->bufs_left == 0) {
1295 softc->changer->flags |=
1296 CHANGER_MANUAL_CALL;
1297 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1298 cdrunchangerqueue(softc->changer);
1300 } else if ((bioq_first(&softc->bio_queue) == NULL)
1301 && (softc->outstanding_cmds == 0)) {
1302 softc->changer->flags |= CHANGER_MANUAL_CALL;
1303 cdrunchangerqueue(softc->changer);
1305 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1306 && (softc->flags & CD_FLAG_ACTIVE)) {
1309 * Now that the first transaction to this
1310 * particular device has completed, we can go ahead
1311 * and schedule our timeouts.
1313 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1314 callout_reset(&changer->long_handle,
1315 changer_max_busy_seconds * hz,
1316 cdrunchangerqueue, changer);
1317 changer->flags |= CHANGER_TIMEOUT_SCHED;
1319 kprintf("cdchangerschedule: already have a long"
1322 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1323 callout_reset(&changer->short_handle,
1324 changer_min_busy_seconds * hz,
1325 cdshorttimeout, changer);
1326 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1328 kprintf("cdchangerschedule: already have a short "
1332 * We just scheduled timeouts, no need to schedule
1335 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1341 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1342 u_int32_t cam_flags,
1343 u_int32_t sense_flags),
1344 u_int32_t cam_flags, u_int32_t sense_flags)
1346 struct cd_softc *softc;
1347 struct cam_periph *periph;
1350 periph = xpt_path_periph(ccb->ccb_h.path);
1351 softc = (struct cd_softc *)periph->softc;
1353 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1354 &softc->device_stats);
1356 if (softc->flags & CD_FLAG_CHANGER)
1357 cdchangerschedule(softc);
1363 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1365 struct cd_softc *softc;
1366 static union ccb *ccb;
1368 softc = (struct cd_softc *)periph->softc;
1370 if (softc->flags & CD_FLAG_CHANGER) {
1372 * This should work the first time this device is woken up,
1373 * but just in case it doesn't, we use a while loop.
1375 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1377 * If this changer isn't already queued, queue it up.
1379 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1380 softc->pinfo.priority = 1;
1381 softc->pinfo.generation =
1382 ++softc->changer->devq.generation;
1383 camq_insert(&softc->changer->devq,
1384 (cam_pinfo *)softc);
1386 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1387 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1388 && ((softc->changer->flags
1389 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1390 softc->changer->flags |= CHANGER_MANUAL_CALL;
1391 cdrunchangerqueue(softc->changer);
1393 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1397 ccb = cam_periph_getccb(periph, priority);
1398 ccb->ccb_h.ccb_state = CD_CCB_POLLED;
1404 * Actually translate the requested transfer into one the physical driver
1405 * can understand. The transfer is described by a buf and will include
1406 * only one physical transfer.
1409 cdstrategy(struct dev_strategy_args *ap)
1411 cdev_t dev = ap->a_head.a_dev;
1412 struct bio *bio = ap->a_bio;
1413 struct buf *bp = bio->bio_buf;
1414 struct cam_periph *periph;
1415 struct cd_softc *softc;
1419 periph = cam_extend_get(cdperiphs, unit);
1420 if (periph == NULL) {
1421 bp->b_error = ENXIO;
1425 cam_periph_lock(periph);
1426 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1428 softc = (struct cd_softc *)periph->softc;
1431 * If the device has been made invalid, error out
1433 if ((softc->flags & CD_FLAG_INVALID)) {
1434 cam_periph_unlock(periph);
1435 bp->b_error = ENXIO;
1440 * If we don't have valid media, look for it before trying to
1443 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1446 error = cdcheckmedia(periph);
1448 cam_periph_unlock(periph);
1449 bp->b_error = error;
1455 * Place it in the queue of disk activities for this disk
1457 bioqdisksort(&softc->bio_queue, bio);
1460 * Schedule ourselves for performing the work. We do things
1461 * differently for changers.
1463 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1464 xpt_schedule(periph, /* XXX priority */1);
1466 cdschedule(periph, /* priority */ 1);
1468 cam_periph_unlock(periph);
1471 bp->b_flags |= B_ERROR;
1473 * Correctly set the buf to indicate a completed xfer
1475 bp->b_resid = bp->b_bcount;
1481 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1483 struct cd_softc *softc;
1486 struct ccb_scsiio *csio;
1487 struct scsi_read_capacity_data *rcap;
1489 softc = (struct cd_softc *)periph->softc;
1491 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1493 switch (softc->state) {
1494 case CD_STATE_NORMAL:
1496 bio = bioq_first(&softc->bio_queue);
1497 if (periph->immediate_priority <= periph->pinfo.priority) {
1498 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1500 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1502 periph->immediate_priority = CAM_PRIORITY_NONE;
1503 wakeup(&periph->ccb_list);
1504 } else if (bio == NULL) {
1505 xpt_release_ccb(start_ccb);
1508 bioq_remove(&softc->bio_queue, bio);
1510 devstat_start_transaction(&softc->device_stats);
1512 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1514 scsi_read_write(&start_ccb->csio,
1516 /* cbfcnp */ cddone,
1518 /* read */(bp->b_cmd == BUF_CMD_READ),
1520 /* minimum_cmd_size */ 10,
1522 bio->bio_offset / softc->params.blksize,
1523 bp->b_bcount / softc->params.blksize,
1524 /* data_ptr */ bp->b_data,
1525 /* dxfer_len */ bp->b_bcount,
1526 /* sense_len */ SSD_FULL_SIZE,
1527 /* timeout */ 30000);
1528 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1531 LIST_INSERT_HEAD(&softc->pending_ccbs,
1532 &start_ccb->ccb_h, periph_links.le);
1534 softc->outstanding_cmds++;
1535 /* We expect a unit attention from this device */
1536 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1537 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1538 softc->flags &= ~CD_FLAG_RETRY_UA;
1541 start_ccb->ccb_h.ccb_bio = bio;
1542 bio = bioq_first(&softc->bio_queue);
1544 xpt_action(start_ccb);
1547 /* Have more work to do, so ensure we stay scheduled */
1548 xpt_schedule(periph, /* XXX priority */1);
1552 case CD_STATE_PROBE:
1555 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1556 csio = &start_ccb->csio;
1557 scsi_read_capacity(csio,
1564 start_ccb->ccb_h.ccb_bio = NULL;
1565 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1566 xpt_action(start_ccb);
1573 cddone(struct cam_periph *periph, union ccb *done_ccb)
1575 struct cd_softc *softc;
1576 struct ccb_scsiio *csio;
1578 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1580 softc = (struct cd_softc *)periph->softc;
1581 csio = &done_ccb->csio;
1583 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1584 case CD_CCB_BUFFER_IO:
1590 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1594 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1597 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1602 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1603 if (error == ERESTART) {
1605 * A retry was scheuled, so
1616 xpt_print(periph->path,
1617 "cddone: got error %#x back\n", error);
1618 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
1619 q_bp = q_bio->bio_buf;
1620 q_bp->b_resid = q_bp->b_bcount;
1621 q_bp->b_error = EIO;
1622 q_bp->b_flags |= B_ERROR;
1625 bp->b_resid = bp->b_bcount;
1626 bp->b_error = error;
1627 bp->b_flags |= B_ERROR;
1628 cam_release_devq(done_ccb->ccb_h.path,
1632 /*getcount_only*/0);
1635 bp->b_resid = csio->resid;
1637 if (bp->b_resid != 0) {
1638 /* Short transfer ??? */
1639 bp->b_flags |= B_ERROR;
1643 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1644 softc->outstanding_cmds--;
1646 if (softc->flags & CD_FLAG_CHANGER)
1647 cdchangerschedule(softc);
1649 devstat_end_transaction_buf(&softc->device_stats, bp);
1656 * Currently (9/30/97) the longest possible announce
1657 * buffer is 108 bytes, for the first error case below.
1658 * That is 39 bytes for the basic string, 16 bytes for the
1659 * biggest sense key (hardware error), 52 bytes for the
1660 * text of the largest sense qualifier valid for a CDROM,
1661 * (0x72, 0x03 or 0x04, 0x03), and one byte for the
1662 * null terminating character. To allow for longer strings,
1663 * the announce buffer is 120 bytes.
1665 * We need to call disk_setdiskinfo() before the disk
1666 * subsystem will allow any opens. Because additional
1667 * probe code is in cdopen() we do this now whether a CD
1668 * is present or not.
1670 struct scsi_read_capacity_data *rdcap;
1671 struct disk_info info;
1672 char announce_buf[120];
1673 struct cd_params *cdp;
1676 cdp = &softc->params;
1677 bzero(&info, sizeof(info));
1678 info.d_type = DTYPE_SCSI;
1679 info.d_dsflags &= ~DSO_COMPATLABEL;
1680 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
1681 info.d_serialno = xpt_path_serialno(periph->path);
1683 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1685 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1686 cdp->blksize = scsi_4btoul (rdcap->length);
1688 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1689 ksnprintf(announce_buf, sizeof(announce_buf),
1690 "cd present [%lu x %lu byte records]",
1691 cdp->disksize, (u_long)cdp->blksize);
1692 info.d_media_blksize = cdp->blksize;
1693 info.d_media_blocks = cdp->disksize;
1699 * Retry any UNIT ATTENTION type errors. They
1700 * are expected at boot.
1702 error = cderror(done_ccb, CAM_RETRY_SELTO,
1703 SF_RETRY_UA | SF_NO_PRINT);
1704 if (error == ERESTART) {
1706 * A retry was scheuled, so
1710 } else if (error != 0) {
1711 struct scsi_sense_data *sense;
1713 int sense_key, error_code;
1716 struct ccb_getdev *cgd;
1718 /* Don't wedge this device's queue */
1719 cam_release_devq(done_ccb->ccb_h.path,
1723 /*getcount_only*/0);
1725 status = done_ccb->ccb_h.status;
1727 cgd = &xpt_alloc_ccb()->cgd;
1728 xpt_setup_ccb(&cgd->ccb_h,
1729 done_ccb->ccb_h.path,
1731 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1732 xpt_action((union ccb *)cgd);
1734 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1735 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1736 || ((status & CAM_AUTOSNS_VALID) == 0))
1742 sense = &csio->sense_data;
1743 scsi_extract_sense(sense, &error_code,
1748 * Attach to anything that claims to be a
1749 * CDROM or WORM device, as long as it
1750 * doesn't return a "Logical unit not
1751 * supported" (0x25) error.
1753 if ((have_sense) && (asc != 0x25)
1754 && (error_code == SSD_CURRENT_ERROR)) {
1755 const char *sense_key_desc;
1756 const char *asc_desc;
1758 scsi_sense_desc(sense_key, asc, ascq,
1762 ksnprintf(announce_buf,
1763 sizeof(announce_buf),
1764 "Attempt to query device "
1765 "size failed: %s, %s",
1768 info.d_media_blksize = 2048;
1770 } else if (SID_TYPE(&cgd->inq_data) == T_CDROM) {
1772 * We only print out an error for
1773 * CDROM type devices. For WORM
1774 * devices, we don't print out an
1775 * error since a few WORM devices
1776 * don't support CDROM commands.
1777 * If we have sense information, go
1778 * ahead and print it out.
1779 * Otherwise, just say that we
1784 * Just print out the error, not
1785 * the full probe message, when we
1792 xpt_print(periph->path,
1793 "got CAM status %#x\n",
1794 done_ccb->ccb_h.status);
1796 xpt_print(periph->path, "fatal error, "
1797 "failed to attach to device\n");
1799 * Invalidate this peripheral.
1801 cam_periph_invalidate(periph);
1803 announce_buf[0] = '\0';
1807 * Invalidate this peripheral.
1809 cam_periph_invalidate(periph);
1810 announce_buf[0] = '\0';
1812 xpt_free_ccb(&cgd->ccb_h);
1815 kfree(rdcap, M_SCSICD);
1816 if (announce_buf[0] != '\0') {
1817 xpt_announce_periph(periph, announce_buf);
1818 if (softc->flags & CD_FLAG_CHANGER)
1819 cdchangerschedule(softc);
1821 * Create our sysctl variables, now that we know
1822 * we have successfully attached.
1824 taskqueue_enqueue(taskqueue_thread[mycpuid],
1825 &softc->sysctl_task);
1827 softc->state = CD_STATE_NORMAL;
1829 * Since our peripheral may be invalidated by an error
1830 * above or an external event, we must release our CCB
1831 * before releasing the probe lock on the peripheral.
1832 * The peripheral will only go away once the last lock
1833 * is removed, and we need it around for the CCB release
1836 xpt_release_ccb(done_ccb);
1837 cam_periph_unhold(periph, 0);
1839 disk_setdiskinfo(&softc->disk, &info);
1843 case CD_CCB_WAITING:
1845 /* Caller will release the CCB */
1846 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1847 ("trying to wakeup ccbwait\n"));
1849 wakeup(&done_ccb->ccb_h.cbfcnp);
1853 /* Caller releases CCB */
1854 wakeup(&done_ccb->ccb_h.cbfcnp);
1859 xpt_release_ccb(done_ccb);
1862 static union cd_pages *
1863 cdgetpage(struct cd_mode_params *mode_params)
1865 union cd_pages *page;
1867 if (mode_params->cdb_size == 10)
1868 page = (union cd_pages *)find_mode_page_10(
1869 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1871 page = (union cd_pages *)find_mode_page_6(
1872 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1878 cdgetpagesize(int page_num)
1882 for (i = 0; i < NELEM(cd_page_size_table); i++) {
1883 if (cd_page_size_table[i].page == page_num)
1884 return (cd_page_size_table[i].page_size);
1890 cdioctl(struct dev_ioctl_args *ap)
1892 cdev_t dev = ap->a_head.a_dev;
1893 caddr_t addr = ap->a_data;
1894 struct cam_periph *periph;
1895 struct cd_softc *softc;
1896 int unit, error = 0;
1900 periph = cam_extend_get(cdperiphs, unit);
1904 cam_periph_lock(periph);
1905 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1907 softc = (struct cd_softc *)periph->softc;
1909 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1910 ("trying to do ioctl %#lx\n", ap->a_cmd));
1912 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1913 cam_periph_unlock(periph);
1914 cam_periph_release(periph);
1919 * If we don't have media loaded, check for it. If still don't
1920 * have media loaded, we can only do a load or eject.
1922 * We only care whether media is loaded if this is a cd-specific ioctl
1923 * (thus the IOCGROUP check below). Note that this will break if
1924 * anyone adds any ioctls into the switch statement below that don't
1925 * have their ioctl group set to 'c'.
1927 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1928 && ((ap->a_cmd != CDIOCCLOSE)
1929 && (ap->a_cmd != CDIOCEJECT))
1930 && (IOCGROUP(ap->a_cmd) == 'c')) {
1931 error = cdcheckmedia(periph);
1933 cam_periph_unhold(periph, 1);
1938 * Drop the lock here so later mallocs can use WAITOK. The periph
1939 * is essentially locked still with the cam_periph_hold call above.
1941 cam_periph_unlock(periph);
1943 switch (ap->a_cmd) {
1945 case CDIOCPLAYTRACKS:
1947 struct ioc_play_track *args
1948 = (struct ioc_play_track *)addr;
1949 struct cd_mode_params params;
1950 union cd_pages *page;
1952 params.alloc_len = sizeof(union cd_mode_data_6_10);
1953 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1956 cam_periph_lock(periph);
1957 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1958 ("trying to do CDIOCPLAYTRACKS\n"));
1960 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1962 kfree(params.mode_buf, M_SCSICD);
1963 cam_periph_unlock(periph);
1966 page = cdgetpage(¶ms);
1968 page->audio.flags &= ~CD_PA_SOTC;
1969 page->audio.flags |= CD_PA_IMMED;
1970 error = cdsetmode(periph, ¶ms);
1971 kfree(params.mode_buf, M_SCSICD);
1973 cam_periph_unlock(periph);
1978 * This was originally implemented with the PLAY
1979 * AUDIO TRACK INDEX command, but that command was
1980 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1981 * drives support it but ATAPI and ATAPI-derivative
1982 * drives don't seem to support it. So we keep a
1983 * cache of the table of contents and translate
1984 * track numbers to MSF format.
1986 if (softc->flags & CD_FLAG_VALID_TOC) {
1987 union msf_lba *sentry, *eentry;
1990 if (args->end_track <
1991 softc->toc.header.ending_track + 1)
1993 if (args->end_track >
1994 softc->toc.header.ending_track + 1)
1996 softc->toc.header.ending_track + 1;
1997 st = args->start_track -
1998 softc->toc.header.starting_track;
1999 et = args->end_track -
2000 softc->toc.header.starting_track;
2003 || (st > (softc->toc.header.ending_track -
2004 softc->toc.header.starting_track))) {
2008 sentry = &softc->toc.entries[st].addr;
2009 eentry = &softc->toc.entries[et].addr;
2010 error = cdplaymsf(periph,
2019 * If we don't have a valid TOC, try the
2020 * play track index command. It is part of
2021 * the SCSI-2 spec, but was removed in the
2022 * MMC specs. ATAPI and ATAPI-derived
2023 * drives don't support it.
2025 if (softc->quirks & CD_Q_BCD_TRACKS) {
2027 bin2bcd(args->start_track);
2029 bin2bcd(args->end_track);
2031 error = cdplaytracks(periph,
2037 cam_periph_unlock(periph);
2042 struct ioc_play_msf *args
2043 = (struct ioc_play_msf *) addr;
2044 struct cd_mode_params params;
2045 union cd_pages *page;
2047 params.alloc_len = sizeof(union cd_mode_data_6_10);
2048 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2051 cam_periph_lock(periph);
2052 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2053 ("trying to do CDIOCPLAYMSF\n"));
2055 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2057 kfree(params.mode_buf, M_SCSICD);
2058 cam_periph_unlock(periph);
2061 page = cdgetpage(¶ms);
2063 page->audio.flags &= ~CD_PA_SOTC;
2064 page->audio.flags |= CD_PA_IMMED;
2065 error = cdsetmode(periph, ¶ms);
2066 kfree(params.mode_buf, M_SCSICD);
2068 cam_periph_unlock(periph);
2071 error = cdplaymsf(periph,
2078 cam_periph_unlock(periph);
2081 case CDIOCPLAYBLOCKS:
2083 struct ioc_play_blocks *args
2084 = (struct ioc_play_blocks *) addr;
2085 struct cd_mode_params params;
2086 union cd_pages *page;
2088 params.alloc_len = sizeof(union cd_mode_data_6_10);
2089 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2091 cam_periph_lock(periph);
2092 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2093 ("trying to do CDIOCPLAYBLOCKS\n"));
2096 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2098 kfree(params.mode_buf, M_SCSICD);
2099 cam_periph_unlock(periph);
2102 page = cdgetpage(¶ms);
2104 page->audio.flags &= ~CD_PA_SOTC;
2105 page->audio.flags |= CD_PA_IMMED;
2106 error = cdsetmode(periph, ¶ms);
2107 kfree(params.mode_buf, M_SCSICD);
2109 cam_periph_unlock(periph);
2112 error = cdplay(periph, args->blk, args->len);
2113 cam_periph_unlock(periph);
2116 case CDIOCREADSUBCHANNEL:
2118 struct ioc_read_subchannel *args
2119 = (struct ioc_read_subchannel *) addr;
2120 struct cd_sub_channel_info *data;
2121 u_int32_t len = args->data_len;
2123 data = kmalloc(sizeof(struct cd_sub_channel_info),
2124 M_SCSICD, M_WAITOK);
2126 cam_periph_lock(periph);
2127 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2128 ("trying to do CDIOCREADSUBCHANNEL\n"));
2130 if ((len > sizeof(struct cd_sub_channel_info)) ||
2131 (len < sizeof(struct cd_sub_channel_header))) {
2133 "scsi_cd: cdioctl: "
2134 "cdioreadsubchannel: error, len=%d\n",
2137 kfree(data, M_SCSICD);
2138 cam_periph_unlock(periph);
2142 if (softc->quirks & CD_Q_BCD_TRACKS)
2143 args->track = bin2bcd(args->track);
2145 error = cdreadsubchannel(periph, args->address_format,
2146 args->data_format, args->track, data, len);
2149 kfree(data, M_SCSICD);
2150 cam_periph_unlock(periph);
2153 if (softc->quirks & CD_Q_BCD_TRACKS)
2154 data->what.track_info.track_number =
2155 bcd2bin(data->what.track_info.track_number);
2156 len = min(len, ((data->header.data_len[0] << 8) +
2157 data->header.data_len[1] +
2158 sizeof(struct cd_sub_channel_header)));
2159 cam_periph_unlock(periph);
2160 if (copyout(data, args->data, len) != 0) {
2163 kfree(data, M_SCSICD);
2167 case CDIOREADTOCHEADER:
2169 struct ioc_toc_header *th;
2171 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2174 cam_periph_lock(periph);
2175 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2176 ("trying to do CDIOREADTOCHEADER\n"));
2178 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2179 sizeof (*th), /*sense_flags*/0);
2181 kfree(th, M_SCSICD);
2182 cam_periph_unlock(periph);
2185 if (softc->quirks & CD_Q_BCD_TRACKS) {
2186 /* we are going to have to convert the BCD
2187 * encoding on the cd to what is expected
2189 th->starting_track =
2190 bcd2bin(th->starting_track);
2191 th->ending_track = bcd2bin(th->ending_track);
2193 th->len = ntohs(th->len);
2194 bcopy(th, addr, sizeof(*th));
2195 kfree(th, M_SCSICD);
2196 cam_periph_unlock(periph);
2199 case CDIOREADTOCENTRYS:
2201 struct cd_tocdata *data;
2202 struct cd_toc_single *lead;
2203 struct ioc_read_toc_entry *te =
2204 (struct ioc_read_toc_entry *) addr;
2205 struct ioc_toc_header *th;
2206 u_int32_t len, readlen, idx, num;
2207 u_int32_t starting_track = te->starting_track;
2209 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2210 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2212 cam_periph_lock(periph);
2213 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2214 ("trying to do CDIOREADTOCENTRYS\n"));
2216 if (te->data_len < sizeof(struct cd_toc_entry)
2217 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2218 || (te->address_format != CD_MSF_FORMAT
2219 && te->address_format != CD_LBA_FORMAT)) {
2221 kprintf("scsi_cd: error in readtocentries, "
2222 "returning EINVAL\n");
2223 kfree(data, M_SCSICD);
2224 kfree(lead, M_SCSICD);
2225 cam_periph_unlock(periph);
2230 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2231 sizeof (*th), /*sense_flags*/0);
2233 kfree(data, M_SCSICD);
2234 kfree(lead, M_SCSICD);
2235 cam_periph_unlock(periph);
2239 if (softc->quirks & CD_Q_BCD_TRACKS) {
2240 /* we are going to have to convert the BCD
2241 * encoding on the cd to what is expected
2243 th->starting_track =
2244 bcd2bin(th->starting_track);
2245 th->ending_track = bcd2bin(th->ending_track);
2248 if (starting_track == 0)
2249 starting_track = th->starting_track;
2250 else if (starting_track == LEADOUT)
2251 starting_track = th->ending_track + 1;
2252 else if (starting_track < th->starting_track ||
2253 starting_track > th->ending_track + 1) {
2254 kprintf("scsi_cd: error in readtocentries, "
2255 "returning EINVAL\n");
2256 kfree(data, M_SCSICD);
2257 kfree(lead, M_SCSICD);
2258 cam_periph_unlock(periph);
2263 /* calculate reading length without leadout entry */
2264 readlen = (th->ending_track - starting_track + 1) *
2265 sizeof(struct cd_toc_entry);
2267 /* and with leadout entry */
2268 len = readlen + sizeof(struct cd_toc_entry);
2269 if (te->data_len < len) {
2274 if (len > sizeof(data->entries)) {
2275 kprintf("scsi_cd: error in readtocentries, "
2276 "returning EINVAL\n");
2278 kfree(data, M_SCSICD);
2279 kfree(lead, M_SCSICD);
2280 cam_periph_unlock(periph);
2283 num = len / sizeof(struct cd_toc_entry);
2286 error = cdreadtoc(periph, te->address_format,
2289 readlen + sizeof (*th),
2292 kfree(data, M_SCSICD);
2293 kfree(lead, M_SCSICD);
2294 cam_periph_unlock(periph);
2299 /* make leadout entry if needed */
2300 idx = starting_track + num - 1;
2301 if (softc->quirks & CD_Q_BCD_TRACKS)
2302 th->ending_track = bcd2bin(th->ending_track);
2303 if (idx == th->ending_track + 1) {
2304 error = cdreadtoc(periph, te->address_format,
2305 LEADOUT, (u_int8_t *)lead,
2309 kfree(data, M_SCSICD);
2310 kfree(lead, M_SCSICD);
2311 cam_periph_unlock(periph);
2314 data->entries[idx - starting_track] =
2317 if (softc->quirks & CD_Q_BCD_TRACKS) {
2318 for (idx = 0; idx < num - 1; idx++) {
2319 data->entries[idx].track =
2320 bcd2bin(data->entries[idx].track);
2324 cam_periph_unlock(periph);
2325 error = copyout(data->entries, te->data, len);
2326 kfree(data, M_SCSICD);
2327 kfree(lead, M_SCSICD);
2330 case CDIOREADTOCENTRY:
2332 struct cd_toc_single *data;
2333 struct ioc_read_toc_single_entry *te =
2334 (struct ioc_read_toc_single_entry *) addr;
2335 struct ioc_toc_header *th;
2338 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2340 cam_periph_lock(periph);
2341 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2342 ("trying to do CDIOREADTOCENTRY\n"));
2344 if (te->address_format != CD_MSF_FORMAT
2345 && te->address_format != CD_LBA_FORMAT) {
2346 kprintf("error in readtocentry, "
2347 " returning EINVAL\n");
2348 kfree(data, M_SCSICD);
2350 cam_periph_unlock(periph);
2355 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2356 sizeof (*th), /*sense_flags*/0);
2358 kfree(data, M_SCSICD);
2359 cam_periph_unlock(periph);
2363 if (softc->quirks & CD_Q_BCD_TRACKS) {
2364 /* we are going to have to convert the BCD
2365 * encoding on the cd to what is expected
2367 th->starting_track =
2368 bcd2bin(th->starting_track);
2369 th->ending_track = bcd2bin(th->ending_track);
2373 track = th->starting_track;
2374 else if (track == LEADOUT)
2376 else if (track < th->starting_track ||
2377 track > th->ending_track + 1) {
2378 kprintf("error in readtocentry, "
2379 " returning EINVAL\n");
2380 kfree(data, M_SCSICD);
2382 cam_periph_unlock(periph);
2386 error = cdreadtoc(periph, te->address_format, track,
2387 (u_int8_t *)data, sizeof(*data),
2390 kfree(data, M_SCSICD);
2391 cam_periph_unlock(periph);
2395 if (softc->quirks & CD_Q_BCD_TRACKS)
2396 data->entry.track = bcd2bin(data->entry.track);
2397 bcopy(&data->entry, &te->entry,
2398 sizeof(struct cd_toc_entry));
2399 kfree(data, M_SCSICD);
2400 cam_periph_unlock(periph);
2405 struct ioc_patch *arg = (struct ioc_patch *)addr;
2406 struct cd_mode_params params;
2407 union cd_pages *page;
2409 params.alloc_len = sizeof(union cd_mode_data_6_10);
2410 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2413 cam_periph_lock(periph);
2414 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2415 ("trying to do CDIOCSETPATCH\n"));
2417 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2419 kfree(params.mode_buf, M_SCSICD);
2420 cam_periph_unlock(periph);
2423 page = cdgetpage(¶ms);
2425 page->audio.port[LEFT_PORT].channels =
2427 page->audio.port[RIGHT_PORT].channels =
2429 page->audio.port[2].channels = arg->patch[2];
2430 page->audio.port[3].channels = arg->patch[3];
2431 error = cdsetmode(periph, ¶ms);
2432 kfree(params.mode_buf, M_SCSICD);
2433 cam_periph_unlock(periph);
2438 struct ioc_vol *arg = (struct ioc_vol *) addr;
2439 struct cd_mode_params params;
2440 union cd_pages *page;
2442 params.alloc_len = sizeof(union cd_mode_data_6_10);
2443 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2446 cam_periph_lock(periph);
2447 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2448 ("trying to do CDIOCGETVOL\n"));
2450 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2452 kfree(params.mode_buf, M_SCSICD);
2453 cam_periph_unlock(periph);
2456 page = cdgetpage(¶ms);
2458 arg->vol[LEFT_PORT] =
2459 page->audio.port[LEFT_PORT].volume;
2460 arg->vol[RIGHT_PORT] =
2461 page->audio.port[RIGHT_PORT].volume;
2462 arg->vol[2] = page->audio.port[2].volume;
2463 arg->vol[3] = page->audio.port[3].volume;
2464 kfree(params.mode_buf, M_SCSICD);
2465 cam_periph_unlock(periph);
2470 struct ioc_vol *arg = (struct ioc_vol *) addr;
2471 struct cd_mode_params params;
2472 union cd_pages *page;
2474 params.alloc_len = sizeof(union cd_mode_data_6_10);
2475 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2478 cam_periph_lock(periph);
2479 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2480 ("trying to do CDIOCSETVOL\n"));
2482 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2484 kfree(params.mode_buf, M_SCSICD);
2485 cam_periph_unlock(periph);
2488 page = cdgetpage(¶ms);
2490 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2491 page->audio.port[LEFT_PORT].volume =
2492 arg->vol[LEFT_PORT];
2493 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2494 page->audio.port[RIGHT_PORT].volume =
2495 arg->vol[RIGHT_PORT];
2496 page->audio.port[2].volume = arg->vol[2];
2497 page->audio.port[3].volume = arg->vol[3];
2498 error = cdsetmode(periph, ¶ms);
2499 cam_periph_unlock(periph);
2500 kfree(params.mode_buf, M_SCSICD);
2505 struct cd_mode_params params;
2506 union cd_pages *page;
2508 params.alloc_len = sizeof(union cd_mode_data_6_10);
2509 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2512 cam_periph_lock(periph);
2513 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2514 ("trying to do CDIOCSETMONO\n"));
2516 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2518 kfree(params.mode_buf, M_SCSICD);
2519 cam_periph_unlock(periph);
2522 page = cdgetpage(¶ms);
2524 page->audio.port[LEFT_PORT].channels =
2525 LEFT_CHANNEL | RIGHT_CHANNEL;
2526 page->audio.port[RIGHT_PORT].channels =
2527 LEFT_CHANNEL | RIGHT_CHANNEL;
2528 page->audio.port[2].channels = 0;
2529 page->audio.port[3].channels = 0;
2530 error = cdsetmode(periph, ¶ms);
2531 cam_periph_unlock(periph);
2532 kfree(params.mode_buf, M_SCSICD);
2535 case CDIOCSETSTEREO:
2537 struct cd_mode_params params;
2538 union cd_pages *page;
2540 params.alloc_len = sizeof(union cd_mode_data_6_10);
2541 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2544 cam_periph_lock(periph);
2545 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2546 ("trying to do CDIOCSETSTEREO\n"));
2548 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2550 kfree(params.mode_buf, M_SCSICD);
2551 cam_periph_unlock(periph);
2554 page = cdgetpage(¶ms);
2556 page->audio.port[LEFT_PORT].channels =
2558 page->audio.port[RIGHT_PORT].channels =
2560 page->audio.port[2].channels = 0;
2561 page->audio.port[3].channels = 0;
2562 error = cdsetmode(periph, ¶ms);
2563 kfree(params.mode_buf, M_SCSICD);
2564 cam_periph_unlock(periph);
2569 struct cd_mode_params params;
2570 union cd_pages *page;
2572 params.alloc_len = sizeof(union cd_mode_data_6_10);
2573 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2576 cam_periph_lock(periph);
2577 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2578 ("trying to do CDIOCSETMUTE\n"));
2580 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2582 kfree(¶ms, M_SCSICD);
2583 cam_periph_unlock(periph);
2586 page = cdgetpage(¶ms);
2588 page->audio.port[LEFT_PORT].channels = 0;
2589 page->audio.port[RIGHT_PORT].channels = 0;
2590 page->audio.port[2].channels = 0;
2591 page->audio.port[3].channels = 0;
2592 error = cdsetmode(periph, ¶ms);
2593 kfree(params.mode_buf, M_SCSICD);
2594 cam_periph_unlock(periph);
2599 struct cd_mode_params params;
2600 union cd_pages *page;
2602 params.alloc_len = sizeof(union cd_mode_data_6_10);
2603 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2606 cam_periph_lock(periph);
2607 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2608 ("trying to do CDIOCSETLEFT\n"));
2610 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2612 kfree(params.mode_buf, M_SCSICD);
2613 cam_periph_unlock(periph);
2616 page = cdgetpage(¶ms);
2618 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2619 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2620 page->audio.port[2].channels = 0;
2621 page->audio.port[3].channels = 0;
2622 error = cdsetmode(periph, ¶ms);
2623 kfree(params.mode_buf, M_SCSICD);
2624 cam_periph_unlock(periph);
2629 struct cd_mode_params params;
2630 union cd_pages *page;
2632 params.alloc_len = sizeof(union cd_mode_data_6_10);
2633 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2636 cam_periph_lock(periph);
2637 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2638 ("trying to do CDIOCSETRIGHT\n"));
2640 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2642 kfree(params.mode_buf, M_SCSICD);
2643 cam_periph_unlock(periph);
2646 page = cdgetpage(¶ms);
2648 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2649 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2650 page->audio.port[2].channels = 0;
2651 page->audio.port[3].channels = 0;
2652 error = cdsetmode(periph, ¶ms);
2653 kfree(params.mode_buf, M_SCSICD);
2654 cam_periph_unlock(periph);
2658 cam_periph_lock(periph);
2659 error = cdpause(periph, 1);
2660 cam_periph_unlock(periph);
2663 cam_periph_lock(periph);
2664 error = cdpause(periph, 0);
2665 cam_periph_unlock(periph);
2668 cam_periph_lock(periph);
2669 error = cdstartunit(periph, 0);
2670 cam_periph_unlock(periph);
2673 cam_periph_lock(periph);
2674 error = cdstartunit(periph, 1);
2675 cam_periph_unlock(periph);
2678 cam_periph_lock(periph);
2679 error = cdstopunit(periph, 0);
2680 cam_periph_unlock(periph);
2683 cam_periph_lock(periph);
2684 error = cdstopunit(periph, 1);
2685 cam_periph_unlock(periph);
2688 cam_periph_lock(periph);
2689 cdprevent(periph, PR_ALLOW);
2690 cam_periph_unlock(periph);
2693 cam_periph_lock(periph);
2694 cdprevent(periph, PR_PREVENT);
2695 cam_periph_unlock(periph);
2698 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2702 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2706 /* return (cd_reset(periph)); */
2709 case CDRIOCREADSPEED:
2710 cam_periph_lock(periph);
2711 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2712 cam_periph_unlock(periph);
2714 case CDRIOCWRITESPEED:
2715 cam_periph_lock(periph);
2716 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2717 cam_periph_unlock(periph);
2720 case DVDIOCREPORTKEY: {
2721 struct dvd_authinfo *authinfo;
2723 authinfo = (struct dvd_authinfo *)addr;
2725 cam_periph_lock(periph);
2726 if (ap->a_cmd == DVDIOCREPORTKEY)
2727 error = cdreportkey(periph, authinfo);
2729 error = cdsendkey(periph, authinfo);
2730 cam_periph_unlock(periph);
2733 case DVDIOCREADSTRUCTURE: {
2734 struct dvd_struct *dvdstruct;
2736 dvdstruct = (struct dvd_struct *)addr;
2738 cam_periph_lock(periph);
2739 error = cdreaddvdstructure(periph, dvdstruct);
2740 cam_periph_unlock(periph);
2745 cam_periph_lock(periph);
2746 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2747 cam_periph_unlock(periph);
2751 cam_periph_lock(periph);
2752 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2753 cam_periph_unhold(periph, 1);
2759 cdprevent(struct cam_periph *periph, int action)
2762 struct cd_softc *softc;
2765 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2767 softc = (struct cd_softc *)periph->softc;
2769 if (((action == PR_ALLOW)
2770 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2771 || ((action == PR_PREVENT)
2772 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2776 ccb = cdgetccb(periph, /* priority */ 1);
2778 scsi_prevent(&ccb->csio,
2784 /* timeout */60000);
2786 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2787 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2789 xpt_release_ccb(ccb);
2792 if (action == PR_ALLOW)
2793 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2795 softc->flags |= CD_FLAG_DISC_LOCKED;
2800 cdcheckmedia(struct cam_periph *periph)
2802 struct cd_softc *softc;
2803 struct ioc_toc_header *toch;
2804 struct cd_toc_single leadout;
2805 struct ccb_getdev *cgd;
2806 u_int32_t size, toclen;
2807 int error, num_entries, cdindex;
2808 int first_track_audio;
2809 struct disk_info info;
2811 softc = (struct cd_softc *)periph->softc;
2813 first_track_audio = -1;
2817 * Set up disk info fields and tell the disk subsystem to reset
2818 * its internal copy of the label.
2820 bzero(&info, sizeof(info));
2821 info.d_type = DTYPE_SCSI;
2822 info.d_dsflags &= ~DSO_COMPATLABEL;
2823 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2824 info.d_serialno = xpt_path_serialno(periph->path);
2827 * Grab the inquiry data to get the vendor and product names.
2828 * Put them in the typename and packname for the label.
2830 cgd = &xpt_alloc_ccb()->cgd;
2831 xpt_setup_ccb(&cgd->ccb_h, periph->path, /*priority*/ 1);
2832 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2833 xpt_action((union ccb *)cgd);
2836 strncpy(label->d_typename, cgd->inq_data.vendor,
2837 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2838 strncpy(label->d_packname, cgd->inq_data.product,
2839 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2841 xpt_free_ccb(&cgd->ccb_h);
2844 * Clear the valid media and TOC flags until we've verified that we
2847 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2850 * Get the disc size and block size. If we can't get it, we don't
2851 * have media, most likely.
2853 if ((error = cdsize(periph, &size)) != 0) {
2855 * Set a bogus sector size, so the slice code won't try to
2856 * divide by 0 and panic the kernel.
2858 info.d_media_blksize = 2048;
2859 disk_setdiskinfo(&softc->disk, &info);
2862 * Tell devstat(9) we don't have a blocksize.
2864 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2868 info.d_media_blksize = softc->params.blksize;
2869 info.d_media_blocks = softc->params.disksize;
2870 disk_setdiskinfo(&softc->disk, &info);
2873 * We unconditionally (re)set the blocksize each time the
2874 * CD device is opened. This is because the CD can change,
2875 * and therefore the blocksize might change.
2876 * XXX problems here if some slice or partition is still
2877 * open with the old size?
2879 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2880 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2881 softc->device_stats.block_size = softc->params.blksize;
2883 softc->flags |= CD_FLAG_VALID_MEDIA;
2887 * Now we check the table of contents. This (currently) is only
2888 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2889 * things like present a separate entry in /dev for each track,
2890 * like that acd(4) driver does.
2892 bzero(&softc->toc, sizeof(softc->toc));
2893 toch = &softc->toc.header;
2896 * We will get errors here for media that doesn't have a table of
2897 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2898 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2899 * has not been recorded (no complete session) and the Format codes
2900 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2901 * with an INVALID FIELD IN CDB. Devices that are not capable of
2902 * reading an incomplete session on DDC/CD-R/RW media shall report
2903 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2905 * So this isn't fatal if we can't read the table of contents, it
2906 * just means that the user won't be able to issue the play tracks
2907 * ioctl, and likely lots of other stuff won't work either. They
2908 * need to burn the CD before we can do a whole lot with it. So
2909 * we don't print anything here if we get an error back.
2911 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2914 * Errors in reading the table of contents aren't fatal, we just
2915 * won't have a valid table of contents cached.
2919 bzero(&softc->toc, sizeof(softc->toc));
2923 if (softc->quirks & CD_Q_BCD_TRACKS) {
2924 toch->starting_track = bcd2bin(toch->starting_track);
2925 toch->ending_track = bcd2bin(toch->ending_track);
2928 /* Number of TOC entries, plus leadout */
2929 num_entries = (toch->ending_track - toch->starting_track) + 2;
2931 if (num_entries <= 0)
2934 toclen = num_entries * sizeof(struct cd_toc_entry);
2936 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2937 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2941 bzero(&softc->toc, sizeof(softc->toc));
2945 if (softc->quirks & CD_Q_BCD_TRACKS) {
2946 toch->starting_track = bcd2bin(toch->starting_track);
2947 toch->ending_track = bcd2bin(toch->ending_track);
2949 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2952 * XXX KDM is this necessary? Probably only if the drive doesn't
2953 * return leadout information with the table of contents.
2955 cdindex = toch->starting_track + num_entries -1;
2956 if (cdindex == toch->ending_track + 1) {
2958 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2959 (u_int8_t *)&leadout, sizeof(leadout),
2965 softc->toc.entries[cdindex - toch->starting_track] =
2968 if (softc->quirks & CD_Q_BCD_TRACKS) {
2969 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2970 softc->toc.entries[cdindex].track =
2971 bcd2bin(softc->toc.entries[cdindex].track);
2976 * Run through the TOC entries, find the first entry and determine
2977 * whether it is an audio or data track.
2979 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2980 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2981 if (softc->toc.entries[cdindex].control & 0x04)
2982 first_track_audio = 0;
2984 first_track_audio = 1;
2990 * If first_track_audio is non-zero, we either have an error (e.g.
2991 * couldn't find the starting track) or the first track is an audio
2992 * track. If first_track_audio is 0, the first track is a data
2993 * track that could have a disklabel. Attempt to read the
2994 * disklabel off the media, just in case the user put one there.
2996 if (first_track_audio == 0) {
2997 info.d_dsflags |= DSO_COMPATLABEL;
2998 info.d_dsflags &= ~DSO_NOLABELS;
2999 disk_setdiskinfo(&softc->disk, &info);
3001 softc->flags |= CD_FLAG_VALID_TOC;
3008 cdsize(struct cam_periph *periph, u_int32_t *size)
3010 struct cd_softc *softc;
3012 struct scsi_read_capacity_data *rcap_buf;
3015 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3017 softc = (struct cd_softc *)periph->softc;
3019 ccb = cdgetccb(periph, /* priority */ 1);
3021 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
3022 M_SCSICD, M_INTWAIT | M_ZERO);
3024 scsi_read_capacity(&ccb->csio,
3030 /* timeout */20000);
3032 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3033 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3035 xpt_release_ccb(ccb);
3037 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3038 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3040 * SCSI-3 mandates that the reported blocksize shall be 2048.
3041 * Older drives sometimes report funny values, trim it down to
3042 * 2048, or other parts of the kernel will get confused.
3044 * XXX we leave drives alone that might report 512 bytes, as
3045 * well as drives reporting more weird sizes like perhaps 4K.
3047 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3048 softc->params.blksize = 2048;
3050 kfree(rcap_buf, M_SCSICD);
3051 *size = softc->params.disksize;
3058 cd6byteworkaround(union ccb *ccb)
3061 struct cam_periph *periph;
3062 struct cd_softc *softc;
3063 struct cd_mode_params *params;
3066 periph = xpt_path_periph(ccb->ccb_h.path);
3067 softc = (struct cd_softc *)periph->softc;
3069 cdb = ccb->csio.cdb_io.cdb_bytes;
3071 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3072 || ((cdb[0] != MODE_SENSE_6)
3073 && (cdb[0] != MODE_SELECT_6)))
3077 * Because there is no convenient place to stash the overall
3078 * cd_mode_params structure pointer, we have to grab it like this.
3079 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3080 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3082 * XXX It would be nice if, at some point, we could increase the
3083 * number of available peripheral private pointers. Both pointers
3084 * are currently used in most every peripheral driver.
3088 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3089 if (params->mode_buf == ccb->csio.data_ptr) {
3096 * This shouldn't happen. All mode sense and mode select
3097 * operations in the cd(4) driver MUST go through cdgetmode() and
3101 xpt_print(periph->path,
3102 "mode buffer not found in mode queue!\n");
3106 params->cdb_size = 10;
3107 softc->minimum_command_size = 10;
3108 xpt_print(ccb->ccb_h.path,
3109 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3110 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3112 if (cdb[0] == MODE_SENSE_6) {
3113 struct scsi_mode_sense_10 ms10;
3114 struct scsi_mode_sense_6 *ms6;
3117 ms6 = (struct scsi_mode_sense_6 *)cdb;
3119 bzero(&ms10, sizeof(ms10));
3120 ms10.opcode = MODE_SENSE_10;
3121 ms10.byte2 = ms6->byte2;
3122 ms10.page = ms6->page;
3125 * 10 byte mode header, block descriptor,
3126 * sizeof(union cd_pages)
3128 len = sizeof(struct cd_mode_data_10);
3129 ccb->csio.dxfer_len = len;
3131 scsi_ulto2b(len, ms10.length);
3132 ms10.control = ms6->control;
3133 bcopy(&ms10, cdb, 10);
3134 ccb->csio.cdb_len = 10;
3136 struct scsi_mode_select_10 ms10;
3137 struct scsi_mode_select_6 *ms6;
3138 struct scsi_mode_header_6 *header6;
3139 struct scsi_mode_header_10 *header10;
3140 struct scsi_mode_page_header *page_header;
3141 int blk_desc_len, page_num, page_size, len;
3143 ms6 = (struct scsi_mode_select_6 *)cdb;
3145 bzero(&ms10, sizeof(ms10));
3146 ms10.opcode = MODE_SELECT_10;
3147 ms10.byte2 = ms6->byte2;
3149 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3150 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3152 page_header = find_mode_page_6(header6);
3153 page_num = page_header->page_code;
3155 blk_desc_len = header6->blk_desc_len;
3157 page_size = cdgetpagesize(page_num);
3159 if (page_size != (page_header->page_length +
3160 sizeof(*page_header)))
3161 page_size = page_header->page_length +
3162 sizeof(*page_header);
3164 len = sizeof(*header10) + blk_desc_len + page_size;
3166 len = min(params->alloc_len, len);
3169 * Since the 6 byte parameter header is shorter than the 10
3170 * byte parameter header, we need to copy the actual mode
3171 * page data, and the block descriptor, if any, so things wind
3172 * up in the right place. The regions will overlap, but
3173 * bcopy() does the right thing.
3175 bcopy(params->mode_buf + sizeof(*header6),
3176 params->mode_buf + sizeof(*header10),
3177 len - sizeof(*header10));
3179 /* Make sure these fields are set correctly. */
3180 scsi_ulto2b(0, header10->data_length);
3181 header10->medium_type = 0;
3182 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3184 ccb->csio.dxfer_len = len;
3186 scsi_ulto2b(len, ms10.length);
3187 ms10.control = ms6->control;
3188 bcopy(&ms10, cdb, 10);
3189 ccb->csio.cdb_len = 10;
3192 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3193 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3196 cam_release_devq(ccb->ccb_h.path,
3200 /*getcount_only*/0);
3207 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3209 struct cd_softc *softc;
3210 struct cam_periph *periph;
3213 periph = xpt_path_periph(ccb->ccb_h.path);
3214 softc = (struct cd_softc *)periph->softc;
3219 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3220 * CDB comes back with this particular error, try transforming it
3221 * into the 10 byte version.
3223 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3224 error = cd6byteworkaround(ccb);
3225 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3226 CAM_SCSI_STATUS_ERROR)
3227 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3228 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3229 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3230 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3231 int sense_key, error_code, asc, ascq;
3233 scsi_extract_sense(&ccb->csio.sense_data,
3234 &error_code, &sense_key, &asc, &ascq);
3235 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3236 error = cd6byteworkaround(ccb);
3239 if (error == ERESTART)
3244 * Until we have a better way of doing pack validation,
3245 * don't treat UAs as errors.
3247 sense_flags |= SF_RETRY_UA;
3248 return (cam_periph_error(ccb, cam_flags, sense_flags,
3249 &softc->saved_ccb));
3253 * Read table of contents
3256 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3257 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3259 struct scsi_read_toc *scsi_cmd;
3261 struct ccb_scsiio *csio;
3268 ccb = cdgetccb(periph, /* priority */ 1);
3274 /* cbfcnp */ cddone,
3275 /* flags */ CAM_DIR_IN,
3276 /* tag_action */ MSG_SIMPLE_Q_TAG,
3277 /* data_ptr */ data,
3278 /* dxfer_len */ len,
3279 /* sense_len */ SSD_FULL_SIZE,
3280 sizeof(struct scsi_read_toc),
3281 /* timeout */ 50000);
3283 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3284 bzero (scsi_cmd, sizeof(*scsi_cmd));
3286 if (mode == CD_MSF_FORMAT)
3287 scsi_cmd->byte2 |= CD_MSF;
3288 scsi_cmd->from_track = start;
3289 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3290 scsi_cmd->data_len[0] = (ntoc) >> 8;
3291 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3293 scsi_cmd->op_code = READ_TOC;
3295 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3296 /*sense_flags*/SF_RETRY_UA | sense_flags);
3298 xpt_release_ccb(ccb);
3304 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3305 u_int32_t format, int track,
3306 struct cd_sub_channel_info *data, u_int32_t len)
3308 struct scsi_read_subchannel *scsi_cmd;
3309 struct ccb_scsiio *csio;
3315 ccb = cdgetccb(periph, /* priority */ 1);
3321 /* cbfcnp */ cddone,
3322 /* flags */ CAM_DIR_IN,
3323 /* tag_action */ MSG_SIMPLE_Q_TAG,
3324 /* data_ptr */ (u_int8_t *)data,
3325 /* dxfer_len */ len,
3326 /* sense_len */ SSD_FULL_SIZE,
3327 sizeof(struct scsi_read_subchannel),
3328 /* timeout */ 50000);
3330 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3331 bzero (scsi_cmd, sizeof(*scsi_cmd));
3333 scsi_cmd->op_code = READ_SUBCHANNEL;
3334 if (mode == CD_MSF_FORMAT)
3335 scsi_cmd->byte1 |= CD_MSF;
3336 scsi_cmd->byte2 = SRS_SUBQ;
3337 scsi_cmd->subchan_format = format;
3338 scsi_cmd->track = track;
3339 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3340 scsi_cmd->control = 0;
3342 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3343 /*sense_flags*/SF_RETRY_UA);
3345 xpt_release_ccb(ccb);
3351 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3352 * routine. See comments in cd6byteworkaround() for details.
3355 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3358 struct ccb_scsiio *csio;
3359 struct cd_softc *softc;
3364 softc = (struct cd_softc *)periph->softc;
3366 ccb = cdgetccb(periph, /* priority */ 1);
3370 data->cdb_size = softc->minimum_command_size;
3371 if (data->cdb_size < 10)
3372 param_len = sizeof(struct cd_mode_data);
3374 param_len = sizeof(struct cd_mode_data_10);
3376 /* Don't say we've got more room than we actually allocated */
3377 param_len = min(param_len, data->alloc_len);
3379 scsi_mode_sense_len(csio,
3381 /* cbfcnp */ cddone,
3382 /* tag_action */ MSG_SIMPLE_Q_TAG,
3384 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3386 /* param_buf */ data->mode_buf,
3387 /* param_len */ param_len,
3388 /* minimum_cmd_size */ softc->minimum_command_size,
3389 /* sense_len */ SSD_FULL_SIZE,
3390 /* timeout */ 50000);
3393 * It would be nice not to have to do this, but there's no
3394 * available pointer in the CCB that would allow us to stuff the
3395 * mode params structure in there and retrieve it in
3396 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3397 * lets the caller know what CDB size we ended up using, so they
3398 * can find the actual mode page offset.
3400 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3402 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3403 /*sense_flags*/SF_RETRY_UA);
3405 xpt_release_ccb(ccb);
3407 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3410 * This is a bit of belt-and-suspenders checking, but if we run
3411 * into a situation where the target sends back multiple block
3412 * descriptors, we might not have enough space in the buffer to
3413 * see the whole mode page. Better to return an error than
3414 * potentially access memory beyond our malloced region.
3419 if (data->cdb_size == 10) {
3420 struct scsi_mode_header_10 *hdr10;
3422 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3423 data_len = scsi_2btoul(hdr10->data_length);
3424 data_len += sizeof(hdr10->data_length);
3426 struct scsi_mode_header_6 *hdr6;
3428 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3429 data_len = hdr6->data_length;
3430 data_len += sizeof(hdr6->data_length);
3434 * Complain if there is more mode data available than we
3435 * allocated space for. This could potentially happen if
3436 * we miscalculated the page length for some reason, if the
3437 * drive returns multiple block descriptors, or if it sets
3438 * the data length incorrectly.
3440 if (data_len > data->alloc_len) {
3441 xpt_print(periph->path, "allocated modepage %d length "
3442 "%d < returned length %d\n", page, data->alloc_len,
3451 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3452 * routine. See comments in cd6byteworkaround() for details.
3455 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3457 struct ccb_scsiio *csio;
3458 struct cd_softc *softc;
3460 int cdb_size, param_len;
3463 softc = (struct cd_softc *)periph->softc;
3465 ccb = cdgetccb(periph, /* priority */ 1);
3472 * If the data is formatted for the 10 byte version of the mode
3473 * select parameter list, we need to use the 10 byte CDB.
3474 * Otherwise, we use whatever the stored minimum command size.
3476 if (data->cdb_size == 10)
3477 cdb_size = data->cdb_size;
3479 cdb_size = softc->minimum_command_size;
3481 if (cdb_size >= 10) {
3482 struct scsi_mode_header_10 *mode_header;
3485 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3487 data_len = scsi_2btoul(mode_header->data_length);
3489 scsi_ulto2b(0, mode_header->data_length);
3491 * SONY drives do not allow a mode select with a medium_type
3492 * value that has just been returned by a mode sense; use a
3493 * medium_type of 0 (Default) instead.
3495 mode_header->medium_type = 0;
3498 * Pass back whatever the drive passed to us, plus the size
3499 * of the data length field.
3501 param_len = data_len + sizeof(mode_header->data_length);
3504 struct scsi_mode_header_6 *mode_header;
3506 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3508 param_len = mode_header->data_length + 1;
3510 mode_header->data_length = 0;
3512 * SONY drives do not allow a mode select with a medium_type
3513 * value that has just been returned by a mode sense; use a
3514 * medium_type of 0 (Default) instead.
3516 mode_header->medium_type = 0;
3519 /* Don't say we've got more room than we actually allocated */
3520 param_len = min(param_len, data->alloc_len);
3522 scsi_mode_select_len(csio,
3524 /* cbfcnp */ cddone,
3525 /* tag_action */ MSG_SIMPLE_Q_TAG,
3526 /* scsi_page_fmt */ 1,
3528 /* param_buf */ data->mode_buf,
3529 /* param_len */ param_len,
3530 /* minimum_cmd_size */ cdb_size,
3531 /* sense_len */ SSD_FULL_SIZE,
3532 /* timeout */ 50000);
3534 /* See comments in cdgetmode() and cd6byteworkaround(). */
3535 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3537 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3538 /*sense_flags*/SF_RETRY_UA);
3540 xpt_release_ccb(ccb);
3542 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3550 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3552 struct ccb_scsiio *csio;
3558 ccb = cdgetccb(periph, /* priority */ 1);
3561 * Use the smallest possible command to perform the operation.
3563 if ((len & 0xffff0000) == 0) {
3565 * We can fit in a 10 byte cdb.
3567 struct scsi_play_10 *scsi_cmd;
3569 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3570 bzero (scsi_cmd, sizeof(*scsi_cmd));
3571 scsi_cmd->op_code = PLAY_10;
3572 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3573 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3574 cdb_len = sizeof(*scsi_cmd);
3576 struct scsi_play_12 *scsi_cmd;
3578 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3579 bzero (scsi_cmd, sizeof(*scsi_cmd));
3580 scsi_cmd->op_code = PLAY_12;
3581 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3582 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3583 cdb_len = sizeof(*scsi_cmd);
3588 /*flags*/CAM_DIR_NONE,
3592 /*sense_len*/SSD_FULL_SIZE,
3594 /*timeout*/50 * 1000);
3596 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3597 /*sense_flags*/SF_RETRY_UA);
3599 xpt_release_ccb(ccb);
3605 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3606 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3608 struct scsi_play_msf *scsi_cmd;
3609 struct ccb_scsiio *csio;
3615 ccb = cdgetccb(periph, /* priority */ 1);
3621 /* cbfcnp */ cddone,
3622 /* flags */ CAM_DIR_NONE,
3623 /* tag_action */ MSG_SIMPLE_Q_TAG,
3624 /* data_ptr */ NULL,
3626 /* sense_len */ SSD_FULL_SIZE,
3627 sizeof(struct scsi_play_msf),
3628 /* timeout */ 50000);
3630 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3631 bzero (scsi_cmd, sizeof(*scsi_cmd));
3633 scsi_cmd->op_code = PLAY_MSF;
3634 scsi_cmd->start_m = startm;
3635 scsi_cmd->start_s = starts;
3636 scsi_cmd->start_f = startf;
3637 scsi_cmd->end_m = endm;
3638 scsi_cmd->end_s = ends;
3639 scsi_cmd->end_f = endf;
3641 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3642 /*sense_flags*/SF_RETRY_UA);
3644 xpt_release_ccb(ccb);
3651 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3652 u_int32_t etrack, u_int32_t eindex)
3654 struct scsi_play_track *scsi_cmd;
3655 struct ccb_scsiio *csio;
3661 ccb = cdgetccb(periph, /* priority */ 1);
3667 /* cbfcnp */ cddone,
3668 /* flags */ CAM_DIR_NONE,
3669 /* tag_action */ MSG_SIMPLE_Q_TAG,
3670 /* data_ptr */ NULL,
3672 /* sense_len */ SSD_FULL_SIZE,
3673 sizeof(struct scsi_play_track),
3674 /* timeout */ 50000);
3676 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3677 bzero (scsi_cmd, sizeof(*scsi_cmd));
3679 scsi_cmd->op_code = PLAY_TRACK;
3680 scsi_cmd->start_track = strack;
3681 scsi_cmd->start_index = sindex;
3682 scsi_cmd->end_track = etrack;
3683 scsi_cmd->end_index = eindex;
3685 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3686 /*sense_flags*/SF_RETRY_UA);
3688 xpt_release_ccb(ccb);
3694 cdpause(struct cam_periph *periph, u_int32_t go)
3696 struct scsi_pause *scsi_cmd;
3697 struct ccb_scsiio *csio;
3703 ccb = cdgetccb(periph, /* priority */ 1);
3709 /* cbfcnp */ cddone,
3710 /* flags */ CAM_DIR_NONE,
3711 /* tag_action */ MSG_SIMPLE_Q_TAG,
3712 /* data_ptr */ NULL,
3714 /* sense_len */ SSD_FULL_SIZE,
3715 sizeof(struct scsi_pause),
3716 /* timeout */ 50000);
3718 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3719 bzero (scsi_cmd, sizeof(*scsi_cmd));
3721 scsi_cmd->op_code = PAUSE;
3722 scsi_cmd->resume = go;
3724 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3725 /*sense_flags*/SF_RETRY_UA);
3727 xpt_release_ccb(ccb);
3733 cdstartunit(struct cam_periph *periph, int load)
3740 ccb = cdgetccb(periph, /* priority */ 1);
3742 scsi_start_stop(&ccb->csio,
3744 /* cbfcnp */ cddone,
3745 /* tag_action */ MSG_SIMPLE_Q_TAG,
3747 /* load_eject */ load,
3748 /* immediate */ FALSE,
3749 /* sense_len */ SSD_FULL_SIZE,
3750 /* timeout */ 50000);
3752 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3753 /*sense_flags*/SF_RETRY_UA);
3755 xpt_release_ccb(ccb);
3761 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3768 ccb = cdgetccb(periph, /* priority */ 1);
3770 scsi_start_stop(&ccb->csio,
3772 /* cbfcnp */ cddone,
3773 /* tag_action */ MSG_SIMPLE_Q_TAG,
3775 /* load_eject */ eject,
3776 /* immediate */ FALSE,
3777 /* sense_len */ SSD_FULL_SIZE,
3778 /* timeout */ 50000);
3780 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3781 /*sense_flags*/SF_RETRY_UA);
3783 xpt_release_ccb(ccb);
3789 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3791 struct scsi_set_speed *scsi_cmd;
3792 struct ccb_scsiio *csio;
3797 ccb = cdgetccb(periph, /* priority */ 1);
3800 /* Preserve old behavior: units in multiples of CDROM speed */
3808 /* cbfcnp */ cddone,
3809 /* flags */ CAM_DIR_NONE,
3810 /* tag_action */ MSG_SIMPLE_Q_TAG,
3811 /* data_ptr */ NULL,
3813 /* sense_len */ SSD_FULL_SIZE,
3814 sizeof(struct scsi_set_speed),
3815 /* timeout */ 50000);
3817 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3818 bzero(scsi_cmd, sizeof(*scsi_cmd));
3820 scsi_cmd->opcode = SET_CD_SPEED;
3821 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3822 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3824 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3825 /*sense_flags*/SF_RETRY_UA);
3827 xpt_release_ccb(ccb);
3833 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3845 ccb = cdgetccb(periph, /* priority */ 1);
3847 switch (authinfo->format) {
3848 case DVD_REPORT_AGID:
3849 length = sizeof(struct scsi_report_key_data_agid);
3851 case DVD_REPORT_CHALLENGE:
3852 length = sizeof(struct scsi_report_key_data_challenge);
3854 case DVD_REPORT_KEY1:
3855 length = sizeof(struct scsi_report_key_data_key1_key2);
3857 case DVD_REPORT_TITLE_KEY:
3858 length = sizeof(struct scsi_report_key_data_title);
3859 /* The lba field is only set for the title key */
3860 lba = authinfo->lba;
3862 case DVD_REPORT_ASF:
3863 length = sizeof(struct scsi_report_key_data_asf);
3865 case DVD_REPORT_RPC:
3866 length = sizeof(struct scsi_report_key_data_rpc);
3868 case DVD_INVALIDATE_AGID:
3874 break; /* NOTREACHED */
3878 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3884 scsi_report_key(&ccb->csio,
3886 /* cbfcnp */ cddone,
3887 /* tag_action */ MSG_SIMPLE_Q_TAG,
3889 /* agid */ authinfo->agid,
3890 /* key_format */ authinfo->format,
3891 /* data_ptr */ databuf,
3892 /* dxfer_len */ length,
3893 /* sense_len */ SSD_FULL_SIZE,
3894 /* timeout */ 50000);
3896 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3897 /*sense_flags*/SF_RETRY_UA);
3902 if (ccb->csio.resid != 0) {
3903 xpt_print(periph->path, "warning, residual for report key "
3904 "command is %d\n", ccb->csio.resid);
3907 switch(authinfo->format) {
3908 case DVD_REPORT_AGID: {
3909 struct scsi_report_key_data_agid *agid_data;
3911 agid_data = (struct scsi_report_key_data_agid *)databuf;
3913 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3917 case DVD_REPORT_CHALLENGE: {
3918 struct scsi_report_key_data_challenge *chal_data;
3920 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3922 bcopy(chal_data->challenge_key, authinfo->keychal,
3923 min(sizeof(chal_data->challenge_key),
3924 sizeof(authinfo->keychal)));
3927 case DVD_REPORT_KEY1: {
3928 struct scsi_report_key_data_key1_key2 *key1_data;
3930 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3932 bcopy(key1_data->key1, authinfo->keychal,
3933 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3936 case DVD_REPORT_TITLE_KEY: {
3937 struct scsi_report_key_data_title *title_data;
3939 title_data = (struct scsi_report_key_data_title *)databuf;
3941 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3942 RKD_TITLE_CPM_SHIFT;
3943 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3944 RKD_TITLE_CP_SEC_SHIFT;
3945 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3946 RKD_TITLE_CMGS_SHIFT;
3947 bcopy(title_data->title_key, authinfo->keychal,
3948 min(sizeof(title_data->title_key),
3949 sizeof(authinfo->keychal)));
3952 case DVD_REPORT_ASF: {
3953 struct scsi_report_key_data_asf *asf_data;
3955 asf_data = (struct scsi_report_key_data_asf *)databuf;
3957 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3960 case DVD_REPORT_RPC: {
3961 struct scsi_report_key_data_rpc *rpc_data;
3963 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3965 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3967 authinfo->vend_rsts =
3968 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3969 RKD_RPC_VENDOR_RESET_SHIFT;
3970 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3971 authinfo->region = rpc_data->region_mask;
3972 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3975 case DVD_INVALIDATE_AGID:
3978 /* This should be impossible, since we checked above */
3981 break; /* NOTREACHED */
3984 if (databuf != NULL)
3985 kfree(databuf, M_DEVBUF);
3987 xpt_release_ccb(ccb);
3993 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4003 ccb = cdgetccb(periph, /* priority */ 1);
4005 switch(authinfo->format) {
4006 case DVD_SEND_CHALLENGE: {
4007 struct scsi_report_key_data_challenge *challenge_data;
4009 length = sizeof(*challenge_data);
4011 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4013 databuf = (u_int8_t *)challenge_data;
4015 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4016 challenge_data->data_len);
4018 bcopy(authinfo->keychal, challenge_data->challenge_key,
4019 min(sizeof(authinfo->keychal),
4020 sizeof(challenge_data->challenge_key)));
4023 case DVD_SEND_KEY2: {
4024 struct scsi_report_key_data_key1_key2 *key2_data;
4026 length = sizeof(*key2_data);
4028 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4030 databuf = (u_int8_t *)key2_data;
4032 scsi_ulto2b(length - sizeof(key2_data->data_len),
4033 key2_data->data_len);
4035 bcopy(authinfo->keychal, key2_data->key1,
4036 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4040 case DVD_SEND_RPC: {
4041 struct scsi_send_key_data_rpc *rpc_data;
4043 length = sizeof(*rpc_data);
4045 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4047 databuf = (u_int8_t *)rpc_data;
4049 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4050 rpc_data->data_len);
4052 rpc_data->region_code = authinfo->region;
4058 break; /* NOTREACHED */
4061 scsi_send_key(&ccb->csio,
4063 /* cbfcnp */ cddone,
4064 /* tag_action */ MSG_SIMPLE_Q_TAG,
4065 /* agid */ authinfo->agid,
4066 /* key_format */ authinfo->format,
4067 /* data_ptr */ databuf,
4068 /* dxfer_len */ length,
4069 /* sense_len */ SSD_FULL_SIZE,
4070 /* timeout */ 50000);
4072 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4073 /*sense_flags*/SF_RETRY_UA);
4077 if (databuf != NULL)
4078 kfree(databuf, M_DEVBUF);
4080 xpt_release_ccb(ccb);
4086 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4096 /* The address is reserved for many of the formats */
4099 ccb = cdgetccb(periph, /* priority */ 1);
4101 switch(dvdstruct->format) {
4102 case DVD_STRUCT_PHYSICAL:
4103 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4105 case DVD_STRUCT_COPYRIGHT:
4106 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4108 case DVD_STRUCT_DISCKEY:
4109 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4111 case DVD_STRUCT_BCA:
4112 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4114 case DVD_STRUCT_MANUFACT:
4115 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4117 case DVD_STRUCT_CMI:
4121 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4122 address = dvdstruct->address;
4124 break; /* NOTREACHED */
4125 case DVD_STRUCT_PROTDISCID:
4126 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4128 case DVD_STRUCT_DISCKEYBLOCK:
4129 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4131 case DVD_STRUCT_DDS:
4132 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4134 case DVD_STRUCT_MEDIUM_STAT:
4135 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4137 case DVD_STRUCT_SPARE_AREA:
4138 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4140 case DVD_STRUCT_RMD_LAST:
4144 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4145 address = dvdstruct->address;
4147 break; /* NOTREACHED */
4148 case DVD_STRUCT_RMD_RMA:
4152 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4153 address = dvdstruct->address;
4155 break; /* NOTREACHED */
4156 case DVD_STRUCT_PRERECORDED:
4157 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4159 case DVD_STRUCT_UNIQUEID:
4160 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4162 case DVD_STRUCT_DCB:
4166 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4167 address = dvdstruct->address;
4169 break; /* NOTREACHED */
4170 case DVD_STRUCT_LIST:
4172 * This is the maximum allocation length for the READ DVD
4173 * STRUCTURE command. There's nothing in the MMC3 spec
4174 * that indicates a limit in the amount of data that can
4175 * be returned from this call, other than the limits
4176 * imposed by the 2-byte length variables.
4183 break; /* NOTREACHED */
4187 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4192 scsi_read_dvd_structure(&ccb->csio,
4194 /* cbfcnp */ cddone,
4195 /* tag_action */ MSG_SIMPLE_Q_TAG,
4197 /* layer_number */ dvdstruct->layer_num,
4198 /* key_format */ dvdstruct->format,
4199 /* agid */ dvdstruct->agid,
4200 /* data_ptr */ databuf,
4201 /* dxfer_len */ length,
4202 /* sense_len */ SSD_FULL_SIZE,
4203 /* timeout */ 50000);
4205 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4206 /*sense_flags*/SF_RETRY_UA);
4211 switch(dvdstruct->format) {
4212 case DVD_STRUCT_PHYSICAL: {
4213 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4214 struct dvd_layer *outlayer;
4215 struct scsi_read_dvd_struct_data_physical *phys_data;
4218 (struct scsi_read_dvd_struct_data_physical *)databuf;
4219 inlayer = &phys_data->layer_desc;
4220 outlayer = (struct dvd_layer *)&dvdstruct->data;
4222 dvdstruct->length = sizeof(*inlayer);
4224 outlayer->book_type = (inlayer->book_type_version &
4225 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4226 outlayer->book_version = (inlayer->book_type_version &
4227 RDSD_BOOK_VERSION_MASK);
4228 outlayer->disc_size = (inlayer->disc_size_max_rate &
4229 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4230 outlayer->max_rate = (inlayer->disc_size_max_rate &
4231 RDSD_MAX_RATE_MASK);
4232 outlayer->nlayers = (inlayer->layer_info &
4233 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4234 outlayer->track_path = (inlayer->layer_info &
4235 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4236 outlayer->layer_type = (inlayer->layer_info &
4237 RDSD_LAYER_TYPE_MASK);
4238 outlayer->linear_density = (inlayer->density &
4239 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4240 outlayer->track_density = (inlayer->density &
4241 RDSD_TRACK_DENSITY_MASK);
4242 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4244 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4245 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4246 outlayer->end_sector_l0 =
4247 scsi_3btoul(inlayer->end_sector_layer0);
4250 case DVD_STRUCT_COPYRIGHT: {
4251 struct scsi_read_dvd_struct_data_copyright *copy_data;
4253 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4256 dvdstruct->cpst = copy_data->cps_type;
4257 dvdstruct->rmi = copy_data->region_info;
4258 dvdstruct->length = 0;
4264 * Tell the user what the overall length is, no matter
4265 * what we can actually fit in the data buffer.
4267 dvdstruct->length = length - ccb->csio.resid -
4268 sizeof(struct scsi_read_dvd_struct_data_header);
4271 * But only actually copy out the smaller of what we read
4272 * in or what the structure can take.
4274 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4276 min(sizeof(dvdstruct->data), dvdstruct->length));
4281 if (databuf != NULL)
4282 kfree(databuf, M_DEVBUF);
4284 xpt_release_ccb(ccb);
4290 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4291 void (*cbfcnp)(struct cam_periph *, union ccb *),
4292 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4293 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4294 u_int8_t sense_len, u_int32_t timeout)
4296 struct scsi_report_key *scsi_cmd;
4298 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4299 bzero(scsi_cmd, sizeof(*scsi_cmd));
4300 scsi_cmd->opcode = REPORT_KEY;
4301 scsi_ulto4b(lba, scsi_cmd->lba);
4302 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4303 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4304 (key_format & RK_KF_KEYFORMAT_MASK);
4309 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4311 /*data_ptr*/ data_ptr,
4312 /*dxfer_len*/ dxfer_len,
4319 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4320 void (*cbfcnp)(struct cam_periph *, union ccb *),
4321 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4322 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4325 struct scsi_send_key *scsi_cmd;
4327 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4328 bzero(scsi_cmd, sizeof(*scsi_cmd));
4329 scsi_cmd->opcode = SEND_KEY;
4331 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4332 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4333 (key_format & RK_KF_KEYFORMAT_MASK);
4338 /*flags*/ CAM_DIR_OUT,
4340 /*data_ptr*/ data_ptr,
4341 /*dxfer_len*/ dxfer_len,
4349 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4350 void (*cbfcnp)(struct cam_periph *, union ccb *),
4351 u_int8_t tag_action, u_int32_t address,
4352 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4353 u_int8_t *data_ptr, u_int32_t dxfer_len,
4354 u_int8_t sense_len, u_int32_t timeout)
4356 struct scsi_read_dvd_structure *scsi_cmd;
4358 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4359 bzero(scsi_cmd, sizeof(*scsi_cmd));
4360 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4362 scsi_ulto4b(address, scsi_cmd->address);
4363 scsi_cmd->layer_number = layer_number;
4364 scsi_cmd->format = format;
4365 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4366 /* The AGID is the top two bits of this byte */
4367 scsi_cmd->agid = agid << 6;
4372 /*flags*/ CAM_DIR_IN,
4374 /*data_ptr*/ data_ptr,
4375 /*dxfer_len*/ dxfer_len,