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