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