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