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