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) == CAM_REQ_CMP) { 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[80], tmpstr2[80]; 481 482 periph = (struct cam_periph *)context; 483 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 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) != CAM_REQ_CMP) { 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) != CAM_REQ_CMP) 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 1061 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1062 1063 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1064 cdp->blksize = scsi_4btoul (rdcap->length); 1065 1066 /* 1067 * Retry any UNIT ATTENTION type errors. They 1068 * are expected at boot. 1069 */ 1070 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP || 1071 (error = cderror(done_ccb, CAM_RETRY_SELTO, 1072 SF_RETRY_UA | SF_NO_PRINT)) == 0) { 1073 snprintf(announce_buf, CD_ANNOUNCETMP_SZ, 1074 "%juMB (%ju %u byte sectors)", 1075 ((uintmax_t)cdp->disksize * cdp->blksize) / 1076 (1024 * 1024), 1077 (uintmax_t)cdp->disksize, cdp->blksize); 1078 } else { 1079 if (error == ERESTART) { 1080 /* 1081 * A retry was scheuled, so 1082 * just return. 1083 */ 1084 return; 1085 } else { 1086 int asc, ascq; 1087 int sense_key, error_code; 1088 int have_sense; 1089 cam_status status; 1090 struct ccb_getdev cgd; 1091 1092 /* Don't wedge this device's queue */ 1093 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1094 cam_release_devq(done_ccb->ccb_h.path, 1095 /*relsim_flags*/0, 1096 /*reduction*/0, 1097 /*timeout*/0, 1098 /*getcount_only*/0); 1099 1100 status = done_ccb->ccb_h.status; 1101 1102 xpt_setup_ccb(&cgd.ccb_h, 1103 done_ccb->ccb_h.path, 1104 CAM_PRIORITY_NORMAL); 1105 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1106 xpt_action((union ccb *)&cgd); 1107 1108 if (scsi_extract_sense_ccb(done_ccb, 1109 &error_code, &sense_key, &asc, &ascq)) 1110 have_sense = TRUE; 1111 else 1112 have_sense = FALSE; 1113 1114 /* 1115 * Attach to anything that claims to be a 1116 * CDROM or WORM device, as long as it 1117 * doesn't return a "Logical unit not 1118 * supported" (0x25) error. 1119 */ 1120 if ((have_sense) && (asc != 0x25) 1121 && (error_code == SSD_CURRENT_ERROR)) { 1122 const char *sense_key_desc; 1123 const char *asc_desc; 1124 1125 scsi_sense_desc(sense_key, asc, ascq, 1126 &cgd.inq_data, 1127 &sense_key_desc, 1128 &asc_desc); 1129 snprintf(announce_buf, 1130 sizeof(announce_buf), 1131 "Attempt to query device " 1132 "size failed: %s, %s", 1133 sense_key_desc, 1134 asc_desc); 1135 } else if ((have_sense == 0) 1136 && ((status & CAM_STATUS_MASK) == 1137 CAM_SCSI_STATUS_ERROR) 1138 && (csio->scsi_status == 1139 SCSI_STATUS_BUSY)) { 1140 snprintf(announce_buf, 1141 sizeof(announce_buf), 1142 "Attempt to query device " 1143 "size failed: SCSI Status: %s", 1144 scsi_status_string(csio)); 1145 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1146 /* 1147 * We only print out an error for 1148 * CDROM type devices. For WORM 1149 * devices, we don't print out an 1150 * error since a few WORM devices 1151 * don't support CDROM commands. 1152 * If we have sense information, go 1153 * ahead and print it out. 1154 * Otherwise, just say that we 1155 * couldn't attach. 1156 */ 1157 1158 /* 1159 * Just print out the error, not 1160 * the full probe message, when we 1161 * don't attach. 1162 */ 1163 if (have_sense) 1164 scsi_sense_print( 1165 &done_ccb->csio); 1166 else { 1167 xpt_print(periph->path, 1168 "got CAM status %#x\n", 1169 done_ccb->ccb_h.status); 1170 } 1171 xpt_print(periph->path, "fatal error, " 1172 "failed to attach to device\n"); 1173 /* 1174 * Invalidate this peripheral. 1175 */ 1176 cam_periph_invalidate(periph); 1177 1178 announce_buf = NULL; 1179 } else { 1180 1181 /* 1182 * Invalidate this peripheral. 1183 */ 1184 cam_periph_invalidate(periph); 1185 announce_buf = NULL; 1186 } 1187 } 1188 } 1189 free(rdcap, M_SCSICD); 1190 if (announce_buf != NULL) { 1191 struct sbuf sb; 1192 1193 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ, 1194 SBUF_FIXEDLEN); 1195 xpt_announce_periph_sbuf(periph, &sb, announce_buf); 1196 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, 1197 CD_Q_BIT_STRING); 1198 sbuf_finish(&sb); 1199 sbuf_putbuf(&sb); 1200 1201 /* 1202 * Create our sysctl variables, now that we know 1203 * we have successfully attached. 1204 */ 1205 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 1206 } 1207 softc->state = CD_STATE_NORMAL; 1208 /* 1209 * Since our peripheral may be invalidated by an error 1210 * above or an external event, we must release our CCB 1211 * before releasing the probe lock on the peripheral. 1212 * The peripheral will only go away once the last lock 1213 * is removed, and we need it around for the CCB release 1214 * operation. 1215 */ 1216 xpt_release_ccb(done_ccb); 1217 cam_periph_unhold(periph); 1218 return; 1219 } 1220 case CD_CCB_TUR: 1221 { 1222 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1223 1224 if (cderror(done_ccb, CAM_RETRY_SELTO, 1225 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == 1226 ERESTART) 1227 return; 1228 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1229 cam_release_devq(done_ccb->ccb_h.path, 1230 /*relsim_flags*/0, 1231 /*reduction*/0, 1232 /*timeout*/0, 1233 /*getcount_only*/0); 1234 } 1235 xpt_release_ccb(done_ccb); 1236 cam_periph_release_locked(periph); 1237 return; 1238 } 1239 default: 1240 break; 1241 } 1242 xpt_release_ccb(done_ccb); 1243 } 1244 1245 static union cd_pages * 1246 cdgetpage(struct cd_mode_params *mode_params) 1247 { 1248 union cd_pages *page; 1249 1250 if (mode_params->cdb_size == 10) 1251 page = (union cd_pages *)find_mode_page_10( 1252 (struct scsi_mode_header_10 *)mode_params->mode_buf); 1253 else 1254 page = (union cd_pages *)find_mode_page_6( 1255 (struct scsi_mode_header_6 *)mode_params->mode_buf); 1256 1257 return (page); 1258 } 1259 1260 static int 1261 cdgetpagesize(int page_num) 1262 { 1263 u_int i; 1264 1265 for (i = 0; i < nitems(cd_page_size_table); i++) { 1266 if (cd_page_size_table[i].page == page_num) 1267 return (cd_page_size_table[i].page_size); 1268 } 1269 1270 return (-1); 1271 } 1272 1273 static int 1274 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 1275 { 1276 1277 struct cam_periph *periph; 1278 struct cd_softc *softc; 1279 int nocopyout, error = 0; 1280 1281 periph = (struct cam_periph *)dp->d_drv1; 1282 cam_periph_lock(periph); 1283 1284 softc = (struct cd_softc *)periph->softc; 1285 1286 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1287 ("cdioctl(%#lx)\n", cmd)); 1288 1289 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1290 cam_periph_unlock(periph); 1291 cam_periph_release(periph); 1292 return (error); 1293 } 1294 1295 /* 1296 * If we don't have media loaded, check for it. If still don't 1297 * have media loaded, we can only do a load or eject. 1298 * 1299 * We only care whether media is loaded if this is a cd-specific ioctl 1300 * (thus the IOCGROUP check below). Note that this will break if 1301 * anyone adds any ioctls into the switch statement below that don't 1302 * have their ioctl group set to 'c'. 1303 */ 1304 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1305 && ((cmd != CDIOCCLOSE) 1306 && (cmd != CDIOCEJECT)) 1307 && (IOCGROUP(cmd) == 'c')) { 1308 error = cdcheckmedia(periph); 1309 if (error != 0) { 1310 cam_periph_unhold(periph); 1311 cam_periph_unlock(periph); 1312 return (error); 1313 } 1314 } 1315 /* 1316 * Drop the lock here so later mallocs can use WAITOK. The periph 1317 * is essentially locked still with the cam_periph_hold call above. 1318 */ 1319 cam_periph_unlock(periph); 1320 1321 nocopyout = 0; 1322 switch (cmd) { 1323 1324 case CDIOCPLAYTRACKS: 1325 { 1326 struct ioc_play_track *args 1327 = (struct ioc_play_track *) addr; 1328 struct cd_mode_params params; 1329 union cd_pages *page; 1330 1331 params.alloc_len = sizeof(union cd_mode_data_6_10); 1332 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1333 M_WAITOK | M_ZERO); 1334 1335 cam_periph_lock(periph); 1336 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1337 ("trying to do CDIOCPLAYTRACKS\n")); 1338 1339 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1340 if (error) { 1341 free(params.mode_buf, M_SCSICD); 1342 cam_periph_unlock(periph); 1343 break; 1344 } 1345 page = cdgetpage(¶ms); 1346 1347 page->audio.flags &= ~CD_PA_SOTC; 1348 page->audio.flags |= CD_PA_IMMED; 1349 error = cdsetmode(periph, ¶ms); 1350 free(params.mode_buf, M_SCSICD); 1351 if (error) { 1352 cam_periph_unlock(periph); 1353 break; 1354 } 1355 1356 /* 1357 * This was originally implemented with the PLAY 1358 * AUDIO TRACK INDEX command, but that command was 1359 * deprecated after SCSI-2. Most (all?) SCSI CDROM 1360 * drives support it but ATAPI and ATAPI-derivative 1361 * drives don't seem to support it. So we keep a 1362 * cache of the table of contents and translate 1363 * track numbers to MSF format. 1364 */ 1365 if (softc->flags & CD_FLAG_VALID_TOC) { 1366 union msf_lba *sentry, *eentry; 1367 int st, et; 1368 1369 if (args->end_track < 1370 softc->toc.header.ending_track + 1) 1371 args->end_track++; 1372 if (args->end_track > 1373 softc->toc.header.ending_track + 1) 1374 args->end_track = 1375 softc->toc.header.ending_track + 1; 1376 st = args->start_track - 1377 softc->toc.header.starting_track; 1378 et = args->end_track - 1379 softc->toc.header.starting_track; 1380 if ((st < 0) 1381 || (et < 0) 1382 || (st > (softc->toc.header.ending_track - 1383 softc->toc.header.starting_track))) { 1384 error = EINVAL; 1385 cam_periph_unlock(periph); 1386 break; 1387 } 1388 sentry = &softc->toc.entries[st].addr; 1389 eentry = &softc->toc.entries[et].addr; 1390 error = cdplaymsf(periph, 1391 sentry->msf.minute, 1392 sentry->msf.second, 1393 sentry->msf.frame, 1394 eentry->msf.minute, 1395 eentry->msf.second, 1396 eentry->msf.frame); 1397 } else { 1398 /* 1399 * If we don't have a valid TOC, try the 1400 * play track index command. It is part of 1401 * the SCSI-2 spec, but was removed in the 1402 * MMC specs. ATAPI and ATAPI-derived 1403 * drives don't support it. 1404 */ 1405 if (softc->quirks & CD_Q_BCD_TRACKS) { 1406 args->start_track = 1407 bin2bcd(args->start_track); 1408 args->end_track = 1409 bin2bcd(args->end_track); 1410 } 1411 error = cdplaytracks(periph, 1412 args->start_track, 1413 args->start_index, 1414 args->end_track, 1415 args->end_index); 1416 } 1417 cam_periph_unlock(periph); 1418 } 1419 break; 1420 case CDIOCPLAYMSF: 1421 { 1422 struct ioc_play_msf *args 1423 = (struct ioc_play_msf *) addr; 1424 struct cd_mode_params params; 1425 union cd_pages *page; 1426 1427 params.alloc_len = sizeof(union cd_mode_data_6_10); 1428 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1429 M_WAITOK | M_ZERO); 1430 1431 cam_periph_lock(periph); 1432 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1433 ("trying to do CDIOCPLAYMSF\n")); 1434 1435 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1436 if (error) { 1437 free(params.mode_buf, M_SCSICD); 1438 cam_periph_unlock(periph); 1439 break; 1440 } 1441 page = cdgetpage(¶ms); 1442 1443 page->audio.flags &= ~CD_PA_SOTC; 1444 page->audio.flags |= CD_PA_IMMED; 1445 error = cdsetmode(periph, ¶ms); 1446 free(params.mode_buf, M_SCSICD); 1447 if (error) { 1448 cam_periph_unlock(periph); 1449 break; 1450 } 1451 error = cdplaymsf(periph, 1452 args->start_m, 1453 args->start_s, 1454 args->start_f, 1455 args->end_m, 1456 args->end_s, 1457 args->end_f); 1458 cam_periph_unlock(periph); 1459 } 1460 break; 1461 case CDIOCPLAYBLOCKS: 1462 { 1463 struct ioc_play_blocks *args 1464 = (struct ioc_play_blocks *) addr; 1465 struct cd_mode_params params; 1466 union cd_pages *page; 1467 1468 params.alloc_len = sizeof(union cd_mode_data_6_10); 1469 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1470 M_WAITOK | M_ZERO); 1471 1472 cam_periph_lock(periph); 1473 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1474 ("trying to do CDIOCPLAYBLOCKS\n")); 1475 1476 1477 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1478 if (error) { 1479 free(params.mode_buf, M_SCSICD); 1480 cam_periph_unlock(periph); 1481 break; 1482 } 1483 page = cdgetpage(¶ms); 1484 1485 page->audio.flags &= ~CD_PA_SOTC; 1486 page->audio.flags |= CD_PA_IMMED; 1487 error = cdsetmode(periph, ¶ms); 1488 free(params.mode_buf, M_SCSICD); 1489 if (error) { 1490 cam_periph_unlock(periph); 1491 break; 1492 } 1493 error = cdplay(periph, args->blk, args->len); 1494 cam_periph_unlock(periph); 1495 } 1496 break; 1497 case CDIOCREADSUBCHANNEL_SYSSPACE: 1498 nocopyout = 1; 1499 /* Fallthrough */ 1500 case CDIOCREADSUBCHANNEL: 1501 { 1502 struct ioc_read_subchannel *args 1503 = (struct ioc_read_subchannel *) addr; 1504 struct cd_sub_channel_info *data; 1505 u_int32_t len = args->data_len; 1506 1507 data = malloc(sizeof(struct cd_sub_channel_info), 1508 M_SCSICD, M_WAITOK | M_ZERO); 1509 1510 cam_periph_lock(periph); 1511 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1512 ("trying to do CDIOCREADSUBCHANNEL\n")); 1513 1514 if ((len > sizeof(struct cd_sub_channel_info)) || 1515 (len < sizeof(struct cd_sub_channel_header))) { 1516 printf( 1517 "scsi_cd: cdioctl: " 1518 "cdioreadsubchannel: error, len=%d\n", 1519 len); 1520 error = EINVAL; 1521 free(data, M_SCSICD); 1522 cam_periph_unlock(periph); 1523 break; 1524 } 1525 1526 if (softc->quirks & CD_Q_BCD_TRACKS) 1527 args->track = bin2bcd(args->track); 1528 1529 error = cdreadsubchannel(periph, args->address_format, 1530 args->data_format, args->track, data, len); 1531 1532 if (error) { 1533 free(data, M_SCSICD); 1534 cam_periph_unlock(periph); 1535 break; 1536 } 1537 if (softc->quirks & CD_Q_BCD_TRACKS) 1538 data->what.track_info.track_number = 1539 bcd2bin(data->what.track_info.track_number); 1540 len = min(len, ((data->header.data_len[0] << 8) + 1541 data->header.data_len[1] + 1542 sizeof(struct cd_sub_channel_header))); 1543 cam_periph_unlock(periph); 1544 if (nocopyout == 0) { 1545 if (copyout(data, args->data, len) != 0) { 1546 error = EFAULT; 1547 } 1548 } else { 1549 bcopy(data, args->data, len); 1550 } 1551 free(data, M_SCSICD); 1552 } 1553 break; 1554 1555 case CDIOREADTOCHEADER: 1556 { 1557 struct ioc_toc_header *th; 1558 1559 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 1560 M_WAITOK | M_ZERO); 1561 1562 cam_periph_lock(periph); 1563 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1564 ("trying to do CDIOREADTOCHEADER\n")); 1565 1566 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 1567 sizeof (*th), /*sense_flags*/SF_NO_PRINT); 1568 if (error) { 1569 free(th, M_SCSICD); 1570 cam_periph_unlock(periph); 1571 break; 1572 } 1573 if (softc->quirks & CD_Q_BCD_TRACKS) { 1574 /* we are going to have to convert the BCD 1575 * encoding on the cd to what is expected 1576 */ 1577 th->starting_track = 1578 bcd2bin(th->starting_track); 1579 th->ending_track = bcd2bin(th->ending_track); 1580 } 1581 th->len = ntohs(th->len); 1582 bcopy(th, addr, sizeof(*th)); 1583 free(th, M_SCSICD); 1584 cam_periph_unlock(periph); 1585 } 1586 break; 1587 case CDIOREADTOCENTRYS: 1588 { 1589 struct cd_tocdata *data; 1590 struct cd_toc_single *lead; 1591 struct ioc_read_toc_entry *te = 1592 (struct ioc_read_toc_entry *) addr; 1593 struct ioc_toc_header *th; 1594 u_int32_t len, readlen, idx, num; 1595 u_int32_t starting_track = te->starting_track; 1596 1597 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 1598 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 1599 1600 cam_periph_lock(periph); 1601 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1602 ("trying to do CDIOREADTOCENTRYS\n")); 1603 1604 if (te->data_len < sizeof(struct cd_toc_entry) 1605 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 1606 || (te->address_format != CD_MSF_FORMAT 1607 && te->address_format != CD_LBA_FORMAT)) { 1608 error = EINVAL; 1609 printf("scsi_cd: error in readtocentries, " 1610 "returning EINVAL\n"); 1611 free(data, M_SCSICD); 1612 free(lead, M_SCSICD); 1613 cam_periph_unlock(periph); 1614 break; 1615 } 1616 1617 th = &data->header; 1618 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 1619 sizeof (*th), /*sense_flags*/0); 1620 if (error) { 1621 free(data, M_SCSICD); 1622 free(lead, M_SCSICD); 1623 cam_periph_unlock(periph); 1624 break; 1625 } 1626 1627 if (softc->quirks & CD_Q_BCD_TRACKS) { 1628 /* we are going to have to convert the BCD 1629 * encoding on the cd to what is expected 1630 */ 1631 th->starting_track = 1632 bcd2bin(th->starting_track); 1633 th->ending_track = bcd2bin(th->ending_track); 1634 } 1635 1636 if (starting_track == 0) 1637 starting_track = th->starting_track; 1638 else if (starting_track == LEADOUT) 1639 starting_track = th->ending_track + 1; 1640 else if (starting_track < th->starting_track || 1641 starting_track > th->ending_track + 1) { 1642 printf("scsi_cd: error in readtocentries, " 1643 "returning EINVAL\n"); 1644 free(data, M_SCSICD); 1645 free(lead, M_SCSICD); 1646 cam_periph_unlock(periph); 1647 error = EINVAL; 1648 break; 1649 } 1650 1651 /* calculate reading length without leadout entry */ 1652 readlen = (th->ending_track - starting_track + 1) * 1653 sizeof(struct cd_toc_entry); 1654 1655 /* and with leadout entry */ 1656 len = readlen + sizeof(struct cd_toc_entry); 1657 if (te->data_len < len) { 1658 len = te->data_len; 1659 if (readlen > len) 1660 readlen = len; 1661 } 1662 if (len > sizeof(data->entries)) { 1663 printf("scsi_cd: error in readtocentries, " 1664 "returning EINVAL\n"); 1665 error = EINVAL; 1666 free(data, M_SCSICD); 1667 free(lead, M_SCSICD); 1668 cam_periph_unlock(periph); 1669 break; 1670 } 1671 num = len / sizeof(struct cd_toc_entry); 1672 1673 if (readlen > 0) { 1674 error = cdreadtoc(periph, te->address_format, 1675 starting_track, 1676 (u_int8_t *)data, 1677 readlen + sizeof (*th), 1678 /*sense_flags*/0); 1679 if (error) { 1680 free(data, M_SCSICD); 1681 free(lead, M_SCSICD); 1682 cam_periph_unlock(periph); 1683 break; 1684 } 1685 } 1686 1687 /* make leadout entry if needed */ 1688 idx = starting_track + num - 1; 1689 if (softc->quirks & CD_Q_BCD_TRACKS) 1690 th->ending_track = bcd2bin(th->ending_track); 1691 if (idx == th->ending_track + 1) { 1692 error = cdreadtoc(periph, te->address_format, 1693 LEADOUT, (u_int8_t *)lead, 1694 sizeof(*lead), 1695 /*sense_flags*/0); 1696 if (error) { 1697 free(data, M_SCSICD); 1698 free(lead, M_SCSICD); 1699 cam_periph_unlock(periph); 1700 break; 1701 } 1702 data->entries[idx - starting_track] = 1703 lead->entry; 1704 } 1705 if (softc->quirks & CD_Q_BCD_TRACKS) { 1706 for (idx = 0; idx < num - 1; idx++) { 1707 data->entries[idx].track = 1708 bcd2bin(data->entries[idx].track); 1709 } 1710 } 1711 1712 cam_periph_unlock(periph); 1713 error = copyout(data->entries, te->data, len); 1714 free(data, M_SCSICD); 1715 free(lead, M_SCSICD); 1716 } 1717 break; 1718 case CDIOREADTOCENTRY: 1719 { 1720 struct cd_toc_single *data; 1721 struct ioc_read_toc_single_entry *te = 1722 (struct ioc_read_toc_single_entry *) addr; 1723 struct ioc_toc_header *th; 1724 u_int32_t track; 1725 1726 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 1727 1728 cam_periph_lock(periph); 1729 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1730 ("trying to do CDIOREADTOCENTRY\n")); 1731 1732 if (te->address_format != CD_MSF_FORMAT 1733 && te->address_format != CD_LBA_FORMAT) { 1734 printf("error in readtocentry, " 1735 " returning EINVAL\n"); 1736 free(data, M_SCSICD); 1737 error = EINVAL; 1738 cam_periph_unlock(periph); 1739 break; 1740 } 1741 1742 th = &data->header; 1743 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 1744 sizeof (*th), /*sense_flags*/0); 1745 if (error) { 1746 free(data, M_SCSICD); 1747 cam_periph_unlock(periph); 1748 break; 1749 } 1750 1751 if (softc->quirks & CD_Q_BCD_TRACKS) { 1752 /* we are going to have to convert the BCD 1753 * encoding on the cd to what is expected 1754 */ 1755 th->starting_track = 1756 bcd2bin(th->starting_track); 1757 th->ending_track = bcd2bin(th->ending_track); 1758 } 1759 track = te->track; 1760 if (track == 0) 1761 track = th->starting_track; 1762 else if (track == LEADOUT) 1763 /* OK */; 1764 else if (track < th->starting_track || 1765 track > th->ending_track + 1) { 1766 printf("error in readtocentry, " 1767 " returning EINVAL\n"); 1768 free(data, M_SCSICD); 1769 error = EINVAL; 1770 cam_periph_unlock(periph); 1771 break; 1772 } 1773 1774 error = cdreadtoc(periph, te->address_format, track, 1775 (u_int8_t *)data, sizeof(*data), 1776 /*sense_flags*/0); 1777 if (error) { 1778 free(data, M_SCSICD); 1779 cam_periph_unlock(periph); 1780 break; 1781 } 1782 1783 if (softc->quirks & CD_Q_BCD_TRACKS) 1784 data->entry.track = bcd2bin(data->entry.track); 1785 bcopy(&data->entry, &te->entry, 1786 sizeof(struct cd_toc_entry)); 1787 free(data, M_SCSICD); 1788 cam_periph_unlock(periph); 1789 } 1790 break; 1791 case CDIOCSETPATCH: 1792 { 1793 struct ioc_patch *arg = (struct ioc_patch *)addr; 1794 struct cd_mode_params params; 1795 union cd_pages *page; 1796 1797 params.alloc_len = sizeof(union cd_mode_data_6_10); 1798 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1799 M_WAITOK | M_ZERO); 1800 1801 cam_periph_lock(periph); 1802 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1803 ("trying to do CDIOCSETPATCH\n")); 1804 1805 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1806 if (error) { 1807 free(params.mode_buf, M_SCSICD); 1808 cam_periph_unlock(periph); 1809 break; 1810 } 1811 page = cdgetpage(¶ms); 1812 1813 page->audio.port[LEFT_PORT].channels = 1814 arg->patch[0]; 1815 page->audio.port[RIGHT_PORT].channels = 1816 arg->patch[1]; 1817 page->audio.port[2].channels = arg->patch[2]; 1818 page->audio.port[3].channels = arg->patch[3]; 1819 error = cdsetmode(periph, ¶ms); 1820 free(params.mode_buf, M_SCSICD); 1821 cam_periph_unlock(periph); 1822 } 1823 break; 1824 case CDIOCGETVOL: 1825 { 1826 struct ioc_vol *arg = (struct ioc_vol *) addr; 1827 struct cd_mode_params params; 1828 union cd_pages *page; 1829 1830 params.alloc_len = sizeof(union cd_mode_data_6_10); 1831 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1832 M_WAITOK | M_ZERO); 1833 1834 cam_periph_lock(periph); 1835 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1836 ("trying to do CDIOCGETVOL\n")); 1837 1838 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1839 if (error) { 1840 free(params.mode_buf, M_SCSICD); 1841 cam_periph_unlock(periph); 1842 break; 1843 } 1844 page = cdgetpage(¶ms); 1845 1846 arg->vol[LEFT_PORT] = 1847 page->audio.port[LEFT_PORT].volume; 1848 arg->vol[RIGHT_PORT] = 1849 page->audio.port[RIGHT_PORT].volume; 1850 arg->vol[2] = page->audio.port[2].volume; 1851 arg->vol[3] = page->audio.port[3].volume; 1852 free(params.mode_buf, M_SCSICD); 1853 cam_periph_unlock(periph); 1854 } 1855 break; 1856 case CDIOCSETVOL: 1857 { 1858 struct ioc_vol *arg = (struct ioc_vol *) addr; 1859 struct cd_mode_params params; 1860 union cd_pages *page; 1861 1862 params.alloc_len = sizeof(union cd_mode_data_6_10); 1863 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1864 M_WAITOK | M_ZERO); 1865 1866 cam_periph_lock(periph); 1867 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1868 ("trying to do CDIOCSETVOL\n")); 1869 1870 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1871 if (error) { 1872 free(params.mode_buf, M_SCSICD); 1873 cam_periph_unlock(periph); 1874 break; 1875 } 1876 page = cdgetpage(¶ms); 1877 1878 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 1879 page->audio.port[LEFT_PORT].volume = 1880 arg->vol[LEFT_PORT]; 1881 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 1882 page->audio.port[RIGHT_PORT].volume = 1883 arg->vol[RIGHT_PORT]; 1884 page->audio.port[2].volume = arg->vol[2]; 1885 page->audio.port[3].volume = arg->vol[3]; 1886 error = cdsetmode(periph, ¶ms); 1887 cam_periph_unlock(periph); 1888 free(params.mode_buf, M_SCSICD); 1889 } 1890 break; 1891 case CDIOCSETMONO: 1892 { 1893 struct cd_mode_params params; 1894 union cd_pages *page; 1895 1896 params.alloc_len = sizeof(union cd_mode_data_6_10); 1897 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1898 M_WAITOK | M_ZERO); 1899 1900 cam_periph_lock(periph); 1901 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1902 ("trying to do CDIOCSETMONO\n")); 1903 1904 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1905 if (error) { 1906 free(params.mode_buf, M_SCSICD); 1907 cam_periph_unlock(periph); 1908 break; 1909 } 1910 page = cdgetpage(¶ms); 1911 1912 page->audio.port[LEFT_PORT].channels = 1913 LEFT_CHANNEL | RIGHT_CHANNEL; 1914 page->audio.port[RIGHT_PORT].channels = 1915 LEFT_CHANNEL | RIGHT_CHANNEL; 1916 page->audio.port[2].channels = 0; 1917 page->audio.port[3].channels = 0; 1918 error = cdsetmode(periph, ¶ms); 1919 cam_periph_unlock(periph); 1920 free(params.mode_buf, M_SCSICD); 1921 } 1922 break; 1923 case CDIOCSETSTEREO: 1924 { 1925 struct cd_mode_params params; 1926 union cd_pages *page; 1927 1928 params.alloc_len = sizeof(union cd_mode_data_6_10); 1929 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1930 M_WAITOK | M_ZERO); 1931 1932 cam_periph_lock(periph); 1933 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1934 ("trying to do CDIOCSETSTEREO\n")); 1935 1936 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1937 if (error) { 1938 free(params.mode_buf, M_SCSICD); 1939 cam_periph_unlock(periph); 1940 break; 1941 } 1942 page = cdgetpage(¶ms); 1943 1944 page->audio.port[LEFT_PORT].channels = 1945 LEFT_CHANNEL; 1946 page->audio.port[RIGHT_PORT].channels = 1947 RIGHT_CHANNEL; 1948 page->audio.port[2].channels = 0; 1949 page->audio.port[3].channels = 0; 1950 error = cdsetmode(periph, ¶ms); 1951 free(params.mode_buf, M_SCSICD); 1952 cam_periph_unlock(periph); 1953 } 1954 break; 1955 case CDIOCSETMUTE: 1956 { 1957 struct cd_mode_params params; 1958 union cd_pages *page; 1959 1960 params.alloc_len = sizeof(union cd_mode_data_6_10); 1961 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1962 M_WAITOK | M_ZERO); 1963 1964 cam_periph_lock(periph); 1965 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1966 ("trying to do CDIOCSETMUTE\n")); 1967 1968 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1969 if (error) { 1970 free(params.mode_buf, M_SCSICD); 1971 cam_periph_unlock(periph); 1972 break; 1973 } 1974 page = cdgetpage(¶ms); 1975 1976 page->audio.port[LEFT_PORT].channels = 0; 1977 page->audio.port[RIGHT_PORT].channels = 0; 1978 page->audio.port[2].channels = 0; 1979 page->audio.port[3].channels = 0; 1980 error = cdsetmode(periph, ¶ms); 1981 free(params.mode_buf, M_SCSICD); 1982 cam_periph_unlock(periph); 1983 } 1984 break; 1985 case CDIOCSETLEFT: 1986 { 1987 struct cd_mode_params params; 1988 union cd_pages *page; 1989 1990 params.alloc_len = sizeof(union cd_mode_data_6_10); 1991 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1992 M_WAITOK | M_ZERO); 1993 1994 cam_periph_lock(periph); 1995 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1996 ("trying to do CDIOCSETLEFT\n")); 1997 1998 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1999 if (error) { 2000 free(params.mode_buf, M_SCSICD); 2001 cam_periph_unlock(periph); 2002 break; 2003 } 2004 page = cdgetpage(¶ms); 2005 2006 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2007 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2008 page->audio.port[2].channels = 0; 2009 page->audio.port[3].channels = 0; 2010 error = cdsetmode(periph, ¶ms); 2011 free(params.mode_buf, M_SCSICD); 2012 cam_periph_unlock(periph); 2013 } 2014 break; 2015 case CDIOCSETRIGHT: 2016 { 2017 struct cd_mode_params params; 2018 union cd_pages *page; 2019 2020 params.alloc_len = sizeof(union cd_mode_data_6_10); 2021 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2022 M_WAITOK | M_ZERO); 2023 2024 cam_periph_lock(periph); 2025 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2026 ("trying to do CDIOCSETRIGHT\n")); 2027 2028 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2029 if (error) { 2030 free(params.mode_buf, M_SCSICD); 2031 cam_periph_unlock(periph); 2032 break; 2033 } 2034 page = cdgetpage(¶ms); 2035 2036 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2037 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2038 page->audio.port[2].channels = 0; 2039 page->audio.port[3].channels = 0; 2040 error = cdsetmode(periph, ¶ms); 2041 free(params.mode_buf, M_SCSICD); 2042 cam_periph_unlock(periph); 2043 } 2044 break; 2045 case CDIOCRESUME: 2046 cam_periph_lock(periph); 2047 error = cdpause(periph, 1); 2048 cam_periph_unlock(periph); 2049 break; 2050 case CDIOCPAUSE: 2051 cam_periph_lock(periph); 2052 error = cdpause(periph, 0); 2053 cam_periph_unlock(periph); 2054 break; 2055 case CDIOCSTART: 2056 cam_periph_lock(periph); 2057 error = cdstartunit(periph, 0); 2058 cam_periph_unlock(periph); 2059 break; 2060 case CDIOCCLOSE: 2061 cam_periph_lock(periph); 2062 error = cdstartunit(periph, 1); 2063 cam_periph_unlock(periph); 2064 break; 2065 case CDIOCSTOP: 2066 cam_periph_lock(periph); 2067 error = cdstopunit(periph, 0); 2068 cam_periph_unlock(periph); 2069 break; 2070 case CDIOCEJECT: 2071 cam_periph_lock(periph); 2072 error = cdstopunit(periph, 1); 2073 cam_periph_unlock(periph); 2074 break; 2075 case CDIOCALLOW: 2076 cam_periph_lock(periph); 2077 cdprevent(periph, PR_ALLOW); 2078 cam_periph_unlock(periph); 2079 break; 2080 case CDIOCPREVENT: 2081 cam_periph_lock(periph); 2082 cdprevent(periph, PR_PREVENT); 2083 cam_periph_unlock(periph); 2084 break; 2085 case CDIOCSETDEBUG: 2086 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2087 error = ENOTTY; 2088 break; 2089 case CDIOCCLRDEBUG: 2090 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2091 error = ENOTTY; 2092 break; 2093 case CDIOCRESET: 2094 /* return (cd_reset(periph)); */ 2095 error = ENOTTY; 2096 break; 2097 case CDRIOCREADSPEED: 2098 cam_periph_lock(periph); 2099 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2100 cam_periph_unlock(periph); 2101 break; 2102 case CDRIOCWRITESPEED: 2103 cam_periph_lock(periph); 2104 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2105 cam_periph_unlock(periph); 2106 break; 2107 case CDRIOCGETBLOCKSIZE: 2108 *(int *)addr = softc->params.blksize; 2109 break; 2110 case CDRIOCSETBLOCKSIZE: 2111 if (*(int *)addr <= 0) { 2112 error = EINVAL; 2113 break; 2114 } 2115 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr; 2116 break; 2117 case DVDIOCSENDKEY: 2118 case DVDIOCREPORTKEY: { 2119 struct dvd_authinfo *authinfo; 2120 2121 authinfo = (struct dvd_authinfo *)addr; 2122 2123 if (cmd == DVDIOCREPORTKEY) 2124 error = cdreportkey(periph, authinfo); 2125 else 2126 error = cdsendkey(periph, authinfo); 2127 break; 2128 } 2129 case DVDIOCREADSTRUCTURE: { 2130 struct dvd_struct *dvdstruct; 2131 2132 dvdstruct = (struct dvd_struct *)addr; 2133 2134 error = cdreaddvdstructure(periph, dvdstruct); 2135 2136 break; 2137 } 2138 default: 2139 cam_periph_lock(periph); 2140 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2141 cam_periph_unlock(periph); 2142 break; 2143 } 2144 2145 cam_periph_lock(periph); 2146 cam_periph_unhold(periph); 2147 2148 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2149 if (error && bootverbose) { 2150 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2151 } 2152 cam_periph_unlock(periph); 2153 2154 return (error); 2155 } 2156 2157 static void 2158 cdprevent(struct cam_periph *periph, int action) 2159 { 2160 union ccb *ccb; 2161 struct cd_softc *softc; 2162 int error; 2163 2164 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2165 2166 softc = (struct cd_softc *)periph->softc; 2167 2168 if (((action == PR_ALLOW) 2169 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2170 || ((action == PR_PREVENT) 2171 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2172 return; 2173 } 2174 2175 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2176 2177 scsi_prevent(&ccb->csio, 2178 /*retries*/ cd_retry_count, 2179 cddone, 2180 MSG_SIMPLE_Q_TAG, 2181 action, 2182 SSD_FULL_SIZE, 2183 /* timeout */60000); 2184 2185 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2186 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2187 2188 xpt_release_ccb(ccb); 2189 2190 if (error == 0) { 2191 if (action == PR_ALLOW) 2192 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2193 else 2194 softc->flags |= CD_FLAG_DISC_LOCKED; 2195 } 2196 } 2197 2198 /* 2199 * XXX: the disk media and sector size is only really able to change 2200 * XXX: while the device is closed. 2201 */ 2202 static int 2203 cdcheckmedia(struct cam_periph *periph) 2204 { 2205 struct cd_softc *softc; 2206 struct ioc_toc_header *toch; 2207 struct cd_toc_single leadout; 2208 u_int32_t size, toclen; 2209 int error, num_entries, cdindex; 2210 2211 softc = (struct cd_softc *)periph->softc; 2212 2213 cdprevent(periph, PR_PREVENT); 2214 softc->disk->d_sectorsize = 2048; 2215 softc->disk->d_mediasize = 0; 2216 2217 /* 2218 * Get the disc size and block size. If we can't get it, we don't 2219 * have media, most likely. 2220 */ 2221 if ((error = cdsize(periph, &size)) != 0) { 2222 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2223 cdprevent(periph, PR_ALLOW); 2224 return (error); 2225 } else { 2226 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA; 2227 softc->disk->d_sectorsize = softc->params.blksize; 2228 softc->disk->d_mediasize = 2229 (off_t)softc->params.blksize * softc->params.disksize; 2230 } 2231 2232 /* 2233 * Now we check the table of contents. This (currently) is only 2234 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2235 * things like present a separate entry in /dev for each track, 2236 * like that acd(4) driver does. 2237 */ 2238 bzero(&softc->toc, sizeof(softc->toc)); 2239 toch = &softc->toc.header; 2240 /* 2241 * We will get errors here for media that doesn't have a table of 2242 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2243 * command is presented for a DDCD/CD-R/RW media, where the first TOC 2244 * has not been recorded (no complete session) and the Format codes 2245 * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2246 * with an INVALID FIELD IN CDB. Devices that are not capable of 2247 * reading an incomplete session on DDC/CD-R/RW media shall report 2248 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2249 * 2250 * So this isn't fatal if we can't read the table of contents, it 2251 * just means that the user won't be able to issue the play tracks 2252 * ioctl, and likely lots of other stuff won't work either. They 2253 * need to burn the CD before we can do a whole lot with it. So 2254 * we don't print anything here if we get an error back. 2255 */ 2256 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2257 SF_NO_PRINT); 2258 /* 2259 * Errors in reading the table of contents aren't fatal, we just 2260 * won't have a valid table of contents cached. 2261 */ 2262 if (error != 0) { 2263 error = 0; 2264 bzero(&softc->toc, sizeof(softc->toc)); 2265 goto bailout; 2266 } 2267 2268 if (softc->quirks & CD_Q_BCD_TRACKS) { 2269 toch->starting_track = bcd2bin(toch->starting_track); 2270 toch->ending_track = bcd2bin(toch->ending_track); 2271 } 2272 2273 /* Number of TOC entries, plus leadout */ 2274 num_entries = (toch->ending_track - toch->starting_track) + 2; 2275 2276 if (num_entries <= 0) 2277 goto bailout; 2278 2279 toclen = num_entries * sizeof(struct cd_toc_entry); 2280 2281 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2282 (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2283 SF_NO_PRINT); 2284 if (error != 0) { 2285 error = 0; 2286 bzero(&softc->toc, sizeof(softc->toc)); 2287 goto bailout; 2288 } 2289 2290 if (softc->quirks & CD_Q_BCD_TRACKS) { 2291 toch->starting_track = bcd2bin(toch->starting_track); 2292 toch->ending_track = bcd2bin(toch->ending_track); 2293 } 2294 /* 2295 * XXX KDM is this necessary? Probably only if the drive doesn't 2296 * return leadout information with the table of contents. 2297 */ 2298 cdindex = toch->starting_track + num_entries -1; 2299 if (cdindex == toch->ending_track + 1) { 2300 2301 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2302 (u_int8_t *)&leadout, sizeof(leadout), 2303 SF_NO_PRINT); 2304 if (error != 0) { 2305 error = 0; 2306 goto bailout; 2307 } 2308 softc->toc.entries[cdindex - toch->starting_track] = 2309 leadout.entry; 2310 } 2311 if (softc->quirks & CD_Q_BCD_TRACKS) { 2312 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2313 softc->toc.entries[cdindex].track = 2314 bcd2bin(softc->toc.entries[cdindex].track); 2315 } 2316 } 2317 2318 softc->flags |= CD_FLAG_VALID_TOC; 2319 2320 /* If the first track is audio, correct sector size. */ 2321 if ((softc->toc.entries[0].control & 4) == 0) { 2322 softc->disk->d_sectorsize = softc->params.blksize = 2352; 2323 softc->disk->d_mediasize = 2324 (off_t)softc->params.blksize * softc->params.disksize; 2325 } 2326 2327 bailout: 2328 2329 /* 2330 * We unconditionally (re)set the blocksize each time the 2331 * CD device is opened. This is because the CD can change, 2332 * and therefore the blocksize might change. 2333 * XXX problems here if some slice or partition is still 2334 * open with the old size? 2335 */ 2336 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2337 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2338 softc->disk->d_devstat->block_size = softc->params.blksize; 2339 2340 return (error); 2341 } 2342 2343 static int 2344 cdsize(struct cam_periph *periph, u_int32_t *size) 2345 { 2346 struct cd_softc *softc; 2347 union ccb *ccb; 2348 struct scsi_read_capacity_data *rcap_buf; 2349 int error; 2350 2351 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2352 2353 softc = (struct cd_softc *)periph->softc; 2354 2355 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2356 2357 /* XXX Should be M_WAITOK */ 2358 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2359 M_SCSICD, M_NOWAIT | M_ZERO); 2360 if (rcap_buf == NULL) 2361 return (ENOMEM); 2362 2363 scsi_read_capacity(&ccb->csio, 2364 /*retries*/ cd_retry_count, 2365 cddone, 2366 MSG_SIMPLE_Q_TAG, 2367 rcap_buf, 2368 SSD_FULL_SIZE, 2369 /* timeout */20000); 2370 2371 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2372 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2373 2374 xpt_release_ccb(ccb); 2375 2376 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 2377 softc->params.blksize = scsi_4btoul(rcap_buf->length); 2378 /* Make sure we got at least some block size. */ 2379 if (error == 0 && softc->params.blksize == 0) 2380 error = EIO; 2381 /* 2382 * SCSI-3 mandates that the reported blocksize shall be 2048. 2383 * Older drives sometimes report funny values, trim it down to 2384 * 2048, or other parts of the kernel will get confused. 2385 * 2386 * XXX we leave drives alone that might report 512 bytes, as 2387 * well as drives reporting more weird sizes like perhaps 4K. 2388 */ 2389 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 2390 softc->params.blksize = 2048; 2391 2392 free(rcap_buf, M_SCSICD); 2393 *size = softc->params.disksize; 2394 2395 return (error); 2396 2397 } 2398 2399 static int 2400 cd6byteworkaround(union ccb *ccb) 2401 { 2402 u_int8_t *cdb; 2403 struct cam_periph *periph; 2404 struct cd_softc *softc; 2405 struct cd_mode_params *params; 2406 int frozen, found; 2407 2408 periph = xpt_path_periph(ccb->ccb_h.path); 2409 softc = (struct cd_softc *)periph->softc; 2410 2411 cdb = ccb->csio.cdb_io.cdb_bytes; 2412 2413 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2414 || ((cdb[0] != MODE_SENSE_6) 2415 && (cdb[0] != MODE_SELECT_6))) 2416 return (0); 2417 2418 /* 2419 * Because there is no convenient place to stash the overall 2420 * cd_mode_params structure pointer, we have to grab it like this. 2421 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2422 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2423 * 2424 * XXX It would be nice if, at some point, we could increase the 2425 * number of available peripheral private pointers. Both pointers 2426 * are currently used in most every peripheral driver. 2427 */ 2428 found = 0; 2429 2430 STAILQ_FOREACH(params, &softc->mode_queue, links) { 2431 if (params->mode_buf == ccb->csio.data_ptr) { 2432 found = 1; 2433 break; 2434 } 2435 } 2436 2437 /* 2438 * This shouldn't happen. All mode sense and mode select 2439 * operations in the cd(4) driver MUST go through cdgetmode() and 2440 * cdsetmode()! 2441 */ 2442 if (found == 0) { 2443 xpt_print(periph->path, 2444 "mode buffer not found in mode queue!\n"); 2445 return (0); 2446 } 2447 2448 params->cdb_size = 10; 2449 softc->minimum_command_size = 10; 2450 xpt_print(ccb->ccb_h.path, 2451 "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 2452 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 2453 2454 if (cdb[0] == MODE_SENSE_6) { 2455 struct scsi_mode_sense_10 ms10; 2456 struct scsi_mode_sense_6 *ms6; 2457 int len; 2458 2459 ms6 = (struct scsi_mode_sense_6 *)cdb; 2460 2461 bzero(&ms10, sizeof(ms10)); 2462 ms10.opcode = MODE_SENSE_10; 2463 ms10.byte2 = ms6->byte2; 2464 ms10.page = ms6->page; 2465 2466 /* 2467 * 10 byte mode header, block descriptor, 2468 * sizeof(union cd_pages) 2469 */ 2470 len = sizeof(struct cd_mode_data_10); 2471 ccb->csio.dxfer_len = len; 2472 2473 scsi_ulto2b(len, ms10.length); 2474 ms10.control = ms6->control; 2475 bcopy(&ms10, cdb, 10); 2476 ccb->csio.cdb_len = 10; 2477 } else { 2478 struct scsi_mode_select_10 ms10; 2479 struct scsi_mode_select_6 *ms6; 2480 struct scsi_mode_header_6 *header6; 2481 struct scsi_mode_header_10 *header10; 2482 struct scsi_mode_page_header *page_header; 2483 int blk_desc_len, page_num, page_size, len; 2484 2485 ms6 = (struct scsi_mode_select_6 *)cdb; 2486 2487 bzero(&ms10, sizeof(ms10)); 2488 ms10.opcode = MODE_SELECT_10; 2489 ms10.byte2 = ms6->byte2; 2490 2491 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 2492 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 2493 2494 page_header = find_mode_page_6(header6); 2495 page_num = page_header->page_code; 2496 2497 blk_desc_len = header6->blk_desc_len; 2498 2499 page_size = cdgetpagesize(page_num); 2500 2501 if (page_size != (page_header->page_length + 2502 sizeof(*page_header))) 2503 page_size = page_header->page_length + 2504 sizeof(*page_header); 2505 2506 len = sizeof(*header10) + blk_desc_len + page_size; 2507 2508 len = min(params->alloc_len, len); 2509 2510 /* 2511 * Since the 6 byte parameter header is shorter than the 10 2512 * byte parameter header, we need to copy the actual mode 2513 * page data, and the block descriptor, if any, so things wind 2514 * up in the right place. The regions will overlap, but 2515 * bcopy() does the right thing. 2516 */ 2517 bcopy(params->mode_buf + sizeof(*header6), 2518 params->mode_buf + sizeof(*header10), 2519 len - sizeof(*header10)); 2520 2521 /* Make sure these fields are set correctly. */ 2522 scsi_ulto2b(0, header10->data_length); 2523 header10->medium_type = 0; 2524 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 2525 2526 ccb->csio.dxfer_len = len; 2527 2528 scsi_ulto2b(len, ms10.length); 2529 ms10.control = ms6->control; 2530 bcopy(&ms10, cdb, 10); 2531 ccb->csio.cdb_len = 10; 2532 } 2533 2534 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 2535 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2536 xpt_action(ccb); 2537 if (frozen) { 2538 cam_release_devq(ccb->ccb_h.path, 2539 /*relsim_flags*/0, 2540 /*openings*/0, 2541 /*timeout*/0, 2542 /*getcount_only*/0); 2543 } 2544 2545 return (ERESTART); 2546 } 2547 2548 static int 2549 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2550 { 2551 struct cd_softc *softc; 2552 struct cam_periph *periph; 2553 int error, error_code, sense_key, asc, ascq; 2554 2555 periph = xpt_path_periph(ccb->ccb_h.path); 2556 softc = (struct cd_softc *)periph->softc; 2557 2558 error = 0; 2559 2560 /* 2561 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 2562 * CDB comes back with this particular error, try transforming it 2563 * into the 10 byte version. 2564 */ 2565 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 2566 error = cd6byteworkaround(ccb); 2567 } else if (scsi_extract_sense_ccb(ccb, 2568 &error_code, &sense_key, &asc, &ascq)) { 2569 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 2570 error = cd6byteworkaround(ccb); 2571 else if (sense_key == SSD_KEY_UNIT_ATTENTION && 2572 asc == 0x28 && ascq == 0x00) 2573 disk_media_changed(softc->disk, M_NOWAIT); 2574 else if (sense_key == SSD_KEY_NOT_READY && 2575 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) { 2576 softc->flags &= ~CD_FLAG_SAW_MEDIA; 2577 disk_media_gone(softc->disk, M_NOWAIT); 2578 } 2579 } 2580 2581 if (error == ERESTART) 2582 return (error); 2583 2584 /* 2585 * XXX 2586 * Until we have a better way of doing pack validation, 2587 * don't treat UAs as errors. 2588 */ 2589 sense_flags |= SF_RETRY_UA; 2590 2591 if (softc->quirks & CD_Q_RETRY_BUSY) 2592 sense_flags |= SF_RETRY_BUSY; 2593 return (cam_periph_error(ccb, cam_flags, sense_flags)); 2594 } 2595 2596 static void 2597 cdmediapoll(void *arg) 2598 { 2599 struct cam_periph *periph = arg; 2600 struct cd_softc *softc = periph->softc; 2601 2602 if (softc->state == CD_STATE_NORMAL && !softc->tur && 2603 softc->outstanding_cmds == 0) { 2604 if (cam_periph_acquire(periph) == CAM_REQ_CMP) { 2605 softc->tur = 1; 2606 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 2607 } 2608 } 2609 /* Queue us up again */ 2610 if (cd_poll_period != 0) 2611 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz); 2612 } 2613 2614 /* 2615 * Read table of contents 2616 */ 2617 static int 2618 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 2619 u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 2620 { 2621 struct scsi_read_toc *scsi_cmd; 2622 u_int32_t ntoc; 2623 struct ccb_scsiio *csio; 2624 union ccb *ccb; 2625 int error; 2626 2627 ntoc = len; 2628 error = 0; 2629 2630 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2631 2632 csio = &ccb->csio; 2633 2634 cam_fill_csio(csio, 2635 /* retries */ cd_retry_count, 2636 /* cbfcnp */ cddone, 2637 /* flags */ CAM_DIR_IN, 2638 /* tag_action */ MSG_SIMPLE_Q_TAG, 2639 /* data_ptr */ data, 2640 /* dxfer_len */ len, 2641 /* sense_len */ SSD_FULL_SIZE, 2642 sizeof(struct scsi_read_toc), 2643 /* timeout */ 50000); 2644 2645 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 2646 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2647 2648 if (mode == CD_MSF_FORMAT) 2649 scsi_cmd->byte2 |= CD_MSF; 2650 scsi_cmd->from_track = start; 2651 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 2652 scsi_cmd->data_len[0] = (ntoc) >> 8; 2653 scsi_cmd->data_len[1] = (ntoc) & 0xff; 2654 2655 scsi_cmd->op_code = READ_TOC; 2656 2657 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2658 /*sense_flags*/SF_RETRY_UA | sense_flags); 2659 2660 xpt_release_ccb(ccb); 2661 2662 return(error); 2663 } 2664 2665 static int 2666 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 2667 u_int32_t format, int track, 2668 struct cd_sub_channel_info *data, u_int32_t len) 2669 { 2670 struct scsi_read_subchannel *scsi_cmd; 2671 struct ccb_scsiio *csio; 2672 union ccb *ccb; 2673 int error; 2674 2675 error = 0; 2676 2677 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2678 2679 csio = &ccb->csio; 2680 2681 cam_fill_csio(csio, 2682 /* retries */ cd_retry_count, 2683 /* cbfcnp */ cddone, 2684 /* flags */ CAM_DIR_IN, 2685 /* tag_action */ MSG_SIMPLE_Q_TAG, 2686 /* data_ptr */ (u_int8_t *)data, 2687 /* dxfer_len */ len, 2688 /* sense_len */ SSD_FULL_SIZE, 2689 sizeof(struct scsi_read_subchannel), 2690 /* timeout */ 50000); 2691 2692 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 2693 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2694 2695 scsi_cmd->op_code = READ_SUBCHANNEL; 2696 if (mode == CD_MSF_FORMAT) 2697 scsi_cmd->byte1 |= CD_MSF; 2698 scsi_cmd->byte2 = SRS_SUBQ; 2699 scsi_cmd->subchan_format = format; 2700 scsi_cmd->track = track; 2701 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 2702 scsi_cmd->control = 0; 2703 2704 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2705 /*sense_flags*/SF_RETRY_UA); 2706 2707 xpt_release_ccb(ccb); 2708 2709 return(error); 2710 } 2711 2712 2713 /* 2714 * All MODE_SENSE requests in the cd(4) driver MUST go through this 2715 * routine. See comments in cd6byteworkaround() for details. 2716 */ 2717 static int 2718 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 2719 u_int32_t page) 2720 { 2721 struct ccb_scsiio *csio; 2722 struct cd_softc *softc; 2723 union ccb *ccb; 2724 int param_len; 2725 int error; 2726 2727 softc = (struct cd_softc *)periph->softc; 2728 2729 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2730 2731 csio = &ccb->csio; 2732 2733 data->cdb_size = softc->minimum_command_size; 2734 if (data->cdb_size < 10) 2735 param_len = sizeof(struct cd_mode_data); 2736 else 2737 param_len = sizeof(struct cd_mode_data_10); 2738 2739 /* Don't say we've got more room than we actually allocated */ 2740 param_len = min(param_len, data->alloc_len); 2741 2742 scsi_mode_sense_len(csio, 2743 /* retries */ cd_retry_count, 2744 /* cbfcnp */ cddone, 2745 /* tag_action */ MSG_SIMPLE_Q_TAG, 2746 /* dbd */ 0, 2747 /* page_code */ SMS_PAGE_CTRL_CURRENT, 2748 /* page */ page, 2749 /* param_buf */ data->mode_buf, 2750 /* param_len */ param_len, 2751 /* minimum_cmd_size */ softc->minimum_command_size, 2752 /* sense_len */ SSD_FULL_SIZE, 2753 /* timeout */ 50000); 2754 2755 /* 2756 * It would be nice not to have to do this, but there's no 2757 * available pointer in the CCB that would allow us to stuff the 2758 * mode params structure in there and retrieve it in 2759 * cd6byteworkaround(), so we can set the cdb size. The cdb size 2760 * lets the caller know what CDB size we ended up using, so they 2761 * can find the actual mode page offset. 2762 */ 2763 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 2764 2765 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2766 /*sense_flags*/SF_RETRY_UA); 2767 2768 xpt_release_ccb(ccb); 2769 2770 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 2771 2772 /* 2773 * This is a bit of belt-and-suspenders checking, but if we run 2774 * into a situation where the target sends back multiple block 2775 * descriptors, we might not have enough space in the buffer to 2776 * see the whole mode page. Better to return an error than 2777 * potentially access memory beyond our malloced region. 2778 */ 2779 if (error == 0) { 2780 u_int32_t data_len; 2781 2782 if (data->cdb_size == 10) { 2783 struct scsi_mode_header_10 *hdr10; 2784 2785 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 2786 data_len = scsi_2btoul(hdr10->data_length); 2787 data_len += sizeof(hdr10->data_length); 2788 } else { 2789 struct scsi_mode_header_6 *hdr6; 2790 2791 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 2792 data_len = hdr6->data_length; 2793 data_len += sizeof(hdr6->data_length); 2794 } 2795 2796 /* 2797 * Complain if there is more mode data available than we 2798 * allocated space for. This could potentially happen if 2799 * we miscalculated the page length for some reason, if the 2800 * drive returns multiple block descriptors, or if it sets 2801 * the data length incorrectly. 2802 */ 2803 if (data_len > data->alloc_len) { 2804 xpt_print(periph->path, "allocated modepage %d length " 2805 "%d < returned length %d\n", page, data->alloc_len, 2806 data_len); 2807 error = ENOSPC; 2808 } 2809 } 2810 return (error); 2811 } 2812 2813 /* 2814 * All MODE_SELECT requests in the cd(4) driver MUST go through this 2815 * routine. See comments in cd6byteworkaround() for details. 2816 */ 2817 static int 2818 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 2819 { 2820 struct ccb_scsiio *csio; 2821 struct cd_softc *softc; 2822 union ccb *ccb; 2823 int cdb_size, param_len; 2824 int error; 2825 2826 softc = (struct cd_softc *)periph->softc; 2827 2828 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2829 2830 csio = &ccb->csio; 2831 2832 error = 0; 2833 2834 /* 2835 * If the data is formatted for the 10 byte version of the mode 2836 * select parameter list, we need to use the 10 byte CDB. 2837 * Otherwise, we use whatever the stored minimum command size. 2838 */ 2839 if (data->cdb_size == 10) 2840 cdb_size = data->cdb_size; 2841 else 2842 cdb_size = softc->minimum_command_size; 2843 2844 if (cdb_size >= 10) { 2845 struct scsi_mode_header_10 *mode_header; 2846 u_int32_t data_len; 2847 2848 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 2849 2850 data_len = scsi_2btoul(mode_header->data_length); 2851 2852 scsi_ulto2b(0, mode_header->data_length); 2853 /* 2854 * SONY drives do not allow a mode select with a medium_type 2855 * value that has just been returned by a mode sense; use a 2856 * medium_type of 0 (Default) instead. 2857 */ 2858 mode_header->medium_type = 0; 2859 2860 /* 2861 * Pass back whatever the drive passed to us, plus the size 2862 * of the data length field. 2863 */ 2864 param_len = data_len + sizeof(mode_header->data_length); 2865 2866 } else { 2867 struct scsi_mode_header_6 *mode_header; 2868 2869 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 2870 2871 param_len = mode_header->data_length + 1; 2872 2873 mode_header->data_length = 0; 2874 /* 2875 * SONY drives do not allow a mode select with a medium_type 2876 * value that has just been returned by a mode sense; use a 2877 * medium_type of 0 (Default) instead. 2878 */ 2879 mode_header->medium_type = 0; 2880 } 2881 2882 /* Don't say we've got more room than we actually allocated */ 2883 param_len = min(param_len, data->alloc_len); 2884 2885 scsi_mode_select_len(csio, 2886 /* retries */ cd_retry_count, 2887 /* cbfcnp */ cddone, 2888 /* tag_action */ MSG_SIMPLE_Q_TAG, 2889 /* scsi_page_fmt */ 1, 2890 /* save_pages */ 0, 2891 /* param_buf */ data->mode_buf, 2892 /* param_len */ param_len, 2893 /* minimum_cmd_size */ cdb_size, 2894 /* sense_len */ SSD_FULL_SIZE, 2895 /* timeout */ 50000); 2896 2897 /* See comments in cdgetmode() and cd6byteworkaround(). */ 2898 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 2899 2900 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2901 /*sense_flags*/SF_RETRY_UA); 2902 2903 xpt_release_ccb(ccb); 2904 2905 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 2906 2907 return (error); 2908 } 2909 2910 2911 static int 2912 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 2913 { 2914 struct ccb_scsiio *csio; 2915 union ccb *ccb; 2916 int error; 2917 u_int8_t cdb_len; 2918 2919 error = 0; 2920 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2921 csio = &ccb->csio; 2922 /* 2923 * Use the smallest possible command to perform the operation. 2924 */ 2925 if ((len & 0xffff0000) == 0) { 2926 /* 2927 * We can fit in a 10 byte cdb. 2928 */ 2929 struct scsi_play_10 *scsi_cmd; 2930 2931 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 2932 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2933 scsi_cmd->op_code = PLAY_10; 2934 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2935 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 2936 cdb_len = sizeof(*scsi_cmd); 2937 } else { 2938 struct scsi_play_12 *scsi_cmd; 2939 2940 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 2941 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2942 scsi_cmd->op_code = PLAY_12; 2943 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2944 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 2945 cdb_len = sizeof(*scsi_cmd); 2946 } 2947 cam_fill_csio(csio, 2948 /*retries*/ cd_retry_count, 2949 cddone, 2950 /*flags*/CAM_DIR_NONE, 2951 MSG_SIMPLE_Q_TAG, 2952 /*dataptr*/NULL, 2953 /*datalen*/0, 2954 /*sense_len*/SSD_FULL_SIZE, 2955 cdb_len, 2956 /*timeout*/50 * 1000); 2957 2958 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2959 /*sense_flags*/SF_RETRY_UA); 2960 2961 xpt_release_ccb(ccb); 2962 2963 return(error); 2964 } 2965 2966 static int 2967 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 2968 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 2969 { 2970 struct scsi_play_msf *scsi_cmd; 2971 struct ccb_scsiio *csio; 2972 union ccb *ccb; 2973 int error; 2974 2975 error = 0; 2976 2977 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2978 2979 csio = &ccb->csio; 2980 2981 cam_fill_csio(csio, 2982 /* retries */ cd_retry_count, 2983 /* cbfcnp */ cddone, 2984 /* flags */ CAM_DIR_NONE, 2985 /* tag_action */ MSG_SIMPLE_Q_TAG, 2986 /* data_ptr */ NULL, 2987 /* dxfer_len */ 0, 2988 /* sense_len */ SSD_FULL_SIZE, 2989 sizeof(struct scsi_play_msf), 2990 /* timeout */ 50000); 2991 2992 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 2993 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2994 2995 scsi_cmd->op_code = PLAY_MSF; 2996 scsi_cmd->start_m = startm; 2997 scsi_cmd->start_s = starts; 2998 scsi_cmd->start_f = startf; 2999 scsi_cmd->end_m = endm; 3000 scsi_cmd->end_s = ends; 3001 scsi_cmd->end_f = endf; 3002 3003 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3004 /*sense_flags*/SF_RETRY_UA); 3005 3006 xpt_release_ccb(ccb); 3007 3008 return(error); 3009 } 3010 3011 3012 static int 3013 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 3014 u_int32_t etrack, u_int32_t eindex) 3015 { 3016 struct scsi_play_track *scsi_cmd; 3017 struct ccb_scsiio *csio; 3018 union ccb *ccb; 3019 int error; 3020 3021 error = 0; 3022 3023 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3024 3025 csio = &ccb->csio; 3026 3027 cam_fill_csio(csio, 3028 /* retries */ cd_retry_count, 3029 /* cbfcnp */ cddone, 3030 /* flags */ CAM_DIR_NONE, 3031 /* tag_action */ MSG_SIMPLE_Q_TAG, 3032 /* data_ptr */ NULL, 3033 /* dxfer_len */ 0, 3034 /* sense_len */ SSD_FULL_SIZE, 3035 sizeof(struct scsi_play_track), 3036 /* timeout */ 50000); 3037 3038 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3039 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3040 3041 scsi_cmd->op_code = PLAY_TRACK; 3042 scsi_cmd->start_track = strack; 3043 scsi_cmd->start_index = sindex; 3044 scsi_cmd->end_track = etrack; 3045 scsi_cmd->end_index = eindex; 3046 3047 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3048 /*sense_flags*/SF_RETRY_UA); 3049 3050 xpt_release_ccb(ccb); 3051 3052 return(error); 3053 } 3054 3055 static int 3056 cdpause(struct cam_periph *periph, u_int32_t go) 3057 { 3058 struct scsi_pause *scsi_cmd; 3059 struct ccb_scsiio *csio; 3060 union ccb *ccb; 3061 int error; 3062 3063 error = 0; 3064 3065 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3066 3067 csio = &ccb->csio; 3068 3069 cam_fill_csio(csio, 3070 /* retries */ cd_retry_count, 3071 /* cbfcnp */ cddone, 3072 /* flags */ CAM_DIR_NONE, 3073 /* tag_action */ MSG_SIMPLE_Q_TAG, 3074 /* data_ptr */ NULL, 3075 /* dxfer_len */ 0, 3076 /* sense_len */ SSD_FULL_SIZE, 3077 sizeof(struct scsi_pause), 3078 /* timeout */ 50000); 3079 3080 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3081 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3082 3083 scsi_cmd->op_code = PAUSE; 3084 scsi_cmd->resume = go; 3085 3086 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3087 /*sense_flags*/SF_RETRY_UA); 3088 3089 xpt_release_ccb(ccb); 3090 3091 return(error); 3092 } 3093 3094 static int 3095 cdstartunit(struct cam_periph *periph, int load) 3096 { 3097 union ccb *ccb; 3098 int error; 3099 3100 error = 0; 3101 3102 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3103 3104 scsi_start_stop(&ccb->csio, 3105 /* retries */ cd_retry_count, 3106 /* cbfcnp */ cddone, 3107 /* tag_action */ MSG_SIMPLE_Q_TAG, 3108 /* start */ TRUE, 3109 /* load_eject */ load, 3110 /* immediate */ FALSE, 3111 /* sense_len */ SSD_FULL_SIZE, 3112 /* timeout */ 50000); 3113 3114 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3115 /*sense_flags*/SF_RETRY_UA); 3116 3117 xpt_release_ccb(ccb); 3118 3119 return(error); 3120 } 3121 3122 static int 3123 cdstopunit(struct cam_periph *periph, u_int32_t eject) 3124 { 3125 union ccb *ccb; 3126 int error; 3127 3128 error = 0; 3129 3130 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3131 3132 scsi_start_stop(&ccb->csio, 3133 /* retries */ cd_retry_count, 3134 /* cbfcnp */ cddone, 3135 /* tag_action */ MSG_SIMPLE_Q_TAG, 3136 /* start */ FALSE, 3137 /* load_eject */ eject, 3138 /* immediate */ FALSE, 3139 /* sense_len */ SSD_FULL_SIZE, 3140 /* timeout */ 50000); 3141 3142 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3143 /*sense_flags*/SF_RETRY_UA); 3144 3145 xpt_release_ccb(ccb); 3146 3147 return(error); 3148 } 3149 3150 static int 3151 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3152 { 3153 struct scsi_set_speed *scsi_cmd; 3154 struct ccb_scsiio *csio; 3155 union ccb *ccb; 3156 int error; 3157 3158 error = 0; 3159 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3160 csio = &ccb->csio; 3161 3162 /* Preserve old behavior: units in multiples of CDROM speed */ 3163 if (rdspeed < 177) 3164 rdspeed *= 177; 3165 if (wrspeed < 177) 3166 wrspeed *= 177; 3167 3168 cam_fill_csio(csio, 3169 /* retries */ cd_retry_count, 3170 /* cbfcnp */ cddone, 3171 /* flags */ CAM_DIR_NONE, 3172 /* tag_action */ MSG_SIMPLE_Q_TAG, 3173 /* data_ptr */ NULL, 3174 /* dxfer_len */ 0, 3175 /* sense_len */ SSD_FULL_SIZE, 3176 sizeof(struct scsi_set_speed), 3177 /* timeout */ 50000); 3178 3179 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3180 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3181 3182 scsi_cmd->opcode = SET_CD_SPEED; 3183 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3184 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3185 3186 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3187 /*sense_flags*/SF_RETRY_UA); 3188 3189 xpt_release_ccb(ccb); 3190 3191 return(error); 3192 } 3193 3194 static int 3195 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3196 { 3197 union ccb *ccb; 3198 u_int8_t *databuf; 3199 u_int32_t lba; 3200 int error; 3201 int length; 3202 3203 error = 0; 3204 databuf = NULL; 3205 lba = 0; 3206 3207 switch (authinfo->format) { 3208 case DVD_REPORT_AGID: 3209 length = sizeof(struct scsi_report_key_data_agid); 3210 break; 3211 case DVD_REPORT_CHALLENGE: 3212 length = sizeof(struct scsi_report_key_data_challenge); 3213 break; 3214 case DVD_REPORT_KEY1: 3215 length = sizeof(struct scsi_report_key_data_key1_key2); 3216 break; 3217 case DVD_REPORT_TITLE_KEY: 3218 length = sizeof(struct scsi_report_key_data_title); 3219 /* The lba field is only set for the title key */ 3220 lba = authinfo->lba; 3221 break; 3222 case DVD_REPORT_ASF: 3223 length = sizeof(struct scsi_report_key_data_asf); 3224 break; 3225 case DVD_REPORT_RPC: 3226 length = sizeof(struct scsi_report_key_data_rpc); 3227 break; 3228 case DVD_INVALIDATE_AGID: 3229 length = 0; 3230 break; 3231 default: 3232 return (EINVAL); 3233 } 3234 3235 if (length != 0) { 3236 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3237 } else 3238 databuf = NULL; 3239 3240 cam_periph_lock(periph); 3241 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3242 3243 scsi_report_key(&ccb->csio, 3244 /* retries */ cd_retry_count, 3245 /* cbfcnp */ cddone, 3246 /* tag_action */ MSG_SIMPLE_Q_TAG, 3247 /* lba */ lba, 3248 /* agid */ authinfo->agid, 3249 /* key_format */ authinfo->format, 3250 /* data_ptr */ databuf, 3251 /* dxfer_len */ length, 3252 /* sense_len */ SSD_FULL_SIZE, 3253 /* timeout */ 50000); 3254 3255 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3256 /*sense_flags*/SF_RETRY_UA); 3257 3258 if (error != 0) 3259 goto bailout; 3260 3261 if (ccb->csio.resid != 0) { 3262 xpt_print(periph->path, "warning, residual for report key " 3263 "command is %d\n", ccb->csio.resid); 3264 } 3265 3266 switch(authinfo->format) { 3267 case DVD_REPORT_AGID: { 3268 struct scsi_report_key_data_agid *agid_data; 3269 3270 agid_data = (struct scsi_report_key_data_agid *)databuf; 3271 3272 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3273 RKD_AGID_SHIFT; 3274 break; 3275 } 3276 case DVD_REPORT_CHALLENGE: { 3277 struct scsi_report_key_data_challenge *chal_data; 3278 3279 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3280 3281 bcopy(chal_data->challenge_key, authinfo->keychal, 3282 min(sizeof(chal_data->challenge_key), 3283 sizeof(authinfo->keychal))); 3284 break; 3285 } 3286 case DVD_REPORT_KEY1: { 3287 struct scsi_report_key_data_key1_key2 *key1_data; 3288 3289 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3290 3291 bcopy(key1_data->key1, authinfo->keychal, 3292 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3293 break; 3294 } 3295 case DVD_REPORT_TITLE_KEY: { 3296 struct scsi_report_key_data_title *title_data; 3297 3298 title_data = (struct scsi_report_key_data_title *)databuf; 3299 3300 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3301 RKD_TITLE_CPM_SHIFT; 3302 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3303 RKD_TITLE_CP_SEC_SHIFT; 3304 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3305 RKD_TITLE_CMGS_SHIFT; 3306 bcopy(title_data->title_key, authinfo->keychal, 3307 min(sizeof(title_data->title_key), 3308 sizeof(authinfo->keychal))); 3309 break; 3310 } 3311 case DVD_REPORT_ASF: { 3312 struct scsi_report_key_data_asf *asf_data; 3313 3314 asf_data = (struct scsi_report_key_data_asf *)databuf; 3315 3316 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3317 break; 3318 } 3319 case DVD_REPORT_RPC: { 3320 struct scsi_report_key_data_rpc *rpc_data; 3321 3322 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3323 3324 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3325 RKD_RPC_TYPE_SHIFT; 3326 authinfo->vend_rsts = 3327 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3328 RKD_RPC_VENDOR_RESET_SHIFT; 3329 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3330 authinfo->region = rpc_data->region_mask; 3331 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3332 break; 3333 } 3334 case DVD_INVALIDATE_AGID: 3335 break; 3336 default: 3337 /* This should be impossible, since we checked above */ 3338 error = EINVAL; 3339 goto bailout; 3340 break; /* NOTREACHED */ 3341 } 3342 3343 bailout: 3344 xpt_release_ccb(ccb); 3345 cam_periph_unlock(periph); 3346 3347 if (databuf != NULL) 3348 free(databuf, M_DEVBUF); 3349 3350 return(error); 3351 } 3352 3353 static int 3354 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3355 { 3356 union ccb *ccb; 3357 u_int8_t *databuf; 3358 int length; 3359 int error; 3360 3361 error = 0; 3362 databuf = NULL; 3363 3364 switch(authinfo->format) { 3365 case DVD_SEND_CHALLENGE: { 3366 struct scsi_report_key_data_challenge *challenge_data; 3367 3368 length = sizeof(*challenge_data); 3369 3370 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3371 3372 databuf = (u_int8_t *)challenge_data; 3373 3374 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3375 challenge_data->data_len); 3376 3377 bcopy(authinfo->keychal, challenge_data->challenge_key, 3378 min(sizeof(authinfo->keychal), 3379 sizeof(challenge_data->challenge_key))); 3380 break; 3381 } 3382 case DVD_SEND_KEY2: { 3383 struct scsi_report_key_data_key1_key2 *key2_data; 3384 3385 length = sizeof(*key2_data); 3386 3387 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3388 3389 databuf = (u_int8_t *)key2_data; 3390 3391 scsi_ulto2b(length - sizeof(key2_data->data_len), 3392 key2_data->data_len); 3393 3394 bcopy(authinfo->keychal, key2_data->key1, 3395 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 3396 3397 break; 3398 } 3399 case DVD_SEND_RPC: { 3400 struct scsi_send_key_data_rpc *rpc_data; 3401 3402 length = sizeof(*rpc_data); 3403 3404 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3405 3406 databuf = (u_int8_t *)rpc_data; 3407 3408 scsi_ulto2b(length - sizeof(rpc_data->data_len), 3409 rpc_data->data_len); 3410 3411 rpc_data->region_code = authinfo->region; 3412 break; 3413 } 3414 default: 3415 return (EINVAL); 3416 } 3417 3418 cam_periph_lock(periph); 3419 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3420 3421 scsi_send_key(&ccb->csio, 3422 /* retries */ cd_retry_count, 3423 /* cbfcnp */ cddone, 3424 /* tag_action */ MSG_SIMPLE_Q_TAG, 3425 /* agid */ authinfo->agid, 3426 /* key_format */ authinfo->format, 3427 /* data_ptr */ databuf, 3428 /* dxfer_len */ length, 3429 /* sense_len */ SSD_FULL_SIZE, 3430 /* timeout */ 50000); 3431 3432 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3433 /*sense_flags*/SF_RETRY_UA); 3434 3435 xpt_release_ccb(ccb); 3436 cam_periph_unlock(periph); 3437 3438 if (databuf != NULL) 3439 free(databuf, M_DEVBUF); 3440 3441 return(error); 3442 } 3443 3444 static int 3445 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 3446 { 3447 union ccb *ccb; 3448 u_int8_t *databuf; 3449 u_int32_t address; 3450 int error; 3451 int length; 3452 3453 error = 0; 3454 databuf = NULL; 3455 /* The address is reserved for many of the formats */ 3456 address = 0; 3457 3458 switch(dvdstruct->format) { 3459 case DVD_STRUCT_PHYSICAL: 3460 length = sizeof(struct scsi_read_dvd_struct_data_physical); 3461 break; 3462 case DVD_STRUCT_COPYRIGHT: 3463 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 3464 break; 3465 case DVD_STRUCT_DISCKEY: 3466 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 3467 break; 3468 case DVD_STRUCT_BCA: 3469 length = sizeof(struct scsi_read_dvd_struct_data_bca); 3470 break; 3471 case DVD_STRUCT_MANUFACT: 3472 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 3473 break; 3474 case DVD_STRUCT_CMI: 3475 return (ENODEV); 3476 case DVD_STRUCT_PROTDISCID: 3477 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 3478 break; 3479 case DVD_STRUCT_DISCKEYBLOCK: 3480 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 3481 break; 3482 case DVD_STRUCT_DDS: 3483 length = sizeof(struct scsi_read_dvd_struct_data_dds); 3484 break; 3485 case DVD_STRUCT_MEDIUM_STAT: 3486 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 3487 break; 3488 case DVD_STRUCT_SPARE_AREA: 3489 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 3490 break; 3491 case DVD_STRUCT_RMD_LAST: 3492 return (ENODEV); 3493 case DVD_STRUCT_RMD_RMA: 3494 return (ENODEV); 3495 case DVD_STRUCT_PRERECORDED: 3496 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 3497 break; 3498 case DVD_STRUCT_UNIQUEID: 3499 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 3500 break; 3501 case DVD_STRUCT_DCB: 3502 return (ENODEV); 3503 case DVD_STRUCT_LIST: 3504 /* 3505 * This is the maximum allocation length for the READ DVD 3506 * STRUCTURE command. There's nothing in the MMC3 spec 3507 * that indicates a limit in the amount of data that can 3508 * be returned from this call, other than the limits 3509 * imposed by the 2-byte length variables. 3510 */ 3511 length = 65535; 3512 break; 3513 default: 3514 return (EINVAL); 3515 } 3516 3517 if (length != 0) { 3518 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3519 } else 3520 databuf = NULL; 3521 3522 cam_periph_lock(periph); 3523 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3524 3525 scsi_read_dvd_structure(&ccb->csio, 3526 /* retries */ cd_retry_count, 3527 /* cbfcnp */ cddone, 3528 /* tag_action */ MSG_SIMPLE_Q_TAG, 3529 /* lba */ address, 3530 /* layer_number */ dvdstruct->layer_num, 3531 /* key_format */ dvdstruct->format, 3532 /* agid */ dvdstruct->agid, 3533 /* data_ptr */ databuf, 3534 /* dxfer_len */ length, 3535 /* sense_len */ SSD_FULL_SIZE, 3536 /* timeout */ 50000); 3537 3538 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3539 /*sense_flags*/SF_RETRY_UA); 3540 3541 if (error != 0) 3542 goto bailout; 3543 3544 switch(dvdstruct->format) { 3545 case DVD_STRUCT_PHYSICAL: { 3546 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 3547 struct dvd_layer *outlayer; 3548 struct scsi_read_dvd_struct_data_physical *phys_data; 3549 3550 phys_data = 3551 (struct scsi_read_dvd_struct_data_physical *)databuf; 3552 inlayer = &phys_data->layer_desc; 3553 outlayer = (struct dvd_layer *)&dvdstruct->data; 3554 3555 dvdstruct->length = sizeof(*inlayer); 3556 3557 outlayer->book_type = (inlayer->book_type_version & 3558 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 3559 outlayer->book_version = (inlayer->book_type_version & 3560 RDSD_BOOK_VERSION_MASK); 3561 outlayer->disc_size = (inlayer->disc_size_max_rate & 3562 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 3563 outlayer->max_rate = (inlayer->disc_size_max_rate & 3564 RDSD_MAX_RATE_MASK); 3565 outlayer->nlayers = (inlayer->layer_info & 3566 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 3567 outlayer->track_path = (inlayer->layer_info & 3568 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 3569 outlayer->layer_type = (inlayer->layer_info & 3570 RDSD_LAYER_TYPE_MASK); 3571 outlayer->linear_density = (inlayer->density & 3572 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 3573 outlayer->track_density = (inlayer->density & 3574 RDSD_TRACK_DENSITY_MASK); 3575 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 3576 RDSD_BCA_SHIFT; 3577 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 3578 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 3579 outlayer->end_sector_l0 = 3580 scsi_3btoul(inlayer->end_sector_layer0); 3581 break; 3582 } 3583 case DVD_STRUCT_COPYRIGHT: { 3584 struct scsi_read_dvd_struct_data_copyright *copy_data; 3585 3586 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 3587 databuf; 3588 3589 dvdstruct->cpst = copy_data->cps_type; 3590 dvdstruct->rmi = copy_data->region_info; 3591 dvdstruct->length = 0; 3592 3593 break; 3594 } 3595 default: 3596 /* 3597 * Tell the user what the overall length is, no matter 3598 * what we can actually fit in the data buffer. 3599 */ 3600 dvdstruct->length = length - ccb->csio.resid - 3601 sizeof(struct scsi_read_dvd_struct_data_header); 3602 3603 /* 3604 * But only actually copy out the smaller of what we read 3605 * in or what the structure can take. 3606 */ 3607 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 3608 dvdstruct->data, 3609 min(sizeof(dvdstruct->data), dvdstruct->length)); 3610 break; 3611 } 3612 3613 bailout: 3614 xpt_release_ccb(ccb); 3615 cam_periph_unlock(periph); 3616 3617 if (databuf != NULL) 3618 free(databuf, M_DEVBUF); 3619 3620 return(error); 3621 } 3622 3623 void 3624 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 3625 void (*cbfcnp)(struct cam_periph *, union ccb *), 3626 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 3627 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 3628 u_int8_t sense_len, u_int32_t timeout) 3629 { 3630 struct scsi_report_key *scsi_cmd; 3631 3632 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 3633 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3634 scsi_cmd->opcode = REPORT_KEY; 3635 scsi_ulto4b(lba, scsi_cmd->lba); 3636 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 3637 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3638 (key_format & RK_KF_KEYFORMAT_MASK); 3639 3640 cam_fill_csio(csio, 3641 retries, 3642 cbfcnp, 3643 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 3644 tag_action, 3645 /*data_ptr*/ data_ptr, 3646 /*dxfer_len*/ dxfer_len, 3647 sense_len, 3648 sizeof(*scsi_cmd), 3649 timeout); 3650 } 3651 3652 void 3653 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 3654 void (*cbfcnp)(struct cam_periph *, union ccb *), 3655 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 3656 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 3657 u_int32_t timeout) 3658 { 3659 struct scsi_send_key *scsi_cmd; 3660 3661 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 3662 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3663 scsi_cmd->opcode = SEND_KEY; 3664 3665 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 3666 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3667 (key_format & RK_KF_KEYFORMAT_MASK); 3668 3669 cam_fill_csio(csio, 3670 retries, 3671 cbfcnp, 3672 /*flags*/ CAM_DIR_OUT, 3673 tag_action, 3674 /*data_ptr*/ data_ptr, 3675 /*dxfer_len*/ dxfer_len, 3676 sense_len, 3677 sizeof(*scsi_cmd), 3678 timeout); 3679 } 3680 3681 3682 void 3683 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 3684 void (*cbfcnp)(struct cam_periph *, union ccb *), 3685 u_int8_t tag_action, u_int32_t address, 3686 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 3687 u_int8_t *data_ptr, u_int32_t dxfer_len, 3688 u_int8_t sense_len, u_int32_t timeout) 3689 { 3690 struct scsi_read_dvd_structure *scsi_cmd; 3691 3692 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 3693 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3694 scsi_cmd->opcode = READ_DVD_STRUCTURE; 3695 3696 scsi_ulto4b(address, scsi_cmd->address); 3697 scsi_cmd->layer_number = layer_number; 3698 scsi_cmd->format = format; 3699 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 3700 /* The AGID is the top two bits of this byte */ 3701 scsi_cmd->agid = agid << 6; 3702 3703 cam_fill_csio(csio, 3704 retries, 3705 cbfcnp, 3706 /*flags*/ CAM_DIR_IN, 3707 tag_action, 3708 /*data_ptr*/ data_ptr, 3709 /*dxfer_len*/ dxfer_len, 3710 sense_len, 3711 sizeof(*scsi_cmd), 3712 timeout); 3713 } 3714