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