2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_cd.c,v 1.41 2007/11/29 02:32:43 pavalos Exp $
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
52 #include <sys/param.h>
53 #include <sys/bootmaj.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
59 #include <sys/dtype.h>
60 #include <sys/malloc.h>
62 #include <sys/cdrio.h>
63 #include <sys/dvdio.h>
64 #include <sys/devicestat.h>
65 #include <sys/sysctl.h>
66 #include <sys/taskqueue.h>
69 #include <sys/thread2.h>
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
76 #include "../cam_queue.h"
78 #include "scsi_message.h"
82 #define LEADOUT 0xaa /* leadout toc entry */
92 CD_Q_BCD_TRACKS = 0x02,
93 CD_Q_NO_CHANGER = 0x04,
95 CD_Q_10_BYTE_ONLY = 0x10
99 CD_FLAG_INVALID = 0x0001,
100 CD_FLAG_NEW_DISC = 0x0002,
101 CD_FLAG_DISC_LOCKED = 0x0004,
102 CD_FLAG_DISC_REMOVABLE = 0x0008,
103 CD_FLAG_TAGGED_QUEUING = 0x0010,
104 CD_FLAG_CHANGER = 0x0040,
105 CD_FLAG_ACTIVE = 0x0080,
106 CD_FLAG_SCHED_ON_COMP = 0x0100,
107 CD_FLAG_RETRY_UA = 0x0200,
108 CD_FLAG_VALID_MEDIA = 0x0400,
109 CD_FLAG_VALID_TOC = 0x0800,
110 CD_FLAG_OPEN = 0x1000,
111 CD_FLAG_SCTX_INIT = 0x2000
116 CD_CCB_BUFFER_IO = 0x02,
117 CD_CCB_WAITING = 0x03,
118 CD_CCB_TYPE_MASK = 0x0F,
119 CD_CCB_RETRY_UA = 0x10
123 CHANGER_TIMEOUT_SCHED = 0x01,
124 CHANGER_SHORT_TMOUT_SCHED = 0x02,
125 CHANGER_MANUAL_CALL = 0x04,
126 CHANGER_NEED_TIMEOUT = 0x08
129 #define ccb_state ppriv_field0
130 #define ccb_bio ppriv_ptr1
133 struct ioc_toc_header header;
134 struct cd_toc_entry entries[100];
137 struct cd_toc_single {
138 struct ioc_toc_header header;
139 struct cd_toc_entry entry;
150 volatile cd_flags flags;
151 struct bio_queue_head bio_queue;
152 LIST_HEAD(, ccb_hdr) pending_ccbs;
153 struct cd_params params;
157 struct devstat device_stats;
158 STAILQ_ENTRY(cd_softc) changer_links;
159 struct cdchanger *changer;
161 struct cam_periph *periph;
162 int minimum_command_size;
163 int outstanding_cmds;
164 struct task sysctl_task;
165 struct sysctl_ctx_list sysctl_ctx;
166 struct sysctl_oid *sysctl_tree;
167 STAILQ_HEAD(, cd_mode_params) mode_queue;
168 struct cd_tocdata toc;
171 struct cd_page_sizes {
176 static struct cd_page_sizes cd_page_size_table[] =
178 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
181 struct cd_quirk_entry {
182 struct scsi_inquiry_pattern inq_pat;
187 * The changer quirk entries aren't strictly necessary. Basically, what
188 * they do is tell cdregister() up front that a device is a changer.
189 * Otherwise, it will figure that fact out once it sees a LUN on the device
190 * that is greater than 0. If it is known up front that a device is a changer,
191 * all I/O to the device will go through the changer scheduling routines, as
192 * opposed to the "normal" CD code.
194 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
195 * your device hangs when it gets a 10 byte command. Adding a quirk just
196 * to get rid of the informative diagnostic message is not acceptable. All
197 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
198 * referenced in a comment along with the quirk) , and must be approved by
199 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
200 * be removed until the submitter can explain why they are needed.
201 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
202 * when the CAM_NEW_TRAN_CODE work is done.
204 static struct cd_quirk_entry cd_quirk_table[] =
207 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
208 /*quirks*/ CD_Q_CHANGER
211 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
212 "*"}, /* quirks */ CD_Q_CHANGER
215 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
216 /* quirks */ CD_Q_CHANGER
219 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
220 /* quirks */ CD_Q_BCD_TRACKS
224 static d_open_t cdopen;
225 static d_close_t cdclose;
226 static d_ioctl_t cdioctl;
227 static d_strategy_t cdstrategy;
229 static periph_init_t cdinit;
230 static periph_ctor_t cdregister;
231 static periph_dtor_t cdcleanup;
232 static periph_start_t cdstart;
233 static periph_oninv_t cdoninvalidate;
234 static void cdasync(void *callback_arg, u_int32_t code,
235 struct cam_path *path, void *arg);
236 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
237 static void cdshorttimeout(void *arg);
238 static void cdschedule(struct cam_periph *periph, int priority);
239 static void cdrunchangerqueue(void *arg);
240 static void cdchangerschedule(struct cd_softc *softc);
241 static int cdrunccb(union ccb *ccb,
242 int (*error_routine)(union ccb *ccb,
244 u_int32_t sense_flags),
245 u_int32_t cam_flags, u_int32_t sense_flags);
246 static union ccb *cdgetccb(struct cam_periph *periph,
248 static void cddone(struct cam_periph *periph,
249 union ccb *start_ccb);
250 static int cderror(union ccb *ccb, u_int32_t cam_flags,
251 u_int32_t sense_flags);
252 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
253 static int cdgetpagesize(int page_num);
254 static void cdprevent(struct cam_periph *periph, int action);
255 static int cdcheckmedia(struct cam_periph *periph);
256 static int cdsize(struct cam_periph *periph, u_int32_t *size);
257 static int cd6byteworkaround(union ccb *ccb);
258 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
259 u_int32_t start, u_int8_t *data,
260 u_int32_t len, u_int32_t sense_flags);
261 static int cdgetmode(struct cam_periph *periph,
262 struct cd_mode_params *data, u_int32_t page);
263 static int cdsetmode(struct cam_periph *periph,
264 struct cd_mode_params *data);
265 static int cdplay(struct cam_periph *periph, u_int32_t blk,
267 static int cdreadsubchannel(struct cam_periph *periph,
268 u_int32_t mode, u_int32_t format,
270 struct cd_sub_channel_info *data,
272 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
273 u_int32_t starts, u_int32_t startf,
274 u_int32_t endm, u_int32_t ends,
276 static int cdplaytracks(struct cam_periph *periph,
277 u_int32_t strack, u_int32_t sindex,
278 u_int32_t etrack, u_int32_t eindex);
279 static int cdpause(struct cam_periph *periph, u_int32_t go);
280 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
281 static int cdstartunit(struct cam_periph *periph, int load);
282 static int cdsetspeed(struct cam_periph *periph,
283 u_int32_t rdspeed, u_int32_t wrspeed);
284 static int cdreportkey(struct cam_periph *periph,
285 struct dvd_authinfo *authinfo);
286 static int cdsendkey(struct cam_periph *periph,
287 struct dvd_authinfo *authinfo);
288 static int cdreaddvdstructure(struct cam_periph *periph,
289 struct dvd_struct *dvdstruct);
291 static struct periph_driver cddriver =
294 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
297 PERIPHDRIVER_DECLARE(cd, cddriver);
299 /* For 2.2-stable support */
303 static struct dev_ops cd_ops = {
304 { "cd", SCSICD_CDEV_MAJOR, D_DISK },
308 .d_write = physwrite,
310 .d_strategy = cdstrategy
313 static struct extend_array *cdperiphs;
314 static int num_changers;
316 #ifndef CHANGER_MIN_BUSY_SECONDS
317 #define CHANGER_MIN_BUSY_SECONDS 5
319 #ifndef CHANGER_MAX_BUSY_SECONDS
320 #define CHANGER_MAX_BUSY_SECONDS 15
323 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
324 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
326 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
327 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
328 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
329 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
330 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
331 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
332 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
333 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
337 target_id_t target_id;
340 struct timeval start_time;
341 struct cd_softc *cur_device;
342 struct callout short_handle;
343 struct callout long_handle;
344 volatile cd_changer_flags flags;
345 STAILQ_ENTRY(cdchanger) changer_links;
346 STAILQ_HEAD(chdevlist, cd_softc) chluns;
349 static STAILQ_HEAD(changerlist, cdchanger) changerq;
355 struct cam_path *path;
358 * Create our extend array for storing the devices we attach to.
360 cdperiphs = cam_extend_new();
361 if (cdperiphs == NULL) {
362 kprintf("cd: Failed to alloc extend array!\n");
367 * Install a global async callback. This callback will
368 * receive async callbacks like "new device found".
370 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
371 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
373 if (status == CAM_REQ_CMP) {
374 struct ccb_setasync csa;
376 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
377 csa.ccb_h.func_code = XPT_SASYNC_CB;
378 csa.event_enable = AC_FOUND_DEVICE;
379 csa.callback = cdasync;
380 csa.callback_arg = NULL;
381 xpt_action((union ccb *)&csa);
382 status = csa.ccb_h.status;
386 if (status != CAM_REQ_CMP) {
387 kprintf("cd: Failed to attach master async callback "
388 "due to status 0x%x!\n", status);
393 cdoninvalidate(struct cam_periph *periph)
395 struct cd_softc *softc;
398 struct ccb_setasync csa;
400 softc = (struct cd_softc *)periph->softc;
403 * De-register any async callbacks.
405 xpt_setup_ccb(&csa.ccb_h, periph->path,
407 csa.ccb_h.func_code = XPT_SASYNC_CB;
408 csa.event_enable = 0;
409 csa.callback = cdasync;
410 csa.callback_arg = periph;
411 xpt_action((union ccb *)&csa);
413 softc->flags |= CD_FLAG_INVALID;
416 * Although the oninvalidate() routines are always while in a
417 * critical section, we need to be in a critical section here to
418 * keep the buffer queue from being modified while we traverse it.
423 * Return all queued I/O with ENXIO.
424 * XXX Handle any transactions queued to the card
425 * with XPT_ABORT_CCB.
427 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
428 bioq_remove(&softc->bio_queue, q_bio);
429 q_bp = q_bio->bio_buf;
430 q_bp->b_resid = q_bp->b_bcount;
431 q_bp->b_error = ENXIO;
432 q_bp->b_flags |= B_ERROR;
438 * If this device is part of a changer, and it was scheduled
439 * to run, remove it from the run queue since we just nuked
440 * all of its scheduled I/O.
442 if ((softc->flags & CD_FLAG_CHANGER)
443 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
444 camq_remove(&softc->changer->devq, softc->pinfo.index);
446 xpt_print_path(periph->path);
447 kprintf("lost device\n");
451 cdcleanup(struct cam_periph *periph)
453 struct cd_softc *softc;
455 softc = (struct cd_softc *)periph->softc;
457 xpt_print_path(periph->path);
458 kprintf("removing device entry\n");
460 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
461 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
462 xpt_print_path(periph->path);
463 kprintf("can't remove sysctl context\n");
468 * In the queued, non-active case, the device in question
469 * has already been removed from the changer run queue. Since this
470 * device is active, we need to de-activate it, and schedule
471 * another device to run. (if there is another one to run)
473 if ((softc->flags & CD_FLAG_CHANGER)
474 && (softc->flags & CD_FLAG_ACTIVE)) {
477 * The purpose of the short timeout is soley to determine
478 * whether the current device has finished or not. Well,
479 * since we're removing the active device, we know that it
480 * is finished. So, get rid of the short timeout.
481 * Otherwise, if we're in the time period before the short
482 * timeout fires, and there are no other devices in the
483 * queue to run, there won't be any other device put in the
484 * active slot. i.e., when we call cdrunchangerqueue()
485 * below, it won't do anything. Then, when the short
486 * timeout fires, it'll look at the "current device", which
487 * we are free below, and possibly panic the kernel on a
488 * bogus pointer reference.
490 * The long timeout doesn't really matter, since we
491 * decrement the qfrozen_cnt to indicate that there is
492 * nothing in the active slot now. Therefore, there won't
493 * be any bogus pointer references there.
495 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
496 callout_stop(&softc->changer->short_handle);
497 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
499 softc->changer->devq.qfrozen_cnt--;
500 softc->changer->flags |= CHANGER_MANUAL_CALL;
501 cdrunchangerqueue(softc->changer);
505 * If we're removing the last device on the changer, go ahead and
506 * remove the changer device structure.
508 if ((softc->flags & CD_FLAG_CHANGER)
509 && (--softc->changer->num_devices == 0)) {
512 * Theoretically, there shouldn't be any timeouts left, but
513 * I'm not completely sure that that will be the case. So,
514 * it won't hurt to check and see if there are any left.
516 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
517 callout_stop(&softc->changer->long_handle);
518 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
521 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
522 callout_stop(&softc->changer->short_handle);
523 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
526 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
528 xpt_print_path(periph->path);
529 kprintf("removing changer entry\n");
530 kfree(softc->changer, M_DEVBUF);
533 devstat_remove_entry(&softc->device_stats);
534 cam_extend_release(cdperiphs, periph->unit_number);
535 if (softc->disk.d_rawdev) {
536 disk_destroy(&softc->disk);
538 kfree(softc, M_DEVBUF);
543 cdasync(void *callback_arg, u_int32_t code,
544 struct cam_path *path, void *arg)
546 struct cam_periph *periph;
548 periph = (struct cam_periph *)callback_arg;
550 case AC_FOUND_DEVICE:
552 struct ccb_getdev *cgd;
555 cgd = (struct ccb_getdev *)arg;
559 if (SID_TYPE(&cgd->inq_data) != T_CDROM
560 && SID_TYPE(&cgd->inq_data) != T_WORM)
564 * Allocate a peripheral instance for
565 * this device and start the probe
568 status = cam_periph_alloc(cdregister, cdoninvalidate,
570 "cd", CAM_PERIPH_BIO,
571 cgd->ccb_h.path, cdasync,
572 AC_FOUND_DEVICE, cgd);
574 if (status != CAM_REQ_CMP
575 && status != CAM_REQ_INPROG)
576 kprintf("cdasync: Unable to attach new device "
577 "due to status 0x%x\n", status);
584 struct cd_softc *softc;
585 struct ccb_hdr *ccbh;
587 softc = (struct cd_softc *)periph->softc;
590 * Don't fail on the expected unit attention
593 softc->flags |= CD_FLAG_RETRY_UA;
594 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
595 ccbh->ccb_state |= CD_CCB_RETRY_UA;
600 cam_periph_async(periph, code, path, arg);
606 cdsysctlinit(void *context, int pending)
608 struct cam_periph *periph;
609 struct cd_softc *softc;
610 char tmpstr[80], tmpstr2[80];
612 periph = (struct cam_periph *)context;
613 softc = (struct cd_softc *)periph->softc;
615 ksnprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
616 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
618 sysctl_ctx_init(&softc->sysctl_ctx);
619 softc->flags |= CD_FLAG_SCTX_INIT;
620 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
621 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
622 tmpstr2, CTLFLAG_RD, 0, tmpstr);
624 if (softc->sysctl_tree == NULL) {
625 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
630 * Now register the sysctl handler, so the user can the value on
633 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
634 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
635 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
640 * We have a handler function for this so we can check the values when the
641 * user sets them, instead of every time we look at them.
644 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
648 value = *(int *)arg1;
650 error = sysctl_handle_int(oidp, &value, 0, req);
652 if ((error != 0) || (req->newptr == NULL))
656 * The only real values we can have here are 6 or 10. I don't
657 * really forsee having 12 be an option at any time in the future.
658 * So if the user sets something less than or equal to 6, we'll set
659 * it to 6. If he sets something greater than 6, we'll set it to 10.
661 * I suppose we could just return an error here for the wrong values,
662 * but I don't think it's necessary to do so, as long as we can
663 * determine the user's intent without too much trouble.
670 *(int *)arg1 = value;
677 cdregister(struct cam_periph *periph, void *arg)
679 struct cd_softc *softc;
680 struct ccb_setasync csa;
681 struct ccb_pathinq cpi;
682 struct ccb_getdev *cgd;
686 cgd = (struct ccb_getdev *)arg;
687 if (periph == NULL) {
688 kprintf("cdregister: periph was NULL!!\n");
689 return(CAM_REQ_CMP_ERR);
692 kprintf("cdregister: no getdev CCB, can't register device\n");
693 return(CAM_REQ_CMP_ERR);
696 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
697 LIST_INIT(&softc->pending_ccbs);
698 STAILQ_INIT(&softc->mode_queue);
699 softc->state = CD_STATE_PROBE;
700 bioq_init(&softc->bio_queue);
701 if (SID_IS_REMOVABLE(&cgd->inq_data))
702 softc->flags |= CD_FLAG_DISC_REMOVABLE;
703 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
704 softc->flags |= CD_FLAG_TAGGED_QUEUING;
706 periph->softc = softc;
707 softc->periph = periph;
709 cam_extend_set(cdperiphs, periph->unit_number, periph);
712 * See if this device has any quirks.
714 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
715 (caddr_t)cd_quirk_table,
716 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
717 sizeof(*cd_quirk_table), scsi_inquiry_match);
720 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
722 softc->quirks = CD_Q_NONE;
724 /* Check if the SIM does not want 6 byte commands */
725 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
726 cpi.ccb_h.func_code = XPT_PATH_INQ;
727 xpt_action((union ccb *)&cpi);
728 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
729 softc->quirks |= CD_Q_10_BYTE_ONLY;
731 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
733 /* The default is 6 byte commands, unless quirked otherwise */
734 if (softc->quirks & CD_Q_10_BYTE_ONLY)
735 softc->minimum_command_size = 10;
737 softc->minimum_command_size = 6;
740 * Load the user's default, if any.
742 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
743 periph->unit_number);
744 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
746 /* 6 and 10 are the only permissible values here. */
747 if (softc->minimum_command_size < 6)
748 softc->minimum_command_size = 6;
749 else if (softc->minimum_command_size > 6)
750 softc->minimum_command_size = 10;
753 * We need to register the statistics structure for this device,
754 * but we don't have the blocksize yet for it. So, we register
755 * the structure and indicate that we don't have the blocksize
756 * yet. Unlike other SCSI peripheral drivers, we explicitly set
757 * the device type here to be CDROM, rather than just ORing in
758 * the device type. This is because this driver can attach to either
759 * CDROM or WORM devices, and we want this peripheral driver to
760 * show up in the devstat list as a CD peripheral driver, not a
761 * WORM peripheral driver. WORM drives will also have the WORM
762 * driver attached to them.
764 devstat_add_entry(&softc->device_stats, "cd",
765 periph->unit_number, 0,
766 DEVSTAT_BS_UNAVAILABLE,
767 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
768 DEVSTAT_PRIORITY_CD);
769 disk_create(periph->unit_number, &softc->disk, &cd_ops);
770 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
771 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
775 * Add an async callback so that we get
776 * notified if this device goes away.
778 xpt_setup_ccb(&csa.ccb_h, periph->path,
780 csa.ccb_h.func_code = XPT_SASYNC_CB;
781 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
782 csa.callback = cdasync;
783 csa.callback_arg = periph;
784 xpt_action((union ccb *)&csa);
787 * If the target lun is greater than 0, we most likely have a CD
788 * changer device. Check the quirk entries as well, though, just
789 * in case someone has a CD tower with one lun per drive or
790 * something like that. Also, if we know up front that a
791 * particular device is a changer, we can mark it as such starting
792 * with lun 0, instead of lun 1. It shouldn't be necessary to have
793 * a quirk entry to define something as a changer, however.
795 if (((cgd->ccb_h.target_lun > 0)
796 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
797 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
798 struct cdchanger *nchanger;
799 struct cam_periph *nperiph;
800 struct cam_path *path;
804 /* Set the changer flag in the current device's softc */
805 softc->flags |= CD_FLAG_CHANGER;
807 if (num_changers == 0)
808 STAILQ_INIT(&changerq);
811 * Now, look around for an existing changer device with the
812 * same path and target ID as the current device.
815 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
817 nchanger = STAILQ_NEXT(nchanger, changer_links)){
818 if ((nchanger->path_id == cgd->ccb_h.path_id)
819 && (nchanger->target_id == cgd->ccb_h.target_id)) {
826 * If we found a matching entry, just add this device to
827 * the list of devices on this changer.
830 struct chdevlist *chlunhead;
832 chlunhead = &nchanger->chluns;
835 * XXX KDM look at consolidating this code with the
836 * code below in a separate function.
840 * Create a path with lun id 0, and see if we can
841 * find a matching device
843 status = xpt_create_path(&path, /*periph*/ periph,
845 cgd->ccb_h.target_id, 0);
847 if ((status == CAM_REQ_CMP)
848 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
849 struct cd_softc *nsoftc;
851 nsoftc = (struct cd_softc *)nperiph->softc;
853 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
854 nsoftc->flags |= CD_FLAG_CHANGER;
855 nchanger->num_devices++;
856 if (camq_resize(&nchanger->devq,
857 nchanger->num_devices)!=CAM_REQ_CMP){
858 kprintf("cdregister: "
864 nsoftc->changer = nchanger;
865 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
867 STAILQ_INSERT_TAIL(&nchanger->chluns,
868 nsoftc,changer_links);
871 } else if (status == CAM_REQ_CMP)
874 kprintf("cdregister: unable to allocate path\n"
875 "cdregister: changer support may be "
879 nchanger->num_devices++;
881 softc->changer = nchanger;
882 softc->pinfo.index = CAM_UNQUEUED_INDEX;
884 if (camq_resize(&nchanger->devq,
885 nchanger->num_devices) != CAM_REQ_CMP) {
886 kprintf("cdregister: camq_resize "
887 "failed, changer support may "
891 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
894 * In this case, we don't already have an entry for this
895 * particular changer, so we need to create one, add it to
896 * the queue, and queue this device on the list for this
897 * changer. Before we queue this device, however, we need
898 * to search for lun id 0 on this target, and add it to the
899 * queue first, if it exists. (and if it hasn't already
900 * been marked as part of the changer.)
903 nchanger = kmalloc(sizeof(struct cdchanger),
904 M_DEVBUF, M_INTWAIT | M_ZERO);
905 callout_init(&nchanger->short_handle);
906 callout_init(&nchanger->long_handle);
907 if (camq_init(&nchanger->devq, 1) != 0) {
908 softc->flags &= ~CD_FLAG_CHANGER;
909 kprintf("cdregister: changer support "
916 nchanger->path_id = cgd->ccb_h.path_id;
917 nchanger->target_id = cgd->ccb_h.target_id;
919 /* this is superfluous, but it makes things clearer */
920 nchanger->num_devices = 0;
922 STAILQ_INIT(&nchanger->chluns);
924 STAILQ_INSERT_TAIL(&changerq, nchanger,
928 * Create a path with lun id 0, and see if we can
929 * find a matching device
931 status = xpt_create_path(&path, /*periph*/ periph,
933 cgd->ccb_h.target_id, 0);
936 * If we were able to allocate the path, and if we
937 * find a matching device and it isn't already
938 * marked as part of a changer, then we add it to
939 * the current changer.
941 if ((status == CAM_REQ_CMP)
942 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
943 && ((((struct cd_softc *)periph->softc)->flags &
944 CD_FLAG_CHANGER) == 0)) {
945 struct cd_softc *nsoftc;
947 nsoftc = (struct cd_softc *)nperiph->softc;
949 nsoftc->flags |= CD_FLAG_CHANGER;
950 nchanger->num_devices++;
951 if (camq_resize(&nchanger->devq,
952 nchanger->num_devices) != CAM_REQ_CMP) {
953 kprintf("cdregister: camq_resize "
954 "failed, changer support may "
957 nsoftc->changer = nchanger;
958 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
960 STAILQ_INSERT_TAIL(&nchanger->chluns,
961 nsoftc, changer_links);
963 } else if (status == CAM_REQ_CMP)
966 kprintf("cdregister: unable to allocate path\n"
967 "cdregister: changer support may be "
971 softc->changer = nchanger;
972 softc->pinfo.index = CAM_UNQUEUED_INDEX;
973 nchanger->num_devices++;
974 if (camq_resize(&nchanger->devq,
975 nchanger->num_devices) != CAM_REQ_CMP) {
976 kprintf("cdregister: camq_resize "
977 "failed, changer support may "
980 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
987 /* Lock this peripheral until we are setup */
989 cam_periph_lock(periph, 0);
991 if ((softc->flags & CD_FLAG_CHANGER) == 0)
992 xpt_schedule(periph, /*priority*/5);
994 cdschedule(periph, /*priority*/ 5);
1000 cdopen(struct dev_open_args *ap)
1002 cdev_t dev = ap->a_head.a_dev;
1003 struct cam_periph *periph;
1004 struct cd_softc *softc;
1008 periph = cam_extend_get(cdperiphs, unit);
1013 softc = (struct cd_softc *)periph->softc;
1016 * Grab a critical section and hold it until we lock the peripheral.
1019 if (softc->flags & CD_FLAG_INVALID) {
1024 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
1031 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1035 * Check for media, and set the appropriate flags. We don't bail
1036 * if we don't have media, but then we don't allow anything but the
1037 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1039 * XXX KDM for now, we do fail the open if we don't have media. We
1040 * can change this once we've figured out how to make the slice
1041 * code work well with media changing underneath it.
1043 error = cdcheckmedia(periph);
1046 softc->flags |= CD_FLAG_OPEN;
1048 cam_periph_unlock(periph);
1050 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1056 cdclose(struct dev_close_args *ap)
1058 cdev_t dev = ap->a_head.a_dev;
1059 struct cam_periph *periph;
1060 struct cd_softc *softc;
1061 struct disk_info *info;
1065 periph = cam_extend_get(cdperiphs, unit);
1069 softc = (struct cd_softc *)periph->softc;
1071 if ((error = cam_periph_lock(periph, 0)) != 0)
1074 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1075 cdprevent(periph, PR_ALLOW);
1078 * Unconditionally set the dsopen() flags back to their default
1081 info = &softc->disk.d_info;
1082 info->d_dsflags &= ~DSO_NOLABELS;
1083 info->d_dsflags |= DSO_COMPATLABEL;
1086 * Since we're closing this CD, mark the blocksize as unavailable.
1087 * It will be marked as available when the CD is opened again.
1089 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1092 * We'll check the media and toc again at the next open().
1094 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1096 softc->flags &= ~CD_FLAG_OPEN;
1098 cam_periph_unlock(periph);
1099 cam_periph_release(periph);
1105 cdshorttimeout(void *arg)
1107 struct cdchanger *changer;
1111 changer = (struct cdchanger *)arg;
1113 /* Always clear the short timeout flag, since that's what we're in */
1114 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1117 * Check to see if there is any more pending or outstanding I/O for
1118 * this device. If not, move it out of the active slot.
1120 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1121 && (changer->cur_device->outstanding_cmds == 0)) {
1122 changer->flags |= CHANGER_MANUAL_CALL;
1123 cdrunchangerqueue(changer);
1129 * This is a wrapper for xpt_schedule. It only applies to changers.
1132 cdschedule(struct cam_periph *periph, int priority)
1134 struct cd_softc *softc;
1138 softc = (struct cd_softc *)periph->softc;
1141 * If this device isn't currently queued, and if it isn't
1142 * the active device, then we queue this device and run the
1143 * changer queue if there is no timeout scheduled to do it.
1144 * If this device is the active device, just schedule it
1145 * to run again. If this device is queued, there should be
1146 * a timeout in place already that will make sure it runs.
1148 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1149 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1151 * We don't do anything with the priority here.
1152 * This is strictly a fifo queue.
1154 softc->pinfo.priority = 1;
1155 softc->pinfo.generation = ++softc->changer->devq.generation;
1156 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1159 * Since we just put a device in the changer queue,
1160 * check and see if there is a timeout scheduled for
1161 * this changer. If so, let the timeout handle
1162 * switching this device into the active slot. If
1163 * not, manually call the timeout routine to
1166 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1167 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1168 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1169 softc->changer->flags |= CHANGER_MANUAL_CALL;
1170 cdrunchangerqueue(softc->changer);
1172 } else if ((softc->flags & CD_FLAG_ACTIVE)
1173 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1174 xpt_schedule(periph, priority);
1180 cdrunchangerqueue(void *arg)
1182 struct cd_softc *softc;
1183 struct cdchanger *changer;
1184 int called_from_timeout;
1188 changer = (struct cdchanger *)arg;
1191 * If we have NOT been called from cdstrategy() or cddone(), and
1192 * instead from a timeout routine, go ahead and clear the
1195 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1196 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1197 called_from_timeout = 1;
1199 called_from_timeout = 0;
1201 /* Always clear the manual call flag */
1202 changer->flags &= ~CHANGER_MANUAL_CALL;
1204 /* nothing to do if the queue is empty */
1205 if (changer->devq.entries <= 0) {
1211 * If the changer queue is frozen, that means we have an active
1214 if (changer->devq.qfrozen_cnt > 0) {
1216 if (changer->cur_device->outstanding_cmds > 0) {
1217 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1218 changer->cur_device->bufs_left =
1219 changer->cur_device->outstanding_cmds;
1220 if (called_from_timeout) {
1221 callout_reset(&changer->long_handle,
1222 changer_max_busy_seconds * hz,
1223 cdrunchangerqueue, changer);
1224 changer->flags |= CHANGER_TIMEOUT_SCHED;
1231 * We always need to reset the frozen count and clear the
1234 changer->devq.qfrozen_cnt--;
1235 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1236 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1239 * Check to see whether the current device has any I/O left
1240 * to do. If so, requeue it at the end of the queue. If
1241 * not, there is no need to requeue it.
1243 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1245 changer->cur_device->pinfo.generation =
1246 ++changer->devq.generation;
1247 camq_insert(&changer->devq,
1248 (cam_pinfo *)changer->cur_device);
1252 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1254 changer->cur_device = softc;
1256 changer->devq.qfrozen_cnt++;
1257 softc->flags |= CD_FLAG_ACTIVE;
1259 /* Just in case this device is waiting */
1260 wakeup(&softc->changer);
1261 xpt_schedule(softc->periph, /*priority*/ 1);
1264 * Get rid of any pending timeouts, and set a flag to schedule new
1265 * ones so this device gets its full time quantum.
1267 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1268 callout_stop(&changer->long_handle);
1269 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1272 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1273 callout_stop(&changer->short_handle);
1274 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1278 * We need to schedule timeouts, but we only do this after the
1279 * first transaction has completed. This eliminates the changer
1282 changer->flags |= CHANGER_NEED_TIMEOUT;
1288 cdchangerschedule(struct cd_softc *softc)
1290 struct cdchanger *changer;
1294 changer = softc->changer;
1297 * If this is a changer, and this is the current device,
1298 * and this device has at least the minimum time quantum to
1299 * run, see if we can switch it out.
1301 if ((softc->flags & CD_FLAG_ACTIVE)
1302 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1303 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1305 * We try three things here. The first is that we
1306 * check to see whether the schedule on completion
1307 * flag is set. If it is, we decrement the number
1308 * of buffers left, and if it's zero, we reschedule.
1309 * Next, we check to see whether the pending buffer
1310 * queue is empty and whether there are no
1311 * outstanding transactions. If so, we reschedule.
1312 * Next, we see if the pending buffer queue is empty.
1313 * If it is, we set the number of buffers left to
1314 * the current active buffer count and set the
1315 * schedule on complete flag.
1317 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1318 if (--softc->bufs_left == 0) {
1319 softc->changer->flags |=
1320 CHANGER_MANUAL_CALL;
1321 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1322 cdrunchangerqueue(softc->changer);
1324 } else if ((bioq_first(&softc->bio_queue) == NULL)
1325 && (softc->outstanding_cmds == 0)) {
1326 softc->changer->flags |= CHANGER_MANUAL_CALL;
1327 cdrunchangerqueue(softc->changer);
1329 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1330 && (softc->flags & CD_FLAG_ACTIVE)) {
1333 * Now that the first transaction to this
1334 * particular device has completed, we can go ahead
1335 * and schedule our timeouts.
1337 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1338 callout_reset(&changer->long_handle,
1339 changer_max_busy_seconds * hz,
1340 cdrunchangerqueue, changer);
1341 changer->flags |= CHANGER_TIMEOUT_SCHED;
1343 kprintf("cdchangerschedule: already have a long"
1346 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1347 callout_reset(&changer->short_handle,
1348 changer_min_busy_seconds * hz,
1349 cdshorttimeout, changer);
1350 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1352 kprintf("cdchangerschedule: already have a short "
1356 * We just scheduled timeouts, no need to schedule
1359 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1366 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1367 u_int32_t cam_flags,
1368 u_int32_t sense_flags),
1369 u_int32_t cam_flags, u_int32_t sense_flags)
1371 struct cd_softc *softc;
1372 struct cam_periph *periph;
1375 periph = xpt_path_periph(ccb->ccb_h.path);
1376 softc = (struct cd_softc *)periph->softc;
1378 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1379 &softc->device_stats);
1381 if (softc->flags & CD_FLAG_CHANGER)
1382 cdchangerschedule(softc);
1388 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1390 struct cd_softc *softc;
1392 softc = (struct cd_softc *)periph->softc;
1394 if (softc->flags & CD_FLAG_CHANGER) {
1398 * This should work the first time this device is woken up,
1399 * but just in case it doesn't, we use a while loop.
1401 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1403 * If this changer isn't already queued, queue it up.
1405 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1406 softc->pinfo.priority = 1;
1407 softc->pinfo.generation =
1408 ++softc->changer->devq.generation;
1409 camq_insert(&softc->changer->devq,
1410 (cam_pinfo *)softc);
1412 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1413 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1414 && ((softc->changer->flags
1415 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1416 softc->changer->flags |= CHANGER_MANUAL_CALL;
1417 cdrunchangerqueue(softc->changer);
1419 tsleep(&softc->changer, 0, "cgticb", 0);
1423 return(cam_periph_getccb(periph, priority));
1427 * Actually translate the requested transfer into one the physical driver
1428 * can understand. The transfer is described by a buf and will include
1429 * only one physical transfer.
1432 cdstrategy(struct dev_strategy_args *ap)
1434 cdev_t dev = ap->a_head.a_dev;
1435 struct bio *bio = ap->a_bio;
1436 struct buf *bp = bio->bio_buf;
1437 struct cam_periph *periph;
1438 struct cd_softc *softc;
1442 periph = cam_extend_get(cdperiphs, unit);
1443 if (periph == NULL) {
1444 bp->b_error = ENXIO;
1448 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1450 softc = (struct cd_softc *)periph->softc;
1453 * Mask interrupts so that the pack cannot be invalidated until
1454 * after we are in the queue. Otherwise, we might not properly
1455 * clean up one of the buffers.
1460 * If the device has been made invalid, error out
1462 if ((softc->flags & CD_FLAG_INVALID)) {
1464 bp->b_error = ENXIO;
1469 * If we don't have valid media, look for it before trying to
1472 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1475 error = cdcheckmedia(periph);
1478 bp->b_error = error;
1484 * Place it in the queue of disk activities for this disk
1486 bioqdisksort(&softc->bio_queue, bio);
1491 * Schedule ourselves for performing the work. We do things
1492 * differently for changers.
1494 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1495 xpt_schedule(periph, /* XXX priority */1);
1497 cdschedule(periph, /* priority */ 1);
1501 bp->b_flags |= B_ERROR;
1503 * Correctly set the buf to indicate a completed xfer
1505 bp->b_resid = bp->b_bcount;
1511 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1513 struct cd_softc *softc;
1516 struct ccb_scsiio *csio;
1517 struct scsi_read_capacity_data *rcap;
1519 softc = (struct cd_softc *)periph->softc;
1521 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1523 switch (softc->state) {
1524 case CD_STATE_NORMAL:
1527 bio = bioq_first(&softc->bio_queue);
1528 if (periph->immediate_priority <= periph->pinfo.priority) {
1529 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1531 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1533 periph->immediate_priority = CAM_PRIORITY_NONE;
1535 wakeup(&periph->ccb_list);
1536 } else if (bio == NULL) {
1538 xpt_release_ccb(start_ccb);
1541 bioq_remove(&softc->bio_queue, bio);
1543 devstat_start_transaction(&softc->device_stats);
1545 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1547 scsi_read_write(&start_ccb->csio,
1549 /* cbfcnp */ cddone,
1550 (bp->b_flags & B_ORDERED) != 0 ?
1553 /* read */(bp->b_cmd == BUF_CMD_READ),
1555 /* minimum_cmd_size */ 10,
1557 bio->bio_offset / softc->params.blksize,
1558 bp->b_bcount / softc->params.blksize,
1559 /* data_ptr */ bp->b_data,
1560 /* dxfer_len */ bp->b_bcount,
1561 /* sense_len */ SSD_FULL_SIZE,
1562 /* timeout */ 30000);
1563 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1567 * Block out any asyncronous callbacks
1568 * while we touch the pending ccb list.
1570 LIST_INSERT_HEAD(&softc->pending_ccbs,
1571 &start_ccb->ccb_h, periph_links.le);
1573 softc->outstanding_cmds++;
1574 /* We expect a unit attention from this device */
1575 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1576 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1577 softc->flags &= ~CD_FLAG_RETRY_UA;
1580 start_ccb->ccb_h.ccb_bio = bio;
1581 bio = bioq_first(&softc->bio_queue);
1584 xpt_action(start_ccb);
1587 /* Have more work to do, so ensure we stay scheduled */
1588 xpt_schedule(periph, /* XXX priority */1);
1592 case CD_STATE_PROBE:
1595 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT);
1596 csio = &start_ccb->csio;
1597 scsi_read_capacity(csio,
1604 start_ccb->ccb_h.ccb_bio = NULL;
1605 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1606 xpt_action(start_ccb);
1613 cddone(struct cam_periph *periph, union ccb *done_ccb)
1615 struct cd_softc *softc;
1616 struct ccb_scsiio *csio;
1618 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1620 softc = (struct cd_softc *)periph->softc;
1621 csio = &done_ccb->csio;
1623 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1624 case CD_CCB_BUFFER_IO:
1630 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1634 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1637 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1642 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1643 if (error == ERESTART) {
1645 * A retry was scheuled, so
1656 xpt_print_path(periph->path);
1657 kprintf("cddone: got error %#x back\n", error);
1659 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1660 bioq_remove(&softc->bio_queue, q_bio);
1661 q_bp = q_bio->bio_buf;
1662 q_bp->b_resid = q_bp->b_bcount;
1663 q_bp->b_error = EIO;
1664 q_bp->b_flags |= B_ERROR;
1668 bp->b_resid = bp->b_bcount;
1669 bp->b_error = error;
1670 bp->b_flags |= B_ERROR;
1671 cam_release_devq(done_ccb->ccb_h.path,
1675 /*getcount_only*/0);
1678 bp->b_resid = csio->resid;
1680 if (bp->b_resid != 0) {
1681 /* Short transfer ??? */
1682 bp->b_flags |= B_ERROR;
1687 * Block out any asyncronous callbacks
1688 * while we touch the pending ccb list.
1691 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1692 softc->outstanding_cmds--;
1695 if (softc->flags & CD_FLAG_CHANGER)
1696 cdchangerschedule(softc);
1698 devstat_end_transaction_buf(&softc->device_stats, bp);
1704 struct scsi_read_capacity_data *rdcap;
1705 char announce_buf[120]; /*
1706 * Currently (9/30/97) the
1707 * longest possible announce
1708 * buffer is 108 bytes, for the
1709 * first error case below.
1710 * That is 39 bytes for the
1711 * basic string, 16 bytes for the
1712 * biggest sense key (hardware
1713 * error), 52 bytes for the
1714 * text of the largest sense
1715 * qualifier valid for a CDROM,
1716 * (0x72, 0x03 or 0x04,
1717 * 0x03), and one byte for the
1718 * null terminating character.
1719 * To allow for longer strings,
1720 * the announce buffer is 120
1723 struct cd_params *cdp;
1725 cdp = &softc->params;
1727 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1729 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1730 cdp->blksize = scsi_4btoul (rdcap->length);
1732 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1734 ksnprintf(announce_buf, sizeof(announce_buf),
1735 "cd present [%lu x %lu byte records]",
1736 cdp->disksize, (u_long)cdp->blksize);
1741 * Retry any UNIT ATTENTION type errors. They
1742 * are expected at boot.
1744 error = cderror(done_ccb, CAM_RETRY_SELTO,
1745 SF_RETRY_UA | SF_NO_PRINT);
1746 if (error == ERESTART) {
1748 * A retry was scheuled, so
1752 } else if (error != 0) {
1754 struct scsi_sense_data *sense;
1756 int sense_key, error_code;
1759 struct ccb_getdev cgd;
1761 /* Don't wedge this device's queue */
1762 cam_release_devq(done_ccb->ccb_h.path,
1766 /*getcount_only*/0);
1768 status = done_ccb->ccb_h.status;
1770 xpt_setup_ccb(&cgd.ccb_h,
1771 done_ccb->ccb_h.path,
1773 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1774 xpt_action((union ccb *)&cgd);
1776 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1777 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1778 || ((status & CAM_AUTOSNS_VALID) == 0))
1784 sense = &csio->sense_data;
1785 scsi_extract_sense(sense, &error_code,
1790 * Attach to anything that claims to be a
1791 * CDROM or WORM device, as long as it
1792 * doesn't return a "Logical unit not
1793 * supported" (0x25) error.
1795 if ((have_sense) && (asc != 0x25)
1796 && (error_code == SSD_CURRENT_ERROR)) {
1797 const char *sense_key_desc;
1798 const char *asc_desc;
1800 scsi_sense_desc(sense_key, asc, ascq,
1804 ksnprintf(announce_buf,
1805 sizeof(announce_buf),
1806 "Attempt to query device "
1807 "size failed: %s, %s",
1810 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1812 * We only print out an error for
1813 * CDROM type devices. For WORM
1814 * devices, we don't print out an
1815 * error since a few WORM devices
1816 * don't support CDROM commands.
1817 * If we have sense information, go
1818 * ahead and print it out.
1819 * Otherwise, just say that we
1824 * Just print out the error, not
1825 * the full probe message, when we
1832 xpt_print_path(periph->path);
1833 kprintf("got CAM status %#x\n",
1834 done_ccb->ccb_h.status);
1836 xpt_print_path(periph->path);
1837 kprintf("fatal error, failed"
1838 " to attach to device\n");
1841 * Invalidate this peripheral.
1843 cam_periph_invalidate(periph);
1845 announce_buf[0] = '\0';
1849 * Invalidate this peripheral.
1851 cam_periph_invalidate(periph);
1852 announce_buf[0] = '\0';
1856 kfree(rdcap, M_TEMP);
1857 if (announce_buf[0] != '\0') {
1858 xpt_announce_periph(periph, announce_buf);
1859 if (softc->flags & CD_FLAG_CHANGER)
1860 cdchangerschedule(softc);
1862 * Create our sysctl variables, now that we know
1863 * we have successfully attached.
1865 taskqueue_enqueue(taskqueue_thread[mycpuid],
1866 &softc->sysctl_task);
1868 softc->state = CD_STATE_NORMAL;
1870 * Since our peripheral may be invalidated by an error
1871 * above or an external event, we must release our CCB
1872 * before releasing the probe lock on the peripheral.
1873 * The peripheral will only go away once the last lock
1874 * is removed, and we need it around for the CCB release
1877 xpt_release_ccb(done_ccb);
1878 cam_periph_unlock(periph);
1881 case CD_CCB_WAITING:
1883 /* Caller will release the CCB */
1884 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1885 ("trying to wakeup ccbwait\n"));
1887 wakeup(&done_ccb->ccb_h.cbfcnp);
1893 xpt_release_ccb(done_ccb);
1896 static union cd_pages *
1897 cdgetpage(struct cd_mode_params *mode_params)
1899 union cd_pages *page;
1901 if (mode_params->cdb_size == 10)
1902 page = (union cd_pages *)find_mode_page_10(
1903 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1905 page = (union cd_pages *)find_mode_page_6(
1906 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1912 cdgetpagesize(int page_num)
1916 for (i = 0; i < (sizeof(cd_page_size_table)/
1917 sizeof(cd_page_size_table[0])); i++) {
1918 if (cd_page_size_table[i].page == page_num)
1919 return (cd_page_size_table[i].page_size);
1925 cdioctl(struct dev_ioctl_args *ap)
1927 cdev_t dev = ap->a_head.a_dev;
1928 caddr_t addr = ap->a_data;
1929 struct cam_periph *periph;
1930 struct cd_softc *softc;
1935 periph = cam_extend_get(cdperiphs, unit);
1939 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1941 softc = (struct cd_softc *)periph->softc;
1943 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1944 ("trying to do ioctl %#lx\n", ap->a_cmd));
1946 error = cam_periph_lock(periph, PCATCH);
1952 * If we don't have media loaded, check for it. If still don't
1953 * have media loaded, we can only do a load or eject.
1955 * We only care whether media is loaded if this is a cd-specific ioctl
1956 * (thus the IOCGROUP check below). Note that this will break if
1957 * anyone adds any ioctls into the switch statement below that don't
1958 * have their ioctl group set to 'c'.
1960 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1961 && ((ap->a_cmd != CDIOCCLOSE)
1962 && (ap->a_cmd != CDIOCEJECT))
1963 && (IOCGROUP(ap->a_cmd) == 'c')) {
1964 error = cdcheckmedia(periph);
1966 cam_periph_unlock(periph);
1971 switch (ap->a_cmd) {
1973 case CDIOCPLAYTRACKS:
1975 struct ioc_play_track *args
1976 = (struct ioc_play_track *)addr;
1977 struct cd_mode_params params;
1978 union cd_pages *page;
1980 params.alloc_len = sizeof(union cd_mode_data_6_10);
1981 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
1984 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1985 ("trying to do CDIOCPLAYTRACKS\n"));
1987 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1989 kfree(params.mode_buf, M_TEMP);
1992 page = cdgetpage(¶ms);
1994 page->audio.flags &= ~CD_PA_SOTC;
1995 page->audio.flags |= CD_PA_IMMED;
1996 error = cdsetmode(periph, ¶ms);
1997 kfree(params.mode_buf, M_TEMP);
2002 * This was originally implemented with the PLAY
2003 * AUDIO TRACK INDEX command, but that command was
2004 * deprecated after SCSI-2. Most (all?) SCSI CDROM
2005 * drives support it but ATAPI and ATAPI-derivative
2006 * drives don't seem to support it. So we keep a
2007 * cache of the table of contents and translate
2008 * track numbers to MSF format.
2010 if (softc->flags & CD_FLAG_VALID_TOC) {
2011 union msf_lba *sentry, *eentry;
2014 if (args->end_track <
2015 softc->toc.header.ending_track + 1)
2017 if (args->end_track >
2018 softc->toc.header.ending_track + 1)
2020 softc->toc.header.ending_track + 1;
2021 st = args->start_track -
2022 softc->toc.header.starting_track;
2023 et = args->end_track -
2024 softc->toc.header.starting_track;
2027 || (st > (softc->toc.header.ending_track -
2028 softc->toc.header.starting_track))) {
2032 sentry = &softc->toc.entries[st].addr;
2033 eentry = &softc->toc.entries[et].addr;
2034 error = cdplaymsf(periph,
2043 * If we don't have a valid TOC, try the
2044 * play track index command. It is part of
2045 * the SCSI-2 spec, but was removed in the
2046 * MMC specs. ATAPI and ATAPI-derived
2047 * drives don't support it.
2049 if (softc->quirks & CD_Q_BCD_TRACKS) {
2051 bin2bcd(args->start_track);
2053 bin2bcd(args->end_track);
2055 error = cdplaytracks(periph,
2065 struct ioc_play_msf *args
2066 = (struct ioc_play_msf *) addr;
2067 struct cd_mode_params params;
2068 union cd_pages *page;
2070 params.alloc_len = sizeof(union cd_mode_data_6_10);
2071 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2074 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2075 ("trying to do CDIOCPLAYMSF\n"));
2077 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2079 kfree(params.mode_buf, M_TEMP);
2082 page = cdgetpage(¶ms);
2084 page->audio.flags &= ~CD_PA_SOTC;
2085 page->audio.flags |= CD_PA_IMMED;
2086 error = cdsetmode(periph, ¶ms);
2087 kfree(params.mode_buf, M_TEMP);
2090 error = cdplaymsf(periph,
2099 case CDIOCPLAYBLOCKS:
2101 struct ioc_play_blocks *args
2102 = (struct ioc_play_blocks *) addr;
2103 struct cd_mode_params params;
2104 union cd_pages *page;
2106 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2107 ("trying to do CDIOCPLAYBLOCKS\n"));
2109 params.alloc_len = sizeof(union cd_mode_data_6_10);
2110 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2113 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2115 kfree(params.mode_buf, M_TEMP);
2118 page = cdgetpage(¶ms);
2120 page->audio.flags &= ~CD_PA_SOTC;
2121 page->audio.flags |= CD_PA_IMMED;
2122 error = cdsetmode(periph, ¶ms);
2123 kfree(params.mode_buf, M_TEMP);
2126 error = cdplay(periph, args->blk, args->len);
2129 case CDIOCREADSUBCHANNEL:
2131 struct ioc_read_subchannel *args
2132 = (struct ioc_read_subchannel *) addr;
2133 struct cd_sub_channel_info *data;
2134 u_int32_t len = args->data_len;
2136 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2137 ("trying to do CDIOCREADSUBCHANNEL\n"));
2139 data = kmalloc(sizeof(struct cd_sub_channel_info),
2142 if ((len > sizeof(struct cd_sub_channel_info)) ||
2143 (len < sizeof(struct cd_sub_channel_header))) {
2145 "scsi_cd: cdioctl: "
2146 "cdioreadsubchannel: error, len=%d\n",
2149 kfree(data, M_TEMP);
2153 if (softc->quirks & CD_Q_BCD_TRACKS)
2154 args->track = bin2bcd(args->track);
2156 error = cdreadsubchannel(periph, args->address_format,
2157 args->data_format, args->track, data, len);
2160 kfree(data, M_TEMP);
2163 if (softc->quirks & CD_Q_BCD_TRACKS)
2164 data->what.track_info.track_number =
2165 bcd2bin(data->what.track_info.track_number);
2166 len = min(len, ((data->header.data_len[0] << 8) +
2167 data->header.data_len[1] +
2168 sizeof(struct cd_sub_channel_header)));
2169 if (copyout(data, args->data, len) != 0) {
2172 kfree(data, M_TEMP);
2176 case CDIOREADTOCHEADER:
2178 struct ioc_toc_header *th;
2180 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2181 ("trying to do CDIOREADTOCHEADER\n"));
2183 th = kmalloc(sizeof(struct ioc_toc_header), M_TEMP,
2185 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2186 sizeof (*th), /*sense_flags*/0);
2191 if (softc->quirks & CD_Q_BCD_TRACKS) {
2192 /* we are going to have to convert the BCD
2193 * encoding on the cd to what is expected
2195 th->starting_track =
2196 bcd2bin(th->starting_track);
2197 th->ending_track = bcd2bin(th->ending_track);
2199 th->len = ntohs(th->len);
2200 bcopy(th, addr, sizeof(*th));
2204 case CDIOREADTOCENTRYS:
2206 struct cd_tocdata *data;
2207 struct cd_toc_single *lead;
2208 struct ioc_read_toc_entry *te =
2209 (struct ioc_read_toc_entry *) addr;
2210 struct ioc_toc_header *th;
2211 u_int32_t len, readlen, idx, num;
2212 u_int32_t starting_track = te->starting_track;
2214 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2215 ("trying to do CDIOREADTOCENTRYS\n"));
2217 data = kmalloc(sizeof(*data), M_TEMP, M_WAITOK);
2218 lead = kmalloc(sizeof(*lead), M_TEMP, M_WAITOK);
2220 if (te->data_len < sizeof(struct cd_toc_entry)
2221 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2222 || (te->address_format != CD_MSF_FORMAT
2223 && te->address_format != CD_LBA_FORMAT)) {
2225 kprintf("scsi_cd: error in readtocentries, "
2226 "returning EINVAL\n");
2227 kfree(data, M_TEMP);
2228 kfree(lead, M_TEMP);
2233 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2234 sizeof (*th), /*sense_flags*/0);
2236 kfree(data, M_TEMP);
2237 kfree(lead, M_TEMP);
2241 if (softc->quirks & CD_Q_BCD_TRACKS) {
2242 /* we are going to have to convert the BCD
2243 * encoding on the cd to what is expected
2245 th->starting_track =
2246 bcd2bin(th->starting_track);
2247 th->ending_track = bcd2bin(th->ending_track);
2250 if (starting_track == 0)
2251 starting_track = th->starting_track;
2252 else if (starting_track == LEADOUT)
2253 starting_track = th->ending_track + 1;
2254 else if (starting_track < th->starting_track ||
2255 starting_track > th->ending_track + 1) {
2256 kprintf("scsi_cd: error in readtocentries, "
2257 "returning EINVAL\n");
2258 kfree(data, M_TEMP);
2259 kfree(lead, M_TEMP);
2264 /* calculate reading length without leadout entry */
2265 readlen = (th->ending_track - starting_track + 1) *
2266 sizeof(struct cd_toc_entry);
2268 /* and with leadout entry */
2269 len = readlen + sizeof(struct cd_toc_entry);
2270 if (te->data_len < len) {
2275 if (len > sizeof(data->entries)) {
2276 kprintf("scsi_cd: error in readtocentries, "
2277 "returning EINVAL\n");
2279 kfree(data, M_TEMP);
2280 kfree(lead, M_TEMP);
2283 num = len / sizeof(struct cd_toc_entry);
2286 error = cdreadtoc(periph, te->address_format,
2289 readlen + sizeof (*th),
2292 kfree(data, M_TEMP);
2293 kfree(lead, M_TEMP);
2298 /* make leadout entry if needed */
2299 idx = starting_track + num - 1;
2300 if (softc->quirks & CD_Q_BCD_TRACKS)
2301 th->ending_track = bcd2bin(th->ending_track);
2302 if (idx == th->ending_track + 1) {
2303 error = cdreadtoc(periph, te->address_format,
2304 LEADOUT, (u_int8_t *)lead,
2308 kfree(data, M_TEMP);
2309 kfree(lead, M_TEMP);
2312 data->entries[idx - starting_track] =
2315 if (softc->quirks & CD_Q_BCD_TRACKS) {
2316 for (idx = 0; idx < num - 1; idx++) {
2317 data->entries[idx].track =
2318 bcd2bin(data->entries[idx].track);
2322 error = copyout(data->entries, te->data, len);
2323 kfree(data, M_TEMP);
2324 kfree(lead, M_TEMP);
2327 case CDIOREADTOCENTRY:
2329 struct cd_toc_single *data;
2330 struct ioc_read_toc_single_entry *te =
2331 (struct ioc_read_toc_single_entry *) addr;
2332 struct ioc_toc_header *th;
2335 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2336 ("trying to do CDIOREADTOCENTRY\n"));
2338 data = kmalloc(sizeof(*data), M_TEMP, M_WAITOK);
2340 if (te->address_format != CD_MSF_FORMAT
2341 && te->address_format != CD_LBA_FORMAT) {
2342 kprintf("error in readtocentry, "
2343 " returning EINVAL\n");
2344 kfree(data, M_TEMP);
2350 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2351 sizeof (*th), /*sense_flags*/0);
2353 kfree(data, M_TEMP);
2357 if (softc->quirks & CD_Q_BCD_TRACKS) {
2358 /* we are going to have to convert the BCD
2359 * encoding on the cd to what is expected
2361 th->starting_track =
2362 bcd2bin(th->starting_track);
2363 th->ending_track = bcd2bin(th->ending_track);
2367 track = th->starting_track;
2368 else if (track == LEADOUT)
2370 else if (track < th->starting_track ||
2371 track > th->ending_track + 1) {
2372 kprintf("error in readtocentry, "
2373 " returning EINVAL\n");
2374 kfree(data, M_TEMP);
2379 error = cdreadtoc(periph, te->address_format, track,
2380 (u_int8_t *)data, sizeof(*data),
2383 kfree(data, M_TEMP);
2387 if (softc->quirks & CD_Q_BCD_TRACKS)
2388 data->entry.track = bcd2bin(data->entry.track);
2389 bcopy(&data->entry, &te->entry,
2390 sizeof(struct cd_toc_entry));
2391 kfree(data, M_TEMP);
2396 struct ioc_patch *arg = (struct ioc_patch *)addr;
2397 struct cd_mode_params params;
2398 union cd_pages *page;
2400 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2401 ("trying to do CDIOCSETPATCH\n"));
2403 params.alloc_len = sizeof(union cd_mode_data_6_10);
2404 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2406 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2408 kfree(params.mode_buf, M_TEMP);
2411 page = cdgetpage(¶ms);
2413 page->audio.port[LEFT_PORT].channels =
2415 page->audio.port[RIGHT_PORT].channels =
2417 page->audio.port[2].channels = arg->patch[2];
2418 page->audio.port[3].channels = arg->patch[3];
2419 error = cdsetmode(periph, ¶ms);
2420 kfree(params.mode_buf, M_TEMP);
2425 struct ioc_vol *arg = (struct ioc_vol *) addr;
2426 struct cd_mode_params params;
2427 union cd_pages *page;
2429 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2430 ("trying to do CDIOCGETVOL\n"));
2432 params.alloc_len = sizeof(union cd_mode_data_6_10);
2433 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2435 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2437 kfree(params.mode_buf, M_TEMP);
2440 page = cdgetpage(¶ms);
2442 arg->vol[LEFT_PORT] =
2443 page->audio.port[LEFT_PORT].volume;
2444 arg->vol[RIGHT_PORT] =
2445 page->audio.port[RIGHT_PORT].volume;
2446 arg->vol[2] = page->audio.port[2].volume;
2447 arg->vol[3] = page->audio.port[3].volume;
2448 kfree(params.mode_buf, M_TEMP);
2453 struct ioc_vol *arg = (struct ioc_vol *) addr;
2454 struct cd_mode_params params;
2455 union cd_pages *page;
2457 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2458 ("trying to do CDIOCSETVOL\n"));
2460 params.alloc_len = sizeof(union cd_mode_data_6_10);
2461 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2463 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2465 kfree(params.mode_buf, M_TEMP);
2468 page = cdgetpage(¶ms);
2470 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2471 page->audio.port[LEFT_PORT].volume =
2472 arg->vol[LEFT_PORT];
2473 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2474 page->audio.port[RIGHT_PORT].volume =
2475 arg->vol[RIGHT_PORT];
2476 page->audio.port[2].volume = arg->vol[2];
2477 page->audio.port[3].volume = arg->vol[3];
2478 error = cdsetmode(periph, ¶ms);
2479 kfree(params.mode_buf, M_TEMP);
2484 struct cd_mode_params params;
2485 union cd_pages *page;
2487 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2488 ("trying to do CDIOCSETMONO\n"));
2490 params.alloc_len = sizeof(union cd_mode_data_6_10);
2491 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2493 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2495 kfree(params.mode_buf, M_TEMP);
2498 page = cdgetpage(¶ms);
2500 page->audio.port[LEFT_PORT].channels =
2501 LEFT_CHANNEL | RIGHT_CHANNEL;
2502 page->audio.port[RIGHT_PORT].channels =
2503 LEFT_CHANNEL | RIGHT_CHANNEL;
2504 page->audio.port[2].channels = 0;
2505 page->audio.port[3].channels = 0;
2506 error = cdsetmode(periph, ¶ms);
2507 kfree(params.mode_buf, M_TEMP);
2510 case CDIOCSETSTEREO:
2512 struct cd_mode_params params;
2513 union cd_pages *page;
2515 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2516 ("trying to do CDIOCSETSTEREO\n"));
2518 params.alloc_len = sizeof(union cd_mode_data_6_10);
2519 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2521 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2523 kfree(params.mode_buf, M_TEMP);
2526 page = cdgetpage(¶ms);
2528 page->audio.port[LEFT_PORT].channels =
2530 page->audio.port[RIGHT_PORT].channels =
2532 page->audio.port[2].channels = 0;
2533 page->audio.port[3].channels = 0;
2534 error = cdsetmode(periph, ¶ms);
2535 kfree(params.mode_buf, M_TEMP);
2540 struct cd_mode_params params;
2541 union cd_pages *page;
2543 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2544 ("trying to do CDIOCSETMUTE\n"));
2546 params.alloc_len = sizeof(union cd_mode_data_6_10);
2547 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2549 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2551 kfree(¶ms, M_TEMP);
2554 page = cdgetpage(¶ms);
2556 page->audio.port[LEFT_PORT].channels = 0;
2557 page->audio.port[RIGHT_PORT].channels = 0;
2558 page->audio.port[2].channels = 0;
2559 page->audio.port[3].channels = 0;
2560 error = cdsetmode(periph, ¶ms);
2561 kfree(params.mode_buf, M_TEMP);
2566 struct cd_mode_params params;
2567 union cd_pages *page;
2569 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2570 ("trying to do CDIOCSETLEFT\n"));
2572 params.alloc_len = sizeof(union cd_mode_data_6_10);
2573 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2576 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2578 kfree(params.mode_buf, M_TEMP);
2581 page = cdgetpage(¶ms);
2583 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2584 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2585 page->audio.port[2].channels = 0;
2586 page->audio.port[3].channels = 0;
2587 error = cdsetmode(periph, ¶ms);
2588 kfree(params.mode_buf, M_TEMP);
2593 struct cd_mode_params params;
2594 union cd_pages *page;
2596 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2597 ("trying to do CDIOCSETRIGHT\n"));
2599 params.alloc_len = sizeof(union cd_mode_data_6_10);
2600 params.mode_buf = kmalloc(params.alloc_len, M_TEMP,
2603 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2605 kfree(params.mode_buf, M_TEMP);
2608 page = cdgetpage(¶ms);
2610 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2611 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2612 page->audio.port[2].channels = 0;
2613 page->audio.port[3].channels = 0;
2614 error = cdsetmode(periph, ¶ms);
2615 kfree(params.mode_buf, M_TEMP);
2619 error = cdpause(periph, 1);
2622 error = cdpause(periph, 0);
2625 error = cdstartunit(periph, 0);
2628 error = cdstartunit(periph, 1);
2635 * The user successfully closed the tray, run
2636 * cdcheckmedia() again so we can re-sync the disklabel
2639 cdcheckmedia(periph);
2643 error = cdstopunit(periph, 0);
2646 error = cdstopunit(periph, 1);
2653 * Since we've successfully ejected the media, run
2654 * cdcheckmedia() again so we re-sync the disklabel
2657 cdcheckmedia(periph);
2661 cdprevent(periph, PR_ALLOW);
2664 cdprevent(periph, PR_PREVENT);
2667 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2671 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2675 /* return (cd_reset(periph)); */
2678 case CDRIOCREADSPEED:
2679 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2681 case CDRIOCWRITESPEED:
2682 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2685 case DVDIOCREPORTKEY: {
2686 struct dvd_authinfo *authinfo;
2688 authinfo = (struct dvd_authinfo *)addr;
2690 if (ap->a_cmd == DVDIOCREPORTKEY)
2691 error = cdreportkey(periph, authinfo);
2693 error = cdsendkey(periph, authinfo);
2696 case DVDIOCREADSTRUCTURE: {
2697 struct dvd_struct *dvdstruct;
2699 dvdstruct = (struct dvd_struct *)addr;
2701 error = cdreaddvdstructure(periph, dvdstruct);
2706 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2710 cam_periph_unlock(periph);
2712 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2718 cdprevent(struct cam_periph *periph, int action)
2721 struct cd_softc *softc;
2724 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2726 softc = (struct cd_softc *)periph->softc;
2728 if (((action == PR_ALLOW)
2729 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2730 || ((action == PR_PREVENT)
2731 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2735 ccb = cdgetccb(periph, /* priority */ 1);
2737 scsi_prevent(&ccb->csio,
2743 /* timeout */60000);
2745 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2746 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2748 xpt_release_ccb(ccb);
2751 if (action == PR_ALLOW)
2752 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2754 softc->flags |= CD_FLAG_DISC_LOCKED;
2759 cdcheckmedia(struct cam_periph *periph)
2761 struct cd_softc *softc;
2762 struct ioc_toc_header *toch;
2763 struct cd_toc_single leadout;
2764 struct ccb_getdev cgd;
2765 u_int32_t size, toclen;
2766 int error, num_entries, cdindex;
2767 int first_track_audio;
2768 struct disk_info info;
2770 softc = (struct cd_softc *)periph->softc;
2772 first_track_audio = -1;
2775 cdprevent(periph, PR_PREVENT);
2778 * Set up disk info fields and tell the disk subsystem to reset
2779 * its internal copy of the label.
2781 bzero(&info, sizeof(info));
2782 info.d_type = DTYPE_SCSI;
2783 info.d_dsflags &= ~DSO_COMPATLABEL;
2784 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2787 * Grab the inquiry data to get the vendor and product names.
2788 * Put them in the typename and packname for the label.
2790 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2791 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2792 xpt_action((union ccb *)&cgd);
2795 strncpy(label->d_typename, cgd.inq_data.vendor,
2796 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2797 strncpy(label->d_packname, cgd.inq_data.product,
2798 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2801 * Clear the valid media and TOC flags until we've verified that we
2804 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2807 * Get the disc size and block size. If we can't get it, we don't
2808 * have media, most likely.
2810 if ((error = cdsize(periph, &size)) != 0) {
2812 * Set a bogus sector size, so the slice code won't try to
2813 * divide by 0 and panic the kernel.
2815 info.d_media_blksize = 2048;
2816 disk_setdiskinfo(&softc->disk, &info);
2819 * XXX KDM is this a good idea? Seems to cause more
2822 if (softc->flags & CD_FLAG_OPEN) {
2828 * We don't bother checking the return value here,
2829 * since we already have an error...
2831 dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2832 /*data*/(caddr_t)&force, /*flags*/ 0,
2833 &softc->disk.d_slice, &softc->disk.d_info);
2837 * Tell devstat(9) we don't have a blocksize.
2839 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2841 cdprevent(periph, PR_ALLOW);
2845 info.d_media_blksize = softc->params.blksize;
2846 info.d_media_blocks = softc->params.disksize;
2847 disk_setdiskinfo(&softc->disk, &info);
2850 * Force a re-sync of slice information, like the blocksize,
2851 * now that we know it. It isn't pretty...but according to
2852 * Bruce Evans, this is probably the best way to do it in
2853 * -stable. We only do this if we're already open, and
2854 * therefore dsopen() has already run. If CD_FLAG_OPEN
2855 * isn't set, this isn't necessary.
2857 if (softc->flags & CD_FLAG_OPEN) {
2862 error = dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2863 /*data*/(caddr_t)&force, /*flags*/ 0,
2864 &softc->disk.d_slice,
2865 &softc->disk.d_info);
2868 * Set a bogus sector size, so the slice code
2869 * won't try to divide by 0 and panic the
2872 info.d_media_blksize = 2048;
2873 info.d_media_blocks = 0;
2874 info.d_media_size = 0;
2875 disk_setdiskinfo(&softc->disk, &info);
2878 * Tell devstat(9) we don't have a blocksize.
2880 softc->device_stats.flags |=
2881 DEVSTAT_BS_UNAVAILABLE;
2883 cdprevent(periph, PR_ALLOW);
2888 * We unconditionally (re)set the blocksize each time the
2889 * CD device is opened. This is because the CD can change,
2890 * and therefore the blocksize might change.
2891 * XXX problems here if some slice or partition is still
2892 * open with the old size?
2894 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2895 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2896 softc->device_stats.block_size = softc->params.blksize;
2898 softc->flags |= CD_FLAG_VALID_MEDIA;
2902 * Now we check the table of contents. This (currently) is only
2903 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2904 * things like present a separate entry in /dev for each track,
2905 * like that acd(4) driver does.
2907 bzero(&softc->toc, sizeof(softc->toc));
2908 toch = &softc->toc.header;
2911 * We will get errors here for media that doesn't have a table of
2912 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2913 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2914 * has not been recorded (no complete session) and the Format codes
2915 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2916 * with an INVALID FIELD IN CDB. Devices that are not capable of
2917 * reading an incomplete session on DDC/CD-R/RW media shall report
2918 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2920 * So this isn't fatal if we can't read the table of contents, it
2921 * just means that the user won't be able to issue the play tracks
2922 * ioctl, and likely lots of other stuff won't work either. They
2923 * need to burn the CD before we can do a whole lot with it. So
2924 * we don't print anything here if we get an error back.
2926 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2929 * Errors in reading the table of contents aren't fatal, we just
2930 * won't have a valid table of contents cached.
2934 bzero(&softc->toc, sizeof(softc->toc));
2938 if (softc->quirks & CD_Q_BCD_TRACKS) {
2939 toch->starting_track = bcd2bin(toch->starting_track);
2940 toch->ending_track = bcd2bin(toch->ending_track);
2943 /* Number of TOC entries, plus leadout */
2944 num_entries = (toch->ending_track - toch->starting_track) + 2;
2946 if (num_entries <= 0)
2949 toclen = num_entries * sizeof(struct cd_toc_entry);
2951 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2952 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2956 bzero(&softc->toc, sizeof(softc->toc));
2960 if (softc->quirks & CD_Q_BCD_TRACKS) {
2961 toch->starting_track = bcd2bin(toch->starting_track);
2962 toch->ending_track = bcd2bin(toch->ending_track);
2964 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2967 * XXX KDM is this necessary? Probably only if the drive doesn't
2968 * return leadout information with the table of contents.
2970 cdindex = toch->starting_track + num_entries -1;
2971 if (cdindex == toch->ending_track + 1) {
2973 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2974 (u_int8_t *)&leadout, sizeof(leadout),
2980 softc->toc.entries[cdindex - toch->starting_track] =
2983 if (softc->quirks & CD_Q_BCD_TRACKS) {
2984 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2985 softc->toc.entries[cdindex].track =
2986 bcd2bin(softc->toc.entries[cdindex].track);
2991 * Run through the TOC entries, find the first entry and determine
2992 * whether it is an audio or data track.
2994 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2995 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2996 if (softc->toc.entries[cdindex].control & 0x04)
2997 first_track_audio = 0;
2999 first_track_audio = 1;
3005 * If first_track_audio is non-zero, we either have an error (e.g.
3006 * couldn't find the starting track) or the first track is an audio
3007 * track. If first_track_audio is 0, the first track is a data
3008 * track that could have a disklabel. Attempt to read the
3009 * disklabel off the media, just in case the user put one there.
3011 if (first_track_audio == 0) {
3012 info.d_dsflags |= DSO_COMPATLABEL;
3013 info.d_dsflags &= ~DSO_NOLABELS;
3014 disk_setdiskinfo(&softc->disk, &info);
3016 softc->flags |= CD_FLAG_VALID_TOC;
3023 cdsize(struct cam_periph *periph, u_int32_t *size)
3025 struct cd_softc *softc;
3027 struct scsi_read_capacity_data *rcap_buf;
3030 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3032 softc = (struct cd_softc *)periph->softc;
3034 ccb = cdgetccb(periph, /* priority */ 1);
3036 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
3037 M_TEMP, M_INTWAIT | M_ZERO);
3039 scsi_read_capacity(&ccb->csio,
3045 /* timeout */20000);
3047 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3048 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3050 xpt_release_ccb(ccb);
3052 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3053 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3055 * SCSI-3 mandates that the reported blocksize shall be 2048.
3056 * Older drives sometimes report funny values, trim it down to
3057 * 2048, or other parts of the kernel will get confused.
3059 * XXX we leave drives alone that might report 512 bytes, as
3060 * well as drives reporting more weird sizes like perhaps 4K.
3062 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3063 softc->params.blksize = 2048;
3065 kfree(rcap_buf, M_TEMP);
3066 *size = softc->params.disksize;
3073 cd6byteworkaround(union ccb *ccb)
3076 struct cam_periph *periph;
3077 struct cd_softc *softc;
3078 struct cd_mode_params *params;
3081 periph = xpt_path_periph(ccb->ccb_h.path);
3082 softc = (struct cd_softc *)periph->softc;
3084 cdb = ccb->csio.cdb_io.cdb_bytes;
3086 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3087 || ((cdb[0] != MODE_SENSE_6)
3088 && (cdb[0] != MODE_SELECT_6)))
3092 * Because there is no convenient place to stash the overall
3093 * cd_mode_params structure pointer, we have to grab it like this.
3094 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3095 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3097 * XXX It would be nice if, at some point, we could increase the
3098 * number of available peripheral private pointers. Both pointers
3099 * are currently used in most every peripheral driver.
3103 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3104 if (params->mode_buf == ccb->csio.data_ptr) {
3111 * This shouldn't happen. All mode sense and mode select
3112 * operations in the cd(4) driver MUST go through cdgetmode() and
3116 xpt_print_path(periph->path);
3117 kprintf("mode buffer not found in mode queue!\n");
3121 params->cdb_size = 10;
3122 softc->minimum_command_size = 10;
3123 xpt_print_path(ccb->ccb_h.path);
3124 kprintf("%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3125 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3127 if (cdb[0] == MODE_SENSE_6) {
3128 struct scsi_mode_sense_10 ms10;
3129 struct scsi_mode_sense_6 *ms6;
3132 ms6 = (struct scsi_mode_sense_6 *)cdb;
3134 bzero(&ms10, sizeof(ms10));
3135 ms10.opcode = MODE_SENSE_10;
3136 ms10.byte2 = ms6->byte2;
3137 ms10.page = ms6->page;
3140 * 10 byte mode header, block descriptor,
3141 * sizeof(union cd_pages)
3143 len = sizeof(struct cd_mode_data_10);
3144 ccb->csio.dxfer_len = len;
3146 scsi_ulto2b(len, ms10.length);
3147 ms10.control = ms6->control;
3148 bcopy(&ms10, cdb, 10);
3149 ccb->csio.cdb_len = 10;
3151 struct scsi_mode_select_10 ms10;
3152 struct scsi_mode_select_6 *ms6;
3153 struct scsi_mode_header_6 *header6;
3154 struct scsi_mode_header_10 *header10;
3155 struct scsi_mode_page_header *page_header;
3156 int blk_desc_len, page_num, page_size, len;
3158 ms6 = (struct scsi_mode_select_6 *)cdb;
3160 bzero(&ms10, sizeof(ms10));
3161 ms10.opcode = MODE_SELECT_10;
3162 ms10.byte2 = ms6->byte2;
3164 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3165 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3167 page_header = find_mode_page_6(header6);
3168 page_num = page_header->page_code;
3170 blk_desc_len = header6->blk_desc_len;
3172 page_size = cdgetpagesize(page_num);
3174 if (page_size != (page_header->page_length +
3175 sizeof(*page_header)))
3176 page_size = page_header->page_length +
3177 sizeof(*page_header);
3179 len = sizeof(*header10) + blk_desc_len + page_size;
3181 len = min(params->alloc_len, len);
3184 * Since the 6 byte parameter header is shorter than the 10
3185 * byte parameter header, we need to copy the actual mode
3186 * page data, and the block descriptor, if any, so things wind
3187 * up in the right place. The regions will overlap, but
3188 * bcopy() does the right thing.
3190 bcopy(params->mode_buf + sizeof(*header6),
3191 params->mode_buf + sizeof(*header10),
3192 len - sizeof(*header10));
3194 /* Make sure these fields are set correctly. */
3195 scsi_ulto2b(0, header10->data_length);
3196 header10->medium_type = 0;
3197 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3199 ccb->csio.dxfer_len = len;
3201 scsi_ulto2b(len, ms10.length);
3202 ms10.control = ms6->control;
3203 bcopy(&ms10, cdb, 10);
3204 ccb->csio.cdb_len = 10;
3207 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3208 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3211 cam_release_devq(ccb->ccb_h.path,
3215 /*getcount_only*/0);
3222 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3224 struct cd_softc *softc;
3225 struct cam_periph *periph;
3228 periph = xpt_path_periph(ccb->ccb_h.path);
3229 softc = (struct cd_softc *)periph->softc;
3234 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3235 * CDB comes back with this particular error, try transforming it
3236 * into the 10 byte version.
3238 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3239 error = cd6byteworkaround(ccb);
3240 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3241 CAM_SCSI_STATUS_ERROR)
3242 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3243 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3244 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3245 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3246 int sense_key, error_code, asc, ascq;
3248 scsi_extract_sense(&ccb->csio.sense_data,
3249 &error_code, &sense_key, &asc, &ascq);
3250 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3251 error = cd6byteworkaround(ccb);
3254 if (error == ERESTART)
3259 * Until we have a better way of doing pack validation,
3260 * don't treat UAs as errors.
3262 sense_flags |= SF_RETRY_UA;
3263 return (cam_periph_error(ccb, cam_flags, sense_flags,
3264 &softc->saved_ccb));
3268 * Read table of contents
3271 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3272 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3274 struct scsi_read_toc *scsi_cmd;
3276 struct ccb_scsiio *csio;
3283 ccb = cdgetccb(periph, /* priority */ 1);
3289 /* cbfcnp */ cddone,
3290 /* flags */ CAM_DIR_IN,
3291 /* tag_action */ MSG_SIMPLE_Q_TAG,
3292 /* data_ptr */ data,
3293 /* dxfer_len */ len,
3294 /* sense_len */ SSD_FULL_SIZE,
3295 sizeof(struct scsi_read_toc),
3296 /* timeout */ 50000);
3298 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3299 bzero (scsi_cmd, sizeof(*scsi_cmd));
3301 if (mode == CD_MSF_FORMAT)
3302 scsi_cmd->byte2 |= CD_MSF;
3303 scsi_cmd->from_track = start;
3304 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3305 scsi_cmd->data_len[0] = (ntoc) >> 8;
3306 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3308 scsi_cmd->op_code = READ_TOC;
3310 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3311 /*sense_flags*/SF_RETRY_UA | sense_flags);
3313 xpt_release_ccb(ccb);
3319 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3320 u_int32_t format, int track,
3321 struct cd_sub_channel_info *data, u_int32_t len)
3323 struct scsi_read_subchannel *scsi_cmd;
3324 struct ccb_scsiio *csio;
3330 ccb = cdgetccb(periph, /* priority */ 1);
3336 /* cbfcnp */ cddone,
3337 /* flags */ CAM_DIR_IN,
3338 /* tag_action */ MSG_SIMPLE_Q_TAG,
3339 /* data_ptr */ (u_int8_t *)data,
3340 /* dxfer_len */ len,
3341 /* sense_len */ SSD_FULL_SIZE,
3342 sizeof(struct scsi_read_subchannel),
3343 /* timeout */ 50000);
3345 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3346 bzero (scsi_cmd, sizeof(*scsi_cmd));
3348 scsi_cmd->op_code = READ_SUBCHANNEL;
3349 if (mode == CD_MSF_FORMAT)
3350 scsi_cmd->byte1 |= CD_MSF;
3351 scsi_cmd->byte2 = SRS_SUBQ;
3352 scsi_cmd->subchan_format = format;
3353 scsi_cmd->track = track;
3354 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3355 scsi_cmd->control = 0;
3357 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3358 /*sense_flags*/SF_RETRY_UA);
3360 xpt_release_ccb(ccb);
3366 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3367 * routine. See comments in cd6byteworkaround() for details.
3370 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3373 struct ccb_scsiio *csio;
3374 struct cd_softc *softc;
3379 softc = (struct cd_softc *)periph->softc;
3381 ccb = cdgetccb(periph, /* priority */ 1);
3385 data->cdb_size = softc->minimum_command_size;
3386 if (data->cdb_size < 10)
3387 param_len = sizeof(struct cd_mode_data);
3389 param_len = sizeof(struct cd_mode_data_10);
3391 /* Don't say we've got more room than we actually allocated */
3392 param_len = min(param_len, data->alloc_len);
3394 scsi_mode_sense_len(csio,
3396 /* cbfcnp */ cddone,
3397 /* tag_action */ MSG_SIMPLE_Q_TAG,
3399 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3401 /* param_buf */ data->mode_buf,
3402 /* param_len */ param_len,
3403 /* minimum_cmd_size */ softc->minimum_command_size,
3404 /* sense_len */ SSD_FULL_SIZE,
3405 /* timeout */ 50000);
3408 * It would be nice not to have to do this, but there's no
3409 * available pointer in the CCB that would allow us to stuff the
3410 * mode params structure in there and retrieve it in
3411 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3412 * lets the caller know what CDB size we ended up using, so they
3413 * can find the actual mode page offset.
3415 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3417 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3418 /*sense_flags*/SF_RETRY_UA);
3420 xpt_release_ccb(ccb);
3422 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3425 * This is a bit of belt-and-suspenders checking, but if we run
3426 * into a situation where the target sends back multiple block
3427 * descriptors, we might not have enough space in the buffer to
3428 * see the whole mode page. Better to return an error than
3429 * potentially access memory beyond our malloced region.
3434 if (data->cdb_size == 10) {
3435 struct scsi_mode_header_10 *hdr10;
3437 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3438 data_len = scsi_2btoul(hdr10->data_length);
3439 data_len += sizeof(hdr10->data_length);
3441 struct scsi_mode_header_6 *hdr6;
3443 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3444 data_len = hdr6->data_length;
3445 data_len += sizeof(hdr6->data_length);
3449 * Complain if there is more mode data available than we
3450 * allocated space for. This could potentially happen if
3451 * we miscalculated the page length for some reason, if the
3452 * drive returns multiple block descriptors, or if it sets
3453 * the data length incorrectly.
3455 if (data_len > data->alloc_len) {
3456 xpt_print_path(periph->path);
3457 kprintf("allocated modepage %d length %d < returned "
3458 "length %d\n", page, data->alloc_len, data_len);
3467 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3468 * routine. See comments in cd6byteworkaround() for details.
3471 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3473 struct ccb_scsiio *csio;
3474 struct cd_softc *softc;
3476 int cdb_size, param_len;
3479 softc = (struct cd_softc *)periph->softc;
3481 ccb = cdgetccb(periph, /* priority */ 1);
3488 * If the data is formatted for the 10 byte version of the mode
3489 * select parameter list, we need to use the 10 byte CDB.
3490 * Otherwise, we use whatever the stored minimum command size.
3492 if (data->cdb_size == 10)
3493 cdb_size = data->cdb_size;
3495 cdb_size = softc->minimum_command_size;
3497 if (cdb_size >= 10) {
3498 struct scsi_mode_header_10 *mode_header;
3501 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3503 data_len = scsi_2btoul(mode_header->data_length);
3505 scsi_ulto2b(0, mode_header->data_length);
3507 * SONY drives do not allow a mode select with a medium_type
3508 * value that has just been returned by a mode sense; use a
3509 * medium_type of 0 (Default) instead.
3511 mode_header->medium_type = 0;
3514 * Pass back whatever the drive passed to us, plus the size
3515 * of the data length field.
3517 param_len = data_len + sizeof(mode_header->data_length);
3520 struct scsi_mode_header_6 *mode_header;
3522 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3524 param_len = mode_header->data_length + 1;
3526 mode_header->data_length = 0;
3528 * SONY drives do not allow a mode select with a medium_type
3529 * value that has just been returned by a mode sense; use a
3530 * medium_type of 0 (Default) instead.
3532 mode_header->medium_type = 0;
3535 /* Don't say we've got more room than we actually allocated */
3536 param_len = min(param_len, data->alloc_len);
3538 scsi_mode_select_len(csio,
3540 /* cbfcnp */ cddone,
3541 /* tag_action */ MSG_SIMPLE_Q_TAG,
3542 /* scsi_page_fmt */ 1,
3544 /* param_buf */ data->mode_buf,
3545 /* param_len */ param_len,
3546 /* minimum_cmd_size */ cdb_size,
3547 /* sense_len */ SSD_FULL_SIZE,
3548 /* timeout */ 50000);
3550 /* See comments in cdgetmode() and cd6byteworkaround(). */
3551 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3553 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3554 /*sense_flags*/SF_RETRY_UA);
3556 xpt_release_ccb(ccb);
3558 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3566 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3568 struct ccb_scsiio *csio;
3574 ccb = cdgetccb(periph, /* priority */ 1);
3577 * Use the smallest possible command to perform the operation.
3579 if ((len & 0xffff0000) == 0) {
3581 * We can fit in a 10 byte cdb.
3583 struct scsi_play_10 *scsi_cmd;
3585 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3586 bzero (scsi_cmd, sizeof(*scsi_cmd));
3587 scsi_cmd->op_code = PLAY_10;
3588 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3589 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3590 cdb_len = sizeof(*scsi_cmd);
3592 struct scsi_play_12 *scsi_cmd;
3594 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3595 bzero (scsi_cmd, sizeof(*scsi_cmd));
3596 scsi_cmd->op_code = PLAY_12;
3597 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3598 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3599 cdb_len = sizeof(*scsi_cmd);
3604 /*flags*/CAM_DIR_NONE,
3608 /*sense_len*/SSD_FULL_SIZE,
3610 /*timeout*/50 * 1000);
3612 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3613 /*sense_flags*/SF_RETRY_UA);
3615 xpt_release_ccb(ccb);
3621 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3622 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3624 struct scsi_play_msf *scsi_cmd;
3625 struct ccb_scsiio *csio;
3631 ccb = cdgetccb(periph, /* priority */ 1);
3637 /* cbfcnp */ cddone,
3638 /* flags */ CAM_DIR_NONE,
3639 /* tag_action */ MSG_SIMPLE_Q_TAG,
3640 /* data_ptr */ NULL,
3642 /* sense_len */ SSD_FULL_SIZE,
3643 sizeof(struct scsi_play_msf),
3644 /* timeout */ 50000);
3646 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3647 bzero (scsi_cmd, sizeof(*scsi_cmd));
3649 scsi_cmd->op_code = PLAY_MSF;
3650 scsi_cmd->start_m = startm;
3651 scsi_cmd->start_s = starts;
3652 scsi_cmd->start_f = startf;
3653 scsi_cmd->end_m = endm;
3654 scsi_cmd->end_s = ends;
3655 scsi_cmd->end_f = endf;
3657 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3658 /*sense_flags*/SF_RETRY_UA);
3660 xpt_release_ccb(ccb);
3667 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3668 u_int32_t etrack, u_int32_t eindex)
3670 struct scsi_play_track *scsi_cmd;
3671 struct ccb_scsiio *csio;
3677 ccb = cdgetccb(periph, /* priority */ 1);
3683 /* cbfcnp */ cddone,
3684 /* flags */ CAM_DIR_NONE,
3685 /* tag_action */ MSG_SIMPLE_Q_TAG,
3686 /* data_ptr */ NULL,
3688 /* sense_len */ SSD_FULL_SIZE,
3689 sizeof(struct scsi_play_track),
3690 /* timeout */ 50000);
3692 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3693 bzero (scsi_cmd, sizeof(*scsi_cmd));
3695 scsi_cmd->op_code = PLAY_TRACK;
3696 scsi_cmd->start_track = strack;
3697 scsi_cmd->start_index = sindex;
3698 scsi_cmd->end_track = etrack;
3699 scsi_cmd->end_index = eindex;
3701 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3702 /*sense_flags*/SF_RETRY_UA);
3704 xpt_release_ccb(ccb);
3710 cdpause(struct cam_periph *periph, u_int32_t go)
3712 struct scsi_pause *scsi_cmd;
3713 struct ccb_scsiio *csio;
3719 ccb = cdgetccb(periph, /* priority */ 1);
3725 /* cbfcnp */ cddone,
3726 /* flags */ CAM_DIR_NONE,
3727 /* tag_action */ MSG_SIMPLE_Q_TAG,
3728 /* data_ptr */ NULL,
3730 /* sense_len */ SSD_FULL_SIZE,
3731 sizeof(struct scsi_pause),
3732 /* timeout */ 50000);
3734 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3735 bzero (scsi_cmd, sizeof(*scsi_cmd));
3737 scsi_cmd->op_code = PAUSE;
3738 scsi_cmd->resume = go;
3740 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3741 /*sense_flags*/SF_RETRY_UA);
3743 xpt_release_ccb(ccb);
3749 cdstartunit(struct cam_periph *periph, int load)
3756 ccb = cdgetccb(periph, /* priority */ 1);
3758 scsi_start_stop(&ccb->csio,
3760 /* cbfcnp */ cddone,
3761 /* tag_action */ MSG_SIMPLE_Q_TAG,
3763 /* load_eject */ load,
3764 /* immediate */ FALSE,
3765 /* sense_len */ SSD_FULL_SIZE,
3766 /* timeout */ 50000);
3768 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3769 /*sense_flags*/SF_RETRY_UA);
3771 xpt_release_ccb(ccb);
3777 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3784 ccb = cdgetccb(periph, /* priority */ 1);
3786 scsi_start_stop(&ccb->csio,
3788 /* cbfcnp */ cddone,
3789 /* tag_action */ MSG_SIMPLE_Q_TAG,
3791 /* load_eject */ eject,
3792 /* immediate */ FALSE,
3793 /* sense_len */ SSD_FULL_SIZE,
3794 /* timeout */ 50000);
3796 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3797 /*sense_flags*/SF_RETRY_UA);
3799 xpt_release_ccb(ccb);
3805 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3807 struct scsi_set_speed *scsi_cmd;
3808 struct ccb_scsiio *csio;
3813 ccb = cdgetccb(periph, /* priority */ 1);
3816 /* Preserve old behavior: units in multiples of CDROM speed */
3824 /* cbfcnp */ cddone,
3825 /* flags */ CAM_DIR_NONE,
3826 /* tag_action */ MSG_SIMPLE_Q_TAG,
3827 /* data_ptr */ NULL,
3829 /* sense_len */ SSD_FULL_SIZE,
3830 sizeof(struct scsi_set_speed),
3831 /* timeout */ 50000);
3833 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3834 bzero(scsi_cmd, sizeof(*scsi_cmd));
3836 scsi_cmd->opcode = SET_CD_SPEED;
3837 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3838 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3840 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3841 /*sense_flags*/SF_RETRY_UA);
3843 xpt_release_ccb(ccb);
3849 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3861 ccb = cdgetccb(periph, /* priority */ 1);
3863 switch (authinfo->format) {
3864 case DVD_REPORT_AGID:
3865 length = sizeof(struct scsi_report_key_data_agid);
3867 case DVD_REPORT_CHALLENGE:
3868 length = sizeof(struct scsi_report_key_data_challenge);
3870 case DVD_REPORT_KEY1:
3871 length = sizeof(struct scsi_report_key_data_key1_key2);
3873 case DVD_REPORT_TITLE_KEY:
3874 length = sizeof(struct scsi_report_key_data_title);
3875 /* The lba field is only set for the title key */
3876 lba = authinfo->lba;
3878 case DVD_REPORT_ASF:
3879 length = sizeof(struct scsi_report_key_data_asf);
3881 case DVD_REPORT_RPC:
3882 length = sizeof(struct scsi_report_key_data_rpc);
3884 case DVD_INVALIDATE_AGID:
3890 break; /* NOTREACHED */
3894 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3900 scsi_report_key(&ccb->csio,
3902 /* cbfcnp */ cddone,
3903 /* tag_action */ MSG_SIMPLE_Q_TAG,
3905 /* agid */ authinfo->agid,
3906 /* key_format */ authinfo->format,
3907 /* data_ptr */ databuf,
3908 /* dxfer_len */ length,
3909 /* sense_len */ SSD_FULL_SIZE,
3910 /* timeout */ 50000);
3912 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3913 /*sense_flags*/SF_RETRY_UA);
3918 if (ccb->csio.resid != 0) {
3919 xpt_print_path(periph->path);
3920 kprintf("warning, residual for report key command is %d\n",
3924 switch(authinfo->format) {
3925 case DVD_REPORT_AGID: {
3926 struct scsi_report_key_data_agid *agid_data;
3928 agid_data = (struct scsi_report_key_data_agid *)databuf;
3930 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3934 case DVD_REPORT_CHALLENGE: {
3935 struct scsi_report_key_data_challenge *chal_data;
3937 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3939 bcopy(chal_data->challenge_key, authinfo->keychal,
3940 min(sizeof(chal_data->challenge_key),
3941 sizeof(authinfo->keychal)));
3944 case DVD_REPORT_KEY1: {
3945 struct scsi_report_key_data_key1_key2 *key1_data;
3947 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3949 bcopy(key1_data->key1, authinfo->keychal,
3950 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3953 case DVD_REPORT_TITLE_KEY: {
3954 struct scsi_report_key_data_title *title_data;
3956 title_data = (struct scsi_report_key_data_title *)databuf;
3958 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3959 RKD_TITLE_CPM_SHIFT;
3960 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3961 RKD_TITLE_CP_SEC_SHIFT;
3962 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3963 RKD_TITLE_CMGS_SHIFT;
3964 bcopy(title_data->title_key, authinfo->keychal,
3965 min(sizeof(title_data->title_key),
3966 sizeof(authinfo->keychal)));
3969 case DVD_REPORT_ASF: {
3970 struct scsi_report_key_data_asf *asf_data;
3972 asf_data = (struct scsi_report_key_data_asf *)databuf;
3974 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3977 case DVD_REPORT_RPC: {
3978 struct scsi_report_key_data_rpc *rpc_data;
3980 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3982 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3984 authinfo->vend_rsts =
3985 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3986 RKD_RPC_VENDOR_RESET_SHIFT;
3987 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3988 authinfo->region = rpc_data->region_mask;
3989 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3992 case DVD_INVALIDATE_AGID:
3995 /* This should be impossible, since we checked above */
3998 break; /* NOTREACHED */
4001 if (databuf != NULL)
4002 kfree(databuf, M_DEVBUF);
4004 xpt_release_ccb(ccb);
4010 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4020 ccb = cdgetccb(periph, /* priority */ 1);
4022 switch(authinfo->format) {
4023 case DVD_SEND_CHALLENGE: {
4024 struct scsi_report_key_data_challenge *challenge_data;
4026 length = sizeof(*challenge_data);
4028 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4030 databuf = (u_int8_t *)challenge_data;
4032 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4033 challenge_data->data_len);
4035 bcopy(authinfo->keychal, challenge_data->challenge_key,
4036 min(sizeof(authinfo->keychal),
4037 sizeof(challenge_data->challenge_key)));
4040 case DVD_SEND_KEY2: {
4041 struct scsi_report_key_data_key1_key2 *key2_data;
4043 length = sizeof(*key2_data);
4045 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4047 databuf = (u_int8_t *)key2_data;
4049 scsi_ulto2b(length - sizeof(key2_data->data_len),
4050 key2_data->data_len);
4052 bcopy(authinfo->keychal, key2_data->key1,
4053 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4057 case DVD_SEND_RPC: {
4058 struct scsi_send_key_data_rpc *rpc_data;
4060 length = sizeof(*rpc_data);
4062 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4064 databuf = (u_int8_t *)rpc_data;
4066 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4067 rpc_data->data_len);
4069 rpc_data->region_code = authinfo->region;
4075 break; /* NOTREACHED */
4078 scsi_send_key(&ccb->csio,
4080 /* cbfcnp */ cddone,
4081 /* tag_action */ MSG_SIMPLE_Q_TAG,
4082 /* agid */ authinfo->agid,
4083 /* key_format */ authinfo->format,
4084 /* data_ptr */ databuf,
4085 /* dxfer_len */ length,
4086 /* sense_len */ SSD_FULL_SIZE,
4087 /* timeout */ 50000);
4089 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4090 /*sense_flags*/SF_RETRY_UA);
4094 if (databuf != NULL)
4095 kfree(databuf, M_DEVBUF);
4097 xpt_release_ccb(ccb);
4103 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4113 /* The address is reserved for many of the formats */
4116 ccb = cdgetccb(periph, /* priority */ 1);
4118 switch(dvdstruct->format) {
4119 case DVD_STRUCT_PHYSICAL:
4120 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4122 case DVD_STRUCT_COPYRIGHT:
4123 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4125 case DVD_STRUCT_DISCKEY:
4126 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4128 case DVD_STRUCT_BCA:
4129 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4131 case DVD_STRUCT_MANUFACT:
4132 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4134 case DVD_STRUCT_CMI:
4138 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4139 address = dvdstruct->address;
4141 break; /* NOTREACHED */
4142 case DVD_STRUCT_PROTDISCID:
4143 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4145 case DVD_STRUCT_DISCKEYBLOCK:
4146 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4148 case DVD_STRUCT_DDS:
4149 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4151 case DVD_STRUCT_MEDIUM_STAT:
4152 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4154 case DVD_STRUCT_SPARE_AREA:
4155 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4157 case DVD_STRUCT_RMD_LAST:
4161 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4162 address = dvdstruct->address;
4164 break; /* NOTREACHED */
4165 case DVD_STRUCT_RMD_RMA:
4169 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4170 address = dvdstruct->address;
4172 break; /* NOTREACHED */
4173 case DVD_STRUCT_PRERECORDED:
4174 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4176 case DVD_STRUCT_UNIQUEID:
4177 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4179 case DVD_STRUCT_DCB:
4183 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4184 address = dvdstruct->address;
4186 break; /* NOTREACHED */
4187 case DVD_STRUCT_LIST:
4189 * This is the maximum allocation length for the READ DVD
4190 * STRUCTURE command. There's nothing in the MMC3 spec
4191 * that indicates a limit in the amount of data that can
4192 * be returned from this call, other than the limits
4193 * imposed by the 2-byte length variables.
4200 break; /* NOTREACHED */
4204 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4209 scsi_read_dvd_structure(&ccb->csio,
4211 /* cbfcnp */ cddone,
4212 /* tag_action */ MSG_SIMPLE_Q_TAG,
4214 /* layer_number */ dvdstruct->layer_num,
4215 /* key_format */ dvdstruct->format,
4216 /* agid */ dvdstruct->agid,
4217 /* data_ptr */ databuf,
4218 /* dxfer_len */ length,
4219 /* sense_len */ SSD_FULL_SIZE,
4220 /* timeout */ 50000);
4222 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4223 /*sense_flags*/SF_RETRY_UA);
4228 switch(dvdstruct->format) {
4229 case DVD_STRUCT_PHYSICAL: {
4230 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4231 struct dvd_layer *outlayer;
4232 struct scsi_read_dvd_struct_data_physical *phys_data;
4235 (struct scsi_read_dvd_struct_data_physical *)databuf;
4236 inlayer = &phys_data->layer_desc;
4237 outlayer = (struct dvd_layer *)&dvdstruct->data;
4239 dvdstruct->length = sizeof(*inlayer);
4241 outlayer->book_type = (inlayer->book_type_version &
4242 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4243 outlayer->book_version = (inlayer->book_type_version &
4244 RDSD_BOOK_VERSION_MASK);
4245 outlayer->disc_size = (inlayer->disc_size_max_rate &
4246 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4247 outlayer->max_rate = (inlayer->disc_size_max_rate &
4248 RDSD_MAX_RATE_MASK);
4249 outlayer->nlayers = (inlayer->layer_info &
4250 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4251 outlayer->track_path = (inlayer->layer_info &
4252 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4253 outlayer->layer_type = (inlayer->layer_info &
4254 RDSD_LAYER_TYPE_MASK);
4255 outlayer->linear_density = (inlayer->density &
4256 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4257 outlayer->track_density = (inlayer->density &
4258 RDSD_TRACK_DENSITY_MASK);
4259 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4261 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4262 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4263 outlayer->end_sector_l0 =
4264 scsi_3btoul(inlayer->end_sector_layer0);
4267 case DVD_STRUCT_COPYRIGHT: {
4268 struct scsi_read_dvd_struct_data_copyright *copy_data;
4270 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4273 dvdstruct->cpst = copy_data->cps_type;
4274 dvdstruct->rmi = copy_data->region_info;
4275 dvdstruct->length = 0;
4281 * Tell the user what the overall length is, no matter
4282 * what we can actually fit in the data buffer.
4284 dvdstruct->length = length - ccb->csio.resid -
4285 sizeof(struct scsi_read_dvd_struct_data_header);
4288 * But only actually copy out the smaller of what we read
4289 * in or what the structure can take.
4291 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4293 min(sizeof(dvdstruct->data), dvdstruct->length));
4298 if (databuf != NULL)
4299 kfree(databuf, M_DEVBUF);
4301 xpt_release_ccb(ccb);
4307 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4308 void (*cbfcnp)(struct cam_periph *, union ccb *),
4309 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4310 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4311 u_int8_t sense_len, u_int32_t timeout)
4313 struct scsi_report_key *scsi_cmd;
4315 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4316 bzero(scsi_cmd, sizeof(*scsi_cmd));
4317 scsi_cmd->opcode = REPORT_KEY;
4318 scsi_ulto4b(lba, scsi_cmd->lba);
4319 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4320 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4321 (key_format & RK_KF_KEYFORMAT_MASK);
4326 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4328 /*data_ptr*/ data_ptr,
4329 /*dxfer_len*/ dxfer_len,
4336 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4337 void (*cbfcnp)(struct cam_periph *, union ccb *),
4338 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4339 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4342 struct scsi_send_key *scsi_cmd;
4344 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4345 bzero(scsi_cmd, sizeof(*scsi_cmd));
4346 scsi_cmd->opcode = SEND_KEY;
4348 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4349 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4350 (key_format & RK_KF_KEYFORMAT_MASK);
4355 /*flags*/ CAM_DIR_OUT,
4357 /*data_ptr*/ data_ptr,
4358 /*dxfer_len*/ dxfer_len,
4366 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4367 void (*cbfcnp)(struct cam_periph *, union ccb *),
4368 u_int8_t tag_action, u_int32_t address,
4369 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4370 u_int8_t *data_ptr, u_int32_t dxfer_len,
4371 u_int8_t sense_len, u_int32_t timeout)
4373 struct scsi_read_dvd_structure *scsi_cmd;
4375 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4376 bzero(scsi_cmd, sizeof(*scsi_cmd));
4377 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4379 scsi_ulto4b(address, scsi_cmd->address);
4380 scsi_cmd->layer_number = layer_number;
4381 scsi_cmd->format = format;
4382 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4383 /* The AGID is the top two bits of this byte */
4384 scsi_cmd->agid = agid << 6;
4389 /*flags*/ CAM_DIR_IN,
4391 /*data_ptr*/ data_ptr,
4392 /*dxfer_len*/ dxfer_len,