scsi_cd.c revision 200171
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 200171 2009-12-06 00:10:13Z mav $"); 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 } 43639213Sgibbs softc->changer->devq.qfrozen_cnt--; 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 64139213Sgibbs softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 64239213Sgibbs 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 bzero(softc, sizeof(*softc)); 65039213Sgibbs LIST_INIT(&softc->pending_ccbs); 651111206Sken STAILQ_INIT(&softc->mode_queue); 65239213Sgibbs softc->state = CD_STATE_PROBE; 65359249Sphk bioq_init(&softc->bio_queue); 65439213Sgibbs if (SID_IS_REMOVABLE(&cgd->inq_data)) 65539213Sgibbs softc->flags |= CD_FLAG_DISC_REMOVABLE; 65639213Sgibbs if ((cgd->inq_data.flags & SID_CmdQue) != 0) 65739213Sgibbs softc->flags |= CD_FLAG_TAGGED_QUEUING; 65839213Sgibbs 65939213Sgibbs periph->softc = softc; 66039213Sgibbs softc->periph = periph; 66139213Sgibbs 66239213Sgibbs /* 66339213Sgibbs * See if this device has any quirks. 66439213Sgibbs */ 66539213Sgibbs match = cam_quirkmatch((caddr_t)&cgd->inq_data, 66639213Sgibbs (caddr_t)cd_quirk_table, 66739213Sgibbs sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 66839213Sgibbs sizeof(*cd_quirk_table), scsi_inquiry_match); 66939213Sgibbs 67039213Sgibbs if (match != NULL) 67139213Sgibbs softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 67239213Sgibbs else 67339213Sgibbs softc->quirks = CD_Q_NONE; 67439213Sgibbs 675118105Snjl /* Check if the SIM does not want 6 byte commands */ 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 69039213Sgibbs /* 691111206Sken * Load the user's default, if any. 692111206Sken */ 693111206Sken snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 694111206Sken periph->unit_number); 695111206Sken TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 696111206Sken 697111206Sken /* 6 and 10 are the only permissible values here. */ 698111206Sken if (softc->minimum_command_size < 6) 699111206Sken softc->minimum_command_size = 6; 700111206Sken else if (softc->minimum_command_size > 6) 701111206Sken softc->minimum_command_size = 10; 702111206Sken 703111206Sken /* 70439213Sgibbs * We need to register the statistics structure for this device, 70539213Sgibbs * but we don't have the blocksize yet for it. So, we register 70639213Sgibbs * the structure and indicate that we don't have the blocksize 70739213Sgibbs * yet. Unlike other SCSI peripheral drivers, we explicitly set 70839213Sgibbs * the device type here to be CDROM, rather than just ORing in 70956148Smjacob * the device type. This is because this driver can attach to either 71039213Sgibbs * CDROM or WORM devices, and we want this peripheral driver to 71139213Sgibbs * show up in the devstat list as a CD peripheral driver, not a 71239213Sgibbs * WORM peripheral driver. WORM drives will also have the WORM 71339213Sgibbs * driver attached to them. 71439213Sgibbs */ 715168752Sscottl cam_periph_unlock(periph); 716125975Sphk softc->disk = disk_alloc(); 717125975Sphk softc->disk->d_devstat = devstat_new_entry("cd", 71839213Sgibbs periph->unit_number, 0, 71939213Sgibbs DEVSTAT_BS_UNAVAILABLE, 72043819Sken DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI, 72143819Sken DEVSTAT_PRIORITY_CD); 722125975Sphk softc->disk->d_open = cdopen; 723125975Sphk softc->disk->d_close = cdclose; 724125975Sphk softc->disk->d_strategy = cdstrategy; 725125975Sphk softc->disk->d_ioctl = cdioctl; 726125975Sphk softc->disk->d_name = "cd"; 727125975Sphk softc->disk->d_unit = periph->unit_number; 728125975Sphk softc->disk->d_drv1 = periph; 729200171Smav if (cpi.maxio == 0) 730200171Smav softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 731200171Smav else if (cpi.maxio > MAXPHYS) 732200171Smav softc->disk->d_maxsize = MAXPHYS; /* for safety */ 733200171Smav else 734200171Smav softc->disk->d_maxsize = cpi.maxio; 735168752Sscottl softc->disk->d_flags = 0; 736125975Sphk disk_create(softc->disk, DISK_VERSION); 737168752Sscottl cam_periph_lock(periph); 73839213Sgibbs 73939213Sgibbs /* 74039213Sgibbs * Add an async callback so that we get 74139213Sgibbs * notified if this device goes away. 74239213Sgibbs */ 743169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 744169605Sscottl cdasync, periph, periph->path); 74539213Sgibbs 74639213Sgibbs /* 74739213Sgibbs * If the target lun is greater than 0, we most likely have a CD 74839213Sgibbs * changer device. Check the quirk entries as well, though, just 74939213Sgibbs * in case someone has a CD tower with one lun per drive or 75039213Sgibbs * something like that. Also, if we know up front that a 75139213Sgibbs * particular device is a changer, we can mark it as such starting 75239213Sgibbs * with lun 0, instead of lun 1. It shouldn't be necessary to have 75339213Sgibbs * a quirk entry to define something as a changer, however. 75439213Sgibbs */ 75539213Sgibbs if (((cgd->ccb_h.target_lun > 0) 75639213Sgibbs && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 75739213Sgibbs || ((softc->quirks & CD_Q_CHANGER) != 0)) { 75839213Sgibbs struct cdchanger *nchanger; 75939213Sgibbs struct cam_periph *nperiph; 76039213Sgibbs struct cam_path *path; 76139213Sgibbs cam_status status; 76239213Sgibbs int found; 76339213Sgibbs 76439213Sgibbs /* Set the changer flag in the current device's softc */ 76539213Sgibbs softc->flags |= CD_FLAG_CHANGER; 76639213Sgibbs 76739213Sgibbs /* 76839213Sgibbs * Now, look around for an existing changer device with the 76939213Sgibbs * same path and target ID as the current device. 77039213Sgibbs */ 771168752Sscottl mtx_lock(&changerq_mtx); 77239213Sgibbs for (found = 0, 77339213Sgibbs nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 77439213Sgibbs nchanger != NULL; 77539213Sgibbs nchanger = STAILQ_NEXT(nchanger, changer_links)){ 77639213Sgibbs if ((nchanger->path_id == cgd->ccb_h.path_id) 77739213Sgibbs && (nchanger->target_id == cgd->ccb_h.target_id)) { 77839213Sgibbs found = 1; 77939213Sgibbs break; 78039213Sgibbs } 78139213Sgibbs } 782168752Sscottl mtx_unlock(&changerq_mtx); 78339213Sgibbs 78439213Sgibbs /* 78539213Sgibbs * If we found a matching entry, just add this device to 78639213Sgibbs * the list of devices on this changer. 78739213Sgibbs */ 78839213Sgibbs if (found == 1) { 78939213Sgibbs struct chdevlist *chlunhead; 79039213Sgibbs 79139213Sgibbs chlunhead = &nchanger->chluns; 79239213Sgibbs 79339213Sgibbs /* 79439213Sgibbs * XXX KDM look at consolidating this code with the 79539213Sgibbs * code below in a separate function. 79639213Sgibbs */ 79739213Sgibbs 79839213Sgibbs /* 79939213Sgibbs * Create a path with lun id 0, and see if we can 80039213Sgibbs * find a matching device 80139213Sgibbs */ 80239213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 80339213Sgibbs cgd->ccb_h.path_id, 80439213Sgibbs cgd->ccb_h.target_id, 0); 80539213Sgibbs 80639213Sgibbs if ((status == CAM_REQ_CMP) 80739213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 80839213Sgibbs struct cd_softc *nsoftc; 80939213Sgibbs 81039213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 81139213Sgibbs 81239213Sgibbs if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 81339213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 81439213Sgibbs nchanger->num_devices++; 81539213Sgibbs if (camq_resize(&nchanger->devq, 81639213Sgibbs nchanger->num_devices)!=CAM_REQ_CMP){ 81739213Sgibbs printf("cdregister: " 81839213Sgibbs "camq_resize " 81939213Sgibbs "failed, changer " 82039213Sgibbs "support may " 82139213Sgibbs "be messed up\n"); 82239213Sgibbs } 82339213Sgibbs nsoftc->changer = nchanger; 82439213Sgibbs nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 82539213Sgibbs 82639213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 82739213Sgibbs nsoftc,changer_links); 82839213Sgibbs } 82967928Sken xpt_free_path(path); 83039213Sgibbs } else if (status == CAM_REQ_CMP) 83139213Sgibbs xpt_free_path(path); 83239213Sgibbs else { 83339213Sgibbs printf("cdregister: unable to allocate path\n" 83439213Sgibbs "cdregister: changer support may be " 83539213Sgibbs "broken\n"); 83639213Sgibbs } 83739213Sgibbs 83839213Sgibbs nchanger->num_devices++; 83939213Sgibbs 84039213Sgibbs softc->changer = nchanger; 84139213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 84239213Sgibbs 84339213Sgibbs if (camq_resize(&nchanger->devq, 84439213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 84539213Sgibbs printf("cdregister: camq_resize " 84639213Sgibbs "failed, changer support may " 84739213Sgibbs "be messed up\n"); 84839213Sgibbs } 84939213Sgibbs 85039213Sgibbs STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 85139213Sgibbs } 85239213Sgibbs /* 85339213Sgibbs * In this case, we don't already have an entry for this 85439213Sgibbs * particular changer, so we need to create one, add it to 85539213Sgibbs * the queue, and queue this device on the list for this 85639213Sgibbs * changer. Before we queue this device, however, we need 85739213Sgibbs * to search for lun id 0 on this target, and add it to the 85839213Sgibbs * queue first, if it exists. (and if it hasn't already 85939213Sgibbs * been marked as part of the changer.) 86039213Sgibbs */ 86139213Sgibbs else { 86239213Sgibbs nchanger = malloc(sizeof(struct cdchanger), 86339213Sgibbs M_DEVBUF, M_NOWAIT); 86439213Sgibbs 86539213Sgibbs if (nchanger == NULL) { 86639213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 86739213Sgibbs printf("cdregister: unable to malloc " 86839213Sgibbs "changer structure\ncdregister: " 86939213Sgibbs "changer support disabled\n"); 87039213Sgibbs 87139213Sgibbs /* 87239213Sgibbs * Yes, gotos can be gross but in this case 87339213Sgibbs * I think it's justified.. 87439213Sgibbs */ 87539213Sgibbs goto cdregisterexit; 87639213Sgibbs } 87739213Sgibbs 87839213Sgibbs /* zero the structure */ 87939213Sgibbs bzero(nchanger, sizeof(struct cdchanger)); 88039213Sgibbs 88139213Sgibbs if (camq_init(&nchanger->devq, 1) != 0) { 88239213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 88339213Sgibbs printf("cdregister: changer support " 88439213Sgibbs "disabled\n"); 88539213Sgibbs goto cdregisterexit; 88639213Sgibbs } 88739213Sgibbs 88839213Sgibbs nchanger->path_id = cgd->ccb_h.path_id; 88939213Sgibbs nchanger->target_id = cgd->ccb_h.target_id; 89039213Sgibbs 89139213Sgibbs /* this is superfluous, but it makes things clearer */ 89239213Sgibbs nchanger->num_devices = 0; 89339213Sgibbs 89439213Sgibbs STAILQ_INIT(&nchanger->chluns); 89539213Sgibbs 896168752Sscottl callout_init_mtx(&nchanger->long_handle, 897168752Sscottl periph->sim->mtx, 0); 898168752Sscottl callout_init_mtx(&nchanger->short_handle, 899168752Sscottl periph->sim->mtx, 0); 900168752Sscottl 901168752Sscottl mtx_lock(&changerq_mtx); 902168752Sscottl num_changers++; 90339213Sgibbs STAILQ_INSERT_TAIL(&changerq, nchanger, 90439213Sgibbs changer_links); 905168752Sscottl mtx_unlock(&changerq_mtx); 90639213Sgibbs 90739213Sgibbs /* 90839213Sgibbs * Create a path with lun id 0, and see if we can 90939213Sgibbs * find a matching device 91039213Sgibbs */ 91139213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 91239213Sgibbs cgd->ccb_h.path_id, 91339213Sgibbs cgd->ccb_h.target_id, 0); 91439213Sgibbs 91539213Sgibbs /* 91639213Sgibbs * If we were able to allocate the path, and if we 91739213Sgibbs * find a matching device and it isn't already 91839213Sgibbs * marked as part of a changer, then we add it to 91939213Sgibbs * the current changer. 92039213Sgibbs */ 92139213Sgibbs if ((status == CAM_REQ_CMP) 92239213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL) 92339213Sgibbs && ((((struct cd_softc *)periph->softc)->flags & 92439213Sgibbs CD_FLAG_CHANGER) == 0)) { 92539213Sgibbs struct cd_softc *nsoftc; 92639213Sgibbs 92739213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 92839213Sgibbs 92939213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 93039213Sgibbs nchanger->num_devices++; 93139213Sgibbs if (camq_resize(&nchanger->devq, 93239213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 93339213Sgibbs printf("cdregister: camq_resize " 93439213Sgibbs "failed, changer support may " 93539213Sgibbs "be messed up\n"); 93639213Sgibbs } 93739213Sgibbs nsoftc->changer = nchanger; 93839213Sgibbs nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 93939213Sgibbs 94039213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 94139213Sgibbs nsoftc, changer_links); 94267928Sken xpt_free_path(path); 94339213Sgibbs } else if (status == CAM_REQ_CMP) 94439213Sgibbs xpt_free_path(path); 94539213Sgibbs else { 94639213Sgibbs printf("cdregister: unable to allocate path\n" 94739213Sgibbs "cdregister: changer support may be " 94839213Sgibbs "broken\n"); 94939213Sgibbs } 95039213Sgibbs 95139213Sgibbs softc->changer = nchanger; 95239213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 95339213Sgibbs nchanger->num_devices++; 95439213Sgibbs if (camq_resize(&nchanger->devq, 95539213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 95639213Sgibbs printf("cdregister: camq_resize " 95739213Sgibbs "failed, changer support may " 95839213Sgibbs "be messed up\n"); 95939213Sgibbs } 96039213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 96139213Sgibbs changer_links); 96239213Sgibbs } 96339213Sgibbs } 96439213Sgibbs 96539213Sgibbscdregisterexit: 96639213Sgibbs 967168752Sscottl /* 968168752Sscottl * Refcount and block open attempts until we are setup 969168752Sscottl * Can't block 970168752Sscottl */ 971168752Sscottl (void)cam_periph_hold(periph, PRIBIO); 97239213Sgibbs 97339213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 97439213Sgibbs xpt_schedule(periph, /*priority*/5); 97539213Sgibbs else 97639213Sgibbs cdschedule(periph, /*priority*/ 5); 97739213Sgibbs 97839213Sgibbs return(CAM_REQ_CMP); 97939213Sgibbs} 98039213Sgibbs 98139213Sgibbsstatic int 982120599Sphkcdopen(struct disk *dp) 98339213Sgibbs{ 98439213Sgibbs struct cam_periph *periph; 98539213Sgibbs struct cd_softc *softc; 986101940Snjl int error; 98739213Sgibbs 988120599Sphk periph = (struct cam_periph *)dp->d_drv1; 98939213Sgibbs if (periph == NULL) 99039213Sgibbs return (ENXIO); 99139213Sgibbs 99239213Sgibbs softc = (struct cd_softc *)periph->softc; 99339213Sgibbs 994168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 995168752Sscottl return(ENXIO); 996168752Sscottl 997168752Sscottl cam_periph_lock(periph); 998168752Sscottl 99940603Sken if (softc->flags & CD_FLAG_INVALID) { 1000168752Sscottl cam_periph_unlock(periph); 1001168752Sscottl cam_periph_release(periph); 100239213Sgibbs return(ENXIO); 100340603Sken } 100439213Sgibbs 1005168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1006168752Sscottl cam_periph_unlock(periph); 1007168752Sscottl cam_periph_release(periph); 100839213Sgibbs return (error); 100941297Sken } 101039213Sgibbs 101140020Sken /* 1012111206Sken * Check for media, and set the appropriate flags. We don't bail 1013111206Sken * if we don't have media, but then we don't allow anything but the 1014111206Sken * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 101551836Sphk */ 1016111206Sken cdcheckmedia(periph); 101740020Sken 1018168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 1019168752Sscottl cam_periph_unhold(periph); 102039213Sgibbs 1021186371Sscottl /* Closes aren't symmetrical with opens, so fix up the refcounting. */ 1022186371Sscottl if ((softc->flags & CD_FLAG_OPEN) == 0) { 1023186371Sscottl softc->flags |= CD_FLAG_OPEN; 1024186371Sscottl cam_periph_unlock(periph); 1025186371Sscottl } else { 1026186371Sscottl cam_periph_unlock(periph); 1027186371Sscottl cam_periph_release(periph); 1028186371Sscottl } 1029186371Sscottl 1030168752Sscottl return (0); 103139213Sgibbs} 103239213Sgibbs 103339213Sgibbsstatic int 1034120599Sphkcdclose(struct disk *dp) 103539213Sgibbs{ 103639213Sgibbs struct cam_periph *periph; 103739213Sgibbs struct cd_softc *softc; 103839213Sgibbs 1039120599Sphk periph = (struct cam_periph *)dp->d_drv1; 104039213Sgibbs if (periph == NULL) 104139213Sgibbs return (ENXIO); 104239213Sgibbs 104339213Sgibbs softc = (struct cd_softc *)periph->softc; 104439213Sgibbs 1045168752Sscottl cam_periph_lock(periph); 1046168752Sscottl cam_periph_hold(periph, PRIBIO); 104739213Sgibbs 104839213Sgibbs if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 104939213Sgibbs cdprevent(periph, PR_ALLOW); 105039213Sgibbs 105139213Sgibbs /* 105239213Sgibbs * Since we're closing this CD, mark the blocksize as unavailable. 1053111206Sken * It will be marked as available when the CD is opened again. 105439213Sgibbs */ 1055125975Sphk softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 105639213Sgibbs 1057111206Sken /* 1058111206Sken * We'll check the media and toc again at the next open(). 1059111206Sken */ 1060168752Sscottl softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 1061111206Sken 1062168752Sscottl cam_periph_unhold(periph); 106339213Sgibbs cam_periph_unlock(periph); 106439213Sgibbs cam_periph_release(periph); 106539213Sgibbs 106639213Sgibbs return (0); 106739213Sgibbs} 106839213Sgibbs 106939213Sgibbsstatic void 107039213Sgibbscdshorttimeout(void *arg) 107139213Sgibbs{ 107239213Sgibbs struct cdchanger *changer; 107339213Sgibbs 107439213Sgibbs changer = (struct cdchanger *)arg; 107539213Sgibbs 107639213Sgibbs /* Always clear the short timeout flag, since that's what we're in */ 107739213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 107839213Sgibbs 107939213Sgibbs /* 108039213Sgibbs * Check to see if there is any more pending or outstanding I/O for 108139213Sgibbs * this device. If not, move it out of the active slot. 108239213Sgibbs */ 108359249Sphk if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1084112262Sphk && (changer->cur_device->outstanding_cmds == 0)) { 108539213Sgibbs changer->flags |= CHANGER_MANUAL_CALL; 108639213Sgibbs cdrunchangerqueue(changer); 108739213Sgibbs } 108839213Sgibbs} 108939213Sgibbs 109039213Sgibbs/* 109139213Sgibbs * This is a wrapper for xpt_schedule. It only applies to changers. 109239213Sgibbs */ 109339213Sgibbsstatic void 109439213Sgibbscdschedule(struct cam_periph *periph, int priority) 109539213Sgibbs{ 109639213Sgibbs struct cd_softc *softc; 109739213Sgibbs 109839213Sgibbs softc = (struct cd_softc *)periph->softc; 109939213Sgibbs 110039213Sgibbs /* 110139213Sgibbs * If this device isn't currently queued, and if it isn't 110239213Sgibbs * the active device, then we queue this device and run the 110339213Sgibbs * changer queue if there is no timeout scheduled to do it. 110439213Sgibbs * If this device is the active device, just schedule it 110539213Sgibbs * to run again. If this device is queued, there should be 110639213Sgibbs * a timeout in place already that will make sure it runs. 110739213Sgibbs */ 110839213Sgibbs if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 110939213Sgibbs && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 111039213Sgibbs /* 111139213Sgibbs * We don't do anything with the priority here. 111239213Sgibbs * This is strictly a fifo queue. 111339213Sgibbs */ 1114198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 111545442Sgibbs softc->pinfo.generation = ++softc->changer->devq.generation; 111639213Sgibbs camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 111739213Sgibbs 111839213Sgibbs /* 111939213Sgibbs * Since we just put a device in the changer queue, 112039213Sgibbs * check and see if there is a timeout scheduled for 112139213Sgibbs * this changer. If so, let the timeout handle 112239213Sgibbs * switching this device into the active slot. If 112339213Sgibbs * not, manually call the timeout routine to 112439213Sgibbs * bootstrap things. 112539213Sgibbs */ 112639213Sgibbs if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 112746581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 112846581Sken && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 112939213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 113039213Sgibbs cdrunchangerqueue(softc->changer); 113139213Sgibbs } 113239213Sgibbs } else if ((softc->flags & CD_FLAG_ACTIVE) 113339213Sgibbs && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 113439213Sgibbs xpt_schedule(periph, priority); 113539213Sgibbs} 113639213Sgibbs 113739213Sgibbsstatic void 113839213Sgibbscdrunchangerqueue(void *arg) 113939213Sgibbs{ 114039213Sgibbs struct cd_softc *softc; 114139213Sgibbs struct cdchanger *changer; 114239213Sgibbs int called_from_timeout; 114339213Sgibbs 114439213Sgibbs changer = (struct cdchanger *)arg; 114539213Sgibbs 114639213Sgibbs /* 114739213Sgibbs * If we have NOT been called from cdstrategy() or cddone(), and 114839213Sgibbs * instead from a timeout routine, go ahead and clear the 114939213Sgibbs * timeout flag. 115039213Sgibbs */ 115139213Sgibbs if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 115239213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 115339213Sgibbs called_from_timeout = 1; 115439213Sgibbs } else 115539213Sgibbs called_from_timeout = 0; 115639213Sgibbs 115739213Sgibbs /* Always clear the manual call flag */ 115839213Sgibbs changer->flags &= ~CHANGER_MANUAL_CALL; 115939213Sgibbs 116039213Sgibbs /* nothing to do if the queue is empty */ 116139213Sgibbs if (changer->devq.entries <= 0) { 116239213Sgibbs return; 116339213Sgibbs } 116439213Sgibbs 116539213Sgibbs /* 116639213Sgibbs * If the changer queue is frozen, that means we have an active 116739213Sgibbs * device. 116839213Sgibbs */ 116939213Sgibbs if (changer->devq.qfrozen_cnt > 0) { 117039213Sgibbs 1171168752Sscottl /* 1172168752Sscottl * We always need to reset the frozen count and clear the 1173168752Sscottl * active flag. 1174168752Sscottl */ 1175168752Sscottl changer->devq.qfrozen_cnt--; 1176168752Sscottl changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1177168752Sscottl changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1178168752Sscottl 1179112262Sphk if (changer->cur_device->outstanding_cmds > 0) { 118039213Sgibbs changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 118139213Sgibbs changer->cur_device->bufs_left = 1182112262Sphk changer->cur_device->outstanding_cmds; 118339213Sgibbs if (called_from_timeout) { 1184168752Sscottl callout_reset(&changer->long_handle, 1185168752Sscottl changer_max_busy_seconds * hz, 1186168752Sscottl cdrunchangerqueue, changer); 118739213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 118839213Sgibbs } 118939213Sgibbs return; 119039213Sgibbs } 119139213Sgibbs 119239213Sgibbs /* 119339213Sgibbs * Check to see whether the current device has any I/O left 119439213Sgibbs * to do. If so, requeue it at the end of the queue. If 119539213Sgibbs * not, there is no need to requeue it. 119639213Sgibbs */ 119759249Sphk if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 119839213Sgibbs 119939213Sgibbs changer->cur_device->pinfo.generation = 120045442Sgibbs ++changer->devq.generation; 120139213Sgibbs camq_insert(&changer->devq, 120239213Sgibbs (cam_pinfo *)changer->cur_device); 120339213Sgibbs } 120439213Sgibbs } 120539213Sgibbs 120645845Sgibbs softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 120739213Sgibbs 120839213Sgibbs changer->cur_device = softc; 120939213Sgibbs 121039213Sgibbs changer->devq.qfrozen_cnt++; 121139213Sgibbs softc->flags |= CD_FLAG_ACTIVE; 121239213Sgibbs 121339213Sgibbs /* Just in case this device is waiting */ 121439213Sgibbs wakeup(&softc->changer); 1215198382Smav xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL); 121639213Sgibbs 121739213Sgibbs /* 121839213Sgibbs * Get rid of any pending timeouts, and set a flag to schedule new 121939213Sgibbs * ones so this device gets its full time quantum. 122039213Sgibbs */ 122139213Sgibbs if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1222168752Sscottl callout_stop(&changer->long_handle); 122339213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 122439213Sgibbs } 122539213Sgibbs 122639213Sgibbs if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1227168752Sscottl callout_stop(&changer->short_handle); 122839213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 122939213Sgibbs } 123039213Sgibbs 123139213Sgibbs /* 123239213Sgibbs * We need to schedule timeouts, but we only do this after the 123339213Sgibbs * first transaction has completed. This eliminates the changer 123439213Sgibbs * switch time. 123539213Sgibbs */ 123639213Sgibbs changer->flags |= CHANGER_NEED_TIMEOUT; 123739213Sgibbs} 123839213Sgibbs 123939213Sgibbsstatic void 124039213Sgibbscdchangerschedule(struct cd_softc *softc) 124139213Sgibbs{ 124239213Sgibbs struct cdchanger *changer; 124339213Sgibbs 124439213Sgibbs changer = softc->changer; 124539213Sgibbs 124639213Sgibbs /* 124739213Sgibbs * If this is a changer, and this is the current device, 124839213Sgibbs * and this device has at least the minimum time quantum to 124939213Sgibbs * run, see if we can switch it out. 125039213Sgibbs */ 125139213Sgibbs if ((softc->flags & CD_FLAG_ACTIVE) 125239213Sgibbs && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 125339213Sgibbs && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 125439213Sgibbs /* 125539213Sgibbs * We try three things here. The first is that we 125639213Sgibbs * check to see whether the schedule on completion 125739213Sgibbs * flag is set. If it is, we decrement the number 125839213Sgibbs * of buffers left, and if it's zero, we reschedule. 125939213Sgibbs * Next, we check to see whether the pending buffer 126039213Sgibbs * queue is empty and whether there are no 126139213Sgibbs * outstanding transactions. If so, we reschedule. 126239213Sgibbs * Next, we see if the pending buffer queue is empty. 126339213Sgibbs * If it is, we set the number of buffers left to 126439213Sgibbs * the current active buffer count and set the 126539213Sgibbs * schedule on complete flag. 126639213Sgibbs */ 126739213Sgibbs if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 126839213Sgibbs if (--softc->bufs_left == 0) { 126939213Sgibbs softc->changer->flags |= 127039213Sgibbs CHANGER_MANUAL_CALL; 127139213Sgibbs softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 127239213Sgibbs cdrunchangerqueue(softc->changer); 127339213Sgibbs } 127459249Sphk } else if ((bioq_first(&softc->bio_queue) == NULL) 1275112262Sphk && (softc->outstanding_cmds == 0)) { 127639213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 127739213Sgibbs cdrunchangerqueue(softc->changer); 127839213Sgibbs } 127939213Sgibbs } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 128039213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 128139213Sgibbs 128239213Sgibbs /* 128339213Sgibbs * Now that the first transaction to this 128439213Sgibbs * particular device has completed, we can go ahead 128539213Sgibbs * and schedule our timeouts. 128639213Sgibbs */ 128739213Sgibbs if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1288168752Sscottl callout_reset(&changer->long_handle, 1289168752Sscottl changer_max_busy_seconds * hz, 1290168752Sscottl cdrunchangerqueue, changer); 129139213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 129239213Sgibbs } else 129339213Sgibbs printf("cdchangerschedule: already have a long" 129439213Sgibbs " timeout!\n"); 129539213Sgibbs 129639213Sgibbs if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1297168752Sscottl callout_reset(&changer->short_handle, 1298168752Sscottl changer_min_busy_seconds * hz, 1299168752Sscottl cdshorttimeout, changer); 130039213Sgibbs changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 130139213Sgibbs } else 130239213Sgibbs printf("cdchangerschedule: already have a short " 130339213Sgibbs "timeout!\n"); 130439213Sgibbs 130539213Sgibbs /* 130639213Sgibbs * We just scheduled timeouts, no need to schedule 130739213Sgibbs * more. 130839213Sgibbs */ 130939213Sgibbs changer->flags &= ~CHANGER_NEED_TIMEOUT; 131039213Sgibbs 131139213Sgibbs } 131239213Sgibbs} 131339213Sgibbs 131439213Sgibbsstatic int 131539213Sgibbscdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 131639213Sgibbs u_int32_t cam_flags, 131739213Sgibbs u_int32_t sense_flags), 131839213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags) 131939213Sgibbs{ 132039213Sgibbs struct cd_softc *softc; 132139213Sgibbs struct cam_periph *periph; 132239213Sgibbs int error; 132339213Sgibbs 132439213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 132539213Sgibbs softc = (struct cd_softc *)periph->softc; 132639213Sgibbs 132739213Sgibbs error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1328125975Sphk softc->disk->d_devstat); 132939213Sgibbs 133039213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 133139213Sgibbs cdchangerschedule(softc); 133239213Sgibbs 133339213Sgibbs return(error); 133439213Sgibbs} 133539213Sgibbs 133642017Seivindstatic union ccb * 133739213Sgibbscdgetccb(struct cam_periph *periph, u_int32_t priority) 133839213Sgibbs{ 133939213Sgibbs struct cd_softc *softc; 134039213Sgibbs 134139213Sgibbs softc = (struct cd_softc *)periph->softc; 134239213Sgibbs 134339213Sgibbs if (softc->flags & CD_FLAG_CHANGER) { 134439213Sgibbs /* 134539213Sgibbs * This should work the first time this device is woken up, 134639213Sgibbs * but just in case it doesn't, we use a while loop. 134739213Sgibbs */ 134846581Sken while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 134939213Sgibbs /* 135039213Sgibbs * If this changer isn't already queued, queue it up. 135139213Sgibbs */ 135239213Sgibbs if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1353198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 135439213Sgibbs softc->pinfo.generation = 135545442Sgibbs ++softc->changer->devq.generation; 135639213Sgibbs camq_insert(&softc->changer->devq, 135739213Sgibbs (cam_pinfo *)softc); 135839213Sgibbs } 135946581Sken if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 136046581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 136146581Sken && ((softc->changer->flags 136246581Sken & CHANGER_SHORT_TMOUT_SCHED)==0)) { 136339213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 136439213Sgibbs cdrunchangerqueue(softc->changer); 136539213Sgibbs } else 1366168752Sscottl msleep(&softc->changer, periph->sim->mtx, 1367168752Sscottl PRIBIO, "cgticb", 0); 136839213Sgibbs } 136939213Sgibbs } 137039213Sgibbs return(cam_periph_getccb(periph, priority)); 137139213Sgibbs} 137239213Sgibbs 137339213Sgibbs 137439213Sgibbs/* 137539213Sgibbs * Actually translate the requested transfer into one the physical driver 137639213Sgibbs * can understand. The transfer is described by a buf and will include 137739213Sgibbs * only one physical transfer. 137839213Sgibbs */ 137939213Sgibbsstatic void 138059249Sphkcdstrategy(struct bio *bp) 138139213Sgibbs{ 138239213Sgibbs struct cam_periph *periph; 138339213Sgibbs struct cd_softc *softc; 138439213Sgibbs 1385120599Sphk periph = (struct cam_periph *)bp->bio_disk->d_drv1; 138639213Sgibbs if (periph == NULL) { 138776362Sphk biofinish(bp, NULL, ENXIO); 138876362Sphk return; 138939213Sgibbs } 139039213Sgibbs 1391168752Sscottl cam_periph_lock(periph); 139239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 139339213Sgibbs 139439213Sgibbs softc = (struct cd_softc *)periph->softc; 139539213Sgibbs 139639213Sgibbs /* 139739213Sgibbs * If the device has been made invalid, error out 139839213Sgibbs */ 139939213Sgibbs if ((softc->flags & CD_FLAG_INVALID)) { 1400168752Sscottl cam_periph_unlock(periph); 140176362Sphk biofinish(bp, NULL, ENXIO); 140276362Sphk return; 140339213Sgibbs } 140439213Sgibbs 1405111206Sken /* 1406111206Sken * If we don't have valid media, look for it before trying to 1407111206Sken * schedule the I/O. 1408111206Sken */ 1409111206Sken if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1410111206Sken int error; 1411111206Sken 1412111206Sken error = cdcheckmedia(periph); 1413111206Sken if (error != 0) { 1414168752Sscottl cam_periph_unlock(periph); 1415111206Sken biofinish(bp, NULL, error); 1416111206Sken return; 1417111206Sken } 1418111206Sken } 1419111206Sken 142039213Sgibbs /* 142139213Sgibbs * Place it in the queue of disk activities for this disk 142239213Sgibbs */ 1423112946Sphk bioq_disksort(&softc->bio_queue, bp); 142439213Sgibbs 142539213Sgibbs /* 142639213Sgibbs * Schedule ourselves for performing the work. We do things 142739213Sgibbs * differently for changers. 142839213Sgibbs */ 142939213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 1430198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 143139213Sgibbs else 1432198382Smav cdschedule(periph, CAM_PRIORITY_NORMAL); 143339213Sgibbs 1434168752Sscottl cam_periph_unlock(periph); 143539213Sgibbs return; 143639213Sgibbs} 143739213Sgibbs 143839213Sgibbsstatic void 143939213Sgibbscdstart(struct cam_periph *periph, union ccb *start_ccb) 144039213Sgibbs{ 144139213Sgibbs struct cd_softc *softc; 144259249Sphk struct bio *bp; 144339213Sgibbs struct ccb_scsiio *csio; 144439213Sgibbs struct scsi_read_capacity_data *rcap; 144539213Sgibbs 144639213Sgibbs softc = (struct cd_softc *)periph->softc; 144739213Sgibbs 144839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 144939213Sgibbs 145039213Sgibbs switch (softc->state) { 145139213Sgibbs case CD_STATE_NORMAL: 145239213Sgibbs { 145359249Sphk bp = bioq_first(&softc->bio_queue); 145439213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 145539213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 145639213Sgibbs 145739213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 145839213Sgibbs periph_links.sle); 145939213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 146039213Sgibbs wakeup(&periph->ccb_list); 146139213Sgibbs } else if (bp == NULL) { 146239213Sgibbs xpt_release_ccb(start_ccb); 146339213Sgibbs } else { 146459249Sphk bioq_remove(&softc->bio_queue, bp); 146539213Sgibbs 1466125975Sphk devstat_start_transaction_bio(softc->disk->d_devstat, bp); 146739213Sgibbs 146839213Sgibbs scsi_read_write(&start_ccb->csio, 1469186882Simp /*retries*/cd_retry_count, 147039213Sgibbs /* cbfcnp */ cddone, 147191062Sphk MSG_SIMPLE_Q_TAG, 147259249Sphk /* read */bp->bio_cmd == BIO_READ, 147339213Sgibbs /* byte2 */ 0, 147439213Sgibbs /* minimum_cmd_size */ 10, 1475121209Sphk /* lba */ bp->bio_offset / 1476121209Sphk softc->params.blksize, 147759249Sphk bp->bio_bcount / softc->params.blksize, 147859249Sphk /* data_ptr */ bp->bio_data, 147959249Sphk /* dxfer_len */ bp->bio_bcount, 148039213Sgibbs /* sense_len */ SSD_FULL_SIZE, 148139213Sgibbs /* timeout */ 30000); 148239213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 148339213Sgibbs 148439213Sgibbs 148539213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, 148639213Sgibbs &start_ccb->ccb_h, periph_links.le); 1487112262Sphk softc->outstanding_cmds++; 148839213Sgibbs 148939213Sgibbs /* We expect a unit attention from this device */ 149039213Sgibbs if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 149139213Sgibbs start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 149239213Sgibbs softc->flags &= ~CD_FLAG_RETRY_UA; 149339213Sgibbs } 149439213Sgibbs 149539213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 149659249Sphk bp = bioq_first(&softc->bio_queue); 149739213Sgibbs 149839213Sgibbs xpt_action(start_ccb); 149939213Sgibbs } 150039213Sgibbs if (bp != NULL) { 150139213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 1502198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 150339213Sgibbs } 150439213Sgibbs break; 150539213Sgibbs } 150639213Sgibbs case CD_STATE_PROBE: 150739213Sgibbs { 150839213Sgibbs 150939213Sgibbs rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1510169562Sscottl M_SCSICD, 151139213Sgibbs M_NOWAIT); 151239213Sgibbs if (rcap == NULL) { 1513164906Smjacob xpt_print(periph->path, 1514164906Smjacob "cdstart: Couldn't malloc read_capacity data\n"); 151539213Sgibbs /* cd_free_periph??? */ 151639213Sgibbs break; 151739213Sgibbs } 151839213Sgibbs csio = &start_ccb->csio; 151939213Sgibbs scsi_read_capacity(csio, 152039213Sgibbs /*retries*/1, 152139213Sgibbs cddone, 152239213Sgibbs MSG_SIMPLE_Q_TAG, 152339213Sgibbs rcap, 152439213Sgibbs SSD_FULL_SIZE, 152539213Sgibbs /*timeout*/20000); 152639213Sgibbs start_ccb->ccb_h.ccb_bp = NULL; 152739213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 152839213Sgibbs xpt_action(start_ccb); 152939213Sgibbs break; 153039213Sgibbs } 153139213Sgibbs } 153239213Sgibbs} 153339213Sgibbs 153439213Sgibbsstatic void 153539213Sgibbscddone(struct cam_periph *periph, union ccb *done_ccb) 153639213Sgibbs{ 153739213Sgibbs struct cd_softc *softc; 153839213Sgibbs struct ccb_scsiio *csio; 153939213Sgibbs 154039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 154139213Sgibbs 154239213Sgibbs softc = (struct cd_softc *)periph->softc; 154339213Sgibbs csio = &done_ccb->csio; 154439213Sgibbs 154539213Sgibbs switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 154639213Sgibbs case CD_CCB_BUFFER_IO: 154739213Sgibbs { 154859249Sphk struct bio *bp; 154939213Sgibbs int error; 155039213Sgibbs 155159249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 155239213Sgibbs error = 0; 155339213Sgibbs 155439213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 155539213Sgibbs int sf; 155639213Sgibbs 155739213Sgibbs if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 155839213Sgibbs sf = SF_RETRY_UA; 155939213Sgibbs else 156039213Sgibbs sf = 0; 156146747Sken 156274840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 156374840Sken if (error == ERESTART) { 156439213Sgibbs /* 156539213Sgibbs * A retry was scheuled, so 156639213Sgibbs * just return. 156739213Sgibbs */ 156839213Sgibbs return; 156939213Sgibbs } 157039213Sgibbs } 157139213Sgibbs 157239213Sgibbs if (error != 0) { 1573164906Smjacob xpt_print(periph->path, 1574164906Smjacob "cddone: got error %#x back\n", error); 1575112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 157659249Sphk bp->bio_resid = bp->bio_bcount; 157759249Sphk bp->bio_error = error; 157859249Sphk bp->bio_flags |= BIO_ERROR; 1579199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1580199279Smav cam_release_devq(done_ccb->ccb_h.path, 158139213Sgibbs /*relsim_flags*/0, 158239213Sgibbs /*reduction*/0, 158339213Sgibbs /*timeout*/0, 158439213Sgibbs /*getcount_only*/0); 158539213Sgibbs 158639213Sgibbs } else { 158759249Sphk bp->bio_resid = csio->resid; 158859249Sphk bp->bio_error = 0; 158959249Sphk if (bp->bio_resid != 0) { 1590104456Sphk /* 1591104456Sphk * Short transfer ??? 1592104456Sphk * XXX: not sure this is correct for partial 1593104456Sphk * transfers at EOM 1594104456Sphk */ 159559249Sphk bp->bio_flags |= BIO_ERROR; 159639213Sgibbs } 159739213Sgibbs } 159839213Sgibbs 159939213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1600112262Sphk softc->outstanding_cmds--; 160139213Sgibbs 160239213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 160339213Sgibbs cdchangerschedule(softc); 160439213Sgibbs 1605157806Smaxim biofinish(bp, NULL, 0); 160639213Sgibbs break; 160739213Sgibbs } 160839213Sgibbs case CD_CCB_PROBE: 160939213Sgibbs { 161039213Sgibbs struct scsi_read_capacity_data *rdcap; 161139213Sgibbs char announce_buf[120]; /* 161239213Sgibbs * Currently (9/30/97) the 161339213Sgibbs * longest possible announce 161439213Sgibbs * buffer is 108 bytes, for the 161539213Sgibbs * first error case below. 161639213Sgibbs * That is 39 bytes for the 161739213Sgibbs * basic string, 16 bytes for the 161839213Sgibbs * biggest sense key (hardware 161939213Sgibbs * error), 52 bytes for the 162039213Sgibbs * text of the largest sense 162139213Sgibbs * qualifier valid for a CDROM, 162239213Sgibbs * (0x72, 0x03 or 0x04, 162339213Sgibbs * 0x03), and one byte for the 162439213Sgibbs * null terminating character. 162539213Sgibbs * To allow for longer strings, 162639213Sgibbs * the announce buffer is 120 162739213Sgibbs * bytes. 162839213Sgibbs */ 162939213Sgibbs struct cd_params *cdp; 163039213Sgibbs 163139213Sgibbs cdp = &softc->params; 163239213Sgibbs 163339213Sgibbs rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 163439213Sgibbs 163539213Sgibbs cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 163639213Sgibbs cdp->blksize = scsi_4btoul (rdcap->length); 163739213Sgibbs 163839213Sgibbs if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 163939213Sgibbs 164041514Sarchie snprintf(announce_buf, sizeof(announce_buf), 164140020Sken "cd present [%lu x %lu byte records]", 164240020Sken cdp->disksize, (u_long)cdp->blksize); 164339213Sgibbs 164439213Sgibbs } else { 164539213Sgibbs int error; 164639213Sgibbs /* 164739213Sgibbs * Retry any UNIT ATTENTION type errors. They 164839213Sgibbs * are expected at boot. 164939213Sgibbs */ 165074840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, 165174840Sken SF_RETRY_UA | SF_NO_PRINT); 165239213Sgibbs if (error == ERESTART) { 165339213Sgibbs /* 165439213Sgibbs * A retry was scheuled, so 165539213Sgibbs * just return. 165639213Sgibbs */ 165739213Sgibbs return; 165839213Sgibbs } else if (error != 0) { 165939213Sgibbs 166039213Sgibbs struct scsi_sense_data *sense; 166139213Sgibbs int asc, ascq; 166239213Sgibbs int sense_key, error_code; 166339213Sgibbs int have_sense; 166439213Sgibbs cam_status status; 166539213Sgibbs struct ccb_getdev cgd; 166639213Sgibbs 166739213Sgibbs /* Don't wedge this device's queue */ 1668199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1669199279Smav cam_release_devq(done_ccb->ccb_h.path, 167039213Sgibbs /*relsim_flags*/0, 167139213Sgibbs /*reduction*/0, 167239213Sgibbs /*timeout*/0, 167339213Sgibbs /*getcount_only*/0); 167439213Sgibbs 167539213Sgibbs status = done_ccb->ccb_h.status; 167639213Sgibbs 167739213Sgibbs xpt_setup_ccb(&cgd.ccb_h, 167839213Sgibbs done_ccb->ccb_h.path, 1679198382Smav CAM_PRIORITY_NORMAL); 168039213Sgibbs cgd.ccb_h.func_code = XPT_GDEV_TYPE; 168139213Sgibbs xpt_action((union ccb *)&cgd); 168239213Sgibbs 168339213Sgibbs if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 168439213Sgibbs || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 168539213Sgibbs || ((status & CAM_AUTOSNS_VALID) == 0)) 168639213Sgibbs have_sense = FALSE; 168739213Sgibbs else 168839213Sgibbs have_sense = TRUE; 168939213Sgibbs 169039213Sgibbs if (have_sense) { 169139213Sgibbs sense = &csio->sense_data; 169239213Sgibbs scsi_extract_sense(sense, &error_code, 169339213Sgibbs &sense_key, 169439213Sgibbs &asc, &ascq); 169539213Sgibbs } 169639213Sgibbs /* 169746581Sken * Attach to anything that claims to be a 169846581Sken * CDROM or WORM device, as long as it 169946581Sken * doesn't return a "Logical unit not 170046581Sken * supported" (0x25) error. 170139213Sgibbs */ 170246581Sken if ((have_sense) && (asc != 0x25) 170374840Sken && (error_code == SSD_CURRENT_ERROR)) { 170474840Sken const char *sense_key_desc; 170574840Sken const char *asc_desc; 170674840Sken 170774840Sken scsi_sense_desc(sense_key, asc, ascq, 170874840Sken &cgd.inq_data, 170974840Sken &sense_key_desc, 171074840Sken &asc_desc); 171141514Sarchie snprintf(announce_buf, 171241514Sarchie sizeof(announce_buf), 171339213Sgibbs "Attempt to query device " 171439213Sgibbs "size failed: %s, %s", 171574840Sken sense_key_desc, 171674840Sken asc_desc); 171782850Sken } else if ((have_sense == 0) 171882850Sken && ((status & CAM_STATUS_MASK) == 171982850Sken CAM_SCSI_STATUS_ERROR) 172082850Sken && (csio->scsi_status == 172182850Sken SCSI_STATUS_BUSY)) { 172282850Sken snprintf(announce_buf, 172382850Sken sizeof(announce_buf), 172482850Sken "Attempt to query device " 172582850Sken "size failed: SCSI Status: %s", 172682850Sken scsi_status_string(csio)); 172774840Sken } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 172839213Sgibbs /* 172939213Sgibbs * We only print out an error for 173039213Sgibbs * CDROM type devices. For WORM 173139213Sgibbs * devices, we don't print out an 173239213Sgibbs * error since a few WORM devices 173339213Sgibbs * don't support CDROM commands. 173439213Sgibbs * If we have sense information, go 173539213Sgibbs * ahead and print it out. 173639213Sgibbs * Otherwise, just say that we 173739213Sgibbs * couldn't attach. 173839213Sgibbs */ 173939213Sgibbs 174039213Sgibbs /* 174139213Sgibbs * Just print out the error, not 174239213Sgibbs * the full probe message, when we 174339213Sgibbs * don't attach. 174439213Sgibbs */ 174540020Sken if (have_sense) 174640020Sken scsi_sense_print( 174740020Sken &done_ccb->csio); 174840020Sken else { 1749164906Smjacob xpt_print(periph->path, 1750164906Smjacob "got CAM status %#x\n", 1751164906Smjacob done_ccb->ccb_h.status); 175240020Sken } 1753164906Smjacob xpt_print(periph->path, "fatal error, " 1754164906Smjacob "failed to attach to device\n"); 175539213Sgibbs /* 175640603Sken * Invalidate this peripheral. 175739213Sgibbs */ 175839213Sgibbs cam_periph_invalidate(periph); 175940020Sken 176040020Sken announce_buf[0] = '\0'; 176139213Sgibbs } else { 176240603Sken 176339213Sgibbs /* 176440603Sken * Invalidate this peripheral. 176539213Sgibbs */ 176639213Sgibbs cam_periph_invalidate(periph); 176740020Sken announce_buf[0] = '\0'; 176839213Sgibbs } 176939213Sgibbs } 177039213Sgibbs } 1771169562Sscottl free(rdcap, M_SCSICD); 177242378Smjacob if (announce_buf[0] != '\0') { 177339213Sgibbs xpt_announce_periph(periph, announce_buf); 177442378Smjacob if (softc->flags & CD_FLAG_CHANGER) 177542378Smjacob cdchangerschedule(softc); 1776119708Sken /* 1777119708Sken * Create our sysctl variables, now that we know 1778119708Sken * we have successfully attached. 1779119708Sken */ 1780119708Sken taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 178142378Smjacob } 178240020Sken softc->state = CD_STATE_NORMAL; 178342378Smjacob /* 178442378Smjacob * Since our peripheral may be invalidated by an error 178542378Smjacob * above or an external event, we must release our CCB 178642378Smjacob * before releasing the probe lock on the peripheral. 178742378Smjacob * The peripheral will only go away once the last lock 178842378Smjacob * is removed, and we need it around for the CCB release 178942378Smjacob * operation. 179042378Smjacob */ 179142378Smjacob xpt_release_ccb(done_ccb); 1792168752Sscottl cam_periph_unhold(periph); 179342378Smjacob return; 179439213Sgibbs } 179539213Sgibbs case CD_CCB_WAITING: 179639213Sgibbs { 179739213Sgibbs /* Caller will release the CCB */ 179839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 179939213Sgibbs ("trying to wakeup ccbwait\n")); 180039213Sgibbs 180139213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 180239213Sgibbs return; 180339213Sgibbs } 180442378Smjacob default: 180542378Smjacob break; 180639213Sgibbs } 180739213Sgibbs xpt_release_ccb(done_ccb); 180839213Sgibbs} 180939213Sgibbs 1810111206Skenstatic union cd_pages * 1811111206Skencdgetpage(struct cd_mode_params *mode_params) 1812111206Sken{ 1813111206Sken union cd_pages *page; 1814111206Sken 1815111206Sken if (mode_params->cdb_size == 10) 1816111206Sken page = (union cd_pages *)find_mode_page_10( 1817111206Sken (struct scsi_mode_header_10 *)mode_params->mode_buf); 1818111206Sken else 1819111206Sken page = (union cd_pages *)find_mode_page_6( 1820111206Sken (struct scsi_mode_header_6 *)mode_params->mode_buf); 1821111206Sken 1822111206Sken return (page); 1823111206Sken} 1824111206Sken 182539213Sgibbsstatic int 1826111206Skencdgetpagesize(int page_num) 1827111206Sken{ 1828111206Sken int i; 1829111206Sken 1830111206Sken for (i = 0; i < (sizeof(cd_page_size_table)/ 1831111206Sken sizeof(cd_page_size_table[0])); i++) { 1832111206Sken if (cd_page_size_table[i].page == page_num) 1833111206Sken return (cd_page_size_table[i].page_size); 1834111206Sken } 1835111206Sken 1836111206Sken return (-1); 1837111206Sken} 1838111206Sken 1839111206Skenstatic int 1840120599Sphkcdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 184139213Sgibbs{ 184239213Sgibbs 184339213Sgibbs struct cam_periph *periph; 184439213Sgibbs struct cd_softc *softc; 1845168752Sscottl int nocopyout, error = 0; 184639213Sgibbs 1847120599Sphk periph = (struct cam_periph *)dp->d_drv1; 184839213Sgibbs if (periph == NULL) 184939213Sgibbs return(ENXIO); 185039213Sgibbs 1851168752Sscottl cam_periph_lock(periph); 185239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 185339213Sgibbs 185439213Sgibbs softc = (struct cd_softc *)periph->softc; 185539213Sgibbs 185639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 185740020Sken ("trying to do ioctl %#lx\n", cmd)); 185839213Sgibbs 1859168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1860168752Sscottl cam_periph_unlock(periph); 1861168752Sscottl cam_periph_release(periph); 1862168752Sscottl return (error); 1863168752Sscottl } 186439213Sgibbs 1865111206Sken /* 1866111206Sken * If we don't have media loaded, check for it. If still don't 1867111206Sken * have media loaded, we can only do a load or eject. 1868144135Sken * 1869144135Sken * We only care whether media is loaded if this is a cd-specific ioctl 1870144135Sken * (thus the IOCGROUP check below). Note that this will break if 1871144135Sken * anyone adds any ioctls into the switch statement below that don't 1872144135Sken * have their ioctl group set to 'c'. 1873111206Sken */ 1874111206Sken if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1875111206Sken && ((cmd != CDIOCCLOSE) 1876144135Sken && (cmd != CDIOCEJECT)) 1877144135Sken && (IOCGROUP(cmd) == 'c')) { 1878111206Sken error = cdcheckmedia(periph); 1879171529Skan if (error != 0) { 1880171529Skan cam_periph_unhold(periph); 1881171529Skan cam_periph_unlock(periph); 1882171529Skan return (error); 1883171529Skan } 1884111206Sken } 1885168752Sscottl /* 1886168752Sscottl * Drop the lock here so later mallocs can use WAITOK. The periph 1887168752Sscottl * is essentially locked still with the cam_periph_hold call above. 1888168752Sscottl */ 1889168752Sscottl cam_periph_unlock(periph); 189040020Sken 1891141031Ssobomax nocopyout = 0; 189239213Sgibbs switch (cmd) { 189339213Sgibbs 189439213Sgibbs case CDIOCPLAYTRACKS: 189539213Sgibbs { 189639213Sgibbs struct ioc_play_track *args 189739213Sgibbs = (struct ioc_play_track *) addr; 1898111206Sken struct cd_mode_params params; 1899111206Sken union cd_pages *page; 190039213Sgibbs 1901111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 1902169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1903111206Sken M_WAITOK | M_ZERO); 190439213Sgibbs 1905168752Sscottl cam_periph_lock(periph); 190639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 190739213Sgibbs ("trying to do CDIOCPLAYTRACKS\n")); 190839213Sgibbs 1909111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 191039213Sgibbs if (error) { 1911169562Sscottl free(params.mode_buf, M_SCSICD); 1912168752Sscottl cam_periph_unlock(periph); 191339213Sgibbs break; 191439213Sgibbs } 1915111206Sken page = cdgetpage(¶ms); 1916111206Sken 1917111206Sken page->audio.flags &= ~CD_PA_SOTC; 1918111206Sken page->audio.flags |= CD_PA_IMMED; 1919111206Sken error = cdsetmode(periph, ¶ms); 1920169562Sscottl free(params.mode_buf, M_SCSICD); 1921168752Sscottl if (error) { 1922168752Sscottl cam_periph_unlock(periph); 192339213Sgibbs break; 1924168752Sscottl } 1925111206Sken 1926111206Sken /* 1927111206Sken * This was originally implemented with the PLAY 1928111206Sken * AUDIO TRACK INDEX command, but that command was 1929111206Sken * deprecated after SCSI-2. Most (all?) SCSI CDROM 1930111206Sken * drives support it but ATAPI and ATAPI-derivative 1931111206Sken * drives don't seem to support it. So we keep a 1932111206Sken * cache of the table of contents and translate 1933111206Sken * track numbers to MSF format. 1934111206Sken */ 1935111206Sken if (softc->flags & CD_FLAG_VALID_TOC) { 1936111206Sken union msf_lba *sentry, *eentry; 1937111206Sken int st, et; 1938111206Sken 1939111206Sken if (args->end_track < 1940111206Sken softc->toc.header.ending_track + 1) 1941111206Sken args->end_track++; 1942111206Sken if (args->end_track > 1943111206Sken softc->toc.header.ending_track + 1) 1944111206Sken args->end_track = 1945111206Sken softc->toc.header.ending_track + 1; 1946111206Sken st = args->start_track - 1947111206Sken softc->toc.header.starting_track; 1948111206Sken et = args->end_track - 1949111206Sken softc->toc.header.starting_track; 1950111206Sken if ((st < 0) 1951111206Sken || (et < 0) 1952111206Sken || (st > (softc->toc.header.ending_track - 1953111206Sken softc->toc.header.starting_track))) { 1954111206Sken error = EINVAL; 1955111206Sken break; 1956111206Sken } 1957111206Sken sentry = &softc->toc.entries[st].addr; 1958111206Sken eentry = &softc->toc.entries[et].addr; 1959111206Sken error = cdplaymsf(periph, 1960111206Sken sentry->msf.minute, 1961111206Sken sentry->msf.second, 1962111206Sken sentry->msf.frame, 1963111206Sken eentry->msf.minute, 1964111206Sken eentry->msf.second, 1965111206Sken eentry->msf.frame); 1966111206Sken } else { 1967111206Sken /* 1968111206Sken * If we don't have a valid TOC, try the 1969111206Sken * play track index command. It is part of 1970111206Sken * the SCSI-2 spec, but was removed in the 1971111206Sken * MMC specs. ATAPI and ATAPI-derived 1972111206Sken * drives don't support it. 1973111206Sken */ 1974111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 1975111206Sken args->start_track = 1976111206Sken bin2bcd(args->start_track); 1977111206Sken args->end_track = 1978111206Sken bin2bcd(args->end_track); 1979111206Sken } 1980111206Sken error = cdplaytracks(periph, 1981111206Sken args->start_track, 1982111206Sken args->start_index, 1983111206Sken args->end_track, 1984111206Sken args->end_index); 198539213Sgibbs } 1986168752Sscottl cam_periph_unlock(periph); 198739213Sgibbs } 198839213Sgibbs break; 198939213Sgibbs case CDIOCPLAYMSF: 199039213Sgibbs { 199139213Sgibbs struct ioc_play_msf *args 199239213Sgibbs = (struct ioc_play_msf *) addr; 1993111206Sken struct cd_mode_params params; 1994111206Sken union cd_pages *page; 199539213Sgibbs 1996111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 1997169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1998111206Sken M_WAITOK | M_ZERO); 199939213Sgibbs 2000168752Sscottl cam_periph_lock(periph); 200139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 200239213Sgibbs ("trying to do CDIOCPLAYMSF\n")); 200339213Sgibbs 2004111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 200539213Sgibbs if (error) { 2006169562Sscottl free(params.mode_buf, M_SCSICD); 2007168752Sscottl cam_periph_unlock(periph); 200839213Sgibbs break; 200939213Sgibbs } 2010111206Sken page = cdgetpage(¶ms); 2011111206Sken 2012111206Sken page->audio.flags &= ~CD_PA_SOTC; 2013111206Sken page->audio.flags |= CD_PA_IMMED; 2014111206Sken error = cdsetmode(periph, ¶ms); 2015169562Sscottl free(params.mode_buf, M_SCSICD); 2016168752Sscottl if (error) { 2017168752Sscottl cam_periph_unlock(periph); 201839213Sgibbs break; 2019168752Sscottl } 202039213Sgibbs error = cdplaymsf(periph, 202139213Sgibbs args->start_m, 202239213Sgibbs args->start_s, 202339213Sgibbs args->start_f, 202439213Sgibbs args->end_m, 202539213Sgibbs args->end_s, 202639213Sgibbs args->end_f); 2027168752Sscottl cam_periph_unlock(periph); 202839213Sgibbs } 202939213Sgibbs break; 203039213Sgibbs case CDIOCPLAYBLOCKS: 203139213Sgibbs { 203239213Sgibbs struct ioc_play_blocks *args 203339213Sgibbs = (struct ioc_play_blocks *) addr; 2034111206Sken struct cd_mode_params params; 2035111206Sken union cd_pages *page; 203639213Sgibbs 2037111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2038169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2039111206Sken M_WAITOK | M_ZERO); 204039213Sgibbs 2041168752Sscottl cam_periph_lock(periph); 2042168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2043168752Sscottl ("trying to do CDIOCPLAYBLOCKS\n")); 2044168752Sscottl 2045168752Sscottl 2046111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 204739213Sgibbs if (error) { 2048169562Sscottl free(params.mode_buf, M_SCSICD); 2049168752Sscottl cam_periph_unlock(periph); 205039213Sgibbs break; 205139213Sgibbs } 2052111206Sken page = cdgetpage(¶ms); 2053111206Sken 2054111206Sken page->audio.flags &= ~CD_PA_SOTC; 2055111206Sken page->audio.flags |= CD_PA_IMMED; 2056111206Sken error = cdsetmode(periph, ¶ms); 2057169562Sscottl free(params.mode_buf, M_SCSICD); 2058168752Sscottl if (error) { 2059168752Sscottl cam_periph_unlock(periph); 206039213Sgibbs break; 2061168752Sscottl } 206239213Sgibbs error = cdplay(periph, args->blk, args->len); 2063168752Sscottl cam_periph_unlock(periph); 206439213Sgibbs } 206539213Sgibbs break; 2066141031Ssobomax case CDIOCREADSUBCHANNEL_SYSSPACE: 2067141031Ssobomax nocopyout = 1; 2068141031Ssobomax /* Fallthrough */ 206939213Sgibbs case CDIOCREADSUBCHANNEL: 207039213Sgibbs { 207139213Sgibbs struct ioc_read_subchannel *args 207239213Sgibbs = (struct ioc_read_subchannel *) addr; 207339213Sgibbs struct cd_sub_channel_info *data; 207439213Sgibbs u_int32_t len = args->data_len; 207539213Sgibbs 2076168752Sscottl data = malloc(sizeof(struct cd_sub_channel_info), 2077169562Sscottl M_SCSICD, M_WAITOK); 2078168752Sscottl 2079168752Sscottl cam_periph_lock(periph); 208039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 208139213Sgibbs ("trying to do CDIOCREADSUBCHANNEL\n")); 208239213Sgibbs 208339213Sgibbs if ((len > sizeof(struct cd_sub_channel_info)) || 208439213Sgibbs (len < sizeof(struct cd_sub_channel_header))) { 208539213Sgibbs printf( 208639213Sgibbs "scsi_cd: cdioctl: " 208739213Sgibbs "cdioreadsubchannel: error, len=%d\n", 208839213Sgibbs len); 208939213Sgibbs error = EINVAL; 2090169562Sscottl free(data, M_SCSICD); 2091168752Sscottl cam_periph_unlock(periph); 209239213Sgibbs break; 209339213Sgibbs } 209439213Sgibbs 209539213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 209639213Sgibbs args->track = bin2bcd(args->track); 209739213Sgibbs 209839213Sgibbs error = cdreadsubchannel(periph, args->address_format, 209939213Sgibbs args->data_format, args->track, data, len); 210039213Sgibbs 210139213Sgibbs if (error) { 2102169562Sscottl free(data, M_SCSICD); 2103168752Sscottl cam_periph_unlock(periph); 210439213Sgibbs break; 210539213Sgibbs } 210639213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 210739213Sgibbs data->what.track_info.track_number = 210839213Sgibbs bcd2bin(data->what.track_info.track_number); 210939213Sgibbs len = min(len, ((data->header.data_len[0] << 8) + 211039213Sgibbs data->header.data_len[1] + 211139213Sgibbs sizeof(struct cd_sub_channel_header))); 2112168752Sscottl cam_periph_unlock(periph); 2113141031Ssobomax if (nocopyout == 0) { 2114141031Ssobomax if (copyout(data, args->data, len) != 0) { 2115141031Ssobomax error = EFAULT; 2116141031Ssobomax } 2117141031Ssobomax } else { 2118141031Ssobomax bcopy(data, args->data, len); 211939213Sgibbs } 2120169562Sscottl free(data, M_SCSICD); 212139213Sgibbs } 212239213Sgibbs break; 212339213Sgibbs 212439213Sgibbs case CDIOREADTOCHEADER: 212539213Sgibbs { 212639213Sgibbs struct ioc_toc_header *th; 212739213Sgibbs 2128169562Sscottl th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2129168752Sscottl M_WAITOK); 2130168752Sscottl 2131168752Sscottl cam_periph_lock(periph); 213239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 213339213Sgibbs ("trying to do CDIOREADTOCHEADER\n")); 213439213Sgibbs 2135111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2136111206Sken sizeof (*th), /*sense_flags*/0); 213739213Sgibbs if (error) { 2138169562Sscottl free(th, M_SCSICD); 2139168752Sscottl cam_periph_unlock(periph); 214039213Sgibbs break; 214139213Sgibbs } 214239213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 214339213Sgibbs /* we are going to have to convert the BCD 214439213Sgibbs * encoding on the cd to what is expected 214539213Sgibbs */ 214639213Sgibbs th->starting_track = 214739213Sgibbs bcd2bin(th->starting_track); 214839213Sgibbs th->ending_track = bcd2bin(th->ending_track); 214939213Sgibbs } 215090868Smike th->len = ntohs(th->len); 215139213Sgibbs bcopy(th, addr, sizeof(*th)); 2152169562Sscottl free(th, M_SCSICD); 2153168752Sscottl cam_periph_unlock(periph); 215439213Sgibbs } 215539213Sgibbs break; 215639213Sgibbs case CDIOREADTOCENTRYS: 215739213Sgibbs { 2158111206Sken struct cd_tocdata *data; 2159111206Sken struct cd_toc_single *lead; 216039213Sgibbs struct ioc_read_toc_entry *te = 216139213Sgibbs (struct ioc_read_toc_entry *) addr; 216239213Sgibbs struct ioc_toc_header *th; 216339213Sgibbs u_int32_t len, readlen, idx, num; 216439213Sgibbs u_int32_t starting_track = te->starting_track; 216539213Sgibbs 2166169562Sscottl data = malloc(sizeof(*data), M_SCSICD, M_WAITOK); 2167169562Sscottl lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK); 2168168752Sscottl 2169168752Sscottl cam_periph_lock(periph); 217039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 217139213Sgibbs ("trying to do CDIOREADTOCENTRYS\n")); 217239213Sgibbs 217339213Sgibbs if (te->data_len < sizeof(struct cd_toc_entry) 217439213Sgibbs || (te->data_len % sizeof(struct cd_toc_entry)) != 0 217539213Sgibbs || (te->address_format != CD_MSF_FORMAT 217639213Sgibbs && te->address_format != CD_LBA_FORMAT)) { 217739213Sgibbs error = EINVAL; 217839213Sgibbs printf("scsi_cd: error in readtocentries, " 217939213Sgibbs "returning EINVAL\n"); 2180169562Sscottl free(data, M_SCSICD); 2181169562Sscottl free(lead, M_SCSICD); 2182168752Sscottl cam_periph_unlock(periph); 218339213Sgibbs break; 218439213Sgibbs } 218539213Sgibbs 218639213Sgibbs th = &data->header; 2187111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2188111206Sken sizeof (*th), /*sense_flags*/0); 218939213Sgibbs if (error) { 2190169562Sscottl free(data, M_SCSICD); 2191169562Sscottl free(lead, M_SCSICD); 2192168752Sscottl cam_periph_unlock(periph); 219339213Sgibbs break; 219439213Sgibbs } 219539213Sgibbs 219639213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 219739213Sgibbs /* we are going to have to convert the BCD 219839213Sgibbs * encoding on the cd to what is expected 219939213Sgibbs */ 220039213Sgibbs th->starting_track = 220139213Sgibbs bcd2bin(th->starting_track); 220239213Sgibbs th->ending_track = bcd2bin(th->ending_track); 220339213Sgibbs } 220439213Sgibbs 220539213Sgibbs if (starting_track == 0) 220639213Sgibbs starting_track = th->starting_track; 220739213Sgibbs else if (starting_track == LEADOUT) 220839213Sgibbs starting_track = th->ending_track + 1; 220939213Sgibbs else if (starting_track < th->starting_track || 221039213Sgibbs starting_track > th->ending_track + 1) { 221139213Sgibbs printf("scsi_cd: error in readtocentries, " 221239213Sgibbs "returning EINVAL\n"); 2213169562Sscottl free(data, M_SCSICD); 2214169562Sscottl free(lead, M_SCSICD); 2215168752Sscottl cam_periph_unlock(periph); 221639213Sgibbs error = EINVAL; 221739213Sgibbs break; 221839213Sgibbs } 221939213Sgibbs 222039213Sgibbs /* calculate reading length without leadout entry */ 222139213Sgibbs readlen = (th->ending_track - starting_track + 1) * 222239213Sgibbs sizeof(struct cd_toc_entry); 222339213Sgibbs 222439213Sgibbs /* and with leadout entry */ 222539213Sgibbs len = readlen + sizeof(struct cd_toc_entry); 222639213Sgibbs if (te->data_len < len) { 222739213Sgibbs len = te->data_len; 222839213Sgibbs if (readlen > len) 222939213Sgibbs readlen = len; 223039213Sgibbs } 223139213Sgibbs if (len > sizeof(data->entries)) { 223239213Sgibbs printf("scsi_cd: error in readtocentries, " 223339213Sgibbs "returning EINVAL\n"); 223439213Sgibbs error = EINVAL; 2235169562Sscottl free(data, M_SCSICD); 2236169562Sscottl free(lead, M_SCSICD); 2237168752Sscottl cam_periph_unlock(periph); 223839213Sgibbs break; 223939213Sgibbs } 224039213Sgibbs num = len / sizeof(struct cd_toc_entry); 224139213Sgibbs 224239213Sgibbs if (readlen > 0) { 224339213Sgibbs error = cdreadtoc(periph, te->address_format, 224439213Sgibbs starting_track, 2245111206Sken (u_int8_t *)data, 2246111206Sken readlen + sizeof (*th), 2247111206Sken /*sense_flags*/0); 224839213Sgibbs if (error) { 2249169562Sscottl free(data, M_SCSICD); 2250169562Sscottl free(lead, M_SCSICD); 2251168752Sscottl cam_periph_unlock(periph); 225239213Sgibbs break; 225339213Sgibbs } 225439213Sgibbs } 225539213Sgibbs 225639213Sgibbs /* make leadout entry if needed */ 225739213Sgibbs idx = starting_track + num - 1; 225839213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 225939213Sgibbs th->ending_track = bcd2bin(th->ending_track); 226039213Sgibbs if (idx == th->ending_track + 1) { 226139213Sgibbs error = cdreadtoc(periph, te->address_format, 2262111206Sken LEADOUT, (u_int8_t *)lead, 2263111206Sken sizeof(*lead), 2264111206Sken /*sense_flags*/0); 226539213Sgibbs if (error) { 2266169562Sscottl free(data, M_SCSICD); 2267169562Sscottl free(lead, M_SCSICD); 2268168752Sscottl cam_periph_unlock(periph); 226939213Sgibbs break; 227039213Sgibbs } 227139213Sgibbs data->entries[idx - starting_track] = 227239213Sgibbs lead->entry; 227339213Sgibbs } 227439213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 227539213Sgibbs for (idx = 0; idx < num - 1; idx++) { 227639213Sgibbs data->entries[idx].track = 227739213Sgibbs bcd2bin(data->entries[idx].track); 227839213Sgibbs } 227939213Sgibbs } 228039213Sgibbs 2281168752Sscottl cam_periph_unlock(periph); 228239213Sgibbs error = copyout(data->entries, te->data, len); 2283169562Sscottl free(data, M_SCSICD); 2284169562Sscottl free(lead, M_SCSICD); 228539213Sgibbs } 228639213Sgibbs break; 228739213Sgibbs case CDIOREADTOCENTRY: 228839213Sgibbs { 2289111206Sken struct cd_toc_single *data; 229039213Sgibbs struct ioc_read_toc_single_entry *te = 229139213Sgibbs (struct ioc_read_toc_single_entry *) addr; 229239213Sgibbs struct ioc_toc_header *th; 229339213Sgibbs u_int32_t track; 229439213Sgibbs 2295169562Sscottl data = malloc(sizeof(*data), M_SCSICD, M_WAITOK); 2296168752Sscottl 2297168752Sscottl cam_periph_lock(periph); 229839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 229939213Sgibbs ("trying to do CDIOREADTOCENTRY\n")); 230039213Sgibbs 230139213Sgibbs if (te->address_format != CD_MSF_FORMAT 230239213Sgibbs && te->address_format != CD_LBA_FORMAT) { 230339213Sgibbs printf("error in readtocentry, " 230439213Sgibbs " returning EINVAL\n"); 2305169562Sscottl free(data, M_SCSICD); 230639213Sgibbs error = EINVAL; 2307168752Sscottl cam_periph_unlock(periph); 230839213Sgibbs break; 230939213Sgibbs } 231039213Sgibbs 231139213Sgibbs th = &data->header; 2312111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2313111206Sken sizeof (*th), /*sense_flags*/0); 231439213Sgibbs if (error) { 2315169562Sscottl free(data, M_SCSICD); 2316168752Sscottl cam_periph_unlock(periph); 231739213Sgibbs break; 231839213Sgibbs } 231939213Sgibbs 232039213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 232139213Sgibbs /* we are going to have to convert the BCD 232239213Sgibbs * encoding on the cd to what is expected 232339213Sgibbs */ 232439213Sgibbs th->starting_track = 232539213Sgibbs bcd2bin(th->starting_track); 232639213Sgibbs th->ending_track = bcd2bin(th->ending_track); 232739213Sgibbs } 232839213Sgibbs track = te->track; 232939213Sgibbs if (track == 0) 233039213Sgibbs track = th->starting_track; 233139213Sgibbs else if (track == LEADOUT) 233239213Sgibbs /* OK */; 233339213Sgibbs else if (track < th->starting_track || 233439213Sgibbs track > th->ending_track + 1) { 233539213Sgibbs printf("error in readtocentry, " 233639213Sgibbs " returning EINVAL\n"); 2337169562Sscottl free(data, M_SCSICD); 233839213Sgibbs error = EINVAL; 2339168752Sscottl cam_periph_unlock(periph); 234039213Sgibbs break; 234139213Sgibbs } 234239213Sgibbs 234339213Sgibbs error = cdreadtoc(periph, te->address_format, track, 2344111206Sken (u_int8_t *)data, sizeof(*data), 2345111206Sken /*sense_flags*/0); 234639213Sgibbs if (error) { 2347169562Sscottl free(data, M_SCSICD); 2348168752Sscottl cam_periph_unlock(periph); 234939213Sgibbs break; 235039213Sgibbs } 235139213Sgibbs 235239213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 235339213Sgibbs data->entry.track = bcd2bin(data->entry.track); 235439213Sgibbs bcopy(&data->entry, &te->entry, 235539213Sgibbs sizeof(struct cd_toc_entry)); 2356169562Sscottl free(data, M_SCSICD); 2357168752Sscottl cam_periph_unlock(periph); 235839213Sgibbs } 235939213Sgibbs break; 236039213Sgibbs case CDIOCSETPATCH: 236139213Sgibbs { 2362111206Sken struct ioc_patch *arg = (struct ioc_patch *)addr; 2363111206Sken struct cd_mode_params params; 2364111206Sken union cd_pages *page; 236539213Sgibbs 2366168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2367169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2368168752Sscottl M_WAITOK | M_ZERO); 2369168752Sscottl 2370168752Sscottl cam_periph_lock(periph); 237139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 237239213Sgibbs ("trying to do CDIOCSETPATCH\n")); 237339213Sgibbs 2374111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 237539213Sgibbs if (error) { 2376169562Sscottl free(params.mode_buf, M_SCSICD); 2377168752Sscottl cam_periph_unlock(periph); 237839213Sgibbs break; 237939213Sgibbs } 2380111206Sken page = cdgetpage(¶ms); 2381111206Sken 2382111206Sken page->audio.port[LEFT_PORT].channels = 238339213Sgibbs arg->patch[0]; 2384111206Sken page->audio.port[RIGHT_PORT].channels = 238539213Sgibbs arg->patch[1]; 2386111206Sken page->audio.port[2].channels = arg->patch[2]; 2387111206Sken page->audio.port[3].channels = arg->patch[3]; 2388111206Sken error = cdsetmode(periph, ¶ms); 2389169562Sscottl free(params.mode_buf, M_SCSICD); 2390168752Sscottl cam_periph_unlock(periph); 239139213Sgibbs } 239239213Sgibbs break; 239339213Sgibbs case CDIOCGETVOL: 239439213Sgibbs { 239539213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2396111206Sken struct cd_mode_params params; 2397111206Sken union cd_pages *page; 239839213Sgibbs 2399168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2400169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2401168752Sscottl M_WAITOK | M_ZERO); 2402168752Sscottl 2403168752Sscottl cam_periph_lock(periph); 240439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 240539213Sgibbs ("trying to do CDIOCGETVOL\n")); 240639213Sgibbs 2407111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 240839213Sgibbs if (error) { 2409169562Sscottl free(params.mode_buf, M_SCSICD); 2410168752Sscottl cam_periph_unlock(periph); 241139213Sgibbs break; 241239213Sgibbs } 2413111206Sken page = cdgetpage(¶ms); 2414111206Sken 241539213Sgibbs arg->vol[LEFT_PORT] = 2416111206Sken page->audio.port[LEFT_PORT].volume; 241739213Sgibbs arg->vol[RIGHT_PORT] = 2418111206Sken page->audio.port[RIGHT_PORT].volume; 2419111206Sken arg->vol[2] = page->audio.port[2].volume; 2420111206Sken arg->vol[3] = page->audio.port[3].volume; 2421169562Sscottl free(params.mode_buf, M_SCSICD); 2422168752Sscottl cam_periph_unlock(periph); 242339213Sgibbs } 242439213Sgibbs break; 242539213Sgibbs case CDIOCSETVOL: 242639213Sgibbs { 242739213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2428111206Sken struct cd_mode_params params; 2429111206Sken union cd_pages *page; 243039213Sgibbs 2431168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2432169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2433168752Sscottl M_WAITOK | M_ZERO); 2434168752Sscottl 2435168752Sscottl cam_periph_lock(periph); 243639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 243739213Sgibbs ("trying to do CDIOCSETVOL\n")); 243839213Sgibbs 2439111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 244039213Sgibbs if (error) { 2441169562Sscottl free(params.mode_buf, M_SCSICD); 2442168752Sscottl cam_periph_unlock(periph); 244339213Sgibbs break; 244439213Sgibbs } 2445111206Sken page = cdgetpage(¶ms); 2446111206Sken 2447111206Sken page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2448111206Sken page->audio.port[LEFT_PORT].volume = 244939213Sgibbs arg->vol[LEFT_PORT]; 2450111206Sken page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2451111206Sken page->audio.port[RIGHT_PORT].volume = 245239213Sgibbs arg->vol[RIGHT_PORT]; 2453111206Sken page->audio.port[2].volume = arg->vol[2]; 2454111206Sken page->audio.port[3].volume = arg->vol[3]; 2455111206Sken error = cdsetmode(periph, ¶ms); 2456168752Sscottl cam_periph_unlock(periph); 2457169562Sscottl free(params.mode_buf, M_SCSICD); 245839213Sgibbs } 245939213Sgibbs break; 246039213Sgibbs case CDIOCSETMONO: 246139213Sgibbs { 2462111206Sken struct cd_mode_params params; 2463111206Sken union cd_pages *page; 246439213Sgibbs 2465168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2466169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2467168752Sscottl M_WAITOK | M_ZERO); 2468168752Sscottl 2469168752Sscottl cam_periph_lock(periph); 247039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 247139213Sgibbs ("trying to do CDIOCSETMONO\n")); 247239213Sgibbs 2473111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 247439213Sgibbs if (error) { 2475169562Sscottl free(params.mode_buf, M_SCSICD); 2476168752Sscottl cam_periph_unlock(periph); 247739213Sgibbs break; 247839213Sgibbs } 2479111206Sken page = cdgetpage(¶ms); 2480111206Sken 2481111206Sken page->audio.port[LEFT_PORT].channels = 248239213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2483111206Sken page->audio.port[RIGHT_PORT].channels = 248439213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2485111206Sken page->audio.port[2].channels = 0; 2486111206Sken page->audio.port[3].channels = 0; 2487111206Sken error = cdsetmode(periph, ¶ms); 2488168752Sscottl cam_periph_unlock(periph); 2489169562Sscottl free(params.mode_buf, M_SCSICD); 249039213Sgibbs } 249139213Sgibbs break; 249239213Sgibbs case CDIOCSETSTEREO: 249339213Sgibbs { 2494111206Sken struct cd_mode_params params; 2495111206Sken union cd_pages *page; 249639213Sgibbs 2497168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2498169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2499168752Sscottl M_WAITOK | M_ZERO); 2500168752Sscottl 2501168752Sscottl cam_periph_lock(periph); 250239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 250339213Sgibbs ("trying to do CDIOCSETSTEREO\n")); 250439213Sgibbs 2505111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 250639213Sgibbs if (error) { 2507169562Sscottl free(params.mode_buf, M_SCSICD); 2508168752Sscottl cam_periph_unlock(periph); 250939213Sgibbs break; 251039213Sgibbs } 2511111206Sken page = cdgetpage(¶ms); 2512111206Sken 2513111206Sken page->audio.port[LEFT_PORT].channels = 251439213Sgibbs LEFT_CHANNEL; 2515111206Sken page->audio.port[RIGHT_PORT].channels = 251639213Sgibbs RIGHT_CHANNEL; 2517111206Sken page->audio.port[2].channels = 0; 2518111206Sken page->audio.port[3].channels = 0; 2519111206Sken error = cdsetmode(periph, ¶ms); 2520169562Sscottl free(params.mode_buf, M_SCSICD); 2521168752Sscottl cam_periph_unlock(periph); 252239213Sgibbs } 252339213Sgibbs break; 252439213Sgibbs case CDIOCSETMUTE: 252539213Sgibbs { 2526111206Sken struct cd_mode_params params; 2527111206Sken union cd_pages *page; 252839213Sgibbs 2529168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2530169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2531168752Sscottl M_WAITOK | M_ZERO); 2532168752Sscottl 2533168752Sscottl cam_periph_lock(periph); 253439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 253539213Sgibbs ("trying to do CDIOCSETMUTE\n")); 253639213Sgibbs 2537111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 253839213Sgibbs if (error) { 2539196983Sscottl free(¶ms.mode_buf, M_SCSICD); 2540168752Sscottl cam_periph_unlock(periph); 254139213Sgibbs break; 254239213Sgibbs } 2543111206Sken page = cdgetpage(¶ms); 2544111206Sken 2545111206Sken page->audio.port[LEFT_PORT].channels = 0; 2546111206Sken page->audio.port[RIGHT_PORT].channels = 0; 2547111206Sken page->audio.port[2].channels = 0; 2548111206Sken page->audio.port[3].channels = 0; 2549111206Sken error = cdsetmode(periph, ¶ms); 2550169562Sscottl free(params.mode_buf, M_SCSICD); 2551168752Sscottl cam_periph_unlock(periph); 255239213Sgibbs } 255339213Sgibbs break; 255439213Sgibbs case CDIOCSETLEFT: 255539213Sgibbs { 2556111206Sken struct cd_mode_params params; 2557111206Sken union cd_pages *page; 255839213Sgibbs 2559168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2560169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2561168752Sscottl M_WAITOK | M_ZERO); 2562168752Sscottl 2563168752Sscottl cam_periph_lock(periph); 256439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 256539213Sgibbs ("trying to do CDIOCSETLEFT\n")); 256639213Sgibbs 2567111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 256839213Sgibbs if (error) { 2569169562Sscottl free(params.mode_buf, M_SCSICD); 2570168752Sscottl cam_periph_unlock(periph); 257139213Sgibbs break; 257239213Sgibbs } 2573111206Sken page = cdgetpage(¶ms); 2574111206Sken 2575111206Sken page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2576111206Sken page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2577111206Sken page->audio.port[2].channels = 0; 2578111206Sken page->audio.port[3].channels = 0; 2579111206Sken error = cdsetmode(periph, ¶ms); 2580169562Sscottl free(params.mode_buf, M_SCSICD); 2581168752Sscottl cam_periph_unlock(periph); 258239213Sgibbs } 258339213Sgibbs break; 258439213Sgibbs case CDIOCSETRIGHT: 258539213Sgibbs { 2586111206Sken struct cd_mode_params params; 2587111206Sken union cd_pages *page; 258839213Sgibbs 2589111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2590169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2591111206Sken M_WAITOK | M_ZERO); 2592111206Sken 2593168752Sscottl cam_periph_lock(periph); 2594168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2595168752Sscottl ("trying to do CDIOCSETRIGHT\n")); 2596168752Sscottl 2597111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 259839213Sgibbs if (error) { 2599169562Sscottl free(params.mode_buf, M_SCSICD); 2600168752Sscottl cam_periph_unlock(periph); 260139213Sgibbs break; 260239213Sgibbs } 2603111206Sken page = cdgetpage(¶ms); 2604111206Sken 2605111206Sken page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2606111206Sken page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2607111206Sken page->audio.port[2].channels = 0; 2608111206Sken page->audio.port[3].channels = 0; 2609111206Sken error = cdsetmode(periph, ¶ms); 2610169562Sscottl free(params.mode_buf, M_SCSICD); 2611168752Sscottl cam_periph_unlock(periph); 261239213Sgibbs } 261339213Sgibbs break; 261439213Sgibbs case CDIOCRESUME: 2615168752Sscottl cam_periph_lock(periph); 261639213Sgibbs error = cdpause(periph, 1); 2617168752Sscottl cam_periph_unlock(periph); 261839213Sgibbs break; 261939213Sgibbs case CDIOCPAUSE: 2620168752Sscottl cam_periph_lock(periph); 262139213Sgibbs error = cdpause(periph, 0); 2622168752Sscottl cam_periph_unlock(periph); 262339213Sgibbs break; 262439213Sgibbs case CDIOCSTART: 2625168752Sscottl cam_periph_lock(periph); 2626111206Sken error = cdstartunit(periph, 0); 2627168752Sscottl cam_periph_unlock(periph); 262839213Sgibbs break; 2629111206Sken case CDIOCCLOSE: 2630168752Sscottl cam_periph_lock(periph); 2631111206Sken error = cdstartunit(periph, 1); 2632168752Sscottl cam_periph_unlock(periph); 2633111206Sken break; 263439213Sgibbs case CDIOCSTOP: 2635168752Sscottl cam_periph_lock(periph); 263639213Sgibbs error = cdstopunit(periph, 0); 2637168752Sscottl cam_periph_unlock(periph); 263839213Sgibbs break; 263939213Sgibbs case CDIOCEJECT: 2640168752Sscottl cam_periph_lock(periph); 264139213Sgibbs error = cdstopunit(periph, 1); 2642168752Sscottl cam_periph_unlock(periph); 264339213Sgibbs break; 264439213Sgibbs case CDIOCALLOW: 2645168752Sscottl cam_periph_lock(periph); 264639213Sgibbs cdprevent(periph, PR_ALLOW); 2647168752Sscottl cam_periph_unlock(periph); 264839213Sgibbs break; 264939213Sgibbs case CDIOCPREVENT: 2650168752Sscottl cam_periph_lock(periph); 265139213Sgibbs cdprevent(periph, PR_PREVENT); 2652168752Sscottl cam_periph_unlock(periph); 265339213Sgibbs break; 265439213Sgibbs case CDIOCSETDEBUG: 265539213Sgibbs /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 265639213Sgibbs error = ENOTTY; 265739213Sgibbs break; 265839213Sgibbs case CDIOCCLRDEBUG: 265939213Sgibbs /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 266039213Sgibbs error = ENOTTY; 266139213Sgibbs break; 266239213Sgibbs case CDIOCRESET: 266339213Sgibbs /* return (cd_reset(periph)); */ 266439213Sgibbs error = ENOTTY; 266539213Sgibbs break; 2666105421Snjl case CDRIOCREADSPEED: 2667168752Sscottl cam_periph_lock(periph); 2668105421Snjl error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2669168752Sscottl cam_periph_unlock(periph); 2670105421Snjl break; 2671105421Snjl case CDRIOCWRITESPEED: 2672168752Sscottl cam_periph_lock(periph); 2673105421Snjl error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2674168752Sscottl cam_periph_unlock(periph); 2675105421Snjl break; 267660422Sken case DVDIOCSENDKEY: 267760422Sken case DVDIOCREPORTKEY: { 267860422Sken struct dvd_authinfo *authinfo; 267960422Sken 268060422Sken authinfo = (struct dvd_authinfo *)addr; 268160422Sken 268260422Sken if (cmd == DVDIOCREPORTKEY) 268360422Sken error = cdreportkey(periph, authinfo); 268460422Sken else 268560422Sken error = cdsendkey(periph, authinfo); 268660422Sken break; 2687104456Sphk } 268860422Sken case DVDIOCREADSTRUCTURE: { 268960422Sken struct dvd_struct *dvdstruct; 269060422Sken 269160422Sken dvdstruct = (struct dvd_struct *)addr; 269260422Sken 269360422Sken error = cdreaddvdstructure(periph, dvdstruct); 269460422Sken 269560422Sken break; 269660422Sken } 269739213Sgibbs default: 2698168752Sscottl cam_periph_lock(periph); 269939213Sgibbs error = cam_periph_ioctl(periph, cmd, addr, cderror); 2700168752Sscottl cam_periph_unlock(periph); 270139213Sgibbs break; 270239213Sgibbs } 270339213Sgibbs 2704168752Sscottl cam_periph_lock(periph); 2705168752Sscottl cam_periph_unhold(periph); 2706168752Sscottl 270739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2708104456Sphk if (error && bootverbose) { 2709104456Sphk printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2710104456Sphk } 2711168752Sscottl cam_periph_unlock(periph); 271239213Sgibbs 271339213Sgibbs return (error); 271439213Sgibbs} 271539213Sgibbs 271639213Sgibbsstatic void 271739213Sgibbscdprevent(struct cam_periph *periph, int action) 271839213Sgibbs{ 271939213Sgibbs union ccb *ccb; 272039213Sgibbs struct cd_softc *softc; 272139213Sgibbs int error; 272239213Sgibbs 272339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 272439213Sgibbs 272539213Sgibbs softc = (struct cd_softc *)periph->softc; 272639213Sgibbs 272739213Sgibbs if (((action == PR_ALLOW) 272839213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 272939213Sgibbs || ((action == PR_PREVENT) 273039213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 273139213Sgibbs return; 273239213Sgibbs } 273339213Sgibbs 2734198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 273539213Sgibbs 273639213Sgibbs scsi_prevent(&ccb->csio, 273739213Sgibbs /*retries*/ 1, 273839213Sgibbs cddone, 273939213Sgibbs MSG_SIMPLE_Q_TAG, 274039213Sgibbs action, 274139213Sgibbs SSD_FULL_SIZE, 274239213Sgibbs /* timeout */60000); 274339213Sgibbs 274474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 274574840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 274639213Sgibbs 274739213Sgibbs xpt_release_ccb(ccb); 274839213Sgibbs 274939213Sgibbs if (error == 0) { 275039213Sgibbs if (action == PR_ALLOW) 275139213Sgibbs softc->flags &= ~CD_FLAG_DISC_LOCKED; 275239213Sgibbs else 275339213Sgibbs softc->flags |= CD_FLAG_DISC_LOCKED; 275439213Sgibbs } 275539213Sgibbs} 275639213Sgibbs 2757120599Sphk/* 2758120599Sphk * XXX: the disk media and sector size is only really able to change 2759120599Sphk * XXX: while the device is closed. 2760120599Sphk */ 276139213Sgibbsstatic int 2762111206Skencdcheckmedia(struct cam_periph *periph) 276339213Sgibbs{ 276439213Sgibbs struct cd_softc *softc; 2765111206Sken struct ioc_toc_header *toch; 2766111206Sken struct cd_toc_single leadout; 2767111206Sken u_int32_t size, toclen; 2768111206Sken int error, num_entries, cdindex; 2769111206Sken 2770111206Sken softc = (struct cd_softc *)periph->softc; 2771111206Sken 2772111206Sken cdprevent(periph, PR_PREVENT); 2773134824Sphk softc->disk->d_sectorsize = 2048; 2774125975Sphk softc->disk->d_mediasize = 0; 2775111206Sken 2776111206Sken /* 2777111206Sken * Get the disc size and block size. If we can't get it, we don't 2778111206Sken * have media, most likely. 2779111206Sken */ 2780111206Sken if ((error = cdsize(periph, &size)) != 0) { 2781111206Sken softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2782111206Sken cdprevent(periph, PR_ALLOW); 2783111206Sken return (error); 2784111206Sken } else 2785111206Sken softc->flags |= CD_FLAG_VALID_MEDIA; 2786111206Sken 2787111206Sken /* 2788111206Sken * Now we check the table of contents. This (currently) is only 2789111206Sken * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2790111206Sken * things like present a separate entry in /dev for each track, 2791111206Sken * like that acd(4) driver does. 2792111206Sken */ 2793111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2794111206Sken toch = &softc->toc.header; 2795111206Sken /* 2796111206Sken * We will get errors here for media that doesn't have a table of 2797111206Sken * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2798111206Sken * command is presented for a DDCD/CD-R/RW media, where the first TOC 2799111206Sken * has not been recorded (no complete session) and the Format codes 2800111206Sken * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2801111206Sken * with an INVALID FIELD IN CDB. Devices that are not capable of 2802111206Sken * reading an incomplete session on DDC/CD-R/RW media shall report 2803111206Sken * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2804111206Sken * 2805111206Sken * So this isn't fatal if we can't read the table of contents, it 2806111206Sken * just means that the user won't be able to issue the play tracks 2807111206Sken * ioctl, and likely lots of other stuff won't work either. They 2808111206Sken * need to burn the CD before we can do a whole lot with it. So 2809111206Sken * we don't print anything here if we get an error back. 2810111206Sken */ 2811111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2812111206Sken SF_NO_PRINT); 2813111206Sken /* 2814111206Sken * Errors in reading the table of contents aren't fatal, we just 2815111206Sken * won't have a valid table of contents cached. 2816111206Sken */ 2817111206Sken if (error != 0) { 2818111206Sken error = 0; 2819111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2820111206Sken goto bailout; 2821111206Sken } 2822111206Sken 2823111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2824111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2825111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2826111206Sken } 2827111206Sken 2828111206Sken /* Number of TOC entries, plus leadout */ 2829111206Sken num_entries = (toch->ending_track - toch->starting_track) + 2; 2830111206Sken 2831111206Sken if (num_entries <= 0) 2832111206Sken goto bailout; 2833111206Sken 2834111206Sken toclen = num_entries * sizeof(struct cd_toc_entry); 2835111206Sken 2836111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2837111206Sken (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2838111206Sken SF_NO_PRINT); 2839111206Sken if (error != 0) { 2840111206Sken error = 0; 2841111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2842111206Sken goto bailout; 2843111206Sken } 2844111206Sken 2845111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2846111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2847111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2848111206Sken } 2849111206Sken /* 2850111206Sken * XXX KDM is this necessary? Probably only if the drive doesn't 2851111206Sken * return leadout information with the table of contents. 2852111206Sken */ 2853111206Sken cdindex = toch->starting_track + num_entries -1; 2854111206Sken if (cdindex == toch->ending_track + 1) { 2855111206Sken 2856111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2857111206Sken (u_int8_t *)&leadout, sizeof(leadout), 2858111206Sken SF_NO_PRINT); 2859111206Sken if (error != 0) { 2860111206Sken error = 0; 2861111206Sken goto bailout; 2862111206Sken } 2863111206Sken softc->toc.entries[cdindex - toch->starting_track] = 2864111206Sken leadout.entry; 2865111206Sken } 2866111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2867111206Sken for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2868111206Sken softc->toc.entries[cdindex].track = 2869111206Sken bcd2bin(softc->toc.entries[cdindex].track); 2870111206Sken } 2871111206Sken } 2872111206Sken 2873111206Sken softc->flags |= CD_FLAG_VALID_TOC; 2874125975Sphk softc->disk->d_sectorsize = softc->params.blksize; 2875125975Sphk softc->disk->d_mediasize = 2876120599Sphk (off_t)softc->params.blksize * softc->params.disksize; 2877111206Sken 2878111206Skenbailout: 2879111206Sken 2880111206Sken /* 2881111206Sken * We unconditionally (re)set the blocksize each time the 2882111206Sken * CD device is opened. This is because the CD can change, 2883111206Sken * and therefore the blocksize might change. 2884111206Sken * XXX problems here if some slice or partition is still 2885111206Sken * open with the old size? 2886111206Sken */ 2887125975Sphk if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2888125975Sphk softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2889125975Sphk softc->disk->d_devstat->block_size = softc->params.blksize; 2890111206Sken 2891111206Sken return (error); 2892111206Sken} 2893111206Sken 2894111206Skenstatic int 2895111206Skencdsize(struct cam_periph *periph, u_int32_t *size) 2896111206Sken{ 2897111206Sken struct cd_softc *softc; 289839213Sgibbs union ccb *ccb; 289939213Sgibbs struct scsi_read_capacity_data *rcap_buf; 290039213Sgibbs int error; 290139213Sgibbs 290239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 290339213Sgibbs 290439213Sgibbs softc = (struct cd_softc *)periph->softc; 290539213Sgibbs 2906198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 290739213Sgibbs 2908168752Sscottl /* XXX Should be M_WAITOK */ 290939213Sgibbs rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2910169562Sscottl M_SCSICD, M_NOWAIT); 2911168752Sscottl if (rcap_buf == NULL) 2912168752Sscottl return (ENOMEM); 291339213Sgibbs 291439213Sgibbs scsi_read_capacity(&ccb->csio, 291539213Sgibbs /*retries*/ 1, 291639213Sgibbs cddone, 291739213Sgibbs MSG_SIMPLE_Q_TAG, 291839213Sgibbs rcap_buf, 291939213Sgibbs SSD_FULL_SIZE, 292039213Sgibbs /* timeout */20000); 292139213Sgibbs 292274840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 292374840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 292439213Sgibbs 292539213Sgibbs xpt_release_ccb(ccb); 292639213Sgibbs 292739213Sgibbs softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 292839213Sgibbs softc->params.blksize = scsi_4btoul(rcap_buf->length); 292960806Sjoerg /* 293060806Sjoerg * SCSI-3 mandates that the reported blocksize shall be 2048. 293160806Sjoerg * Older drives sometimes report funny values, trim it down to 293260806Sjoerg * 2048, or other parts of the kernel will get confused. 293360806Sjoerg * 293460806Sjoerg * XXX we leave drives alone that might report 512 bytes, as 293560806Sjoerg * well as drives reporting more weird sizes like perhaps 4K. 293660806Sjoerg */ 293760806Sjoerg if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 293860806Sjoerg softc->params.blksize = 2048; 293939213Sgibbs 2940169562Sscottl free(rcap_buf, M_SCSICD); 294139213Sgibbs *size = softc->params.disksize; 294239213Sgibbs 294339213Sgibbs return (error); 294439213Sgibbs 294539213Sgibbs} 294639213Sgibbs 294739213Sgibbsstatic int 2948111206Skencd6byteworkaround(union ccb *ccb) 2949111206Sken{ 2950111206Sken u_int8_t *cdb; 2951111206Sken struct cam_periph *periph; 2952111206Sken struct cd_softc *softc; 2953111206Sken struct cd_mode_params *params; 2954111206Sken int frozen, found; 2955111206Sken 2956111206Sken periph = xpt_path_periph(ccb->ccb_h.path); 2957111206Sken softc = (struct cd_softc *)periph->softc; 2958111206Sken 2959111206Sken cdb = ccb->csio.cdb_io.cdb_bytes; 2960111206Sken 2961111206Sken if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2962111206Sken || ((cdb[0] != MODE_SENSE_6) 2963111206Sken && (cdb[0] != MODE_SELECT_6))) 2964111206Sken return (0); 2965111206Sken 2966111206Sken /* 2967111206Sken * Because there is no convenient place to stash the overall 2968111206Sken * cd_mode_params structure pointer, we have to grab it like this. 2969111206Sken * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2970111206Sken * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2971111206Sken * 2972111206Sken * XXX It would be nice if, at some point, we could increase the 2973111206Sken * number of available peripheral private pointers. Both pointers 2974111206Sken * are currently used in most every peripheral driver. 2975111206Sken */ 2976111206Sken found = 0; 2977111206Sken 2978111206Sken STAILQ_FOREACH(params, &softc->mode_queue, links) { 2979111206Sken if (params->mode_buf == ccb->csio.data_ptr) { 2980111206Sken found = 1; 2981111206Sken break; 2982111206Sken } 2983111206Sken } 2984111206Sken 2985111206Sken /* 2986111206Sken * This shouldn't happen. All mode sense and mode select 2987111206Sken * operations in the cd(4) driver MUST go through cdgetmode() and 2988111206Sken * cdsetmode()! 2989111206Sken */ 2990111206Sken if (found == 0) { 2991164906Smjacob xpt_print(periph->path, 2992164906Smjacob "mode buffer not found in mode queue!\n"); 2993111206Sken return (0); 2994111206Sken } 2995111206Sken 2996111206Sken params->cdb_size = 10; 2997111206Sken softc->minimum_command_size = 10; 2998164906Smjacob xpt_print(ccb->ccb_h.path, 2999164906Smjacob "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3000164906Smjacob (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3001111206Sken 3002111206Sken if (cdb[0] == MODE_SENSE_6) { 3003111206Sken struct scsi_mode_sense_10 ms10; 3004111206Sken struct scsi_mode_sense_6 *ms6; 3005111206Sken int len; 3006111206Sken 3007111206Sken ms6 = (struct scsi_mode_sense_6 *)cdb; 3008111206Sken 3009111206Sken bzero(&ms10, sizeof(ms10)); 3010111206Sken ms10.opcode = MODE_SENSE_10; 3011111206Sken ms10.byte2 = ms6->byte2; 3012111206Sken ms10.page = ms6->page; 3013111206Sken 3014111206Sken /* 3015111206Sken * 10 byte mode header, block descriptor, 3016111206Sken * sizeof(union cd_pages) 3017111206Sken */ 3018111206Sken len = sizeof(struct cd_mode_data_10); 3019111206Sken ccb->csio.dxfer_len = len; 3020111206Sken 3021111206Sken scsi_ulto2b(len, ms10.length); 3022111206Sken ms10.control = ms6->control; 3023111206Sken bcopy(&ms10, cdb, 10); 3024111206Sken ccb->csio.cdb_len = 10; 3025111206Sken } else { 3026111206Sken struct scsi_mode_select_10 ms10; 3027111206Sken struct scsi_mode_select_6 *ms6; 3028111206Sken struct scsi_mode_header_6 *header6; 3029111206Sken struct scsi_mode_header_10 *header10; 3030111206Sken struct scsi_mode_page_header *page_header; 3031111206Sken int blk_desc_len, page_num, page_size, len; 3032111206Sken 3033111206Sken ms6 = (struct scsi_mode_select_6 *)cdb; 3034111206Sken 3035111206Sken bzero(&ms10, sizeof(ms10)); 3036111206Sken ms10.opcode = MODE_SELECT_10; 3037111206Sken ms10.byte2 = ms6->byte2; 3038111206Sken 3039111206Sken header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3040111206Sken header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3041111206Sken 3042111206Sken page_header = find_mode_page_6(header6); 3043111206Sken page_num = page_header->page_code; 3044111206Sken 3045111206Sken blk_desc_len = header6->blk_desc_len; 3046111206Sken 3047111206Sken page_size = cdgetpagesize(page_num); 3048111206Sken 3049111206Sken if (page_size != (page_header->page_length + 3050111206Sken sizeof(*page_header))) 3051111206Sken page_size = page_header->page_length + 3052111206Sken sizeof(*page_header); 3053111206Sken 3054111206Sken len = sizeof(*header10) + blk_desc_len + page_size; 3055111206Sken 3056111206Sken len = min(params->alloc_len, len); 3057111206Sken 3058111206Sken /* 3059111206Sken * Since the 6 byte parameter header is shorter than the 10 3060111206Sken * byte parameter header, we need to copy the actual mode 3061111206Sken * page data, and the block descriptor, if any, so things wind 3062111206Sken * up in the right place. The regions will overlap, but 3063111206Sken * bcopy() does the right thing. 3064111206Sken */ 3065111206Sken bcopy(params->mode_buf + sizeof(*header6), 3066111206Sken params->mode_buf + sizeof(*header10), 3067111206Sken len - sizeof(*header10)); 3068111206Sken 3069111206Sken /* Make sure these fields are set correctly. */ 3070111206Sken scsi_ulto2b(0, header10->data_length); 3071111206Sken header10->medium_type = 0; 3072111206Sken scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3073111206Sken 3074111206Sken ccb->csio.dxfer_len = len; 3075111206Sken 3076111206Sken scsi_ulto2b(len, ms10.length); 3077111206Sken ms10.control = ms6->control; 3078111206Sken bcopy(&ms10, cdb, 10); 3079111206Sken ccb->csio.cdb_len = 10; 3080111206Sken } 3081111206Sken 3082111206Sken frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3083111206Sken ccb->ccb_h.status = CAM_REQUEUE_REQ; 3084111206Sken xpt_action(ccb); 3085111206Sken if (frozen) { 3086111206Sken cam_release_devq(ccb->ccb_h.path, 3087111206Sken /*relsim_flags*/0, 3088111206Sken /*openings*/0, 3089111206Sken /*timeout*/0, 3090111206Sken /*getcount_only*/0); 3091111206Sken } 3092111206Sken 3093111206Sken return (ERESTART); 3094111206Sken} 3095111206Sken 3096111206Skenstatic int 309739213Sgibbscderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 309839213Sgibbs{ 309939213Sgibbs struct cd_softc *softc; 310039213Sgibbs struct cam_periph *periph; 3101111206Sken int error; 310239213Sgibbs 310339213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 310439213Sgibbs softc = (struct cd_softc *)periph->softc; 310539213Sgibbs 3106111206Sken error = 0; 3107111206Sken 310839514Sgibbs /* 3109111206Sken * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3110111206Sken * CDB comes back with this particular error, try transforming it 3111111206Sken * into the 10 byte version. 3112111206Sken */ 3113111206Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3114111206Sken error = cd6byteworkaround(ccb); 3115111206Sken } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3116111206Sken CAM_SCSI_STATUS_ERROR) 3117111206Sken && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3118111206Sken && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3119111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3120111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3121111206Sken int sense_key, error_code, asc, ascq; 3122111206Sken 3123111206Sken scsi_extract_sense(&ccb->csio.sense_data, 3124111206Sken &error_code, &sense_key, &asc, &ascq); 3125111206Sken if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3126111206Sken error = cd6byteworkaround(ccb); 3127111206Sken } 3128111206Sken 3129111206Sken if (error == ERESTART) 3130111206Sken return (error); 3131111206Sken 3132111206Sken /* 313339514Sgibbs * XXX 313439514Sgibbs * Until we have a better way of doing pack validation, 313539514Sgibbs * don't treat UAs as errors. 313639514Sgibbs */ 313739514Sgibbs sense_flags |= SF_RETRY_UA; 313839213Sgibbs return (cam_periph_error(ccb, cam_flags, sense_flags, 313939213Sgibbs &softc->saved_ccb)); 314039213Sgibbs} 314139213Sgibbs 314239213Sgibbs/* 314339213Sgibbs * Read table of contents 314439213Sgibbs */ 314539213Sgibbsstatic int 314639213Sgibbscdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3147111206Sken u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 314839213Sgibbs{ 314939213Sgibbs struct scsi_read_toc *scsi_cmd; 315039213Sgibbs u_int32_t ntoc; 315139213Sgibbs struct ccb_scsiio *csio; 315239213Sgibbs union ccb *ccb; 315339213Sgibbs int error; 315439213Sgibbs 315539213Sgibbs ntoc = len; 315639213Sgibbs error = 0; 315739213Sgibbs 3158198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 315939213Sgibbs 316039213Sgibbs csio = &ccb->csio; 316139213Sgibbs 316239213Sgibbs cam_fill_csio(csio, 316339213Sgibbs /* retries */ 1, 316439213Sgibbs /* cbfcnp */ cddone, 316539213Sgibbs /* flags */ CAM_DIR_IN, 316639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 3167111206Sken /* data_ptr */ data, 316839213Sgibbs /* dxfer_len */ len, 316939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 317039213Sgibbs sizeof(struct scsi_read_toc), 317139213Sgibbs /* timeout */ 50000); 317239213Sgibbs 317339213Sgibbs scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 317439213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 317539213Sgibbs 317639213Sgibbs if (mode == CD_MSF_FORMAT) 317739213Sgibbs scsi_cmd->byte2 |= CD_MSF; 317839213Sgibbs scsi_cmd->from_track = start; 317939213Sgibbs /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 318039213Sgibbs scsi_cmd->data_len[0] = (ntoc) >> 8; 318139213Sgibbs scsi_cmd->data_len[1] = (ntoc) & 0xff; 318239213Sgibbs 318339213Sgibbs scsi_cmd->op_code = READ_TOC; 318439213Sgibbs 318574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3186111206Sken /*sense_flags*/SF_RETRY_UA | sense_flags); 318739213Sgibbs 318839213Sgibbs xpt_release_ccb(ccb); 318939213Sgibbs 319039213Sgibbs return(error); 319139213Sgibbs} 319239213Sgibbs 319339213Sgibbsstatic int 319439213Sgibbscdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 319539213Sgibbs u_int32_t format, int track, 319639213Sgibbs struct cd_sub_channel_info *data, u_int32_t len) 319739213Sgibbs{ 319839213Sgibbs struct scsi_read_subchannel *scsi_cmd; 319939213Sgibbs struct ccb_scsiio *csio; 320039213Sgibbs union ccb *ccb; 320139213Sgibbs int error; 320239213Sgibbs 320339213Sgibbs error = 0; 320439213Sgibbs 3205198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 320639213Sgibbs 320739213Sgibbs csio = &ccb->csio; 320839213Sgibbs 320939213Sgibbs cam_fill_csio(csio, 321039213Sgibbs /* retries */ 1, 321139213Sgibbs /* cbfcnp */ cddone, 321239213Sgibbs /* flags */ CAM_DIR_IN, 321339213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 321439213Sgibbs /* data_ptr */ (u_int8_t *)data, 321539213Sgibbs /* dxfer_len */ len, 321639213Sgibbs /* sense_len */ SSD_FULL_SIZE, 321739213Sgibbs sizeof(struct scsi_read_subchannel), 321839213Sgibbs /* timeout */ 50000); 321939213Sgibbs 322039213Sgibbs scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 322139213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 322239213Sgibbs 322339213Sgibbs scsi_cmd->op_code = READ_SUBCHANNEL; 322439213Sgibbs if (mode == CD_MSF_FORMAT) 322539213Sgibbs scsi_cmd->byte1 |= CD_MSF; 322639213Sgibbs scsi_cmd->byte2 = SRS_SUBQ; 322739213Sgibbs scsi_cmd->subchan_format = format; 322839213Sgibbs scsi_cmd->track = track; 322939213Sgibbs scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 323039213Sgibbs scsi_cmd->control = 0; 323139213Sgibbs 323274840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 323374840Sken /*sense_flags*/SF_RETRY_UA); 323439213Sgibbs 323539213Sgibbs xpt_release_ccb(ccb); 323639213Sgibbs 323739213Sgibbs return(error); 323839213Sgibbs} 323939213Sgibbs 324039213Sgibbs 3241111206Sken/* 3242111206Sken * All MODE_SENSE requests in the cd(4) driver MUST go through this 3243111206Sken * routine. See comments in cd6byteworkaround() for details. 3244111206Sken */ 324539213Sgibbsstatic int 3246111206Skencdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3247111206Sken u_int32_t page) 324839213Sgibbs{ 3249111206Sken struct ccb_scsiio *csio; 3250111206Sken struct cd_softc *softc; 325139213Sgibbs union ccb *ccb; 3252111206Sken int param_len; 325339213Sgibbs int error; 325439213Sgibbs 3255111206Sken softc = (struct cd_softc *)periph->softc; 3256111206Sken 3257198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 325839213Sgibbs 325939213Sgibbs csio = &ccb->csio; 326039213Sgibbs 3261111206Sken data->cdb_size = softc->minimum_command_size; 3262111206Sken if (data->cdb_size < 10) 3263111206Sken param_len = sizeof(struct cd_mode_data); 3264111206Sken else 3265111206Sken param_len = sizeof(struct cd_mode_data_10); 326639213Sgibbs 3267111206Sken /* Don't say we've got more room than we actually allocated */ 3268111206Sken param_len = min(param_len, data->alloc_len); 326939213Sgibbs 3270111206Sken scsi_mode_sense_len(csio, 3271111206Sken /* retries */ 1, 3272111206Sken /* cbfcnp */ cddone, 3273111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3274111206Sken /* dbd */ 0, 3275111206Sken /* page_code */ SMS_PAGE_CTRL_CURRENT, 3276111206Sken /* page */ page, 3277111206Sken /* param_buf */ data->mode_buf, 3278111206Sken /* param_len */ param_len, 3279111206Sken /* minimum_cmd_size */ softc->minimum_command_size, 3280111206Sken /* sense_len */ SSD_FULL_SIZE, 3281111206Sken /* timeout */ 50000); 328239213Sgibbs 3283111206Sken /* 3284111206Sken * It would be nice not to have to do this, but there's no 3285111206Sken * available pointer in the CCB that would allow us to stuff the 3286111206Sken * mode params structure in there and retrieve it in 3287111206Sken * cd6byteworkaround(), so we can set the cdb size. The cdb size 3288111206Sken * lets the caller know what CDB size we ended up using, so they 3289111206Sken * can find the actual mode page offset. 3290111206Sken */ 3291111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3292111206Sken 329374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 329474840Sken /*sense_flags*/SF_RETRY_UA); 329539213Sgibbs 329639213Sgibbs xpt_release_ccb(ccb); 329739213Sgibbs 3298111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3299111206Sken 3300111206Sken /* 3301111206Sken * This is a bit of belt-and-suspenders checking, but if we run 3302111206Sken * into a situation where the target sends back multiple block 3303111206Sken * descriptors, we might not have enough space in the buffer to 3304111206Sken * see the whole mode page. Better to return an error than 3305111206Sken * potentially access memory beyond our malloced region. 3306111206Sken */ 3307111206Sken if (error == 0) { 3308111206Sken u_int32_t data_len; 3309111206Sken 3310111206Sken if (data->cdb_size == 10) { 3311111206Sken struct scsi_mode_header_10 *hdr10; 3312111206Sken 3313111206Sken hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3314111206Sken data_len = scsi_2btoul(hdr10->data_length); 3315111206Sken data_len += sizeof(hdr10->data_length); 3316111206Sken } else { 3317111206Sken struct scsi_mode_header_6 *hdr6; 3318111206Sken 3319111206Sken hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3320111206Sken data_len = hdr6->data_length; 3321111206Sken data_len += sizeof(hdr6->data_length); 3322111206Sken } 3323111206Sken 3324111206Sken /* 3325111206Sken * Complain if there is more mode data available than we 3326111206Sken * allocated space for. This could potentially happen if 3327111206Sken * we miscalculated the page length for some reason, if the 3328111206Sken * drive returns multiple block descriptors, or if it sets 3329111206Sken * the data length incorrectly. 3330111206Sken */ 3331111206Sken if (data_len > data->alloc_len) { 3332164906Smjacob xpt_print(periph->path, "allocated modepage %d length " 3333164906Smjacob "%d < returned length %d\n", page, data->alloc_len, 3334164906Smjacob data_len); 3335111206Sken error = ENOSPC; 3336111206Sken } 3337111206Sken } 3338111206Sken return (error); 333939213Sgibbs} 334039213Sgibbs 3341111206Sken/* 3342111206Sken * All MODE_SELECT requests in the cd(4) driver MUST go through this 3343111206Sken * routine. See comments in cd6byteworkaround() for details. 3344111206Sken */ 334539213Sgibbsstatic int 3346111206Skencdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 334739213Sgibbs{ 3348111206Sken struct ccb_scsiio *csio; 3349111206Sken struct cd_softc *softc; 335039213Sgibbs union ccb *ccb; 3351111206Sken int cdb_size, param_len; 335239213Sgibbs int error; 335339213Sgibbs 3354111206Sken softc = (struct cd_softc *)periph->softc; 3355111206Sken 3356198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 335739213Sgibbs 335839213Sgibbs csio = &ccb->csio; 335939213Sgibbs 336039213Sgibbs error = 0; 336139213Sgibbs 336239213Sgibbs /* 3363111206Sken * If the data is formatted for the 10 byte version of the mode 3364111206Sken * select parameter list, we need to use the 10 byte CDB. 3365111206Sken * Otherwise, we use whatever the stored minimum command size. 336639213Sgibbs */ 3367111206Sken if (data->cdb_size == 10) 3368111206Sken cdb_size = data->cdb_size; 3369111206Sken else 3370111206Sken cdb_size = softc->minimum_command_size; 337139213Sgibbs 3372111206Sken if (cdb_size >= 10) { 3373111206Sken struct scsi_mode_header_10 *mode_header; 3374111206Sken u_int32_t data_len; 3375111206Sken 3376111206Sken mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3377111206Sken 3378111206Sken data_len = scsi_2btoul(mode_header->data_length); 3379111206Sken 3380111206Sken scsi_ulto2b(0, mode_header->data_length); 3381111206Sken /* 3382111206Sken * SONY drives do not allow a mode select with a medium_type 3383111206Sken * value that has just been returned by a mode sense; use a 3384111206Sken * medium_type of 0 (Default) instead. 3385111206Sken */ 3386111206Sken mode_header->medium_type = 0; 3387111206Sken 3388111206Sken /* 3389111206Sken * Pass back whatever the drive passed to us, plus the size 3390111206Sken * of the data length field. 3391111206Sken */ 3392111206Sken param_len = data_len + sizeof(mode_header->data_length); 3393111206Sken 3394111206Sken } else { 3395111206Sken struct scsi_mode_header_6 *mode_header; 3396111206Sken 3397111206Sken mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3398111206Sken 3399111206Sken param_len = mode_header->data_length + 1; 3400111206Sken 3401111206Sken mode_header->data_length = 0; 3402111206Sken /* 3403111206Sken * SONY drives do not allow a mode select with a medium_type 3404111206Sken * value that has just been returned by a mode sense; use a 3405111206Sken * medium_type of 0 (Default) instead. 3406111206Sken */ 3407111206Sken mode_header->medium_type = 0; 3408111206Sken } 3409111206Sken 3410111206Sken /* Don't say we've got more room than we actually allocated */ 3411111206Sken param_len = min(param_len, data->alloc_len); 3412111206Sken 3413111206Sken scsi_mode_select_len(csio, 3414111206Sken /* retries */ 1, 3415111206Sken /* cbfcnp */ cddone, 3416111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3417111206Sken /* scsi_page_fmt */ 1, 3418111206Sken /* save_pages */ 0, 3419111206Sken /* param_buf */ data->mode_buf, 3420111206Sken /* param_len */ param_len, 3421111206Sken /* minimum_cmd_size */ cdb_size, 3422111206Sken /* sense_len */ SSD_FULL_SIZE, 3423111206Sken /* timeout */ 50000); 3424111206Sken 3425111206Sken /* See comments in cdgetmode() and cd6byteworkaround(). */ 3426111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3427111206Sken 342874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 342974840Sken /*sense_flags*/SF_RETRY_UA); 343039213Sgibbs 343139213Sgibbs xpt_release_ccb(ccb); 343239213Sgibbs 3433111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3434111206Sken 3435111206Sken return (error); 343639213Sgibbs} 343739213Sgibbs 343839213Sgibbs 343939213Sgibbsstatic int 344039213Sgibbscdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 344139213Sgibbs{ 344239531Sken struct ccb_scsiio *csio; 344339213Sgibbs union ccb *ccb; 344439213Sgibbs int error; 344539531Sken u_int8_t cdb_len; 344639213Sgibbs 344739213Sgibbs error = 0; 3448198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 344939213Sgibbs csio = &ccb->csio; 345039531Sken /* 345139531Sken * Use the smallest possible command to perform the operation. 345239531Sken */ 345339531Sken if ((len & 0xffff0000) == 0) { 345439531Sken /* 345539531Sken * We can fit in a 10 byte cdb. 345639531Sken */ 345739531Sken struct scsi_play_10 *scsi_cmd; 345839213Sgibbs 345939531Sken scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 346039531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 346139531Sken scsi_cmd->op_code = PLAY_10; 346239531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 346339531Sken scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 346439531Sken cdb_len = sizeof(*scsi_cmd); 346539531Sken } else { 346639531Sken struct scsi_play_12 *scsi_cmd; 346739213Sgibbs 346839531Sken scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 346939531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 347039531Sken scsi_cmd->op_code = PLAY_12; 347139531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 347239531Sken scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 347339531Sken cdb_len = sizeof(*scsi_cmd); 347439531Sken } 347539531Sken cam_fill_csio(csio, 347639531Sken /*retries*/2, 347739531Sken cddone, 347839531Sken /*flags*/CAM_DIR_NONE, 347939531Sken MSG_SIMPLE_Q_TAG, 348039531Sken /*dataptr*/NULL, 348139531Sken /*datalen*/0, 348239531Sken /*sense_len*/SSD_FULL_SIZE, 348339531Sken cdb_len, 348439531Sken /*timeout*/50 * 1000); 348539213Sgibbs 348674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 348774840Sken /*sense_flags*/SF_RETRY_UA); 348839213Sgibbs 348939213Sgibbs xpt_release_ccb(ccb); 349039213Sgibbs 349139213Sgibbs return(error); 349239213Sgibbs} 349339213Sgibbs 349439213Sgibbsstatic int 349539213Sgibbscdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 349639213Sgibbs u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 349739213Sgibbs{ 349839213Sgibbs struct scsi_play_msf *scsi_cmd; 349939213Sgibbs struct ccb_scsiio *csio; 350039213Sgibbs union ccb *ccb; 350139213Sgibbs int error; 350239213Sgibbs 350339213Sgibbs error = 0; 350439213Sgibbs 3505198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 350639213Sgibbs 350739213Sgibbs csio = &ccb->csio; 350839213Sgibbs 350939213Sgibbs cam_fill_csio(csio, 351039213Sgibbs /* retries */ 1, 351139213Sgibbs /* cbfcnp */ cddone, 351239531Sken /* flags */ CAM_DIR_NONE, 351339213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 351439213Sgibbs /* data_ptr */ NULL, 351539213Sgibbs /* dxfer_len */ 0, 351639213Sgibbs /* sense_len */ SSD_FULL_SIZE, 351739213Sgibbs sizeof(struct scsi_play_msf), 351839213Sgibbs /* timeout */ 50000); 351939213Sgibbs 352039213Sgibbs scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 352139213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 352239213Sgibbs 352339213Sgibbs scsi_cmd->op_code = PLAY_MSF; 352439213Sgibbs scsi_cmd->start_m = startm; 352539213Sgibbs scsi_cmd->start_s = starts; 352639213Sgibbs scsi_cmd->start_f = startf; 352739213Sgibbs scsi_cmd->end_m = endm; 352839213Sgibbs scsi_cmd->end_s = ends; 352939213Sgibbs scsi_cmd->end_f = endf; 353039213Sgibbs 353174840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 353274840Sken /*sense_flags*/SF_RETRY_UA); 353339213Sgibbs 353439213Sgibbs xpt_release_ccb(ccb); 353539213Sgibbs 353639213Sgibbs return(error); 353739213Sgibbs} 353839213Sgibbs 353939213Sgibbs 354039213Sgibbsstatic int 354139213Sgibbscdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 354239213Sgibbs u_int32_t etrack, u_int32_t eindex) 354339213Sgibbs{ 354439213Sgibbs struct scsi_play_track *scsi_cmd; 354539213Sgibbs struct ccb_scsiio *csio; 354639213Sgibbs union ccb *ccb; 354739213Sgibbs int error; 354839213Sgibbs 354939213Sgibbs error = 0; 355039213Sgibbs 3551198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 355239213Sgibbs 355339213Sgibbs csio = &ccb->csio; 355439213Sgibbs 355539213Sgibbs cam_fill_csio(csio, 355639213Sgibbs /* retries */ 1, 355739213Sgibbs /* cbfcnp */ cddone, 355839531Sken /* flags */ CAM_DIR_NONE, 355939213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 356039213Sgibbs /* data_ptr */ NULL, 356139213Sgibbs /* dxfer_len */ 0, 356239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 356339213Sgibbs sizeof(struct scsi_play_track), 356439213Sgibbs /* timeout */ 50000); 356539213Sgibbs 356639213Sgibbs scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 356739213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 356839213Sgibbs 356939213Sgibbs scsi_cmd->op_code = PLAY_TRACK; 357039213Sgibbs scsi_cmd->start_track = strack; 357139213Sgibbs scsi_cmd->start_index = sindex; 357239213Sgibbs scsi_cmd->end_track = etrack; 357339213Sgibbs scsi_cmd->end_index = eindex; 357439213Sgibbs 357574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 357674840Sken /*sense_flags*/SF_RETRY_UA); 357739213Sgibbs 357839213Sgibbs xpt_release_ccb(ccb); 357939213Sgibbs 358039213Sgibbs return(error); 358139213Sgibbs} 358239213Sgibbs 358339213Sgibbsstatic int 358439213Sgibbscdpause(struct cam_periph *periph, u_int32_t go) 358539213Sgibbs{ 358639213Sgibbs struct scsi_pause *scsi_cmd; 358739213Sgibbs struct ccb_scsiio *csio; 358839213Sgibbs union ccb *ccb; 358939213Sgibbs int error; 359039213Sgibbs 359139213Sgibbs error = 0; 359239213Sgibbs 3593198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 359439213Sgibbs 359539213Sgibbs csio = &ccb->csio; 359639213Sgibbs 359739213Sgibbs cam_fill_csio(csio, 359839213Sgibbs /* retries */ 1, 359939213Sgibbs /* cbfcnp */ cddone, 360039531Sken /* flags */ CAM_DIR_NONE, 360139213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 360239213Sgibbs /* data_ptr */ NULL, 360339213Sgibbs /* dxfer_len */ 0, 360439213Sgibbs /* sense_len */ SSD_FULL_SIZE, 360539213Sgibbs sizeof(struct scsi_pause), 360639213Sgibbs /* timeout */ 50000); 360739213Sgibbs 360839213Sgibbs scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 360939213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 361039213Sgibbs 361139213Sgibbs scsi_cmd->op_code = PAUSE; 361239213Sgibbs scsi_cmd->resume = go; 361339213Sgibbs 361474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 361574840Sken /*sense_flags*/SF_RETRY_UA); 361639213Sgibbs 361739213Sgibbs xpt_release_ccb(ccb); 361839213Sgibbs 361939213Sgibbs return(error); 362039213Sgibbs} 362139213Sgibbs 362239213Sgibbsstatic int 3623111206Skencdstartunit(struct cam_periph *periph, int load) 362439213Sgibbs{ 362539213Sgibbs union ccb *ccb; 362639213Sgibbs int error; 362739213Sgibbs 362839213Sgibbs error = 0; 362939213Sgibbs 3630198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 363139213Sgibbs 363239213Sgibbs scsi_start_stop(&ccb->csio, 363339213Sgibbs /* retries */ 1, 363439213Sgibbs /* cbfcnp */ cddone, 363539213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 363639213Sgibbs /* start */ TRUE, 3637111206Sken /* load_eject */ load, 363839213Sgibbs /* immediate */ FALSE, 363939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 364039213Sgibbs /* timeout */ 50000); 364139213Sgibbs 364274840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 364374840Sken /*sense_flags*/SF_RETRY_UA); 364439213Sgibbs 364539213Sgibbs xpt_release_ccb(ccb); 364639213Sgibbs 364739213Sgibbs return(error); 364839213Sgibbs} 364939213Sgibbs 365039213Sgibbsstatic int 365139213Sgibbscdstopunit(struct cam_periph *periph, u_int32_t eject) 365239213Sgibbs{ 365339213Sgibbs union ccb *ccb; 365439213Sgibbs int error; 365539213Sgibbs 365639213Sgibbs error = 0; 365739213Sgibbs 3658198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 365939213Sgibbs 366039213Sgibbs scsi_start_stop(&ccb->csio, 366139213Sgibbs /* retries */ 1, 366239213Sgibbs /* cbfcnp */ cddone, 366339213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 366439213Sgibbs /* start */ FALSE, 366539213Sgibbs /* load_eject */ eject, 366639213Sgibbs /* immediate */ FALSE, 366739213Sgibbs /* sense_len */ SSD_FULL_SIZE, 366839213Sgibbs /* timeout */ 50000); 366939213Sgibbs 367074840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 367174840Sken /*sense_flags*/SF_RETRY_UA); 367239213Sgibbs 367339213Sgibbs xpt_release_ccb(ccb); 367439213Sgibbs 367539213Sgibbs return(error); 367639213Sgibbs} 367760422Sken 367860422Skenstatic int 3679105421Snjlcdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3680105421Snjl{ 3681105421Snjl struct scsi_set_speed *scsi_cmd; 3682105421Snjl struct ccb_scsiio *csio; 3683105421Snjl union ccb *ccb; 3684105421Snjl int error; 3685105421Snjl 3686105421Snjl error = 0; 3687198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3688105421Snjl csio = &ccb->csio; 3689105421Snjl 3690107193Snjl /* Preserve old behavior: units in multiples of CDROM speed */ 3691107193Snjl if (rdspeed < 177) 3692107193Snjl rdspeed *= 177; 3693107193Snjl if (wrspeed < 177) 3694107193Snjl wrspeed *= 177; 3695107193Snjl 3696105421Snjl cam_fill_csio(csio, 3697105421Snjl /* retries */ 1, 3698105421Snjl /* cbfcnp */ cddone, 3699105421Snjl /* flags */ CAM_DIR_NONE, 3700105421Snjl /* tag_action */ MSG_SIMPLE_Q_TAG, 3701105421Snjl /* data_ptr */ NULL, 3702105421Snjl /* dxfer_len */ 0, 3703105421Snjl /* sense_len */ SSD_FULL_SIZE, 3704105421Snjl sizeof(struct scsi_set_speed), 3705105421Snjl /* timeout */ 50000); 3706105421Snjl 3707105421Snjl scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3708105421Snjl bzero(scsi_cmd, sizeof(*scsi_cmd)); 3709105421Snjl 3710105421Snjl scsi_cmd->opcode = SET_CD_SPEED; 3711105421Snjl scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3712105421Snjl scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3713105421Snjl 3714105421Snjl error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3715105421Snjl /*sense_flags*/SF_RETRY_UA); 3716105421Snjl 3717105421Snjl xpt_release_ccb(ccb); 3718105421Snjl 3719105421Snjl return(error); 3720105421Snjl} 3721105421Snjl 3722105421Snjlstatic int 372360422Skencdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 372460422Sken{ 372560422Sken union ccb *ccb; 372660422Sken u_int8_t *databuf; 372760422Sken u_int32_t lba; 372860422Sken int error; 372960422Sken int length; 373060422Sken 373160422Sken error = 0; 373260422Sken databuf = NULL; 373360422Sken lba = 0; 373460422Sken 373560422Sken switch (authinfo->format) { 373660422Sken case DVD_REPORT_AGID: 373760422Sken length = sizeof(struct scsi_report_key_data_agid); 373860422Sken break; 373960422Sken case DVD_REPORT_CHALLENGE: 374060422Sken length = sizeof(struct scsi_report_key_data_challenge); 374160422Sken break; 374260422Sken case DVD_REPORT_KEY1: 374360422Sken length = sizeof(struct scsi_report_key_data_key1_key2); 374460422Sken break; 374560422Sken case DVD_REPORT_TITLE_KEY: 374660422Sken length = sizeof(struct scsi_report_key_data_title); 374760422Sken /* The lba field is only set for the title key */ 374860422Sken lba = authinfo->lba; 374960422Sken break; 375060422Sken case DVD_REPORT_ASF: 375160422Sken length = sizeof(struct scsi_report_key_data_asf); 375260422Sken break; 375360422Sken case DVD_REPORT_RPC: 375460422Sken length = sizeof(struct scsi_report_key_data_rpc); 375560422Sken break; 375660422Sken case DVD_INVALIDATE_AGID: 375760422Sken length = 0; 375860422Sken break; 375960422Sken default: 3760200036Sscottl return (EINVAL); 376160422Sken } 376260422Sken 376360422Sken if (length != 0) { 3764111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 376560422Sken } else 376660422Sken databuf = NULL; 376760422Sken 3768200036Sscottl cam_periph_lock(periph); 3769200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 377060422Sken 377160422Sken scsi_report_key(&ccb->csio, 377260422Sken /* retries */ 1, 377360422Sken /* cbfcnp */ cddone, 377460422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 377560422Sken /* lba */ lba, 377660422Sken /* agid */ authinfo->agid, 377760422Sken /* key_format */ authinfo->format, 377860422Sken /* data_ptr */ databuf, 377960422Sken /* dxfer_len */ length, 378060422Sken /* sense_len */ SSD_FULL_SIZE, 378160422Sken /* timeout */ 50000); 378260422Sken 378374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 378474840Sken /*sense_flags*/SF_RETRY_UA); 378560422Sken 378660422Sken if (error != 0) 378760422Sken goto bailout; 378860422Sken 378960422Sken if (ccb->csio.resid != 0) { 3790164906Smjacob xpt_print(periph->path, "warning, residual for report key " 3791164906Smjacob "command is %d\n", ccb->csio.resid); 379260422Sken } 379360422Sken 379460422Sken switch(authinfo->format) { 379560422Sken case DVD_REPORT_AGID: { 379660422Sken struct scsi_report_key_data_agid *agid_data; 379760422Sken 379860422Sken agid_data = (struct scsi_report_key_data_agid *)databuf; 379960422Sken 380060422Sken authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 380160422Sken RKD_AGID_SHIFT; 380260422Sken break; 380360422Sken } 380460422Sken case DVD_REPORT_CHALLENGE: { 380560422Sken struct scsi_report_key_data_challenge *chal_data; 380660422Sken 380760422Sken chal_data = (struct scsi_report_key_data_challenge *)databuf; 380860422Sken 380960422Sken bcopy(chal_data->challenge_key, authinfo->keychal, 381060422Sken min(sizeof(chal_data->challenge_key), 381160422Sken sizeof(authinfo->keychal))); 381260422Sken break; 381360422Sken } 381460422Sken case DVD_REPORT_KEY1: { 381560422Sken struct scsi_report_key_data_key1_key2 *key1_data; 381660422Sken 381760422Sken key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 381860422Sken 381960422Sken bcopy(key1_data->key1, authinfo->keychal, 382060422Sken min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 382160422Sken break; 382260422Sken } 382360422Sken case DVD_REPORT_TITLE_KEY: { 382460422Sken struct scsi_report_key_data_title *title_data; 382560422Sken 382660422Sken title_data = (struct scsi_report_key_data_title *)databuf; 382760422Sken 382860422Sken authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 382960422Sken RKD_TITLE_CPM_SHIFT; 383060422Sken authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 383160422Sken RKD_TITLE_CP_SEC_SHIFT; 383260422Sken authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 383360422Sken RKD_TITLE_CMGS_SHIFT; 383460422Sken bcopy(title_data->title_key, authinfo->keychal, 383560422Sken min(sizeof(title_data->title_key), 383660422Sken sizeof(authinfo->keychal))); 383760422Sken break; 383860422Sken } 383960422Sken case DVD_REPORT_ASF: { 384060422Sken struct scsi_report_key_data_asf *asf_data; 384160422Sken 384260422Sken asf_data = (struct scsi_report_key_data_asf *)databuf; 384360422Sken 384460422Sken authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 384560422Sken break; 384660422Sken } 384760422Sken case DVD_REPORT_RPC: { 384860422Sken struct scsi_report_key_data_rpc *rpc_data; 384960422Sken 385060422Sken rpc_data = (struct scsi_report_key_data_rpc *)databuf; 385160422Sken 385260422Sken authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 385360422Sken RKD_RPC_TYPE_SHIFT; 385460422Sken authinfo->vend_rsts = 385560422Sken (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 385660422Sken RKD_RPC_VENDOR_RESET_SHIFT; 385760422Sken authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 385871752Sken authinfo->region = rpc_data->region_mask; 385971752Sken authinfo->rpc_scheme = rpc_data->rpc_scheme1; 386060422Sken break; 386160422Sken } 386260422Sken case DVD_INVALIDATE_AGID: 386360422Sken break; 386460422Sken default: 386560422Sken /* This should be impossible, since we checked above */ 386660422Sken error = EINVAL; 386760422Sken goto bailout; 386860422Sken break; /* NOTREACHED */ 386960422Sken } 3870200036Sscottl 387160422Skenbailout: 3872200036Sscottl xpt_release_ccb(ccb); 3873200036Sscottl cam_periph_unlock(periph); 3874200036Sscottl 387560422Sken if (databuf != NULL) 387660422Sken free(databuf, M_DEVBUF); 387760422Sken 387860422Sken return(error); 387960422Sken} 388060422Sken 388160422Skenstatic int 388260422Skencdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 388360422Sken{ 388460422Sken union ccb *ccb; 388560422Sken u_int8_t *databuf; 388660422Sken int length; 388760422Sken int error; 388860422Sken 388960422Sken error = 0; 389060422Sken databuf = NULL; 389160422Sken 389260422Sken switch(authinfo->format) { 389360422Sken case DVD_SEND_CHALLENGE: { 389460422Sken struct scsi_report_key_data_challenge *challenge_data; 389560422Sken 389660422Sken length = sizeof(*challenge_data); 389760422Sken 3898111119Simp challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 389960422Sken 390060422Sken databuf = (u_int8_t *)challenge_data; 390160422Sken 390260422Sken scsi_ulto2b(length - sizeof(challenge_data->data_len), 390360422Sken challenge_data->data_len); 390460422Sken 390560422Sken bcopy(authinfo->keychal, challenge_data->challenge_key, 390660422Sken min(sizeof(authinfo->keychal), 390760422Sken sizeof(challenge_data->challenge_key))); 390860422Sken break; 390960422Sken } 391060422Sken case DVD_SEND_KEY2: { 391160422Sken struct scsi_report_key_data_key1_key2 *key2_data; 391260422Sken 391360422Sken length = sizeof(*key2_data); 391460422Sken 3915111119Simp key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 391660422Sken 391760422Sken databuf = (u_int8_t *)key2_data; 391860422Sken 391960422Sken scsi_ulto2b(length - sizeof(key2_data->data_len), 392060422Sken key2_data->data_len); 392160422Sken 392260422Sken bcopy(authinfo->keychal, key2_data->key1, 392360422Sken min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 392460422Sken 392560422Sken break; 392660422Sken } 392760422Sken case DVD_SEND_RPC: { 392860422Sken struct scsi_send_key_data_rpc *rpc_data; 392960422Sken 393060422Sken length = sizeof(*rpc_data); 393160422Sken 3932111119Simp rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 393360422Sken 393460422Sken databuf = (u_int8_t *)rpc_data; 393560422Sken 393660422Sken scsi_ulto2b(length - sizeof(rpc_data->data_len), 393760422Sken rpc_data->data_len); 393860422Sken 393960422Sken rpc_data->region_code = authinfo->region; 394060422Sken break; 394160422Sken } 394260422Sken default: 3943200036Sscottl return (EINVAL); 394460422Sken } 394560422Sken 3946200036Sscottl cam_periph_lock(periph); 3947200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3948200036Sscottl 394960422Sken scsi_send_key(&ccb->csio, 395060422Sken /* retries */ 1, 395160422Sken /* cbfcnp */ cddone, 395260422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 395360422Sken /* agid */ authinfo->agid, 395460422Sken /* key_format */ authinfo->format, 395560422Sken /* data_ptr */ databuf, 395660422Sken /* dxfer_len */ length, 395760422Sken /* sense_len */ SSD_FULL_SIZE, 395860422Sken /* timeout */ 50000); 395960422Sken 396074840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 396174840Sken /*sense_flags*/SF_RETRY_UA); 396260422Sken 3963200036Sscottl xpt_release_ccb(ccb); 3964200036Sscottl cam_periph_unlock(periph); 396560422Sken 396660422Sken if (databuf != NULL) 396760422Sken free(databuf, M_DEVBUF); 396860422Sken 396960422Sken return(error); 397060422Sken} 397160422Sken 397260422Skenstatic int 397360422Skencdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 397460422Sken{ 397560422Sken union ccb *ccb; 397660422Sken u_int8_t *databuf; 397760422Sken u_int32_t address; 397860422Sken int error; 397960422Sken int length; 398060422Sken 398160422Sken error = 0; 398260422Sken databuf = NULL; 398360422Sken /* The address is reserved for many of the formats */ 398460422Sken address = 0; 398560422Sken 398660422Sken switch(dvdstruct->format) { 398760422Sken case DVD_STRUCT_PHYSICAL: 398860422Sken length = sizeof(struct scsi_read_dvd_struct_data_physical); 398960422Sken break; 399060422Sken case DVD_STRUCT_COPYRIGHT: 399160422Sken length = sizeof(struct scsi_read_dvd_struct_data_copyright); 399260422Sken break; 399360422Sken case DVD_STRUCT_DISCKEY: 399460422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 399560422Sken break; 399660422Sken case DVD_STRUCT_BCA: 399760422Sken length = sizeof(struct scsi_read_dvd_struct_data_bca); 399860422Sken break; 399960422Sken case DVD_STRUCT_MANUFACT: 400060422Sken length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 400160422Sken break; 400260422Sken case DVD_STRUCT_CMI: 4003200036Sscottl return (ENODEV); 400460422Sken case DVD_STRUCT_PROTDISCID: 400560422Sken length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 400660422Sken break; 400760422Sken case DVD_STRUCT_DISCKEYBLOCK: 400860422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 400960422Sken break; 401060422Sken case DVD_STRUCT_DDS: 401160422Sken length = sizeof(struct scsi_read_dvd_struct_data_dds); 401260422Sken break; 401360422Sken case DVD_STRUCT_MEDIUM_STAT: 401460422Sken length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 401560422Sken break; 401660422Sken case DVD_STRUCT_SPARE_AREA: 401760422Sken length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 401860422Sken break; 401960422Sken case DVD_STRUCT_RMD_LAST: 4020200036Sscottl return (ENODEV); 402160422Sken case DVD_STRUCT_RMD_RMA: 4022200036Sscottl return (ENODEV); 402360422Sken case DVD_STRUCT_PRERECORDED: 402460422Sken length = sizeof(struct scsi_read_dvd_struct_data_leadin); 402560422Sken break; 402660422Sken case DVD_STRUCT_UNIQUEID: 402760422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 402860422Sken break; 402960422Sken case DVD_STRUCT_DCB: 4030200036Sscottl return (ENODEV); 403160422Sken case DVD_STRUCT_LIST: 403260422Sken /* 403360422Sken * This is the maximum allocation length for the READ DVD 403460422Sken * STRUCTURE command. There's nothing in the MMC3 spec 403560422Sken * that indicates a limit in the amount of data that can 403660422Sken * be returned from this call, other than the limits 403760422Sken * imposed by the 2-byte length variables. 403860422Sken */ 403960422Sken length = 65535; 404060422Sken break; 404160422Sken default: 4042200036Sscottl return (EINVAL); 404360422Sken } 404460422Sken 404560422Sken if (length != 0) { 4046111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 404760422Sken } else 404860422Sken databuf = NULL; 404960422Sken 4050200036Sscottl cam_periph_lock(periph); 4051200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4052200036Sscottl 405360422Sken scsi_read_dvd_structure(&ccb->csio, 405460422Sken /* retries */ 1, 405560422Sken /* cbfcnp */ cddone, 405660422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 405760422Sken /* lba */ address, 405860422Sken /* layer_number */ dvdstruct->layer_num, 405960422Sken /* key_format */ dvdstruct->format, 406060422Sken /* agid */ dvdstruct->agid, 406160422Sken /* data_ptr */ databuf, 406260422Sken /* dxfer_len */ length, 406360422Sken /* sense_len */ SSD_FULL_SIZE, 406460422Sken /* timeout */ 50000); 406560422Sken 406674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 406774840Sken /*sense_flags*/SF_RETRY_UA); 406860422Sken 406960422Sken if (error != 0) 407060422Sken goto bailout; 407160422Sken 407260422Sken switch(dvdstruct->format) { 407360422Sken case DVD_STRUCT_PHYSICAL: { 407460422Sken struct scsi_read_dvd_struct_data_layer_desc *inlayer; 407560422Sken struct dvd_layer *outlayer; 407660422Sken struct scsi_read_dvd_struct_data_physical *phys_data; 407760422Sken 407860422Sken phys_data = 407960422Sken (struct scsi_read_dvd_struct_data_physical *)databuf; 408060422Sken inlayer = &phys_data->layer_desc; 408160422Sken outlayer = (struct dvd_layer *)&dvdstruct->data; 408260422Sken 408360422Sken dvdstruct->length = sizeof(*inlayer); 408460422Sken 408560422Sken outlayer->book_type = (inlayer->book_type_version & 408660422Sken RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 408760422Sken outlayer->book_version = (inlayer->book_type_version & 408860422Sken RDSD_BOOK_VERSION_MASK); 408960422Sken outlayer->disc_size = (inlayer->disc_size_max_rate & 409060422Sken RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 409160422Sken outlayer->max_rate = (inlayer->disc_size_max_rate & 409260422Sken RDSD_MAX_RATE_MASK); 409360422Sken outlayer->nlayers = (inlayer->layer_info & 409460422Sken RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 409560422Sken outlayer->track_path = (inlayer->layer_info & 409660422Sken RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 409760422Sken outlayer->layer_type = (inlayer->layer_info & 409860422Sken RDSD_LAYER_TYPE_MASK); 409960422Sken outlayer->linear_density = (inlayer->density & 410060422Sken RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 410160422Sken outlayer->track_density = (inlayer->density & 410260422Sken RDSD_TRACK_DENSITY_MASK); 410360422Sken outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 410460422Sken RDSD_BCA_SHIFT; 410560422Sken outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 410660422Sken outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 410760422Sken outlayer->end_sector_l0 = 410860422Sken scsi_3btoul(inlayer->end_sector_layer0); 410960422Sken break; 411060422Sken } 411160422Sken case DVD_STRUCT_COPYRIGHT: { 411260422Sken struct scsi_read_dvd_struct_data_copyright *copy_data; 411360422Sken 411460422Sken copy_data = (struct scsi_read_dvd_struct_data_copyright *) 411560422Sken databuf; 411660422Sken 411760422Sken dvdstruct->cpst = copy_data->cps_type; 411860422Sken dvdstruct->rmi = copy_data->region_info; 411960422Sken dvdstruct->length = 0; 412060422Sken 412160422Sken break; 412260422Sken } 412360422Sken default: 412460422Sken /* 412560422Sken * Tell the user what the overall length is, no matter 412660422Sken * what we can actually fit in the data buffer. 412760422Sken */ 412860422Sken dvdstruct->length = length - ccb->csio.resid - 412960422Sken sizeof(struct scsi_read_dvd_struct_data_header); 413060422Sken 413160422Sken /* 413260422Sken * But only actually copy out the smaller of what we read 413360422Sken * in or what the structure can take. 413460422Sken */ 413560422Sken bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 413660422Sken dvdstruct->data, 413760422Sken min(sizeof(dvdstruct->data), dvdstruct->length)); 413860422Sken break; 413960422Sken } 4140200036Sscottl 414160422Skenbailout: 4142200036Sscottl xpt_release_ccb(ccb); 4143200036Sscottl cam_periph_unlock(periph); 414460422Sken 414560422Sken if (databuf != NULL) 414660422Sken free(databuf, M_DEVBUF); 414760422Sken 414860422Sken return(error); 414960422Sken} 415060422Sken 415160422Skenvoid 415260422Skenscsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 415360422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 415460422Sken u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 415560422Sken u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 415660422Sken u_int8_t sense_len, u_int32_t timeout) 415760422Sken{ 415860422Sken struct scsi_report_key *scsi_cmd; 415960422Sken 416060422Sken scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 416160422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 416260422Sken scsi_cmd->opcode = REPORT_KEY; 416360422Sken scsi_ulto4b(lba, scsi_cmd->lba); 416460422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 416560422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 416660422Sken (key_format & RK_KF_KEYFORMAT_MASK); 416760422Sken 416860422Sken cam_fill_csio(csio, 416960422Sken retries, 417060422Sken cbfcnp, 417160422Sken /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 417260422Sken tag_action, 417360422Sken /*data_ptr*/ data_ptr, 417460422Sken /*dxfer_len*/ dxfer_len, 417560422Sken sense_len, 417660422Sken sizeof(*scsi_cmd), 417760422Sken timeout); 417860422Sken} 417960422Sken 418060422Skenvoid 418160422Skenscsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 418260422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 418360422Sken u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 418460422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 418560422Sken u_int32_t timeout) 418660422Sken{ 418760422Sken struct scsi_send_key *scsi_cmd; 418860422Sken 418960422Sken scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 419060422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 419160422Sken scsi_cmd->opcode = SEND_KEY; 419260422Sken 419360422Sken scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 419460422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 419560422Sken (key_format & RK_KF_KEYFORMAT_MASK); 419660422Sken 419760422Sken cam_fill_csio(csio, 419860422Sken retries, 419960422Sken cbfcnp, 420060422Sken /*flags*/ CAM_DIR_OUT, 420160422Sken tag_action, 420260422Sken /*data_ptr*/ data_ptr, 420360422Sken /*dxfer_len*/ dxfer_len, 420460422Sken sense_len, 420560422Sken sizeof(*scsi_cmd), 420660422Sken timeout); 420760422Sken} 420860422Sken 420960422Sken 421060422Skenvoid 421160422Skenscsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 421260422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 421360422Sken u_int8_t tag_action, u_int32_t address, 421460422Sken u_int8_t layer_number, u_int8_t format, u_int8_t agid, 421560422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, 421660422Sken u_int8_t sense_len, u_int32_t timeout) 421760422Sken{ 421860422Sken struct scsi_read_dvd_structure *scsi_cmd; 421960422Sken 422060422Sken scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 422160422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 422260422Sken scsi_cmd->opcode = READ_DVD_STRUCTURE; 422360422Sken 422460422Sken scsi_ulto4b(address, scsi_cmd->address); 422560422Sken scsi_cmd->layer_number = layer_number; 422660422Sken scsi_cmd->format = format; 422760422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 422860422Sken /* The AGID is the top two bits of this byte */ 422960422Sken scsi_cmd->agid = agid << 6; 423060422Sken 423160422Sken cam_fill_csio(csio, 423260422Sken retries, 423360422Sken cbfcnp, 423460422Sken /*flags*/ CAM_DIR_IN, 423560422Sken tag_action, 423660422Sken /*data_ptr*/ data_ptr, 423760422Sken /*dxfer_len*/ dxfer_len, 423860422Sken sense_len, 423960422Sken sizeof(*scsi_cmd), 424060422Sken timeout); 424160422Sken} 4242