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