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