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