1 /* 2 * Copyright (c) 1997 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998 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 * $Id: scsi_cd.c,v 1.7 1998/10/15 17:46:26 ken Exp $ 28 */ 29 /* 30 * Portions of this driver taken from the original FreeBSD cd driver. 31 * Written by Julian Elischer (julian@tfs.com) 32 * for TRW Financial Systems for use under the MACH(2.5) operating system. 33 * 34 * TRW Financial Systems, in accordance with their agreement with Carnegie 35 * Mellon University, makes this software available to CMU to distribute 36 * or use in any manner that they see fit as long as this message is kept with 37 * the software. For this reason TFS also grants any other persons or 38 * organisations permission to use or modify this software. 39 * 40 * TFS supplies this software to be publicly redistributed 41 * on the understanding that TFS is not responsible for the correct 42 * functioning of this software in any circumstances. 43 * 44 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 45 * 46 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $ 47 */ 48 49 #include "opt_cd.h" 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/kernel.h> 54 #include <sys/buf.h> 55 #include <sys/dkbad.h> 56 #include <sys/disklabel.h> 57 #include <sys/diskslice.h> 58 #include <sys/malloc.h> 59 #include <sys/conf.h> 60 #include <sys/cdio.h> 61 #include <sys/devicestat.h> 62 #include <sys/sysctl.h> 63 64 #include <cam/cam.h> 65 #include <cam/cam_ccb.h> 66 #include <cam/cam_extend.h> 67 #include <cam/cam_periph.h> 68 #include <cam/cam_xpt_periph.h> 69 #include <cam/cam_queue.h> 70 71 #include <cam/scsi/scsi_message.h> 72 #include <cam/scsi/scsi_da.h> 73 #include <cam/scsi/scsi_cd.h> 74 75 #define LEADOUT 0xaa /* leadout toc entry */ 76 77 struct cd_params { 78 u_int32_t blksize; 79 u_long disksize; 80 }; 81 82 typedef enum { 83 CD_Q_NONE = 0x00, 84 CD_Q_NO_TOUCH = 0x01, 85 CD_Q_BCD_TRACKS = 0x02, 86 CD_Q_NO_CHANGER = 0x04, 87 CD_Q_CHANGER = 0x08 88 } cd_quirks; 89 90 typedef enum { 91 CD_FLAG_INVALID = 0x001, 92 CD_FLAG_NEW_DISC = 0x002, 93 CD_FLAG_DISC_LOCKED = 0x004, 94 CD_FLAG_DISC_REMOVABLE = 0x008, 95 CD_FLAG_TAGGED_QUEUING = 0x010, 96 CD_FLAG_OPEN = 0x020, 97 CD_FLAG_CHANGER = 0x040, 98 CD_FLAG_ACTIVE = 0x080, 99 CD_FLAG_SCHED_ON_COMP = 0x100, 100 CD_FLAG_RETRY_UA = 0x200 101 } cd_flags; 102 103 typedef enum { 104 CD_CCB_PROBE = 0x01, 105 CD_CCB_BUFFER_IO = 0x02, 106 CD_CCB_WAITING = 0x03, 107 CD_CCB_TYPE_MASK = 0x0F, 108 CD_CCB_RETRY_UA = 0x10 109 } cd_ccb_state; 110 111 typedef enum { 112 CHANGER_TIMEOUT_SCHED = 0x01, 113 CHANGER_SHORT_TMOUT_SCHED = 0x02, 114 CHANGER_MANUAL_CALL = 0x04, 115 CHANGER_NEED_TIMEOUT = 0x08 116 } cd_changer_flags; 117 118 #define ccb_state ppriv_field0 119 #define ccb_bp ppriv_ptr1 120 121 typedef enum { 122 CD_STATE_PROBE, 123 CD_STATE_NORMAL 124 } cd_state; 125 126 struct cd_softc { 127 cam_pinfo pinfo; 128 cd_state state; 129 cd_flags flags; 130 struct buf_queue_head buf_queue; 131 LIST_HEAD(, ccb_hdr) pending_ccbs; 132 struct cd_params params; 133 struct diskslices *cd_slices; 134 union ccb saved_ccb; 135 cd_quirks quirks; 136 struct devstat device_stats; 137 STAILQ_ENTRY(cd_softc) changer_links; 138 struct cdchanger *changer; 139 int bufs_left; 140 struct cam_periph *periph; 141 }; 142 143 struct cd_quirk_entry { 144 struct scsi_inquiry_pattern inq_pat; 145 cd_quirks quirks; 146 }; 147 148 /* 149 * These quirk entries aren't strictly necessary. Basically, what they do 150 * is tell cdregister() up front that a device is a changer. Otherwise, it 151 * will figure that fact out once it sees a LUN on the device that is 152 * greater than 0. If it is known up front that a device is a changer, all 153 * I/O to the device will go through the changer scheduling routines, as 154 * opposed to the "normal" CD code. 155 */ 156 static struct cd_quirk_entry cd_quirk_table[] = 157 { 158 { 159 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 160 /*quirks*/ CD_Q_CHANGER 161 }, 162 { 163 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM-604X", 164 "*"}, /* quirks */ CD_Q_CHANGER 165 }, 166 { 167 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 168 /* quirks */ CD_Q_BCD_TRACKS 169 } 170 }; 171 172 #ifndef MIN 173 #define MIN(x,y) ((x<y) ? x : y) 174 #endif 175 176 #define CD_CDEV_MAJOR 15 177 #define CD_BDEV_MAJOR 6 178 179 static d_open_t cdopen; 180 static d_read_t cdread; 181 static d_close_t cdclose; 182 static d_ioctl_t cdioctl; 183 static d_strategy_t cdstrategy; 184 static d_strategy_t cdstrategy1; 185 186 static periph_init_t cdinit; 187 static periph_ctor_t cdregister; 188 static periph_dtor_t cdcleanup; 189 static periph_start_t cdstart; 190 static periph_oninv_t cdoninvalidate; 191 static void cdasync(void *callback_arg, u_int32_t code, 192 struct cam_path *path, void *arg); 193 static void cdshorttimeout(void *arg); 194 static void cdschedule(struct cam_periph *periph, int priority); 195 static void cdrunchangerqueue(void *arg); 196 static void cdchangerschedule(struct cd_softc *softc); 197 static int cdrunccb(union ccb *ccb, 198 int (*error_routine)(union ccb *ccb, 199 u_int32_t cam_flags, 200 u_int32_t sense_flags), 201 u_int32_t cam_flags, u_int32_t sense_flags); 202 union ccb *cdgetccb(struct cam_periph *periph, 203 u_int32_t priority); 204 static void cddone(struct cam_periph *periph, 205 union ccb *start_ccb); 206 static int cderror(union ccb *ccb, u_int32_t cam_flags, 207 u_int32_t sense_flags); 208 static void cdprevent(struct cam_periph *periph, int action); 209 static int cdsize(dev_t dev, u_int32_t *size); 210 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 211 u_int32_t start, struct cd_toc_entry *data, 212 u_int32_t len); 213 static int cdgetmode(struct cam_periph *periph, 214 struct cd_mode_data *data, u_int32_t page); 215 static int cdsetmode(struct cam_periph *periph, 216 struct cd_mode_data *data); 217 static int cdplay(struct cam_periph *periph, u_int32_t blk, 218 u_int32_t len); 219 static int cdreadsubchannel(struct cam_periph *periph, 220 u_int32_t mode, u_int32_t format, 221 int track, 222 struct cd_sub_channel_info *data, 223 u_int32_t len); 224 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 225 u_int32_t starts, u_int32_t startf, 226 u_int32_t endm, u_int32_t ends, 227 u_int32_t endf); 228 static int cdplaytracks(struct cam_periph *periph, 229 u_int32_t strack, u_int32_t sindex, 230 u_int32_t etrack, u_int32_t eindex); 231 static int cdpause(struct cam_periph *periph, u_int32_t go); 232 static int cdstopunit(struct cam_periph *periph, u_int32_t eject); 233 static int cdstartunit(struct cam_periph *periph); 234 235 static struct periph_driver cddriver = 236 { 237 cdinit, "cd", 238 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 239 }; 240 241 DATA_SET(periphdriver_set, cddriver); 242 243 /* For 2.2-stable support */ 244 #ifndef D_DISK 245 #define D_DISK 0 246 #endif 247 static struct cdevsw cd_cdevsw = 248 { 249 /*d_open*/ cdopen, 250 /*d_close*/ cdclose, 251 /*d_read*/ cdread, 252 /*d_write*/ nowrite, 253 /*d_ioctl*/ cdioctl, 254 /*d_stop*/ nostop, 255 /*d_reset*/ noreset, 256 /*d_devtotty*/ nodevtotty, 257 /*d_poll*/ seltrue, 258 /*d_mmap*/ nommap, 259 /*d_strategy*/ cdstrategy, 260 /*d_name*/ "cd", 261 /*d_spare*/ NULL, 262 /*d_maj*/ -1, 263 /*d_dump*/ nodump, 264 /*d_psize*/ nopsize, 265 /*d_flags*/ D_DISK, 266 /*d_maxio*/ 0, 267 /*b_maj*/ -1 268 }; 269 270 static struct extend_array *cdperiphs; 271 static int num_changers; 272 273 #ifndef CHANGER_MIN_BUSY_SECONDS 274 #define CHANGER_MIN_BUSY_SECONDS 2 275 #endif 276 #ifndef CHANGER_MAX_BUSY_SECONDS 277 #define CHANGER_MAX_BUSY_SECONDS 10 278 #endif 279 280 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 281 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 282 283 /* 284 * XXX KDM this CAM node should be moved if we ever get more CAM sysctl 285 * variables. 286 */ 287 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem"); 288 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 289 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 290 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 291 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 292 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 293 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 294 295 struct cdchanger { 296 path_id_t path_id; 297 target_id_t target_id; 298 int num_devices; 299 struct camq devq; 300 struct timeval start_time; 301 struct cd_softc *cur_device; 302 struct callout_handle short_handle; 303 struct callout_handle long_handle; 304 cd_changer_flags flags; 305 STAILQ_ENTRY(cdchanger) changer_links; 306 STAILQ_HEAD(chdevlist, cd_softc) chluns; 307 }; 308 309 STAILQ_HEAD(changerlist, cdchanger) changerq; 310 311 void 312 cdinit(void) 313 { 314 cam_status status; 315 struct cam_path *path; 316 317 /* 318 * Create our extend array for storing the devices we attach to. 319 */ 320 cdperiphs = cam_extend_new(); 321 if (cdperiphs == NULL) { 322 printf("cd: Failed to alloc extend array!\n"); 323 return; 324 } 325 326 /* 327 * Install a global async callback. This callback will 328 * receive async callbacks like "new device found". 329 */ 330 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 331 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 332 333 if (status == CAM_REQ_CMP) { 334 struct ccb_setasync csa; 335 336 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 337 csa.ccb_h.func_code = XPT_SASYNC_CB; 338 csa.event_enable = AC_FOUND_DEVICE; 339 csa.callback = cdasync; 340 csa.callback_arg = NULL; 341 xpt_action((union ccb *)&csa); 342 status = csa.ccb_h.status; 343 xpt_free_path(path); 344 } 345 346 if (status != CAM_REQ_CMP) { 347 printf("cd: Failed to attach master async callback " 348 "due to status 0x%x!\n", status); 349 } else { 350 /* If we were successfull, register our devsw */ 351 cdevsw_add_generic(CD_BDEV_MAJOR, CD_CDEV_MAJOR, &cd_cdevsw); 352 } 353 } 354 355 static void 356 cdoninvalidate(struct cam_periph *periph) 357 { 358 int s; 359 struct cd_softc *softc; 360 struct buf *q_bp; 361 struct ccb_setasync csa; 362 363 softc = (struct cd_softc *)periph->softc; 364 365 /* 366 * De-register any async callbacks. 367 */ 368 xpt_setup_ccb(&csa.ccb_h, periph->path, 369 /* priority */ 5); 370 csa.ccb_h.func_code = XPT_SASYNC_CB; 371 csa.event_enable = 0; 372 csa.callback = cdasync; 373 csa.callback_arg = periph; 374 xpt_action((union ccb *)&csa); 375 376 softc->flags |= CD_FLAG_INVALID; 377 378 /* 379 * Although the oninvalidate() routines are always called at 380 * splsoftcam, we need to be at splbio() here to keep the buffer 381 * queue from being modified while we traverse it. 382 */ 383 s = splbio(); 384 385 /* 386 * Return all queued I/O with ENXIO. 387 * XXX Handle any transactions queued to the card 388 * with XPT_ABORT_CCB. 389 */ 390 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){ 391 bufq_remove(&softc->buf_queue, q_bp); 392 q_bp->b_resid = q_bp->b_bcount; 393 q_bp->b_error = ENXIO; 394 q_bp->b_flags |= B_ERROR; 395 biodone(q_bp); 396 } 397 splx(s); 398 399 /* 400 * If this device is part of a changer, and it was scheduled 401 * to run, remove it from the run queue since we just nuked 402 * all of its scheduled I/O. 403 */ 404 if ((softc->flags & CD_FLAG_CHANGER) 405 && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 406 camq_remove(&softc->changer->devq, softc->pinfo.index); 407 408 xpt_print_path(periph->path); 409 printf("lost device\n"); 410 } 411 412 static void 413 cdcleanup(struct cam_periph *periph) 414 { 415 struct cd_softc *softc; 416 int s; 417 418 softc = (struct cd_softc *)periph->softc; 419 420 xpt_print_path(periph->path); 421 printf("removing device entry\n"); 422 423 s = splsoftcam(); 424 /* 425 * In the queued, non-active case, the device in question 426 * has already been removed from the changer run queue. Since this 427 * device is active, we need to de-activate it, and schedule 428 * another device to run. (if there is another one to run) 429 */ 430 if ((softc->flags & CD_FLAG_CHANGER) 431 && (softc->flags & CD_FLAG_ACTIVE)) { 432 433 /* 434 * The purpose of the short timeout is soley to determine 435 * whether the current device has finished or not. Well, 436 * since we're removing the active device, we know that it 437 * is finished. So, get rid of the short timeout. 438 * Otherwise, if we're in the time period before the short 439 * timeout fires, and there are no other devices in the 440 * queue to run, there won't be any other device put in the 441 * active slot. i.e., when we call cdrunchangerqueue() 442 * below, it won't do anything. Then, when the short 443 * timeout fires, it'll look at the "current device", which 444 * we are free below, and possibly panic the kernel on a 445 * bogus pointer reference. 446 * 447 * The long timeout doesn't really matter, since we 448 * decrement the qfrozen_cnt to indicate that there is 449 * nothing in the active slot now. Therefore, there won't 450 * be any bogus pointer references there. 451 */ 452 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 453 untimeout(cdshorttimeout, softc->changer, 454 softc->changer->short_handle); 455 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 456 } 457 softc->changer->devq.qfrozen_cnt--; 458 softc->changer->flags |= CHANGER_MANUAL_CALL; 459 cdrunchangerqueue(softc->changer); 460 } 461 462 /* 463 * If we're removing the last device on the changer, go ahead and 464 * remove the changer device structure. 465 */ 466 if ((softc->flags & CD_FLAG_CHANGER) 467 && (--softc->changer->num_devices == 0)) { 468 469 /* 470 * Theoretically, there shouldn't be any timeouts left, but 471 * I'm not completely sure that that will be the case. So, 472 * it won't hurt to check and see if there are any left. 473 */ 474 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 475 untimeout(cdrunchangerqueue, softc->changer, 476 softc->changer->long_handle); 477 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 478 } 479 480 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 481 untimeout(cdshorttimeout, softc->changer, 482 softc->changer->short_handle); 483 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 484 } 485 486 STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 487 changer_links); 488 xpt_print_path(periph->path); 489 printf("removing changer entry\n"); 490 free(softc->changer, M_DEVBUF); 491 num_changers--; 492 } 493 devstat_remove_entry(&softc->device_stats); 494 cam_extend_release(cdperiphs, periph->unit_number); 495 free(softc, M_DEVBUF); 496 splx(s); 497 } 498 499 static void 500 cdasync(void *callback_arg, u_int32_t code, 501 struct cam_path *path, void *arg) 502 { 503 struct cam_periph *periph; 504 505 periph = (struct cam_periph *)callback_arg; 506 switch (code) { 507 case AC_FOUND_DEVICE: 508 { 509 struct ccb_getdev *cgd; 510 cam_status status; 511 512 cgd = (struct ccb_getdev *)arg; 513 514 if ((cgd->pd_type != T_CDROM) && (cgd->pd_type != T_WORM)) 515 break; 516 517 /* 518 * Allocate a peripheral instance for 519 * this device and start the probe 520 * process. 521 */ 522 status = cam_periph_alloc(cdregister, cdoninvalidate, 523 cdcleanup, cdstart, 524 "cd", CAM_PERIPH_BIO, 525 cgd->ccb_h.path, cdasync, 526 AC_FOUND_DEVICE, cgd); 527 528 if (status != CAM_REQ_CMP 529 && status != CAM_REQ_INPROG) 530 printf("cdasync: Unable to attach new device " 531 "due to status 0x%x\n", status); 532 533 break; 534 } 535 case AC_LOST_DEVICE: 536 cam_periph_invalidate(periph); 537 break; 538 case AC_SENT_BDR: 539 case AC_BUS_RESET: 540 { 541 struct cd_softc *softc; 542 struct ccb_hdr *ccbh; 543 int s; 544 545 softc = (struct cd_softc *)periph->softc; 546 s = splsoftcam(); 547 /* 548 * Don't fail on the expected unit attention 549 * that will occur. 550 */ 551 softc->flags |= CD_FLAG_RETRY_UA; 552 for (ccbh = LIST_FIRST(&softc->pending_ccbs); 553 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le)) 554 ccbh->ccb_state |= CD_CCB_RETRY_UA; 555 splx(s); 556 break; 557 } 558 case AC_TRANSFER_NEG: 559 case AC_SCSI_AEN: 560 case AC_UNSOL_RESEL: 561 default: 562 break; 563 } 564 } 565 566 static cam_status 567 cdregister(struct cam_periph *periph, void *arg) 568 { 569 struct cd_softc *softc; 570 struct ccb_setasync csa; 571 struct ccb_getdev *cgd; 572 caddr_t match; 573 574 cgd = (struct ccb_getdev *)arg; 575 if (periph == NULL) { 576 printf("cdregister: periph was NULL!!\n"); 577 return(CAM_REQ_CMP_ERR); 578 } 579 if (cgd == NULL) { 580 printf("cdregister: no getdev CCB, can't register device\n"); 581 return(CAM_REQ_CMP_ERR); 582 } 583 584 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 585 586 if (softc == NULL) { 587 printf("cdregister: Unable to probe new device. " 588 "Unable to allocate softc\n"); 589 return(CAM_REQ_CMP_ERR); 590 } 591 592 bzero(softc, sizeof(*softc)); 593 LIST_INIT(&softc->pending_ccbs); 594 softc->state = CD_STATE_PROBE; 595 bufq_init(&softc->buf_queue); 596 if (SID_IS_REMOVABLE(&cgd->inq_data)) 597 softc->flags |= CD_FLAG_DISC_REMOVABLE; 598 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 599 softc->flags |= CD_FLAG_TAGGED_QUEUING; 600 601 periph->softc = softc; 602 softc->periph = periph; 603 604 cam_extend_set(cdperiphs, periph->unit_number, periph); 605 606 /* 607 * See if this device has any quirks. 608 */ 609 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 610 (caddr_t)cd_quirk_table, 611 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 612 sizeof(*cd_quirk_table), scsi_inquiry_match); 613 614 if (match != NULL) 615 softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 616 else 617 softc->quirks = CD_Q_NONE; 618 619 /* 620 * We need to register the statistics structure for this device, 621 * but we don't have the blocksize yet for it. So, we register 622 * the structure and indicate that we don't have the blocksize 623 * yet. Unlike other SCSI peripheral drivers, we explicitly set 624 * the device type here to be CDROM, rather than just ORing in 625 * cgd->pd_type. This is because this driver can attach to either 626 * CDROM or WORM devices, and we want this peripheral driver to 627 * show up in the devstat list as a CD peripheral driver, not a 628 * WORM peripheral driver. WORM drives will also have the WORM 629 * driver attached to them. 630 */ 631 devstat_add_entry(&softc->device_stats, "cd", 632 periph->unit_number, 0, 633 DEVSTAT_BS_UNAVAILABLE, 634 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI); 635 636 /* 637 * Add an async callback so that we get 638 * notified if this device goes away. 639 */ 640 xpt_setup_ccb(&csa.ccb_h, periph->path, 641 /* priority */ 5); 642 csa.ccb_h.func_code = XPT_SASYNC_CB; 643 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 644 csa.callback = cdasync; 645 csa.callback_arg = periph; 646 xpt_action((union ccb *)&csa); 647 648 /* 649 * If the target lun is greater than 0, we most likely have a CD 650 * changer device. Check the quirk entries as well, though, just 651 * in case someone has a CD tower with one lun per drive or 652 * something like that. Also, if we know up front that a 653 * particular device is a changer, we can mark it as such starting 654 * with lun 0, instead of lun 1. It shouldn't be necessary to have 655 * a quirk entry to define something as a changer, however. 656 */ 657 if (((cgd->ccb_h.target_lun > 0) 658 && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 659 || ((softc->quirks & CD_Q_CHANGER) != 0)) { 660 struct cdchanger *nchanger; 661 struct cam_periph *nperiph; 662 struct cam_path *path; 663 cam_status status; 664 int found; 665 666 /* Set the changer flag in the current device's softc */ 667 softc->flags |= CD_FLAG_CHANGER; 668 669 if (num_changers == 0) 670 STAILQ_INIT(&changerq); 671 672 /* 673 * Now, look around for an existing changer device with the 674 * same path and target ID as the current device. 675 */ 676 for (found = 0, 677 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 678 nchanger != NULL; 679 nchanger = STAILQ_NEXT(nchanger, changer_links)){ 680 if ((nchanger->path_id == cgd->ccb_h.path_id) 681 && (nchanger->target_id == cgd->ccb_h.target_id)) { 682 found = 1; 683 break; 684 } 685 } 686 687 /* 688 * If we found a matching entry, just add this device to 689 * the list of devices on this changer. 690 */ 691 if (found == 1) { 692 struct chdevlist *chlunhead; 693 694 chlunhead = &nchanger->chluns; 695 696 /* 697 * XXX KDM look at consolidating this code with the 698 * code below in a separate function. 699 */ 700 701 /* 702 * Create a path with lun id 0, and see if we can 703 * find a matching device 704 */ 705 status = xpt_create_path(&path, /*periph*/ periph, 706 cgd->ccb_h.path_id, 707 cgd->ccb_h.target_id, 0); 708 709 if ((status == CAM_REQ_CMP) 710 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 711 struct cd_softc *nsoftc; 712 713 nsoftc = (struct cd_softc *)nperiph->softc; 714 715 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 716 nsoftc->flags |= CD_FLAG_CHANGER; 717 nchanger->num_devices++; 718 if (camq_resize(&nchanger->devq, 719 nchanger->num_devices)!=CAM_REQ_CMP){ 720 printf("cdregister: " 721 "camq_resize " 722 "failed, changer " 723 "support may " 724 "be messed up\n"); 725 } 726 nsoftc->changer = nchanger; 727 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 728 729 STAILQ_INSERT_TAIL(&nchanger->chluns, 730 nsoftc,changer_links); 731 } 732 } else if (status == CAM_REQ_CMP) 733 xpt_free_path(path); 734 else { 735 printf("cdregister: unable to allocate path\n" 736 "cdregister: changer support may be " 737 "broken\n"); 738 } 739 740 nchanger->num_devices++; 741 742 softc->changer = nchanger; 743 softc->pinfo.index = CAM_UNQUEUED_INDEX; 744 745 if (camq_resize(&nchanger->devq, 746 nchanger->num_devices) != CAM_REQ_CMP) { 747 printf("cdregister: camq_resize " 748 "failed, changer support may " 749 "be messed up\n"); 750 } 751 752 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 753 } 754 /* 755 * In this case, we don't already have an entry for this 756 * particular changer, so we need to create one, add it to 757 * the queue, and queue this device on the list for this 758 * changer. Before we queue this device, however, we need 759 * to search for lun id 0 on this target, and add it to the 760 * queue first, if it exists. (and if it hasn't already 761 * been marked as part of the changer.) 762 */ 763 else { 764 nchanger = malloc(sizeof(struct cdchanger), 765 M_DEVBUF, M_NOWAIT); 766 767 if (nchanger == NULL) { 768 softc->flags &= ~CD_FLAG_CHANGER; 769 printf("cdregister: unable to malloc " 770 "changer structure\ncdregister: " 771 "changer support disabled\n"); 772 773 /* 774 * Yes, gotos can be gross but in this case 775 * I think it's justified.. 776 */ 777 goto cdregisterexit; 778 } 779 780 /* zero the structure */ 781 bzero(nchanger, sizeof(struct cdchanger)); 782 783 if (camq_init(&nchanger->devq, 1) != 0) { 784 softc->flags &= ~CD_FLAG_CHANGER; 785 printf("cdregister: changer support " 786 "disabled\n"); 787 goto cdregisterexit; 788 } 789 790 num_changers++; 791 792 nchanger->path_id = cgd->ccb_h.path_id; 793 nchanger->target_id = cgd->ccb_h.target_id; 794 795 /* this is superfluous, but it makes things clearer */ 796 nchanger->num_devices = 0; 797 798 STAILQ_INIT(&nchanger->chluns); 799 800 STAILQ_INSERT_TAIL(&changerq, nchanger, 801 changer_links); 802 803 /* 804 * Create a path with lun id 0, and see if we can 805 * find a matching device 806 */ 807 status = xpt_create_path(&path, /*periph*/ periph, 808 cgd->ccb_h.path_id, 809 cgd->ccb_h.target_id, 0); 810 811 /* 812 * If we were able to allocate the path, and if we 813 * find a matching device and it isn't already 814 * marked as part of a changer, then we add it to 815 * the current changer. 816 */ 817 if ((status == CAM_REQ_CMP) 818 && ((nperiph = cam_periph_find(path, "cd")) != NULL) 819 && ((((struct cd_softc *)periph->softc)->flags & 820 CD_FLAG_CHANGER) == 0)) { 821 struct cd_softc *nsoftc; 822 823 nsoftc = (struct cd_softc *)nperiph->softc; 824 825 nsoftc->flags |= CD_FLAG_CHANGER; 826 nchanger->num_devices++; 827 if (camq_resize(&nchanger->devq, 828 nchanger->num_devices) != CAM_REQ_CMP) { 829 printf("cdregister: camq_resize " 830 "failed, changer support may " 831 "be messed up\n"); 832 } 833 nsoftc->changer = nchanger; 834 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 835 836 STAILQ_INSERT_TAIL(&nchanger->chluns, 837 nsoftc, changer_links); 838 } else if (status == CAM_REQ_CMP) 839 xpt_free_path(path); 840 else { 841 printf("cdregister: unable to allocate path\n" 842 "cdregister: changer support may be " 843 "broken\n"); 844 } 845 846 softc->changer = nchanger; 847 softc->pinfo.index = CAM_UNQUEUED_INDEX; 848 nchanger->num_devices++; 849 if (camq_resize(&nchanger->devq, 850 nchanger->num_devices) != CAM_REQ_CMP) { 851 printf("cdregister: camq_resize " 852 "failed, changer support may " 853 "be messed up\n"); 854 } 855 STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 856 changer_links); 857 } 858 } 859 860 cdregisterexit: 861 862 /* Lock this peripheral until we are setup */ 863 /* Can't block */ 864 cam_periph_lock(periph, PRIBIO); 865 866 if ((softc->flags & CD_FLAG_CHANGER) == 0) 867 xpt_schedule(periph, /*priority*/5); 868 else 869 cdschedule(periph, /*priority*/ 5); 870 871 return(CAM_REQ_CMP); 872 } 873 874 static int 875 cdopen(dev_t dev, int flags, int fmt, struct proc *p) 876 { 877 struct disklabel label; 878 struct cam_periph *periph; 879 struct cd_softc *softc; 880 struct ccb_getdev cgd; 881 u_int32_t size; 882 int unit, error; 883 int s; 884 885 unit = dkunit(dev); 886 periph = cam_extend_get(cdperiphs, unit); 887 888 if (periph == NULL) 889 return (ENXIO); 890 891 softc = (struct cd_softc *)periph->softc; 892 893 s = splsoftcam(); 894 if (softc->flags & CD_FLAG_INVALID) { 895 splx(s); 896 return(ENXIO); 897 } 898 splx(s); 899 900 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) 901 return (error); 902 903 if ((softc->flags & CD_FLAG_OPEN) == 0) { 904 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 905 return(ENXIO); 906 softc->flags |= CD_FLAG_OPEN; 907 908 cdprevent(periph, PR_PREVENT); 909 } 910 911 /* find out the size */ 912 if ((error = cdsize(dev, &size)) != 0) { 913 if (dsisopen(softc->cd_slices) == 0) { 914 cdprevent(periph, PR_ALLOW); 915 softc->flags &= ~CD_FLAG_OPEN; 916 } 917 cam_periph_unlock(periph); 918 919 if ((softc->flags & CD_FLAG_OPEN) == 0) 920 cam_periph_release(periph); 921 922 return(error); 923 } 924 925 /* 926 * Build prototype label for whole disk. 927 * Should take information about different data tracks from the 928 * TOC and put it in the partition table. 929 */ 930 bzero(&label, sizeof(label)); 931 label.d_type = DTYPE_SCSI; 932 933 /* 934 * Grab the inquiry data to get the vendor and product names. 935 * Put them in the typename and packname for the label. 936 */ 937 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); 938 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 939 xpt_action((union ccb *)&cgd); 940 941 strncpy(label.d_typename, cgd.inq_data.vendor, 942 min(SID_VENDOR_SIZE, sizeof(label.d_typename))); 943 strncpy(label.d_packname, cgd.inq_data.product, 944 min(SID_PRODUCT_SIZE, sizeof(label.d_packname))); 945 946 label.d_secsize = softc->params.blksize; 947 label.d_secperunit = softc->params.disksize; 948 label.d_flags = D_REMOVABLE; 949 /* 950 * Make partition 'a' cover the whole disk. This is a temporary 951 * compatibility hack. The 'a' partition should not exist, so 952 * the slice code won't create it. The slice code will make 953 * partition (RAW_PART + 'a') cover the whole disk and fill in 954 * some more defaults. 955 */ 956 label.d_partitions[0].p_size = label.d_secperunit; 957 label.d_partitions[0].p_fstype = FS_OTHER; 958 959 /* Initialize slice tables. */ 960 error = dsopen("cd", dev, fmt, DSO_NOLABELS | DSO_ONESLICE, 961 &softc->cd_slices, &label, cdstrategy, 962 (ds_setgeom_t *)NULL, &cd_cdevsw); 963 964 if (error == 0) { 965 /* 966 * We unconditionally (re)set the blocksize each time the 967 * CD device is opened. This is because the CD can change, 968 * and therefore the blocksize might change. 969 * XXX problems here if some slice or partition is still 970 * open with the old size? 971 */ 972 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0) 973 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; 974 softc->device_stats.block_size = softc->params.blksize; 975 } else { 976 if ((dsisopen(softc->cd_slices) == 0) 977 && ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)) 978 cdprevent(periph, PR_ALLOW); 979 } 980 981 cam_periph_unlock(periph); 982 983 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 984 985 return (error); 986 } 987 988 static int 989 cdclose(dev_t dev, int flag, int fmt, struct proc *p) 990 { 991 struct cam_periph *periph; 992 struct cd_softc *softc; 993 int unit, error; 994 995 unit = dkunit(dev); 996 periph = cam_extend_get(cdperiphs, unit); 997 if (periph == NULL) 998 return (ENXIO); 999 1000 softc = (struct cd_softc *)periph->softc; 1001 1002 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 1003 return (error); 1004 1005 dsclose(dev, fmt, softc->cd_slices); 1006 if (dsisopen(softc->cd_slices)) { 1007 cam_periph_unlock(periph); 1008 return (0); 1009 } 1010 1011 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 1012 cdprevent(periph, PR_ALLOW); 1013 1014 /* 1015 * Since we're closing this CD, mark the blocksize as unavailable. 1016 * It will be marked as available whence the CD is opened again. 1017 */ 1018 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; 1019 1020 softc->flags &= ~CD_FLAG_OPEN; 1021 cam_periph_unlock(periph); 1022 cam_periph_release(periph); 1023 1024 return (0); 1025 } 1026 1027 static int 1028 cdread(dev_t dev, struct uio *uio, int ioflag) 1029 { 1030 return(physio(cdstrategy, NULL, dev, 1, minphys, uio)); 1031 } 1032 1033 static void 1034 cdshorttimeout(void *arg) 1035 { 1036 struct cdchanger *changer; 1037 int s; 1038 1039 s = splsoftcam(); 1040 1041 changer = (struct cdchanger *)arg; 1042 1043 /* Always clear the short timeout flag, since that's what we're in */ 1044 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1045 1046 /* 1047 * Check to see if there is any more pending or outstanding I/O for 1048 * this device. If not, move it out of the active slot. 1049 */ 1050 if ((bufq_first(&changer->cur_device->buf_queue) == NULL) 1051 && (changer->cur_device->device_stats.busy_count == 0)) { 1052 changer->flags |= CHANGER_MANUAL_CALL; 1053 cdrunchangerqueue(changer); 1054 } 1055 1056 splx(s); 1057 } 1058 1059 /* 1060 * This is a wrapper for xpt_schedule. It only applies to changers. 1061 */ 1062 static void 1063 cdschedule(struct cam_periph *periph, int priority) 1064 { 1065 struct cd_softc *softc; 1066 int s; 1067 1068 s = splsoftcam(); 1069 1070 softc = (struct cd_softc *)periph->softc; 1071 1072 /* 1073 * If this device isn't currently queued, and if it isn't 1074 * the active device, then we queue this device and run the 1075 * changer queue if there is no timeout scheduled to do it. 1076 * If this device is the active device, just schedule it 1077 * to run again. If this device is queued, there should be 1078 * a timeout in place already that will make sure it runs. 1079 */ 1080 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 1081 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 1082 /* 1083 * We don't do anything with the priority here. 1084 * This is strictly a fifo queue. 1085 */ 1086 softc->pinfo.priority = 1; 1087 if (softc->changer->devq.generation++ == 0) 1088 camq_regen(&softc->changer->devq); 1089 softc->pinfo.generation = 1090 softc->changer->devq.generation; 1091 camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 1092 1093 /* 1094 * Since we just put a device in the changer queue, 1095 * check and see if there is a timeout scheduled for 1096 * this changer. If so, let the timeout handle 1097 * switching this device into the active slot. If 1098 * not, manually call the timeout routine to 1099 * bootstrap things. 1100 */ 1101 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1102 &&((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)){ 1103 softc->changer->flags |= CHANGER_MANUAL_CALL; 1104 cdrunchangerqueue(softc->changer); 1105 } 1106 } else if ((softc->flags & CD_FLAG_ACTIVE) 1107 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 1108 xpt_schedule(periph, priority); 1109 1110 splx(s); 1111 1112 } 1113 1114 static void 1115 cdrunchangerqueue(void *arg) 1116 { 1117 struct cd_softc *softc; 1118 struct cdchanger *changer; 1119 int called_from_timeout; 1120 int s; 1121 1122 s = splsoftcam(); 1123 1124 changer = (struct cdchanger *)arg; 1125 1126 /* 1127 * If we have NOT been called from cdstrategy() or cddone(), and 1128 * instead from a timeout routine, go ahead and clear the 1129 * timeout flag. 1130 */ 1131 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 1132 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1133 called_from_timeout = 1; 1134 } else 1135 called_from_timeout = 0; 1136 1137 /* Always clear the manual call flag */ 1138 changer->flags &= ~CHANGER_MANUAL_CALL; 1139 1140 /* nothing to do if the queue is empty */ 1141 if (changer->devq.entries <= 0) { 1142 splx(s); 1143 return; 1144 } 1145 1146 /* 1147 * If the changer queue is frozen, that means we have an active 1148 * device. 1149 */ 1150 if (changer->devq.qfrozen_cnt > 0) { 1151 1152 if (changer->cur_device->device_stats.busy_count > 0) { 1153 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 1154 changer->cur_device->bufs_left = 1155 changer->cur_device->device_stats.busy_count; 1156 if (called_from_timeout) { 1157 changer->long_handle = 1158 timeout(cdrunchangerqueue, changer, 1159 changer_max_busy_seconds * hz); 1160 changer->flags |= CHANGER_TIMEOUT_SCHED; 1161 } 1162 splx(s); 1163 return; 1164 } 1165 1166 /* 1167 * We always need to reset the frozen count and clear the 1168 * active flag. 1169 */ 1170 changer->devq.qfrozen_cnt--; 1171 changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1172 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1173 1174 /* 1175 * Check to see whether the current device has any I/O left 1176 * to do. If so, requeue it at the end of the queue. If 1177 * not, there is no need to requeue it. 1178 */ 1179 if (bufq_first(&changer->cur_device->buf_queue) != NULL) { 1180 1181 if (changer->devq.generation++ == 0) 1182 camq_regen(&changer->devq); 1183 1184 changer->cur_device->pinfo.generation = 1185 changer->devq.generation; 1186 camq_insert(&changer->devq, 1187 (cam_pinfo *)changer->cur_device); 1188 } 1189 } 1190 1191 softc = (struct cd_softc *)camq_remove(&changer->devq, 0); 1192 1193 changer->cur_device = softc; 1194 1195 changer->devq.qfrozen_cnt++; 1196 softc->flags |= CD_FLAG_ACTIVE; 1197 1198 /* Just in case this device is waiting */ 1199 wakeup(&softc->changer); 1200 xpt_schedule(softc->periph, /*priority*/ 1); 1201 1202 /* 1203 * Get rid of any pending timeouts, and set a flag to schedule new 1204 * ones so this device gets its full time quantum. 1205 */ 1206 if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1207 untimeout(cdrunchangerqueue, changer, changer->long_handle); 1208 changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1209 } 1210 1211 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1212 untimeout(cdshorttimeout, changer, changer->short_handle); 1213 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1214 } 1215 1216 /* 1217 * We need to schedule timeouts, but we only do this after the 1218 * first transaction has completed. This eliminates the changer 1219 * switch time. 1220 */ 1221 changer->flags |= CHANGER_NEED_TIMEOUT; 1222 1223 splx(s); 1224 } 1225 1226 static void 1227 cdchangerschedule(struct cd_softc *softc) 1228 { 1229 struct cdchanger *changer; 1230 int s; 1231 1232 s = splsoftcam(); 1233 1234 changer = softc->changer; 1235 1236 /* 1237 * If this is a changer, and this is the current device, 1238 * and this device has at least the minimum time quantum to 1239 * run, see if we can switch it out. 1240 */ 1241 if ((softc->flags & CD_FLAG_ACTIVE) 1242 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 1243 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 1244 /* 1245 * We try three things here. The first is that we 1246 * check to see whether the schedule on completion 1247 * flag is set. If it is, we decrement the number 1248 * of buffers left, and if it's zero, we reschedule. 1249 * Next, we check to see whether the pending buffer 1250 * queue is empty and whether there are no 1251 * outstanding transactions. If so, we reschedule. 1252 * Next, we see if the pending buffer queue is empty. 1253 * If it is, we set the number of buffers left to 1254 * the current active buffer count and set the 1255 * schedule on complete flag. 1256 */ 1257 if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 1258 if (--softc->bufs_left == 0) { 1259 softc->changer->flags |= 1260 CHANGER_MANUAL_CALL; 1261 softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 1262 cdrunchangerqueue(softc->changer); 1263 } 1264 } else if ((bufq_first(&softc->buf_queue) == NULL) 1265 && (softc->device_stats.busy_count == 0)) { 1266 softc->changer->flags |= CHANGER_MANUAL_CALL; 1267 cdrunchangerqueue(softc->changer); 1268 } 1269 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 1270 && (softc->flags & CD_FLAG_ACTIVE)) { 1271 1272 /* 1273 * Now that the first transaction to this 1274 * particular device has completed, we can go ahead 1275 * and schedule our timeouts. 1276 */ 1277 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1278 changer->long_handle = 1279 timeout(cdrunchangerqueue, changer, 1280 changer_max_busy_seconds * hz); 1281 changer->flags |= CHANGER_TIMEOUT_SCHED; 1282 } else 1283 printf("cdchangerschedule: already have a long" 1284 " timeout!\n"); 1285 1286 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1287 changer->short_handle = 1288 timeout(cdshorttimeout, changer, 1289 changer_min_busy_seconds * hz); 1290 changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 1291 } else 1292 printf("cdchangerschedule: already have a short " 1293 "timeout!\n"); 1294 1295 /* 1296 * We just scheduled timeouts, no need to schedule 1297 * more. 1298 */ 1299 changer->flags &= ~CHANGER_NEED_TIMEOUT; 1300 1301 } 1302 splx(s); 1303 } 1304 1305 static int 1306 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 1307 u_int32_t cam_flags, 1308 u_int32_t sense_flags), 1309 u_int32_t cam_flags, u_int32_t sense_flags) 1310 { 1311 struct cd_softc *softc; 1312 struct cam_periph *periph; 1313 int error; 1314 1315 periph = xpt_path_periph(ccb->ccb_h.path); 1316 softc = (struct cd_softc *)periph->softc; 1317 1318 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1319 &softc->device_stats); 1320 1321 if (softc->flags & CD_FLAG_CHANGER) 1322 cdchangerschedule(softc); 1323 1324 return(error); 1325 } 1326 1327 union ccb * 1328 cdgetccb(struct cam_periph *periph, u_int32_t priority) 1329 { 1330 struct cd_softc *softc; 1331 int s; 1332 1333 softc = (struct cd_softc *)periph->softc; 1334 1335 if (softc->flags & CD_FLAG_CHANGER) { 1336 1337 s = splsoftcam(); 1338 1339 /* 1340 * This should work the first time this device is woken up, 1341 * but just in case it doesn't, we use a while loop. 1342 */ 1343 while ((((volatile cd_flags)softc->flags) & CD_FLAG_ACTIVE)==0){ 1344 /* 1345 * If this changer isn't already queued, queue it up. 1346 */ 1347 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1348 softc->pinfo.priority = 1; 1349 if (softc->changer->devq.generation++ == 0) 1350 camq_regen(&softc->changer->devq); 1351 softc->pinfo.generation = 1352 softc->changer->devq.generation; 1353 camq_insert(&softc->changer->devq, 1354 (cam_pinfo *)softc); 1355 } 1356 if (((((volatile cd_changer_flags)softc->changer->flags) 1357 & CHANGER_TIMEOUT_SCHED)==0) 1358 &&((((volatile cd_changer_flags)softc->changer->flags) 1359 & CHANGER_NEED_TIMEOUT)==0)){ 1360 softc->changer->flags |= CHANGER_MANUAL_CALL; 1361 cdrunchangerqueue(softc->changer); 1362 } else 1363 tsleep(&softc->changer, PRIBIO, "cgticb", 0); 1364 } 1365 splx(s); 1366 } 1367 return(cam_periph_getccb(periph, priority)); 1368 } 1369 1370 1371 /* 1372 * Actually translate the requested transfer into one the physical driver 1373 * can understand. The transfer is described by a buf and will include 1374 * only one physical transfer. 1375 */ 1376 static void 1377 cdstrategy(struct buf *bp) 1378 { 1379 struct cam_periph *periph; 1380 struct cd_softc *softc; 1381 u_int unit, part; 1382 int s; 1383 1384 unit = dkunit(bp->b_dev); 1385 part = dkpart(bp->b_dev); 1386 periph = cam_extend_get(cdperiphs, unit); 1387 if (periph == NULL) { 1388 bp->b_error = ENXIO; 1389 goto bad; 1390 } 1391 1392 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 1393 1394 softc = (struct cd_softc *)periph->softc; 1395 1396 /* 1397 * Do bounds checking, adjust transfer, and set b_pbklno. 1398 */ 1399 if (dscheck(bp, softc->cd_slices) <= 0) 1400 goto done; 1401 1402 /* 1403 * Mask interrupts so that the pack cannot be invalidated until 1404 * after we are in the queue. Otherwise, we might not properly 1405 * clean up one of the buffers. 1406 */ 1407 s = splbio(); 1408 1409 /* 1410 * If the device has been made invalid, error out 1411 */ 1412 if ((softc->flags & CD_FLAG_INVALID)) { 1413 splx(s); 1414 bp->b_error = ENXIO; 1415 goto bad; 1416 } 1417 1418 /* 1419 * Place it in the queue of disk activities for this disk 1420 */ 1421 bufqdisksort(&softc->buf_queue, bp); 1422 1423 splx(s); 1424 1425 /* 1426 * Schedule ourselves for performing the work. We do things 1427 * differently for changers. 1428 */ 1429 if ((softc->flags & CD_FLAG_CHANGER) == 0) 1430 xpt_schedule(periph, /* XXX priority */1); 1431 else 1432 cdschedule(periph, /* priority */ 1); 1433 1434 return; 1435 bad: 1436 bp->b_flags |= B_ERROR; 1437 done: 1438 /* 1439 * Correctly set the buf to indicate a completed xfer 1440 */ 1441 bp->b_resid = bp->b_bcount; 1442 biodone(bp); 1443 return; 1444 } 1445 1446 static void 1447 cdstrategy1(struct buf *bp) 1448 { 1449 /* 1450 * XXX - do something to make cdstrategy() but not this block while 1451 * we're doing dsopen() and dsioctl(). 1452 */ 1453 cdstrategy(bp); 1454 } 1455 1456 static void 1457 cdstart(struct cam_periph *periph, union ccb *start_ccb) 1458 { 1459 struct cd_softc *softc; 1460 struct buf *bp; 1461 struct ccb_scsiio *csio; 1462 struct scsi_read_capacity_data *rcap; 1463 int s; 1464 1465 softc = (struct cd_softc *)periph->softc; 1466 1467 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 1468 1469 switch (softc->state) { 1470 case CD_STATE_NORMAL: 1471 { 1472 int oldspl; 1473 1474 s = splbio(); 1475 bp = bufq_first(&softc->buf_queue); 1476 if (periph->immediate_priority <= periph->pinfo.priority) { 1477 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 1478 1479 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1480 periph_links.sle); 1481 periph->immediate_priority = CAM_PRIORITY_NONE; 1482 splx(s); 1483 wakeup(&periph->ccb_list); 1484 } else if (bp == NULL) { 1485 splx(s); 1486 xpt_release_ccb(start_ccb); 1487 } else { 1488 bufq_remove(&softc->buf_queue, bp); 1489 1490 devstat_start_transaction(&softc->device_stats); 1491 1492 scsi_read_write(&start_ccb->csio, 1493 /*retries*/4, 1494 /* cbfcnp */ cddone, 1495 (bp->b_flags & B_ORDERED) != 0 ? 1496 MSG_ORDERED_Q_TAG : 1497 MSG_SIMPLE_Q_TAG, 1498 /* read */bp->b_flags & B_READ, 1499 /* byte2 */ 0, 1500 /* minimum_cmd_size */ 10, 1501 /* lba */ bp->b_pblkno, 1502 bp->b_bcount / softc->params.blksize, 1503 /* data_ptr */ bp->b_data, 1504 /* dxfer_len */ bp->b_bcount, 1505 /* sense_len */ SSD_FULL_SIZE, 1506 /* timeout */ 30000); 1507 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 1508 1509 1510 /* 1511 * Block out any asyncronous callbacks 1512 * while we touch the pending ccb list. 1513 */ 1514 oldspl = splcam(); 1515 LIST_INSERT_HEAD(&softc->pending_ccbs, 1516 &start_ccb->ccb_h, periph_links.le); 1517 splx(oldspl); 1518 1519 /* We expect a unit attention from this device */ 1520 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 1521 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 1522 softc->flags &= ~CD_FLAG_RETRY_UA; 1523 } 1524 1525 start_ccb->ccb_h.ccb_bp = bp; 1526 bp = bufq_first(&softc->buf_queue); 1527 splx(s); 1528 1529 xpt_action(start_ccb); 1530 } 1531 if (bp != NULL) { 1532 /* Have more work to do, so ensure we stay scheduled */ 1533 xpt_schedule(periph, /* XXX priority */1); 1534 } 1535 break; 1536 } 1537 case CD_STATE_PROBE: 1538 { 1539 1540 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1541 M_TEMP, 1542 M_NOWAIT); 1543 if (rcap == NULL) { 1544 xpt_print_path(periph->path); 1545 printf("cdstart: Couldn't malloc read_capacity data\n"); 1546 /* cd_free_periph??? */ 1547 break; 1548 } 1549 csio = &start_ccb->csio; 1550 scsi_read_capacity(csio, 1551 /*retries*/1, 1552 cddone, 1553 MSG_SIMPLE_Q_TAG, 1554 rcap, 1555 SSD_FULL_SIZE, 1556 /*timeout*/20000); 1557 start_ccb->ccb_h.ccb_bp = NULL; 1558 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 1559 xpt_action(start_ccb); 1560 break; 1561 } 1562 } 1563 } 1564 1565 static void 1566 cddone(struct cam_periph *periph, union ccb *done_ccb) 1567 { 1568 struct cd_softc *softc; 1569 struct ccb_scsiio *csio; 1570 1571 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 1572 1573 softc = (struct cd_softc *)periph->softc; 1574 csio = &done_ccb->csio; 1575 1576 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 1577 case CD_CCB_BUFFER_IO: 1578 { 1579 struct buf *bp; 1580 int error; 1581 int oldspl; 1582 1583 bp = (struct buf *)done_ccb->ccb_h.ccb_bp; 1584 error = 0; 1585 1586 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1587 int sf; 1588 1589 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 1590 sf = SF_RETRY_UA; 1591 else 1592 sf = 0; 1593 1594 if ((error = cderror(done_ccb, 0, sf)) == ERESTART) { 1595 /* 1596 * A retry was scheuled, so 1597 * just return. 1598 */ 1599 return; 1600 } 1601 } 1602 1603 if (error != 0) { 1604 int s; 1605 struct buf *q_bp; 1606 1607 xpt_print_path(periph->path); 1608 printf("cddone: got error %#x back\n", error); 1609 s = splbio(); 1610 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) { 1611 bufq_remove(&softc->buf_queue, q_bp); 1612 q_bp->b_resid = q_bp->b_bcount; 1613 q_bp->b_error = EIO; 1614 q_bp->b_flags |= B_ERROR; 1615 biodone(q_bp); 1616 } 1617 splx(s); 1618 bp->b_resid = bp->b_bcount; 1619 bp->b_error = error; 1620 bp->b_flags |= B_ERROR; 1621 cam_release_devq(done_ccb->ccb_h.path, 1622 /*relsim_flags*/0, 1623 /*reduction*/0, 1624 /*timeout*/0, 1625 /*getcount_only*/0); 1626 1627 } else { 1628 bp->b_resid = csio->resid; 1629 bp->b_error = 0; 1630 if (bp->b_resid != 0) { 1631 /* Short transfer ??? */ 1632 bp->b_flags |= B_ERROR; 1633 } 1634 } 1635 1636 /* 1637 * Block out any asyncronous callbacks 1638 * while we touch the pending ccb list. 1639 */ 1640 oldspl = splcam(); 1641 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1642 splx(oldspl); 1643 1644 devstat_end_transaction(&softc->device_stats, 1645 bp->b_bcount - bp->b_resid, 1646 done_ccb->csio.tag_action & 0xf, 1647 (bp->b_flags & B_READ) ? DEVSTAT_READ 1648 : DEVSTAT_WRITE); 1649 1650 if (softc->flags & CD_FLAG_CHANGER) 1651 cdchangerschedule(softc); 1652 1653 biodone(bp); 1654 break; 1655 } 1656 case CD_CCB_PROBE: 1657 { 1658 struct scsi_read_capacity_data *rdcap; 1659 char announce_buf[120]; /* 1660 * Currently (9/30/97) the 1661 * longest possible announce 1662 * buffer is 108 bytes, for the 1663 * first error case below. 1664 * That is 39 bytes for the 1665 * basic string, 16 bytes for the 1666 * biggest sense key (hardware 1667 * error), 52 bytes for the 1668 * text of the largest sense 1669 * qualifier valid for a CDROM, 1670 * (0x72, 0x03 or 0x04, 1671 * 0x03), and one byte for the 1672 * null terminating character. 1673 * To allow for longer strings, 1674 * the announce buffer is 120 1675 * bytes. 1676 */ 1677 struct cd_params *cdp; 1678 1679 cdp = &softc->params; 1680 1681 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1682 1683 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1684 cdp->blksize = scsi_4btoul (rdcap->length); 1685 1686 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1687 1688 sprintf(announce_buf, 1689 "cd present [%lu x %lu byte records]", 1690 cdp->disksize, (u_long)cdp->blksize); 1691 1692 } else { 1693 int error; 1694 /* 1695 * Retry any UNIT ATTENTION type errors. They 1696 * are expected at boot. 1697 */ 1698 error = cderror(done_ccb, 0, SF_RETRY_UA|SF_NO_PRINT); 1699 if (error == ERESTART) { 1700 /* 1701 * A retry was scheuled, so 1702 * just return. 1703 */ 1704 return; 1705 } else if (error != 0) { 1706 1707 struct scsi_sense_data *sense; 1708 int asc, ascq; 1709 int sense_key, error_code; 1710 int have_sense; 1711 cam_status status; 1712 struct ccb_getdev cgd; 1713 1714 /* Don't wedge this device's queue */ 1715 cam_release_devq(done_ccb->ccb_h.path, 1716 /*relsim_flags*/0, 1717 /*reduction*/0, 1718 /*timeout*/0, 1719 /*getcount_only*/0); 1720 1721 status = done_ccb->ccb_h.status; 1722 1723 xpt_setup_ccb(&cgd.ccb_h, 1724 done_ccb->ccb_h.path, 1725 /* priority */ 1); 1726 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1727 xpt_action((union ccb *)&cgd); 1728 1729 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1730 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1731 || ((status & CAM_AUTOSNS_VALID) == 0)) 1732 have_sense = FALSE; 1733 else 1734 have_sense = TRUE; 1735 1736 if (have_sense) { 1737 sense = &csio->sense_data; 1738 scsi_extract_sense(sense, &error_code, 1739 &sense_key, 1740 &asc, &ascq); 1741 } 1742 /* 1743 * With CDROM devices, we expect 0x3a 1744 * (Medium not present) errors, since not 1745 * everyone leaves a CD in the drive. Some 1746 * broken Philips and HP WORM drives return 1747 * 0x04,0x00 (logical unit not ready, cause 1748 * not reportable), so we accept any "not 1749 * ready" type errors as well. If the error 1750 * is anything else, though, we shouldn't 1751 * attach. 1752 */ 1753 if ((have_sense) 1754 && ((asc == 0x3a) || (asc == 0x04)) 1755 && (error_code == SSD_CURRENT_ERROR)) 1756 sprintf(announce_buf, 1757 "Attempt to query device " 1758 "size failed: %s, %s", 1759 scsi_sense_key_text[sense_key], 1760 scsi_sense_desc(asc,ascq, 1761 &cgd.inq_data)); 1762 else if (cgd.pd_type == T_CDROM) { 1763 /* 1764 * We only print out an error for 1765 * CDROM type devices. For WORM 1766 * devices, we don't print out an 1767 * error since a few WORM devices 1768 * don't support CDROM commands. 1769 * If we have sense information, go 1770 * ahead and print it out. 1771 * Otherwise, just say that we 1772 * couldn't attach. 1773 */ 1774 1775 /* 1776 * Just print out the error, not 1777 * the full probe message, when we 1778 * don't attach. 1779 */ 1780 if (have_sense) 1781 scsi_sense_print( 1782 &done_ccb->csio); 1783 else { 1784 xpt_print_path(periph->path); 1785 printf("got CAM status %#x\n", 1786 done_ccb->ccb_h.status); 1787 } 1788 xpt_print_path(periph->path); 1789 printf("fatal error, failed" 1790 " to attach to device\n"); 1791 1792 /* 1793 * Invalidate this peripheral. 1794 */ 1795 cam_periph_invalidate(periph); 1796 1797 announce_buf[0] = '\0'; 1798 } else { 1799 1800 /* 1801 * Invalidate this peripheral. 1802 */ 1803 cam_periph_invalidate(periph); 1804 announce_buf[0] = '\0'; 1805 } 1806 } 1807 } 1808 free(rdcap, M_TEMP); 1809 if (announce_buf[0] != '\0') 1810 xpt_announce_periph(periph, announce_buf); 1811 softc->state = CD_STATE_NORMAL; 1812 if (softc->flags & CD_FLAG_CHANGER) 1813 cdchangerschedule(softc); 1814 cam_periph_unlock(periph); 1815 1816 break; 1817 } 1818 case CD_CCB_WAITING: 1819 { 1820 /* Caller will release the CCB */ 1821 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1822 ("trying to wakeup ccbwait\n")); 1823 1824 wakeup(&done_ccb->ccb_h.cbfcnp); 1825 return; 1826 } 1827 } 1828 xpt_release_ccb(done_ccb); 1829 } 1830 1831 static int 1832 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1833 { 1834 1835 struct cam_periph *periph; 1836 struct cd_softc *softc; 1837 int error, unit; 1838 1839 unit = dkunit(dev); 1840 1841 periph = cam_extend_get(cdperiphs, unit); 1842 if (periph == NULL) 1843 return(ENXIO); 1844 1845 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 1846 1847 softc = (struct cd_softc *)periph->softc; 1848 1849 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1850 ("trying to do ioctl %#lx\n", cmd)); 1851 1852 error = cam_periph_lock(periph, PRIBIO | PCATCH); 1853 1854 if (error != 0) 1855 return(error); 1856 1857 switch (cmd) { 1858 1859 case CDIOCPLAYTRACKS: 1860 { 1861 struct ioc_play_track *args 1862 = (struct ioc_play_track *) addr; 1863 struct cd_mode_data *data; 1864 1865 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1866 M_WAITOK); 1867 1868 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1869 ("trying to do CDIOCPLAYTRACKS\n")); 1870 1871 error = cdgetmode(periph, data, AUDIO_PAGE); 1872 if (error) { 1873 free(data, M_TEMP); 1874 break; 1875 } 1876 data->page.audio.flags &= ~CD_PA_SOTC; 1877 data->page.audio.flags |= CD_PA_IMMED; 1878 error = cdsetmode(periph, data); 1879 free(data, M_TEMP); 1880 if (error) 1881 break; 1882 if (softc->quirks & CD_Q_BCD_TRACKS) { 1883 args->start_track = bin2bcd(args->start_track); 1884 args->end_track = bin2bcd(args->end_track); 1885 } 1886 error = cdplaytracks(periph, 1887 args->start_track, 1888 args->start_index, 1889 args->end_track, 1890 args->end_index); 1891 } 1892 break; 1893 case CDIOCPLAYMSF: 1894 { 1895 struct ioc_play_msf *args 1896 = (struct ioc_play_msf *) addr; 1897 struct cd_mode_data *data; 1898 1899 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1900 M_WAITOK); 1901 1902 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1903 ("trying to do CDIOCPLAYMSF\n")); 1904 1905 error = cdgetmode(periph, data, AUDIO_PAGE); 1906 if (error) { 1907 free(data, M_TEMP); 1908 break; 1909 } 1910 data->page.audio.flags &= ~CD_PA_SOTC; 1911 data->page.audio.flags |= CD_PA_IMMED; 1912 error = cdsetmode(periph, data); 1913 free(data, M_TEMP); 1914 if (error) 1915 break; 1916 error = cdplaymsf(periph, 1917 args->start_m, 1918 args->start_s, 1919 args->start_f, 1920 args->end_m, 1921 args->end_s, 1922 args->end_f); 1923 } 1924 break; 1925 case CDIOCPLAYBLOCKS: 1926 { 1927 struct ioc_play_blocks *args 1928 = (struct ioc_play_blocks *) addr; 1929 struct cd_mode_data *data; 1930 1931 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1932 ("trying to do CDIOCPLAYBLOCKS\n")); 1933 1934 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 1935 M_WAITOK); 1936 1937 error = cdgetmode(periph, data, AUDIO_PAGE); 1938 if (error) { 1939 free(data, M_TEMP); 1940 break; 1941 } 1942 data->page.audio.flags &= ~CD_PA_SOTC; 1943 data->page.audio.flags |= CD_PA_IMMED; 1944 error = cdsetmode(periph, data); 1945 free(data, M_TEMP); 1946 if (error) 1947 break; 1948 error = cdplay(periph, args->blk, args->len); 1949 } 1950 break; 1951 case CDIOCREADSUBCHANNEL: 1952 { 1953 struct ioc_read_subchannel *args 1954 = (struct ioc_read_subchannel *) addr; 1955 struct cd_sub_channel_info *data; 1956 u_int32_t len = args->data_len; 1957 1958 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1959 ("trying to do CDIOCREADSUBCHANNEL\n")); 1960 1961 data = malloc(sizeof(struct cd_sub_channel_info), 1962 M_TEMP, M_WAITOK); 1963 1964 if ((len > sizeof(struct cd_sub_channel_info)) || 1965 (len < sizeof(struct cd_sub_channel_header))) { 1966 printf( 1967 "scsi_cd: cdioctl: " 1968 "cdioreadsubchannel: error, len=%d\n", 1969 len); 1970 error = EINVAL; 1971 free(data, M_TEMP); 1972 break; 1973 } 1974 1975 if (softc->quirks & CD_Q_BCD_TRACKS) 1976 args->track = bin2bcd(args->track); 1977 1978 error = cdreadsubchannel(periph, args->address_format, 1979 args->data_format, args->track, data, len); 1980 1981 if (error) { 1982 free(data, M_TEMP); 1983 break; 1984 } 1985 if (softc->quirks & CD_Q_BCD_TRACKS) 1986 data->what.track_info.track_number = 1987 bcd2bin(data->what.track_info.track_number); 1988 len = min(len, ((data->header.data_len[0] << 8) + 1989 data->header.data_len[1] + 1990 sizeof(struct cd_sub_channel_header))); 1991 if (copyout(data, args->data, len) != 0) { 1992 error = EFAULT; 1993 } 1994 free(data, M_TEMP); 1995 } 1996 break; 1997 1998 case CDIOREADTOCHEADER: 1999 { 2000 struct ioc_toc_header *th; 2001 2002 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2003 ("trying to do CDIOREADTOCHEADER\n")); 2004 2005 th = malloc(sizeof(struct ioc_toc_header), M_TEMP, 2006 M_WAITOK); 2007 error = cdreadtoc(periph, 0, 0, 2008 (struct cd_toc_entry *)th, 2009 sizeof (*th)); 2010 if (error) { 2011 free(th, M_TEMP); 2012 break; 2013 } 2014 if (softc->quirks & CD_Q_BCD_TRACKS) { 2015 /* we are going to have to convert the BCD 2016 * encoding on the cd to what is expected 2017 */ 2018 th->starting_track = 2019 bcd2bin(th->starting_track); 2020 th->ending_track = bcd2bin(th->ending_track); 2021 } 2022 NTOHS(th->len); 2023 bcopy(th, addr, sizeof(*th)); 2024 free(th, M_TEMP); 2025 } 2026 break; 2027 case CDIOREADTOCENTRYS: 2028 { 2029 typedef struct { 2030 struct ioc_toc_header header; 2031 struct cd_toc_entry entries[100]; 2032 } data_t; 2033 typedef struct { 2034 struct ioc_toc_header header; 2035 struct cd_toc_entry entry; 2036 } lead_t; 2037 2038 data_t *data; 2039 lead_t *lead; 2040 struct ioc_read_toc_entry *te = 2041 (struct ioc_read_toc_entry *) addr; 2042 struct ioc_toc_header *th; 2043 u_int32_t len, readlen, idx, num; 2044 u_int32_t starting_track = te->starting_track; 2045 2046 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2047 ("trying to do CDIOREADTOCENTRYS\n")); 2048 2049 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK); 2050 lead = malloc(sizeof(lead_t), M_TEMP, M_WAITOK); 2051 2052 if (te->data_len < sizeof(struct cd_toc_entry) 2053 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2054 || (te->address_format != CD_MSF_FORMAT 2055 && te->address_format != CD_LBA_FORMAT)) { 2056 error = EINVAL; 2057 printf("scsi_cd: error in readtocentries, " 2058 "returning EINVAL\n"); 2059 free(data, M_TEMP); 2060 free(lead, M_TEMP); 2061 break; 2062 } 2063 2064 th = &data->header; 2065 error = cdreadtoc(periph, 0, 0, 2066 (struct cd_toc_entry *)th, 2067 sizeof (*th)); 2068 if (error) { 2069 free(data, M_TEMP); 2070 free(lead, M_TEMP); 2071 break; 2072 } 2073 2074 if (softc->quirks & CD_Q_BCD_TRACKS) { 2075 /* we are going to have to convert the BCD 2076 * encoding on the cd to what is expected 2077 */ 2078 th->starting_track = 2079 bcd2bin(th->starting_track); 2080 th->ending_track = bcd2bin(th->ending_track); 2081 } 2082 2083 if (starting_track == 0) 2084 starting_track = th->starting_track; 2085 else if (starting_track == LEADOUT) 2086 starting_track = th->ending_track + 1; 2087 else if (starting_track < th->starting_track || 2088 starting_track > th->ending_track + 1) { 2089 printf("scsi_cd: error in readtocentries, " 2090 "returning EINVAL\n"); 2091 free(data, M_TEMP); 2092 free(lead, M_TEMP); 2093 error = EINVAL; 2094 break; 2095 } 2096 2097 /* calculate reading length without leadout entry */ 2098 readlen = (th->ending_track - starting_track + 1) * 2099 sizeof(struct cd_toc_entry); 2100 2101 /* and with leadout entry */ 2102 len = readlen + sizeof(struct cd_toc_entry); 2103 if (te->data_len < len) { 2104 len = te->data_len; 2105 if (readlen > len) 2106 readlen = len; 2107 } 2108 if (len > sizeof(data->entries)) { 2109 printf("scsi_cd: error in readtocentries, " 2110 "returning EINVAL\n"); 2111 error = EINVAL; 2112 free(data, M_TEMP); 2113 free(lead, M_TEMP); 2114 break; 2115 } 2116 num = len / sizeof(struct cd_toc_entry); 2117 2118 if (readlen > 0) { 2119 error = cdreadtoc(periph, te->address_format, 2120 starting_track, 2121 (struct cd_toc_entry *)data, 2122 readlen + sizeof (*th)); 2123 if (error) { 2124 free(data, M_TEMP); 2125 free(lead, M_TEMP); 2126 break; 2127 } 2128 } 2129 2130 /* make leadout entry if needed */ 2131 idx = starting_track + num - 1; 2132 if (softc->quirks & CD_Q_BCD_TRACKS) 2133 th->ending_track = bcd2bin(th->ending_track); 2134 if (idx == th->ending_track + 1) { 2135 error = cdreadtoc(periph, te->address_format, 2136 LEADOUT, 2137 (struct cd_toc_entry *)lead, 2138 sizeof(*lead)); 2139 if (error) { 2140 free(data, M_TEMP); 2141 free(lead, M_TEMP); 2142 break; 2143 } 2144 data->entries[idx - starting_track] = 2145 lead->entry; 2146 } 2147 if (softc->quirks & CD_Q_BCD_TRACKS) { 2148 for (idx = 0; idx < num - 1; idx++) { 2149 data->entries[idx].track = 2150 bcd2bin(data->entries[idx].track); 2151 } 2152 } 2153 2154 error = copyout(data->entries, te->data, len); 2155 free(data, M_TEMP); 2156 free(lead, M_TEMP); 2157 } 2158 break; 2159 case CDIOREADTOCENTRY: 2160 { 2161 /* yeah yeah, this is ugly */ 2162 typedef struct { 2163 struct ioc_toc_header header; 2164 struct cd_toc_entry entry; 2165 } data_t; 2166 2167 data_t *data; 2168 struct ioc_read_toc_single_entry *te = 2169 (struct ioc_read_toc_single_entry *) addr; 2170 struct ioc_toc_header *th; 2171 u_int32_t track; 2172 2173 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2174 ("trying to do CDIOREADTOCENTRY\n")); 2175 2176 data = malloc(sizeof(data_t), M_TEMP, M_WAITOK); 2177 2178 if (te->address_format != CD_MSF_FORMAT 2179 && te->address_format != CD_LBA_FORMAT) { 2180 printf("error in readtocentry, " 2181 " returning EINVAL\n"); 2182 free(data, M_TEMP); 2183 error = EINVAL; 2184 break; 2185 } 2186 2187 th = &data->header; 2188 error = cdreadtoc(periph, 0, 0, 2189 (struct cd_toc_entry *)th, 2190 sizeof (*th)); 2191 if (error) { 2192 free(data, M_TEMP); 2193 break; 2194 } 2195 2196 if (softc->quirks & CD_Q_BCD_TRACKS) { 2197 /* we are going to have to convert the BCD 2198 * encoding on the cd to what is expected 2199 */ 2200 th->starting_track = 2201 bcd2bin(th->starting_track); 2202 th->ending_track = bcd2bin(th->ending_track); 2203 } 2204 track = te->track; 2205 if (track == 0) 2206 track = th->starting_track; 2207 else if (track == LEADOUT) 2208 /* OK */; 2209 else if (track < th->starting_track || 2210 track > th->ending_track + 1) { 2211 printf("error in readtocentry, " 2212 " returning EINVAL\n"); 2213 free(data, M_TEMP); 2214 error = EINVAL; 2215 break; 2216 } 2217 2218 error = cdreadtoc(periph, te->address_format, track, 2219 (struct cd_toc_entry *)data, 2220 sizeof(data_t)); 2221 if (error) { 2222 free(data, M_TEMP); 2223 break; 2224 } 2225 2226 if (softc->quirks & CD_Q_BCD_TRACKS) 2227 data->entry.track = bcd2bin(data->entry.track); 2228 bcopy(&data->entry, &te->entry, 2229 sizeof(struct cd_toc_entry)); 2230 free(data, M_TEMP); 2231 } 2232 break; 2233 case CDIOCSETPATCH: 2234 { 2235 struct ioc_patch *arg = (struct ioc_patch *) addr; 2236 struct cd_mode_data *data; 2237 2238 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2239 ("trying to do CDIOCSETPATCH\n")); 2240 2241 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2242 M_WAITOK); 2243 error = cdgetmode(periph, data, AUDIO_PAGE); 2244 if (error) { 2245 free(data, M_TEMP); 2246 break; 2247 } 2248 data->page.audio.port[LEFT_PORT].channels = 2249 arg->patch[0]; 2250 data->page.audio.port[RIGHT_PORT].channels = 2251 arg->patch[1]; 2252 data->page.audio.port[2].channels = arg->patch[2]; 2253 data->page.audio.port[3].channels = arg->patch[3]; 2254 error = cdsetmode(periph, data); 2255 free(data, M_TEMP); 2256 } 2257 break; 2258 case CDIOCGETVOL: 2259 { 2260 struct ioc_vol *arg = (struct ioc_vol *) addr; 2261 struct cd_mode_data *data; 2262 2263 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2264 ("trying to do CDIOCGETVOL\n")); 2265 2266 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2267 M_WAITOK); 2268 error = cdgetmode(periph, data, AUDIO_PAGE); 2269 if (error) { 2270 free(data, M_TEMP); 2271 break; 2272 } 2273 arg->vol[LEFT_PORT] = 2274 data->page.audio.port[LEFT_PORT].volume; 2275 arg->vol[RIGHT_PORT] = 2276 data->page.audio.port[RIGHT_PORT].volume; 2277 arg->vol[2] = data->page.audio.port[2].volume; 2278 arg->vol[3] = data->page.audio.port[3].volume; 2279 free(data, M_TEMP); 2280 } 2281 break; 2282 case CDIOCSETVOL: 2283 { 2284 struct ioc_vol *arg = (struct ioc_vol *) addr; 2285 struct cd_mode_data *data; 2286 2287 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2288 ("trying to do CDIOCSETVOL\n")); 2289 2290 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2291 M_WAITOK); 2292 error = cdgetmode(periph, data, AUDIO_PAGE); 2293 if (error) { 2294 free(data, M_TEMP); 2295 break; 2296 } 2297 data->page.audio.port[LEFT_PORT].channels = CHANNEL_0; 2298 data->page.audio.port[LEFT_PORT].volume = 2299 arg->vol[LEFT_PORT]; 2300 data->page.audio.port[RIGHT_PORT].channels = CHANNEL_1; 2301 data->page.audio.port[RIGHT_PORT].volume = 2302 arg->vol[RIGHT_PORT]; 2303 data->page.audio.port[2].volume = arg->vol[2]; 2304 data->page.audio.port[3].volume = arg->vol[3]; 2305 error = cdsetmode(periph, data); 2306 free(data, M_TEMP); 2307 } 2308 break; 2309 case CDIOCSETMONO: 2310 { 2311 struct cd_mode_data *data; 2312 2313 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2314 ("trying to do CDIOCSETMONO\n")); 2315 2316 data = malloc(sizeof(struct cd_mode_data), 2317 M_TEMP, M_WAITOK); 2318 error = cdgetmode(periph, data, AUDIO_PAGE); 2319 if (error) { 2320 free(data, M_TEMP); 2321 break; 2322 } 2323 data->page.audio.port[LEFT_PORT].channels = 2324 LEFT_CHANNEL | RIGHT_CHANNEL; 2325 data->page.audio.port[RIGHT_PORT].channels = 2326 LEFT_CHANNEL | RIGHT_CHANNEL; 2327 data->page.audio.port[2].channels = 0; 2328 data->page.audio.port[3].channels = 0; 2329 error = cdsetmode(periph, data); 2330 free(data, M_TEMP); 2331 } 2332 break; 2333 case CDIOCSETSTEREO: 2334 { 2335 struct cd_mode_data *data; 2336 2337 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2338 ("trying to do CDIOCSETSTEREO\n")); 2339 2340 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2341 M_WAITOK); 2342 error = cdgetmode(periph, data, AUDIO_PAGE); 2343 if (error) { 2344 free(data, M_TEMP); 2345 break; 2346 } 2347 data->page.audio.port[LEFT_PORT].channels = 2348 LEFT_CHANNEL; 2349 data->page.audio.port[RIGHT_PORT].channels = 2350 RIGHT_CHANNEL; 2351 data->page.audio.port[2].channels = 0; 2352 data->page.audio.port[3].channels = 0; 2353 error = cdsetmode(periph, data); 2354 free(data, M_TEMP); 2355 } 2356 break; 2357 case CDIOCSETMUTE: 2358 { 2359 struct cd_mode_data *data; 2360 2361 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2362 ("trying to do CDIOCSETMUTE\n")); 2363 2364 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2365 M_WAITOK); 2366 error = cdgetmode(periph, data, AUDIO_PAGE); 2367 if (error) { 2368 free(data, M_TEMP); 2369 break; 2370 } 2371 data->page.audio.port[LEFT_PORT].channels = 0; 2372 data->page.audio.port[RIGHT_PORT].channels = 0; 2373 data->page.audio.port[2].channels = 0; 2374 data->page.audio.port[3].channels = 0; 2375 error = cdsetmode(periph, data); 2376 free(data, M_TEMP); 2377 } 2378 break; 2379 case CDIOCSETLEFT: 2380 { 2381 struct cd_mode_data *data; 2382 2383 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2384 ("trying to do CDIOCSETLEFT\n")); 2385 2386 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2387 M_WAITOK); 2388 error = cdgetmode(periph, data, AUDIO_PAGE); 2389 if (error) { 2390 free(data, M_TEMP); 2391 break; 2392 } 2393 data->page.audio.port[LEFT_PORT].channels = 2394 LEFT_CHANNEL; 2395 data->page.audio.port[RIGHT_PORT].channels = 2396 LEFT_CHANNEL; 2397 data->page.audio.port[2].channels = 0; 2398 data->page.audio.port[3].channels = 0; 2399 error = cdsetmode(periph, data); 2400 free(data, M_TEMP); 2401 } 2402 break; 2403 case CDIOCSETRIGHT: 2404 { 2405 struct cd_mode_data *data; 2406 2407 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2408 ("trying to do CDIOCSETRIGHT\n")); 2409 2410 data = malloc(sizeof(struct cd_mode_data), M_TEMP, 2411 M_WAITOK); 2412 error = cdgetmode(periph, data, AUDIO_PAGE); 2413 if (error) { 2414 free(data, M_TEMP); 2415 break; 2416 } 2417 data->page.audio.port[LEFT_PORT].channels = 2418 RIGHT_CHANNEL; 2419 data->page.audio.port[RIGHT_PORT].channels = 2420 RIGHT_CHANNEL; 2421 data->page.audio.port[2].channels = 0; 2422 data->page.audio.port[3].channels = 0; 2423 error = cdsetmode(periph, data); 2424 free(data, M_TEMP); 2425 } 2426 break; 2427 case CDIOCRESUME: 2428 error = cdpause(periph, 1); 2429 break; 2430 case CDIOCPAUSE: 2431 error = cdpause(periph, 0); 2432 break; 2433 case CDIOCSTART: 2434 error = cdstartunit(periph); 2435 break; 2436 case CDIOCSTOP: 2437 error = cdstopunit(periph, 0); 2438 break; 2439 case CDIOCEJECT: 2440 error = cdstopunit(periph, 1); 2441 break; 2442 case CDIOCALLOW: 2443 cdprevent(periph, PR_ALLOW); 2444 break; 2445 case CDIOCPREVENT: 2446 cdprevent(periph, PR_PREVENT); 2447 break; 2448 case CDIOCSETDEBUG: 2449 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2450 error = ENOTTY; 2451 break; 2452 case CDIOCCLRDEBUG: 2453 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2454 error = ENOTTY; 2455 break; 2456 case CDIOCRESET: 2457 /* return (cd_reset(periph)); */ 2458 error = ENOTTY; 2459 break; 2460 default: 2461 if (cmd == DIOCSBAD) { 2462 error = EINVAL; /* XXX */ 2463 break; 2464 } 2465 2466 /* 2467 * Check to see whether we've got a disk-type ioctl. If we 2468 * don't, dsioctl will pass back an error code of ENOIOCTL. 2469 */ 2470 error = dsioctl("cd", dev, cmd, addr, flag, &softc->cd_slices, 2471 cdstrategy, (ds_setgeom_t *)NULL); 2472 2473 if (error != ENOIOCTL) 2474 break; 2475 2476 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2477 break; 2478 } 2479 2480 cam_periph_unlock(periph); 2481 2482 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2483 2484 return (error); 2485 } 2486 2487 static void 2488 cdprevent(struct cam_periph *periph, int action) 2489 { 2490 union ccb *ccb; 2491 struct cd_softc *softc; 2492 int error; 2493 2494 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2495 2496 softc = (struct cd_softc *)periph->softc; 2497 2498 if (((action == PR_ALLOW) 2499 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2500 || ((action == PR_PREVENT) 2501 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2502 return; 2503 } 2504 2505 ccb = cdgetccb(periph, /* priority */ 1); 2506 2507 scsi_prevent(&ccb->csio, 2508 /*retries*/ 1, 2509 cddone, 2510 MSG_SIMPLE_Q_TAG, 2511 action, 2512 SSD_FULL_SIZE, 2513 /* timeout */60000); 2514 2515 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2516 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2517 2518 xpt_release_ccb(ccb); 2519 2520 if (error == 0) { 2521 if (action == PR_ALLOW) 2522 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2523 else 2524 softc->flags |= CD_FLAG_DISC_LOCKED; 2525 } 2526 } 2527 2528 static int 2529 cdsize(dev_t dev, u_int32_t *size) 2530 { 2531 struct cam_periph *periph; 2532 struct cd_softc *softc; 2533 union ccb *ccb; 2534 struct scsi_read_capacity_data *rcap_buf; 2535 int error; 2536 2537 periph = cam_extend_get(cdperiphs, dkunit(dev)); 2538 2539 if (periph == NULL) 2540 return (ENXIO); 2541 2542 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2543 2544 softc = (struct cd_softc *)periph->softc; 2545 2546 ccb = cdgetccb(periph, /* priority */ 1); 2547 2548 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2549 M_TEMP, M_WAITOK); 2550 2551 scsi_read_capacity(&ccb->csio, 2552 /*retries*/ 1, 2553 cddone, 2554 MSG_SIMPLE_Q_TAG, 2555 rcap_buf, 2556 SSD_FULL_SIZE, 2557 /* timeout */20000); 2558 2559 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2560 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2561 2562 xpt_release_ccb(ccb); 2563 2564 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 2565 softc->params.blksize = scsi_4btoul(rcap_buf->length); 2566 2567 free(rcap_buf, M_TEMP); 2568 *size = softc->params.disksize; 2569 2570 return (error); 2571 2572 } 2573 2574 static int 2575 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2576 { 2577 struct cd_softc *softc; 2578 struct cam_periph *periph; 2579 2580 periph = xpt_path_periph(ccb->ccb_h.path); 2581 softc = (struct cd_softc *)periph->softc; 2582 2583 /* 2584 * XXX 2585 * Until we have a better way of doing pack validation, 2586 * don't treat UAs as errors. 2587 */ 2588 sense_flags |= SF_RETRY_UA; 2589 return (cam_periph_error(ccb, cam_flags, sense_flags, 2590 &softc->saved_ccb)); 2591 } 2592 2593 /* 2594 * Read table of contents 2595 */ 2596 static int 2597 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 2598 struct cd_toc_entry *data, u_int32_t len) 2599 { 2600 struct scsi_read_toc *scsi_cmd; 2601 u_int32_t ntoc; 2602 struct ccb_scsiio *csio; 2603 union ccb *ccb; 2604 int error; 2605 2606 ntoc = len; 2607 error = 0; 2608 2609 ccb = cdgetccb(periph, /* priority */ 1); 2610 2611 csio = &ccb->csio; 2612 2613 cam_fill_csio(csio, 2614 /* retries */ 1, 2615 /* cbfcnp */ cddone, 2616 /* flags */ CAM_DIR_IN, 2617 /* tag_action */ MSG_SIMPLE_Q_TAG, 2618 /* data_ptr */ (u_int8_t *)data, 2619 /* dxfer_len */ len, 2620 /* sense_len */ SSD_FULL_SIZE, 2621 sizeof(struct scsi_read_toc), 2622 /* timeout */ 50000); 2623 2624 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 2625 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2626 2627 if (mode == CD_MSF_FORMAT) 2628 scsi_cmd->byte2 |= CD_MSF; 2629 scsi_cmd->from_track = start; 2630 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 2631 scsi_cmd->data_len[0] = (ntoc) >> 8; 2632 scsi_cmd->data_len[1] = (ntoc) & 0xff; 2633 2634 scsi_cmd->op_code = READ_TOC; 2635 2636 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2637 /*sense_flags*/SF_RETRY_UA); 2638 2639 xpt_release_ccb(ccb); 2640 2641 return(error); 2642 } 2643 2644 static int 2645 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 2646 u_int32_t format, int track, 2647 struct cd_sub_channel_info *data, u_int32_t len) 2648 { 2649 struct scsi_read_subchannel *scsi_cmd; 2650 struct ccb_scsiio *csio; 2651 union ccb *ccb; 2652 int error; 2653 2654 error = 0; 2655 2656 ccb = cdgetccb(periph, /* priority */ 1); 2657 2658 csio = &ccb->csio; 2659 2660 cam_fill_csio(csio, 2661 /* retries */ 1, 2662 /* cbfcnp */ cddone, 2663 /* flags */ CAM_DIR_IN, 2664 /* tag_action */ MSG_SIMPLE_Q_TAG, 2665 /* data_ptr */ (u_int8_t *)data, 2666 /* dxfer_len */ len, 2667 /* sense_len */ SSD_FULL_SIZE, 2668 sizeof(struct scsi_read_subchannel), 2669 /* timeout */ 50000); 2670 2671 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 2672 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2673 2674 scsi_cmd->op_code = READ_SUBCHANNEL; 2675 if (mode == CD_MSF_FORMAT) 2676 scsi_cmd->byte1 |= CD_MSF; 2677 scsi_cmd->byte2 = SRS_SUBQ; 2678 scsi_cmd->subchan_format = format; 2679 scsi_cmd->track = track; 2680 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 2681 scsi_cmd->control = 0; 2682 2683 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2684 /*sense_flags*/SF_RETRY_UA); 2685 2686 xpt_release_ccb(ccb); 2687 2688 return(error); 2689 } 2690 2691 2692 static int 2693 cdgetmode(struct cam_periph *periph, struct cd_mode_data *data, u_int32_t page) 2694 { 2695 struct scsi_mode_sense_6 *scsi_cmd; 2696 struct ccb_scsiio *csio; 2697 union ccb *ccb; 2698 int error; 2699 2700 ccb = cdgetccb(periph, /* priority */ 1); 2701 2702 csio = &ccb->csio; 2703 2704 bzero(data, sizeof(*data)); 2705 cam_fill_csio(csio, 2706 /* retries */ 1, 2707 /* cbfcnp */ cddone, 2708 /* flags */ CAM_DIR_IN, 2709 /* tag_action */ MSG_SIMPLE_Q_TAG, 2710 /* data_ptr */ (u_int8_t *)data, 2711 /* dxfer_len */ sizeof(*data), 2712 /* sense_len */ SSD_FULL_SIZE, 2713 sizeof(struct scsi_mode_sense_6), 2714 /* timeout */ 50000); 2715 2716 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes; 2717 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2718 2719 scsi_cmd->page = page; 2720 scsi_cmd->length = sizeof(*data) & 0xff; 2721 scsi_cmd->opcode = MODE_SENSE; 2722 2723 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2724 /*sense_flags*/SF_RETRY_UA); 2725 2726 xpt_release_ccb(ccb); 2727 2728 return(error); 2729 } 2730 2731 static int 2732 cdsetmode(struct cam_periph *periph, struct cd_mode_data *data) 2733 { 2734 struct scsi_mode_select_6 *scsi_cmd; 2735 struct ccb_scsiio *csio; 2736 union ccb *ccb; 2737 int error; 2738 2739 ccb = cdgetccb(periph, /* priority */ 1); 2740 2741 csio = &ccb->csio; 2742 2743 error = 0; 2744 2745 cam_fill_csio(csio, 2746 /* retries */ 1, 2747 /* cbfcnp */ cddone, 2748 /* flags */ CAM_DIR_OUT, 2749 /* tag_action */ MSG_SIMPLE_Q_TAG, 2750 /* data_ptr */ (u_int8_t *)data, 2751 /* dxfer_len */ sizeof(*data), 2752 /* sense_len */ SSD_FULL_SIZE, 2753 sizeof(struct scsi_mode_select_6), 2754 /* timeout */ 50000); 2755 2756 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes; 2757 2758 bzero(scsi_cmd, sizeof(*scsi_cmd)); 2759 scsi_cmd->opcode = MODE_SELECT; 2760 scsi_cmd->byte2 |= SMS_PF; 2761 scsi_cmd->length = sizeof(*data) & 0xff; 2762 data->header.data_length = 0; 2763 /* 2764 * SONY drives do not allow a mode select with a medium_type 2765 * value that has just been returned by a mode sense; use a 2766 * medium_type of 0 (Default) instead. 2767 */ 2768 data->header.medium_type = 0; 2769 2770 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2771 /*sense_flags*/SF_RETRY_UA); 2772 2773 xpt_release_ccb(ccb); 2774 2775 return(error); 2776 } 2777 2778 2779 static int 2780 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 2781 { 2782 struct ccb_scsiio *csio; 2783 union ccb *ccb; 2784 int error; 2785 u_int8_t cdb_len; 2786 2787 error = 0; 2788 ccb = cdgetccb(periph, /* priority */ 1); 2789 csio = &ccb->csio; 2790 /* 2791 * Use the smallest possible command to perform the operation. 2792 */ 2793 if ((len & 0xffff0000) == 0) { 2794 /* 2795 * We can fit in a 10 byte cdb. 2796 */ 2797 struct scsi_play_10 *scsi_cmd; 2798 2799 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 2800 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2801 scsi_cmd->op_code = PLAY_10; 2802 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2803 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 2804 cdb_len = sizeof(*scsi_cmd); 2805 } else { 2806 struct scsi_play_12 *scsi_cmd; 2807 2808 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 2809 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2810 scsi_cmd->op_code = PLAY_12; 2811 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 2812 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 2813 cdb_len = sizeof(*scsi_cmd); 2814 } 2815 cam_fill_csio(csio, 2816 /*retries*/2, 2817 cddone, 2818 /*flags*/CAM_DIR_NONE, 2819 MSG_SIMPLE_Q_TAG, 2820 /*dataptr*/NULL, 2821 /*datalen*/0, 2822 /*sense_len*/SSD_FULL_SIZE, 2823 cdb_len, 2824 /*timeout*/50 * 1000); 2825 2826 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2827 /*sense_flags*/SF_RETRY_UA); 2828 2829 xpt_release_ccb(ccb); 2830 2831 return(error); 2832 } 2833 2834 static int 2835 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 2836 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 2837 { 2838 struct scsi_play_msf *scsi_cmd; 2839 struct ccb_scsiio *csio; 2840 union ccb *ccb; 2841 int error; 2842 2843 error = 0; 2844 2845 ccb = cdgetccb(periph, /* priority */ 1); 2846 2847 csio = &ccb->csio; 2848 2849 cam_fill_csio(csio, 2850 /* retries */ 1, 2851 /* cbfcnp */ cddone, 2852 /* flags */ CAM_DIR_NONE, 2853 /* tag_action */ MSG_SIMPLE_Q_TAG, 2854 /* data_ptr */ NULL, 2855 /* dxfer_len */ 0, 2856 /* sense_len */ SSD_FULL_SIZE, 2857 sizeof(struct scsi_play_msf), 2858 /* timeout */ 50000); 2859 2860 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 2861 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2862 2863 scsi_cmd->op_code = PLAY_MSF; 2864 scsi_cmd->start_m = startm; 2865 scsi_cmd->start_s = starts; 2866 scsi_cmd->start_f = startf; 2867 scsi_cmd->end_m = endm; 2868 scsi_cmd->end_s = ends; 2869 scsi_cmd->end_f = endf; 2870 2871 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2872 /*sense_flags*/SF_RETRY_UA); 2873 2874 xpt_release_ccb(ccb); 2875 2876 return(error); 2877 } 2878 2879 2880 static int 2881 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 2882 u_int32_t etrack, u_int32_t eindex) 2883 { 2884 struct scsi_play_track *scsi_cmd; 2885 struct ccb_scsiio *csio; 2886 union ccb *ccb; 2887 int error; 2888 2889 error = 0; 2890 2891 ccb = cdgetccb(periph, /* priority */ 1); 2892 2893 csio = &ccb->csio; 2894 2895 cam_fill_csio(csio, 2896 /* retries */ 1, 2897 /* cbfcnp */ cddone, 2898 /* flags */ CAM_DIR_NONE, 2899 /* tag_action */ MSG_SIMPLE_Q_TAG, 2900 /* data_ptr */ NULL, 2901 /* dxfer_len */ 0, 2902 /* sense_len */ SSD_FULL_SIZE, 2903 sizeof(struct scsi_play_track), 2904 /* timeout */ 50000); 2905 2906 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 2907 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2908 2909 scsi_cmd->op_code = PLAY_TRACK; 2910 scsi_cmd->start_track = strack; 2911 scsi_cmd->start_index = sindex; 2912 scsi_cmd->end_track = etrack; 2913 scsi_cmd->end_index = eindex; 2914 2915 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2916 /*sense_flags*/SF_RETRY_UA); 2917 2918 xpt_release_ccb(ccb); 2919 2920 return(error); 2921 } 2922 2923 static int 2924 cdpause(struct cam_periph *periph, u_int32_t go) 2925 { 2926 struct scsi_pause *scsi_cmd; 2927 struct ccb_scsiio *csio; 2928 union ccb *ccb; 2929 int error; 2930 2931 error = 0; 2932 2933 ccb = cdgetccb(periph, /* priority */ 1); 2934 2935 csio = &ccb->csio; 2936 2937 cam_fill_csio(csio, 2938 /* retries */ 1, 2939 /* cbfcnp */ cddone, 2940 /* flags */ CAM_DIR_NONE, 2941 /* tag_action */ MSG_SIMPLE_Q_TAG, 2942 /* data_ptr */ NULL, 2943 /* dxfer_len */ 0, 2944 /* sense_len */ SSD_FULL_SIZE, 2945 sizeof(struct scsi_pause), 2946 /* timeout */ 50000); 2947 2948 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 2949 bzero (scsi_cmd, sizeof(*scsi_cmd)); 2950 2951 scsi_cmd->op_code = PAUSE; 2952 scsi_cmd->resume = go; 2953 2954 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2955 /*sense_flags*/SF_RETRY_UA); 2956 2957 xpt_release_ccb(ccb); 2958 2959 return(error); 2960 } 2961 2962 static int 2963 cdstartunit(struct cam_periph *periph) 2964 { 2965 union ccb *ccb; 2966 int error; 2967 2968 error = 0; 2969 2970 ccb = cdgetccb(periph, /* priority */ 1); 2971 2972 scsi_start_stop(&ccb->csio, 2973 /* retries */ 1, 2974 /* cbfcnp */ cddone, 2975 /* tag_action */ MSG_SIMPLE_Q_TAG, 2976 /* start */ TRUE, 2977 /* load_eject */ TRUE, 2978 /* immediate */ FALSE, 2979 /* sense_len */ SSD_FULL_SIZE, 2980 /* timeout */ 50000); 2981 2982 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 2983 /*sense_flags*/SF_RETRY_UA); 2984 2985 xpt_release_ccb(ccb); 2986 2987 return(error); 2988 } 2989 2990 static int 2991 cdstopunit(struct cam_periph *periph, u_int32_t eject) 2992 { 2993 union ccb *ccb; 2994 int error; 2995 2996 error = 0; 2997 2998 ccb = cdgetccb(periph, /* priority */ 1); 2999 3000 scsi_start_stop(&ccb->csio, 3001 /* retries */ 1, 3002 /* cbfcnp */ cddone, 3003 /* tag_action */ MSG_SIMPLE_Q_TAG, 3004 /* start */ FALSE, 3005 /* load_eject */ eject, 3006 /* immediate */ FALSE, 3007 /* sense_len */ SSD_FULL_SIZE, 3008 /* timeout */ 50000); 3009 3010 error = cdrunccb(ccb, cderror, /*cam_flags*/0, 3011 /*sense_flags*/SF_RETRY_UA); 3012 3013 xpt_release_ccb(ccb); 3014 3015 return(error); 3016 } 3017