scsi_cd.c revision 79575
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: head/sys/cam/scsi/scsi_cd.c 79575 2001-07-11 05:16:27Z dd $ 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 76struct cd_params { 77 u_int32_t blksize; 78 u_long disksize; 79}; 80 81typedef 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 89typedef 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 101typedef 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 109typedef 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 119typedef enum { 120 CD_STATE_PROBE, 121 CD_STATE_NORMAL 122} cd_state; 123 124struct 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 141struct 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 */ 154static 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 180static d_open_t cdopen; 181static d_close_t cdclose; 182static d_ioctl_t cdioctl; 183static d_strategy_t cdstrategy; 184 185static periph_init_t cdinit; 186static periph_ctor_t cdregister; 187static periph_dtor_t cdcleanup; 188static periph_start_t cdstart; 189static periph_oninv_t cdoninvalidate; 190static void cdasync(void *callback_arg, u_int32_t code, 191 struct cam_path *path, void *arg); 192static void cdshorttimeout(void *arg); 193static void cdschedule(struct cam_periph *periph, int priority); 194static void cdrunchangerqueue(void *arg); 195static void cdchangerschedule(struct cd_softc *softc); 196static 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); 201static union ccb *cdgetccb(struct cam_periph *periph, 202 u_int32_t priority); 203static void cddone(struct cam_periph *periph, 204 union ccb *start_ccb); 205static int cderror(union ccb *ccb, u_int32_t cam_flags, 206 u_int32_t sense_flags); 207static void cdprevent(struct cam_periph *periph, int action); 208static int cdsize(dev_t dev, u_int32_t *size); 209static int cdfirsttrackisdata(struct cam_periph *periph); 210static 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); 213static int cdgetmode(struct cam_periph *periph, 214 struct cd_mode_data *data, u_int32_t page); 215static int cdsetmode(struct cam_periph *periph, 216 struct cd_mode_data *data); 217static int cdplay(struct cam_periph *periph, u_int32_t blk, 218 u_int32_t len); 219static 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); 224static 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); 228static int cdplaytracks(struct cam_periph *periph, 229 u_int32_t strack, u_int32_t sindex, 230 u_int32_t etrack, u_int32_t eindex); 231static int cdpause(struct cam_periph *periph, u_int32_t go); 232static int cdstopunit(struct cam_periph *periph, u_int32_t eject); 233static int cdstartunit(struct cam_periph *periph); 234static int cdreportkey(struct cam_periph *periph, 235 struct dvd_authinfo *authinfo); 236static int cdsendkey(struct cam_periph *periph, 237 struct dvd_authinfo *authinfo); 238static int cdreaddvdstructure(struct cam_periph *periph, 239 struct dvd_struct *dvdstruct); 240 241static struct periph_driver cddriver = 242{ 243 cdinit, "cd", 244 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 245}; 246 247PERIPHDRIVER_DECLARE(cd, cddriver); 248 249/* For 2.2-stable support */ 250#ifndef D_DISK 251#define D_DISK 0 252#endif 253static 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}; 268static struct cdevsw cddisk_cdevsw; 269 270static struct extend_array *cdperiphs; 271static 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 280static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 281static 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 */ 287SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem"); 288SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 289SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 290SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 291 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 292SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 293 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 294 295struct 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 309static STAILQ_HEAD(changerlist, cdchanger) changerq; 310 311void 312cdinit(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 352static void 353cdoninvalidate(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 407static void 408cdcleanup(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 497static void 498cdasync(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 561static cam_status 562cdregister(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 861cdregisterexit: 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 875static int 876cdopen(dev_t dev, int flags, int fmt, struct proc *p) 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 987static int 988cdclose(dev_t dev, int flag, int fmt, struct proc *p) 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 1026static void 1027cdshorttimeout(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 */ 1055static void 1056cdschedule(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 1105static void 1106cdrunchangerqueue(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 1214static void 1215cdchangerschedule(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 1293static int 1294cdrunccb(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 1315static union ccb * 1316cdgetccb(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 */ 1362static void 1363cdstrategy(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 1417static void 1418cdstart(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 1526static void 1527cddone(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 (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1719 /* 1720 * We only print out an error for 1721 * CDROM type devices. For WORM 1722 * devices, we don't print out an 1723 * error since a few WORM devices 1724 * don't support CDROM commands. 1725 * If we have sense information, go 1726 * ahead and print it out. 1727 * Otherwise, just say that we 1728 * couldn't attach. 1729 */ 1730 1731 /* 1732 * Just print out the error, not 1733 * the full probe message, when we 1734 * don't attach. 1735 */ 1736 if (have_sense) 1737 scsi_sense_print( 1738 &done_ccb->csio); 1739 else { 1740 xpt_print_path(periph->path); 1741 printf("got CAM status %#x\n", 1742 done_ccb->ccb_h.status); 1743 } 1744 xpt_print_path(periph->path); 1745 printf("fatal error, failed" 1746 " to attach to device\n"); 1747 1748 /* 1749 * Invalidate this peripheral. 1750 */ 1751 cam_periph_invalidate(periph); 1752 1753 announce_buf[0] = '\0'; 1754 } else { 1755 1756 /* 1757 * Invalidate this peripheral. 1758 */ 1759 cam_periph_invalidate(periph); 1760 announce_buf[0] = '\0'; 1761 } 1762 } 1763 } 1764 free(rdcap, M_TEMP); 1765 if (announce_buf[0] != '\0') { 1766 xpt_announce_periph(periph, announce_buf); 1767 if (softc->flags & CD_FLAG_CHANGER) 1768 cdchangerschedule(softc); 1769 } 1770 softc->state = CD_STATE_NORMAL; 1771 /* 1772 * Since our peripheral may be invalidated by an error 1773 * above or an external event, we must release our CCB 1774 * before releasing the probe lock on the peripheral. 1775 * The peripheral will only go away once the last lock 1776 * is removed, and we need it around for the CCB release 1777 * operation. 1778 */ 1779 xpt_release_ccb(done_ccb); 1780 cam_periph_unlock(periph); 1781 return; 1782 } 1783 case CD_CCB_WAITING: 1784 { 1785 /* Caller will release the CCB */ 1786 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1787 ("trying to wakeup ccbwait\n")); 1788 1789 wakeup(&done_ccb->ccb_h.cbfcnp); 1790 return; 1791 } 1792 default: 1793 break; 1794 } 1795 xpt_release_ccb(done_ccb); 1796} 1797 1798static int 1799cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1800{ 1801 1802 struct cam_periph *periph; 1803 struct cd_softc *softc; 1804 int error, unit; 1805 1806 unit = dkunit(dev); 1807 1808 periph = cam_extend_get(cdperiphs, unit); 1809 if (periph == NULL) 1810 return(ENXIO); 1811 1812 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 1813 1814 softc = (struct cd_softc *)periph->softc; 1815 1816 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1817 ("trying to do ioctl %#lx\n", cmd)); 1818 1819 error = cam_periph_lock(periph, PRIBIO | PCATCH); 1820 1821 if (error != 0) 1822 return(error); 1823 1824 switch (cmd) { 1825 1826 case CDIOCPLAYTRACKS: 1827 { 1828 struct ioc_play_track *args 1829 = (struct ioc_play_track *) addr; 1830 struct cd_mode_data *data; 1831 1832 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1833 M_WAITOK); 1834 1835 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1836 ("trying to do CDIOCPLAYTRACKS\n")); 1837 1838 error = cdgetmode(periph, data, AUDIO_PAGE); 1839 if (error) { 1840 free(data, M_TEMP); 1841 break; 1842 } 1843 data->page.audio.flags &= ~CD_PA_SOTC; 1844 data->page.audio.flags |= CD_PA_IMMED; 1845 error = cdsetmode(periph, data); 1846 free(data, M_TEMP); 1847 if (error) 1848 break; 1849 if (softc->quirks & CD_Q_BCD_TRACKS) { 1850 args->start_track = bin2bcd(args->start_track); 1851 args->end_track = bin2bcd(args->end_track); 1852 } 1853 error = cdplaytracks(periph, 1854 args->start_track, 1855 args->start_index, 1856 args->end_track, 1857 args->end_index); 1858 } 1859 break; 1860 case CDIOCPLAYMSF: 1861 { 1862 struct ioc_play_msf *args 1863 = (struct ioc_play_msf *) addr; 1864 struct cd_mode_data *data; 1865 1866 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1867 M_WAITOK); 1868 1869 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1870 ("trying to do CDIOCPLAYMSF\n")); 1871 1872 error = cdgetmode(periph, data, AUDIO_PAGE); 1873 if (error) { 1874 free(data, M_TEMP); 1875 break; 1876 } 1877 data->page.audio.flags &= ~CD_PA_SOTC; 1878 data->page.audio.flags |= CD_PA_IMMED; 1879 error = cdsetmode(periph, data); 1880 free(data, M_TEMP); 1881 if (error) 1882 break; 1883 error = cdplaymsf(periph, 1884 args->start_m, 1885 args->start_s, 1886 args->start_f, 1887 args->end_m, 1888 args->end_s, 1889 args->end_f); 1890 } 1891 break; 1892 case CDIOCPLAYBLOCKS: 1893 { 1894 struct ioc_play_blocks *args 1895 = (struct ioc_play_blocks *) addr; 1896 struct cd_mode_data *data; 1897 1898 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1899 ("trying to do CDIOCPLAYBLOCKS\n")); 1900 1901 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1902 M_WAITOK); 1903 1904 error = cdgetmode(periph, data, AUDIO_PAGE); 1905 if (error) { 1906 free(data, M_TEMP); 1907 break; 1908 } 1909 data->page.audio.flags &= ~CD_PA_SOTC; 1910 data->page.audio.flags |= CD_PA_IMMED; 1911 error = cdsetmode(periph, data); 1912 free(data, M_TEMP); 1913 if (error) 1914 break; 1915 error = cdplay(periph, args->blk, args->len); 1916 } 1917 break; 1918 case CDIOCREADSUBCHANNEL: 1919 { 1920 struct ioc_read_subchannel *args 1921 = (struct ioc_read_subchannel *) addr; 1922 struct cd_sub_channel_info *data; 1923 u_int32_t len = args->data_len; 1924 1925 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1926 ("trying to do CDIOCREADSUBCHANNEL\n")); 1927 1928 data = malloc(sizeof(struct cd_sub_channel_info), 1929 M_TEMP, M_WAITOK); 1930 1931 if ((len > sizeof(struct cd_sub_channel_info)) || 1932 (len < sizeof(struct cd_sub_channel_header))) { 1933 printf( 1934 "scsi_cd: cdioctl: " 1935 "cdioreadsubchannel: error, len=%d\n", 1936 len); 1937 error = EINVAL; 1938 free(data, M_TEMP); 1939 break; 1940 } 1941 1942 if (softc->quirks & CD_Q_BCD_TRACKS) 1943 args->track = bin2bcd(args->track); 1944 1945 error = cdreadsubchannel(periph, args->address_format, 1946 args->data_format, args->track, data, len); 1947 1948 if (error) { 1949 free(data, M_TEMP); 1950 break; 1951 } 1952 if (softc->quirks & CD_Q_BCD_TRACKS) 1953 data->what.track_info.track_number = 1954 bcd2bin(data->what.track_info.track_number); 1955 len = min(len, ((data->header.data_len[0] << 8) + 1956 data->header.data_len[1] + 1957 sizeof(struct cd_sub_channel_header))); 1958 if (copyout(data, args->data, len) != 0) { 1959 error = EFAULT; 1960 } 1961 free(data, M_TEMP); 1962 } 1963 break; 1964 1965 case CDIOREADTOCHEADER: 1966 { 1967 struct ioc_toc_header *th; 1968 1969 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1970 ("trying to do CDIOREADTOCHEADER\n")); 1971 1972 th = malloc(sizeof(struct ioc_toc_header), M_TEMP, 1973 M_WAITOK); 1974 error = cdreadtoc(periph, 0, 0, 1975 (struct cd_toc_entry *)th, 1976 sizeof (*th)); 1977 if (error) { 1978 free(th, M_TEMP); 1979 break; 1980 } 1981 if (softc->quirks & CD_Q_BCD_TRACKS) { 1982 /* we are going to have to convert the BCD 1983 * encoding on the cd to what is expected 1984 */ 1985 th->starting_track = 1986 bcd2bin(th->starting_track); 1987 th->ending_track = bcd2bin(th->ending_track); 1988 } 1989 NTOHS(th->len); 1990 bcopy(th, addr, sizeof(*th)); 1991 free(th, M_TEMP); 1992 } 1993 break; 1994 case CDIOREADTOCENTRYS: 1995 { 1996 typedef struct { 1997 struct ioc_toc_header header; 1998 struct cd_toc_entry entries[100]; 1999 } data_t; 2000 typedef struct { 2001 struct ioc_toc_header header; 2002 struct cd_toc_entry entry; 2003 } lead_t; 2004 2005 data_t *data; 2006 lead_t *lead; 2007 struct ioc_read_toc_entry *te = 2008 (struct ioc_read_toc_entry *) addr; 2009 struct ioc_toc_header *th; 2010 u_int32_t len, readlen, idx, num; 2011 u_int32_t starting_track = te->starting_track; 2012 2013 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2014 ("trying to do CDIOREADTOCENTRYS\n")); 2015 2016 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK); 2017 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK); 2018 2019 if (te->data_len < sizeof(struct cd_toc_entry) 2020 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2021 || (te->address_format != CD_MSF_FORMAT 2022 && te->address_format != CD_LBA_FORMAT)) { 2023 error = EINVAL; 2024 printf("scsi_cd: error in readtocentries, " 2025 "returning EINVAL\n"); 2026 free(data, M_TEMP); 2027 free(lead, M_TEMP); 2028 break; 2029 } 2030 2031 th = &data->header; 2032 error = cdreadtoc(periph, 0, 0, 2033 (struct cd_toc_entry *)th, 2034 sizeof (*th)); 2035 if (error) { 2036 free(data, M_TEMP); 2037 free(lead, M_TEMP); 2038 break; 2039 } 2040 2041 if (softc->quirks & CD_Q_BCD_TRACKS) { 2042 /* we are going to have to convert the BCD 2043 * encoding on the cd to what is expected 2044 */ 2045 th->starting_track = 2046 bcd2bin(th->starting_track); 2047 th->ending_track = bcd2bin(th->ending_track); 2048 } 2049 2050 if (starting_track == 0) 2051 starting_track = th->starting_track; 2052 else if (starting_track == LEADOUT) 2053 starting_track = th->ending_track + 1; 2054 else if (starting_track < th->starting_track || 2055 starting_track > th->ending_track + 1) { 2056 printf("scsi_cd: error in readtocentries, " 2057 "returning EINVAL\n"); 2058 free(data, M_TEMP); 2059 free(lead, M_TEMP); 2060 error = EINVAL; 2061 break; 2062 } 2063 2064 /* calculate reading length without leadout entry */ 2065 readlen = (th->ending_track - starting_track + 1) * 2066 sizeof(struct cd_toc_entry); 2067 2068 /* and with leadout entry */ 2069 len = readlen + sizeof(struct cd_toc_entry); 2070 if (te->data_len < len) { 2071 len = te->data_len; 2072 if (readlen > len) 2073 readlen = len; 2074 } 2075 if (len > sizeof(data->entries)) { 2076 printf("scsi_cd: error in readtocentries, " 2077 "returning EINVAL\n"); 2078 error = EINVAL; 2079 free(data, M_TEMP); 2080 free(lead, M_TEMP); 2081 break; 2082 } 2083 num = len / sizeof(struct cd_toc_entry); 2084 2085 if (readlen > 0) { 2086 error = cdreadtoc(periph, te->address_format, 2087 starting_track, 2088 (struct cd_toc_entry *)data, 2089 readlen + sizeof (*th)); 2090 if (error) { 2091 free(data, M_TEMP); 2092 free(lead, M_TEMP); 2093 break; 2094 } 2095 } 2096 2097 /* make leadout entry if needed */ 2098 idx = starting_track + num - 1; 2099 if (softc->quirks & CD_Q_BCD_TRACKS) 2100 th->ending_track = bcd2bin(th->ending_track); 2101 if (idx == th->ending_track + 1) { 2102 error = cdreadtoc(periph, te->address_format, 2103 LEADOUT, 2104 (struct cd_toc_entry *)lead, 2105 sizeof(*lead)); 2106 if (error) { 2107 free(data, M_TEMP); 2108 free(lead, M_TEMP); 2109 break; 2110 } 2111 data->entries[idx - starting_track] = 2112 lead->entry; 2113 } 2114 if (softc->quirks & CD_Q_BCD_TRACKS) { 2115 for (idx = 0; idx < num - 1; idx++) { 2116 data->entries[idx].track = 2117 bcd2bin(data->entries[idx].track); 2118 } 2119 } 2120 2121 error = copyout(data->entries, te->data, len); 2122 free(data, M_TEMP); 2123 free(lead, M_TEMP); 2124 } 2125 break; 2126 case CDIOREADTOCENTRY: 2127 { 2128 /* yeah yeah, this is ugly */ 2129 typedef struct { 2130 struct ioc_toc_header header; 2131 struct cd_toc_entry entry; 2132 } data_t; 2133 2134 data_t *data; 2135 struct ioc_read_toc_single_entry *te = 2136 (struct ioc_read_toc_single_entry *) addr; 2137 struct ioc_toc_header *th; 2138 u_int32_t track; 2139 2140 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2141 ("trying to do CDIOREADTOCENTRY\n")); 2142 2143 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK); 2144 2145 if (te->address_format != CD_MSF_FORMAT 2146 && te->address_format != CD_LBA_FORMAT) { 2147 printf("error in readtocentry, " 2148 " returning EINVAL\n"); 2149 free(data, M_TEMP); 2150 error = EINVAL; 2151 break; 2152 } 2153 2154 th = &data->header; 2155 error = cdreadtoc(periph, 0, 0, 2156 (struct cd_toc_entry *)th, 2157 sizeof (*th)); 2158 if (error) { 2159 free(data, M_TEMP); 2160 break; 2161 } 2162 2163 if (softc->quirks & CD_Q_BCD_TRACKS) { 2164 /* we are going to have to convert the BCD 2165 * encoding on the cd to what is expected 2166 */ 2167 th->starting_track = 2168 bcd2bin(th->starting_track); 2169 th->ending_track = bcd2bin(th->ending_track); 2170 } 2171 track = te->track; 2172 if (track == 0) 2173 track = th->starting_track; 2174 else if (track == LEADOUT) 2175 /* OK */; 2176 else if (track < th->starting_track || 2177 track > th->ending_track + 1) { 2178 printf("error in readtocentry, " 2179 " returning EINVAL\n"); 2180 free(data, M_TEMP); 2181 error = EINVAL; 2182 break; 2183 } 2184 2185 error = cdreadtoc(periph, te->address_format, track, 2186 (struct cd_toc_entry *)data, 2187 sizeof(data_t)); 2188 if (error) { 2189 free(data, M_TEMP); 2190 break; 2191 } 2192 2193 if (softc->quirks & CD_Q_BCD_TRACKS) 2194 data->entry.track = bcd2bin(data->entry.track); 2195 bcopy(&data->entry, &te->entry, 2196 sizeof(struct cd_toc_entry)); 2197 free(data, M_TEMP); 2198 } 2199 break; 2200 case CDIOCSETPATCH: 2201 { 2202 struct ioc_patch *arg = (struct ioc_patch *) addr; 2203 struct cd_mode_data *data; 2204 2205 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2206 ("trying to do CDIOCSETPATCH\n")); 2207 2208 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2209 M_WAITOK); 2210 error = cdgetmode(periph, data, AUDIO_PAGE); 2211 if (error) { 2212 free(data, M_TEMP); 2213 break; 2214 } 2215 data->page.audio.port[LEFT_PORT].channels = 2216 arg->patch[0]; 2217 data->page.audio.port[RIGHT_PORT].channels = 2218 arg->patch[1]; 2219 data->page.audio.port[2].channels = arg->patch[2]; 2220 data->page.audio.port[3].channels = arg->patch[3]; 2221 error = cdsetmode(periph, data); 2222 free(data, M_TEMP); 2223 } 2224 break; 2225 case CDIOCGETVOL: 2226 { 2227 struct ioc_vol *arg = (struct ioc_vol *) addr; 2228 struct cd_mode_data *data; 2229 2230 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2231 ("trying to do CDIOCGETVOL\n")); 2232 2233 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2234 M_WAITOK); 2235 error = cdgetmode(periph, data, AUDIO_PAGE); 2236 if (error) { 2237 free(data, M_TEMP); 2238 break; 2239 } 2240 arg->vol[LEFT_PORT] = 2241 data->page.audio.port[LEFT_PORT].volume; 2242 arg->vol[RIGHT_PORT] = 2243 data->page.audio.port[RIGHT_PORT].volume; 2244 arg->vol[2] = data->page.audio.port[2].volume; 2245 arg->vol[3] = data->page.audio.port[3].volume; 2246 free(data, M_TEMP); 2247 } 2248 break; 2249 case CDIOCSETVOL: 2250 { 2251 struct ioc_vol *arg = (struct ioc_vol *) addr; 2252 struct cd_mode_data *data; 2253 2254 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2255 ("trying to do CDIOCSETVOL\n")); 2256 2257 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2258 M_WAITOK); 2259 error = cdgetmode(periph, data, AUDIO_PAGE); 2260 if (error) { 2261 free(data, M_TEMP); 2262 break; 2263 } 2264 data->page.audio.port[LEFT_PORT].channels = CHANNEL_0; 2265 data->page.audio.port[LEFT_PORT].volume = 2266 arg->vol[LEFT_PORT]; 2267 data->page.audio.port[RIGHT_PORT].channels = CHANNEL_1; 2268 data->page.audio.port[RIGHT_PORT].volume = 2269 arg->vol[RIGHT_PORT]; 2270 data->page.audio.port[2].volume = arg->vol[2]; 2271 data->page.audio.port[3].volume = arg->vol[3]; 2272 error = cdsetmode(periph, data); 2273 free(data, M_TEMP); 2274 } 2275 break; 2276 case CDIOCSETMONO: 2277 { 2278 struct cd_mode_data *data; 2279 2280 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2281 ("trying to do CDIOCSETMONO\n")); 2282 2283 data = malloc(sizeof(struct cd_mode_data), 2284 M_TEMP, M_WAITOK); 2285 error = cdgetmode(periph, data, AUDIO_PAGE); 2286 if (error) { 2287 free(data, M_TEMP); 2288 break; 2289 } 2290 data->page.audio.port[LEFT_PORT].channels = 2291 LEFT_CHANNEL | RIGHT_CHANNEL; 2292 data->page.audio.port[RIGHT_PORT].channels = 2293 LEFT_CHANNEL | RIGHT_CHANNEL; 2294 data->page.audio.port[2].channels = 0; 2295 data->page.audio.port[3].channels = 0; 2296 error = cdsetmode(periph, data); 2297 free(data, M_TEMP); 2298 } 2299 break; 2300 case CDIOCSETSTEREO: 2301 { 2302 struct cd_mode_data *data; 2303 2304 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2305 ("trying to do CDIOCSETSTEREO\n")); 2306 2307 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2308 M_WAITOK); 2309 error = cdgetmode(periph, data, AUDIO_PAGE); 2310 if (error) { 2311 free(data, M_TEMP); 2312 break; 2313 } 2314 data->page.audio.port[LEFT_PORT].channels = 2315 LEFT_CHANNEL; 2316 data->page.audio.port[RIGHT_PORT].channels = 2317 RIGHT_CHANNEL; 2318 data->page.audio.port[2].channels = 0; 2319 data->page.audio.port[3].channels = 0; 2320 error = cdsetmode(periph, data); 2321 free(data, M_TEMP); 2322 } 2323 break; 2324 case CDIOCSETMUTE: 2325 { 2326 struct cd_mode_data *data; 2327 2328 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2329 ("trying to do CDIOCSETMUTE\n")); 2330 2331 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2332 M_WAITOK); 2333 error = cdgetmode(periph, data, AUDIO_PAGE); 2334 if (error) { 2335 free(data, M_TEMP); 2336 break; 2337 } 2338 data->page.audio.port[LEFT_PORT].channels = 0; 2339 data->page.audio.port[RIGHT_PORT].channels = 0; 2340 data->page.audio.port[2].channels = 0; 2341 data->page.audio.port[3].channels = 0; 2342 error = cdsetmode(periph, data); 2343 free(data, M_TEMP); 2344 } 2345 break; 2346 case CDIOCSETLEFT: 2347 { 2348 struct cd_mode_data *data; 2349 2350 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2351 ("trying to do CDIOCSETLEFT\n")); 2352 2353 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2354 M_WAITOK); 2355 error = cdgetmode(periph, data, AUDIO_PAGE); 2356 if (error) { 2357 free(data, M_TEMP); 2358 break; 2359 } 2360 data->page.audio.port[LEFT_PORT].channels = 2361 LEFT_CHANNEL; 2362 data->page.audio.port[RIGHT_PORT].channels = 2363 LEFT_CHANNEL; 2364 data->page.audio.port[2].channels = 0; 2365 data->page.audio.port[3].channels = 0; 2366 error = cdsetmode(periph, data); 2367 free(data, M_TEMP); 2368 } 2369 break; 2370 case CDIOCSETRIGHT: 2371 { 2372 struct cd_mode_data *data; 2373 2374 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2375 ("trying to do CDIOCSETRIGHT\n")); 2376 2377 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2378 M_WAITOK); 2379 error = cdgetmode(periph, data, AUDIO_PAGE); 2380 if (error) { 2381 free(data, M_TEMP); 2382 break; 2383 } 2384 data->page.audio.port[LEFT_PORT].channels = 2385 RIGHT_CHANNEL; 2386 data->page.audio.port[RIGHT_PORT].channels = 2387 RIGHT_CHANNEL; 2388 data->page.audio.port[2].channels = 0; 2389 data->page.audio.port[3].channels = 0; 2390 error = cdsetmode(periph, data); 2391 free(data, M_TEMP); 2392 } 2393 break; 2394 case CDIOCRESUME: 2395 error = cdpause(periph, 1); 2396 break; 2397 case CDIOCPAUSE: 2398 error = cdpause(periph, 0); 2399 break; 2400 case CDIOCSTART: 2401 error = cdstartunit(periph); 2402 break; 2403 case CDIOCSTOP: 2404 error = cdstopunit(periph, 0); 2405 break; 2406 case CDIOCEJECT: 2407 error = cdstopunit(periph, 1); 2408 break; 2409 case CDIOCALLOW: 2410 cdprevent(periph, PR_ALLOW); 2411 break; 2412 case CDIOCPREVENT: 2413 cdprevent(periph, PR_PREVENT); 2414 break; 2415 case CDIOCSETDEBUG: 2416 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2417 error = ENOTTY; 2418 break; 2419 case CDIOCCLRDEBUG: 2420 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2421 error = ENOTTY; 2422 break; 2423 case CDIOCRESET: 2424 /* return (cd_reset(periph)); */ 2425 error = ENOTTY; 2426 break; 2427 case DVDIOCSENDKEY: 2428 case DVDIOCREPORTKEY: { 2429 struct dvd_authinfo *authinfo; 2430 2431 authinfo = (struct dvd_authinfo *)addr; 2432 2433 if (cmd == DVDIOCREPORTKEY) 2434 error = cdreportkey(periph, authinfo); 2435 else 2436 error = cdsendkey(periph, authinfo); 2437 break; 2438 } 2439 case DVDIOCREADSTRUCTURE: { 2440 struct dvd_struct *dvdstruct; 2441 2442 dvdstruct = (struct dvd_struct *)addr; 2443 2444 error = cdreaddvdstructure(periph, dvdstruct); 2445 2446 break; 2447 } 2448 default: 2449 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2450 break; 2451 } 2452 2453 cam_periph_unlock(periph); 2454 2455 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2456 2457 return (error); 2458} 2459 2460static void 2461cdprevent(struct cam_periph *periph, int action) 2462{ 2463 union ccb *ccb; 2464 struct cd_softc *softc; 2465 int error; 2466 2467 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2468 2469 softc = (struct cd_softc *)periph->softc; 2470 2471 if (((action == PR_ALLOW) 2472 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2473 || ((action == PR_PREVENT) 2474 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2475 return; 2476 } 2477 2478 ccb = cdgetccb(periph, /* priority */ 1); 2479 2480 scsi_prevent(&ccb->csio, 2481 /*retries*/ 1, 2482 cddone, 2483 MSG_SIMPLE_Q_TAG, 2484 action, 2485 SSD_FULL_SIZE, 2486 /* timeout */60000); 2487 2488 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2489 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2490 2491 xpt_release_ccb(ccb); 2492 2493 if (error == 0) { 2494 if (action == PR_ALLOW) 2495 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2496 else 2497 softc->flags |= CD_FLAG_DISC_LOCKED; 2498 } 2499} 2500 2501static int 2502cdsize(dev_t dev, u_int32_t *size) 2503{ 2504 struct cam_periph *periph; 2505 struct cd_softc *softc; 2506 union ccb *ccb; 2507 struct scsi_read_capacity_data *rcap_buf; 2508 int error; 2509 2510 periph = cam_extend_get(cdperiphs, dkunit(dev)); 2511 2512 if (periph == NULL) 2513 return (ENXIO); 2514 2515 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2516 2517 softc = (struct cd_softc *)periph->softc; 2518 2519 ccb = cdgetccb(periph, /* priority */ 1); 2520 2521 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2522 M_TEMP, M_WAITOK); 2523 2524 scsi_read_capacity(&ccb->csio, 2525 /*retries*/ 1, 2526 cddone, 2527 MSG_SIMPLE_Q_TAG, 2528 rcap_buf, 2529 SSD_FULL_SIZE, 2530 /* timeout */20000); 2531 2532 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2533 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2534 2535 xpt_release_ccb(ccb); 2536 2537 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 2538 softc->params.blksize = scsi_4btoul(rcap_buf->length); 2539 /* 2540 * SCSI-3 mandates that the reported blocksize shall be 2048. 2541 * Older drives sometimes report funny values, trim it down to 2542 * 2048, or other parts of the kernel will get confused. 2543 * 2544 * XXX we leave drives alone that might report 512 bytes, as 2545 * well as drives reporting more weird sizes like perhaps 4K. 2546 */ 2547 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 2548 softc->params.blksize = 2048; 2549 2550 free(rcap_buf, M_TEMP); 2551 *size = softc->params.disksize; 2552 2553 return (error); 2554 2555} 2556 2557/* 2558 * The idea here is to try to figure out whether the first track is data or 2559 * audio. If it is data, we can at least attempt to read a disklabel off 2560 * the first sector of the disk. If it is audio, there won't be a 2561 * disklabel. 2562 * 2563 * This routine returns 0 if the first track is data, and non-zero if there 2564 * is an error or the first track is audio. (If either non-zero case, we 2565 * should not attempt to read the disklabel.) 2566 */ 2567static int 2568cdfirsttrackisdata(struct cam_periph *periph) 2569{ 2570 struct cdtocdata { 2571 struct ioc_toc_header header; 2572 struct cd_toc_entry entries[100]; 2573 }; 2574 struct cd_softc *softc; 2575 struct ioc_toc_header *th; 2576 struct cdtocdata *data; 2577 int num_entries, i; 2578 int error, first_track_audio; 2579 2580 error = 0; 2581 first_track_audio = -1; 2582 2583 softc = (struct cd_softc *)periph->softc; 2584 2585 data = malloc(sizeof(struct cdtocdata), M_TEMP, M_WAITOK); 2586 2587 th = &data->header; 2588 error = cdreadtoc(periph, 0, 0, (struct cd_toc_entry *)data, 2589 sizeof(*data)); 2590 2591 if (error) 2592 goto bailout; 2593 2594 if (softc->quirks & CD_Q_BCD_TRACKS) { 2595 /* we are going to have to convert the BCD 2596 * encoding on the cd to what is expected 2597 */ 2598 th->starting_track = 2599 bcd2bin(th->starting_track); 2600 th->ending_track = bcd2bin(th->ending_track); 2601 } 2602 th->len = scsi_2btoul((u_int8_t *)&th->len); 2603 2604 if ((th->len - 2) > 0) 2605 num_entries = (th->len - 2) / sizeof(struct cd_toc_entry); 2606 else 2607 num_entries = 0; 2608 2609 for (i = 0; i < num_entries; i++) { 2610 if (data->entries[i].track == th->starting_track) { 2611 if (data->entries[i].control & 0x4) 2612 first_track_audio = 0; 2613 else 2614 first_track_audio = 1; 2615 break; 2616 } 2617 } 2618 2619 if (first_track_audio == -1) 2620 error = ENOENT; 2621 else if (first_track_audio == 1) 2622 error = EINVAL; 2623 else 2624 error = 0; 2625bailout: 2626 free(data, M_TEMP); 2627 2628 return(error); 2629} 2630 2631static int 2632cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2633{ 2634 struct cd_softc *softc; 2635 struct cam_periph *periph; 2636 2637 periph = xpt_path_periph(ccb->ccb_h.path); 2638 softc = (struct cd_softc *)periph->softc; 2639 2640 /* 2641 * XXX 2642 * Until we have a better way of doing pack validation, 2643 * don't treat UAs as errors. 2644 */ 2645 sense_flags |= SF_RETRY_UA; 2646 return (cam_periph_error(ccb, cam_flags, sense_flags, 2647 &softc->saved_ccb)); 2648} 2649 2650/* 2651 * Read table of contents 2652 */ 2653static int 2654cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 2655 struct cd_toc_entry *data, u_int32_t len) 2656{ 2657 struct scsi_read_toc *scsi_cmd; 2658 u_int32_t ntoc; 2659 struct ccb_scsiio *csio; 2660 union ccb *ccb; 2661 int error; 2662 2663 ntoc = len; 2664 error = 0; 2665 2666 ccb = cdgetccb(periph, /* priority */ 1); 2667 2668 csio = &ccb->csio; 2669 2670 cam_fill_csio(csio, 2671 /* retries */ 1, 2672 /* cbfcnp */ cddone, 2673 /* flags */ CAM_DIR_IN, 2674 /* tag_action */ MSG_SIMPLE_Q_TAG, 2675 /* data_ptr */ (u_int8_t *)data, 2676 /* dxfer_len */ len, 2677 /* sense_len */ SSD_FULL_SIZE, 2678 sizeof(struct scsi_read_toc), 2679 /* timeout */ 50000); 2680 2681 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 2682 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2683 2684 if (mode == CD_MSF_FORMAT) 2685 scsi_cmd->byte2 |= CD_MSF; 2686 scsi_cmd->from_track = start; 2687 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 2688 scsi_cmd->data_len[0] = (ntoc) >> 8; 2689 scsi_cmd->data_len[1] = (ntoc) & 0xff; 2690 2691 scsi_cmd->op_code = READ_TOC; 2692 2693 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2694 /*sense_flags*/SF_RETRY_UA); 2695 2696 xpt_release_ccb(ccb); 2697 2698 return(error); 2699} 2700 2701static int 2702cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 2703 u_int32_t format, int track, 2704 struct cd_sub_channel_info *data, u_int32_t len) 2705{ 2706 struct scsi_read_subchannel *scsi_cmd; 2707 struct ccb_scsiio *csio; 2708 union ccb *ccb; 2709 int error; 2710 2711 error = 0; 2712 2713 ccb = cdgetccb(periph, /* priority */ 1); 2714 2715 csio = &ccb->csio; 2716 2717 cam_fill_csio(csio, 2718 /* retries */ 1, 2719 /* cbfcnp */ cddone, 2720 /* flags */ CAM_DIR_IN, 2721 /* tag_action */ MSG_SIMPLE_Q_TAG, 2722 /* data_ptr */ (u_int8_t *)data, 2723 /* dxfer_len */ len, 2724 /* sense_len */ SSD_FULL_SIZE, 2725 sizeof(struct scsi_read_subchannel), 2726 /* timeout */ 50000); 2727 2728 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 2729 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2730 2731 scsi_cmd->op_code = READ_SUBCHANNEL; 2732 if (mode == CD_MSF_FORMAT) 2733 scsi_cmd->byte1 |= CD_MSF; 2734 scsi_cmd->byte2 = SRS_SUBQ; 2735 scsi_cmd->subchan_format = format; 2736 scsi_cmd->track = track; 2737 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 2738 scsi_cmd->control = 0; 2739 2740 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2741 /*sense_flags*/SF_RETRY_UA); 2742 2743 xpt_release_ccb(ccb); 2744 2745 return(error); 2746} 2747 2748 2749static int 2750cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page) 2751{ 2752 struct scsi_mode_sense_6 *scsi_cmd; 2753 struct ccb_scsiio *csio; 2754 union ccb *ccb; 2755 int error; 2756 2757 ccb = cdgetccb(periph, /* priority */ 1); 2758 2759 csio = &ccb->csio; 2760 2761 bzero(data, sizeof(*data)); 2762 cam_fill_csio(csio, 2763 /* retries */ 1, 2764 /* cbfcnp */ cddone, 2765 /* flags */ CAM_DIR_IN, 2766 /* tag_action */ MSG_SIMPLE_Q_TAG, 2767 /* data_ptr */ (u_int8_t *)data, 2768 /* dxfer_len */ sizeof(*data), 2769 /* sense_len */ SSD_FULL_SIZE, 2770 sizeof(struct scsi_mode_sense_6), 2771 /* timeout */ 50000); 2772 2773 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes; 2774 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2775 2776 scsi_cmd->page = page; 2777 scsi_cmd->length = sizeof(*data) & 0xff; 2778 scsi_cmd->opcode = MODE_SENSE; 2779 2780 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2781 /*sense_flags*/SF_RETRY_UA); 2782 2783 xpt_release_ccb(ccb); 2784 2785 return(error); 2786} 2787 2788static int 2789cdsetmode(struct cam_periph *periph, struct cd_mode_data *data) 2790{ 2791 struct scsi_mode_select_6 *scsi_cmd; 2792 struct ccb_scsiio *csio; 2793 union ccb *ccb; 2794 int error; 2795 2796 ccb = cdgetccb(periph, /* priority */ 1); 2797 2798 csio = &ccb->csio; 2799 2800 error = 0; 2801 2802 cam_fill_csio(csio, 2803 /* retries */ 1, 2804 /* cbfcnp */ cddone, 2805 /* flags */ CAM_DIR_OUT, 2806 /* tag_action */ MSG_SIMPLE_Q_TAG, 2807 /* data_ptr */ (u_int8_t *)data, 2808 /* dxfer_len */ sizeof(*data), 2809 /* sense_len */ SSD_FULL_SIZE, 2810 sizeof(struct scsi_mode_select_6), 2811 /* timeout */ 50000); 2812 2813 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes; 2814 2815 bzero(scsi_cmd, sizeof(*scsi_cmd)); 2816 scsi_cmd->opcode = MODE_SELECT; 2817 scsi_cmd->byte2 |= SMS_PF; 2818 scsi_cmd->length = sizeof(*data) & 0xff; 2819 data->header.data_length = 0; 2820 /* 2821 * SONY drives do not allow a mode select with a medium_type 2822 * value that has just been returned by a mode sense; use a 2823 * medium_type of 0 (Default) instead. 2824 */ 2825 data->header.medium_type = 0; 2826 2827 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2828 /*sense_flags*/SF_RETRY_UA); 2829 2830 xpt_release_ccb(ccb); 2831 2832 return(error); 2833} 2834 2835 2836static int 2837cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 2838{ 2839 struct ccb_scsiio *csio; 2840 union ccb *ccb; 2841 int error; 2842 u_int8_t cdb_len; 2843 2844 error = 0; 2845 ccb = cdgetccb(periph, /* priority */ 1); 2846 csio = &ccb->csio; 2847 /* 2848 * Use the smallest possible command to perform the operation. 2849 */ 2850 if ((len & 0xffff0000) == 0) { 2851 /* 2852 * We can fit in a 10 byte cdb. 2853 */ 2854 struct scsi_play_10 *scsi_cmd; 2855 2856 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 2857 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2858 scsi_cmd->op_code = PLAY_10; 2859 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2860 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 2861 cdb_len = sizeof(*scsi_cmd); 2862 } else { 2863 struct scsi_play_12 *scsi_cmd; 2864 2865 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 2866 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2867 scsi_cmd->op_code = PLAY_12; 2868 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2869 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 2870 cdb_len = sizeof(*scsi_cmd); 2871 } 2872 cam_fill_csio(csio, 2873 /*retries*/2, 2874 cddone, 2875 /*flags*/CAM_DIR_NONE, 2876 MSG_SIMPLE_Q_TAG, 2877 /*dataptr*/NULL, 2878 /*datalen*/0, 2879 /*sense_len*/SSD_FULL_SIZE, 2880 cdb_len, 2881 /*timeout*/50 * 1000); 2882 2883 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2884 /*sense_flags*/SF_RETRY_UA); 2885 2886 xpt_release_ccb(ccb); 2887 2888 return(error); 2889} 2890 2891static int 2892cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 2893 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 2894{ 2895 struct scsi_play_msf *scsi_cmd; 2896 struct ccb_scsiio *csio; 2897 union ccb *ccb; 2898 int error; 2899 2900 error = 0; 2901 2902 ccb = cdgetccb(periph, /* priority */ 1); 2903 2904 csio = &ccb->csio; 2905 2906 cam_fill_csio(csio, 2907 /* retries */ 1, 2908 /* cbfcnp */ cddone, 2909 /* flags */ CAM_DIR_NONE, 2910 /* tag_action */ MSG_SIMPLE_Q_TAG, 2911 /* data_ptr */ NULL, 2912 /* dxfer_len */ 0, 2913 /* sense_len */ SSD_FULL_SIZE, 2914 sizeof(struct scsi_play_msf), 2915 /* timeout */ 50000); 2916 2917 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 2918 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2919 2920 scsi_cmd->op_code = PLAY_MSF; 2921 scsi_cmd->start_m = startm; 2922 scsi_cmd->start_s = starts; 2923 scsi_cmd->start_f = startf; 2924 scsi_cmd->end_m = endm; 2925 scsi_cmd->end_s = ends; 2926 scsi_cmd->end_f = endf; 2927 2928 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2929 /*sense_flags*/SF_RETRY_UA); 2930 2931 xpt_release_ccb(ccb); 2932 2933 return(error); 2934} 2935 2936 2937static int 2938cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 2939 u_int32_t etrack, u_int32_t eindex) 2940{ 2941 struct scsi_play_track *scsi_cmd; 2942 struct ccb_scsiio *csio; 2943 union ccb *ccb; 2944 int error; 2945 2946 error = 0; 2947 2948 ccb = cdgetccb(periph, /* priority */ 1); 2949 2950 csio = &ccb->csio; 2951 2952 cam_fill_csio(csio, 2953 /* retries */ 1, 2954 /* cbfcnp */ cddone, 2955 /* flags */ CAM_DIR_NONE, 2956 /* tag_action */ MSG_SIMPLE_Q_TAG, 2957 /* data_ptr */ NULL, 2958 /* dxfer_len */ 0, 2959 /* sense_len */ SSD_FULL_SIZE, 2960 sizeof(struct scsi_play_track), 2961 /* timeout */ 50000); 2962 2963 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 2964 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2965 2966 scsi_cmd->op_code = PLAY_TRACK; 2967 scsi_cmd->start_track = strack; 2968 scsi_cmd->start_index = sindex; 2969 scsi_cmd->end_track = etrack; 2970 scsi_cmd->end_index = eindex; 2971 2972 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2973 /*sense_flags*/SF_RETRY_UA); 2974 2975 xpt_release_ccb(ccb); 2976 2977 return(error); 2978} 2979 2980static int 2981cdpause(struct cam_periph *periph, u_int32_t go) 2982{ 2983 struct scsi_pause *scsi_cmd; 2984 struct ccb_scsiio *csio; 2985 union ccb *ccb; 2986 int error; 2987 2988 error = 0; 2989 2990 ccb = cdgetccb(periph, /* priority */ 1); 2991 2992 csio = &ccb->csio; 2993 2994 cam_fill_csio(csio, 2995 /* retries */ 1, 2996 /* cbfcnp */ cddone, 2997 /* flags */ CAM_DIR_NONE, 2998 /* tag_action */ MSG_SIMPLE_Q_TAG, 2999 /* data_ptr */ NULL, 3000 /* dxfer_len */ 0, 3001 /* sense_len */ SSD_FULL_SIZE, 3002 sizeof(struct scsi_pause), 3003 /* timeout */ 50000); 3004 3005 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3006 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3007 3008 scsi_cmd->op_code = PAUSE; 3009 scsi_cmd->resume = go; 3010 3011 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3012 /*sense_flags*/SF_RETRY_UA); 3013 3014 xpt_release_ccb(ccb); 3015 3016 return(error); 3017} 3018 3019static int 3020cdstartunit(struct cam_periph *periph) 3021{ 3022 union ccb *ccb; 3023 int error; 3024 3025 error = 0; 3026 3027 ccb = cdgetccb(periph, /* priority */ 1); 3028 3029 scsi_start_stop(&ccb->csio, 3030 /* retries */ 1, 3031 /* cbfcnp */ cddone, 3032 /* tag_action */ MSG_SIMPLE_Q_TAG, 3033 /* start */ TRUE, 3034 /* load_eject */ FALSE, 3035 /* immediate */ FALSE, 3036 /* sense_len */ SSD_FULL_SIZE, 3037 /* timeout */ 50000); 3038 3039 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3040 /*sense_flags*/SF_RETRY_UA); 3041 3042 xpt_release_ccb(ccb); 3043 3044 return(error); 3045} 3046 3047static int 3048cdstopunit(struct cam_periph *periph, u_int32_t eject) 3049{ 3050 union ccb *ccb; 3051 int error; 3052 3053 error = 0; 3054 3055 ccb = cdgetccb(periph, /* priority */ 1); 3056 3057 scsi_start_stop(&ccb->csio, 3058 /* retries */ 1, 3059 /* cbfcnp */ cddone, 3060 /* tag_action */ MSG_SIMPLE_Q_TAG, 3061 /* start */ FALSE, 3062 /* load_eject */ eject, 3063 /* immediate */ FALSE, 3064 /* sense_len */ SSD_FULL_SIZE, 3065 /* timeout */ 50000); 3066 3067 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3068 /*sense_flags*/SF_RETRY_UA); 3069 3070 xpt_release_ccb(ccb); 3071 3072 return(error); 3073} 3074 3075static int 3076cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3077{ 3078 union ccb *ccb; 3079 u_int8_t *databuf; 3080 u_int32_t lba; 3081 int error; 3082 int length; 3083 3084 error = 0; 3085 databuf = NULL; 3086 lba = 0; 3087 3088 ccb = cdgetccb(periph, /* priority */ 1); 3089 3090 switch (authinfo->format) { 3091 case DVD_REPORT_AGID: 3092 length = sizeof(struct scsi_report_key_data_agid); 3093 break; 3094 case DVD_REPORT_CHALLENGE: 3095 length = sizeof(struct scsi_report_key_data_challenge); 3096 break; 3097 case DVD_REPORT_KEY1: 3098 length = sizeof(struct scsi_report_key_data_key1_key2); 3099 break; 3100 case DVD_REPORT_TITLE_KEY: 3101 length = sizeof(struct scsi_report_key_data_title); 3102 /* The lba field is only set for the title key */ 3103 lba = authinfo->lba; 3104 break; 3105 case DVD_REPORT_ASF: 3106 length = sizeof(struct scsi_report_key_data_asf); 3107 break; 3108 case DVD_REPORT_RPC: 3109 length = sizeof(struct scsi_report_key_data_rpc); 3110 break; 3111 case DVD_INVALIDATE_AGID: 3112 length = 0; 3113 break; 3114 default: 3115 error = EINVAL; 3116 goto bailout; 3117 break; /* NOTREACHED */ 3118 } 3119 3120 if (length != 0) { 3121 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3122 } else 3123 databuf = NULL; 3124 3125 3126 scsi_report_key(&ccb->csio, 3127 /* retries */ 1, 3128 /* cbfcnp */ cddone, 3129 /* tag_action */ MSG_SIMPLE_Q_TAG, 3130 /* lba */ lba, 3131 /* agid */ authinfo->agid, 3132 /* key_format */ authinfo->format, 3133 /* data_ptr */ databuf, 3134 /* dxfer_len */ length, 3135 /* sense_len */ SSD_FULL_SIZE, 3136 /* timeout */ 50000); 3137 3138 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3139 /*sense_flags*/SF_RETRY_UA); 3140 3141 if (error != 0) 3142 goto bailout; 3143 3144 if (ccb->csio.resid != 0) { 3145 xpt_print_path(periph->path); 3146 printf("warning, residual for report key command is %d\n", 3147 ccb->csio.resid); 3148 } 3149 3150 switch(authinfo->format) { 3151 case DVD_REPORT_AGID: { 3152 struct scsi_report_key_data_agid *agid_data; 3153 3154 agid_data = (struct scsi_report_key_data_agid *)databuf; 3155 3156 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3157 RKD_AGID_SHIFT; 3158 break; 3159 } 3160 case DVD_REPORT_CHALLENGE: { 3161 struct scsi_report_key_data_challenge *chal_data; 3162 3163 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3164 3165 bcopy(chal_data->challenge_key, authinfo->keychal, 3166 min(sizeof(chal_data->challenge_key), 3167 sizeof(authinfo->keychal))); 3168 break; 3169 } 3170 case DVD_REPORT_KEY1: { 3171 struct scsi_report_key_data_key1_key2 *key1_data; 3172 3173 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3174 3175 bcopy(key1_data->key1, authinfo->keychal, 3176 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3177 break; 3178 } 3179 case DVD_REPORT_TITLE_KEY: { 3180 struct scsi_report_key_data_title *title_data; 3181 3182 title_data = (struct scsi_report_key_data_title *)databuf; 3183 3184 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3185 RKD_TITLE_CPM_SHIFT; 3186 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3187 RKD_TITLE_CP_SEC_SHIFT; 3188 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3189 RKD_TITLE_CMGS_SHIFT; 3190 bcopy(title_data->title_key, authinfo->keychal, 3191 min(sizeof(title_data->title_key), 3192 sizeof(authinfo->keychal))); 3193 break; 3194 } 3195 case DVD_REPORT_ASF: { 3196 struct scsi_report_key_data_asf *asf_data; 3197 3198 asf_data = (struct scsi_report_key_data_asf *)databuf; 3199 3200 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3201 break; 3202 } 3203 case DVD_REPORT_RPC: { 3204 struct scsi_report_key_data_rpc *rpc_data; 3205 3206 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3207 3208 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3209 RKD_RPC_TYPE_SHIFT; 3210 authinfo->vend_rsts = 3211 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3212 RKD_RPC_VENDOR_RESET_SHIFT; 3213 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3214 authinfo->region = rpc_data->region_mask; 3215 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3216 break; 3217 } 3218 case DVD_INVALIDATE_AGID: 3219 break; 3220 default: 3221 /* This should be impossible, since we checked above */ 3222 error = EINVAL; 3223 goto bailout; 3224 break; /* NOTREACHED */ 3225 } 3226bailout: 3227 if (databuf != NULL) 3228 free(databuf, M_DEVBUF); 3229 3230 xpt_release_ccb(ccb); 3231 3232 return(error); 3233} 3234 3235static int 3236cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3237{ 3238 union ccb *ccb; 3239 u_int8_t *databuf; 3240 int length; 3241 int error; 3242 3243 error = 0; 3244 databuf = NULL; 3245 3246 ccb = cdgetccb(periph, /* priority */ 1); 3247 3248 switch(authinfo->format) { 3249 case DVD_SEND_CHALLENGE: { 3250 struct scsi_report_key_data_challenge *challenge_data; 3251 3252 length = sizeof(*challenge_data); 3253 3254 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3255 3256 databuf = (u_int8_t *)challenge_data; 3257 3258 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3259 challenge_data->data_len); 3260 3261 bcopy(authinfo->keychal, challenge_data->challenge_key, 3262 min(sizeof(authinfo->keychal), 3263 sizeof(challenge_data->challenge_key))); 3264 break; 3265 } 3266 case DVD_SEND_KEY2: { 3267 struct scsi_report_key_data_key1_key2 *key2_data; 3268 3269 length = sizeof(*key2_data); 3270 3271 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3272 3273 databuf = (u_int8_t *)key2_data; 3274 3275 scsi_ulto2b(length - sizeof(key2_data->data_len), 3276 key2_data->data_len); 3277 3278 bcopy(authinfo->keychal, key2_data->key1, 3279 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 3280 3281 break; 3282 } 3283 case DVD_SEND_RPC: { 3284 struct scsi_send_key_data_rpc *rpc_data; 3285 3286 length = sizeof(*rpc_data); 3287 3288 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3289 3290 databuf = (u_int8_t *)rpc_data; 3291 3292 scsi_ulto2b(length - sizeof(rpc_data->data_len), 3293 rpc_data->data_len); 3294 3295 rpc_data->region_code = authinfo->region; 3296 break; 3297 } 3298 default: 3299 error = EINVAL; 3300 goto bailout; 3301 break; /* NOTREACHED */ 3302 } 3303 3304 scsi_send_key(&ccb->csio, 3305 /* retries */ 1, 3306 /* cbfcnp */ cddone, 3307 /* tag_action */ MSG_SIMPLE_Q_TAG, 3308 /* agid */ authinfo->agid, 3309 /* key_format */ authinfo->format, 3310 /* data_ptr */ databuf, 3311 /* dxfer_len */ length, 3312 /* sense_len */ SSD_FULL_SIZE, 3313 /* timeout */ 50000); 3314 3315 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3316 /*sense_flags*/SF_RETRY_UA); 3317 3318bailout: 3319 3320 if (databuf != NULL) 3321 free(databuf, M_DEVBUF); 3322 3323 xpt_release_ccb(ccb); 3324 3325 return(error); 3326} 3327 3328static int 3329cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 3330{ 3331 union ccb *ccb; 3332 u_int8_t *databuf; 3333 u_int32_t address; 3334 int error; 3335 int length; 3336 3337 error = 0; 3338 databuf = NULL; 3339 /* The address is reserved for many of the formats */ 3340 address = 0; 3341 3342 ccb = cdgetccb(periph, /* priority */ 1); 3343 3344 switch(dvdstruct->format) { 3345 case DVD_STRUCT_PHYSICAL: 3346 length = sizeof(struct scsi_read_dvd_struct_data_physical); 3347 break; 3348 case DVD_STRUCT_COPYRIGHT: 3349 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 3350 break; 3351 case DVD_STRUCT_DISCKEY: 3352 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 3353 break; 3354 case DVD_STRUCT_BCA: 3355 length = sizeof(struct scsi_read_dvd_struct_data_bca); 3356 break; 3357 case DVD_STRUCT_MANUFACT: 3358 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 3359 break; 3360 case DVD_STRUCT_CMI: 3361 error = ENODEV; 3362 goto bailout; 3363#ifdef notyet 3364 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage); 3365 address = dvdstruct->address; 3366#endif 3367 break; /* NOTREACHED */ 3368 case DVD_STRUCT_PROTDISCID: 3369 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 3370 break; 3371 case DVD_STRUCT_DISCKEYBLOCK: 3372 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 3373 break; 3374 case DVD_STRUCT_DDS: 3375 length = sizeof(struct scsi_read_dvd_struct_data_dds); 3376 break; 3377 case DVD_STRUCT_MEDIUM_STAT: 3378 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 3379 break; 3380 case DVD_STRUCT_SPARE_AREA: 3381 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 3382 break; 3383 case DVD_STRUCT_RMD_LAST: 3384 error = ENODEV; 3385 goto bailout; 3386#ifdef notyet 3387 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout); 3388 address = dvdstruct->address; 3389#endif 3390 break; /* NOTREACHED */ 3391 case DVD_STRUCT_RMD_RMA: 3392 error = ENODEV; 3393 goto bailout; 3394#ifdef notyet 3395 length = sizeof(struct scsi_read_dvd_struct_data_rmd); 3396 address = dvdstruct->address; 3397#endif 3398 break; /* NOTREACHED */ 3399 case DVD_STRUCT_PRERECORDED: 3400 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 3401 break; 3402 case DVD_STRUCT_UNIQUEID: 3403 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 3404 break; 3405 case DVD_STRUCT_DCB: 3406 error = ENODEV; 3407 goto bailout; 3408#ifdef notyet 3409 length = sizeof(struct scsi_read_dvd_struct_data_dcb); 3410 address = dvdstruct->address; 3411#endif 3412 break; /* NOTREACHED */ 3413 case DVD_STRUCT_LIST: 3414 /* 3415 * This is the maximum allocation length for the READ DVD 3416 * STRUCTURE command. There's nothing in the MMC3 spec 3417 * that indicates a limit in the amount of data that can 3418 * be returned from this call, other than the limits 3419 * imposed by the 2-byte length variables. 3420 */ 3421 length = 65535; 3422 break; 3423 default: 3424 error = EINVAL; 3425 goto bailout; 3426 break; /* NOTREACHED */ 3427 } 3428 3429 if (length != 0) { 3430 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3431 } else 3432 databuf = NULL; 3433 3434 scsi_read_dvd_structure(&ccb->csio, 3435 /* retries */ 1, 3436 /* cbfcnp */ cddone, 3437 /* tag_action */ MSG_SIMPLE_Q_TAG, 3438 /* lba */ address, 3439 /* layer_number */ dvdstruct->layer_num, 3440 /* key_format */ dvdstruct->format, 3441 /* agid */ dvdstruct->agid, 3442 /* data_ptr */ databuf, 3443 /* dxfer_len */ length, 3444 /* sense_len */ SSD_FULL_SIZE, 3445 /* timeout */ 50000); 3446 3447 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3448 /*sense_flags*/SF_RETRY_UA); 3449 3450 if (error != 0) 3451 goto bailout; 3452 3453 switch(dvdstruct->format) { 3454 case DVD_STRUCT_PHYSICAL: { 3455 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 3456 struct dvd_layer *outlayer; 3457 struct scsi_read_dvd_struct_data_physical *phys_data; 3458 3459 phys_data = 3460 (struct scsi_read_dvd_struct_data_physical *)databuf; 3461 inlayer = &phys_data->layer_desc; 3462 outlayer = (struct dvd_layer *)&dvdstruct->data; 3463 3464 dvdstruct->length = sizeof(*inlayer); 3465 3466 outlayer->book_type = (inlayer->book_type_version & 3467 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 3468 outlayer->book_version = (inlayer->book_type_version & 3469 RDSD_BOOK_VERSION_MASK); 3470 outlayer->disc_size = (inlayer->disc_size_max_rate & 3471 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 3472 outlayer->max_rate = (inlayer->disc_size_max_rate & 3473 RDSD_MAX_RATE_MASK); 3474 outlayer->nlayers = (inlayer->layer_info & 3475 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 3476 outlayer->track_path = (inlayer->layer_info & 3477 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 3478 outlayer->layer_type = (inlayer->layer_info & 3479 RDSD_LAYER_TYPE_MASK); 3480 outlayer->linear_density = (inlayer->density & 3481 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 3482 outlayer->track_density = (inlayer->density & 3483 RDSD_TRACK_DENSITY_MASK); 3484 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 3485 RDSD_BCA_SHIFT; 3486 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 3487 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 3488 outlayer->end_sector_l0 = 3489 scsi_3btoul(inlayer->end_sector_layer0); 3490 break; 3491 } 3492 case DVD_STRUCT_COPYRIGHT: { 3493 struct scsi_read_dvd_struct_data_copyright *copy_data; 3494 3495 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 3496 databuf; 3497 3498 dvdstruct->cpst = copy_data->cps_type; 3499 dvdstruct->rmi = copy_data->region_info; 3500 dvdstruct->length = 0; 3501 3502 break; 3503 } 3504 default: 3505 /* 3506 * Tell the user what the overall length is, no matter 3507 * what we can actually fit in the data buffer. 3508 */ 3509 dvdstruct->length = length - ccb->csio.resid - 3510 sizeof(struct scsi_read_dvd_struct_data_header); 3511 3512 /* 3513 * But only actually copy out the smaller of what we read 3514 * in or what the structure can take. 3515 */ 3516 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 3517 dvdstruct->data, 3518 min(sizeof(dvdstruct->data), dvdstruct->length)); 3519 break; 3520 } 3521bailout: 3522 3523 if (databuf != NULL) 3524 free(databuf, M_DEVBUF); 3525 3526 xpt_release_ccb(ccb); 3527 3528 return(error); 3529} 3530 3531void 3532scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 3533 void (*cbfcnp)(struct cam_periph *, union ccb *), 3534 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 3535 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 3536 u_int8_t sense_len, u_int32_t timeout) 3537{ 3538 struct scsi_report_key *scsi_cmd; 3539 3540 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 3541 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3542 scsi_cmd->opcode = REPORT_KEY; 3543 scsi_ulto4b(lba, scsi_cmd->lba); 3544 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 3545 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3546 (key_format & RK_KF_KEYFORMAT_MASK); 3547 3548 cam_fill_csio(csio, 3549 retries, 3550 cbfcnp, 3551 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 3552 tag_action, 3553 /*data_ptr*/ data_ptr, 3554 /*dxfer_len*/ dxfer_len, 3555 sense_len, 3556 sizeof(*scsi_cmd), 3557 timeout); 3558} 3559 3560void 3561scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 3562 void (*cbfcnp)(struct cam_periph *, union ccb *), 3563 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 3564 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 3565 u_int32_t timeout) 3566{ 3567 struct scsi_send_key *scsi_cmd; 3568 3569 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 3570 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3571 scsi_cmd->opcode = SEND_KEY; 3572 3573 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 3574 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3575 (key_format & RK_KF_KEYFORMAT_MASK); 3576 3577 cam_fill_csio(csio, 3578 retries, 3579 cbfcnp, 3580 /*flags*/ CAM_DIR_OUT, 3581 tag_action, 3582 /*data_ptr*/ data_ptr, 3583 /*dxfer_len*/ dxfer_len, 3584 sense_len, 3585 sizeof(*scsi_cmd), 3586 timeout); 3587} 3588 3589 3590void 3591scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 3592 void (*cbfcnp)(struct cam_periph *, union ccb *), 3593 u_int8_t tag_action, u_int32_t address, 3594 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 3595 u_int8_t *data_ptr, u_int32_t dxfer_len, 3596 u_int8_t sense_len, u_int32_t timeout) 3597{ 3598 struct scsi_read_dvd_structure *scsi_cmd; 3599 3600 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 3601 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3602 scsi_cmd->opcode = READ_DVD_STRUCTURE; 3603 3604 scsi_ulto4b(address, scsi_cmd->address); 3605 scsi_cmd->layer_number = layer_number; 3606 scsi_cmd->format = format; 3607 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 3608 /* The AGID is the top two bits of this byte */ 3609 scsi_cmd->agid = agid << 6; 3610 3611 cam_fill_csio(csio, 3612 retries, 3613 cbfcnp, 3614 /*flags*/ CAM_DIR_IN, 3615 tag_action, 3616 /*data_ptr*/ data_ptr, 3617 /*dxfer_len*/ dxfer_len, 3618 sense_len, 3619 sizeof(*scsi_cmd), 3620 timeout); 3621} 3622