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