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