scsi_cd.c revision 249615
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: stable/9/sys/cam/scsi/scsi_cd.c 249615 2013-04-18 10:06:52Z mav $"); 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 81struct cd_params { 82 u_int32_t blksize; 83 u_long disksize; 84}; 85 86typedef 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 95typedef 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 110typedef 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 119typedef 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 129struct cd_tocdata { 130 struct ioc_toc_header header; 131 struct cd_toc_entry entries[100]; 132}; 133 134struct cd_toc_single { 135 struct ioc_toc_header header; 136 struct cd_toc_entry entry; 137}; 138 139typedef enum { 140 CD_STATE_PROBE, 141 CD_STATE_NORMAL 142} cd_state; 143 144struct 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 169struct cd_page_sizes { 170 int page; 171 int page_size; 172}; 173 174static struct cd_page_sizes cd_page_size_table[] = 175{ 176 { AUDIO_PAGE, sizeof(struct cd_audio_page)} 177}; 178 179struct 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 */ 202static 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 222static disk_open_t cdopen; 223static disk_close_t cdclose; 224static disk_ioctl_t cdioctl; 225static disk_strategy_t cdstrategy; 226 227static periph_init_t cdinit; 228static periph_ctor_t cdregister; 229static periph_dtor_t cdcleanup; 230static periph_start_t cdstart; 231static periph_oninv_t cdoninvalidate; 232static void cdasync(void *callback_arg, u_int32_t code, 233 struct cam_path *path, void *arg); 234static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 235static void cdshorttimeout(void *arg); 236static void cdschedule(struct cam_periph *periph, int priority); 237static void cdrunchangerqueue(void *arg); 238static void cdchangerschedule(struct cd_softc *softc); 239static 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); 244static union ccb *cdgetccb(struct cam_periph *periph, 245 u_int32_t priority); 246static void cddone(struct cam_periph *periph, 247 union ccb *start_ccb); 248static union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 249static int cdgetpagesize(int page_num); 250static void cdprevent(struct cam_periph *periph, int action); 251static int cdcheckmedia(struct cam_periph *periph); 252static int cdsize(struct cam_periph *periph, u_int32_t *size); 253static int cd6byteworkaround(union ccb *ccb); 254static int cderror(union ccb *ccb, u_int32_t cam_flags, 255 u_int32_t sense_flags); 256static 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); 259static int cdgetmode(struct cam_periph *periph, 260 struct cd_mode_params *data, u_int32_t page); 261static int cdsetmode(struct cam_periph *periph, 262 struct cd_mode_params *data); 263static int cdplay(struct cam_periph *periph, u_int32_t blk, 264 u_int32_t len); 265static 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); 270static 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); 274static int cdplaytracks(struct cam_periph *periph, 275 u_int32_t strack, u_int32_t sindex, 276 u_int32_t etrack, u_int32_t eindex); 277static int cdpause(struct cam_periph *periph, u_int32_t go); 278static int cdstopunit(struct cam_periph *periph, u_int32_t eject); 279static int cdstartunit(struct cam_periph *periph, int load); 280static int cdsetspeed(struct cam_periph *periph, 281 u_int32_t rdspeed, u_int32_t wrspeed); 282static int cdreportkey(struct cam_periph *periph, 283 struct dvd_authinfo *authinfo); 284static int cdsendkey(struct cam_periph *periph, 285 struct dvd_authinfo *authinfo); 286static int cdreaddvdstructure(struct cam_periph *periph, 287 struct dvd_struct *dvdstruct); 288static timeout_t cdmediapoll; 289 290static struct periph_driver cddriver = 291{ 292 cdinit, "cd", 293 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 294}; 295 296PERIPHDRIVER_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 314static int cd_poll_period = CD_DEFAULT_POLL_PERIOD; 315static int cd_retry_count = CD_DEFAULT_RETRY; 316static int cd_timeout = CD_DEFAULT_TIMEOUT; 317static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 318static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 319 320static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 321static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, 322 "CD Changer"); 323SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RW, 324 &cd_poll_period, 0, "Media polling period in seconds"); 325TUNABLE_INT("kern.cam.cd.poll_period", &cd_poll_period); 326SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW, 327 &cd_retry_count, 0, "Normal I/O retry count"); 328TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count); 329SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RW, 330 &cd_timeout, 0, "Timeout, in us, for read operations"); 331TUNABLE_INT("kern.cam.cd.timeout", &cd_timeout); 332SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 333 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 334TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds); 335SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 336 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 337TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds); 338 339struct 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 353static struct mtx changerq_mtx; 354static STAILQ_HEAD(changerlist, cdchanger) changerq; 355static int num_changers; 356 357static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers"); 358 359static void 360cdinit(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 */ 383static void 384cddiskgonecb(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 392static void 393cdoninvalidate(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 426static void 427cdcleanup(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 514static void 515cdasync(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 608static void 609cdsysctlinit(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 */ 651static int 652cdcmdsizesysctl(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 684static cam_status 685cdregister(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 1057cdregisterexit: 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 1067static int 1068cdopen(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 1112static int 1113cdclose(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 1152static void 1153cdshorttimeout(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 */ 1176static void 1177cdschedule(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 1220static void 1221cdrunchangerqueue(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 1322static void 1323cdchangerschedule(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 1397static int 1398cdrunccb(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 1419static union ccb * 1420cdgetccb(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 */ 1462static void 1463cdstrategy(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 1517static void 1518cdstart(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 /* byte2 */ 0, 1568 /* minimum_cmd_size */ 10, 1569 /* lba */ bp->bio_offset / 1570 softc->params.blksize, 1571 bp->bio_bcount / softc->params.blksize, 1572 /* data_ptr */ bp->bio_data, 1573 /* dxfer_len */ bp->bio_bcount, 1574 /* sense_len */ cd_retry_count ? 1575 SSD_FULL_SIZE : SF_NO_PRINT, 1576 /* timeout */ cd_timeout); 1577 /* Use READ CD command for audio tracks. */ 1578 if (softc->params.blksize == 2352) { 1579 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD; 1580 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8; 1581 start_ccb->csio.cdb_io.cdb_bytes[10] = 0; 1582 start_ccb->csio.cdb_io.cdb_bytes[11] = 0; 1583 start_ccb->csio.cdb_len = 12; 1584 } 1585 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 1586 1587 1588 LIST_INSERT_HEAD(&softc->pending_ccbs, 1589 &start_ccb->ccb_h, periph_links.le); 1590 softc->outstanding_cmds++; 1591 1592 /* We expect a unit attention from this device */ 1593 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 1594 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 1595 softc->flags &= ~CD_FLAG_RETRY_UA; 1596 } 1597 1598 start_ccb->ccb_h.ccb_bp = bp; 1599 bp = bioq_first(&softc->bio_queue); 1600 1601 xpt_action(start_ccb); 1602 } 1603 if (bp != NULL || softc->tur || 1604 periph->immediate_priority != CAM_PRIORITY_NONE) { 1605 /* Have more work to do, so ensure we stay scheduled */ 1606 xpt_schedule(periph, min(CAM_PRIORITY_NORMAL, 1607 periph->immediate_priority)); 1608 } 1609 break; 1610 } 1611 case CD_STATE_PROBE: 1612 { 1613 1614 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1615 M_SCSICD, M_NOWAIT | M_ZERO); 1616 if (rcap == NULL) { 1617 xpt_print(periph->path, 1618 "cdstart: Couldn't malloc read_capacity data\n"); 1619 /* cd_free_periph??? */ 1620 break; 1621 } 1622 csio = &start_ccb->csio; 1623 scsi_read_capacity(csio, 1624 /*retries*/ cd_retry_count, 1625 cddone, 1626 MSG_SIMPLE_Q_TAG, 1627 rcap, 1628 SSD_FULL_SIZE, 1629 /*timeout*/20000); 1630 start_ccb->ccb_h.ccb_bp = NULL; 1631 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 1632 xpt_action(start_ccb); 1633 break; 1634 } 1635 } 1636} 1637 1638static void 1639cddone(struct cam_periph *periph, union ccb *done_ccb) 1640{ 1641 struct cd_softc *softc; 1642 struct ccb_scsiio *csio; 1643 1644 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 1645 1646 softc = (struct cd_softc *)periph->softc; 1647 csio = &done_ccb->csio; 1648 1649 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 1650 case CD_CCB_BUFFER_IO: 1651 { 1652 struct bio *bp; 1653 int error; 1654 1655 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1656 error = 0; 1657 1658 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1659 int sf; 1660 1661 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 1662 sf = SF_RETRY_UA; 1663 else 1664 sf = 0; 1665 1666 error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 1667 if (error == ERESTART) { 1668 /* 1669 * A retry was scheuled, so 1670 * just return. 1671 */ 1672 return; 1673 } 1674 } 1675 1676 if (error != 0) { 1677 xpt_print(periph->path, 1678 "cddone: got error %#x back\n", error); 1679 bioq_flush(&softc->bio_queue, NULL, EIO); 1680 bp->bio_resid = bp->bio_bcount; 1681 bp->bio_error = error; 1682 bp->bio_flags |= BIO_ERROR; 1683 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1684 cam_release_devq(done_ccb->ccb_h.path, 1685 /*relsim_flags*/0, 1686 /*reduction*/0, 1687 /*timeout*/0, 1688 /*getcount_only*/0); 1689 1690 } else { 1691 bp->bio_resid = csio->resid; 1692 bp->bio_error = 0; 1693 if (bp->bio_resid != 0) { 1694 /* 1695 * Short transfer ??? 1696 * XXX: not sure this is correct for partial 1697 * transfers at EOM 1698 */ 1699 bp->bio_flags |= BIO_ERROR; 1700 } 1701 } 1702 1703 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1704 softc->outstanding_cmds--; 1705 1706 if (softc->flags & CD_FLAG_CHANGER) 1707 cdchangerschedule(softc); 1708 1709 biofinish(bp, NULL, 0); 1710 break; 1711 } 1712 case CD_CCB_PROBE: 1713 { 1714 struct scsi_read_capacity_data *rdcap; 1715 char announce_buf[120]; /* 1716 * Currently (9/30/97) the 1717 * longest possible announce 1718 * buffer is 108 bytes, for the 1719 * first error case below. 1720 * That is 39 bytes for the 1721 * basic string, 16 bytes for the 1722 * biggest sense key (hardware 1723 * error), 52 bytes for the 1724 * text of the largest sense 1725 * qualifier valid for a CDROM, 1726 * (0x72, 0x03 or 0x04, 1727 * 0x03), and one byte for the 1728 * null terminating character. 1729 * To allow for longer strings, 1730 * the announce buffer is 120 1731 * bytes. 1732 */ 1733 struct cd_params *cdp; 1734 int error; 1735 1736 cdp = &softc->params; 1737 1738 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1739 1740 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1741 cdp->blksize = scsi_4btoul (rdcap->length); 1742 1743 /* 1744 * Retry any UNIT ATTENTION type errors. They 1745 * are expected at boot. 1746 */ 1747 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP || 1748 (error = cderror(done_ccb, CAM_RETRY_SELTO, 1749 SF_RETRY_UA | SF_NO_PRINT)) == 0) { 1750 1751 snprintf(announce_buf, sizeof(announce_buf), 1752 "cd present [%lu x %lu byte records]", 1753 cdp->disksize, (u_long)cdp->blksize); 1754 1755 } else { 1756 if (error == ERESTART) { 1757 /* 1758 * A retry was scheuled, so 1759 * just return. 1760 */ 1761 return; 1762 } else { 1763 int asc, ascq; 1764 int sense_key, error_code; 1765 int have_sense; 1766 cam_status status; 1767 struct ccb_getdev cgd; 1768 1769 /* Don't wedge this device's queue */ 1770 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1771 cam_release_devq(done_ccb->ccb_h.path, 1772 /*relsim_flags*/0, 1773 /*reduction*/0, 1774 /*timeout*/0, 1775 /*getcount_only*/0); 1776 1777 status = done_ccb->ccb_h.status; 1778 1779 xpt_setup_ccb(&cgd.ccb_h, 1780 done_ccb->ccb_h.path, 1781 CAM_PRIORITY_NORMAL); 1782 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1783 xpt_action((union ccb *)&cgd); 1784 1785 if (scsi_extract_sense_ccb(done_ccb, 1786 &error_code, &sense_key, &asc, &ascq)) 1787 have_sense = TRUE; 1788 else 1789 have_sense = FALSE; 1790 1791 /* 1792 * Attach to anything that claims to be a 1793 * CDROM or WORM device, as long as it 1794 * doesn't return a "Logical unit not 1795 * supported" (0x25) error. 1796 */ 1797 if ((have_sense) && (asc != 0x25) 1798 && (error_code == SSD_CURRENT_ERROR)) { 1799 const char *sense_key_desc; 1800 const char *asc_desc; 1801 1802 scsi_sense_desc(sense_key, asc, ascq, 1803 &cgd.inq_data, 1804 &sense_key_desc, 1805 &asc_desc); 1806 snprintf(announce_buf, 1807 sizeof(announce_buf), 1808 "Attempt to query device " 1809 "size failed: %s, %s", 1810 sense_key_desc, 1811 asc_desc); 1812 } else if ((have_sense == 0) 1813 && ((status & CAM_STATUS_MASK) == 1814 CAM_SCSI_STATUS_ERROR) 1815 && (csio->scsi_status == 1816 SCSI_STATUS_BUSY)) { 1817 snprintf(announce_buf, 1818 sizeof(announce_buf), 1819 "Attempt to query device " 1820 "size failed: SCSI Status: %s", 1821 scsi_status_string(csio)); 1822 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1823 /* 1824 * We only print out an error for 1825 * CDROM type devices. For WORM 1826 * devices, we don't print out an 1827 * error since a few WORM devices 1828 * don't support CDROM commands. 1829 * If we have sense information, go 1830 * ahead and print it out. 1831 * Otherwise, just say that we 1832 * couldn't attach. 1833 */ 1834 1835 /* 1836 * Just print out the error, not 1837 * the full probe message, when we 1838 * don't attach. 1839 */ 1840 if (have_sense) 1841 scsi_sense_print( 1842 &done_ccb->csio); 1843 else { 1844 xpt_print(periph->path, 1845 "got CAM status %#x\n", 1846 done_ccb->ccb_h.status); 1847 } 1848 xpt_print(periph->path, "fatal error, " 1849 "failed to attach to device\n"); 1850 /* 1851 * Invalidate this peripheral. 1852 */ 1853 cam_periph_invalidate(periph); 1854 1855 announce_buf[0] = '\0'; 1856 } else { 1857 1858 /* 1859 * Invalidate this peripheral. 1860 */ 1861 cam_periph_invalidate(periph); 1862 announce_buf[0] = '\0'; 1863 } 1864 } 1865 } 1866 free(rdcap, M_SCSICD); 1867 if (announce_buf[0] != '\0') { 1868 xpt_announce_periph(periph, announce_buf); 1869 if (softc->flags & CD_FLAG_CHANGER) 1870 cdchangerschedule(softc); 1871 /* 1872 * Create our sysctl variables, now that we know 1873 * we have successfully attached. 1874 */ 1875 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 1876 } 1877 softc->state = CD_STATE_NORMAL; 1878 /* 1879 * Since our peripheral may be invalidated by an error 1880 * above or an external event, we must release our CCB 1881 * before releasing the probe lock on the peripheral. 1882 * The peripheral will only go away once the last lock 1883 * is removed, and we need it around for the CCB release 1884 * operation. 1885 */ 1886 xpt_release_ccb(done_ccb); 1887 cam_periph_unhold(periph); 1888 return; 1889 } 1890 case CD_CCB_WAITING: 1891 { 1892 /* Caller will release the CCB */ 1893 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1894 ("trying to wakeup ccbwait\n")); 1895 1896 wakeup(&done_ccb->ccb_h.cbfcnp); 1897 return; 1898 } 1899 case CD_CCB_TUR: 1900 { 1901 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1902 1903 if (cderror(done_ccb, CAM_RETRY_SELTO, 1904 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == 1905 ERESTART) 1906 return; 1907 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1908 cam_release_devq(done_ccb->ccb_h.path, 1909 /*relsim_flags*/0, 1910 /*reduction*/0, 1911 /*timeout*/0, 1912 /*getcount_only*/0); 1913 } 1914 xpt_release_ccb(done_ccb); 1915 cam_periph_release_locked(periph); 1916 return; 1917 } 1918 default: 1919 break; 1920 } 1921 xpt_release_ccb(done_ccb); 1922} 1923 1924static union cd_pages * 1925cdgetpage(struct cd_mode_params *mode_params) 1926{ 1927 union cd_pages *page; 1928 1929 if (mode_params->cdb_size == 10) 1930 page = (union cd_pages *)find_mode_page_10( 1931 (struct scsi_mode_header_10 *)mode_params->mode_buf); 1932 else 1933 page = (union cd_pages *)find_mode_page_6( 1934 (struct scsi_mode_header_6 *)mode_params->mode_buf); 1935 1936 return (page); 1937} 1938 1939static int 1940cdgetpagesize(int page_num) 1941{ 1942 int i; 1943 1944 for (i = 0; i < (sizeof(cd_page_size_table)/ 1945 sizeof(cd_page_size_table[0])); i++) { 1946 if (cd_page_size_table[i].page == page_num) 1947 return (cd_page_size_table[i].page_size); 1948 } 1949 1950 return (-1); 1951} 1952 1953static int 1954cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 1955{ 1956 1957 struct cam_periph *periph; 1958 struct cd_softc *softc; 1959 int nocopyout, error = 0; 1960 1961 periph = (struct cam_periph *)dp->d_drv1; 1962 cam_periph_lock(periph); 1963 1964 softc = (struct cd_softc *)periph->softc; 1965 1966 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1967 ("cdioctl(%#lx)\n", cmd)); 1968 1969 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1970 cam_periph_unlock(periph); 1971 cam_periph_release(periph); 1972 return (error); 1973 } 1974 1975 /* 1976 * If we don't have media loaded, check for it. If still don't 1977 * have media loaded, we can only do a load or eject. 1978 * 1979 * We only care whether media is loaded if this is a cd-specific ioctl 1980 * (thus the IOCGROUP check below). Note that this will break if 1981 * anyone adds any ioctls into the switch statement below that don't 1982 * have their ioctl group set to 'c'. 1983 */ 1984 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1985 && ((cmd != CDIOCCLOSE) 1986 && (cmd != CDIOCEJECT)) 1987 && (IOCGROUP(cmd) == 'c')) { 1988 error = cdcheckmedia(periph); 1989 if (error != 0) { 1990 cam_periph_unhold(periph); 1991 cam_periph_unlock(periph); 1992 return (error); 1993 } 1994 } 1995 /* 1996 * Drop the lock here so later mallocs can use WAITOK. The periph 1997 * is essentially locked still with the cam_periph_hold call above. 1998 */ 1999 cam_periph_unlock(periph); 2000 2001 nocopyout = 0; 2002 switch (cmd) { 2003 2004 case CDIOCPLAYTRACKS: 2005 { 2006 struct ioc_play_track *args 2007 = (struct ioc_play_track *) addr; 2008 struct cd_mode_params params; 2009 union cd_pages *page; 2010 2011 params.alloc_len = sizeof(union cd_mode_data_6_10); 2012 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2013 M_WAITOK | M_ZERO); 2014 2015 cam_periph_lock(periph); 2016 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2017 ("trying to do CDIOCPLAYTRACKS\n")); 2018 2019 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2020 if (error) { 2021 free(params.mode_buf, M_SCSICD); 2022 cam_periph_unlock(periph); 2023 break; 2024 } 2025 page = cdgetpage(¶ms); 2026 2027 page->audio.flags &= ~CD_PA_SOTC; 2028 page->audio.flags |= CD_PA_IMMED; 2029 error = cdsetmode(periph, ¶ms); 2030 free(params.mode_buf, M_SCSICD); 2031 if (error) { 2032 cam_periph_unlock(periph); 2033 break; 2034 } 2035 2036 /* 2037 * This was originally implemented with the PLAY 2038 * AUDIO TRACK INDEX command, but that command was 2039 * deprecated after SCSI-2. Most (all?) SCSI CDROM 2040 * drives support it but ATAPI and ATAPI-derivative 2041 * drives don't seem to support it. So we keep a 2042 * cache of the table of contents and translate 2043 * track numbers to MSF format. 2044 */ 2045 if (softc->flags & CD_FLAG_VALID_TOC) { 2046 union msf_lba *sentry, *eentry; 2047 int st, et; 2048 2049 if (args->end_track < 2050 softc->toc.header.ending_track + 1) 2051 args->end_track++; 2052 if (args->end_track > 2053 softc->toc.header.ending_track + 1) 2054 args->end_track = 2055 softc->toc.header.ending_track + 1; 2056 st = args->start_track - 2057 softc->toc.header.starting_track; 2058 et = args->end_track - 2059 softc->toc.header.starting_track; 2060 if ((st < 0) 2061 || (et < 0) 2062 || (st > (softc->toc.header.ending_track - 2063 softc->toc.header.starting_track))) { 2064 error = EINVAL; 2065 cam_periph_unlock(periph); 2066 break; 2067 } 2068 sentry = &softc->toc.entries[st].addr; 2069 eentry = &softc->toc.entries[et].addr; 2070 error = cdplaymsf(periph, 2071 sentry->msf.minute, 2072 sentry->msf.second, 2073 sentry->msf.frame, 2074 eentry->msf.minute, 2075 eentry->msf.second, 2076 eentry->msf.frame); 2077 } else { 2078 /* 2079 * If we don't have a valid TOC, try the 2080 * play track index command. It is part of 2081 * the SCSI-2 spec, but was removed in the 2082 * MMC specs. ATAPI and ATAPI-derived 2083 * drives don't support it. 2084 */ 2085 if (softc->quirks & CD_Q_BCD_TRACKS) { 2086 args->start_track = 2087 bin2bcd(args->start_track); 2088 args->end_track = 2089 bin2bcd(args->end_track); 2090 } 2091 error = cdplaytracks(periph, 2092 args->start_track, 2093 args->start_index, 2094 args->end_track, 2095 args->end_index); 2096 } 2097 cam_periph_unlock(periph); 2098 } 2099 break; 2100 case CDIOCPLAYMSF: 2101 { 2102 struct ioc_play_msf *args 2103 = (struct ioc_play_msf *) addr; 2104 struct cd_mode_params params; 2105 union cd_pages *page; 2106 2107 params.alloc_len = sizeof(union cd_mode_data_6_10); 2108 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2109 M_WAITOK | M_ZERO); 2110 2111 cam_periph_lock(periph); 2112 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2113 ("trying to do CDIOCPLAYMSF\n")); 2114 2115 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2116 if (error) { 2117 free(params.mode_buf, M_SCSICD); 2118 cam_periph_unlock(periph); 2119 break; 2120 } 2121 page = cdgetpage(¶ms); 2122 2123 page->audio.flags &= ~CD_PA_SOTC; 2124 page->audio.flags |= CD_PA_IMMED; 2125 error = cdsetmode(periph, ¶ms); 2126 free(params.mode_buf, M_SCSICD); 2127 if (error) { 2128 cam_periph_unlock(periph); 2129 break; 2130 } 2131 error = cdplaymsf(periph, 2132 args->start_m, 2133 args->start_s, 2134 args->start_f, 2135 args->end_m, 2136 args->end_s, 2137 args->end_f); 2138 cam_periph_unlock(periph); 2139 } 2140 break; 2141 case CDIOCPLAYBLOCKS: 2142 { 2143 struct ioc_play_blocks *args 2144 = (struct ioc_play_blocks *) addr; 2145 struct cd_mode_params params; 2146 union cd_pages *page; 2147 2148 params.alloc_len = sizeof(union cd_mode_data_6_10); 2149 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2150 M_WAITOK | M_ZERO); 2151 2152 cam_periph_lock(periph); 2153 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2154 ("trying to do CDIOCPLAYBLOCKS\n")); 2155 2156 2157 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2158 if (error) { 2159 free(params.mode_buf, M_SCSICD); 2160 cam_periph_unlock(periph); 2161 break; 2162 } 2163 page = cdgetpage(¶ms); 2164 2165 page->audio.flags &= ~CD_PA_SOTC; 2166 page->audio.flags |= CD_PA_IMMED; 2167 error = cdsetmode(periph, ¶ms); 2168 free(params.mode_buf, M_SCSICD); 2169 if (error) { 2170 cam_periph_unlock(periph); 2171 break; 2172 } 2173 error = cdplay(periph, args->blk, args->len); 2174 cam_periph_unlock(periph); 2175 } 2176 break; 2177 case CDIOCREADSUBCHANNEL_SYSSPACE: 2178 nocopyout = 1; 2179 /* Fallthrough */ 2180 case CDIOCREADSUBCHANNEL: 2181 { 2182 struct ioc_read_subchannel *args 2183 = (struct ioc_read_subchannel *) addr; 2184 struct cd_sub_channel_info *data; 2185 u_int32_t len = args->data_len; 2186 2187 data = malloc(sizeof(struct cd_sub_channel_info), 2188 M_SCSICD, M_WAITOK | M_ZERO); 2189 2190 cam_periph_lock(periph); 2191 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2192 ("trying to do CDIOCREADSUBCHANNEL\n")); 2193 2194 if ((len > sizeof(struct cd_sub_channel_info)) || 2195 (len < sizeof(struct cd_sub_channel_header))) { 2196 printf( 2197 "scsi_cd: cdioctl: " 2198 "cdioreadsubchannel: error, len=%d\n", 2199 len); 2200 error = EINVAL; 2201 free(data, M_SCSICD); 2202 cam_periph_unlock(periph); 2203 break; 2204 } 2205 2206 if (softc->quirks & CD_Q_BCD_TRACKS) 2207 args->track = bin2bcd(args->track); 2208 2209 error = cdreadsubchannel(periph, args->address_format, 2210 args->data_format, args->track, data, len); 2211 2212 if (error) { 2213 free(data, M_SCSICD); 2214 cam_periph_unlock(periph); 2215 break; 2216 } 2217 if (softc->quirks & CD_Q_BCD_TRACKS) 2218 data->what.track_info.track_number = 2219 bcd2bin(data->what.track_info.track_number); 2220 len = min(len, ((data->header.data_len[0] << 8) + 2221 data->header.data_len[1] + 2222 sizeof(struct cd_sub_channel_header))); 2223 cam_periph_unlock(periph); 2224 if (nocopyout == 0) { 2225 if (copyout(data, args->data, len) != 0) { 2226 error = EFAULT; 2227 } 2228 } else { 2229 bcopy(data, args->data, len); 2230 } 2231 free(data, M_SCSICD); 2232 } 2233 break; 2234 2235 case CDIOREADTOCHEADER: 2236 { 2237 struct ioc_toc_header *th; 2238 2239 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2240 M_WAITOK | M_ZERO); 2241 2242 cam_periph_lock(periph); 2243 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2244 ("trying to do CDIOREADTOCHEADER\n")); 2245 2246 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2247 sizeof (*th), /*sense_flags*/SF_NO_PRINT); 2248 if (error) { 2249 free(th, M_SCSICD); 2250 cam_periph_unlock(periph); 2251 break; 2252 } 2253 if (softc->quirks & CD_Q_BCD_TRACKS) { 2254 /* we are going to have to convert the BCD 2255 * encoding on the cd to what is expected 2256 */ 2257 th->starting_track = 2258 bcd2bin(th->starting_track); 2259 th->ending_track = bcd2bin(th->ending_track); 2260 } 2261 th->len = ntohs(th->len); 2262 bcopy(th, addr, sizeof(*th)); 2263 free(th, M_SCSICD); 2264 cam_periph_unlock(periph); 2265 } 2266 break; 2267 case CDIOREADTOCENTRYS: 2268 { 2269 struct cd_tocdata *data; 2270 struct cd_toc_single *lead; 2271 struct ioc_read_toc_entry *te = 2272 (struct ioc_read_toc_entry *) addr; 2273 struct ioc_toc_header *th; 2274 u_int32_t len, readlen, idx, num; 2275 u_int32_t starting_track = te->starting_track; 2276 2277 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2278 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 2279 2280 cam_periph_lock(periph); 2281 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2282 ("trying to do CDIOREADTOCENTRYS\n")); 2283 2284 if (te->data_len < sizeof(struct cd_toc_entry) 2285 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2286 || (te->address_format != CD_MSF_FORMAT 2287 && te->address_format != CD_LBA_FORMAT)) { 2288 error = EINVAL; 2289 printf("scsi_cd: error in readtocentries, " 2290 "returning EINVAL\n"); 2291 free(data, M_SCSICD); 2292 free(lead, M_SCSICD); 2293 cam_periph_unlock(periph); 2294 break; 2295 } 2296 2297 th = &data->header; 2298 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2299 sizeof (*th), /*sense_flags*/0); 2300 if (error) { 2301 free(data, M_SCSICD); 2302 free(lead, M_SCSICD); 2303 cam_periph_unlock(periph); 2304 break; 2305 } 2306 2307 if (softc->quirks & CD_Q_BCD_TRACKS) { 2308 /* we are going to have to convert the BCD 2309 * encoding on the cd to what is expected 2310 */ 2311 th->starting_track = 2312 bcd2bin(th->starting_track); 2313 th->ending_track = bcd2bin(th->ending_track); 2314 } 2315 2316 if (starting_track == 0) 2317 starting_track = th->starting_track; 2318 else if (starting_track == LEADOUT) 2319 starting_track = th->ending_track + 1; 2320 else if (starting_track < th->starting_track || 2321 starting_track > th->ending_track + 1) { 2322 printf("scsi_cd: error in readtocentries, " 2323 "returning EINVAL\n"); 2324 free(data, M_SCSICD); 2325 free(lead, M_SCSICD); 2326 cam_periph_unlock(periph); 2327 error = EINVAL; 2328 break; 2329 } 2330 2331 /* calculate reading length without leadout entry */ 2332 readlen = (th->ending_track - starting_track + 1) * 2333 sizeof(struct cd_toc_entry); 2334 2335 /* and with leadout entry */ 2336 len = readlen + sizeof(struct cd_toc_entry); 2337 if (te->data_len < len) { 2338 len = te->data_len; 2339 if (readlen > len) 2340 readlen = len; 2341 } 2342 if (len > sizeof(data->entries)) { 2343 printf("scsi_cd: error in readtocentries, " 2344 "returning EINVAL\n"); 2345 error = EINVAL; 2346 free(data, M_SCSICD); 2347 free(lead, M_SCSICD); 2348 cam_periph_unlock(periph); 2349 break; 2350 } 2351 num = len / sizeof(struct cd_toc_entry); 2352 2353 if (readlen > 0) { 2354 error = cdreadtoc(periph, te->address_format, 2355 starting_track, 2356 (u_int8_t *)data, 2357 readlen + sizeof (*th), 2358 /*sense_flags*/0); 2359 if (error) { 2360 free(data, M_SCSICD); 2361 free(lead, M_SCSICD); 2362 cam_periph_unlock(periph); 2363 break; 2364 } 2365 } 2366 2367 /* make leadout entry if needed */ 2368 idx = starting_track + num - 1; 2369 if (softc->quirks & CD_Q_BCD_TRACKS) 2370 th->ending_track = bcd2bin(th->ending_track); 2371 if (idx == th->ending_track + 1) { 2372 error = cdreadtoc(periph, te->address_format, 2373 LEADOUT, (u_int8_t *)lead, 2374 sizeof(*lead), 2375 /*sense_flags*/0); 2376 if (error) { 2377 free(data, M_SCSICD); 2378 free(lead, M_SCSICD); 2379 cam_periph_unlock(periph); 2380 break; 2381 } 2382 data->entries[idx - starting_track] = 2383 lead->entry; 2384 } 2385 if (softc->quirks & CD_Q_BCD_TRACKS) { 2386 for (idx = 0; idx < num - 1; idx++) { 2387 data->entries[idx].track = 2388 bcd2bin(data->entries[idx].track); 2389 } 2390 } 2391 2392 cam_periph_unlock(periph); 2393 error = copyout(data->entries, te->data, len); 2394 free(data, M_SCSICD); 2395 free(lead, M_SCSICD); 2396 } 2397 break; 2398 case CDIOREADTOCENTRY: 2399 { 2400 struct cd_toc_single *data; 2401 struct ioc_read_toc_single_entry *te = 2402 (struct ioc_read_toc_single_entry *) addr; 2403 struct ioc_toc_header *th; 2404 u_int32_t track; 2405 2406 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2407 2408 cam_periph_lock(periph); 2409 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2410 ("trying to do CDIOREADTOCENTRY\n")); 2411 2412 if (te->address_format != CD_MSF_FORMAT 2413 && te->address_format != CD_LBA_FORMAT) { 2414 printf("error in readtocentry, " 2415 " returning EINVAL\n"); 2416 free(data, M_SCSICD); 2417 error = EINVAL; 2418 cam_periph_unlock(periph); 2419 break; 2420 } 2421 2422 th = &data->header; 2423 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2424 sizeof (*th), /*sense_flags*/0); 2425 if (error) { 2426 free(data, M_SCSICD); 2427 cam_periph_unlock(periph); 2428 break; 2429 } 2430 2431 if (softc->quirks & CD_Q_BCD_TRACKS) { 2432 /* we are going to have to convert the BCD 2433 * encoding on the cd to what is expected 2434 */ 2435 th->starting_track = 2436 bcd2bin(th->starting_track); 2437 th->ending_track = bcd2bin(th->ending_track); 2438 } 2439 track = te->track; 2440 if (track == 0) 2441 track = th->starting_track; 2442 else if (track == LEADOUT) 2443 /* OK */; 2444 else if (track < th->starting_track || 2445 track > th->ending_track + 1) { 2446 printf("error in readtocentry, " 2447 " returning EINVAL\n"); 2448 free(data, M_SCSICD); 2449 error = EINVAL; 2450 cam_periph_unlock(periph); 2451 break; 2452 } 2453 2454 error = cdreadtoc(periph, te->address_format, track, 2455 (u_int8_t *)data, sizeof(*data), 2456 /*sense_flags*/0); 2457 if (error) { 2458 free(data, M_SCSICD); 2459 cam_periph_unlock(periph); 2460 break; 2461 } 2462 2463 if (softc->quirks & CD_Q_BCD_TRACKS) 2464 data->entry.track = bcd2bin(data->entry.track); 2465 bcopy(&data->entry, &te->entry, 2466 sizeof(struct cd_toc_entry)); 2467 free(data, M_SCSICD); 2468 cam_periph_unlock(periph); 2469 } 2470 break; 2471 case CDIOCSETPATCH: 2472 { 2473 struct ioc_patch *arg = (struct ioc_patch *)addr; 2474 struct cd_mode_params params; 2475 union cd_pages *page; 2476 2477 params.alloc_len = sizeof(union cd_mode_data_6_10); 2478 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2479 M_WAITOK | M_ZERO); 2480 2481 cam_periph_lock(periph); 2482 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2483 ("trying to do CDIOCSETPATCH\n")); 2484 2485 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2486 if (error) { 2487 free(params.mode_buf, M_SCSICD); 2488 cam_periph_unlock(periph); 2489 break; 2490 } 2491 page = cdgetpage(¶ms); 2492 2493 page->audio.port[LEFT_PORT].channels = 2494 arg->patch[0]; 2495 page->audio.port[RIGHT_PORT].channels = 2496 arg->patch[1]; 2497 page->audio.port[2].channels = arg->patch[2]; 2498 page->audio.port[3].channels = arg->patch[3]; 2499 error = cdsetmode(periph, ¶ms); 2500 free(params.mode_buf, M_SCSICD); 2501 cam_periph_unlock(periph); 2502 } 2503 break; 2504 case CDIOCGETVOL: 2505 { 2506 struct ioc_vol *arg = (struct ioc_vol *) addr; 2507 struct cd_mode_params params; 2508 union cd_pages *page; 2509 2510 params.alloc_len = sizeof(union cd_mode_data_6_10); 2511 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2512 M_WAITOK | M_ZERO); 2513 2514 cam_periph_lock(periph); 2515 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2516 ("trying to do CDIOCGETVOL\n")); 2517 2518 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2519 if (error) { 2520 free(params.mode_buf, M_SCSICD); 2521 cam_periph_unlock(periph); 2522 break; 2523 } 2524 page = cdgetpage(¶ms); 2525 2526 arg->vol[LEFT_PORT] = 2527 page->audio.port[LEFT_PORT].volume; 2528 arg->vol[RIGHT_PORT] = 2529 page->audio.port[RIGHT_PORT].volume; 2530 arg->vol[2] = page->audio.port[2].volume; 2531 arg->vol[3] = page->audio.port[3].volume; 2532 free(params.mode_buf, M_SCSICD); 2533 cam_periph_unlock(periph); 2534 } 2535 break; 2536 case CDIOCSETVOL: 2537 { 2538 struct ioc_vol *arg = (struct ioc_vol *) addr; 2539 struct cd_mode_params params; 2540 union cd_pages *page; 2541 2542 params.alloc_len = sizeof(union cd_mode_data_6_10); 2543 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2544 M_WAITOK | M_ZERO); 2545 2546 cam_periph_lock(periph); 2547 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2548 ("trying to do CDIOCSETVOL\n")); 2549 2550 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2551 if (error) { 2552 free(params.mode_buf, M_SCSICD); 2553 cam_periph_unlock(periph); 2554 break; 2555 } 2556 page = cdgetpage(¶ms); 2557 2558 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2559 page->audio.port[LEFT_PORT].volume = 2560 arg->vol[LEFT_PORT]; 2561 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2562 page->audio.port[RIGHT_PORT].volume = 2563 arg->vol[RIGHT_PORT]; 2564 page->audio.port[2].volume = arg->vol[2]; 2565 page->audio.port[3].volume = arg->vol[3]; 2566 error = cdsetmode(periph, ¶ms); 2567 cam_periph_unlock(periph); 2568 free(params.mode_buf, M_SCSICD); 2569 } 2570 break; 2571 case CDIOCSETMONO: 2572 { 2573 struct cd_mode_params params; 2574 union cd_pages *page; 2575 2576 params.alloc_len = sizeof(union cd_mode_data_6_10); 2577 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2578 M_WAITOK | M_ZERO); 2579 2580 cam_periph_lock(periph); 2581 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2582 ("trying to do CDIOCSETMONO\n")); 2583 2584 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2585 if (error) { 2586 free(params.mode_buf, M_SCSICD); 2587 cam_periph_unlock(periph); 2588 break; 2589 } 2590 page = cdgetpage(¶ms); 2591 2592 page->audio.port[LEFT_PORT].channels = 2593 LEFT_CHANNEL | RIGHT_CHANNEL; 2594 page->audio.port[RIGHT_PORT].channels = 2595 LEFT_CHANNEL | RIGHT_CHANNEL; 2596 page->audio.port[2].channels = 0; 2597 page->audio.port[3].channels = 0; 2598 error = cdsetmode(periph, ¶ms); 2599 cam_periph_unlock(periph); 2600 free(params.mode_buf, M_SCSICD); 2601 } 2602 break; 2603 case CDIOCSETSTEREO: 2604 { 2605 struct cd_mode_params params; 2606 union cd_pages *page; 2607 2608 params.alloc_len = sizeof(union cd_mode_data_6_10); 2609 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2610 M_WAITOK | M_ZERO); 2611 2612 cam_periph_lock(periph); 2613 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2614 ("trying to do CDIOCSETSTEREO\n")); 2615 2616 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2617 if (error) { 2618 free(params.mode_buf, M_SCSICD); 2619 cam_periph_unlock(periph); 2620 break; 2621 } 2622 page = cdgetpage(¶ms); 2623 2624 page->audio.port[LEFT_PORT].channels = 2625 LEFT_CHANNEL; 2626 page->audio.port[RIGHT_PORT].channels = 2627 RIGHT_CHANNEL; 2628 page->audio.port[2].channels = 0; 2629 page->audio.port[3].channels = 0; 2630 error = cdsetmode(periph, ¶ms); 2631 free(params.mode_buf, M_SCSICD); 2632 cam_periph_unlock(periph); 2633 } 2634 break; 2635 case CDIOCSETMUTE: 2636 { 2637 struct cd_mode_params params; 2638 union cd_pages *page; 2639 2640 params.alloc_len = sizeof(union cd_mode_data_6_10); 2641 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2642 M_WAITOK | M_ZERO); 2643 2644 cam_periph_lock(periph); 2645 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2646 ("trying to do CDIOCSETMUTE\n")); 2647 2648 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2649 if (error) { 2650 free(params.mode_buf, M_SCSICD); 2651 cam_periph_unlock(periph); 2652 break; 2653 } 2654 page = cdgetpage(¶ms); 2655 2656 page->audio.port[LEFT_PORT].channels = 0; 2657 page->audio.port[RIGHT_PORT].channels = 0; 2658 page->audio.port[2].channels = 0; 2659 page->audio.port[3].channels = 0; 2660 error = cdsetmode(periph, ¶ms); 2661 free(params.mode_buf, M_SCSICD); 2662 cam_periph_unlock(periph); 2663 } 2664 break; 2665 case CDIOCSETLEFT: 2666 { 2667 struct cd_mode_params params; 2668 union cd_pages *page; 2669 2670 params.alloc_len = sizeof(union cd_mode_data_6_10); 2671 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2672 M_WAITOK | M_ZERO); 2673 2674 cam_periph_lock(periph); 2675 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2676 ("trying to do CDIOCSETLEFT\n")); 2677 2678 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2679 if (error) { 2680 free(params.mode_buf, M_SCSICD); 2681 cam_periph_unlock(periph); 2682 break; 2683 } 2684 page = cdgetpage(¶ms); 2685 2686 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2687 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2688 page->audio.port[2].channels = 0; 2689 page->audio.port[3].channels = 0; 2690 error = cdsetmode(periph, ¶ms); 2691 free(params.mode_buf, M_SCSICD); 2692 cam_periph_unlock(periph); 2693 } 2694 break; 2695 case CDIOCSETRIGHT: 2696 { 2697 struct cd_mode_params params; 2698 union cd_pages *page; 2699 2700 params.alloc_len = sizeof(union cd_mode_data_6_10); 2701 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2702 M_WAITOK | M_ZERO); 2703 2704 cam_periph_lock(periph); 2705 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2706 ("trying to do CDIOCSETRIGHT\n")); 2707 2708 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2709 if (error) { 2710 free(params.mode_buf, M_SCSICD); 2711 cam_periph_unlock(periph); 2712 break; 2713 } 2714 page = cdgetpage(¶ms); 2715 2716 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2717 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2718 page->audio.port[2].channels = 0; 2719 page->audio.port[3].channels = 0; 2720 error = cdsetmode(periph, ¶ms); 2721 free(params.mode_buf, M_SCSICD); 2722 cam_periph_unlock(periph); 2723 } 2724 break; 2725 case CDIOCRESUME: 2726 cam_periph_lock(periph); 2727 error = cdpause(periph, 1); 2728 cam_periph_unlock(periph); 2729 break; 2730 case CDIOCPAUSE: 2731 cam_periph_lock(periph); 2732 error = cdpause(periph, 0); 2733 cam_periph_unlock(periph); 2734 break; 2735 case CDIOCSTART: 2736 cam_periph_lock(periph); 2737 error = cdstartunit(periph, 0); 2738 cam_periph_unlock(periph); 2739 break; 2740 case CDIOCCLOSE: 2741 cam_periph_lock(periph); 2742 error = cdstartunit(periph, 1); 2743 cam_periph_unlock(periph); 2744 break; 2745 case CDIOCSTOP: 2746 cam_periph_lock(periph); 2747 error = cdstopunit(periph, 0); 2748 cam_periph_unlock(periph); 2749 break; 2750 case CDIOCEJECT: 2751 cam_periph_lock(periph); 2752 error = cdstopunit(periph, 1); 2753 cam_periph_unlock(periph); 2754 break; 2755 case CDIOCALLOW: 2756 cam_periph_lock(periph); 2757 cdprevent(periph, PR_ALLOW); 2758 cam_periph_unlock(periph); 2759 break; 2760 case CDIOCPREVENT: 2761 cam_periph_lock(periph); 2762 cdprevent(periph, PR_PREVENT); 2763 cam_periph_unlock(periph); 2764 break; 2765 case CDIOCSETDEBUG: 2766 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2767 error = ENOTTY; 2768 break; 2769 case CDIOCCLRDEBUG: 2770 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2771 error = ENOTTY; 2772 break; 2773 case CDIOCRESET: 2774 /* return (cd_reset(periph)); */ 2775 error = ENOTTY; 2776 break; 2777 case CDRIOCREADSPEED: 2778 cam_periph_lock(periph); 2779 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2780 cam_periph_unlock(periph); 2781 break; 2782 case CDRIOCWRITESPEED: 2783 cam_periph_lock(periph); 2784 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2785 cam_periph_unlock(periph); 2786 break; 2787 case CDRIOCGETBLOCKSIZE: 2788 *(int *)addr = softc->params.blksize; 2789 break; 2790 case CDRIOCSETBLOCKSIZE: 2791 if (*(int *)addr <= 0) { 2792 error = EINVAL; 2793 break; 2794 } 2795 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr; 2796 break; 2797 case DVDIOCSENDKEY: 2798 case DVDIOCREPORTKEY: { 2799 struct dvd_authinfo *authinfo; 2800 2801 authinfo = (struct dvd_authinfo *)addr; 2802 2803 if (cmd == DVDIOCREPORTKEY) 2804 error = cdreportkey(periph, authinfo); 2805 else 2806 error = cdsendkey(periph, authinfo); 2807 break; 2808 } 2809 case DVDIOCREADSTRUCTURE: { 2810 struct dvd_struct *dvdstruct; 2811 2812 dvdstruct = (struct dvd_struct *)addr; 2813 2814 error = cdreaddvdstructure(periph, dvdstruct); 2815 2816 break; 2817 } 2818 default: 2819 cam_periph_lock(periph); 2820 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2821 cam_periph_unlock(periph); 2822 break; 2823 } 2824 2825 cam_periph_lock(periph); 2826 cam_periph_unhold(periph); 2827 2828 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2829 if (error && bootverbose) { 2830 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2831 } 2832 cam_periph_unlock(periph); 2833 2834 return (error); 2835} 2836 2837static void 2838cdprevent(struct cam_periph *periph, int action) 2839{ 2840 union ccb *ccb; 2841 struct cd_softc *softc; 2842 int error; 2843 2844 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2845 2846 softc = (struct cd_softc *)periph->softc; 2847 2848 if (((action == PR_ALLOW) 2849 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2850 || ((action == PR_PREVENT) 2851 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2852 return; 2853 } 2854 2855 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 2856 2857 scsi_prevent(&ccb->csio, 2858 /*retries*/ cd_retry_count, 2859 cddone, 2860 MSG_SIMPLE_Q_TAG, 2861 action, 2862 SSD_FULL_SIZE, 2863 /* timeout */60000); 2864 2865 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2866 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2867 2868 xpt_release_ccb(ccb); 2869 2870 if (error == 0) { 2871 if (action == PR_ALLOW) 2872 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2873 else 2874 softc->flags |= CD_FLAG_DISC_LOCKED; 2875 } 2876} 2877 2878/* 2879 * XXX: the disk media and sector size is only really able to change 2880 * XXX: while the device is closed. 2881 */ 2882static int 2883cdcheckmedia(struct cam_periph *periph) 2884{ 2885 struct cd_softc *softc; 2886 struct ioc_toc_header *toch; 2887 struct cd_toc_single leadout; 2888 u_int32_t size, toclen; 2889 int error, num_entries, cdindex; 2890 2891 softc = (struct cd_softc *)periph->softc; 2892 2893 cdprevent(periph, PR_PREVENT); 2894 softc->disk->d_sectorsize = 2048; 2895 softc->disk->d_mediasize = 0; 2896 2897 /* 2898 * Get the disc size and block size. If we can't get it, we don't 2899 * have media, most likely. 2900 */ 2901 if ((error = cdsize(periph, &size)) != 0) { 2902 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2903 cdprevent(periph, PR_ALLOW); 2904 return (error); 2905 } else { 2906 softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA; 2907 softc->disk->d_sectorsize = softc->params.blksize; 2908 softc->disk->d_mediasize = 2909 (off_t)softc->params.blksize * softc->params.disksize; 2910 } 2911 2912 /* 2913 * Now we check the table of contents. This (currently) is only 2914 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2915 * things like present a separate entry in /dev for each track, 2916 * like that acd(4) driver does. 2917 */ 2918 bzero(&softc->toc, sizeof(softc->toc)); 2919 toch = &softc->toc.header; 2920 /* 2921 * We will get errors here for media that doesn't have a table of 2922 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2923 * command is presented for a DDCD/CD-R/RW media, where the first TOC 2924 * has not been recorded (no complete session) and the Format codes 2925 * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2926 * with an INVALID FIELD IN CDB. Devices that are not capable of 2927 * reading an incomplete session on DDC/CD-R/RW media shall report 2928 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2929 * 2930 * So this isn't fatal if we can't read the table of contents, it 2931 * just means that the user won't be able to issue the play tracks 2932 * ioctl, and likely lots of other stuff won't work either. They 2933 * need to burn the CD before we can do a whole lot with it. So 2934 * we don't print anything here if we get an error back. 2935 */ 2936 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2937 SF_NO_PRINT); 2938 /* 2939 * Errors in reading the table of contents aren't fatal, we just 2940 * won't have a valid table of contents cached. 2941 */ 2942 if (error != 0) { 2943 error = 0; 2944 bzero(&softc->toc, sizeof(softc->toc)); 2945 goto bailout; 2946 } 2947 2948 if (softc->quirks & CD_Q_BCD_TRACKS) { 2949 toch->starting_track = bcd2bin(toch->starting_track); 2950 toch->ending_track = bcd2bin(toch->ending_track); 2951 } 2952 2953 /* Number of TOC entries, plus leadout */ 2954 num_entries = (toch->ending_track - toch->starting_track) + 2; 2955 2956 if (num_entries <= 0) 2957 goto bailout; 2958 2959 toclen = num_entries * sizeof(struct cd_toc_entry); 2960 2961 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2962 (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2963 SF_NO_PRINT); 2964 if (error != 0) { 2965 error = 0; 2966 bzero(&softc->toc, sizeof(softc->toc)); 2967 goto bailout; 2968 } 2969 2970 if (softc->quirks & CD_Q_BCD_TRACKS) { 2971 toch->starting_track = bcd2bin(toch->starting_track); 2972 toch->ending_track = bcd2bin(toch->ending_track); 2973 } 2974 /* 2975 * XXX KDM is this necessary? Probably only if the drive doesn't 2976 * return leadout information with the table of contents. 2977 */ 2978 cdindex = toch->starting_track + num_entries -1; 2979 if (cdindex == toch->ending_track + 1) { 2980 2981 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2982 (u_int8_t *)&leadout, sizeof(leadout), 2983 SF_NO_PRINT); 2984 if (error != 0) { 2985 error = 0; 2986 goto bailout; 2987 } 2988 softc->toc.entries[cdindex - toch->starting_track] = 2989 leadout.entry; 2990 } 2991 if (softc->quirks & CD_Q_BCD_TRACKS) { 2992 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2993 softc->toc.entries[cdindex].track = 2994 bcd2bin(softc->toc.entries[cdindex].track); 2995 } 2996 } 2997 2998 softc->flags |= CD_FLAG_VALID_TOC; 2999 3000 /* If the first track is audio, correct sector size. */ 3001 if ((softc->toc.entries[0].control & 4) == 0) { 3002 softc->disk->d_sectorsize = softc->params.blksize = 2352; 3003 softc->disk->d_mediasize = 3004 (off_t)softc->params.blksize * softc->params.disksize; 3005 } 3006 3007bailout: 3008 3009 /* 3010 * We unconditionally (re)set the blocksize each time the 3011 * CD device is opened. This is because the CD can change, 3012 * and therefore the blocksize might change. 3013 * XXX problems here if some slice or partition is still 3014 * open with the old size? 3015 */ 3016 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 3017 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 3018 softc->disk->d_devstat->block_size = softc->params.blksize; 3019 3020 return (error); 3021} 3022 3023static int 3024cdsize(struct cam_periph *periph, u_int32_t *size) 3025{ 3026 struct cd_softc *softc; 3027 union ccb *ccb; 3028 struct scsi_read_capacity_data *rcap_buf; 3029 int error; 3030 3031 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 3032 3033 softc = (struct cd_softc *)periph->softc; 3034 3035 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3036 3037 /* XXX Should be M_WAITOK */ 3038 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 3039 M_SCSICD, M_NOWAIT | M_ZERO); 3040 if (rcap_buf == NULL) 3041 return (ENOMEM); 3042 3043 scsi_read_capacity(&ccb->csio, 3044 /*retries*/ cd_retry_count, 3045 cddone, 3046 MSG_SIMPLE_Q_TAG, 3047 rcap_buf, 3048 SSD_FULL_SIZE, 3049 /* timeout */20000); 3050 3051 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3052 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 3053 3054 xpt_release_ccb(ccb); 3055 3056 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 3057 softc->params.blksize = scsi_4btoul(rcap_buf->length); 3058 /* Make sure we got at least some block size. */ 3059 if (error == 0 && softc->params.blksize == 0) 3060 error = EIO; 3061 /* 3062 * SCSI-3 mandates that the reported blocksize shall be 2048. 3063 * Older drives sometimes report funny values, trim it down to 3064 * 2048, or other parts of the kernel will get confused. 3065 * 3066 * XXX we leave drives alone that might report 512 bytes, as 3067 * well as drives reporting more weird sizes like perhaps 4K. 3068 */ 3069 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 3070 softc->params.blksize = 2048; 3071 3072 free(rcap_buf, M_SCSICD); 3073 *size = softc->params.disksize; 3074 3075 return (error); 3076 3077} 3078 3079static int 3080cd6byteworkaround(union ccb *ccb) 3081{ 3082 u_int8_t *cdb; 3083 struct cam_periph *periph; 3084 struct cd_softc *softc; 3085 struct cd_mode_params *params; 3086 int frozen, found; 3087 3088 periph = xpt_path_periph(ccb->ccb_h.path); 3089 softc = (struct cd_softc *)periph->softc; 3090 3091 cdb = ccb->csio.cdb_io.cdb_bytes; 3092 3093 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 3094 || ((cdb[0] != MODE_SENSE_6) 3095 && (cdb[0] != MODE_SELECT_6))) 3096 return (0); 3097 3098 /* 3099 * Because there is no convenient place to stash the overall 3100 * cd_mode_params structure pointer, we have to grab it like this. 3101 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 3102 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 3103 * 3104 * XXX It would be nice if, at some point, we could increase the 3105 * number of available peripheral private pointers. Both pointers 3106 * are currently used in most every peripheral driver. 3107 */ 3108 found = 0; 3109 3110 STAILQ_FOREACH(params, &softc->mode_queue, links) { 3111 if (params->mode_buf == ccb->csio.data_ptr) { 3112 found = 1; 3113 break; 3114 } 3115 } 3116 3117 /* 3118 * This shouldn't happen. All mode sense and mode select 3119 * operations in the cd(4) driver MUST go through cdgetmode() and 3120 * cdsetmode()! 3121 */ 3122 if (found == 0) { 3123 xpt_print(periph->path, 3124 "mode buffer not found in mode queue!\n"); 3125 return (0); 3126 } 3127 3128 params->cdb_size = 10; 3129 softc->minimum_command_size = 10; 3130 xpt_print(ccb->ccb_h.path, 3131 "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3132 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3133 3134 if (cdb[0] == MODE_SENSE_6) { 3135 struct scsi_mode_sense_10 ms10; 3136 struct scsi_mode_sense_6 *ms6; 3137 int len; 3138 3139 ms6 = (struct scsi_mode_sense_6 *)cdb; 3140 3141 bzero(&ms10, sizeof(ms10)); 3142 ms10.opcode = MODE_SENSE_10; 3143 ms10.byte2 = ms6->byte2; 3144 ms10.page = ms6->page; 3145 3146 /* 3147 * 10 byte mode header, block descriptor, 3148 * sizeof(union cd_pages) 3149 */ 3150 len = sizeof(struct cd_mode_data_10); 3151 ccb->csio.dxfer_len = len; 3152 3153 scsi_ulto2b(len, ms10.length); 3154 ms10.control = ms6->control; 3155 bcopy(&ms10, cdb, 10); 3156 ccb->csio.cdb_len = 10; 3157 } else { 3158 struct scsi_mode_select_10 ms10; 3159 struct scsi_mode_select_6 *ms6; 3160 struct scsi_mode_header_6 *header6; 3161 struct scsi_mode_header_10 *header10; 3162 struct scsi_mode_page_header *page_header; 3163 int blk_desc_len, page_num, page_size, len; 3164 3165 ms6 = (struct scsi_mode_select_6 *)cdb; 3166 3167 bzero(&ms10, sizeof(ms10)); 3168 ms10.opcode = MODE_SELECT_10; 3169 ms10.byte2 = ms6->byte2; 3170 3171 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3172 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3173 3174 page_header = find_mode_page_6(header6); 3175 page_num = page_header->page_code; 3176 3177 blk_desc_len = header6->blk_desc_len; 3178 3179 page_size = cdgetpagesize(page_num); 3180 3181 if (page_size != (page_header->page_length + 3182 sizeof(*page_header))) 3183 page_size = page_header->page_length + 3184 sizeof(*page_header); 3185 3186 len = sizeof(*header10) + blk_desc_len + page_size; 3187 3188 len = min(params->alloc_len, len); 3189 3190 /* 3191 * Since the 6 byte parameter header is shorter than the 10 3192 * byte parameter header, we need to copy the actual mode 3193 * page data, and the block descriptor, if any, so things wind 3194 * up in the right place. The regions will overlap, but 3195 * bcopy() does the right thing. 3196 */ 3197 bcopy(params->mode_buf + sizeof(*header6), 3198 params->mode_buf + sizeof(*header10), 3199 len - sizeof(*header10)); 3200 3201 /* Make sure these fields are set correctly. */ 3202 scsi_ulto2b(0, header10->data_length); 3203 header10->medium_type = 0; 3204 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3205 3206 ccb->csio.dxfer_len = len; 3207 3208 scsi_ulto2b(len, ms10.length); 3209 ms10.control = ms6->control; 3210 bcopy(&ms10, cdb, 10); 3211 ccb->csio.cdb_len = 10; 3212 } 3213 3214 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3215 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3216 xpt_action(ccb); 3217 if (frozen) { 3218 cam_release_devq(ccb->ccb_h.path, 3219 /*relsim_flags*/0, 3220 /*openings*/0, 3221 /*timeout*/0, 3222 /*getcount_only*/0); 3223 } 3224 3225 return (ERESTART); 3226} 3227 3228static int 3229cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3230{ 3231 struct cd_softc *softc; 3232 struct cam_periph *periph; 3233 int error, error_code, sense_key, asc, ascq; 3234 3235 periph = xpt_path_periph(ccb->ccb_h.path); 3236 softc = (struct cd_softc *)periph->softc; 3237 3238 error = 0; 3239 3240 /* 3241 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3242 * CDB comes back with this particular error, try transforming it 3243 * into the 10 byte version. 3244 */ 3245 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3246 error = cd6byteworkaround(ccb); 3247 } else if (scsi_extract_sense_ccb(ccb, 3248 &error_code, &sense_key, &asc, &ascq)) { 3249 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3250 error = cd6byteworkaround(ccb); 3251 else if (sense_key == SSD_KEY_UNIT_ATTENTION && 3252 asc == 0x28 && ascq == 0x00) 3253 disk_media_changed(softc->disk, M_NOWAIT); 3254 else if (sense_key == SSD_KEY_NOT_READY && 3255 asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) { 3256 softc->flags &= ~CD_FLAG_SAW_MEDIA; 3257 disk_media_gone(softc->disk, M_NOWAIT); 3258 } 3259 } 3260 3261 if (error == ERESTART) 3262 return (error); 3263 3264 /* 3265 * XXX 3266 * Until we have a better way of doing pack validation, 3267 * don't treat UAs as errors. 3268 */ 3269 sense_flags |= SF_RETRY_UA; 3270 return (cam_periph_error(ccb, cam_flags, sense_flags, 3271 &softc->saved_ccb)); 3272} 3273 3274static void 3275cdmediapoll(void *arg) 3276{ 3277 struct cam_periph *periph = arg; 3278 struct cd_softc *softc = periph->softc; 3279 3280 if (softc->flags & CD_FLAG_CHANGER) 3281 return; 3282 3283 if (softc->state == CD_STATE_NORMAL && !softc->tur && 3284 softc->outstanding_cmds == 0) { 3285 if (cam_periph_acquire(periph) == CAM_REQ_CMP) { 3286 softc->tur = 1; 3287 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 3288 } 3289 } 3290 /* Queue us up again */ 3291 if (cd_poll_period != 0) 3292 callout_schedule(&softc->mediapoll_c, cd_poll_period * hz); 3293} 3294 3295/* 3296 * Read table of contents 3297 */ 3298static int 3299cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3300 u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 3301{ 3302 struct scsi_read_toc *scsi_cmd; 3303 u_int32_t ntoc; 3304 struct ccb_scsiio *csio; 3305 union ccb *ccb; 3306 int error; 3307 3308 ntoc = len; 3309 error = 0; 3310 3311 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3312 3313 csio = &ccb->csio; 3314 3315 cam_fill_csio(csio, 3316 /* retries */ cd_retry_count, 3317 /* cbfcnp */ cddone, 3318 /* flags */ CAM_DIR_IN, 3319 /* tag_action */ MSG_SIMPLE_Q_TAG, 3320 /* data_ptr */ data, 3321 /* dxfer_len */ len, 3322 /* sense_len */ SSD_FULL_SIZE, 3323 sizeof(struct scsi_read_toc), 3324 /* timeout */ 50000); 3325 3326 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 3327 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3328 3329 if (mode == CD_MSF_FORMAT) 3330 scsi_cmd->byte2 |= CD_MSF; 3331 scsi_cmd->from_track = start; 3332 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 3333 scsi_cmd->data_len[0] = (ntoc) >> 8; 3334 scsi_cmd->data_len[1] = (ntoc) & 0xff; 3335 3336 scsi_cmd->op_code = READ_TOC; 3337 3338 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3339 /*sense_flags*/SF_RETRY_UA | sense_flags); 3340 3341 xpt_release_ccb(ccb); 3342 3343 return(error); 3344} 3345 3346static int 3347cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 3348 u_int32_t format, int track, 3349 struct cd_sub_channel_info *data, u_int32_t len) 3350{ 3351 struct scsi_read_subchannel *scsi_cmd; 3352 struct ccb_scsiio *csio; 3353 union ccb *ccb; 3354 int error; 3355 3356 error = 0; 3357 3358 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3359 3360 csio = &ccb->csio; 3361 3362 cam_fill_csio(csio, 3363 /* retries */ cd_retry_count, 3364 /* cbfcnp */ cddone, 3365 /* flags */ CAM_DIR_IN, 3366 /* tag_action */ MSG_SIMPLE_Q_TAG, 3367 /* data_ptr */ (u_int8_t *)data, 3368 /* dxfer_len */ len, 3369 /* sense_len */ SSD_FULL_SIZE, 3370 sizeof(struct scsi_read_subchannel), 3371 /* timeout */ 50000); 3372 3373 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 3374 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3375 3376 scsi_cmd->op_code = READ_SUBCHANNEL; 3377 if (mode == CD_MSF_FORMAT) 3378 scsi_cmd->byte1 |= CD_MSF; 3379 scsi_cmd->byte2 = SRS_SUBQ; 3380 scsi_cmd->subchan_format = format; 3381 scsi_cmd->track = track; 3382 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 3383 scsi_cmd->control = 0; 3384 3385 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3386 /*sense_flags*/SF_RETRY_UA); 3387 3388 xpt_release_ccb(ccb); 3389 3390 return(error); 3391} 3392 3393 3394/* 3395 * All MODE_SENSE requests in the cd(4) driver MUST go through this 3396 * routine. See comments in cd6byteworkaround() for details. 3397 */ 3398static int 3399cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3400 u_int32_t page) 3401{ 3402 struct ccb_scsiio *csio; 3403 struct cd_softc *softc; 3404 union ccb *ccb; 3405 int param_len; 3406 int error; 3407 3408 softc = (struct cd_softc *)periph->softc; 3409 3410 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3411 3412 csio = &ccb->csio; 3413 3414 data->cdb_size = softc->minimum_command_size; 3415 if (data->cdb_size < 10) 3416 param_len = sizeof(struct cd_mode_data); 3417 else 3418 param_len = sizeof(struct cd_mode_data_10); 3419 3420 /* Don't say we've got more room than we actually allocated */ 3421 param_len = min(param_len, data->alloc_len); 3422 3423 scsi_mode_sense_len(csio, 3424 /* retries */ cd_retry_count, 3425 /* cbfcnp */ cddone, 3426 /* tag_action */ MSG_SIMPLE_Q_TAG, 3427 /* dbd */ 0, 3428 /* page_code */ SMS_PAGE_CTRL_CURRENT, 3429 /* page */ page, 3430 /* param_buf */ data->mode_buf, 3431 /* param_len */ param_len, 3432 /* minimum_cmd_size */ softc->minimum_command_size, 3433 /* sense_len */ SSD_FULL_SIZE, 3434 /* timeout */ 50000); 3435 3436 /* 3437 * It would be nice not to have to do this, but there's no 3438 * available pointer in the CCB that would allow us to stuff the 3439 * mode params structure in there and retrieve it in 3440 * cd6byteworkaround(), so we can set the cdb size. The cdb size 3441 * lets the caller know what CDB size we ended up using, so they 3442 * can find the actual mode page offset. 3443 */ 3444 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3445 3446 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3447 /*sense_flags*/SF_RETRY_UA); 3448 3449 xpt_release_ccb(ccb); 3450 3451 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3452 3453 /* 3454 * This is a bit of belt-and-suspenders checking, but if we run 3455 * into a situation where the target sends back multiple block 3456 * descriptors, we might not have enough space in the buffer to 3457 * see the whole mode page. Better to return an error than 3458 * potentially access memory beyond our malloced region. 3459 */ 3460 if (error == 0) { 3461 u_int32_t data_len; 3462 3463 if (data->cdb_size == 10) { 3464 struct scsi_mode_header_10 *hdr10; 3465 3466 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3467 data_len = scsi_2btoul(hdr10->data_length); 3468 data_len += sizeof(hdr10->data_length); 3469 } else { 3470 struct scsi_mode_header_6 *hdr6; 3471 3472 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3473 data_len = hdr6->data_length; 3474 data_len += sizeof(hdr6->data_length); 3475 } 3476 3477 /* 3478 * Complain if there is more mode data available than we 3479 * allocated space for. This could potentially happen if 3480 * we miscalculated the page length for some reason, if the 3481 * drive returns multiple block descriptors, or if it sets 3482 * the data length incorrectly. 3483 */ 3484 if (data_len > data->alloc_len) { 3485 xpt_print(periph->path, "allocated modepage %d length " 3486 "%d < returned length %d\n", page, data->alloc_len, 3487 data_len); 3488 error = ENOSPC; 3489 } 3490 } 3491 return (error); 3492} 3493 3494/* 3495 * All MODE_SELECT requests in the cd(4) driver MUST go through this 3496 * routine. See comments in cd6byteworkaround() for details. 3497 */ 3498static int 3499cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 3500{ 3501 struct ccb_scsiio *csio; 3502 struct cd_softc *softc; 3503 union ccb *ccb; 3504 int cdb_size, param_len; 3505 int error; 3506 3507 softc = (struct cd_softc *)periph->softc; 3508 3509 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3510 3511 csio = &ccb->csio; 3512 3513 error = 0; 3514 3515 /* 3516 * If the data is formatted for the 10 byte version of the mode 3517 * select parameter list, we need to use the 10 byte CDB. 3518 * Otherwise, we use whatever the stored minimum command size. 3519 */ 3520 if (data->cdb_size == 10) 3521 cdb_size = data->cdb_size; 3522 else 3523 cdb_size = softc->minimum_command_size; 3524 3525 if (cdb_size >= 10) { 3526 struct scsi_mode_header_10 *mode_header; 3527 u_int32_t data_len; 3528 3529 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3530 3531 data_len = scsi_2btoul(mode_header->data_length); 3532 3533 scsi_ulto2b(0, mode_header->data_length); 3534 /* 3535 * SONY drives do not allow a mode select with a medium_type 3536 * value that has just been returned by a mode sense; use a 3537 * medium_type of 0 (Default) instead. 3538 */ 3539 mode_header->medium_type = 0; 3540 3541 /* 3542 * Pass back whatever the drive passed to us, plus the size 3543 * of the data length field. 3544 */ 3545 param_len = data_len + sizeof(mode_header->data_length); 3546 3547 } else { 3548 struct scsi_mode_header_6 *mode_header; 3549 3550 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3551 3552 param_len = mode_header->data_length + 1; 3553 3554 mode_header->data_length = 0; 3555 /* 3556 * SONY drives do not allow a mode select with a medium_type 3557 * value that has just been returned by a mode sense; use a 3558 * medium_type of 0 (Default) instead. 3559 */ 3560 mode_header->medium_type = 0; 3561 } 3562 3563 /* Don't say we've got more room than we actually allocated */ 3564 param_len = min(param_len, data->alloc_len); 3565 3566 scsi_mode_select_len(csio, 3567 /* retries */ cd_retry_count, 3568 /* cbfcnp */ cddone, 3569 /* tag_action */ MSG_SIMPLE_Q_TAG, 3570 /* scsi_page_fmt */ 1, 3571 /* save_pages */ 0, 3572 /* param_buf */ data->mode_buf, 3573 /* param_len */ param_len, 3574 /* minimum_cmd_size */ cdb_size, 3575 /* sense_len */ SSD_FULL_SIZE, 3576 /* timeout */ 50000); 3577 3578 /* See comments in cdgetmode() and cd6byteworkaround(). */ 3579 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3580 3581 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3582 /*sense_flags*/SF_RETRY_UA); 3583 3584 xpt_release_ccb(ccb); 3585 3586 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3587 3588 return (error); 3589} 3590 3591 3592static int 3593cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 3594{ 3595 struct ccb_scsiio *csio; 3596 union ccb *ccb; 3597 int error; 3598 u_int8_t cdb_len; 3599 3600 error = 0; 3601 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3602 csio = &ccb->csio; 3603 /* 3604 * Use the smallest possible command to perform the operation. 3605 */ 3606 if ((len & 0xffff0000) == 0) { 3607 /* 3608 * We can fit in a 10 byte cdb. 3609 */ 3610 struct scsi_play_10 *scsi_cmd; 3611 3612 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 3613 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3614 scsi_cmd->op_code = PLAY_10; 3615 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3616 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 3617 cdb_len = sizeof(*scsi_cmd); 3618 } else { 3619 struct scsi_play_12 *scsi_cmd; 3620 3621 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 3622 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3623 scsi_cmd->op_code = PLAY_12; 3624 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3625 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 3626 cdb_len = sizeof(*scsi_cmd); 3627 } 3628 cam_fill_csio(csio, 3629 /*retries*/ cd_retry_count, 3630 cddone, 3631 /*flags*/CAM_DIR_NONE, 3632 MSG_SIMPLE_Q_TAG, 3633 /*dataptr*/NULL, 3634 /*datalen*/0, 3635 /*sense_len*/SSD_FULL_SIZE, 3636 cdb_len, 3637 /*timeout*/50 * 1000); 3638 3639 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3640 /*sense_flags*/SF_RETRY_UA); 3641 3642 xpt_release_ccb(ccb); 3643 3644 return(error); 3645} 3646 3647static int 3648cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 3649 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 3650{ 3651 struct scsi_play_msf *scsi_cmd; 3652 struct ccb_scsiio *csio; 3653 union ccb *ccb; 3654 int error; 3655 3656 error = 0; 3657 3658 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3659 3660 csio = &ccb->csio; 3661 3662 cam_fill_csio(csio, 3663 /* retries */ cd_retry_count, 3664 /* cbfcnp */ cddone, 3665 /* flags */ CAM_DIR_NONE, 3666 /* tag_action */ MSG_SIMPLE_Q_TAG, 3667 /* data_ptr */ NULL, 3668 /* dxfer_len */ 0, 3669 /* sense_len */ SSD_FULL_SIZE, 3670 sizeof(struct scsi_play_msf), 3671 /* timeout */ 50000); 3672 3673 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 3674 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3675 3676 scsi_cmd->op_code = PLAY_MSF; 3677 scsi_cmd->start_m = startm; 3678 scsi_cmd->start_s = starts; 3679 scsi_cmd->start_f = startf; 3680 scsi_cmd->end_m = endm; 3681 scsi_cmd->end_s = ends; 3682 scsi_cmd->end_f = endf; 3683 3684 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3685 /*sense_flags*/SF_RETRY_UA); 3686 3687 xpt_release_ccb(ccb); 3688 3689 return(error); 3690} 3691 3692 3693static int 3694cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 3695 u_int32_t etrack, u_int32_t eindex) 3696{ 3697 struct scsi_play_track *scsi_cmd; 3698 struct ccb_scsiio *csio; 3699 union ccb *ccb; 3700 int error; 3701 3702 error = 0; 3703 3704 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3705 3706 csio = &ccb->csio; 3707 3708 cam_fill_csio(csio, 3709 /* retries */ cd_retry_count, 3710 /* cbfcnp */ cddone, 3711 /* flags */ CAM_DIR_NONE, 3712 /* tag_action */ MSG_SIMPLE_Q_TAG, 3713 /* data_ptr */ NULL, 3714 /* dxfer_len */ 0, 3715 /* sense_len */ SSD_FULL_SIZE, 3716 sizeof(struct scsi_play_track), 3717 /* timeout */ 50000); 3718 3719 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3720 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3721 3722 scsi_cmd->op_code = PLAY_TRACK; 3723 scsi_cmd->start_track = strack; 3724 scsi_cmd->start_index = sindex; 3725 scsi_cmd->end_track = etrack; 3726 scsi_cmd->end_index = eindex; 3727 3728 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3729 /*sense_flags*/SF_RETRY_UA); 3730 3731 xpt_release_ccb(ccb); 3732 3733 return(error); 3734} 3735 3736static int 3737cdpause(struct cam_periph *periph, u_int32_t go) 3738{ 3739 struct scsi_pause *scsi_cmd; 3740 struct ccb_scsiio *csio; 3741 union ccb *ccb; 3742 int error; 3743 3744 error = 0; 3745 3746 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3747 3748 csio = &ccb->csio; 3749 3750 cam_fill_csio(csio, 3751 /* retries */ cd_retry_count, 3752 /* cbfcnp */ cddone, 3753 /* flags */ CAM_DIR_NONE, 3754 /* tag_action */ MSG_SIMPLE_Q_TAG, 3755 /* data_ptr */ NULL, 3756 /* dxfer_len */ 0, 3757 /* sense_len */ SSD_FULL_SIZE, 3758 sizeof(struct scsi_pause), 3759 /* timeout */ 50000); 3760 3761 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3762 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3763 3764 scsi_cmd->op_code = PAUSE; 3765 scsi_cmd->resume = go; 3766 3767 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3768 /*sense_flags*/SF_RETRY_UA); 3769 3770 xpt_release_ccb(ccb); 3771 3772 return(error); 3773} 3774 3775static int 3776cdstartunit(struct cam_periph *periph, int load) 3777{ 3778 union ccb *ccb; 3779 int error; 3780 3781 error = 0; 3782 3783 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3784 3785 scsi_start_stop(&ccb->csio, 3786 /* retries */ cd_retry_count, 3787 /* cbfcnp */ cddone, 3788 /* tag_action */ MSG_SIMPLE_Q_TAG, 3789 /* start */ TRUE, 3790 /* load_eject */ load, 3791 /* immediate */ FALSE, 3792 /* sense_len */ SSD_FULL_SIZE, 3793 /* timeout */ 50000); 3794 3795 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3796 /*sense_flags*/SF_RETRY_UA); 3797 3798 xpt_release_ccb(ccb); 3799 3800 return(error); 3801} 3802 3803static int 3804cdstopunit(struct cam_periph *periph, u_int32_t eject) 3805{ 3806 union ccb *ccb; 3807 int error; 3808 3809 error = 0; 3810 3811 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3812 3813 scsi_start_stop(&ccb->csio, 3814 /* retries */ cd_retry_count, 3815 /* cbfcnp */ cddone, 3816 /* tag_action */ MSG_SIMPLE_Q_TAG, 3817 /* start */ FALSE, 3818 /* load_eject */ eject, 3819 /* immediate */ FALSE, 3820 /* sense_len */ SSD_FULL_SIZE, 3821 /* timeout */ 50000); 3822 3823 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3824 /*sense_flags*/SF_RETRY_UA); 3825 3826 xpt_release_ccb(ccb); 3827 3828 return(error); 3829} 3830 3831static int 3832cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3833{ 3834 struct scsi_set_speed *scsi_cmd; 3835 struct ccb_scsiio *csio; 3836 union ccb *ccb; 3837 int error; 3838 3839 error = 0; 3840 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3841 csio = &ccb->csio; 3842 3843 /* Preserve old behavior: units in multiples of CDROM speed */ 3844 if (rdspeed < 177) 3845 rdspeed *= 177; 3846 if (wrspeed < 177) 3847 wrspeed *= 177; 3848 3849 cam_fill_csio(csio, 3850 /* retries */ cd_retry_count, 3851 /* cbfcnp */ cddone, 3852 /* flags */ CAM_DIR_NONE, 3853 /* tag_action */ MSG_SIMPLE_Q_TAG, 3854 /* data_ptr */ NULL, 3855 /* dxfer_len */ 0, 3856 /* sense_len */ SSD_FULL_SIZE, 3857 sizeof(struct scsi_set_speed), 3858 /* timeout */ 50000); 3859 3860 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3861 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3862 3863 scsi_cmd->opcode = SET_CD_SPEED; 3864 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3865 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3866 3867 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3868 /*sense_flags*/SF_RETRY_UA); 3869 3870 xpt_release_ccb(ccb); 3871 3872 return(error); 3873} 3874 3875static int 3876cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3877{ 3878 union ccb *ccb; 3879 u_int8_t *databuf; 3880 u_int32_t lba; 3881 int error; 3882 int length; 3883 3884 error = 0; 3885 databuf = NULL; 3886 lba = 0; 3887 3888 switch (authinfo->format) { 3889 case DVD_REPORT_AGID: 3890 length = sizeof(struct scsi_report_key_data_agid); 3891 break; 3892 case DVD_REPORT_CHALLENGE: 3893 length = sizeof(struct scsi_report_key_data_challenge); 3894 break; 3895 case DVD_REPORT_KEY1: 3896 length = sizeof(struct scsi_report_key_data_key1_key2); 3897 break; 3898 case DVD_REPORT_TITLE_KEY: 3899 length = sizeof(struct scsi_report_key_data_title); 3900 /* The lba field is only set for the title key */ 3901 lba = authinfo->lba; 3902 break; 3903 case DVD_REPORT_ASF: 3904 length = sizeof(struct scsi_report_key_data_asf); 3905 break; 3906 case DVD_REPORT_RPC: 3907 length = sizeof(struct scsi_report_key_data_rpc); 3908 break; 3909 case DVD_INVALIDATE_AGID: 3910 length = 0; 3911 break; 3912 default: 3913 return (EINVAL); 3914 } 3915 3916 if (length != 0) { 3917 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3918 } else 3919 databuf = NULL; 3920 3921 cam_periph_lock(periph); 3922 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3923 3924 scsi_report_key(&ccb->csio, 3925 /* retries */ cd_retry_count, 3926 /* cbfcnp */ cddone, 3927 /* tag_action */ MSG_SIMPLE_Q_TAG, 3928 /* lba */ lba, 3929 /* agid */ authinfo->agid, 3930 /* key_format */ authinfo->format, 3931 /* data_ptr */ databuf, 3932 /* dxfer_len */ length, 3933 /* sense_len */ SSD_FULL_SIZE, 3934 /* timeout */ 50000); 3935 3936 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3937 /*sense_flags*/SF_RETRY_UA); 3938 3939 if (error != 0) 3940 goto bailout; 3941 3942 if (ccb->csio.resid != 0) { 3943 xpt_print(periph->path, "warning, residual for report key " 3944 "command is %d\n", ccb->csio.resid); 3945 } 3946 3947 switch(authinfo->format) { 3948 case DVD_REPORT_AGID: { 3949 struct scsi_report_key_data_agid *agid_data; 3950 3951 agid_data = (struct scsi_report_key_data_agid *)databuf; 3952 3953 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3954 RKD_AGID_SHIFT; 3955 break; 3956 } 3957 case DVD_REPORT_CHALLENGE: { 3958 struct scsi_report_key_data_challenge *chal_data; 3959 3960 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3961 3962 bcopy(chal_data->challenge_key, authinfo->keychal, 3963 min(sizeof(chal_data->challenge_key), 3964 sizeof(authinfo->keychal))); 3965 break; 3966 } 3967 case DVD_REPORT_KEY1: { 3968 struct scsi_report_key_data_key1_key2 *key1_data; 3969 3970 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3971 3972 bcopy(key1_data->key1, authinfo->keychal, 3973 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3974 break; 3975 } 3976 case DVD_REPORT_TITLE_KEY: { 3977 struct scsi_report_key_data_title *title_data; 3978 3979 title_data = (struct scsi_report_key_data_title *)databuf; 3980 3981 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3982 RKD_TITLE_CPM_SHIFT; 3983 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3984 RKD_TITLE_CP_SEC_SHIFT; 3985 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3986 RKD_TITLE_CMGS_SHIFT; 3987 bcopy(title_data->title_key, authinfo->keychal, 3988 min(sizeof(title_data->title_key), 3989 sizeof(authinfo->keychal))); 3990 break; 3991 } 3992 case DVD_REPORT_ASF: { 3993 struct scsi_report_key_data_asf *asf_data; 3994 3995 asf_data = (struct scsi_report_key_data_asf *)databuf; 3996 3997 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3998 break; 3999 } 4000 case DVD_REPORT_RPC: { 4001 struct scsi_report_key_data_rpc *rpc_data; 4002 4003 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 4004 4005 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 4006 RKD_RPC_TYPE_SHIFT; 4007 authinfo->vend_rsts = 4008 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 4009 RKD_RPC_VENDOR_RESET_SHIFT; 4010 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 4011 authinfo->region = rpc_data->region_mask; 4012 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 4013 break; 4014 } 4015 case DVD_INVALIDATE_AGID: 4016 break; 4017 default: 4018 /* This should be impossible, since we checked above */ 4019 error = EINVAL; 4020 goto bailout; 4021 break; /* NOTREACHED */ 4022 } 4023 4024bailout: 4025 xpt_release_ccb(ccb); 4026 cam_periph_unlock(periph); 4027 4028 if (databuf != NULL) 4029 free(databuf, M_DEVBUF); 4030 4031 return(error); 4032} 4033 4034static int 4035cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 4036{ 4037 union ccb *ccb; 4038 u_int8_t *databuf; 4039 int length; 4040 int error; 4041 4042 error = 0; 4043 databuf = NULL; 4044 4045 switch(authinfo->format) { 4046 case DVD_SEND_CHALLENGE: { 4047 struct scsi_report_key_data_challenge *challenge_data; 4048 4049 length = sizeof(*challenge_data); 4050 4051 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4052 4053 databuf = (u_int8_t *)challenge_data; 4054 4055 scsi_ulto2b(length - sizeof(challenge_data->data_len), 4056 challenge_data->data_len); 4057 4058 bcopy(authinfo->keychal, challenge_data->challenge_key, 4059 min(sizeof(authinfo->keychal), 4060 sizeof(challenge_data->challenge_key))); 4061 break; 4062 } 4063 case DVD_SEND_KEY2: { 4064 struct scsi_report_key_data_key1_key2 *key2_data; 4065 4066 length = sizeof(*key2_data); 4067 4068 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4069 4070 databuf = (u_int8_t *)key2_data; 4071 4072 scsi_ulto2b(length - sizeof(key2_data->data_len), 4073 key2_data->data_len); 4074 4075 bcopy(authinfo->keychal, key2_data->key1, 4076 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 4077 4078 break; 4079 } 4080 case DVD_SEND_RPC: { 4081 struct scsi_send_key_data_rpc *rpc_data; 4082 4083 length = sizeof(*rpc_data); 4084 4085 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4086 4087 databuf = (u_int8_t *)rpc_data; 4088 4089 scsi_ulto2b(length - sizeof(rpc_data->data_len), 4090 rpc_data->data_len); 4091 4092 rpc_data->region_code = authinfo->region; 4093 break; 4094 } 4095 default: 4096 return (EINVAL); 4097 } 4098 4099 cam_periph_lock(periph); 4100 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4101 4102 scsi_send_key(&ccb->csio, 4103 /* retries */ cd_retry_count, 4104 /* cbfcnp */ cddone, 4105 /* tag_action */ MSG_SIMPLE_Q_TAG, 4106 /* agid */ authinfo->agid, 4107 /* key_format */ authinfo->format, 4108 /* data_ptr */ databuf, 4109 /* dxfer_len */ length, 4110 /* sense_len */ SSD_FULL_SIZE, 4111 /* timeout */ 50000); 4112 4113 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4114 /*sense_flags*/SF_RETRY_UA); 4115 4116 xpt_release_ccb(ccb); 4117 cam_periph_unlock(periph); 4118 4119 if (databuf != NULL) 4120 free(databuf, M_DEVBUF); 4121 4122 return(error); 4123} 4124 4125static int 4126cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 4127{ 4128 union ccb *ccb; 4129 u_int8_t *databuf; 4130 u_int32_t address; 4131 int error; 4132 int length; 4133 4134 error = 0; 4135 databuf = NULL; 4136 /* The address is reserved for many of the formats */ 4137 address = 0; 4138 4139 switch(dvdstruct->format) { 4140 case DVD_STRUCT_PHYSICAL: 4141 length = sizeof(struct scsi_read_dvd_struct_data_physical); 4142 break; 4143 case DVD_STRUCT_COPYRIGHT: 4144 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 4145 break; 4146 case DVD_STRUCT_DISCKEY: 4147 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 4148 break; 4149 case DVD_STRUCT_BCA: 4150 length = sizeof(struct scsi_read_dvd_struct_data_bca); 4151 break; 4152 case DVD_STRUCT_MANUFACT: 4153 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 4154 break; 4155 case DVD_STRUCT_CMI: 4156 return (ENODEV); 4157 case DVD_STRUCT_PROTDISCID: 4158 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 4159 break; 4160 case DVD_STRUCT_DISCKEYBLOCK: 4161 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 4162 break; 4163 case DVD_STRUCT_DDS: 4164 length = sizeof(struct scsi_read_dvd_struct_data_dds); 4165 break; 4166 case DVD_STRUCT_MEDIUM_STAT: 4167 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 4168 break; 4169 case DVD_STRUCT_SPARE_AREA: 4170 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 4171 break; 4172 case DVD_STRUCT_RMD_LAST: 4173 return (ENODEV); 4174 case DVD_STRUCT_RMD_RMA: 4175 return (ENODEV); 4176 case DVD_STRUCT_PRERECORDED: 4177 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 4178 break; 4179 case DVD_STRUCT_UNIQUEID: 4180 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 4181 break; 4182 case DVD_STRUCT_DCB: 4183 return (ENODEV); 4184 case DVD_STRUCT_LIST: 4185 /* 4186 * This is the maximum allocation length for the READ DVD 4187 * STRUCTURE command. There's nothing in the MMC3 spec 4188 * that indicates a limit in the amount of data that can 4189 * be returned from this call, other than the limits 4190 * imposed by the 2-byte length variables. 4191 */ 4192 length = 65535; 4193 break; 4194 default: 4195 return (EINVAL); 4196 } 4197 4198 if (length != 0) { 4199 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4200 } else 4201 databuf = NULL; 4202 4203 cam_periph_lock(periph); 4204 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4205 4206 scsi_read_dvd_structure(&ccb->csio, 4207 /* retries */ cd_retry_count, 4208 /* cbfcnp */ cddone, 4209 /* tag_action */ MSG_SIMPLE_Q_TAG, 4210 /* lba */ address, 4211 /* layer_number */ dvdstruct->layer_num, 4212 /* key_format */ dvdstruct->format, 4213 /* agid */ dvdstruct->agid, 4214 /* data_ptr */ databuf, 4215 /* dxfer_len */ length, 4216 /* sense_len */ SSD_FULL_SIZE, 4217 /* timeout */ 50000); 4218 4219 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4220 /*sense_flags*/SF_RETRY_UA); 4221 4222 if (error != 0) 4223 goto bailout; 4224 4225 switch(dvdstruct->format) { 4226 case DVD_STRUCT_PHYSICAL: { 4227 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 4228 struct dvd_layer *outlayer; 4229 struct scsi_read_dvd_struct_data_physical *phys_data; 4230 4231 phys_data = 4232 (struct scsi_read_dvd_struct_data_physical *)databuf; 4233 inlayer = &phys_data->layer_desc; 4234 outlayer = (struct dvd_layer *)&dvdstruct->data; 4235 4236 dvdstruct->length = sizeof(*inlayer); 4237 4238 outlayer->book_type = (inlayer->book_type_version & 4239 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 4240 outlayer->book_version = (inlayer->book_type_version & 4241 RDSD_BOOK_VERSION_MASK); 4242 outlayer->disc_size = (inlayer->disc_size_max_rate & 4243 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 4244 outlayer->max_rate = (inlayer->disc_size_max_rate & 4245 RDSD_MAX_RATE_MASK); 4246 outlayer->nlayers = (inlayer->layer_info & 4247 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 4248 outlayer->track_path = (inlayer->layer_info & 4249 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 4250 outlayer->layer_type = (inlayer->layer_info & 4251 RDSD_LAYER_TYPE_MASK); 4252 outlayer->linear_density = (inlayer->density & 4253 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 4254 outlayer->track_density = (inlayer->density & 4255 RDSD_TRACK_DENSITY_MASK); 4256 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 4257 RDSD_BCA_SHIFT; 4258 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 4259 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 4260 outlayer->end_sector_l0 = 4261 scsi_3btoul(inlayer->end_sector_layer0); 4262 break; 4263 } 4264 case DVD_STRUCT_COPYRIGHT: { 4265 struct scsi_read_dvd_struct_data_copyright *copy_data; 4266 4267 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 4268 databuf; 4269 4270 dvdstruct->cpst = copy_data->cps_type; 4271 dvdstruct->rmi = copy_data->region_info; 4272 dvdstruct->length = 0; 4273 4274 break; 4275 } 4276 default: 4277 /* 4278 * Tell the user what the overall length is, no matter 4279 * what we can actually fit in the data buffer. 4280 */ 4281 dvdstruct->length = length - ccb->csio.resid - 4282 sizeof(struct scsi_read_dvd_struct_data_header); 4283 4284 /* 4285 * But only actually copy out the smaller of what we read 4286 * in or what the structure can take. 4287 */ 4288 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 4289 dvdstruct->data, 4290 min(sizeof(dvdstruct->data), dvdstruct->length)); 4291 break; 4292 } 4293 4294bailout: 4295 xpt_release_ccb(ccb); 4296 cam_periph_unlock(periph); 4297 4298 if (databuf != NULL) 4299 free(databuf, M_DEVBUF); 4300 4301 return(error); 4302} 4303 4304void 4305scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 4306 void (*cbfcnp)(struct cam_periph *, union ccb *), 4307 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 4308 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 4309 u_int8_t sense_len, u_int32_t timeout) 4310{ 4311 struct scsi_report_key *scsi_cmd; 4312 4313 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 4314 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4315 scsi_cmd->opcode = REPORT_KEY; 4316 scsi_ulto4b(lba, scsi_cmd->lba); 4317 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4318 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4319 (key_format & RK_KF_KEYFORMAT_MASK); 4320 4321 cam_fill_csio(csio, 4322 retries, 4323 cbfcnp, 4324 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 4325 tag_action, 4326 /*data_ptr*/ data_ptr, 4327 /*dxfer_len*/ dxfer_len, 4328 sense_len, 4329 sizeof(*scsi_cmd), 4330 timeout); 4331} 4332 4333void 4334scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 4335 void (*cbfcnp)(struct cam_periph *, union ccb *), 4336 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 4337 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 4338 u_int32_t timeout) 4339{ 4340 struct scsi_send_key *scsi_cmd; 4341 4342 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 4343 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4344 scsi_cmd->opcode = SEND_KEY; 4345 4346 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 4347 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4348 (key_format & RK_KF_KEYFORMAT_MASK); 4349 4350 cam_fill_csio(csio, 4351 retries, 4352 cbfcnp, 4353 /*flags*/ CAM_DIR_OUT, 4354 tag_action, 4355 /*data_ptr*/ data_ptr, 4356 /*dxfer_len*/ dxfer_len, 4357 sense_len, 4358 sizeof(*scsi_cmd), 4359 timeout); 4360} 4361 4362 4363void 4364scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 4365 void (*cbfcnp)(struct cam_periph *, union ccb *), 4366 u_int8_t tag_action, u_int32_t address, 4367 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 4368 u_int8_t *data_ptr, u_int32_t dxfer_len, 4369 u_int8_t sense_len, u_int32_t timeout) 4370{ 4371 struct scsi_read_dvd_structure *scsi_cmd; 4372 4373 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 4374 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4375 scsi_cmd->opcode = READ_DVD_STRUCTURE; 4376 4377 scsi_ulto4b(address, scsi_cmd->address); 4378 scsi_cmd->layer_number = layer_number; 4379 scsi_cmd->format = format; 4380 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4381 /* The AGID is the top two bits of this byte */ 4382 scsi_cmd->agid = agid << 6; 4383 4384 cam_fill_csio(csio, 4385 retries, 4386 cbfcnp, 4387 /*flags*/ CAM_DIR_IN, 4388 tag_action, 4389 /*data_ptr*/ data_ptr, 4390 /*dxfer_len*/ dxfer_len, 4391 sense_len, 4392 sizeof(*scsi_cmd), 4393 timeout); 4394} 4395