scsi_cd.c revision 206648
1139823Simp/*- 211150Swollman * Copyright (c) 1997 Justin T. Gibbs. 31541Srgrimes * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry. 41541Srgrimes * All rights reserved. 51541Srgrimes * 61541Srgrimes * Redistribution and use in source and binary forms, with or without 71541Srgrimes * modification, are permitted provided that the following conditions 81541Srgrimes * are met: 91541Srgrimes * 1. Redistributions of source code must retain the above copyright 101541Srgrimes * notice, this list of conditions, and the following disclaimer, 111541Srgrimes * without modification, immediately at the beginning of the file. 121541Srgrimes * 2. The name of the author may not be used to endorse or promote products 131541Srgrimes * derived from this software without specific prior written permission. 141541Srgrimes * 151541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 161541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 171541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 181541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 191541Srgrimes * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 201541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 211541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 221541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 231541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 241541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 251541Srgrimes * SUCH DAMAGE. 261541Srgrimes */ 271541Srgrimes 281541Srgrimes/*- 2911150Swollman * Portions of this driver taken from the original FreeBSD cd driver. 3050477Speter * Written by Julian Elischer (julian@tfs.com) 311541Srgrimes * for TRW Financial Systems for use under the MACH(2.5) operating system. 321541Srgrimes * 3332752Seivind * TRW Financial Systems, in accordance with their agreement with Carnegie 34125680Sbms * Mellon University, makes this software available to CMU to distribute 3554263Sshin * or use in any manner that they see fit as long as this message is kept with 3656041Sshin * the software. For this reason TFS also grants any other persons or 37101106Srwatson * organisations permission to use or modify this software. 3829514Sjoerg * 39130989Sps * TFS supplies this software to be publicly redistributed 4029514Sjoerg * on the understanding that TFS is not responsible for the correct 411541Srgrimes * functioning of this software in any circumstances. 421541Srgrimes * 4350673Sjlemon * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 4412172Sphk * 4512172Sphk * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $ 46101106Srwatson */ 471541Srgrimes 481541Srgrimes#include <sys/cdefs.h> 4955679Sshin__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_cd.c 206648 2010-04-15 08:29:14Z avg $"); 5055679Sshin 5155679Sshin#include "opt_cd.h" 5248758Sgreen 531541Srgrimes#include <sys/param.h> 541541Srgrimes#include <sys/systm.h> 551541Srgrimes#include <sys/kernel.h> 5675619Skris#include <sys/bio.h> 5734923Sbde#include <sys/conf.h> 5892760Sjeff#include <sys/disk.h> 591541Srgrimes#include <sys/malloc.h> 601541Srgrimes#include <sys/cdio.h> 611541Srgrimes#include <sys/cdrio.h> 621541Srgrimes#include <sys/dvdio.h> 631541Srgrimes#include <sys/devicestat.h> 641541Srgrimes#include <sys/sysctl.h> 651541Srgrimes#include <sys/taskqueue.h> 6655679Sshin#include <geom/geom_disk.h> 6755679Sshin 6855679Sshin#include <cam/cam.h> 691541Srgrimes#include <cam/cam_ccb.h> 7055679Sshin#include <cam/cam_periph.h> 7155679Sshin#include <cam/cam_xpt_periph.h> 7255679Sshin#include <cam/cam_queue.h> 737090Sbde#include <cam/cam_sim.h> 741541Srgrimes 7555679Sshin#include <cam/scsi/scsi_message.h> 7655679Sshin#include <cam/scsi/scsi_da.h> 77148385Sume#include <cam/scsi/scsi_cd.h> 78122922Sandre 7955679Sshin#define LEADOUT 0xaa /* leadout toc entry */ 80145360Sandre 811541Srgrimesstruct cd_params { 821541Srgrimes u_int32_t blksize; 831541Srgrimes u_long disksize; 841541Srgrimes}; 851541Srgrimes 8655679Sshintypedef enum { 8755679Sshin CD_Q_NONE = 0x00, 8855679Sshin CD_Q_NO_TOUCH = 0x01, 891541Srgrimes CD_Q_BCD_TRACKS = 0x02, 906283Swollman CD_Q_NO_CHANGER = 0x04, 916283Swollman CD_Q_CHANGER = 0x08, 926283Swollman CD_Q_10_BYTE_ONLY = 0x10 9355679Sshin} cd_quirks; 941541Srgrimes 9555679Sshintypedef enum { 9655679Sshin CD_FLAG_INVALID = 0x0001, 9762587Sitojun CD_FLAG_NEW_DISC = 0x0002, 9862587Sitojun CD_FLAG_DISC_LOCKED = 0x0004, 9962587Sitojun CD_FLAG_DISC_REMOVABLE = 0x0008, 100137396Ssuz CD_FLAG_TAGGED_QUEUING = 0x0010, 10155679Sshin CD_FLAG_CHANGER = 0x0040, 10255679Sshin CD_FLAG_ACTIVE = 0x0080, 103105199Ssam CD_FLAG_SCHED_ON_COMP = 0x0100, 104105199Ssam CD_FLAG_RETRY_UA = 0x0200, 105125680Sbms CD_FLAG_VALID_MEDIA = 0x0400, 106105199Ssam CD_FLAG_VALID_TOC = 0x0800, 107105199Ssam CD_FLAG_SCTX_INIT = 0x1000, 108105199Ssam CD_FLAG_OPEN = 0x2000 109125680Sbms} cd_flags; 110105199Ssam 111105199Ssamtypedef enum { 112105199Ssam CD_CCB_PROBE = 0x01, 11358698Sjlemon CD_CCB_BUFFER_IO = 0x02, 11482122Ssilby CD_CCB_WAITING = 0x03, 11558698Sjlemon CD_CCB_TYPE_MASK = 0x0F, 116133874Srwatson CD_CCB_RETRY_UA = 0x10 117133874Srwatson} cd_ccb_state; 11846381Sbillf 11912296Sphktypedef enum { 12052904Sshin CHANGER_TIMEOUT_SCHED = 0x01, 12152904Sshin CHANGER_SHORT_TMOUT_SCHED = 0x02, 12252904Sshin CHANGER_MANUAL_CALL = 0x04, 12355679Sshin CHANGER_NEED_TIMEOUT = 0x08 12455679Sshin} cd_changer_flags; 12552904Sshin 12652904Sshin#define ccb_state ppriv_field0 127124258Sandre#define ccb_bp ppriv_ptr1 128124258Sandre 129124258Sandrestruct cd_tocdata { 130124258Sandre struct ioc_toc_header header; 131124258Sandre struct cd_toc_entry entries[100]; 132124258Sandre}; 133124258Sandre 134124258Sandrestruct cd_toc_single { 135124258Sandre struct ioc_toc_header header; 136124258Sandre struct cd_toc_entry entry; 137124258Sandre}; 138124258Sandre 139124258Sandretypedef enum { 140124258Sandre CD_STATE_PROBE, 141124258Sandre CD_STATE_NORMAL 142124258Sandre} cd_state; 143124258Sandre 144124258Sandrestruct cd_softc { 145124258Sandre cam_pinfo pinfo; 146124258Sandre cd_state state; 147124258Sandre volatile cd_flags flags; 148124258Sandre struct bio_queue_head bio_queue; 149124258Sandre LIST_HEAD(, ccb_hdr) pending_ccbs; 150124258Sandre struct cd_params params; 15150673Sjlemon union ccb saved_ccb; 152133874Srwatson cd_quirks quirks; 153133874Srwatson STAILQ_ENTRY(cd_softc) changer_links; 15446381Sbillf struct cdchanger *changer; 15550673Sjlemon int bufs_left; 15612296Sphk struct cam_periph *periph; 15786764Sjlemon int minimum_command_size; 158133874Srwatson int outstanding_cmds; 15946381Sbillf struct task sysctl_task; 16012296Sphk struct sysctl_ctx_list sysctl_ctx; 16150426Sjlemon struct sysctl_oid *sysctl_tree; 162121307Ssilby STAILQ_HEAD(, cd_mode_params) mode_queue; 16350426Sjlemon struct cd_tocdata toc; 16450426Sjlemon struct disk *disk; 16555198Smsmith}; 16666376Sbmilekic 16766376Sbmilekicstruct cd_page_sizes { 16855198Smsmith int page; 169133874Srwatson int page_size; 17046381Sbillf}; 17136079Swollman 17272959Sjlemonstatic struct cd_page_sizes cd_page_size_table[] = 173133874Srwatson{ 17472959Sjlemon { AUDIO_PAGE, sizeof(struct cd_audio_page)} 17570103Sphk}; 17682122Ssilby 17782122Ssilbystruct cd_quirk_entry { 17882122Ssilby struct scsi_inquiry_pattern inq_pat; 17982122Ssilby cd_quirks quirks; 180102017Sdillon}; 181133874Srwatson 182133874Srwatson/* 183102017Sdillon * The changer quirk entries aren't strictly necessary. Basically, what 184102017Sdillon * they do is tell cdregister() up front that a device is a changer. 185133072Sandre * Otherwise, it will figure that fact out once it sees a LUN on the device 186133072Sandre * that is greater than 0. If it is known up front that a device is a changer, 187133072Sandre * all I/O to the device will go through the changer scheduling routines, as 188124199Sandre * opposed to the "normal" CD code. 189133072Sandre * 190102017Sdillon * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 191102017Sdillon * your device hangs when it gets a 10 byte command. Adding a quirk just 192104825Sdillon * to get rid of the informative diagnostic message is not acceptable. All 193133072Sandre * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 194102017Sdillon * referenced in a comment along with the quirk) , and must be approved by 195102017Sdillon * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 196155767Sandre * be removed until the submitter can explain why they are needed. 197155767Sandre * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 198155767Sandre * when the CAM_NEW_TRAN_CODE work is done. 199155767Sandre */ 200155767Sandrestatic struct cd_quirk_entry cd_quirk_table[] = 201107881Sdillon{ 202133072Sandre { 203102017Sdillon { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 204102017Sdillon /*quirks*/ CD_Q_CHANGER 205102017Sdillon }, 206133072Sandre { 207102017Sdillon { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*", 208133072Sandre "*"}, /* quirks */ CD_Q_CHANGER 209107881Sdillon }, 210133072Sandre { 211107881Sdillon { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"}, 212102017Sdillon /* quirks */ CD_Q_CHANGER 213130989Sps }, 214130989Sps { 21598211Shsu { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 216128452Ssilby /* quirks */ CD_Q_BCD_TRACKS 21712296Sphk } 2187684Sdg}; 21932821Sdg 22043562Smsmithstatic disk_open_t cdopen; 22143562Smsmithstatic disk_close_t cdclose; 22243562Smsmithstatic disk_ioctl_t cdioctl; 2237684Sdgstatic disk_strategy_t cdstrategy; 2247684Sdg 22532821Sdgstatic periph_init_t cdinit; 2267684Sdgstatic periph_ctor_t cdregister; 2271541Srgrimesstatic periph_dtor_t cdcleanup; 2281541Srgrimesstatic periph_start_t cdstart; 229111145Sjlemonstatic periph_oninv_t cdoninvalidate; 230111145Sjlemonstatic void cdasync(void *callback_arg, u_int32_t code, 231123608Sjhb struct cam_path *path, void *arg); 232111145Sjlemonstatic int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 23334881Swollmanstatic void cdshorttimeout(void *arg); 234111145Sjlemonstatic void cdschedule(struct cam_periph *periph, int priority); 23534881Swollmanstatic void cdrunchangerqueue(void *arg); 236111145Sjlemonstatic void cdchangerschedule(struct cd_softc *softc); 237111145Sjlemonstatic int cdrunccb(union ccb *ccb, 23834881Swollman int (*error_routine)(union ccb *ccb, 23934881Swollman u_int32_t cam_flags, 240111145Sjlemon u_int32_t sense_flags), 241111145Sjlemon u_int32_t cam_flags, u_int32_t sense_flags); 242128452Ssilbystatic union ccb *cdgetccb(struct cam_periph *periph, 243111145Sjlemon u_int32_t priority); 24434881Swollmanstatic void cddone(struct cam_periph *periph, 2451541Srgrimes union ccb *start_ccb); 2461541Srgrimesstatic union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 2471541Srgrimesstatic int cdgetpagesize(int page_num); 2481541Srgrimesstatic void cdprevent(struct cam_periph *periph, int action); 2491541Srgrimesstatic int cdcheckmedia(struct cam_periph *periph); 25077843Speterstatic int cdsize(struct cam_periph *periph, u_int32_t *size); 251133874Srwatsonstatic int cd6byteworkaround(union ccb *ccb); 25250673Sjlemonstatic int cderror(union ccb *ccb, u_int32_t cam_flags, 25350673Sjlemon u_int32_t sense_flags); 25450673Sjlemonstatic int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 25550673Sjlemon u_int32_t start, u_int8_t *data, 25650673Sjlemon u_int32_t len, u_int32_t sense_flags); 25750673Sjlemonstatic int cdgetmode(struct cam_periph *periph, 258100335Sdillon struct cd_mode_params *data, u_int32_t page); 259100335Sdillonstatic int cdsetmode(struct cam_periph *periph, 260155767Sandre struct cd_mode_params *data); 26150673Sjlemonstatic int cdplay(struct cam_periph *periph, u_int32_t blk, 26298102Shsu u_int32_t len); 2637684Sdgstatic int cdreadsubchannel(struct cam_periph *periph, 2647684Sdg u_int32_t mode, u_int32_t format, 26577900Speter int track, 26643576Smsmith struct cd_sub_channel_info *data, 26743562Smsmith u_int32_t len); 26843562Smsmithstatic int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 26943562Smsmith u_int32_t starts, u_int32_t startf, 27050426Sjlemon u_int32_t endm, u_int32_t ends, 27143562Smsmith u_int32_t endf); 27243562Smsmithstatic int cdplaytracks(struct cam_periph *periph, 27334923Sbde u_int32_t strack, u_int32_t sindex, 274133874Srwatson u_int32_t etrack, u_int32_t eindex); 275133517Sandrestatic int cdpause(struct cam_periph *periph, u_int32_t go); 27692760Sjeffstatic int cdstopunit(struct cam_periph *periph, u_int32_t eject); 27755679Sshinstatic int cdstartunit(struct cam_periph *periph, int load); 27855679Sshinstatic int cdsetspeed(struct cam_periph *periph, 27955679Sshin u_int32_t rdspeed, u_int32_t wrspeed); 28055679Sshinstatic int cdreportkey(struct cam_periph *periph, 28155679Sshin struct dvd_authinfo *authinfo); 28255679Sshinstatic int cdsendkey(struct cam_periph *periph, 28355679Sshin struct dvd_authinfo *authinfo); 28455679Sshinstatic int cdreaddvdstructure(struct cam_periph *periph, 2851541Srgrimes struct dvd_struct *dvdstruct); 28655679Sshin 287111145Sjlemonstatic struct periph_driver cddriver = 288111145Sjlemon{ 289111145Sjlemon cdinit, "cd", 290133874Srwatson TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 291133517Sandre}; 292111145Sjlemon 293133874SrwatsonPERIPHDRIVER_DECLARE(cd, cddriver); 294133517Sandre 295121453Ssilby#ifndef CD_DEFAULT_RETRY 296112009Sjlemon#define CD_DEFAULT_RETRY 4 29786764Sjlemon#endif 298122922Sandre#ifndef CHANGER_MIN_BUSY_SECONDS 299126193Sandre#define CHANGER_MIN_BUSY_SECONDS 5 300128452Ssilby#endif 301128452Ssilby#ifndef CHANGER_MAX_BUSY_SECONDS 302128452Ssilby#define CHANGER_MAX_BUSY_SECONDS 15 303128452Ssilby#endif 304133874Srwatson 305133517Sandrestatic int cd_retry_count = CD_DEFAULT_RETRY; 3061541Srgrimesstatic int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 3071541Srgrimesstatic int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 308128452Ssilby 309128452SsilbySYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 310128452SsilbySYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 311128452SsilbySYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW, 312128452Ssilby &cd_retry_count, 0, "Normal I/O retry count"); 313128452SsilbyTUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count); 314128452SsilbySYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 315128452Ssilby &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 3161541SrgrimesTUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds); 31778642SsilbySYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 31878642Ssilby &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 31978642SsilbyTUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds); 3201541Srgrimes 32178642Ssilbystruct cdchanger { 322111144Sjlemon path_id_t path_id; 323111144Sjlemon target_id_t target_id; 32478642Ssilby int num_devices; 32578642Ssilby struct camq devq; 3261541Srgrimes struct timeval start_time; 327111144Sjlemon struct cd_softc *cur_device; 3281541Srgrimes struct callout short_handle; 329138410Srwatson struct callout long_handle; 330138410Srwatson volatile cd_changer_flags flags; 33155679Sshin STAILQ_ENTRY(cdchanger) changer_links; 33255679Sshin STAILQ_HEAD(chdevlist, cd_softc) chluns; 33378642Ssilby}; 33455679Sshin 33578642Ssilbystatic struct mtx changerq_mtx; 33655679Sshinstatic STAILQ_HEAD(changerlist, cdchanger) changerq; 33755679Sshinstatic int num_changers; 33855679Sshin 33955679SshinMALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers"); 34055679Sshin 34155679Sshinstatic void 34255679Sshincdinit(void) 34355679Sshin{ 34455679Sshin cam_status status; 34555679Sshin 34678642Ssilby mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF); 347111144Sjlemon STAILQ_INIT(&changerq); 34855679Sshin 349111144Sjlemon /* 350111144Sjlemon * Install a global async callback. This callback will 351111144Sjlemon * receive async callbacks like "new device found". 352111144Sjlemon */ 353111144Sjlemon status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL); 354111144Sjlemon 355111144Sjlemon if (status != CAM_REQ_CMP) { 356111144Sjlemon printf("cd: Failed to attach master async callback " 357111144Sjlemon "due to status 0x%x!\n", status); 358111144Sjlemon } 359111144Sjlemon} 360111144Sjlemon 36178642Ssilbystatic void 362111144Sjlemoncdoninvalidate(struct cam_periph *periph) 363111144Sjlemon{ 364111144Sjlemon struct cd_softc *softc; 365111144Sjlemon 366111144Sjlemon softc = (struct cd_softc *)periph->softc; 367111144Sjlemon 368111144Sjlemon /* 369111144Sjlemon * De-register any async callbacks. 370111144Sjlemon */ 371111144Sjlemon xpt_register_async(0, cdasync, periph, periph->path); 37278642Ssilby 37378642Ssilby softc->flags |= CD_FLAG_INVALID; 37478642Ssilby 37578642Ssilby /* 37678642Ssilby * Return all queued I/O with ENXIO. 37778642Ssilby * XXX Handle any transactions queued to the card 37878642Ssilby * with XPT_ABORT_CCB. 37978642Ssilby */ 380111144Sjlemon bioq_flush(&softc->bio_queue, NULL, ENXIO); 381111144Sjlemon 38278642Ssilby /* 38378642Ssilby * If this device is part of a changer, and it was scheduled 38478642Ssilby * to run, remove it from the run queue since we just nuked 38578642Ssilby * all of its scheduled I/O. 386149635Sandre */ 38778642Ssilby if ((softc->flags & CD_FLAG_CHANGER) 38878642Ssilby && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 38978642Ssilby camq_remove(&softc->changer->devq, softc->pinfo.index); 39078642Ssilby 39178642Ssilby disk_gone(softc->disk); 392111144Sjlemon xpt_print(periph->path, "lost device\n"); 3931541Srgrimes} 3941541Srgrimes 3951541Srgrimesstatic void 3961541Srgrimescdcleanup(struct cam_periph *periph) 3971541Srgrimes{ 398127870Srwatson struct cd_softc *softc; 3991541Srgrimes 4001541Srgrimes softc = (struct cd_softc *)periph->softc; 40178642Ssilby 40278642Ssilby xpt_print(periph->path, "removing device entry\n"); 40378642Ssilby 4041541Srgrimes /* 4051541Srgrimes * In the queued, non-active case, the device in question 4061541Srgrimes * has already been removed from the changer run queue. Since this 40731848Sjulian * device is active, we need to de-activate it, and schedule 40831848Sjulian * another device to run. (if there is another one to run) 4091541Srgrimes */ 4101541Srgrimes if ((softc->flags & CD_FLAG_CHANGER) 41155679Sshin && (softc->flags & CD_FLAG_ACTIVE)) { 4121541Srgrimes 41355679Sshin /* 41455679Sshin * The purpose of the short timeout is soley to determine 4151541Srgrimes * whether the current device has finished or not. Well, 4161541Srgrimes * since we're removing the active device, we know that it 4171541Srgrimes * is finished. So, get rid of the short timeout. 4181541Srgrimes * Otherwise, if we're in the time period before the short 4191541Srgrimes * timeout fires, and there are no other devices in the 4201541Srgrimes * queue to run, there won't be any other device put in the 42155679Sshin * active slot. i.e., when we call cdrunchangerqueue() 42255679Sshin * below, it won't do anything. Then, when the short 42355679Sshin * timeout fires, it'll look at the "current device", which 42455679Sshin * we are free below, and possibly panic the kernel on a 42555679Sshin * bogus pointer reference. 42655679Sshin * 42755679Sshin * The long timeout doesn't really matter, since we 428128905Srwatson * decrement the qfrozen_cnt to indicate that there is 4291541Srgrimes * nothing in the active slot now. Therefore, there won't 430101137Srwatson * be any bogus pointer references there. 431101137Srwatson */ 43255679Sshin if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 433105586Sphk callout_stop(&softc->changer->short_handle); 43455679Sshin softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 43555679Sshin } 43655679Sshin softc->changer->devq.qfrozen_cnt[0]--; 43755679Sshin softc->changer->flags |= CHANGER_MANUAL_CALL; 438127871Srwatson cdrunchangerqueue(softc->changer); 439122327Ssam } 440122327Ssam 441122327Ssam /* 442122327Ssam * If we're removing the last device on the changer, go ahead and 443128905Srwatson * remove the changer device structure. 444128905Srwatson */ 445128905Srwatson if ((softc->flags & CD_FLAG_CHANGER) 446128905Srwatson && (--softc->changer->num_devices == 0)) { 44757576Sps 448122327Ssam /* 44957576Sps * Theoretically, there shouldn't be any timeouts left, but 45057576Sps * I'm not completely sure that that will be the case. So, 45157576Sps * it won't hurt to check and see if there are any left. 4521541Srgrimes */ 453127870Srwatson if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 454151967Sandre callout_stop(&softc->changer->long_handle); 4551541Srgrimes softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 4561541Srgrimes } 4571541Srgrimes 4581541Srgrimes if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 45955679Sshin callout_stop(&softc->changer->short_handle); 46055679Sshin softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 461133874Srwatson } 46255679Sshin 46355679Sshin mtx_lock(&changerq_mtx); 46455679Sshin STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 46555679Sshin changer_links); 46655679Sshin num_changers--; 46755679Sshin mtx_unlock(&changerq_mtx); 46855679Sshin xpt_print(periph->path, "removing changer entry\n"); 46955679Sshin free(softc->changer, M_DEVBUF); 47055679Sshin } 47155679Sshin cam_periph_unlock(periph); 47255679Sshin if ((softc->flags & CD_FLAG_SCTX_INIT) != 0 4731541Srgrimes && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 4741541Srgrimes xpt_print(periph->path, "can't remove sysctl context\n"); 4751541Srgrimes } 476127870Srwatson 47755679Sshin disk_destroy(softc->disk); 47855679Sshin free(softc, M_DEVBUF); 4791541Srgrimes cam_periph_lock(periph); 4801541Srgrimes} 48155679Sshin 48255679Sshinstatic void 48355679Sshincdasync(void *callback_arg, u_int32_t code, 48455679Sshin struct cam_path *path, void *arg) 48555679Sshin{ 48655679Sshin struct cam_periph *periph; 48755679Sshin 48855679Sshin periph = (struct cam_periph *)callback_arg; 48955679Sshin switch (code) { 49055679Sshin case AC_FOUND_DEVICE: 49155679Sshin { 49255679Sshin struct ccb_getdev *cgd; 49355679Sshin cam_status status; 49455679Sshin 49555679Sshin cgd = (struct ccb_getdev *)arg; 49655679Sshin if (cgd == NULL) 49755679Sshin break; 49855679Sshin 49955679Sshin if (cgd->protocol != PROTO_SCSI) 50055679Sshin break; 5011541Srgrimes 5021541Srgrimes if (SID_TYPE(&cgd->inq_data) != T_CDROM 50355679Sshin && SID_TYPE(&cgd->inq_data) != T_WORM) 50455679Sshin break; 50590198Sume 50690198Sume /* 50790198Sume * Allocate a peripheral instance for 50855679Sshin * this device and start the probe 50955679Sshin * process. 51055679Sshin */ 51156039Sshin status = cam_periph_alloc(cdregister, cdoninvalidate, 51255679Sshin cdcleanup, cdstart, 513133874Srwatson "cd", CAM_PERIPH_BIO, 514133874Srwatson cgd->ccb_h.path, cdasync, 515133874Srwatson AC_FOUND_DEVICE, cgd); 516133874Srwatson 517133874Srwatson if (status != CAM_REQ_CMP 518133874Srwatson && status != CAM_REQ_INPROG) 519133874Srwatson printf("cdasync: Unable to attach new device " 5201541Srgrimes "due to status 0x%x\n", status); 5211541Srgrimes 522127870Srwatson break; 523101106Srwatson } 524122327Ssam case AC_SENT_BDR: 525101106Srwatson case AC_BUS_RESET: 526101106Srwatson { 527101106Srwatson struct cd_softc *softc; 528101106Srwatson struct ccb_hdr *ccbh; 529128905Srwatson 530128905Srwatson softc = (struct cd_softc *)periph->softc; 531101106Srwatson /* 532101106Srwatson * Don't fail on the expected unit attention 533119245Srwatson * that will occur. 534119245Srwatson */ 535101106Srwatson softc->flags |= CD_FLAG_RETRY_UA; 536119245Srwatson LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 537101106Srwatson ccbh->ccb_state |= CD_CCB_RETRY_UA; 538101106Srwatson /* FALLTHROUGH */ 53955679Sshin } 54055679Sshin default: 54155679Sshin cam_periph_async(periph, code, path, arg); 54255679Sshin break; 54355679Sshin } 544127870Srwatson} 54555679Sshin 5461541Srgrimesstatic void 54755679Sshincdsysctlinit(void *context, int pending) 54855679Sshin{ 54955679Sshin struct cam_periph *periph; 55055679Sshin struct cd_softc *softc; 55159392Sshin char tmpstr[80], tmpstr2[80]; 55255679Sshin 55355679Sshin periph = (struct cam_periph *)context; 55455679Sshin if (cam_periph_acquire(periph) != CAM_REQ_CMP) 555127870Srwatson return; 556127870Srwatson 55755679Sshin softc = (struct cd_softc *)periph->softc; 55855679Sshin snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 559133874Srwatson snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 560133874Srwatson 561133874Srwatson sysctl_ctx_init(&softc->sysctl_ctx); 562133874Srwatson softc->flags |= CD_FLAG_SCTX_INIT; 563133874Srwatson softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 564133874Srwatson SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 5656283Swollman tmpstr2, CTLFLAG_RD, 0, tmpstr); 566122327Ssam 56755679Sshin if (softc->sysctl_tree == NULL) { 5686283Swollman printf("cdsysctlinit: unable to allocate sysctl tree\n"); 56955679Sshin cam_periph_release(periph); 570122922Sandre return; 571122922Sandre } 572122922Sandre 57355679Sshin /* 574122922Sandre * Now register the sysctl handler, so the user can the value on 5751541Srgrimes * the fly. 5761541Srgrimes */ 5771541Srgrimes SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 5781541Srgrimes OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 5791541Srgrimes &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 58034881Swollman "Minimum CDB size"); 58134881Swollman 5821541Srgrimes cam_periph_release(periph); 5831541Srgrimes} 5841541Srgrimes 5851541Srgrimes/* 5861541Srgrimes * We have a handler function for this so we can check the values when the 587111145Sjlemon * user sets them, instead of every time we look at them. 588111145Sjlemon */ 58955679Sshinstatic int 59055679Sshincdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 59155679Sshin{ 5921541Srgrimes int error, value; 593111145Sjlemon 594111145Sjlemon value = *(int *)arg1; 595111145Sjlemon 596111145Sjlemon error = sysctl_handle_int(oidp, &value, 0, req); 597111145Sjlemon 59855679Sshin if ((error != 0) 59955679Sshin || (req->newptr == NULL)) 60055679Sshin return (error); 60155679Sshin 60255679Sshin /* 6031541Srgrimes * The only real values we can have here are 6 or 10. I don't 60450673Sjlemon * really forsee having 12 be an option at any time in the future. 605142906Sglebius * So if the user sets something less than or equal to 6, we'll set 606142906Sglebius * it to 6. If he sets something greater than 6, we'll set it to 10. 607142906Sglebius * 608142906Sglebius * I suppose we could just return an error here for the wrong values, 609142906Sglebius * but I don't think it's necessary to do so, as long as we can 61050673Sjlemon * determine the user's intent without too much trouble. 6116283Swollman */ 6126283Swollman if (value < 6) 613130989Sps value = 6; 614147735Sps else if (value > 6) 61534881Swollman value = 10; 6161541Srgrimes 6171541Srgrimes *(int *)arg1 = value; 61816367Swollman 6191541Srgrimes return (0); 6201541Srgrimes} 6211541Srgrimes 62216367Swollmanstatic cam_status 623100335Sdilloncdregister(struct cam_periph *periph, void *arg) 62416367Swollman{ 6251541Srgrimes struct cd_softc *softc; 626102017Sdillon struct ccb_pathinq cpi; 6271541Srgrimes struct ccb_getdev *cgd; 62850673Sjlemon char tmpstr[80]; 629102017Sdillon caddr_t match; 630133874Srwatson 63156564Sshin cgd = (struct ccb_getdev *)arg; 63256564Sshin if (periph == NULL) { 63356564Sshin printf("cdregister: periph was NULL!!\n"); 63456564Sshin return(CAM_REQ_CMP_ERR); 63524570Sdg } 6361541Srgrimes if (cgd == NULL) { 63734881Swollman printf("cdregister: no getdev CCB, can't register device\n"); 6381541Srgrimes return(CAM_REQ_CMP_ERR); 6391541Srgrimes } 6401541Srgrimes 6411541Srgrimes softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF, 6421541Srgrimes M_NOWAIT | M_ZERO); 6431541Srgrimes if (softc == NULL) { 6441541Srgrimes printf("cdregister: Unable to probe new device. " 6451541Srgrimes "Unable to allocate softc\n"); 6461541Srgrimes return(CAM_REQ_CMP_ERR); 6471541Srgrimes } 6481541Srgrimes 6491541Srgrimes LIST_INIT(&softc->pending_ccbs); 6501541Srgrimes STAILQ_INIT(&softc->mode_queue); 6511541Srgrimes softc->state = CD_STATE_PROBE; 652146864Srwatson bioq_init(&softc->bio_queue); 653138019Srwatson if (SID_IS_REMOVABLE(&cgd->inq_data)) 654146864Srwatson softc->flags |= CD_FLAG_DISC_REMOVABLE; 6551541Srgrimes if ((cgd->inq_data.flags & SID_CmdQue) != 0) 6561541Srgrimes softc->flags |= CD_FLAG_TAGGED_QUEUING; 6571541Srgrimes 6581541Srgrimes periph->softc = softc; 6591541Srgrimes softc->periph = periph; 6601541Srgrimes 6611541Srgrimes /* 6621541Srgrimes * See if this device has any quirks. 6631541Srgrimes */ 6641541Srgrimes match = cam_quirkmatch((caddr_t)&cgd->inq_data, 6651541Srgrimes (caddr_t)cd_quirk_table, 6661541Srgrimes sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 667157376Srwatson sizeof(*cd_quirk_table), scsi_inquiry_match); 668111145Sjlemon 669111145Sjlemon if (match != NULL) 6701541Srgrimes softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 671111145Sjlemon else 6721541Srgrimes softc->quirks = CD_Q_NONE; 6731541Srgrimes 67455679Sshin /* Check if the SIM does not want 6 byte commands */ 67555679Sshin bzero(&cpi, sizeof(cpi)); 67655679Sshin xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 6771541Srgrimes cpi.ccb_h.func_code = XPT_PATH_INQ; 678157376Srwatson xpt_action((union ccb *)&cpi); 679157376Srwatson if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 680157376Srwatson softc->quirks |= CD_Q_10_BYTE_ONLY; 681157376Srwatson 682157376Srwatson TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph); 683157376Srwatson 684138410Srwatson /* The default is 6 byte commands, unless quirked otherwise */ 685138410Srwatson if (softc->quirks & CD_Q_10_BYTE_ONLY) 6861541Srgrimes softc->minimum_command_size = 10; 68750673Sjlemon else 68850673Sjlemon softc->minimum_command_size = 6; 68950673Sjlemon 69050673Sjlemon /* 69150673Sjlemon * Load the user's default, if any. 69250673Sjlemon */ 69350673Sjlemon snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 69450673Sjlemon periph->unit_number); 69550673Sjlemon TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 69650673Sjlemon 6979373Swollman /* 6 and 10 are the only permissible values here. */ 6989373Swollman if (softc->minimum_command_size < 6) 699122922Sandre softc->minimum_command_size = 6; 700122922Sandre else if (softc->minimum_command_size > 6) 701122922Sandre softc->minimum_command_size = 10; 702122922Sandre 703122922Sandre /* 704122922Sandre * We need to register the statistics structure for this device, 7051541Srgrimes * but we don't have the blocksize yet for it. So, we register 706122922Sandre * the structure and indicate that we don't have the blocksize 707122922Sandre * yet. Unlike other SCSI peripheral drivers, we explicitly set 708122922Sandre * the device type here to be CDROM, rather than just ORing in 7091541Srgrimes * the device type. This is because this driver can attach to either 710122922Sandre * CDROM or WORM devices, and we want this peripheral driver to 7111541Srgrimes * show up in the devstat list as a CD peripheral driver, not a 712122922Sandre * WORM peripheral driver. WORM drives will also have the WORM 713122922Sandre * driver attached to them. 714122922Sandre */ 715122922Sandre cam_periph_unlock(periph); 7161541Srgrimes softc->disk = disk_alloc(); 717122922Sandre softc->disk->d_devstat = devstat_new_entry("cd", 718122922Sandre periph->unit_number, 0, 7191541Srgrimes DEVSTAT_BS_UNAVAILABLE, 7201541Srgrimes DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI, 7211541Srgrimes DEVSTAT_PRIORITY_CD); 7221541Srgrimes softc->disk->d_open = cdopen; 723122922Sandre softc->disk->d_close = cdclose; 724122922Sandre softc->disk->d_strategy = cdstrategy; 725122922Sandre softc->disk->d_ioctl = cdioctl; 726122922Sandre softc->disk->d_name = "cd"; 72755679Sshin softc->disk->d_unit = periph->unit_number; 72855679Sshin softc->disk->d_drv1 = periph; 72955679Sshin if (cpi.maxio == 0) 73055679Sshin softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 73155679Sshin else if (cpi.maxio > MAXPHYS) 73255679Sshin softc->disk->d_maxsize = MAXPHYS; /* for safety */ 73355679Sshin else 73455679Sshin softc->disk->d_maxsize = cpi.maxio; 73555679Sshin softc->disk->d_flags = 0; 736122922Sandre disk_create(softc->disk, DISK_VERSION); 737122922Sandre cam_periph_lock(periph); 738122922Sandre 739122922Sandre /* 740122922Sandre * Add an async callback so that we get 741122922Sandre * notified if this device goes away. 742122922Sandre */ 743122922Sandre xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 744122922Sandre cdasync, periph, periph->path); 745133874Srwatson 746122922Sandre /* 747122922Sandre * If the target lun is greater than 0, we most likely have a CD 748122922Sandre * changer device. Check the quirk entries as well, though, just 7491541Srgrimes * in case someone has a CD tower with one lun per drive or 750122922Sandre * something like that. Also, if we know up front that a 7511541Srgrimes * particular device is a changer, we can mark it as such starting 752111145Sjlemon * with lun 0, instead of lun 1. It shouldn't be necessary to have 75355679Sshin * a quirk entry to define something as a changer, however. 75455679Sshin */ 755126193Sandre if (((cgd->ccb_h.target_lun > 0) 756126193Sandre && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 757126193Sandre || ((softc->quirks & CD_Q_CHANGER) != 0)) { 7581541Srgrimes struct cdchanger *nchanger; 759130989Sps struct cam_periph *nperiph; 76032821Sdg struct cam_path *path; 761108265Shsu cam_status status; 762111145Sjlemon int found; 763157376Srwatson 764157376Srwatson /* Set the changer flag in the current device's softc */ 765157376Srwatson softc->flags |= CD_FLAG_CHANGER; 766157376Srwatson 767157376Srwatson /* 768157376Srwatson * Now, look around for an existing changer device with the 769111145Sjlemon * same path and target ID as the current device. 770111145Sjlemon */ 771111145Sjlemon mtx_lock(&changerq_mtx); 772111145Sjlemon for (found = 0, 773111145Sjlemon nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 774111145Sjlemon nchanger != NULL; 775111145Sjlemon nchanger = STAILQ_NEXT(nchanger, changer_links)){ 776111145Sjlemon if ((nchanger->path_id == cgd->ccb_h.path_id) 777111145Sjlemon && (nchanger->target_id == cgd->ccb_h.target_id)) { 778111145Sjlemon found = 1; 779111145Sjlemon break; 780111145Sjlemon } 781111145Sjlemon } 782157376Srwatson mtx_unlock(&changerq_mtx); 783111145Sjlemon 784146864Srwatson /* 785138410Srwatson * If we found a matching entry, just add this device to 786138410Srwatson * the list of devices on this changer. 787157376Srwatson */ 788157376Srwatson if (found == 1) { 789157376Srwatson struct chdevlist *chlunhead; 790157376Srwatson 791157376Srwatson chlunhead = &nchanger->chluns; 792157376Srwatson 793157376Srwatson /* 794157376Srwatson * XXX KDM look at consolidating this code with the 795157376Srwatson * code below in a separate function. 796157376Srwatson */ 797157376Srwatson 79855679Sshin /* 799157376Srwatson * Create a path with lun id 0, and see if we can 800157376Srwatson * find a matching device 801157376Srwatson */ 802157376Srwatson status = xpt_create_path(&path, /*periph*/ periph, 803111145Sjlemon cgd->ccb_h.path_id, 804157376Srwatson cgd->ccb_h.target_id, 0); 805157376Srwatson 806157376Srwatson if ((status == CAM_REQ_CMP) 807157376Srwatson && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 808157376Srwatson struct cd_softc *nsoftc; 809157376Srwatson 810157376Srwatson nsoftc = (struct cd_softc *)nperiph->softc; 811157376Srwatson 812157376Srwatson if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 813157376Srwatson nsoftc->flags |= CD_FLAG_CHANGER; 814157376Srwatson nchanger->num_devices++; 815157376Srwatson if (camq_resize(&nchanger->devq, 8161541Srgrimes nchanger->num_devices)!=CAM_REQ_CMP){ 8171541Srgrimes printf("cdregister: " 8181541Srgrimes "camq_resize " 8191541Srgrimes "failed, changer " 8201541Srgrimes "support may " 82155198Smsmith "be messed up\n"); 82255198Smsmith } 82355198Smsmith nsoftc->changer = nchanger; 82455198Smsmith nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 82555679Sshin 8261541Srgrimes STAILQ_INSERT_TAIL(&nchanger->chluns, 82755198Smsmith nsoftc,changer_links); 82855198Smsmith } 82955198Smsmith xpt_free_path(path); 83055198Smsmith } else if (status == CAM_REQ_CMP) 83155198Smsmith xpt_free_path(path); 832133874Srwatson else { 833133874Srwatson printf("cdregister: unable to allocate path\n" 83455198Smsmith "cdregister: changer support may be " 83598102Shsu "broken\n"); 83674362Sphk } 837111145Sjlemon 838111145Sjlemon nchanger->num_devices++; 83998102Shsu 840127870Srwatson softc->changer = nchanger; 84174362Sphk softc->pinfo.index = CAM_UNQUEUED_INDEX; 84274362Sphk 84355679Sshin if (camq_resize(&nchanger->devq, 84455679Sshin nchanger->num_devices) != CAM_REQ_CMP) { 845126193Sandre printf("cdregister: camq_resize " 846126193Sandre "failed, changer support may " 847126193Sandre "be messed up\n"); 84855198Smsmith } 849144857Sps 85055198Smsmith STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 85198102Shsu } 85255198Smsmith /* 85398102Shsu * In this case, we don't already have an entry for this 85455198Smsmith * particular changer, so we need to create one, add it to 8551541Srgrimes * the queue, and queue this device on the list for this 8561541Srgrimes * changer. Before we queue this device, however, we need 8571541Srgrimes * to search for lun id 0 on this target, and add it to the 8581541Srgrimes * queue first, if it exists. (and if it hasn't already 8591541Srgrimes * been marked as part of the changer.) 8601541Srgrimes */ 86172960Sjlemon else { 86272960Sjlemon nchanger = malloc(sizeof(struct cdchanger), 86372960Sjlemon M_DEVBUF, M_NOWAIT | M_ZERO); 8641541Srgrimes if (nchanger == NULL) { 86598211Shsu softc->flags &= ~CD_FLAG_CHANGER; 8661541Srgrimes printf("cdregister: unable to malloc " 8671541Srgrimes "changer structure\ncdregister: " 8681541Srgrimes "changer support disabled\n"); 8691541Srgrimes 87072960Sjlemon /* 8711541Srgrimes * Yes, gotos can be gross but in this case 872146864Srwatson * I think it's justified.. 873138410Srwatson */ 874138410Srwatson goto cdregisterexit; 8751541Srgrimes } 8761541Srgrimes if (camq_init(&nchanger->devq, 1) != 0) { 8771541Srgrimes softc->flags &= ~CD_FLAG_CHANGER; 8781541Srgrimes printf("cdregister: changer support " 8791541Srgrimes "disabled\n"); 8801541Srgrimes goto cdregisterexit; 8811541Srgrimes } 8821541Srgrimes 883110896Shsu nchanger->path_id = cgd->ccb_h.path_id; 884110896Shsu nchanger->target_id = cgd->ccb_h.target_id; 885139222Srwatson 8861541Srgrimes /* this is superfluous, but it makes things clearer */ 88798211Shsu nchanger->num_devices = 0; 888157376Srwatson 889157376Srwatson STAILQ_INIT(&nchanger->chluns); 890157376Srwatson 891157376Srwatson callout_init_mtx(&nchanger->long_handle, 892157376Srwatson periph->sim->mtx, 0); 89398211Shsu callout_init_mtx(&nchanger->short_handle, 8941541Srgrimes periph->sim->mtx, 0); 895139222Srwatson 89698211Shsu mtx_lock(&changerq_mtx); 89772960Sjlemon num_changers++; 898111748Sdes STAILQ_INSERT_TAIL(&changerq, nchanger, 8991541Srgrimes changer_links); 9001541Srgrimes mtx_unlock(&changerq_mtx); 90172960Sjlemon 9021541Srgrimes /* 9031541Srgrimes * Create a path with lun id 0, and see if we can 90436079Swollman * find a matching device 90562573Sphk */ 90636079Swollman status = xpt_create_path(&path, /*periph*/ periph, 907148156Srwatson cgd->ccb_h.path_id, 90836079Swollman cgd->ccb_h.target_id, 0); 90936079Swollman 91036079Swollman /* 91136079Swollman * If we were able to allocate the path, and if we 91236079Swollman * find a matching device and it isn't already 91336079Swollman * marked as part of a changer, then we add it to 91436079Swollman * the current changer. 91536079Swollman */ 916127870Srwatson if ((status == CAM_REQ_CMP) 91736079Swollman && ((nperiph = cam_periph_find(path, "cd")) != NULL) 91836079Swollman && ((((struct cd_softc *)periph->softc)->flags & 91936079Swollman CD_FLAG_CHANGER) == 0)) { 920139222Srwatson struct cd_softc *nsoftc; 92136079Swollman 92236079Swollman nsoftc = (struct cd_softc *)nperiph->softc; 923127870Srwatson 924139222Srwatson nsoftc->flags |= CD_FLAG_CHANGER; 92536079Swollman nchanger->num_devices++; 92636079Swollman if (camq_resize(&nchanger->devq, 92736079Swollman nchanger->num_devices) != CAM_REQ_CMP) { 92836079Swollman printf("cdregister: camq_resize " 92998102Shsu "failed, changer support may " 93036079Swollman "be messed up\n"); 93136079Swollman } 93298102Shsu nsoftc->changer = nchanger; 93336079Swollman nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 934126253Struckman 935100831Struckman STAILQ_INSERT_TAIL(&nchanger->chluns, 936126253Struckman nsoftc, changer_links); 937126253Struckman xpt_free_path(path); 938100831Struckman } else if (status == CAM_REQ_CMP) 93936079Swollman xpt_free_path(path); 94036079Swollman else { 94136079Swollman printf("cdregister: unable to allocate path\n" 94236079Swollman "cdregister: changer support may be " 94336079Swollman "broken\n"); 94436079Swollman } 945139222Srwatson 94636079Swollman softc->changer = nchanger; 947111119Simp softc->pinfo.index = CAM_UNQUEUED_INDEX; 948127870Srwatson nchanger->num_devices++; 949139222Srwatson if (camq_resize(&nchanger->devq, 950133874Srwatson nchanger->num_devices) != CAM_REQ_CMP) { 95198102Shsu printf("cdregister: camq_resize " 952127870Srwatson "failed, changer support may " 95371999Sphk "be messed up\n"); 95498102Shsu } 955113345Srwatson STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 956113345Srwatson changer_links); 957113345Srwatson } 958113345Srwatson } 959113345Srwatson 960113345Srwatsoncdregisterexit: 961113345Srwatson 962113345Srwatson /* 963113345Srwatson * Refcount and block open attempts until we are setup 964113345Srwatson * Can't block 965113345Srwatson */ 966113345Srwatson (void)cam_periph_hold(periph, PRIBIO); 967113345Srwatson 968113345Srwatson if ((softc->flags & CD_FLAG_CHANGER) == 0) 969113345Srwatson xpt_schedule(periph, CAM_PRIORITY_DEV); 97098102Shsu else 97136079Swollman cdschedule(periph, CAM_PRIORITY_DEV); 97298102Shsu 97336079Swollman return(CAM_REQ_CMP); 97436079Swollman} 97536079Swollman 97636079Swollmanstatic int 97736079Swollmancdopen(struct disk *dp) 97836079Swollman{ 97936079Swollman struct cam_periph *periph; 98047960Stegge struct cd_softc *softc; 981145978Scperciva int error; 982145978Scperciva 98336079Swollman periph = (struct cam_periph *)dp->d_drv1; 98436079Swollman if (periph == NULL) 98536079Swollman return (ENXIO); 98647960Stegge 987111145Sjlemon softc = (struct cd_softc *)periph->softc; 988111145Sjlemon 989111145Sjlemon if (cam_periph_acquire(periph) != CAM_REQ_CMP) 990111145Sjlemon return(ENXIO); 991111145Sjlemon 992111145Sjlemon cam_periph_lock(periph); 99347960Stegge 994127870Srwatson if (softc->flags & CD_FLAG_INVALID) { 99536079Swollman cam_periph_unlock(periph); 996111145Sjlemon cam_periph_release(periph); 997111145Sjlemon return(ENXIO); 998111145Sjlemon } 999111145Sjlemon 1000110896Shsu if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 100136079Swollman cam_periph_unlock(periph); 100236079Swollman cam_periph_release(periph); 100336079Swollman return (error); 100436079Swollman } 100536079Swollman 100636079Swollman /* 100736079Swollman * Check for media, and set the appropriate flags. We don't bail 100836079Swollman * if we don't have media, but then we don't allow anything but the 100936079Swollman * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 101036079Swollman */ 101136079Swollman cdcheckmedia(periph); 101298102Shsu 101336079Swollman CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 101436079Swollman cam_periph_unhold(periph); 101536079Swollman 101698102Shsu /* Closes aren't symmetrical with opens, so fix up the refcounting. */ 101736079Swollman if ((softc->flags & CD_FLAG_OPEN) == 0) { 101836079Swollman softc->flags |= CD_FLAG_OPEN; 101936079Swollman cam_periph_unlock(periph); 1020139222Srwatson } else { 102136079Swollman cam_periph_unlock(periph); 102236079Swollman cam_periph_release(periph); 102336079Swollman } 102436079Swollman 102536079Swollman return (0); 102648758Sgreen} 102762573Sphk 102848758Sgreenstatic int 102972650Sgreencdclose(struct disk *dp) 103048758Sgreen{ 103148758Sgreen struct cam_periph *periph; 1032148156Srwatson struct cd_softc *softc; 103348758Sgreen 1034132653Scperciva periph = (struct cam_periph *)dp->d_drv1; 103548758Sgreen if (periph == NULL) 103648758Sgreen return (ENXIO); 103748758Sgreen 103848758Sgreen softc = (struct cd_softc *)periph->softc; 103948758Sgreen 104098102Shsu cam_periph_lock(periph); 104148758Sgreen cam_periph_hold(periph, PRIBIO); 104254263Sshin 104398102Shsu if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 104448758Sgreen cdprevent(periph, PR_ALLOW); 104598102Shsu 104648758Sgreen /* 104799837Struckman * Since we're closing this CD, mark the blocksize as unavailable. 104899837Struckman * It will be marked as available when the CD is opened again. 104999837Struckman */ 105099837Struckman softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 105199837Struckman 105292976Srwatson /* 105378697Sdwmalone * We'll check the media and toc again at the next open(). 105478697Sdwmalone */ 105591354Sdd softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 105648758Sgreen 105798102Shsu cam_periph_unhold(periph); 105898102Shsu cam_periph_unlock(periph); 105998102Shsu cam_periph_release(periph); 106099838Struckman 106199838Struckman return (0); 106248758Sgreen} 106348758Sgreen 106448758Sgreenstatic void 106578697Sdwmalonecdshorttimeout(void *arg) 106678697Sdwmalone{ 106778697Sdwmalone struct cdchanger *changer; 106848758Sgreen 106955679Sshin changer = (struct cdchanger *)arg; 107055679Sshin 107162573Sphk /* Always clear the short timeout flag, since that's what we're in */ 107255679Sshin changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 107372650Sgreen 107455679Sshin /* 107555679Sshin * Check to see if there is any more pending or outstanding I/O for 1076148156Srwatson * this device. If not, move it out of the active slot. 107755679Sshin */ 1078132653Scperciva if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 107955679Sshin && (changer->cur_device->outstanding_cmds == 0)) { 108055679Sshin changer->flags |= CHANGER_MANUAL_CALL; 108155679Sshin cdrunchangerqueue(changer); 108255679Sshin } 108355679Sshin} 1084148385Sume 1085148385Sume/* 1086148385Sume * This is a wrapper for xpt_schedule. It only applies to changers. 1087148385Sume */ 108855679Sshinstatic void 108955679Sshincdschedule(struct cam_periph *periph, int priority) 109055679Sshin{ 109155679Sshin struct cd_softc *softc; 109255679Sshin 109355679Sshin softc = (struct cd_softc *)periph->softc; 1094148385Sume 109598102Shsu /* 109655679Sshin * If this device isn't currently queued, and if it isn't 109755679Sshin * the active device, then we queue this device and run the 109855679Sshin * changer queue if there is no timeout scheduled to do it. 109955679Sshin * If this device is the active device, just schedule it 110055679Sshin * to run again. If this device is queued, there should be 110155679Sshin * a timeout in place already that will make sure it runs. 110255679Sshin */ 110355679Sshin if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 1104151254Sphilip && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 1105151254Sphilip /* 1106151254Sphilip * We don't do anything with the priority here. 110798102Shsu * This is strictly a fifo queue. 110855679Sshin */ 110998102Shsu softc->pinfo.priority = CAM_PRIORITY_NORMAL; 111055679Sshin softc->pinfo.generation = ++softc->changer->devq.generation; 111199837Struckman camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 111299837Struckman 111399837Struckman /* 111499837Struckman * Since we just put a device in the changer queue, 111599837Struckman * check and see if there is a timeout scheduled for 111692976Srwatson * this changer. If so, let the timeout handle 111778697Sdwmalone * switching this device into the active slot. If 111878697Sdwmalone * not, manually call the timeout routine to 111991354Sdd * bootstrap things. 112055679Sshin */ 112198102Shsu if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 112298102Shsu && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 112398102Shsu && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 112499838Struckman softc->changer->flags |= CHANGER_MANUAL_CALL; 112599838Struckman cdrunchangerqueue(softc->changer); 112655679Sshin } 112755679Sshin } else if ((softc->flags & CD_FLAG_ACTIVE) 112855679Sshin && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 112978697Sdwmalone xpt_schedule(periph, priority); 113078697Sdwmalone} 113178697Sdwmalone 113255679Sshinstatic void 113355679Sshincdrunchangerqueue(void *arg) 113455679Sshin{ 11351541Srgrimes struct cd_softc *softc; 113612881Sbde struct cdchanger *changer; 11371541Srgrimes int called_from_timeout; 11381541Srgrimes 113912881Sbde changer = (struct cdchanger *)arg; 11401541Srgrimes 114172959Sjlemon /* 114272959Sjlemon * If we have NOT been called from cdstrategy() or cddone(), and 114373109Sjlemon * instead from a timeout routine, go ahead and clear the 114473109Sjlemon * timeout flag. 114573109Sjlemon */ 114698211Shsu if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 1147145360Sandre changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1148145360Sandre called_from_timeout = 1; 1149145360Sandre } else 1150148156Srwatson called_from_timeout = 0; 11511541Srgrimes 115273109Sjlemon /* Always clear the manual call flag */ 115373109Sjlemon changer->flags &= ~CHANGER_MANUAL_CALL; 115473109Sjlemon 115573109Sjlemon /* nothing to do if the queue is empty */ 1156145355Sandre if (changer->devq.entries <= 0) { 1157145355Sandre return; 115874937Sjesper } 115999156Sjesper 116072959Sjlemon /* 1161122922Sandre * If the changer queue is frozen, that means we have an active 1162122922Sandre * device. 1163122922Sandre */ 1164122922Sandre if (changer->devq.qfrozen_cnt[0] > 0) { 1165122922Sandre 1166122922Sandre /* 1167145355Sandre * We always need to reset the frozen count and clear the 1168145355Sandre * active flag. 1169145355Sandre */ 1170145355Sandre changer->devq.qfrozen_cnt[0]--; 1171145355Sandre changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1172122922Sandre changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1173122922Sandre 1174122922Sandre if (changer->cur_device->outstanding_cmds > 0) { 1175122922Sandre changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 1176122922Sandre changer->cur_device->bufs_left = 1177127870Srwatson changer->cur_device->outstanding_cmds; 1178119995Sru if (called_from_timeout) { 11791541Srgrimes callout_reset(&changer->long_handle, 1180127870Srwatson changer_max_busy_seconds * hz, 1181145360Sandre cdrunchangerqueue, changer); 1182145360Sandre changer->flags |= CHANGER_TIMEOUT_SCHED; 1183133874Srwatson } 1184105586Sphk return; 118598596Shsu } 118673109Sjlemon 118773109Sjlemon /* 118898102Shsu * Check to see whether the current device has any I/O left 118998102Shsu * to do. If so, requeue it at the end of the queue. If 119098102Shsu * not, there is no need to requeue it. 1191145360Sandre */ 119298102Shsu if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 1193145360Sandre 1194145360Sandre changer->cur_device->pinfo.generation = 1195145360Sandre ++changer->devq.generation; 1196145360Sandre camq_insert(&changer->devq, 1197145360Sandre (cam_pinfo *)changer->cur_device); 1198145360Sandre } 1199145360Sandre } 1200145360Sandre 1201145360Sandre softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 1202145360Sandre 1203145360Sandre changer->cur_device = softc; 1204145360Sandre 1205145360Sandre changer->devq.qfrozen_cnt[0]++; 1206145360Sandre softc->flags |= CD_FLAG_ACTIVE; 1207145869Sandre 1208145869Sandre /* Just in case this device is waiting */ 1209145869Sandre wakeup(&softc->changer); 1210149929Sandre xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL); 1211149929Sandre 1212145869Sandre /* 1213145360Sandre * Get rid of any pending timeouts, and set a flag to schedule new 1214149929Sandre * ones so this device gets its full time quantum. 1215145869Sandre */ 1216145869Sandre if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1217145869Sandre callout_stop(&changer->long_handle); 1218145869Sandre changer->flags &= ~CHANGER_TIMEOUT_SCHED; 1219145869Sandre } 1220145869Sandre 1221145869Sandre if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1222145869Sandre callout_stop(&changer->short_handle); 1223145869Sandre changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 1224145869Sandre } 1225145869Sandre 1226145869Sandre /* 1227145869Sandre * We need to schedule timeouts, but we only do this after the 1228145869Sandre * first transaction has completed. This eliminates the changer 1229145360Sandre * switch time. 1230145360Sandre */ 1231145360Sandre changer->flags |= CHANGER_NEED_TIMEOUT; 123298211Shsu} 1233145360Sandre 123498102Shsustatic void 1235127870Srwatsoncdchangerschedule(struct cd_softc *softc) 123698211Shsu{ 123786764Sjlemon struct cdchanger *changer; 123886764Sjlemon 123986764Sjlemon changer = softc->changer; 124086764Sjlemon 124186764Sjlemon /* 124286764Sjlemon * If this is a changer, and this is the current device, 124386764Sjlemon * and this device has at least the minimum time quantum to 124486764Sjlemon * run, see if we can switch it out. 124586764Sjlemon */ 124673109Sjlemon if ((softc->flags & CD_FLAG_ACTIVE) 124798596Shsu && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 12481541Srgrimes && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 124998102Shsu /* 12501541Srgrimes * We try three things here. The first is that we 12511541Srgrimes * check to see whether the schedule on completion 125255679Sshin * flag is set. If it is, we decrement the number 125355679Sshin * of buffers left, and if it's zero, we reschedule. 125455679Sshin * Next, we check to see whether the pending buffer 125555679Sshin * queue is empty and whether there are no 125655679Sshin * outstanding transactions. If so, we reschedule. 125755679Sshin * Next, we see if the pending buffer queue is empty. 125855679Sshin * If it is, we set the number of buffers left to 125955679Sshin * the current active buffer count and set the 126098211Shsu * schedule on complete flag. 126155679Sshin */ 126255679Sshin if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 126378064Sume if (--softc->bufs_left == 0) { 126478064Sume softc->changer->flags |= 126555679Sshin CHANGER_MANUAL_CALL; 126678064Sume softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 126778064Sume cdrunchangerqueue(softc->changer); 126878064Sume } 126978064Sume } else if ((bioq_first(&softc->bio_queue) == NULL) 127055679Sshin && (softc->outstanding_cmds == 0)) { 127155679Sshin softc->changer->flags |= CHANGER_MANUAL_CALL; 127255679Sshin cdrunchangerqueue(softc->changer); 127355679Sshin } 127455679Sshin } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 1275145355Sandre && (softc->flags & CD_FLAG_ACTIVE)) { 127655679Sshin 127755679Sshin /* 1278119995Sru * Now that the first transaction to this 127955679Sshin * particular device has completed, we can go ahead 1280145355Sandre * and schedule our timeouts. 1281145355Sandre */ 1282145355Sandre if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 128355679Sshin callout_reset(&changer->long_handle, 128455679Sshin changer_max_busy_seconds * hz, 128555679Sshin cdrunchangerqueue, changer); 128678064Sume changer->flags |= CHANGER_TIMEOUT_SCHED; 128755679Sshin } else 128855679Sshin printf("cdchangerschedule: already have a long" 128955679Sshin " timeout!\n"); 129078064Sume 129155679Sshin if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 129255679Sshin callout_reset(&changer->short_handle, 129355679Sshin changer_min_busy_seconds * hz, 129467456Sitojun cdshorttimeout, changer); 129578064Sume changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 129655679Sshin } else 129755679Sshin printf("cdchangerschedule: already have a short " 1298127870Srwatson "timeout!\n"); 129986764Sjlemon 130055679Sshin /* 130155679Sshin * We just scheduled timeouts, no need to schedule 130255679Sshin * more. 130355679Sshin */ 130455679Sshin changer->flags &= ~CHANGER_NEED_TIMEOUT; 130567456Sitojun 130678064Sume } 130767456Sitojun} 130867456Sitojun 130978064Sumestatic int 131078064Sumecdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 131178064Sume u_int32_t cam_flags, 1312133192Srwatson u_int32_t sense_flags), 131378064Sume u_int32_t cam_flags, u_int32_t sense_flags) 1314125776Sume{ 131586764Sjlemon struct cd_softc *softc; 131686764Sjlemon struct cam_periph *periph; 131786764Sjlemon int error; 131886764Sjlemon 131986764Sjlemon periph = xpt_path_periph(ccb->ccb_h.path); 132086764Sjlemon softc = (struct cd_softc *)periph->softc; 1321133591Sdwmalone 132286764Sjlemon error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1323133591Sdwmalone softc->disk->d_devstat); 132455679Sshin 1325133192Srwatson if (softc->flags & CD_FLAG_CHANGER) 1326125776Sume cdchangerschedule(softc); 132755679Sshin 132855679Sshin return(error); 132955679Sshin} 133080428Speter 133182122Ssilbystatic union ccb * 133282122Ssilbycdgetccb(struct cam_periph *periph, u_int32_t priority) 133382122Ssilby{ 133482122Ssilby struct cd_softc *softc; 133582122Ssilby 133682122Ssilby softc = (struct cd_softc *)periph->softc; 133782122Ssilby 133894390Ssilby if (softc->flags & CD_FLAG_CHANGER) { 133994390Ssilby /* 134082122Ssilby * This should work the first time this device is woken up, 134182122Ssilby * but just in case it doesn't, we use a while loop. 134282122Ssilby */ 134382122Ssilby while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 134482122Ssilby /* 1345133874Srwatson * If this changer isn't already queued, queue it up. 134682122Ssilby */ 134782122Ssilby if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 134882122Ssilby softc->pinfo.priority = CAM_PRIORITY_NORMAL; 134982122Ssilby softc->pinfo.generation = 135082122Ssilby ++softc->changer->devq.generation; 135182122Ssilby camq_insert(&softc->changer->devq, 135282122Ssilby (cam_pinfo *)softc); 135382122Ssilby } 1354128452Ssilby if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 1355128452Ssilby && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 1356128452Ssilby && ((softc->changer->flags 1357128452Ssilby & CHANGER_SHORT_TMOUT_SCHED)==0)) { 1358128452Ssilby softc->changer->flags |= CHANGER_MANUAL_CALL; 1359128452Ssilby cdrunchangerqueue(softc->changer); 1360128452Ssilby } else 1361128452Ssilby msleep(&softc->changer, periph->sim->mtx, 1362128452Ssilby PRIBIO, "cgticb", 0); 1363128452Ssilby } 1364128452Ssilby } 1365128452Ssilby return(cam_periph_getccb(periph, priority)); 136682122Ssilby} 136782122Ssilby 136882122Ssilby 136982122Ssilby/* 1370138018Srwatson * Actually translate the requested transfer into one the physical driver 1371138018Srwatson * can understand. The transfer is described by a buf and will include 1372138018Srwatson * only one physical transfer. 137382122Ssilby */ 137479413Ssilbystatic void 137582122Ssilbycdstrategy(struct bio *bp) 1376128452Ssilby{ 1377128452Ssilby struct cam_periph *periph; 137879413Ssilby struct cd_softc *softc; 1379138018Srwatson 1380138018Srwatson periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1381138018Srwatson if (periph == NULL) { 1382138018Srwatson biofinish(bp, NULL, ENXIO); 138375619Skris return; 138475619Skris } 138582122Ssilby 138682122Ssilby cam_periph_lock(periph); 138775619Skris CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 138882122Ssilby 138982122Ssilby softc = (struct cd_softc *)periph->softc; 139075619Skris 1391138018Srwatson /* 1392138410Srwatson * If the device has been made invalid, error out 1393138018Srwatson */ 139482122Ssilby if ((softc->flags & CD_FLAG_INVALID)) { 139594390Ssilby cam_periph_unlock(periph); 139682122Ssilby biofinish(bp, NULL, ENXIO); 139782122Ssilby return; 139882122Ssilby } 139982122Ssilby 140082122Ssilby /* 1401133874Srwatson * If we don't have valid media, look for it before trying to 140282122Ssilby * schedule the I/O. 140382122Ssilby */ 140482122Ssilby if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 140582122Ssilby int error; 140682122Ssilby 140782122Ssilby error = cdcheckmedia(periph); 140882122Ssilby if (error != 0) { 140982122Ssilby cam_periph_unlock(periph); 141082122Ssilby biofinish(bp, NULL, error); 141182122Ssilby return; 141282122Ssilby } 141382122Ssilby } 141482122Ssilby 141582122Ssilby /* 141682122Ssilby * Place it in the queue of disk activities for this disk 141782122Ssilby */ 141882122Ssilby bioq_disksort(&softc->bio_queue, bp); 141982122Ssilby 142082122Ssilby /* 142182122Ssilby * Schedule ourselves for performing the work. We do things 142282122Ssilby * differently for changers. 1423128452Ssilby */ 1424128452Ssilby if ((softc->flags & CD_FLAG_CHANGER) == 0) 1425128452Ssilby xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1426139222Srwatson else 142775619Skris cdschedule(periph, CAM_PRIORITY_NORMAL); 142875619Skris 14291541Srgrimes cam_periph_unlock(periph); 1430128452Ssilby return; 1431128452Ssilby} 1432128452Ssilby 1433128452Ssilbystatic void 1434128452Ssilbycdstart(struct cam_periph *periph, union ccb *start_ccb) 1435128452Ssilby{ 1436128452Ssilby struct cd_softc *softc; 1437128452Ssilby struct bio *bp; 1438128452Ssilby struct ccb_scsiio *csio; 1439133874Srwatson struct scsi_read_capacity_data *rcap; 1440138018Srwatson 1441141078Srwatson softc = (struct cd_softc *)periph->softc; 1442128452Ssilby 1443128452Ssilby CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 1444128452Ssilby 1445128452Ssilby switch (softc->state) { 1446128452Ssilby case CD_STATE_NORMAL: 1447141072Srwatson { 1448138018Srwatson bp = bioq_first(&softc->bio_queue); 1449128452Ssilby if (periph->immediate_priority <= periph->pinfo.priority) { 1450128452Ssilby start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 1451128452Ssilby 145272959Sjlemon SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 145372959Sjlemon periph_links.sle); 145472959Sjlemon periph->immediate_priority = CAM_PRIORITY_NONE; 145570103Sphk wakeup(&periph->ccb_list); 145698211Shsu } else if (bp == NULL) { 145770103Sphk xpt_release_ccb(start_ccb); 145870103Sphk } else { 145970103Sphk bioq_remove(&softc->bio_queue, bp); 146070103Sphk 146170103Sphk scsi_read_write(&start_ccb->csio, 146270103Sphk /*retries*/ cd_retry_count, 1463146864Srwatson /* cbfcnp */ cddone, 1464138019Srwatson MSG_SIMPLE_Q_TAG, 1465146864Srwatson /* read */bp->bio_cmd == BIO_READ, 1466127871Srwatson /* byte2 */ 0, 1467157376Srwatson /* minimum_cmd_size */ 10, 1468157376Srwatson /* lba */ bp->bio_offset / 1469157376Srwatson softc->params.blksize, 1470157376Srwatson bp->bio_bcount / softc->params.blksize, 1471157376Srwatson /* data_ptr */ bp->bio_data, 147298211Shsu /* dxfer_len */ bp->bio_bcount, 1473139222Srwatson /* sense_len */ SSD_FULL_SIZE, 147472638Sphk /* timeout */ 30000); 147572638Sphk start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 147672638Sphk 147710881Swollman 147810881Swollman LIST_INSERT_HEAD(&softc->pending_ccbs, 147910881Swollman &start_ccb->ccb_h, periph_links.le); 148010930Swollman softc->outstanding_cmds++; 148110881Swollman 148298211Shsu /* We expect a unit attention from this device */ 148310881Swollman if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 148410881Swollman start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 148510881Swollman softc->flags &= ~CD_FLAG_RETRY_UA; 148610881Swollman } 148710881Swollman 148810930Swollman start_ccb->ccb_h.ccb_bp = bp; 1489122922Sandre bp = bioq_first(&softc->bio_queue); 1490122922Sandre 149110930Swollman xpt_action(start_ccb); 149255679Sshin } 1493134041Sandre if (bp != NULL) { 149455679Sshin /* Have more work to do, so ensure we stay scheduled */ 149510881Swollman xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1496138019Srwatson } 1497127870Srwatson break; 1498134041Sandre } 1499134041Sandre case CD_STATE_PROBE: 1500134041Sandre { 1501122922Sandre 1502122922Sandre rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 150355679Sshin M_SCSICD, M_NOWAIT | M_ZERO); 1504122922Sandre if (rcap == NULL) { 1505122922Sandre xpt_print(periph->path, 1506122922Sandre "cdstart: Couldn't malloc read_capacity data\n"); 1507122922Sandre /* cd_free_periph??? */ 1508122922Sandre break; 150955679Sshin } 1510122922Sandre csio = &start_ccb->csio; 1511122922Sandre scsi_read_capacity(csio, 151255679Sshin /*retries*/ cd_retry_count, 151355679Sshin cddone, 151455679Sshin MSG_SIMPLE_Q_TAG, 151555679Sshin rcap, 151655679Sshin SSD_FULL_SIZE, 1517139222Srwatson /*timeout*/20000); 151810930Swollman start_ccb->ccb_h.ccb_bp = NULL; 1519122922Sandre start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 152055679Sshin xpt_action(start_ccb); 152155679Sshin break; 152255679Sshin } 152355679Sshin } 152455679Sshin} 152555679Sshin 152655679Sshinstatic void 152755679Sshincddone(struct cam_periph *periph, union ccb *done_ccb) 152855679Sshin{ 152955679Sshin struct cd_softc *softc; 153012939Swollman struct ccb_scsiio *csio; 153112939Swollman 153212939Swollman CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 153312939Swollman 153412939Swollman softc = (struct cd_softc *)periph->softc; 153512939Swollman csio = &done_ccb->csio; 153612939Swollman 153712939Swollman switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 153812939Swollman case CD_CCB_BUFFER_IO: 153912939Swollman { 154012939Swollman struct bio *bp; 154112939Swollman int error; 154212939Swollman 154312939Swollman bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 154412939Swollman error = 0; 154512939Swollman 154612939Swollman if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 154711415Swollman int sf; 1548139222Srwatson 154910930Swollman if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 155010930Swollman sf = SF_RETRY_UA; 155110930Swollman else 155210930Swollman sf = 0; 155310930Swollman 155410930Swollman error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 155510930Swollman if (error == ERESTART) { 155610930Swollman /* 155710930Swollman * A retry was scheuled, so 155810930Swollman * just return. 155910930Swollman */ 156010881Swollman return; 156110930Swollman } 156210930Swollman } 156310930Swollman 156410930Swollman if (error != 0) { 156510930Swollman xpt_print(periph->path, 156611450Swollman "cddone: got error %#x back\n", error); 156750673Sjlemon bioq_flush(&softc->bio_queue, NULL, EIO); 156811450Swollman bp->bio_resid = bp->bio_bcount; 156911450Swollman bp->bio_error = error; 157010930Swollman bp->bio_flags |= BIO_ERROR; 1571139222Srwatson if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 157210881Swollman cam_release_devq(done_ccb->ccb_h.path, 157310881Swollman /*relsim_flags*/0, 157410881Swollman /*reduction*/0, 15756283Swollman /*timeout*/0, 1576108265Shsu /*getcount_only*/0); 15776283Swollman 15786283Swollman } else { 15796283Swollman bp->bio_resid = csio->resid; 1580133874Srwatson bp->bio_error = 0; 1581122922Sandre if (bp->bio_resid != 0) { 158286764Sjlemon /* 15836283Swollman * Short transfer ??? 1584122922Sandre * XXX: not sure this is correct for partial 1585122922Sandre * transfers at EOM 1586122922Sandre */ 1587122922Sandre bp->bio_flags |= BIO_ERROR; 15886283Swollman } 1589122922Sandre } 1590122922Sandre 1591122996Sandre LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1592122922Sandre softc->outstanding_cmds--; 1593122922Sandre 1594122922Sandre if (softc->flags & CD_FLAG_CHANGER) 1595122922Sandre cdchangerschedule(softc); 1596122922Sandre 1597122922Sandre biofinish(bp, NULL, 0); 15986283Swollman break; 1599122922Sandre } 1600122922Sandre case CD_CCB_PROBE: 1601122922Sandre { 1602122922Sandre struct scsi_read_capacity_data *rdcap; 1603122922Sandre char announce_buf[120]; /* 1604122922Sandre * Currently (9/30/97) the 1605122922Sandre * longest possible announce 1606122922Sandre * buffer is 108 bytes, for the 1607122922Sandre * first error case below. 16086283Swollman * That is 39 bytes for the 16096283Swollman * basic string, 16 bytes for the 161055679Sshin * biggest sense key (hardware 1611122922Sandre * error), 52 bytes for the 1612122922Sandre * text of the largest sense 161386764Sjlemon * qualifier valid for a CDROM, 161455679Sshin * (0x72, 0x03 or 0x04, 1615122922Sandre * 0x03), and one byte for the 1616122922Sandre * null terminating character. 1617122922Sandre * To allow for longer strings, 161855679Sshin * the announce buffer is 120 1619122922Sandre * bytes. 1620122922Sandre */ 1621122996Sandre struct cd_params *cdp; 1622122922Sandre 1623122922Sandre cdp = &softc->params; 1624122922Sandre 1625122922Sandre rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1626122922Sandre 162755679Sshin cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1628122922Sandre cdp->blksize = scsi_4btoul (rdcap->length); 1629122922Sandre 1630122922Sandre if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1631122922Sandre 1632122922Sandre snprintf(announce_buf, sizeof(announce_buf), 1633122922Sandre "cd present [%lu x %lu byte records]", 1634122922Sandre cdp->disksize, (u_long)cdp->blksize); 1635122922Sandre 1636122922Sandre } else { 1637122922Sandre int error; 1638122922Sandre /* 163955679Sshin * Retry any UNIT ATTENTION type errors. They 164055679Sshin * are expected at boot. 164155679Sshin */ 164255679Sshin error = cderror(done_ccb, CAM_RETRY_SELTO, 164355679Sshin SF_RETRY_UA | SF_NO_PRINT); 164455679Sshin if (error == ERESTART) { 164555679Sshin /* 164655679Sshin * A retry was scheuled, so 164755679Sshin * just return. 164855679Sshin */ 164955679Sshin return; 165055679Sshin } else if (error != 0) { 165155679Sshin 165255679Sshin struct scsi_sense_data *sense; 165355679Sshin int asc, ascq; 1654111145Sjlemon int sense_key, error_code; 165555679Sshin int have_sense; 165655679Sshin cam_status status; 165778642Ssilby struct ccb_getdev cgd; 1658139222Srwatson 1659111119Simp /* Don't wedge this device's queue */ 166055679Sshin if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1661139222Srwatson cam_release_devq(done_ccb->ccb_h.path, 166255679Sshin /*relsim_flags*/0, 166355679Sshin /*reduction*/0, 166455679Sshin /*timeout*/0, 166555679Sshin /*getcount_only*/0); 166655679Sshin 166755679Sshin status = done_ccb->ccb_h.status; 166855679Sshin 1669111144Sjlemon xpt_setup_ccb(&cgd.ccb_h, 167055679Sshin done_ccb->ccb_h.path, 167155679Sshin CAM_PRIORITY_NORMAL); 167255679Sshin cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1673133874Srwatson xpt_action((union ccb *)&cgd); 1674133874Srwatson 1675133874Srwatson if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1676133874Srwatson || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1677133874Srwatson || ((status & CAM_AUTOSNS_VALID) == 0)) 1678133874Srwatson have_sense = FALSE; 1679133874Srwatson else 168055679Sshin have_sense = TRUE; 168155679Sshin 1682139222Srwatson if (have_sense) { 168355679Sshin sense = &csio->sense_data; 168455679Sshin scsi_extract_sense(sense, &error_code, 168555679Sshin &sense_key, 16866283Swollman &asc, &ascq); 1687111145Sjlemon } 1688138410Srwatson /* 1689111145Sjlemon * Attach to anything that claims to be a 1690111145Sjlemon * CDROM or WORM device, as long as it 1691111145Sjlemon * doesn't return a "Logical unit not 1692111145Sjlemon * supported" (0x25) error. 1693111145Sjlemon */ 1694111145Sjlemon if ((have_sense) && (asc != 0x25) 1695111145Sjlemon && (error_code == SSD_CURRENT_ERROR)) { 1696111145Sjlemon const char *sense_key_desc; 1697111145Sjlemon const char *asc_desc; 1698111145Sjlemon 1699111145Sjlemon scsi_sense_desc(sense_key, asc, ascq, 1700138025Srwatson &cgd.inq_data, 1701138020Srwatson &sense_key_desc, 1702138020Srwatson &asc_desc); 1703112009Sjlemon snprintf(announce_buf, 1704112009Sjlemon sizeof(announce_buf), 1705112009Sjlemon "Attempt to query device " 1706112009Sjlemon "size failed: %s, %s", 1707157376Srwatson sense_key_desc, 1708157376Srwatson asc_desc); 1709157376Srwatson } else if ((have_sense == 0) 1710112009Sjlemon && ((status & CAM_STATUS_MASK) == 1711112009Sjlemon CAM_SCSI_STATUS_ERROR) 1712112009Sjlemon && (csio->scsi_status == 1713111145Sjlemon SCSI_STATUS_BUSY)) { 1714111145Sjlemon snprintf(announce_buf, 1715111145Sjlemon sizeof(announce_buf), 1716111145Sjlemon "Attempt to query device " 1717111145Sjlemon "size failed: SCSI Status: %s", 1718111145Sjlemon scsi_status_string(csio)); 1719111145Sjlemon } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1720111145Sjlemon /* 1721111145Sjlemon * We only print out an error for 1722111145Sjlemon * CDROM type devices. For WORM 1723111145Sjlemon * devices, we don't print out an 1724133874Srwatson * error since a few WORM devices 1725133874Srwatson * don't support CDROM commands. 1726111145Sjlemon * If we have sense information, go 1727111145Sjlemon * ahead and print it out. 1728111145Sjlemon * Otherwise, just say that we 1729111145Sjlemon * couldn't attach. 1730111145Sjlemon */ 1731111145Sjlemon 1732121850Ssilby /* 1733121884Ssilby * Just print out the error, not 1734111145Sjlemon * the full probe message, when we 1735112009Sjlemon * don't attach. 1736111145Sjlemon */ 1737111145Sjlemon if (have_sense) 1738111145Sjlemon scsi_sense_print( 1739111145Sjlemon &done_ccb->csio); 1740111145Sjlemon else { 1741111145Sjlemon xpt_print(periph->path, 1742137139Sandre "got CAM status %#x\n", 1743137139Sandre done_ccb->ccb_h.status); 1744157376Srwatson } 1745157376Srwatson xpt_print(periph->path, "fatal error, " 1746157376Srwatson "failed to attach to device\n"); 1747157376Srwatson /* 1748157376Srwatson * Invalidate this peripheral. 1749157376Srwatson */ 1750111145Sjlemon cam_periph_invalidate(periph); 1751111145Sjlemon 1752130387Srwatson announce_buf[0] = '\0'; 1753111145Sjlemon } else { 1754111145Sjlemon 1755157376Srwatson /* 1756114794Srwatson * Invalidate this peripheral. 1757126351Srwatson */ 1758111145Sjlemon cam_periph_invalidate(periph); 1759111145Sjlemon announce_buf[0] = '\0'; 1760112009Sjlemon } 1761157376Srwatson } 1762157376Srwatson } 1763157376Srwatson free(rdcap, M_SCSICD); 1764157376Srwatson if (announce_buf[0] != '\0') { 1765157376Srwatson xpt_announce_periph(periph, announce_buf); 1766157376Srwatson if (softc->flags & CD_FLAG_CHANGER) 1767157376Srwatson cdchangerschedule(softc); 1768157376Srwatson /* 1769157376Srwatson * Create our sysctl variables, now that we know 1770157376Srwatson * we have successfully attached. 1771157376Srwatson */ 1772157376Srwatson taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 1773157376Srwatson } 1774157376Srwatson softc->state = CD_STATE_NORMAL; 1775157376Srwatson /* 1776157376Srwatson * Since our peripheral may be invalidated by an error 1777157376Srwatson * above or an external event, we must release our CCB 1778157376Srwatson * before releasing the probe lock on the peripheral. 1779157376Srwatson * The peripheral will only go away once the last lock 1780157376Srwatson * is removed, and we need it around for the CCB release 1781157376Srwatson * operation. 1782157376Srwatson */ 1783111145Sjlemon xpt_release_ccb(done_ccb); 1784111145Sjlemon cam_periph_unhold(periph); 1785121850Ssilby return; 1786121884Ssilby } 1787121884Ssilby case CD_CCB_WAITING: 1788121884Ssilby { 1789121884Ssilby /* Caller will release the CCB */ 1790121884Ssilby CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1791121884Ssilby ("trying to wakeup ccbwait\n")); 1792121884Ssilby 1793121884Ssilby wakeup(&done_ccb->ccb_h.cbfcnp); 1794121884Ssilby return; 1795121884Ssilby } 1796121850Ssilby default: 1797121850Ssilby break; 1798121850Ssilby } 1799138020Srwatson xpt_release_ccb(done_ccb); 1800138020Srwatson} 1801139221Srwatson 1802138020Srwatsonstatic union cd_pages * 1803121850Ssilbycdgetpage(struct cd_mode_params *mode_params) 1804121850Ssilby{ 1805121850Ssilby union cd_pages *page; 1806121850Ssilby 1807121884Ssilby if (mode_params->cdb_size == 10) 1808121884Ssilby page = (union cd_pages *)find_mode_page_10( 1809121850Ssilby (struct scsi_mode_header_10 *)mode_params->mode_buf); 1810121884Ssilby else 1811121884Ssilby page = (union cd_pages *)find_mode_page_6( 1812133874Srwatson (struct scsi_mode_header_6 *)mode_params->mode_buf); 1813121884Ssilby 1814139222Srwatson return (page); 1815121850Ssilby} 1816139222Srwatson 1817121850Ssilbystatic int 1818121850Ssilbycdgetpagesize(int page_num) 1819157376Srwatson{ 1820112009Sjlemon int i; 1821111145Sjlemon 1822157376Srwatson for (i = 0; i < (sizeof(cd_page_size_table)/ 1823111145Sjlemon sizeof(cd_page_size_table[0])); i++) { 1824111145Sjlemon if (cd_page_size_table[i].page == page_num) 1825157376Srwatson return (cd_page_size_table[i].page_size); 1826157386Srwatson } 1827157376Srwatson 1828157386Srwatson return (-1); 1829157386Srwatson} 1830157386Srwatson 1831157386Srwatsonstatic int 1832157386Srwatsoncdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 1833157386Srwatson{ 1834157376Srwatson 1835111145Sjlemon struct cam_periph *periph; 1836157376Srwatson struct cd_softc *softc; 1837157376Srwatson int nocopyout, error = 0; 1838138025Srwatson 1839138020Srwatson periph = (struct cam_periph *)dp->d_drv1; 1840138020Srwatson if (periph == NULL) 1841111145Sjlemon return(ENXIO); 1842112009Sjlemon 1843111145Sjlemon cam_periph_lock(periph); 1844157376Srwatson CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 1845157376Srwatson 1846157376Srwatson softc = (struct cd_softc *)periph->softc; 1847157386Srwatson 1848157386Srwatson CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1849157386Srwatson ("trying to do ioctl %#lx\n", cmd)); 1850157386Srwatson 1851157386Srwatson if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1852157386Srwatson cam_periph_unlock(periph); 1853157386Srwatson cam_periph_release(periph); 1854157386Srwatson return (error); 1855111145Sjlemon } 1856157386Srwatson 1857157386Srwatson /* 1858157386Srwatson * If we don't have media loaded, check for it. If still don't 1859157386Srwatson * have media loaded, we can only do a load or eject. 1860157386Srwatson * 1861157386Srwatson * We only care whether media is loaded if this is a cd-specific ioctl 1862157386Srwatson * (thus the IOCGROUP check below). Note that this will break if 1863157386Srwatson * anyone adds any ioctls into the switch statement below that don't 1864157386Srwatson * have their ioctl group set to 'c'. 1865157386Srwatson */ 1866157386Srwatson if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1867157386Srwatson && ((cmd != CDIOCCLOSE) 1868157386Srwatson && (cmd != CDIOCEJECT)) 1869157386Srwatson && (IOCGROUP(cmd) == 'c')) { 1870157376Srwatson error = cdcheckmedia(periph); 1871157386Srwatson if (error != 0) { 1872157386Srwatson cam_periph_unhold(periph); 1873157386Srwatson cam_periph_unlock(periph); 1874157386Srwatson return (error); 1875157386Srwatson } 1876157386Srwatson } 1877157376Srwatson /* 1878157386Srwatson * Drop the lock here so later mallocs can use WAITOK. The periph 1879157376Srwatson * is essentially locked still with the cam_periph_hold call above. 1880157376Srwatson */ 1881157376Srwatson cam_periph_unlock(periph); 1882157376Srwatson 1883157376Srwatson nocopyout = 0; 1884157376Srwatson switch (cmd) { 1885157386Srwatson 1886111145Sjlemon case CDIOCPLAYTRACKS: 1887126002Spjd { 1888126002Spjd struct ioc_play_track *args 1889112009Sjlemon = (struct ioc_play_track *) addr; 1890157376Srwatson struct cd_mode_params params; 1891112009Sjlemon union cd_pages *page; 1892111145Sjlemon 1893111145Sjlemon params.alloc_len = sizeof(union cd_mode_data_6_10); 1894111145Sjlemon params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1895126351Srwatson M_WAITOK | M_ZERO); 1896111145Sjlemon 1897111145Sjlemon cam_periph_lock(periph); 1898111145Sjlemon CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1899111145Sjlemon ("trying to do CDIOCPLAYTRACKS\n")); 1900111145Sjlemon 1901111145Sjlemon error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1902111145Sjlemon if (error) { 1903111145Sjlemon free(params.mode_buf, M_SCSICD); 1904111145Sjlemon cam_periph_unlock(periph); 1905111145Sjlemon break; 1906111145Sjlemon } 1907111145Sjlemon page = cdgetpage(¶ms); 1908111145Sjlemon 1909138020Srwatson page->audio.flags &= ~CD_PA_SOTC; 1910138020Srwatson page->audio.flags |= CD_PA_IMMED; 1911151967Sandre error = cdsetmode(periph, ¶ms); 1912111145Sjlemon free(params.mode_buf, M_SCSICD); 1913111145Sjlemon if (error) { 1914111145Sjlemon cam_periph_unlock(periph); 1915111145Sjlemon break; 1916114794Srwatson } 1917123607Srwatson 1918114794Srwatson /* 1919114794Srwatson * This was originally implemented with the PLAY 1920111153Sjlemon * AUDIO TRACK INDEX command, but that command was 1921111145Sjlemon * deprecated after SCSI-2. Most (all?) SCSI CDROM 1922111145Sjlemon * drives support it but ATAPI and ATAPI-derivative 1923111145Sjlemon * drives don't seem to support it. So we keep a 1924111145Sjlemon * cache of the table of contents and translate 1925111145Sjlemon * track numbers to MSF format. 1926111153Sjlemon */ 1927111153Sjlemon if (softc->flags & CD_FLAG_VALID_TOC) { 1928111153Sjlemon union msf_lba *sentry, *eentry; 1929111145Sjlemon int st, et; 1930111145Sjlemon 1931111145Sjlemon if (args->end_track < 1932111145Sjlemon softc->toc.header.ending_track + 1) 1933111145Sjlemon args->end_track++; 1934111145Sjlemon if (args->end_track > 1935133874Srwatson softc->toc.header.ending_track + 1) 1936133874Srwatson args->end_track = 1937111145Sjlemon softc->toc.header.ending_track + 1; 1938111145Sjlemon st = args->start_track - 1939133874Srwatson softc->toc.header.starting_track; 1940111145Sjlemon et = args->end_track - 1941111145Sjlemon softc->toc.header.starting_track; 1942111145Sjlemon if ((st < 0) 1943133874Srwatson || (et < 0) 1944133874Srwatson || (st > (softc->toc.header.ending_track - 1945133874Srwatson softc->toc.header.starting_track))) { 1946133874Srwatson error = EINVAL; 1947133874Srwatson break; 1948133874Srwatson } 1949111145Sjlemon sentry = &softc->toc.entries[st].addr; 1950111145Sjlemon eentry = &softc->toc.entries[et].addr; 1951111145Sjlemon error = cdplaymsf(periph, 1952111145Sjlemon sentry->msf.minute, 1953111145Sjlemon sentry->msf.second, 1954111145Sjlemon sentry->msf.frame, 1955111145Sjlemon eentry->msf.minute, 1956111145Sjlemon eentry->msf.second, 1957111145Sjlemon eentry->msf.frame); 1958111145Sjlemon } else { 1959111145Sjlemon /* 1960111145Sjlemon * If we don't have a valid TOC, try the 1961111145Sjlemon * play track index command. It is part of 1962111145Sjlemon * the SCSI-2 spec, but was removed in the 1963111153Sjlemon * MMC specs. ATAPI and ATAPI-derived 1964111145Sjlemon * drives don't support it. 1965111145Sjlemon */ 1966111145Sjlemon if (softc->quirks & CD_Q_BCD_TRACKS) { 1967122922Sandre args->start_track = 1968122922Sandre bin2bcd(args->start_track); 1969111145Sjlemon args->end_track = 1970111153Sjlemon bin2bcd(args->end_track); 1971111153Sjlemon } 1972111153Sjlemon error = cdplaytracks(periph, 1973111145Sjlemon args->start_track, 1974133874Srwatson args->start_index, 1975111145Sjlemon args->end_track, 1976111145Sjlemon args->end_index); 1977111145Sjlemon } 1978124248Sandre cam_periph_unlock(periph); 1979124248Sandre } 1980122922Sandre break; 1981134793Sjmg case CDIOCPLAYMSF: 1982134793Sjmg { 1983111145Sjlemon struct ioc_play_msf *args 1984111145Sjlemon = (struct ioc_play_msf *) addr; 1985111145Sjlemon struct cd_mode_params params; 1986111145Sjlemon union cd_pages *page; 1987111145Sjlemon 1988111145Sjlemon params.alloc_len = sizeof(union cd_mode_data_6_10); 1989111145Sjlemon params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1990111145Sjlemon M_WAITOK | M_ZERO); 1991111145Sjlemon 1992111145Sjlemon cam_periph_lock(periph); 1993102017Sdillon CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1994102017Sdillon ("trying to do CDIOCPLAYMSF\n")); 1995102017Sdillon 1996102017Sdillon error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1997102017Sdillon if (error) { 1998102017Sdillon free(params.mode_buf, M_SCSICD); 1999102017Sdillon cam_periph_unlock(periph); 2000102017Sdillon break; 2001102017Sdillon } 2002102017Sdillon page = cdgetpage(¶ms); 2003102017Sdillon 2004102017Sdillon page->audio.flags &= ~CD_PA_SOTC; 2005133874Srwatson page->audio.flags |= CD_PA_IMMED; 2006102017Sdillon error = cdsetmode(periph, ¶ms); 2007102017Sdillon free(params.mode_buf, M_SCSICD); 2008102017Sdillon if (error) { 2009102017Sdillon cam_periph_unlock(periph); 2010102017Sdillon break; 2011133874Srwatson } 2012102017Sdillon error = cdplaymsf(periph, 2013102017Sdillon args->start_m, 2014102017Sdillon args->start_s, 2015102017Sdillon args->start_f, 2016102017Sdillon args->end_m, 2017102017Sdillon args->end_s, 2018102017Sdillon args->end_f); 2019102017Sdillon cam_periph_unlock(periph); 2020102017Sdillon } 2021102017Sdillon break; 2022102017Sdillon case CDIOCPLAYBLOCKS: 2023102017Sdillon { 2024102017Sdillon struct ioc_play_blocks *args 2025102017Sdillon = (struct ioc_play_blocks *) addr; 2026102017Sdillon struct cd_mode_params params; 2027102017Sdillon union cd_pages *page; 2028102017Sdillon 2029102017Sdillon params.alloc_len = sizeof(union cd_mode_data_6_10); 2030102017Sdillon params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2031102017Sdillon M_WAITOK | M_ZERO); 2032102017Sdillon 2033133874Srwatson cam_periph_lock(periph); 2034102017Sdillon CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2035102017Sdillon ("trying to do CDIOCPLAYBLOCKS\n")); 2036102017Sdillon 2037102017Sdillon 2038102017Sdillon error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2039102017Sdillon if (error) { 2040102017Sdillon free(params.mode_buf, M_SCSICD); 2041102017Sdillon cam_periph_unlock(periph); 2042102017Sdillon break; 2043102017Sdillon } 2044102017Sdillon page = cdgetpage(¶ms); 2045102017Sdillon 2046102017Sdillon page->audio.flags &= ~CD_PA_SOTC; 2047102017Sdillon page->audio.flags |= CD_PA_IMMED; 2048138410Srwatson error = cdsetmode(periph, ¶ms); 2049138410Srwatson free(params.mode_buf, M_SCSICD); 2050102017Sdillon if (error) { 2051102017Sdillon cam_periph_unlock(periph); 2052102017Sdillon break; 2053102017Sdillon } 2054155767Sandre error = cdplay(periph, args->blk, args->len); 2055102017Sdillon cam_periph_unlock(periph); 2056102017Sdillon } 2057102017Sdillon break; 2058102017Sdillon case CDIOCREADSUBCHANNEL_SYSSPACE: 2059102017Sdillon nocopyout = 1; 2060102017Sdillon /* Fallthrough */ 2061102017Sdillon case CDIOCREADSUBCHANNEL: 2062102017Sdillon { 2063102017Sdillon struct ioc_read_subchannel *args 2064102017Sdillon = (struct ioc_read_subchannel *) addr; 2065102017Sdillon struct cd_sub_channel_info *data; 2066102017Sdillon u_int32_t len = args->data_len; 2067102368Sdillon 2068102368Sdillon data = malloc(sizeof(struct cd_sub_channel_info), 2069102368Sdillon M_SCSICD, M_WAITOK | M_ZERO); 2070102017Sdillon 2071102017Sdillon cam_periph_lock(periph); 2072102017Sdillon CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2073102017Sdillon ("trying to do CDIOCREADSUBCHANNEL\n")); 2074102017Sdillon 2075133874Srwatson if ((len > sizeof(struct cd_sub_channel_info)) || 2076102017Sdillon (len < sizeof(struct cd_sub_channel_header))) { 2077102017Sdillon printf( 2078102017Sdillon "scsi_cd: cdioctl: " 2079102368Sdillon "cdioreadsubchannel: error, len=%d\n", 2080102017Sdillon len); 2081102017Sdillon error = EINVAL; 2082102017Sdillon free(data, M_SCSICD); 2083102017Sdillon cam_periph_unlock(periph); 2084102017Sdillon break; 2085102017Sdillon } 2086102017Sdillon 2087102017Sdillon if (softc->quirks & CD_Q_BCD_TRACKS) 2088107881Sdillon args->track = bin2bcd(args->track); 2089107881Sdillon 2090107881Sdillon error = cdreadsubchannel(periph, args->address_format, 2091102017Sdillon args->data_format, args->track, data, len); 2092102017Sdillon 2093102017Sdillon if (error) { 2094102017Sdillon free(data, M_SCSICD); 2095133874Srwatson cam_periph_unlock(periph); 2096102017Sdillon break; 2097102017Sdillon } 2098102017Sdillon if (softc->quirks & CD_Q_BCD_TRACKS) 2099102017Sdillon data->what.track_info.track_number = 2100102017Sdillon bcd2bin(data->what.track_info.track_number); 2101102017Sdillon len = min(len, ((data->header.data_len[0] << 8) + 2102102017Sdillon data->header.data_len[1] + 2103102017Sdillon sizeof(struct cd_sub_channel_header))); 2104102017Sdillon cam_periph_unlock(periph); 2105102017Sdillon if (nocopyout == 0) { 2106107881Sdillon if (copyout(data, args->data, len) != 0) { 2107107881Sdillon error = EFAULT; 2108107881Sdillon } 2109107881Sdillon } else { 2110107881Sdillon bcopy(data, args->data, len); 2111107881Sdillon } 2112102017Sdillon free(data, M_SCSICD); 2113102017Sdillon } 2114107881Sdillon break; 2115102368Sdillon 2116102017Sdillon case CDIOREADTOCHEADER: 2117102017Sdillon { 2118102017Sdillon struct ioc_toc_header *th; 2119102017Sdillon 2120102017Sdillon th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2121102017Sdillon M_WAITOK | M_ZERO); 2122102017Sdillon 2123102017Sdillon cam_periph_lock(periph); 2124102017Sdillon CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2125102017Sdillon ("trying to do CDIOREADTOCHEADER\n")); 2126102017Sdillon 2127102017Sdillon error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2128102017Sdillon sizeof (*th), /*sense_flags*/0); 2129102017Sdillon if (error) { 2130102017Sdillon free(th, M_SCSICD); 2131102017Sdillon cam_periph_unlock(periph); 2132102017Sdillon break; 2133102017Sdillon } 2134102017Sdillon if (softc->quirks & CD_Q_BCD_TRACKS) { 2135102017Sdillon /* we are going to have to convert the BCD 2136102017Sdillon * encoding on the cd to what is expected 2137102017Sdillon */ 2138102017Sdillon th->starting_track = 2139125680Sbms bcd2bin(th->starting_track); 2140125680Sbms th->ending_track = bcd2bin(th->ending_track); 2141125783Sbms } 2142125783Sbms th->len = ntohs(th->len); 2143125783Sbms bcopy(th, addr, sizeof(*th)); 2144125783Sbms free(th, M_SCSICD); 2145125783Sbms cam_periph_unlock(periph); 2146125783Sbms } 2147125783Sbms break; 2148125819Sbms case CDIOREADTOCENTRYS: 2149125783Sbms { 2150125783Sbms struct cd_tocdata *data; 2151125783Sbms struct cd_toc_single *lead; 2152125783Sbms struct ioc_read_toc_entry *te = 2153125680Sbms (struct ioc_read_toc_entry *) addr; 2154125680Sbms struct ioc_toc_header *th; 2155125741Sbms u_int32_t len, readlen, idx, num; 2156125741Sbms u_int32_t starting_track = te->starting_track; 2157125741Sbms 2158125741Sbms data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2159125741Sbms lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 2160125741Sbms 2161125741Sbms cam_periph_lock(periph); 2162125741Sbms CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2163125680Sbms ("trying to do CDIOREADTOCENTRYS\n")); 2164125680Sbms 2165125680Sbms if (te->data_len < sizeof(struct cd_toc_entry) 2166125680Sbms || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2167125680Sbms || (te->address_format != CD_MSF_FORMAT 2168125680Sbms && te->address_format != CD_LBA_FORMAT)) { 2169125680Sbms error = EINVAL; 2170125680Sbms printf("scsi_cd: error in readtocentries, " 2171125680Sbms "returning EINVAL\n"); 2172125680Sbms free(data, M_SCSICD); 2173125680Sbms free(lead, M_SCSICD); 2174125680Sbms cam_periph_unlock(periph); 2175125680Sbms break; 2176125680Sbms } 2177125680Sbms 2178125680Sbms th = &data->header; 2179125783Sbms error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2180125741Sbms sizeof (*th), /*sense_flags*/0); 2181125680Sbms if (error) { 2182125680Sbms free(data, M_SCSICD); 2183125680Sbms free(lead, M_SCSICD); 2184125680Sbms cam_periph_unlock(periph); 2185125680Sbms break; 2186125680Sbms } 2187125680Sbms 2188125680Sbms if (softc->quirks & CD_Q_BCD_TRACKS) { 2189125680Sbms /* we are going to have to convert the BCD 2190125680Sbms * encoding on the cd to what is expected 2191125680Sbms */ 2192125741Sbms th->starting_track = 2193125741Sbms bcd2bin(th->starting_track); 2194125741Sbms th->ending_track = bcd2bin(th->ending_track); 2195125741Sbms } 2196125680Sbms 2197125680Sbms if (starting_track == 0) 2198125680Sbms starting_track = th->starting_track; 2199125680Sbms else if (starting_track == LEADOUT) 2200125680Sbms starting_track = th->ending_track + 1; 2201125680Sbms else if (starting_track < th->starting_track || 2202125741Sbms starting_track > th->ending_track + 1) { 2203125741Sbms printf("scsi_cd: error in readtocentries, " 2204125680Sbms "returning EINVAL\n"); 2205125680Sbms free(data, M_SCSICD); 2206125680Sbms free(lead, M_SCSICD); 2207125680Sbms cam_periph_unlock(periph); 2208125680Sbms error = EINVAL; 2209125680Sbms break; 2210125741Sbms } 2211125680Sbms 2212125680Sbms /* calculate reading length without leadout entry */ 2213125680Sbms readlen = (th->ending_track - starting_track + 1) * 2214125680Sbms sizeof(struct cd_toc_entry); 2215125741Sbms 2216125680Sbms /* and with leadout entry */ 2217125680Sbms len = readlen + sizeof(struct cd_toc_entry); 2218125680Sbms if (te->data_len < len) { 2219125680Sbms len = te->data_len; 2220125680Sbms if (readlen > len) 2221125680Sbms readlen = len; 2222125680Sbms } 2223125680Sbms if (len > sizeof(data->entries)) { 2224125680Sbms printf("scsi_cd: error in readtocentries, " 2225125680Sbms "returning EINVAL\n"); 2226125680Sbms error = EINVAL; 2227125680Sbms free(data, M_SCSICD); 2228125680Sbms free(lead, M_SCSICD); 2229125680Sbms cam_periph_unlock(periph); 2230125680Sbms break; 2231125741Sbms } 2232125680Sbms num = len / sizeof(struct cd_toc_entry); 2233125680Sbms 2234125680Sbms if (readlen > 0) { 2235125680Sbms error = cdreadtoc(periph, te->address_format, 2236125680Sbms starting_track, 2237125680Sbms (u_int8_t *)data, 2238125680Sbms readlen + sizeof (*th), 2239125680Sbms /*sense_flags*/0); 2240125741Sbms if (error) { 2241125680Sbms free(data, M_SCSICD); 2242125680Sbms free(lead, M_SCSICD); 2243125680Sbms cam_periph_unlock(periph); 2244125680Sbms break; 2245125680Sbms } 2246125783Sbms } 2247125741Sbms 2248125680Sbms /* make leadout entry if needed */ 2249125680Sbms idx = starting_track + num - 1; 2250125680Sbms if (softc->quirks & CD_Q_BCD_TRACKS) 2251125680Sbms th->ending_track = bcd2bin(th->ending_track); 2252125680Sbms if (idx == th->ending_track + 1) { 2253125741Sbms error = cdreadtoc(periph, te->address_format, 2254125680Sbms LEADOUT, (u_int8_t *)lead, 2255125680Sbms sizeof(*lead), 2256125680Sbms /*sense_flags*/0); 2257125680Sbms if (error) { 2258125680Sbms free(data, M_SCSICD); 2259141886Smaxim free(lead, M_SCSICD); 2260141886Smaxim cam_periph_unlock(periph); 2261141886Smaxim break; 2262141886Smaxim } 2263141886Smaxim data->entries[idx - starting_track] = 2264141886Smaxim lead->entry; 2265141886Smaxim } 2266141886Smaxim if (softc->quirks & CD_Q_BCD_TRACKS) { 2267150804Smaxim for (idx = 0; idx < num - 1; idx++) { 2268141886Smaxim data->entries[idx].track = 2269141886Smaxim bcd2bin(data->entries[idx].track); 2270141886Smaxim } 2271141886Smaxim } 2272141886Smaxim 2273141886Smaxim cam_periph_unlock(periph); 2274141886Smaxim error = copyout(data->entries, te->data, len); 2275141886Smaxim free(data, M_SCSICD); 2276141886Smaxim free(lead, M_SCSICD); 2277141886Smaxim } 2278141886Smaxim break; 2279141886Smaxim case CDIOREADTOCENTRY: 2280141886Smaxim { 2281141886Smaxim struct cd_toc_single *data; 2282141886Smaxim struct ioc_read_toc_single_entry *te = 2283141886Smaxim (struct ioc_read_toc_single_entry *) addr; 2284141886Smaxim struct ioc_toc_header *th; 2285141886Smaxim u_int32_t track; 2286141886Smaxim 2287141886Smaxim data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2288141886Smaxim 2289141886Smaxim cam_periph_lock(periph); 2290141886Smaxim CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2291141886Smaxim ("trying to do CDIOREADTOCENTRY\n")); 2292141886Smaxim 2293141886Smaxim if (te->address_format != CD_MSF_FORMAT 2294141886Smaxim && te->address_format != CD_LBA_FORMAT) { 2295141886Smaxim printf("error in readtocentry, " 2296141886Smaxim " returning EINVAL\n"); 2297141886Smaxim free(data, M_SCSICD); 2298141886Smaxim error = EINVAL; 2299141886Smaxim cam_periph_unlock(periph); 2300141886Smaxim break; 2301141886Smaxim } 2302141886Smaxim 2303141886Smaxim th = &data->header; 2304141886Smaxim error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2305141886Smaxim sizeof (*th), /*sense_flags*/0); 2306141886Smaxim if (error) { 2307141886Smaxim free(data, M_SCSICD); 2308141886Smaxim cam_periph_unlock(periph); 2309148385Sume break; 2310141886Smaxim } 2311148385Sume 2312148385Sume if (softc->quirks & CD_Q_BCD_TRACKS) { 2313148616Sume /* we are going to have to convert the BCD 2314148385Sume * encoding on the cd to what is expected 2315141886Smaxim */ 2316141886Smaxim th->starting_track = 2317141886Smaxim bcd2bin(th->starting_track); 2318141886Smaxim th->ending_track = bcd2bin(th->ending_track); 2319141886Smaxim } 2320141886Smaxim track = te->track; 2321141886Smaxim if (track == 0) 2322141886Smaxim track = th->starting_track; 2323141886Smaxim else if (track == LEADOUT) 2324141886Smaxim /* OK */; 2325141886Smaxim else if (track < th->starting_track || 2326141886Smaxim track > th->ending_track + 1) { 2327141886Smaxim printf("error in readtocentry, " 2328141886Smaxim " returning EINVAL\n"); 2329141886Smaxim free(data, M_SCSICD); 2330141886Smaxim error = EINVAL; 2331141886Smaxim cam_periph_unlock(periph); 2332141886Smaxim break; 2333141886Smaxim } 2334141886Smaxim 2335141886Smaxim error = cdreadtoc(periph, te->address_format, track, 2336141886Smaxim (u_int8_t *)data, sizeof(*data), 2337141886Smaxim /*sense_flags*/0); 2338141886Smaxim if (error) { 2339141886Smaxim free(data, M_SCSICD); 2340141886Smaxim cam_periph_unlock(periph); 2341141886Smaxim break; 2342150804Smaxim } 2343150804Smaxim 2344157376Srwatson if (softc->quirks & CD_Q_BCD_TRACKS) 2345150804Smaxim data->entry.track = bcd2bin(data->entry.track); 2346141886Smaxim bcopy(&data->entry, &te->entry, 2347157376Srwatson sizeof(struct cd_toc_entry)); 2348141886Smaxim free(data, M_SCSICD); 2349141886Smaxim cam_periph_unlock(periph); 2350141886Smaxim } 2351141886Smaxim break; 2352141886Smaxim case CDIOCSETPATCH: 2353141886Smaxim { 2354141886Smaxim struct ioc_patch *arg = (struct ioc_patch *)addr; 2355141886Smaxim struct cd_mode_params params; 2356141886Smaxim union cd_pages *page; 2357141886Smaxim 2358141886Smaxim params.alloc_len = sizeof(union cd_mode_data_6_10); 2359141886Smaxim params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2360 M_WAITOK | M_ZERO); 2361 2362 cam_periph_lock(periph); 2363 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2364 ("trying to do CDIOCSETPATCH\n")); 2365 2366 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2367 if (error) { 2368 free(params.mode_buf, M_SCSICD); 2369 cam_periph_unlock(periph); 2370 break; 2371 } 2372 page = cdgetpage(¶ms); 2373 2374 page->audio.port[LEFT_PORT].channels = 2375 arg->patch[0]; 2376 page->audio.port[RIGHT_PORT].channels = 2377 arg->patch[1]; 2378 page->audio.port[2].channels = arg->patch[2]; 2379 page->audio.port[3].channels = arg->patch[3]; 2380 error = cdsetmode(periph, ¶ms); 2381 free(params.mode_buf, M_SCSICD); 2382 cam_periph_unlock(periph); 2383 } 2384 break; 2385 case CDIOCGETVOL: 2386 { 2387 struct ioc_vol *arg = (struct ioc_vol *) 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 = malloc(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 CDIOCGETVOL\n")); 2398 2399 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2400 if (error) { 2401 free(params.mode_buf, M_SCSICD); 2402 cam_periph_unlock(periph); 2403 break; 2404 } 2405 page = cdgetpage(¶ms); 2406 2407 arg->vol[LEFT_PORT] = 2408 page->audio.port[LEFT_PORT].volume; 2409 arg->vol[RIGHT_PORT] = 2410 page->audio.port[RIGHT_PORT].volume; 2411 arg->vol[2] = page->audio.port[2].volume; 2412 arg->vol[3] = page->audio.port[3].volume; 2413 free(params.mode_buf, M_SCSICD); 2414 cam_periph_unlock(periph); 2415 } 2416 break; 2417 case CDIOCSETVOL: 2418 { 2419 struct ioc_vol *arg = (struct ioc_vol *) addr; 2420 struct cd_mode_params params; 2421 union cd_pages *page; 2422 2423 params.alloc_len = sizeof(union cd_mode_data_6_10); 2424 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2425 M_WAITOK | M_ZERO); 2426 2427 cam_periph_lock(periph); 2428 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2429 ("trying to do CDIOCSETVOL\n")); 2430 2431 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2432 if (error) { 2433 free(params.mode_buf, M_SCSICD); 2434 cam_periph_unlock(periph); 2435 break; 2436 } 2437 page = cdgetpage(¶ms); 2438 2439 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2440 page->audio.port[LEFT_PORT].volume = 2441 arg->vol[LEFT_PORT]; 2442 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2443 page->audio.port[RIGHT_PORT].volume = 2444 arg->vol[RIGHT_PORT]; 2445 page->audio.port[2].volume = arg->vol[2]; 2446 page->audio.port[3].volume = arg->vol[3]; 2447 error = cdsetmode(periph, ¶ms); 2448 cam_periph_unlock(periph); 2449 free(params.mode_buf, M_SCSICD); 2450 } 2451 break; 2452 case CDIOCSETMONO: 2453 { 2454 struct cd_mode_params params; 2455 union cd_pages *page; 2456 2457 params.alloc_len = sizeof(union cd_mode_data_6_10); 2458 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2459 M_WAITOK | M_ZERO); 2460 2461 cam_periph_lock(periph); 2462 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2463 ("trying to do CDIOCSETMONO\n")); 2464 2465 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2466 if (error) { 2467 free(params.mode_buf, M_SCSICD); 2468 cam_periph_unlock(periph); 2469 break; 2470 } 2471 page = cdgetpage(¶ms); 2472 2473 page->audio.port[LEFT_PORT].channels = 2474 LEFT_CHANNEL | RIGHT_CHANNEL; 2475 page->audio.port[RIGHT_PORT].channels = 2476 LEFT_CHANNEL | RIGHT_CHANNEL; 2477 page->audio.port[2].channels = 0; 2478 page->audio.port[3].channels = 0; 2479 error = cdsetmode(periph, ¶ms); 2480 cam_periph_unlock(periph); 2481 free(params.mode_buf, M_SCSICD); 2482 } 2483 break; 2484 case CDIOCSETSTEREO: 2485 { 2486 struct cd_mode_params params; 2487 union cd_pages *page; 2488 2489 params.alloc_len = sizeof(union cd_mode_data_6_10); 2490 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2491 M_WAITOK | M_ZERO); 2492 2493 cam_periph_lock(periph); 2494 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2495 ("trying to do CDIOCSETSTEREO\n")); 2496 2497 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2498 if (error) { 2499 free(params.mode_buf, M_SCSICD); 2500 cam_periph_unlock(periph); 2501 break; 2502 } 2503 page = cdgetpage(¶ms); 2504 2505 page->audio.port[LEFT_PORT].channels = 2506 LEFT_CHANNEL; 2507 page->audio.port[RIGHT_PORT].channels = 2508 RIGHT_CHANNEL; 2509 page->audio.port[2].channels = 0; 2510 page->audio.port[3].channels = 0; 2511 error = cdsetmode(periph, ¶ms); 2512 free(params.mode_buf, M_SCSICD); 2513 cam_periph_unlock(periph); 2514 } 2515 break; 2516 case CDIOCSETMUTE: 2517 { 2518 struct cd_mode_params params; 2519 union cd_pages *page; 2520 2521 params.alloc_len = sizeof(union cd_mode_data_6_10); 2522 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2523 M_WAITOK | M_ZERO); 2524 2525 cam_periph_lock(periph); 2526 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2527 ("trying to do CDIOCSETMUTE\n")); 2528 2529 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2530 if (error) { 2531 free(¶ms.mode_buf, M_SCSICD); 2532 cam_periph_unlock(periph); 2533 break; 2534 } 2535 page = cdgetpage(¶ms); 2536 2537 page->audio.port[LEFT_PORT].channels = 0; 2538 page->audio.port[RIGHT_PORT].channels = 0; 2539 page->audio.port[2].channels = 0; 2540 page->audio.port[3].channels = 0; 2541 error = cdsetmode(periph, ¶ms); 2542 free(params.mode_buf, M_SCSICD); 2543 cam_periph_unlock(periph); 2544 } 2545 break; 2546 case CDIOCSETLEFT: 2547 { 2548 struct cd_mode_params params; 2549 union cd_pages *page; 2550 2551 params.alloc_len = sizeof(union cd_mode_data_6_10); 2552 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2553 M_WAITOK | M_ZERO); 2554 2555 cam_periph_lock(periph); 2556 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2557 ("trying to do CDIOCSETLEFT\n")); 2558 2559 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2560 if (error) { 2561 free(params.mode_buf, M_SCSICD); 2562 cam_periph_unlock(periph); 2563 break; 2564 } 2565 page = cdgetpage(¶ms); 2566 2567 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2568 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2569 page->audio.port[2].channels = 0; 2570 page->audio.port[3].channels = 0; 2571 error = cdsetmode(periph, ¶ms); 2572 free(params.mode_buf, M_SCSICD); 2573 cam_periph_unlock(periph); 2574 } 2575 break; 2576 case CDIOCSETRIGHT: 2577 { 2578 struct cd_mode_params params; 2579 union cd_pages *page; 2580 2581 params.alloc_len = sizeof(union cd_mode_data_6_10); 2582 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2583 M_WAITOK | M_ZERO); 2584 2585 cam_periph_lock(periph); 2586 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2587 ("trying to do CDIOCSETRIGHT\n")); 2588 2589 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2590 if (error) { 2591 free(params.mode_buf, M_SCSICD); 2592 cam_periph_unlock(periph); 2593 break; 2594 } 2595 page = cdgetpage(¶ms); 2596 2597 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2598 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2599 page->audio.port[2].channels = 0; 2600 page->audio.port[3].channels = 0; 2601 error = cdsetmode(periph, ¶ms); 2602 free(params.mode_buf, M_SCSICD); 2603 cam_periph_unlock(periph); 2604 } 2605 break; 2606 case CDIOCRESUME: 2607 cam_periph_lock(periph); 2608 error = cdpause(periph, 1); 2609 cam_periph_unlock(periph); 2610 break; 2611 case CDIOCPAUSE: 2612 cam_periph_lock(periph); 2613 error = cdpause(periph, 0); 2614 cam_periph_unlock(periph); 2615 break; 2616 case CDIOCSTART: 2617 cam_periph_lock(periph); 2618 error = cdstartunit(periph, 0); 2619 cam_periph_unlock(periph); 2620 break; 2621 case CDIOCCLOSE: 2622 cam_periph_lock(periph); 2623 error = cdstartunit(periph, 1); 2624 cam_periph_unlock(periph); 2625 break; 2626 case CDIOCSTOP: 2627 cam_periph_lock(periph); 2628 error = cdstopunit(periph, 0); 2629 cam_periph_unlock(periph); 2630 break; 2631 case CDIOCEJECT: 2632 cam_periph_lock(periph); 2633 error = cdstopunit(periph, 1); 2634 cam_periph_unlock(periph); 2635 break; 2636 case CDIOCALLOW: 2637 cam_periph_lock(periph); 2638 cdprevent(periph, PR_ALLOW); 2639 cam_periph_unlock(periph); 2640 break; 2641 case CDIOCPREVENT: 2642 cam_periph_lock(periph); 2643 cdprevent(periph, PR_PREVENT); 2644 cam_periph_unlock(periph); 2645 break; 2646 case CDIOCSETDEBUG: 2647 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2648 error = ENOTTY; 2649 break; 2650 case CDIOCCLRDEBUG: 2651 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2652 error = ENOTTY; 2653 break; 2654 case CDIOCRESET: 2655 /* return (cd_reset(periph)); */ 2656 error = ENOTTY; 2657 break; 2658 case CDRIOCREADSPEED: 2659 cam_periph_lock(periph); 2660 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2661 cam_periph_unlock(periph); 2662 break; 2663 case CDRIOCWRITESPEED: 2664 cam_periph_lock(periph); 2665 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2666 cam_periph_unlock(periph); 2667 break; 2668 case DVDIOCSENDKEY: 2669 case DVDIOCREPORTKEY: { 2670 struct dvd_authinfo *authinfo; 2671 2672 authinfo = (struct dvd_authinfo *)addr; 2673 2674 if (cmd == DVDIOCREPORTKEY) 2675 error = cdreportkey(periph, authinfo); 2676 else 2677 error = cdsendkey(periph, authinfo); 2678 break; 2679 } 2680 case DVDIOCREADSTRUCTURE: { 2681 struct dvd_struct *dvdstruct; 2682 2683 dvdstruct = (struct dvd_struct *)addr; 2684 2685 error = cdreaddvdstructure(periph, dvdstruct); 2686 2687 break; 2688 } 2689 default: 2690 cam_periph_lock(periph); 2691 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2692 cam_periph_unlock(periph); 2693 break; 2694 } 2695 2696 cam_periph_lock(periph); 2697 cam_periph_unhold(periph); 2698 2699 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2700 if (error && bootverbose) { 2701 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2702 } 2703 cam_periph_unlock(periph); 2704 2705 return (error); 2706} 2707 2708static void 2709cdprevent(struct cam_periph *periph, int action) 2710{ 2711 union ccb *ccb; 2712 struct cd_softc *softc; 2713 int error; 2714 2715 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2716 2717 softc = (struct cd_softc *)periph->softc; 2718 2719 if (((action == PR_ALLOW) 2720 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2721 || ((action == PR_PREVENT) 2722 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2723 return; 2724 } 2725 2726 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 2727 2728 scsi_prevent(&ccb->csio, 2729 /*retries*/ cd_retry_count, 2730 cddone, 2731 MSG_SIMPLE_Q_TAG, 2732 action, 2733 SSD_FULL_SIZE, 2734 /* timeout */60000); 2735 2736 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2737 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2738 2739 xpt_release_ccb(ccb); 2740 2741 if (error == 0) { 2742 if (action == PR_ALLOW) 2743 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2744 else 2745 softc->flags |= CD_FLAG_DISC_LOCKED; 2746 } 2747} 2748 2749/* 2750 * XXX: the disk media and sector size is only really able to change 2751 * XXX: while the device is closed. 2752 */ 2753static int 2754cdcheckmedia(struct cam_periph *periph) 2755{ 2756 struct cd_softc *softc; 2757 struct ioc_toc_header *toch; 2758 struct cd_toc_single leadout; 2759 u_int32_t size, toclen; 2760 int error, num_entries, cdindex; 2761 2762 softc = (struct cd_softc *)periph->softc; 2763 2764 cdprevent(periph, PR_PREVENT); 2765 softc->disk->d_sectorsize = 2048; 2766 softc->disk->d_mediasize = 0; 2767 2768 /* 2769 * Get the disc size and block size. If we can't get it, we don't 2770 * have media, most likely. 2771 */ 2772 if ((error = cdsize(periph, &size)) != 0) { 2773 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2774 cdprevent(periph, PR_ALLOW); 2775 return (error); 2776 } else { 2777 softc->flags |= CD_FLAG_VALID_MEDIA; 2778 softc->disk->d_sectorsize = softc->params.blksize; 2779 softc->disk->d_mediasize = 2780 (off_t)softc->params.blksize * softc->params.disksize; 2781 } 2782 2783 /* 2784 * Now we check the table of contents. This (currently) is only 2785 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2786 * things like present a separate entry in /dev for each track, 2787 * like that acd(4) driver does. 2788 */ 2789 bzero(&softc->toc, sizeof(softc->toc)); 2790 toch = &softc->toc.header; 2791 /* 2792 * We will get errors here for media that doesn't have a table of 2793 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2794 * command is presented for a DDCD/CD-R/RW media, where the first TOC 2795 * has not been recorded (no complete session) and the Format codes 2796 * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2797 * with an INVALID FIELD IN CDB. Devices that are not capable of 2798 * reading an incomplete session on DDC/CD-R/RW media shall report 2799 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2800 * 2801 * So this isn't fatal if we can't read the table of contents, it 2802 * just means that the user won't be able to issue the play tracks 2803 * ioctl, and likely lots of other stuff won't work either. They 2804 * need to burn the CD before we can do a whole lot with it. So 2805 * we don't print anything here if we get an error back. 2806 */ 2807 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2808 SF_NO_PRINT); 2809 /* 2810 * Errors in reading the table of contents aren't fatal, we just 2811 * won't have a valid table of contents cached. 2812 */ 2813 if (error != 0) { 2814 error = 0; 2815 bzero(&softc->toc, sizeof(softc->toc)); 2816 goto bailout; 2817 } 2818 2819 if (softc->quirks & CD_Q_BCD_TRACKS) { 2820 toch->starting_track = bcd2bin(toch->starting_track); 2821 toch->ending_track = bcd2bin(toch->ending_track); 2822 } 2823 2824 /* Number of TOC entries, plus leadout */ 2825 num_entries = (toch->ending_track - toch->starting_track) + 2; 2826 2827 if (num_entries <= 0) 2828 goto bailout; 2829 2830 toclen = num_entries * sizeof(struct cd_toc_entry); 2831 2832 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2833 (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2834 SF_NO_PRINT); 2835 if (error != 0) { 2836 error = 0; 2837 bzero(&softc->toc, sizeof(softc->toc)); 2838 goto bailout; 2839 } 2840 2841 if (softc->quirks & CD_Q_BCD_TRACKS) { 2842 toch->starting_track = bcd2bin(toch->starting_track); 2843 toch->ending_track = bcd2bin(toch->ending_track); 2844 } 2845 /* 2846 * XXX KDM is this necessary? Probably only if the drive doesn't 2847 * return leadout information with the table of contents. 2848 */ 2849 cdindex = toch->starting_track + num_entries -1; 2850 if (cdindex == toch->ending_track + 1) { 2851 2852 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2853 (u_int8_t *)&leadout, sizeof(leadout), 2854 SF_NO_PRINT); 2855 if (error != 0) { 2856 error = 0; 2857 goto bailout; 2858 } 2859 softc->toc.entries[cdindex - toch->starting_track] = 2860 leadout.entry; 2861 } 2862 if (softc->quirks & CD_Q_BCD_TRACKS) { 2863 for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2864 softc->toc.entries[cdindex].track = 2865 bcd2bin(softc->toc.entries[cdindex].track); 2866 } 2867 } 2868 2869 softc->flags |= CD_FLAG_VALID_TOC; 2870 2871bailout: 2872 2873 /* 2874 * We unconditionally (re)set the blocksize each time the 2875 * CD device is opened. This is because the CD can change, 2876 * and therefore the blocksize might change. 2877 * XXX problems here if some slice or partition is still 2878 * open with the old size? 2879 */ 2880 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2881 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2882 softc->disk->d_devstat->block_size = softc->params.blksize; 2883 2884 return (error); 2885} 2886 2887static int 2888cdsize(struct cam_periph *periph, u_int32_t *size) 2889{ 2890 struct cd_softc *softc; 2891 union ccb *ccb; 2892 struct scsi_read_capacity_data *rcap_buf; 2893 int error; 2894 2895 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 2896 2897 softc = (struct cd_softc *)periph->softc; 2898 2899 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 2900 2901 /* XXX Should be M_WAITOK */ 2902 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2903 M_SCSICD, M_NOWAIT | M_ZERO); 2904 if (rcap_buf == NULL) 2905 return (ENOMEM); 2906 2907 scsi_read_capacity(&ccb->csio, 2908 /*retries*/ cd_retry_count, 2909 cddone, 2910 MSG_SIMPLE_Q_TAG, 2911 rcap_buf, 2912 SSD_FULL_SIZE, 2913 /* timeout */20000); 2914 2915 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2916 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2917 2918 xpt_release_ccb(ccb); 2919 2920 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 2921 softc->params.blksize = scsi_4btoul(rcap_buf->length); 2922 /* Make sure we got at least some block size. */ 2923 if (error == 0 && softc->params.blksize == 0) 2924 error = EIO; 2925 /* 2926 * SCSI-3 mandates that the reported blocksize shall be 2048. 2927 * Older drives sometimes report funny values, trim it down to 2928 * 2048, or other parts of the kernel will get confused. 2929 * 2930 * XXX we leave drives alone that might report 512 bytes, as 2931 * well as drives reporting more weird sizes like perhaps 4K. 2932 */ 2933 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 2934 softc->params.blksize = 2048; 2935 2936 free(rcap_buf, M_SCSICD); 2937 *size = softc->params.disksize; 2938 2939 return (error); 2940 2941} 2942 2943static int 2944cd6byteworkaround(union ccb *ccb) 2945{ 2946 u_int8_t *cdb; 2947 struct cam_periph *periph; 2948 struct cd_softc *softc; 2949 struct cd_mode_params *params; 2950 int frozen, found; 2951 2952 periph = xpt_path_periph(ccb->ccb_h.path); 2953 softc = (struct cd_softc *)periph->softc; 2954 2955 cdb = ccb->csio.cdb_io.cdb_bytes; 2956 2957 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2958 || ((cdb[0] != MODE_SENSE_6) 2959 && (cdb[0] != MODE_SELECT_6))) 2960 return (0); 2961 2962 /* 2963 * Because there is no convenient place to stash the overall 2964 * cd_mode_params structure pointer, we have to grab it like this. 2965 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2966 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2967 * 2968 * XXX It would be nice if, at some point, we could increase the 2969 * number of available peripheral private pointers. Both pointers 2970 * are currently used in most every peripheral driver. 2971 */ 2972 found = 0; 2973 2974 STAILQ_FOREACH(params, &softc->mode_queue, links) { 2975 if (params->mode_buf == ccb->csio.data_ptr) { 2976 found = 1; 2977 break; 2978 } 2979 } 2980 2981 /* 2982 * This shouldn't happen. All mode sense and mode select 2983 * operations in the cd(4) driver MUST go through cdgetmode() and 2984 * cdsetmode()! 2985 */ 2986 if (found == 0) { 2987 xpt_print(periph->path, 2988 "mode buffer not found in mode queue!\n"); 2989 return (0); 2990 } 2991 2992 params->cdb_size = 10; 2993 softc->minimum_command_size = 10; 2994 xpt_print(ccb->ccb_h.path, 2995 "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 2996 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 2997 2998 if (cdb[0] == MODE_SENSE_6) { 2999 struct scsi_mode_sense_10 ms10; 3000 struct scsi_mode_sense_6 *ms6; 3001 int len; 3002 3003 ms6 = (struct scsi_mode_sense_6 *)cdb; 3004 3005 bzero(&ms10, sizeof(ms10)); 3006 ms10.opcode = MODE_SENSE_10; 3007 ms10.byte2 = ms6->byte2; 3008 ms10.page = ms6->page; 3009 3010 /* 3011 * 10 byte mode header, block descriptor, 3012 * sizeof(union cd_pages) 3013 */ 3014 len = sizeof(struct cd_mode_data_10); 3015 ccb->csio.dxfer_len = len; 3016 3017 scsi_ulto2b(len, ms10.length); 3018 ms10.control = ms6->control; 3019 bcopy(&ms10, cdb, 10); 3020 ccb->csio.cdb_len = 10; 3021 } else { 3022 struct scsi_mode_select_10 ms10; 3023 struct scsi_mode_select_6 *ms6; 3024 struct scsi_mode_header_6 *header6; 3025 struct scsi_mode_header_10 *header10; 3026 struct scsi_mode_page_header *page_header; 3027 int blk_desc_len, page_num, page_size, len; 3028 3029 ms6 = (struct scsi_mode_select_6 *)cdb; 3030 3031 bzero(&ms10, sizeof(ms10)); 3032 ms10.opcode = MODE_SELECT_10; 3033 ms10.byte2 = ms6->byte2; 3034 3035 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3036 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3037 3038 page_header = find_mode_page_6(header6); 3039 page_num = page_header->page_code; 3040 3041 blk_desc_len = header6->blk_desc_len; 3042 3043 page_size = cdgetpagesize(page_num); 3044 3045 if (page_size != (page_header->page_length + 3046 sizeof(*page_header))) 3047 page_size = page_header->page_length + 3048 sizeof(*page_header); 3049 3050 len = sizeof(*header10) + blk_desc_len + page_size; 3051 3052 len = min(params->alloc_len, len); 3053 3054 /* 3055 * Since the 6 byte parameter header is shorter than the 10 3056 * byte parameter header, we need to copy the actual mode 3057 * page data, and the block descriptor, if any, so things wind 3058 * up in the right place. The regions will overlap, but 3059 * bcopy() does the right thing. 3060 */ 3061 bcopy(params->mode_buf + sizeof(*header6), 3062 params->mode_buf + sizeof(*header10), 3063 len - sizeof(*header10)); 3064 3065 /* Make sure these fields are set correctly. */ 3066 scsi_ulto2b(0, header10->data_length); 3067 header10->medium_type = 0; 3068 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3069 3070 ccb->csio.dxfer_len = len; 3071 3072 scsi_ulto2b(len, ms10.length); 3073 ms10.control = ms6->control; 3074 bcopy(&ms10, cdb, 10); 3075 ccb->csio.cdb_len = 10; 3076 } 3077 3078 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3079 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3080 xpt_action(ccb); 3081 if (frozen) { 3082 cam_release_devq(ccb->ccb_h.path, 3083 /*relsim_flags*/0, 3084 /*openings*/0, 3085 /*timeout*/0, 3086 /*getcount_only*/0); 3087 } 3088 3089 return (ERESTART); 3090} 3091 3092static int 3093cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3094{ 3095 struct cd_softc *softc; 3096 struct cam_periph *periph; 3097 int error; 3098 3099 periph = xpt_path_periph(ccb->ccb_h.path); 3100 softc = (struct cd_softc *)periph->softc; 3101 3102 error = 0; 3103 3104 /* 3105 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3106 * CDB comes back with this particular error, try transforming it 3107 * into the 10 byte version. 3108 */ 3109 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3110 error = cd6byteworkaround(ccb); 3111 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3112 CAM_SCSI_STATUS_ERROR) 3113 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3114 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3115 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3116 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3117 int sense_key, error_code, asc, ascq; 3118 3119 scsi_extract_sense(&ccb->csio.sense_data, 3120 &error_code, &sense_key, &asc, &ascq); 3121 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3122 error = cd6byteworkaround(ccb); 3123 } 3124 3125 if (error == ERESTART) 3126 return (error); 3127 3128 /* 3129 * XXX 3130 * Until we have a better way of doing pack validation, 3131 * don't treat UAs as errors. 3132 */ 3133 sense_flags |= SF_RETRY_UA; 3134 return (cam_periph_error(ccb, cam_flags, sense_flags, 3135 &softc->saved_ccb)); 3136} 3137 3138/* 3139 * Read table of contents 3140 */ 3141static int 3142cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3143 u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 3144{ 3145 struct scsi_read_toc *scsi_cmd; 3146 u_int32_t ntoc; 3147 struct ccb_scsiio *csio; 3148 union ccb *ccb; 3149 int error; 3150 3151 ntoc = len; 3152 error = 0; 3153 3154 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3155 3156 csio = &ccb->csio; 3157 3158 cam_fill_csio(csio, 3159 /* retries */ cd_retry_count, 3160 /* cbfcnp */ cddone, 3161 /* flags */ CAM_DIR_IN, 3162 /* tag_action */ MSG_SIMPLE_Q_TAG, 3163 /* data_ptr */ data, 3164 /* dxfer_len */ len, 3165 /* sense_len */ SSD_FULL_SIZE, 3166 sizeof(struct scsi_read_toc), 3167 /* timeout */ 50000); 3168 3169 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 3170 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3171 3172 if (mode == CD_MSF_FORMAT) 3173 scsi_cmd->byte2 |= CD_MSF; 3174 scsi_cmd->from_track = start; 3175 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 3176 scsi_cmd->data_len[0] = (ntoc) >> 8; 3177 scsi_cmd->data_len[1] = (ntoc) & 0xff; 3178 3179 scsi_cmd->op_code = READ_TOC; 3180 3181 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3182 /*sense_flags*/SF_RETRY_UA | sense_flags); 3183 3184 xpt_release_ccb(ccb); 3185 3186 return(error); 3187} 3188 3189static int 3190cdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 3191 u_int32_t format, int track, 3192 struct cd_sub_channel_info *data, u_int32_t len) 3193{ 3194 struct scsi_read_subchannel *scsi_cmd; 3195 struct ccb_scsiio *csio; 3196 union ccb *ccb; 3197 int error; 3198 3199 error = 0; 3200 3201 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3202 3203 csio = &ccb->csio; 3204 3205 cam_fill_csio(csio, 3206 /* retries */ cd_retry_count, 3207 /* cbfcnp */ cddone, 3208 /* flags */ CAM_DIR_IN, 3209 /* tag_action */ MSG_SIMPLE_Q_TAG, 3210 /* data_ptr */ (u_int8_t *)data, 3211 /* dxfer_len */ len, 3212 /* sense_len */ SSD_FULL_SIZE, 3213 sizeof(struct scsi_read_subchannel), 3214 /* timeout */ 50000); 3215 3216 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 3217 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3218 3219 scsi_cmd->op_code = READ_SUBCHANNEL; 3220 if (mode == CD_MSF_FORMAT) 3221 scsi_cmd->byte1 |= CD_MSF; 3222 scsi_cmd->byte2 = SRS_SUBQ; 3223 scsi_cmd->subchan_format = format; 3224 scsi_cmd->track = track; 3225 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 3226 scsi_cmd->control = 0; 3227 3228 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3229 /*sense_flags*/SF_RETRY_UA); 3230 3231 xpt_release_ccb(ccb); 3232 3233 return(error); 3234} 3235 3236 3237/* 3238 * All MODE_SENSE requests in the cd(4) driver MUST go through this 3239 * routine. See comments in cd6byteworkaround() for details. 3240 */ 3241static int 3242cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3243 u_int32_t page) 3244{ 3245 struct ccb_scsiio *csio; 3246 struct cd_softc *softc; 3247 union ccb *ccb; 3248 int param_len; 3249 int error; 3250 3251 softc = (struct cd_softc *)periph->softc; 3252 3253 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3254 3255 csio = &ccb->csio; 3256 3257 data->cdb_size = softc->minimum_command_size; 3258 if (data->cdb_size < 10) 3259 param_len = sizeof(struct cd_mode_data); 3260 else 3261 param_len = sizeof(struct cd_mode_data_10); 3262 3263 /* Don't say we've got more room than we actually allocated */ 3264 param_len = min(param_len, data->alloc_len); 3265 3266 scsi_mode_sense_len(csio, 3267 /* retries */ cd_retry_count, 3268 /* cbfcnp */ cddone, 3269 /* tag_action */ MSG_SIMPLE_Q_TAG, 3270 /* dbd */ 0, 3271 /* page_code */ SMS_PAGE_CTRL_CURRENT, 3272 /* page */ page, 3273 /* param_buf */ data->mode_buf, 3274 /* param_len */ param_len, 3275 /* minimum_cmd_size */ softc->minimum_command_size, 3276 /* sense_len */ SSD_FULL_SIZE, 3277 /* timeout */ 50000); 3278 3279 /* 3280 * It would be nice not to have to do this, but there's no 3281 * available pointer in the CCB that would allow us to stuff the 3282 * mode params structure in there and retrieve it in 3283 * cd6byteworkaround(), so we can set the cdb size. The cdb size 3284 * lets the caller know what CDB size we ended up using, so they 3285 * can find the actual mode page offset. 3286 */ 3287 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3288 3289 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3290 /*sense_flags*/SF_RETRY_UA); 3291 3292 xpt_release_ccb(ccb); 3293 3294 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3295 3296 /* 3297 * This is a bit of belt-and-suspenders checking, but if we run 3298 * into a situation where the target sends back multiple block 3299 * descriptors, we might not have enough space in the buffer to 3300 * see the whole mode page. Better to return an error than 3301 * potentially access memory beyond our malloced region. 3302 */ 3303 if (error == 0) { 3304 u_int32_t data_len; 3305 3306 if (data->cdb_size == 10) { 3307 struct scsi_mode_header_10 *hdr10; 3308 3309 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3310 data_len = scsi_2btoul(hdr10->data_length); 3311 data_len += sizeof(hdr10->data_length); 3312 } else { 3313 struct scsi_mode_header_6 *hdr6; 3314 3315 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3316 data_len = hdr6->data_length; 3317 data_len += sizeof(hdr6->data_length); 3318 } 3319 3320 /* 3321 * Complain if there is more mode data available than we 3322 * allocated space for. This could potentially happen if 3323 * we miscalculated the page length for some reason, if the 3324 * drive returns multiple block descriptors, or if it sets 3325 * the data length incorrectly. 3326 */ 3327 if (data_len > data->alloc_len) { 3328 xpt_print(periph->path, "allocated modepage %d length " 3329 "%d < returned length %d\n", page, data->alloc_len, 3330 data_len); 3331 error = ENOSPC; 3332 } 3333 } 3334 return (error); 3335} 3336 3337/* 3338 * All MODE_SELECT requests in the cd(4) driver MUST go through this 3339 * routine. See comments in cd6byteworkaround() for details. 3340 */ 3341static int 3342cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 3343{ 3344 struct ccb_scsiio *csio; 3345 struct cd_softc *softc; 3346 union ccb *ccb; 3347 int cdb_size, param_len; 3348 int error; 3349 3350 softc = (struct cd_softc *)periph->softc; 3351 3352 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3353 3354 csio = &ccb->csio; 3355 3356 error = 0; 3357 3358 /* 3359 * If the data is formatted for the 10 byte version of the mode 3360 * select parameter list, we need to use the 10 byte CDB. 3361 * Otherwise, we use whatever the stored minimum command size. 3362 */ 3363 if (data->cdb_size == 10) 3364 cdb_size = data->cdb_size; 3365 else 3366 cdb_size = softc->minimum_command_size; 3367 3368 if (cdb_size >= 10) { 3369 struct scsi_mode_header_10 *mode_header; 3370 u_int32_t data_len; 3371 3372 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3373 3374 data_len = scsi_2btoul(mode_header->data_length); 3375 3376 scsi_ulto2b(0, mode_header->data_length); 3377 /* 3378 * SONY drives do not allow a mode select with a medium_type 3379 * value that has just been returned by a mode sense; use a 3380 * medium_type of 0 (Default) instead. 3381 */ 3382 mode_header->medium_type = 0; 3383 3384 /* 3385 * Pass back whatever the drive passed to us, plus the size 3386 * of the data length field. 3387 */ 3388 param_len = data_len + sizeof(mode_header->data_length); 3389 3390 } else { 3391 struct scsi_mode_header_6 *mode_header; 3392 3393 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3394 3395 param_len = mode_header->data_length + 1; 3396 3397 mode_header->data_length = 0; 3398 /* 3399 * SONY drives do not allow a mode select with a medium_type 3400 * value that has just been returned by a mode sense; use a 3401 * medium_type of 0 (Default) instead. 3402 */ 3403 mode_header->medium_type = 0; 3404 } 3405 3406 /* Don't say we've got more room than we actually allocated */ 3407 param_len = min(param_len, data->alloc_len); 3408 3409 scsi_mode_select_len(csio, 3410 /* retries */ cd_retry_count, 3411 /* cbfcnp */ cddone, 3412 /* tag_action */ MSG_SIMPLE_Q_TAG, 3413 /* scsi_page_fmt */ 1, 3414 /* save_pages */ 0, 3415 /* param_buf */ data->mode_buf, 3416 /* param_len */ param_len, 3417 /* minimum_cmd_size */ cdb_size, 3418 /* sense_len */ SSD_FULL_SIZE, 3419 /* timeout */ 50000); 3420 3421 /* See comments in cdgetmode() and cd6byteworkaround(). */ 3422 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3423 3424 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3425 /*sense_flags*/SF_RETRY_UA); 3426 3427 xpt_release_ccb(ccb); 3428 3429 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3430 3431 return (error); 3432} 3433 3434 3435static int 3436cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 3437{ 3438 struct ccb_scsiio *csio; 3439 union ccb *ccb; 3440 int error; 3441 u_int8_t cdb_len; 3442 3443 error = 0; 3444 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3445 csio = &ccb->csio; 3446 /* 3447 * Use the smallest possible command to perform the operation. 3448 */ 3449 if ((len & 0xffff0000) == 0) { 3450 /* 3451 * We can fit in a 10 byte cdb. 3452 */ 3453 struct scsi_play_10 *scsi_cmd; 3454 3455 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 3456 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3457 scsi_cmd->op_code = PLAY_10; 3458 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3459 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 3460 cdb_len = sizeof(*scsi_cmd); 3461 } else { 3462 struct scsi_play_12 *scsi_cmd; 3463 3464 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 3465 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3466 scsi_cmd->op_code = PLAY_12; 3467 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 3468 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 3469 cdb_len = sizeof(*scsi_cmd); 3470 } 3471 cam_fill_csio(csio, 3472 /*retries*/ cd_retry_count, 3473 cddone, 3474 /*flags*/CAM_DIR_NONE, 3475 MSG_SIMPLE_Q_TAG, 3476 /*dataptr*/NULL, 3477 /*datalen*/0, 3478 /*sense_len*/SSD_FULL_SIZE, 3479 cdb_len, 3480 /*timeout*/50 * 1000); 3481 3482 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3483 /*sense_flags*/SF_RETRY_UA); 3484 3485 xpt_release_ccb(ccb); 3486 3487 return(error); 3488} 3489 3490static int 3491cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 3492 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 3493{ 3494 struct scsi_play_msf *scsi_cmd; 3495 struct ccb_scsiio *csio; 3496 union ccb *ccb; 3497 int error; 3498 3499 error = 0; 3500 3501 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3502 3503 csio = &ccb->csio; 3504 3505 cam_fill_csio(csio, 3506 /* retries */ cd_retry_count, 3507 /* cbfcnp */ cddone, 3508 /* flags */ CAM_DIR_NONE, 3509 /* tag_action */ MSG_SIMPLE_Q_TAG, 3510 /* data_ptr */ NULL, 3511 /* dxfer_len */ 0, 3512 /* sense_len */ SSD_FULL_SIZE, 3513 sizeof(struct scsi_play_msf), 3514 /* timeout */ 50000); 3515 3516 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 3517 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3518 3519 scsi_cmd->op_code = PLAY_MSF; 3520 scsi_cmd->start_m = startm; 3521 scsi_cmd->start_s = starts; 3522 scsi_cmd->start_f = startf; 3523 scsi_cmd->end_m = endm; 3524 scsi_cmd->end_s = ends; 3525 scsi_cmd->end_f = endf; 3526 3527 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3528 /*sense_flags*/SF_RETRY_UA); 3529 3530 xpt_release_ccb(ccb); 3531 3532 return(error); 3533} 3534 3535 3536static int 3537cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 3538 u_int32_t etrack, u_int32_t eindex) 3539{ 3540 struct scsi_play_track *scsi_cmd; 3541 struct ccb_scsiio *csio; 3542 union ccb *ccb; 3543 int error; 3544 3545 error = 0; 3546 3547 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3548 3549 csio = &ccb->csio; 3550 3551 cam_fill_csio(csio, 3552 /* retries */ cd_retry_count, 3553 /* cbfcnp */ cddone, 3554 /* flags */ CAM_DIR_NONE, 3555 /* tag_action */ MSG_SIMPLE_Q_TAG, 3556 /* data_ptr */ NULL, 3557 /* dxfer_len */ 0, 3558 /* sense_len */ SSD_FULL_SIZE, 3559 sizeof(struct scsi_play_track), 3560 /* timeout */ 50000); 3561 3562 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3563 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3564 3565 scsi_cmd->op_code = PLAY_TRACK; 3566 scsi_cmd->start_track = strack; 3567 scsi_cmd->start_index = sindex; 3568 scsi_cmd->end_track = etrack; 3569 scsi_cmd->end_index = eindex; 3570 3571 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3572 /*sense_flags*/SF_RETRY_UA); 3573 3574 xpt_release_ccb(ccb); 3575 3576 return(error); 3577} 3578 3579static int 3580cdpause(struct cam_periph *periph, u_int32_t go) 3581{ 3582 struct scsi_pause *scsi_cmd; 3583 struct ccb_scsiio *csio; 3584 union ccb *ccb; 3585 int error; 3586 3587 error = 0; 3588 3589 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3590 3591 csio = &ccb->csio; 3592 3593 cam_fill_csio(csio, 3594 /* retries */ cd_retry_count, 3595 /* cbfcnp */ cddone, 3596 /* flags */ CAM_DIR_NONE, 3597 /* tag_action */ MSG_SIMPLE_Q_TAG, 3598 /* data_ptr */ NULL, 3599 /* dxfer_len */ 0, 3600 /* sense_len */ SSD_FULL_SIZE, 3601 sizeof(struct scsi_pause), 3602 /* timeout */ 50000); 3603 3604 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3605 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3606 3607 scsi_cmd->op_code = PAUSE; 3608 scsi_cmd->resume = go; 3609 3610 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3611 /*sense_flags*/SF_RETRY_UA); 3612 3613 xpt_release_ccb(ccb); 3614 3615 return(error); 3616} 3617 3618static int 3619cdstartunit(struct cam_periph *periph, int load) 3620{ 3621 union ccb *ccb; 3622 int error; 3623 3624 error = 0; 3625 3626 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3627 3628 scsi_start_stop(&ccb->csio, 3629 /* retries */ cd_retry_count, 3630 /* cbfcnp */ cddone, 3631 /* tag_action */ MSG_SIMPLE_Q_TAG, 3632 /* start */ TRUE, 3633 /* load_eject */ load, 3634 /* immediate */ FALSE, 3635 /* sense_len */ SSD_FULL_SIZE, 3636 /* timeout */ 50000); 3637 3638 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3639 /*sense_flags*/SF_RETRY_UA); 3640 3641 xpt_release_ccb(ccb); 3642 3643 return(error); 3644} 3645 3646static int 3647cdstopunit(struct cam_periph *periph, u_int32_t eject) 3648{ 3649 union ccb *ccb; 3650 int error; 3651 3652 error = 0; 3653 3654 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3655 3656 scsi_start_stop(&ccb->csio, 3657 /* retries */ cd_retry_count, 3658 /* cbfcnp */ cddone, 3659 /* tag_action */ MSG_SIMPLE_Q_TAG, 3660 /* start */ FALSE, 3661 /* load_eject */ eject, 3662 /* immediate */ FALSE, 3663 /* sense_len */ SSD_FULL_SIZE, 3664 /* timeout */ 50000); 3665 3666 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3667 /*sense_flags*/SF_RETRY_UA); 3668 3669 xpt_release_ccb(ccb); 3670 3671 return(error); 3672} 3673 3674static int 3675cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3676{ 3677 struct scsi_set_speed *scsi_cmd; 3678 struct ccb_scsiio *csio; 3679 union ccb *ccb; 3680 int error; 3681 3682 error = 0; 3683 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3684 csio = &ccb->csio; 3685 3686 /* Preserve old behavior: units in multiples of CDROM speed */ 3687 if (rdspeed < 177) 3688 rdspeed *= 177; 3689 if (wrspeed < 177) 3690 wrspeed *= 177; 3691 3692 cam_fill_csio(csio, 3693 /* retries */ cd_retry_count, 3694 /* cbfcnp */ cddone, 3695 /* flags */ CAM_DIR_NONE, 3696 /* tag_action */ MSG_SIMPLE_Q_TAG, 3697 /* data_ptr */ NULL, 3698 /* dxfer_len */ 0, 3699 /* sense_len */ SSD_FULL_SIZE, 3700 sizeof(struct scsi_set_speed), 3701 /* timeout */ 50000); 3702 3703 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3704 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3705 3706 scsi_cmd->opcode = SET_CD_SPEED; 3707 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3708 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3709 3710 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3711 /*sense_flags*/SF_RETRY_UA); 3712 3713 xpt_release_ccb(ccb); 3714 3715 return(error); 3716} 3717 3718static int 3719cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3720{ 3721 union ccb *ccb; 3722 u_int8_t *databuf; 3723 u_int32_t lba; 3724 int error; 3725 int length; 3726 3727 error = 0; 3728 databuf = NULL; 3729 lba = 0; 3730 3731 switch (authinfo->format) { 3732 case DVD_REPORT_AGID: 3733 length = sizeof(struct scsi_report_key_data_agid); 3734 break; 3735 case DVD_REPORT_CHALLENGE: 3736 length = sizeof(struct scsi_report_key_data_challenge); 3737 break; 3738 case DVD_REPORT_KEY1: 3739 length = sizeof(struct scsi_report_key_data_key1_key2); 3740 break; 3741 case DVD_REPORT_TITLE_KEY: 3742 length = sizeof(struct scsi_report_key_data_title); 3743 /* The lba field is only set for the title key */ 3744 lba = authinfo->lba; 3745 break; 3746 case DVD_REPORT_ASF: 3747 length = sizeof(struct scsi_report_key_data_asf); 3748 break; 3749 case DVD_REPORT_RPC: 3750 length = sizeof(struct scsi_report_key_data_rpc); 3751 break; 3752 case DVD_INVALIDATE_AGID: 3753 length = 0; 3754 break; 3755 default: 3756 return (EINVAL); 3757 } 3758 3759 if (length != 0) { 3760 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3761 } else 3762 databuf = NULL; 3763 3764 cam_periph_lock(periph); 3765 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3766 3767 scsi_report_key(&ccb->csio, 3768 /* retries */ cd_retry_count, 3769 /* cbfcnp */ cddone, 3770 /* tag_action */ MSG_SIMPLE_Q_TAG, 3771 /* lba */ lba, 3772 /* agid */ authinfo->agid, 3773 /* key_format */ authinfo->format, 3774 /* data_ptr */ databuf, 3775 /* dxfer_len */ length, 3776 /* sense_len */ SSD_FULL_SIZE, 3777 /* timeout */ 50000); 3778 3779 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3780 /*sense_flags*/SF_RETRY_UA); 3781 3782 if (error != 0) 3783 goto bailout; 3784 3785 if (ccb->csio.resid != 0) { 3786 xpt_print(periph->path, "warning, residual for report key " 3787 "command is %d\n", ccb->csio.resid); 3788 } 3789 3790 switch(authinfo->format) { 3791 case DVD_REPORT_AGID: { 3792 struct scsi_report_key_data_agid *agid_data; 3793 3794 agid_data = (struct scsi_report_key_data_agid *)databuf; 3795 3796 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3797 RKD_AGID_SHIFT; 3798 break; 3799 } 3800 case DVD_REPORT_CHALLENGE: { 3801 struct scsi_report_key_data_challenge *chal_data; 3802 3803 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3804 3805 bcopy(chal_data->challenge_key, authinfo->keychal, 3806 min(sizeof(chal_data->challenge_key), 3807 sizeof(authinfo->keychal))); 3808 break; 3809 } 3810 case DVD_REPORT_KEY1: { 3811 struct scsi_report_key_data_key1_key2 *key1_data; 3812 3813 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3814 3815 bcopy(key1_data->key1, authinfo->keychal, 3816 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3817 break; 3818 } 3819 case DVD_REPORT_TITLE_KEY: { 3820 struct scsi_report_key_data_title *title_data; 3821 3822 title_data = (struct scsi_report_key_data_title *)databuf; 3823 3824 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3825 RKD_TITLE_CPM_SHIFT; 3826 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3827 RKD_TITLE_CP_SEC_SHIFT; 3828 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3829 RKD_TITLE_CMGS_SHIFT; 3830 bcopy(title_data->title_key, authinfo->keychal, 3831 min(sizeof(title_data->title_key), 3832 sizeof(authinfo->keychal))); 3833 break; 3834 } 3835 case DVD_REPORT_ASF: { 3836 struct scsi_report_key_data_asf *asf_data; 3837 3838 asf_data = (struct scsi_report_key_data_asf *)databuf; 3839 3840 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3841 break; 3842 } 3843 case DVD_REPORT_RPC: { 3844 struct scsi_report_key_data_rpc *rpc_data; 3845 3846 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3847 3848 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3849 RKD_RPC_TYPE_SHIFT; 3850 authinfo->vend_rsts = 3851 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3852 RKD_RPC_VENDOR_RESET_SHIFT; 3853 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3854 authinfo->region = rpc_data->region_mask; 3855 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3856 break; 3857 } 3858 case DVD_INVALIDATE_AGID: 3859 break; 3860 default: 3861 /* This should be impossible, since we checked above */ 3862 error = EINVAL; 3863 goto bailout; 3864 break; /* NOTREACHED */ 3865 } 3866 3867bailout: 3868 xpt_release_ccb(ccb); 3869 cam_periph_unlock(periph); 3870 3871 if (databuf != NULL) 3872 free(databuf, M_DEVBUF); 3873 3874 return(error); 3875} 3876 3877static int 3878cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3879{ 3880 union ccb *ccb; 3881 u_int8_t *databuf; 3882 int length; 3883 int error; 3884 3885 error = 0; 3886 databuf = NULL; 3887 3888 switch(authinfo->format) { 3889 case DVD_SEND_CHALLENGE: { 3890 struct scsi_report_key_data_challenge *challenge_data; 3891 3892 length = sizeof(*challenge_data); 3893 3894 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3895 3896 databuf = (u_int8_t *)challenge_data; 3897 3898 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3899 challenge_data->data_len); 3900 3901 bcopy(authinfo->keychal, challenge_data->challenge_key, 3902 min(sizeof(authinfo->keychal), 3903 sizeof(challenge_data->challenge_key))); 3904 break; 3905 } 3906 case DVD_SEND_KEY2: { 3907 struct scsi_report_key_data_key1_key2 *key2_data; 3908 3909 length = sizeof(*key2_data); 3910 3911 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3912 3913 databuf = (u_int8_t *)key2_data; 3914 3915 scsi_ulto2b(length - sizeof(key2_data->data_len), 3916 key2_data->data_len); 3917 3918 bcopy(authinfo->keychal, key2_data->key1, 3919 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 3920 3921 break; 3922 } 3923 case DVD_SEND_RPC: { 3924 struct scsi_send_key_data_rpc *rpc_data; 3925 3926 length = sizeof(*rpc_data); 3927 3928 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3929 3930 databuf = (u_int8_t *)rpc_data; 3931 3932 scsi_ulto2b(length - sizeof(rpc_data->data_len), 3933 rpc_data->data_len); 3934 3935 rpc_data->region_code = authinfo->region; 3936 break; 3937 } 3938 default: 3939 return (EINVAL); 3940 } 3941 3942 cam_periph_lock(periph); 3943 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3944 3945 scsi_send_key(&ccb->csio, 3946 /* retries */ cd_retry_count, 3947 /* cbfcnp */ cddone, 3948 /* tag_action */ MSG_SIMPLE_Q_TAG, 3949 /* agid */ authinfo->agid, 3950 /* key_format */ authinfo->format, 3951 /* data_ptr */ databuf, 3952 /* dxfer_len */ length, 3953 /* sense_len */ SSD_FULL_SIZE, 3954 /* timeout */ 50000); 3955 3956 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3957 /*sense_flags*/SF_RETRY_UA); 3958 3959 xpt_release_ccb(ccb); 3960 cam_periph_unlock(periph); 3961 3962 if (databuf != NULL) 3963 free(databuf, M_DEVBUF); 3964 3965 return(error); 3966} 3967 3968static int 3969cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 3970{ 3971 union ccb *ccb; 3972 u_int8_t *databuf; 3973 u_int32_t address; 3974 int error; 3975 int length; 3976 3977 error = 0; 3978 databuf = NULL; 3979 /* The address is reserved for many of the formats */ 3980 address = 0; 3981 3982 switch(dvdstruct->format) { 3983 case DVD_STRUCT_PHYSICAL: 3984 length = sizeof(struct scsi_read_dvd_struct_data_physical); 3985 break; 3986 case DVD_STRUCT_COPYRIGHT: 3987 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 3988 break; 3989 case DVD_STRUCT_DISCKEY: 3990 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 3991 break; 3992 case DVD_STRUCT_BCA: 3993 length = sizeof(struct scsi_read_dvd_struct_data_bca); 3994 break; 3995 case DVD_STRUCT_MANUFACT: 3996 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 3997 break; 3998 case DVD_STRUCT_CMI: 3999 return (ENODEV); 4000 case DVD_STRUCT_PROTDISCID: 4001 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 4002 break; 4003 case DVD_STRUCT_DISCKEYBLOCK: 4004 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 4005 break; 4006 case DVD_STRUCT_DDS: 4007 length = sizeof(struct scsi_read_dvd_struct_data_dds); 4008 break; 4009 case DVD_STRUCT_MEDIUM_STAT: 4010 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 4011 break; 4012 case DVD_STRUCT_SPARE_AREA: 4013 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 4014 break; 4015 case DVD_STRUCT_RMD_LAST: 4016 return (ENODEV); 4017 case DVD_STRUCT_RMD_RMA: 4018 return (ENODEV); 4019 case DVD_STRUCT_PRERECORDED: 4020 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 4021 break; 4022 case DVD_STRUCT_UNIQUEID: 4023 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 4024 break; 4025 case DVD_STRUCT_DCB: 4026 return (ENODEV); 4027 case DVD_STRUCT_LIST: 4028 /* 4029 * This is the maximum allocation length for the READ DVD 4030 * STRUCTURE command. There's nothing in the MMC3 spec 4031 * that indicates a limit in the amount of data that can 4032 * be returned from this call, other than the limits 4033 * imposed by the 2-byte length variables. 4034 */ 4035 length = 65535; 4036 break; 4037 default: 4038 return (EINVAL); 4039 } 4040 4041 if (length != 0) { 4042 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 4043 } else 4044 databuf = NULL; 4045 4046 cam_periph_lock(periph); 4047 ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4048 4049 scsi_read_dvd_structure(&ccb->csio, 4050 /* retries */ cd_retry_count, 4051 /* cbfcnp */ cddone, 4052 /* tag_action */ MSG_SIMPLE_Q_TAG, 4053 /* lba */ address, 4054 /* layer_number */ dvdstruct->layer_num, 4055 /* key_format */ dvdstruct->format, 4056 /* agid */ dvdstruct->agid, 4057 /* data_ptr */ databuf, 4058 /* dxfer_len */ length, 4059 /* sense_len */ SSD_FULL_SIZE, 4060 /* timeout */ 50000); 4061 4062 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 4063 /*sense_flags*/SF_RETRY_UA); 4064 4065 if (error != 0) 4066 goto bailout; 4067 4068 switch(dvdstruct->format) { 4069 case DVD_STRUCT_PHYSICAL: { 4070 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 4071 struct dvd_layer *outlayer; 4072 struct scsi_read_dvd_struct_data_physical *phys_data; 4073 4074 phys_data = 4075 (struct scsi_read_dvd_struct_data_physical *)databuf; 4076 inlayer = &phys_data->layer_desc; 4077 outlayer = (struct dvd_layer *)&dvdstruct->data; 4078 4079 dvdstruct->length = sizeof(*inlayer); 4080 4081 outlayer->book_type = (inlayer->book_type_version & 4082 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 4083 outlayer->book_version = (inlayer->book_type_version & 4084 RDSD_BOOK_VERSION_MASK); 4085 outlayer->disc_size = (inlayer->disc_size_max_rate & 4086 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 4087 outlayer->max_rate = (inlayer->disc_size_max_rate & 4088 RDSD_MAX_RATE_MASK); 4089 outlayer->nlayers = (inlayer->layer_info & 4090 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 4091 outlayer->track_path = (inlayer->layer_info & 4092 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 4093 outlayer->layer_type = (inlayer->layer_info & 4094 RDSD_LAYER_TYPE_MASK); 4095 outlayer->linear_density = (inlayer->density & 4096 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 4097 outlayer->track_density = (inlayer->density & 4098 RDSD_TRACK_DENSITY_MASK); 4099 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 4100 RDSD_BCA_SHIFT; 4101 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 4102 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 4103 outlayer->end_sector_l0 = 4104 scsi_3btoul(inlayer->end_sector_layer0); 4105 break; 4106 } 4107 case DVD_STRUCT_COPYRIGHT: { 4108 struct scsi_read_dvd_struct_data_copyright *copy_data; 4109 4110 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 4111 databuf; 4112 4113 dvdstruct->cpst = copy_data->cps_type; 4114 dvdstruct->rmi = copy_data->region_info; 4115 dvdstruct->length = 0; 4116 4117 break; 4118 } 4119 default: 4120 /* 4121 * Tell the user what the overall length is, no matter 4122 * what we can actually fit in the data buffer. 4123 */ 4124 dvdstruct->length = length - ccb->csio.resid - 4125 sizeof(struct scsi_read_dvd_struct_data_header); 4126 4127 /* 4128 * But only actually copy out the smaller of what we read 4129 * in or what the structure can take. 4130 */ 4131 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 4132 dvdstruct->data, 4133 min(sizeof(dvdstruct->data), dvdstruct->length)); 4134 break; 4135 } 4136 4137bailout: 4138 xpt_release_ccb(ccb); 4139 cam_periph_unlock(periph); 4140 4141 if (databuf != NULL) 4142 free(databuf, M_DEVBUF); 4143 4144 return(error); 4145} 4146 4147void 4148scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 4149 void (*cbfcnp)(struct cam_periph *, union ccb *), 4150 u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 4151 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 4152 u_int8_t sense_len, u_int32_t timeout) 4153{ 4154 struct scsi_report_key *scsi_cmd; 4155 4156 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 4157 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4158 scsi_cmd->opcode = REPORT_KEY; 4159 scsi_ulto4b(lba, scsi_cmd->lba); 4160 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4161 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4162 (key_format & RK_KF_KEYFORMAT_MASK); 4163 4164 cam_fill_csio(csio, 4165 retries, 4166 cbfcnp, 4167 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 4168 tag_action, 4169 /*data_ptr*/ data_ptr, 4170 /*dxfer_len*/ dxfer_len, 4171 sense_len, 4172 sizeof(*scsi_cmd), 4173 timeout); 4174} 4175 4176void 4177scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 4178 void (*cbfcnp)(struct cam_periph *, union ccb *), 4179 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 4180 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 4181 u_int32_t timeout) 4182{ 4183 struct scsi_send_key *scsi_cmd; 4184 4185 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 4186 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4187 scsi_cmd->opcode = SEND_KEY; 4188 4189 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 4190 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 4191 (key_format & RK_KF_KEYFORMAT_MASK); 4192 4193 cam_fill_csio(csio, 4194 retries, 4195 cbfcnp, 4196 /*flags*/ CAM_DIR_OUT, 4197 tag_action, 4198 /*data_ptr*/ data_ptr, 4199 /*dxfer_len*/ dxfer_len, 4200 sense_len, 4201 sizeof(*scsi_cmd), 4202 timeout); 4203} 4204 4205 4206void 4207scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 4208 void (*cbfcnp)(struct cam_periph *, union ccb *), 4209 u_int8_t tag_action, u_int32_t address, 4210 u_int8_t layer_number, u_int8_t format, u_int8_t agid, 4211 u_int8_t *data_ptr, u_int32_t dxfer_len, 4212 u_int8_t sense_len, u_int32_t timeout) 4213{ 4214 struct scsi_read_dvd_structure *scsi_cmd; 4215 4216 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 4217 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4218 scsi_cmd->opcode = READ_DVD_STRUCTURE; 4219 4220 scsi_ulto4b(address, scsi_cmd->address); 4221 scsi_cmd->layer_number = layer_number; 4222 scsi_cmd->format = format; 4223 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4224 /* The AGID is the top two bits of this byte */ 4225 scsi_cmd->agid = agid << 6; 4226 4227 cam_fill_csio(csio, 4228 retries, 4229 cbfcnp, 4230 /*flags*/ CAM_DIR_IN, 4231 tag_action, 4232 /*data_ptr*/ data_ptr, 4233 /*dxfer_len*/ dxfer_len, 4234 sense_len, 4235 sizeof(*scsi_cmd), 4236 timeout); 4237} 4238