scsi_cd.c revision 112262
1/* 2 * Copyright (c) 1997 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/cam/scsi/scsi_cd.c 112262 2003-03-15 11:00:56Z phk $ 28 */ 29/* 30 * Portions of this driver taken from the original FreeBSD cd driver. 31 * Written by Julian Elischer (julian@tfs.com) 32 * for TRW Financial Systems for use under the MACH(2.5) operating system. 33 * 34 * TRW Financial Systems, in accordance with their agreement with Carnegie 35 * Mellon University, makes this software available to CMU to distribute 36 * or use in any manner that they see fit as long as this message is kept with 37 * the software. For this reason TFS also grants any other persons or 38 * organisations permission to use or modify this software. 39 * 40 * TFS supplies this software to be publicly redistributed 41 * on the understanding that TFS is not responsible for the correct 42 * functioning of this software in any circumstances. 43 * 44 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 45 * 46 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $ 47 */ 48 49#include "opt_cd.h" 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/kernel.h> 54#include <sys/bio.h> 55#include <sys/conf.h> 56#include <sys/disk.h> 57#include <sys/malloc.h> 58#include <sys/cdio.h> 59#include <sys/cdrio.h> 60#include <sys/dvdio.h> 61#include <sys/devicestat.h> 62#include <sys/sysctl.h> 63 64#include <cam/cam.h> 65#include <cam/cam_ccb.h> 66#include <cam/cam_periph.h> 67#include <cam/cam_xpt_periph.h> 68#include <cam/cam_queue.h> 69 70#include <cam/scsi/scsi_message.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/scsi/scsi_cd.h> 73 74#define LEADOUT 0xaa /* leadout toc entry */ 75 76struct cd_params { 77 u_int32_t blksize; 78 u_long disksize; 79}; 80 81typedef enum { 82 CD_Q_NONE = 0x00, 83 CD_Q_NO_TOUCH = 0x01, 84 CD_Q_BCD_TRACKS = 0x02, 85 CD_Q_NO_CHANGER = 0x04, 86 CD_Q_CHANGER = 0x08, 87 CD_Q_10_BYTE_ONLY = 0x10 88} cd_quirks; 89 90typedef enum { 91 CD_FLAG_INVALID = 0x001, 92 CD_FLAG_NEW_DISC = 0x002, 93 CD_FLAG_DISC_LOCKED = 0x004, 94 CD_FLAG_DISC_REMOVABLE = 0x008, 95 CD_FLAG_TAGGED_QUEUING = 0x010, 96 CD_FLAG_CHANGER = 0x040, 97 CD_FLAG_ACTIVE = 0x080, 98 CD_FLAG_SCHED_ON_COMP = 0x100, 99 CD_FLAG_RETRY_UA = 0x200, 100 CD_FLAG_VALID_MEDIA = 0x400, 101 CD_FLAG_VALID_TOC = 0x800 102} cd_flags; 103 104typedef enum { 105 CD_CCB_PROBE = 0x01, 106 CD_CCB_BUFFER_IO = 0x02, 107 CD_CCB_WAITING = 0x03, 108 CD_CCB_TYPE_MASK = 0x0F, 109 CD_CCB_RETRY_UA = 0x10 110} cd_ccb_state; 111 112typedef enum { 113 CHANGER_TIMEOUT_SCHED = 0x01, 114 CHANGER_SHORT_TMOUT_SCHED = 0x02, 115 CHANGER_MANUAL_CALL = 0x04, 116 CHANGER_NEED_TIMEOUT = 0x08 117} cd_changer_flags; 118 119#define ccb_state ppriv_field0 120#define ccb_bp ppriv_ptr1 121 122struct cd_tocdata { 123 struct ioc_toc_header header; 124 struct cd_toc_entry entries[100]; 125}; 126 127struct cd_toc_single { 128 struct ioc_toc_header header; 129 struct cd_toc_entry entry; 130}; 131 132typedef enum { 133 CD_STATE_PROBE, 134 CD_STATE_NORMAL 135} cd_state; 136 137struct cd_softc { 138 cam_pinfo pinfo; 139 cd_state state; 140 volatile cd_flags flags; 141 struct bio_queue_head bio_queue; 142 LIST_HEAD(, ccb_hdr) pending_ccbs; 143 struct cd_params params; 144 union ccb saved_ccb; 145 cd_quirks quirks; 146 struct devstat *device_stats; 147 STAILQ_ENTRY(cd_softc) changer_links; 148 struct cdchanger *changer; 149 int bufs_left; 150 struct cam_periph *periph; 151 dev_t dev; 152 eventhandler_tag clonetag; 153 int minimum_command_size; 154 int outstanding_cmds; 155 struct sysctl_ctx_list sysctl_ctx; 156 struct sysctl_oid *sysctl_tree; 157 STAILQ_HEAD(, cd_mode_params) mode_queue; 158 struct cd_tocdata toc; 159}; 160 161struct cd_page_sizes { 162 int page; 163 int page_size; 164}; 165 166static struct cd_page_sizes cd_page_size_table[] = 167{ 168 { AUDIO_PAGE, sizeof(struct cd_audio_page)} 169}; 170 171struct cd_quirk_entry { 172 struct scsi_inquiry_pattern inq_pat; 173 cd_quirks quirks; 174}; 175 176/* 177 * The changer quirk entries aren't strictly necessary. Basically, what 178 * they do is tell cdregister() up front that a device is a changer. 179 * Otherwise, it will figure that fact out once it sees a LUN on the device 180 * that is greater than 0. If it is known up front that a device is a changer, 181 * all I/O to the device will go through the changer scheduling routines, as 182 * opposed to the "normal" CD code. 183 * 184 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 185 * your device hangs when it gets a 10 byte command. Adding a quirk just 186 * to get rid of the informative diagnostic message is not acceptable. All 187 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 188 * referenced in a comment along with the quirk) , and must be approved by 189 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 190 * be removed until the submitter can explain why they are needed. 191 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 192 * when the CAM_NEW_TRAN_CODE work is done. 193 */ 194static struct cd_quirk_entry cd_quirk_table[] = 195{ 196 { 197 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 198 /*quirks*/ CD_Q_CHANGER 199 }, 200 { 201 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*", 202 "*"}, /* quirks */ CD_Q_CHANGER 203 }, 204 { 205 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"}, 206 /* quirks */ CD_Q_CHANGER 207 }, 208 { 209 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 210 /* quirks */ CD_Q_BCD_TRACKS 211 } 212}; 213 214#define CD_CDEV_MAJOR 15 215 216static d_open_t cdopen; 217static d_close_t cdclose; 218static d_ioctl_t cdioctl; 219static d_strategy_t cdstrategy; 220 221static periph_init_t cdinit; 222static periph_ctor_t cdregister; 223static periph_dtor_t cdcleanup; 224static periph_start_t cdstart; 225static periph_oninv_t cdoninvalidate; 226static void cdasync(void *callback_arg, u_int32_t code, 227 struct cam_path *path, void *arg); 228static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 229static void cdshorttimeout(void *arg); 230static void cdschedule(struct cam_periph *periph, int priority); 231static void cdrunchangerqueue(void *arg); 232static void cdchangerschedule(struct cd_softc *softc); 233static int cdrunccb(union ccb *ccb, 234 int (*error_routine)(union ccb *ccb, 235 u_int32_t cam_flags, 236 u_int32_t sense_flags), 237 u_int32_t cam_flags, u_int32_t sense_flags); 238static union ccb *cdgetccb(struct cam_periph *periph, 239 u_int32_t priority); 240static void cddone(struct cam_periph *periph, 241 union ccb *start_ccb); 242static union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 243static int cdgetpagesize(int page_num); 244static void cdprevent(struct cam_periph *periph, int action); 245static int cdcheckmedia(struct cam_periph *periph); 246static int cdsize(struct cam_periph *periph, u_int32_t *size); 247static int cd6byteworkaround(union ccb *ccb); 248static int cderror(union ccb *ccb, u_int32_t cam_flags, 249 u_int32_t sense_flags); 250static int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 251 u_int32_t start, u_int8_t *data, 252 u_int32_t len, u_int32_t sense_flags); 253static int cdgetmode(struct cam_periph *periph, 254 struct cd_mode_params *data, u_int32_t page); 255static int cdsetmode(struct cam_periph *periph, 256 struct cd_mode_params *data); 257static int cdplay(struct cam_periph *periph, u_int32_t blk, 258 u_int32_t len); 259static int cdreadsubchannel(struct cam_periph *periph, 260 u_int32_t mode, u_int32_t format, 261 int track, 262 struct cd_sub_channel_info *data, 263 u_int32_t len); 264static int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 265 u_int32_t starts, u_int32_t startf, 266 u_int32_t endm, u_int32_t ends, 267 u_int32_t endf); 268static int cdplaytracks(struct cam_periph *periph, 269 u_int32_t strack, u_int32_t sindex, 270 u_int32_t etrack, u_int32_t eindex); 271static int cdpause(struct cam_periph *periph, u_int32_t go); 272static int cdstopunit(struct cam_periph *periph, u_int32_t eject); 273static int cdstartunit(struct cam_periph *periph, int load); 274static int cdsetspeed(struct cam_periph *periph, 275 u_int32_t rdspeed, u_int32_t wrspeed); 276static int cdreportkey(struct cam_periph *periph, 277 struct dvd_authinfo *authinfo); 278static int cdsendkey(struct cam_periph *periph, 279 struct dvd_authinfo *authinfo); 280static int cdreaddvdstructure(struct cam_periph *periph, 281 struct dvd_struct *dvdstruct); 282 283static struct periph_driver cddriver = 284{ 285 cdinit, "cd", 286 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 287}; 288 289PERIPHDRIVER_DECLARE(cd, cddriver); 290 291static struct cdevsw cd_cdevsw = { 292 .d_open = cdopen, 293 .d_close = cdclose, 294 .d_read = physread, 295 .d_write = physwrite, 296 .d_ioctl = cdioctl, 297 .d_strategy = cdstrategy, 298 .d_name = "cd", 299 .d_maj = CD_CDEV_MAJOR, 300 .d_flags = D_DISK, 301}; 302 303static int num_changers; 304 305#ifndef CHANGER_MIN_BUSY_SECONDS 306#define CHANGER_MIN_BUSY_SECONDS 5 307#endif 308#ifndef CHANGER_MAX_BUSY_SECONDS 309#define CHANGER_MAX_BUSY_SECONDS 15 310#endif 311 312static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 313static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 314 315SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 316SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 317SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 318 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 319TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds); 320SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 321 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 322TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds); 323 324struct cdchanger { 325 path_id_t path_id; 326 target_id_t target_id; 327 int num_devices; 328 struct camq devq; 329 struct timeval start_time; 330 struct cd_softc *cur_device; 331 struct callout_handle short_handle; 332 struct callout_handle long_handle; 333 volatile cd_changer_flags flags; 334 STAILQ_ENTRY(cdchanger) changer_links; 335 STAILQ_HEAD(chdevlist, cd_softc) chluns; 336}; 337 338static STAILQ_HEAD(changerlist, cdchanger) changerq; 339 340static void 341cdclone(void *arg, char *name, int namelen, dev_t *dev) 342{ 343 struct cd_softc *softc; 344 const char *p; 345 int l; 346 347 softc = arg; 348 p = devtoname(softc->dev); 349 l = strlen(p); 350 if (bcmp(name, p, l)) 351 return; 352 if (name[l] != 'a' && name[l] != 'c') 353 return; 354 if (name[l + 1] != '\0') 355 return; 356 *dev = softc->dev; 357 return; 358} 359 360static void 361cdinit(void) 362{ 363 cam_status status; 364 struct cam_path *path; 365 366 /* 367 * Install a global async callback. This callback will 368 * receive async callbacks like "new device found". 369 */ 370 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 371 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 372 373 if (status == CAM_REQ_CMP) { 374 struct ccb_setasync csa; 375 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; 383 xpt_free_path(path); 384 } 385 386 if (status != CAM_REQ_CMP) { 387 printf("cd: Failed to attach master async callback " 388 "due to status 0x%x!\n", status); 389 } 390} 391 392static void 393cdoninvalidate(struct cam_periph *periph) 394{ 395 int s; 396 struct cd_softc *softc; 397 struct bio *q_bp; 398 struct ccb_setasync csa; 399 400 softc = (struct cd_softc *)periph->softc; 401 402 /* 403 * De-register any async callbacks. 404 */ 405 xpt_setup_ccb(&csa.ccb_h, periph->path, 406 /* priority */ 5); 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); 412 413 softc->flags |= CD_FLAG_INVALID; 414 415 /* 416 * Although the oninvalidate() routines are always called at 417 * splsoftcam, we need to be at splbio() here to keep the buffer 418 * queue from being modified while we traverse it. 419 */ 420 s = splbio(); 421 422 /* 423 * Return all queued I/O with ENXIO. 424 * XXX Handle any transactions queued to the card 425 * with XPT_ABORT_CCB. 426 */ 427 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){ 428 bioq_remove(&softc->bio_queue, q_bp); 429 q_bp->bio_resid = q_bp->bio_bcount; 430 biofinish(q_bp, NULL, ENXIO); 431 } 432 splx(s); 433 434 /* 435 * If this device is part of a changer, and it was scheduled 436 * to run, remove it from the run queue since we just nuked 437 * all of its scheduled I/O. 438 */ 439 if ((softc->flags & CD_FLAG_CHANGER) 440 && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 441 camq_remove(&softc->changer->devq, softc->pinfo.index); 442 443 xpt_print_path(periph->path); 444 printf("lost device\n"); 445} 446 447static void 448cdcleanup(struct cam_periph *periph) 449{ 450 struct cd_softc *softc; 451 int s; 452 453 softc = (struct cd_softc *)periph->softc; 454 455 xpt_print_path(periph->path); 456 printf("removing device entry\n"); 457 458 s = splsoftcam(); 459 /* 460 * In the queued, non-active case, the device in question 461 * has already been removed from the changer run queue. Since this 462 * device is active, we need to de-activate it, and schedule 463 * another device to run. (if there is another one to run) 464 */ 465 if ((softc->flags & CD_FLAG_CHANGER) 466 && (softc->flags & CD_FLAG_ACTIVE)) { 467 468 /* 469 * The purpose of the short timeout is soley to determine 470 * whether the current device has finished or not. Well, 471 * since we're removing the active device, we know that it 472 * is finished. So, get rid of the short timeout. 473 * Otherwise, if we're in the time period before the short 474 * timeout fires, and there are no other devices in the 475 * queue to run, there won't be any other device put in the 476 * active slot. i.e., when we call cdrunchangerqueue() 477 * below, it won't do anything. Then, when the short 478 * timeout fires, it'll look at the "current device", which 479 * we are free below, and possibly panic the kernel on a 480 * bogus pointer reference. 481 * 482 * The long timeout doesn't really matter, since we 483 * decrement the qfrozen_cnt to indicate that there is 484 * nothing in the active slot now. Therefore, there won't 485 * be any bogus pointer references there. 486 */ 487 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 488 untimeout(cdshorttimeout, softc->changer, 489 softc->changer->short_handle); 490 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 491 } 492 softc->changer->devq.qfrozen_cnt--; 493 softc->changer->flags |= CHANGER_MANUAL_CALL; 494 cdrunchangerqueue(softc->changer); 495 } 496 497 /* 498 * If we're removing the last device on the changer, go ahead and 499 * remove the changer device structure. 500 */ 501 if ((softc->flags & CD_FLAG_CHANGER) 502 && (--softc->changer->num_devices == 0)) { 503 504 /* 505 * Theoretically, there shouldn't be any timeouts left, but 506 * I'm not completely sure that that will be the case. So, 507 * it won't hurt to check and see if there are any left. 508 */ 509 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 510 untimeout(cdrunchangerqueue, softc->changer, 511 softc->changer->long_handle); 512 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 513 } 514 515 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 516 untimeout(cdshorttimeout, softc->changer, 517 softc->changer->short_handle); 518 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 519 } 520 521 STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 522 changer_links); 523 xpt_print_path(periph->path); 524 printf("removing changer entry\n"); 525 free(softc->changer, M_DEVBUF); 526 num_changers--; 527 } 528 devstat_remove_entry(softc->device_stats); 529 destroy_dev(softc->dev); 530 EVENTHANDLER_DEREGISTER(dev_clone, softc->clonetag); 531 free(softc, M_DEVBUF); 532 splx(s); 533} 534 535static void 536cdasync(void *callback_arg, u_int32_t code, 537 struct cam_path *path, void *arg) 538{ 539 struct cam_periph *periph; 540 541 periph = (struct cam_periph *)callback_arg; 542 switch (code) { 543 case AC_FOUND_DEVICE: 544 { 545 struct ccb_getdev *cgd; 546 cam_status status; 547 548 cgd = (struct ccb_getdev *)arg; 549 if (cgd == NULL) 550 break; 551 552 if (SID_TYPE(&cgd->inq_data) != T_CDROM 553 && SID_TYPE(&cgd->inq_data) != T_WORM) 554 break; 555 556 /* 557 * Allocate a peripheral instance for 558 * this device and start the probe 559 * process. 560 */ 561 status = cam_periph_alloc(cdregister, cdoninvalidate, 562 cdcleanup, cdstart, 563 "cd", CAM_PERIPH_BIO, 564 cgd->ccb_h.path, cdasync, 565 AC_FOUND_DEVICE, cgd); 566 567 if (status != CAM_REQ_CMP 568 && status != CAM_REQ_INPROG) 569 printf("cdasync: Unable to attach new device " 570 "due to status 0x%x\n", status); 571 572 break; 573 } 574 case AC_SENT_BDR: 575 case AC_BUS_RESET: 576 { 577 struct cd_softc *softc; 578 struct ccb_hdr *ccbh; 579 int s; 580 581 softc = (struct cd_softc *)periph->softc; 582 s = splsoftcam(); 583 /* 584 * Don't fail on the expected unit attention 585 * that will occur. 586 */ 587 softc->flags |= CD_FLAG_RETRY_UA; 588 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 589 ccbh->ccb_state |= CD_CCB_RETRY_UA; 590 splx(s); 591 /* FALLTHROUGH */ 592 } 593 default: 594 cam_periph_async(periph, code, path, arg); 595 break; 596 } 597} 598 599/* 600 * We have a handler function for this so we can check the values when the 601 * user sets them, instead of every time we look at them. 602 */ 603static int 604cdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 605{ 606 int error, value; 607 608 value = *(int *)arg1; 609 610 error = sysctl_handle_int(oidp, &value, 0, req); 611 612 if ((error != 0) 613 || (req->newptr == NULL)) 614 return (error); 615 616 /* 617 * The only real values we can have here are 6 or 10. I don't 618 * really forsee having 12 be an option at any time in the future. 619 * So if the user sets something less than or equal to 6, we'll set 620 * it to 6. If he sets something greater than 6, we'll set it to 10. 621 * 622 * I suppose we could just return an error here for the wrong values, 623 * but I don't think it's necessary to do so, as long as we can 624 * determine the user's intent without too much trouble. 625 */ 626 if (value < 6) 627 value = 6; 628 else if (value > 6) 629 value = 10; 630 631 *(int *)arg1 = value; 632 633 return (0); 634} 635 636static cam_status 637cdregister(struct cam_periph *periph, void *arg) 638{ 639 struct cd_softc *softc; 640 struct ccb_setasync csa; 641 struct ccb_getdev *cgd; 642 char tmpstr[80], tmpstr2[80]; 643 caddr_t match; 644 645 cgd = (struct ccb_getdev *)arg; 646 if (periph == NULL) { 647 printf("cdregister: periph was NULL!!\n"); 648 return(CAM_REQ_CMP_ERR); 649 } 650 if (cgd == NULL) { 651 printf("cdregister: no getdev CCB, can't register device\n"); 652 return(CAM_REQ_CMP_ERR); 653 } 654 655 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 656 657 if (softc == NULL) { 658 printf("cdregister: Unable to probe new device. " 659 "Unable to allocate softc\n"); 660 return(CAM_REQ_CMP_ERR); 661 } 662 663 bzero(softc, sizeof(*softc)); 664 LIST_INIT(&softc->pending_ccbs); 665 STAILQ_INIT(&softc->mode_queue); 666 softc->state = CD_STATE_PROBE; 667 bioq_init(&softc->bio_queue); 668 if (SID_IS_REMOVABLE(&cgd->inq_data)) 669 softc->flags |= CD_FLAG_DISC_REMOVABLE; 670 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 671 softc->flags |= CD_FLAG_TAGGED_QUEUING; 672 673 periph->softc = softc; 674 softc->periph = periph; 675 676 /* 677 * See if this device has any quirks. 678 */ 679 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 680 (caddr_t)cd_quirk_table, 681 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 682 sizeof(*cd_quirk_table), scsi_inquiry_match); 683 684 if (match != NULL) 685 softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 686 else 687 softc->quirks = CD_Q_NONE; 688 689 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 690 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 691 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 692 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 693 tmpstr2, CTLFLAG_RD, 0, tmpstr); 694 if (softc->sysctl_tree == NULL) { 695 printf("cdregister: unable to allocate sysctl tree\n"); 696 free(softc, M_DEVBUF); 697 return (CAM_REQ_CMP_ERR); 698 } 699 700 /* The default is 6 byte commands, unless quirked otherwise */ 701 if (softc->quirks & CD_Q_10_BYTE_ONLY) 702 softc->minimum_command_size = 10; 703 else 704 softc->minimum_command_size = 6; 705 706 /* 707 * Load the user's default, if any. 708 */ 709 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 710 periph->unit_number); 711 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 712 713 /* 6 and 10 are the only permissible values here. */ 714 if (softc->minimum_command_size < 6) 715 softc->minimum_command_size = 6; 716 else if (softc->minimum_command_size > 6) 717 softc->minimum_command_size = 10; 718 719 /* 720 * Now register the sysctl handler, so the user can the value on 721 * the fly. 722 */ 723 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 724 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 725 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 726 "Minimum CDB size"); 727 728 /* 729 * We need to register the statistics structure for this device, 730 * but we don't have the blocksize yet for it. So, we register 731 * the structure and indicate that we don't have the blocksize 732 * yet. Unlike other SCSI peripheral drivers, we explicitly set 733 * the device type here to be CDROM, rather than just ORing in 734 * the device type. This is because this driver can attach to either 735 * CDROM or WORM devices, and we want this peripheral driver to 736 * show up in the devstat list as a CD peripheral driver, not a 737 * WORM peripheral driver. WORM drives will also have the WORM 738 * driver attached to them. 739 */ 740 softc->device_stats = devstat_new_entry("cd", 741 periph->unit_number, 0, 742 DEVSTAT_BS_UNAVAILABLE, 743 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI, 744 DEVSTAT_PRIORITY_CD); 745 softc->dev = make_dev(&cd_cdevsw, periph->unit_number, 746 UID_ROOT, GID_OPERATOR, 0640, "cd%d", periph->unit_number); 747 softc->dev->si_drv1 = periph; 748 softc->clonetag = 749 EVENTHANDLER_REGISTER(dev_clone, cdclone, softc, 1000); 750 751 /* 752 * Add an async callback so that we get 753 * notified if this device goes away. 754 */ 755 xpt_setup_ccb(&csa.ccb_h, periph->path, 756 /* priority */ 5); 757 csa.ccb_h.func_code = XPT_SASYNC_CB; 758 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 759 csa.callback = cdasync; 760 csa.callback_arg = periph; 761 xpt_action((union ccb *)&csa); 762 763 /* 764 * If the target lun is greater than 0, we most likely have a CD 765 * changer device. Check the quirk entries as well, though, just 766 * in case someone has a CD tower with one lun per drive or 767 * something like that. Also, if we know up front that a 768 * particular device is a changer, we can mark it as such starting 769 * with lun 0, instead of lun 1. It shouldn't be necessary to have 770 * a quirk entry to define something as a changer, however. 771 */ 772 if (((cgd->ccb_h.target_lun > 0) 773 && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 774 || ((softc->quirks & CD_Q_CHANGER) != 0)) { 775 struct cdchanger *nchanger; 776 struct cam_periph *nperiph; 777 struct cam_path *path; 778 cam_status status; 779 int found; 780 781 /* Set the changer flag in the current device's softc */ 782 softc->flags |= CD_FLAG_CHANGER; 783 784 if (num_changers == 0) 785 STAILQ_INIT(&changerq); 786 787 /* 788 * Now, look around for an existing changer device with the 789 * same path and target ID as the current device. 790 */ 791 for (found = 0, 792 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 793 nchanger != NULL; 794 nchanger = STAILQ_NEXT(nchanger, changer_links)){ 795 if ((nchanger->path_id == cgd->ccb_h.path_id) 796 && (nchanger->target_id == cgd->ccb_h.target_id)) { 797 found = 1; 798 break; 799 } 800 } 801 802 /* 803 * If we found a matching entry, just add this device to 804 * the list of devices on this changer. 805 */ 806 if (found == 1) { 807 struct chdevlist *chlunhead; 808 809 chlunhead = &nchanger->chluns; 810 811 /* 812 * XXX KDM look at consolidating this code with the 813 * code below in a separate function. 814 */ 815 816 /* 817 * Create a path with lun id 0, and see if we can 818 * find a matching device 819 */ 820 status = xpt_create_path(&path, /*periph*/ periph, 821 cgd->ccb_h.path_id, 822 cgd->ccb_h.target_id, 0); 823 824 if ((status == CAM_REQ_CMP) 825 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 826 struct cd_softc *nsoftc; 827 828 nsoftc = (struct cd_softc *)nperiph->softc; 829 830 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 831 nsoftc->flags |= CD_FLAG_CHANGER; 832 nchanger->num_devices++; 833 if (camq_resize(&nchanger->devq, 834 nchanger->num_devices)!=CAM_REQ_CMP){ 835 printf("cdregister: " 836 "camq_resize " 837 "failed, changer " 838 "support may " 839 "be messed up\n"); 840 } 841 nsoftc->changer = nchanger; 842 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 843 844 STAILQ_INSERT_TAIL(&nchanger->chluns, 845 nsoftc,changer_links); 846 } 847 xpt_free_path(path); 848 } else if (status == CAM_REQ_CMP) 849 xpt_free_path(path); 850 else { 851 printf("cdregister: unable to allocate path\n" 852 "cdregister: changer support may be " 853 "broken\n"); 854 } 855 856 nchanger->num_devices++; 857 858 softc->changer = nchanger; 859 softc->pinfo.index = CAM_UNQUEUED_INDEX; 860 861 if (camq_resize(&nchanger->devq, 862 nchanger->num_devices) != CAM_REQ_CMP) { 863 printf("cdregister: camq_resize " 864 "failed, changer support may " 865 "be messed up\n"); 866 } 867 868 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 869 } 870 /* 871 * In this case, we don't already have an entry for this 872 * particular changer, so we need to create one, add it to 873 * the queue, and queue this device on the list for this 874 * changer. Before we queue this device, however, we need 875 * to search for lun id 0 on this target, and add it to the 876 * queue first, if it exists. (and if it hasn't already 877 * been marked as part of the changer.) 878 */ 879 else { 880 nchanger = malloc(sizeof(struct cdchanger), 881 M_DEVBUF, M_NOWAIT); 882 883 if (nchanger == NULL) { 884 softc->flags &= ~CD_FLAG_CHANGER; 885 printf("cdregister: unable to malloc " 886 "changer structure\ncdregister: " 887 "changer support disabled\n"); 888 889 /* 890 * Yes, gotos can be gross but in this case 891 * I think it's justified.. 892 */ 893 goto cdregisterexit; 894 } 895 896 /* zero the structure */ 897 bzero(nchanger, sizeof(struct cdchanger)); 898 899 if (camq_init(&nchanger->devq, 1) != 0) { 900 softc->flags &= ~CD_FLAG_CHANGER; 901 printf("cdregister: changer support " 902 "disabled\n"); 903 goto cdregisterexit; 904 } 905 906 num_changers++; 907 908 nchanger->path_id = cgd->ccb_h.path_id; 909 nchanger->target_id = cgd->ccb_h.target_id; 910 911 /* this is superfluous, but it makes things clearer */ 912 nchanger->num_devices = 0; 913 914 STAILQ_INIT(&nchanger->chluns); 915 916 STAILQ_INSERT_TAIL(&changerq, nchanger, 917 changer_links); 918 919 /* 920 * Create a path with lun id 0, and see if we can 921 * find a matching device 922 */ 923 status = xpt_create_path(&path, /*periph*/ periph, 924 cgd->ccb_h.path_id, 925 cgd->ccb_h.target_id, 0); 926 927 /* 928 * If we were able to allocate the path, and if we 929 * find a matching device and it isn't already 930 * marked as part of a changer, then we add it to 931 * the current changer. 932 */ 933 if ((status == CAM_REQ_CMP) 934 && ((nperiph = cam_periph_find(path, "cd")) != NULL) 935 && ((((struct cd_softc *)periph->softc)->flags & 936 CD_FLAG_CHANGER) == 0)) { 937 struct cd_softc *nsoftc; 938 939 nsoftc = (struct cd_softc *)nperiph->softc; 940 941 nsoftc->flags |= CD_FLAG_CHANGER; 942 nchanger->num_devices++; 943 if (camq_resize(&nchanger->devq, 944 nchanger->num_devices) != CAM_REQ_CMP) { 945 printf("cdregister: camq_resize " 946 "failed, changer support may " 947 "be messed up\n"); 948 } 949 nsoftc->changer = nchanger; 950 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 951 952 STAILQ_INSERT_TAIL(&nchanger->chluns, 953 nsoftc, changer_links); 954 xpt_free_path(path); 955 } else if (status == CAM_REQ_CMP) 956 xpt_free_path(path); 957 else { 958 printf("cdregister: unable to allocate path\n" 959 "cdregister: changer support may be " 960 "broken\n"); 961 } 962 963 softc->changer = nchanger; 964 softc->pinfo.index = CAM_UNQUEUED_INDEX; 965 nchanger->num_devices++; 966 if (camq_resize(&nchanger->devq, 967 nchanger->num_devices) != CAM_REQ_CMP) { 968 printf("cdregister: camq_resize " 969 "failed, changer support may " 970 "be messed up\n"); 971 } 972 STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 973 changer_links); 974 } 975 } 976 977cdregisterexit: 978 979 /* Lock this peripheral until we are setup */ 980 /* Can't block */ 981 cam_periph_lock(periph, PRIBIO); 982 983 if ((softc->flags & CD_FLAG_CHANGER) == 0) 984 xpt_schedule(periph, /*priority*/5); 985 else 986 cdschedule(periph, /*priority*/ 5); 987 988 return(CAM_REQ_CMP); 989} 990 991static int 992cdopen(dev_t dev, int flags, int fmt, struct thread *td) 993{ 994 struct cam_periph *periph; 995 struct cd_softc *softc; 996 int error; 997 int s; 998 999 periph = (struct cam_periph *)dev->si_drv1; 1000 if (periph == NULL) 1001 return (ENXIO); 1002 1003 softc = (struct cd_softc *)periph->softc; 1004 1005 /* 1006 * Grab splsoftcam and hold it until we lock the peripheral. 1007 */ 1008 s = splsoftcam(); 1009 if (softc->flags & CD_FLAG_INVALID) { 1010 splx(s); 1011 return(ENXIO); 1012 } 1013 1014 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 1015 splx(s); 1016 return (error); 1017 } 1018 1019 splx(s); 1020 1021 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1022 return(ENXIO); 1023 1024 /* 1025 * Check for media, and set the appropriate flags. We don't bail 1026 * if we don't have media, but then we don't allow anything but the 1027 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 1028 */ 1029 cdcheckmedia(periph); 1030 1031 cam_periph_unlock(periph); 1032 1033 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 1034 1035 return (error); 1036} 1037 1038static int 1039cdclose(dev_t dev, int flag, int fmt, struct thread *td) 1040{ 1041 struct cam_periph *periph; 1042 struct cd_softc *softc; 1043 int error; 1044 1045 periph = (struct cam_periph *)dev->si_drv1; 1046 if (periph == NULL) 1047 return (ENXIO); 1048 1049 softc = (struct cd_softc *)periph->softc; 1050 1051 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 1052 return (error); 1053 1054 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 1055 cdprevent(periph, PR_ALLOW); 1056 1057 /* 1058 * Since we're closing this CD, mark the blocksize as unavailable. 1059 * It will be marked as available when the CD is opened again. 1060 */ 1061 softc->device_stats->flags |= DEVSTAT_BS_UNAVAILABLE; 1062 1063 /* 1064 * We'll check the media and toc again at the next open(). 1065 */ 1066 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 1067 1068 cam_periph_unlock(periph); 1069 cam_periph_release(periph); 1070 1071 return (0); 1072} 1073 1074static void 1075cdshorttimeout(void *arg) 1076{ 1077 struct cdchanger *changer; 1078 int s; 1079 1080 s = splsoftcam(); 1081 1082 changer = (struct cdchanger *)arg; 1083 1084 /* Always clear the short timeout flag, since that's what we're in */ 1085 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1086 1087 /* 1088 * Check to see if there is any more pending or outstanding I/O for 1089 * this device. If not, move it out of the active slot. 1090 */ 1091 if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1092 && (changer->cur_device->outstanding_cmds == 0)) { 1093 changer->flags |= CHANGER_MANUAL_CALL; 1094 cdrunchangerqueue(changer); 1095 } 1096 1097 splx(s); 1098} 1099 1100/* 1101 * This is a wrapper for xpt_schedule. It only applies to changers. 1102 */ 1103static void 1104cdschedule(struct cam_periph *periph, int priority) 1105{ 1106 struct cd_softc *softc; 1107 int s; 1108 1109 s = splsoftcam(); 1110 1111 softc = (struct cd_softc *)periph->softc; 1112 1113 /* 1114 * If this device isn't currently queued, and if it isn't 1115 * the active device, then we queue this device and run the 1116 * changer queue if there is no timeout scheduled to do it. 1117 * If this device is the active device, just schedule it 1118 * to run again. If this device is queued, there should be 1119 * a timeout in place already that will make sure it runs. 1120 */ 1121 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 1122 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 1123 /* 1124 * We don't do anything with the priority here. 1125 * This is strictly a fifo queue. 1126 */ 1127 softc->pinfo.priority = 1; 1128 softc->pinfo.generation = ++softc->changer->devq.generation; 1129 camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 1130 1131 /* 1132 * Since we just put a device in the changer queue, 1133 * check and see if there is a timeout scheduled for 1134 * this changer. If so, let the timeout handle 1135 * switching this device into the active slot. If 1136 * not, manually call the timeout routine to 1137 * bootstrap things. 1138 */ 1139 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1140 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 1141 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 1142 softc->changer->flags |= CHANGER_MANUAL_CALL; 1143 cdrunchangerqueue(softc->changer); 1144 } 1145 } else if ((softc->flags & CD_FLAG_ACTIVE) 1146 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 1147 xpt_schedule(periph, priority); 1148 1149 splx(s); 1150 1151} 1152 1153static void 1154cdrunchangerqueue(void *arg) 1155{ 1156 struct cd_softc *softc; 1157 struct cdchanger *changer; 1158 int called_from_timeout; 1159 int s; 1160 1161 s = splsoftcam(); 1162 1163 changer = (struct cdchanger *)arg; 1164 1165 /* 1166 * If we have NOT been called from cdstrategy() or cddone(), and 1167 * instead from a timeout routine, go ahead and clear the 1168 * timeout flag. 1169 */ 1170 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 1171 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1172 called_from_timeout = 1; 1173 } else 1174 called_from_timeout = 0; 1175 1176 /* Always clear the manual call flag */ 1177 changer->flags &= ~CHANGER_MANUAL_CALL; 1178 1179 /* nothing to do if the queue is empty */ 1180 if (changer->devq.entries <= 0) { 1181 splx(s); 1182 return; 1183 } 1184 1185 /* 1186 * If the changer queue is frozen, that means we have an active 1187 * device. 1188 */ 1189 if (changer->devq.qfrozen_cnt > 0) { 1190 1191 if (changer->cur_device->outstanding_cmds > 0) { 1192 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 1193 changer->cur_device->bufs_left = 1194 changer->cur_device->outstanding_cmds; 1195 if (called_from_timeout) { 1196 changer->long_handle = 1197 timeout(cdrunchangerqueue, changer, 1198 changer_max_busy_seconds * hz); 1199 changer->flags |= CHANGER_TIMEOUT_SCHED; 1200 } 1201 splx(s); 1202 return; 1203 } 1204 1205 /* 1206 * We always need to reset the frozen count and clear the 1207 * active flag. 1208 */ 1209 changer->devq.qfrozen_cnt--; 1210 changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1211 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1212 1213 /* 1214 * Check to see whether the current device has any I/O left 1215 * to do. If so, requeue it at the end of the queue. If 1216 * not, there is no need to requeue it. 1217 */ 1218 if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 1219 1220 changer->cur_device->pinfo.generation = 1221 ++changer->devq.generation; 1222 camq_insert(&changer->devq, 1223 (cam_pinfo *)changer->cur_device); 1224 } 1225 } 1226 1227 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 1228 1229 changer->cur_device = softc; 1230 1231 changer->devq.qfrozen_cnt++; 1232 softc->flags |= CD_FLAG_ACTIVE; 1233 1234 /* Just in case this device is waiting */ 1235 wakeup(&softc->changer); 1236 xpt_schedule(softc->periph, /*priority*/ 1); 1237 1238 /* 1239 * Get rid of any pending timeouts, and set a flag to schedule new 1240 * ones so this device gets its full time quantum. 1241 */ 1242 if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1243 untimeout(cdrunchangerqueue, changer, changer->long_handle); 1244 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1245 } 1246 1247 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1248 untimeout(cdshorttimeout, changer, changer->short_handle); 1249 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1250 } 1251 1252 /* 1253 * We need to schedule timeouts, but we only do this after the 1254 * first transaction has completed. This eliminates the changer 1255 * switch time. 1256 */ 1257 changer->flags |= CHANGER_NEED_TIMEOUT; 1258 1259 splx(s); 1260} 1261 1262static void 1263cdchangerschedule(struct cd_softc *softc) 1264{ 1265 struct cdchanger *changer; 1266 int s; 1267 1268 s = splsoftcam(); 1269 1270 changer = softc->changer; 1271 1272 /* 1273 * If this is a changer, and this is the current device, 1274 * and this device has at least the minimum time quantum to 1275 * run, see if we can switch it out. 1276 */ 1277 if ((softc->flags & CD_FLAG_ACTIVE) 1278 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 1279 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 1280 /* 1281 * We try three things here. The first is that we 1282 * check to see whether the schedule on completion 1283 * flag is set. If it is, we decrement the number 1284 * of buffers left, and if it's zero, we reschedule. 1285 * Next, we check to see whether the pending buffer 1286 * queue is empty and whether there are no 1287 * outstanding transactions. If so, we reschedule. 1288 * Next, we see if the pending buffer queue is empty. 1289 * If it is, we set the number of buffers left to 1290 * the current active buffer count and set the 1291 * schedule on complete flag. 1292 */ 1293 if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 1294 if (--softc->bufs_left == 0) { 1295 softc->changer->flags |= 1296 CHANGER_MANUAL_CALL; 1297 softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 1298 cdrunchangerqueue(softc->changer); 1299 } 1300 } else if ((bioq_first(&softc->bio_queue) == NULL) 1301 && (softc->outstanding_cmds == 0)) { 1302 softc->changer->flags |= CHANGER_MANUAL_CALL; 1303 cdrunchangerqueue(softc->changer); 1304 } 1305 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 1306 && (softc->flags & CD_FLAG_ACTIVE)) { 1307 1308 /* 1309 * Now that the first transaction to this 1310 * particular device has completed, we can go ahead 1311 * and schedule our timeouts. 1312 */ 1313 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1314 changer->long_handle = 1315 timeout(cdrunchangerqueue, changer, 1316 changer_max_busy_seconds * hz); 1317 changer->flags |= CHANGER_TIMEOUT_SCHED; 1318 } else 1319 printf("cdchangerschedule: already have a long" 1320 " timeout!\n"); 1321 1322 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1323 changer->short_handle = 1324 timeout(cdshorttimeout, changer, 1325 changer_min_busy_seconds * hz); 1326 changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 1327 } else 1328 printf("cdchangerschedule: already have a short " 1329 "timeout!\n"); 1330 1331 /* 1332 * We just scheduled timeouts, no need to schedule 1333 * more. 1334 */ 1335 changer->flags &= ~CHANGER_NEED_TIMEOUT; 1336 1337 } 1338 splx(s); 1339} 1340 1341static int 1342cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 1343 u_int32_t cam_flags, 1344 u_int32_t sense_flags), 1345 u_int32_t cam_flags, u_int32_t sense_flags) 1346{ 1347 struct cd_softc *softc; 1348 struct cam_periph *periph; 1349 int error; 1350 1351 periph = xpt_path_periph(ccb->ccb_h.path); 1352 softc = (struct cd_softc *)periph->softc; 1353 1354 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1355 softc->device_stats); 1356 1357 if (softc->flags & CD_FLAG_CHANGER) 1358 cdchangerschedule(softc); 1359 1360 return(error); 1361} 1362 1363static union ccb * 1364cdgetccb(struct cam_periph *periph, u_int32_t priority) 1365{ 1366 struct cd_softc *softc; 1367 int s; 1368 1369 softc = (struct cd_softc *)periph->softc; 1370 1371 if (softc->flags & CD_FLAG_CHANGER) { 1372 1373 s = splsoftcam(); 1374 1375 /* 1376 * This should work the first time this device is woken up, 1377 * but just in case it doesn't, we use a while loop. 1378 */ 1379 while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 1380 /* 1381 * If this changer isn't already queued, queue it up. 1382 */ 1383 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1384 softc->pinfo.priority = 1; 1385 softc->pinfo.generation = 1386 ++softc->changer->devq.generation; 1387 camq_insert(&softc->changer->devq, 1388 (cam_pinfo *)softc); 1389 } 1390 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1391 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 1392 && ((softc->changer->flags 1393 & CHANGER_SHORT_TMOUT_SCHED)==0)) { 1394 softc->changer->flags |= CHANGER_MANUAL_CALL; 1395 cdrunchangerqueue(softc->changer); 1396 } else 1397 tsleep(&softc->changer, PRIBIO, "cgticb", 0); 1398 } 1399 splx(s); 1400 } 1401 return(cam_periph_getccb(periph, priority)); 1402} 1403 1404 1405/* 1406 * Actually translate the requested transfer into one the physical driver 1407 * can understand. The transfer is described by a buf and will include 1408 * only one physical transfer. 1409 */ 1410static void 1411cdstrategy(struct bio *bp) 1412{ 1413 struct cam_periph *periph; 1414 struct cd_softc *softc; 1415 int s; 1416 1417 periph = (struct cam_periph *)bp->bio_dev->si_drv1; 1418 if (periph == NULL) { 1419 biofinish(bp, NULL, ENXIO); 1420 return; 1421 } 1422 1423 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 1424 1425 softc = (struct cd_softc *)periph->softc; 1426 1427 /* 1428 * Mask interrupts so that the pack cannot be invalidated until 1429 * after we are in the queue. Otherwise, we might not properly 1430 * clean up one of the buffers. 1431 */ 1432 s = splbio(); 1433 1434 /* 1435 * If the device has been made invalid, error out 1436 */ 1437 if ((softc->flags & CD_FLAG_INVALID)) { 1438 splx(s); 1439 biofinish(bp, NULL, ENXIO); 1440 return; 1441 } 1442 1443 /* 1444 * If we don't have valid media, look for it before trying to 1445 * schedule the I/O. 1446 */ 1447 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1448 int error; 1449 1450 error = cdcheckmedia(periph); 1451 if (error != 0) { 1452 splx(s); 1453 biofinish(bp, NULL, error); 1454 return; 1455 } 1456 } 1457 1458 /* 1459 * Place it in the queue of disk activities for this disk 1460 */ 1461 bioqdisksort(&softc->bio_queue, bp); 1462 1463 splx(s); 1464 1465 /* 1466 * Schedule ourselves for performing the work. We do things 1467 * differently for changers. 1468 */ 1469 if ((softc->flags & CD_FLAG_CHANGER) == 0) 1470 xpt_schedule(periph, /* XXX priority */1); 1471 else 1472 cdschedule(periph, /* priority */ 1); 1473 1474 return; 1475} 1476 1477static void 1478cdstart(struct cam_periph *periph, union ccb *start_ccb) 1479{ 1480 struct cd_softc *softc; 1481 struct bio *bp; 1482 struct ccb_scsiio *csio; 1483 struct scsi_read_capacity_data *rcap; 1484 int s; 1485 1486 softc = (struct cd_softc *)periph->softc; 1487 1488 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 1489 1490 switch (softc->state) { 1491 case CD_STATE_NORMAL: 1492 { 1493 int oldspl; 1494 1495 s = splbio(); 1496 bp = bioq_first(&softc->bio_queue); 1497 if (periph->immediate_priority <= periph->pinfo.priority) { 1498 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 1499 1500 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1501 periph_links.sle); 1502 periph->immediate_priority = CAM_PRIORITY_NONE; 1503 splx(s); 1504 wakeup(&periph->ccb_list); 1505 } else if (bp == NULL) { 1506 splx(s); 1507 xpt_release_ccb(start_ccb); 1508 } else { 1509 bioq_remove(&softc->bio_queue, bp); 1510 1511 devstat_start_transaction_bio(softc->device_stats, bp); 1512 1513 scsi_read_write(&start_ccb->csio, 1514 /*retries*/4, 1515 /* cbfcnp */ cddone, 1516 MSG_SIMPLE_Q_TAG, 1517 /* read */bp->bio_cmd == BIO_READ, 1518 /* byte2 */ 0, 1519 /* minimum_cmd_size */ 10, 1520 /* lba */ bp->bio_blkno / 1521 (softc->params.blksize / DEV_BSIZE), 1522 bp->bio_bcount / softc->params.blksize, 1523 /* data_ptr */ bp->bio_data, 1524 /* dxfer_len */ bp->bio_bcount, 1525 /* sense_len */ SSD_FULL_SIZE, 1526 /* timeout */ 30000); 1527 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 1528 1529 1530 /* 1531 * Block out any asyncronous callbacks 1532 * while we touch the pending ccb list. 1533 */ 1534 oldspl = splcam(); 1535 LIST_INSERT_HEAD(&softc->pending_ccbs, 1536 &start_ccb->ccb_h, periph_links.le); 1537 softc->outstanding_cmds++; 1538 splx(oldspl); 1539 1540 /* We expect a unit attention from this device */ 1541 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 1542 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 1543 softc->flags &= ~CD_FLAG_RETRY_UA; 1544 } 1545 1546 start_ccb->ccb_h.ccb_bp = bp; 1547 bp = bioq_first(&softc->bio_queue); 1548 splx(s); 1549 1550 xpt_action(start_ccb); 1551 } 1552 if (bp != NULL) { 1553 /* Have more work to do, so ensure we stay scheduled */ 1554 xpt_schedule(periph, /* XXX priority */1); 1555 } 1556 break; 1557 } 1558 case CD_STATE_PROBE: 1559 { 1560 1561 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1562 M_TEMP, 1563 M_NOWAIT); 1564 if (rcap == NULL) { 1565 xpt_print_path(periph->path); 1566 printf("cdstart: Couldn't malloc read_capacity data\n"); 1567 /* cd_free_periph??? */ 1568 break; 1569 } 1570 csio = &start_ccb->csio; 1571 scsi_read_capacity(csio, 1572 /*retries*/1, 1573 cddone, 1574 MSG_SIMPLE_Q_TAG, 1575 rcap, 1576 SSD_FULL_SIZE, 1577 /*timeout*/20000); 1578 start_ccb->ccb_h.ccb_bp = NULL; 1579 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 1580 xpt_action(start_ccb); 1581 break; 1582 } 1583 } 1584} 1585 1586static void 1587cddone(struct cam_periph *periph, union ccb *done_ccb) 1588{ 1589 struct cd_softc *softc; 1590 struct ccb_scsiio *csio; 1591 1592 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 1593 1594 softc = (struct cd_softc *)periph->softc; 1595 csio = &done_ccb->csio; 1596 1597 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 1598 case CD_CCB_BUFFER_IO: 1599 { 1600 struct bio *bp; 1601 int error; 1602 int oldspl; 1603 1604 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1605 error = 0; 1606 1607 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1608 int sf; 1609 1610 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 1611 sf = SF_RETRY_UA; 1612 else 1613 sf = 0; 1614 1615 error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 1616 if (error == ERESTART) { 1617 /* 1618 * A retry was scheuled, so 1619 * just return. 1620 */ 1621 return; 1622 } 1623 } 1624 1625 if (error != 0) { 1626 int s; 1627 struct bio *q_bp; 1628 1629 xpt_print_path(periph->path); 1630 printf("cddone: got error %#x back\n", error); 1631 s = splbio(); 1632 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL) { 1633 bioq_remove(&softc->bio_queue, q_bp); 1634 q_bp->bio_resid = q_bp->bio_bcount; 1635 biofinish(q_bp, NULL, EIO); 1636 } 1637 splx(s); 1638 bp->bio_resid = bp->bio_bcount; 1639 bp->bio_error = error; 1640 bp->bio_flags |= BIO_ERROR; 1641 cam_release_devq(done_ccb->ccb_h.path, 1642 /*relsim_flags*/0, 1643 /*reduction*/0, 1644 /*timeout*/0, 1645 /*getcount_only*/0); 1646 1647 } else { 1648 bp->bio_resid = csio->resid; 1649 bp->bio_error = 0; 1650 if (bp->bio_resid != 0) { 1651 /* 1652 * Short transfer ??? 1653 * XXX: not sure this is correct for partial 1654 * transfers at EOM 1655 */ 1656 bp->bio_flags |= BIO_ERROR; 1657 } 1658 } 1659 1660 /* 1661 * Block out any asyncronous callbacks 1662 * while we touch the pending ccb list. 1663 */ 1664 oldspl = splcam(); 1665 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1666 softc->outstanding_cmds--; 1667 splx(oldspl); 1668 1669 if (softc->flags & CD_FLAG_CHANGER) 1670 cdchangerschedule(softc); 1671 1672 biofinish(bp, softc->device_stats, 0); 1673 break; 1674 } 1675 case CD_CCB_PROBE: 1676 { 1677 struct scsi_read_capacity_data *rdcap; 1678 char announce_buf[120]; /* 1679 * Currently (9/30/97) the 1680 * longest possible announce 1681 * buffer is 108 bytes, for the 1682 * first error case below. 1683 * That is 39 bytes for the 1684 * basic string, 16 bytes for the 1685 * biggest sense key (hardware 1686 * error), 52 bytes for the 1687 * text of the largest sense 1688 * qualifier valid for a CDROM, 1689 * (0x72, 0x03 or 0x04, 1690 * 0x03), and one byte for the 1691 * null terminating character. 1692 * To allow for longer strings, 1693 * the announce buffer is 120 1694 * bytes. 1695 */ 1696 struct cd_params *cdp; 1697 1698 cdp = &softc->params; 1699 1700 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1701 1702 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1703 cdp->blksize = scsi_4btoul (rdcap->length); 1704 1705 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1706 1707 snprintf(announce_buf, sizeof(announce_buf), 1708 "cd present [%lu x %lu byte records]", 1709 cdp->disksize, (u_long)cdp->blksize); 1710 1711 } else { 1712 int error; 1713 /* 1714 * Retry any UNIT ATTENTION type errors. They 1715 * are expected at boot. 1716 */ 1717 error = cderror(done_ccb, CAM_RETRY_SELTO, 1718 SF_RETRY_UA | SF_NO_PRINT); 1719 if (error == ERESTART) { 1720 /* 1721 * A retry was scheuled, so 1722 * just return. 1723 */ 1724 return; 1725 } else if (error != 0) { 1726 1727 struct scsi_sense_data *sense; 1728 int asc, ascq; 1729 int sense_key, error_code; 1730 int have_sense; 1731 cam_status status; 1732 struct ccb_getdev cgd; 1733 1734 /* Don't wedge this device's queue */ 1735 cam_release_devq(done_ccb->ccb_h.path, 1736 /*relsim_flags*/0, 1737 /*reduction*/0, 1738 /*timeout*/0, 1739 /*getcount_only*/0); 1740 1741 status = done_ccb->ccb_h.status; 1742 1743 xpt_setup_ccb(&cgd.ccb_h, 1744 done_ccb->ccb_h.path, 1745 /* priority */ 1); 1746 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1747 xpt_action((union ccb *)&cgd); 1748 1749 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1750 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1751 || ((status & CAM_AUTOSNS_VALID) == 0)) 1752 have_sense = FALSE; 1753 else 1754 have_sense = TRUE; 1755 1756 if (have_sense) { 1757 sense = &csio->sense_data; 1758 scsi_extract_sense(sense, &error_code, 1759 &sense_key, 1760 &asc, &ascq); 1761 } 1762 /* 1763 * Attach to anything that claims to be a 1764 * CDROM or WORM device, as long as it 1765 * doesn't return a "Logical unit not 1766 * supported" (0x25) error. 1767 */ 1768 if ((have_sense) && (asc != 0x25) 1769 && (error_code == SSD_CURRENT_ERROR)) { 1770 const char *sense_key_desc; 1771 const char *asc_desc; 1772 1773 scsi_sense_desc(sense_key, asc, ascq, 1774 &cgd.inq_data, 1775 &sense_key_desc, 1776 &asc_desc); 1777 snprintf(announce_buf, 1778 sizeof(announce_buf), 1779 "Attempt to query device " 1780 "size failed: %s, %s", 1781 sense_key_desc, 1782 asc_desc); 1783 } else if ((have_sense == 0) 1784 && ((status & CAM_STATUS_MASK) == 1785 CAM_SCSI_STATUS_ERROR) 1786 && (csio->scsi_status == 1787 SCSI_STATUS_BUSY)) { 1788 snprintf(announce_buf, 1789 sizeof(announce_buf), 1790 "Attempt to query device " 1791 "size failed: SCSI Status: %s", 1792 scsi_status_string(csio)); 1793 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1794 /* 1795 * We only print out an error for 1796 * CDROM type devices. For WORM 1797 * devices, we don't print out an 1798 * error since a few WORM devices 1799 * don't support CDROM commands. 1800 * If we have sense information, go 1801 * ahead and print it out. 1802 * Otherwise, just say that we 1803 * couldn't attach. 1804 */ 1805 1806 /* 1807 * Just print out the error, not 1808 * the full probe message, when we 1809 * don't attach. 1810 */ 1811 if (have_sense) 1812 scsi_sense_print( 1813 &done_ccb->csio); 1814 else { 1815 xpt_print_path(periph->path); 1816 printf("got CAM status %#x\n", 1817 done_ccb->ccb_h.status); 1818 } 1819 xpt_print_path(periph->path); 1820 printf("fatal error, failed" 1821 " to attach to device\n"); 1822 1823 /* 1824 * Invalidate this peripheral. 1825 */ 1826 cam_periph_invalidate(periph); 1827 1828 announce_buf[0] = '\0'; 1829 } else { 1830 1831 /* 1832 * Invalidate this peripheral. 1833 */ 1834 cam_periph_invalidate(periph); 1835 announce_buf[0] = '\0'; 1836 } 1837 } 1838 } 1839 free(rdcap, M_TEMP); 1840 if (announce_buf[0] != '\0') { 1841 xpt_announce_periph(periph, announce_buf); 1842 if (softc->flags & CD_FLAG_CHANGER) 1843 cdchangerschedule(softc); 1844 } 1845 softc->state = CD_STATE_NORMAL; 1846 /* 1847 * Since our peripheral may be invalidated by an error 1848 * above or an external event, we must release our CCB 1849 * before releasing the probe lock on the peripheral. 1850 * The peripheral will only go away once the last lock 1851 * is removed, and we need it around for the CCB release 1852 * operation. 1853 */ 1854 xpt_release_ccb(done_ccb); 1855 cam_periph_unlock(periph); 1856 return; 1857 } 1858 case CD_CCB_WAITING: 1859 { 1860 /* Caller will release the CCB */ 1861 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1862 ("trying to wakeup ccbwait\n")); 1863 1864 wakeup(&done_ccb->ccb_h.cbfcnp); 1865 return; 1866 } 1867 default: 1868 break; 1869 } 1870 xpt_release_ccb(done_ccb); 1871} 1872 1873static union cd_pages * 1874cdgetpage(struct cd_mode_params *mode_params) 1875{ 1876 union cd_pages *page; 1877 1878 if (mode_params->cdb_size == 10) 1879 page = (union cd_pages *)find_mode_page_10( 1880 (struct scsi_mode_header_10 *)mode_params->mode_buf); 1881 else 1882 page = (union cd_pages *)find_mode_page_6( 1883 (struct scsi_mode_header_6 *)mode_params->mode_buf); 1884 1885 return (page); 1886} 1887 1888static int 1889cdgetpagesize(int page_num) 1890{ 1891 int i; 1892 1893 for (i = 0; i < (sizeof(cd_page_size_table)/ 1894 sizeof(cd_page_size_table[0])); i++) { 1895 if (cd_page_size_table[i].page == page_num) 1896 return (cd_page_size_table[i].page_size); 1897 } 1898 1899 return (-1); 1900} 1901 1902static int 1903cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1904{ 1905 1906 struct cam_periph *periph; 1907 struct cd_softc *softc; 1908 int error; 1909 1910 periph = (struct cam_periph *)dev->si_drv1; 1911 if (periph == NULL) 1912 return(ENXIO); 1913 1914 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 1915 1916 softc = (struct cd_softc *)periph->softc; 1917 1918 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1919 ("trying to do ioctl %#lx\n", cmd)); 1920 1921 error = cam_periph_lock(periph, PRIBIO | PCATCH); 1922 1923 if (error != 0) 1924 return(error); 1925 /* 1926 * If we don't have media loaded, check for it. If still don't 1927 * have media loaded, we can only do a load or eject. 1928 */ 1929 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1930 && ((cmd != CDIOCCLOSE) 1931 && (cmd != CDIOCEJECT))) { 1932 error = cdcheckmedia(periph); 1933 if (error != 0) { 1934 cam_periph_unlock(periph); 1935 return (error); 1936 } 1937 } 1938 1939 switch (cmd) { 1940 1941 case DIOCGMEDIASIZE: 1942 *(off_t *)addr = 1943 (off_t)softc->params.blksize * softc->params.disksize; 1944 break; 1945 case DIOCGSECTORSIZE: 1946 *(u_int *)addr = softc->params.blksize; 1947 break; 1948 1949 case CDIOCPLAYTRACKS: 1950 { 1951 struct ioc_play_track *args 1952 = (struct ioc_play_track *) addr; 1953 struct cd_mode_params params; 1954 union cd_pages *page; 1955 1956 params.alloc_len = sizeof(union cd_mode_data_6_10); 1957 params.mode_buf = malloc(params.alloc_len, M_TEMP, 1958 M_WAITOK | M_ZERO); 1959 1960 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1961 ("trying to do CDIOCPLAYTRACKS\n")); 1962 1963 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1964 if (error) { 1965 free(params.mode_buf, M_TEMP); 1966 break; 1967 } 1968 page = cdgetpage(¶ms); 1969 1970 page->audio.flags &= ~CD_PA_SOTC; 1971 page->audio.flags |= CD_PA_IMMED; 1972 error = cdsetmode(periph, ¶ms); 1973 free(params.mode_buf, M_TEMP); 1974 if (error) 1975 break; 1976 1977 /* 1978 * This was originally implemented with the PLAY 1979 * AUDIO TRACK INDEX command, but that command was 1980 * deprecated after SCSI-2. Most (all?) SCSI CDROM 1981 * drives support it but ATAPI and ATAPI-derivative 1982 * drives don't seem to support it. So we keep a 1983 * cache of the table of contents and translate 1984 * track numbers to MSF format. 1985 */ 1986 if (softc->flags & CD_FLAG_VALID_TOC) { 1987 union msf_lba *sentry, *eentry; 1988 int st, et; 1989 1990 if (args->end_track < 1991 softc->toc.header.ending_track + 1) 1992 args->end_track++; 1993 if (args->end_track > 1994 softc->toc.header.ending_track + 1) 1995 args->end_track = 1996 softc->toc.header.ending_track + 1; 1997 st = args->start_track - 1998 softc->toc.header.starting_track; 1999 et = args->end_track - 2000 softc->toc.header.starting_track; 2001 if ((st < 0) 2002 || (et < 0) 2003 || (st > (softc->toc.header.ending_track - 2004 softc->toc.header.starting_track))) { 2005 error = EINVAL; 2006 break; 2007 } 2008 sentry = &softc->toc.entries[st].addr; 2009 eentry = &softc->toc.entries[et].addr; 2010 error = cdplaymsf(periph, 2011 sentry->msf.minute, 2012 sentry->msf.second, 2013 sentry->msf.frame, 2014 eentry->msf.minute, 2015 eentry->msf.second, 2016 eentry->msf.frame); 2017 } else { 2018 /* 2019 * If we don't have a valid TOC, try the 2020 * play track index command. It is part of 2021 * the SCSI-2 spec, but was removed in the 2022 * MMC specs. ATAPI and ATAPI-derived 2023 * drives don't support it. 2024 */ 2025 if (softc->quirks & CD_Q_BCD_TRACKS) { 2026 args->start_track = 2027 bin2bcd(args->start_track); 2028 args->end_track = 2029 bin2bcd(args->end_track); 2030 } 2031 error = cdplaytracks(periph, 2032 args->start_track, 2033 args->start_index, 2034 args->end_track, 2035 args->end_index); 2036 } 2037 } 2038 break; 2039 case CDIOCPLAYMSF: 2040 { 2041 struct ioc_play_msf *args 2042 = (struct ioc_play_msf *) addr; 2043 struct cd_mode_params params; 2044 union cd_pages *page; 2045 2046 params.alloc_len = sizeof(union cd_mode_data_6_10); 2047 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2048 M_WAITOK | M_ZERO); 2049 2050 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2051 ("trying to do CDIOCPLAYMSF\n")); 2052 2053 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2054 if (error) { 2055 free(params.mode_buf, M_TEMP); 2056 break; 2057 } 2058 page = cdgetpage(¶ms); 2059 2060 page->audio.flags &= ~CD_PA_SOTC; 2061 page->audio.flags |= CD_PA_IMMED; 2062 error = cdsetmode(periph, ¶ms); 2063 free(params.mode_buf, M_TEMP); 2064 if (error) 2065 break; 2066 error = cdplaymsf(periph, 2067 args->start_m, 2068 args->start_s, 2069 args->start_f, 2070 args->end_m, 2071 args->end_s, 2072 args->end_f); 2073 } 2074 break; 2075 case CDIOCPLAYBLOCKS: 2076 { 2077 struct ioc_play_blocks *args 2078 = (struct ioc_play_blocks *) addr; 2079 struct cd_mode_params params; 2080 union cd_pages *page; 2081 2082 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2083 ("trying to do CDIOCPLAYBLOCKS\n")); 2084 2085 params.alloc_len = sizeof(union cd_mode_data_6_10); 2086 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2087 M_WAITOK | M_ZERO); 2088 2089 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2090 if (error) { 2091 free(params.mode_buf, M_TEMP); 2092 break; 2093 } 2094 page = cdgetpage(¶ms); 2095 2096 page->audio.flags &= ~CD_PA_SOTC; 2097 page->audio.flags |= CD_PA_IMMED; 2098 error = cdsetmode(periph, ¶ms); 2099 free(params.mode_buf, M_TEMP); 2100 if (error) 2101 break; 2102 error = cdplay(periph, args->blk, args->len); 2103 } 2104 break; 2105 case CDIOCREADSUBCHANNEL: 2106 { 2107 struct ioc_read_subchannel *args 2108 = (struct ioc_read_subchannel *) addr; 2109 struct cd_sub_channel_info *data; 2110 u_int32_t len = args->data_len; 2111 2112 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2113 ("trying to do CDIOCREADSUBCHANNEL\n")); 2114 2115 data = malloc(sizeof(struct cd_sub_channel_info), 2116 M_TEMP, M_WAITOK); 2117 2118 if ((len > sizeof(struct cd_sub_channel_info)) || 2119 (len < sizeof(struct cd_sub_channel_header))) { 2120 printf( 2121 "scsi_cd: cdioctl: " 2122 "cdioreadsubchannel: error, len=%d\n", 2123 len); 2124 error = EINVAL; 2125 free(data, M_TEMP); 2126 break; 2127 } 2128 2129 if (softc->quirks & CD_Q_BCD_TRACKS) 2130 args->track = bin2bcd(args->track); 2131 2132 error = cdreadsubchannel(periph, args->address_format, 2133 args->data_format, args->track, data, len); 2134 2135 if (error) { 2136 free(data, M_TEMP); 2137 break; 2138 } 2139 if (softc->quirks & CD_Q_BCD_TRACKS) 2140 data->what.track_info.track_number = 2141 bcd2bin(data->what.track_info.track_number); 2142 len = min(len, ((data->header.data_len[0] << 8) + 2143 data->header.data_len[1] + 2144 sizeof(struct cd_sub_channel_header))); 2145 if (copyout(data, args->data, len) != 0) { 2146 error = EFAULT; 2147 } 2148 free(data, M_TEMP); 2149 } 2150 break; 2151 2152 case CDIOREADTOCHEADER: 2153 { 2154 struct ioc_toc_header *th; 2155 2156 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2157 ("trying to do CDIOREADTOCHEADER\n")); 2158 2159 th = malloc(sizeof(struct ioc_toc_header), M_TEMP, 2160 M_WAITOK); 2161 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2162 sizeof (*th), /*sense_flags*/0); 2163 if (error) { 2164 free(th, M_TEMP); 2165 break; 2166 } 2167 if (softc->quirks & CD_Q_BCD_TRACKS) { 2168 /* we are going to have to convert the BCD 2169 * encoding on the cd to what is expected 2170 */ 2171 th->starting_track = 2172 bcd2bin(th->starting_track); 2173 th->ending_track = bcd2bin(th->ending_track); 2174 } 2175 th->len = ntohs(th->len); 2176 bcopy(th, addr, sizeof(*th)); 2177 free(th, M_TEMP); 2178 } 2179 break; 2180 case CDIOREADTOCENTRYS: 2181 { 2182 struct cd_tocdata *data; 2183 struct cd_toc_single *lead; 2184 struct ioc_read_toc_entry *te = 2185 (struct ioc_read_toc_entry *) addr; 2186 struct ioc_toc_header *th; 2187 u_int32_t len, readlen, idx, num; 2188 u_int32_t starting_track = te->starting_track; 2189 2190 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2191 ("trying to do CDIOREADTOCENTRYS\n")); 2192 2193 data = malloc(sizeof(*data), M_TEMP, M_WAITOK); 2194 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK); 2195 2196 if (te->data_len < sizeof(struct cd_toc_entry) 2197 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2198 || (te->address_format != CD_MSF_FORMAT 2199 && te->address_format != CD_LBA_FORMAT)) { 2200 error = EINVAL; 2201 printf("scsi_cd: error in readtocentries, " 2202 "returning EINVAL\n"); 2203 free(data, M_TEMP); 2204 free(lead, M_TEMP); 2205 break; 2206 } 2207 2208 th = &data->header; 2209 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2210 sizeof (*th), /*sense_flags*/0); 2211 if (error) { 2212 free(data, M_TEMP); 2213 free(lead, M_TEMP); 2214 break; 2215 } 2216 2217 if (softc->quirks & CD_Q_BCD_TRACKS) { 2218 /* we are going to have to convert the BCD 2219 * encoding on the cd to what is expected 2220 */ 2221 th->starting_track = 2222 bcd2bin(th->starting_track); 2223 th->ending_track = bcd2bin(th->ending_track); 2224 } 2225 2226 if (starting_track == 0) 2227 starting_track = th->starting_track; 2228 else if (starting_track == LEADOUT) 2229 starting_track = th->ending_track + 1; 2230 else if (starting_track < th->starting_track || 2231 starting_track > th->ending_track + 1) { 2232 printf("scsi_cd: error in readtocentries, " 2233 "returning EINVAL\n"); 2234 free(data, M_TEMP); 2235 free(lead, M_TEMP); 2236 error = EINVAL; 2237 break; 2238 } 2239 2240 /* calculate reading length without leadout entry */ 2241 readlen = (th->ending_track - starting_track + 1) * 2242 sizeof(struct cd_toc_entry); 2243 2244 /* and with leadout entry */ 2245 len = readlen + sizeof(struct cd_toc_entry); 2246 if (te->data_len < len) { 2247 len = te->data_len; 2248 if (readlen > len) 2249 readlen = len; 2250 } 2251 if (len > sizeof(data->entries)) { 2252 printf("scsi_cd: error in readtocentries, " 2253 "returning EINVAL\n"); 2254 error = EINVAL; 2255 free(data, M_TEMP); 2256 free(lead, M_TEMP); 2257 break; 2258 } 2259 num = len / sizeof(struct cd_toc_entry); 2260 2261 if (readlen > 0) { 2262 error = cdreadtoc(periph, te->address_format, 2263 starting_track, 2264 (u_int8_t *)data, 2265 readlen + sizeof (*th), 2266 /*sense_flags*/0); 2267 if (error) { 2268 free(data, M_TEMP); 2269 free(lead, M_TEMP); 2270 break; 2271 } 2272 } 2273 2274 /* make leadout entry if needed */ 2275 idx = starting_track + num - 1; 2276 if (softc->quirks & CD_Q_BCD_TRACKS) 2277 th->ending_track = bcd2bin(th->ending_track); 2278 if (idx == th->ending_track + 1) { 2279 error = cdreadtoc(periph, te->address_format, 2280 LEADOUT, (u_int8_t *)lead, 2281 sizeof(*lead), 2282 /*sense_flags*/0); 2283 if (error) { 2284 free(data, M_TEMP); 2285 free(lead, M_TEMP); 2286 break; 2287 } 2288 data->entries[idx - starting_track] = 2289 lead->entry; 2290 } 2291 if (softc->quirks & CD_Q_BCD_TRACKS) { 2292 for (idx = 0; idx < num - 1; idx++) { 2293 data->entries[idx].track = 2294 bcd2bin(data->entries[idx].track); 2295 } 2296 } 2297 2298 error = copyout(data->entries, te->data, len); 2299 free(data, M_TEMP); 2300 free(lead, M_TEMP); 2301 } 2302 break; 2303 case CDIOREADTOCENTRY: 2304 { 2305 struct cd_toc_single *data; 2306 struct ioc_read_toc_single_entry *te = 2307 (struct ioc_read_toc_single_entry *) addr; 2308 struct ioc_toc_header *th; 2309 u_int32_t track; 2310 2311 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2312 ("trying to do CDIOREADTOCENTRY\n")); 2313 2314 data = malloc(sizeof(*data), M_TEMP, M_WAITOK); 2315 2316 if (te->address_format != CD_MSF_FORMAT 2317 && te->address_format != CD_LBA_FORMAT) { 2318 printf("error in readtocentry, " 2319 " returning EINVAL\n"); 2320 free(data, M_TEMP); 2321 error = EINVAL; 2322 break; 2323 } 2324 2325 th = &data->header; 2326 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2327 sizeof (*th), /*sense_flags*/0); 2328 if (error) { 2329 free(data, M_TEMP); 2330 break; 2331 } 2332 2333 if (softc->quirks & CD_Q_BCD_TRACKS) { 2334 /* we are going to have to convert the BCD 2335 * encoding on the cd to what is expected 2336 */ 2337 th->starting_track = 2338 bcd2bin(th->starting_track); 2339 th->ending_track = bcd2bin(th->ending_track); 2340 } 2341 track = te->track; 2342 if (track == 0) 2343 track = th->starting_track; 2344 else if (track == LEADOUT) 2345 /* OK */; 2346 else if (track < th->starting_track || 2347 track > th->ending_track + 1) { 2348 printf("error in readtocentry, " 2349 " returning EINVAL\n"); 2350 free(data, M_TEMP); 2351 error = EINVAL; 2352 break; 2353 } 2354 2355 error = cdreadtoc(periph, te->address_format, track, 2356 (u_int8_t *)data, sizeof(*data), 2357 /*sense_flags*/0); 2358 if (error) { 2359 free(data, M_TEMP); 2360 break; 2361 } 2362 2363 if (softc->quirks & CD_Q_BCD_TRACKS) 2364 data->entry.track = bcd2bin(data->entry.track); 2365 bcopy(&data->entry, &te->entry, 2366 sizeof(struct cd_toc_entry)); 2367 free(data, M_TEMP); 2368 } 2369 break; 2370 case CDIOCSETPATCH: 2371 { 2372 struct ioc_patch *arg = (struct ioc_patch *)addr; 2373 struct cd_mode_params params; 2374 union cd_pages *page; 2375 2376 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2377 ("trying to do CDIOCSETPATCH\n")); 2378 2379 params.alloc_len = sizeof(union cd_mode_data_6_10); 2380 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2381 M_WAITOK | M_ZERO); 2382 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2383 if (error) { 2384 free(params.mode_buf, M_TEMP); 2385 break; 2386 } 2387 page = cdgetpage(¶ms); 2388 2389 page->audio.port[LEFT_PORT].channels = 2390 arg->patch[0]; 2391 page->audio.port[RIGHT_PORT].channels = 2392 arg->patch[1]; 2393 page->audio.port[2].channels = arg->patch[2]; 2394 page->audio.port[3].channels = arg->patch[3]; 2395 error = cdsetmode(periph, ¶ms); 2396 free(params.mode_buf, M_TEMP); 2397 } 2398 break; 2399 case CDIOCGETVOL: 2400 { 2401 struct ioc_vol *arg = (struct ioc_vol *) addr; 2402 struct cd_mode_params params; 2403 union cd_pages *page; 2404 2405 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2406 ("trying to do CDIOCGETVOL\n")); 2407 2408 params.alloc_len = sizeof(union cd_mode_data_6_10); 2409 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2410 M_WAITOK | M_ZERO); 2411 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2412 if (error) { 2413 free(params.mode_buf, M_TEMP); 2414 break; 2415 } 2416 page = cdgetpage(¶ms); 2417 2418 arg->vol[LEFT_PORT] = 2419 page->audio.port[LEFT_PORT].volume; 2420 arg->vol[RIGHT_PORT] = 2421 page->audio.port[RIGHT_PORT].volume; 2422 arg->vol[2] = page->audio.port[2].volume; 2423 arg->vol[3] = page->audio.port[3].volume; 2424 free(params.mode_buf, M_TEMP); 2425 } 2426 break; 2427 case CDIOCSETVOL: 2428 { 2429 struct ioc_vol *arg = (struct ioc_vol *) addr; 2430 struct cd_mode_params params; 2431 union cd_pages *page; 2432 2433 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2434 ("trying to do CDIOCSETVOL\n")); 2435 2436 params.alloc_len = sizeof(union cd_mode_data_6_10); 2437 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2438 M_WAITOK | M_ZERO); 2439 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2440 if (error) { 2441 free(params.mode_buf, M_TEMP); 2442 break; 2443 } 2444 page = cdgetpage(¶ms); 2445 2446 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2447 page->audio.port[LEFT_PORT].volume = 2448 arg->vol[LEFT_PORT]; 2449 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2450 page->audio.port[RIGHT_PORT].volume = 2451 arg->vol[RIGHT_PORT]; 2452 page->audio.port[2].volume = arg->vol[2]; 2453 page->audio.port[3].volume = arg->vol[3]; 2454 error = cdsetmode(periph, ¶ms); 2455 free(params.mode_buf, M_TEMP); 2456 } 2457 break; 2458 case CDIOCSETMONO: 2459 { 2460 struct cd_mode_params params; 2461 union cd_pages *page; 2462 2463 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2464 ("trying to do CDIOCSETMONO\n")); 2465 2466 params.alloc_len = sizeof(union cd_mode_data_6_10); 2467 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2468 M_WAITOK | M_ZERO); 2469 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2470 if (error) { 2471 free(params.mode_buf, M_TEMP); 2472 break; 2473 } 2474 page = cdgetpage(¶ms); 2475 2476 page->audio.port[LEFT_PORT].channels = 2477 LEFT_CHANNEL | RIGHT_CHANNEL; 2478 page->audio.port[RIGHT_PORT].channels = 2479 LEFT_CHANNEL | RIGHT_CHANNEL; 2480 page->audio.port[2].channels = 0; 2481 page->audio.port[3].channels = 0; 2482 error = cdsetmode(periph, ¶ms); 2483 free(params.mode_buf, M_TEMP); 2484 } 2485 break; 2486 case CDIOCSETSTEREO: 2487 { 2488 struct cd_mode_params params; 2489 union cd_pages *page; 2490 2491 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2492 ("trying to do CDIOCSETSTEREO\n")); 2493 2494 params.alloc_len = sizeof(union cd_mode_data_6_10); 2495 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2496 M_WAITOK | M_ZERO); 2497 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2498 if (error) { 2499 free(params.mode_buf, M_TEMP); 2500 break; 2501 } 2502 page = cdgetpage(¶ms); 2503 2504 page->audio.port[LEFT_PORT].channels = 2505 LEFT_CHANNEL; 2506 page->audio.port[RIGHT_PORT].channels = 2507 RIGHT_CHANNEL; 2508 page->audio.port[2].channels = 0; 2509 page->audio.port[3].channels = 0; 2510 error = cdsetmode(periph, ¶ms); 2511 free(params.mode_buf, M_TEMP); 2512 } 2513 break; 2514 case CDIOCSETMUTE: 2515 { 2516 struct cd_mode_params params; 2517 union cd_pages *page; 2518 2519 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2520 ("trying to do CDIOCSETMUTE\n")); 2521 2522 params.alloc_len = sizeof(union cd_mode_data_6_10); 2523 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2524 M_WAITOK | M_ZERO); 2525 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2526 if (error) { 2527 free(¶ms, M_TEMP); 2528 break; 2529 } 2530 page = cdgetpage(¶ms); 2531 2532 page->audio.port[LEFT_PORT].channels = 0; 2533 page->audio.port[RIGHT_PORT].channels = 0; 2534 page->audio.port[2].channels = 0; 2535 page->audio.port[3].channels = 0; 2536 error = cdsetmode(periph, ¶ms); 2537 free(params.mode_buf, M_TEMP); 2538 } 2539 break; 2540 case CDIOCSETLEFT: 2541 { 2542 struct cd_mode_params params; 2543 union cd_pages *page; 2544 2545 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2546 ("trying to do CDIOCSETLEFT\n")); 2547 2548 params.alloc_len = sizeof(union cd_mode_data_6_10); 2549 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2550 M_WAITOK | M_ZERO); 2551 2552 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2553 if (error) { 2554 free(params.mode_buf, M_TEMP); 2555 break; 2556 } 2557 page = cdgetpage(¶ms); 2558 2559 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2560 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2561 page->audio.port[2].channels = 0; 2562 page->audio.port[3].channels = 0; 2563 error = cdsetmode(periph, ¶ms); 2564 free(params.mode_buf, M_TEMP); 2565 } 2566 break; 2567 case CDIOCSETRIGHT: 2568 { 2569 struct cd_mode_params params; 2570 union cd_pages *page; 2571 2572 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2573 ("trying to do CDIOCSETRIGHT\n")); 2574 2575 params.alloc_len = sizeof(union cd_mode_data_6_10); 2576 params.mode_buf = malloc(params.alloc_len, M_TEMP, 2577 M_WAITOK | M_ZERO); 2578 2579 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2580 if (error) { 2581 free(params.mode_buf, M_TEMP); 2582 break; 2583 } 2584 page = cdgetpage(¶ms); 2585 2586 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2587 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2588 page->audio.port[2].channels = 0; 2589 page->audio.port[3].channels = 0; 2590 error = cdsetmode(periph, ¶ms); 2591 free(params.mode_buf, M_TEMP); 2592 } 2593 break; 2594 case CDIOCRESUME: 2595 error = cdpause(periph, 1); 2596 break; 2597 case CDIOCPAUSE: 2598 error = cdpause(periph, 0); 2599 break; 2600 case CDIOCSTART: 2601 error = cdstartunit(periph, 0); 2602 break; 2603 case CDIOCCLOSE: 2604 error = cdstartunit(periph, 1); 2605 break; 2606 case CDIOCSTOP: 2607 error = cdstopunit(periph, 0); 2608 break; 2609 case CDIOCEJECT: 2610 error = cdstopunit(periph, 1); 2611 break; 2612 case CDIOCALLOW: 2613 cdprevent(periph, PR_ALLOW); 2614 break; 2615 case CDIOCPREVENT: 2616 cdprevent(periph, PR_PREVENT); 2617 break; 2618 case CDIOCSETDEBUG: 2619 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2620 error = ENOTTY; 2621 break; 2622 case CDIOCCLRDEBUG: 2623 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2624 error = ENOTTY; 2625 break; 2626 case CDIOCRESET: 2627 /* return (cd_reset(periph)); */ 2628 error = ENOTTY; 2629 break; 2630 case CDRIOCREADSPEED: 2631 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2632 break; 2633 case CDRIOCWRITESPEED: 2634 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2635 break; 2636 case DVDIOCSENDKEY: 2637 case DVDIOCREPORTKEY: { 2638 struct dvd_authinfo *authinfo; 2639 2640 authinfo = (struct dvd_authinfo *)addr; 2641 2642 if (cmd == DVDIOCREPORTKEY) 2643 error = cdreportkey(periph, authinfo); 2644 else 2645 error = cdsendkey(periph, authinfo); 2646 break; 2647 } 2648 case DVDIOCREADSTRUCTURE: { 2649 struct dvd_struct *dvdstruct; 2650 2651 dvdstruct = (struct dvd_struct *)addr; 2652 2653 error = cdreaddvdstructure(periph, dvdstruct); 2654 2655 break; 2656 } 2657 default: 2658 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2659 break; 2660 } 2661 2662 cam_periph_unlock(periph); 2663 2664 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2665 if (error && bootverbose) { 2666 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2667 } 2668 2669 return (error); 2670} 2671 2672static void 2673cdprevent(struct cam_periph *periph, int action) 2674{ 2675 union ccb *ccb; 2676 struct cd_softc *softc; 2677 int error; 2678 2679 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2680 2681 softc = (struct cd_softc *)periph->softc; 2682 2683 if (((action == PR_ALLOW) 2684 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2685 || ((action == PR_PREVENT) 2686 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2687 return; 2688 } 2689 2690 ccb = cdgetccb(periph, /* priority */ 1); 2691 2692 scsi_prevent(&ccb->csio, 2693 /*retries*/ 1, 2694 cddone, 2695 MSG_SIMPLE_Q_TAG, 2696 action, 2697 SSD_FULL_SIZE, 2698 /* timeout */60000); 2699 2700 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2701 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2702 2703 xpt_release_ccb(ccb); 2704 2705 if (error == 0) { 2706 if (action == PR_ALLOW) 2707 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2708 else 2709 softc->flags |= CD_FLAG_DISC_LOCKED; 2710 } 2711} 2712 2713static int 2714cdcheckmedia(struct cam_periph *periph) 2715{ 2716 struct cd_softc *softc; 2717 struct ioc_toc_header *toch; 2718 struct cd_toc_single leadout; 2719 u_int32_t size, toclen; 2720 int error, num_entries, cdindex; 2721 2722 softc = (struct cd_softc *)periph->softc; 2723 2724 cdprevent(periph, PR_PREVENT); 2725 2726 /* 2727 * Get the disc size and block size. If we can't get it, we don't 2728 * have media, most likely. 2729 */ 2730 if ((error = cdsize(periph, &size)) != 0) { 2731 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2732 cdprevent(periph, PR_ALLOW); 2733 return (error); 2734 } else 2735 softc->flags |= CD_FLAG_VALID_MEDIA; 2736 2737 /* 2738 * Now we check the table of contents. This (currently) is only 2739 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2740 * things like present a separate entry in /dev for each track, 2741 * like that acd(4) driver does. 2742 */ 2743 bzero(&softc->toc, sizeof(softc->toc)); 2744 toch = &softc->toc.header; 2745 /* 2746 * We will get errors here for media that doesn't have a table of 2747 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2748 * command is presented for a DDCD/CD-R/RW media, where the first TOC 2749 * has not been recorded (no complete session) and the Format codes 2750 * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2751 * with an INVALID FIELD IN CDB. Devices that are not capable of 2752 * reading an incomplete session on DDC/CD-R/RW media shall report 2753 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2754 * 2755 * So this isn't fatal if we can't read the table of contents, it 2756 * just means that the user won't be able to issue the play tracks 2757 * ioctl, and likely lots of other stuff won't work either. They 2758 * need to burn the CD before we can do a whole lot with it. So 2759 * we don't print anything here if we get an error back. 2760 */ 2761 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2762 SF_NO_PRINT); 2763 /* 2764 * Errors in reading the table of contents aren't fatal, we just 2765 * won't have a valid table of contents cached. 2766 */ 2767 if (error != 0) { 2768 error = 0; 2769 bzero(&softc->toc, sizeof(softc->toc)); 2770 goto bailout; 2771 } 2772 2773 if (softc->quirks & CD_Q_BCD_TRACKS) { 2774 toch->starting_track = bcd2bin(toch->starting_track); 2775 toch->ending_track = bcd2bin(toch->ending_track); 2776 } 2777 2778 /* Number of TOC entries, plus leadout */ 2779 num_entries = (toch->ending_track - toch->starting_track) + 2; 2780 2781 if (num_entries <= 0) 2782 goto bailout; 2783 2784 toclen = num_entries * sizeof(struct cd_toc_entry); 2785 2786 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2787 (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2788 SF_NO_PRINT); 2789 if (error != 0) { 2790 error = 0; 2791 bzero(&softc->toc, sizeof(softc->toc)); 2792 goto bailout; 2793 } 2794 2795 if (softc->quirks & CD_Q_BCD_TRACKS) { 2796 toch->starting_track = bcd2bin(toch->starting_track); 2797 toch->ending_track = bcd2bin(toch->ending_track); 2798 } 2799 /* 2800 * XXX KDM is this necessary? Probably only if the drive doesn't 2801 * return leadout information with the table of contents. 2802 */ 2803 cdindex = toch->starting_track + num_entries -1; 2804 if (cdindex == toch->ending_track + 1) { 2805 2806 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2807 (u_int8_t *)&leadout, sizeof(leadout), 2808 SF_NO_PRINT); 2809 if (error != 0) { 2810 error = 0; 2811 goto bailout; 2812 } 2813 softc->toc.entries[cdindex - toch->starting_track] = 2814 leadout.entry; 2815 } 2816 if (softc->quirks & CD_Q_BCD_TRACKS) { 2817 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2818 softc->toc.entries[cdindex].track = 2819 bcd2bin(softc->toc.entries[cdindex].track); 2820 } 2821 } 2822 2823 softc->flags |= CD_FLAG_VALID_TOC; 2824 2825bailout: 2826 2827 /* 2828 * We unconditionally (re)set the blocksize each time the 2829 * CD device is opened. This is because the CD can change, 2830 * and therefore the blocksize might change. 2831 * XXX problems here if some slice or partition is still 2832 * open with the old size? 2833 */ 2834 if ((softc->device_stats->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2835 softc->device_stats->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2836 softc->device_stats->block_size = softc->params.blksize; 2837 2838 return (error); 2839} 2840 2841static int 2842cdsize(struct cam_periph *periph, u_int32_t *size) 2843{ 2844 struct cd_softc *softc; 2845 union ccb *ccb; 2846 struct scsi_read_capacity_data *rcap_buf; 2847 int error; 2848 2849 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2850 2851 softc = (struct cd_softc *)periph->softc; 2852 2853 ccb = cdgetccb(periph, /* priority */ 1); 2854 2855 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2856 M_TEMP, M_WAITOK); 2857 2858 scsi_read_capacity(&ccb->csio, 2859 /*retries*/ 1, 2860 cddone, 2861 MSG_SIMPLE_Q_TAG, 2862 rcap_buf, 2863 SSD_FULL_SIZE, 2864 /* timeout */20000); 2865 2866 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2867 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2868 2869 xpt_release_ccb(ccb); 2870 2871 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 2872 softc->params.blksize = scsi_4btoul(rcap_buf->length); 2873 /* 2874 * SCSI-3 mandates that the reported blocksize shall be 2048. 2875 * Older drives sometimes report funny values, trim it down to 2876 * 2048, or other parts of the kernel will get confused. 2877 * 2878 * XXX we leave drives alone that might report 512 bytes, as 2879 * well as drives reporting more weird sizes like perhaps 4K. 2880 */ 2881 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 2882 softc->params.blksize = 2048; 2883 2884 free(rcap_buf, M_TEMP); 2885 *size = softc->params.disksize; 2886 2887 return (error); 2888 2889} 2890 2891static int 2892cd6byteworkaround(union ccb *ccb) 2893{ 2894 u_int8_t *cdb; 2895 struct cam_periph *periph; 2896 struct cd_softc *softc; 2897 struct cd_mode_params *params; 2898 int frozen, found; 2899 2900 periph = xpt_path_periph(ccb->ccb_h.path); 2901 softc = (struct cd_softc *)periph->softc; 2902 2903 cdb = ccb->csio.cdb_io.cdb_bytes; 2904 2905 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2906 || ((cdb[0] != MODE_SENSE_6) 2907 && (cdb[0] != MODE_SELECT_6))) 2908 return (0); 2909 2910 /* 2911 * Because there is no convenient place to stash the overall 2912 * cd_mode_params structure pointer, we have to grab it like this. 2913 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2914 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2915 * 2916 * XXX It would be nice if, at some point, we could increase the 2917 * number of available peripheral private pointers. Both pointers 2918 * are currently used in most every peripheral driver. 2919 */ 2920 found = 0; 2921 2922 STAILQ_FOREACH(params, &softc->mode_queue, links) { 2923 if (params->mode_buf == ccb->csio.data_ptr) { 2924 found = 1; 2925 break; 2926 } 2927 } 2928 2929 /* 2930 * This shouldn't happen. All mode sense and mode select 2931 * operations in the cd(4) driver MUST go through cdgetmode() and 2932 * cdsetmode()! 2933 */ 2934 if (found == 0) { 2935 xpt_print_path(periph->path); 2936 printf("mode buffer not found in mode queue!\n"); 2937 return (0); 2938 } 2939 2940 params->cdb_size = 10; 2941 softc->minimum_command_size = 10; 2942 xpt_print_path(ccb->ccb_h.path); 2943 printf("%s(6) failed, increasing minimum CDB size to 10 bytes\n", 2944 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 2945 2946 if (cdb[0] == MODE_SENSE_6) { 2947 struct scsi_mode_sense_10 ms10; 2948 struct scsi_mode_sense_6 *ms6; 2949 int len; 2950 2951 ms6 = (struct scsi_mode_sense_6 *)cdb; 2952 2953 bzero(&ms10, sizeof(ms10)); 2954 ms10.opcode = MODE_SENSE_10; 2955 ms10.byte2 = ms6->byte2; 2956 ms10.page = ms6->page; 2957 2958 /* 2959 * 10 byte mode header, block descriptor, 2960 * sizeof(union cd_pages) 2961 */ 2962 len = sizeof(struct cd_mode_data_10); 2963 ccb->csio.dxfer_len = len; 2964 2965 scsi_ulto2b(len, ms10.length); 2966 ms10.control = ms6->control; 2967 bcopy(&ms10, cdb, 10); 2968 ccb->csio.cdb_len = 10; 2969 } else { 2970 struct scsi_mode_select_10 ms10; 2971 struct scsi_mode_select_6 *ms6; 2972 struct scsi_mode_header_6 *header6; 2973 struct scsi_mode_header_10 *header10; 2974 struct scsi_mode_page_header *page_header; 2975 int blk_desc_len, page_num, page_size, len; 2976 2977 ms6 = (struct scsi_mode_select_6 *)cdb; 2978 2979 bzero(&ms10, sizeof(ms10)); 2980 ms10.opcode = MODE_SELECT_10; 2981 ms10.byte2 = ms6->byte2; 2982 2983 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 2984 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 2985 2986 page_header = find_mode_page_6(header6); 2987 page_num = page_header->page_code; 2988 2989 blk_desc_len = header6->blk_desc_len; 2990 2991 page_size = cdgetpagesize(page_num); 2992 2993 if (page_size != (page_header->page_length + 2994 sizeof(*page_header))) 2995 page_size = page_header->page_length + 2996 sizeof(*page_header); 2997 2998 len = sizeof(*header10) + blk_desc_len + page_size; 2999 3000 len = min(params->alloc_len, len); 3001 3002 /* 3003 * Since the 6 byte parameter header is shorter than the 10 3004 * byte parameter header, we need to copy the actual mode 3005 * page data, and the block descriptor, if any, so things wind 3006 * up in the right place. The regions will overlap, but 3007 * bcopy() does the right thing. 3008 */ 3009 bcopy(params->mode_buf + sizeof(*header6), 3010 params->mode_buf + sizeof(*header10), 3011 len - sizeof(*header10)); 3012 3013 /* Make sure these fields are set correctly. */ 3014 scsi_ulto2b(0, header10->data_length); 3015 header10->medium_type = 0; 3016 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3017 3018 ccb->csio.dxfer_len = len; 3019 3020 scsi_ulto2b(len, ms10.length); 3021 ms10.control = ms6->control; 3022 bcopy(&ms10, cdb, 10); 3023 ccb->csio.cdb_len = 10; 3024 } 3025 3026 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3027 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3028 xpt_action(ccb); 3029 if (frozen) { 3030 cam_release_devq(ccb->ccb_h.path, 3031 /*relsim_flags*/0, 3032 /*openings*/0, 3033 /*timeout*/0, 3034 /*getcount_only*/0); 3035 } 3036 3037 return (ERESTART); 3038} 3039 3040static int 3041cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3042{ 3043 struct cd_softc *softc; 3044 struct cam_periph *periph; 3045 int error; 3046 3047 periph = xpt_path_periph(ccb->ccb_h.path); 3048 softc = (struct cd_softc *)periph->softc; 3049 3050 error = 0; 3051 3052 /* 3053 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3054 * CDB comes back with this particular error, try transforming it 3055 * into the 10 byte version. 3056 */ 3057 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3058 error = cd6byteworkaround(ccb); 3059 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3060 CAM_SCSI_STATUS_ERROR) 3061 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3062 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3063 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3064 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3065 int sense_key, error_code, asc, ascq; 3066 3067 scsi_extract_sense(&ccb->csio.sense_data, 3068 &error_code, &sense_key, &asc, &ascq); 3069 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3070 error = cd6byteworkaround(ccb); 3071 } 3072 3073 if (error == ERESTART) 3074 return (error); 3075 3076 /* 3077 * XXX 3078 * Until we have a better way of doing pack validation, 3079 * don't treat UAs as errors. 3080 */ 3081 sense_flags |= SF_RETRY_UA; 3082 return (cam_periph_error(ccb, cam_flags, sense_flags, 3083 &softc->saved_ccb)); 3084} 3085 3086/* 3087 * Read table of contents 3088 */ 3089static int 3090cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3091 u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 3092{ 3093 struct scsi_read_toc *scsi_cmd; 3094 u_int32_t ntoc; 3095 struct ccb_scsiio *csio; 3096 union ccb *ccb; 3097 int error; 3098 3099 ntoc = len; 3100 error = 0; 3101 3102 ccb = cdgetccb(periph, /* priority */ 1); 3103 3104 csio = &ccb->csio; 3105 3106 cam_fill_csio(csio, 3107 /* retries */ 1, 3108 /* cbfcnp */ cddone, 3109 /* flags */ CAM_DIR_IN, 3110 /* tag_action */ MSG_SIMPLE_Q_TAG, 3111 /* data_ptr */ data, 3112 /* dxfer_len */ len, 3113 /* sense_len */ SSD_FULL_SIZE, 3114 sizeof(struct scsi_read_toc), 3115 /* timeout */ 50000); 3116 3117 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 3118 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3119 3120 if (mode == CD_MSF_FORMAT) 3121 scsi_cmd->byte2 |= CD_MSF; 3122 scsi_cmd->from_track = start; 3123 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 3124 scsi_cmd->data_len[0] = (ntoc) >> 8; 3125 scsi_cmd->data_len[1] = (ntoc) & 0xff; 3126 3127 scsi_cmd->op_code = READ_TOC; 3128 3129 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3130 /*sense_flags*/SF_RETRY_UA | sense_flags); 3131 3132 xpt_release_ccb(ccb); 3133 3134 return(error); 3135} 3136 3137static int 3138cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 3139 u_int32_t format, int track, 3140 struct cd_sub_channel_info *data, u_int32_t len) 3141{ 3142 struct scsi_read_subchannel *scsi_cmd; 3143 struct ccb_scsiio *csio; 3144 union ccb *ccb; 3145 int error; 3146 3147 error = 0; 3148 3149 ccb = cdgetccb(periph, /* priority */ 1); 3150 3151 csio = &ccb->csio; 3152 3153 cam_fill_csio(csio, 3154 /* retries */ 1, 3155 /* cbfcnp */ cddone, 3156 /* flags */ CAM_DIR_IN, 3157 /* tag_action */ MSG_SIMPLE_Q_TAG, 3158 /* data_ptr */ (u_int8_t *)data, 3159 /* dxfer_len */ len, 3160 /* sense_len */ SSD_FULL_SIZE, 3161 sizeof(struct scsi_read_subchannel), 3162 /* timeout */ 50000); 3163 3164 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 3165 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3166 3167 scsi_cmd->op_code = READ_SUBCHANNEL; 3168 if (mode == CD_MSF_FORMAT) 3169 scsi_cmd->byte1 |= CD_MSF; 3170 scsi_cmd->byte2 = SRS_SUBQ; 3171 scsi_cmd->subchan_format = format; 3172 scsi_cmd->track = track; 3173 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 3174 scsi_cmd->control = 0; 3175 3176 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3177 /*sense_flags*/SF_RETRY_UA); 3178 3179 xpt_release_ccb(ccb); 3180 3181 return(error); 3182} 3183 3184 3185/* 3186 * All MODE_SENSE requests in the cd(4) driver MUST go through this 3187 * routine. See comments in cd6byteworkaround() for details. 3188 */ 3189static int 3190cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3191 u_int32_t page) 3192{ 3193 struct ccb_scsiio *csio; 3194 struct cd_softc *softc; 3195 union ccb *ccb; 3196 int param_len; 3197 int error; 3198 3199 softc = (struct cd_softc *)periph->softc; 3200 3201 ccb = cdgetccb(periph, /* priority */ 1); 3202 3203 csio = &ccb->csio; 3204 3205 data->cdb_size = softc->minimum_command_size; 3206 if (data->cdb_size < 10) 3207 param_len = sizeof(struct cd_mode_data); 3208 else 3209 param_len = sizeof(struct cd_mode_data_10); 3210 3211 /* Don't say we've got more room than we actually allocated */ 3212 param_len = min(param_len, data->alloc_len); 3213 3214 scsi_mode_sense_len(csio, 3215 /* retries */ 1, 3216 /* cbfcnp */ cddone, 3217 /* tag_action */ MSG_SIMPLE_Q_TAG, 3218 /* dbd */ 0, 3219 /* page_code */ SMS_PAGE_CTRL_CURRENT, 3220 /* page */ page, 3221 /* param_buf */ data->mode_buf, 3222 /* param_len */ param_len, 3223 /* minimum_cmd_size */ softc->minimum_command_size, 3224 /* sense_len */ SSD_FULL_SIZE, 3225 /* timeout */ 50000); 3226 3227 /* 3228 * It would be nice not to have to do this, but there's no 3229 * available pointer in the CCB that would allow us to stuff the 3230 * mode params structure in there and retrieve it in 3231 * cd6byteworkaround(), so we can set the cdb size. The cdb size 3232 * lets the caller know what CDB size we ended up using, so they 3233 * can find the actual mode page offset. 3234 */ 3235 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3236 3237 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3238 /*sense_flags*/SF_RETRY_UA); 3239 3240 xpt_release_ccb(ccb); 3241 3242 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3243 3244 /* 3245 * This is a bit of belt-and-suspenders checking, but if we run 3246 * into a situation where the target sends back multiple block 3247 * descriptors, we might not have enough space in the buffer to 3248 * see the whole mode page. Better to return an error than 3249 * potentially access memory beyond our malloced region. 3250 */ 3251 if (error == 0) { 3252 u_int32_t data_len; 3253 3254 if (data->cdb_size == 10) { 3255 struct scsi_mode_header_10 *hdr10; 3256 3257 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3258 data_len = scsi_2btoul(hdr10->data_length); 3259 data_len += sizeof(hdr10->data_length); 3260 } else { 3261 struct scsi_mode_header_6 *hdr6; 3262 3263 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3264 data_len = hdr6->data_length; 3265 data_len += sizeof(hdr6->data_length); 3266 } 3267 3268 /* 3269 * Complain if there is more mode data available than we 3270 * allocated space for. This could potentially happen if 3271 * we miscalculated the page length for some reason, if the 3272 * drive returns multiple block descriptors, or if it sets 3273 * the data length incorrectly. 3274 */ 3275 if (data_len > data->alloc_len) { 3276 xpt_print_path(periph->path); 3277 printf("allocated modepage %d length %d < returned " 3278 "length %d\n", page, data->alloc_len, data_len); 3279 3280 error = ENOSPC; 3281 } 3282 } 3283 return (error); 3284} 3285 3286/* 3287 * All MODE_SELECT requests in the cd(4) driver MUST go through this 3288 * routine. See comments in cd6byteworkaround() for details. 3289 */ 3290static int 3291cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 3292{ 3293 struct ccb_scsiio *csio; 3294 struct cd_softc *softc; 3295 union ccb *ccb; 3296 int cdb_size, param_len; 3297 int error; 3298 3299 softc = (struct cd_softc *)periph->softc; 3300 3301 ccb = cdgetccb(periph, /* priority */ 1); 3302 3303 csio = &ccb->csio; 3304 3305 error = 0; 3306 3307 /* 3308 * If the data is formatted for the 10 byte version of the mode 3309 * select parameter list, we need to use the 10 byte CDB. 3310 * Otherwise, we use whatever the stored minimum command size. 3311 */ 3312 if (data->cdb_size == 10) 3313 cdb_size = data->cdb_size; 3314 else 3315 cdb_size = softc->minimum_command_size; 3316 3317 if (cdb_size >= 10) { 3318 struct scsi_mode_header_10 *mode_header; 3319 u_int32_t data_len; 3320 3321 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3322 3323 data_len = scsi_2btoul(mode_header->data_length); 3324 3325 scsi_ulto2b(0, mode_header->data_length); 3326 /* 3327 * SONY drives do not allow a mode select with a medium_type 3328 * value that has just been returned by a mode sense; use a 3329 * medium_type of 0 (Default) instead. 3330 */ 3331 mode_header->medium_type = 0; 3332 3333 /* 3334 * Pass back whatever the drive passed to us, plus the size 3335 * of the data length field. 3336 */ 3337 param_len = data_len + sizeof(mode_header->data_length); 3338 3339 } else { 3340 struct scsi_mode_header_6 *mode_header; 3341 3342 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3343 3344 param_len = mode_header->data_length + 1; 3345 3346 mode_header->data_length = 0; 3347 /* 3348 * SONY drives do not allow a mode select with a medium_type 3349 * value that has just been returned by a mode sense; use a 3350 * medium_type of 0 (Default) instead. 3351 */ 3352 mode_header->medium_type = 0; 3353 } 3354 3355 /* Don't say we've got more room than we actually allocated */ 3356 param_len = min(param_len, data->alloc_len); 3357 3358 scsi_mode_select_len(csio, 3359 /* retries */ 1, 3360 /* cbfcnp */ cddone, 3361 /* tag_action */ MSG_SIMPLE_Q_TAG, 3362 /* scsi_page_fmt */ 1, 3363 /* save_pages */ 0, 3364 /* param_buf */ data->mode_buf, 3365 /* param_len */ param_len, 3366 /* minimum_cmd_size */ cdb_size, 3367 /* sense_len */ SSD_FULL_SIZE, 3368 /* timeout */ 50000); 3369 3370 /* See comments in cdgetmode() and cd6byteworkaround(). */ 3371 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3372 3373 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3374 /*sense_flags*/SF_RETRY_UA); 3375 3376 xpt_release_ccb(ccb); 3377 3378 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3379 3380 return (error); 3381} 3382 3383 3384static int 3385cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 3386{ 3387 struct ccb_scsiio *csio; 3388 union ccb *ccb; 3389 int error; 3390 u_int8_t cdb_len; 3391 3392 error = 0; 3393 ccb = cdgetccb(periph, /* priority */ 1); 3394 csio = &ccb->csio; 3395 /* 3396 * Use the smallest possible command to perform the operation. 3397 */ 3398 if ((len & 0xffff0000) == 0) { 3399 /* 3400 * We can fit in a 10 byte cdb. 3401 */ 3402 struct scsi_play_10 *scsi_cmd; 3403 3404 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 3405 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3406 scsi_cmd->op_code = PLAY_10; 3407 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3408 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 3409 cdb_len = sizeof(*scsi_cmd); 3410 } else { 3411 struct scsi_play_12 *scsi_cmd; 3412 3413 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 3414 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3415 scsi_cmd->op_code = PLAY_12; 3416 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3417 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 3418 cdb_len = sizeof(*scsi_cmd); 3419 } 3420 cam_fill_csio(csio, 3421 /*retries*/2, 3422 cddone, 3423 /*flags*/CAM_DIR_NONE, 3424 MSG_SIMPLE_Q_TAG, 3425 /*dataptr*/NULL, 3426 /*datalen*/0, 3427 /*sense_len*/SSD_FULL_SIZE, 3428 cdb_len, 3429 /*timeout*/50 * 1000); 3430 3431 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3432 /*sense_flags*/SF_RETRY_UA); 3433 3434 xpt_release_ccb(ccb); 3435 3436 return(error); 3437} 3438 3439static int 3440cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 3441 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 3442{ 3443 struct scsi_play_msf *scsi_cmd; 3444 struct ccb_scsiio *csio; 3445 union ccb *ccb; 3446 int error; 3447 3448 error = 0; 3449 3450 ccb = cdgetccb(periph, /* priority */ 1); 3451 3452 csio = &ccb->csio; 3453 3454 cam_fill_csio(csio, 3455 /* retries */ 1, 3456 /* cbfcnp */ cddone, 3457 /* flags */ CAM_DIR_NONE, 3458 /* tag_action */ MSG_SIMPLE_Q_TAG, 3459 /* data_ptr */ NULL, 3460 /* dxfer_len */ 0, 3461 /* sense_len */ SSD_FULL_SIZE, 3462 sizeof(struct scsi_play_msf), 3463 /* timeout */ 50000); 3464 3465 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 3466 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3467 3468 scsi_cmd->op_code = PLAY_MSF; 3469 scsi_cmd->start_m = startm; 3470 scsi_cmd->start_s = starts; 3471 scsi_cmd->start_f = startf; 3472 scsi_cmd->end_m = endm; 3473 scsi_cmd->end_s = ends; 3474 scsi_cmd->end_f = endf; 3475 3476 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3477 /*sense_flags*/SF_RETRY_UA); 3478 3479 xpt_release_ccb(ccb); 3480 3481 return(error); 3482} 3483 3484 3485static int 3486cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 3487 u_int32_t etrack, u_int32_t eindex) 3488{ 3489 struct scsi_play_track *scsi_cmd; 3490 struct ccb_scsiio *csio; 3491 union ccb *ccb; 3492 int error; 3493 3494 error = 0; 3495 3496 ccb = cdgetccb(periph, /* priority */ 1); 3497 3498 csio = &ccb->csio; 3499 3500 cam_fill_csio(csio, 3501 /* retries */ 1, 3502 /* cbfcnp */ cddone, 3503 /* flags */ CAM_DIR_NONE, 3504 /* tag_action */ MSG_SIMPLE_Q_TAG, 3505 /* data_ptr */ NULL, 3506 /* dxfer_len */ 0, 3507 /* sense_len */ SSD_FULL_SIZE, 3508 sizeof(struct scsi_play_track), 3509 /* timeout */ 50000); 3510 3511 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3512 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3513 3514 scsi_cmd->op_code = PLAY_TRACK; 3515 scsi_cmd->start_track = strack; 3516 scsi_cmd->start_index = sindex; 3517 scsi_cmd->end_track = etrack; 3518 scsi_cmd->end_index = eindex; 3519 3520 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3521 /*sense_flags*/SF_RETRY_UA); 3522 3523 xpt_release_ccb(ccb); 3524 3525 return(error); 3526} 3527 3528static int 3529cdpause(struct cam_periph *periph, u_int32_t go) 3530{ 3531 struct scsi_pause *scsi_cmd; 3532 struct ccb_scsiio *csio; 3533 union ccb *ccb; 3534 int error; 3535 3536 error = 0; 3537 3538 ccb = cdgetccb(periph, /* priority */ 1); 3539 3540 csio = &ccb->csio; 3541 3542 cam_fill_csio(csio, 3543 /* retries */ 1, 3544 /* cbfcnp */ cddone, 3545 /* flags */ CAM_DIR_NONE, 3546 /* tag_action */ MSG_SIMPLE_Q_TAG, 3547 /* data_ptr */ NULL, 3548 /* dxfer_len */ 0, 3549 /* sense_len */ SSD_FULL_SIZE, 3550 sizeof(struct scsi_pause), 3551 /* timeout */ 50000); 3552 3553 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3554 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3555 3556 scsi_cmd->op_code = PAUSE; 3557 scsi_cmd->resume = go; 3558 3559 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3560 /*sense_flags*/SF_RETRY_UA); 3561 3562 xpt_release_ccb(ccb); 3563 3564 return(error); 3565} 3566 3567static int 3568cdstartunit(struct cam_periph *periph, int load) 3569{ 3570 union ccb *ccb; 3571 int error; 3572 3573 error = 0; 3574 3575 ccb = cdgetccb(periph, /* priority */ 1); 3576 3577 scsi_start_stop(&ccb->csio, 3578 /* retries */ 1, 3579 /* cbfcnp */ cddone, 3580 /* tag_action */ MSG_SIMPLE_Q_TAG, 3581 /* start */ TRUE, 3582 /* load_eject */ load, 3583 /* immediate */ FALSE, 3584 /* sense_len */ SSD_FULL_SIZE, 3585 /* timeout */ 50000); 3586 3587 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3588 /*sense_flags*/SF_RETRY_UA); 3589 3590 xpt_release_ccb(ccb); 3591 3592 return(error); 3593} 3594 3595static int 3596cdstopunit(struct cam_periph *periph, u_int32_t eject) 3597{ 3598 union ccb *ccb; 3599 int error; 3600 3601 error = 0; 3602 3603 ccb = cdgetccb(periph, /* priority */ 1); 3604 3605 scsi_start_stop(&ccb->csio, 3606 /* retries */ 1, 3607 /* cbfcnp */ cddone, 3608 /* tag_action */ MSG_SIMPLE_Q_TAG, 3609 /* start */ FALSE, 3610 /* load_eject */ eject, 3611 /* immediate */ FALSE, 3612 /* sense_len */ SSD_FULL_SIZE, 3613 /* timeout */ 50000); 3614 3615 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3616 /*sense_flags*/SF_RETRY_UA); 3617 3618 xpt_release_ccb(ccb); 3619 3620 return(error); 3621} 3622 3623static int 3624cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3625{ 3626 struct scsi_set_speed *scsi_cmd; 3627 struct ccb_scsiio *csio; 3628 union ccb *ccb; 3629 int error; 3630 3631 error = 0; 3632 ccb = cdgetccb(periph, /* priority */ 1); 3633 csio = &ccb->csio; 3634 3635 /* Preserve old behavior: units in multiples of CDROM speed */ 3636 if (rdspeed < 177) 3637 rdspeed *= 177; 3638 if (wrspeed < 177) 3639 wrspeed *= 177; 3640 3641 cam_fill_csio(csio, 3642 /* retries */ 1, 3643 /* cbfcnp */ cddone, 3644 /* flags */ CAM_DIR_NONE, 3645 /* tag_action */ MSG_SIMPLE_Q_TAG, 3646 /* data_ptr */ NULL, 3647 /* dxfer_len */ 0, 3648 /* sense_len */ SSD_FULL_SIZE, 3649 sizeof(struct scsi_set_speed), 3650 /* timeout */ 50000); 3651 3652 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3653 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3654 3655 scsi_cmd->opcode = SET_CD_SPEED; 3656 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3657 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3658 3659 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3660 /*sense_flags*/SF_RETRY_UA); 3661 3662 xpt_release_ccb(ccb); 3663 3664 return(error); 3665} 3666 3667static int 3668cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3669{ 3670 union ccb *ccb; 3671 u_int8_t *databuf; 3672 u_int32_t lba; 3673 int error; 3674 int length; 3675 3676 error = 0; 3677 databuf = NULL; 3678 lba = 0; 3679 3680 ccb = cdgetccb(periph, /* priority */ 1); 3681 3682 switch (authinfo->format) { 3683 case DVD_REPORT_AGID: 3684 length = sizeof(struct scsi_report_key_data_agid); 3685 break; 3686 case DVD_REPORT_CHALLENGE: 3687 length = sizeof(struct scsi_report_key_data_challenge); 3688 break; 3689 case DVD_REPORT_KEY1: 3690 length = sizeof(struct scsi_report_key_data_key1_key2); 3691 break; 3692 case DVD_REPORT_TITLE_KEY: 3693 length = sizeof(struct scsi_report_key_data_title); 3694 /* The lba field is only set for the title key */ 3695 lba = authinfo->lba; 3696 break; 3697 case DVD_REPORT_ASF: 3698 length = sizeof(struct scsi_report_key_data_asf); 3699 break; 3700 case DVD_REPORT_RPC: 3701 length = sizeof(struct scsi_report_key_data_rpc); 3702 break; 3703 case DVD_INVALIDATE_AGID: 3704 length = 0; 3705 break; 3706 default: 3707 error = EINVAL; 3708 goto bailout; 3709 break; /* NOTREACHED */ 3710 } 3711 3712 if (length != 0) { 3713 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3714 } else 3715 databuf = NULL; 3716 3717 3718 scsi_report_key(&ccb->csio, 3719 /* retries */ 1, 3720 /* cbfcnp */ cddone, 3721 /* tag_action */ MSG_SIMPLE_Q_TAG, 3722 /* lba */ lba, 3723 /* agid */ authinfo->agid, 3724 /* key_format */ authinfo->format, 3725 /* data_ptr */ databuf, 3726 /* dxfer_len */ length, 3727 /* sense_len */ SSD_FULL_SIZE, 3728 /* timeout */ 50000); 3729 3730 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3731 /*sense_flags*/SF_RETRY_UA); 3732 3733 if (error != 0) 3734 goto bailout; 3735 3736 if (ccb->csio.resid != 0) { 3737 xpt_print_path(periph->path); 3738 printf("warning, residual for report key command is %d\n", 3739 ccb->csio.resid); 3740 } 3741 3742 switch(authinfo->format) { 3743 case DVD_REPORT_AGID: { 3744 struct scsi_report_key_data_agid *agid_data; 3745 3746 agid_data = (struct scsi_report_key_data_agid *)databuf; 3747 3748 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3749 RKD_AGID_SHIFT; 3750 break; 3751 } 3752 case DVD_REPORT_CHALLENGE: { 3753 struct scsi_report_key_data_challenge *chal_data; 3754 3755 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3756 3757 bcopy(chal_data->challenge_key, authinfo->keychal, 3758 min(sizeof(chal_data->challenge_key), 3759 sizeof(authinfo->keychal))); 3760 break; 3761 } 3762 case DVD_REPORT_KEY1: { 3763 struct scsi_report_key_data_key1_key2 *key1_data; 3764 3765 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3766 3767 bcopy(key1_data->key1, authinfo->keychal, 3768 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3769 break; 3770 } 3771 case DVD_REPORT_TITLE_KEY: { 3772 struct scsi_report_key_data_title *title_data; 3773 3774 title_data = (struct scsi_report_key_data_title *)databuf; 3775 3776 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3777 RKD_TITLE_CPM_SHIFT; 3778 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3779 RKD_TITLE_CP_SEC_SHIFT; 3780 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3781 RKD_TITLE_CMGS_SHIFT; 3782 bcopy(title_data->title_key, authinfo->keychal, 3783 min(sizeof(title_data->title_key), 3784 sizeof(authinfo->keychal))); 3785 break; 3786 } 3787 case DVD_REPORT_ASF: { 3788 struct scsi_report_key_data_asf *asf_data; 3789 3790 asf_data = (struct scsi_report_key_data_asf *)databuf; 3791 3792 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3793 break; 3794 } 3795 case DVD_REPORT_RPC: { 3796 struct scsi_report_key_data_rpc *rpc_data; 3797 3798 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3799 3800 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3801 RKD_RPC_TYPE_SHIFT; 3802 authinfo->vend_rsts = 3803 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3804 RKD_RPC_VENDOR_RESET_SHIFT; 3805 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3806 authinfo->region = rpc_data->region_mask; 3807 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3808 break; 3809 } 3810 case DVD_INVALIDATE_AGID: 3811 break; 3812 default: 3813 /* This should be impossible, since we checked above */ 3814 error = EINVAL; 3815 goto bailout; 3816 break; /* NOTREACHED */ 3817 } 3818bailout: 3819 if (databuf != NULL) 3820 free(databuf, M_DEVBUF); 3821 3822 xpt_release_ccb(ccb); 3823 3824 return(error); 3825} 3826 3827static int 3828cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3829{ 3830 union ccb *ccb; 3831 u_int8_t *databuf; 3832 int length; 3833 int error; 3834 3835 error = 0; 3836 databuf = NULL; 3837 3838 ccb = cdgetccb(periph, /* priority */ 1); 3839 3840 switch(authinfo->format) { 3841 case DVD_SEND_CHALLENGE: { 3842 struct scsi_report_key_data_challenge *challenge_data; 3843 3844 length = sizeof(*challenge_data); 3845 3846 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3847 3848 databuf = (u_int8_t *)challenge_data; 3849 3850 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3851 challenge_data->data_len); 3852 3853 bcopy(authinfo->keychal, challenge_data->challenge_key, 3854 min(sizeof(authinfo->keychal), 3855 sizeof(challenge_data->challenge_key))); 3856 break; 3857 } 3858 case DVD_SEND_KEY2: { 3859 struct scsi_report_key_data_key1_key2 *key2_data; 3860 3861 length = sizeof(*key2_data); 3862 3863 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3864 3865 databuf = (u_int8_t *)key2_data; 3866 3867 scsi_ulto2b(length - sizeof(key2_data->data_len), 3868 key2_data->data_len); 3869 3870 bcopy(authinfo->keychal, key2_data->key1, 3871 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 3872 3873 break; 3874 } 3875 case DVD_SEND_RPC: { 3876 struct scsi_send_key_data_rpc *rpc_data; 3877 3878 length = sizeof(*rpc_data); 3879 3880 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3881 3882 databuf = (u_int8_t *)rpc_data; 3883 3884 scsi_ulto2b(length - sizeof(rpc_data->data_len), 3885 rpc_data->data_len); 3886 3887 rpc_data->region_code = authinfo->region; 3888 break; 3889 } 3890 default: 3891 error = EINVAL; 3892 goto bailout; 3893 break; /* NOTREACHED */ 3894 } 3895 3896 scsi_send_key(&ccb->csio, 3897 /* retries */ 1, 3898 /* cbfcnp */ cddone, 3899 /* tag_action */ MSG_SIMPLE_Q_TAG, 3900 /* agid */ authinfo->agid, 3901 /* key_format */ authinfo->format, 3902 /* data_ptr */ databuf, 3903 /* dxfer_len */ length, 3904 /* sense_len */ SSD_FULL_SIZE, 3905 /* timeout */ 50000); 3906 3907 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3908 /*sense_flags*/SF_RETRY_UA); 3909 3910bailout: 3911 3912 if (databuf != NULL) 3913 free(databuf, M_DEVBUF); 3914 3915 xpt_release_ccb(ccb); 3916 3917 return(error); 3918} 3919 3920static int 3921cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 3922{ 3923 union ccb *ccb; 3924 u_int8_t *databuf; 3925 u_int32_t address; 3926 int error; 3927 int length; 3928 3929 error = 0; 3930 databuf = NULL; 3931 /* The address is reserved for many of the formats */ 3932 address = 0; 3933 3934 ccb = cdgetccb(periph, /* priority */ 1); 3935 3936 switch(dvdstruct->format) { 3937 case DVD_STRUCT_PHYSICAL: 3938 length = sizeof(struct scsi_read_dvd_struct_data_physical); 3939 break; 3940 case DVD_STRUCT_COPYRIGHT: 3941 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 3942 break; 3943 case DVD_STRUCT_DISCKEY: 3944 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 3945 break; 3946 case DVD_STRUCT_BCA: 3947 length = sizeof(struct scsi_read_dvd_struct_data_bca); 3948 break; 3949 case DVD_STRUCT_MANUFACT: 3950 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 3951 break; 3952 case DVD_STRUCT_CMI: 3953 error = ENODEV; 3954 goto bailout; 3955#ifdef notyet 3956 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage); 3957 address = dvdstruct->address; 3958#endif 3959 break; /* NOTREACHED */ 3960 case DVD_STRUCT_PROTDISCID: 3961 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 3962 break; 3963 case DVD_STRUCT_DISCKEYBLOCK: 3964 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 3965 break; 3966 case DVD_STRUCT_DDS: 3967 length = sizeof(struct scsi_read_dvd_struct_data_dds); 3968 break; 3969 case DVD_STRUCT_MEDIUM_STAT: 3970 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 3971 break; 3972 case DVD_STRUCT_SPARE_AREA: 3973 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 3974 break; 3975 case DVD_STRUCT_RMD_LAST: 3976 error = ENODEV; 3977 goto bailout; 3978#ifdef notyet 3979 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout); 3980 address = dvdstruct->address; 3981#endif 3982 break; /* NOTREACHED */ 3983 case DVD_STRUCT_RMD_RMA: 3984 error = ENODEV; 3985 goto bailout; 3986#ifdef notyet 3987 length = sizeof(struct scsi_read_dvd_struct_data_rmd); 3988 address = dvdstruct->address; 3989#endif 3990 break; /* NOTREACHED */ 3991 case DVD_STRUCT_PRERECORDED: 3992 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 3993 break; 3994 case DVD_STRUCT_UNIQUEID: 3995 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 3996 break; 3997 case DVD_STRUCT_DCB: 3998 error = ENODEV; 3999 goto bailout; 4000#ifdef notyet 4001 length = sizeof(struct scsi_read_dvd_struct_data_dcb); 4002 address = dvdstruct->address; 4003#endif 4004 break; /* NOTREACHED */ 4005 case DVD_STRUCT_LIST: 4006 /* 4007 * This is the maximum allocation length for the READ DVD 4008 * STRUCTURE command. There's nothing in the MMC3 spec 4009 * that indicates a limit in the amount of data that can 4010 * be returned from this call, other than the limits 4011 * imposed by the 2-byte length variables. 4012 */ 4013 length = 65535; 4014 break; 4015 default: 4016 error = EINVAL; 4017 goto bailout; 4018 break; /* NOTREACHED */ 4019 } 4020 4021 if (length != 0) { 4022 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4023 } else 4024 databuf = NULL; 4025 4026 scsi_read_dvd_structure(&ccb->csio, 4027 /* retries */ 1, 4028 /* cbfcnp */ cddone, 4029 /* tag_action */ MSG_SIMPLE_Q_TAG, 4030 /* lba */ address, 4031 /* layer_number */ dvdstruct->layer_num, 4032 /* key_format */ dvdstruct->format, 4033 /* agid */ dvdstruct->agid, 4034 /* data_ptr */ databuf, 4035 /* dxfer_len */ length, 4036 /* sense_len */ SSD_FULL_SIZE, 4037 /* timeout */ 50000); 4038 4039 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4040 /*sense_flags*/SF_RETRY_UA); 4041 4042 if (error != 0) 4043 goto bailout; 4044 4045 switch(dvdstruct->format) { 4046 case DVD_STRUCT_PHYSICAL: { 4047 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 4048 struct dvd_layer *outlayer; 4049 struct scsi_read_dvd_struct_data_physical *phys_data; 4050 4051 phys_data = 4052 (struct scsi_read_dvd_struct_data_physical *)databuf; 4053 inlayer = &phys_data->layer_desc; 4054 outlayer = (struct dvd_layer *)&dvdstruct->data; 4055 4056 dvdstruct->length = sizeof(*inlayer); 4057 4058 outlayer->book_type = (inlayer->book_type_version & 4059 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 4060 outlayer->book_version = (inlayer->book_type_version & 4061 RDSD_BOOK_VERSION_MASK); 4062 outlayer->disc_size = (inlayer->disc_size_max_rate & 4063 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 4064 outlayer->max_rate = (inlayer->disc_size_max_rate & 4065 RDSD_MAX_RATE_MASK); 4066 outlayer->nlayers = (inlayer->layer_info & 4067 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 4068 outlayer->track_path = (inlayer->layer_info & 4069 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 4070 outlayer->layer_type = (inlayer->layer_info & 4071 RDSD_LAYER_TYPE_MASK); 4072 outlayer->linear_density = (inlayer->density & 4073 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 4074 outlayer->track_density = (inlayer->density & 4075 RDSD_TRACK_DENSITY_MASK); 4076 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 4077 RDSD_BCA_SHIFT; 4078 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 4079 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 4080 outlayer->end_sector_l0 = 4081 scsi_3btoul(inlayer->end_sector_layer0); 4082 break; 4083 } 4084 case DVD_STRUCT_COPYRIGHT: { 4085 struct scsi_read_dvd_struct_data_copyright *copy_data; 4086 4087 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 4088 databuf; 4089 4090 dvdstruct->cpst = copy_data->cps_type; 4091 dvdstruct->rmi = copy_data->region_info; 4092 dvdstruct->length = 0; 4093 4094 break; 4095 } 4096 default: 4097 /* 4098 * Tell the user what the overall length is, no matter 4099 * what we can actually fit in the data buffer. 4100 */ 4101 dvdstruct->length = length - ccb->csio.resid - 4102 sizeof(struct scsi_read_dvd_struct_data_header); 4103 4104 /* 4105 * But only actually copy out the smaller of what we read 4106 * in or what the structure can take. 4107 */ 4108 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 4109 dvdstruct->data, 4110 min(sizeof(dvdstruct->data), dvdstruct->length)); 4111 break; 4112 } 4113bailout: 4114 4115 if (databuf != NULL) 4116 free(databuf, M_DEVBUF); 4117 4118 xpt_release_ccb(ccb); 4119 4120 return(error); 4121} 4122 4123void 4124scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 4125 void (*cbfcnp)(struct cam_periph *, union ccb *), 4126 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 4127 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 4128 u_int8_t sense_len, u_int32_t timeout) 4129{ 4130 struct scsi_report_key *scsi_cmd; 4131 4132 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 4133 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4134 scsi_cmd->opcode = REPORT_KEY; 4135 scsi_ulto4b(lba, scsi_cmd->lba); 4136 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4137 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4138 (key_format & RK_KF_KEYFORMAT_MASK); 4139 4140 cam_fill_csio(csio, 4141 retries, 4142 cbfcnp, 4143 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 4144 tag_action, 4145 /*data_ptr*/ data_ptr, 4146 /*dxfer_len*/ dxfer_len, 4147 sense_len, 4148 sizeof(*scsi_cmd), 4149 timeout); 4150} 4151 4152void 4153scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 4154 void (*cbfcnp)(struct cam_periph *, union ccb *), 4155 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 4156 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 4157 u_int32_t timeout) 4158{ 4159 struct scsi_send_key *scsi_cmd; 4160 4161 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 4162 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4163 scsi_cmd->opcode = SEND_KEY; 4164 4165 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 4166 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4167 (key_format & RK_KF_KEYFORMAT_MASK); 4168 4169 cam_fill_csio(csio, 4170 retries, 4171 cbfcnp, 4172 /*flags*/ CAM_DIR_OUT, 4173 tag_action, 4174 /*data_ptr*/ data_ptr, 4175 /*dxfer_len*/ dxfer_len, 4176 sense_len, 4177 sizeof(*scsi_cmd), 4178 timeout); 4179} 4180 4181 4182void 4183scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 4184 void (*cbfcnp)(struct cam_periph *, union ccb *), 4185 u_int8_t tag_action, u_int32_t address, 4186 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 4187 u_int8_t *data_ptr, u_int32_t dxfer_len, 4188 u_int8_t sense_len, u_int32_t timeout) 4189{ 4190 struct scsi_read_dvd_structure *scsi_cmd; 4191 4192 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 4193 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4194 scsi_cmd->opcode = READ_DVD_STRUCTURE; 4195 4196 scsi_ulto4b(address, scsi_cmd->address); 4197 scsi_cmd->layer_number = layer_number; 4198 scsi_cmd->format = format; 4199 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4200 /* The AGID is the top two bits of this byte */ 4201 scsi_cmd->agid = agid << 6; 4202 4203 cam_fill_csio(csio, 4204 retries, 4205 cbfcnp, 4206 /*flags*/ CAM_DIR_IN, 4207 tag_action, 4208 /*data_ptr*/ data_ptr, 4209 /*dxfer_len*/ dxfer_len, 4210 sense_len, 4211 sizeof(*scsi_cmd), 4212 timeout); 4213} 4214