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