scsi_cd.c revision 221585
1139743Simp/*- 239213Sgibbs * Copyright (c) 1997 Justin T. Gibbs. 3111206Sken * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry. 439213Sgibbs * All rights reserved. 539213Sgibbs * 639213Sgibbs * Redistribution and use in source and binary forms, with or without 739213Sgibbs * modification, are permitted provided that the following conditions 839213Sgibbs * are met: 939213Sgibbs * 1. Redistributions of source code must retain the above copyright 1039213Sgibbs * notice, this list of conditions, and the following disclaimer, 1139213Sgibbs * without modification, immediately at the beginning of the file. 1239213Sgibbs * 2. The name of the author may not be used to endorse or promote products 1339213Sgibbs * derived from this software without specific prior written permission. 1439213Sgibbs * 1539213Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1639213Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1739213Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1839213Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 1939213Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2039213Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2139213Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2239213Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2339213Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2439213Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2539213Sgibbs * SUCH DAMAGE. 2639213Sgibbs */ 27116162Sobrien 28139743Simp/*- 2939213Sgibbs * Portions of this driver taken from the original FreeBSD cd driver. 3039213Sgibbs * Written by Julian Elischer (julian@tfs.com) 3139213Sgibbs * for TRW Financial Systems for use under the MACH(2.5) operating system. 3239213Sgibbs * 3339213Sgibbs * TRW Financial Systems, in accordance with their agreement with Carnegie 3439213Sgibbs * Mellon University, makes this software available to CMU to distribute 3539213Sgibbs * or use in any manner that they see fit as long as this message is kept with 3639213Sgibbs * the software. For this reason TFS also grants any other persons or 3739213Sgibbs * organisations permission to use or modify this software. 3839213Sgibbs * 3939213Sgibbs * TFS supplies this software to be publicly redistributed 4039213Sgibbs * on the understanding that TFS is not responsible for the correct 4139213Sgibbs * functioning of this software in any circumstances. 4239213Sgibbs * 4339213Sgibbs * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 4439213Sgibbs * 4539213Sgibbs * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $ 4639213Sgibbs */ 4739213Sgibbs 48116162Sobrien#include <sys/cdefs.h> 49116162Sobrien__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_cd.c 221585 2011-05-07 10:06:43Z avg $"); 50116162Sobrien 5140020Sken#include "opt_cd.h" 5239213Sgibbs 5339213Sgibbs#include <sys/param.h> 5439213Sgibbs#include <sys/systm.h> 5539213Sgibbs#include <sys/kernel.h> 5660041Sphk#include <sys/bio.h> 5751836Sphk#include <sys/conf.h> 5851836Sphk#include <sys/disk.h> 5939213Sgibbs#include <sys/malloc.h> 6039213Sgibbs#include <sys/cdio.h> 61105421Snjl#include <sys/cdrio.h> 6260422Sken#include <sys/dvdio.h> 6339213Sgibbs#include <sys/devicestat.h> 6439213Sgibbs#include <sys/sysctl.h> 65119708Sken#include <sys/taskqueue.h> 66120599Sphk#include <geom/geom_disk.h> 6739213Sgibbs 6839213Sgibbs#include <cam/cam.h> 6939213Sgibbs#include <cam/cam_ccb.h> 7039213Sgibbs#include <cam/cam_periph.h> 7139213Sgibbs#include <cam/cam_xpt_periph.h> 7239213Sgibbs#include <cam/cam_queue.h> 73168752Sscottl#include <cam/cam_sim.h> 7439213Sgibbs 7539213Sgibbs#include <cam/scsi/scsi_message.h> 7639213Sgibbs#include <cam/scsi/scsi_da.h> 7739213Sgibbs#include <cam/scsi/scsi_cd.h> 7839213Sgibbs 7939213Sgibbs#define LEADOUT 0xaa /* leadout toc entry */ 8039213Sgibbs 8139213Sgibbsstruct cd_params { 8239213Sgibbs u_int32_t blksize; 8339213Sgibbs u_long disksize; 8439213Sgibbs}; 8539213Sgibbs 8639213Sgibbstypedef enum { 87111206Sken CD_Q_NONE = 0x00, 88111206Sken CD_Q_NO_TOUCH = 0x01, 89111206Sken CD_Q_BCD_TRACKS = 0x02, 90111206Sken CD_Q_NO_CHANGER = 0x04, 91111206Sken CD_Q_CHANGER = 0x08, 92111206Sken CD_Q_10_BYTE_ONLY = 0x10 9339213Sgibbs} cd_quirks; 9439213Sgibbs 9539213Sgibbstypedef enum { 96120884Sthomas CD_FLAG_INVALID = 0x0001, 97120884Sthomas CD_FLAG_NEW_DISC = 0x0002, 98120884Sthomas CD_FLAG_DISC_LOCKED = 0x0004, 99120884Sthomas CD_FLAG_DISC_REMOVABLE = 0x0008, 100120884Sthomas CD_FLAG_TAGGED_QUEUING = 0x0010, 101120884Sthomas CD_FLAG_CHANGER = 0x0040, 102120884Sthomas CD_FLAG_ACTIVE = 0x0080, 103120884Sthomas CD_FLAG_SCHED_ON_COMP = 0x0100, 104120884Sthomas CD_FLAG_RETRY_UA = 0x0200, 105120884Sthomas CD_FLAG_VALID_MEDIA = 0x0400, 106120884Sthomas CD_FLAG_VALID_TOC = 0x0800, 107168752Sscottl CD_FLAG_SCTX_INIT = 0x1000, 108168752Sscottl CD_FLAG_OPEN = 0x2000 10939213Sgibbs} cd_flags; 11039213Sgibbs 11139213Sgibbstypedef enum { 11239213Sgibbs CD_CCB_PROBE = 0x01, 11339213Sgibbs CD_CCB_BUFFER_IO = 0x02, 11439213Sgibbs CD_CCB_WAITING = 0x03, 11539213Sgibbs CD_CCB_TYPE_MASK = 0x0F, 11639213Sgibbs CD_CCB_RETRY_UA = 0x10 11739213Sgibbs} cd_ccb_state; 11839213Sgibbs 11939213Sgibbstypedef enum { 12039213Sgibbs CHANGER_TIMEOUT_SCHED = 0x01, 12139213Sgibbs CHANGER_SHORT_TMOUT_SCHED = 0x02, 12239213Sgibbs CHANGER_MANUAL_CALL = 0x04, 12339213Sgibbs CHANGER_NEED_TIMEOUT = 0x08 12439213Sgibbs} cd_changer_flags; 12539213Sgibbs 12639213Sgibbs#define ccb_state ppriv_field0 12739213Sgibbs#define ccb_bp ppriv_ptr1 12839213Sgibbs 129111206Skenstruct cd_tocdata { 130111206Sken struct ioc_toc_header header; 131111206Sken struct cd_toc_entry entries[100]; 132111206Sken}; 133111206Sken 134111206Skenstruct cd_toc_single { 135111206Sken struct ioc_toc_header header; 136111206Sken struct cd_toc_entry entry; 137111206Sken}; 138111206Sken 13939213Sgibbstypedef enum { 14039213Sgibbs CD_STATE_PROBE, 14139213Sgibbs CD_STATE_NORMAL 14239213Sgibbs} cd_state; 14339213Sgibbs 14439213Sgibbsstruct cd_softc { 14539213Sgibbs cam_pinfo pinfo; 14639213Sgibbs cd_state state; 14746581Sken volatile cd_flags flags; 14859249Sphk struct bio_queue_head bio_queue; 14960938Sjake LIST_HEAD(, ccb_hdr) pending_ccbs; 15039213Sgibbs struct cd_params params; 15139213Sgibbs union ccb saved_ccb; 15239213Sgibbs cd_quirks quirks; 15360938Sjake STAILQ_ENTRY(cd_softc) changer_links; 15439213Sgibbs struct cdchanger *changer; 15539213Sgibbs int bufs_left; 15639213Sgibbs struct cam_periph *periph; 157111206Sken int minimum_command_size; 158112262Sphk int outstanding_cmds; 159119708Sken struct task sysctl_task; 160111206Sken struct sysctl_ctx_list sysctl_ctx; 161111206Sken struct sysctl_oid *sysctl_tree; 162111206Sken STAILQ_HEAD(, cd_mode_params) mode_queue; 163111206Sken struct cd_tocdata toc; 164125975Sphk struct disk *disk; 16539213Sgibbs}; 16639213Sgibbs 167111206Skenstruct cd_page_sizes { 168111206Sken int page; 169111206Sken int page_size; 170111206Sken}; 171111206Sken 172111206Skenstatic struct cd_page_sizes cd_page_size_table[] = 173111206Sken{ 174111206Sken { AUDIO_PAGE, sizeof(struct cd_audio_page)} 175111206Sken}; 176111206Sken 17739213Sgibbsstruct cd_quirk_entry { 17839213Sgibbs struct scsi_inquiry_pattern inq_pat; 17939213Sgibbs cd_quirks quirks; 18039213Sgibbs}; 18139213Sgibbs 18239213Sgibbs/* 183111206Sken * The changer quirk entries aren't strictly necessary. Basically, what 184111206Sken * they do is tell cdregister() up front that a device is a changer. 185111206Sken * Otherwise, it will figure that fact out once it sees a LUN on the device 186111206Sken * that is greater than 0. If it is known up front that a device is a changer, 187111206Sken * all I/O to the device will go through the changer scheduling routines, as 18839213Sgibbs * opposed to the "normal" CD code. 189111206Sken * 190111206Sken * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 191111206Sken * your device hangs when it gets a 10 byte command. Adding a quirk just 192111206Sken * to get rid of the informative diagnostic message is not acceptable. All 193111206Sken * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 194111206Sken * referenced in a comment along with the quirk) , and must be approved by 195111206Sken * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 196111206Sken * be removed until the submitter can explain why they are needed. 197111206Sken * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 198111206Sken * when the CAM_NEW_TRAN_CODE work is done. 19939213Sgibbs */ 20039213Sgibbsstatic struct cd_quirk_entry cd_quirk_table[] = 20139213Sgibbs{ 20239213Sgibbs { 20339213Sgibbs { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 20439213Sgibbs /*quirks*/ CD_Q_CHANGER 20539213Sgibbs }, 20639213Sgibbs { 20754451Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*", 20839213Sgibbs "*"}, /* quirks */ CD_Q_CHANGER 20940262Sken }, 21040262Sken { 21167752Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"}, 21267752Sken /* quirks */ CD_Q_CHANGER 21367752Sken }, 21467752Sken { 21540262Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 21640262Sken /* quirks */ CD_Q_BCD_TRACKS 21739213Sgibbs } 21839213Sgibbs}; 21939213Sgibbs 220120599Sphkstatic disk_open_t cdopen; 221120599Sphkstatic disk_close_t cdclose; 222120599Sphkstatic disk_ioctl_t cdioctl; 223120599Sphkstatic disk_strategy_t cdstrategy; 22439213Sgibbs 22539213Sgibbsstatic periph_init_t cdinit; 22639213Sgibbsstatic periph_ctor_t cdregister; 22739213Sgibbsstatic periph_dtor_t cdcleanup; 22839213Sgibbsstatic periph_start_t cdstart; 22940603Skenstatic periph_oninv_t cdoninvalidate; 23039213Sgibbsstatic void cdasync(void *callback_arg, u_int32_t code, 23139213Sgibbs struct cam_path *path, void *arg); 232111206Skenstatic int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 23339213Sgibbsstatic void cdshorttimeout(void *arg); 23439213Sgibbsstatic void cdschedule(struct cam_periph *periph, int priority); 23539213Sgibbsstatic void cdrunchangerqueue(void *arg); 23639213Sgibbsstatic void cdchangerschedule(struct cd_softc *softc); 23739213Sgibbsstatic int cdrunccb(union ccb *ccb, 23839213Sgibbs int (*error_routine)(union ccb *ccb, 23939213Sgibbs u_int32_t cam_flags, 24039213Sgibbs u_int32_t sense_flags), 24139213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags); 242111206Skenstatic union ccb *cdgetccb(struct cam_periph *periph, 24339213Sgibbs u_int32_t priority); 24439213Sgibbsstatic void cddone(struct cam_periph *periph, 24539213Sgibbs union ccb *start_ccb); 246111206Skenstatic union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 247111206Skenstatic int cdgetpagesize(int page_num); 248111206Skenstatic void cdprevent(struct cam_periph *periph, int action); 249111206Skenstatic int cdcheckmedia(struct cam_periph *periph); 250111206Skenstatic int cdsize(struct cam_periph *periph, u_int32_t *size); 251111206Skenstatic int cd6byteworkaround(union ccb *ccb); 25239213Sgibbsstatic int cderror(union ccb *ccb, u_int32_t cam_flags, 25339213Sgibbs u_int32_t sense_flags); 25439213Sgibbsstatic int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 255111206Sken u_int32_t start, u_int8_t *data, 256111206Sken u_int32_t len, u_int32_t sense_flags); 25739213Sgibbsstatic int cdgetmode(struct cam_periph *periph, 258111206Sken struct cd_mode_params *data, u_int32_t page); 25939213Sgibbsstatic int cdsetmode(struct cam_periph *periph, 260111206Sken struct cd_mode_params *data); 26139213Sgibbsstatic int cdplay(struct cam_periph *periph, u_int32_t blk, 26239213Sgibbs u_int32_t len); 26339213Sgibbsstatic int cdreadsubchannel(struct cam_periph *periph, 26439213Sgibbs u_int32_t mode, u_int32_t format, 26539213Sgibbs int track, 26639213Sgibbs struct cd_sub_channel_info *data, 26739213Sgibbs u_int32_t len); 26839213Sgibbsstatic int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 26939213Sgibbs u_int32_t starts, u_int32_t startf, 27039213Sgibbs u_int32_t endm, u_int32_t ends, 27139213Sgibbs u_int32_t endf); 27239213Sgibbsstatic int cdplaytracks(struct cam_periph *periph, 27339213Sgibbs u_int32_t strack, u_int32_t sindex, 27439213Sgibbs u_int32_t etrack, u_int32_t eindex); 27539213Sgibbsstatic int cdpause(struct cam_periph *periph, u_int32_t go); 27639213Sgibbsstatic int cdstopunit(struct cam_periph *periph, u_int32_t eject); 277111206Skenstatic int cdstartunit(struct cam_periph *periph, int load); 278105421Snjlstatic int cdsetspeed(struct cam_periph *periph, 279105421Snjl u_int32_t rdspeed, u_int32_t wrspeed); 28060422Skenstatic int cdreportkey(struct cam_periph *periph, 28160422Sken struct dvd_authinfo *authinfo); 28260422Skenstatic int cdsendkey(struct cam_periph *periph, 28360422Sken struct dvd_authinfo *authinfo); 28460422Skenstatic int cdreaddvdstructure(struct cam_periph *periph, 28560422Sken struct dvd_struct *dvdstruct); 28639213Sgibbs 28739213Sgibbsstatic struct periph_driver cddriver = 28839213Sgibbs{ 28939213Sgibbs cdinit, "cd", 29039213Sgibbs TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 29139213Sgibbs}; 29239213Sgibbs 29372119SpeterPERIPHDRIVER_DECLARE(cd, cddriver); 29439213Sgibbs 295186882Simp#ifndef CD_DEFAULT_RETRY 296186882Simp#define CD_DEFAULT_RETRY 4 297186882Simp#endif 29839213Sgibbs#ifndef CHANGER_MIN_BUSY_SECONDS 29946747Sken#define CHANGER_MIN_BUSY_SECONDS 5 30039213Sgibbs#endif 30139213Sgibbs#ifndef CHANGER_MAX_BUSY_SECONDS 30246747Sken#define CHANGER_MAX_BUSY_SECONDS 15 30339213Sgibbs#endif 30439213Sgibbs 305186882Simpstatic int cd_retry_count = CD_DEFAULT_RETRY; 30639213Sgibbsstatic int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 30739213Sgibbsstatic int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 30839213Sgibbs 30939213SgibbsSYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 31039213SgibbsSYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 311186882SimpSYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW, 312186882Simp &cd_retry_count, 0, "Normal I/O retry count"); 313186882SimpTUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count); 31439213SgibbsSYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 31539213Sgibbs &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 316111206SkenTUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds); 31739213SgibbsSYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 31839213Sgibbs &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 319111206SkenTUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds); 32039213Sgibbs 32139213Sgibbsstruct cdchanger { 32239213Sgibbs path_id_t path_id; 32339213Sgibbs target_id_t target_id; 32439213Sgibbs int num_devices; 32539213Sgibbs struct camq devq; 32639213Sgibbs struct timeval start_time; 32739213Sgibbs struct cd_softc *cur_device; 328168752Sscottl struct callout short_handle; 329168752Sscottl struct callout long_handle; 33046581Sken volatile cd_changer_flags flags; 33160938Sjake STAILQ_ENTRY(cdchanger) changer_links; 33260938Sjake STAILQ_HEAD(chdevlist, cd_softc) chluns; 33339213Sgibbs}; 33439213Sgibbs 335168752Sscottlstatic struct mtx changerq_mtx; 33660938Sjakestatic STAILQ_HEAD(changerlist, cdchanger) changerq; 337168752Sscottlstatic int num_changers; 33839213Sgibbs 339169562SscottlMALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers"); 340169562Sscottl 341104880Sphkstatic void 34239213Sgibbscdinit(void) 34339213Sgibbs{ 34439213Sgibbs cam_status status; 34539213Sgibbs 346168752Sscottl mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF); 347168752Sscottl STAILQ_INIT(&changerq); 348168752Sscottl 34939213Sgibbs /* 35039213Sgibbs * Install a global async callback. This callback will 35139213Sgibbs * receive async callbacks like "new device found". 35239213Sgibbs */ 353169605Sscottl status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL); 35439213Sgibbs 35539213Sgibbs if (status != CAM_REQ_CMP) { 35639213Sgibbs printf("cd: Failed to attach master async callback " 35739213Sgibbs "due to status 0x%x!\n", status); 35839213Sgibbs } 35939213Sgibbs} 36039213Sgibbs 36139213Sgibbsstatic void 36240603Skencdoninvalidate(struct cam_periph *periph) 36340603Sken{ 36440603Sken struct cd_softc *softc; 36540603Sken 36640603Sken softc = (struct cd_softc *)periph->softc; 36740603Sken 36840603Sken /* 36940603Sken * De-register any async callbacks. 37040603Sken */ 371169605Sscottl xpt_register_async(0, cdasync, periph, periph->path); 37240603Sken 37340603Sken softc->flags |= CD_FLAG_INVALID; 37440603Sken 37540603Sken /* 37640603Sken * Return all queued I/O with ENXIO. 37740603Sken * XXX Handle any transactions queued to the card 37840603Sken * with XPT_ABORT_CCB. 37940603Sken */ 380112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 38140603Sken 38240603Sken /* 38340603Sken * If this device is part of a changer, and it was scheduled 38440603Sken * to run, remove it from the run queue since we just nuked 38540603Sken * all of its scheduled I/O. 38640603Sken */ 38740603Sken if ((softc->flags & CD_FLAG_CHANGER) 38840603Sken && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 38940603Sken camq_remove(&softc->changer->devq, softc->pinfo.index); 39040603Sken 391152565Sjdp disk_gone(softc->disk); 392164906Smjacob xpt_print(periph->path, "lost device\n"); 39340603Sken} 39440603Sken 39540603Skenstatic void 39639213Sgibbscdcleanup(struct cam_periph *periph) 39739213Sgibbs{ 39839213Sgibbs struct cd_softc *softc; 39939213Sgibbs 40039213Sgibbs softc = (struct cd_softc *)periph->softc; 40139213Sgibbs 402164906Smjacob xpt_print(periph->path, "removing device entry\n"); 40340603Sken 40439213Sgibbs /* 40539213Sgibbs * In the queued, non-active case, the device in question 40639213Sgibbs * has already been removed from the changer run queue. Since this 40739213Sgibbs * device is active, we need to de-activate it, and schedule 40839213Sgibbs * another device to run. (if there is another one to run) 40939213Sgibbs */ 41039213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 41139213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 41239213Sgibbs 41339213Sgibbs /* 41439213Sgibbs * The purpose of the short timeout is soley to determine 41539213Sgibbs * whether the current device has finished or not. Well, 41639213Sgibbs * since we're removing the active device, we know that it 41739213Sgibbs * is finished. So, get rid of the short timeout. 41839213Sgibbs * Otherwise, if we're in the time period before the short 41939213Sgibbs * timeout fires, and there are no other devices in the 42039213Sgibbs * queue to run, there won't be any other device put in the 42139213Sgibbs * active slot. i.e., when we call cdrunchangerqueue() 42239213Sgibbs * below, it won't do anything. Then, when the short 42339213Sgibbs * timeout fires, it'll look at the "current device", which 42439213Sgibbs * we are free below, and possibly panic the kernel on a 42539213Sgibbs * bogus pointer reference. 42639213Sgibbs * 42739213Sgibbs * The long timeout doesn't really matter, since we 42839213Sgibbs * decrement the qfrozen_cnt to indicate that there is 42939213Sgibbs * nothing in the active slot now. Therefore, there won't 43039213Sgibbs * be any bogus pointer references there. 43139213Sgibbs */ 43239213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 433168752Sscottl callout_stop(&softc->changer->short_handle); 43439213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 43539213Sgibbs } 436203108Smav softc->changer->devq.qfrozen_cnt[0]--; 43739213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 43839213Sgibbs cdrunchangerqueue(softc->changer); 43939213Sgibbs } 44039213Sgibbs 44139213Sgibbs /* 44239213Sgibbs * If we're removing the last device on the changer, go ahead and 44339213Sgibbs * remove the changer device structure. 44439213Sgibbs */ 44539213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 44639213Sgibbs && (--softc->changer->num_devices == 0)) { 44739213Sgibbs 44839213Sgibbs /* 44939213Sgibbs * Theoretically, there shouldn't be any timeouts left, but 45039213Sgibbs * I'm not completely sure that that will be the case. So, 45139213Sgibbs * it won't hurt to check and see if there are any left. 45239213Sgibbs */ 45339213Sgibbs if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 454168752Sscottl callout_stop(&softc->changer->long_handle); 45539213Sgibbs softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 45639213Sgibbs } 45739213Sgibbs 45839213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 459168752Sscottl callout_stop(&softc->changer->short_handle); 46039213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 46139213Sgibbs } 46239213Sgibbs 463168752Sscottl mtx_lock(&changerq_mtx); 46460938Sjake STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 46539213Sgibbs changer_links); 466168752Sscottl num_changers--; 467168752Sscottl mtx_unlock(&changerq_mtx); 468164906Smjacob xpt_print(periph->path, "removing changer entry\n"); 46939213Sgibbs free(softc->changer, M_DEVBUF); 47039213Sgibbs } 471168786Sscottl cam_periph_unlock(periph); 472188503Sjhb if ((softc->flags & CD_FLAG_SCTX_INIT) != 0 473188503Sjhb && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 474188503Sjhb xpt_print(periph->path, "can't remove sysctl context\n"); 475188503Sjhb } 476188503Sjhb 477125975Sphk disk_destroy(softc->disk); 478188503Sjhb free(softc, M_DEVBUF); 479168786Sscottl cam_periph_lock(periph); 48039213Sgibbs} 48139213Sgibbs 48239213Sgibbsstatic void 48339213Sgibbscdasync(void *callback_arg, u_int32_t code, 48439213Sgibbs struct cam_path *path, void *arg) 48539213Sgibbs{ 48639213Sgibbs struct cam_periph *periph; 48739213Sgibbs 48839213Sgibbs periph = (struct cam_periph *)callback_arg; 48939213Sgibbs switch (code) { 49039213Sgibbs case AC_FOUND_DEVICE: 49139213Sgibbs { 49239213Sgibbs struct ccb_getdev *cgd; 49339213Sgibbs cam_status status; 49439213Sgibbs 49539213Sgibbs cgd = (struct ccb_getdev *)arg; 49679177Smjacob if (cgd == NULL) 49779177Smjacob break; 49839213Sgibbs 499195534Sscottl if (cgd->protocol != PROTO_SCSI) 500195534Sscottl break; 501195534Sscottl 50256148Smjacob if (SID_TYPE(&cgd->inq_data) != T_CDROM 50356148Smjacob && SID_TYPE(&cgd->inq_data) != T_WORM) 50439213Sgibbs break; 50539213Sgibbs 50639213Sgibbs /* 50739213Sgibbs * Allocate a peripheral instance for 50839213Sgibbs * this device and start the probe 50939213Sgibbs * process. 51039213Sgibbs */ 51140603Sken status = cam_periph_alloc(cdregister, cdoninvalidate, 51240603Sken cdcleanup, cdstart, 51340603Sken "cd", CAM_PERIPH_BIO, 51440603Sken cgd->ccb_h.path, cdasync, 51540603Sken AC_FOUND_DEVICE, cgd); 51639213Sgibbs 51739213Sgibbs if (status != CAM_REQ_CMP 51839213Sgibbs && status != CAM_REQ_INPROG) 51939213Sgibbs printf("cdasync: Unable to attach new device " 52039213Sgibbs "due to status 0x%x\n", status); 52139213Sgibbs 52239213Sgibbs break; 52339213Sgibbs } 52439213Sgibbs case AC_SENT_BDR: 52539213Sgibbs case AC_BUS_RESET: 52639213Sgibbs { 52739213Sgibbs struct cd_softc *softc; 52839213Sgibbs struct ccb_hdr *ccbh; 52939213Sgibbs 53039213Sgibbs softc = (struct cd_softc *)periph->softc; 53139213Sgibbs /* 53239213Sgibbs * Don't fail on the expected unit attention 53339213Sgibbs * that will occur. 53439213Sgibbs */ 53539213Sgibbs softc->flags |= CD_FLAG_RETRY_UA; 53671999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 53739213Sgibbs ccbh->ccb_state |= CD_CCB_RETRY_UA; 53847413Sgibbs /* FALLTHROUGH */ 53939213Sgibbs } 54039213Sgibbs default: 54147413Sgibbs cam_periph_async(periph, code, path, arg); 54239213Sgibbs break; 54339213Sgibbs } 54439213Sgibbs} 54539213Sgibbs 546119708Skenstatic void 547119708Skencdsysctlinit(void *context, int pending) 548119708Sken{ 549119708Sken struct cam_periph *periph; 550119708Sken struct cd_softc *softc; 551119708Sken char tmpstr[80], tmpstr2[80]; 552119708Sken 553119708Sken periph = (struct cam_periph *)context; 554168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 555168752Sscottl return; 556168752Sscottl 557119708Sken softc = (struct cd_softc *)periph->softc; 558119708Sken snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 559119708Sken snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 560119708Sken 561119708Sken sysctl_ctx_init(&softc->sysctl_ctx); 562120884Sthomas softc->flags |= CD_FLAG_SCTX_INIT; 563119708Sken softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 564119708Sken SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 565119708Sken tmpstr2, CTLFLAG_RD, 0, tmpstr); 566119708Sken 567119708Sken if (softc->sysctl_tree == NULL) { 568119708Sken printf("cdsysctlinit: unable to allocate sysctl tree\n"); 569168752Sscottl cam_periph_release(periph); 570119708Sken return; 571119708Sken } 572119708Sken 573119708Sken /* 574119708Sken * Now register the sysctl handler, so the user can the value on 575119708Sken * the fly. 576119708Sken */ 577119708Sken SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 578119708Sken OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 579119708Sken &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 580119708Sken "Minimum CDB size"); 581119708Sken 582168752Sscottl cam_periph_release(periph); 583119708Sken} 584119708Sken 585111206Sken/* 586111206Sken * We have a handler function for this so we can check the values when the 587111206Sken * user sets them, instead of every time we look at them. 588111206Sken */ 589111206Skenstatic int 590111206Skencdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 591111206Sken{ 592111206Sken int error, value; 593111206Sken 594111206Sken value = *(int *)arg1; 595111206Sken 596111206Sken error = sysctl_handle_int(oidp, &value, 0, req); 597111206Sken 598111206Sken if ((error != 0) 599111206Sken || (req->newptr == NULL)) 600111206Sken return (error); 601111206Sken 602111206Sken /* 603111206Sken * The only real values we can have here are 6 or 10. I don't 604111206Sken * really forsee having 12 be an option at any time in the future. 605111206Sken * So if the user sets something less than or equal to 6, we'll set 606111206Sken * it to 6. If he sets something greater than 6, we'll set it to 10. 607111206Sken * 608111206Sken * I suppose we could just return an error here for the wrong values, 609111206Sken * but I don't think it's necessary to do so, as long as we can 610111206Sken * determine the user's intent without too much trouble. 611111206Sken */ 612111206Sken if (value < 6) 613111206Sken value = 6; 614111206Sken else if (value > 6) 615111206Sken value = 10; 616111206Sken 617111206Sken *(int *)arg1 = value; 618111206Sken 619111206Sken return (0); 620111206Sken} 621111206Sken 62239213Sgibbsstatic cam_status 62339213Sgibbscdregister(struct cam_periph *periph, void *arg) 62439213Sgibbs{ 62539213Sgibbs struct cd_softc *softc; 626118105Snjl struct ccb_pathinq cpi; 62739213Sgibbs struct ccb_getdev *cgd; 628119708Sken char tmpstr[80]; 62939213Sgibbs caddr_t match; 63039213Sgibbs 63139213Sgibbs cgd = (struct ccb_getdev *)arg; 63239213Sgibbs if (periph == NULL) { 63339213Sgibbs printf("cdregister: periph was NULL!!\n"); 63439213Sgibbs return(CAM_REQ_CMP_ERR); 63539213Sgibbs } 63639213Sgibbs if (cgd == NULL) { 63739213Sgibbs printf("cdregister: no getdev CCB, can't register device\n"); 63839213Sgibbs return(CAM_REQ_CMP_ERR); 63939213Sgibbs } 64039213Sgibbs 641203931Smav softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF, 642203931Smav M_NOWAIT | M_ZERO); 64339213Sgibbs if (softc == NULL) { 64439213Sgibbs printf("cdregister: Unable to probe new device. " 64539213Sgibbs "Unable to allocate softc\n"); 64639213Sgibbs return(CAM_REQ_CMP_ERR); 64739213Sgibbs } 64839213Sgibbs 64939213Sgibbs LIST_INIT(&softc->pending_ccbs); 650111206Sken STAILQ_INIT(&softc->mode_queue); 65139213Sgibbs softc->state = CD_STATE_PROBE; 65259249Sphk bioq_init(&softc->bio_queue); 65339213Sgibbs if (SID_IS_REMOVABLE(&cgd->inq_data)) 65439213Sgibbs softc->flags |= CD_FLAG_DISC_REMOVABLE; 65539213Sgibbs if ((cgd->inq_data.flags & SID_CmdQue) != 0) 65639213Sgibbs softc->flags |= CD_FLAG_TAGGED_QUEUING; 65739213Sgibbs 65839213Sgibbs periph->softc = softc; 65939213Sgibbs softc->periph = periph; 66039213Sgibbs 66139213Sgibbs /* 66239213Sgibbs * See if this device has any quirks. 66339213Sgibbs */ 66439213Sgibbs match = cam_quirkmatch((caddr_t)&cgd->inq_data, 66539213Sgibbs (caddr_t)cd_quirk_table, 66639213Sgibbs sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 66739213Sgibbs sizeof(*cd_quirk_table), scsi_inquiry_match); 66839213Sgibbs 66939213Sgibbs if (match != NULL) 67039213Sgibbs softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 67139213Sgibbs else 67239213Sgibbs softc->quirks = CD_Q_NONE; 67339213Sgibbs 674118105Snjl /* Check if the SIM does not want 6 byte commands */ 675200295Smav bzero(&cpi, sizeof(cpi)); 676198382Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 677118105Snjl cpi.ccb_h.func_code = XPT_PATH_INQ; 678118105Snjl xpt_action((union ccb *)&cpi); 679118105Snjl if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 680118105Snjl softc->quirks |= CD_Q_10_BYTE_ONLY; 681118105Snjl 682119708Sken TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph); 683111206Sken 684111206Sken /* The default is 6 byte commands, unless quirked otherwise */ 685111206Sken if (softc->quirks & CD_Q_10_BYTE_ONLY) 686111206Sken softc->minimum_command_size = 10; 687111206Sken else 688111206Sken softc->minimum_command_size = 6; 689111206Sken 690220686Sjh (void)cam_periph_hold(periph, PRIBIO); 691220686Sjh cam_periph_unlock(periph); 69239213Sgibbs /* 693111206Sken * Load the user's default, if any. 694111206Sken */ 695111206Sken snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 696111206Sken periph->unit_number); 697111206Sken TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 698111206Sken 699111206Sken /* 6 and 10 are the only permissible values here. */ 700111206Sken if (softc->minimum_command_size < 6) 701111206Sken softc->minimum_command_size = 6; 702111206Sken else if (softc->minimum_command_size > 6) 703111206Sken softc->minimum_command_size = 10; 704111206Sken 705111206Sken /* 70639213Sgibbs * We need to register the statistics structure for this device, 70739213Sgibbs * but we don't have the blocksize yet for it. So, we register 70839213Sgibbs * the structure and indicate that we don't have the blocksize 70939213Sgibbs * yet. Unlike other SCSI peripheral drivers, we explicitly set 71039213Sgibbs * the device type here to be CDROM, rather than just ORing in 71156148Smjacob * the device type. This is because this driver can attach to either 71239213Sgibbs * CDROM or WORM devices, and we want this peripheral driver to 71339213Sgibbs * show up in the devstat list as a CD peripheral driver, not a 71439213Sgibbs * WORM peripheral driver. WORM drives will also have the WORM 71539213Sgibbs * driver attached to them. 71639213Sgibbs */ 717125975Sphk softc->disk = disk_alloc(); 718220644Smav softc->disk->d_devstat = devstat_new_entry("cd", 71939213Sgibbs periph->unit_number, 0, 720220644Smav DEVSTAT_BS_UNAVAILABLE, 721220644Smav DEVSTAT_TYPE_CDROM | 722220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 72343819Sken DEVSTAT_PRIORITY_CD); 724125975Sphk softc->disk->d_open = cdopen; 725125975Sphk softc->disk->d_close = cdclose; 726125975Sphk softc->disk->d_strategy = cdstrategy; 727125975Sphk softc->disk->d_ioctl = cdioctl; 728125975Sphk softc->disk->d_name = "cd"; 729219056Snwhitehorn cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor, 730219056Snwhitehorn sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr)); 731219056Snwhitehorn strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr)); 732219056Snwhitehorn cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)], 733219056Snwhitehorn cgd->inq_data.product, sizeof(cgd->inq_data.product), 734219056Snwhitehorn sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr)); 735125975Sphk softc->disk->d_unit = periph->unit_number; 736125975Sphk softc->disk->d_drv1 = periph; 737200171Smav if (cpi.maxio == 0) 738200171Smav softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 739200171Smav else if (cpi.maxio > MAXPHYS) 740200171Smav softc->disk->d_maxsize = MAXPHYS; /* for safety */ 741200171Smav else 742200171Smav softc->disk->d_maxsize = cpi.maxio; 743168752Sscottl softc->disk->d_flags = 0; 744210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 745210471Smav softc->disk->d_hba_device = cpi.hba_device; 746210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 747210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 748125975Sphk disk_create(softc->disk, DISK_VERSION); 749168752Sscottl cam_periph_lock(periph); 750220686Sjh cam_periph_unhold(periph); 75139213Sgibbs 75239213Sgibbs /* 75339213Sgibbs * Add an async callback so that we get 75439213Sgibbs * notified if this device goes away. 75539213Sgibbs */ 756169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 757169605Sscottl cdasync, periph, periph->path); 75839213Sgibbs 75939213Sgibbs /* 76039213Sgibbs * If the target lun is greater than 0, we most likely have a CD 76139213Sgibbs * changer device. Check the quirk entries as well, though, just 76239213Sgibbs * in case someone has a CD tower with one lun per drive or 76339213Sgibbs * something like that. Also, if we know up front that a 76439213Sgibbs * particular device is a changer, we can mark it as such starting 76539213Sgibbs * with lun 0, instead of lun 1. It shouldn't be necessary to have 76639213Sgibbs * a quirk entry to define something as a changer, however. 76739213Sgibbs */ 76839213Sgibbs if (((cgd->ccb_h.target_lun > 0) 76939213Sgibbs && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 77039213Sgibbs || ((softc->quirks & CD_Q_CHANGER) != 0)) { 77139213Sgibbs struct cdchanger *nchanger; 77239213Sgibbs struct cam_periph *nperiph; 77339213Sgibbs struct cam_path *path; 77439213Sgibbs cam_status status; 77539213Sgibbs int found; 77639213Sgibbs 77739213Sgibbs /* Set the changer flag in the current device's softc */ 77839213Sgibbs softc->flags |= CD_FLAG_CHANGER; 77939213Sgibbs 78039213Sgibbs /* 78139213Sgibbs * Now, look around for an existing changer device with the 78239213Sgibbs * same path and target ID as the current device. 78339213Sgibbs */ 784168752Sscottl mtx_lock(&changerq_mtx); 78539213Sgibbs for (found = 0, 78639213Sgibbs nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 78739213Sgibbs nchanger != NULL; 78839213Sgibbs nchanger = STAILQ_NEXT(nchanger, changer_links)){ 78939213Sgibbs if ((nchanger->path_id == cgd->ccb_h.path_id) 79039213Sgibbs && (nchanger->target_id == cgd->ccb_h.target_id)) { 79139213Sgibbs found = 1; 79239213Sgibbs break; 79339213Sgibbs } 79439213Sgibbs } 795168752Sscottl mtx_unlock(&changerq_mtx); 79639213Sgibbs 79739213Sgibbs /* 79839213Sgibbs * If we found a matching entry, just add this device to 79939213Sgibbs * the list of devices on this changer. 80039213Sgibbs */ 80139213Sgibbs if (found == 1) { 80239213Sgibbs struct chdevlist *chlunhead; 80339213Sgibbs 80439213Sgibbs chlunhead = &nchanger->chluns; 80539213Sgibbs 80639213Sgibbs /* 80739213Sgibbs * XXX KDM look at consolidating this code with the 80839213Sgibbs * code below in a separate function. 80939213Sgibbs */ 81039213Sgibbs 81139213Sgibbs /* 81239213Sgibbs * Create a path with lun id 0, and see if we can 81339213Sgibbs * find a matching device 81439213Sgibbs */ 81539213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 81639213Sgibbs cgd->ccb_h.path_id, 81739213Sgibbs cgd->ccb_h.target_id, 0); 81839213Sgibbs 81939213Sgibbs if ((status == CAM_REQ_CMP) 82039213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 82139213Sgibbs struct cd_softc *nsoftc; 82239213Sgibbs 82339213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 82439213Sgibbs 82539213Sgibbs if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 82639213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 82739213Sgibbs nchanger->num_devices++; 82839213Sgibbs if (camq_resize(&nchanger->devq, 82939213Sgibbs nchanger->num_devices)!=CAM_REQ_CMP){ 83039213Sgibbs printf("cdregister: " 83139213Sgibbs "camq_resize " 83239213Sgibbs "failed, changer " 83339213Sgibbs "support may " 83439213Sgibbs "be messed up\n"); 83539213Sgibbs } 83639213Sgibbs nsoftc->changer = nchanger; 83739213Sgibbs nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 83839213Sgibbs 83939213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 84039213Sgibbs nsoftc,changer_links); 84139213Sgibbs } 84267928Sken xpt_free_path(path); 84339213Sgibbs } else if (status == CAM_REQ_CMP) 84439213Sgibbs xpt_free_path(path); 84539213Sgibbs else { 84639213Sgibbs printf("cdregister: unable to allocate path\n" 84739213Sgibbs "cdregister: changer support may be " 84839213Sgibbs "broken\n"); 84939213Sgibbs } 85039213Sgibbs 85139213Sgibbs nchanger->num_devices++; 85239213Sgibbs 85339213Sgibbs softc->changer = nchanger; 85439213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 85539213Sgibbs 85639213Sgibbs if (camq_resize(&nchanger->devq, 85739213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 85839213Sgibbs printf("cdregister: camq_resize " 85939213Sgibbs "failed, changer support may " 86039213Sgibbs "be messed up\n"); 86139213Sgibbs } 86239213Sgibbs 86339213Sgibbs STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 86439213Sgibbs } 86539213Sgibbs /* 86639213Sgibbs * In this case, we don't already have an entry for this 86739213Sgibbs * particular changer, so we need to create one, add it to 86839213Sgibbs * the queue, and queue this device on the list for this 86939213Sgibbs * changer. Before we queue this device, however, we need 87039213Sgibbs * to search for lun id 0 on this target, and add it to the 87139213Sgibbs * queue first, if it exists. (and if it hasn't already 87239213Sgibbs * been marked as part of the changer.) 87339213Sgibbs */ 87439213Sgibbs else { 87539213Sgibbs nchanger = malloc(sizeof(struct cdchanger), 876203931Smav M_DEVBUF, M_NOWAIT | M_ZERO); 87739213Sgibbs if (nchanger == NULL) { 87839213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 87939213Sgibbs printf("cdregister: unable to malloc " 88039213Sgibbs "changer structure\ncdregister: " 88139213Sgibbs "changer support disabled\n"); 88239213Sgibbs 88339213Sgibbs /* 88439213Sgibbs * Yes, gotos can be gross but in this case 88539213Sgibbs * I think it's justified.. 88639213Sgibbs */ 88739213Sgibbs goto cdregisterexit; 88839213Sgibbs } 88939213Sgibbs if (camq_init(&nchanger->devq, 1) != 0) { 89039213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 89139213Sgibbs printf("cdregister: changer support " 89239213Sgibbs "disabled\n"); 89339213Sgibbs goto cdregisterexit; 89439213Sgibbs } 89539213Sgibbs 89639213Sgibbs nchanger->path_id = cgd->ccb_h.path_id; 89739213Sgibbs nchanger->target_id = cgd->ccb_h.target_id; 89839213Sgibbs 89939213Sgibbs /* this is superfluous, but it makes things clearer */ 90039213Sgibbs nchanger->num_devices = 0; 90139213Sgibbs 90239213Sgibbs STAILQ_INIT(&nchanger->chluns); 90339213Sgibbs 904168752Sscottl callout_init_mtx(&nchanger->long_handle, 905168752Sscottl periph->sim->mtx, 0); 906168752Sscottl callout_init_mtx(&nchanger->short_handle, 907168752Sscottl periph->sim->mtx, 0); 908168752Sscottl 909168752Sscottl mtx_lock(&changerq_mtx); 910168752Sscottl num_changers++; 91139213Sgibbs STAILQ_INSERT_TAIL(&changerq, nchanger, 91239213Sgibbs changer_links); 913168752Sscottl mtx_unlock(&changerq_mtx); 91439213Sgibbs 91539213Sgibbs /* 91639213Sgibbs * Create a path with lun id 0, and see if we can 91739213Sgibbs * find a matching device 91839213Sgibbs */ 91939213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 92039213Sgibbs cgd->ccb_h.path_id, 92139213Sgibbs cgd->ccb_h.target_id, 0); 92239213Sgibbs 92339213Sgibbs /* 92439213Sgibbs * If we were able to allocate the path, and if we 92539213Sgibbs * find a matching device and it isn't already 92639213Sgibbs * marked as part of a changer, then we add it to 92739213Sgibbs * the current changer. 92839213Sgibbs */ 92939213Sgibbs if ((status == CAM_REQ_CMP) 93039213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL) 93139213Sgibbs && ((((struct cd_softc *)periph->softc)->flags & 93239213Sgibbs CD_FLAG_CHANGER) == 0)) { 93339213Sgibbs struct cd_softc *nsoftc; 93439213Sgibbs 93539213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 93639213Sgibbs 93739213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 93839213Sgibbs nchanger->num_devices++; 93939213Sgibbs if (camq_resize(&nchanger->devq, 94039213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 94139213Sgibbs printf("cdregister: camq_resize " 94239213Sgibbs "failed, changer support may " 94339213Sgibbs "be messed up\n"); 94439213Sgibbs } 94539213Sgibbs nsoftc->changer = nchanger; 94639213Sgibbs nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 94739213Sgibbs 94839213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 94939213Sgibbs nsoftc, changer_links); 95067928Sken xpt_free_path(path); 95139213Sgibbs } else if (status == CAM_REQ_CMP) 95239213Sgibbs xpt_free_path(path); 95339213Sgibbs else { 95439213Sgibbs printf("cdregister: unable to allocate path\n" 95539213Sgibbs "cdregister: changer support may be " 95639213Sgibbs "broken\n"); 95739213Sgibbs } 95839213Sgibbs 95939213Sgibbs softc->changer = nchanger; 96039213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 96139213Sgibbs nchanger->num_devices++; 96239213Sgibbs if (camq_resize(&nchanger->devq, 96339213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 96439213Sgibbs printf("cdregister: camq_resize " 96539213Sgibbs "failed, changer support may " 96639213Sgibbs "be messed up\n"); 96739213Sgibbs } 96839213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 96939213Sgibbs changer_links); 97039213Sgibbs } 97139213Sgibbs } 97239213Sgibbs 97339213Sgibbscdregisterexit: 97439213Sgibbs 975168752Sscottl /* 976168752Sscottl * Refcount and block open attempts until we are setup 977168752Sscottl * Can't block 978168752Sscottl */ 979168752Sscottl (void)cam_periph_hold(periph, PRIBIO); 98039213Sgibbs 98139213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 982203108Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 98339213Sgibbs else 984203108Smav cdschedule(periph, CAM_PRIORITY_DEV); 98539213Sgibbs 98639213Sgibbs return(CAM_REQ_CMP); 98739213Sgibbs} 98839213Sgibbs 98939213Sgibbsstatic int 990120599Sphkcdopen(struct disk *dp) 99139213Sgibbs{ 99239213Sgibbs struct cam_periph *periph; 99339213Sgibbs struct cd_softc *softc; 994101940Snjl int error; 99539213Sgibbs 996120599Sphk periph = (struct cam_periph *)dp->d_drv1; 99739213Sgibbs if (periph == NULL) 99839213Sgibbs return (ENXIO); 99939213Sgibbs 100039213Sgibbs softc = (struct cd_softc *)periph->softc; 100139213Sgibbs 1002168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1003168752Sscottl return(ENXIO); 1004168752Sscottl 1005168752Sscottl cam_periph_lock(periph); 1006168752Sscottl 100740603Sken if (softc->flags & CD_FLAG_INVALID) { 1008168752Sscottl cam_periph_unlock(periph); 1009168752Sscottl cam_periph_release(periph); 101039213Sgibbs return(ENXIO); 101140603Sken } 101239213Sgibbs 1013168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1014168752Sscottl cam_periph_unlock(periph); 1015168752Sscottl cam_periph_release(periph); 101639213Sgibbs return (error); 101741297Sken } 101839213Sgibbs 101940020Sken /* 1020111206Sken * Check for media, and set the appropriate flags. We don't bail 1021111206Sken * if we don't have media, but then we don't allow anything but the 1022111206Sken * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 102351836Sphk */ 1024111206Sken cdcheckmedia(periph); 102540020Sken 1026168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 1027168752Sscottl cam_periph_unhold(periph); 102839213Sgibbs 1029186371Sscottl /* Closes aren't symmetrical with opens, so fix up the refcounting. */ 1030186371Sscottl if ((softc->flags & CD_FLAG_OPEN) == 0) { 1031186371Sscottl softc->flags |= CD_FLAG_OPEN; 1032186371Sscottl cam_periph_unlock(periph); 1033186371Sscottl } else { 1034186371Sscottl cam_periph_unlock(periph); 1035186371Sscottl cam_periph_release(periph); 1036186371Sscottl } 1037186371Sscottl 1038168752Sscottl return (0); 103939213Sgibbs} 104039213Sgibbs 104139213Sgibbsstatic int 1042120599Sphkcdclose(struct disk *dp) 104339213Sgibbs{ 104439213Sgibbs struct cam_periph *periph; 104539213Sgibbs struct cd_softc *softc; 104639213Sgibbs 1047120599Sphk periph = (struct cam_periph *)dp->d_drv1; 104839213Sgibbs if (periph == NULL) 104939213Sgibbs return (ENXIO); 105039213Sgibbs 105139213Sgibbs softc = (struct cd_softc *)periph->softc; 105239213Sgibbs 1053168752Sscottl cam_periph_lock(periph); 1054168752Sscottl cam_periph_hold(periph, PRIBIO); 105539213Sgibbs 105639213Sgibbs if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 105739213Sgibbs cdprevent(periph, PR_ALLOW); 105839213Sgibbs 105939213Sgibbs /* 106039213Sgibbs * Since we're closing this CD, mark the blocksize as unavailable. 1061111206Sken * It will be marked as available when the CD is opened again. 106239213Sgibbs */ 1063125975Sphk softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 106439213Sgibbs 1065111206Sken /* 1066111206Sken * We'll check the media and toc again at the next open(). 1067111206Sken */ 1068168752Sscottl softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 1069111206Sken 1070168752Sscottl cam_periph_unhold(periph); 107139213Sgibbs cam_periph_unlock(periph); 107239213Sgibbs cam_periph_release(periph); 107339213Sgibbs 107439213Sgibbs return (0); 107539213Sgibbs} 107639213Sgibbs 107739213Sgibbsstatic void 107839213Sgibbscdshorttimeout(void *arg) 107939213Sgibbs{ 108039213Sgibbs struct cdchanger *changer; 108139213Sgibbs 108239213Sgibbs changer = (struct cdchanger *)arg; 108339213Sgibbs 108439213Sgibbs /* Always clear the short timeout flag, since that's what we're in */ 108539213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 108639213Sgibbs 108739213Sgibbs /* 108839213Sgibbs * Check to see if there is any more pending or outstanding I/O for 108939213Sgibbs * this device. If not, move it out of the active slot. 109039213Sgibbs */ 109159249Sphk if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1092112262Sphk && (changer->cur_device->outstanding_cmds == 0)) { 109339213Sgibbs changer->flags |= CHANGER_MANUAL_CALL; 109439213Sgibbs cdrunchangerqueue(changer); 109539213Sgibbs } 109639213Sgibbs} 109739213Sgibbs 109839213Sgibbs/* 109939213Sgibbs * This is a wrapper for xpt_schedule. It only applies to changers. 110039213Sgibbs */ 110139213Sgibbsstatic void 110239213Sgibbscdschedule(struct cam_periph *periph, int priority) 110339213Sgibbs{ 110439213Sgibbs struct cd_softc *softc; 110539213Sgibbs 110639213Sgibbs softc = (struct cd_softc *)periph->softc; 110739213Sgibbs 110839213Sgibbs /* 110939213Sgibbs * If this device isn't currently queued, and if it isn't 111039213Sgibbs * the active device, then we queue this device and run the 111139213Sgibbs * changer queue if there is no timeout scheduled to do it. 111239213Sgibbs * If this device is the active device, just schedule it 111339213Sgibbs * to run again. If this device is queued, there should be 111439213Sgibbs * a timeout in place already that will make sure it runs. 111539213Sgibbs */ 111639213Sgibbs if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 111739213Sgibbs && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 111839213Sgibbs /* 111939213Sgibbs * We don't do anything with the priority here. 112039213Sgibbs * This is strictly a fifo queue. 112139213Sgibbs */ 1122198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 112345442Sgibbs softc->pinfo.generation = ++softc->changer->devq.generation; 112439213Sgibbs camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 112539213Sgibbs 112639213Sgibbs /* 112739213Sgibbs * Since we just put a device in the changer queue, 112839213Sgibbs * check and see if there is a timeout scheduled for 112939213Sgibbs * this changer. If so, let the timeout handle 113039213Sgibbs * switching this device into the active slot. If 113139213Sgibbs * not, manually call the timeout routine to 113239213Sgibbs * bootstrap things. 113339213Sgibbs */ 113439213Sgibbs if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 113546581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 113646581Sken && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 113739213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 113839213Sgibbs cdrunchangerqueue(softc->changer); 113939213Sgibbs } 114039213Sgibbs } else if ((softc->flags & CD_FLAG_ACTIVE) 114139213Sgibbs && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 114239213Sgibbs xpt_schedule(periph, priority); 114339213Sgibbs} 114439213Sgibbs 114539213Sgibbsstatic void 114639213Sgibbscdrunchangerqueue(void *arg) 114739213Sgibbs{ 114839213Sgibbs struct cd_softc *softc; 114939213Sgibbs struct cdchanger *changer; 115039213Sgibbs int called_from_timeout; 115139213Sgibbs 115239213Sgibbs changer = (struct cdchanger *)arg; 115339213Sgibbs 115439213Sgibbs /* 115539213Sgibbs * If we have NOT been called from cdstrategy() or cddone(), and 115639213Sgibbs * instead from a timeout routine, go ahead and clear the 115739213Sgibbs * timeout flag. 115839213Sgibbs */ 115939213Sgibbs if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 116039213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 116139213Sgibbs called_from_timeout = 1; 116239213Sgibbs } else 116339213Sgibbs called_from_timeout = 0; 116439213Sgibbs 116539213Sgibbs /* Always clear the manual call flag */ 116639213Sgibbs changer->flags &= ~CHANGER_MANUAL_CALL; 116739213Sgibbs 116839213Sgibbs /* nothing to do if the queue is empty */ 116939213Sgibbs if (changer->devq.entries <= 0) { 117039213Sgibbs return; 117139213Sgibbs } 117239213Sgibbs 117339213Sgibbs /* 117439213Sgibbs * If the changer queue is frozen, that means we have an active 117539213Sgibbs * device. 117639213Sgibbs */ 1177203108Smav if (changer->devq.qfrozen_cnt[0] > 0) { 117839213Sgibbs 1179168752Sscottl /* 1180168752Sscottl * We always need to reset the frozen count and clear the 1181168752Sscottl * active flag. 1182168752Sscottl */ 1183203108Smav changer->devq.qfrozen_cnt[0]--; 1184168752Sscottl changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1185168752Sscottl changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1186168752Sscottl 1187112262Sphk if (changer->cur_device->outstanding_cmds > 0) { 118839213Sgibbs changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 118939213Sgibbs changer->cur_device->bufs_left = 1190112262Sphk changer->cur_device->outstanding_cmds; 119139213Sgibbs if (called_from_timeout) { 1192168752Sscottl callout_reset(&changer->long_handle, 1193168752Sscottl changer_max_busy_seconds * hz, 1194168752Sscottl cdrunchangerqueue, changer); 119539213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 119639213Sgibbs } 119739213Sgibbs return; 119839213Sgibbs } 119939213Sgibbs 120039213Sgibbs /* 120139213Sgibbs * Check to see whether the current device has any I/O left 120239213Sgibbs * to do. If so, requeue it at the end of the queue. If 120339213Sgibbs * not, there is no need to requeue it. 120439213Sgibbs */ 120559249Sphk if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 120639213Sgibbs 120739213Sgibbs changer->cur_device->pinfo.generation = 120845442Sgibbs ++changer->devq.generation; 120939213Sgibbs camq_insert(&changer->devq, 121039213Sgibbs (cam_pinfo *)changer->cur_device); 121139213Sgibbs } 121239213Sgibbs } 121339213Sgibbs 121445845Sgibbs softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 121539213Sgibbs 121639213Sgibbs changer->cur_device = softc; 121739213Sgibbs 1218203108Smav changer->devq.qfrozen_cnt[0]++; 121939213Sgibbs softc->flags |= CD_FLAG_ACTIVE; 122039213Sgibbs 122139213Sgibbs /* Just in case this device is waiting */ 122239213Sgibbs wakeup(&softc->changer); 1223198382Smav xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL); 122439213Sgibbs 122539213Sgibbs /* 122639213Sgibbs * Get rid of any pending timeouts, and set a flag to schedule new 122739213Sgibbs * ones so this device gets its full time quantum. 122839213Sgibbs */ 122939213Sgibbs if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1230168752Sscottl callout_stop(&changer->long_handle); 123139213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 123239213Sgibbs } 123339213Sgibbs 123439213Sgibbs if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1235168752Sscottl callout_stop(&changer->short_handle); 123639213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 123739213Sgibbs } 123839213Sgibbs 123939213Sgibbs /* 124039213Sgibbs * We need to schedule timeouts, but we only do this after the 124139213Sgibbs * first transaction has completed. This eliminates the changer 124239213Sgibbs * switch time. 124339213Sgibbs */ 124439213Sgibbs changer->flags |= CHANGER_NEED_TIMEOUT; 124539213Sgibbs} 124639213Sgibbs 124739213Sgibbsstatic void 124839213Sgibbscdchangerschedule(struct cd_softc *softc) 124939213Sgibbs{ 125039213Sgibbs struct cdchanger *changer; 125139213Sgibbs 125239213Sgibbs changer = softc->changer; 125339213Sgibbs 125439213Sgibbs /* 125539213Sgibbs * If this is a changer, and this is the current device, 125639213Sgibbs * and this device has at least the minimum time quantum to 125739213Sgibbs * run, see if we can switch it out. 125839213Sgibbs */ 125939213Sgibbs if ((softc->flags & CD_FLAG_ACTIVE) 126039213Sgibbs && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 126139213Sgibbs && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 126239213Sgibbs /* 126339213Sgibbs * We try three things here. The first is that we 126439213Sgibbs * check to see whether the schedule on completion 126539213Sgibbs * flag is set. If it is, we decrement the number 126639213Sgibbs * of buffers left, and if it's zero, we reschedule. 126739213Sgibbs * Next, we check to see whether the pending buffer 126839213Sgibbs * queue is empty and whether there are no 126939213Sgibbs * outstanding transactions. If so, we reschedule. 127039213Sgibbs * Next, we see if the pending buffer queue is empty. 127139213Sgibbs * If it is, we set the number of buffers left to 127239213Sgibbs * the current active buffer count and set the 127339213Sgibbs * schedule on complete flag. 127439213Sgibbs */ 127539213Sgibbs if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 127639213Sgibbs if (--softc->bufs_left == 0) { 127739213Sgibbs softc->changer->flags |= 127839213Sgibbs CHANGER_MANUAL_CALL; 127939213Sgibbs softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 128039213Sgibbs cdrunchangerqueue(softc->changer); 128139213Sgibbs } 128259249Sphk } else if ((bioq_first(&softc->bio_queue) == NULL) 1283112262Sphk && (softc->outstanding_cmds == 0)) { 128439213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 128539213Sgibbs cdrunchangerqueue(softc->changer); 128639213Sgibbs } 128739213Sgibbs } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 128839213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 128939213Sgibbs 129039213Sgibbs /* 129139213Sgibbs * Now that the first transaction to this 129239213Sgibbs * particular device has completed, we can go ahead 129339213Sgibbs * and schedule our timeouts. 129439213Sgibbs */ 129539213Sgibbs if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1296168752Sscottl callout_reset(&changer->long_handle, 1297168752Sscottl changer_max_busy_seconds * hz, 1298168752Sscottl cdrunchangerqueue, changer); 129939213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 130039213Sgibbs } else 130139213Sgibbs printf("cdchangerschedule: already have a long" 130239213Sgibbs " timeout!\n"); 130339213Sgibbs 130439213Sgibbs if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1305168752Sscottl callout_reset(&changer->short_handle, 1306168752Sscottl changer_min_busy_seconds * hz, 1307168752Sscottl cdshorttimeout, changer); 130839213Sgibbs changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 130939213Sgibbs } else 131039213Sgibbs printf("cdchangerschedule: already have a short " 131139213Sgibbs "timeout!\n"); 131239213Sgibbs 131339213Sgibbs /* 131439213Sgibbs * We just scheduled timeouts, no need to schedule 131539213Sgibbs * more. 131639213Sgibbs */ 131739213Sgibbs changer->flags &= ~CHANGER_NEED_TIMEOUT; 131839213Sgibbs 131939213Sgibbs } 132039213Sgibbs} 132139213Sgibbs 132239213Sgibbsstatic int 132339213Sgibbscdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 132439213Sgibbs u_int32_t cam_flags, 132539213Sgibbs u_int32_t sense_flags), 132639213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags) 132739213Sgibbs{ 132839213Sgibbs struct cd_softc *softc; 132939213Sgibbs struct cam_periph *periph; 133039213Sgibbs int error; 133139213Sgibbs 133239213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 133339213Sgibbs softc = (struct cd_softc *)periph->softc; 133439213Sgibbs 133539213Sgibbs error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1336125975Sphk softc->disk->d_devstat); 133739213Sgibbs 133839213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 133939213Sgibbs cdchangerschedule(softc); 134039213Sgibbs 134139213Sgibbs return(error); 134239213Sgibbs} 134339213Sgibbs 134442017Seivindstatic union ccb * 134539213Sgibbscdgetccb(struct cam_periph *periph, u_int32_t priority) 134639213Sgibbs{ 134739213Sgibbs struct cd_softc *softc; 134839213Sgibbs 134939213Sgibbs softc = (struct cd_softc *)periph->softc; 135039213Sgibbs 135139213Sgibbs if (softc->flags & CD_FLAG_CHANGER) { 135239213Sgibbs /* 135339213Sgibbs * This should work the first time this device is woken up, 135439213Sgibbs * but just in case it doesn't, we use a while loop. 135539213Sgibbs */ 135646581Sken while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 135739213Sgibbs /* 135839213Sgibbs * If this changer isn't already queued, queue it up. 135939213Sgibbs */ 136039213Sgibbs if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1361198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 136239213Sgibbs softc->pinfo.generation = 136345442Sgibbs ++softc->changer->devq.generation; 136439213Sgibbs camq_insert(&softc->changer->devq, 136539213Sgibbs (cam_pinfo *)softc); 136639213Sgibbs } 136746581Sken if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 136846581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 136946581Sken && ((softc->changer->flags 137046581Sken & CHANGER_SHORT_TMOUT_SCHED)==0)) { 137139213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 137239213Sgibbs cdrunchangerqueue(softc->changer); 137339213Sgibbs } else 1374168752Sscottl msleep(&softc->changer, periph->sim->mtx, 1375168752Sscottl PRIBIO, "cgticb", 0); 137639213Sgibbs } 137739213Sgibbs } 137839213Sgibbs return(cam_periph_getccb(periph, priority)); 137939213Sgibbs} 138039213Sgibbs 138139213Sgibbs 138239213Sgibbs/* 138339213Sgibbs * Actually translate the requested transfer into one the physical driver 138439213Sgibbs * can understand. The transfer is described by a buf and will include 138539213Sgibbs * only one physical transfer. 138639213Sgibbs */ 138739213Sgibbsstatic void 138859249Sphkcdstrategy(struct bio *bp) 138939213Sgibbs{ 139039213Sgibbs struct cam_periph *periph; 139139213Sgibbs struct cd_softc *softc; 139239213Sgibbs 1393120599Sphk periph = (struct cam_periph *)bp->bio_disk->d_drv1; 139439213Sgibbs if (periph == NULL) { 139576362Sphk biofinish(bp, NULL, ENXIO); 139676362Sphk return; 139739213Sgibbs } 139839213Sgibbs 1399168752Sscottl cam_periph_lock(periph); 140039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 140139213Sgibbs 140239213Sgibbs softc = (struct cd_softc *)periph->softc; 140339213Sgibbs 140439213Sgibbs /* 140539213Sgibbs * If the device has been made invalid, error out 140639213Sgibbs */ 140739213Sgibbs if ((softc->flags & CD_FLAG_INVALID)) { 1408168752Sscottl cam_periph_unlock(periph); 140976362Sphk biofinish(bp, NULL, ENXIO); 141076362Sphk return; 141139213Sgibbs } 141239213Sgibbs 1413111206Sken /* 1414111206Sken * If we don't have valid media, look for it before trying to 1415111206Sken * schedule the I/O. 1416111206Sken */ 1417111206Sken if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1418111206Sken int error; 1419111206Sken 1420111206Sken error = cdcheckmedia(periph); 1421111206Sken if (error != 0) { 1422168752Sscottl cam_periph_unlock(periph); 1423111206Sken biofinish(bp, NULL, error); 1424111206Sken return; 1425111206Sken } 1426111206Sken } 1427111206Sken 142839213Sgibbs /* 142939213Sgibbs * Place it in the queue of disk activities for this disk 143039213Sgibbs */ 1431112946Sphk bioq_disksort(&softc->bio_queue, bp); 143239213Sgibbs 143339213Sgibbs /* 143439213Sgibbs * Schedule ourselves for performing the work. We do things 143539213Sgibbs * differently for changers. 143639213Sgibbs */ 143739213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 1438198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 143939213Sgibbs else 1440198382Smav cdschedule(periph, CAM_PRIORITY_NORMAL); 144139213Sgibbs 1442168752Sscottl cam_periph_unlock(periph); 144339213Sgibbs return; 144439213Sgibbs} 144539213Sgibbs 144639213Sgibbsstatic void 144739213Sgibbscdstart(struct cam_periph *periph, union ccb *start_ccb) 144839213Sgibbs{ 144939213Sgibbs struct cd_softc *softc; 145059249Sphk struct bio *bp; 145139213Sgibbs struct ccb_scsiio *csio; 145239213Sgibbs struct scsi_read_capacity_data *rcap; 145339213Sgibbs 145439213Sgibbs softc = (struct cd_softc *)periph->softc; 145539213Sgibbs 145639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 145739213Sgibbs 145839213Sgibbs switch (softc->state) { 145939213Sgibbs case CD_STATE_NORMAL: 146039213Sgibbs { 146159249Sphk bp = bioq_first(&softc->bio_queue); 146239213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 146339213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 146439213Sgibbs 146539213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 146639213Sgibbs periph_links.sle); 146739213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 146839213Sgibbs wakeup(&periph->ccb_list); 146939213Sgibbs } else if (bp == NULL) { 147039213Sgibbs xpt_release_ccb(start_ccb); 147139213Sgibbs } else { 147259249Sphk bioq_remove(&softc->bio_queue, bp); 147339213Sgibbs 147439213Sgibbs scsi_read_write(&start_ccb->csio, 1475203108Smav /*retries*/ cd_retry_count, 147639213Sgibbs /* cbfcnp */ cddone, 147791062Sphk MSG_SIMPLE_Q_TAG, 147859249Sphk /* read */bp->bio_cmd == BIO_READ, 147939213Sgibbs /* byte2 */ 0, 148039213Sgibbs /* minimum_cmd_size */ 10, 1481121209Sphk /* lba */ bp->bio_offset / 1482121209Sphk softc->params.blksize, 148359249Sphk bp->bio_bcount / softc->params.blksize, 148459249Sphk /* data_ptr */ bp->bio_data, 148559249Sphk /* dxfer_len */ bp->bio_bcount, 148639213Sgibbs /* sense_len */ SSD_FULL_SIZE, 148739213Sgibbs /* timeout */ 30000); 148839213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 148939213Sgibbs 149039213Sgibbs 149139213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, 149239213Sgibbs &start_ccb->ccb_h, periph_links.le); 1493112262Sphk softc->outstanding_cmds++; 149439213Sgibbs 149539213Sgibbs /* We expect a unit attention from this device */ 149639213Sgibbs if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 149739213Sgibbs start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 149839213Sgibbs softc->flags &= ~CD_FLAG_RETRY_UA; 149939213Sgibbs } 150039213Sgibbs 150139213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 150259249Sphk bp = bioq_first(&softc->bio_queue); 150339213Sgibbs 150439213Sgibbs xpt_action(start_ccb); 150539213Sgibbs } 150639213Sgibbs if (bp != NULL) { 150739213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 1508198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 150939213Sgibbs } 151039213Sgibbs break; 151139213Sgibbs } 151239213Sgibbs case CD_STATE_PROBE: 151339213Sgibbs { 151439213Sgibbs 151539213Sgibbs rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1516203931Smav M_SCSICD, M_NOWAIT | M_ZERO); 151739213Sgibbs if (rcap == NULL) { 1518164906Smjacob xpt_print(periph->path, 1519164906Smjacob "cdstart: Couldn't malloc read_capacity data\n"); 152039213Sgibbs /* cd_free_periph??? */ 152139213Sgibbs break; 152239213Sgibbs } 152339213Sgibbs csio = &start_ccb->csio; 152439213Sgibbs scsi_read_capacity(csio, 1525203108Smav /*retries*/ cd_retry_count, 152639213Sgibbs cddone, 152739213Sgibbs MSG_SIMPLE_Q_TAG, 152839213Sgibbs rcap, 152939213Sgibbs SSD_FULL_SIZE, 153039213Sgibbs /*timeout*/20000); 153139213Sgibbs start_ccb->ccb_h.ccb_bp = NULL; 153239213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 153339213Sgibbs xpt_action(start_ccb); 153439213Sgibbs break; 153539213Sgibbs } 153639213Sgibbs } 153739213Sgibbs} 153839213Sgibbs 153939213Sgibbsstatic void 154039213Sgibbscddone(struct cam_periph *periph, union ccb *done_ccb) 154139213Sgibbs{ 154239213Sgibbs struct cd_softc *softc; 154339213Sgibbs struct ccb_scsiio *csio; 154439213Sgibbs 154539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 154639213Sgibbs 154739213Sgibbs softc = (struct cd_softc *)periph->softc; 154839213Sgibbs csio = &done_ccb->csio; 154939213Sgibbs 155039213Sgibbs switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 155139213Sgibbs case CD_CCB_BUFFER_IO: 155239213Sgibbs { 155359249Sphk struct bio *bp; 155439213Sgibbs int error; 155539213Sgibbs 155659249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 155739213Sgibbs error = 0; 155839213Sgibbs 155939213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 156039213Sgibbs int sf; 156139213Sgibbs 156239213Sgibbs if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 156339213Sgibbs sf = SF_RETRY_UA; 156439213Sgibbs else 156539213Sgibbs sf = 0; 156646747Sken 156774840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 156874840Sken if (error == ERESTART) { 156939213Sgibbs /* 157039213Sgibbs * A retry was scheuled, so 157139213Sgibbs * just return. 157239213Sgibbs */ 157339213Sgibbs return; 157439213Sgibbs } 157539213Sgibbs } 157639213Sgibbs 157739213Sgibbs if (error != 0) { 1578164906Smjacob xpt_print(periph->path, 1579164906Smjacob "cddone: got error %#x back\n", error); 1580112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 158159249Sphk bp->bio_resid = bp->bio_bcount; 158259249Sphk bp->bio_error = error; 158359249Sphk bp->bio_flags |= BIO_ERROR; 1584199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1585199279Smav cam_release_devq(done_ccb->ccb_h.path, 158639213Sgibbs /*relsim_flags*/0, 158739213Sgibbs /*reduction*/0, 158839213Sgibbs /*timeout*/0, 158939213Sgibbs /*getcount_only*/0); 159039213Sgibbs 159139213Sgibbs } else { 159259249Sphk bp->bio_resid = csio->resid; 159359249Sphk bp->bio_error = 0; 159459249Sphk if (bp->bio_resid != 0) { 1595104456Sphk /* 1596104456Sphk * Short transfer ??? 1597104456Sphk * XXX: not sure this is correct for partial 1598104456Sphk * transfers at EOM 1599104456Sphk */ 160059249Sphk bp->bio_flags |= BIO_ERROR; 160139213Sgibbs } 160239213Sgibbs } 160339213Sgibbs 160439213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1605112262Sphk softc->outstanding_cmds--; 160639213Sgibbs 160739213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 160839213Sgibbs cdchangerschedule(softc); 160939213Sgibbs 1610157806Smaxim biofinish(bp, NULL, 0); 161139213Sgibbs break; 161239213Sgibbs } 161339213Sgibbs case CD_CCB_PROBE: 161439213Sgibbs { 161539213Sgibbs struct scsi_read_capacity_data *rdcap; 161639213Sgibbs char announce_buf[120]; /* 161739213Sgibbs * Currently (9/30/97) the 161839213Sgibbs * longest possible announce 161939213Sgibbs * buffer is 108 bytes, for the 162039213Sgibbs * first error case below. 162139213Sgibbs * That is 39 bytes for the 162239213Sgibbs * basic string, 16 bytes for the 162339213Sgibbs * biggest sense key (hardware 162439213Sgibbs * error), 52 bytes for the 162539213Sgibbs * text of the largest sense 162639213Sgibbs * qualifier valid for a CDROM, 162739213Sgibbs * (0x72, 0x03 or 0x04, 162839213Sgibbs * 0x03), and one byte for the 162939213Sgibbs * null terminating character. 163039213Sgibbs * To allow for longer strings, 163139213Sgibbs * the announce buffer is 120 163239213Sgibbs * bytes. 163339213Sgibbs */ 163439213Sgibbs struct cd_params *cdp; 163539213Sgibbs 163639213Sgibbs cdp = &softc->params; 163739213Sgibbs 163839213Sgibbs rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 163939213Sgibbs 164039213Sgibbs cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 164139213Sgibbs cdp->blksize = scsi_4btoul (rdcap->length); 164239213Sgibbs 164339213Sgibbs if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 164439213Sgibbs 164541514Sarchie snprintf(announce_buf, sizeof(announce_buf), 164640020Sken "cd present [%lu x %lu byte records]", 164740020Sken cdp->disksize, (u_long)cdp->blksize); 164839213Sgibbs 164939213Sgibbs } else { 165039213Sgibbs int error; 165139213Sgibbs /* 165239213Sgibbs * Retry any UNIT ATTENTION type errors. They 165339213Sgibbs * are expected at boot. 165439213Sgibbs */ 165574840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, 165674840Sken SF_RETRY_UA | SF_NO_PRINT); 165739213Sgibbs if (error == ERESTART) { 165839213Sgibbs /* 165939213Sgibbs * A retry was scheuled, so 166039213Sgibbs * just return. 166139213Sgibbs */ 166239213Sgibbs return; 166339213Sgibbs } else if (error != 0) { 166439213Sgibbs 166539213Sgibbs struct scsi_sense_data *sense; 166639213Sgibbs int asc, ascq; 166739213Sgibbs int sense_key, error_code; 166839213Sgibbs int have_sense; 166939213Sgibbs cam_status status; 167039213Sgibbs struct ccb_getdev cgd; 167139213Sgibbs 167239213Sgibbs /* Don't wedge this device's queue */ 1673199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1674199279Smav cam_release_devq(done_ccb->ccb_h.path, 167539213Sgibbs /*relsim_flags*/0, 167639213Sgibbs /*reduction*/0, 167739213Sgibbs /*timeout*/0, 167839213Sgibbs /*getcount_only*/0); 167939213Sgibbs 168039213Sgibbs status = done_ccb->ccb_h.status; 168139213Sgibbs 168239213Sgibbs xpt_setup_ccb(&cgd.ccb_h, 168339213Sgibbs done_ccb->ccb_h.path, 1684198382Smav CAM_PRIORITY_NORMAL); 168539213Sgibbs cgd.ccb_h.func_code = XPT_GDEV_TYPE; 168639213Sgibbs xpt_action((union ccb *)&cgd); 168739213Sgibbs 168839213Sgibbs if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 168939213Sgibbs || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 169039213Sgibbs || ((status & CAM_AUTOSNS_VALID) == 0)) 169139213Sgibbs have_sense = FALSE; 169239213Sgibbs else 169339213Sgibbs have_sense = TRUE; 169439213Sgibbs 169539213Sgibbs if (have_sense) { 169639213Sgibbs sense = &csio->sense_data; 169739213Sgibbs scsi_extract_sense(sense, &error_code, 169839213Sgibbs &sense_key, 169939213Sgibbs &asc, &ascq); 170039213Sgibbs } 170139213Sgibbs /* 170246581Sken * Attach to anything that claims to be a 170346581Sken * CDROM or WORM device, as long as it 170446581Sken * doesn't return a "Logical unit not 170546581Sken * supported" (0x25) error. 170639213Sgibbs */ 170746581Sken if ((have_sense) && (asc != 0x25) 170874840Sken && (error_code == SSD_CURRENT_ERROR)) { 170974840Sken const char *sense_key_desc; 171074840Sken const char *asc_desc; 171174840Sken 171274840Sken scsi_sense_desc(sense_key, asc, ascq, 171374840Sken &cgd.inq_data, 171474840Sken &sense_key_desc, 171574840Sken &asc_desc); 171641514Sarchie snprintf(announce_buf, 171741514Sarchie sizeof(announce_buf), 171839213Sgibbs "Attempt to query device " 171939213Sgibbs "size failed: %s, %s", 172074840Sken sense_key_desc, 172174840Sken asc_desc); 172282850Sken } else if ((have_sense == 0) 172382850Sken && ((status & CAM_STATUS_MASK) == 172482850Sken CAM_SCSI_STATUS_ERROR) 172582850Sken && (csio->scsi_status == 172682850Sken SCSI_STATUS_BUSY)) { 172782850Sken snprintf(announce_buf, 172882850Sken sizeof(announce_buf), 172982850Sken "Attempt to query device " 173082850Sken "size failed: SCSI Status: %s", 173182850Sken scsi_status_string(csio)); 173274840Sken } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 173339213Sgibbs /* 173439213Sgibbs * We only print out an error for 173539213Sgibbs * CDROM type devices. For WORM 173639213Sgibbs * devices, we don't print out an 173739213Sgibbs * error since a few WORM devices 173839213Sgibbs * don't support CDROM commands. 173939213Sgibbs * If we have sense information, go 174039213Sgibbs * ahead and print it out. 174139213Sgibbs * Otherwise, just say that we 174239213Sgibbs * couldn't attach. 174339213Sgibbs */ 174439213Sgibbs 174539213Sgibbs /* 174639213Sgibbs * Just print out the error, not 174739213Sgibbs * the full probe message, when we 174839213Sgibbs * don't attach. 174939213Sgibbs */ 175040020Sken if (have_sense) 175140020Sken scsi_sense_print( 175240020Sken &done_ccb->csio); 175340020Sken else { 1754164906Smjacob xpt_print(periph->path, 1755164906Smjacob "got CAM status %#x\n", 1756164906Smjacob done_ccb->ccb_h.status); 175740020Sken } 1758164906Smjacob xpt_print(periph->path, "fatal error, " 1759164906Smjacob "failed to attach to device\n"); 176039213Sgibbs /* 176140603Sken * Invalidate this peripheral. 176239213Sgibbs */ 176339213Sgibbs cam_periph_invalidate(periph); 176440020Sken 176540020Sken announce_buf[0] = '\0'; 176639213Sgibbs } else { 176740603Sken 176839213Sgibbs /* 176940603Sken * Invalidate this peripheral. 177039213Sgibbs */ 177139213Sgibbs cam_periph_invalidate(periph); 177240020Sken announce_buf[0] = '\0'; 177339213Sgibbs } 177439213Sgibbs } 177539213Sgibbs } 1776169562Sscottl free(rdcap, M_SCSICD); 177742378Smjacob if (announce_buf[0] != '\0') { 177839213Sgibbs xpt_announce_periph(periph, announce_buf); 177942378Smjacob if (softc->flags & CD_FLAG_CHANGER) 178042378Smjacob cdchangerschedule(softc); 1781119708Sken /* 1782119708Sken * Create our sysctl variables, now that we know 1783119708Sken * we have successfully attached. 1784119708Sken */ 1785119708Sken taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 178642378Smjacob } 178740020Sken softc->state = CD_STATE_NORMAL; 178842378Smjacob /* 178942378Smjacob * Since our peripheral may be invalidated by an error 179042378Smjacob * above or an external event, we must release our CCB 179142378Smjacob * before releasing the probe lock on the peripheral. 179242378Smjacob * The peripheral will only go away once the last lock 179342378Smjacob * is removed, and we need it around for the CCB release 179442378Smjacob * operation. 179542378Smjacob */ 179642378Smjacob xpt_release_ccb(done_ccb); 1797168752Sscottl cam_periph_unhold(periph); 179842378Smjacob return; 179939213Sgibbs } 180039213Sgibbs case CD_CCB_WAITING: 180139213Sgibbs { 180239213Sgibbs /* Caller will release the CCB */ 180339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 180439213Sgibbs ("trying to wakeup ccbwait\n")); 180539213Sgibbs 180639213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 180739213Sgibbs return; 180839213Sgibbs } 180942378Smjacob default: 181042378Smjacob break; 181139213Sgibbs } 181239213Sgibbs xpt_release_ccb(done_ccb); 181339213Sgibbs} 181439213Sgibbs 1815111206Skenstatic union cd_pages * 1816111206Skencdgetpage(struct cd_mode_params *mode_params) 1817111206Sken{ 1818111206Sken union cd_pages *page; 1819111206Sken 1820111206Sken if (mode_params->cdb_size == 10) 1821111206Sken page = (union cd_pages *)find_mode_page_10( 1822111206Sken (struct scsi_mode_header_10 *)mode_params->mode_buf); 1823111206Sken else 1824111206Sken page = (union cd_pages *)find_mode_page_6( 1825111206Sken (struct scsi_mode_header_6 *)mode_params->mode_buf); 1826111206Sken 1827111206Sken return (page); 1828111206Sken} 1829111206Sken 183039213Sgibbsstatic int 1831111206Skencdgetpagesize(int page_num) 1832111206Sken{ 1833111206Sken int i; 1834111206Sken 1835111206Sken for (i = 0; i < (sizeof(cd_page_size_table)/ 1836111206Sken sizeof(cd_page_size_table[0])); i++) { 1837111206Sken if (cd_page_size_table[i].page == page_num) 1838111206Sken return (cd_page_size_table[i].page_size); 1839111206Sken } 1840111206Sken 1841111206Sken return (-1); 1842111206Sken} 1843111206Sken 1844111206Skenstatic int 1845120599Sphkcdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 184639213Sgibbs{ 184739213Sgibbs 184839213Sgibbs struct cam_periph *periph; 184939213Sgibbs struct cd_softc *softc; 1850168752Sscottl int nocopyout, error = 0; 185139213Sgibbs 1852120599Sphk periph = (struct cam_periph *)dp->d_drv1; 185339213Sgibbs if (periph == NULL) 185439213Sgibbs return(ENXIO); 185539213Sgibbs 1856168752Sscottl cam_periph_lock(periph); 185739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 185839213Sgibbs 185939213Sgibbs softc = (struct cd_softc *)periph->softc; 186039213Sgibbs 186139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 186240020Sken ("trying to do ioctl %#lx\n", cmd)); 186339213Sgibbs 1864168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1865168752Sscottl cam_periph_unlock(periph); 1866168752Sscottl cam_periph_release(periph); 1867168752Sscottl return (error); 1868168752Sscottl } 186939213Sgibbs 1870111206Sken /* 1871111206Sken * If we don't have media loaded, check for it. If still don't 1872111206Sken * have media loaded, we can only do a load or eject. 1873144135Sken * 1874144135Sken * We only care whether media is loaded if this is a cd-specific ioctl 1875144135Sken * (thus the IOCGROUP check below). Note that this will break if 1876144135Sken * anyone adds any ioctls into the switch statement below that don't 1877144135Sken * have their ioctl group set to 'c'. 1878111206Sken */ 1879111206Sken if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1880111206Sken && ((cmd != CDIOCCLOSE) 1881144135Sken && (cmd != CDIOCEJECT)) 1882144135Sken && (IOCGROUP(cmd) == 'c')) { 1883111206Sken error = cdcheckmedia(periph); 1884171529Skan if (error != 0) { 1885171529Skan cam_periph_unhold(periph); 1886171529Skan cam_periph_unlock(periph); 1887171529Skan return (error); 1888171529Skan } 1889111206Sken } 1890168752Sscottl /* 1891168752Sscottl * Drop the lock here so later mallocs can use WAITOK. The periph 1892168752Sscottl * is essentially locked still with the cam_periph_hold call above. 1893168752Sscottl */ 1894168752Sscottl cam_periph_unlock(periph); 189540020Sken 1896141031Ssobomax nocopyout = 0; 189739213Sgibbs switch (cmd) { 189839213Sgibbs 189939213Sgibbs case CDIOCPLAYTRACKS: 190039213Sgibbs { 190139213Sgibbs struct ioc_play_track *args 190239213Sgibbs = (struct ioc_play_track *) addr; 1903111206Sken struct cd_mode_params params; 1904111206Sken union cd_pages *page; 190539213Sgibbs 1906111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 1907169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1908111206Sken M_WAITOK | M_ZERO); 190939213Sgibbs 1910168752Sscottl cam_periph_lock(periph); 191139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 191239213Sgibbs ("trying to do CDIOCPLAYTRACKS\n")); 191339213Sgibbs 1914111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 191539213Sgibbs if (error) { 1916169562Sscottl free(params.mode_buf, M_SCSICD); 1917168752Sscottl cam_periph_unlock(periph); 191839213Sgibbs break; 191939213Sgibbs } 1920111206Sken page = cdgetpage(¶ms); 1921111206Sken 1922111206Sken page->audio.flags &= ~CD_PA_SOTC; 1923111206Sken page->audio.flags |= CD_PA_IMMED; 1924111206Sken error = cdsetmode(periph, ¶ms); 1925169562Sscottl free(params.mode_buf, M_SCSICD); 1926168752Sscottl if (error) { 1927168752Sscottl cam_periph_unlock(periph); 192839213Sgibbs break; 1929168752Sscottl } 1930111206Sken 1931111206Sken /* 1932111206Sken * This was originally implemented with the PLAY 1933111206Sken * AUDIO TRACK INDEX command, but that command was 1934111206Sken * deprecated after SCSI-2. Most (all?) SCSI CDROM 1935111206Sken * drives support it but ATAPI and ATAPI-derivative 1936111206Sken * drives don't seem to support it. So we keep a 1937111206Sken * cache of the table of contents and translate 1938111206Sken * track numbers to MSF format. 1939111206Sken */ 1940111206Sken if (softc->flags & CD_FLAG_VALID_TOC) { 1941111206Sken union msf_lba *sentry, *eentry; 1942111206Sken int st, et; 1943111206Sken 1944111206Sken if (args->end_track < 1945111206Sken softc->toc.header.ending_track + 1) 1946111206Sken args->end_track++; 1947111206Sken if (args->end_track > 1948111206Sken softc->toc.header.ending_track + 1) 1949111206Sken args->end_track = 1950111206Sken softc->toc.header.ending_track + 1; 1951111206Sken st = args->start_track - 1952111206Sken softc->toc.header.starting_track; 1953111206Sken et = args->end_track - 1954111206Sken softc->toc.header.starting_track; 1955111206Sken if ((st < 0) 1956111206Sken || (et < 0) 1957111206Sken || (st > (softc->toc.header.ending_track - 1958111206Sken softc->toc.header.starting_track))) { 1959111206Sken error = EINVAL; 1960111206Sken break; 1961111206Sken } 1962111206Sken sentry = &softc->toc.entries[st].addr; 1963111206Sken eentry = &softc->toc.entries[et].addr; 1964111206Sken error = cdplaymsf(periph, 1965111206Sken sentry->msf.minute, 1966111206Sken sentry->msf.second, 1967111206Sken sentry->msf.frame, 1968111206Sken eentry->msf.minute, 1969111206Sken eentry->msf.second, 1970111206Sken eentry->msf.frame); 1971111206Sken } else { 1972111206Sken /* 1973111206Sken * If we don't have a valid TOC, try the 1974111206Sken * play track index command. It is part of 1975111206Sken * the SCSI-2 spec, but was removed in the 1976111206Sken * MMC specs. ATAPI and ATAPI-derived 1977111206Sken * drives don't support it. 1978111206Sken */ 1979111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 1980111206Sken args->start_track = 1981111206Sken bin2bcd(args->start_track); 1982111206Sken args->end_track = 1983111206Sken bin2bcd(args->end_track); 1984111206Sken } 1985111206Sken error = cdplaytracks(periph, 1986111206Sken args->start_track, 1987111206Sken args->start_index, 1988111206Sken args->end_track, 1989111206Sken args->end_index); 199039213Sgibbs } 1991168752Sscottl cam_periph_unlock(periph); 199239213Sgibbs } 199339213Sgibbs break; 199439213Sgibbs case CDIOCPLAYMSF: 199539213Sgibbs { 199639213Sgibbs struct ioc_play_msf *args 199739213Sgibbs = (struct ioc_play_msf *) addr; 1998111206Sken struct cd_mode_params params; 1999111206Sken union cd_pages *page; 200039213Sgibbs 2001111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2002169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2003111206Sken M_WAITOK | M_ZERO); 200439213Sgibbs 2005168752Sscottl cam_periph_lock(periph); 200639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 200739213Sgibbs ("trying to do CDIOCPLAYMSF\n")); 200839213Sgibbs 2009111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 201039213Sgibbs if (error) { 2011169562Sscottl free(params.mode_buf, M_SCSICD); 2012168752Sscottl cam_periph_unlock(periph); 201339213Sgibbs break; 201439213Sgibbs } 2015111206Sken page = cdgetpage(¶ms); 2016111206Sken 2017111206Sken page->audio.flags &= ~CD_PA_SOTC; 2018111206Sken page->audio.flags |= CD_PA_IMMED; 2019111206Sken error = cdsetmode(periph, ¶ms); 2020169562Sscottl free(params.mode_buf, M_SCSICD); 2021168752Sscottl if (error) { 2022168752Sscottl cam_periph_unlock(periph); 202339213Sgibbs break; 2024168752Sscottl } 202539213Sgibbs error = cdplaymsf(periph, 202639213Sgibbs args->start_m, 202739213Sgibbs args->start_s, 202839213Sgibbs args->start_f, 202939213Sgibbs args->end_m, 203039213Sgibbs args->end_s, 203139213Sgibbs args->end_f); 2032168752Sscottl cam_periph_unlock(periph); 203339213Sgibbs } 203439213Sgibbs break; 203539213Sgibbs case CDIOCPLAYBLOCKS: 203639213Sgibbs { 203739213Sgibbs struct ioc_play_blocks *args 203839213Sgibbs = (struct ioc_play_blocks *) addr; 2039111206Sken struct cd_mode_params params; 2040111206Sken union cd_pages *page; 204139213Sgibbs 2042111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2043169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2044111206Sken M_WAITOK | M_ZERO); 204539213Sgibbs 2046168752Sscottl cam_periph_lock(periph); 2047168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2048168752Sscottl ("trying to do CDIOCPLAYBLOCKS\n")); 2049168752Sscottl 2050168752Sscottl 2051111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 205239213Sgibbs if (error) { 2053169562Sscottl free(params.mode_buf, M_SCSICD); 2054168752Sscottl cam_periph_unlock(periph); 205539213Sgibbs break; 205639213Sgibbs } 2057111206Sken page = cdgetpage(¶ms); 2058111206Sken 2059111206Sken page->audio.flags &= ~CD_PA_SOTC; 2060111206Sken page->audio.flags |= CD_PA_IMMED; 2061111206Sken error = cdsetmode(periph, ¶ms); 2062169562Sscottl free(params.mode_buf, M_SCSICD); 2063168752Sscottl if (error) { 2064168752Sscottl cam_periph_unlock(periph); 206539213Sgibbs break; 2066168752Sscottl } 206739213Sgibbs error = cdplay(periph, args->blk, args->len); 2068168752Sscottl cam_periph_unlock(periph); 206939213Sgibbs } 207039213Sgibbs break; 2071141031Ssobomax case CDIOCREADSUBCHANNEL_SYSSPACE: 2072141031Ssobomax nocopyout = 1; 2073141031Ssobomax /* Fallthrough */ 207439213Sgibbs case CDIOCREADSUBCHANNEL: 207539213Sgibbs { 207639213Sgibbs struct ioc_read_subchannel *args 207739213Sgibbs = (struct ioc_read_subchannel *) addr; 207839213Sgibbs struct cd_sub_channel_info *data; 207939213Sgibbs u_int32_t len = args->data_len; 208039213Sgibbs 2081168752Sscottl data = malloc(sizeof(struct cd_sub_channel_info), 2082203931Smav M_SCSICD, M_WAITOK | M_ZERO); 2083168752Sscottl 2084168752Sscottl cam_periph_lock(periph); 208539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 208639213Sgibbs ("trying to do CDIOCREADSUBCHANNEL\n")); 208739213Sgibbs 208839213Sgibbs if ((len > sizeof(struct cd_sub_channel_info)) || 208939213Sgibbs (len < sizeof(struct cd_sub_channel_header))) { 209039213Sgibbs printf( 209139213Sgibbs "scsi_cd: cdioctl: " 209239213Sgibbs "cdioreadsubchannel: error, len=%d\n", 209339213Sgibbs len); 209439213Sgibbs error = EINVAL; 2095169562Sscottl free(data, M_SCSICD); 2096168752Sscottl cam_periph_unlock(periph); 209739213Sgibbs break; 209839213Sgibbs } 209939213Sgibbs 210039213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 210139213Sgibbs args->track = bin2bcd(args->track); 210239213Sgibbs 210339213Sgibbs error = cdreadsubchannel(periph, args->address_format, 210439213Sgibbs args->data_format, args->track, data, len); 210539213Sgibbs 210639213Sgibbs if (error) { 2107169562Sscottl free(data, M_SCSICD); 2108168752Sscottl cam_periph_unlock(periph); 210939213Sgibbs break; 211039213Sgibbs } 211139213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 211239213Sgibbs data->what.track_info.track_number = 211339213Sgibbs bcd2bin(data->what.track_info.track_number); 211439213Sgibbs len = min(len, ((data->header.data_len[0] << 8) + 211539213Sgibbs data->header.data_len[1] + 211639213Sgibbs sizeof(struct cd_sub_channel_header))); 2117168752Sscottl cam_periph_unlock(periph); 2118141031Ssobomax if (nocopyout == 0) { 2119141031Ssobomax if (copyout(data, args->data, len) != 0) { 2120141031Ssobomax error = EFAULT; 2121141031Ssobomax } 2122141031Ssobomax } else { 2123141031Ssobomax bcopy(data, args->data, len); 212439213Sgibbs } 2125169562Sscottl free(data, M_SCSICD); 212639213Sgibbs } 212739213Sgibbs break; 212839213Sgibbs 212939213Sgibbs case CDIOREADTOCHEADER: 213039213Sgibbs { 213139213Sgibbs struct ioc_toc_header *th; 213239213Sgibbs 2133169562Sscottl th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2134203931Smav M_WAITOK | M_ZERO); 2135168752Sscottl 2136168752Sscottl cam_periph_lock(periph); 213739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 213839213Sgibbs ("trying to do CDIOREADTOCHEADER\n")); 213939213Sgibbs 2140111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2141221585Savg sizeof (*th), /*sense_flags*/SF_NO_PRINT); 214239213Sgibbs if (error) { 2143169562Sscottl free(th, M_SCSICD); 2144168752Sscottl cam_periph_unlock(periph); 214539213Sgibbs break; 214639213Sgibbs } 214739213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 214839213Sgibbs /* we are going to have to convert the BCD 214939213Sgibbs * encoding on the cd to what is expected 215039213Sgibbs */ 215139213Sgibbs th->starting_track = 215239213Sgibbs bcd2bin(th->starting_track); 215339213Sgibbs th->ending_track = bcd2bin(th->ending_track); 215439213Sgibbs } 215590868Smike th->len = ntohs(th->len); 215639213Sgibbs bcopy(th, addr, sizeof(*th)); 2157169562Sscottl free(th, M_SCSICD); 2158168752Sscottl cam_periph_unlock(periph); 215939213Sgibbs } 216039213Sgibbs break; 216139213Sgibbs case CDIOREADTOCENTRYS: 216239213Sgibbs { 2163111206Sken struct cd_tocdata *data; 2164111206Sken struct cd_toc_single *lead; 216539213Sgibbs struct ioc_read_toc_entry *te = 216639213Sgibbs (struct ioc_read_toc_entry *) addr; 216739213Sgibbs struct ioc_toc_header *th; 216839213Sgibbs u_int32_t len, readlen, idx, num; 216939213Sgibbs u_int32_t starting_track = te->starting_track; 217039213Sgibbs 2171203931Smav data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2172203931Smav lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 2173168752Sscottl 2174168752Sscottl cam_periph_lock(periph); 217539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 217639213Sgibbs ("trying to do CDIOREADTOCENTRYS\n")); 217739213Sgibbs 217839213Sgibbs if (te->data_len < sizeof(struct cd_toc_entry) 217939213Sgibbs || (te->data_len % sizeof(struct cd_toc_entry)) != 0 218039213Sgibbs || (te->address_format != CD_MSF_FORMAT 218139213Sgibbs && te->address_format != CD_LBA_FORMAT)) { 218239213Sgibbs error = EINVAL; 218339213Sgibbs printf("scsi_cd: error in readtocentries, " 218439213Sgibbs "returning EINVAL\n"); 2185169562Sscottl free(data, M_SCSICD); 2186169562Sscottl free(lead, M_SCSICD); 2187168752Sscottl cam_periph_unlock(periph); 218839213Sgibbs break; 218939213Sgibbs } 219039213Sgibbs 219139213Sgibbs th = &data->header; 2192111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2193111206Sken sizeof (*th), /*sense_flags*/0); 219439213Sgibbs if (error) { 2195169562Sscottl free(data, M_SCSICD); 2196169562Sscottl free(lead, M_SCSICD); 2197168752Sscottl cam_periph_unlock(periph); 219839213Sgibbs break; 219939213Sgibbs } 220039213Sgibbs 220139213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 220239213Sgibbs /* we are going to have to convert the BCD 220339213Sgibbs * encoding on the cd to what is expected 220439213Sgibbs */ 220539213Sgibbs th->starting_track = 220639213Sgibbs bcd2bin(th->starting_track); 220739213Sgibbs th->ending_track = bcd2bin(th->ending_track); 220839213Sgibbs } 220939213Sgibbs 221039213Sgibbs if (starting_track == 0) 221139213Sgibbs starting_track = th->starting_track; 221239213Sgibbs else if (starting_track == LEADOUT) 221339213Sgibbs starting_track = th->ending_track + 1; 221439213Sgibbs else if (starting_track < th->starting_track || 221539213Sgibbs starting_track > th->ending_track + 1) { 221639213Sgibbs printf("scsi_cd: error in readtocentries, " 221739213Sgibbs "returning EINVAL\n"); 2218169562Sscottl free(data, M_SCSICD); 2219169562Sscottl free(lead, M_SCSICD); 2220168752Sscottl cam_periph_unlock(periph); 222139213Sgibbs error = EINVAL; 222239213Sgibbs break; 222339213Sgibbs } 222439213Sgibbs 222539213Sgibbs /* calculate reading length without leadout entry */ 222639213Sgibbs readlen = (th->ending_track - starting_track + 1) * 222739213Sgibbs sizeof(struct cd_toc_entry); 222839213Sgibbs 222939213Sgibbs /* and with leadout entry */ 223039213Sgibbs len = readlen + sizeof(struct cd_toc_entry); 223139213Sgibbs if (te->data_len < len) { 223239213Sgibbs len = te->data_len; 223339213Sgibbs if (readlen > len) 223439213Sgibbs readlen = len; 223539213Sgibbs } 223639213Sgibbs if (len > sizeof(data->entries)) { 223739213Sgibbs printf("scsi_cd: error in readtocentries, " 223839213Sgibbs "returning EINVAL\n"); 223939213Sgibbs error = EINVAL; 2240169562Sscottl free(data, M_SCSICD); 2241169562Sscottl free(lead, M_SCSICD); 2242168752Sscottl cam_periph_unlock(periph); 224339213Sgibbs break; 224439213Sgibbs } 224539213Sgibbs num = len / sizeof(struct cd_toc_entry); 224639213Sgibbs 224739213Sgibbs if (readlen > 0) { 224839213Sgibbs error = cdreadtoc(periph, te->address_format, 224939213Sgibbs starting_track, 2250111206Sken (u_int8_t *)data, 2251111206Sken readlen + sizeof (*th), 2252111206Sken /*sense_flags*/0); 225339213Sgibbs if (error) { 2254169562Sscottl free(data, M_SCSICD); 2255169562Sscottl free(lead, M_SCSICD); 2256168752Sscottl cam_periph_unlock(periph); 225739213Sgibbs break; 225839213Sgibbs } 225939213Sgibbs } 226039213Sgibbs 226139213Sgibbs /* make leadout entry if needed */ 226239213Sgibbs idx = starting_track + num - 1; 226339213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 226439213Sgibbs th->ending_track = bcd2bin(th->ending_track); 226539213Sgibbs if (idx == th->ending_track + 1) { 226639213Sgibbs error = cdreadtoc(periph, te->address_format, 2267111206Sken LEADOUT, (u_int8_t *)lead, 2268111206Sken sizeof(*lead), 2269111206Sken /*sense_flags*/0); 227039213Sgibbs if (error) { 2271169562Sscottl free(data, M_SCSICD); 2272169562Sscottl free(lead, M_SCSICD); 2273168752Sscottl cam_periph_unlock(periph); 227439213Sgibbs break; 227539213Sgibbs } 227639213Sgibbs data->entries[idx - starting_track] = 227739213Sgibbs lead->entry; 227839213Sgibbs } 227939213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 228039213Sgibbs for (idx = 0; idx < num - 1; idx++) { 228139213Sgibbs data->entries[idx].track = 228239213Sgibbs bcd2bin(data->entries[idx].track); 228339213Sgibbs } 228439213Sgibbs } 228539213Sgibbs 2286168752Sscottl cam_periph_unlock(periph); 228739213Sgibbs error = copyout(data->entries, te->data, len); 2288169562Sscottl free(data, M_SCSICD); 2289169562Sscottl free(lead, M_SCSICD); 229039213Sgibbs } 229139213Sgibbs break; 229239213Sgibbs case CDIOREADTOCENTRY: 229339213Sgibbs { 2294111206Sken struct cd_toc_single *data; 229539213Sgibbs struct ioc_read_toc_single_entry *te = 229639213Sgibbs (struct ioc_read_toc_single_entry *) addr; 229739213Sgibbs struct ioc_toc_header *th; 229839213Sgibbs u_int32_t track; 229939213Sgibbs 2300203931Smav data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2301168752Sscottl 2302168752Sscottl cam_periph_lock(periph); 230339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 230439213Sgibbs ("trying to do CDIOREADTOCENTRY\n")); 230539213Sgibbs 230639213Sgibbs if (te->address_format != CD_MSF_FORMAT 230739213Sgibbs && te->address_format != CD_LBA_FORMAT) { 230839213Sgibbs printf("error in readtocentry, " 230939213Sgibbs " returning EINVAL\n"); 2310169562Sscottl free(data, M_SCSICD); 231139213Sgibbs error = EINVAL; 2312168752Sscottl cam_periph_unlock(periph); 231339213Sgibbs break; 231439213Sgibbs } 231539213Sgibbs 231639213Sgibbs th = &data->header; 2317111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2318111206Sken sizeof (*th), /*sense_flags*/0); 231939213Sgibbs if (error) { 2320169562Sscottl free(data, M_SCSICD); 2321168752Sscottl cam_periph_unlock(periph); 232239213Sgibbs break; 232339213Sgibbs } 232439213Sgibbs 232539213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 232639213Sgibbs /* we are going to have to convert the BCD 232739213Sgibbs * encoding on the cd to what is expected 232839213Sgibbs */ 232939213Sgibbs th->starting_track = 233039213Sgibbs bcd2bin(th->starting_track); 233139213Sgibbs th->ending_track = bcd2bin(th->ending_track); 233239213Sgibbs } 233339213Sgibbs track = te->track; 233439213Sgibbs if (track == 0) 233539213Sgibbs track = th->starting_track; 233639213Sgibbs else if (track == LEADOUT) 233739213Sgibbs /* OK */; 233839213Sgibbs else if (track < th->starting_track || 233939213Sgibbs track > th->ending_track + 1) { 234039213Sgibbs printf("error in readtocentry, " 234139213Sgibbs " returning EINVAL\n"); 2342169562Sscottl free(data, M_SCSICD); 234339213Sgibbs error = EINVAL; 2344168752Sscottl cam_periph_unlock(periph); 234539213Sgibbs break; 234639213Sgibbs } 234739213Sgibbs 234839213Sgibbs error = cdreadtoc(periph, te->address_format, track, 2349111206Sken (u_int8_t *)data, sizeof(*data), 2350111206Sken /*sense_flags*/0); 235139213Sgibbs if (error) { 2352169562Sscottl free(data, M_SCSICD); 2353168752Sscottl cam_periph_unlock(periph); 235439213Sgibbs break; 235539213Sgibbs } 235639213Sgibbs 235739213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 235839213Sgibbs data->entry.track = bcd2bin(data->entry.track); 235939213Sgibbs bcopy(&data->entry, &te->entry, 236039213Sgibbs sizeof(struct cd_toc_entry)); 2361169562Sscottl free(data, M_SCSICD); 2362168752Sscottl cam_periph_unlock(periph); 236339213Sgibbs } 236439213Sgibbs break; 236539213Sgibbs case CDIOCSETPATCH: 236639213Sgibbs { 2367111206Sken struct ioc_patch *arg = (struct ioc_patch *)addr; 2368111206Sken struct cd_mode_params params; 2369111206Sken union cd_pages *page; 237039213Sgibbs 2371168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2372169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2373168752Sscottl M_WAITOK | M_ZERO); 2374168752Sscottl 2375168752Sscottl cam_periph_lock(periph); 237639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 237739213Sgibbs ("trying to do CDIOCSETPATCH\n")); 237839213Sgibbs 2379111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 238039213Sgibbs if (error) { 2381169562Sscottl free(params.mode_buf, M_SCSICD); 2382168752Sscottl cam_periph_unlock(periph); 238339213Sgibbs break; 238439213Sgibbs } 2385111206Sken page = cdgetpage(¶ms); 2386111206Sken 2387111206Sken page->audio.port[LEFT_PORT].channels = 238839213Sgibbs arg->patch[0]; 2389111206Sken page->audio.port[RIGHT_PORT].channels = 239039213Sgibbs arg->patch[1]; 2391111206Sken page->audio.port[2].channels = arg->patch[2]; 2392111206Sken page->audio.port[3].channels = arg->patch[3]; 2393111206Sken error = cdsetmode(periph, ¶ms); 2394169562Sscottl free(params.mode_buf, M_SCSICD); 2395168752Sscottl cam_periph_unlock(periph); 239639213Sgibbs } 239739213Sgibbs break; 239839213Sgibbs case CDIOCGETVOL: 239939213Sgibbs { 240039213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2401111206Sken struct cd_mode_params params; 2402111206Sken union cd_pages *page; 240339213Sgibbs 2404168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2405169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2406168752Sscottl M_WAITOK | M_ZERO); 2407168752Sscottl 2408168752Sscottl cam_periph_lock(periph); 240939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 241039213Sgibbs ("trying to do CDIOCGETVOL\n")); 241139213Sgibbs 2412111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 241339213Sgibbs if (error) { 2414169562Sscottl free(params.mode_buf, M_SCSICD); 2415168752Sscottl cam_periph_unlock(periph); 241639213Sgibbs break; 241739213Sgibbs } 2418111206Sken page = cdgetpage(¶ms); 2419111206Sken 242039213Sgibbs arg->vol[LEFT_PORT] = 2421111206Sken page->audio.port[LEFT_PORT].volume; 242239213Sgibbs arg->vol[RIGHT_PORT] = 2423111206Sken page->audio.port[RIGHT_PORT].volume; 2424111206Sken arg->vol[2] = page->audio.port[2].volume; 2425111206Sken arg->vol[3] = page->audio.port[3].volume; 2426169562Sscottl free(params.mode_buf, M_SCSICD); 2427168752Sscottl cam_periph_unlock(periph); 242839213Sgibbs } 242939213Sgibbs break; 243039213Sgibbs case CDIOCSETVOL: 243139213Sgibbs { 243239213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2433111206Sken struct cd_mode_params params; 2434111206Sken union cd_pages *page; 243539213Sgibbs 2436168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2437169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2438168752Sscottl M_WAITOK | M_ZERO); 2439168752Sscottl 2440168752Sscottl cam_periph_lock(periph); 244139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 244239213Sgibbs ("trying to do CDIOCSETVOL\n")); 244339213Sgibbs 2444111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 244539213Sgibbs if (error) { 2446169562Sscottl free(params.mode_buf, M_SCSICD); 2447168752Sscottl cam_periph_unlock(periph); 244839213Sgibbs break; 244939213Sgibbs } 2450111206Sken page = cdgetpage(¶ms); 2451111206Sken 2452111206Sken page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2453111206Sken page->audio.port[LEFT_PORT].volume = 245439213Sgibbs arg->vol[LEFT_PORT]; 2455111206Sken page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2456111206Sken page->audio.port[RIGHT_PORT].volume = 245739213Sgibbs arg->vol[RIGHT_PORT]; 2458111206Sken page->audio.port[2].volume = arg->vol[2]; 2459111206Sken page->audio.port[3].volume = arg->vol[3]; 2460111206Sken error = cdsetmode(periph, ¶ms); 2461168752Sscottl cam_periph_unlock(periph); 2462169562Sscottl free(params.mode_buf, M_SCSICD); 246339213Sgibbs } 246439213Sgibbs break; 246539213Sgibbs case CDIOCSETMONO: 246639213Sgibbs { 2467111206Sken struct cd_mode_params params; 2468111206Sken union cd_pages *page; 246939213Sgibbs 2470168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2471169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2472168752Sscottl M_WAITOK | M_ZERO); 2473168752Sscottl 2474168752Sscottl cam_periph_lock(periph); 247539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 247639213Sgibbs ("trying to do CDIOCSETMONO\n")); 247739213Sgibbs 2478111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 247939213Sgibbs if (error) { 2480169562Sscottl free(params.mode_buf, M_SCSICD); 2481168752Sscottl cam_periph_unlock(periph); 248239213Sgibbs break; 248339213Sgibbs } 2484111206Sken page = cdgetpage(¶ms); 2485111206Sken 2486111206Sken page->audio.port[LEFT_PORT].channels = 248739213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2488111206Sken page->audio.port[RIGHT_PORT].channels = 248939213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2490111206Sken page->audio.port[2].channels = 0; 2491111206Sken page->audio.port[3].channels = 0; 2492111206Sken error = cdsetmode(periph, ¶ms); 2493168752Sscottl cam_periph_unlock(periph); 2494169562Sscottl free(params.mode_buf, M_SCSICD); 249539213Sgibbs } 249639213Sgibbs break; 249739213Sgibbs case CDIOCSETSTEREO: 249839213Sgibbs { 2499111206Sken struct cd_mode_params params; 2500111206Sken union cd_pages *page; 250139213Sgibbs 2502168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2503169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2504168752Sscottl M_WAITOK | M_ZERO); 2505168752Sscottl 2506168752Sscottl cam_periph_lock(periph); 250739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 250839213Sgibbs ("trying to do CDIOCSETSTEREO\n")); 250939213Sgibbs 2510111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 251139213Sgibbs if (error) { 2512169562Sscottl free(params.mode_buf, M_SCSICD); 2513168752Sscottl cam_periph_unlock(periph); 251439213Sgibbs break; 251539213Sgibbs } 2516111206Sken page = cdgetpage(¶ms); 2517111206Sken 2518111206Sken page->audio.port[LEFT_PORT].channels = 251939213Sgibbs LEFT_CHANNEL; 2520111206Sken page->audio.port[RIGHT_PORT].channels = 252139213Sgibbs RIGHT_CHANNEL; 2522111206Sken page->audio.port[2].channels = 0; 2523111206Sken page->audio.port[3].channels = 0; 2524111206Sken error = cdsetmode(periph, ¶ms); 2525169562Sscottl free(params.mode_buf, M_SCSICD); 2526168752Sscottl cam_periph_unlock(periph); 252739213Sgibbs } 252839213Sgibbs break; 252939213Sgibbs case CDIOCSETMUTE: 253039213Sgibbs { 2531111206Sken struct cd_mode_params params; 2532111206Sken union cd_pages *page; 253339213Sgibbs 2534168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2535169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2536168752Sscottl M_WAITOK | M_ZERO); 2537168752Sscottl 2538168752Sscottl cam_periph_lock(periph); 253939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 254039213Sgibbs ("trying to do CDIOCSETMUTE\n")); 254139213Sgibbs 2542111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 254339213Sgibbs if (error) { 2544208800Savg free(params.mode_buf, M_SCSICD); 2545168752Sscottl cam_periph_unlock(periph); 254639213Sgibbs break; 254739213Sgibbs } 2548111206Sken page = cdgetpage(¶ms); 2549111206Sken 2550111206Sken page->audio.port[LEFT_PORT].channels = 0; 2551111206Sken page->audio.port[RIGHT_PORT].channels = 0; 2552111206Sken page->audio.port[2].channels = 0; 2553111206Sken page->audio.port[3].channels = 0; 2554111206Sken error = cdsetmode(periph, ¶ms); 2555169562Sscottl free(params.mode_buf, M_SCSICD); 2556168752Sscottl cam_periph_unlock(periph); 255739213Sgibbs } 255839213Sgibbs break; 255939213Sgibbs case CDIOCSETLEFT: 256039213Sgibbs { 2561111206Sken struct cd_mode_params params; 2562111206Sken union cd_pages *page; 256339213Sgibbs 2564168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2565169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2566168752Sscottl M_WAITOK | M_ZERO); 2567168752Sscottl 2568168752Sscottl cam_periph_lock(periph); 256939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 257039213Sgibbs ("trying to do CDIOCSETLEFT\n")); 257139213Sgibbs 2572111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 257339213Sgibbs if (error) { 2574169562Sscottl free(params.mode_buf, M_SCSICD); 2575168752Sscottl cam_periph_unlock(periph); 257639213Sgibbs break; 257739213Sgibbs } 2578111206Sken page = cdgetpage(¶ms); 2579111206Sken 2580111206Sken page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2581111206Sken page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2582111206Sken page->audio.port[2].channels = 0; 2583111206Sken page->audio.port[3].channels = 0; 2584111206Sken error = cdsetmode(periph, ¶ms); 2585169562Sscottl free(params.mode_buf, M_SCSICD); 2586168752Sscottl cam_periph_unlock(periph); 258739213Sgibbs } 258839213Sgibbs break; 258939213Sgibbs case CDIOCSETRIGHT: 259039213Sgibbs { 2591111206Sken struct cd_mode_params params; 2592111206Sken union cd_pages *page; 259339213Sgibbs 2594111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2595169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2596111206Sken M_WAITOK | M_ZERO); 2597111206Sken 2598168752Sscottl cam_periph_lock(periph); 2599168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2600168752Sscottl ("trying to do CDIOCSETRIGHT\n")); 2601168752Sscottl 2602111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 260339213Sgibbs if (error) { 2604169562Sscottl free(params.mode_buf, M_SCSICD); 2605168752Sscottl cam_periph_unlock(periph); 260639213Sgibbs break; 260739213Sgibbs } 2608111206Sken page = cdgetpage(¶ms); 2609111206Sken 2610111206Sken page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2611111206Sken page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2612111206Sken page->audio.port[2].channels = 0; 2613111206Sken page->audio.port[3].channels = 0; 2614111206Sken error = cdsetmode(periph, ¶ms); 2615169562Sscottl free(params.mode_buf, M_SCSICD); 2616168752Sscottl cam_periph_unlock(periph); 261739213Sgibbs } 261839213Sgibbs break; 261939213Sgibbs case CDIOCRESUME: 2620168752Sscottl cam_periph_lock(periph); 262139213Sgibbs error = cdpause(periph, 1); 2622168752Sscottl cam_periph_unlock(periph); 262339213Sgibbs break; 262439213Sgibbs case CDIOCPAUSE: 2625168752Sscottl cam_periph_lock(periph); 262639213Sgibbs error = cdpause(periph, 0); 2627168752Sscottl cam_periph_unlock(periph); 262839213Sgibbs break; 262939213Sgibbs case CDIOCSTART: 2630168752Sscottl cam_periph_lock(periph); 2631111206Sken error = cdstartunit(periph, 0); 2632168752Sscottl cam_periph_unlock(periph); 263339213Sgibbs break; 2634111206Sken case CDIOCCLOSE: 2635168752Sscottl cam_periph_lock(periph); 2636111206Sken error = cdstartunit(periph, 1); 2637168752Sscottl cam_periph_unlock(periph); 2638111206Sken break; 263939213Sgibbs case CDIOCSTOP: 2640168752Sscottl cam_periph_lock(periph); 264139213Sgibbs error = cdstopunit(periph, 0); 2642168752Sscottl cam_periph_unlock(periph); 264339213Sgibbs break; 264439213Sgibbs case CDIOCEJECT: 2645168752Sscottl cam_periph_lock(periph); 264639213Sgibbs error = cdstopunit(periph, 1); 2647168752Sscottl cam_periph_unlock(periph); 264839213Sgibbs break; 264939213Sgibbs case CDIOCALLOW: 2650168752Sscottl cam_periph_lock(periph); 265139213Sgibbs cdprevent(periph, PR_ALLOW); 2652168752Sscottl cam_periph_unlock(periph); 265339213Sgibbs break; 265439213Sgibbs case CDIOCPREVENT: 2655168752Sscottl cam_periph_lock(periph); 265639213Sgibbs cdprevent(periph, PR_PREVENT); 2657168752Sscottl cam_periph_unlock(periph); 265839213Sgibbs break; 265939213Sgibbs case CDIOCSETDEBUG: 266039213Sgibbs /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 266139213Sgibbs error = ENOTTY; 266239213Sgibbs break; 266339213Sgibbs case CDIOCCLRDEBUG: 266439213Sgibbs /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 266539213Sgibbs error = ENOTTY; 266639213Sgibbs break; 266739213Sgibbs case CDIOCRESET: 266839213Sgibbs /* return (cd_reset(periph)); */ 266939213Sgibbs error = ENOTTY; 267039213Sgibbs break; 2671105421Snjl case CDRIOCREADSPEED: 2672168752Sscottl cam_periph_lock(periph); 2673105421Snjl error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2674168752Sscottl cam_periph_unlock(periph); 2675105421Snjl break; 2676105421Snjl case CDRIOCWRITESPEED: 2677168752Sscottl cam_periph_lock(periph); 2678105421Snjl error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2679168752Sscottl cam_periph_unlock(periph); 2680105421Snjl break; 268160422Sken case DVDIOCSENDKEY: 268260422Sken case DVDIOCREPORTKEY: { 268360422Sken struct dvd_authinfo *authinfo; 268460422Sken 268560422Sken authinfo = (struct dvd_authinfo *)addr; 268660422Sken 268760422Sken if (cmd == DVDIOCREPORTKEY) 268860422Sken error = cdreportkey(periph, authinfo); 268960422Sken else 269060422Sken error = cdsendkey(periph, authinfo); 269160422Sken break; 2692104456Sphk } 269360422Sken case DVDIOCREADSTRUCTURE: { 269460422Sken struct dvd_struct *dvdstruct; 269560422Sken 269660422Sken dvdstruct = (struct dvd_struct *)addr; 269760422Sken 269860422Sken error = cdreaddvdstructure(periph, dvdstruct); 269960422Sken 270060422Sken break; 270160422Sken } 270239213Sgibbs default: 2703168752Sscottl cam_periph_lock(periph); 270439213Sgibbs error = cam_periph_ioctl(periph, cmd, addr, cderror); 2705168752Sscottl cam_periph_unlock(periph); 270639213Sgibbs break; 270739213Sgibbs } 270839213Sgibbs 2709168752Sscottl cam_periph_lock(periph); 2710168752Sscottl cam_periph_unhold(periph); 2711168752Sscottl 271239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2713104456Sphk if (error && bootverbose) { 2714104456Sphk printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2715104456Sphk } 2716168752Sscottl cam_periph_unlock(periph); 271739213Sgibbs 271839213Sgibbs return (error); 271939213Sgibbs} 272039213Sgibbs 272139213Sgibbsstatic void 272239213Sgibbscdprevent(struct cam_periph *periph, int action) 272339213Sgibbs{ 272439213Sgibbs union ccb *ccb; 272539213Sgibbs struct cd_softc *softc; 272639213Sgibbs int error; 272739213Sgibbs 272839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 272939213Sgibbs 273039213Sgibbs softc = (struct cd_softc *)periph->softc; 273139213Sgibbs 273239213Sgibbs if (((action == PR_ALLOW) 273339213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 273439213Sgibbs || ((action == PR_PREVENT) 273539213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 273639213Sgibbs return; 273739213Sgibbs } 273839213Sgibbs 2739198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 274039213Sgibbs 274139213Sgibbs scsi_prevent(&ccb->csio, 2742203108Smav /*retries*/ cd_retry_count, 274339213Sgibbs cddone, 274439213Sgibbs MSG_SIMPLE_Q_TAG, 274539213Sgibbs action, 274639213Sgibbs SSD_FULL_SIZE, 274739213Sgibbs /* timeout */60000); 274839213Sgibbs 274974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 275074840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 275139213Sgibbs 275239213Sgibbs xpt_release_ccb(ccb); 275339213Sgibbs 275439213Sgibbs if (error == 0) { 275539213Sgibbs if (action == PR_ALLOW) 275639213Sgibbs softc->flags &= ~CD_FLAG_DISC_LOCKED; 275739213Sgibbs else 275839213Sgibbs softc->flags |= CD_FLAG_DISC_LOCKED; 275939213Sgibbs } 276039213Sgibbs} 276139213Sgibbs 2762120599Sphk/* 2763120599Sphk * XXX: the disk media and sector size is only really able to change 2764120599Sphk * XXX: while the device is closed. 2765120599Sphk */ 276639213Sgibbsstatic int 2767111206Skencdcheckmedia(struct cam_periph *periph) 276839213Sgibbs{ 276939213Sgibbs struct cd_softc *softc; 2770111206Sken struct ioc_toc_header *toch; 2771111206Sken struct cd_toc_single leadout; 2772111206Sken u_int32_t size, toclen; 2773111206Sken int error, num_entries, cdindex; 2774111206Sken 2775111206Sken softc = (struct cd_softc *)periph->softc; 2776111206Sken 2777111206Sken cdprevent(periph, PR_PREVENT); 2778134824Sphk softc->disk->d_sectorsize = 2048; 2779125975Sphk softc->disk->d_mediasize = 0; 2780111206Sken 2781111206Sken /* 2782111206Sken * Get the disc size and block size. If we can't get it, we don't 2783111206Sken * have media, most likely. 2784111206Sken */ 2785111206Sken if ((error = cdsize(periph, &size)) != 0) { 2786111206Sken softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2787111206Sken cdprevent(periph, PR_ALLOW); 2788111206Sken return (error); 2789206648Savg } else { 2790111206Sken softc->flags |= CD_FLAG_VALID_MEDIA; 2791206648Savg softc->disk->d_sectorsize = softc->params.blksize; 2792206648Savg softc->disk->d_mediasize = 2793206648Savg (off_t)softc->params.blksize * softc->params.disksize; 2794206648Savg } 2795111206Sken 2796111206Sken /* 2797111206Sken * Now we check the table of contents. This (currently) is only 2798111206Sken * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2799111206Sken * things like present a separate entry in /dev for each track, 2800111206Sken * like that acd(4) driver does. 2801111206Sken */ 2802111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2803111206Sken toch = &softc->toc.header; 2804111206Sken /* 2805111206Sken * We will get errors here for media that doesn't have a table of 2806111206Sken * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2807111206Sken * command is presented for a DDCD/CD-R/RW media, where the first TOC 2808111206Sken * has not been recorded (no complete session) and the Format codes 2809111206Sken * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2810111206Sken * with an INVALID FIELD IN CDB. Devices that are not capable of 2811111206Sken * reading an incomplete session on DDC/CD-R/RW media shall report 2812111206Sken * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2813111206Sken * 2814111206Sken * So this isn't fatal if we can't read the table of contents, it 2815111206Sken * just means that the user won't be able to issue the play tracks 2816111206Sken * ioctl, and likely lots of other stuff won't work either. They 2817111206Sken * need to burn the CD before we can do a whole lot with it. So 2818111206Sken * we don't print anything here if we get an error back. 2819111206Sken */ 2820111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2821111206Sken SF_NO_PRINT); 2822111206Sken /* 2823111206Sken * Errors in reading the table of contents aren't fatal, we just 2824111206Sken * won't have a valid table of contents cached. 2825111206Sken */ 2826111206Sken if (error != 0) { 2827111206Sken error = 0; 2828111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2829111206Sken goto bailout; 2830111206Sken } 2831111206Sken 2832111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2833111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2834111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2835111206Sken } 2836111206Sken 2837111206Sken /* Number of TOC entries, plus leadout */ 2838111206Sken num_entries = (toch->ending_track - toch->starting_track) + 2; 2839111206Sken 2840111206Sken if (num_entries <= 0) 2841111206Sken goto bailout; 2842111206Sken 2843111206Sken toclen = num_entries * sizeof(struct cd_toc_entry); 2844111206Sken 2845111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2846111206Sken (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2847111206Sken SF_NO_PRINT); 2848111206Sken if (error != 0) { 2849111206Sken error = 0; 2850111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2851111206Sken goto bailout; 2852111206Sken } 2853111206Sken 2854111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2855111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2856111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2857111206Sken } 2858111206Sken /* 2859111206Sken * XXX KDM is this necessary? Probably only if the drive doesn't 2860111206Sken * return leadout information with the table of contents. 2861111206Sken */ 2862111206Sken cdindex = toch->starting_track + num_entries -1; 2863111206Sken if (cdindex == toch->ending_track + 1) { 2864111206Sken 2865111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2866111206Sken (u_int8_t *)&leadout, sizeof(leadout), 2867111206Sken SF_NO_PRINT); 2868111206Sken if (error != 0) { 2869111206Sken error = 0; 2870111206Sken goto bailout; 2871111206Sken } 2872111206Sken softc->toc.entries[cdindex - toch->starting_track] = 2873111206Sken leadout.entry; 2874111206Sken } 2875111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2876111206Sken for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2877111206Sken softc->toc.entries[cdindex].track = 2878111206Sken bcd2bin(softc->toc.entries[cdindex].track); 2879111206Sken } 2880111206Sken } 2881111206Sken 2882111206Sken softc->flags |= CD_FLAG_VALID_TOC; 2883111206Sken 2884111206Skenbailout: 2885111206Sken 2886111206Sken /* 2887111206Sken * We unconditionally (re)set the blocksize each time the 2888111206Sken * CD device is opened. This is because the CD can change, 2889111206Sken * and therefore the blocksize might change. 2890111206Sken * XXX problems here if some slice or partition is still 2891111206Sken * open with the old size? 2892111206Sken */ 2893125975Sphk if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2894125975Sphk softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2895125975Sphk softc->disk->d_devstat->block_size = softc->params.blksize; 2896111206Sken 2897111206Sken return (error); 2898111206Sken} 2899111206Sken 2900111206Skenstatic int 2901111206Skencdsize(struct cam_periph *periph, u_int32_t *size) 2902111206Sken{ 2903111206Sken struct cd_softc *softc; 290439213Sgibbs union ccb *ccb; 290539213Sgibbs struct scsi_read_capacity_data *rcap_buf; 290639213Sgibbs int error; 290739213Sgibbs 290839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 290939213Sgibbs 291039213Sgibbs softc = (struct cd_softc *)periph->softc; 291139213Sgibbs 2912198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 291339213Sgibbs 2914168752Sscottl /* XXX Should be M_WAITOK */ 291539213Sgibbs rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2916203931Smav M_SCSICD, M_NOWAIT | M_ZERO); 2917168752Sscottl if (rcap_buf == NULL) 2918168752Sscottl return (ENOMEM); 291939213Sgibbs 292039213Sgibbs scsi_read_capacity(&ccb->csio, 2921203108Smav /*retries*/ cd_retry_count, 292239213Sgibbs cddone, 292339213Sgibbs MSG_SIMPLE_Q_TAG, 292439213Sgibbs rcap_buf, 292539213Sgibbs SSD_FULL_SIZE, 292639213Sgibbs /* timeout */20000); 292739213Sgibbs 292874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 292974840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 293039213Sgibbs 293139213Sgibbs xpt_release_ccb(ccb); 293239213Sgibbs 293339213Sgibbs softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 293439213Sgibbs softc->params.blksize = scsi_4btoul(rcap_buf->length); 2935203931Smav /* Make sure we got at least some block size. */ 2936203931Smav if (error == 0 && softc->params.blksize == 0) 2937203931Smav error = EIO; 293860806Sjoerg /* 293960806Sjoerg * SCSI-3 mandates that the reported blocksize shall be 2048. 294060806Sjoerg * Older drives sometimes report funny values, trim it down to 294160806Sjoerg * 2048, or other parts of the kernel will get confused. 294260806Sjoerg * 294360806Sjoerg * XXX we leave drives alone that might report 512 bytes, as 294460806Sjoerg * well as drives reporting more weird sizes like perhaps 4K. 294560806Sjoerg */ 294660806Sjoerg if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 294760806Sjoerg softc->params.blksize = 2048; 294839213Sgibbs 2949169562Sscottl free(rcap_buf, M_SCSICD); 295039213Sgibbs *size = softc->params.disksize; 295139213Sgibbs 295239213Sgibbs return (error); 295339213Sgibbs 295439213Sgibbs} 295539213Sgibbs 295639213Sgibbsstatic int 2957111206Skencd6byteworkaround(union ccb *ccb) 2958111206Sken{ 2959111206Sken u_int8_t *cdb; 2960111206Sken struct cam_periph *periph; 2961111206Sken struct cd_softc *softc; 2962111206Sken struct cd_mode_params *params; 2963111206Sken int frozen, found; 2964111206Sken 2965111206Sken periph = xpt_path_periph(ccb->ccb_h.path); 2966111206Sken softc = (struct cd_softc *)periph->softc; 2967111206Sken 2968111206Sken cdb = ccb->csio.cdb_io.cdb_bytes; 2969111206Sken 2970111206Sken if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2971111206Sken || ((cdb[0] != MODE_SENSE_6) 2972111206Sken && (cdb[0] != MODE_SELECT_6))) 2973111206Sken return (0); 2974111206Sken 2975111206Sken /* 2976111206Sken * Because there is no convenient place to stash the overall 2977111206Sken * cd_mode_params structure pointer, we have to grab it like this. 2978111206Sken * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2979111206Sken * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2980111206Sken * 2981111206Sken * XXX It would be nice if, at some point, we could increase the 2982111206Sken * number of available peripheral private pointers. Both pointers 2983111206Sken * are currently used in most every peripheral driver. 2984111206Sken */ 2985111206Sken found = 0; 2986111206Sken 2987111206Sken STAILQ_FOREACH(params, &softc->mode_queue, links) { 2988111206Sken if (params->mode_buf == ccb->csio.data_ptr) { 2989111206Sken found = 1; 2990111206Sken break; 2991111206Sken } 2992111206Sken } 2993111206Sken 2994111206Sken /* 2995111206Sken * This shouldn't happen. All mode sense and mode select 2996111206Sken * operations in the cd(4) driver MUST go through cdgetmode() and 2997111206Sken * cdsetmode()! 2998111206Sken */ 2999111206Sken if (found == 0) { 3000164906Smjacob xpt_print(periph->path, 3001164906Smjacob "mode buffer not found in mode queue!\n"); 3002111206Sken return (0); 3003111206Sken } 3004111206Sken 3005111206Sken params->cdb_size = 10; 3006111206Sken softc->minimum_command_size = 10; 3007164906Smjacob xpt_print(ccb->ccb_h.path, 3008164906Smjacob "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3009164906Smjacob (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3010111206Sken 3011111206Sken if (cdb[0] == MODE_SENSE_6) { 3012111206Sken struct scsi_mode_sense_10 ms10; 3013111206Sken struct scsi_mode_sense_6 *ms6; 3014111206Sken int len; 3015111206Sken 3016111206Sken ms6 = (struct scsi_mode_sense_6 *)cdb; 3017111206Sken 3018111206Sken bzero(&ms10, sizeof(ms10)); 3019111206Sken ms10.opcode = MODE_SENSE_10; 3020111206Sken ms10.byte2 = ms6->byte2; 3021111206Sken ms10.page = ms6->page; 3022111206Sken 3023111206Sken /* 3024111206Sken * 10 byte mode header, block descriptor, 3025111206Sken * sizeof(union cd_pages) 3026111206Sken */ 3027111206Sken len = sizeof(struct cd_mode_data_10); 3028111206Sken ccb->csio.dxfer_len = len; 3029111206Sken 3030111206Sken scsi_ulto2b(len, ms10.length); 3031111206Sken ms10.control = ms6->control; 3032111206Sken bcopy(&ms10, cdb, 10); 3033111206Sken ccb->csio.cdb_len = 10; 3034111206Sken } else { 3035111206Sken struct scsi_mode_select_10 ms10; 3036111206Sken struct scsi_mode_select_6 *ms6; 3037111206Sken struct scsi_mode_header_6 *header6; 3038111206Sken struct scsi_mode_header_10 *header10; 3039111206Sken struct scsi_mode_page_header *page_header; 3040111206Sken int blk_desc_len, page_num, page_size, len; 3041111206Sken 3042111206Sken ms6 = (struct scsi_mode_select_6 *)cdb; 3043111206Sken 3044111206Sken bzero(&ms10, sizeof(ms10)); 3045111206Sken ms10.opcode = MODE_SELECT_10; 3046111206Sken ms10.byte2 = ms6->byte2; 3047111206Sken 3048111206Sken header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3049111206Sken header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3050111206Sken 3051111206Sken page_header = find_mode_page_6(header6); 3052111206Sken page_num = page_header->page_code; 3053111206Sken 3054111206Sken blk_desc_len = header6->blk_desc_len; 3055111206Sken 3056111206Sken page_size = cdgetpagesize(page_num); 3057111206Sken 3058111206Sken if (page_size != (page_header->page_length + 3059111206Sken sizeof(*page_header))) 3060111206Sken page_size = page_header->page_length + 3061111206Sken sizeof(*page_header); 3062111206Sken 3063111206Sken len = sizeof(*header10) + blk_desc_len + page_size; 3064111206Sken 3065111206Sken len = min(params->alloc_len, len); 3066111206Sken 3067111206Sken /* 3068111206Sken * Since the 6 byte parameter header is shorter than the 10 3069111206Sken * byte parameter header, we need to copy the actual mode 3070111206Sken * page data, and the block descriptor, if any, so things wind 3071111206Sken * up in the right place. The regions will overlap, but 3072111206Sken * bcopy() does the right thing. 3073111206Sken */ 3074111206Sken bcopy(params->mode_buf + sizeof(*header6), 3075111206Sken params->mode_buf + sizeof(*header10), 3076111206Sken len - sizeof(*header10)); 3077111206Sken 3078111206Sken /* Make sure these fields are set correctly. */ 3079111206Sken scsi_ulto2b(0, header10->data_length); 3080111206Sken header10->medium_type = 0; 3081111206Sken scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3082111206Sken 3083111206Sken ccb->csio.dxfer_len = len; 3084111206Sken 3085111206Sken scsi_ulto2b(len, ms10.length); 3086111206Sken ms10.control = ms6->control; 3087111206Sken bcopy(&ms10, cdb, 10); 3088111206Sken ccb->csio.cdb_len = 10; 3089111206Sken } 3090111206Sken 3091111206Sken frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3092111206Sken ccb->ccb_h.status = CAM_REQUEUE_REQ; 3093111206Sken xpt_action(ccb); 3094111206Sken if (frozen) { 3095111206Sken cam_release_devq(ccb->ccb_h.path, 3096111206Sken /*relsim_flags*/0, 3097111206Sken /*openings*/0, 3098111206Sken /*timeout*/0, 3099111206Sken /*getcount_only*/0); 3100111206Sken } 3101111206Sken 3102111206Sken return (ERESTART); 3103111206Sken} 3104111206Sken 3105111206Skenstatic int 310639213Sgibbscderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 310739213Sgibbs{ 310839213Sgibbs struct cd_softc *softc; 310939213Sgibbs struct cam_periph *periph; 3110111206Sken int error; 311139213Sgibbs 311239213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 311339213Sgibbs softc = (struct cd_softc *)periph->softc; 311439213Sgibbs 3115111206Sken error = 0; 3116111206Sken 311739514Sgibbs /* 3118111206Sken * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3119111206Sken * CDB comes back with this particular error, try transforming it 3120111206Sken * into the 10 byte version. 3121111206Sken */ 3122111206Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3123111206Sken error = cd6byteworkaround(ccb); 3124111206Sken } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3125111206Sken CAM_SCSI_STATUS_ERROR) 3126111206Sken && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3127111206Sken && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3128111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3129111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3130111206Sken int sense_key, error_code, asc, ascq; 3131111206Sken 3132111206Sken scsi_extract_sense(&ccb->csio.sense_data, 3133111206Sken &error_code, &sense_key, &asc, &ascq); 3134111206Sken if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3135111206Sken error = cd6byteworkaround(ccb); 3136111206Sken } 3137111206Sken 3138111206Sken if (error == ERESTART) 3139111206Sken return (error); 3140111206Sken 3141111206Sken /* 314239514Sgibbs * XXX 314339514Sgibbs * Until we have a better way of doing pack validation, 314439514Sgibbs * don't treat UAs as errors. 314539514Sgibbs */ 314639514Sgibbs sense_flags |= SF_RETRY_UA; 314739213Sgibbs return (cam_periph_error(ccb, cam_flags, sense_flags, 314839213Sgibbs &softc->saved_ccb)); 314939213Sgibbs} 315039213Sgibbs 315139213Sgibbs/* 315239213Sgibbs * Read table of contents 315339213Sgibbs */ 315439213Sgibbsstatic int 315539213Sgibbscdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3156111206Sken u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 315739213Sgibbs{ 315839213Sgibbs struct scsi_read_toc *scsi_cmd; 315939213Sgibbs u_int32_t ntoc; 316039213Sgibbs struct ccb_scsiio *csio; 316139213Sgibbs union ccb *ccb; 316239213Sgibbs int error; 316339213Sgibbs 316439213Sgibbs ntoc = len; 316539213Sgibbs error = 0; 316639213Sgibbs 3167198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 316839213Sgibbs 316939213Sgibbs csio = &ccb->csio; 317039213Sgibbs 317139213Sgibbs cam_fill_csio(csio, 3172203108Smav /* retries */ cd_retry_count, 317339213Sgibbs /* cbfcnp */ cddone, 317439213Sgibbs /* flags */ CAM_DIR_IN, 317539213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 3176111206Sken /* data_ptr */ data, 317739213Sgibbs /* dxfer_len */ len, 317839213Sgibbs /* sense_len */ SSD_FULL_SIZE, 317939213Sgibbs sizeof(struct scsi_read_toc), 318039213Sgibbs /* timeout */ 50000); 318139213Sgibbs 318239213Sgibbs scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 318339213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 318439213Sgibbs 318539213Sgibbs if (mode == CD_MSF_FORMAT) 318639213Sgibbs scsi_cmd->byte2 |= CD_MSF; 318739213Sgibbs scsi_cmd->from_track = start; 318839213Sgibbs /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 318939213Sgibbs scsi_cmd->data_len[0] = (ntoc) >> 8; 319039213Sgibbs scsi_cmd->data_len[1] = (ntoc) & 0xff; 319139213Sgibbs 319239213Sgibbs scsi_cmd->op_code = READ_TOC; 319339213Sgibbs 319474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3195111206Sken /*sense_flags*/SF_RETRY_UA | sense_flags); 319639213Sgibbs 319739213Sgibbs xpt_release_ccb(ccb); 319839213Sgibbs 319939213Sgibbs return(error); 320039213Sgibbs} 320139213Sgibbs 320239213Sgibbsstatic int 320339213Sgibbscdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 320439213Sgibbs u_int32_t format, int track, 320539213Sgibbs struct cd_sub_channel_info *data, u_int32_t len) 320639213Sgibbs{ 320739213Sgibbs struct scsi_read_subchannel *scsi_cmd; 320839213Sgibbs struct ccb_scsiio *csio; 320939213Sgibbs union ccb *ccb; 321039213Sgibbs int error; 321139213Sgibbs 321239213Sgibbs error = 0; 321339213Sgibbs 3214198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 321539213Sgibbs 321639213Sgibbs csio = &ccb->csio; 321739213Sgibbs 321839213Sgibbs cam_fill_csio(csio, 3219203108Smav /* retries */ cd_retry_count, 322039213Sgibbs /* cbfcnp */ cddone, 322139213Sgibbs /* flags */ CAM_DIR_IN, 322239213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 322339213Sgibbs /* data_ptr */ (u_int8_t *)data, 322439213Sgibbs /* dxfer_len */ len, 322539213Sgibbs /* sense_len */ SSD_FULL_SIZE, 322639213Sgibbs sizeof(struct scsi_read_subchannel), 322739213Sgibbs /* timeout */ 50000); 322839213Sgibbs 322939213Sgibbs scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 323039213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 323139213Sgibbs 323239213Sgibbs scsi_cmd->op_code = READ_SUBCHANNEL; 323339213Sgibbs if (mode == CD_MSF_FORMAT) 323439213Sgibbs scsi_cmd->byte1 |= CD_MSF; 323539213Sgibbs scsi_cmd->byte2 = SRS_SUBQ; 323639213Sgibbs scsi_cmd->subchan_format = format; 323739213Sgibbs scsi_cmd->track = track; 323839213Sgibbs scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 323939213Sgibbs scsi_cmd->control = 0; 324039213Sgibbs 324174840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 324274840Sken /*sense_flags*/SF_RETRY_UA); 324339213Sgibbs 324439213Sgibbs xpt_release_ccb(ccb); 324539213Sgibbs 324639213Sgibbs return(error); 324739213Sgibbs} 324839213Sgibbs 324939213Sgibbs 3250111206Sken/* 3251111206Sken * All MODE_SENSE requests in the cd(4) driver MUST go through this 3252111206Sken * routine. See comments in cd6byteworkaround() for details. 3253111206Sken */ 325439213Sgibbsstatic int 3255111206Skencdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3256111206Sken u_int32_t page) 325739213Sgibbs{ 3258111206Sken struct ccb_scsiio *csio; 3259111206Sken struct cd_softc *softc; 326039213Sgibbs union ccb *ccb; 3261111206Sken int param_len; 326239213Sgibbs int error; 326339213Sgibbs 3264111206Sken softc = (struct cd_softc *)periph->softc; 3265111206Sken 3266198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 326739213Sgibbs 326839213Sgibbs csio = &ccb->csio; 326939213Sgibbs 3270111206Sken data->cdb_size = softc->minimum_command_size; 3271111206Sken if (data->cdb_size < 10) 3272111206Sken param_len = sizeof(struct cd_mode_data); 3273111206Sken else 3274111206Sken param_len = sizeof(struct cd_mode_data_10); 327539213Sgibbs 3276111206Sken /* Don't say we've got more room than we actually allocated */ 3277111206Sken param_len = min(param_len, data->alloc_len); 327839213Sgibbs 3279111206Sken scsi_mode_sense_len(csio, 3280203108Smav /* retries */ cd_retry_count, 3281111206Sken /* cbfcnp */ cddone, 3282111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3283111206Sken /* dbd */ 0, 3284111206Sken /* page_code */ SMS_PAGE_CTRL_CURRENT, 3285111206Sken /* page */ page, 3286111206Sken /* param_buf */ data->mode_buf, 3287111206Sken /* param_len */ param_len, 3288111206Sken /* minimum_cmd_size */ softc->minimum_command_size, 3289111206Sken /* sense_len */ SSD_FULL_SIZE, 3290111206Sken /* timeout */ 50000); 329139213Sgibbs 3292111206Sken /* 3293111206Sken * It would be nice not to have to do this, but there's no 3294111206Sken * available pointer in the CCB that would allow us to stuff the 3295111206Sken * mode params structure in there and retrieve it in 3296111206Sken * cd6byteworkaround(), so we can set the cdb size. The cdb size 3297111206Sken * lets the caller know what CDB size we ended up using, so they 3298111206Sken * can find the actual mode page offset. 3299111206Sken */ 3300111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3301111206Sken 330274840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 330374840Sken /*sense_flags*/SF_RETRY_UA); 330439213Sgibbs 330539213Sgibbs xpt_release_ccb(ccb); 330639213Sgibbs 3307111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3308111206Sken 3309111206Sken /* 3310111206Sken * This is a bit of belt-and-suspenders checking, but if we run 3311111206Sken * into a situation where the target sends back multiple block 3312111206Sken * descriptors, we might not have enough space in the buffer to 3313111206Sken * see the whole mode page. Better to return an error than 3314111206Sken * potentially access memory beyond our malloced region. 3315111206Sken */ 3316111206Sken if (error == 0) { 3317111206Sken u_int32_t data_len; 3318111206Sken 3319111206Sken if (data->cdb_size == 10) { 3320111206Sken struct scsi_mode_header_10 *hdr10; 3321111206Sken 3322111206Sken hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3323111206Sken data_len = scsi_2btoul(hdr10->data_length); 3324111206Sken data_len += sizeof(hdr10->data_length); 3325111206Sken } else { 3326111206Sken struct scsi_mode_header_6 *hdr6; 3327111206Sken 3328111206Sken hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3329111206Sken data_len = hdr6->data_length; 3330111206Sken data_len += sizeof(hdr6->data_length); 3331111206Sken } 3332111206Sken 3333111206Sken /* 3334111206Sken * Complain if there is more mode data available than we 3335111206Sken * allocated space for. This could potentially happen if 3336111206Sken * we miscalculated the page length for some reason, if the 3337111206Sken * drive returns multiple block descriptors, or if it sets 3338111206Sken * the data length incorrectly. 3339111206Sken */ 3340111206Sken if (data_len > data->alloc_len) { 3341164906Smjacob xpt_print(periph->path, "allocated modepage %d length " 3342164906Smjacob "%d < returned length %d\n", page, data->alloc_len, 3343164906Smjacob data_len); 3344111206Sken error = ENOSPC; 3345111206Sken } 3346111206Sken } 3347111206Sken return (error); 334839213Sgibbs} 334939213Sgibbs 3350111206Sken/* 3351111206Sken * All MODE_SELECT requests in the cd(4) driver MUST go through this 3352111206Sken * routine. See comments in cd6byteworkaround() for details. 3353111206Sken */ 335439213Sgibbsstatic int 3355111206Skencdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 335639213Sgibbs{ 3357111206Sken struct ccb_scsiio *csio; 3358111206Sken struct cd_softc *softc; 335939213Sgibbs union ccb *ccb; 3360111206Sken int cdb_size, param_len; 336139213Sgibbs int error; 336239213Sgibbs 3363111206Sken softc = (struct cd_softc *)periph->softc; 3364111206Sken 3365198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 336639213Sgibbs 336739213Sgibbs csio = &ccb->csio; 336839213Sgibbs 336939213Sgibbs error = 0; 337039213Sgibbs 337139213Sgibbs /* 3372111206Sken * If the data is formatted for the 10 byte version of the mode 3373111206Sken * select parameter list, we need to use the 10 byte CDB. 3374111206Sken * Otherwise, we use whatever the stored minimum command size. 337539213Sgibbs */ 3376111206Sken if (data->cdb_size == 10) 3377111206Sken cdb_size = data->cdb_size; 3378111206Sken else 3379111206Sken cdb_size = softc->minimum_command_size; 338039213Sgibbs 3381111206Sken if (cdb_size >= 10) { 3382111206Sken struct scsi_mode_header_10 *mode_header; 3383111206Sken u_int32_t data_len; 3384111206Sken 3385111206Sken mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3386111206Sken 3387111206Sken data_len = scsi_2btoul(mode_header->data_length); 3388111206Sken 3389111206Sken scsi_ulto2b(0, mode_header->data_length); 3390111206Sken /* 3391111206Sken * SONY drives do not allow a mode select with a medium_type 3392111206Sken * value that has just been returned by a mode sense; use a 3393111206Sken * medium_type of 0 (Default) instead. 3394111206Sken */ 3395111206Sken mode_header->medium_type = 0; 3396111206Sken 3397111206Sken /* 3398111206Sken * Pass back whatever the drive passed to us, plus the size 3399111206Sken * of the data length field. 3400111206Sken */ 3401111206Sken param_len = data_len + sizeof(mode_header->data_length); 3402111206Sken 3403111206Sken } else { 3404111206Sken struct scsi_mode_header_6 *mode_header; 3405111206Sken 3406111206Sken mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3407111206Sken 3408111206Sken param_len = mode_header->data_length + 1; 3409111206Sken 3410111206Sken mode_header->data_length = 0; 3411111206Sken /* 3412111206Sken * SONY drives do not allow a mode select with a medium_type 3413111206Sken * value that has just been returned by a mode sense; use a 3414111206Sken * medium_type of 0 (Default) instead. 3415111206Sken */ 3416111206Sken mode_header->medium_type = 0; 3417111206Sken } 3418111206Sken 3419111206Sken /* Don't say we've got more room than we actually allocated */ 3420111206Sken param_len = min(param_len, data->alloc_len); 3421111206Sken 3422111206Sken scsi_mode_select_len(csio, 3423203108Smav /* retries */ cd_retry_count, 3424111206Sken /* cbfcnp */ cddone, 3425111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3426111206Sken /* scsi_page_fmt */ 1, 3427111206Sken /* save_pages */ 0, 3428111206Sken /* param_buf */ data->mode_buf, 3429111206Sken /* param_len */ param_len, 3430111206Sken /* minimum_cmd_size */ cdb_size, 3431111206Sken /* sense_len */ SSD_FULL_SIZE, 3432111206Sken /* timeout */ 50000); 3433111206Sken 3434111206Sken /* See comments in cdgetmode() and cd6byteworkaround(). */ 3435111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3436111206Sken 343774840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 343874840Sken /*sense_flags*/SF_RETRY_UA); 343939213Sgibbs 344039213Sgibbs xpt_release_ccb(ccb); 344139213Sgibbs 3442111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3443111206Sken 3444111206Sken return (error); 344539213Sgibbs} 344639213Sgibbs 344739213Sgibbs 344839213Sgibbsstatic int 344939213Sgibbscdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 345039213Sgibbs{ 345139531Sken struct ccb_scsiio *csio; 345239213Sgibbs union ccb *ccb; 345339213Sgibbs int error; 345439531Sken u_int8_t cdb_len; 345539213Sgibbs 345639213Sgibbs error = 0; 3457198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 345839213Sgibbs csio = &ccb->csio; 345939531Sken /* 346039531Sken * Use the smallest possible command to perform the operation. 346139531Sken */ 346239531Sken if ((len & 0xffff0000) == 0) { 346339531Sken /* 346439531Sken * We can fit in a 10 byte cdb. 346539531Sken */ 346639531Sken struct scsi_play_10 *scsi_cmd; 346739213Sgibbs 346839531Sken scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 346939531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 347039531Sken scsi_cmd->op_code = PLAY_10; 347139531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 347239531Sken scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 347339531Sken cdb_len = sizeof(*scsi_cmd); 347439531Sken } else { 347539531Sken struct scsi_play_12 *scsi_cmd; 347639213Sgibbs 347739531Sken scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 347839531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 347939531Sken scsi_cmd->op_code = PLAY_12; 348039531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 348139531Sken scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 348239531Sken cdb_len = sizeof(*scsi_cmd); 348339531Sken } 348439531Sken cam_fill_csio(csio, 3485203108Smav /*retries*/ cd_retry_count, 348639531Sken cddone, 348739531Sken /*flags*/CAM_DIR_NONE, 348839531Sken MSG_SIMPLE_Q_TAG, 348939531Sken /*dataptr*/NULL, 349039531Sken /*datalen*/0, 349139531Sken /*sense_len*/SSD_FULL_SIZE, 349239531Sken cdb_len, 349339531Sken /*timeout*/50 * 1000); 349439213Sgibbs 349574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 349674840Sken /*sense_flags*/SF_RETRY_UA); 349739213Sgibbs 349839213Sgibbs xpt_release_ccb(ccb); 349939213Sgibbs 350039213Sgibbs return(error); 350139213Sgibbs} 350239213Sgibbs 350339213Sgibbsstatic int 350439213Sgibbscdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 350539213Sgibbs u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 350639213Sgibbs{ 350739213Sgibbs struct scsi_play_msf *scsi_cmd; 350839213Sgibbs struct ccb_scsiio *csio; 350939213Sgibbs union ccb *ccb; 351039213Sgibbs int error; 351139213Sgibbs 351239213Sgibbs error = 0; 351339213Sgibbs 3514198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 351539213Sgibbs 351639213Sgibbs csio = &ccb->csio; 351739213Sgibbs 351839213Sgibbs cam_fill_csio(csio, 3519203108Smav /* retries */ cd_retry_count, 352039213Sgibbs /* cbfcnp */ cddone, 352139531Sken /* flags */ CAM_DIR_NONE, 352239213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 352339213Sgibbs /* data_ptr */ NULL, 352439213Sgibbs /* dxfer_len */ 0, 352539213Sgibbs /* sense_len */ SSD_FULL_SIZE, 352639213Sgibbs sizeof(struct scsi_play_msf), 352739213Sgibbs /* timeout */ 50000); 352839213Sgibbs 352939213Sgibbs scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 353039213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 353139213Sgibbs 353239213Sgibbs scsi_cmd->op_code = PLAY_MSF; 353339213Sgibbs scsi_cmd->start_m = startm; 353439213Sgibbs scsi_cmd->start_s = starts; 353539213Sgibbs scsi_cmd->start_f = startf; 353639213Sgibbs scsi_cmd->end_m = endm; 353739213Sgibbs scsi_cmd->end_s = ends; 353839213Sgibbs scsi_cmd->end_f = endf; 353939213Sgibbs 354074840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 354174840Sken /*sense_flags*/SF_RETRY_UA); 354239213Sgibbs 354339213Sgibbs xpt_release_ccb(ccb); 354439213Sgibbs 354539213Sgibbs return(error); 354639213Sgibbs} 354739213Sgibbs 354839213Sgibbs 354939213Sgibbsstatic int 355039213Sgibbscdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 355139213Sgibbs u_int32_t etrack, u_int32_t eindex) 355239213Sgibbs{ 355339213Sgibbs struct scsi_play_track *scsi_cmd; 355439213Sgibbs struct ccb_scsiio *csio; 355539213Sgibbs union ccb *ccb; 355639213Sgibbs int error; 355739213Sgibbs 355839213Sgibbs error = 0; 355939213Sgibbs 3560198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 356139213Sgibbs 356239213Sgibbs csio = &ccb->csio; 356339213Sgibbs 356439213Sgibbs cam_fill_csio(csio, 3565203108Smav /* retries */ cd_retry_count, 356639213Sgibbs /* cbfcnp */ cddone, 356739531Sken /* flags */ CAM_DIR_NONE, 356839213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 356939213Sgibbs /* data_ptr */ NULL, 357039213Sgibbs /* dxfer_len */ 0, 357139213Sgibbs /* sense_len */ SSD_FULL_SIZE, 357239213Sgibbs sizeof(struct scsi_play_track), 357339213Sgibbs /* timeout */ 50000); 357439213Sgibbs 357539213Sgibbs scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 357639213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 357739213Sgibbs 357839213Sgibbs scsi_cmd->op_code = PLAY_TRACK; 357939213Sgibbs scsi_cmd->start_track = strack; 358039213Sgibbs scsi_cmd->start_index = sindex; 358139213Sgibbs scsi_cmd->end_track = etrack; 358239213Sgibbs scsi_cmd->end_index = eindex; 358339213Sgibbs 358474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 358574840Sken /*sense_flags*/SF_RETRY_UA); 358639213Sgibbs 358739213Sgibbs xpt_release_ccb(ccb); 358839213Sgibbs 358939213Sgibbs return(error); 359039213Sgibbs} 359139213Sgibbs 359239213Sgibbsstatic int 359339213Sgibbscdpause(struct cam_periph *periph, u_int32_t go) 359439213Sgibbs{ 359539213Sgibbs struct scsi_pause *scsi_cmd; 359639213Sgibbs struct ccb_scsiio *csio; 359739213Sgibbs union ccb *ccb; 359839213Sgibbs int error; 359939213Sgibbs 360039213Sgibbs error = 0; 360139213Sgibbs 3602198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 360339213Sgibbs 360439213Sgibbs csio = &ccb->csio; 360539213Sgibbs 360639213Sgibbs cam_fill_csio(csio, 3607203108Smav /* retries */ cd_retry_count, 360839213Sgibbs /* cbfcnp */ cddone, 360939531Sken /* flags */ CAM_DIR_NONE, 361039213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 361139213Sgibbs /* data_ptr */ NULL, 361239213Sgibbs /* dxfer_len */ 0, 361339213Sgibbs /* sense_len */ SSD_FULL_SIZE, 361439213Sgibbs sizeof(struct scsi_pause), 361539213Sgibbs /* timeout */ 50000); 361639213Sgibbs 361739213Sgibbs scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 361839213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 361939213Sgibbs 362039213Sgibbs scsi_cmd->op_code = PAUSE; 362139213Sgibbs scsi_cmd->resume = go; 362239213Sgibbs 362374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 362474840Sken /*sense_flags*/SF_RETRY_UA); 362539213Sgibbs 362639213Sgibbs xpt_release_ccb(ccb); 362739213Sgibbs 362839213Sgibbs return(error); 362939213Sgibbs} 363039213Sgibbs 363139213Sgibbsstatic int 3632111206Skencdstartunit(struct cam_periph *periph, int load) 363339213Sgibbs{ 363439213Sgibbs union ccb *ccb; 363539213Sgibbs int error; 363639213Sgibbs 363739213Sgibbs error = 0; 363839213Sgibbs 3639198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 364039213Sgibbs 364139213Sgibbs scsi_start_stop(&ccb->csio, 3642203108Smav /* retries */ cd_retry_count, 364339213Sgibbs /* cbfcnp */ cddone, 364439213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 364539213Sgibbs /* start */ TRUE, 3646111206Sken /* load_eject */ load, 364739213Sgibbs /* immediate */ FALSE, 364839213Sgibbs /* sense_len */ SSD_FULL_SIZE, 364939213Sgibbs /* timeout */ 50000); 365039213Sgibbs 365174840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 365274840Sken /*sense_flags*/SF_RETRY_UA); 365339213Sgibbs 365439213Sgibbs xpt_release_ccb(ccb); 365539213Sgibbs 365639213Sgibbs return(error); 365739213Sgibbs} 365839213Sgibbs 365939213Sgibbsstatic int 366039213Sgibbscdstopunit(struct cam_periph *periph, u_int32_t eject) 366139213Sgibbs{ 366239213Sgibbs union ccb *ccb; 366339213Sgibbs int error; 366439213Sgibbs 366539213Sgibbs error = 0; 366639213Sgibbs 3667198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 366839213Sgibbs 366939213Sgibbs scsi_start_stop(&ccb->csio, 3670203108Smav /* retries */ cd_retry_count, 367139213Sgibbs /* cbfcnp */ cddone, 367239213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 367339213Sgibbs /* start */ FALSE, 367439213Sgibbs /* load_eject */ eject, 367539213Sgibbs /* immediate */ FALSE, 367639213Sgibbs /* sense_len */ SSD_FULL_SIZE, 367739213Sgibbs /* timeout */ 50000); 367839213Sgibbs 367974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 368074840Sken /*sense_flags*/SF_RETRY_UA); 368139213Sgibbs 368239213Sgibbs xpt_release_ccb(ccb); 368339213Sgibbs 368439213Sgibbs return(error); 368539213Sgibbs} 368660422Sken 368760422Skenstatic int 3688105421Snjlcdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3689105421Snjl{ 3690105421Snjl struct scsi_set_speed *scsi_cmd; 3691105421Snjl struct ccb_scsiio *csio; 3692105421Snjl union ccb *ccb; 3693105421Snjl int error; 3694105421Snjl 3695105421Snjl error = 0; 3696198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3697105421Snjl csio = &ccb->csio; 3698105421Snjl 3699107193Snjl /* Preserve old behavior: units in multiples of CDROM speed */ 3700107193Snjl if (rdspeed < 177) 3701107193Snjl rdspeed *= 177; 3702107193Snjl if (wrspeed < 177) 3703107193Snjl wrspeed *= 177; 3704107193Snjl 3705105421Snjl cam_fill_csio(csio, 3706203108Smav /* retries */ cd_retry_count, 3707105421Snjl /* cbfcnp */ cddone, 3708105421Snjl /* flags */ CAM_DIR_NONE, 3709105421Snjl /* tag_action */ MSG_SIMPLE_Q_TAG, 3710105421Snjl /* data_ptr */ NULL, 3711105421Snjl /* dxfer_len */ 0, 3712105421Snjl /* sense_len */ SSD_FULL_SIZE, 3713105421Snjl sizeof(struct scsi_set_speed), 3714105421Snjl /* timeout */ 50000); 3715105421Snjl 3716105421Snjl scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3717105421Snjl bzero(scsi_cmd, sizeof(*scsi_cmd)); 3718105421Snjl 3719105421Snjl scsi_cmd->opcode = SET_CD_SPEED; 3720105421Snjl scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3721105421Snjl scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3722105421Snjl 3723105421Snjl error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3724105421Snjl /*sense_flags*/SF_RETRY_UA); 3725105421Snjl 3726105421Snjl xpt_release_ccb(ccb); 3727105421Snjl 3728105421Snjl return(error); 3729105421Snjl} 3730105421Snjl 3731105421Snjlstatic int 373260422Skencdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 373360422Sken{ 373460422Sken union ccb *ccb; 373560422Sken u_int8_t *databuf; 373660422Sken u_int32_t lba; 373760422Sken int error; 373860422Sken int length; 373960422Sken 374060422Sken error = 0; 374160422Sken databuf = NULL; 374260422Sken lba = 0; 374360422Sken 374460422Sken switch (authinfo->format) { 374560422Sken case DVD_REPORT_AGID: 374660422Sken length = sizeof(struct scsi_report_key_data_agid); 374760422Sken break; 374860422Sken case DVD_REPORT_CHALLENGE: 374960422Sken length = sizeof(struct scsi_report_key_data_challenge); 375060422Sken break; 375160422Sken case DVD_REPORT_KEY1: 375260422Sken length = sizeof(struct scsi_report_key_data_key1_key2); 375360422Sken break; 375460422Sken case DVD_REPORT_TITLE_KEY: 375560422Sken length = sizeof(struct scsi_report_key_data_title); 375660422Sken /* The lba field is only set for the title key */ 375760422Sken lba = authinfo->lba; 375860422Sken break; 375960422Sken case DVD_REPORT_ASF: 376060422Sken length = sizeof(struct scsi_report_key_data_asf); 376160422Sken break; 376260422Sken case DVD_REPORT_RPC: 376360422Sken length = sizeof(struct scsi_report_key_data_rpc); 376460422Sken break; 376560422Sken case DVD_INVALIDATE_AGID: 376660422Sken length = 0; 376760422Sken break; 376860422Sken default: 3769200036Sscottl return (EINVAL); 377060422Sken } 377160422Sken 377260422Sken if (length != 0) { 3773111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 377460422Sken } else 377560422Sken databuf = NULL; 377660422Sken 3777200036Sscottl cam_periph_lock(periph); 3778200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 377960422Sken 378060422Sken scsi_report_key(&ccb->csio, 3781203108Smav /* retries */ cd_retry_count, 378260422Sken /* cbfcnp */ cddone, 378360422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 378460422Sken /* lba */ lba, 378560422Sken /* agid */ authinfo->agid, 378660422Sken /* key_format */ authinfo->format, 378760422Sken /* data_ptr */ databuf, 378860422Sken /* dxfer_len */ length, 378960422Sken /* sense_len */ SSD_FULL_SIZE, 379060422Sken /* timeout */ 50000); 379160422Sken 379274840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 379374840Sken /*sense_flags*/SF_RETRY_UA); 379460422Sken 379560422Sken if (error != 0) 379660422Sken goto bailout; 379760422Sken 379860422Sken if (ccb->csio.resid != 0) { 3799164906Smjacob xpt_print(periph->path, "warning, residual for report key " 3800164906Smjacob "command is %d\n", ccb->csio.resid); 380160422Sken } 380260422Sken 380360422Sken switch(authinfo->format) { 380460422Sken case DVD_REPORT_AGID: { 380560422Sken struct scsi_report_key_data_agid *agid_data; 380660422Sken 380760422Sken agid_data = (struct scsi_report_key_data_agid *)databuf; 380860422Sken 380960422Sken authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 381060422Sken RKD_AGID_SHIFT; 381160422Sken break; 381260422Sken } 381360422Sken case DVD_REPORT_CHALLENGE: { 381460422Sken struct scsi_report_key_data_challenge *chal_data; 381560422Sken 381660422Sken chal_data = (struct scsi_report_key_data_challenge *)databuf; 381760422Sken 381860422Sken bcopy(chal_data->challenge_key, authinfo->keychal, 381960422Sken min(sizeof(chal_data->challenge_key), 382060422Sken sizeof(authinfo->keychal))); 382160422Sken break; 382260422Sken } 382360422Sken case DVD_REPORT_KEY1: { 382460422Sken struct scsi_report_key_data_key1_key2 *key1_data; 382560422Sken 382660422Sken key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 382760422Sken 382860422Sken bcopy(key1_data->key1, authinfo->keychal, 382960422Sken min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 383060422Sken break; 383160422Sken } 383260422Sken case DVD_REPORT_TITLE_KEY: { 383360422Sken struct scsi_report_key_data_title *title_data; 383460422Sken 383560422Sken title_data = (struct scsi_report_key_data_title *)databuf; 383660422Sken 383760422Sken authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 383860422Sken RKD_TITLE_CPM_SHIFT; 383960422Sken authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 384060422Sken RKD_TITLE_CP_SEC_SHIFT; 384160422Sken authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 384260422Sken RKD_TITLE_CMGS_SHIFT; 384360422Sken bcopy(title_data->title_key, authinfo->keychal, 384460422Sken min(sizeof(title_data->title_key), 384560422Sken sizeof(authinfo->keychal))); 384660422Sken break; 384760422Sken } 384860422Sken case DVD_REPORT_ASF: { 384960422Sken struct scsi_report_key_data_asf *asf_data; 385060422Sken 385160422Sken asf_data = (struct scsi_report_key_data_asf *)databuf; 385260422Sken 385360422Sken authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 385460422Sken break; 385560422Sken } 385660422Sken case DVD_REPORT_RPC: { 385760422Sken struct scsi_report_key_data_rpc *rpc_data; 385860422Sken 385960422Sken rpc_data = (struct scsi_report_key_data_rpc *)databuf; 386060422Sken 386160422Sken authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 386260422Sken RKD_RPC_TYPE_SHIFT; 386360422Sken authinfo->vend_rsts = 386460422Sken (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 386560422Sken RKD_RPC_VENDOR_RESET_SHIFT; 386660422Sken authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 386771752Sken authinfo->region = rpc_data->region_mask; 386871752Sken authinfo->rpc_scheme = rpc_data->rpc_scheme1; 386960422Sken break; 387060422Sken } 387160422Sken case DVD_INVALIDATE_AGID: 387260422Sken break; 387360422Sken default: 387460422Sken /* This should be impossible, since we checked above */ 387560422Sken error = EINVAL; 387660422Sken goto bailout; 387760422Sken break; /* NOTREACHED */ 387860422Sken } 3879200036Sscottl 388060422Skenbailout: 3881200036Sscottl xpt_release_ccb(ccb); 3882200036Sscottl cam_periph_unlock(periph); 3883200036Sscottl 388460422Sken if (databuf != NULL) 388560422Sken free(databuf, M_DEVBUF); 388660422Sken 388760422Sken return(error); 388860422Sken} 388960422Sken 389060422Skenstatic int 389160422Skencdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 389260422Sken{ 389360422Sken union ccb *ccb; 389460422Sken u_int8_t *databuf; 389560422Sken int length; 389660422Sken int error; 389760422Sken 389860422Sken error = 0; 389960422Sken databuf = NULL; 390060422Sken 390160422Sken switch(authinfo->format) { 390260422Sken case DVD_SEND_CHALLENGE: { 390360422Sken struct scsi_report_key_data_challenge *challenge_data; 390460422Sken 390560422Sken length = sizeof(*challenge_data); 390660422Sken 3907111119Simp challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 390860422Sken 390960422Sken databuf = (u_int8_t *)challenge_data; 391060422Sken 391160422Sken scsi_ulto2b(length - sizeof(challenge_data->data_len), 391260422Sken challenge_data->data_len); 391360422Sken 391460422Sken bcopy(authinfo->keychal, challenge_data->challenge_key, 391560422Sken min(sizeof(authinfo->keychal), 391660422Sken sizeof(challenge_data->challenge_key))); 391760422Sken break; 391860422Sken } 391960422Sken case DVD_SEND_KEY2: { 392060422Sken struct scsi_report_key_data_key1_key2 *key2_data; 392160422Sken 392260422Sken length = sizeof(*key2_data); 392360422Sken 3924111119Simp key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 392560422Sken 392660422Sken databuf = (u_int8_t *)key2_data; 392760422Sken 392860422Sken scsi_ulto2b(length - sizeof(key2_data->data_len), 392960422Sken key2_data->data_len); 393060422Sken 393160422Sken bcopy(authinfo->keychal, key2_data->key1, 393260422Sken min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 393360422Sken 393460422Sken break; 393560422Sken } 393660422Sken case DVD_SEND_RPC: { 393760422Sken struct scsi_send_key_data_rpc *rpc_data; 393860422Sken 393960422Sken length = sizeof(*rpc_data); 394060422Sken 3941111119Simp rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 394260422Sken 394360422Sken databuf = (u_int8_t *)rpc_data; 394460422Sken 394560422Sken scsi_ulto2b(length - sizeof(rpc_data->data_len), 394660422Sken rpc_data->data_len); 394760422Sken 394860422Sken rpc_data->region_code = authinfo->region; 394960422Sken break; 395060422Sken } 395160422Sken default: 3952200036Sscottl return (EINVAL); 395360422Sken } 395460422Sken 3955200036Sscottl cam_periph_lock(periph); 3956200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3957200036Sscottl 395860422Sken scsi_send_key(&ccb->csio, 3959203108Smav /* retries */ cd_retry_count, 396060422Sken /* cbfcnp */ cddone, 396160422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 396260422Sken /* agid */ authinfo->agid, 396360422Sken /* key_format */ authinfo->format, 396460422Sken /* data_ptr */ databuf, 396560422Sken /* dxfer_len */ length, 396660422Sken /* sense_len */ SSD_FULL_SIZE, 396760422Sken /* timeout */ 50000); 396860422Sken 396974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 397074840Sken /*sense_flags*/SF_RETRY_UA); 397160422Sken 3972200036Sscottl xpt_release_ccb(ccb); 3973200036Sscottl cam_periph_unlock(periph); 397460422Sken 397560422Sken if (databuf != NULL) 397660422Sken free(databuf, M_DEVBUF); 397760422Sken 397860422Sken return(error); 397960422Sken} 398060422Sken 398160422Skenstatic int 398260422Skencdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 398360422Sken{ 398460422Sken union ccb *ccb; 398560422Sken u_int8_t *databuf; 398660422Sken u_int32_t address; 398760422Sken int error; 398860422Sken int length; 398960422Sken 399060422Sken error = 0; 399160422Sken databuf = NULL; 399260422Sken /* The address is reserved for many of the formats */ 399360422Sken address = 0; 399460422Sken 399560422Sken switch(dvdstruct->format) { 399660422Sken case DVD_STRUCT_PHYSICAL: 399760422Sken length = sizeof(struct scsi_read_dvd_struct_data_physical); 399860422Sken break; 399960422Sken case DVD_STRUCT_COPYRIGHT: 400060422Sken length = sizeof(struct scsi_read_dvd_struct_data_copyright); 400160422Sken break; 400260422Sken case DVD_STRUCT_DISCKEY: 400360422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 400460422Sken break; 400560422Sken case DVD_STRUCT_BCA: 400660422Sken length = sizeof(struct scsi_read_dvd_struct_data_bca); 400760422Sken break; 400860422Sken case DVD_STRUCT_MANUFACT: 400960422Sken length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 401060422Sken break; 401160422Sken case DVD_STRUCT_CMI: 4012200036Sscottl return (ENODEV); 401360422Sken case DVD_STRUCT_PROTDISCID: 401460422Sken length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 401560422Sken break; 401660422Sken case DVD_STRUCT_DISCKEYBLOCK: 401760422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 401860422Sken break; 401960422Sken case DVD_STRUCT_DDS: 402060422Sken length = sizeof(struct scsi_read_dvd_struct_data_dds); 402160422Sken break; 402260422Sken case DVD_STRUCT_MEDIUM_STAT: 402360422Sken length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 402460422Sken break; 402560422Sken case DVD_STRUCT_SPARE_AREA: 402660422Sken length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 402760422Sken break; 402860422Sken case DVD_STRUCT_RMD_LAST: 4029200036Sscottl return (ENODEV); 403060422Sken case DVD_STRUCT_RMD_RMA: 4031200036Sscottl return (ENODEV); 403260422Sken case DVD_STRUCT_PRERECORDED: 403360422Sken length = sizeof(struct scsi_read_dvd_struct_data_leadin); 403460422Sken break; 403560422Sken case DVD_STRUCT_UNIQUEID: 403660422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 403760422Sken break; 403860422Sken case DVD_STRUCT_DCB: 4039200036Sscottl return (ENODEV); 404060422Sken case DVD_STRUCT_LIST: 404160422Sken /* 404260422Sken * This is the maximum allocation length for the READ DVD 404360422Sken * STRUCTURE command. There's nothing in the MMC3 spec 404460422Sken * that indicates a limit in the amount of data that can 404560422Sken * be returned from this call, other than the limits 404660422Sken * imposed by the 2-byte length variables. 404760422Sken */ 404860422Sken length = 65535; 404960422Sken break; 405060422Sken default: 4051200036Sscottl return (EINVAL); 405260422Sken } 405360422Sken 405460422Sken if (length != 0) { 4055111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 405660422Sken } else 405760422Sken databuf = NULL; 405860422Sken 4059200036Sscottl cam_periph_lock(periph); 4060200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4061200036Sscottl 406260422Sken scsi_read_dvd_structure(&ccb->csio, 4063203108Smav /* retries */ cd_retry_count, 406460422Sken /* cbfcnp */ cddone, 406560422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 406660422Sken /* lba */ address, 406760422Sken /* layer_number */ dvdstruct->layer_num, 406860422Sken /* key_format */ dvdstruct->format, 406960422Sken /* agid */ dvdstruct->agid, 407060422Sken /* data_ptr */ databuf, 407160422Sken /* dxfer_len */ length, 407260422Sken /* sense_len */ SSD_FULL_SIZE, 407360422Sken /* timeout */ 50000); 407460422Sken 407574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 407674840Sken /*sense_flags*/SF_RETRY_UA); 407760422Sken 407860422Sken if (error != 0) 407960422Sken goto bailout; 408060422Sken 408160422Sken switch(dvdstruct->format) { 408260422Sken case DVD_STRUCT_PHYSICAL: { 408360422Sken struct scsi_read_dvd_struct_data_layer_desc *inlayer; 408460422Sken struct dvd_layer *outlayer; 408560422Sken struct scsi_read_dvd_struct_data_physical *phys_data; 408660422Sken 408760422Sken phys_data = 408860422Sken (struct scsi_read_dvd_struct_data_physical *)databuf; 408960422Sken inlayer = &phys_data->layer_desc; 409060422Sken outlayer = (struct dvd_layer *)&dvdstruct->data; 409160422Sken 409260422Sken dvdstruct->length = sizeof(*inlayer); 409360422Sken 409460422Sken outlayer->book_type = (inlayer->book_type_version & 409560422Sken RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 409660422Sken outlayer->book_version = (inlayer->book_type_version & 409760422Sken RDSD_BOOK_VERSION_MASK); 409860422Sken outlayer->disc_size = (inlayer->disc_size_max_rate & 409960422Sken RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 410060422Sken outlayer->max_rate = (inlayer->disc_size_max_rate & 410160422Sken RDSD_MAX_RATE_MASK); 410260422Sken outlayer->nlayers = (inlayer->layer_info & 410360422Sken RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 410460422Sken outlayer->track_path = (inlayer->layer_info & 410560422Sken RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 410660422Sken outlayer->layer_type = (inlayer->layer_info & 410760422Sken RDSD_LAYER_TYPE_MASK); 410860422Sken outlayer->linear_density = (inlayer->density & 410960422Sken RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 411060422Sken outlayer->track_density = (inlayer->density & 411160422Sken RDSD_TRACK_DENSITY_MASK); 411260422Sken outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 411360422Sken RDSD_BCA_SHIFT; 411460422Sken outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 411560422Sken outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 411660422Sken outlayer->end_sector_l0 = 411760422Sken scsi_3btoul(inlayer->end_sector_layer0); 411860422Sken break; 411960422Sken } 412060422Sken case DVD_STRUCT_COPYRIGHT: { 412160422Sken struct scsi_read_dvd_struct_data_copyright *copy_data; 412260422Sken 412360422Sken copy_data = (struct scsi_read_dvd_struct_data_copyright *) 412460422Sken databuf; 412560422Sken 412660422Sken dvdstruct->cpst = copy_data->cps_type; 412760422Sken dvdstruct->rmi = copy_data->region_info; 412860422Sken dvdstruct->length = 0; 412960422Sken 413060422Sken break; 413160422Sken } 413260422Sken default: 413360422Sken /* 413460422Sken * Tell the user what the overall length is, no matter 413560422Sken * what we can actually fit in the data buffer. 413660422Sken */ 413760422Sken dvdstruct->length = length - ccb->csio.resid - 413860422Sken sizeof(struct scsi_read_dvd_struct_data_header); 413960422Sken 414060422Sken /* 414160422Sken * But only actually copy out the smaller of what we read 414260422Sken * in or what the structure can take. 414360422Sken */ 414460422Sken bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 414560422Sken dvdstruct->data, 414660422Sken min(sizeof(dvdstruct->data), dvdstruct->length)); 414760422Sken break; 414860422Sken } 4149200036Sscottl 415060422Skenbailout: 4151200036Sscottl xpt_release_ccb(ccb); 4152200036Sscottl cam_periph_unlock(periph); 415360422Sken 415460422Sken if (databuf != NULL) 415560422Sken free(databuf, M_DEVBUF); 415660422Sken 415760422Sken return(error); 415860422Sken} 415960422Sken 416060422Skenvoid 416160422Skenscsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 416260422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 416360422Sken u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 416460422Sken u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 416560422Sken u_int8_t sense_len, u_int32_t timeout) 416660422Sken{ 416760422Sken struct scsi_report_key *scsi_cmd; 416860422Sken 416960422Sken scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 417060422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 417160422Sken scsi_cmd->opcode = REPORT_KEY; 417260422Sken scsi_ulto4b(lba, scsi_cmd->lba); 417360422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 417460422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 417560422Sken (key_format & RK_KF_KEYFORMAT_MASK); 417660422Sken 417760422Sken cam_fill_csio(csio, 417860422Sken retries, 417960422Sken cbfcnp, 418060422Sken /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 418160422Sken tag_action, 418260422Sken /*data_ptr*/ data_ptr, 418360422Sken /*dxfer_len*/ dxfer_len, 418460422Sken sense_len, 418560422Sken sizeof(*scsi_cmd), 418660422Sken timeout); 418760422Sken} 418860422Sken 418960422Skenvoid 419060422Skenscsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 419160422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 419260422Sken u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 419360422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 419460422Sken u_int32_t timeout) 419560422Sken{ 419660422Sken struct scsi_send_key *scsi_cmd; 419760422Sken 419860422Sken scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 419960422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 420060422Sken scsi_cmd->opcode = SEND_KEY; 420160422Sken 420260422Sken scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 420360422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 420460422Sken (key_format & RK_KF_KEYFORMAT_MASK); 420560422Sken 420660422Sken cam_fill_csio(csio, 420760422Sken retries, 420860422Sken cbfcnp, 420960422Sken /*flags*/ CAM_DIR_OUT, 421060422Sken tag_action, 421160422Sken /*data_ptr*/ data_ptr, 421260422Sken /*dxfer_len*/ dxfer_len, 421360422Sken sense_len, 421460422Sken sizeof(*scsi_cmd), 421560422Sken timeout); 421660422Sken} 421760422Sken 421860422Sken 421960422Skenvoid 422060422Skenscsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 422160422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 422260422Sken u_int8_t tag_action, u_int32_t address, 422360422Sken u_int8_t layer_number, u_int8_t format, u_int8_t agid, 422460422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, 422560422Sken u_int8_t sense_len, u_int32_t timeout) 422660422Sken{ 422760422Sken struct scsi_read_dvd_structure *scsi_cmd; 422860422Sken 422960422Sken scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 423060422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 423160422Sken scsi_cmd->opcode = READ_DVD_STRUCTURE; 423260422Sken 423360422Sken scsi_ulto4b(address, scsi_cmd->address); 423460422Sken scsi_cmd->layer_number = layer_number; 423560422Sken scsi_cmd->format = format; 423660422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 423760422Sken /* The AGID is the top two bits of this byte */ 423860422Sken scsi_cmd->agid = agid << 6; 423960422Sken 424060422Sken cam_fill_csio(csio, 424160422Sken retries, 424260422Sken cbfcnp, 424360422Sken /*flags*/ CAM_DIR_IN, 424460422Sken tag_action, 424560422Sken /*data_ptr*/ data_ptr, 424660422Sken /*dxfer_len*/ dxfer_len, 424760422Sken sense_len, 424860422Sken sizeof(*scsi_cmd), 424960422Sken timeout); 425060422Sken} 4251