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