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