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