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