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