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