scsi_cd.c revision 237478
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 237478 2012-06-23 12:32:53Z 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_CHANGER = 0x0040, 101120884Sthomas CD_FLAG_ACTIVE = 0x0080, 102120884Sthomas CD_FLAG_SCHED_ON_COMP = 0x0100, 103120884Sthomas CD_FLAG_RETRY_UA = 0x0200, 104120884Sthomas CD_FLAG_VALID_MEDIA = 0x0400, 105120884Sthomas CD_FLAG_VALID_TOC = 0x0800, 106168752Sscottl CD_FLAG_SCTX_INIT = 0x1000, 107168752Sscottl CD_FLAG_OPEN = 0x2000 10839213Sgibbs} cd_flags; 10939213Sgibbs 11039213Sgibbstypedef enum { 11139213Sgibbs CD_CCB_PROBE = 0x01, 11239213Sgibbs CD_CCB_BUFFER_IO = 0x02, 11339213Sgibbs CD_CCB_WAITING = 0x03, 11439213Sgibbs CD_CCB_TYPE_MASK = 0x0F, 11539213Sgibbs CD_CCB_RETRY_UA = 0x10 11639213Sgibbs} cd_ccb_state; 11739213Sgibbs 11839213Sgibbstypedef enum { 11939213Sgibbs CHANGER_TIMEOUT_SCHED = 0x01, 12039213Sgibbs CHANGER_SHORT_TMOUT_SCHED = 0x02, 12139213Sgibbs CHANGER_MANUAL_CALL = 0x04, 12239213Sgibbs CHANGER_NEED_TIMEOUT = 0x08 12339213Sgibbs} cd_changer_flags; 12439213Sgibbs 12539213Sgibbs#define ccb_state ppriv_field0 12639213Sgibbs#define ccb_bp ppriv_ptr1 12739213Sgibbs 128111206Skenstruct cd_tocdata { 129111206Sken struct ioc_toc_header header; 130111206Sken struct cd_toc_entry entries[100]; 131111206Sken}; 132111206Sken 133111206Skenstruct cd_toc_single { 134111206Sken struct ioc_toc_header header; 135111206Sken struct cd_toc_entry entry; 136111206Sken}; 137111206Sken 13839213Sgibbstypedef enum { 13939213Sgibbs CD_STATE_PROBE, 14039213Sgibbs CD_STATE_NORMAL 14139213Sgibbs} cd_state; 14239213Sgibbs 14339213Sgibbsstruct cd_softc { 14439213Sgibbs cam_pinfo pinfo; 14539213Sgibbs cd_state state; 14646581Sken volatile cd_flags flags; 14759249Sphk struct bio_queue_head bio_queue; 14860938Sjake LIST_HEAD(, ccb_hdr) pending_ccbs; 14939213Sgibbs struct cd_params params; 15039213Sgibbs union ccb saved_ccb; 15139213Sgibbs cd_quirks quirks; 15260938Sjake STAILQ_ENTRY(cd_softc) changer_links; 15339213Sgibbs struct cdchanger *changer; 15439213Sgibbs int bufs_left; 15539213Sgibbs struct cam_periph *periph; 156111206Sken int minimum_command_size; 157112262Sphk int outstanding_cmds; 158119708Sken struct task sysctl_task; 159111206Sken struct sysctl_ctx_list sysctl_ctx; 160111206Sken struct sysctl_oid *sysctl_tree; 161111206Sken STAILQ_HEAD(, cd_mode_params) mode_queue; 162111206Sken struct cd_tocdata toc; 163125975Sphk struct disk *disk; 16439213Sgibbs}; 16539213Sgibbs 166111206Skenstruct cd_page_sizes { 167111206Sken int page; 168111206Sken int page_size; 169111206Sken}; 170111206Sken 171111206Skenstatic struct cd_page_sizes cd_page_size_table[] = 172111206Sken{ 173111206Sken { AUDIO_PAGE, sizeof(struct cd_audio_page)} 174111206Sken}; 175111206Sken 17639213Sgibbsstruct cd_quirk_entry { 17739213Sgibbs struct scsi_inquiry_pattern inq_pat; 17839213Sgibbs cd_quirks quirks; 17939213Sgibbs}; 18039213Sgibbs 18139213Sgibbs/* 182111206Sken * The changer quirk entries aren't strictly necessary. Basically, what 183111206Sken * they do is tell cdregister() up front that a device is a changer. 184111206Sken * Otherwise, it will figure that fact out once it sees a LUN on the device 185111206Sken * that is greater than 0. If it is known up front that a device is a changer, 186111206Sken * all I/O to the device will go through the changer scheduling routines, as 18739213Sgibbs * opposed to the "normal" CD code. 188111206Sken * 189111206Sken * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 190111206Sken * your device hangs when it gets a 10 byte command. Adding a quirk just 191111206Sken * to get rid of the informative diagnostic message is not acceptable. All 192111206Sken * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 193111206Sken * referenced in a comment along with the quirk) , and must be approved by 194111206Sken * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 195111206Sken * be removed until the submitter can explain why they are needed. 196111206Sken * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 197111206Sken * when the CAM_NEW_TRAN_CODE work is done. 19839213Sgibbs */ 19939213Sgibbsstatic struct cd_quirk_entry cd_quirk_table[] = 20039213Sgibbs{ 20139213Sgibbs { 20239213Sgibbs { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 20339213Sgibbs /*quirks*/ CD_Q_CHANGER 20439213Sgibbs }, 20539213Sgibbs { 20654451Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*", 20739213Sgibbs "*"}, /* quirks */ CD_Q_CHANGER 20840262Sken }, 20940262Sken { 21067752Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"}, 21167752Sken /* quirks */ CD_Q_CHANGER 21267752Sken }, 21367752Sken { 21440262Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 21540262Sken /* quirks */ CD_Q_BCD_TRACKS 21639213Sgibbs } 21739213Sgibbs}; 21839213Sgibbs 219120599Sphkstatic disk_open_t cdopen; 220120599Sphkstatic disk_close_t cdclose; 221120599Sphkstatic disk_ioctl_t cdioctl; 222120599Sphkstatic disk_strategy_t cdstrategy; 22339213Sgibbs 22439213Sgibbsstatic periph_init_t cdinit; 22539213Sgibbsstatic periph_ctor_t cdregister; 22639213Sgibbsstatic periph_dtor_t cdcleanup; 22739213Sgibbsstatic periph_start_t cdstart; 22840603Skenstatic periph_oninv_t cdoninvalidate; 22939213Sgibbsstatic void cdasync(void *callback_arg, u_int32_t code, 23039213Sgibbs struct cam_path *path, void *arg); 231111206Skenstatic int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 23239213Sgibbsstatic void cdshorttimeout(void *arg); 23339213Sgibbsstatic void cdschedule(struct cam_periph *periph, int priority); 23439213Sgibbsstatic void cdrunchangerqueue(void *arg); 23539213Sgibbsstatic void cdchangerschedule(struct cd_softc *softc); 23639213Sgibbsstatic int cdrunccb(union ccb *ccb, 23739213Sgibbs int (*error_routine)(union ccb *ccb, 23839213Sgibbs u_int32_t cam_flags, 23939213Sgibbs u_int32_t sense_flags), 24039213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags); 241111206Skenstatic union ccb *cdgetccb(struct cam_periph *periph, 24239213Sgibbs u_int32_t priority); 24339213Sgibbsstatic void cddone(struct cam_periph *periph, 24439213Sgibbs union ccb *start_ccb); 245111206Skenstatic union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 246111206Skenstatic int cdgetpagesize(int page_num); 247111206Skenstatic void cdprevent(struct cam_periph *periph, int action); 248111206Skenstatic int cdcheckmedia(struct cam_periph *periph); 249111206Skenstatic int cdsize(struct cam_periph *periph, u_int32_t *size); 250111206Skenstatic int cd6byteworkaround(union ccb *ccb); 25139213Sgibbsstatic int cderror(union ccb *ccb, u_int32_t cam_flags, 25239213Sgibbs u_int32_t sense_flags); 25339213Sgibbsstatic int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 254111206Sken u_int32_t start, u_int8_t *data, 255111206Sken u_int32_t len, u_int32_t sense_flags); 25639213Sgibbsstatic int cdgetmode(struct cam_periph *periph, 257111206Sken struct cd_mode_params *data, u_int32_t page); 25839213Sgibbsstatic int cdsetmode(struct cam_periph *periph, 259111206Sken struct cd_mode_params *data); 26039213Sgibbsstatic int cdplay(struct cam_periph *periph, u_int32_t blk, 26139213Sgibbs u_int32_t len); 26239213Sgibbsstatic int cdreadsubchannel(struct cam_periph *periph, 26339213Sgibbs u_int32_t mode, u_int32_t format, 26439213Sgibbs int track, 26539213Sgibbs struct cd_sub_channel_info *data, 26639213Sgibbs u_int32_t len); 26739213Sgibbsstatic int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 26839213Sgibbs u_int32_t starts, u_int32_t startf, 26939213Sgibbs u_int32_t endm, u_int32_t ends, 27039213Sgibbs u_int32_t endf); 27139213Sgibbsstatic int cdplaytracks(struct cam_periph *periph, 27239213Sgibbs u_int32_t strack, u_int32_t sindex, 27339213Sgibbs u_int32_t etrack, u_int32_t eindex); 27439213Sgibbsstatic int cdpause(struct cam_periph *periph, u_int32_t go); 27539213Sgibbsstatic int cdstopunit(struct cam_periph *periph, u_int32_t eject); 276111206Skenstatic int cdstartunit(struct cam_periph *periph, int load); 277105421Snjlstatic int cdsetspeed(struct cam_periph *periph, 278105421Snjl u_int32_t rdspeed, u_int32_t wrspeed); 27960422Skenstatic int cdreportkey(struct cam_periph *periph, 28060422Sken struct dvd_authinfo *authinfo); 28160422Skenstatic int cdsendkey(struct cam_periph *periph, 28260422Sken struct dvd_authinfo *authinfo); 28360422Skenstatic int cdreaddvdstructure(struct cam_periph *periph, 28460422Sken struct dvd_struct *dvdstruct); 28539213Sgibbs 28639213Sgibbsstatic struct periph_driver cddriver = 28739213Sgibbs{ 28839213Sgibbs cdinit, "cd", 28939213Sgibbs TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 29039213Sgibbs}; 29139213Sgibbs 29272119SpeterPERIPHDRIVER_DECLARE(cd, cddriver); 29339213Sgibbs 294186882Simp#ifndef CD_DEFAULT_RETRY 295186882Simp#define CD_DEFAULT_RETRY 4 296186882Simp#endif 29739213Sgibbs#ifndef CHANGER_MIN_BUSY_SECONDS 29846747Sken#define CHANGER_MIN_BUSY_SECONDS 5 29939213Sgibbs#endif 30039213Sgibbs#ifndef CHANGER_MAX_BUSY_SECONDS 30146747Sken#define CHANGER_MAX_BUSY_SECONDS 15 30239213Sgibbs#endif 30339213Sgibbs 304186882Simpstatic int cd_retry_count = CD_DEFAULT_RETRY; 30539213Sgibbsstatic int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 30639213Sgibbsstatic int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 30739213Sgibbs 308227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 309227309Sedstatic SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, 310227309Sed "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 339227293Sedstatic MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers"); 340169562Sscottl 341104880Sphkstatic void 34239213Sgibbscdinit(void) 34339213Sgibbs{ 34439213Sgibbs cam_status status; 34539213Sgibbs 346168752Sscottl mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF); 347168752Sscottl STAILQ_INIT(&changerq); 348168752Sscottl 34939213Sgibbs /* 35039213Sgibbs * Install a global async callback. This callback will 35139213Sgibbs * receive async callbacks like "new device found". 35239213Sgibbs */ 353169605Sscottl status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL); 35439213Sgibbs 35539213Sgibbs if (status != CAM_REQ_CMP) { 35639213Sgibbs printf("cd: Failed to attach master async callback " 35739213Sgibbs "due to status 0x%x!\n", status); 35839213Sgibbs } 35939213Sgibbs} 36039213Sgibbs 36139213Sgibbsstatic void 36240603Skencdoninvalidate(struct cam_periph *periph) 36340603Sken{ 36440603Sken struct cd_softc *softc; 36540603Sken 36640603Sken softc = (struct cd_softc *)periph->softc; 36740603Sken 36840603Sken /* 36940603Sken * De-register any async callbacks. 37040603Sken */ 371169605Sscottl xpt_register_async(0, cdasync, periph, periph->path); 37240603Sken 37340603Sken softc->flags |= CD_FLAG_INVALID; 37440603Sken 37540603Sken /* 37640603Sken * Return all queued I/O with ENXIO. 37740603Sken * XXX Handle any transactions queued to the card 37840603Sken * with XPT_ABORT_CCB. 37940603Sken */ 380112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 38140603Sken 38240603Sken /* 38340603Sken * If this device is part of a changer, and it was scheduled 38440603Sken * to run, remove it from the run queue since we just nuked 38540603Sken * all of its scheduled I/O. 38640603Sken */ 38740603Sken if ((softc->flags & CD_FLAG_CHANGER) 38840603Sken && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 38940603Sken camq_remove(&softc->changer->devq, softc->pinfo.index); 39040603Sken 391152565Sjdp disk_gone(softc->disk); 392164906Smjacob xpt_print(periph->path, "lost device\n"); 39340603Sken} 39440603Sken 39540603Skenstatic void 39639213Sgibbscdcleanup(struct cam_periph *periph) 39739213Sgibbs{ 39839213Sgibbs struct cd_softc *softc; 39939213Sgibbs 40039213Sgibbs softc = (struct cd_softc *)periph->softc; 40139213Sgibbs 402164906Smjacob xpt_print(periph->path, "removing device entry\n"); 40340603Sken 40439213Sgibbs /* 40539213Sgibbs * In the queued, non-active case, the device in question 40639213Sgibbs * has already been removed from the changer run queue. Since this 40739213Sgibbs * device is active, we need to de-activate it, and schedule 40839213Sgibbs * another device to run. (if there is another one to run) 40939213Sgibbs */ 41039213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 41139213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 41239213Sgibbs 41339213Sgibbs /* 41439213Sgibbs * The purpose of the short timeout is soley to determine 41539213Sgibbs * whether the current device has finished or not. Well, 41639213Sgibbs * since we're removing the active device, we know that it 41739213Sgibbs * is finished. So, get rid of the short timeout. 41839213Sgibbs * Otherwise, if we're in the time period before the short 41939213Sgibbs * timeout fires, and there are no other devices in the 42039213Sgibbs * queue to run, there won't be any other device put in the 42139213Sgibbs * active slot. i.e., when we call cdrunchangerqueue() 42239213Sgibbs * below, it won't do anything. Then, when the short 42339213Sgibbs * timeout fires, it'll look at the "current device", which 42439213Sgibbs * we are free below, and possibly panic the kernel on a 42539213Sgibbs * bogus pointer reference. 42639213Sgibbs * 42739213Sgibbs * The long timeout doesn't really matter, since we 42839213Sgibbs * decrement the qfrozen_cnt to indicate that there is 42939213Sgibbs * nothing in the active slot now. Therefore, there won't 43039213Sgibbs * be any bogus pointer references there. 43139213Sgibbs */ 43239213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 433168752Sscottl callout_stop(&softc->changer->short_handle); 43439213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 43539213Sgibbs } 436203108Smav softc->changer->devq.qfrozen_cnt[0]--; 43739213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 43839213Sgibbs cdrunchangerqueue(softc->changer); 43939213Sgibbs } 44039213Sgibbs 44139213Sgibbs /* 44239213Sgibbs * If we're removing the last device on the changer, go ahead and 44339213Sgibbs * remove the changer device structure. 44439213Sgibbs */ 44539213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 44639213Sgibbs && (--softc->changer->num_devices == 0)) { 44739213Sgibbs 44839213Sgibbs /* 44939213Sgibbs * Theoretically, there shouldn't be any timeouts left, but 45039213Sgibbs * I'm not completely sure that that will be the case. So, 45139213Sgibbs * it won't hurt to check and see if there are any left. 45239213Sgibbs */ 45339213Sgibbs if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 454168752Sscottl callout_stop(&softc->changer->long_handle); 45539213Sgibbs softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 45639213Sgibbs } 45739213Sgibbs 45839213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 459168752Sscottl callout_stop(&softc->changer->short_handle); 46039213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 46139213Sgibbs } 46239213Sgibbs 463168752Sscottl mtx_lock(&changerq_mtx); 46460938Sjake STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 46539213Sgibbs changer_links); 466168752Sscottl num_changers--; 467168752Sscottl mtx_unlock(&changerq_mtx); 468164906Smjacob xpt_print(periph->path, "removing changer entry\n"); 46939213Sgibbs free(softc->changer, M_DEVBUF); 47039213Sgibbs } 471168786Sscottl cam_periph_unlock(periph); 472188503Sjhb if ((softc->flags & CD_FLAG_SCTX_INIT) != 0 473188503Sjhb && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 474188503Sjhb xpt_print(periph->path, "can't remove sysctl context\n"); 475188503Sjhb } 476188503Sjhb 477125975Sphk disk_destroy(softc->disk); 478188503Sjhb free(softc, M_DEVBUF); 479168786Sscottl cam_periph_lock(periph); 48039213Sgibbs} 48139213Sgibbs 48239213Sgibbsstatic void 48339213Sgibbscdasync(void *callback_arg, u_int32_t code, 48439213Sgibbs struct cam_path *path, void *arg) 48539213Sgibbs{ 48639213Sgibbs struct cam_periph *periph; 48739213Sgibbs 48839213Sgibbs periph = (struct cam_periph *)callback_arg; 48939213Sgibbs switch (code) { 49039213Sgibbs case AC_FOUND_DEVICE: 49139213Sgibbs { 49239213Sgibbs struct ccb_getdev *cgd; 49339213Sgibbs cam_status status; 49439213Sgibbs 49539213Sgibbs cgd = (struct ccb_getdev *)arg; 49679177Smjacob if (cgd == NULL) 49779177Smjacob break; 49839213Sgibbs 499195534Sscottl if (cgd->protocol != PROTO_SCSI) 500195534Sscottl break; 501195534Sscottl 50256148Smjacob if (SID_TYPE(&cgd->inq_data) != T_CDROM 50356148Smjacob && SID_TYPE(&cgd->inq_data) != T_WORM) 50439213Sgibbs break; 50539213Sgibbs 50639213Sgibbs /* 50739213Sgibbs * Allocate a peripheral instance for 50839213Sgibbs * this device and start the probe 50939213Sgibbs * process. 51039213Sgibbs */ 51140603Sken status = cam_periph_alloc(cdregister, cdoninvalidate, 51240603Sken cdcleanup, cdstart, 51340603Sken "cd", CAM_PERIPH_BIO, 51440603Sken cgd->ccb_h.path, cdasync, 51540603Sken AC_FOUND_DEVICE, cgd); 51639213Sgibbs 51739213Sgibbs if (status != CAM_REQ_CMP 51839213Sgibbs && status != CAM_REQ_INPROG) 51939213Sgibbs printf("cdasync: Unable to attach new device " 52039213Sgibbs "due to status 0x%x\n", status); 52139213Sgibbs 52239213Sgibbs break; 52339213Sgibbs } 52439213Sgibbs case AC_SENT_BDR: 52539213Sgibbs case AC_BUS_RESET: 52639213Sgibbs { 52739213Sgibbs struct cd_softc *softc; 52839213Sgibbs struct ccb_hdr *ccbh; 52939213Sgibbs 53039213Sgibbs softc = (struct cd_softc *)periph->softc; 53139213Sgibbs /* 53239213Sgibbs * Don't fail on the expected unit attention 53339213Sgibbs * that will occur. 53439213Sgibbs */ 53539213Sgibbs softc->flags |= CD_FLAG_RETRY_UA; 53671999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 53739213Sgibbs ccbh->ccb_state |= CD_CCB_RETRY_UA; 53847413Sgibbs /* FALLTHROUGH */ 53939213Sgibbs } 54039213Sgibbs default: 54147413Sgibbs cam_periph_async(periph, code, path, arg); 54239213Sgibbs break; 54339213Sgibbs } 54439213Sgibbs} 54539213Sgibbs 546119708Skenstatic void 547119708Skencdsysctlinit(void *context, int pending) 548119708Sken{ 549119708Sken struct cam_periph *periph; 550119708Sken struct cd_softc *softc; 551119708Sken char tmpstr[80], tmpstr2[80]; 552119708Sken 553119708Sken periph = (struct cam_periph *)context; 554168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 555168752Sscottl return; 556168752Sscottl 557119708Sken softc = (struct cd_softc *)periph->softc; 558119708Sken snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 559119708Sken snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 560119708Sken 561119708Sken sysctl_ctx_init(&softc->sysctl_ctx); 562120884Sthomas softc->flags |= CD_FLAG_SCTX_INIT; 563119708Sken softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 564119708Sken SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 565119708Sken tmpstr2, CTLFLAG_RD, 0, tmpstr); 566119708Sken 567119708Sken if (softc->sysctl_tree == NULL) { 568119708Sken printf("cdsysctlinit: unable to allocate sysctl tree\n"); 569168752Sscottl cam_periph_release(periph); 570119708Sken return; 571119708Sken } 572119708Sken 573119708Sken /* 574119708Sken * Now register the sysctl handler, so the user can the value on 575119708Sken * the fly. 576119708Sken */ 577119708Sken SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 578119708Sken OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 579119708Sken &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 580119708Sken "Minimum CDB size"); 581119708Sken 582168752Sscottl cam_periph_release(periph); 583119708Sken} 584119708Sken 585111206Sken/* 586111206Sken * We have a handler function for this so we can check the values when the 587111206Sken * user sets them, instead of every time we look at them. 588111206Sken */ 589111206Skenstatic int 590111206Skencdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 591111206Sken{ 592111206Sken int error, value; 593111206Sken 594111206Sken value = *(int *)arg1; 595111206Sken 596111206Sken error = sysctl_handle_int(oidp, &value, 0, req); 597111206Sken 598111206Sken if ((error != 0) 599111206Sken || (req->newptr == NULL)) 600111206Sken return (error); 601111206Sken 602111206Sken /* 603111206Sken * The only real values we can have here are 6 or 10. I don't 604111206Sken * really forsee having 12 be an option at any time in the future. 605111206Sken * So if the user sets something less than or equal to 6, we'll set 606111206Sken * it to 6. If he sets something greater than 6, we'll set it to 10. 607111206Sken * 608111206Sken * I suppose we could just return an error here for the wrong values, 609111206Sken * but I don't think it's necessary to do so, as long as we can 610111206Sken * determine the user's intent without too much trouble. 611111206Sken */ 612111206Sken if (value < 6) 613111206Sken value = 6; 614111206Sken else if (value > 6) 615111206Sken value = 10; 616111206Sken 617111206Sken *(int *)arg1 = value; 618111206Sken 619111206Sken return (0); 620111206Sken} 621111206Sken 62239213Sgibbsstatic cam_status 62339213Sgibbscdregister(struct cam_periph *periph, void *arg) 62439213Sgibbs{ 62539213Sgibbs struct cd_softc *softc; 626118105Snjl struct ccb_pathinq cpi; 62739213Sgibbs struct ccb_getdev *cgd; 628119708Sken char tmpstr[80]; 62939213Sgibbs caddr_t match; 63039213Sgibbs 63139213Sgibbs cgd = (struct ccb_getdev *)arg; 63239213Sgibbs if (periph == NULL) { 63339213Sgibbs printf("cdregister: periph was NULL!!\n"); 63439213Sgibbs return(CAM_REQ_CMP_ERR); 63539213Sgibbs } 63639213Sgibbs if (cgd == NULL) { 63739213Sgibbs printf("cdregister: no getdev CCB, can't register device\n"); 63839213Sgibbs return(CAM_REQ_CMP_ERR); 63939213Sgibbs } 64039213Sgibbs 641203931Smav softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF, 642203931Smav M_NOWAIT | M_ZERO); 64339213Sgibbs if (softc == NULL) { 64439213Sgibbs printf("cdregister: Unable to probe new device. " 64539213Sgibbs "Unable to allocate softc\n"); 64639213Sgibbs return(CAM_REQ_CMP_ERR); 64739213Sgibbs } 64839213Sgibbs 64939213Sgibbs LIST_INIT(&softc->pending_ccbs); 650111206Sken STAILQ_INIT(&softc->mode_queue); 65139213Sgibbs softc->state = CD_STATE_PROBE; 65259249Sphk bioq_init(&softc->bio_queue); 65339213Sgibbs if (SID_IS_REMOVABLE(&cgd->inq_data)) 65439213Sgibbs softc->flags |= CD_FLAG_DISC_REMOVABLE; 65539213Sgibbs 65639213Sgibbs periph->softc = softc; 65739213Sgibbs softc->periph = periph; 65839213Sgibbs 65939213Sgibbs /* 66039213Sgibbs * See if this device has any quirks. 66139213Sgibbs */ 66239213Sgibbs match = cam_quirkmatch((caddr_t)&cgd->inq_data, 66339213Sgibbs (caddr_t)cd_quirk_table, 66439213Sgibbs sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 66539213Sgibbs sizeof(*cd_quirk_table), scsi_inquiry_match); 66639213Sgibbs 66739213Sgibbs if (match != NULL) 66839213Sgibbs softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 66939213Sgibbs else 67039213Sgibbs softc->quirks = CD_Q_NONE; 67139213Sgibbs 672118105Snjl /* Check if the SIM does not want 6 byte commands */ 673200295Smav bzero(&cpi, sizeof(cpi)); 674198382Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 675118105Snjl cpi.ccb_h.func_code = XPT_PATH_INQ; 676118105Snjl xpt_action((union ccb *)&cpi); 677118105Snjl if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 678118105Snjl softc->quirks |= CD_Q_10_BYTE_ONLY; 679118105Snjl 680119708Sken TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph); 681111206Sken 682111206Sken /* The default is 6 byte commands, unless quirked otherwise */ 683111206Sken if (softc->quirks & CD_Q_10_BYTE_ONLY) 684111206Sken softc->minimum_command_size = 10; 685111206Sken else 686111206Sken softc->minimum_command_size = 6; 687111206Sken 688223557Sgibbs /* 689223557Sgibbs * Refcount and block open attempts until we are setup 690223557Sgibbs * Can't block 691223557Sgibbs */ 692220686Sjh (void)cam_periph_hold(periph, PRIBIO); 693220686Sjh cam_periph_unlock(periph); 69439213Sgibbs /* 695111206Sken * Load the user's default, if any. 696111206Sken */ 697111206Sken snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 698111206Sken periph->unit_number); 699111206Sken TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 700111206Sken 701111206Sken /* 6 and 10 are the only permissible values here. */ 702111206Sken if (softc->minimum_command_size < 6) 703111206Sken softc->minimum_command_size = 6; 704111206Sken else if (softc->minimum_command_size > 6) 705111206Sken softc->minimum_command_size = 10; 706111206Sken 707111206Sken /* 70839213Sgibbs * We need to register the statistics structure for this device, 70939213Sgibbs * but we don't have the blocksize yet for it. So, we register 71039213Sgibbs * the structure and indicate that we don't have the blocksize 71139213Sgibbs * yet. Unlike other SCSI peripheral drivers, we explicitly set 71239213Sgibbs * the device type here to be CDROM, rather than just ORing in 71356148Smjacob * the device type. This is because this driver can attach to either 71439213Sgibbs * CDROM or WORM devices, and we want this peripheral driver to 71539213Sgibbs * show up in the devstat list as a CD peripheral driver, not a 71639213Sgibbs * WORM peripheral driver. WORM drives will also have the WORM 71739213Sgibbs * driver attached to them. 71839213Sgibbs */ 719125975Sphk softc->disk = disk_alloc(); 720220644Smav softc->disk->d_devstat = devstat_new_entry("cd", 72139213Sgibbs periph->unit_number, 0, 722220644Smav DEVSTAT_BS_UNAVAILABLE, 723220644Smav DEVSTAT_TYPE_CDROM | 724220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 72543819Sken DEVSTAT_PRIORITY_CD); 726125975Sphk softc->disk->d_open = cdopen; 727125975Sphk softc->disk->d_close = cdclose; 728125975Sphk softc->disk->d_strategy = cdstrategy; 729125975Sphk softc->disk->d_ioctl = cdioctl; 730125975Sphk softc->disk->d_name = "cd"; 731219056Snwhitehorn cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor, 732219056Snwhitehorn sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr)); 733219056Snwhitehorn strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr)); 734219056Snwhitehorn cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)], 735219056Snwhitehorn cgd->inq_data.product, sizeof(cgd->inq_data.product), 736219056Snwhitehorn sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr)); 737125975Sphk softc->disk->d_unit = periph->unit_number; 738125975Sphk softc->disk->d_drv1 = periph; 739200171Smav if (cpi.maxio == 0) 740200171Smav softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 741200171Smav else if (cpi.maxio > MAXPHYS) 742200171Smav softc->disk->d_maxsize = MAXPHYS; /* for safety */ 743200171Smav else 744200171Smav softc->disk->d_maxsize = cpi.maxio; 745168752Sscottl softc->disk->d_flags = 0; 746210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 747210471Smav softc->disk->d_hba_device = cpi.hba_device; 748210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 749210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 750125975Sphk disk_create(softc->disk, DISK_VERSION); 751168752Sscottl cam_periph_lock(periph); 75239213Sgibbs 75339213Sgibbs /* 75439213Sgibbs * Add an async callback so that we get 75539213Sgibbs * notified if this device goes away. 75639213Sgibbs */ 757169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 758169605Sscottl cdasync, periph, periph->path); 75939213Sgibbs 76039213Sgibbs /* 76139213Sgibbs * If the target lun is greater than 0, we most likely have a CD 76239213Sgibbs * changer device. Check the quirk entries as well, though, just 76339213Sgibbs * in case someone has a CD tower with one lun per drive or 76439213Sgibbs * something like that. Also, if we know up front that a 76539213Sgibbs * particular device is a changer, we can mark it as such starting 76639213Sgibbs * with lun 0, instead of lun 1. It shouldn't be necessary to have 76739213Sgibbs * a quirk entry to define something as a changer, however. 76839213Sgibbs */ 76939213Sgibbs if (((cgd->ccb_h.target_lun > 0) 77039213Sgibbs && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) 77139213Sgibbs || ((softc->quirks & CD_Q_CHANGER) != 0)) { 77239213Sgibbs struct cdchanger *nchanger; 77339213Sgibbs struct cam_periph *nperiph; 77439213Sgibbs struct cam_path *path; 77539213Sgibbs cam_status status; 77639213Sgibbs int found; 77739213Sgibbs 77839213Sgibbs /* Set the changer flag in the current device's softc */ 77939213Sgibbs softc->flags |= CD_FLAG_CHANGER; 78039213Sgibbs 78139213Sgibbs /* 78239213Sgibbs * Now, look around for an existing changer device with the 78339213Sgibbs * same path and target ID as the current device. 78439213Sgibbs */ 785168752Sscottl mtx_lock(&changerq_mtx); 78639213Sgibbs for (found = 0, 78739213Sgibbs nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); 78839213Sgibbs nchanger != NULL; 78939213Sgibbs nchanger = STAILQ_NEXT(nchanger, changer_links)){ 79039213Sgibbs if ((nchanger->path_id == cgd->ccb_h.path_id) 79139213Sgibbs && (nchanger->target_id == cgd->ccb_h.target_id)) { 79239213Sgibbs found = 1; 79339213Sgibbs break; 79439213Sgibbs } 79539213Sgibbs } 796168752Sscottl mtx_unlock(&changerq_mtx); 79739213Sgibbs 79839213Sgibbs /* 79939213Sgibbs * If we found a matching entry, just add this device to 80039213Sgibbs * the list of devices on this changer. 80139213Sgibbs */ 80239213Sgibbs if (found == 1) { 80339213Sgibbs struct chdevlist *chlunhead; 80439213Sgibbs 80539213Sgibbs chlunhead = &nchanger->chluns; 80639213Sgibbs 80739213Sgibbs /* 80839213Sgibbs * XXX KDM look at consolidating this code with the 80939213Sgibbs * code below in a separate function. 81039213Sgibbs */ 81139213Sgibbs 81239213Sgibbs /* 81339213Sgibbs * Create a path with lun id 0, and see if we can 81439213Sgibbs * find a matching device 81539213Sgibbs */ 81639213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 81739213Sgibbs cgd->ccb_h.path_id, 81839213Sgibbs cgd->ccb_h.target_id, 0); 81939213Sgibbs 82039213Sgibbs if ((status == CAM_REQ_CMP) 82139213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ 82239213Sgibbs struct cd_softc *nsoftc; 82339213Sgibbs 82439213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 82539213Sgibbs 82639213Sgibbs if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ 82739213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 82839213Sgibbs nchanger->num_devices++; 82939213Sgibbs if (camq_resize(&nchanger->devq, 83039213Sgibbs nchanger->num_devices)!=CAM_REQ_CMP){ 83139213Sgibbs printf("cdregister: " 83239213Sgibbs "camq_resize " 83339213Sgibbs "failed, changer " 83439213Sgibbs "support may " 83539213Sgibbs "be messed up\n"); 83639213Sgibbs } 83739213Sgibbs nsoftc->changer = nchanger; 83839213Sgibbs nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; 83939213Sgibbs 84039213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 84139213Sgibbs nsoftc,changer_links); 84239213Sgibbs } 84367928Sken xpt_free_path(path); 84439213Sgibbs } else if (status == CAM_REQ_CMP) 84539213Sgibbs xpt_free_path(path); 84639213Sgibbs else { 84739213Sgibbs printf("cdregister: unable to allocate path\n" 84839213Sgibbs "cdregister: changer support may be " 84939213Sgibbs "broken\n"); 85039213Sgibbs } 85139213Sgibbs 85239213Sgibbs nchanger->num_devices++; 85339213Sgibbs 85439213Sgibbs softc->changer = nchanger; 85539213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 85639213Sgibbs 85739213Sgibbs if (camq_resize(&nchanger->devq, 85839213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 85939213Sgibbs printf("cdregister: camq_resize " 86039213Sgibbs "failed, changer support may " 86139213Sgibbs "be messed up\n"); 86239213Sgibbs } 86339213Sgibbs 86439213Sgibbs STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); 86539213Sgibbs } 86639213Sgibbs /* 86739213Sgibbs * In this case, we don't already have an entry for this 86839213Sgibbs * particular changer, so we need to create one, add it to 86939213Sgibbs * the queue, and queue this device on the list for this 87039213Sgibbs * changer. Before we queue this device, however, we need 87139213Sgibbs * to search for lun id 0 on this target, and add it to the 87239213Sgibbs * queue first, if it exists. (and if it hasn't already 87339213Sgibbs * been marked as part of the changer.) 87439213Sgibbs */ 87539213Sgibbs else { 87639213Sgibbs nchanger = malloc(sizeof(struct cdchanger), 877203931Smav M_DEVBUF, M_NOWAIT | M_ZERO); 87839213Sgibbs if (nchanger == NULL) { 87939213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 88039213Sgibbs printf("cdregister: unable to malloc " 88139213Sgibbs "changer structure\ncdregister: " 88239213Sgibbs "changer support disabled\n"); 88339213Sgibbs 88439213Sgibbs /* 88539213Sgibbs * Yes, gotos can be gross but in this case 88639213Sgibbs * I think it's justified.. 88739213Sgibbs */ 88839213Sgibbs goto cdregisterexit; 88939213Sgibbs } 89039213Sgibbs if (camq_init(&nchanger->devq, 1) != 0) { 89139213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 89239213Sgibbs printf("cdregister: changer support " 89339213Sgibbs "disabled\n"); 89439213Sgibbs goto cdregisterexit; 89539213Sgibbs } 89639213Sgibbs 89739213Sgibbs nchanger->path_id = cgd->ccb_h.path_id; 89839213Sgibbs nchanger->target_id = cgd->ccb_h.target_id; 89939213Sgibbs 90039213Sgibbs /* this is superfluous, but it makes things clearer */ 90139213Sgibbs nchanger->num_devices = 0; 90239213Sgibbs 90339213Sgibbs STAILQ_INIT(&nchanger->chluns); 90439213Sgibbs 905168752Sscottl callout_init_mtx(&nchanger->long_handle, 906168752Sscottl periph->sim->mtx, 0); 907168752Sscottl callout_init_mtx(&nchanger->short_handle, 908168752Sscottl periph->sim->mtx, 0); 909168752Sscottl 910168752Sscottl mtx_lock(&changerq_mtx); 911168752Sscottl num_changers++; 91239213Sgibbs STAILQ_INSERT_TAIL(&changerq, nchanger, 91339213Sgibbs changer_links); 914168752Sscottl mtx_unlock(&changerq_mtx); 91539213Sgibbs 91639213Sgibbs /* 91739213Sgibbs * Create a path with lun id 0, and see if we can 91839213Sgibbs * find a matching device 91939213Sgibbs */ 92039213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 92139213Sgibbs cgd->ccb_h.path_id, 92239213Sgibbs cgd->ccb_h.target_id, 0); 92339213Sgibbs 92439213Sgibbs /* 92539213Sgibbs * If we were able to allocate the path, and if we 92639213Sgibbs * find a matching device and it isn't already 92739213Sgibbs * marked as part of a changer, then we add it to 92839213Sgibbs * the current changer. 92939213Sgibbs */ 93039213Sgibbs if ((status == CAM_REQ_CMP) 93139213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL) 93239213Sgibbs && ((((struct cd_softc *)periph->softc)->flags & 93339213Sgibbs CD_FLAG_CHANGER) == 0)) { 93439213Sgibbs struct cd_softc *nsoftc; 93539213Sgibbs 93639213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 93739213Sgibbs 93839213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 93939213Sgibbs nchanger->num_devices++; 94039213Sgibbs if (camq_resize(&nchanger->devq, 94139213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 94239213Sgibbs printf("cdregister: camq_resize " 94339213Sgibbs "failed, changer support may " 94439213Sgibbs "be messed up\n"); 94539213Sgibbs } 94639213Sgibbs nsoftc->changer = nchanger; 94739213Sgibbs nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 94839213Sgibbs 94939213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 95039213Sgibbs nsoftc, changer_links); 95167928Sken xpt_free_path(path); 95239213Sgibbs } else if (status == CAM_REQ_CMP) 95339213Sgibbs xpt_free_path(path); 95439213Sgibbs else { 95539213Sgibbs printf("cdregister: unable to allocate path\n" 95639213Sgibbs "cdregister: changer support may be " 95739213Sgibbs "broken\n"); 95839213Sgibbs } 95939213Sgibbs 96039213Sgibbs softc->changer = nchanger; 96139213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 96239213Sgibbs nchanger->num_devices++; 96339213Sgibbs if (camq_resize(&nchanger->devq, 96439213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 96539213Sgibbs printf("cdregister: camq_resize " 96639213Sgibbs "failed, changer support may " 96739213Sgibbs "be messed up\n"); 96839213Sgibbs } 96939213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 97039213Sgibbs changer_links); 97139213Sgibbs } 97239213Sgibbs } 97339213Sgibbs 97439213Sgibbscdregisterexit: 97539213Sgibbs 97639213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 977203108Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 97839213Sgibbs else 979203108Smav cdschedule(periph, CAM_PRIORITY_DEV); 98039213Sgibbs 98139213Sgibbs return(CAM_REQ_CMP); 98239213Sgibbs} 98339213Sgibbs 98439213Sgibbsstatic int 985120599Sphkcdopen(struct disk *dp) 98639213Sgibbs{ 98739213Sgibbs struct cam_periph *periph; 98839213Sgibbs struct cd_softc *softc; 989101940Snjl int error; 99039213Sgibbs 991120599Sphk periph = (struct cam_periph *)dp->d_drv1; 99239213Sgibbs if (periph == NULL) 99339213Sgibbs return (ENXIO); 99439213Sgibbs 99539213Sgibbs softc = (struct cd_softc *)periph->softc; 99639213Sgibbs 997168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 998168752Sscottl return(ENXIO); 999168752Sscottl 1000168752Sscottl cam_periph_lock(periph); 1001168752Sscottl 100240603Sken if (softc->flags & CD_FLAG_INVALID) { 1003168752Sscottl cam_periph_unlock(periph); 1004168752Sscottl cam_periph_release(periph); 100539213Sgibbs return(ENXIO); 100640603Sken } 100739213Sgibbs 1008168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1009168752Sscottl cam_periph_unlock(periph); 1010168752Sscottl cam_periph_release(periph); 101139213Sgibbs return (error); 101241297Sken } 101339213Sgibbs 1014236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 1015236602Smav ("cdopen\n")); 1016236602Smav 101740020Sken /* 1018111206Sken * Check for media, and set the appropriate flags. We don't bail 1019111206Sken * if we don't have media, but then we don't allow anything but the 1020111206Sken * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 102151836Sphk */ 1022111206Sken cdcheckmedia(periph); 102340020Sken 1024168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 1025168752Sscottl cam_periph_unhold(periph); 102639213Sgibbs 1027186371Sscottl /* Closes aren't symmetrical with opens, so fix up the refcounting. */ 1028186371Sscottl if ((softc->flags & CD_FLAG_OPEN) == 0) { 1029186371Sscottl softc->flags |= CD_FLAG_OPEN; 1030186371Sscottl cam_periph_unlock(periph); 1031186371Sscottl } else { 1032186371Sscottl cam_periph_unlock(periph); 1033186371Sscottl cam_periph_release(periph); 1034186371Sscottl } 1035186371Sscottl 1036168752Sscottl return (0); 103739213Sgibbs} 103839213Sgibbs 103939213Sgibbsstatic int 1040120599Sphkcdclose(struct disk *dp) 104139213Sgibbs{ 104239213Sgibbs struct cam_periph *periph; 104339213Sgibbs struct cd_softc *softc; 104439213Sgibbs 1045120599Sphk periph = (struct cam_periph *)dp->d_drv1; 104639213Sgibbs if (periph == NULL) 104739213Sgibbs return (ENXIO); 104839213Sgibbs 104939213Sgibbs softc = (struct cd_softc *)periph->softc; 105039213Sgibbs 1051168752Sscottl cam_periph_lock(periph); 1052237336Smav if (cam_periph_hold(periph, PRIBIO) != 0) { 1053237335Smav cam_periph_unlock(periph); 1054237335Smav cam_periph_release(periph); 1055237335Smav return (0); 1056237335Smav } 105739213Sgibbs 1058236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 1059236602Smav ("cdclose\n")); 1060236602Smav 106139213Sgibbs if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 106239213Sgibbs cdprevent(periph, PR_ALLOW); 106339213Sgibbs 106439213Sgibbs /* 106539213Sgibbs * Since we're closing this CD, mark the blocksize as unavailable. 1066111206Sken * It will be marked as available when the CD is opened again. 106739213Sgibbs */ 1068125975Sphk softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 106939213Sgibbs 1070111206Sken /* 1071111206Sken * We'll check the media and toc again at the next open(). 1072111206Sken */ 1073168752Sscottl softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 1074111206Sken 1075168752Sscottl cam_periph_unhold(periph); 107639213Sgibbs cam_periph_unlock(periph); 107739213Sgibbs cam_periph_release(periph); 107839213Sgibbs 107939213Sgibbs return (0); 108039213Sgibbs} 108139213Sgibbs 108239213Sgibbsstatic void 108339213Sgibbscdshorttimeout(void *arg) 108439213Sgibbs{ 108539213Sgibbs struct cdchanger *changer; 108639213Sgibbs 108739213Sgibbs changer = (struct cdchanger *)arg; 108839213Sgibbs 108939213Sgibbs /* Always clear the short timeout flag, since that's what we're in */ 109039213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 109139213Sgibbs 109239213Sgibbs /* 109339213Sgibbs * Check to see if there is any more pending or outstanding I/O for 109439213Sgibbs * this device. If not, move it out of the active slot. 109539213Sgibbs */ 109659249Sphk if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1097112262Sphk && (changer->cur_device->outstanding_cmds == 0)) { 109839213Sgibbs changer->flags |= CHANGER_MANUAL_CALL; 109939213Sgibbs cdrunchangerqueue(changer); 110039213Sgibbs } 110139213Sgibbs} 110239213Sgibbs 110339213Sgibbs/* 110439213Sgibbs * This is a wrapper for xpt_schedule. It only applies to changers. 110539213Sgibbs */ 110639213Sgibbsstatic void 110739213Sgibbscdschedule(struct cam_periph *periph, int priority) 110839213Sgibbs{ 110939213Sgibbs struct cd_softc *softc; 111039213Sgibbs 111139213Sgibbs softc = (struct cd_softc *)periph->softc; 111239213Sgibbs 111339213Sgibbs /* 111439213Sgibbs * If this device isn't currently queued, and if it isn't 111539213Sgibbs * the active device, then we queue this device and run the 111639213Sgibbs * changer queue if there is no timeout scheduled to do it. 111739213Sgibbs * If this device is the active device, just schedule it 111839213Sgibbs * to run again. If this device is queued, there should be 111939213Sgibbs * a timeout in place already that will make sure it runs. 112039213Sgibbs */ 112139213Sgibbs if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 112239213Sgibbs && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 112339213Sgibbs /* 112439213Sgibbs * We don't do anything with the priority here. 112539213Sgibbs * This is strictly a fifo queue. 112639213Sgibbs */ 1127198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 112845442Sgibbs softc->pinfo.generation = ++softc->changer->devq.generation; 112939213Sgibbs camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 113039213Sgibbs 113139213Sgibbs /* 113239213Sgibbs * Since we just put a device in the changer queue, 113339213Sgibbs * check and see if there is a timeout scheduled for 113439213Sgibbs * this changer. If so, let the timeout handle 113539213Sgibbs * switching this device into the active slot. If 113639213Sgibbs * not, manually call the timeout routine to 113739213Sgibbs * bootstrap things. 113839213Sgibbs */ 113939213Sgibbs if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 114046581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 114146581Sken && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 114239213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 114339213Sgibbs cdrunchangerqueue(softc->changer); 114439213Sgibbs } 114539213Sgibbs } else if ((softc->flags & CD_FLAG_ACTIVE) 114639213Sgibbs && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 114739213Sgibbs xpt_schedule(periph, priority); 114839213Sgibbs} 114939213Sgibbs 115039213Sgibbsstatic void 115139213Sgibbscdrunchangerqueue(void *arg) 115239213Sgibbs{ 115339213Sgibbs struct cd_softc *softc; 115439213Sgibbs struct cdchanger *changer; 115539213Sgibbs int called_from_timeout; 115639213Sgibbs 115739213Sgibbs changer = (struct cdchanger *)arg; 115839213Sgibbs 115939213Sgibbs /* 116039213Sgibbs * If we have NOT been called from cdstrategy() or cddone(), and 116139213Sgibbs * instead from a timeout routine, go ahead and clear the 116239213Sgibbs * timeout flag. 116339213Sgibbs */ 116439213Sgibbs if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 116539213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 116639213Sgibbs called_from_timeout = 1; 116739213Sgibbs } else 116839213Sgibbs called_from_timeout = 0; 116939213Sgibbs 117039213Sgibbs /* Always clear the manual call flag */ 117139213Sgibbs changer->flags &= ~CHANGER_MANUAL_CALL; 117239213Sgibbs 117339213Sgibbs /* nothing to do if the queue is empty */ 117439213Sgibbs if (changer->devq.entries <= 0) { 117539213Sgibbs return; 117639213Sgibbs } 117739213Sgibbs 117839213Sgibbs /* 117939213Sgibbs * If the changer queue is frozen, that means we have an active 118039213Sgibbs * device. 118139213Sgibbs */ 1182203108Smav if (changer->devq.qfrozen_cnt[0] > 0) { 118339213Sgibbs 1184168752Sscottl /* 1185168752Sscottl * We always need to reset the frozen count and clear the 1186168752Sscottl * active flag. 1187168752Sscottl */ 1188203108Smav changer->devq.qfrozen_cnt[0]--; 1189168752Sscottl changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1190168752Sscottl changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1191168752Sscottl 1192112262Sphk if (changer->cur_device->outstanding_cmds > 0) { 119339213Sgibbs changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 119439213Sgibbs changer->cur_device->bufs_left = 1195112262Sphk changer->cur_device->outstanding_cmds; 119639213Sgibbs if (called_from_timeout) { 1197168752Sscottl callout_reset(&changer->long_handle, 1198168752Sscottl changer_max_busy_seconds * hz, 1199168752Sscottl cdrunchangerqueue, changer); 120039213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 120139213Sgibbs } 120239213Sgibbs return; 120339213Sgibbs } 120439213Sgibbs 120539213Sgibbs /* 120639213Sgibbs * Check to see whether the current device has any I/O left 120739213Sgibbs * to do. If so, requeue it at the end of the queue. If 120839213Sgibbs * not, there is no need to requeue it. 120939213Sgibbs */ 121059249Sphk if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 121139213Sgibbs 121239213Sgibbs changer->cur_device->pinfo.generation = 121345442Sgibbs ++changer->devq.generation; 121439213Sgibbs camq_insert(&changer->devq, 121539213Sgibbs (cam_pinfo *)changer->cur_device); 121639213Sgibbs } 121739213Sgibbs } 121839213Sgibbs 121945845Sgibbs softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 122039213Sgibbs 122139213Sgibbs changer->cur_device = softc; 122239213Sgibbs 1223203108Smav changer->devq.qfrozen_cnt[0]++; 122439213Sgibbs softc->flags |= CD_FLAG_ACTIVE; 122539213Sgibbs 122639213Sgibbs /* Just in case this device is waiting */ 122739213Sgibbs wakeup(&softc->changer); 1228198382Smav xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL); 122939213Sgibbs 123039213Sgibbs /* 123139213Sgibbs * Get rid of any pending timeouts, and set a flag to schedule new 123239213Sgibbs * ones so this device gets its full time quantum. 123339213Sgibbs */ 123439213Sgibbs if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1235168752Sscottl callout_stop(&changer->long_handle); 123639213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 123739213Sgibbs } 123839213Sgibbs 123939213Sgibbs if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1240168752Sscottl callout_stop(&changer->short_handle); 124139213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 124239213Sgibbs } 124339213Sgibbs 124439213Sgibbs /* 124539213Sgibbs * We need to schedule timeouts, but we only do this after the 124639213Sgibbs * first transaction has completed. This eliminates the changer 124739213Sgibbs * switch time. 124839213Sgibbs */ 124939213Sgibbs changer->flags |= CHANGER_NEED_TIMEOUT; 125039213Sgibbs} 125139213Sgibbs 125239213Sgibbsstatic void 125339213Sgibbscdchangerschedule(struct cd_softc *softc) 125439213Sgibbs{ 125539213Sgibbs struct cdchanger *changer; 125639213Sgibbs 125739213Sgibbs changer = softc->changer; 125839213Sgibbs 125939213Sgibbs /* 126039213Sgibbs * If this is a changer, and this is the current device, 126139213Sgibbs * and this device has at least the minimum time quantum to 126239213Sgibbs * run, see if we can switch it out. 126339213Sgibbs */ 126439213Sgibbs if ((softc->flags & CD_FLAG_ACTIVE) 126539213Sgibbs && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 126639213Sgibbs && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 126739213Sgibbs /* 126839213Sgibbs * We try three things here. The first is that we 126939213Sgibbs * check to see whether the schedule on completion 127039213Sgibbs * flag is set. If it is, we decrement the number 127139213Sgibbs * of buffers left, and if it's zero, we reschedule. 127239213Sgibbs * Next, we check to see whether the pending buffer 127339213Sgibbs * queue is empty and whether there are no 127439213Sgibbs * outstanding transactions. If so, we reschedule. 127539213Sgibbs * Next, we see if the pending buffer queue is empty. 127639213Sgibbs * If it is, we set the number of buffers left to 127739213Sgibbs * the current active buffer count and set the 127839213Sgibbs * schedule on complete flag. 127939213Sgibbs */ 128039213Sgibbs if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 128139213Sgibbs if (--softc->bufs_left == 0) { 128239213Sgibbs softc->changer->flags |= 128339213Sgibbs CHANGER_MANUAL_CALL; 128439213Sgibbs softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 128539213Sgibbs cdrunchangerqueue(softc->changer); 128639213Sgibbs } 128759249Sphk } else if ((bioq_first(&softc->bio_queue) == NULL) 1288112262Sphk && (softc->outstanding_cmds == 0)) { 128939213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 129039213Sgibbs cdrunchangerqueue(softc->changer); 129139213Sgibbs } 129239213Sgibbs } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 129339213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 129439213Sgibbs 129539213Sgibbs /* 129639213Sgibbs * Now that the first transaction to this 129739213Sgibbs * particular device has completed, we can go ahead 129839213Sgibbs * and schedule our timeouts. 129939213Sgibbs */ 130039213Sgibbs if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1301168752Sscottl callout_reset(&changer->long_handle, 1302168752Sscottl changer_max_busy_seconds * hz, 1303168752Sscottl cdrunchangerqueue, changer); 130439213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 130539213Sgibbs } else 130639213Sgibbs printf("cdchangerschedule: already have a long" 130739213Sgibbs " timeout!\n"); 130839213Sgibbs 130939213Sgibbs if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1310168752Sscottl callout_reset(&changer->short_handle, 1311168752Sscottl changer_min_busy_seconds * hz, 1312168752Sscottl cdshorttimeout, changer); 131339213Sgibbs changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 131439213Sgibbs } else 131539213Sgibbs printf("cdchangerschedule: already have a short " 131639213Sgibbs "timeout!\n"); 131739213Sgibbs 131839213Sgibbs /* 131939213Sgibbs * We just scheduled timeouts, no need to schedule 132039213Sgibbs * more. 132139213Sgibbs */ 132239213Sgibbs changer->flags &= ~CHANGER_NEED_TIMEOUT; 132339213Sgibbs 132439213Sgibbs } 132539213Sgibbs} 132639213Sgibbs 132739213Sgibbsstatic int 132839213Sgibbscdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 132939213Sgibbs u_int32_t cam_flags, 133039213Sgibbs u_int32_t sense_flags), 133139213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags) 133239213Sgibbs{ 133339213Sgibbs struct cd_softc *softc; 133439213Sgibbs struct cam_periph *periph; 133539213Sgibbs int error; 133639213Sgibbs 133739213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 133839213Sgibbs softc = (struct cd_softc *)periph->softc; 133939213Sgibbs 134039213Sgibbs error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1341125975Sphk softc->disk->d_devstat); 134239213Sgibbs 134339213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 134439213Sgibbs cdchangerschedule(softc); 134539213Sgibbs 134639213Sgibbs return(error); 134739213Sgibbs} 134839213Sgibbs 134942017Seivindstatic union ccb * 135039213Sgibbscdgetccb(struct cam_periph *periph, u_int32_t priority) 135139213Sgibbs{ 135239213Sgibbs struct cd_softc *softc; 135339213Sgibbs 135439213Sgibbs softc = (struct cd_softc *)periph->softc; 135539213Sgibbs 135639213Sgibbs if (softc->flags & CD_FLAG_CHANGER) { 135739213Sgibbs /* 135839213Sgibbs * This should work the first time this device is woken up, 135939213Sgibbs * but just in case it doesn't, we use a while loop. 136039213Sgibbs */ 136146581Sken while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 136239213Sgibbs /* 136339213Sgibbs * If this changer isn't already queued, queue it up. 136439213Sgibbs */ 136539213Sgibbs if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 1366198382Smav softc->pinfo.priority = CAM_PRIORITY_NORMAL; 136739213Sgibbs softc->pinfo.generation = 136845442Sgibbs ++softc->changer->devq.generation; 136939213Sgibbs camq_insert(&softc->changer->devq, 137039213Sgibbs (cam_pinfo *)softc); 137139213Sgibbs } 137246581Sken if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 137346581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 137446581Sken && ((softc->changer->flags 137546581Sken & CHANGER_SHORT_TMOUT_SCHED)==0)) { 137639213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 137739213Sgibbs cdrunchangerqueue(softc->changer); 137839213Sgibbs } else 1379168752Sscottl msleep(&softc->changer, periph->sim->mtx, 1380168752Sscottl PRIBIO, "cgticb", 0); 138139213Sgibbs } 138239213Sgibbs } 138339213Sgibbs return(cam_periph_getccb(periph, priority)); 138439213Sgibbs} 138539213Sgibbs 138639213Sgibbs 138739213Sgibbs/* 138839213Sgibbs * Actually translate the requested transfer into one the physical driver 138939213Sgibbs * can understand. The transfer is described by a buf and will include 139039213Sgibbs * only one physical transfer. 139139213Sgibbs */ 139239213Sgibbsstatic void 139359249Sphkcdstrategy(struct bio *bp) 139439213Sgibbs{ 139539213Sgibbs struct cam_periph *periph; 139639213Sgibbs struct cd_softc *softc; 139739213Sgibbs 1398120599Sphk periph = (struct cam_periph *)bp->bio_disk->d_drv1; 139939213Sgibbs if (periph == NULL) { 140076362Sphk biofinish(bp, NULL, ENXIO); 140176362Sphk return; 140239213Sgibbs } 140339213Sgibbs 1404168752Sscottl cam_periph_lock(periph); 1405236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1406236602Smav ("cdstrategy(%p)\n", bp)); 140739213Sgibbs 140839213Sgibbs softc = (struct cd_softc *)periph->softc; 140939213Sgibbs 141039213Sgibbs /* 141139213Sgibbs * If the device has been made invalid, error out 141239213Sgibbs */ 141339213Sgibbs if ((softc->flags & CD_FLAG_INVALID)) { 1414168752Sscottl cam_periph_unlock(periph); 141576362Sphk biofinish(bp, NULL, ENXIO); 141676362Sphk return; 141739213Sgibbs } 141839213Sgibbs 1419111206Sken /* 1420111206Sken * If we don't have valid media, look for it before trying to 1421111206Sken * schedule the I/O. 1422111206Sken */ 1423111206Sken if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1424111206Sken int error; 1425111206Sken 1426111206Sken error = cdcheckmedia(periph); 1427111206Sken if (error != 0) { 1428168752Sscottl cam_periph_unlock(periph); 1429111206Sken biofinish(bp, NULL, error); 1430111206Sken return; 1431111206Sken } 1432111206Sken } 1433111206Sken 143439213Sgibbs /* 143539213Sgibbs * Place it in the queue of disk activities for this disk 143639213Sgibbs */ 1437112946Sphk bioq_disksort(&softc->bio_queue, bp); 143839213Sgibbs 143939213Sgibbs /* 144039213Sgibbs * Schedule ourselves for performing the work. We do things 144139213Sgibbs * differently for changers. 144239213Sgibbs */ 144339213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 1444198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 144539213Sgibbs else 1446198382Smav cdschedule(periph, CAM_PRIORITY_NORMAL); 144739213Sgibbs 1448168752Sscottl cam_periph_unlock(periph); 144939213Sgibbs return; 145039213Sgibbs} 145139213Sgibbs 145239213Sgibbsstatic void 145339213Sgibbscdstart(struct cam_periph *periph, union ccb *start_ccb) 145439213Sgibbs{ 145539213Sgibbs struct cd_softc *softc; 145659249Sphk struct bio *bp; 145739213Sgibbs struct ccb_scsiio *csio; 145839213Sgibbs struct scsi_read_capacity_data *rcap; 145939213Sgibbs 146039213Sgibbs softc = (struct cd_softc *)periph->softc; 146139213Sgibbs 146239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 146339213Sgibbs 146439213Sgibbs switch (softc->state) { 146539213Sgibbs case CD_STATE_NORMAL: 146639213Sgibbs { 146759249Sphk bp = bioq_first(&softc->bio_queue); 146839213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 146939213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 147039213Sgibbs 147139213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 147239213Sgibbs periph_links.sle); 147339213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 147439213Sgibbs wakeup(&periph->ccb_list); 147539213Sgibbs } else if (bp == NULL) { 147639213Sgibbs xpt_release_ccb(start_ccb); 147739213Sgibbs } else { 147859249Sphk bioq_remove(&softc->bio_queue, bp); 147939213Sgibbs 148039213Sgibbs scsi_read_write(&start_ccb->csio, 1481203108Smav /*retries*/ cd_retry_count, 148239213Sgibbs /* cbfcnp */ cddone, 148391062Sphk MSG_SIMPLE_Q_TAG, 148459249Sphk /* read */bp->bio_cmd == BIO_READ, 148539213Sgibbs /* byte2 */ 0, 148639213Sgibbs /* minimum_cmd_size */ 10, 1487121209Sphk /* lba */ bp->bio_offset / 1488121209Sphk softc->params.blksize, 148959249Sphk bp->bio_bcount / softc->params.blksize, 149059249Sphk /* data_ptr */ bp->bio_data, 149159249Sphk /* dxfer_len */ bp->bio_bcount, 149239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 149339213Sgibbs /* timeout */ 30000); 1494228808Smav /* Use READ CD command for audio tracks. */ 1495228808Smav if (softc->params.blksize == 2352) { 1496228808Smav start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD; 1497228808Smav start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8; 1498228847Smav start_ccb->csio.cdb_io.cdb_bytes[10] = 0; 1499228847Smav start_ccb->csio.cdb_io.cdb_bytes[11] = 0; 1500228847Smav start_ccb->csio.cdb_len = 12; 1501228808Smav } 150239213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 150339213Sgibbs 150439213Sgibbs 150539213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, 150639213Sgibbs &start_ccb->ccb_h, periph_links.le); 1507112262Sphk softc->outstanding_cmds++; 150839213Sgibbs 150939213Sgibbs /* We expect a unit attention from this device */ 151039213Sgibbs if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 151139213Sgibbs start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 151239213Sgibbs softc->flags &= ~CD_FLAG_RETRY_UA; 151339213Sgibbs } 151439213Sgibbs 151539213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 151659249Sphk bp = bioq_first(&softc->bio_queue); 151739213Sgibbs 151839213Sgibbs xpt_action(start_ccb); 151939213Sgibbs } 152039213Sgibbs if (bp != NULL) { 152139213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 1522198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 152339213Sgibbs } 152439213Sgibbs break; 152539213Sgibbs } 152639213Sgibbs case CD_STATE_PROBE: 152739213Sgibbs { 152839213Sgibbs 152939213Sgibbs rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1530203931Smav M_SCSICD, M_NOWAIT | M_ZERO); 153139213Sgibbs if (rcap == NULL) { 1532164906Smjacob xpt_print(periph->path, 1533164906Smjacob "cdstart: Couldn't malloc read_capacity data\n"); 153439213Sgibbs /* cd_free_periph??? */ 153539213Sgibbs break; 153639213Sgibbs } 153739213Sgibbs csio = &start_ccb->csio; 153839213Sgibbs scsi_read_capacity(csio, 1539203108Smav /*retries*/ cd_retry_count, 154039213Sgibbs cddone, 154139213Sgibbs MSG_SIMPLE_Q_TAG, 154239213Sgibbs rcap, 154339213Sgibbs SSD_FULL_SIZE, 154439213Sgibbs /*timeout*/20000); 154539213Sgibbs start_ccb->ccb_h.ccb_bp = NULL; 154639213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 154739213Sgibbs xpt_action(start_ccb); 154839213Sgibbs break; 154939213Sgibbs } 155039213Sgibbs } 155139213Sgibbs} 155239213Sgibbs 155339213Sgibbsstatic void 155439213Sgibbscddone(struct cam_periph *periph, union ccb *done_ccb) 155539213Sgibbs{ 155639213Sgibbs struct cd_softc *softc; 155739213Sgibbs struct ccb_scsiio *csio; 155839213Sgibbs 155939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 156039213Sgibbs 156139213Sgibbs softc = (struct cd_softc *)periph->softc; 156239213Sgibbs csio = &done_ccb->csio; 156339213Sgibbs 156439213Sgibbs switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 156539213Sgibbs case CD_CCB_BUFFER_IO: 156639213Sgibbs { 156759249Sphk struct bio *bp; 156839213Sgibbs int error; 156939213Sgibbs 157059249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 157139213Sgibbs error = 0; 157239213Sgibbs 157339213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 157439213Sgibbs int sf; 157539213Sgibbs 157639213Sgibbs if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 157739213Sgibbs sf = SF_RETRY_UA; 157839213Sgibbs else 157939213Sgibbs sf = 0; 158046747Sken 158174840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 158274840Sken if (error == ERESTART) { 158339213Sgibbs /* 158439213Sgibbs * A retry was scheuled, so 158539213Sgibbs * just return. 158639213Sgibbs */ 158739213Sgibbs return; 158839213Sgibbs } 158939213Sgibbs } 159039213Sgibbs 159139213Sgibbs if (error != 0) { 1592164906Smjacob xpt_print(periph->path, 1593164906Smjacob "cddone: got error %#x back\n", error); 1594112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 159559249Sphk bp->bio_resid = bp->bio_bcount; 159659249Sphk bp->bio_error = error; 159759249Sphk bp->bio_flags |= BIO_ERROR; 1598199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1599199279Smav cam_release_devq(done_ccb->ccb_h.path, 160039213Sgibbs /*relsim_flags*/0, 160139213Sgibbs /*reduction*/0, 160239213Sgibbs /*timeout*/0, 160339213Sgibbs /*getcount_only*/0); 160439213Sgibbs 160539213Sgibbs } else { 160659249Sphk bp->bio_resid = csio->resid; 160759249Sphk bp->bio_error = 0; 160859249Sphk if (bp->bio_resid != 0) { 1609104456Sphk /* 1610104456Sphk * Short transfer ??? 1611104456Sphk * XXX: not sure this is correct for partial 1612104456Sphk * transfers at EOM 1613104456Sphk */ 161459249Sphk bp->bio_flags |= BIO_ERROR; 161539213Sgibbs } 161639213Sgibbs } 161739213Sgibbs 161839213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1619112262Sphk softc->outstanding_cmds--; 162039213Sgibbs 162139213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 162239213Sgibbs cdchangerschedule(softc); 162339213Sgibbs 1624157806Smaxim biofinish(bp, NULL, 0); 162539213Sgibbs break; 162639213Sgibbs } 162739213Sgibbs case CD_CCB_PROBE: 162839213Sgibbs { 162939213Sgibbs struct scsi_read_capacity_data *rdcap; 163039213Sgibbs char announce_buf[120]; /* 163139213Sgibbs * Currently (9/30/97) the 163239213Sgibbs * longest possible announce 163339213Sgibbs * buffer is 108 bytes, for the 163439213Sgibbs * first error case below. 163539213Sgibbs * That is 39 bytes for the 163639213Sgibbs * basic string, 16 bytes for the 163739213Sgibbs * biggest sense key (hardware 163839213Sgibbs * error), 52 bytes for the 163939213Sgibbs * text of the largest sense 164039213Sgibbs * qualifier valid for a CDROM, 164139213Sgibbs * (0x72, 0x03 or 0x04, 164239213Sgibbs * 0x03), and one byte for the 164339213Sgibbs * null terminating character. 164439213Sgibbs * To allow for longer strings, 164539213Sgibbs * the announce buffer is 120 164639213Sgibbs * bytes. 164739213Sgibbs */ 164839213Sgibbs struct cd_params *cdp; 164939213Sgibbs 165039213Sgibbs cdp = &softc->params; 165139213Sgibbs 165239213Sgibbs rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 165339213Sgibbs 165439213Sgibbs cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 165539213Sgibbs cdp->blksize = scsi_4btoul (rdcap->length); 165639213Sgibbs 165739213Sgibbs if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 165839213Sgibbs 165941514Sarchie snprintf(announce_buf, sizeof(announce_buf), 166040020Sken "cd present [%lu x %lu byte records]", 166140020Sken cdp->disksize, (u_long)cdp->blksize); 166239213Sgibbs 166339213Sgibbs } else { 166439213Sgibbs int error; 166539213Sgibbs /* 166639213Sgibbs * Retry any UNIT ATTENTION type errors. They 166739213Sgibbs * are expected at boot. 166839213Sgibbs */ 166974840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, 167074840Sken SF_RETRY_UA | SF_NO_PRINT); 167139213Sgibbs if (error == ERESTART) { 167239213Sgibbs /* 167339213Sgibbs * A retry was scheuled, so 167439213Sgibbs * just return. 167539213Sgibbs */ 167639213Sgibbs return; 167739213Sgibbs } else if (error != 0) { 167839213Sgibbs 167939213Sgibbs int asc, ascq; 168039213Sgibbs int sense_key, error_code; 168139213Sgibbs int have_sense; 168239213Sgibbs cam_status status; 168339213Sgibbs struct ccb_getdev cgd; 168439213Sgibbs 168539213Sgibbs /* Don't wedge this device's queue */ 1686199279Smav if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1687199279Smav cam_release_devq(done_ccb->ccb_h.path, 168839213Sgibbs /*relsim_flags*/0, 168939213Sgibbs /*reduction*/0, 169039213Sgibbs /*timeout*/0, 169139213Sgibbs /*getcount_only*/0); 169239213Sgibbs 169339213Sgibbs status = done_ccb->ccb_h.status; 169439213Sgibbs 169539213Sgibbs xpt_setup_ccb(&cgd.ccb_h, 169639213Sgibbs done_ccb->ccb_h.path, 1697198382Smav CAM_PRIORITY_NORMAL); 169839213Sgibbs cgd.ccb_h.func_code = XPT_GDEV_TYPE; 169939213Sgibbs xpt_action((union ccb *)&cgd); 170039213Sgibbs 1701237478Smav if (scsi_extract_sense_ccb(done_ccb, 1702237478Smav &error_code, &sense_key, &asc, &ascq)) 1703237478Smav have_sense = TRUE; 1704237478Smav else 170539213Sgibbs have_sense = FALSE; 170639213Sgibbs 170739213Sgibbs /* 170846581Sken * Attach to anything that claims to be a 170946581Sken * CDROM or WORM device, as long as it 171046581Sken * doesn't return a "Logical unit not 171146581Sken * supported" (0x25) error. 171239213Sgibbs */ 171346581Sken if ((have_sense) && (asc != 0x25) 171474840Sken && (error_code == SSD_CURRENT_ERROR)) { 171574840Sken const char *sense_key_desc; 171674840Sken const char *asc_desc; 171774840Sken 171874840Sken scsi_sense_desc(sense_key, asc, ascq, 171974840Sken &cgd.inq_data, 172074840Sken &sense_key_desc, 172174840Sken &asc_desc); 172241514Sarchie snprintf(announce_buf, 172341514Sarchie sizeof(announce_buf), 172439213Sgibbs "Attempt to query device " 172539213Sgibbs "size failed: %s, %s", 172674840Sken sense_key_desc, 172774840Sken asc_desc); 172882850Sken } else if ((have_sense == 0) 172982850Sken && ((status & CAM_STATUS_MASK) == 173082850Sken CAM_SCSI_STATUS_ERROR) 173182850Sken && (csio->scsi_status == 173282850Sken SCSI_STATUS_BUSY)) { 173382850Sken snprintf(announce_buf, 173482850Sken sizeof(announce_buf), 173582850Sken "Attempt to query device " 173682850Sken "size failed: SCSI Status: %s", 173782850Sken scsi_status_string(csio)); 173874840Sken } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 173939213Sgibbs /* 174039213Sgibbs * We only print out an error for 174139213Sgibbs * CDROM type devices. For WORM 174239213Sgibbs * devices, we don't print out an 174339213Sgibbs * error since a few WORM devices 174439213Sgibbs * don't support CDROM commands. 174539213Sgibbs * If we have sense information, go 174639213Sgibbs * ahead and print it out. 174739213Sgibbs * Otherwise, just say that we 174839213Sgibbs * couldn't attach. 174939213Sgibbs */ 175039213Sgibbs 175139213Sgibbs /* 175239213Sgibbs * Just print out the error, not 175339213Sgibbs * the full probe message, when we 175439213Sgibbs * don't attach. 175539213Sgibbs */ 175640020Sken if (have_sense) 175740020Sken scsi_sense_print( 175840020Sken &done_ccb->csio); 175940020Sken else { 1760164906Smjacob xpt_print(periph->path, 1761164906Smjacob "got CAM status %#x\n", 1762164906Smjacob done_ccb->ccb_h.status); 176340020Sken } 1764164906Smjacob xpt_print(periph->path, "fatal error, " 1765164906Smjacob "failed to attach to device\n"); 176639213Sgibbs /* 176740603Sken * Invalidate this peripheral. 176839213Sgibbs */ 176939213Sgibbs cam_periph_invalidate(periph); 177040020Sken 177140020Sken announce_buf[0] = '\0'; 177239213Sgibbs } else { 177340603Sken 177439213Sgibbs /* 177540603Sken * Invalidate this peripheral. 177639213Sgibbs */ 177739213Sgibbs cam_periph_invalidate(periph); 177840020Sken announce_buf[0] = '\0'; 177939213Sgibbs } 178039213Sgibbs } 178139213Sgibbs } 1782169562Sscottl free(rdcap, M_SCSICD); 178342378Smjacob if (announce_buf[0] != '\0') { 178439213Sgibbs xpt_announce_periph(periph, announce_buf); 178542378Smjacob if (softc->flags & CD_FLAG_CHANGER) 178642378Smjacob cdchangerschedule(softc); 1787119708Sken /* 1788119708Sken * Create our sysctl variables, now that we know 1789119708Sken * we have successfully attached. 1790119708Sken */ 1791119708Sken taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 179242378Smjacob } 179340020Sken softc->state = CD_STATE_NORMAL; 179442378Smjacob /* 179542378Smjacob * Since our peripheral may be invalidated by an error 179642378Smjacob * above or an external event, we must release our CCB 179742378Smjacob * before releasing the probe lock on the peripheral. 179842378Smjacob * The peripheral will only go away once the last lock 179942378Smjacob * is removed, and we need it around for the CCB release 180042378Smjacob * operation. 180142378Smjacob */ 180242378Smjacob xpt_release_ccb(done_ccb); 1803168752Sscottl cam_periph_unhold(periph); 180442378Smjacob return; 180539213Sgibbs } 180639213Sgibbs case CD_CCB_WAITING: 180739213Sgibbs { 180839213Sgibbs /* Caller will release the CCB */ 180939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 181039213Sgibbs ("trying to wakeup ccbwait\n")); 181139213Sgibbs 181239213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 181339213Sgibbs return; 181439213Sgibbs } 181542378Smjacob default: 181642378Smjacob break; 181739213Sgibbs } 181839213Sgibbs xpt_release_ccb(done_ccb); 181939213Sgibbs} 182039213Sgibbs 1821111206Skenstatic union cd_pages * 1822111206Skencdgetpage(struct cd_mode_params *mode_params) 1823111206Sken{ 1824111206Sken union cd_pages *page; 1825111206Sken 1826111206Sken if (mode_params->cdb_size == 10) 1827111206Sken page = (union cd_pages *)find_mode_page_10( 1828111206Sken (struct scsi_mode_header_10 *)mode_params->mode_buf); 1829111206Sken else 1830111206Sken page = (union cd_pages *)find_mode_page_6( 1831111206Sken (struct scsi_mode_header_6 *)mode_params->mode_buf); 1832111206Sken 1833111206Sken return (page); 1834111206Sken} 1835111206Sken 183639213Sgibbsstatic int 1837111206Skencdgetpagesize(int page_num) 1838111206Sken{ 1839111206Sken int i; 1840111206Sken 1841111206Sken for (i = 0; i < (sizeof(cd_page_size_table)/ 1842111206Sken sizeof(cd_page_size_table[0])); i++) { 1843111206Sken if (cd_page_size_table[i].page == page_num) 1844111206Sken return (cd_page_size_table[i].page_size); 1845111206Sken } 1846111206Sken 1847111206Sken return (-1); 1848111206Sken} 1849111206Sken 1850111206Skenstatic int 1851120599Sphkcdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 185239213Sgibbs{ 185339213Sgibbs 185439213Sgibbs struct cam_periph *periph; 185539213Sgibbs struct cd_softc *softc; 1856168752Sscottl int nocopyout, error = 0; 185739213Sgibbs 1858120599Sphk periph = (struct cam_periph *)dp->d_drv1; 185939213Sgibbs if (periph == NULL) 186039213Sgibbs return(ENXIO); 186139213Sgibbs 1862168752Sscottl cam_periph_lock(periph); 186339213Sgibbs 186439213Sgibbs softc = (struct cd_softc *)periph->softc; 186539213Sgibbs 1866236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1867236602Smav ("cdioctl(%#lx)\n", cmd)); 186839213Sgibbs 1869168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1870168752Sscottl cam_periph_unlock(periph); 1871168752Sscottl cam_periph_release(periph); 1872168752Sscottl return (error); 1873168752Sscottl } 187439213Sgibbs 1875111206Sken /* 1876111206Sken * If we don't have media loaded, check for it. If still don't 1877111206Sken * have media loaded, we can only do a load or eject. 1878144135Sken * 1879144135Sken * We only care whether media is loaded if this is a cd-specific ioctl 1880144135Sken * (thus the IOCGROUP check below). Note that this will break if 1881144135Sken * anyone adds any ioctls into the switch statement below that don't 1882144135Sken * have their ioctl group set to 'c'. 1883111206Sken */ 1884111206Sken if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1885111206Sken && ((cmd != CDIOCCLOSE) 1886144135Sken && (cmd != CDIOCEJECT)) 1887144135Sken && (IOCGROUP(cmd) == 'c')) { 1888111206Sken error = cdcheckmedia(periph); 1889171529Skan if (error != 0) { 1890171529Skan cam_periph_unhold(periph); 1891171529Skan cam_periph_unlock(periph); 1892171529Skan return (error); 1893171529Skan } 1894111206Sken } 1895168752Sscottl /* 1896168752Sscottl * Drop the lock here so later mallocs can use WAITOK. The periph 1897168752Sscottl * is essentially locked still with the cam_periph_hold call above. 1898168752Sscottl */ 1899168752Sscottl cam_periph_unlock(periph); 190040020Sken 1901141031Ssobomax nocopyout = 0; 190239213Sgibbs switch (cmd) { 190339213Sgibbs 190439213Sgibbs case CDIOCPLAYTRACKS: 190539213Sgibbs { 190639213Sgibbs struct ioc_play_track *args 190739213Sgibbs = (struct ioc_play_track *) addr; 1908111206Sken struct cd_mode_params params; 1909111206Sken union cd_pages *page; 191039213Sgibbs 1911111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 1912169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1913111206Sken M_WAITOK | M_ZERO); 191439213Sgibbs 1915168752Sscottl cam_periph_lock(periph); 191639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 191739213Sgibbs ("trying to do CDIOCPLAYTRACKS\n")); 191839213Sgibbs 1919111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 192039213Sgibbs if (error) { 1921169562Sscottl free(params.mode_buf, M_SCSICD); 1922168752Sscottl cam_periph_unlock(periph); 192339213Sgibbs break; 192439213Sgibbs } 1925111206Sken page = cdgetpage(¶ms); 1926111206Sken 1927111206Sken page->audio.flags &= ~CD_PA_SOTC; 1928111206Sken page->audio.flags |= CD_PA_IMMED; 1929111206Sken error = cdsetmode(periph, ¶ms); 1930169562Sscottl free(params.mode_buf, M_SCSICD); 1931168752Sscottl if (error) { 1932168752Sscottl cam_periph_unlock(periph); 193339213Sgibbs break; 1934168752Sscottl } 1935111206Sken 1936111206Sken /* 1937111206Sken * This was originally implemented with the PLAY 1938111206Sken * AUDIO TRACK INDEX command, but that command was 1939111206Sken * deprecated after SCSI-2. Most (all?) SCSI CDROM 1940111206Sken * drives support it but ATAPI and ATAPI-derivative 1941111206Sken * drives don't seem to support it. So we keep a 1942111206Sken * cache of the table of contents and translate 1943111206Sken * track numbers to MSF format. 1944111206Sken */ 1945111206Sken if (softc->flags & CD_FLAG_VALID_TOC) { 1946111206Sken union msf_lba *sentry, *eentry; 1947111206Sken int st, et; 1948111206Sken 1949111206Sken if (args->end_track < 1950111206Sken softc->toc.header.ending_track + 1) 1951111206Sken args->end_track++; 1952111206Sken if (args->end_track > 1953111206Sken softc->toc.header.ending_track + 1) 1954111206Sken args->end_track = 1955111206Sken softc->toc.header.ending_track + 1; 1956111206Sken st = args->start_track - 1957111206Sken softc->toc.header.starting_track; 1958111206Sken et = args->end_track - 1959111206Sken softc->toc.header.starting_track; 1960111206Sken if ((st < 0) 1961111206Sken || (et < 0) 1962111206Sken || (st > (softc->toc.header.ending_track - 1963111206Sken softc->toc.header.starting_track))) { 1964111206Sken error = EINVAL; 1965111206Sken break; 1966111206Sken } 1967111206Sken sentry = &softc->toc.entries[st].addr; 1968111206Sken eentry = &softc->toc.entries[et].addr; 1969111206Sken error = cdplaymsf(periph, 1970111206Sken sentry->msf.minute, 1971111206Sken sentry->msf.second, 1972111206Sken sentry->msf.frame, 1973111206Sken eentry->msf.minute, 1974111206Sken eentry->msf.second, 1975111206Sken eentry->msf.frame); 1976111206Sken } else { 1977111206Sken /* 1978111206Sken * If we don't have a valid TOC, try the 1979111206Sken * play track index command. It is part of 1980111206Sken * the SCSI-2 spec, but was removed in the 1981111206Sken * MMC specs. ATAPI and ATAPI-derived 1982111206Sken * drives don't support it. 1983111206Sken */ 1984111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 1985111206Sken args->start_track = 1986111206Sken bin2bcd(args->start_track); 1987111206Sken args->end_track = 1988111206Sken bin2bcd(args->end_track); 1989111206Sken } 1990111206Sken error = cdplaytracks(periph, 1991111206Sken args->start_track, 1992111206Sken args->start_index, 1993111206Sken args->end_track, 1994111206Sken args->end_index); 199539213Sgibbs } 1996168752Sscottl cam_periph_unlock(periph); 199739213Sgibbs } 199839213Sgibbs break; 199939213Sgibbs case CDIOCPLAYMSF: 200039213Sgibbs { 200139213Sgibbs struct ioc_play_msf *args 200239213Sgibbs = (struct ioc_play_msf *) addr; 2003111206Sken struct cd_mode_params params; 2004111206Sken union cd_pages *page; 200539213Sgibbs 2006111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2007169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2008111206Sken M_WAITOK | M_ZERO); 200939213Sgibbs 2010168752Sscottl cam_periph_lock(periph); 201139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 201239213Sgibbs ("trying to do CDIOCPLAYMSF\n")); 201339213Sgibbs 2014111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 201539213Sgibbs if (error) { 2016169562Sscottl free(params.mode_buf, M_SCSICD); 2017168752Sscottl cam_periph_unlock(periph); 201839213Sgibbs break; 201939213Sgibbs } 2020111206Sken page = cdgetpage(¶ms); 2021111206Sken 2022111206Sken page->audio.flags &= ~CD_PA_SOTC; 2023111206Sken page->audio.flags |= CD_PA_IMMED; 2024111206Sken error = cdsetmode(periph, ¶ms); 2025169562Sscottl free(params.mode_buf, M_SCSICD); 2026168752Sscottl if (error) { 2027168752Sscottl cam_periph_unlock(periph); 202839213Sgibbs break; 2029168752Sscottl } 203039213Sgibbs error = cdplaymsf(periph, 203139213Sgibbs args->start_m, 203239213Sgibbs args->start_s, 203339213Sgibbs args->start_f, 203439213Sgibbs args->end_m, 203539213Sgibbs args->end_s, 203639213Sgibbs args->end_f); 2037168752Sscottl cam_periph_unlock(periph); 203839213Sgibbs } 203939213Sgibbs break; 204039213Sgibbs case CDIOCPLAYBLOCKS: 204139213Sgibbs { 204239213Sgibbs struct ioc_play_blocks *args 204339213Sgibbs = (struct ioc_play_blocks *) addr; 2044111206Sken struct cd_mode_params params; 2045111206Sken union cd_pages *page; 204639213Sgibbs 2047111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2048169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2049111206Sken M_WAITOK | M_ZERO); 205039213Sgibbs 2051168752Sscottl cam_periph_lock(periph); 2052168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2053168752Sscottl ("trying to do CDIOCPLAYBLOCKS\n")); 2054168752Sscottl 2055168752Sscottl 2056111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 205739213Sgibbs if (error) { 2058169562Sscottl free(params.mode_buf, M_SCSICD); 2059168752Sscottl cam_periph_unlock(periph); 206039213Sgibbs break; 206139213Sgibbs } 2062111206Sken page = cdgetpage(¶ms); 2063111206Sken 2064111206Sken page->audio.flags &= ~CD_PA_SOTC; 2065111206Sken page->audio.flags |= CD_PA_IMMED; 2066111206Sken error = cdsetmode(periph, ¶ms); 2067169562Sscottl free(params.mode_buf, M_SCSICD); 2068168752Sscottl if (error) { 2069168752Sscottl cam_periph_unlock(periph); 207039213Sgibbs break; 2071168752Sscottl } 207239213Sgibbs error = cdplay(periph, args->blk, args->len); 2073168752Sscottl cam_periph_unlock(periph); 207439213Sgibbs } 207539213Sgibbs break; 2076141031Ssobomax case CDIOCREADSUBCHANNEL_SYSSPACE: 2077141031Ssobomax nocopyout = 1; 2078141031Ssobomax /* Fallthrough */ 207939213Sgibbs case CDIOCREADSUBCHANNEL: 208039213Sgibbs { 208139213Sgibbs struct ioc_read_subchannel *args 208239213Sgibbs = (struct ioc_read_subchannel *) addr; 208339213Sgibbs struct cd_sub_channel_info *data; 208439213Sgibbs u_int32_t len = args->data_len; 208539213Sgibbs 2086168752Sscottl data = malloc(sizeof(struct cd_sub_channel_info), 2087203931Smav M_SCSICD, M_WAITOK | M_ZERO); 2088168752Sscottl 2089168752Sscottl cam_periph_lock(periph); 209039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 209139213Sgibbs ("trying to do CDIOCREADSUBCHANNEL\n")); 209239213Sgibbs 209339213Sgibbs if ((len > sizeof(struct cd_sub_channel_info)) || 209439213Sgibbs (len < sizeof(struct cd_sub_channel_header))) { 209539213Sgibbs printf( 209639213Sgibbs "scsi_cd: cdioctl: " 209739213Sgibbs "cdioreadsubchannel: error, len=%d\n", 209839213Sgibbs len); 209939213Sgibbs error = EINVAL; 2100169562Sscottl free(data, M_SCSICD); 2101168752Sscottl cam_periph_unlock(periph); 210239213Sgibbs break; 210339213Sgibbs } 210439213Sgibbs 210539213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 210639213Sgibbs args->track = bin2bcd(args->track); 210739213Sgibbs 210839213Sgibbs error = cdreadsubchannel(periph, args->address_format, 210939213Sgibbs args->data_format, args->track, data, len); 211039213Sgibbs 211139213Sgibbs if (error) { 2112169562Sscottl free(data, M_SCSICD); 2113168752Sscottl cam_periph_unlock(periph); 211439213Sgibbs break; 211539213Sgibbs } 211639213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 211739213Sgibbs data->what.track_info.track_number = 211839213Sgibbs bcd2bin(data->what.track_info.track_number); 211939213Sgibbs len = min(len, ((data->header.data_len[0] << 8) + 212039213Sgibbs data->header.data_len[1] + 212139213Sgibbs sizeof(struct cd_sub_channel_header))); 2122168752Sscottl cam_periph_unlock(periph); 2123141031Ssobomax if (nocopyout == 0) { 2124141031Ssobomax if (copyout(data, args->data, len) != 0) { 2125141031Ssobomax error = EFAULT; 2126141031Ssobomax } 2127141031Ssobomax } else { 2128141031Ssobomax bcopy(data, args->data, len); 212939213Sgibbs } 2130169562Sscottl free(data, M_SCSICD); 213139213Sgibbs } 213239213Sgibbs break; 213339213Sgibbs 213439213Sgibbs case CDIOREADTOCHEADER: 213539213Sgibbs { 213639213Sgibbs struct ioc_toc_header *th; 213739213Sgibbs 2138169562Sscottl th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2139203931Smav M_WAITOK | M_ZERO); 2140168752Sscottl 2141168752Sscottl cam_periph_lock(periph); 214239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 214339213Sgibbs ("trying to do CDIOREADTOCHEADER\n")); 214439213Sgibbs 2145111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2146221585Savg sizeof (*th), /*sense_flags*/SF_NO_PRINT); 214739213Sgibbs if (error) { 2148169562Sscottl free(th, M_SCSICD); 2149168752Sscottl cam_periph_unlock(periph); 215039213Sgibbs break; 215139213Sgibbs } 215239213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 215339213Sgibbs /* we are going to have to convert the BCD 215439213Sgibbs * encoding on the cd to what is expected 215539213Sgibbs */ 215639213Sgibbs th->starting_track = 215739213Sgibbs bcd2bin(th->starting_track); 215839213Sgibbs th->ending_track = bcd2bin(th->ending_track); 215939213Sgibbs } 216090868Smike th->len = ntohs(th->len); 216139213Sgibbs bcopy(th, addr, sizeof(*th)); 2162169562Sscottl free(th, M_SCSICD); 2163168752Sscottl cam_periph_unlock(periph); 216439213Sgibbs } 216539213Sgibbs break; 216639213Sgibbs case CDIOREADTOCENTRYS: 216739213Sgibbs { 2168111206Sken struct cd_tocdata *data; 2169111206Sken struct cd_toc_single *lead; 217039213Sgibbs struct ioc_read_toc_entry *te = 217139213Sgibbs (struct ioc_read_toc_entry *) addr; 217239213Sgibbs struct ioc_toc_header *th; 217339213Sgibbs u_int32_t len, readlen, idx, num; 217439213Sgibbs u_int32_t starting_track = te->starting_track; 217539213Sgibbs 2176203931Smav data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2177203931Smav lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 2178168752Sscottl 2179168752Sscottl cam_periph_lock(periph); 218039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 218139213Sgibbs ("trying to do CDIOREADTOCENTRYS\n")); 218239213Sgibbs 218339213Sgibbs if (te->data_len < sizeof(struct cd_toc_entry) 218439213Sgibbs || (te->data_len % sizeof(struct cd_toc_entry)) != 0 218539213Sgibbs || (te->address_format != CD_MSF_FORMAT 218639213Sgibbs && te->address_format != CD_LBA_FORMAT)) { 218739213Sgibbs error = EINVAL; 218839213Sgibbs printf("scsi_cd: error in readtocentries, " 218939213Sgibbs "returning EINVAL\n"); 2190169562Sscottl free(data, M_SCSICD); 2191169562Sscottl free(lead, M_SCSICD); 2192168752Sscottl cam_periph_unlock(periph); 219339213Sgibbs break; 219439213Sgibbs } 219539213Sgibbs 219639213Sgibbs th = &data->header; 2197111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2198111206Sken sizeof (*th), /*sense_flags*/0); 219939213Sgibbs if (error) { 2200169562Sscottl free(data, M_SCSICD); 2201169562Sscottl free(lead, M_SCSICD); 2202168752Sscottl cam_periph_unlock(periph); 220339213Sgibbs break; 220439213Sgibbs } 220539213Sgibbs 220639213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 220739213Sgibbs /* we are going to have to convert the BCD 220839213Sgibbs * encoding on the cd to what is expected 220939213Sgibbs */ 221039213Sgibbs th->starting_track = 221139213Sgibbs bcd2bin(th->starting_track); 221239213Sgibbs th->ending_track = bcd2bin(th->ending_track); 221339213Sgibbs } 221439213Sgibbs 221539213Sgibbs if (starting_track == 0) 221639213Sgibbs starting_track = th->starting_track; 221739213Sgibbs else if (starting_track == LEADOUT) 221839213Sgibbs starting_track = th->ending_track + 1; 221939213Sgibbs else if (starting_track < th->starting_track || 222039213Sgibbs starting_track > th->ending_track + 1) { 222139213Sgibbs printf("scsi_cd: error in readtocentries, " 222239213Sgibbs "returning EINVAL\n"); 2223169562Sscottl free(data, M_SCSICD); 2224169562Sscottl free(lead, M_SCSICD); 2225168752Sscottl cam_periph_unlock(periph); 222639213Sgibbs error = EINVAL; 222739213Sgibbs break; 222839213Sgibbs } 222939213Sgibbs 223039213Sgibbs /* calculate reading length without leadout entry */ 223139213Sgibbs readlen = (th->ending_track - starting_track + 1) * 223239213Sgibbs sizeof(struct cd_toc_entry); 223339213Sgibbs 223439213Sgibbs /* and with leadout entry */ 223539213Sgibbs len = readlen + sizeof(struct cd_toc_entry); 223639213Sgibbs if (te->data_len < len) { 223739213Sgibbs len = te->data_len; 223839213Sgibbs if (readlen > len) 223939213Sgibbs readlen = len; 224039213Sgibbs } 224139213Sgibbs if (len > sizeof(data->entries)) { 224239213Sgibbs printf("scsi_cd: error in readtocentries, " 224339213Sgibbs "returning EINVAL\n"); 224439213Sgibbs error = EINVAL; 2245169562Sscottl free(data, M_SCSICD); 2246169562Sscottl free(lead, M_SCSICD); 2247168752Sscottl cam_periph_unlock(periph); 224839213Sgibbs break; 224939213Sgibbs } 225039213Sgibbs num = len / sizeof(struct cd_toc_entry); 225139213Sgibbs 225239213Sgibbs if (readlen > 0) { 225339213Sgibbs error = cdreadtoc(periph, te->address_format, 225439213Sgibbs starting_track, 2255111206Sken (u_int8_t *)data, 2256111206Sken readlen + sizeof (*th), 2257111206Sken /*sense_flags*/0); 225839213Sgibbs if (error) { 2259169562Sscottl free(data, M_SCSICD); 2260169562Sscottl free(lead, M_SCSICD); 2261168752Sscottl cam_periph_unlock(periph); 226239213Sgibbs break; 226339213Sgibbs } 226439213Sgibbs } 226539213Sgibbs 226639213Sgibbs /* make leadout entry if needed */ 226739213Sgibbs idx = starting_track + num - 1; 226839213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 226939213Sgibbs th->ending_track = bcd2bin(th->ending_track); 227039213Sgibbs if (idx == th->ending_track + 1) { 227139213Sgibbs error = cdreadtoc(periph, te->address_format, 2272111206Sken LEADOUT, (u_int8_t *)lead, 2273111206Sken sizeof(*lead), 2274111206Sken /*sense_flags*/0); 227539213Sgibbs if (error) { 2276169562Sscottl free(data, M_SCSICD); 2277169562Sscottl free(lead, M_SCSICD); 2278168752Sscottl cam_periph_unlock(periph); 227939213Sgibbs break; 228039213Sgibbs } 228139213Sgibbs data->entries[idx - starting_track] = 228239213Sgibbs lead->entry; 228339213Sgibbs } 228439213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 228539213Sgibbs for (idx = 0; idx < num - 1; idx++) { 228639213Sgibbs data->entries[idx].track = 228739213Sgibbs bcd2bin(data->entries[idx].track); 228839213Sgibbs } 228939213Sgibbs } 229039213Sgibbs 2291168752Sscottl cam_periph_unlock(periph); 229239213Sgibbs error = copyout(data->entries, te->data, len); 2293169562Sscottl free(data, M_SCSICD); 2294169562Sscottl free(lead, M_SCSICD); 229539213Sgibbs } 229639213Sgibbs break; 229739213Sgibbs case CDIOREADTOCENTRY: 229839213Sgibbs { 2299111206Sken struct cd_toc_single *data; 230039213Sgibbs struct ioc_read_toc_single_entry *te = 230139213Sgibbs (struct ioc_read_toc_single_entry *) addr; 230239213Sgibbs struct ioc_toc_header *th; 230339213Sgibbs u_int32_t track; 230439213Sgibbs 2305203931Smav data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2306168752Sscottl 2307168752Sscottl cam_periph_lock(periph); 230839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 230939213Sgibbs ("trying to do CDIOREADTOCENTRY\n")); 231039213Sgibbs 231139213Sgibbs if (te->address_format != CD_MSF_FORMAT 231239213Sgibbs && te->address_format != CD_LBA_FORMAT) { 231339213Sgibbs printf("error in readtocentry, " 231439213Sgibbs " returning EINVAL\n"); 2315169562Sscottl free(data, M_SCSICD); 231639213Sgibbs error = EINVAL; 2317168752Sscottl cam_periph_unlock(periph); 231839213Sgibbs break; 231939213Sgibbs } 232039213Sgibbs 232139213Sgibbs th = &data->header; 2322111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2323111206Sken sizeof (*th), /*sense_flags*/0); 232439213Sgibbs if (error) { 2325169562Sscottl free(data, M_SCSICD); 2326168752Sscottl cam_periph_unlock(periph); 232739213Sgibbs break; 232839213Sgibbs } 232939213Sgibbs 233039213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 233139213Sgibbs /* we are going to have to convert the BCD 233239213Sgibbs * encoding on the cd to what is expected 233339213Sgibbs */ 233439213Sgibbs th->starting_track = 233539213Sgibbs bcd2bin(th->starting_track); 233639213Sgibbs th->ending_track = bcd2bin(th->ending_track); 233739213Sgibbs } 233839213Sgibbs track = te->track; 233939213Sgibbs if (track == 0) 234039213Sgibbs track = th->starting_track; 234139213Sgibbs else if (track == LEADOUT) 234239213Sgibbs /* OK */; 234339213Sgibbs else if (track < th->starting_track || 234439213Sgibbs track > th->ending_track + 1) { 234539213Sgibbs printf("error in readtocentry, " 234639213Sgibbs " returning EINVAL\n"); 2347169562Sscottl free(data, M_SCSICD); 234839213Sgibbs error = EINVAL; 2349168752Sscottl cam_periph_unlock(periph); 235039213Sgibbs break; 235139213Sgibbs } 235239213Sgibbs 235339213Sgibbs error = cdreadtoc(periph, te->address_format, track, 2354111206Sken (u_int8_t *)data, sizeof(*data), 2355111206Sken /*sense_flags*/0); 235639213Sgibbs if (error) { 2357169562Sscottl free(data, M_SCSICD); 2358168752Sscottl cam_periph_unlock(periph); 235939213Sgibbs break; 236039213Sgibbs } 236139213Sgibbs 236239213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 236339213Sgibbs data->entry.track = bcd2bin(data->entry.track); 236439213Sgibbs bcopy(&data->entry, &te->entry, 236539213Sgibbs sizeof(struct cd_toc_entry)); 2366169562Sscottl free(data, M_SCSICD); 2367168752Sscottl cam_periph_unlock(periph); 236839213Sgibbs } 236939213Sgibbs break; 237039213Sgibbs case CDIOCSETPATCH: 237139213Sgibbs { 2372111206Sken struct ioc_patch *arg = (struct ioc_patch *)addr; 2373111206Sken struct cd_mode_params params; 2374111206Sken union cd_pages *page; 237539213Sgibbs 2376168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2377169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2378168752Sscottl M_WAITOK | M_ZERO); 2379168752Sscottl 2380168752Sscottl cam_periph_lock(periph); 238139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 238239213Sgibbs ("trying to do CDIOCSETPATCH\n")); 238339213Sgibbs 2384111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 238539213Sgibbs if (error) { 2386169562Sscottl free(params.mode_buf, M_SCSICD); 2387168752Sscottl cam_periph_unlock(periph); 238839213Sgibbs break; 238939213Sgibbs } 2390111206Sken page = cdgetpage(¶ms); 2391111206Sken 2392111206Sken page->audio.port[LEFT_PORT].channels = 239339213Sgibbs arg->patch[0]; 2394111206Sken page->audio.port[RIGHT_PORT].channels = 239539213Sgibbs arg->patch[1]; 2396111206Sken page->audio.port[2].channels = arg->patch[2]; 2397111206Sken page->audio.port[3].channels = arg->patch[3]; 2398111206Sken error = cdsetmode(periph, ¶ms); 2399169562Sscottl free(params.mode_buf, M_SCSICD); 2400168752Sscottl cam_periph_unlock(periph); 240139213Sgibbs } 240239213Sgibbs break; 240339213Sgibbs case CDIOCGETVOL: 240439213Sgibbs { 240539213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2406111206Sken struct cd_mode_params params; 2407111206Sken union cd_pages *page; 240839213Sgibbs 2409168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2410169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2411168752Sscottl M_WAITOK | M_ZERO); 2412168752Sscottl 2413168752Sscottl cam_periph_lock(periph); 241439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 241539213Sgibbs ("trying to do CDIOCGETVOL\n")); 241639213Sgibbs 2417111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 241839213Sgibbs if (error) { 2419169562Sscottl free(params.mode_buf, M_SCSICD); 2420168752Sscottl cam_periph_unlock(periph); 242139213Sgibbs break; 242239213Sgibbs } 2423111206Sken page = cdgetpage(¶ms); 2424111206Sken 242539213Sgibbs arg->vol[LEFT_PORT] = 2426111206Sken page->audio.port[LEFT_PORT].volume; 242739213Sgibbs arg->vol[RIGHT_PORT] = 2428111206Sken page->audio.port[RIGHT_PORT].volume; 2429111206Sken arg->vol[2] = page->audio.port[2].volume; 2430111206Sken arg->vol[3] = page->audio.port[3].volume; 2431169562Sscottl free(params.mode_buf, M_SCSICD); 2432168752Sscottl cam_periph_unlock(periph); 243339213Sgibbs } 243439213Sgibbs break; 243539213Sgibbs case CDIOCSETVOL: 243639213Sgibbs { 243739213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2438111206Sken struct cd_mode_params params; 2439111206Sken union cd_pages *page; 244039213Sgibbs 2441168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2442169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2443168752Sscottl M_WAITOK | M_ZERO); 2444168752Sscottl 2445168752Sscottl cam_periph_lock(periph); 244639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 244739213Sgibbs ("trying to do CDIOCSETVOL\n")); 244839213Sgibbs 2449111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 245039213Sgibbs if (error) { 2451169562Sscottl free(params.mode_buf, M_SCSICD); 2452168752Sscottl cam_periph_unlock(periph); 245339213Sgibbs break; 245439213Sgibbs } 2455111206Sken page = cdgetpage(¶ms); 2456111206Sken 2457111206Sken page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2458111206Sken page->audio.port[LEFT_PORT].volume = 245939213Sgibbs arg->vol[LEFT_PORT]; 2460111206Sken page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2461111206Sken page->audio.port[RIGHT_PORT].volume = 246239213Sgibbs arg->vol[RIGHT_PORT]; 2463111206Sken page->audio.port[2].volume = arg->vol[2]; 2464111206Sken page->audio.port[3].volume = arg->vol[3]; 2465111206Sken error = cdsetmode(periph, ¶ms); 2466168752Sscottl cam_periph_unlock(periph); 2467169562Sscottl free(params.mode_buf, M_SCSICD); 246839213Sgibbs } 246939213Sgibbs break; 247039213Sgibbs case CDIOCSETMONO: 247139213Sgibbs { 2472111206Sken struct cd_mode_params params; 2473111206Sken union cd_pages *page; 247439213Sgibbs 2475168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2476169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2477168752Sscottl M_WAITOK | M_ZERO); 2478168752Sscottl 2479168752Sscottl cam_periph_lock(periph); 248039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 248139213Sgibbs ("trying to do CDIOCSETMONO\n")); 248239213Sgibbs 2483111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 248439213Sgibbs if (error) { 2485169562Sscottl free(params.mode_buf, M_SCSICD); 2486168752Sscottl cam_periph_unlock(periph); 248739213Sgibbs break; 248839213Sgibbs } 2489111206Sken page = cdgetpage(¶ms); 2490111206Sken 2491111206Sken page->audio.port[LEFT_PORT].channels = 249239213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2493111206Sken page->audio.port[RIGHT_PORT].channels = 249439213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2495111206Sken page->audio.port[2].channels = 0; 2496111206Sken page->audio.port[3].channels = 0; 2497111206Sken error = cdsetmode(periph, ¶ms); 2498168752Sscottl cam_periph_unlock(periph); 2499169562Sscottl free(params.mode_buf, M_SCSICD); 250039213Sgibbs } 250139213Sgibbs break; 250239213Sgibbs case CDIOCSETSTEREO: 250339213Sgibbs { 2504111206Sken struct cd_mode_params params; 2505111206Sken union cd_pages *page; 250639213Sgibbs 2507168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2508169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2509168752Sscottl M_WAITOK | M_ZERO); 2510168752Sscottl 2511168752Sscottl cam_periph_lock(periph); 251239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 251339213Sgibbs ("trying to do CDIOCSETSTEREO\n")); 251439213Sgibbs 2515111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 251639213Sgibbs if (error) { 2517169562Sscottl free(params.mode_buf, M_SCSICD); 2518168752Sscottl cam_periph_unlock(periph); 251939213Sgibbs break; 252039213Sgibbs } 2521111206Sken page = cdgetpage(¶ms); 2522111206Sken 2523111206Sken page->audio.port[LEFT_PORT].channels = 252439213Sgibbs LEFT_CHANNEL; 2525111206Sken page->audio.port[RIGHT_PORT].channels = 252639213Sgibbs RIGHT_CHANNEL; 2527111206Sken page->audio.port[2].channels = 0; 2528111206Sken page->audio.port[3].channels = 0; 2529111206Sken error = cdsetmode(periph, ¶ms); 2530169562Sscottl free(params.mode_buf, M_SCSICD); 2531168752Sscottl cam_periph_unlock(periph); 253239213Sgibbs } 253339213Sgibbs break; 253439213Sgibbs case CDIOCSETMUTE: 253539213Sgibbs { 2536111206Sken struct cd_mode_params params; 2537111206Sken union cd_pages *page; 253839213Sgibbs 2539168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2540169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2541168752Sscottl M_WAITOK | M_ZERO); 2542168752Sscottl 2543168752Sscottl cam_periph_lock(periph); 254439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 254539213Sgibbs ("trying to do CDIOCSETMUTE\n")); 254639213Sgibbs 2547111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 254839213Sgibbs if (error) { 2549208800Savg free(params.mode_buf, M_SCSICD); 2550168752Sscottl cam_periph_unlock(periph); 255139213Sgibbs break; 255239213Sgibbs } 2553111206Sken page = cdgetpage(¶ms); 2554111206Sken 2555111206Sken page->audio.port[LEFT_PORT].channels = 0; 2556111206Sken page->audio.port[RIGHT_PORT].channels = 0; 2557111206Sken page->audio.port[2].channels = 0; 2558111206Sken page->audio.port[3].channels = 0; 2559111206Sken error = cdsetmode(periph, ¶ms); 2560169562Sscottl free(params.mode_buf, M_SCSICD); 2561168752Sscottl cam_periph_unlock(periph); 256239213Sgibbs } 256339213Sgibbs break; 256439213Sgibbs case CDIOCSETLEFT: 256539213Sgibbs { 2566111206Sken struct cd_mode_params params; 2567111206Sken union cd_pages *page; 256839213Sgibbs 2569168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2570169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2571168752Sscottl M_WAITOK | M_ZERO); 2572168752Sscottl 2573168752Sscottl cam_periph_lock(periph); 257439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 257539213Sgibbs ("trying to do CDIOCSETLEFT\n")); 257639213Sgibbs 2577111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 257839213Sgibbs if (error) { 2579169562Sscottl free(params.mode_buf, M_SCSICD); 2580168752Sscottl cam_periph_unlock(periph); 258139213Sgibbs break; 258239213Sgibbs } 2583111206Sken page = cdgetpage(¶ms); 2584111206Sken 2585111206Sken page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2586111206Sken page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2587111206Sken page->audio.port[2].channels = 0; 2588111206Sken page->audio.port[3].channels = 0; 2589111206Sken error = cdsetmode(periph, ¶ms); 2590169562Sscottl free(params.mode_buf, M_SCSICD); 2591168752Sscottl cam_periph_unlock(periph); 259239213Sgibbs } 259339213Sgibbs break; 259439213Sgibbs case CDIOCSETRIGHT: 259539213Sgibbs { 2596111206Sken struct cd_mode_params params; 2597111206Sken union cd_pages *page; 259839213Sgibbs 2599111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2600169562Sscottl params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2601111206Sken M_WAITOK | M_ZERO); 2602111206Sken 2603168752Sscottl cam_periph_lock(periph); 2604168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2605168752Sscottl ("trying to do CDIOCSETRIGHT\n")); 2606168752Sscottl 2607111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 260839213Sgibbs if (error) { 2609169562Sscottl free(params.mode_buf, M_SCSICD); 2610168752Sscottl cam_periph_unlock(periph); 261139213Sgibbs break; 261239213Sgibbs } 2613111206Sken page = cdgetpage(¶ms); 2614111206Sken 2615111206Sken page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2616111206Sken page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2617111206Sken page->audio.port[2].channels = 0; 2618111206Sken page->audio.port[3].channels = 0; 2619111206Sken error = cdsetmode(periph, ¶ms); 2620169562Sscottl free(params.mode_buf, M_SCSICD); 2621168752Sscottl cam_periph_unlock(periph); 262239213Sgibbs } 262339213Sgibbs break; 262439213Sgibbs case CDIOCRESUME: 2625168752Sscottl cam_periph_lock(periph); 262639213Sgibbs error = cdpause(periph, 1); 2627168752Sscottl cam_periph_unlock(periph); 262839213Sgibbs break; 262939213Sgibbs case CDIOCPAUSE: 2630168752Sscottl cam_periph_lock(periph); 263139213Sgibbs error = cdpause(periph, 0); 2632168752Sscottl cam_periph_unlock(periph); 263339213Sgibbs break; 263439213Sgibbs case CDIOCSTART: 2635168752Sscottl cam_periph_lock(periph); 2636111206Sken error = cdstartunit(periph, 0); 2637168752Sscottl cam_periph_unlock(periph); 263839213Sgibbs break; 2639111206Sken case CDIOCCLOSE: 2640168752Sscottl cam_periph_lock(periph); 2641111206Sken error = cdstartunit(periph, 1); 2642168752Sscottl cam_periph_unlock(periph); 2643111206Sken break; 264439213Sgibbs case CDIOCSTOP: 2645168752Sscottl cam_periph_lock(periph); 264639213Sgibbs error = cdstopunit(periph, 0); 2647168752Sscottl cam_periph_unlock(periph); 264839213Sgibbs break; 264939213Sgibbs case CDIOCEJECT: 2650168752Sscottl cam_periph_lock(periph); 265139213Sgibbs error = cdstopunit(periph, 1); 2652168752Sscottl cam_periph_unlock(periph); 265339213Sgibbs break; 265439213Sgibbs case CDIOCALLOW: 2655168752Sscottl cam_periph_lock(periph); 265639213Sgibbs cdprevent(periph, PR_ALLOW); 2657168752Sscottl cam_periph_unlock(periph); 265839213Sgibbs break; 265939213Sgibbs case CDIOCPREVENT: 2660168752Sscottl cam_periph_lock(periph); 266139213Sgibbs cdprevent(periph, PR_PREVENT); 2662168752Sscottl cam_periph_unlock(periph); 266339213Sgibbs break; 266439213Sgibbs case CDIOCSETDEBUG: 266539213Sgibbs /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 266639213Sgibbs error = ENOTTY; 266739213Sgibbs break; 266839213Sgibbs case CDIOCCLRDEBUG: 266939213Sgibbs /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 267039213Sgibbs error = ENOTTY; 267139213Sgibbs break; 267239213Sgibbs case CDIOCRESET: 267339213Sgibbs /* return (cd_reset(periph)); */ 267439213Sgibbs error = ENOTTY; 267539213Sgibbs break; 2676105421Snjl case CDRIOCREADSPEED: 2677168752Sscottl cam_periph_lock(periph); 2678105421Snjl error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2679168752Sscottl cam_periph_unlock(periph); 2680105421Snjl break; 2681105421Snjl case CDRIOCWRITESPEED: 2682168752Sscottl cam_periph_lock(periph); 2683105421Snjl error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2684168752Sscottl cam_periph_unlock(periph); 2685105421Snjl break; 2686229395Smav case CDRIOCGETBLOCKSIZE: 2687229395Smav *(int *)addr = softc->params.blksize; 2688229395Smav break; 2689229395Smav case CDRIOCSETBLOCKSIZE: 2690229395Smav if (*(int *)addr <= 0) { 2691229395Smav error = EINVAL; 2692229395Smav break; 2693229395Smav } 2694229395Smav softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr; 2695229395Smav break; 269660422Sken case DVDIOCSENDKEY: 269760422Sken case DVDIOCREPORTKEY: { 269860422Sken struct dvd_authinfo *authinfo; 269960422Sken 270060422Sken authinfo = (struct dvd_authinfo *)addr; 270160422Sken 270260422Sken if (cmd == DVDIOCREPORTKEY) 270360422Sken error = cdreportkey(periph, authinfo); 270460422Sken else 270560422Sken error = cdsendkey(periph, authinfo); 270660422Sken break; 2707104456Sphk } 270860422Sken case DVDIOCREADSTRUCTURE: { 270960422Sken struct dvd_struct *dvdstruct; 271060422Sken 271160422Sken dvdstruct = (struct dvd_struct *)addr; 271260422Sken 271360422Sken error = cdreaddvdstructure(periph, dvdstruct); 271460422Sken 271560422Sken break; 271660422Sken } 271739213Sgibbs default: 2718168752Sscottl cam_periph_lock(periph); 271939213Sgibbs error = cam_periph_ioctl(periph, cmd, addr, cderror); 2720168752Sscottl cam_periph_unlock(periph); 272139213Sgibbs break; 272239213Sgibbs } 272339213Sgibbs 2724168752Sscottl cam_periph_lock(periph); 2725168752Sscottl cam_periph_unhold(periph); 2726168752Sscottl 272739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2728104456Sphk if (error && bootverbose) { 2729104456Sphk printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2730104456Sphk } 2731168752Sscottl cam_periph_unlock(periph); 273239213Sgibbs 273339213Sgibbs return (error); 273439213Sgibbs} 273539213Sgibbs 273639213Sgibbsstatic void 273739213Sgibbscdprevent(struct cam_periph *periph, int action) 273839213Sgibbs{ 273939213Sgibbs union ccb *ccb; 274039213Sgibbs struct cd_softc *softc; 274139213Sgibbs int error; 274239213Sgibbs 274339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 274439213Sgibbs 274539213Sgibbs softc = (struct cd_softc *)periph->softc; 274639213Sgibbs 274739213Sgibbs if (((action == PR_ALLOW) 274839213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 274939213Sgibbs || ((action == PR_PREVENT) 275039213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 275139213Sgibbs return; 275239213Sgibbs } 275339213Sgibbs 2754198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 275539213Sgibbs 275639213Sgibbs scsi_prevent(&ccb->csio, 2757203108Smav /*retries*/ cd_retry_count, 275839213Sgibbs cddone, 275939213Sgibbs MSG_SIMPLE_Q_TAG, 276039213Sgibbs action, 276139213Sgibbs SSD_FULL_SIZE, 276239213Sgibbs /* timeout */60000); 276339213Sgibbs 276474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 276574840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 276639213Sgibbs 276739213Sgibbs xpt_release_ccb(ccb); 276839213Sgibbs 276939213Sgibbs if (error == 0) { 277039213Sgibbs if (action == PR_ALLOW) 277139213Sgibbs softc->flags &= ~CD_FLAG_DISC_LOCKED; 277239213Sgibbs else 277339213Sgibbs softc->flags |= CD_FLAG_DISC_LOCKED; 277439213Sgibbs } 277539213Sgibbs} 277639213Sgibbs 2777120599Sphk/* 2778120599Sphk * XXX: the disk media and sector size is only really able to change 2779120599Sphk * XXX: while the device is closed. 2780120599Sphk */ 278139213Sgibbsstatic int 2782111206Skencdcheckmedia(struct cam_periph *periph) 278339213Sgibbs{ 278439213Sgibbs struct cd_softc *softc; 2785111206Sken struct ioc_toc_header *toch; 2786111206Sken struct cd_toc_single leadout; 2787111206Sken u_int32_t size, toclen; 2788111206Sken int error, num_entries, cdindex; 2789111206Sken 2790111206Sken softc = (struct cd_softc *)periph->softc; 2791111206Sken 2792111206Sken cdprevent(periph, PR_PREVENT); 2793134824Sphk softc->disk->d_sectorsize = 2048; 2794125975Sphk softc->disk->d_mediasize = 0; 2795111206Sken 2796111206Sken /* 2797111206Sken * Get the disc size and block size. If we can't get it, we don't 2798111206Sken * have media, most likely. 2799111206Sken */ 2800111206Sken if ((error = cdsize(periph, &size)) != 0) { 2801111206Sken softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2802111206Sken cdprevent(periph, PR_ALLOW); 2803111206Sken return (error); 2804206648Savg } else { 2805111206Sken softc->flags |= CD_FLAG_VALID_MEDIA; 2806206648Savg softc->disk->d_sectorsize = softc->params.blksize; 2807206648Savg softc->disk->d_mediasize = 2808206648Savg (off_t)softc->params.blksize * softc->params.disksize; 2809206648Savg } 2810111206Sken 2811111206Sken /* 2812111206Sken * Now we check the table of contents. This (currently) is only 2813111206Sken * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2814111206Sken * things like present a separate entry in /dev for each track, 2815111206Sken * like that acd(4) driver does. 2816111206Sken */ 2817111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2818111206Sken toch = &softc->toc.header; 2819111206Sken /* 2820111206Sken * We will get errors here for media that doesn't have a table of 2821111206Sken * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2822111206Sken * command is presented for a DDCD/CD-R/RW media, where the first TOC 2823111206Sken * has not been recorded (no complete session) and the Format codes 2824111206Sken * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2825111206Sken * with an INVALID FIELD IN CDB. Devices that are not capable of 2826111206Sken * reading an incomplete session on DDC/CD-R/RW media shall report 2827111206Sken * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2828111206Sken * 2829111206Sken * So this isn't fatal if we can't read the table of contents, it 2830111206Sken * just means that the user won't be able to issue the play tracks 2831111206Sken * ioctl, and likely lots of other stuff won't work either. They 2832111206Sken * need to burn the CD before we can do a whole lot with it. So 2833111206Sken * we don't print anything here if we get an error back. 2834111206Sken */ 2835111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2836111206Sken SF_NO_PRINT); 2837111206Sken /* 2838111206Sken * Errors in reading the table of contents aren't fatal, we just 2839111206Sken * won't have a valid table of contents cached. 2840111206Sken */ 2841111206Sken if (error != 0) { 2842111206Sken error = 0; 2843111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2844111206Sken goto bailout; 2845111206Sken } 2846111206Sken 2847111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2848111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2849111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2850111206Sken } 2851111206Sken 2852111206Sken /* Number of TOC entries, plus leadout */ 2853111206Sken num_entries = (toch->ending_track - toch->starting_track) + 2; 2854111206Sken 2855111206Sken if (num_entries <= 0) 2856111206Sken goto bailout; 2857111206Sken 2858111206Sken toclen = num_entries * sizeof(struct cd_toc_entry); 2859111206Sken 2860111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2861111206Sken (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2862111206Sken SF_NO_PRINT); 2863111206Sken if (error != 0) { 2864111206Sken error = 0; 2865111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2866111206Sken goto bailout; 2867111206Sken } 2868111206Sken 2869111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2870111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2871111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2872111206Sken } 2873111206Sken /* 2874111206Sken * XXX KDM is this necessary? Probably only if the drive doesn't 2875111206Sken * return leadout information with the table of contents. 2876111206Sken */ 2877111206Sken cdindex = toch->starting_track + num_entries -1; 2878111206Sken if (cdindex == toch->ending_track + 1) { 2879111206Sken 2880111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2881111206Sken (u_int8_t *)&leadout, sizeof(leadout), 2882111206Sken SF_NO_PRINT); 2883111206Sken if (error != 0) { 2884111206Sken error = 0; 2885111206Sken goto bailout; 2886111206Sken } 2887111206Sken softc->toc.entries[cdindex - toch->starting_track] = 2888111206Sken leadout.entry; 2889111206Sken } 2890111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2891111206Sken for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2892111206Sken softc->toc.entries[cdindex].track = 2893111206Sken bcd2bin(softc->toc.entries[cdindex].track); 2894111206Sken } 2895111206Sken } 2896111206Sken 2897111206Sken softc->flags |= CD_FLAG_VALID_TOC; 2898111206Sken 2899228808Smav /* If the first track is audio, correct sector size. */ 2900228808Smav if ((softc->toc.entries[0].control & 4) == 0) { 2901228808Smav softc->disk->d_sectorsize = softc->params.blksize = 2352; 2902228808Smav softc->disk->d_mediasize = 2903228808Smav (off_t)softc->params.blksize * softc->params.disksize; 2904228808Smav } 2905228808Smav 2906111206Skenbailout: 2907111206Sken 2908111206Sken /* 2909111206Sken * We unconditionally (re)set the blocksize each time the 2910111206Sken * CD device is opened. This is because the CD can change, 2911111206Sken * and therefore the blocksize might change. 2912111206Sken * XXX problems here if some slice or partition is still 2913111206Sken * open with the old size? 2914111206Sken */ 2915125975Sphk if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2916125975Sphk softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2917125975Sphk softc->disk->d_devstat->block_size = softc->params.blksize; 2918111206Sken 2919111206Sken return (error); 2920111206Sken} 2921111206Sken 2922111206Skenstatic int 2923111206Skencdsize(struct cam_periph *periph, u_int32_t *size) 2924111206Sken{ 2925111206Sken struct cd_softc *softc; 292639213Sgibbs union ccb *ccb; 292739213Sgibbs struct scsi_read_capacity_data *rcap_buf; 292839213Sgibbs int error; 292939213Sgibbs 293039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 293139213Sgibbs 293239213Sgibbs softc = (struct cd_softc *)periph->softc; 293339213Sgibbs 2934198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 293539213Sgibbs 2936168752Sscottl /* XXX Should be M_WAITOK */ 293739213Sgibbs rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2938203931Smav M_SCSICD, M_NOWAIT | M_ZERO); 2939168752Sscottl if (rcap_buf == NULL) 2940168752Sscottl return (ENOMEM); 294139213Sgibbs 294239213Sgibbs scsi_read_capacity(&ccb->csio, 2943203108Smav /*retries*/ cd_retry_count, 294439213Sgibbs cddone, 294539213Sgibbs MSG_SIMPLE_Q_TAG, 294639213Sgibbs rcap_buf, 294739213Sgibbs SSD_FULL_SIZE, 294839213Sgibbs /* timeout */20000); 294939213Sgibbs 295074840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 295174840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 295239213Sgibbs 295339213Sgibbs xpt_release_ccb(ccb); 295439213Sgibbs 295539213Sgibbs softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 295639213Sgibbs softc->params.blksize = scsi_4btoul(rcap_buf->length); 2957203931Smav /* Make sure we got at least some block size. */ 2958203931Smav if (error == 0 && softc->params.blksize == 0) 2959203931Smav error = EIO; 296060806Sjoerg /* 296160806Sjoerg * SCSI-3 mandates that the reported blocksize shall be 2048. 296260806Sjoerg * Older drives sometimes report funny values, trim it down to 296360806Sjoerg * 2048, or other parts of the kernel will get confused. 296460806Sjoerg * 296560806Sjoerg * XXX we leave drives alone that might report 512 bytes, as 296660806Sjoerg * well as drives reporting more weird sizes like perhaps 4K. 296760806Sjoerg */ 296860806Sjoerg if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 296960806Sjoerg softc->params.blksize = 2048; 297039213Sgibbs 2971169562Sscottl free(rcap_buf, M_SCSICD); 297239213Sgibbs *size = softc->params.disksize; 297339213Sgibbs 297439213Sgibbs return (error); 297539213Sgibbs 297639213Sgibbs} 297739213Sgibbs 297839213Sgibbsstatic int 2979111206Skencd6byteworkaround(union ccb *ccb) 2980111206Sken{ 2981111206Sken u_int8_t *cdb; 2982111206Sken struct cam_periph *periph; 2983111206Sken struct cd_softc *softc; 2984111206Sken struct cd_mode_params *params; 2985111206Sken int frozen, found; 2986111206Sken 2987111206Sken periph = xpt_path_periph(ccb->ccb_h.path); 2988111206Sken softc = (struct cd_softc *)periph->softc; 2989111206Sken 2990111206Sken cdb = ccb->csio.cdb_io.cdb_bytes; 2991111206Sken 2992111206Sken if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2993111206Sken || ((cdb[0] != MODE_SENSE_6) 2994111206Sken && (cdb[0] != MODE_SELECT_6))) 2995111206Sken return (0); 2996111206Sken 2997111206Sken /* 2998111206Sken * Because there is no convenient place to stash the overall 2999111206Sken * cd_mode_params structure pointer, we have to grab it like this. 3000111206Sken * This means that ALL MODE_SENSE and MODE_SELECT requests in the 3001111206Sken * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 3002111206Sken * 3003111206Sken * XXX It would be nice if, at some point, we could increase the 3004111206Sken * number of available peripheral private pointers. Both pointers 3005111206Sken * are currently used in most every peripheral driver. 3006111206Sken */ 3007111206Sken found = 0; 3008111206Sken 3009111206Sken STAILQ_FOREACH(params, &softc->mode_queue, links) { 3010111206Sken if (params->mode_buf == ccb->csio.data_ptr) { 3011111206Sken found = 1; 3012111206Sken break; 3013111206Sken } 3014111206Sken } 3015111206Sken 3016111206Sken /* 3017111206Sken * This shouldn't happen. All mode sense and mode select 3018111206Sken * operations in the cd(4) driver MUST go through cdgetmode() and 3019111206Sken * cdsetmode()! 3020111206Sken */ 3021111206Sken if (found == 0) { 3022164906Smjacob xpt_print(periph->path, 3023164906Smjacob "mode buffer not found in mode queue!\n"); 3024111206Sken return (0); 3025111206Sken } 3026111206Sken 3027111206Sken params->cdb_size = 10; 3028111206Sken softc->minimum_command_size = 10; 3029164906Smjacob xpt_print(ccb->ccb_h.path, 3030164906Smjacob "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3031164906Smjacob (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3032111206Sken 3033111206Sken if (cdb[0] == MODE_SENSE_6) { 3034111206Sken struct scsi_mode_sense_10 ms10; 3035111206Sken struct scsi_mode_sense_6 *ms6; 3036111206Sken int len; 3037111206Sken 3038111206Sken ms6 = (struct scsi_mode_sense_6 *)cdb; 3039111206Sken 3040111206Sken bzero(&ms10, sizeof(ms10)); 3041111206Sken ms10.opcode = MODE_SENSE_10; 3042111206Sken ms10.byte2 = ms6->byte2; 3043111206Sken ms10.page = ms6->page; 3044111206Sken 3045111206Sken /* 3046111206Sken * 10 byte mode header, block descriptor, 3047111206Sken * sizeof(union cd_pages) 3048111206Sken */ 3049111206Sken len = sizeof(struct cd_mode_data_10); 3050111206Sken ccb->csio.dxfer_len = len; 3051111206Sken 3052111206Sken scsi_ulto2b(len, ms10.length); 3053111206Sken ms10.control = ms6->control; 3054111206Sken bcopy(&ms10, cdb, 10); 3055111206Sken ccb->csio.cdb_len = 10; 3056111206Sken } else { 3057111206Sken struct scsi_mode_select_10 ms10; 3058111206Sken struct scsi_mode_select_6 *ms6; 3059111206Sken struct scsi_mode_header_6 *header6; 3060111206Sken struct scsi_mode_header_10 *header10; 3061111206Sken struct scsi_mode_page_header *page_header; 3062111206Sken int blk_desc_len, page_num, page_size, len; 3063111206Sken 3064111206Sken ms6 = (struct scsi_mode_select_6 *)cdb; 3065111206Sken 3066111206Sken bzero(&ms10, sizeof(ms10)); 3067111206Sken ms10.opcode = MODE_SELECT_10; 3068111206Sken ms10.byte2 = ms6->byte2; 3069111206Sken 3070111206Sken header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3071111206Sken header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3072111206Sken 3073111206Sken page_header = find_mode_page_6(header6); 3074111206Sken page_num = page_header->page_code; 3075111206Sken 3076111206Sken blk_desc_len = header6->blk_desc_len; 3077111206Sken 3078111206Sken page_size = cdgetpagesize(page_num); 3079111206Sken 3080111206Sken if (page_size != (page_header->page_length + 3081111206Sken sizeof(*page_header))) 3082111206Sken page_size = page_header->page_length + 3083111206Sken sizeof(*page_header); 3084111206Sken 3085111206Sken len = sizeof(*header10) + blk_desc_len + page_size; 3086111206Sken 3087111206Sken len = min(params->alloc_len, len); 3088111206Sken 3089111206Sken /* 3090111206Sken * Since the 6 byte parameter header is shorter than the 10 3091111206Sken * byte parameter header, we need to copy the actual mode 3092111206Sken * page data, and the block descriptor, if any, so things wind 3093111206Sken * up in the right place. The regions will overlap, but 3094111206Sken * bcopy() does the right thing. 3095111206Sken */ 3096111206Sken bcopy(params->mode_buf + sizeof(*header6), 3097111206Sken params->mode_buf + sizeof(*header10), 3098111206Sken len - sizeof(*header10)); 3099111206Sken 3100111206Sken /* Make sure these fields are set correctly. */ 3101111206Sken scsi_ulto2b(0, header10->data_length); 3102111206Sken header10->medium_type = 0; 3103111206Sken scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3104111206Sken 3105111206Sken ccb->csio.dxfer_len = len; 3106111206Sken 3107111206Sken scsi_ulto2b(len, ms10.length); 3108111206Sken ms10.control = ms6->control; 3109111206Sken bcopy(&ms10, cdb, 10); 3110111206Sken ccb->csio.cdb_len = 10; 3111111206Sken } 3112111206Sken 3113111206Sken frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3114111206Sken ccb->ccb_h.status = CAM_REQUEUE_REQ; 3115111206Sken xpt_action(ccb); 3116111206Sken if (frozen) { 3117111206Sken cam_release_devq(ccb->ccb_h.path, 3118111206Sken /*relsim_flags*/0, 3119111206Sken /*openings*/0, 3120111206Sken /*timeout*/0, 3121111206Sken /*getcount_only*/0); 3122111206Sken } 3123111206Sken 3124111206Sken return (ERESTART); 3125111206Sken} 3126111206Sken 3127111206Skenstatic int 312839213Sgibbscderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 312939213Sgibbs{ 313039213Sgibbs struct cd_softc *softc; 313139213Sgibbs struct cam_periph *periph; 3132237478Smav int error, error_code, sense_key, asc, ascq; 313339213Sgibbs 313439213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 313539213Sgibbs softc = (struct cd_softc *)periph->softc; 313639213Sgibbs 3137111206Sken error = 0; 3138111206Sken 313939514Sgibbs /* 3140111206Sken * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3141111206Sken * CDB comes back with this particular error, try transforming it 3142111206Sken * into the 10 byte version. 3143111206Sken */ 3144111206Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3145111206Sken error = cd6byteworkaround(ccb); 3146237478Smav } else if (scsi_extract_sense_ccb(ccb, 3147237478Smav &error_code, &sense_key, &asc, &ascq)) { 3148111206Sken if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3149237478Smav error = cd6byteworkaround(ccb); 3150111206Sken } 3151111206Sken 3152111206Sken if (error == ERESTART) 3153111206Sken return (error); 3154111206Sken 3155111206Sken /* 315639514Sgibbs * XXX 315739514Sgibbs * Until we have a better way of doing pack validation, 315839514Sgibbs * don't treat UAs as errors. 315939514Sgibbs */ 316039514Sgibbs sense_flags |= SF_RETRY_UA; 316139213Sgibbs return (cam_periph_error(ccb, cam_flags, sense_flags, 316239213Sgibbs &softc->saved_ccb)); 316339213Sgibbs} 316439213Sgibbs 316539213Sgibbs/* 316639213Sgibbs * Read table of contents 316739213Sgibbs */ 316839213Sgibbsstatic int 316939213Sgibbscdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3170111206Sken u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 317139213Sgibbs{ 317239213Sgibbs struct scsi_read_toc *scsi_cmd; 317339213Sgibbs u_int32_t ntoc; 317439213Sgibbs struct ccb_scsiio *csio; 317539213Sgibbs union ccb *ccb; 317639213Sgibbs int error; 317739213Sgibbs 317839213Sgibbs ntoc = len; 317939213Sgibbs error = 0; 318039213Sgibbs 3181198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 318239213Sgibbs 318339213Sgibbs csio = &ccb->csio; 318439213Sgibbs 318539213Sgibbs cam_fill_csio(csio, 3186203108Smav /* retries */ cd_retry_count, 318739213Sgibbs /* cbfcnp */ cddone, 318839213Sgibbs /* flags */ CAM_DIR_IN, 318939213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 3190111206Sken /* data_ptr */ data, 319139213Sgibbs /* dxfer_len */ len, 319239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 319339213Sgibbs sizeof(struct scsi_read_toc), 319439213Sgibbs /* timeout */ 50000); 319539213Sgibbs 319639213Sgibbs scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 319739213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 319839213Sgibbs 319939213Sgibbs if (mode == CD_MSF_FORMAT) 320039213Sgibbs scsi_cmd->byte2 |= CD_MSF; 320139213Sgibbs scsi_cmd->from_track = start; 320239213Sgibbs /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 320339213Sgibbs scsi_cmd->data_len[0] = (ntoc) >> 8; 320439213Sgibbs scsi_cmd->data_len[1] = (ntoc) & 0xff; 320539213Sgibbs 320639213Sgibbs scsi_cmd->op_code = READ_TOC; 320739213Sgibbs 320874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3209111206Sken /*sense_flags*/SF_RETRY_UA | sense_flags); 321039213Sgibbs 321139213Sgibbs xpt_release_ccb(ccb); 321239213Sgibbs 321339213Sgibbs return(error); 321439213Sgibbs} 321539213Sgibbs 321639213Sgibbsstatic int 321739213Sgibbscdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 321839213Sgibbs u_int32_t format, int track, 321939213Sgibbs struct cd_sub_channel_info *data, u_int32_t len) 322039213Sgibbs{ 322139213Sgibbs struct scsi_read_subchannel *scsi_cmd; 322239213Sgibbs struct ccb_scsiio *csio; 322339213Sgibbs union ccb *ccb; 322439213Sgibbs int error; 322539213Sgibbs 322639213Sgibbs error = 0; 322739213Sgibbs 3228198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 322939213Sgibbs 323039213Sgibbs csio = &ccb->csio; 323139213Sgibbs 323239213Sgibbs cam_fill_csio(csio, 3233203108Smav /* retries */ cd_retry_count, 323439213Sgibbs /* cbfcnp */ cddone, 323539213Sgibbs /* flags */ CAM_DIR_IN, 323639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 323739213Sgibbs /* data_ptr */ (u_int8_t *)data, 323839213Sgibbs /* dxfer_len */ len, 323939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 324039213Sgibbs sizeof(struct scsi_read_subchannel), 324139213Sgibbs /* timeout */ 50000); 324239213Sgibbs 324339213Sgibbs scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 324439213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 324539213Sgibbs 324639213Sgibbs scsi_cmd->op_code = READ_SUBCHANNEL; 324739213Sgibbs if (mode == CD_MSF_FORMAT) 324839213Sgibbs scsi_cmd->byte1 |= CD_MSF; 324939213Sgibbs scsi_cmd->byte2 = SRS_SUBQ; 325039213Sgibbs scsi_cmd->subchan_format = format; 325139213Sgibbs scsi_cmd->track = track; 325239213Sgibbs scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 325339213Sgibbs scsi_cmd->control = 0; 325439213Sgibbs 325574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 325674840Sken /*sense_flags*/SF_RETRY_UA); 325739213Sgibbs 325839213Sgibbs xpt_release_ccb(ccb); 325939213Sgibbs 326039213Sgibbs return(error); 326139213Sgibbs} 326239213Sgibbs 326339213Sgibbs 3264111206Sken/* 3265111206Sken * All MODE_SENSE requests in the cd(4) driver MUST go through this 3266111206Sken * routine. See comments in cd6byteworkaround() for details. 3267111206Sken */ 326839213Sgibbsstatic int 3269111206Skencdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3270111206Sken u_int32_t page) 327139213Sgibbs{ 3272111206Sken struct ccb_scsiio *csio; 3273111206Sken struct cd_softc *softc; 327439213Sgibbs union ccb *ccb; 3275111206Sken int param_len; 327639213Sgibbs int error; 327739213Sgibbs 3278111206Sken softc = (struct cd_softc *)periph->softc; 3279111206Sken 3280198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 328139213Sgibbs 328239213Sgibbs csio = &ccb->csio; 328339213Sgibbs 3284111206Sken data->cdb_size = softc->minimum_command_size; 3285111206Sken if (data->cdb_size < 10) 3286111206Sken param_len = sizeof(struct cd_mode_data); 3287111206Sken else 3288111206Sken param_len = sizeof(struct cd_mode_data_10); 328939213Sgibbs 3290111206Sken /* Don't say we've got more room than we actually allocated */ 3291111206Sken param_len = min(param_len, data->alloc_len); 329239213Sgibbs 3293111206Sken scsi_mode_sense_len(csio, 3294203108Smav /* retries */ cd_retry_count, 3295111206Sken /* cbfcnp */ cddone, 3296111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3297111206Sken /* dbd */ 0, 3298111206Sken /* page_code */ SMS_PAGE_CTRL_CURRENT, 3299111206Sken /* page */ page, 3300111206Sken /* param_buf */ data->mode_buf, 3301111206Sken /* param_len */ param_len, 3302111206Sken /* minimum_cmd_size */ softc->minimum_command_size, 3303111206Sken /* sense_len */ SSD_FULL_SIZE, 3304111206Sken /* timeout */ 50000); 330539213Sgibbs 3306111206Sken /* 3307111206Sken * It would be nice not to have to do this, but there's no 3308111206Sken * available pointer in the CCB that would allow us to stuff the 3309111206Sken * mode params structure in there and retrieve it in 3310111206Sken * cd6byteworkaround(), so we can set the cdb size. The cdb size 3311111206Sken * lets the caller know what CDB size we ended up using, so they 3312111206Sken * can find the actual mode page offset. 3313111206Sken */ 3314111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3315111206Sken 331674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 331774840Sken /*sense_flags*/SF_RETRY_UA); 331839213Sgibbs 331939213Sgibbs xpt_release_ccb(ccb); 332039213Sgibbs 3321111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3322111206Sken 3323111206Sken /* 3324111206Sken * This is a bit of belt-and-suspenders checking, but if we run 3325111206Sken * into a situation where the target sends back multiple block 3326111206Sken * descriptors, we might not have enough space in the buffer to 3327111206Sken * see the whole mode page. Better to return an error than 3328111206Sken * potentially access memory beyond our malloced region. 3329111206Sken */ 3330111206Sken if (error == 0) { 3331111206Sken u_int32_t data_len; 3332111206Sken 3333111206Sken if (data->cdb_size == 10) { 3334111206Sken struct scsi_mode_header_10 *hdr10; 3335111206Sken 3336111206Sken hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3337111206Sken data_len = scsi_2btoul(hdr10->data_length); 3338111206Sken data_len += sizeof(hdr10->data_length); 3339111206Sken } else { 3340111206Sken struct scsi_mode_header_6 *hdr6; 3341111206Sken 3342111206Sken hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3343111206Sken data_len = hdr6->data_length; 3344111206Sken data_len += sizeof(hdr6->data_length); 3345111206Sken } 3346111206Sken 3347111206Sken /* 3348111206Sken * Complain if there is more mode data available than we 3349111206Sken * allocated space for. This could potentially happen if 3350111206Sken * we miscalculated the page length for some reason, if the 3351111206Sken * drive returns multiple block descriptors, or if it sets 3352111206Sken * the data length incorrectly. 3353111206Sken */ 3354111206Sken if (data_len > data->alloc_len) { 3355164906Smjacob xpt_print(periph->path, "allocated modepage %d length " 3356164906Smjacob "%d < returned length %d\n", page, data->alloc_len, 3357164906Smjacob data_len); 3358111206Sken error = ENOSPC; 3359111206Sken } 3360111206Sken } 3361111206Sken return (error); 336239213Sgibbs} 336339213Sgibbs 3364111206Sken/* 3365111206Sken * All MODE_SELECT requests in the cd(4) driver MUST go through this 3366111206Sken * routine. See comments in cd6byteworkaround() for details. 3367111206Sken */ 336839213Sgibbsstatic int 3369111206Skencdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 337039213Sgibbs{ 3371111206Sken struct ccb_scsiio *csio; 3372111206Sken struct cd_softc *softc; 337339213Sgibbs union ccb *ccb; 3374111206Sken int cdb_size, param_len; 337539213Sgibbs int error; 337639213Sgibbs 3377111206Sken softc = (struct cd_softc *)periph->softc; 3378111206Sken 3379198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 338039213Sgibbs 338139213Sgibbs csio = &ccb->csio; 338239213Sgibbs 338339213Sgibbs error = 0; 338439213Sgibbs 338539213Sgibbs /* 3386111206Sken * If the data is formatted for the 10 byte version of the mode 3387111206Sken * select parameter list, we need to use the 10 byte CDB. 3388111206Sken * Otherwise, we use whatever the stored minimum command size. 338939213Sgibbs */ 3390111206Sken if (data->cdb_size == 10) 3391111206Sken cdb_size = data->cdb_size; 3392111206Sken else 3393111206Sken cdb_size = softc->minimum_command_size; 339439213Sgibbs 3395111206Sken if (cdb_size >= 10) { 3396111206Sken struct scsi_mode_header_10 *mode_header; 3397111206Sken u_int32_t data_len; 3398111206Sken 3399111206Sken mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3400111206Sken 3401111206Sken data_len = scsi_2btoul(mode_header->data_length); 3402111206Sken 3403111206Sken scsi_ulto2b(0, mode_header->data_length); 3404111206Sken /* 3405111206Sken * SONY drives do not allow a mode select with a medium_type 3406111206Sken * value that has just been returned by a mode sense; use a 3407111206Sken * medium_type of 0 (Default) instead. 3408111206Sken */ 3409111206Sken mode_header->medium_type = 0; 3410111206Sken 3411111206Sken /* 3412111206Sken * Pass back whatever the drive passed to us, plus the size 3413111206Sken * of the data length field. 3414111206Sken */ 3415111206Sken param_len = data_len + sizeof(mode_header->data_length); 3416111206Sken 3417111206Sken } else { 3418111206Sken struct scsi_mode_header_6 *mode_header; 3419111206Sken 3420111206Sken mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3421111206Sken 3422111206Sken param_len = mode_header->data_length + 1; 3423111206Sken 3424111206Sken mode_header->data_length = 0; 3425111206Sken /* 3426111206Sken * SONY drives do not allow a mode select with a medium_type 3427111206Sken * value that has just been returned by a mode sense; use a 3428111206Sken * medium_type of 0 (Default) instead. 3429111206Sken */ 3430111206Sken mode_header->medium_type = 0; 3431111206Sken } 3432111206Sken 3433111206Sken /* Don't say we've got more room than we actually allocated */ 3434111206Sken param_len = min(param_len, data->alloc_len); 3435111206Sken 3436111206Sken scsi_mode_select_len(csio, 3437203108Smav /* retries */ cd_retry_count, 3438111206Sken /* cbfcnp */ cddone, 3439111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3440111206Sken /* scsi_page_fmt */ 1, 3441111206Sken /* save_pages */ 0, 3442111206Sken /* param_buf */ data->mode_buf, 3443111206Sken /* param_len */ param_len, 3444111206Sken /* minimum_cmd_size */ cdb_size, 3445111206Sken /* sense_len */ SSD_FULL_SIZE, 3446111206Sken /* timeout */ 50000); 3447111206Sken 3448111206Sken /* See comments in cdgetmode() and cd6byteworkaround(). */ 3449111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3450111206Sken 345174840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 345274840Sken /*sense_flags*/SF_RETRY_UA); 345339213Sgibbs 345439213Sgibbs xpt_release_ccb(ccb); 345539213Sgibbs 3456111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3457111206Sken 3458111206Sken return (error); 345939213Sgibbs} 346039213Sgibbs 346139213Sgibbs 346239213Sgibbsstatic int 346339213Sgibbscdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 346439213Sgibbs{ 346539531Sken struct ccb_scsiio *csio; 346639213Sgibbs union ccb *ccb; 346739213Sgibbs int error; 346839531Sken u_int8_t cdb_len; 346939213Sgibbs 347039213Sgibbs error = 0; 3471198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 347239213Sgibbs csio = &ccb->csio; 347339531Sken /* 347439531Sken * Use the smallest possible command to perform the operation. 347539531Sken */ 347639531Sken if ((len & 0xffff0000) == 0) { 347739531Sken /* 347839531Sken * We can fit in a 10 byte cdb. 347939531Sken */ 348039531Sken struct scsi_play_10 *scsi_cmd; 348139213Sgibbs 348239531Sken scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 348339531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 348439531Sken scsi_cmd->op_code = PLAY_10; 348539531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 348639531Sken scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 348739531Sken cdb_len = sizeof(*scsi_cmd); 348839531Sken } else { 348939531Sken struct scsi_play_12 *scsi_cmd; 349039213Sgibbs 349139531Sken scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 349239531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 349339531Sken scsi_cmd->op_code = PLAY_12; 349439531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 349539531Sken scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 349639531Sken cdb_len = sizeof(*scsi_cmd); 349739531Sken } 349839531Sken cam_fill_csio(csio, 3499203108Smav /*retries*/ cd_retry_count, 350039531Sken cddone, 350139531Sken /*flags*/CAM_DIR_NONE, 350239531Sken MSG_SIMPLE_Q_TAG, 350339531Sken /*dataptr*/NULL, 350439531Sken /*datalen*/0, 350539531Sken /*sense_len*/SSD_FULL_SIZE, 350639531Sken cdb_len, 350739531Sken /*timeout*/50 * 1000); 350839213Sgibbs 350974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 351074840Sken /*sense_flags*/SF_RETRY_UA); 351139213Sgibbs 351239213Sgibbs xpt_release_ccb(ccb); 351339213Sgibbs 351439213Sgibbs return(error); 351539213Sgibbs} 351639213Sgibbs 351739213Sgibbsstatic int 351839213Sgibbscdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 351939213Sgibbs u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 352039213Sgibbs{ 352139213Sgibbs struct scsi_play_msf *scsi_cmd; 352239213Sgibbs struct ccb_scsiio *csio; 352339213Sgibbs union ccb *ccb; 352439213Sgibbs int error; 352539213Sgibbs 352639213Sgibbs error = 0; 352739213Sgibbs 3528198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 352939213Sgibbs 353039213Sgibbs csio = &ccb->csio; 353139213Sgibbs 353239213Sgibbs cam_fill_csio(csio, 3533203108Smav /* retries */ cd_retry_count, 353439213Sgibbs /* cbfcnp */ cddone, 353539531Sken /* flags */ CAM_DIR_NONE, 353639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 353739213Sgibbs /* data_ptr */ NULL, 353839213Sgibbs /* dxfer_len */ 0, 353939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 354039213Sgibbs sizeof(struct scsi_play_msf), 354139213Sgibbs /* timeout */ 50000); 354239213Sgibbs 354339213Sgibbs scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 354439213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 354539213Sgibbs 354639213Sgibbs scsi_cmd->op_code = PLAY_MSF; 354739213Sgibbs scsi_cmd->start_m = startm; 354839213Sgibbs scsi_cmd->start_s = starts; 354939213Sgibbs scsi_cmd->start_f = startf; 355039213Sgibbs scsi_cmd->end_m = endm; 355139213Sgibbs scsi_cmd->end_s = ends; 355239213Sgibbs scsi_cmd->end_f = endf; 355339213Sgibbs 355474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 355574840Sken /*sense_flags*/SF_RETRY_UA); 355639213Sgibbs 355739213Sgibbs xpt_release_ccb(ccb); 355839213Sgibbs 355939213Sgibbs return(error); 356039213Sgibbs} 356139213Sgibbs 356239213Sgibbs 356339213Sgibbsstatic int 356439213Sgibbscdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 356539213Sgibbs u_int32_t etrack, u_int32_t eindex) 356639213Sgibbs{ 356739213Sgibbs struct scsi_play_track *scsi_cmd; 356839213Sgibbs struct ccb_scsiio *csio; 356939213Sgibbs union ccb *ccb; 357039213Sgibbs int error; 357139213Sgibbs 357239213Sgibbs error = 0; 357339213Sgibbs 3574198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 357539213Sgibbs 357639213Sgibbs csio = &ccb->csio; 357739213Sgibbs 357839213Sgibbs cam_fill_csio(csio, 3579203108Smav /* retries */ cd_retry_count, 358039213Sgibbs /* cbfcnp */ cddone, 358139531Sken /* flags */ CAM_DIR_NONE, 358239213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 358339213Sgibbs /* data_ptr */ NULL, 358439213Sgibbs /* dxfer_len */ 0, 358539213Sgibbs /* sense_len */ SSD_FULL_SIZE, 358639213Sgibbs sizeof(struct scsi_play_track), 358739213Sgibbs /* timeout */ 50000); 358839213Sgibbs 358939213Sgibbs scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 359039213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 359139213Sgibbs 359239213Sgibbs scsi_cmd->op_code = PLAY_TRACK; 359339213Sgibbs scsi_cmd->start_track = strack; 359439213Sgibbs scsi_cmd->start_index = sindex; 359539213Sgibbs scsi_cmd->end_track = etrack; 359639213Sgibbs scsi_cmd->end_index = eindex; 359739213Sgibbs 359874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 359974840Sken /*sense_flags*/SF_RETRY_UA); 360039213Sgibbs 360139213Sgibbs xpt_release_ccb(ccb); 360239213Sgibbs 360339213Sgibbs return(error); 360439213Sgibbs} 360539213Sgibbs 360639213Sgibbsstatic int 360739213Sgibbscdpause(struct cam_periph *periph, u_int32_t go) 360839213Sgibbs{ 360939213Sgibbs struct scsi_pause *scsi_cmd; 361039213Sgibbs struct ccb_scsiio *csio; 361139213Sgibbs union ccb *ccb; 361239213Sgibbs int error; 361339213Sgibbs 361439213Sgibbs error = 0; 361539213Sgibbs 3616198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 361739213Sgibbs 361839213Sgibbs csio = &ccb->csio; 361939213Sgibbs 362039213Sgibbs cam_fill_csio(csio, 3621203108Smav /* retries */ cd_retry_count, 362239213Sgibbs /* cbfcnp */ cddone, 362339531Sken /* flags */ CAM_DIR_NONE, 362439213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 362539213Sgibbs /* data_ptr */ NULL, 362639213Sgibbs /* dxfer_len */ 0, 362739213Sgibbs /* sense_len */ SSD_FULL_SIZE, 362839213Sgibbs sizeof(struct scsi_pause), 362939213Sgibbs /* timeout */ 50000); 363039213Sgibbs 363139213Sgibbs scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 363239213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 363339213Sgibbs 363439213Sgibbs scsi_cmd->op_code = PAUSE; 363539213Sgibbs scsi_cmd->resume = go; 363639213Sgibbs 363774840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 363874840Sken /*sense_flags*/SF_RETRY_UA); 363939213Sgibbs 364039213Sgibbs xpt_release_ccb(ccb); 364139213Sgibbs 364239213Sgibbs return(error); 364339213Sgibbs} 364439213Sgibbs 364539213Sgibbsstatic int 3646111206Skencdstartunit(struct cam_periph *periph, int load) 364739213Sgibbs{ 364839213Sgibbs union ccb *ccb; 364939213Sgibbs int error; 365039213Sgibbs 365139213Sgibbs error = 0; 365239213Sgibbs 3653198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 365439213Sgibbs 365539213Sgibbs scsi_start_stop(&ccb->csio, 3656203108Smav /* retries */ cd_retry_count, 365739213Sgibbs /* cbfcnp */ cddone, 365839213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 365939213Sgibbs /* start */ TRUE, 3660111206Sken /* load_eject */ load, 366139213Sgibbs /* immediate */ FALSE, 366239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 366339213Sgibbs /* timeout */ 50000); 366439213Sgibbs 366574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 366674840Sken /*sense_flags*/SF_RETRY_UA); 366739213Sgibbs 366839213Sgibbs xpt_release_ccb(ccb); 366939213Sgibbs 367039213Sgibbs return(error); 367139213Sgibbs} 367239213Sgibbs 367339213Sgibbsstatic int 367439213Sgibbscdstopunit(struct cam_periph *periph, u_int32_t eject) 367539213Sgibbs{ 367639213Sgibbs union ccb *ccb; 367739213Sgibbs int error; 367839213Sgibbs 367939213Sgibbs error = 0; 368039213Sgibbs 3681198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 368239213Sgibbs 368339213Sgibbs scsi_start_stop(&ccb->csio, 3684203108Smav /* retries */ cd_retry_count, 368539213Sgibbs /* cbfcnp */ cddone, 368639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 368739213Sgibbs /* start */ FALSE, 368839213Sgibbs /* load_eject */ eject, 368939213Sgibbs /* immediate */ FALSE, 369039213Sgibbs /* sense_len */ SSD_FULL_SIZE, 369139213Sgibbs /* timeout */ 50000); 369239213Sgibbs 369374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 369474840Sken /*sense_flags*/SF_RETRY_UA); 369539213Sgibbs 369639213Sgibbs xpt_release_ccb(ccb); 369739213Sgibbs 369839213Sgibbs return(error); 369939213Sgibbs} 370060422Sken 370160422Skenstatic int 3702105421Snjlcdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3703105421Snjl{ 3704105421Snjl struct scsi_set_speed *scsi_cmd; 3705105421Snjl struct ccb_scsiio *csio; 3706105421Snjl union ccb *ccb; 3707105421Snjl int error; 3708105421Snjl 3709105421Snjl error = 0; 3710198382Smav ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3711105421Snjl csio = &ccb->csio; 3712105421Snjl 3713107193Snjl /* Preserve old behavior: units in multiples of CDROM speed */ 3714107193Snjl if (rdspeed < 177) 3715107193Snjl rdspeed *= 177; 3716107193Snjl if (wrspeed < 177) 3717107193Snjl wrspeed *= 177; 3718107193Snjl 3719105421Snjl cam_fill_csio(csio, 3720203108Smav /* retries */ cd_retry_count, 3721105421Snjl /* cbfcnp */ cddone, 3722105421Snjl /* flags */ CAM_DIR_NONE, 3723105421Snjl /* tag_action */ MSG_SIMPLE_Q_TAG, 3724105421Snjl /* data_ptr */ NULL, 3725105421Snjl /* dxfer_len */ 0, 3726105421Snjl /* sense_len */ SSD_FULL_SIZE, 3727105421Snjl sizeof(struct scsi_set_speed), 3728105421Snjl /* timeout */ 50000); 3729105421Snjl 3730105421Snjl scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3731105421Snjl bzero(scsi_cmd, sizeof(*scsi_cmd)); 3732105421Snjl 3733105421Snjl scsi_cmd->opcode = SET_CD_SPEED; 3734105421Snjl scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3735105421Snjl scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3736105421Snjl 3737105421Snjl error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3738105421Snjl /*sense_flags*/SF_RETRY_UA); 3739105421Snjl 3740105421Snjl xpt_release_ccb(ccb); 3741105421Snjl 3742105421Snjl return(error); 3743105421Snjl} 3744105421Snjl 3745105421Snjlstatic int 374660422Skencdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 374760422Sken{ 374860422Sken union ccb *ccb; 374960422Sken u_int8_t *databuf; 375060422Sken u_int32_t lba; 375160422Sken int error; 375260422Sken int length; 375360422Sken 375460422Sken error = 0; 375560422Sken databuf = NULL; 375660422Sken lba = 0; 375760422Sken 375860422Sken switch (authinfo->format) { 375960422Sken case DVD_REPORT_AGID: 376060422Sken length = sizeof(struct scsi_report_key_data_agid); 376160422Sken break; 376260422Sken case DVD_REPORT_CHALLENGE: 376360422Sken length = sizeof(struct scsi_report_key_data_challenge); 376460422Sken break; 376560422Sken case DVD_REPORT_KEY1: 376660422Sken length = sizeof(struct scsi_report_key_data_key1_key2); 376760422Sken break; 376860422Sken case DVD_REPORT_TITLE_KEY: 376960422Sken length = sizeof(struct scsi_report_key_data_title); 377060422Sken /* The lba field is only set for the title key */ 377160422Sken lba = authinfo->lba; 377260422Sken break; 377360422Sken case DVD_REPORT_ASF: 377460422Sken length = sizeof(struct scsi_report_key_data_asf); 377560422Sken break; 377660422Sken case DVD_REPORT_RPC: 377760422Sken length = sizeof(struct scsi_report_key_data_rpc); 377860422Sken break; 377960422Sken case DVD_INVALIDATE_AGID: 378060422Sken length = 0; 378160422Sken break; 378260422Sken default: 3783200036Sscottl return (EINVAL); 378460422Sken } 378560422Sken 378660422Sken if (length != 0) { 3787111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 378860422Sken } else 378960422Sken databuf = NULL; 379060422Sken 3791200036Sscottl cam_periph_lock(periph); 3792200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 379360422Sken 379460422Sken scsi_report_key(&ccb->csio, 3795203108Smav /* retries */ cd_retry_count, 379660422Sken /* cbfcnp */ cddone, 379760422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 379860422Sken /* lba */ lba, 379960422Sken /* agid */ authinfo->agid, 380060422Sken /* key_format */ authinfo->format, 380160422Sken /* data_ptr */ databuf, 380260422Sken /* dxfer_len */ length, 380360422Sken /* sense_len */ SSD_FULL_SIZE, 380460422Sken /* timeout */ 50000); 380560422Sken 380674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 380774840Sken /*sense_flags*/SF_RETRY_UA); 380860422Sken 380960422Sken if (error != 0) 381060422Sken goto bailout; 381160422Sken 381260422Sken if (ccb->csio.resid != 0) { 3813164906Smjacob xpt_print(periph->path, "warning, residual for report key " 3814164906Smjacob "command is %d\n", ccb->csio.resid); 381560422Sken } 381660422Sken 381760422Sken switch(authinfo->format) { 381860422Sken case DVD_REPORT_AGID: { 381960422Sken struct scsi_report_key_data_agid *agid_data; 382060422Sken 382160422Sken agid_data = (struct scsi_report_key_data_agid *)databuf; 382260422Sken 382360422Sken authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 382460422Sken RKD_AGID_SHIFT; 382560422Sken break; 382660422Sken } 382760422Sken case DVD_REPORT_CHALLENGE: { 382860422Sken struct scsi_report_key_data_challenge *chal_data; 382960422Sken 383060422Sken chal_data = (struct scsi_report_key_data_challenge *)databuf; 383160422Sken 383260422Sken bcopy(chal_data->challenge_key, authinfo->keychal, 383360422Sken min(sizeof(chal_data->challenge_key), 383460422Sken sizeof(authinfo->keychal))); 383560422Sken break; 383660422Sken } 383760422Sken case DVD_REPORT_KEY1: { 383860422Sken struct scsi_report_key_data_key1_key2 *key1_data; 383960422Sken 384060422Sken key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 384160422Sken 384260422Sken bcopy(key1_data->key1, authinfo->keychal, 384360422Sken min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 384460422Sken break; 384560422Sken } 384660422Sken case DVD_REPORT_TITLE_KEY: { 384760422Sken struct scsi_report_key_data_title *title_data; 384860422Sken 384960422Sken title_data = (struct scsi_report_key_data_title *)databuf; 385060422Sken 385160422Sken authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 385260422Sken RKD_TITLE_CPM_SHIFT; 385360422Sken authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 385460422Sken RKD_TITLE_CP_SEC_SHIFT; 385560422Sken authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 385660422Sken RKD_TITLE_CMGS_SHIFT; 385760422Sken bcopy(title_data->title_key, authinfo->keychal, 385860422Sken min(sizeof(title_data->title_key), 385960422Sken sizeof(authinfo->keychal))); 386060422Sken break; 386160422Sken } 386260422Sken case DVD_REPORT_ASF: { 386360422Sken struct scsi_report_key_data_asf *asf_data; 386460422Sken 386560422Sken asf_data = (struct scsi_report_key_data_asf *)databuf; 386660422Sken 386760422Sken authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 386860422Sken break; 386960422Sken } 387060422Sken case DVD_REPORT_RPC: { 387160422Sken struct scsi_report_key_data_rpc *rpc_data; 387260422Sken 387360422Sken rpc_data = (struct scsi_report_key_data_rpc *)databuf; 387460422Sken 387560422Sken authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 387660422Sken RKD_RPC_TYPE_SHIFT; 387760422Sken authinfo->vend_rsts = 387860422Sken (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 387960422Sken RKD_RPC_VENDOR_RESET_SHIFT; 388060422Sken authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 388171752Sken authinfo->region = rpc_data->region_mask; 388271752Sken authinfo->rpc_scheme = rpc_data->rpc_scheme1; 388360422Sken break; 388460422Sken } 388560422Sken case DVD_INVALIDATE_AGID: 388660422Sken break; 388760422Sken default: 388860422Sken /* This should be impossible, since we checked above */ 388960422Sken error = EINVAL; 389060422Sken goto bailout; 389160422Sken break; /* NOTREACHED */ 389260422Sken } 3893200036Sscottl 389460422Skenbailout: 3895200036Sscottl xpt_release_ccb(ccb); 3896200036Sscottl cam_periph_unlock(periph); 3897200036Sscottl 389860422Sken if (databuf != NULL) 389960422Sken free(databuf, M_DEVBUF); 390060422Sken 390160422Sken return(error); 390260422Sken} 390360422Sken 390460422Skenstatic int 390560422Skencdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 390660422Sken{ 390760422Sken union ccb *ccb; 390860422Sken u_int8_t *databuf; 390960422Sken int length; 391060422Sken int error; 391160422Sken 391260422Sken error = 0; 391360422Sken databuf = NULL; 391460422Sken 391560422Sken switch(authinfo->format) { 391660422Sken case DVD_SEND_CHALLENGE: { 391760422Sken struct scsi_report_key_data_challenge *challenge_data; 391860422Sken 391960422Sken length = sizeof(*challenge_data); 392060422Sken 3921111119Simp challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 392260422Sken 392360422Sken databuf = (u_int8_t *)challenge_data; 392460422Sken 392560422Sken scsi_ulto2b(length - sizeof(challenge_data->data_len), 392660422Sken challenge_data->data_len); 392760422Sken 392860422Sken bcopy(authinfo->keychal, challenge_data->challenge_key, 392960422Sken min(sizeof(authinfo->keychal), 393060422Sken sizeof(challenge_data->challenge_key))); 393160422Sken break; 393260422Sken } 393360422Sken case DVD_SEND_KEY2: { 393460422Sken struct scsi_report_key_data_key1_key2 *key2_data; 393560422Sken 393660422Sken length = sizeof(*key2_data); 393760422Sken 3938111119Simp key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 393960422Sken 394060422Sken databuf = (u_int8_t *)key2_data; 394160422Sken 394260422Sken scsi_ulto2b(length - sizeof(key2_data->data_len), 394360422Sken key2_data->data_len); 394460422Sken 394560422Sken bcopy(authinfo->keychal, key2_data->key1, 394660422Sken min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 394760422Sken 394860422Sken break; 394960422Sken } 395060422Sken case DVD_SEND_RPC: { 395160422Sken struct scsi_send_key_data_rpc *rpc_data; 395260422Sken 395360422Sken length = sizeof(*rpc_data); 395460422Sken 3955111119Simp rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 395660422Sken 395760422Sken databuf = (u_int8_t *)rpc_data; 395860422Sken 395960422Sken scsi_ulto2b(length - sizeof(rpc_data->data_len), 396060422Sken rpc_data->data_len); 396160422Sken 396260422Sken rpc_data->region_code = authinfo->region; 396360422Sken break; 396460422Sken } 396560422Sken default: 3966200036Sscottl return (EINVAL); 396760422Sken } 396860422Sken 3969200036Sscottl cam_periph_lock(periph); 3970200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 3971200036Sscottl 397260422Sken scsi_send_key(&ccb->csio, 3973203108Smav /* retries */ cd_retry_count, 397460422Sken /* cbfcnp */ cddone, 397560422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 397660422Sken /* agid */ authinfo->agid, 397760422Sken /* key_format */ authinfo->format, 397860422Sken /* data_ptr */ databuf, 397960422Sken /* dxfer_len */ length, 398060422Sken /* sense_len */ SSD_FULL_SIZE, 398160422Sken /* timeout */ 50000); 398260422Sken 398374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 398474840Sken /*sense_flags*/SF_RETRY_UA); 398560422Sken 3986200036Sscottl xpt_release_ccb(ccb); 3987200036Sscottl cam_periph_unlock(periph); 398860422Sken 398960422Sken if (databuf != NULL) 399060422Sken free(databuf, M_DEVBUF); 399160422Sken 399260422Sken return(error); 399360422Sken} 399460422Sken 399560422Skenstatic int 399660422Skencdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 399760422Sken{ 399860422Sken union ccb *ccb; 399960422Sken u_int8_t *databuf; 400060422Sken u_int32_t address; 400160422Sken int error; 400260422Sken int length; 400360422Sken 400460422Sken error = 0; 400560422Sken databuf = NULL; 400660422Sken /* The address is reserved for many of the formats */ 400760422Sken address = 0; 400860422Sken 400960422Sken switch(dvdstruct->format) { 401060422Sken case DVD_STRUCT_PHYSICAL: 401160422Sken length = sizeof(struct scsi_read_dvd_struct_data_physical); 401260422Sken break; 401360422Sken case DVD_STRUCT_COPYRIGHT: 401460422Sken length = sizeof(struct scsi_read_dvd_struct_data_copyright); 401560422Sken break; 401660422Sken case DVD_STRUCT_DISCKEY: 401760422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 401860422Sken break; 401960422Sken case DVD_STRUCT_BCA: 402060422Sken length = sizeof(struct scsi_read_dvd_struct_data_bca); 402160422Sken break; 402260422Sken case DVD_STRUCT_MANUFACT: 402360422Sken length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 402460422Sken break; 402560422Sken case DVD_STRUCT_CMI: 4026200036Sscottl return (ENODEV); 402760422Sken case DVD_STRUCT_PROTDISCID: 402860422Sken length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 402960422Sken break; 403060422Sken case DVD_STRUCT_DISCKEYBLOCK: 403160422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 403260422Sken break; 403360422Sken case DVD_STRUCT_DDS: 403460422Sken length = sizeof(struct scsi_read_dvd_struct_data_dds); 403560422Sken break; 403660422Sken case DVD_STRUCT_MEDIUM_STAT: 403760422Sken length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 403860422Sken break; 403960422Sken case DVD_STRUCT_SPARE_AREA: 404060422Sken length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 404160422Sken break; 404260422Sken case DVD_STRUCT_RMD_LAST: 4043200036Sscottl return (ENODEV); 404460422Sken case DVD_STRUCT_RMD_RMA: 4045200036Sscottl return (ENODEV); 404660422Sken case DVD_STRUCT_PRERECORDED: 404760422Sken length = sizeof(struct scsi_read_dvd_struct_data_leadin); 404860422Sken break; 404960422Sken case DVD_STRUCT_UNIQUEID: 405060422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 405160422Sken break; 405260422Sken case DVD_STRUCT_DCB: 4053200036Sscottl return (ENODEV); 405460422Sken case DVD_STRUCT_LIST: 405560422Sken /* 405660422Sken * This is the maximum allocation length for the READ DVD 405760422Sken * STRUCTURE command. There's nothing in the MMC3 spec 405860422Sken * that indicates a limit in the amount of data that can 405960422Sken * be returned from this call, other than the limits 406060422Sken * imposed by the 2-byte length variables. 406160422Sken */ 406260422Sken length = 65535; 406360422Sken break; 406460422Sken default: 4065200036Sscottl return (EINVAL); 406660422Sken } 406760422Sken 406860422Sken if (length != 0) { 4069111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 407060422Sken } else 407160422Sken databuf = NULL; 407260422Sken 4073200036Sscottl cam_periph_lock(periph); 4074200036Sscottl ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL); 4075200036Sscottl 407660422Sken scsi_read_dvd_structure(&ccb->csio, 4077203108Smav /* retries */ cd_retry_count, 407860422Sken /* cbfcnp */ cddone, 407960422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 408060422Sken /* lba */ address, 408160422Sken /* layer_number */ dvdstruct->layer_num, 408260422Sken /* key_format */ dvdstruct->format, 408360422Sken /* agid */ dvdstruct->agid, 408460422Sken /* data_ptr */ databuf, 408560422Sken /* dxfer_len */ length, 408660422Sken /* sense_len */ SSD_FULL_SIZE, 408760422Sken /* timeout */ 50000); 408860422Sken 408974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 409074840Sken /*sense_flags*/SF_RETRY_UA); 409160422Sken 409260422Sken if (error != 0) 409360422Sken goto bailout; 409460422Sken 409560422Sken switch(dvdstruct->format) { 409660422Sken case DVD_STRUCT_PHYSICAL: { 409760422Sken struct scsi_read_dvd_struct_data_layer_desc *inlayer; 409860422Sken struct dvd_layer *outlayer; 409960422Sken struct scsi_read_dvd_struct_data_physical *phys_data; 410060422Sken 410160422Sken phys_data = 410260422Sken (struct scsi_read_dvd_struct_data_physical *)databuf; 410360422Sken inlayer = &phys_data->layer_desc; 410460422Sken outlayer = (struct dvd_layer *)&dvdstruct->data; 410560422Sken 410660422Sken dvdstruct->length = sizeof(*inlayer); 410760422Sken 410860422Sken outlayer->book_type = (inlayer->book_type_version & 410960422Sken RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 411060422Sken outlayer->book_version = (inlayer->book_type_version & 411160422Sken RDSD_BOOK_VERSION_MASK); 411260422Sken outlayer->disc_size = (inlayer->disc_size_max_rate & 411360422Sken RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 411460422Sken outlayer->max_rate = (inlayer->disc_size_max_rate & 411560422Sken RDSD_MAX_RATE_MASK); 411660422Sken outlayer->nlayers = (inlayer->layer_info & 411760422Sken RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 411860422Sken outlayer->track_path = (inlayer->layer_info & 411960422Sken RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 412060422Sken outlayer->layer_type = (inlayer->layer_info & 412160422Sken RDSD_LAYER_TYPE_MASK); 412260422Sken outlayer->linear_density = (inlayer->density & 412360422Sken RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 412460422Sken outlayer->track_density = (inlayer->density & 412560422Sken RDSD_TRACK_DENSITY_MASK); 412660422Sken outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 412760422Sken RDSD_BCA_SHIFT; 412860422Sken outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 412960422Sken outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 413060422Sken outlayer->end_sector_l0 = 413160422Sken scsi_3btoul(inlayer->end_sector_layer0); 413260422Sken break; 413360422Sken } 413460422Sken case DVD_STRUCT_COPYRIGHT: { 413560422Sken struct scsi_read_dvd_struct_data_copyright *copy_data; 413660422Sken 413760422Sken copy_data = (struct scsi_read_dvd_struct_data_copyright *) 413860422Sken databuf; 413960422Sken 414060422Sken dvdstruct->cpst = copy_data->cps_type; 414160422Sken dvdstruct->rmi = copy_data->region_info; 414260422Sken dvdstruct->length = 0; 414360422Sken 414460422Sken break; 414560422Sken } 414660422Sken default: 414760422Sken /* 414860422Sken * Tell the user what the overall length is, no matter 414960422Sken * what we can actually fit in the data buffer. 415060422Sken */ 415160422Sken dvdstruct->length = length - ccb->csio.resid - 415260422Sken sizeof(struct scsi_read_dvd_struct_data_header); 415360422Sken 415460422Sken /* 415560422Sken * But only actually copy out the smaller of what we read 415660422Sken * in or what the structure can take. 415760422Sken */ 415860422Sken bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 415960422Sken dvdstruct->data, 416060422Sken min(sizeof(dvdstruct->data), dvdstruct->length)); 416160422Sken break; 416260422Sken } 4163200036Sscottl 416460422Skenbailout: 4165200036Sscottl xpt_release_ccb(ccb); 4166200036Sscottl cam_periph_unlock(periph); 416760422Sken 416860422Sken if (databuf != NULL) 416960422Sken free(databuf, M_DEVBUF); 417060422Sken 417160422Sken return(error); 417260422Sken} 417360422Sken 417460422Skenvoid 417560422Skenscsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 417660422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 417760422Sken u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 417860422Sken u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 417960422Sken u_int8_t sense_len, u_int32_t timeout) 418060422Sken{ 418160422Sken struct scsi_report_key *scsi_cmd; 418260422Sken 418360422Sken scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 418460422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 418560422Sken scsi_cmd->opcode = REPORT_KEY; 418660422Sken scsi_ulto4b(lba, scsi_cmd->lba); 418760422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 418860422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 418960422Sken (key_format & RK_KF_KEYFORMAT_MASK); 419060422Sken 419160422Sken cam_fill_csio(csio, 419260422Sken retries, 419360422Sken cbfcnp, 419460422Sken /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 419560422Sken tag_action, 419660422Sken /*data_ptr*/ data_ptr, 419760422Sken /*dxfer_len*/ dxfer_len, 419860422Sken sense_len, 419960422Sken sizeof(*scsi_cmd), 420060422Sken timeout); 420160422Sken} 420260422Sken 420360422Skenvoid 420460422Skenscsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 420560422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 420660422Sken u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 420760422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 420860422Sken u_int32_t timeout) 420960422Sken{ 421060422Sken struct scsi_send_key *scsi_cmd; 421160422Sken 421260422Sken scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 421360422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 421460422Sken scsi_cmd->opcode = SEND_KEY; 421560422Sken 421660422Sken scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 421760422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 421860422Sken (key_format & RK_KF_KEYFORMAT_MASK); 421960422Sken 422060422Sken cam_fill_csio(csio, 422160422Sken retries, 422260422Sken cbfcnp, 422360422Sken /*flags*/ CAM_DIR_OUT, 422460422Sken tag_action, 422560422Sken /*data_ptr*/ data_ptr, 422660422Sken /*dxfer_len*/ dxfer_len, 422760422Sken sense_len, 422860422Sken sizeof(*scsi_cmd), 422960422Sken timeout); 423060422Sken} 423160422Sken 423260422Sken 423360422Skenvoid 423460422Skenscsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 423560422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 423660422Sken u_int8_t tag_action, u_int32_t address, 423760422Sken u_int8_t layer_number, u_int8_t format, u_int8_t agid, 423860422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, 423960422Sken u_int8_t sense_len, u_int32_t timeout) 424060422Sken{ 424160422Sken struct scsi_read_dvd_structure *scsi_cmd; 424260422Sken 424360422Sken scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 424460422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 424560422Sken scsi_cmd->opcode = READ_DVD_STRUCTURE; 424660422Sken 424760422Sken scsi_ulto4b(address, scsi_cmd->address); 424860422Sken scsi_cmd->layer_number = layer_number; 424960422Sken scsi_cmd->format = format; 425060422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 425160422Sken /* The AGID is the top two bits of this byte */ 425260422Sken scsi_cmd->agid = agid << 6; 425360422Sken 425460422Sken cam_fill_csio(csio, 425560422Sken retries, 425660422Sken cbfcnp, 425760422Sken /*flags*/ CAM_DIR_IN, 425860422Sken tag_action, 425960422Sken /*data_ptr*/ data_ptr, 426060422Sken /*dxfer_len*/ dxfer_len, 426160422Sken sense_len, 426260422Sken sizeof(*scsi_cmd), 426360422Sken timeout); 426460422Sken} 4265