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