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.43 2008/05/23 19:46:37 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); 1026 cam_periph_unlock(periph); 1027 /* stays acquired */ 1028 1029 return (0); 1030 } 1031 1032 static int 1033 cdclose(struct dev_close_args *ap) 1034 { 1035 cdev_t dev = ap->a_head.a_dev; 1036 struct cam_periph *periph; 1037 struct cd_softc *softc; 1038 struct disk_info *info; 1039 int unit; 1040 1041 unit = dkunit(dev); 1042 periph = cam_extend_get(cdperiphs, unit); 1043 if (periph == NULL) 1044 return (ENXIO); 1045 1046 softc = (struct cd_softc *)periph->softc; 1047 1048 cam_periph_lock(periph); 1049 cam_periph_hold(periph, 0); 1050 1051 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 1052 cdprevent(periph, PR_ALLOW); 1053 1054 /* 1055 * Unconditionally set the dsopen() flags back to their default 1056 * state. 1057 */ 1058 info = &softc->disk.d_info; 1059 info->d_dsflags &= ~DSO_NOLABELS; 1060 info->d_dsflags |= DSO_COMPATLABEL; 1061 1062 /* 1063 * Since we're closing this CD, mark the blocksize as unavailable. 1064 * It will be marked as available when the CD is opened again. 1065 */ 1066 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; 1067 1068 /* 1069 * We'll check the media and toc again at the next open(). 1070 */ 1071 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 1072 1073 cam_periph_unhold(periph); 1074 cam_periph_unlock(periph); 1075 cam_periph_release(periph); 1076 1077 return (0); 1078 } 1079 1080 static void 1081 cdshorttimeout(void *arg) 1082 { 1083 struct cdchanger *changer; 1084 1085 changer = (struct cdchanger *)arg; 1086 1087 /* Always clear the short timeout flag, since that's what we're in */ 1088 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1089 1090 /* 1091 * Check to see if there is any more pending or outstanding I/O for 1092 * this device. If not, move it out of the active slot. 1093 */ 1094 if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1095 && (changer->cur_device->outstanding_cmds == 0)) { 1096 changer->flags |= CHANGER_MANUAL_CALL; 1097 cdrunchangerqueue(changer); 1098 } 1099 } 1100 1101 /* 1102 * This is a wrapper for xpt_schedule. It only applies to changers. 1103 */ 1104 static void 1105 cdschedule(struct cam_periph *periph, int priority) 1106 { 1107 struct cd_softc *softc; 1108 1109 softc = (struct cd_softc *)periph->softc; 1110 1111 /* 1112 * If this device isn't currently queued, and if it isn't 1113 * the active device, then we queue this device and run the 1114 * changer queue if there is no timeout scheduled to do it. 1115 * If this device is the active device, just schedule it 1116 * to run again. If this device is queued, there should be 1117 * a timeout in place already that will make sure it runs. 1118 */ 1119 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 1120 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 1121 /* 1122 * We don't do anything with the priority here. 1123 * This is strictly a fifo queue. 1124 */ 1125 softc->pinfo.priority = 1; 1126 softc->pinfo.generation = ++softc->changer->devq.generation; 1127 camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 1128 1129 /* 1130 * Since we just put a device in the changer queue, 1131 * check and see if there is a timeout scheduled for 1132 * this changer. If so, let the timeout handle 1133 * switching this device into the active slot. If 1134 * not, manually call the timeout routine to 1135 * bootstrap things. 1136 */ 1137 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1138 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 1139 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 1140 softc->changer->flags |= CHANGER_MANUAL_CALL; 1141 cdrunchangerqueue(softc->changer); 1142 } 1143 } else if ((softc->flags & CD_FLAG_ACTIVE) 1144 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) { 1145 xpt_schedule(periph, priority); 1146 } 1147 } 1148 1149 static void 1150 cdrunchangerqueue(void *arg) 1151 { 1152 struct cd_softc *softc; 1153 struct cdchanger *changer; 1154 int called_from_timeout; 1155 1156 changer = (struct cdchanger *)arg; 1157 1158 /* 1159 * If we have NOT been called from cdstrategy() or cddone(), and 1160 * instead from a timeout routine, go ahead and clear the 1161 * timeout flag. 1162 */ 1163 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 1164 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1165 called_from_timeout = 1; 1166 } else 1167 called_from_timeout = 0; 1168 1169 /* Always clear the manual call flag */ 1170 changer->flags &= ~CHANGER_MANUAL_CALL; 1171 1172 /* nothing to do if the queue is empty */ 1173 if (changer->devq.entries <= 0) { 1174 return; 1175 } 1176 1177 /* 1178 * If the changer queue is frozen, that means we have an active 1179 * device. 1180 */ 1181 if (changer->devq.qfrozen_cnt > 0) { 1182 1183 /* 1184 * We always need to reset the frozen count and clear the 1185 * active flag. 1186 */ 1187 changer->devq.qfrozen_cnt--; 1188 changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1189 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1190 1191 if (changer->cur_device->outstanding_cmds > 0) { 1192 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 1193 changer->cur_device->bufs_left = 1194 changer->cur_device->outstanding_cmds; 1195 if (called_from_timeout) { 1196 callout_reset(&changer->long_handle, 1197 changer_max_busy_seconds * hz, 1198 cdrunchangerqueue, changer); 1199 changer->flags |= CHANGER_TIMEOUT_SCHED; 1200 } 1201 return; 1202 } 1203 1204 /* 1205 * Check to see whether the current device has any I/O left 1206 * to do. If so, requeue it at the end of the queue. If 1207 * not, there is no need to requeue it. 1208 */ 1209 if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 1210 1211 changer->cur_device->pinfo.generation = 1212 ++changer->devq.generation; 1213 camq_insert(&changer->devq, 1214 (cam_pinfo *)changer->cur_device); 1215 } 1216 } 1217 1218 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 1219 1220 changer->cur_device = softc; 1221 1222 changer->devq.qfrozen_cnt++; 1223 softc->flags |= CD_FLAG_ACTIVE; 1224 1225 /* Just in case this device is waiting */ 1226 wakeup(&softc->changer); 1227 xpt_schedule(softc->periph, /*priority*/ 1); 1228 1229 /* 1230 * Get rid of any pending timeouts, and set a flag to schedule new 1231 * ones so this device gets its full time quantum. 1232 */ 1233 if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1234 callout_stop(&changer->long_handle); 1235 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1236 } 1237 1238 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1239 callout_stop(&changer->short_handle); 1240 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1241 } 1242 1243 /* 1244 * We need to schedule timeouts, but we only do this after the 1245 * first transaction has completed. This eliminates the changer 1246 * switch time. 1247 */ 1248 changer->flags |= CHANGER_NEED_TIMEOUT; 1249 } 1250 1251 static void 1252 cdchangerschedule(struct cd_softc *softc) 1253 { 1254 struct cdchanger *changer; 1255 1256 changer = softc->changer; 1257 1258 /* 1259 * If this is a changer, and this is the current device, 1260 * and this device has at least the minimum time quantum to 1261 * run, see if we can switch it out. 1262 */ 1263 if ((softc->flags & CD_FLAG_ACTIVE) 1264 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 1265 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 1266 /* 1267 * We try three things here. The first is that we 1268 * check to see whether the schedule on completion 1269 * flag is set. If it is, we decrement the number 1270 * of buffers left, and if it's zero, we reschedule. 1271 * Next, we check to see whether the pending buffer 1272 * queue is empty and whether there are no 1273 * outstanding transactions. If so, we reschedule. 1274 * Next, we see if the pending buffer queue is empty. 1275 * If it is, we set the number of buffers left to 1276 * the current active buffer count and set the 1277 * schedule on complete flag. 1278 */ 1279 if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 1280 if (--softc->bufs_left == 0) { 1281 softc->changer->flags |= 1282 CHANGER_MANUAL_CALL; 1283 softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 1284 cdrunchangerqueue(softc->changer); 1285 } 1286 } else if ((bioq_first(&softc->bio_queue) == NULL) 1287 && (softc->outstanding_cmds == 0)) { 1288 softc->changer->flags |= CHANGER_MANUAL_CALL; 1289 cdrunchangerqueue(softc->changer); 1290 } 1291 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 1292 && (softc->flags & CD_FLAG_ACTIVE)) { 1293 1294 /* 1295 * Now that the first transaction to this 1296 * particular device has completed, we can go ahead 1297 * and schedule our timeouts. 1298 */ 1299 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1300 callout_reset(&changer->long_handle, 1301 changer_max_busy_seconds * hz, 1302 cdrunchangerqueue, changer); 1303 changer->flags |= CHANGER_TIMEOUT_SCHED; 1304 } else 1305 kprintf("cdchangerschedule: already have a long" 1306 " timeout!\n"); 1307 1308 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1309 callout_reset(&changer->short_handle, 1310 changer_min_busy_seconds * hz, 1311 cdshorttimeout, changer); 1312 changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 1313 } else 1314 kprintf("cdchangerschedule: already have a short " 1315 "timeout!\n"); 1316 1317 /* 1318 * We just scheduled timeouts, no need to schedule 1319 * more. 1320 */ 1321 changer->flags &= ~CHANGER_NEED_TIMEOUT; 1322 1323 } 1324 } 1325 1326 static int 1327 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 1328 u_int32_t cam_flags, 1329 u_int32_t sense_flags), 1330 u_int32_t cam_flags, u_int32_t sense_flags) 1331 { 1332 struct cd_softc *softc; 1333 struct cam_periph *periph; 1334 int error; 1335 1336 periph = xpt_path_periph(ccb->ccb_h.path); 1337 softc = (struct cd_softc *)periph->softc; 1338 1339 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1340 &softc->device_stats); 1341 1342 if (softc->flags & CD_FLAG_CHANGER) 1343 cdchangerschedule(softc); 1344 1345 return(error); 1346 } 1347 1348 static union ccb * 1349 cdgetccb(struct cam_periph *periph, u_int32_t priority) 1350 { 1351 struct cd_softc *softc; 1352 1353 softc = (struct cd_softc *)periph->softc; 1354 1355 if (softc->flags & CD_FLAG_CHANGER) { 1356 /* 1357 * This should work the first time this device is woken up, 1358 * but just in case it doesn't, we use a while loop. 1359 */ 1360 while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 1361 /* 1362 * If this changer isn't already queued, queue it up. 1363 */ 1364 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1365 softc->pinfo.priority = 1; 1366 softc->pinfo.generation = 1367 ++softc->changer->devq.generation; 1368 camq_insert(&softc->changer->devq, 1369 (cam_pinfo *)softc); 1370 } 1371 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1372 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 1373 && ((softc->changer->flags 1374 & CHANGER_SHORT_TMOUT_SCHED)==0)) { 1375 softc->changer->flags |= CHANGER_MANUAL_CALL; 1376 cdrunchangerqueue(softc->changer); 1377 } else 1378 sim_lock_sleep(&softc->changer, 0, "cgticb", 0, 1379 periph->sim->lock); 1380 } 1381 } 1382 return(cam_periph_getccb(periph, priority)); 1383 } 1384 1385 /* 1386 * Actually translate the requested transfer into one the physical driver 1387 * can understand. The transfer is described by a buf and will include 1388 * only one physical transfer. 1389 */ 1390 static int 1391 cdstrategy(struct dev_strategy_args *ap) 1392 { 1393 cdev_t dev = ap->a_head.a_dev; 1394 struct bio *bio = ap->a_bio; 1395 struct buf *bp = bio->bio_buf; 1396 struct cam_periph *periph; 1397 struct cd_softc *softc; 1398 u_int unit; 1399 1400 unit = dkunit(dev); 1401 periph = cam_extend_get(cdperiphs, unit); 1402 if (periph == NULL) { 1403 bp->b_error = ENXIO; 1404 goto bad; 1405 } 1406 1407 cam_periph_lock(periph); 1408 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 1409 1410 softc = (struct cd_softc *)periph->softc; 1411 1412 /* 1413 * If the device has been made invalid, error out 1414 */ 1415 if ((softc->flags & CD_FLAG_INVALID)) { 1416 cam_periph_unlock(periph); 1417 bp->b_error = ENXIO; 1418 goto bad; 1419 } 1420 1421 /* 1422 * If we don't have valid media, look for it before trying to 1423 * schedule the I/O. 1424 */ 1425 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1426 int error; 1427 1428 error = cdcheckmedia(periph); 1429 if (error != 0) { 1430 cam_periph_unlock(periph); 1431 bp->b_error = error; 1432 goto bad; 1433 } 1434 } 1435 1436 /* 1437 * Place it in the queue of disk activities for this disk 1438 */ 1439 bioqdisksort(&softc->bio_queue, bio); 1440 1441 /* 1442 * Schedule ourselves for performing the work. We do things 1443 * differently for changers. 1444 */ 1445 if ((softc->flags & CD_FLAG_CHANGER) == 0) 1446 xpt_schedule(periph, /* XXX priority */1); 1447 else 1448 cdschedule(periph, /* priority */ 1); 1449 1450 cam_periph_unlock(periph); 1451 return(0); 1452 bad: 1453 bp->b_flags |= B_ERROR; 1454 /* 1455 * Correctly set the buf to indicate a completed xfer 1456 */ 1457 bp->b_resid = bp->b_bcount; 1458 biodone(bio); 1459 return(0); 1460 } 1461 1462 static void 1463 cdstart(struct cam_periph *periph, union ccb *start_ccb) 1464 { 1465 struct cd_softc *softc; 1466 struct bio *bio; 1467 struct buf *bp; 1468 struct ccb_scsiio *csio; 1469 struct scsi_read_capacity_data *rcap; 1470 1471 softc = (struct cd_softc *)periph->softc; 1472 1473 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 1474 1475 switch (softc->state) { 1476 case CD_STATE_NORMAL: 1477 { 1478 bio = bioq_first(&softc->bio_queue); 1479 if (periph->immediate_priority <= periph->pinfo.priority) { 1480 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 1481 1482 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1483 periph_links.sle); 1484 periph->immediate_priority = CAM_PRIORITY_NONE; 1485 wakeup(&periph->ccb_list); 1486 } else if (bio == NULL) { 1487 xpt_release_ccb(start_ccb); 1488 } else { 1489 bp = bio->bio_buf; 1490 bioq_remove(&softc->bio_queue, bio); 1491 1492 devstat_start_transaction(&softc->device_stats); 1493 1494 KKASSERT(bio->bio_offset % softc->params.blksize == 0); 1495 1496 scsi_read_write(&start_ccb->csio, 1497 /*retries*/4, 1498 /* cbfcnp */ cddone, 1499 (bp->b_flags & B_ORDERED) != 0 ? 1500 MSG_ORDERED_Q_TAG : 1501 MSG_SIMPLE_Q_TAG, 1502 /* read */(bp->b_cmd == BUF_CMD_READ), 1503 /* byte2 */ 0, 1504 /* minimum_cmd_size */ 10, 1505 /* lba */ 1506 bio->bio_offset / softc->params.blksize, 1507 bp->b_bcount / softc->params.blksize, 1508 /* data_ptr */ bp->b_data, 1509 /* dxfer_len */ bp->b_bcount, 1510 /* sense_len */ SSD_FULL_SIZE, 1511 /* timeout */ 30000); 1512 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 1513 1514 1515 LIST_INSERT_HEAD(&softc->pending_ccbs, 1516 &start_ccb->ccb_h, periph_links.le); 1517 1518 softc->outstanding_cmds++; 1519 /* We expect a unit attention from this device */ 1520 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 1521 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 1522 softc->flags &= ~CD_FLAG_RETRY_UA; 1523 } 1524 1525 start_ccb->ccb_h.ccb_bio = bio; 1526 bio = bioq_first(&softc->bio_queue); 1527 1528 xpt_action(start_ccb); 1529 } 1530 if (bio != NULL) { 1531 /* Have more work to do, so ensure we stay scheduled */ 1532 xpt_schedule(periph, /* XXX priority */1); 1533 } 1534 break; 1535 } 1536 case CD_STATE_PROBE: 1537 { 1538 1539 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT); 1540 csio = &start_ccb->csio; 1541 scsi_read_capacity(csio, 1542 /*retries*/1, 1543 cddone, 1544 MSG_SIMPLE_Q_TAG, 1545 rcap, 1546 SSD_FULL_SIZE, 1547 /*timeout*/20000); 1548 start_ccb->ccb_h.ccb_bio = NULL; 1549 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 1550 xpt_action(start_ccb); 1551 break; 1552 } 1553 } 1554 } 1555 1556 static void 1557 cddone(struct cam_periph *periph, union ccb *done_ccb) 1558 { 1559 struct cd_softc *softc; 1560 struct ccb_scsiio *csio; 1561 1562 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 1563 1564 softc = (struct cd_softc *)periph->softc; 1565 csio = &done_ccb->csio; 1566 1567 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 1568 case CD_CCB_BUFFER_IO: 1569 { 1570 struct buf *bp; 1571 struct bio *bio; 1572 int error; 1573 1574 bio = (struct bio *)done_ccb->ccb_h.ccb_bio; 1575 bp = bio->bio_buf; 1576 error = 0; 1577 1578 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1579 int sf; 1580 1581 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 1582 sf = SF_RETRY_UA; 1583 else 1584 sf = 0; 1585 1586 error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 1587 if (error == ERESTART) { 1588 /* 1589 * A retry was scheuled, so 1590 * just return. 1591 */ 1592 return; 1593 } 1594 } 1595 1596 if (error != 0) { 1597 struct bio *q_bio; 1598 struct buf *q_bp; 1599 1600 xpt_print(periph->path, 1601 "cddone: got error %#x back\n", error); 1602 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) { 1603 bioq_remove(&softc->bio_queue, q_bio); 1604 q_bp = q_bio->bio_buf; 1605 q_bp->b_resid = q_bp->b_bcount; 1606 q_bp->b_error = EIO; 1607 q_bp->b_flags |= B_ERROR; 1608 biodone(q_bio); 1609 } 1610 bp->b_resid = bp->b_bcount; 1611 bp->b_error = error; 1612 bp->b_flags |= B_ERROR; 1613 cam_release_devq(done_ccb->ccb_h.path, 1614 /*relsim_flags*/0, 1615 /*reduction*/0, 1616 /*timeout*/0, 1617 /*getcount_only*/0); 1618 1619 } else { 1620 bp->b_resid = csio->resid; 1621 bp->b_error = 0; 1622 if (bp->b_resid != 0) { 1623 /* Short transfer ??? */ 1624 bp->b_flags |= B_ERROR; 1625 } 1626 } 1627 1628 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1629 softc->outstanding_cmds--; 1630 1631 if (softc->flags & CD_FLAG_CHANGER) 1632 cdchangerschedule(softc); 1633 1634 devstat_end_transaction_buf(&softc->device_stats, bp); 1635 biodone(bio); 1636 break; 1637 } 1638 case CD_CCB_PROBE: 1639 { 1640 struct scsi_read_capacity_data *rdcap; 1641 char announce_buf[120]; /* 1642 * Currently (9/30/97) the 1643 * longest possible announce 1644 * buffer is 108 bytes, for the 1645 * first error case below. 1646 * That is 39 bytes for the 1647 * basic string, 16 bytes for the 1648 * biggest sense key (hardware 1649 * error), 52 bytes for the 1650 * text of the largest sense 1651 * qualifier valid for a CDROM, 1652 * (0x72, 0x03 or 0x04, 1653 * 0x03), and one byte for the 1654 * null terminating character. 1655 * To allow for longer strings, 1656 * the announce buffer is 120 1657 * bytes. 1658 */ 1659 struct cd_params *cdp; 1660 1661 cdp = &softc->params; 1662 1663 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1664 1665 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1666 cdp->blksize = scsi_4btoul (rdcap->length); 1667 1668 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1669 1670 ksnprintf(announce_buf, sizeof(announce_buf), 1671 "cd present [%lu x %lu byte records]", 1672 cdp->disksize, (u_long)cdp->blksize); 1673 1674 } else { 1675 int error; 1676 /* 1677 * Retry any UNIT ATTENTION type errors. They 1678 * are expected at boot. 1679 */ 1680 error = cderror(done_ccb, CAM_RETRY_SELTO, 1681 SF_RETRY_UA | SF_NO_PRINT); 1682 if (error == ERESTART) { 1683 /* 1684 * A retry was scheuled, so 1685 * just return. 1686 */ 1687 return; 1688 } else if (error != 0) { 1689 1690 struct scsi_sense_data *sense; 1691 int asc, ascq; 1692 int sense_key, error_code; 1693 int have_sense; 1694 cam_status status; 1695 struct ccb_getdev cgd; 1696 1697 /* Don't wedge this device's queue */ 1698 cam_release_devq(done_ccb->ccb_h.path, 1699 /*relsim_flags*/0, 1700 /*reduction*/0, 1701 /*timeout*/0, 1702 /*getcount_only*/0); 1703 1704 status = done_ccb->ccb_h.status; 1705 1706 xpt_setup_ccb(&cgd.ccb_h, 1707 done_ccb->ccb_h.path, 1708 /* priority */ 1); 1709 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1710 xpt_action((union ccb *)&cgd); 1711 1712 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1713 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1714 || ((status & CAM_AUTOSNS_VALID) == 0)) 1715 have_sense = FALSE; 1716 else 1717 have_sense = TRUE; 1718 1719 if (have_sense) { 1720 sense = &csio->sense_data; 1721 scsi_extract_sense(sense, &error_code, 1722 &sense_key, 1723 &asc, &ascq); 1724 } 1725 /* 1726 * Attach to anything that claims to be a 1727 * CDROM or WORM device, as long as it 1728 * doesn't return a "Logical unit not 1729 * supported" (0x25) error. 1730 */ 1731 if ((have_sense) && (asc != 0x25) 1732 && (error_code == SSD_CURRENT_ERROR)) { 1733 const char *sense_key_desc; 1734 const char *asc_desc; 1735 1736 scsi_sense_desc(sense_key, asc, ascq, 1737 &cgd.inq_data, 1738 &sense_key_desc, 1739 &asc_desc); 1740 ksnprintf(announce_buf, 1741 sizeof(announce_buf), 1742 "Attempt to query device " 1743 "size failed: %s, %s", 1744 sense_key_desc, 1745 asc_desc); 1746 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1747 /* 1748 * We only print out an error for 1749 * CDROM type devices. For WORM 1750 * devices, we don't print out an 1751 * error since a few WORM devices 1752 * don't support CDROM commands. 1753 * If we have sense information, go 1754 * ahead and print it out. 1755 * Otherwise, just say that we 1756 * couldn't attach. 1757 */ 1758 1759 /* 1760 * Just print out the error, not 1761 * the full probe message, when we 1762 * don't attach. 1763 */ 1764 if (have_sense) 1765 scsi_sense_print( 1766 &done_ccb->csio); 1767 else { 1768 xpt_print(periph->path, 1769 "got CAM status %#x\n", 1770 done_ccb->ccb_h.status); 1771 } 1772 xpt_print(periph->path, "fatal error, " 1773 "failed to attach to device\n"); 1774 /* 1775 * Invalidate this peripheral. 1776 */ 1777 cam_periph_invalidate(periph); 1778 1779 announce_buf[0] = '\0'; 1780 } else { 1781 1782 /* 1783 * Invalidate this peripheral. 1784 */ 1785 cam_periph_invalidate(periph); 1786 announce_buf[0] = '\0'; 1787 } 1788 } 1789 } 1790 kfree(rdcap, M_SCSICD); 1791 if (announce_buf[0] != '\0') { 1792 xpt_announce_periph(periph, announce_buf); 1793 if (softc->flags & CD_FLAG_CHANGER) 1794 cdchangerschedule(softc); 1795 /* 1796 * Create our sysctl variables, now that we know 1797 * we have successfully attached. 1798 */ 1799 taskqueue_enqueue(taskqueue_thread[mycpuid], 1800 &softc->sysctl_task); 1801 } 1802 softc->state = CD_STATE_NORMAL; 1803 /* 1804 * Since our peripheral may be invalidated by an error 1805 * above or an external event, we must release our CCB 1806 * before releasing the probe lock on the peripheral. 1807 * The peripheral will only go away once the last lock 1808 * is removed, and we need it around for the CCB release 1809 * operation. 1810 */ 1811 xpt_release_ccb(done_ccb); 1812 cam_periph_unhold(periph); 1813 return; 1814 } 1815 case CD_CCB_WAITING: 1816 { 1817 /* Caller will release the CCB */ 1818 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1819 ("trying to wakeup ccbwait\n")); 1820 1821 wakeup(&done_ccb->ccb_h.cbfcnp); 1822 return; 1823 } 1824 default: 1825 break; 1826 } 1827 xpt_release_ccb(done_ccb); 1828 } 1829 1830 static union cd_pages * 1831 cdgetpage(struct cd_mode_params *mode_params) 1832 { 1833 union cd_pages *page; 1834 1835 if (mode_params->cdb_size == 10) 1836 page = (union cd_pages *)find_mode_page_10( 1837 (struct scsi_mode_header_10 *)mode_params->mode_buf); 1838 else 1839 page = (union cd_pages *)find_mode_page_6( 1840 (struct scsi_mode_header_6 *)mode_params->mode_buf); 1841 1842 return (page); 1843 } 1844 1845 static int 1846 cdgetpagesize(int page_num) 1847 { 1848 int i; 1849 1850 for (i = 0; i < (sizeof(cd_page_size_table)/ 1851 sizeof(cd_page_size_table[0])); i++) { 1852 if (cd_page_size_table[i].page == page_num) 1853 return (cd_page_size_table[i].page_size); 1854 } 1855 return (-1); 1856 } 1857 1858 static int 1859 cdioctl(struct dev_ioctl_args *ap) 1860 { 1861 cdev_t dev = ap->a_head.a_dev; 1862 caddr_t addr = ap->a_data; 1863 struct cam_periph *periph; 1864 struct cd_softc *softc; 1865 int unit, error = 0; 1866 1867 unit = dkunit(dev); 1868 1869 periph = cam_extend_get(cdperiphs, unit); 1870 if (periph == NULL) 1871 return(ENXIO); 1872 1873 cam_periph_lock(periph); 1874 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 1875 1876 softc = (struct cd_softc *)periph->softc; 1877 1878 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1879 ("trying to do ioctl %#lx\n", ap->a_cmd)); 1880 1881 if ((error = cam_periph_hold(periph, PCATCH)) != 0) { 1882 cam_periph_unlock(periph); 1883 cam_periph_release(periph); 1884 return (error); 1885 } 1886 1887 /* 1888 * If we don't have media loaded, check for it. If still don't 1889 * have media loaded, we can only do a load or eject. 1890 * 1891 * We only care whether media is loaded if this is a cd-specific ioctl 1892 * (thus the IOCGROUP check below). Note that this will break if 1893 * anyone adds any ioctls into the switch statement below that don't 1894 * have their ioctl group set to 'c'. 1895 */ 1896 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1897 && ((ap->a_cmd != CDIOCCLOSE) 1898 && (ap->a_cmd != CDIOCEJECT)) 1899 && (IOCGROUP(ap->a_cmd) == 'c')) { 1900 error = cdcheckmedia(periph); 1901 if (error != 0) { 1902 cam_periph_unhold(periph); 1903 cam_periph_unlock(periph); 1904 return (error); 1905 } 1906 } 1907 /* 1908 * Drop the lock here so later mallocs can use WAITOK. The periph 1909 * is essentially locked still with the cam_periph_hold call above. 1910 */ 1911 cam_periph_unlock(periph); 1912 1913 switch (ap->a_cmd) { 1914 1915 case CDIOCPLAYTRACKS: 1916 { 1917 struct ioc_play_track *args 1918 = (struct ioc_play_track *)addr; 1919 struct cd_mode_params params; 1920 union cd_pages *page; 1921 1922 params.alloc_len = sizeof(union cd_mode_data_6_10); 1923 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 1924 M_WAITOK | M_ZERO); 1925 1926 cam_periph_lock(periph); 1927 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1928 ("trying to do CDIOCPLAYTRACKS\n")); 1929 1930 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1931 if (error) { 1932 kfree(params.mode_buf, M_SCSICD); 1933 cam_periph_unlock(periph); 1934 break; 1935 } 1936 page = cdgetpage(¶ms); 1937 1938 page->audio.flags &= ~CD_PA_SOTC; 1939 page->audio.flags |= CD_PA_IMMED; 1940 error = cdsetmode(periph, ¶ms); 1941 kfree(params.mode_buf, M_SCSICD); 1942 if (error) { 1943 cam_periph_unlock(periph); 1944 break; 1945 } 1946 1947 /* 1948 * This was originally implemented with the PLAY 1949 * AUDIO TRACK INDEX command, but that command was 1950 * deprecated after SCSI-2. Most (all?) SCSI CDROM 1951 * drives support it but ATAPI and ATAPI-derivative 1952 * drives don't seem to support it. So we keep a 1953 * cache of the table of contents and translate 1954 * track numbers to MSF format. 1955 */ 1956 if (softc->flags & CD_FLAG_VALID_TOC) { 1957 union msf_lba *sentry, *eentry; 1958 int st, et; 1959 1960 if (args->end_track < 1961 softc->toc.header.ending_track + 1) 1962 args->end_track++; 1963 if (args->end_track > 1964 softc->toc.header.ending_track + 1) 1965 args->end_track = 1966 softc->toc.header.ending_track + 1; 1967 st = args->start_track - 1968 softc->toc.header.starting_track; 1969 et = args->end_track - 1970 softc->toc.header.starting_track; 1971 if ((st < 0) 1972 || (et < 0) 1973 || (st > (softc->toc.header.ending_track - 1974 softc->toc.header.starting_track))) { 1975 error = EINVAL; 1976 break; 1977 } 1978 sentry = &softc->toc.entries[st].addr; 1979 eentry = &softc->toc.entries[et].addr; 1980 error = cdplaymsf(periph, 1981 sentry->msf.minute, 1982 sentry->msf.second, 1983 sentry->msf.frame, 1984 eentry->msf.minute, 1985 eentry->msf.second, 1986 eentry->msf.frame); 1987 } else { 1988 /* 1989 * If we don't have a valid TOC, try the 1990 * play track index command. It is part of 1991 * the SCSI-2 spec, but was removed in the 1992 * MMC specs. ATAPI and ATAPI-derived 1993 * drives don't support it. 1994 */ 1995 if (softc->quirks & CD_Q_BCD_TRACKS) { 1996 args->start_track = 1997 bin2bcd(args->start_track); 1998 args->end_track = 1999 bin2bcd(args->end_track); 2000 } 2001 error = cdplaytracks(periph, 2002 args->start_track, 2003 args->start_index, 2004 args->end_track, 2005 args->end_index); 2006 } 2007 cam_periph_unlock(periph); 2008 } 2009 break; 2010 case CDIOCPLAYMSF: 2011 { 2012 struct ioc_play_msf *args 2013 = (struct ioc_play_msf *) addr; 2014 struct cd_mode_params params; 2015 union cd_pages *page; 2016 2017 params.alloc_len = sizeof(union cd_mode_data_6_10); 2018 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2019 M_WAITOK | M_ZERO); 2020 2021 cam_periph_lock(periph); 2022 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2023 ("trying to do CDIOCPLAYMSF\n")); 2024 2025 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2026 if (error) { 2027 kfree(params.mode_buf, M_SCSICD); 2028 cam_periph_unlock(periph); 2029 break; 2030 } 2031 page = cdgetpage(¶ms); 2032 2033 page->audio.flags &= ~CD_PA_SOTC; 2034 page->audio.flags |= CD_PA_IMMED; 2035 error = cdsetmode(periph, ¶ms); 2036 kfree(params.mode_buf, M_SCSICD); 2037 if (error) { 2038 cam_periph_unlock(periph); 2039 break; 2040 } 2041 error = cdplaymsf(periph, 2042 args->start_m, 2043 args->start_s, 2044 args->start_f, 2045 args->end_m, 2046 args->end_s, 2047 args->end_f); 2048 cam_periph_unlock(periph); 2049 } 2050 break; 2051 case CDIOCPLAYBLOCKS: 2052 { 2053 struct ioc_play_blocks *args 2054 = (struct ioc_play_blocks *) addr; 2055 struct cd_mode_params params; 2056 union cd_pages *page; 2057 2058 params.alloc_len = sizeof(union cd_mode_data_6_10); 2059 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2060 M_WAITOK | M_ZERO); 2061 cam_periph_lock(periph); 2062 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2063 ("trying to do CDIOCPLAYBLOCKS\n")); 2064 2065 2066 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2067 if (error) { 2068 kfree(params.mode_buf, M_SCSICD); 2069 cam_periph_unlock(periph); 2070 break; 2071 } 2072 page = cdgetpage(¶ms); 2073 2074 page->audio.flags &= ~CD_PA_SOTC; 2075 page->audio.flags |= CD_PA_IMMED; 2076 error = cdsetmode(periph, ¶ms); 2077 kfree(params.mode_buf, M_SCSICD); 2078 if (error) { 2079 cam_periph_unlock(periph); 2080 break; 2081 } 2082 error = cdplay(periph, args->blk, args->len); 2083 cam_periph_unlock(periph); 2084 } 2085 break; 2086 case CDIOCREADSUBCHANNEL: 2087 { 2088 struct ioc_read_subchannel *args 2089 = (struct ioc_read_subchannel *) addr; 2090 struct cd_sub_channel_info *data; 2091 u_int32_t len = args->data_len; 2092 2093 data = kmalloc(sizeof(struct cd_sub_channel_info), 2094 M_SCSICD, M_WAITOK); 2095 2096 cam_periph_lock(periph); 2097 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2098 ("trying to do CDIOCREADSUBCHANNEL\n")); 2099 2100 if ((len > sizeof(struct cd_sub_channel_info)) || 2101 (len < sizeof(struct cd_sub_channel_header))) { 2102 kprintf( 2103 "scsi_cd: cdioctl: " 2104 "cdioreadsubchannel: error, len=%d\n", 2105 len); 2106 error = EINVAL; 2107 kfree(data, M_SCSICD); 2108 cam_periph_unlock(periph); 2109 break; 2110 } 2111 2112 if (softc->quirks & CD_Q_BCD_TRACKS) 2113 args->track = bin2bcd(args->track); 2114 2115 error = cdreadsubchannel(periph, args->address_format, 2116 args->data_format, args->track, data, len); 2117 2118 if (error) { 2119 kfree(data, M_SCSICD); 2120 cam_periph_unlock(periph); 2121 break; 2122 } 2123 if (softc->quirks & CD_Q_BCD_TRACKS) 2124 data->what.track_info.track_number = 2125 bcd2bin(data->what.track_info.track_number); 2126 len = min(len, ((data->header.data_len[0] << 8) + 2127 data->header.data_len[1] + 2128 sizeof(struct cd_sub_channel_header))); 2129 cam_periph_unlock(periph); 2130 if (copyout(data, args->data, len) != 0) { 2131 error = EFAULT; 2132 } 2133 kfree(data, M_SCSICD); 2134 } 2135 break; 2136 2137 case CDIOREADTOCHEADER: 2138 { 2139 struct ioc_toc_header *th; 2140 2141 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD, 2142 M_WAITOK); 2143 2144 cam_periph_lock(periph); 2145 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2146 ("trying to do CDIOREADTOCHEADER\n")); 2147 2148 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2149 sizeof (*th), /*sense_flags*/0); 2150 if (error) { 2151 kfree(th, M_SCSICD); 2152 cam_periph_unlock(periph); 2153 break; 2154 } 2155 if (softc->quirks & CD_Q_BCD_TRACKS) { 2156 /* we are going to have to convert the BCD 2157 * encoding on the cd to what is expected 2158 */ 2159 th->starting_track = 2160 bcd2bin(th->starting_track); 2161 th->ending_track = bcd2bin(th->ending_track); 2162 } 2163 th->len = ntohs(th->len); 2164 bcopy(th, addr, sizeof(*th)); 2165 kfree(th, M_SCSICD); 2166 cam_periph_unlock(periph); 2167 } 2168 break; 2169 case CDIOREADTOCENTRYS: 2170 { 2171 struct cd_tocdata *data; 2172 struct cd_toc_single *lead; 2173 struct ioc_read_toc_entry *te = 2174 (struct ioc_read_toc_entry *) addr; 2175 struct ioc_toc_header *th; 2176 u_int32_t len, readlen, idx, num; 2177 u_int32_t starting_track = te->starting_track; 2178 2179 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK); 2180 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK); 2181 2182 cam_periph_lock(periph); 2183 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2184 ("trying to do CDIOREADTOCENTRYS\n")); 2185 2186 if (te->data_len < sizeof(struct cd_toc_entry) 2187 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2188 || (te->address_format != CD_MSF_FORMAT 2189 && te->address_format != CD_LBA_FORMAT)) { 2190 error = EINVAL; 2191 kprintf("scsi_cd: error in readtocentries, " 2192 "returning EINVAL\n"); 2193 kfree(data, M_SCSICD); 2194 kfree(lead, M_SCSICD); 2195 cam_periph_unlock(periph); 2196 break; 2197 } 2198 2199 th = &data->header; 2200 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2201 sizeof (*th), /*sense_flags*/0); 2202 if (error) { 2203 kfree(data, M_SCSICD); 2204 kfree(lead, M_SCSICD); 2205 cam_periph_unlock(periph); 2206 break; 2207 } 2208 2209 if (softc->quirks & CD_Q_BCD_TRACKS) { 2210 /* we are going to have to convert the BCD 2211 * encoding on the cd to what is expected 2212 */ 2213 th->starting_track = 2214 bcd2bin(th->starting_track); 2215 th->ending_track = bcd2bin(th->ending_track); 2216 } 2217 2218 if (starting_track == 0) 2219 starting_track = th->starting_track; 2220 else if (starting_track == LEADOUT) 2221 starting_track = th->ending_track + 1; 2222 else if (starting_track < th->starting_track || 2223 starting_track > th->ending_track + 1) { 2224 kprintf("scsi_cd: error in readtocentries, " 2225 "returning EINVAL\n"); 2226 kfree(data, M_SCSICD); 2227 kfree(lead, M_SCSICD); 2228 cam_periph_unlock(periph); 2229 error = EINVAL; 2230 break; 2231 } 2232 2233 /* calculate reading length without leadout entry */ 2234 readlen = (th->ending_track - starting_track + 1) * 2235 sizeof(struct cd_toc_entry); 2236 2237 /* and with leadout entry */ 2238 len = readlen + sizeof(struct cd_toc_entry); 2239 if (te->data_len < len) { 2240 len = te->data_len; 2241 if (readlen > len) 2242 readlen = len; 2243 } 2244 if (len > sizeof(data->entries)) { 2245 kprintf("scsi_cd: error in readtocentries, " 2246 "returning EINVAL\n"); 2247 error = EINVAL; 2248 kfree(data, M_SCSICD); 2249 kfree(lead, M_SCSICD); 2250 cam_periph_unlock(periph); 2251 break; 2252 } 2253 num = len / sizeof(struct cd_toc_entry); 2254 2255 if (readlen > 0) { 2256 error = cdreadtoc(periph, te->address_format, 2257 starting_track, 2258 (u_int8_t *)data, 2259 readlen + sizeof (*th), 2260 /*sense_flags*/0); 2261 if (error) { 2262 kfree(data, M_SCSICD); 2263 kfree(lead, M_SCSICD); 2264 cam_periph_unlock(periph); 2265 break; 2266 } 2267 } 2268 2269 /* make leadout entry if needed */ 2270 idx = starting_track + num - 1; 2271 if (softc->quirks & CD_Q_BCD_TRACKS) 2272 th->ending_track = bcd2bin(th->ending_track); 2273 if (idx == th->ending_track + 1) { 2274 error = cdreadtoc(periph, te->address_format, 2275 LEADOUT, (u_int8_t *)lead, 2276 sizeof(*lead), 2277 /*sense_flags*/0); 2278 if (error) { 2279 kfree(data, M_SCSICD); 2280 kfree(lead, M_SCSICD); 2281 cam_periph_unlock(periph); 2282 break; 2283 } 2284 data->entries[idx - starting_track] = 2285 lead->entry; 2286 } 2287 if (softc->quirks & CD_Q_BCD_TRACKS) { 2288 for (idx = 0; idx < num - 1; idx++) { 2289 data->entries[idx].track = 2290 bcd2bin(data->entries[idx].track); 2291 } 2292 } 2293 2294 cam_periph_unlock(periph); 2295 error = copyout(data->entries, te->data, len); 2296 kfree(data, M_SCSICD); 2297 kfree(lead, M_SCSICD); 2298 } 2299 break; 2300 case CDIOREADTOCENTRY: 2301 { 2302 struct cd_toc_single *data; 2303 struct ioc_read_toc_single_entry *te = 2304 (struct ioc_read_toc_single_entry *) addr; 2305 struct ioc_toc_header *th; 2306 u_int32_t track; 2307 2308 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK); 2309 2310 cam_periph_lock(periph); 2311 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2312 ("trying to do CDIOREADTOCENTRY\n")); 2313 2314 if (te->address_format != CD_MSF_FORMAT 2315 && te->address_format != CD_LBA_FORMAT) { 2316 kprintf("error in readtocentry, " 2317 " returning EINVAL\n"); 2318 kfree(data, M_SCSICD); 2319 error = EINVAL; 2320 cam_periph_unlock(periph); 2321 break; 2322 } 2323 2324 th = &data->header; 2325 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2326 sizeof (*th), /*sense_flags*/0); 2327 if (error) { 2328 kfree(data, M_SCSICD); 2329 cam_periph_unlock(periph); 2330 break; 2331 } 2332 2333 if (softc->quirks & CD_Q_BCD_TRACKS) { 2334 /* we are going to have to convert the BCD 2335 * encoding on the cd to what is expected 2336 */ 2337 th->starting_track = 2338 bcd2bin(th->starting_track); 2339 th->ending_track = bcd2bin(th->ending_track); 2340 } 2341 track = te->track; 2342 if (track == 0) 2343 track = th->starting_track; 2344 else if (track == LEADOUT) 2345 /* OK */; 2346 else if (track < th->starting_track || 2347 track > th->ending_track + 1) { 2348 kprintf("error in readtocentry, " 2349 " returning EINVAL\n"); 2350 kfree(data, M_SCSICD); 2351 error = EINVAL; 2352 cam_periph_unlock(periph); 2353 break; 2354 } 2355 2356 error = cdreadtoc(periph, te->address_format, track, 2357 (u_int8_t *)data, sizeof(*data), 2358 /*sense_flags*/0); 2359 if (error) { 2360 kfree(data, M_SCSICD); 2361 cam_periph_unlock(periph); 2362 break; 2363 } 2364 2365 if (softc->quirks & CD_Q_BCD_TRACKS) 2366 data->entry.track = bcd2bin(data->entry.track); 2367 bcopy(&data->entry, &te->entry, 2368 sizeof(struct cd_toc_entry)); 2369 kfree(data, M_SCSICD); 2370 cam_periph_unlock(periph); 2371 } 2372 break; 2373 case CDIOCSETPATCH: 2374 { 2375 struct ioc_patch *arg = (struct ioc_patch *)addr; 2376 struct cd_mode_params params; 2377 union cd_pages *page; 2378 2379 params.alloc_len = sizeof(union cd_mode_data_6_10); 2380 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2381 M_WAITOK | M_ZERO); 2382 2383 cam_periph_lock(periph); 2384 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2385 ("trying to do CDIOCSETPATCH\n")); 2386 2387 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2388 if (error) { 2389 kfree(params.mode_buf, M_SCSICD); 2390 cam_periph_unlock(periph); 2391 break; 2392 } 2393 page = cdgetpage(¶ms); 2394 2395 page->audio.port[LEFT_PORT].channels = 2396 arg->patch[0]; 2397 page->audio.port[RIGHT_PORT].channels = 2398 arg->patch[1]; 2399 page->audio.port[2].channels = arg->patch[2]; 2400 page->audio.port[3].channels = arg->patch[3]; 2401 error = cdsetmode(periph, ¶ms); 2402 kfree(params.mode_buf, M_SCSICD); 2403 cam_periph_unlock(periph); 2404 } 2405 break; 2406 case CDIOCGETVOL: 2407 { 2408 struct ioc_vol *arg = (struct ioc_vol *) addr; 2409 struct cd_mode_params params; 2410 union cd_pages *page; 2411 2412 params.alloc_len = sizeof(union cd_mode_data_6_10); 2413 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2414 M_WAITOK | M_ZERO); 2415 2416 cam_periph_lock(periph); 2417 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2418 ("trying to do CDIOCGETVOL\n")); 2419 2420 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2421 if (error) { 2422 kfree(params.mode_buf, M_SCSICD); 2423 cam_periph_unlock(periph); 2424 break; 2425 } 2426 page = cdgetpage(¶ms); 2427 2428 arg->vol[LEFT_PORT] = 2429 page->audio.port[LEFT_PORT].volume; 2430 arg->vol[RIGHT_PORT] = 2431 page->audio.port[RIGHT_PORT].volume; 2432 arg->vol[2] = page->audio.port[2].volume; 2433 arg->vol[3] = page->audio.port[3].volume; 2434 kfree(params.mode_buf, M_SCSICD); 2435 cam_periph_unlock(periph); 2436 } 2437 break; 2438 case CDIOCSETVOL: 2439 { 2440 struct ioc_vol *arg = (struct ioc_vol *) addr; 2441 struct cd_mode_params params; 2442 union cd_pages *page; 2443 2444 params.alloc_len = sizeof(union cd_mode_data_6_10); 2445 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2446 M_WAITOK | M_ZERO); 2447 2448 cam_periph_lock(periph); 2449 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2450 ("trying to do CDIOCSETVOL\n")); 2451 2452 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2453 if (error) { 2454 kfree(params.mode_buf, M_SCSICD); 2455 cam_periph_unlock(periph); 2456 break; 2457 } 2458 page = cdgetpage(¶ms); 2459 2460 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2461 page->audio.port[LEFT_PORT].volume = 2462 arg->vol[LEFT_PORT]; 2463 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2464 page->audio.port[RIGHT_PORT].volume = 2465 arg->vol[RIGHT_PORT]; 2466 page->audio.port[2].volume = arg->vol[2]; 2467 page->audio.port[3].volume = arg->vol[3]; 2468 error = cdsetmode(periph, ¶ms); 2469 cam_periph_unlock(periph); 2470 kfree(params.mode_buf, M_SCSICD); 2471 } 2472 break; 2473 case CDIOCSETMONO: 2474 { 2475 struct cd_mode_params params; 2476 union cd_pages *page; 2477 2478 params.alloc_len = sizeof(union cd_mode_data_6_10); 2479 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2480 M_WAITOK | M_ZERO); 2481 2482 cam_periph_lock(periph); 2483 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2484 ("trying to do CDIOCSETMONO\n")); 2485 2486 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2487 if (error) { 2488 kfree(params.mode_buf, M_SCSICD); 2489 cam_periph_unlock(periph); 2490 break; 2491 } 2492 page = cdgetpage(¶ms); 2493 2494 page->audio.port[LEFT_PORT].channels = 2495 LEFT_CHANNEL | RIGHT_CHANNEL; 2496 page->audio.port[RIGHT_PORT].channels = 2497 LEFT_CHANNEL | RIGHT_CHANNEL; 2498 page->audio.port[2].channels = 0; 2499 page->audio.port[3].channels = 0; 2500 error = cdsetmode(periph, ¶ms); 2501 cam_periph_unlock(periph); 2502 kfree(params.mode_buf, M_SCSICD); 2503 } 2504 break; 2505 case CDIOCSETSTEREO: 2506 { 2507 struct cd_mode_params params; 2508 union cd_pages *page; 2509 2510 params.alloc_len = sizeof(union cd_mode_data_6_10); 2511 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2512 M_WAITOK | M_ZERO); 2513 2514 cam_periph_lock(periph); 2515 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2516 ("trying to do CDIOCSETSTEREO\n")); 2517 2518 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2519 if (error) { 2520 kfree(params.mode_buf, M_SCSICD); 2521 cam_periph_unlock(periph); 2522 break; 2523 } 2524 page = cdgetpage(¶ms); 2525 2526 page->audio.port[LEFT_PORT].channels = 2527 LEFT_CHANNEL; 2528 page->audio.port[RIGHT_PORT].channels = 2529 RIGHT_CHANNEL; 2530 page->audio.port[2].channels = 0; 2531 page->audio.port[3].channels = 0; 2532 error = cdsetmode(periph, ¶ms); 2533 kfree(params.mode_buf, M_SCSICD); 2534 cam_periph_unlock(periph); 2535 } 2536 break; 2537 case CDIOCSETMUTE: 2538 { 2539 struct cd_mode_params params; 2540 union cd_pages *page; 2541 2542 params.alloc_len = sizeof(union cd_mode_data_6_10); 2543 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2544 M_WAITOK | M_ZERO); 2545 2546 cam_periph_lock(periph); 2547 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2548 ("trying to do CDIOCSETMUTE\n")); 2549 2550 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2551 if (error) { 2552 kfree(¶ms, M_SCSICD); 2553 cam_periph_unlock(periph); 2554 break; 2555 } 2556 page = cdgetpage(¶ms); 2557 2558 page->audio.port[LEFT_PORT].channels = 0; 2559 page->audio.port[RIGHT_PORT].channels = 0; 2560 page->audio.port[2].channels = 0; 2561 page->audio.port[3].channels = 0; 2562 error = cdsetmode(periph, ¶ms); 2563 kfree(params.mode_buf, M_SCSICD); 2564 cam_periph_unlock(periph); 2565 } 2566 break; 2567 case CDIOCSETLEFT: 2568 { 2569 struct cd_mode_params params; 2570 union cd_pages *page; 2571 2572 params.alloc_len = sizeof(union cd_mode_data_6_10); 2573 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2574 M_WAITOK | M_ZERO); 2575 2576 cam_periph_lock(periph); 2577 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2578 ("trying to do CDIOCSETLEFT\n")); 2579 2580 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2581 if (error) { 2582 kfree(params.mode_buf, M_SCSICD); 2583 cam_periph_unlock(periph); 2584 break; 2585 } 2586 page = cdgetpage(¶ms); 2587 2588 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2589 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2590 page->audio.port[2].channels = 0; 2591 page->audio.port[3].channels = 0; 2592 error = cdsetmode(periph, ¶ms); 2593 kfree(params.mode_buf, M_SCSICD); 2594 cam_periph_unlock(periph); 2595 } 2596 break; 2597 case CDIOCSETRIGHT: 2598 { 2599 struct cd_mode_params params; 2600 union cd_pages *page; 2601 2602 params.alloc_len = sizeof(union cd_mode_data_6_10); 2603 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD, 2604 M_WAITOK | M_ZERO); 2605 2606 cam_periph_lock(periph); 2607 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2608 ("trying to do CDIOCSETRIGHT\n")); 2609 2610 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2611 if (error) { 2612 kfree(params.mode_buf, M_SCSICD); 2613 cam_periph_unlock(periph); 2614 break; 2615 } 2616 page = cdgetpage(¶ms); 2617 2618 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2619 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2620 page->audio.port[2].channels = 0; 2621 page->audio.port[3].channels = 0; 2622 error = cdsetmode(periph, ¶ms); 2623 kfree(params.mode_buf, M_SCSICD); 2624 cam_periph_unlock(periph); 2625 } 2626 break; 2627 case CDIOCRESUME: 2628 cam_periph_lock(periph); 2629 error = cdpause(periph, 1); 2630 cam_periph_unlock(periph); 2631 break; 2632 case CDIOCPAUSE: 2633 cam_periph_lock(periph); 2634 error = cdpause(periph, 0); 2635 cam_periph_unlock(periph); 2636 break; 2637 case CDIOCSTART: 2638 cam_periph_lock(periph); 2639 error = cdstartunit(periph, 0); 2640 cam_periph_unlock(periph); 2641 break; 2642 case CDIOCCLOSE: 2643 cam_periph_lock(periph); 2644 error = cdstartunit(periph, 1); 2645 cam_periph_unlock(periph); 2646 break; 2647 case CDIOCSTOP: 2648 cam_periph_lock(periph); 2649 error = cdstopunit(periph, 0); 2650 cam_periph_unlock(periph); 2651 break; 2652 case CDIOCEJECT: 2653 cam_periph_lock(periph); 2654 error = cdstopunit(periph, 1); 2655 cam_periph_unlock(periph); 2656 break; 2657 case CDIOCALLOW: 2658 cam_periph_lock(periph); 2659 cdprevent(periph, PR_ALLOW); 2660 cam_periph_unlock(periph); 2661 break; 2662 case CDIOCPREVENT: 2663 cam_periph_lock(periph); 2664 cdprevent(periph, PR_PREVENT); 2665 cam_periph_unlock(periph); 2666 break; 2667 case CDIOCSETDEBUG: 2668 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2669 error = ENOTTY; 2670 break; 2671 case CDIOCCLRDEBUG: 2672 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2673 error = ENOTTY; 2674 break; 2675 case CDIOCRESET: 2676 /* return (cd_reset(periph)); */ 2677 error = ENOTTY; 2678 break; 2679 case CDRIOCREADSPEED: 2680 cam_periph_lock(periph); 2681 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2682 cam_periph_unlock(periph); 2683 break; 2684 case CDRIOCWRITESPEED: 2685 cam_periph_lock(periph); 2686 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2687 cam_periph_unlock(periph); 2688 break; 2689 case DVDIOCSENDKEY: 2690 case DVDIOCREPORTKEY: { 2691 struct dvd_authinfo *authinfo; 2692 2693 authinfo = (struct dvd_authinfo *)addr; 2694 2695 cam_periph_lock(periph); 2696 if (ap->a_cmd == DVDIOCREPORTKEY) 2697 error = cdreportkey(periph, authinfo); 2698 else 2699 error = cdsendkey(periph, authinfo); 2700 cam_periph_unlock(periph); 2701 break; 2702 } 2703 case DVDIOCREADSTRUCTURE: { 2704 struct dvd_struct *dvdstruct; 2705 2706 dvdstruct = (struct dvd_struct *)addr; 2707 2708 cam_periph_lock(periph); 2709 error = cdreaddvdstructure(periph, dvdstruct); 2710 cam_periph_unlock(periph); 2711 2712 break; 2713 } 2714 default: 2715 cam_periph_lock(periph); 2716 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror); 2717 cam_periph_unlock(periph); 2718 break; 2719 } 2720 2721 cam_periph_lock(periph); 2722 cam_periph_unhold(periph); 2723 2724 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2725 2726 cam_periph_unlock(periph); 2727 2728 return (error); 2729 } 2730 2731 static void 2732 cdprevent(struct cam_periph *periph, int action) 2733 { 2734 union ccb *ccb; 2735 struct cd_softc *softc; 2736 int error; 2737 2738 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2739 2740 softc = (struct cd_softc *)periph->softc; 2741 2742 if (((action == PR_ALLOW) 2743 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2744 || ((action == PR_PREVENT) 2745 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2746 return; 2747 } 2748 2749 ccb = cdgetccb(periph, /* priority */ 1); 2750 2751 scsi_prevent(&ccb->csio, 2752 /*retries*/ 1, 2753 cddone, 2754 MSG_SIMPLE_Q_TAG, 2755 action, 2756 SSD_FULL_SIZE, 2757 /* timeout */60000); 2758 2759 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2760 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2761 2762 xpt_release_ccb(ccb); 2763 2764 if (error == 0) { 2765 if (action == PR_ALLOW) 2766 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2767 else 2768 softc->flags |= CD_FLAG_DISC_LOCKED; 2769 } 2770 } 2771 2772 int 2773 cdcheckmedia(struct cam_periph *periph) 2774 { 2775 struct cd_softc *softc; 2776 struct ioc_toc_header *toch; 2777 struct cd_toc_single leadout; 2778 struct ccb_getdev cgd; 2779 u_int32_t size, toclen; 2780 int error, num_entries, cdindex; 2781 int first_track_audio; 2782 struct disk_info info; 2783 2784 softc = (struct cd_softc *)periph->softc; 2785 2786 first_track_audio = -1; 2787 error = 0; 2788 2789 cdprevent(periph, PR_PREVENT); 2790 2791 /* 2792 * Set up disk info fields and tell the disk subsystem to reset 2793 * its internal copy of the label. 2794 */ 2795 bzero(&info, sizeof(info)); 2796 info.d_type = DTYPE_SCSI; 2797 info.d_dsflags &= ~DSO_COMPATLABEL; 2798 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA; 2799 2800 /* 2801 * Grab the inquiry data to get the vendor and product names. 2802 * Put them in the typename and packname for the label. 2803 */ 2804 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); 2805 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2806 xpt_action((union ccb *)&cgd); 2807 2808 #if 0 2809 strncpy(label->d_typename, cgd.inq_data.vendor, 2810 min(SID_VENDOR_SIZE, sizeof(label->d_typename))); 2811 strncpy(label->d_packname, cgd.inq_data.product, 2812 min(SID_PRODUCT_SIZE, sizeof(label->d_packname))); 2813 #endif 2814 /* 2815 * Clear the valid media and TOC flags until we've verified that we 2816 * have both. 2817 */ 2818 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2819 2820 /* 2821 * Get the disc size and block size. If we can't get it, we don't 2822 * have media, most likely. 2823 */ 2824 if ((error = cdsize(periph, &size)) != 0) { 2825 /* 2826 * Set a bogus sector size, so the slice code won't try to 2827 * divide by 0 and panic the kernel. 2828 */ 2829 info.d_media_blksize = 2048; 2830 disk_setdiskinfo(&softc->disk, &info); 2831 2832 /* 2833 * Tell devstat(9) we don't have a blocksize. 2834 */ 2835 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; 2836 2837 cdprevent(periph, PR_ALLOW); 2838 2839 return (error); 2840 } else { 2841 info.d_media_blksize = softc->params.blksize; 2842 info.d_media_blocks = softc->params.disksize; 2843 disk_setdiskinfo(&softc->disk, &info); 2844 2845 /* 2846 * We unconditionally (re)set the blocksize each time the 2847 * CD device is opened. This is because the CD can change, 2848 * and therefore the blocksize might change. 2849 * XXX problems here if some slice or partition is still 2850 * open with the old size? 2851 */ 2852 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2853 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; 2854 softc->device_stats.block_size = softc->params.blksize; 2855 2856 softc->flags |= CD_FLAG_VALID_MEDIA; 2857 } 2858 2859 /* 2860 * Now we check the table of contents. This (currently) is only 2861 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2862 * things like present a separate entry in /dev for each track, 2863 * like that acd(4) driver does. 2864 */ 2865 bzero(&softc->toc, sizeof(softc->toc)); 2866 toch = &softc->toc.header; 2867 2868 /* 2869 * We will get errors here for media that doesn't have a table of 2870 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2871 * command is presented for a DDCD/CD-R/RW media, where the first TOC 2872 * has not been recorded (no complete session) and the Format codes 2873 * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2874 * with an INVALID FIELD IN CDB. Devices that are not capable of 2875 * reading an incomplete session on DDC/CD-R/RW media shall report 2876 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2877 * 2878 * So this isn't fatal if we can't read the table of contents, it 2879 * just means that the user won't be able to issue the play tracks 2880 * ioctl, and likely lots of other stuff won't work either. They 2881 * need to burn the CD before we can do a whole lot with it. So 2882 * we don't print anything here if we get an error back. 2883 */ 2884 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2885 SF_NO_PRINT); 2886 /* 2887 * Errors in reading the table of contents aren't fatal, we just 2888 * won't have a valid table of contents cached. 2889 */ 2890 if (error != 0) { 2891 error = 0; 2892 bzero(&softc->toc, sizeof(softc->toc)); 2893 goto bailout; 2894 } 2895 2896 if (softc->quirks & CD_Q_BCD_TRACKS) { 2897 toch->starting_track = bcd2bin(toch->starting_track); 2898 toch->ending_track = bcd2bin(toch->ending_track); 2899 } 2900 2901 /* Number of TOC entries, plus leadout */ 2902 num_entries = (toch->ending_track - toch->starting_track) + 2; 2903 2904 if (num_entries <= 0) 2905 goto bailout; 2906 2907 toclen = num_entries * sizeof(struct cd_toc_entry); 2908 2909 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2910 (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2911 SF_NO_PRINT); 2912 if (error != 0) { 2913 error = 0; 2914 bzero(&softc->toc, sizeof(softc->toc)); 2915 goto bailout; 2916 } 2917 2918 if (softc->quirks & CD_Q_BCD_TRACKS) { 2919 toch->starting_track = bcd2bin(toch->starting_track); 2920 toch->ending_track = bcd2bin(toch->ending_track); 2921 } 2922 toch->len = scsi_2btoul((uint8_t *)&toch->len); 2923 2924 /* 2925 * XXX KDM is this necessary? Probably only if the drive doesn't 2926 * return leadout information with the table of contents. 2927 */ 2928 cdindex = toch->starting_track + num_entries -1; 2929 if (cdindex == toch->ending_track + 1) { 2930 2931 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2932 (u_int8_t *)&leadout, sizeof(leadout), 2933 SF_NO_PRINT); 2934 if (error != 0) { 2935 error = 0; 2936 goto bailout; 2937 } 2938 softc->toc.entries[cdindex - toch->starting_track] = 2939 leadout.entry; 2940 } 2941 if (softc->quirks & CD_Q_BCD_TRACKS) { 2942 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) { 2943 softc->toc.entries[cdindex].track = 2944 bcd2bin(softc->toc.entries[cdindex].track); 2945 } 2946 } 2947 2948 /* 2949 * Run through the TOC entries, find the first entry and determine 2950 * whether it is an audio or data track. 2951 */ 2952 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) { 2953 if (softc->toc.entries[cdindex].track == toch->starting_track) { 2954 if (softc->toc.entries[cdindex].control & 0x04) 2955 first_track_audio = 0; 2956 else 2957 first_track_audio = 1; 2958 break; 2959 } 2960 } 2961 2962 /* 2963 * If first_track_audio is non-zero, we either have an error (e.g. 2964 * couldn't find the starting track) or the first track is an audio 2965 * track. If first_track_audio is 0, the first track is a data 2966 * track that could have a disklabel. Attempt to read the 2967 * disklabel off the media, just in case the user put one there. 2968 */ 2969 if (first_track_audio == 0) { 2970 info.d_dsflags |= DSO_COMPATLABEL; 2971 info.d_dsflags &= ~DSO_NOLABELS; 2972 disk_setdiskinfo(&softc->disk, &info); 2973 } 2974 softc->flags |= CD_FLAG_VALID_TOC; 2975 2976 bailout: 2977 return (error); 2978 } 2979 2980 static int 2981 cdsize(struct cam_periph *periph, u_int32_t *size) 2982 { 2983 struct cd_softc *softc; 2984 union ccb *ccb; 2985 struct scsi_read_capacity_data *rcap_buf; 2986 int error; 2987 2988 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2989 2990 softc = (struct cd_softc *)periph->softc; 2991 2992 ccb = cdgetccb(periph, /* priority */ 1); 2993 2994 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data), 2995 M_SCSICD, M_INTWAIT | M_ZERO); 2996 2997 scsi_read_capacity(&ccb->csio, 2998 /*retries*/ 1, 2999 cddone, 3000 MSG_SIMPLE_Q_TAG, 3001 rcap_buf, 3002 SSD_FULL_SIZE, 3003 /* timeout */20000); 3004 3005 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3006 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 3007 3008 xpt_release_ccb(ccb); 3009 3010 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 3011 softc->params.blksize = scsi_4btoul(rcap_buf->length); 3012 /* 3013 * SCSI-3 mandates that the reported blocksize shall be 2048. 3014 * Older drives sometimes report funny values, trim it down to 3015 * 2048, or other parts of the kernel will get confused. 3016 * 3017 * XXX we leave drives alone that might report 512 bytes, as 3018 * well as drives reporting more weird sizes like perhaps 4K. 3019 */ 3020 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 3021 softc->params.blksize = 2048; 3022 3023 kfree(rcap_buf, M_SCSICD); 3024 *size = softc->params.disksize; 3025 3026 return (error); 3027 3028 } 3029 3030 static int 3031 cd6byteworkaround(union ccb *ccb) 3032 { 3033 u_int8_t *cdb; 3034 struct cam_periph *periph; 3035 struct cd_softc *softc; 3036 struct cd_mode_params *params; 3037 int frozen, found; 3038 3039 periph = xpt_path_periph(ccb->ccb_h.path); 3040 softc = (struct cd_softc *)periph->softc; 3041 3042 cdb = ccb->csio.cdb_io.cdb_bytes; 3043 3044 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 3045 || ((cdb[0] != MODE_SENSE_6) 3046 && (cdb[0] != MODE_SELECT_6))) 3047 return (0); 3048 3049 /* 3050 * Because there is no convenient place to stash the overall 3051 * cd_mode_params structure pointer, we have to grab it like this. 3052 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 3053 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 3054 * 3055 * XXX It would be nice if, at some point, we could increase the 3056 * number of available peripheral private pointers. Both pointers 3057 * are currently used in most every peripheral driver. 3058 */ 3059 found = 0; 3060 3061 STAILQ_FOREACH(params, &softc->mode_queue, links) { 3062 if (params->mode_buf == ccb->csio.data_ptr) { 3063 found = 1; 3064 break; 3065 } 3066 } 3067 3068 /* 3069 * This shouldn't happen. All mode sense and mode select 3070 * operations in the cd(4) driver MUST go through cdgetmode() and 3071 * cdsetmode()! 3072 */ 3073 if (found == 0) { 3074 xpt_print(periph->path, 3075 "mode buffer not found in mode queue!\n"); 3076 return (0); 3077 } 3078 3079 params->cdb_size = 10; 3080 softc->minimum_command_size = 10; 3081 xpt_print(ccb->ccb_h.path, 3082 "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3083 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3084 3085 if (cdb[0] == MODE_SENSE_6) { 3086 struct scsi_mode_sense_10 ms10; 3087 struct scsi_mode_sense_6 *ms6; 3088 int len; 3089 3090 ms6 = (struct scsi_mode_sense_6 *)cdb; 3091 3092 bzero(&ms10, sizeof(ms10)); 3093 ms10.opcode = MODE_SENSE_10; 3094 ms10.byte2 = ms6->byte2; 3095 ms10.page = ms6->page; 3096 3097 /* 3098 * 10 byte mode header, block descriptor, 3099 * sizeof(union cd_pages) 3100 */ 3101 len = sizeof(struct cd_mode_data_10); 3102 ccb->csio.dxfer_len = len; 3103 3104 scsi_ulto2b(len, ms10.length); 3105 ms10.control = ms6->control; 3106 bcopy(&ms10, cdb, 10); 3107 ccb->csio.cdb_len = 10; 3108 } else { 3109 struct scsi_mode_select_10 ms10; 3110 struct scsi_mode_select_6 *ms6; 3111 struct scsi_mode_header_6 *header6; 3112 struct scsi_mode_header_10 *header10; 3113 struct scsi_mode_page_header *page_header; 3114 int blk_desc_len, page_num, page_size, len; 3115 3116 ms6 = (struct scsi_mode_select_6 *)cdb; 3117 3118 bzero(&ms10, sizeof(ms10)); 3119 ms10.opcode = MODE_SELECT_10; 3120 ms10.byte2 = ms6->byte2; 3121 3122 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3123 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3124 3125 page_header = find_mode_page_6(header6); 3126 page_num = page_header->page_code; 3127 3128 blk_desc_len = header6->blk_desc_len; 3129 3130 page_size = cdgetpagesize(page_num); 3131 3132 if (page_size != (page_header->page_length + 3133 sizeof(*page_header))) 3134 page_size = page_header->page_length + 3135 sizeof(*page_header); 3136 3137 len = sizeof(*header10) + blk_desc_len + page_size; 3138 3139 len = min(params->alloc_len, len); 3140 3141 /* 3142 * Since the 6 byte parameter header is shorter than the 10 3143 * byte parameter header, we need to copy the actual mode 3144 * page data, and the block descriptor, if any, so things wind 3145 * up in the right place. The regions will overlap, but 3146 * bcopy() does the right thing. 3147 */ 3148 bcopy(params->mode_buf + sizeof(*header6), 3149 params->mode_buf + sizeof(*header10), 3150 len - sizeof(*header10)); 3151 3152 /* Make sure these fields are set correctly. */ 3153 scsi_ulto2b(0, header10->data_length); 3154 header10->medium_type = 0; 3155 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3156 3157 ccb->csio.dxfer_len = len; 3158 3159 scsi_ulto2b(len, ms10.length); 3160 ms10.control = ms6->control; 3161 bcopy(&ms10, cdb, 10); 3162 ccb->csio.cdb_len = 10; 3163 } 3164 3165 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3166 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3167 xpt_action(ccb); 3168 if (frozen) { 3169 cam_release_devq(ccb->ccb_h.path, 3170 /*relsim_flags*/0, 3171 /*openings*/0, 3172 /*timeout*/0, 3173 /*getcount_only*/0); 3174 } 3175 3176 return (ERESTART); 3177 } 3178 3179 static int 3180 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3181 { 3182 struct cd_softc *softc; 3183 struct cam_periph *periph; 3184 int error; 3185 3186 periph = xpt_path_periph(ccb->ccb_h.path); 3187 softc = (struct cd_softc *)periph->softc; 3188 3189 error = 0; 3190 3191 /* 3192 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3193 * CDB comes back with this particular error, try transforming it 3194 * into the 10 byte version. 3195 */ 3196 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3197 error = cd6byteworkaround(ccb); 3198 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3199 CAM_SCSI_STATUS_ERROR) 3200 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3201 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3202 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3203 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3204 int sense_key, error_code, asc, ascq; 3205 3206 scsi_extract_sense(&ccb->csio.sense_data, 3207 &error_code, &sense_key, &asc, &ascq); 3208 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3209 error = cd6byteworkaround(ccb); 3210 } 3211 3212 if (error == ERESTART) 3213 return (error); 3214 3215 /* 3216 * XXX 3217 * Until we have a better way of doing pack validation, 3218 * don't treat UAs as errors. 3219 */ 3220 sense_flags |= SF_RETRY_UA; 3221 return (cam_periph_error(ccb, cam_flags, sense_flags, 3222 &softc->saved_ccb)); 3223 } 3224 3225 /* 3226 * Read table of contents 3227 */ 3228 static int 3229 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3230 u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 3231 { 3232 struct scsi_read_toc *scsi_cmd; 3233 u_int32_t ntoc; 3234 struct ccb_scsiio *csio; 3235 union ccb *ccb; 3236 int error; 3237 3238 ntoc = len; 3239 error = 0; 3240 3241 ccb = cdgetccb(periph, /* priority */ 1); 3242 3243 csio = &ccb->csio; 3244 3245 cam_fill_csio(csio, 3246 /* retries */ 1, 3247 /* cbfcnp */ cddone, 3248 /* flags */ CAM_DIR_IN, 3249 /* tag_action */ MSG_SIMPLE_Q_TAG, 3250 /* data_ptr */ data, 3251 /* dxfer_len */ len, 3252 /* sense_len */ SSD_FULL_SIZE, 3253 sizeof(struct scsi_read_toc), 3254 /* timeout */ 50000); 3255 3256 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 3257 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3258 3259 if (mode == CD_MSF_FORMAT) 3260 scsi_cmd->byte2 |= CD_MSF; 3261 scsi_cmd->from_track = start; 3262 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 3263 scsi_cmd->data_len[0] = (ntoc) >> 8; 3264 scsi_cmd->data_len[1] = (ntoc) & 0xff; 3265 3266 scsi_cmd->op_code = READ_TOC; 3267 3268 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3269 /*sense_flags*/SF_RETRY_UA | sense_flags); 3270 3271 xpt_release_ccb(ccb); 3272 3273 return(error); 3274 } 3275 3276 static int 3277 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 3278 u_int32_t format, int track, 3279 struct cd_sub_channel_info *data, u_int32_t len) 3280 { 3281 struct scsi_read_subchannel *scsi_cmd; 3282 struct ccb_scsiio *csio; 3283 union ccb *ccb; 3284 int error; 3285 3286 error = 0; 3287 3288 ccb = cdgetccb(periph, /* priority */ 1); 3289 3290 csio = &ccb->csio; 3291 3292 cam_fill_csio(csio, 3293 /* retries */ 1, 3294 /* cbfcnp */ cddone, 3295 /* flags */ CAM_DIR_IN, 3296 /* tag_action */ MSG_SIMPLE_Q_TAG, 3297 /* data_ptr */ (u_int8_t *)data, 3298 /* dxfer_len */ len, 3299 /* sense_len */ SSD_FULL_SIZE, 3300 sizeof(struct scsi_read_subchannel), 3301 /* timeout */ 50000); 3302 3303 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 3304 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3305 3306 scsi_cmd->op_code = READ_SUBCHANNEL; 3307 if (mode == CD_MSF_FORMAT) 3308 scsi_cmd->byte1 |= CD_MSF; 3309 scsi_cmd->byte2 = SRS_SUBQ; 3310 scsi_cmd->subchan_format = format; 3311 scsi_cmd->track = track; 3312 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 3313 scsi_cmd->control = 0; 3314 3315 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3316 /*sense_flags*/SF_RETRY_UA); 3317 3318 xpt_release_ccb(ccb); 3319 3320 return(error); 3321 } 3322 3323 /* 3324 * All MODE_SENSE requests in the cd(4) driver MUST go through this 3325 * routine. See comments in cd6byteworkaround() for details. 3326 */ 3327 static int 3328 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3329 u_int32_t page) 3330 { 3331 struct ccb_scsiio *csio; 3332 struct cd_softc *softc; 3333 union ccb *ccb; 3334 int param_len; 3335 int error; 3336 3337 softc = (struct cd_softc *)periph->softc; 3338 3339 ccb = cdgetccb(periph, /* priority */ 1); 3340 3341 csio = &ccb->csio; 3342 3343 data->cdb_size = softc->minimum_command_size; 3344 if (data->cdb_size < 10) 3345 param_len = sizeof(struct cd_mode_data); 3346 else 3347 param_len = sizeof(struct cd_mode_data_10); 3348 3349 /* Don't say we've got more room than we actually allocated */ 3350 param_len = min(param_len, data->alloc_len); 3351 3352 scsi_mode_sense_len(csio, 3353 /* retries */ 1, 3354 /* cbfcnp */ cddone, 3355 /* tag_action */ MSG_SIMPLE_Q_TAG, 3356 /* dbd */ 0, 3357 /* page_code */ SMS_PAGE_CTRL_CURRENT, 3358 /* page */ page, 3359 /* param_buf */ data->mode_buf, 3360 /* param_len */ param_len, 3361 /* minimum_cmd_size */ softc->minimum_command_size, 3362 /* sense_len */ SSD_FULL_SIZE, 3363 /* timeout */ 50000); 3364 3365 /* 3366 * It would be nice not to have to do this, but there's no 3367 * available pointer in the CCB that would allow us to stuff the 3368 * mode params structure in there and retrieve it in 3369 * cd6byteworkaround(), so we can set the cdb size. The cdb size 3370 * lets the caller know what CDB size we ended up using, so they 3371 * can find the actual mode page offset. 3372 */ 3373 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3374 3375 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3376 /*sense_flags*/SF_RETRY_UA); 3377 3378 xpt_release_ccb(ccb); 3379 3380 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3381 3382 /* 3383 * This is a bit of belt-and-suspenders checking, but if we run 3384 * into a situation where the target sends back multiple block 3385 * descriptors, we might not have enough space in the buffer to 3386 * see the whole mode page. Better to return an error than 3387 * potentially access memory beyond our malloced region. 3388 */ 3389 if (error == 0) { 3390 u_int32_t data_len; 3391 3392 if (data->cdb_size == 10) { 3393 struct scsi_mode_header_10 *hdr10; 3394 3395 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3396 data_len = scsi_2btoul(hdr10->data_length); 3397 data_len += sizeof(hdr10->data_length); 3398 } else { 3399 struct scsi_mode_header_6 *hdr6; 3400 3401 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3402 data_len = hdr6->data_length; 3403 data_len += sizeof(hdr6->data_length); 3404 } 3405 3406 /* 3407 * Complain if there is more mode data available than we 3408 * allocated space for. This could potentially happen if 3409 * we miscalculated the page length for some reason, if the 3410 * drive returns multiple block descriptors, or if it sets 3411 * the data length incorrectly. 3412 */ 3413 if (data_len > data->alloc_len) { 3414 xpt_print(periph->path, "allocated modepage %d length " 3415 "%d < returned length %d\n", page, data->alloc_len, 3416 data_len); 3417 error = ENOSPC; 3418 } 3419 } 3420 return (error); 3421 } 3422 3423 /* 3424 * All MODE_SELECT requests in the cd(4) driver MUST go through this 3425 * routine. See comments in cd6byteworkaround() for details. 3426 */ 3427 static int 3428 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 3429 { 3430 struct ccb_scsiio *csio; 3431 struct cd_softc *softc; 3432 union ccb *ccb; 3433 int cdb_size, param_len; 3434 int error; 3435 3436 softc = (struct cd_softc *)periph->softc; 3437 3438 ccb = cdgetccb(periph, /* priority */ 1); 3439 3440 csio = &ccb->csio; 3441 3442 error = 0; 3443 3444 /* 3445 * If the data is formatted for the 10 byte version of the mode 3446 * select parameter list, we need to use the 10 byte CDB. 3447 * Otherwise, we use whatever the stored minimum command size. 3448 */ 3449 if (data->cdb_size == 10) 3450 cdb_size = data->cdb_size; 3451 else 3452 cdb_size = softc->minimum_command_size; 3453 3454 if (cdb_size >= 10) { 3455 struct scsi_mode_header_10 *mode_header; 3456 u_int32_t data_len; 3457 3458 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3459 3460 data_len = scsi_2btoul(mode_header->data_length); 3461 3462 scsi_ulto2b(0, mode_header->data_length); 3463 /* 3464 * SONY drives do not allow a mode select with a medium_type 3465 * value that has just been returned by a mode sense; use a 3466 * medium_type of 0 (Default) instead. 3467 */ 3468 mode_header->medium_type = 0; 3469 3470 /* 3471 * Pass back whatever the drive passed to us, plus the size 3472 * of the data length field. 3473 */ 3474 param_len = data_len + sizeof(mode_header->data_length); 3475 3476 } else { 3477 struct scsi_mode_header_6 *mode_header; 3478 3479 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3480 3481 param_len = mode_header->data_length + 1; 3482 3483 mode_header->data_length = 0; 3484 /* 3485 * SONY drives do not allow a mode select with a medium_type 3486 * value that has just been returned by a mode sense; use a 3487 * medium_type of 0 (Default) instead. 3488 */ 3489 mode_header->medium_type = 0; 3490 } 3491 3492 /* Don't say we've got more room than we actually allocated */ 3493 param_len = min(param_len, data->alloc_len); 3494 3495 scsi_mode_select_len(csio, 3496 /* retries */ 1, 3497 /* cbfcnp */ cddone, 3498 /* tag_action */ MSG_SIMPLE_Q_TAG, 3499 /* scsi_page_fmt */ 1, 3500 /* save_pages */ 0, 3501 /* param_buf */ data->mode_buf, 3502 /* param_len */ param_len, 3503 /* minimum_cmd_size */ cdb_size, 3504 /* sense_len */ SSD_FULL_SIZE, 3505 /* timeout */ 50000); 3506 3507 /* See comments in cdgetmode() and cd6byteworkaround(). */ 3508 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3509 3510 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3511 /*sense_flags*/SF_RETRY_UA); 3512 3513 xpt_release_ccb(ccb); 3514 3515 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3516 3517 return (error); 3518 } 3519 3520 3521 3522 static int 3523 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 3524 { 3525 struct ccb_scsiio *csio; 3526 union ccb *ccb; 3527 int error; 3528 u_int8_t cdb_len; 3529 3530 error = 0; 3531 ccb = cdgetccb(periph, /* priority */ 1); 3532 csio = &ccb->csio; 3533 /* 3534 * Use the smallest possible command to perform the operation. 3535 */ 3536 if ((len & 0xffff0000) == 0) { 3537 /* 3538 * We can fit in a 10 byte cdb. 3539 */ 3540 struct scsi_play_10 *scsi_cmd; 3541 3542 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 3543 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3544 scsi_cmd->op_code = PLAY_10; 3545 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3546 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 3547 cdb_len = sizeof(*scsi_cmd); 3548 } else { 3549 struct scsi_play_12 *scsi_cmd; 3550 3551 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 3552 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3553 scsi_cmd->op_code = PLAY_12; 3554 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3555 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 3556 cdb_len = sizeof(*scsi_cmd); 3557 } 3558 cam_fill_csio(csio, 3559 /*retries*/2, 3560 cddone, 3561 /*flags*/CAM_DIR_NONE, 3562 MSG_SIMPLE_Q_TAG, 3563 /*dataptr*/NULL, 3564 /*datalen*/0, 3565 /*sense_len*/SSD_FULL_SIZE, 3566 cdb_len, 3567 /*timeout*/50 * 1000); 3568 3569 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3570 /*sense_flags*/SF_RETRY_UA); 3571 3572 xpt_release_ccb(ccb); 3573 3574 return(error); 3575 } 3576 3577 static int 3578 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 3579 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 3580 { 3581 struct scsi_play_msf *scsi_cmd; 3582 struct ccb_scsiio *csio; 3583 union ccb *ccb; 3584 int error; 3585 3586 error = 0; 3587 3588 ccb = cdgetccb(periph, /* priority */ 1); 3589 3590 csio = &ccb->csio; 3591 3592 cam_fill_csio(csio, 3593 /* retries */ 1, 3594 /* cbfcnp */ cddone, 3595 /* flags */ CAM_DIR_NONE, 3596 /* tag_action */ MSG_SIMPLE_Q_TAG, 3597 /* data_ptr */ NULL, 3598 /* dxfer_len */ 0, 3599 /* sense_len */ SSD_FULL_SIZE, 3600 sizeof(struct scsi_play_msf), 3601 /* timeout */ 50000); 3602 3603 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 3604 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3605 3606 scsi_cmd->op_code = PLAY_MSF; 3607 scsi_cmd->start_m = startm; 3608 scsi_cmd->start_s = starts; 3609 scsi_cmd->start_f = startf; 3610 scsi_cmd->end_m = endm; 3611 scsi_cmd->end_s = ends; 3612 scsi_cmd->end_f = endf; 3613 3614 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3615 /*sense_flags*/SF_RETRY_UA); 3616 3617 xpt_release_ccb(ccb); 3618 3619 return(error); 3620 } 3621 3622 3623 static int 3624 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 3625 u_int32_t etrack, u_int32_t eindex) 3626 { 3627 struct scsi_play_track *scsi_cmd; 3628 struct ccb_scsiio *csio; 3629 union ccb *ccb; 3630 int error; 3631 3632 error = 0; 3633 3634 ccb = cdgetccb(periph, /* priority */ 1); 3635 3636 csio = &ccb->csio; 3637 3638 cam_fill_csio(csio, 3639 /* retries */ 1, 3640 /* cbfcnp */ cddone, 3641 /* flags */ CAM_DIR_NONE, 3642 /* tag_action */ MSG_SIMPLE_Q_TAG, 3643 /* data_ptr */ NULL, 3644 /* dxfer_len */ 0, 3645 /* sense_len */ SSD_FULL_SIZE, 3646 sizeof(struct scsi_play_track), 3647 /* timeout */ 50000); 3648 3649 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3650 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3651 3652 scsi_cmd->op_code = PLAY_TRACK; 3653 scsi_cmd->start_track = strack; 3654 scsi_cmd->start_index = sindex; 3655 scsi_cmd->end_track = etrack; 3656 scsi_cmd->end_index = eindex; 3657 3658 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3659 /*sense_flags*/SF_RETRY_UA); 3660 3661 xpt_release_ccb(ccb); 3662 3663 return(error); 3664 } 3665 3666 static int 3667 cdpause(struct cam_periph *periph, u_int32_t go) 3668 { 3669 struct scsi_pause *scsi_cmd; 3670 struct ccb_scsiio *csio; 3671 union ccb *ccb; 3672 int error; 3673 3674 error = 0; 3675 3676 ccb = cdgetccb(periph, /* priority */ 1); 3677 3678 csio = &ccb->csio; 3679 3680 cam_fill_csio(csio, 3681 /* retries */ 1, 3682 /* cbfcnp */ cddone, 3683 /* flags */ CAM_DIR_NONE, 3684 /* tag_action */ MSG_SIMPLE_Q_TAG, 3685 /* data_ptr */ NULL, 3686 /* dxfer_len */ 0, 3687 /* sense_len */ SSD_FULL_SIZE, 3688 sizeof(struct scsi_pause), 3689 /* timeout */ 50000); 3690 3691 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3692 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3693 3694 scsi_cmd->op_code = PAUSE; 3695 scsi_cmd->resume = go; 3696 3697 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3698 /*sense_flags*/SF_RETRY_UA); 3699 3700 xpt_release_ccb(ccb); 3701 3702 return(error); 3703 } 3704 3705 static int 3706 cdstartunit(struct cam_periph *periph, int load) 3707 { 3708 union ccb *ccb; 3709 int error; 3710 3711 error = 0; 3712 3713 ccb = cdgetccb(periph, /* priority */ 1); 3714 3715 scsi_start_stop(&ccb->csio, 3716 /* retries */ 1, 3717 /* cbfcnp */ cddone, 3718 /* tag_action */ MSG_SIMPLE_Q_TAG, 3719 /* start */ TRUE, 3720 /* load_eject */ load, 3721 /* immediate */ FALSE, 3722 /* sense_len */ SSD_FULL_SIZE, 3723 /* timeout */ 50000); 3724 3725 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3726 /*sense_flags*/SF_RETRY_UA); 3727 3728 xpt_release_ccb(ccb); 3729 3730 return(error); 3731 } 3732 3733 static int 3734 cdstopunit(struct cam_periph *periph, u_int32_t eject) 3735 { 3736 union ccb *ccb; 3737 int error; 3738 3739 error = 0; 3740 3741 ccb = cdgetccb(periph, /* priority */ 1); 3742 3743 scsi_start_stop(&ccb->csio, 3744 /* retries */ 1, 3745 /* cbfcnp */ cddone, 3746 /* tag_action */ MSG_SIMPLE_Q_TAG, 3747 /* start */ FALSE, 3748 /* load_eject */ eject, 3749 /* immediate */ FALSE, 3750 /* sense_len */ SSD_FULL_SIZE, 3751 /* timeout */ 50000); 3752 3753 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3754 /*sense_flags*/SF_RETRY_UA); 3755 3756 xpt_release_ccb(ccb); 3757 3758 return(error); 3759 } 3760 3761 static int 3762 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3763 { 3764 struct scsi_set_speed *scsi_cmd; 3765 struct ccb_scsiio *csio; 3766 union ccb *ccb; 3767 int error; 3768 3769 error = 0; 3770 ccb = cdgetccb(periph, /* priority */ 1); 3771 csio = &ccb->csio; 3772 3773 /* Preserve old behavior: units in multiples of CDROM speed */ 3774 if (rdspeed < 177) 3775 rdspeed *= 177; 3776 if (wrspeed < 177) 3777 wrspeed *= 177; 3778 3779 cam_fill_csio(csio, 3780 /* retries */ 1, 3781 /* cbfcnp */ cddone, 3782 /* flags */ CAM_DIR_NONE, 3783 /* tag_action */ MSG_SIMPLE_Q_TAG, 3784 /* data_ptr */ NULL, 3785 /* dxfer_len */ 0, 3786 /* sense_len */ SSD_FULL_SIZE, 3787 sizeof(struct scsi_set_speed), 3788 /* timeout */ 50000); 3789 3790 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3791 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3792 3793 scsi_cmd->opcode = SET_CD_SPEED; 3794 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3795 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3796 3797 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3798 /*sense_flags*/SF_RETRY_UA); 3799 3800 xpt_release_ccb(ccb); 3801 3802 return(error); 3803 } 3804 3805 static int 3806 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3807 { 3808 union ccb *ccb; 3809 u_int8_t *databuf; 3810 u_int32_t lba; 3811 int error; 3812 int length; 3813 3814 error = 0; 3815 databuf = NULL; 3816 lba = 0; 3817 3818 ccb = cdgetccb(periph, /* priority */ 1); 3819 3820 switch (authinfo->format) { 3821 case DVD_REPORT_AGID: 3822 length = sizeof(struct scsi_report_key_data_agid); 3823 break; 3824 case DVD_REPORT_CHALLENGE: 3825 length = sizeof(struct scsi_report_key_data_challenge); 3826 break; 3827 case DVD_REPORT_KEY1: 3828 length = sizeof(struct scsi_report_key_data_key1_key2); 3829 break; 3830 case DVD_REPORT_TITLE_KEY: 3831 length = sizeof(struct scsi_report_key_data_title); 3832 /* The lba field is only set for the title key */ 3833 lba = authinfo->lba; 3834 break; 3835 case DVD_REPORT_ASF: 3836 length = sizeof(struct scsi_report_key_data_asf); 3837 break; 3838 case DVD_REPORT_RPC: 3839 length = sizeof(struct scsi_report_key_data_rpc); 3840 break; 3841 case DVD_INVALIDATE_AGID: 3842 length = 0; 3843 break; 3844 default: 3845 error = EINVAL; 3846 goto bailout; 3847 break; /* NOTREACHED */ 3848 } 3849 3850 if (length != 0) { 3851 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO); 3852 } else { 3853 databuf = NULL; 3854 } 3855 3856 3857 scsi_report_key(&ccb->csio, 3858 /* retries */ 1, 3859 /* cbfcnp */ cddone, 3860 /* tag_action */ MSG_SIMPLE_Q_TAG, 3861 /* lba */ lba, 3862 /* agid */ authinfo->agid, 3863 /* key_format */ authinfo->format, 3864 /* data_ptr */ databuf, 3865 /* dxfer_len */ length, 3866 /* sense_len */ SSD_FULL_SIZE, 3867 /* timeout */ 50000); 3868 3869 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3870 /*sense_flags*/SF_RETRY_UA); 3871 3872 if (error != 0) 3873 goto bailout; 3874 3875 if (ccb->csio.resid != 0) { 3876 xpt_print(periph->path, "warning, residual for report key " 3877 "command is %d\n", ccb->csio.resid); 3878 } 3879 3880 switch(authinfo->format) { 3881 case DVD_REPORT_AGID: { 3882 struct scsi_report_key_data_agid *agid_data; 3883 3884 agid_data = (struct scsi_report_key_data_agid *)databuf; 3885 3886 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3887 RKD_AGID_SHIFT; 3888 break; 3889 } 3890 case DVD_REPORT_CHALLENGE: { 3891 struct scsi_report_key_data_challenge *chal_data; 3892 3893 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3894 3895 bcopy(chal_data->challenge_key, authinfo->keychal, 3896 min(sizeof(chal_data->challenge_key), 3897 sizeof(authinfo->keychal))); 3898 break; 3899 } 3900 case DVD_REPORT_KEY1: { 3901 struct scsi_report_key_data_key1_key2 *key1_data; 3902 3903 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3904 3905 bcopy(key1_data->key1, authinfo->keychal, 3906 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3907 break; 3908 } 3909 case DVD_REPORT_TITLE_KEY: { 3910 struct scsi_report_key_data_title *title_data; 3911 3912 title_data = (struct scsi_report_key_data_title *)databuf; 3913 3914 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3915 RKD_TITLE_CPM_SHIFT; 3916 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3917 RKD_TITLE_CP_SEC_SHIFT; 3918 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3919 RKD_TITLE_CMGS_SHIFT; 3920 bcopy(title_data->title_key, authinfo->keychal, 3921 min(sizeof(title_data->title_key), 3922 sizeof(authinfo->keychal))); 3923 break; 3924 } 3925 case DVD_REPORT_ASF: { 3926 struct scsi_report_key_data_asf *asf_data; 3927 3928 asf_data = (struct scsi_report_key_data_asf *)databuf; 3929 3930 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3931 break; 3932 } 3933 case DVD_REPORT_RPC: { 3934 struct scsi_report_key_data_rpc *rpc_data; 3935 3936 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3937 3938 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3939 RKD_RPC_TYPE_SHIFT; 3940 authinfo->vend_rsts = 3941 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3942 RKD_RPC_VENDOR_RESET_SHIFT; 3943 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3944 authinfo->region = rpc_data->region_mask; 3945 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3946 break; 3947 } 3948 case DVD_INVALIDATE_AGID: 3949 break; 3950 default: 3951 /* This should be impossible, since we checked above */ 3952 error = EINVAL; 3953 goto bailout; 3954 break; /* NOTREACHED */ 3955 } 3956 bailout: 3957 if (databuf != NULL) 3958 kfree(databuf, M_DEVBUF); 3959 3960 xpt_release_ccb(ccb); 3961 3962 return(error); 3963 } 3964 3965 static int 3966 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3967 { 3968 union ccb *ccb; 3969 u_int8_t *databuf; 3970 int length; 3971 int error; 3972 3973 error = 0; 3974 databuf = NULL; 3975 3976 ccb = cdgetccb(periph, /* priority */ 1); 3977 3978 switch(authinfo->format) { 3979 case DVD_SEND_CHALLENGE: { 3980 struct scsi_report_key_data_challenge *challenge_data; 3981 3982 length = sizeof(*challenge_data); 3983 3984 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO); 3985 3986 databuf = (u_int8_t *)challenge_data; 3987 3988 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3989 challenge_data->data_len); 3990 3991 bcopy(authinfo->keychal, challenge_data->challenge_key, 3992 min(sizeof(authinfo->keychal), 3993 sizeof(challenge_data->challenge_key))); 3994 break; 3995 } 3996 case DVD_SEND_KEY2: { 3997 struct scsi_report_key_data_key1_key2 *key2_data; 3998 3999 length = sizeof(*key2_data); 4000 4001 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO); 4002 4003 databuf = (u_int8_t *)key2_data; 4004 4005 scsi_ulto2b(length - sizeof(key2_data->data_len), 4006 key2_data->data_len); 4007 4008 bcopy(authinfo->keychal, key2_data->key1, 4009 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 4010 4011 break; 4012 } 4013 case DVD_SEND_RPC: { 4014 struct scsi_send_key_data_rpc *rpc_data; 4015 4016 length = sizeof(*rpc_data); 4017 4018 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO); 4019 4020 databuf = (u_int8_t *)rpc_data; 4021 4022 scsi_ulto2b(length - sizeof(rpc_data->data_len), 4023 rpc_data->data_len); 4024 4025 rpc_data->region_code = authinfo->region; 4026 break; 4027 } 4028 default: 4029 error = EINVAL; 4030 goto bailout; 4031 break; /* NOTREACHED */ 4032 } 4033 4034 scsi_send_key(&ccb->csio, 4035 /* retries */ 1, 4036 /* cbfcnp */ cddone, 4037 /* tag_action */ MSG_SIMPLE_Q_TAG, 4038 /* agid */ authinfo->agid, 4039 /* key_format */ authinfo->format, 4040 /* data_ptr */ databuf, 4041 /* dxfer_len */ length, 4042 /* sense_len */ SSD_FULL_SIZE, 4043 /* timeout */ 50000); 4044 4045 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4046 /*sense_flags*/SF_RETRY_UA); 4047 4048 bailout: 4049 4050 if (databuf != NULL) 4051 kfree(databuf, M_DEVBUF); 4052 4053 xpt_release_ccb(ccb); 4054 4055 return(error); 4056 } 4057 4058 static int 4059 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 4060 { 4061 union ccb *ccb; 4062 u_int8_t *databuf; 4063 u_int32_t address; 4064 int error; 4065 int length; 4066 4067 error = 0; 4068 databuf = NULL; 4069 /* The address is reserved for many of the formats */ 4070 address = 0; 4071 4072 ccb = cdgetccb(periph, /* priority */ 1); 4073 4074 switch(dvdstruct->format) { 4075 case DVD_STRUCT_PHYSICAL: 4076 length = sizeof(struct scsi_read_dvd_struct_data_physical); 4077 break; 4078 case DVD_STRUCT_COPYRIGHT: 4079 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 4080 break; 4081 case DVD_STRUCT_DISCKEY: 4082 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 4083 break; 4084 case DVD_STRUCT_BCA: 4085 length = sizeof(struct scsi_read_dvd_struct_data_bca); 4086 break; 4087 case DVD_STRUCT_MANUFACT: 4088 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 4089 break; 4090 case DVD_STRUCT_CMI: 4091 error = ENODEV; 4092 goto bailout; 4093 #ifdef notyet 4094 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage); 4095 address = dvdstruct->address; 4096 #endif 4097 break; /* NOTREACHED */ 4098 case DVD_STRUCT_PROTDISCID: 4099 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 4100 break; 4101 case DVD_STRUCT_DISCKEYBLOCK: 4102 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 4103 break; 4104 case DVD_STRUCT_DDS: 4105 length = sizeof(struct scsi_read_dvd_struct_data_dds); 4106 break; 4107 case DVD_STRUCT_MEDIUM_STAT: 4108 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 4109 break; 4110 case DVD_STRUCT_SPARE_AREA: 4111 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 4112 break; 4113 case DVD_STRUCT_RMD_LAST: 4114 error = ENODEV; 4115 goto bailout; 4116 #ifdef notyet 4117 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout); 4118 address = dvdstruct->address; 4119 #endif 4120 break; /* NOTREACHED */ 4121 case DVD_STRUCT_RMD_RMA: 4122 error = ENODEV; 4123 goto bailout; 4124 #ifdef notyet 4125 length = sizeof(struct scsi_read_dvd_struct_data_rmd); 4126 address = dvdstruct->address; 4127 #endif 4128 break; /* NOTREACHED */ 4129 case DVD_STRUCT_PRERECORDED: 4130 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 4131 break; 4132 case DVD_STRUCT_UNIQUEID: 4133 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 4134 break; 4135 case DVD_STRUCT_DCB: 4136 error = ENODEV; 4137 goto bailout; 4138 #ifdef notyet 4139 length = sizeof(struct scsi_read_dvd_struct_data_dcb); 4140 address = dvdstruct->address; 4141 #endif 4142 break; /* NOTREACHED */ 4143 case DVD_STRUCT_LIST: 4144 /* 4145 * This is the maximum allocation length for the READ DVD 4146 * STRUCTURE command. There's nothing in the MMC3 spec 4147 * that indicates a limit in the amount of data that can 4148 * be returned from this call, other than the limits 4149 * imposed by the 2-byte length variables. 4150 */ 4151 length = 65535; 4152 break; 4153 default: 4154 error = EINVAL; 4155 goto bailout; 4156 break; /* NOTREACHED */ 4157 } 4158 4159 if (length != 0) { 4160 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO); 4161 } else { 4162 databuf = NULL; 4163 } 4164 4165 scsi_read_dvd_structure(&ccb->csio, 4166 /* retries */ 1, 4167 /* cbfcnp */ cddone, 4168 /* tag_action */ MSG_SIMPLE_Q_TAG, 4169 /* lba */ address, 4170 /* layer_number */ dvdstruct->layer_num, 4171 /* key_format */ dvdstruct->format, 4172 /* agid */ dvdstruct->agid, 4173 /* data_ptr */ databuf, 4174 /* dxfer_len */ length, 4175 /* sense_len */ SSD_FULL_SIZE, 4176 /* timeout */ 50000); 4177 4178 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4179 /*sense_flags*/SF_RETRY_UA); 4180 4181 if (error != 0) 4182 goto bailout; 4183 4184 switch(dvdstruct->format) { 4185 case DVD_STRUCT_PHYSICAL: { 4186 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 4187 struct dvd_layer *outlayer; 4188 struct scsi_read_dvd_struct_data_physical *phys_data; 4189 4190 phys_data = 4191 (struct scsi_read_dvd_struct_data_physical *)databuf; 4192 inlayer = &phys_data->layer_desc; 4193 outlayer = (struct dvd_layer *)&dvdstruct->data; 4194 4195 dvdstruct->length = sizeof(*inlayer); 4196 4197 outlayer->book_type = (inlayer->book_type_version & 4198 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 4199 outlayer->book_version = (inlayer->book_type_version & 4200 RDSD_BOOK_VERSION_MASK); 4201 outlayer->disc_size = (inlayer->disc_size_max_rate & 4202 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 4203 outlayer->max_rate = (inlayer->disc_size_max_rate & 4204 RDSD_MAX_RATE_MASK); 4205 outlayer->nlayers = (inlayer->layer_info & 4206 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 4207 outlayer->track_path = (inlayer->layer_info & 4208 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 4209 outlayer->layer_type = (inlayer->layer_info & 4210 RDSD_LAYER_TYPE_MASK); 4211 outlayer->linear_density = (inlayer->density & 4212 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 4213 outlayer->track_density = (inlayer->density & 4214 RDSD_TRACK_DENSITY_MASK); 4215 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 4216 RDSD_BCA_SHIFT; 4217 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 4218 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 4219 outlayer->end_sector_l0 = 4220 scsi_3btoul(inlayer->end_sector_layer0); 4221 break; 4222 } 4223 case DVD_STRUCT_COPYRIGHT: { 4224 struct scsi_read_dvd_struct_data_copyright *copy_data; 4225 4226 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 4227 databuf; 4228 4229 dvdstruct->cpst = copy_data->cps_type; 4230 dvdstruct->rmi = copy_data->region_info; 4231 dvdstruct->length = 0; 4232 4233 break; 4234 } 4235 default: 4236 /* 4237 * Tell the user what the overall length is, no matter 4238 * what we can actually fit in the data buffer. 4239 */ 4240 dvdstruct->length = length - ccb->csio.resid - 4241 sizeof(struct scsi_read_dvd_struct_data_header); 4242 4243 /* 4244 * But only actually copy out the smaller of what we read 4245 * in or what the structure can take. 4246 */ 4247 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 4248 dvdstruct->data, 4249 min(sizeof(dvdstruct->data), dvdstruct->length)); 4250 break; 4251 } 4252 bailout: 4253 4254 if (databuf != NULL) 4255 kfree(databuf, M_DEVBUF); 4256 4257 xpt_release_ccb(ccb); 4258 4259 return(error); 4260 } 4261 4262 void 4263 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 4264 void (*cbfcnp)(struct cam_periph *, union ccb *), 4265 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 4266 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 4267 u_int8_t sense_len, u_int32_t timeout) 4268 { 4269 struct scsi_report_key *scsi_cmd; 4270 4271 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 4272 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4273 scsi_cmd->opcode = REPORT_KEY; 4274 scsi_ulto4b(lba, scsi_cmd->lba); 4275 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4276 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4277 (key_format & RK_KF_KEYFORMAT_MASK); 4278 4279 cam_fill_csio(csio, 4280 retries, 4281 cbfcnp, 4282 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 4283 tag_action, 4284 /*data_ptr*/ data_ptr, 4285 /*dxfer_len*/ dxfer_len, 4286 sense_len, 4287 sizeof(*scsi_cmd), 4288 timeout); 4289 } 4290 4291 void 4292 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 4293 void (*cbfcnp)(struct cam_periph *, union ccb *), 4294 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 4295 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 4296 u_int32_t timeout) 4297 { 4298 struct scsi_send_key *scsi_cmd; 4299 4300 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 4301 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4302 scsi_cmd->opcode = SEND_KEY; 4303 4304 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 4305 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4306 (key_format & RK_KF_KEYFORMAT_MASK); 4307 4308 cam_fill_csio(csio, 4309 retries, 4310 cbfcnp, 4311 /*flags*/ CAM_DIR_OUT, 4312 tag_action, 4313 /*data_ptr*/ data_ptr, 4314 /*dxfer_len*/ dxfer_len, 4315 sense_len, 4316 sizeof(*scsi_cmd), 4317 timeout); 4318 } 4319 4320 4321 void 4322 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 4323 void (*cbfcnp)(struct cam_periph *, union ccb *), 4324 u_int8_t tag_action, u_int32_t address, 4325 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 4326 u_int8_t *data_ptr, u_int32_t dxfer_len, 4327 u_int8_t sense_len, u_int32_t timeout) 4328 { 4329 struct scsi_read_dvd_structure *scsi_cmd; 4330 4331 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 4332 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4333 scsi_cmd->opcode = READ_DVD_STRUCTURE; 4334 4335 scsi_ulto4b(address, scsi_cmd->address); 4336 scsi_cmd->layer_number = layer_number; 4337 scsi_cmd->format = format; 4338 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4339 /* The AGID is the top two bits of this byte */ 4340 scsi_cmd->agid = agid << 6; 4341 4342 cam_fill_csio(csio, 4343 retries, 4344 cbfcnp, 4345 /*flags*/ CAM_DIR_IN, 4346 tag_action, 4347 /*data_ptr*/ data_ptr, 4348 /*dxfer_len*/ dxfer_len, 4349 sense_len, 4350 sizeof(*scsi_cmd), 4351 timeout); 4352 } 4353