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