scsi_cd.c revision 168786
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 168786 2007-04-16 19:41:14Z scottl $"); 50116162Sobrien 5140020Sken#include "opt_cd.h" 5239213Sgibbs 5339213Sgibbs#include <sys/param.h> 5439213Sgibbs#include <sys/systm.h> 5539213Sgibbs#include <sys/kernel.h> 5660041Sphk#include <sys/bio.h> 5751836Sphk#include <sys/conf.h> 5851836Sphk#include <sys/disk.h> 5939213Sgibbs#include <sys/malloc.h> 6039213Sgibbs#include <sys/cdio.h> 61105421Snjl#include <sys/cdrio.h> 6260422Sken#include <sys/dvdio.h> 6339213Sgibbs#include <sys/devicestat.h> 6439213Sgibbs#include <sys/sysctl.h> 65119708Sken#include <sys/taskqueue.h> 66120599Sphk#include <geom/geom_disk.h> 6739213Sgibbs 6839213Sgibbs#include <cam/cam.h> 6939213Sgibbs#include <cam/cam_ccb.h> 7039213Sgibbs#include <cam/cam_periph.h> 7139213Sgibbs#include <cam/cam_xpt_periph.h> 7239213Sgibbs#include <cam/cam_queue.h> 73168752Sscottl#include <cam/cam_sim.h> 7439213Sgibbs 7539213Sgibbs#include <cam/scsi/scsi_message.h> 7639213Sgibbs#include <cam/scsi/scsi_da.h> 7739213Sgibbs#include <cam/scsi/scsi_cd.h> 7839213Sgibbs 7939213Sgibbs#define LEADOUT 0xaa /* leadout toc entry */ 8039213Sgibbs 8139213Sgibbsstruct cd_params { 8239213Sgibbs u_int32_t blksize; 8339213Sgibbs u_long disksize; 8439213Sgibbs}; 8539213Sgibbs 8639213Sgibbstypedef enum { 87111206Sken CD_Q_NONE = 0x00, 88111206Sken CD_Q_NO_TOUCH = 0x01, 89111206Sken CD_Q_BCD_TRACKS = 0x02, 90111206Sken CD_Q_NO_CHANGER = 0x04, 91111206Sken CD_Q_CHANGER = 0x08, 92111206Sken CD_Q_10_BYTE_ONLY = 0x10 9339213Sgibbs} cd_quirks; 9439213Sgibbs 9539213Sgibbstypedef enum { 96120884Sthomas CD_FLAG_INVALID = 0x0001, 97120884Sthomas CD_FLAG_NEW_DISC = 0x0002, 98120884Sthomas CD_FLAG_DISC_LOCKED = 0x0004, 99120884Sthomas CD_FLAG_DISC_REMOVABLE = 0x0008, 100120884Sthomas CD_FLAG_TAGGED_QUEUING = 0x0010, 101120884Sthomas CD_FLAG_CHANGER = 0x0040, 102120884Sthomas CD_FLAG_ACTIVE = 0x0080, 103120884Sthomas CD_FLAG_SCHED_ON_COMP = 0x0100, 104120884Sthomas CD_FLAG_RETRY_UA = 0x0200, 105120884Sthomas CD_FLAG_VALID_MEDIA = 0x0400, 106120884Sthomas CD_FLAG_VALID_TOC = 0x0800, 107168752Sscottl CD_FLAG_SCTX_INIT = 0x1000, 108168752Sscottl CD_FLAG_OPEN = 0x2000 10939213Sgibbs} cd_flags; 11039213Sgibbs 11139213Sgibbstypedef enum { 11239213Sgibbs CD_CCB_PROBE = 0x01, 11339213Sgibbs CD_CCB_BUFFER_IO = 0x02, 11439213Sgibbs CD_CCB_WAITING = 0x03, 11539213Sgibbs CD_CCB_TYPE_MASK = 0x0F, 11639213Sgibbs CD_CCB_RETRY_UA = 0x10 11739213Sgibbs} cd_ccb_state; 11839213Sgibbs 11939213Sgibbstypedef enum { 12039213Sgibbs CHANGER_TIMEOUT_SCHED = 0x01, 12139213Sgibbs CHANGER_SHORT_TMOUT_SCHED = 0x02, 12239213Sgibbs CHANGER_MANUAL_CALL = 0x04, 12339213Sgibbs CHANGER_NEED_TIMEOUT = 0x08 12439213Sgibbs} cd_changer_flags; 12539213Sgibbs 12639213Sgibbs#define ccb_state ppriv_field0 12739213Sgibbs#define ccb_bp ppriv_ptr1 12839213Sgibbs 129111206Skenstruct cd_tocdata { 130111206Sken struct ioc_toc_header header; 131111206Sken struct cd_toc_entry entries[100]; 132111206Sken}; 133111206Sken 134111206Skenstruct cd_toc_single { 135111206Sken struct ioc_toc_header header; 136111206Sken struct cd_toc_entry entry; 137111206Sken}; 138111206Sken 13939213Sgibbstypedef enum { 14039213Sgibbs CD_STATE_PROBE, 14139213Sgibbs CD_STATE_NORMAL 14239213Sgibbs} cd_state; 14339213Sgibbs 14439213Sgibbsstruct cd_softc { 14539213Sgibbs cam_pinfo pinfo; 14639213Sgibbs cd_state state; 14746581Sken volatile cd_flags flags; 14859249Sphk struct bio_queue_head bio_queue; 14960938Sjake LIST_HEAD(, ccb_hdr) pending_ccbs; 15039213Sgibbs struct cd_params params; 15139213Sgibbs union ccb saved_ccb; 15239213Sgibbs cd_quirks quirks; 15360938Sjake STAILQ_ENTRY(cd_softc) changer_links; 15439213Sgibbs struct cdchanger *changer; 15539213Sgibbs int bufs_left; 15639213Sgibbs struct cam_periph *periph; 157111206Sken int minimum_command_size; 158112262Sphk int outstanding_cmds; 159119708Sken struct task sysctl_task; 160111206Sken struct sysctl_ctx_list sysctl_ctx; 161111206Sken struct sysctl_oid *sysctl_tree; 162111206Sken STAILQ_HEAD(, cd_mode_params) mode_queue; 163111206Sken struct cd_tocdata toc; 164125975Sphk struct disk *disk; 16539213Sgibbs}; 16639213Sgibbs 167111206Skenstruct cd_page_sizes { 168111206Sken int page; 169111206Sken int page_size; 170111206Sken}; 171111206Sken 172111206Skenstatic struct cd_page_sizes cd_page_size_table[] = 173111206Sken{ 174111206Sken { AUDIO_PAGE, sizeof(struct cd_audio_page)} 175111206Sken}; 176111206Sken 17739213Sgibbsstruct cd_quirk_entry { 17839213Sgibbs struct scsi_inquiry_pattern inq_pat; 17939213Sgibbs cd_quirks quirks; 18039213Sgibbs}; 18139213Sgibbs 18239213Sgibbs/* 183111206Sken * The changer quirk entries aren't strictly necessary. Basically, what 184111206Sken * they do is tell cdregister() up front that a device is a changer. 185111206Sken * Otherwise, it will figure that fact out once it sees a LUN on the device 186111206Sken * that is greater than 0. If it is known up front that a device is a changer, 187111206Sken * all I/O to the device will go through the changer scheduling routines, as 18839213Sgibbs * opposed to the "normal" CD code. 189111206Sken * 190111206Sken * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 191111206Sken * your device hangs when it gets a 10 byte command. Adding a quirk just 192111206Sken * to get rid of the informative diagnostic message is not acceptable. All 193111206Sken * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 194111206Sken * referenced in a comment along with the quirk) , and must be approved by 195111206Sken * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 196111206Sken * be removed until the submitter can explain why they are needed. 197111206Sken * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 198111206Sken * when the CAM_NEW_TRAN_CODE work is done. 19939213Sgibbs */ 20039213Sgibbsstatic struct cd_quirk_entry cd_quirk_table[] = 20139213Sgibbs{ 20239213Sgibbs { 20339213Sgibbs { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"}, 20439213Sgibbs /*quirks*/ CD_Q_CHANGER 20539213Sgibbs }, 20639213Sgibbs { 20754451Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*", 20839213Sgibbs "*"}, /* quirks */ CD_Q_CHANGER 20940262Sken }, 21040262Sken { 21167752Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"}, 21267752Sken /* quirks */ CD_Q_CHANGER 21367752Sken }, 21467752Sken { 21540262Sken { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 21640262Sken /* quirks */ CD_Q_BCD_TRACKS 21739213Sgibbs } 21839213Sgibbs}; 21939213Sgibbs 220120599Sphkstatic disk_open_t cdopen; 221120599Sphkstatic disk_close_t cdclose; 222120599Sphkstatic disk_ioctl_t cdioctl; 223120599Sphkstatic disk_strategy_t cdstrategy; 22439213Sgibbs 22539213Sgibbsstatic periph_init_t cdinit; 22639213Sgibbsstatic periph_ctor_t cdregister; 22739213Sgibbsstatic periph_dtor_t cdcleanup; 22839213Sgibbsstatic periph_start_t cdstart; 22940603Skenstatic periph_oninv_t cdoninvalidate; 23039213Sgibbsstatic void cdasync(void *callback_arg, u_int32_t code, 23139213Sgibbs struct cam_path *path, void *arg); 232111206Skenstatic int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 23339213Sgibbsstatic void cdshorttimeout(void *arg); 23439213Sgibbsstatic void cdschedule(struct cam_periph *periph, int priority); 23539213Sgibbsstatic void cdrunchangerqueue(void *arg); 23639213Sgibbsstatic void cdchangerschedule(struct cd_softc *softc); 23739213Sgibbsstatic int cdrunccb(union ccb *ccb, 23839213Sgibbs int (*error_routine)(union ccb *ccb, 23939213Sgibbs u_int32_t cam_flags, 24039213Sgibbs u_int32_t sense_flags), 24139213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags); 242111206Skenstatic union ccb *cdgetccb(struct cam_periph *periph, 24339213Sgibbs u_int32_t priority); 24439213Sgibbsstatic void cddone(struct cam_periph *periph, 24539213Sgibbs union ccb *start_ccb); 246111206Skenstatic union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 247111206Skenstatic int cdgetpagesize(int page_num); 248111206Skenstatic void cdprevent(struct cam_periph *periph, int action); 249111206Skenstatic int cdcheckmedia(struct cam_periph *periph); 250111206Skenstatic int cdsize(struct cam_periph *periph, u_int32_t *size); 251111206Skenstatic int cd6byteworkaround(union ccb *ccb); 25239213Sgibbsstatic int cderror(union ccb *ccb, u_int32_t cam_flags, 25339213Sgibbs u_int32_t sense_flags); 25439213Sgibbsstatic int cdreadtoc(struct cam_periph *periph, u_int32_t mode, 255111206Sken u_int32_t start, u_int8_t *data, 256111206Sken u_int32_t len, u_int32_t sense_flags); 25739213Sgibbsstatic int cdgetmode(struct cam_periph *periph, 258111206Sken struct cd_mode_params *data, u_int32_t page); 25939213Sgibbsstatic int cdsetmode(struct cam_periph *periph, 260111206Sken struct cd_mode_params *data); 26139213Sgibbsstatic int cdplay(struct cam_periph *periph, u_int32_t blk, 26239213Sgibbs u_int32_t len); 26339213Sgibbsstatic int cdreadsubchannel(struct cam_periph *periph, 26439213Sgibbs u_int32_t mode, u_int32_t format, 26539213Sgibbs int track, 26639213Sgibbs struct cd_sub_channel_info *data, 26739213Sgibbs u_int32_t len); 26839213Sgibbsstatic int cdplaymsf(struct cam_periph *periph, u_int32_t startm, 26939213Sgibbs u_int32_t starts, u_int32_t startf, 27039213Sgibbs u_int32_t endm, u_int32_t ends, 27139213Sgibbs u_int32_t endf); 27239213Sgibbsstatic int cdplaytracks(struct cam_periph *periph, 27339213Sgibbs u_int32_t strack, u_int32_t sindex, 27439213Sgibbs u_int32_t etrack, u_int32_t eindex); 27539213Sgibbsstatic int cdpause(struct cam_periph *periph, u_int32_t go); 27639213Sgibbsstatic int cdstopunit(struct cam_periph *periph, u_int32_t eject); 277111206Skenstatic int cdstartunit(struct cam_periph *periph, int load); 278105421Snjlstatic int cdsetspeed(struct cam_periph *periph, 279105421Snjl u_int32_t rdspeed, u_int32_t wrspeed); 28060422Skenstatic int cdreportkey(struct cam_periph *periph, 28160422Sken struct dvd_authinfo *authinfo); 28260422Skenstatic int cdsendkey(struct cam_periph *periph, 28360422Sken struct dvd_authinfo *authinfo); 28460422Skenstatic int cdreaddvdstructure(struct cam_periph *periph, 28560422Sken struct dvd_struct *dvdstruct); 28639213Sgibbs 28739213Sgibbsstatic struct periph_driver cddriver = 28839213Sgibbs{ 28939213Sgibbs cdinit, "cd", 29039213Sgibbs TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 29139213Sgibbs}; 29239213Sgibbs 29372119SpeterPERIPHDRIVER_DECLARE(cd, cddriver); 29439213Sgibbs 29539213Sgibbs#ifndef CHANGER_MIN_BUSY_SECONDS 29646747Sken#define CHANGER_MIN_BUSY_SECONDS 5 29739213Sgibbs#endif 29839213Sgibbs#ifndef CHANGER_MAX_BUSY_SECONDS 29946747Sken#define CHANGER_MAX_BUSY_SECONDS 15 30039213Sgibbs#endif 30139213Sgibbs 30239213Sgibbsstatic int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS; 30339213Sgibbsstatic int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS; 30439213Sgibbs 30539213SgibbsSYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver"); 30639213SgibbsSYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer"); 30739213SgibbsSYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW, 30839213Sgibbs &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum"); 309111206SkenTUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds); 31039213SgibbsSYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW, 31139213Sgibbs &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum"); 312111206SkenTUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds); 31339213Sgibbs 31439213Sgibbsstruct cdchanger { 31539213Sgibbs path_id_t path_id; 31639213Sgibbs target_id_t target_id; 31739213Sgibbs int num_devices; 31839213Sgibbs struct camq devq; 31939213Sgibbs struct timeval start_time; 32039213Sgibbs struct cd_softc *cur_device; 321168752Sscottl struct callout short_handle; 322168752Sscottl struct callout long_handle; 32346581Sken volatile cd_changer_flags flags; 32460938Sjake STAILQ_ENTRY(cdchanger) changer_links; 32560938Sjake STAILQ_HEAD(chdevlist, cd_softc) chluns; 32639213Sgibbs}; 32739213Sgibbs 328168752Sscottlstatic struct mtx changerq_mtx; 32960938Sjakestatic STAILQ_HEAD(changerlist, cdchanger) changerq; 330168752Sscottlstatic int num_changers; 33139213Sgibbs 332104880Sphkstatic void 33339213Sgibbscdinit(void) 33439213Sgibbs{ 33539213Sgibbs cam_status status; 33639213Sgibbs struct cam_path *path; 33739213Sgibbs 338168752Sscottl mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF); 339168752Sscottl STAILQ_INIT(&changerq); 340168752Sscottl 34139213Sgibbs /* 34239213Sgibbs * Install a global async callback. This callback will 34339213Sgibbs * receive async callbacks like "new device found". 34439213Sgibbs */ 34539213Sgibbs status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 34639213Sgibbs CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 34739213Sgibbs 34839213Sgibbs if (status == CAM_REQ_CMP) { 34939213Sgibbs struct ccb_setasync csa; 35039213Sgibbs 35139213Sgibbs xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 35239213Sgibbs csa.ccb_h.func_code = XPT_SASYNC_CB; 35339213Sgibbs csa.event_enable = AC_FOUND_DEVICE; 35439213Sgibbs csa.callback = cdasync; 35539213Sgibbs csa.callback_arg = NULL; 35639213Sgibbs xpt_action((union ccb *)&csa); 35739213Sgibbs status = csa.ccb_h.status; 35839213Sgibbs xpt_free_path(path); 35939213Sgibbs } 36039213Sgibbs 36139213Sgibbs if (status != CAM_REQ_CMP) { 36239213Sgibbs printf("cd: Failed to attach master async callback " 36339213Sgibbs "due to status 0x%x!\n", status); 36439213Sgibbs } 36539213Sgibbs} 36639213Sgibbs 36739213Sgibbsstatic void 36840603Skencdoninvalidate(struct cam_periph *periph) 36940603Sken{ 37040603Sken struct cd_softc *softc; 37140603Sken struct ccb_setasync csa; 37240603Sken 37340603Sken softc = (struct cd_softc *)periph->softc; 37440603Sken 37540603Sken /* 37640603Sken * De-register any async callbacks. 37740603Sken */ 37840603Sken xpt_setup_ccb(&csa.ccb_h, periph->path, 37940603Sken /* priority */ 5); 38040603Sken csa.ccb_h.func_code = XPT_SASYNC_CB; 38140603Sken csa.event_enable = 0; 38240603Sken csa.callback = cdasync; 38340603Sken csa.callback_arg = periph; 38440603Sken xpt_action((union ccb *)&csa); 38540603Sken 38640603Sken softc->flags |= CD_FLAG_INVALID; 38740603Sken 38840603Sken /* 38940603Sken * Return all queued I/O with ENXIO. 39040603Sken * XXX Handle any transactions queued to the card 39140603Sken * with XPT_ABORT_CCB. 39240603Sken */ 393112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 39440603Sken 39540603Sken /* 39640603Sken * If this device is part of a changer, and it was scheduled 39740603Sken * to run, remove it from the run queue since we just nuked 39840603Sken * all of its scheduled I/O. 39940603Sken */ 40040603Sken if ((softc->flags & CD_FLAG_CHANGER) 40140603Sken && (softc->pinfo.index != CAM_UNQUEUED_INDEX)) 40240603Sken camq_remove(&softc->changer->devq, softc->pinfo.index); 40340603Sken 404152565Sjdp disk_gone(softc->disk); 405164906Smjacob xpt_print(periph->path, "lost device\n"); 40640603Sken} 40740603Sken 40840603Skenstatic void 40939213Sgibbscdcleanup(struct cam_periph *periph) 41039213Sgibbs{ 41139213Sgibbs struct cd_softc *softc; 41239213Sgibbs 41339213Sgibbs softc = (struct cd_softc *)periph->softc; 41439213Sgibbs 415164906Smjacob xpt_print(periph->path, "removing device entry\n"); 41640603Sken 417120884Sthomas if ((softc->flags & CD_FLAG_SCTX_INIT) != 0 418120884Sthomas && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 419164906Smjacob xpt_print(periph->path, "can't remove sysctl context\n"); 420112668Sken } 421112668Sken 42239213Sgibbs /* 42339213Sgibbs * In the queued, non-active case, the device in question 42439213Sgibbs * has already been removed from the changer run queue. Since this 42539213Sgibbs * device is active, we need to de-activate it, and schedule 42639213Sgibbs * another device to run. (if there is another one to run) 42739213Sgibbs */ 42839213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 42939213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 43039213Sgibbs 43139213Sgibbs /* 43239213Sgibbs * The purpose of the short timeout is soley to determine 43339213Sgibbs * whether the current device has finished or not. Well, 43439213Sgibbs * since we're removing the active device, we know that it 43539213Sgibbs * is finished. So, get rid of the short timeout. 43639213Sgibbs * Otherwise, if we're in the time period before the short 43739213Sgibbs * timeout fires, and there are no other devices in the 43839213Sgibbs * queue to run, there won't be any other device put in the 43939213Sgibbs * active slot. i.e., when we call cdrunchangerqueue() 44039213Sgibbs * below, it won't do anything. Then, when the short 44139213Sgibbs * timeout fires, it'll look at the "current device", which 44239213Sgibbs * we are free below, and possibly panic the kernel on a 44339213Sgibbs * bogus pointer reference. 44439213Sgibbs * 44539213Sgibbs * The long timeout doesn't really matter, since we 44639213Sgibbs * decrement the qfrozen_cnt to indicate that there is 44739213Sgibbs * nothing in the active slot now. Therefore, there won't 44839213Sgibbs * be any bogus pointer references there. 44939213Sgibbs */ 45039213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 451168752Sscottl callout_stop(&softc->changer->short_handle); 45239213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 45339213Sgibbs } 45439213Sgibbs softc->changer->devq.qfrozen_cnt--; 45539213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 45639213Sgibbs cdrunchangerqueue(softc->changer); 45739213Sgibbs } 45839213Sgibbs 45939213Sgibbs /* 46039213Sgibbs * If we're removing the last device on the changer, go ahead and 46139213Sgibbs * remove the changer device structure. 46239213Sgibbs */ 46339213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) 46439213Sgibbs && (--softc->changer->num_devices == 0)) { 46539213Sgibbs 46639213Sgibbs /* 46739213Sgibbs * Theoretically, there shouldn't be any timeouts left, but 46839213Sgibbs * I'm not completely sure that that will be the case. So, 46939213Sgibbs * it won't hurt to check and see if there are any left. 47039213Sgibbs */ 47139213Sgibbs if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) { 472168752Sscottl callout_stop(&softc->changer->long_handle); 47339213Sgibbs softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED; 47439213Sgibbs } 47539213Sgibbs 47639213Sgibbs if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 477168752Sscottl callout_stop(&softc->changer->short_handle); 47839213Sgibbs softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 47939213Sgibbs } 48039213Sgibbs 481168752Sscottl mtx_lock(&changerq_mtx); 48260938Sjake STAILQ_REMOVE(&changerq, softc->changer, cdchanger, 48339213Sgibbs changer_links); 484168752Sscottl num_changers--; 485168752Sscottl mtx_unlock(&changerq_mtx); 486164906Smjacob xpt_print(periph->path, "removing changer entry\n"); 48739213Sgibbs free(softc->changer, M_DEVBUF); 48839213Sgibbs } 489168786Sscottl cam_periph_unlock(periph); 490125975Sphk disk_destroy(softc->disk); 491168786Sscottl cam_periph_lock(periph); 49240603Sken free(softc, M_DEVBUF); 49339213Sgibbs} 49439213Sgibbs 49539213Sgibbsstatic void 49639213Sgibbscdasync(void *callback_arg, u_int32_t code, 49739213Sgibbs struct cam_path *path, void *arg) 49839213Sgibbs{ 49939213Sgibbs struct cam_periph *periph; 50039213Sgibbs 50139213Sgibbs periph = (struct cam_periph *)callback_arg; 50239213Sgibbs switch (code) { 50339213Sgibbs case AC_FOUND_DEVICE: 50439213Sgibbs { 50539213Sgibbs struct ccb_getdev *cgd; 50639213Sgibbs cam_status status; 50739213Sgibbs 50839213Sgibbs cgd = (struct ccb_getdev *)arg; 50979177Smjacob if (cgd == NULL) 51079177Smjacob break; 51139213Sgibbs 51256148Smjacob if (SID_TYPE(&cgd->inq_data) != T_CDROM 51356148Smjacob && SID_TYPE(&cgd->inq_data) != T_WORM) 51439213Sgibbs break; 51539213Sgibbs 51639213Sgibbs /* 51739213Sgibbs * Allocate a peripheral instance for 51839213Sgibbs * this device and start the probe 51939213Sgibbs * process. 52039213Sgibbs */ 52140603Sken status = cam_periph_alloc(cdregister, cdoninvalidate, 52240603Sken cdcleanup, cdstart, 52340603Sken "cd", CAM_PERIPH_BIO, 52440603Sken cgd->ccb_h.path, cdasync, 52540603Sken AC_FOUND_DEVICE, cgd); 52639213Sgibbs 52739213Sgibbs if (status != CAM_REQ_CMP 52839213Sgibbs && status != CAM_REQ_INPROG) 52939213Sgibbs printf("cdasync: Unable to attach new device " 53039213Sgibbs "due to status 0x%x\n", status); 53139213Sgibbs 53239213Sgibbs break; 53339213Sgibbs } 53439213Sgibbs case AC_SENT_BDR: 53539213Sgibbs case AC_BUS_RESET: 53639213Sgibbs { 53739213Sgibbs struct cd_softc *softc; 53839213Sgibbs struct ccb_hdr *ccbh; 53939213Sgibbs 54039213Sgibbs softc = (struct cd_softc *)periph->softc; 54139213Sgibbs /* 54239213Sgibbs * Don't fail on the expected unit attention 54339213Sgibbs * that will occur. 54439213Sgibbs */ 54539213Sgibbs softc->flags |= CD_FLAG_RETRY_UA; 54671999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 54739213Sgibbs ccbh->ccb_state |= CD_CCB_RETRY_UA; 54847413Sgibbs /* FALLTHROUGH */ 54939213Sgibbs } 55039213Sgibbs default: 55147413Sgibbs cam_periph_async(periph, code, path, arg); 55239213Sgibbs break; 55339213Sgibbs } 55439213Sgibbs} 55539213Sgibbs 556119708Skenstatic void 557119708Skencdsysctlinit(void *context, int pending) 558119708Sken{ 559119708Sken struct cam_periph *periph; 560119708Sken struct cd_softc *softc; 561119708Sken char tmpstr[80], tmpstr2[80]; 562119708Sken 563119708Sken periph = (struct cam_periph *)context; 564168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 565168752Sscottl return; 566168752Sscottl 567119708Sken softc = (struct cd_softc *)periph->softc; 568119708Sken snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 569119708Sken snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 570119708Sken 571119708Sken mtx_lock(&Giant); 572119708Sken 573119708Sken sysctl_ctx_init(&softc->sysctl_ctx); 574120884Sthomas softc->flags |= CD_FLAG_SCTX_INIT; 575119708Sken softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 576119708Sken SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 577119708Sken tmpstr2, CTLFLAG_RD, 0, tmpstr); 578119708Sken 579119708Sken if (softc->sysctl_tree == NULL) { 580119708Sken printf("cdsysctlinit: unable to allocate sysctl tree\n"); 581160345Sdelphij mtx_unlock(&Giant); 582168752Sscottl cam_periph_release(periph); 583119708Sken return; 584119708Sken } 585119708Sken 586119708Sken /* 587119708Sken * Now register the sysctl handler, so the user can the value on 588119708Sken * the fly. 589119708Sken */ 590119708Sken SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 591119708Sken OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 592119708Sken &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 593119708Sken "Minimum CDB size"); 594119708Sken 595119708Sken mtx_unlock(&Giant); 596168752Sscottl cam_periph_release(periph); 597119708Sken} 598119708Sken 599111206Sken/* 600111206Sken * We have a handler function for this so we can check the values when the 601111206Sken * user sets them, instead of every time we look at them. 602111206Sken */ 603111206Skenstatic int 604111206Skencdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 605111206Sken{ 606111206Sken int error, value; 607111206Sken 608111206Sken value = *(int *)arg1; 609111206Sken 610111206Sken error = sysctl_handle_int(oidp, &value, 0, req); 611111206Sken 612111206Sken if ((error != 0) 613111206Sken || (req->newptr == NULL)) 614111206Sken return (error); 615111206Sken 616111206Sken /* 617111206Sken * The only real values we can have here are 6 or 10. I don't 618111206Sken * really forsee having 12 be an option at any time in the future. 619111206Sken * So if the user sets something less than or equal to 6, we'll set 620111206Sken * it to 6. If he sets something greater than 6, we'll set it to 10. 621111206Sken * 622111206Sken * I suppose we could just return an error here for the wrong values, 623111206Sken * but I don't think it's necessary to do so, as long as we can 624111206Sken * determine the user's intent without too much trouble. 625111206Sken */ 626111206Sken if (value < 6) 627111206Sken value = 6; 628111206Sken else if (value > 6) 629111206Sken value = 10; 630111206Sken 631111206Sken *(int *)arg1 = value; 632111206Sken 633111206Sken return (0); 634111206Sken} 635111206Sken 63639213Sgibbsstatic cam_status 63739213Sgibbscdregister(struct cam_periph *periph, void *arg) 63839213Sgibbs{ 63939213Sgibbs struct cd_softc *softc; 64039213Sgibbs struct ccb_setasync csa; 641118105Snjl struct ccb_pathinq cpi; 64239213Sgibbs struct ccb_getdev *cgd; 643119708Sken char tmpstr[80]; 64439213Sgibbs caddr_t match; 64539213Sgibbs 64639213Sgibbs cgd = (struct ccb_getdev *)arg; 64739213Sgibbs if (periph == NULL) { 64839213Sgibbs printf("cdregister: periph was NULL!!\n"); 64939213Sgibbs return(CAM_REQ_CMP_ERR); 65039213Sgibbs } 65139213Sgibbs if (cgd == NULL) { 65239213Sgibbs printf("cdregister: no getdev CCB, can't register device\n"); 65339213Sgibbs return(CAM_REQ_CMP_ERR); 65439213Sgibbs } 65539213Sgibbs 65639213Sgibbs softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 65739213Sgibbs 65839213Sgibbs if (softc == NULL) { 65939213Sgibbs printf("cdregister: Unable to probe new device. " 66039213Sgibbs "Unable to allocate softc\n"); 66139213Sgibbs return(CAM_REQ_CMP_ERR); 66239213Sgibbs } 66339213Sgibbs 66439213Sgibbs bzero(softc, sizeof(*softc)); 66539213Sgibbs LIST_INIT(&softc->pending_ccbs); 666111206Sken STAILQ_INIT(&softc->mode_queue); 66739213Sgibbs softc->state = CD_STATE_PROBE; 66859249Sphk bioq_init(&softc->bio_queue); 66939213Sgibbs if (SID_IS_REMOVABLE(&cgd->inq_data)) 67039213Sgibbs softc->flags |= CD_FLAG_DISC_REMOVABLE; 67139213Sgibbs if ((cgd->inq_data.flags & SID_CmdQue) != 0) 67239213Sgibbs softc->flags |= CD_FLAG_TAGGED_QUEUING; 67339213Sgibbs 67439213Sgibbs periph->softc = softc; 67539213Sgibbs softc->periph = periph; 67639213Sgibbs 67739213Sgibbs /* 67839213Sgibbs * See if this device has any quirks. 67939213Sgibbs */ 68039213Sgibbs match = cam_quirkmatch((caddr_t)&cgd->inq_data, 68139213Sgibbs (caddr_t)cd_quirk_table, 68239213Sgibbs sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), 68339213Sgibbs sizeof(*cd_quirk_table), scsi_inquiry_match); 68439213Sgibbs 68539213Sgibbs if (match != NULL) 68639213Sgibbs softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 68739213Sgibbs else 68839213Sgibbs softc->quirks = CD_Q_NONE; 68939213Sgibbs 690118105Snjl /* Check if the SIM does not want 6 byte commands */ 691118105Snjl xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); 692118105Snjl cpi.ccb_h.func_code = XPT_PATH_INQ; 693118105Snjl xpt_action((union ccb *)&cpi); 694118105Snjl if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 695118105Snjl softc->quirks |= CD_Q_10_BYTE_ONLY; 696118105Snjl 697119708Sken TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph); 698111206Sken 699111206Sken /* The default is 6 byte commands, unless quirked otherwise */ 700111206Sken if (softc->quirks & CD_Q_10_BYTE_ONLY) 701111206Sken softc->minimum_command_size = 10; 702111206Sken else 703111206Sken softc->minimum_command_size = 6; 704111206Sken 70539213Sgibbs /* 706111206Sken * Load the user's default, if any. 707111206Sken */ 708111206Sken snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 709111206Sken periph->unit_number); 710111206Sken TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 711111206Sken 712111206Sken /* 6 and 10 are the only permissible values here. */ 713111206Sken if (softc->minimum_command_size < 6) 714111206Sken softc->minimum_command_size = 6; 715111206Sken else if (softc->minimum_command_size > 6) 716111206Sken softc->minimum_command_size = 10; 717111206Sken 718111206Sken /* 71939213Sgibbs * We need to register the statistics structure for this device, 72039213Sgibbs * but we don't have the blocksize yet for it. So, we register 72139213Sgibbs * the structure and indicate that we don't have the blocksize 72239213Sgibbs * yet. Unlike other SCSI peripheral drivers, we explicitly set 72339213Sgibbs * the device type here to be CDROM, rather than just ORing in 72456148Smjacob * the device type. This is because this driver can attach to either 72539213Sgibbs * CDROM or WORM devices, and we want this peripheral driver to 72639213Sgibbs * show up in the devstat list as a CD peripheral driver, not a 72739213Sgibbs * WORM peripheral driver. WORM drives will also have the WORM 72839213Sgibbs * driver attached to them. 72939213Sgibbs */ 730168752Sscottl cam_periph_unlock(periph); 731125975Sphk softc->disk = disk_alloc(); 732125975Sphk softc->disk->d_devstat = devstat_new_entry("cd", 73339213Sgibbs periph->unit_number, 0, 73439213Sgibbs DEVSTAT_BS_UNAVAILABLE, 73543819Sken DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI, 73643819Sken DEVSTAT_PRIORITY_CD); 737125975Sphk softc->disk->d_open = cdopen; 738125975Sphk softc->disk->d_close = cdclose; 739125975Sphk softc->disk->d_strategy = cdstrategy; 740125975Sphk softc->disk->d_ioctl = cdioctl; 741125975Sphk softc->disk->d_name = "cd"; 742125975Sphk softc->disk->d_unit = periph->unit_number; 743125975Sphk softc->disk->d_drv1 = periph; 744168752Sscottl softc->disk->d_flags = 0; 745125975Sphk disk_create(softc->disk, DISK_VERSION); 746168752Sscottl cam_periph_lock(periph); 74739213Sgibbs 74839213Sgibbs /* 74939213Sgibbs * Add an async callback so that we get 75039213Sgibbs * notified if this device goes away. 75139213Sgibbs */ 75239213Sgibbs xpt_setup_ccb(&csa.ccb_h, periph->path, 75339213Sgibbs /* priority */ 5); 75439213Sgibbs csa.ccb_h.func_code = XPT_SASYNC_CB; 75539213Sgibbs csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 75639213Sgibbs csa.callback = cdasync; 75739213Sgibbs csa.callback_arg = periph; 75839213Sgibbs xpt_action((union ccb *)&csa); 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), 87739213Sgibbs M_DEVBUF, M_NOWAIT); 87839213Sgibbs 87939213Sgibbs if (nchanger == NULL) { 88039213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 88139213Sgibbs printf("cdregister: unable to malloc " 88239213Sgibbs "changer structure\ncdregister: " 88339213Sgibbs "changer support disabled\n"); 88439213Sgibbs 88539213Sgibbs /* 88639213Sgibbs * Yes, gotos can be gross but in this case 88739213Sgibbs * I think it's justified.. 88839213Sgibbs */ 88939213Sgibbs goto cdregisterexit; 89039213Sgibbs } 89139213Sgibbs 89239213Sgibbs /* zero the structure */ 89339213Sgibbs bzero(nchanger, sizeof(struct cdchanger)); 89439213Sgibbs 89539213Sgibbs if (camq_init(&nchanger->devq, 1) != 0) { 89639213Sgibbs softc->flags &= ~CD_FLAG_CHANGER; 89739213Sgibbs printf("cdregister: changer support " 89839213Sgibbs "disabled\n"); 89939213Sgibbs goto cdregisterexit; 90039213Sgibbs } 90139213Sgibbs 90239213Sgibbs nchanger->path_id = cgd->ccb_h.path_id; 90339213Sgibbs nchanger->target_id = cgd->ccb_h.target_id; 90439213Sgibbs 90539213Sgibbs /* this is superfluous, but it makes things clearer */ 90639213Sgibbs nchanger->num_devices = 0; 90739213Sgibbs 90839213Sgibbs STAILQ_INIT(&nchanger->chluns); 90939213Sgibbs 910168752Sscottl callout_init_mtx(&nchanger->long_handle, 911168752Sscottl periph->sim->mtx, 0); 912168752Sscottl callout_init_mtx(&nchanger->short_handle, 913168752Sscottl periph->sim->mtx, 0); 914168752Sscottl 915168752Sscottl mtx_lock(&changerq_mtx); 916168752Sscottl num_changers++; 91739213Sgibbs STAILQ_INSERT_TAIL(&changerq, nchanger, 91839213Sgibbs changer_links); 919168752Sscottl mtx_unlock(&changerq_mtx); 92039213Sgibbs 92139213Sgibbs /* 92239213Sgibbs * Create a path with lun id 0, and see if we can 92339213Sgibbs * find a matching device 92439213Sgibbs */ 92539213Sgibbs status = xpt_create_path(&path, /*periph*/ periph, 92639213Sgibbs cgd->ccb_h.path_id, 92739213Sgibbs cgd->ccb_h.target_id, 0); 92839213Sgibbs 92939213Sgibbs /* 93039213Sgibbs * If we were able to allocate the path, and if we 93139213Sgibbs * find a matching device and it isn't already 93239213Sgibbs * marked as part of a changer, then we add it to 93339213Sgibbs * the current changer. 93439213Sgibbs */ 93539213Sgibbs if ((status == CAM_REQ_CMP) 93639213Sgibbs && ((nperiph = cam_periph_find(path, "cd")) != NULL) 93739213Sgibbs && ((((struct cd_softc *)periph->softc)->flags & 93839213Sgibbs CD_FLAG_CHANGER) == 0)) { 93939213Sgibbs struct cd_softc *nsoftc; 94039213Sgibbs 94139213Sgibbs nsoftc = (struct cd_softc *)nperiph->softc; 94239213Sgibbs 94339213Sgibbs nsoftc->flags |= CD_FLAG_CHANGER; 94439213Sgibbs nchanger->num_devices++; 94539213Sgibbs if (camq_resize(&nchanger->devq, 94639213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 94739213Sgibbs printf("cdregister: camq_resize " 94839213Sgibbs "failed, changer support may " 94939213Sgibbs "be messed up\n"); 95039213Sgibbs } 95139213Sgibbs nsoftc->changer = nchanger; 95239213Sgibbs nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; 95339213Sgibbs 95439213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, 95539213Sgibbs nsoftc, changer_links); 95667928Sken xpt_free_path(path); 95739213Sgibbs } else if (status == CAM_REQ_CMP) 95839213Sgibbs xpt_free_path(path); 95939213Sgibbs else { 96039213Sgibbs printf("cdregister: unable to allocate path\n" 96139213Sgibbs "cdregister: changer support may be " 96239213Sgibbs "broken\n"); 96339213Sgibbs } 96439213Sgibbs 96539213Sgibbs softc->changer = nchanger; 96639213Sgibbs softc->pinfo.index = CAM_UNQUEUED_INDEX; 96739213Sgibbs nchanger->num_devices++; 96839213Sgibbs if (camq_resize(&nchanger->devq, 96939213Sgibbs nchanger->num_devices) != CAM_REQ_CMP) { 97039213Sgibbs printf("cdregister: camq_resize " 97139213Sgibbs "failed, changer support may " 97239213Sgibbs "be messed up\n"); 97339213Sgibbs } 97439213Sgibbs STAILQ_INSERT_TAIL(&nchanger->chluns, softc, 97539213Sgibbs changer_links); 97639213Sgibbs } 97739213Sgibbs } 97839213Sgibbs 97939213Sgibbscdregisterexit: 98039213Sgibbs 981168752Sscottl /* 982168752Sscottl * Refcount and block open attempts until we are setup 983168752Sscottl * Can't block 984168752Sscottl */ 985168752Sscottl (void)cam_periph_hold(periph, PRIBIO); 98639213Sgibbs 98739213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 98839213Sgibbs xpt_schedule(periph, /*priority*/5); 98939213Sgibbs else 99039213Sgibbs cdschedule(periph, /*priority*/ 5); 99139213Sgibbs 99239213Sgibbs return(CAM_REQ_CMP); 99339213Sgibbs} 99439213Sgibbs 99539213Sgibbsstatic int 996120599Sphkcdopen(struct disk *dp) 99739213Sgibbs{ 99839213Sgibbs struct cam_periph *periph; 99939213Sgibbs struct cd_softc *softc; 1000101940Snjl int error; 100139213Sgibbs 1002120599Sphk periph = (struct cam_periph *)dp->d_drv1; 100339213Sgibbs if (periph == NULL) 100439213Sgibbs return (ENXIO); 100539213Sgibbs 100639213Sgibbs softc = (struct cd_softc *)periph->softc; 100739213Sgibbs 1008168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1009168752Sscottl return(ENXIO); 1010168752Sscottl 1011168752Sscottl cam_periph_lock(periph); 1012168752Sscottl 101340603Sken if (softc->flags & CD_FLAG_INVALID) { 1014168752Sscottl cam_periph_unlock(periph); 1015168752Sscottl cam_periph_release(periph); 101639213Sgibbs return(ENXIO); 101740603Sken } 101839213Sgibbs 1019168752Sscottl if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1020168752Sscottl cam_periph_unlock(periph); 1021168752Sscottl cam_periph_release(periph); 102239213Sgibbs return (error); 102341297Sken } 102439213Sgibbs 1025168752Sscottl /* Closes aren't symmetrical with opens, so fix up the refcounting. */ 1026168752Sscottl if (softc->flags & CD_FLAG_OPEN) 1027168752Sscottl cam_periph_release(periph); 1028168752Sscottl else 1029168752Sscottl softc->flags |= CD_FLAG_OPEN; 103041297Sken 103140020Sken /* 1032111206Sken * Check for media, and set the appropriate flags. We don't bail 1033111206Sken * if we don't have media, but then we don't allow anything but the 1034111206Sken * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 103551836Sphk */ 1036111206Sken cdcheckmedia(periph); 103740020Sken 1038168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 1039168752Sscottl cam_periph_unhold(periph); 104039213Sgibbs cam_periph_unlock(periph); 104139213Sgibbs 1042168752Sscottl return (0); 104339213Sgibbs} 104439213Sgibbs 104539213Sgibbsstatic int 1046120599Sphkcdclose(struct disk *dp) 104739213Sgibbs{ 104839213Sgibbs struct cam_periph *periph; 104939213Sgibbs struct cd_softc *softc; 105039213Sgibbs 1051120599Sphk periph = (struct cam_periph *)dp->d_drv1; 105239213Sgibbs if (periph == NULL) 105339213Sgibbs return (ENXIO); 105439213Sgibbs 105539213Sgibbs softc = (struct cd_softc *)periph->softc; 105639213Sgibbs 1057168752Sscottl cam_periph_lock(periph); 1058168752Sscottl cam_periph_hold(periph, PRIBIO); 105939213Sgibbs 106039213Sgibbs if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 106139213Sgibbs cdprevent(periph, PR_ALLOW); 106239213Sgibbs 106339213Sgibbs /* 106439213Sgibbs * Since we're closing this CD, mark the blocksize as unavailable. 1065111206Sken * It will be marked as available when the CD is opened again. 106639213Sgibbs */ 1067125975Sphk softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 106839213Sgibbs 1069111206Sken /* 1070111206Sken * We'll check the media and toc again at the next open(). 1071111206Sken */ 1072168752Sscottl softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN); 1073111206Sken 1074168752Sscottl cam_periph_unhold(periph); 107539213Sgibbs cam_periph_unlock(periph); 107639213Sgibbs cam_periph_release(periph); 107739213Sgibbs 107839213Sgibbs return (0); 107939213Sgibbs} 108039213Sgibbs 108139213Sgibbsstatic void 108239213Sgibbscdshorttimeout(void *arg) 108339213Sgibbs{ 108439213Sgibbs struct cdchanger *changer; 108539213Sgibbs 108639213Sgibbs changer = (struct cdchanger *)arg; 108739213Sgibbs 108839213Sgibbs /* Always clear the short timeout flag, since that's what we're in */ 108939213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 109039213Sgibbs 109139213Sgibbs /* 109239213Sgibbs * Check to see if there is any more pending or outstanding I/O for 109339213Sgibbs * this device. If not, move it out of the active slot. 109439213Sgibbs */ 109559249Sphk if ((bioq_first(&changer->cur_device->bio_queue) == NULL) 1096112262Sphk && (changer->cur_device->outstanding_cmds == 0)) { 109739213Sgibbs changer->flags |= CHANGER_MANUAL_CALL; 109839213Sgibbs cdrunchangerqueue(changer); 109939213Sgibbs } 110039213Sgibbs} 110139213Sgibbs 110239213Sgibbs/* 110339213Sgibbs * This is a wrapper for xpt_schedule. It only applies to changers. 110439213Sgibbs */ 110539213Sgibbsstatic void 110639213Sgibbscdschedule(struct cam_periph *periph, int priority) 110739213Sgibbs{ 110839213Sgibbs struct cd_softc *softc; 110939213Sgibbs 111039213Sgibbs softc = (struct cd_softc *)periph->softc; 111139213Sgibbs 111239213Sgibbs /* 111339213Sgibbs * If this device isn't currently queued, and if it isn't 111439213Sgibbs * the active device, then we queue this device and run the 111539213Sgibbs * changer queue if there is no timeout scheduled to do it. 111639213Sgibbs * If this device is the active device, just schedule it 111739213Sgibbs * to run again. If this device is queued, there should be 111839213Sgibbs * a timeout in place already that will make sure it runs. 111939213Sgibbs */ 112039213Sgibbs if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 112139213Sgibbs && ((softc->flags & CD_FLAG_ACTIVE) == 0)) { 112239213Sgibbs /* 112339213Sgibbs * We don't do anything with the priority here. 112439213Sgibbs * This is strictly a fifo queue. 112539213Sgibbs */ 112639213Sgibbs softc->pinfo.priority = 1; 112745442Sgibbs softc->pinfo.generation = ++softc->changer->devq.generation; 112839213Sgibbs camq_insert(&softc->changer->devq, (cam_pinfo *)softc); 112939213Sgibbs 113039213Sgibbs /* 113139213Sgibbs * Since we just put a device in the changer queue, 113239213Sgibbs * check and see if there is a timeout scheduled for 113339213Sgibbs * this changer. If so, let the timeout handle 113439213Sgibbs * switching this device into the active slot. If 113539213Sgibbs * not, manually call the timeout routine to 113639213Sgibbs * bootstrap things. 113739213Sgibbs */ 113839213Sgibbs if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 113946581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 114046581Sken && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){ 114139213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 114239213Sgibbs cdrunchangerqueue(softc->changer); 114339213Sgibbs } 114439213Sgibbs } else if ((softc->flags & CD_FLAG_ACTIVE) 114539213Sgibbs && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) 114639213Sgibbs xpt_schedule(periph, priority); 114739213Sgibbs} 114839213Sgibbs 114939213Sgibbsstatic void 115039213Sgibbscdrunchangerqueue(void *arg) 115139213Sgibbs{ 115239213Sgibbs struct cd_softc *softc; 115339213Sgibbs struct cdchanger *changer; 115439213Sgibbs int called_from_timeout; 115539213Sgibbs 115639213Sgibbs changer = (struct cdchanger *)arg; 115739213Sgibbs 115839213Sgibbs /* 115939213Sgibbs * If we have NOT been called from cdstrategy() or cddone(), and 116039213Sgibbs * instead from a timeout routine, go ahead and clear the 116139213Sgibbs * timeout flag. 116239213Sgibbs */ 116339213Sgibbs if ((changer->flags & CHANGER_MANUAL_CALL) == 0) { 116439213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 116539213Sgibbs called_from_timeout = 1; 116639213Sgibbs } else 116739213Sgibbs called_from_timeout = 0; 116839213Sgibbs 116939213Sgibbs /* Always clear the manual call flag */ 117039213Sgibbs changer->flags &= ~CHANGER_MANUAL_CALL; 117139213Sgibbs 117239213Sgibbs /* nothing to do if the queue is empty */ 117339213Sgibbs if (changer->devq.entries <= 0) { 117439213Sgibbs return; 117539213Sgibbs } 117639213Sgibbs 117739213Sgibbs /* 117839213Sgibbs * If the changer queue is frozen, that means we have an active 117939213Sgibbs * device. 118039213Sgibbs */ 118139213Sgibbs if (changer->devq.qfrozen_cnt > 0) { 118239213Sgibbs 1183168752Sscottl /* 1184168752Sscottl * We always need to reset the frozen count and clear the 1185168752Sscottl * active flag. 1186168752Sscottl */ 1187168752Sscottl changer->devq.qfrozen_cnt--; 1188168752Sscottl changer->cur_device->flags &= ~CD_FLAG_ACTIVE; 1189168752Sscottl changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP; 1190168752Sscottl 1191112262Sphk if (changer->cur_device->outstanding_cmds > 0) { 119239213Sgibbs changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP; 119339213Sgibbs changer->cur_device->bufs_left = 1194112262Sphk changer->cur_device->outstanding_cmds; 119539213Sgibbs if (called_from_timeout) { 1196168752Sscottl callout_reset(&changer->long_handle, 1197168752Sscottl changer_max_busy_seconds * hz, 1198168752Sscottl cdrunchangerqueue, changer); 119939213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 120039213Sgibbs } 120139213Sgibbs return; 120239213Sgibbs } 120339213Sgibbs 120439213Sgibbs /* 120539213Sgibbs * Check to see whether the current device has any I/O left 120639213Sgibbs * to do. If so, requeue it at the end of the queue. If 120739213Sgibbs * not, there is no need to requeue it. 120839213Sgibbs */ 120959249Sphk if (bioq_first(&changer->cur_device->bio_queue) != NULL) { 121039213Sgibbs 121139213Sgibbs changer->cur_device->pinfo.generation = 121245442Sgibbs ++changer->devq.generation; 121339213Sgibbs camq_insert(&changer->devq, 121439213Sgibbs (cam_pinfo *)changer->cur_device); 121539213Sgibbs } 121639213Sgibbs } 121739213Sgibbs 121845845Sgibbs softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD); 121939213Sgibbs 122039213Sgibbs changer->cur_device = softc; 122139213Sgibbs 122239213Sgibbs changer->devq.qfrozen_cnt++; 122339213Sgibbs softc->flags |= CD_FLAG_ACTIVE; 122439213Sgibbs 122539213Sgibbs /* Just in case this device is waiting */ 122639213Sgibbs wakeup(&softc->changer); 122739213Sgibbs xpt_schedule(softc->periph, /*priority*/ 1); 122839213Sgibbs 122939213Sgibbs /* 123039213Sgibbs * Get rid of any pending timeouts, and set a flag to schedule new 123139213Sgibbs * ones so this device gets its full time quantum. 123239213Sgibbs */ 123339213Sgibbs if (changer->flags & CHANGER_TIMEOUT_SCHED) { 1234168752Sscottl callout_stop(&changer->long_handle); 123539213Sgibbs changer->flags &= ~CHANGER_TIMEOUT_SCHED; 123639213Sgibbs } 123739213Sgibbs 123839213Sgibbs if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) { 1239168752Sscottl callout_stop(&changer->short_handle); 124039213Sgibbs changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED; 124139213Sgibbs } 124239213Sgibbs 124339213Sgibbs /* 124439213Sgibbs * We need to schedule timeouts, but we only do this after the 124539213Sgibbs * first transaction has completed. This eliminates the changer 124639213Sgibbs * switch time. 124739213Sgibbs */ 124839213Sgibbs changer->flags |= CHANGER_NEED_TIMEOUT; 124939213Sgibbs} 125039213Sgibbs 125139213Sgibbsstatic void 125239213Sgibbscdchangerschedule(struct cd_softc *softc) 125339213Sgibbs{ 125439213Sgibbs struct cdchanger *changer; 125539213Sgibbs 125639213Sgibbs changer = softc->changer; 125739213Sgibbs 125839213Sgibbs /* 125939213Sgibbs * If this is a changer, and this is the current device, 126039213Sgibbs * and this device has at least the minimum time quantum to 126139213Sgibbs * run, see if we can switch it out. 126239213Sgibbs */ 126339213Sgibbs if ((softc->flags & CD_FLAG_ACTIVE) 126439213Sgibbs && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) 126539213Sgibbs && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) { 126639213Sgibbs /* 126739213Sgibbs * We try three things here. The first is that we 126839213Sgibbs * check to see whether the schedule on completion 126939213Sgibbs * flag is set. If it is, we decrement the number 127039213Sgibbs * of buffers left, and if it's zero, we reschedule. 127139213Sgibbs * Next, we check to see whether the pending buffer 127239213Sgibbs * queue is empty and whether there are no 127339213Sgibbs * outstanding transactions. If so, we reschedule. 127439213Sgibbs * Next, we see if the pending buffer queue is empty. 127539213Sgibbs * If it is, we set the number of buffers left to 127639213Sgibbs * the current active buffer count and set the 127739213Sgibbs * schedule on complete flag. 127839213Sgibbs */ 127939213Sgibbs if (softc->flags & CD_FLAG_SCHED_ON_COMP) { 128039213Sgibbs if (--softc->bufs_left == 0) { 128139213Sgibbs softc->changer->flags |= 128239213Sgibbs CHANGER_MANUAL_CALL; 128339213Sgibbs softc->flags &= ~CD_FLAG_SCHED_ON_COMP; 128439213Sgibbs cdrunchangerqueue(softc->changer); 128539213Sgibbs } 128659249Sphk } else if ((bioq_first(&softc->bio_queue) == NULL) 1287112262Sphk && (softc->outstanding_cmds == 0)) { 128839213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 128939213Sgibbs cdrunchangerqueue(softc->changer); 129039213Sgibbs } 129139213Sgibbs } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 129239213Sgibbs && (softc->flags & CD_FLAG_ACTIVE)) { 129339213Sgibbs 129439213Sgibbs /* 129539213Sgibbs * Now that the first transaction to this 129639213Sgibbs * particular device has completed, we can go ahead 129739213Sgibbs * and schedule our timeouts. 129839213Sgibbs */ 129939213Sgibbs if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) { 1300168752Sscottl callout_reset(&changer->long_handle, 1301168752Sscottl changer_max_busy_seconds * hz, 1302168752Sscottl cdrunchangerqueue, changer); 130339213Sgibbs changer->flags |= CHANGER_TIMEOUT_SCHED; 130439213Sgibbs } else 130539213Sgibbs printf("cdchangerschedule: already have a long" 130639213Sgibbs " timeout!\n"); 130739213Sgibbs 130839213Sgibbs if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) { 1309168752Sscottl callout_reset(&changer->short_handle, 1310168752Sscottl changer_min_busy_seconds * hz, 1311168752Sscottl cdshorttimeout, changer); 131239213Sgibbs changer->flags |= CHANGER_SHORT_TMOUT_SCHED; 131339213Sgibbs } else 131439213Sgibbs printf("cdchangerschedule: already have a short " 131539213Sgibbs "timeout!\n"); 131639213Sgibbs 131739213Sgibbs /* 131839213Sgibbs * We just scheduled timeouts, no need to schedule 131939213Sgibbs * more. 132039213Sgibbs */ 132139213Sgibbs changer->flags &= ~CHANGER_NEED_TIMEOUT; 132239213Sgibbs 132339213Sgibbs } 132439213Sgibbs} 132539213Sgibbs 132639213Sgibbsstatic int 132739213Sgibbscdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 132839213Sgibbs u_int32_t cam_flags, 132939213Sgibbs u_int32_t sense_flags), 133039213Sgibbs u_int32_t cam_flags, u_int32_t sense_flags) 133139213Sgibbs{ 133239213Sgibbs struct cd_softc *softc; 133339213Sgibbs struct cam_periph *periph; 133439213Sgibbs int error; 133539213Sgibbs 133639213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 133739213Sgibbs softc = (struct cd_softc *)periph->softc; 133839213Sgibbs 133939213Sgibbs error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 1340125975Sphk softc->disk->d_devstat); 134139213Sgibbs 134239213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 134339213Sgibbs cdchangerschedule(softc); 134439213Sgibbs 134539213Sgibbs return(error); 134639213Sgibbs} 134739213Sgibbs 134842017Seivindstatic union ccb * 134939213Sgibbscdgetccb(struct cam_periph *periph, u_int32_t priority) 135039213Sgibbs{ 135139213Sgibbs struct cd_softc *softc; 135239213Sgibbs 135339213Sgibbs softc = (struct cd_softc *)periph->softc; 135439213Sgibbs 135539213Sgibbs if (softc->flags & CD_FLAG_CHANGER) { 135639213Sgibbs /* 135739213Sgibbs * This should work the first time this device is woken up, 135839213Sgibbs * but just in case it doesn't, we use a while loop. 135939213Sgibbs */ 136046581Sken while ((softc->flags & CD_FLAG_ACTIVE) == 0) { 136139213Sgibbs /* 136239213Sgibbs * If this changer isn't already queued, queue it up. 136339213Sgibbs */ 136439213Sgibbs if (softc->pinfo.index == CAM_UNQUEUED_INDEX) { 136539213Sgibbs softc->pinfo.priority = 1; 136639213Sgibbs softc->pinfo.generation = 136745442Sgibbs ++softc->changer->devq.generation; 136839213Sgibbs camq_insert(&softc->changer->devq, 136939213Sgibbs (cam_pinfo *)softc); 137039213Sgibbs } 137146581Sken if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0) 137246581Sken && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0) 137346581Sken && ((softc->changer->flags 137446581Sken & CHANGER_SHORT_TMOUT_SCHED)==0)) { 137539213Sgibbs softc->changer->flags |= CHANGER_MANUAL_CALL; 137639213Sgibbs cdrunchangerqueue(softc->changer); 137739213Sgibbs } else 1378168752Sscottl msleep(&softc->changer, periph->sim->mtx, 1379168752Sscottl PRIBIO, "cgticb", 0); 138039213Sgibbs } 138139213Sgibbs } 138239213Sgibbs return(cam_periph_getccb(periph, priority)); 138339213Sgibbs} 138439213Sgibbs 138539213Sgibbs 138639213Sgibbs/* 138739213Sgibbs * Actually translate the requested transfer into one the physical driver 138839213Sgibbs * can understand. The transfer is described by a buf and will include 138939213Sgibbs * only one physical transfer. 139039213Sgibbs */ 139139213Sgibbsstatic void 139259249Sphkcdstrategy(struct bio *bp) 139339213Sgibbs{ 139439213Sgibbs struct cam_periph *periph; 139539213Sgibbs struct cd_softc *softc; 139639213Sgibbs 1397120599Sphk periph = (struct cam_periph *)bp->bio_disk->d_drv1; 139839213Sgibbs if (periph == NULL) { 139976362Sphk biofinish(bp, NULL, ENXIO); 140076362Sphk return; 140139213Sgibbs } 140239213Sgibbs 1403168752Sscottl cam_periph_lock(periph); 140439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n")); 140539213Sgibbs 140639213Sgibbs softc = (struct cd_softc *)periph->softc; 140739213Sgibbs 140839213Sgibbs /* 140939213Sgibbs * If the device has been made invalid, error out 141039213Sgibbs */ 141139213Sgibbs if ((softc->flags & CD_FLAG_INVALID)) { 1412168752Sscottl cam_periph_unlock(periph); 141376362Sphk biofinish(bp, NULL, ENXIO); 141476362Sphk return; 141539213Sgibbs } 141639213Sgibbs 1417111206Sken /* 1418111206Sken * If we don't have valid media, look for it before trying to 1419111206Sken * schedule the I/O. 1420111206Sken */ 1421111206Sken if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) { 1422111206Sken int error; 1423111206Sken 1424111206Sken error = cdcheckmedia(periph); 1425111206Sken if (error != 0) { 1426168752Sscottl cam_periph_unlock(periph); 1427111206Sken biofinish(bp, NULL, error); 1428111206Sken return; 1429111206Sken } 1430111206Sken } 1431111206Sken 143239213Sgibbs /* 143339213Sgibbs * Place it in the queue of disk activities for this disk 143439213Sgibbs */ 1435112946Sphk bioq_disksort(&softc->bio_queue, bp); 143639213Sgibbs 143739213Sgibbs /* 143839213Sgibbs * Schedule ourselves for performing the work. We do things 143939213Sgibbs * differently for changers. 144039213Sgibbs */ 144139213Sgibbs if ((softc->flags & CD_FLAG_CHANGER) == 0) 144239213Sgibbs xpt_schedule(periph, /* XXX priority */1); 144339213Sgibbs else 144439213Sgibbs cdschedule(periph, /* priority */ 1); 144539213Sgibbs 1446168752Sscottl cam_periph_unlock(periph); 144739213Sgibbs return; 144839213Sgibbs} 144939213Sgibbs 145039213Sgibbsstatic void 145139213Sgibbscdstart(struct cam_periph *periph, union ccb *start_ccb) 145239213Sgibbs{ 145339213Sgibbs struct cd_softc *softc; 145459249Sphk struct bio *bp; 145539213Sgibbs struct ccb_scsiio *csio; 145639213Sgibbs struct scsi_read_capacity_data *rcap; 145739213Sgibbs 145839213Sgibbs softc = (struct cd_softc *)periph->softc; 145939213Sgibbs 146039213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 146139213Sgibbs 146239213Sgibbs switch (softc->state) { 146339213Sgibbs case CD_STATE_NORMAL: 146439213Sgibbs { 146559249Sphk bp = bioq_first(&softc->bio_queue); 146639213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 146739213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_WAITING; 146839213Sgibbs 146939213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 147039213Sgibbs periph_links.sle); 147139213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 147239213Sgibbs wakeup(&periph->ccb_list); 147339213Sgibbs } else if (bp == NULL) { 147439213Sgibbs xpt_release_ccb(start_ccb); 147539213Sgibbs } else { 147659249Sphk bioq_remove(&softc->bio_queue, bp); 147739213Sgibbs 1478125975Sphk devstat_start_transaction_bio(softc->disk->d_devstat, bp); 147939213Sgibbs 148039213Sgibbs scsi_read_write(&start_ccb->csio, 148139213Sgibbs /*retries*/4, 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); 149439213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 149539213Sgibbs 149639213Sgibbs 149739213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, 149839213Sgibbs &start_ccb->ccb_h, periph_links.le); 1499112262Sphk softc->outstanding_cmds++; 150039213Sgibbs 150139213Sgibbs /* We expect a unit attention from this device */ 150239213Sgibbs if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 150339213Sgibbs start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 150439213Sgibbs softc->flags &= ~CD_FLAG_RETRY_UA; 150539213Sgibbs } 150639213Sgibbs 150739213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 150859249Sphk bp = bioq_first(&softc->bio_queue); 150939213Sgibbs 151039213Sgibbs xpt_action(start_ccb); 151139213Sgibbs } 151239213Sgibbs if (bp != NULL) { 151339213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 151439213Sgibbs xpt_schedule(periph, /* XXX priority */1); 151539213Sgibbs } 151639213Sgibbs break; 151739213Sgibbs } 151839213Sgibbs case CD_STATE_PROBE: 151939213Sgibbs { 152039213Sgibbs 152139213Sgibbs rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 152239213Sgibbs M_TEMP, 152339213Sgibbs M_NOWAIT); 152439213Sgibbs if (rcap == NULL) { 1525164906Smjacob xpt_print(periph->path, 1526164906Smjacob "cdstart: Couldn't malloc read_capacity data\n"); 152739213Sgibbs /* cd_free_periph??? */ 152839213Sgibbs break; 152939213Sgibbs } 153039213Sgibbs csio = &start_ccb->csio; 153139213Sgibbs scsi_read_capacity(csio, 153239213Sgibbs /*retries*/1, 153339213Sgibbs cddone, 153439213Sgibbs MSG_SIMPLE_Q_TAG, 153539213Sgibbs rcap, 153639213Sgibbs SSD_FULL_SIZE, 153739213Sgibbs /*timeout*/20000); 153839213Sgibbs start_ccb->ccb_h.ccb_bp = NULL; 153939213Sgibbs start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 154039213Sgibbs xpt_action(start_ccb); 154139213Sgibbs break; 154239213Sgibbs } 154339213Sgibbs } 154439213Sgibbs} 154539213Sgibbs 154639213Sgibbsstatic void 154739213Sgibbscddone(struct cam_periph *periph, union ccb *done_ccb) 154839213Sgibbs{ 154939213Sgibbs struct cd_softc *softc; 155039213Sgibbs struct ccb_scsiio *csio; 155139213Sgibbs 155239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 155339213Sgibbs 155439213Sgibbs softc = (struct cd_softc *)periph->softc; 155539213Sgibbs csio = &done_ccb->csio; 155639213Sgibbs 155739213Sgibbs switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 155839213Sgibbs case CD_CCB_BUFFER_IO: 155939213Sgibbs { 156059249Sphk struct bio *bp; 156139213Sgibbs int error; 156239213Sgibbs 156359249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 156439213Sgibbs error = 0; 156539213Sgibbs 156639213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 156739213Sgibbs int sf; 156839213Sgibbs 156939213Sgibbs if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 157039213Sgibbs sf = SF_RETRY_UA; 157139213Sgibbs else 157239213Sgibbs sf = 0; 157346747Sken 157474840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 157574840Sken if (error == ERESTART) { 157639213Sgibbs /* 157739213Sgibbs * A retry was scheuled, so 157839213Sgibbs * just return. 157939213Sgibbs */ 158039213Sgibbs return; 158139213Sgibbs } 158239213Sgibbs } 158339213Sgibbs 158439213Sgibbs if (error != 0) { 1585164906Smjacob xpt_print(periph->path, 1586164906Smjacob "cddone: got error %#x back\n", error); 1587112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 158859249Sphk bp->bio_resid = bp->bio_bcount; 158959249Sphk bp->bio_error = error; 159059249Sphk bp->bio_flags |= BIO_ERROR; 159139213Sgibbs cam_release_devq(done_ccb->ccb_h.path, 159239213Sgibbs /*relsim_flags*/0, 159339213Sgibbs /*reduction*/0, 159439213Sgibbs /*timeout*/0, 159539213Sgibbs /*getcount_only*/0); 159639213Sgibbs 159739213Sgibbs } else { 159859249Sphk bp->bio_resid = csio->resid; 159959249Sphk bp->bio_error = 0; 160059249Sphk if (bp->bio_resid != 0) { 1601104456Sphk /* 1602104456Sphk * Short transfer ??? 1603104456Sphk * XXX: not sure this is correct for partial 1604104456Sphk * transfers at EOM 1605104456Sphk */ 160659249Sphk bp->bio_flags |= BIO_ERROR; 160739213Sgibbs } 160839213Sgibbs } 160939213Sgibbs 161039213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1611112262Sphk softc->outstanding_cmds--; 161239213Sgibbs 161339213Sgibbs if (softc->flags & CD_FLAG_CHANGER) 161439213Sgibbs cdchangerschedule(softc); 161539213Sgibbs 1616157806Smaxim biofinish(bp, NULL, 0); 161739213Sgibbs break; 161839213Sgibbs } 161939213Sgibbs case CD_CCB_PROBE: 162039213Sgibbs { 162139213Sgibbs struct scsi_read_capacity_data *rdcap; 162239213Sgibbs char announce_buf[120]; /* 162339213Sgibbs * Currently (9/30/97) the 162439213Sgibbs * longest possible announce 162539213Sgibbs * buffer is 108 bytes, for the 162639213Sgibbs * first error case below. 162739213Sgibbs * That is 39 bytes for the 162839213Sgibbs * basic string, 16 bytes for the 162939213Sgibbs * biggest sense key (hardware 163039213Sgibbs * error), 52 bytes for the 163139213Sgibbs * text of the largest sense 163239213Sgibbs * qualifier valid for a CDROM, 163339213Sgibbs * (0x72, 0x03 or 0x04, 163439213Sgibbs * 0x03), and one byte for the 163539213Sgibbs * null terminating character. 163639213Sgibbs * To allow for longer strings, 163739213Sgibbs * the announce buffer is 120 163839213Sgibbs * bytes. 163939213Sgibbs */ 164039213Sgibbs struct cd_params *cdp; 164139213Sgibbs 164239213Sgibbs cdp = &softc->params; 164339213Sgibbs 164439213Sgibbs rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 164539213Sgibbs 164639213Sgibbs cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 164739213Sgibbs cdp->blksize = scsi_4btoul (rdcap->length); 164839213Sgibbs 164939213Sgibbs if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 165039213Sgibbs 165141514Sarchie snprintf(announce_buf, sizeof(announce_buf), 165240020Sken "cd present [%lu x %lu byte records]", 165340020Sken cdp->disksize, (u_long)cdp->blksize); 165439213Sgibbs 165539213Sgibbs } else { 165639213Sgibbs int error; 165739213Sgibbs /* 165839213Sgibbs * Retry any UNIT ATTENTION type errors. They 165939213Sgibbs * are expected at boot. 166039213Sgibbs */ 166174840Sken error = cderror(done_ccb, CAM_RETRY_SELTO, 166274840Sken SF_RETRY_UA | SF_NO_PRINT); 166339213Sgibbs if (error == ERESTART) { 166439213Sgibbs /* 166539213Sgibbs * A retry was scheuled, so 166639213Sgibbs * just return. 166739213Sgibbs */ 166839213Sgibbs return; 166939213Sgibbs } else if (error != 0) { 167039213Sgibbs 167139213Sgibbs struct scsi_sense_data *sense; 167239213Sgibbs int asc, ascq; 167339213Sgibbs int sense_key, error_code; 167439213Sgibbs int have_sense; 167539213Sgibbs cam_status status; 167639213Sgibbs struct ccb_getdev cgd; 167739213Sgibbs 167839213Sgibbs /* Don't wedge this device's queue */ 167939213Sgibbs cam_release_devq(done_ccb->ccb_h.path, 168039213Sgibbs /*relsim_flags*/0, 168139213Sgibbs /*reduction*/0, 168239213Sgibbs /*timeout*/0, 168339213Sgibbs /*getcount_only*/0); 168439213Sgibbs 168539213Sgibbs status = done_ccb->ccb_h.status; 168639213Sgibbs 168739213Sgibbs xpt_setup_ccb(&cgd.ccb_h, 168839213Sgibbs done_ccb->ccb_h.path, 168939213Sgibbs /* priority */ 1); 169039213Sgibbs cgd.ccb_h.func_code = XPT_GDEV_TYPE; 169139213Sgibbs xpt_action((union ccb *)&cgd); 169239213Sgibbs 169339213Sgibbs if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 169439213Sgibbs || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 169539213Sgibbs || ((status & CAM_AUTOSNS_VALID) == 0)) 169639213Sgibbs have_sense = FALSE; 169739213Sgibbs else 169839213Sgibbs have_sense = TRUE; 169939213Sgibbs 170039213Sgibbs if (have_sense) { 170139213Sgibbs sense = &csio->sense_data; 170239213Sgibbs scsi_extract_sense(sense, &error_code, 170339213Sgibbs &sense_key, 170439213Sgibbs &asc, &ascq); 170539213Sgibbs } 170639213Sgibbs /* 170746581Sken * Attach to anything that claims to be a 170846581Sken * CDROM or WORM device, as long as it 170946581Sken * doesn't return a "Logical unit not 171046581Sken * supported" (0x25) error. 171139213Sgibbs */ 171246581Sken if ((have_sense) && (asc != 0x25) 171374840Sken && (error_code == SSD_CURRENT_ERROR)) { 171474840Sken const char *sense_key_desc; 171574840Sken const char *asc_desc; 171674840Sken 171774840Sken scsi_sense_desc(sense_key, asc, ascq, 171874840Sken &cgd.inq_data, 171974840Sken &sense_key_desc, 172074840Sken &asc_desc); 172141514Sarchie snprintf(announce_buf, 172241514Sarchie sizeof(announce_buf), 172339213Sgibbs "Attempt to query device " 172439213Sgibbs "size failed: %s, %s", 172574840Sken sense_key_desc, 172674840Sken asc_desc); 172782850Sken } else if ((have_sense == 0) 172882850Sken && ((status & CAM_STATUS_MASK) == 172982850Sken CAM_SCSI_STATUS_ERROR) 173082850Sken && (csio->scsi_status == 173182850Sken SCSI_STATUS_BUSY)) { 173282850Sken snprintf(announce_buf, 173382850Sken sizeof(announce_buf), 173482850Sken "Attempt to query device " 173582850Sken "size failed: SCSI Status: %s", 173682850Sken scsi_status_string(csio)); 173774840Sken } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 173839213Sgibbs /* 173939213Sgibbs * We only print out an error for 174039213Sgibbs * CDROM type devices. For WORM 174139213Sgibbs * devices, we don't print out an 174239213Sgibbs * error since a few WORM devices 174339213Sgibbs * don't support CDROM commands. 174439213Sgibbs * If we have sense information, go 174539213Sgibbs * ahead and print it out. 174639213Sgibbs * Otherwise, just say that we 174739213Sgibbs * couldn't attach. 174839213Sgibbs */ 174939213Sgibbs 175039213Sgibbs /* 175139213Sgibbs * Just print out the error, not 175239213Sgibbs * the full probe message, when we 175339213Sgibbs * don't attach. 175439213Sgibbs */ 175540020Sken if (have_sense) 175640020Sken scsi_sense_print( 175740020Sken &done_ccb->csio); 175840020Sken else { 1759164906Smjacob xpt_print(periph->path, 1760164906Smjacob "got CAM status %#x\n", 1761164906Smjacob done_ccb->ccb_h.status); 176240020Sken } 1763164906Smjacob xpt_print(periph->path, "fatal error, " 1764164906Smjacob "failed to attach to device\n"); 176539213Sgibbs /* 176640603Sken * Invalidate this peripheral. 176739213Sgibbs */ 176839213Sgibbs cam_periph_invalidate(periph); 176940020Sken 177040020Sken announce_buf[0] = '\0'; 177139213Sgibbs } else { 177240603Sken 177339213Sgibbs /* 177440603Sken * Invalidate this peripheral. 177539213Sgibbs */ 177639213Sgibbs cam_periph_invalidate(periph); 177740020Sken announce_buf[0] = '\0'; 177839213Sgibbs } 177939213Sgibbs } 178039213Sgibbs } 178139213Sgibbs free(rdcap, M_TEMP); 178242378Smjacob if (announce_buf[0] != '\0') { 178339213Sgibbs xpt_announce_periph(periph, announce_buf); 178442378Smjacob if (softc->flags & CD_FLAG_CHANGER) 178542378Smjacob cdchangerschedule(softc); 1786119708Sken /* 1787119708Sken * Create our sysctl variables, now that we know 1788119708Sken * we have successfully attached. 1789119708Sken */ 1790119708Sken taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 179142378Smjacob } 179240020Sken softc->state = CD_STATE_NORMAL; 179342378Smjacob /* 179442378Smjacob * Since our peripheral may be invalidated by an error 179542378Smjacob * above or an external event, we must release our CCB 179642378Smjacob * before releasing the probe lock on the peripheral. 179742378Smjacob * The peripheral will only go away once the last lock 179842378Smjacob * is removed, and we need it around for the CCB release 179942378Smjacob * operation. 180042378Smjacob */ 180142378Smjacob xpt_release_ccb(done_ccb); 1802168752Sscottl cam_periph_unhold(periph); 180342378Smjacob return; 180439213Sgibbs } 180539213Sgibbs case CD_CCB_WAITING: 180639213Sgibbs { 180739213Sgibbs /* Caller will release the CCB */ 180839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 180939213Sgibbs ("trying to wakeup ccbwait\n")); 181039213Sgibbs 181139213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 181239213Sgibbs return; 181339213Sgibbs } 181442378Smjacob default: 181542378Smjacob break; 181639213Sgibbs } 181739213Sgibbs xpt_release_ccb(done_ccb); 181839213Sgibbs} 181939213Sgibbs 1820111206Skenstatic union cd_pages * 1821111206Skencdgetpage(struct cd_mode_params *mode_params) 1822111206Sken{ 1823111206Sken union cd_pages *page; 1824111206Sken 1825111206Sken if (mode_params->cdb_size == 10) 1826111206Sken page = (union cd_pages *)find_mode_page_10( 1827111206Sken (struct scsi_mode_header_10 *)mode_params->mode_buf); 1828111206Sken else 1829111206Sken page = (union cd_pages *)find_mode_page_6( 1830111206Sken (struct scsi_mode_header_6 *)mode_params->mode_buf); 1831111206Sken 1832111206Sken return (page); 1833111206Sken} 1834111206Sken 183539213Sgibbsstatic int 1836111206Skencdgetpagesize(int page_num) 1837111206Sken{ 1838111206Sken int i; 1839111206Sken 1840111206Sken for (i = 0; i < (sizeof(cd_page_size_table)/ 1841111206Sken sizeof(cd_page_size_table[0])); i++) { 1842111206Sken if (cd_page_size_table[i].page == page_num) 1843111206Sken return (cd_page_size_table[i].page_size); 1844111206Sken } 1845111206Sken 1846111206Sken return (-1); 1847111206Sken} 1848111206Sken 1849111206Skenstatic int 1850120599Sphkcdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 185139213Sgibbs{ 185239213Sgibbs 185339213Sgibbs struct cam_periph *periph; 185439213Sgibbs struct cd_softc *softc; 1855168752Sscottl int nocopyout, error = 0; 185639213Sgibbs 1857120599Sphk periph = (struct cam_periph *)dp->d_drv1; 185839213Sgibbs if (periph == NULL) 185939213Sgibbs return(ENXIO); 186039213Sgibbs 1861168752Sscottl cam_periph_lock(periph); 186239213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n")); 186339213Sgibbs 186439213Sgibbs softc = (struct cd_softc *)periph->softc; 186539213Sgibbs 186639213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 186740020Sken ("trying to do ioctl %#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); 1889111206Sken } 1890168752Sscottl /* 1891168752Sscottl * Drop the lock here so later mallocs can use WAITOK. The periph 1892168752Sscottl * is essentially locked still with the cam_periph_hold call above. 1893168752Sscottl */ 1894168752Sscottl cam_periph_unlock(periph); 1895168752Sscottl if (error != 0) 1896168752Sscottl return (error); 189740020Sken 1898141031Ssobomax nocopyout = 0; 189939213Sgibbs switch (cmd) { 190039213Sgibbs 190139213Sgibbs case CDIOCPLAYTRACKS: 190239213Sgibbs { 190339213Sgibbs struct ioc_play_track *args 190439213Sgibbs = (struct ioc_play_track *) addr; 1905111206Sken struct cd_mode_params params; 1906111206Sken union cd_pages *page; 190739213Sgibbs 1908111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 1909111206Sken params.mode_buf = malloc(params.alloc_len, M_TEMP, 1910111206Sken M_WAITOK | M_ZERO); 191139213Sgibbs 1912168752Sscottl cam_periph_lock(periph); 191339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 191439213Sgibbs ("trying to do CDIOCPLAYTRACKS\n")); 191539213Sgibbs 1916111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 191739213Sgibbs if (error) { 1918111206Sken free(params.mode_buf, M_TEMP); 1919168752Sscottl cam_periph_unlock(periph); 192039213Sgibbs break; 192139213Sgibbs } 1922111206Sken page = cdgetpage(¶ms); 1923111206Sken 1924111206Sken page->audio.flags &= ~CD_PA_SOTC; 1925111206Sken page->audio.flags |= CD_PA_IMMED; 1926111206Sken error = cdsetmode(periph, ¶ms); 1927111206Sken free(params.mode_buf, M_TEMP); 1928168752Sscottl if (error) { 1929168752Sscottl cam_periph_unlock(periph); 193039213Sgibbs break; 1931168752Sscottl } 1932111206Sken 1933111206Sken /* 1934111206Sken * This was originally implemented with the PLAY 1935111206Sken * AUDIO TRACK INDEX command, but that command was 1936111206Sken * deprecated after SCSI-2. Most (all?) SCSI CDROM 1937111206Sken * drives support it but ATAPI and ATAPI-derivative 1938111206Sken * drives don't seem to support it. So we keep a 1939111206Sken * cache of the table of contents and translate 1940111206Sken * track numbers to MSF format. 1941111206Sken */ 1942111206Sken if (softc->flags & CD_FLAG_VALID_TOC) { 1943111206Sken union msf_lba *sentry, *eentry; 1944111206Sken int st, et; 1945111206Sken 1946111206Sken if (args->end_track < 1947111206Sken softc->toc.header.ending_track + 1) 1948111206Sken args->end_track++; 1949111206Sken if (args->end_track > 1950111206Sken softc->toc.header.ending_track + 1) 1951111206Sken args->end_track = 1952111206Sken softc->toc.header.ending_track + 1; 1953111206Sken st = args->start_track - 1954111206Sken softc->toc.header.starting_track; 1955111206Sken et = args->end_track - 1956111206Sken softc->toc.header.starting_track; 1957111206Sken if ((st < 0) 1958111206Sken || (et < 0) 1959111206Sken || (st > (softc->toc.header.ending_track - 1960111206Sken softc->toc.header.starting_track))) { 1961111206Sken error = EINVAL; 1962111206Sken break; 1963111206Sken } 1964111206Sken sentry = &softc->toc.entries[st].addr; 1965111206Sken eentry = &softc->toc.entries[et].addr; 1966111206Sken error = cdplaymsf(periph, 1967111206Sken sentry->msf.minute, 1968111206Sken sentry->msf.second, 1969111206Sken sentry->msf.frame, 1970111206Sken eentry->msf.minute, 1971111206Sken eentry->msf.second, 1972111206Sken eentry->msf.frame); 1973111206Sken } else { 1974111206Sken /* 1975111206Sken * If we don't have a valid TOC, try the 1976111206Sken * play track index command. It is part of 1977111206Sken * the SCSI-2 spec, but was removed in the 1978111206Sken * MMC specs. ATAPI and ATAPI-derived 1979111206Sken * drives don't support it. 1980111206Sken */ 1981111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 1982111206Sken args->start_track = 1983111206Sken bin2bcd(args->start_track); 1984111206Sken args->end_track = 1985111206Sken bin2bcd(args->end_track); 1986111206Sken } 1987111206Sken error = cdplaytracks(periph, 1988111206Sken args->start_track, 1989111206Sken args->start_index, 1990111206Sken args->end_track, 1991111206Sken args->end_index); 199239213Sgibbs } 1993168752Sscottl cam_periph_unlock(periph); 199439213Sgibbs } 199539213Sgibbs break; 199639213Sgibbs case CDIOCPLAYMSF: 199739213Sgibbs { 199839213Sgibbs struct ioc_play_msf *args 199939213Sgibbs = (struct ioc_play_msf *) addr; 2000111206Sken struct cd_mode_params params; 2001111206Sken union cd_pages *page; 200239213Sgibbs 2003111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2004111206Sken params.mode_buf = malloc(params.alloc_len, M_TEMP, 2005111206Sken M_WAITOK | M_ZERO); 200639213Sgibbs 2007168752Sscottl cam_periph_lock(periph); 200839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 200939213Sgibbs ("trying to do CDIOCPLAYMSF\n")); 201039213Sgibbs 2011111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 201239213Sgibbs if (error) { 2013111206Sken free(params.mode_buf, M_TEMP); 2014168752Sscottl cam_periph_unlock(periph); 201539213Sgibbs break; 201639213Sgibbs } 2017111206Sken page = cdgetpage(¶ms); 2018111206Sken 2019111206Sken page->audio.flags &= ~CD_PA_SOTC; 2020111206Sken page->audio.flags |= CD_PA_IMMED; 2021111206Sken error = cdsetmode(periph, ¶ms); 2022111206Sken free(params.mode_buf, M_TEMP); 2023168752Sscottl if (error) { 2024168752Sscottl cam_periph_unlock(periph); 202539213Sgibbs break; 2026168752Sscottl } 202739213Sgibbs error = cdplaymsf(periph, 202839213Sgibbs args->start_m, 202939213Sgibbs args->start_s, 203039213Sgibbs args->start_f, 203139213Sgibbs args->end_m, 203239213Sgibbs args->end_s, 203339213Sgibbs args->end_f); 2034168752Sscottl cam_periph_unlock(periph); 203539213Sgibbs } 203639213Sgibbs break; 203739213Sgibbs case CDIOCPLAYBLOCKS: 203839213Sgibbs { 203939213Sgibbs struct ioc_play_blocks *args 204039213Sgibbs = (struct ioc_play_blocks *) addr; 2041111206Sken struct cd_mode_params params; 2042111206Sken union cd_pages *page; 204339213Sgibbs 2044111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2045111206Sken params.mode_buf = malloc(params.alloc_len, M_TEMP, 2046111206Sken M_WAITOK | M_ZERO); 204739213Sgibbs 2048168752Sscottl cam_periph_lock(periph); 2049168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2050168752Sscottl ("trying to do CDIOCPLAYBLOCKS\n")); 2051168752Sscottl 2052168752Sscottl 2053111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 205439213Sgibbs if (error) { 2055111206Sken free(params.mode_buf, M_TEMP); 2056168752Sscottl cam_periph_unlock(periph); 205739213Sgibbs break; 205839213Sgibbs } 2059111206Sken page = cdgetpage(¶ms); 2060111206Sken 2061111206Sken page->audio.flags &= ~CD_PA_SOTC; 2062111206Sken page->audio.flags |= CD_PA_IMMED; 2063111206Sken error = cdsetmode(periph, ¶ms); 2064111206Sken free(params.mode_buf, M_TEMP); 2065168752Sscottl if (error) { 2066168752Sscottl cam_periph_unlock(periph); 206739213Sgibbs break; 2068168752Sscottl } 206939213Sgibbs error = cdplay(periph, args->blk, args->len); 2070168752Sscottl cam_periph_unlock(periph); 207139213Sgibbs } 207239213Sgibbs break; 2073141031Ssobomax case CDIOCREADSUBCHANNEL_SYSSPACE: 2074141031Ssobomax nocopyout = 1; 2075141031Ssobomax /* Fallthrough */ 207639213Sgibbs case CDIOCREADSUBCHANNEL: 207739213Sgibbs { 207839213Sgibbs struct ioc_read_subchannel *args 207939213Sgibbs = (struct ioc_read_subchannel *) addr; 208039213Sgibbs struct cd_sub_channel_info *data; 208139213Sgibbs u_int32_t len = args->data_len; 208239213Sgibbs 2083168752Sscottl data = malloc(sizeof(struct cd_sub_channel_info), 2084168752Sscottl M_TEMP, M_WAITOK); 2085168752Sscottl 2086168752Sscottl cam_periph_lock(periph); 208739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 208839213Sgibbs ("trying to do CDIOCREADSUBCHANNEL\n")); 208939213Sgibbs 209039213Sgibbs if ((len > sizeof(struct cd_sub_channel_info)) || 209139213Sgibbs (len < sizeof(struct cd_sub_channel_header))) { 209239213Sgibbs printf( 209339213Sgibbs "scsi_cd: cdioctl: " 209439213Sgibbs "cdioreadsubchannel: error, len=%d\n", 209539213Sgibbs len); 209639213Sgibbs error = EINVAL; 209739213Sgibbs free(data, M_TEMP); 2098168752Sscottl cam_periph_unlock(periph); 209939213Sgibbs break; 210039213Sgibbs } 210139213Sgibbs 210239213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 210339213Sgibbs args->track = bin2bcd(args->track); 210439213Sgibbs 210539213Sgibbs error = cdreadsubchannel(periph, args->address_format, 210639213Sgibbs args->data_format, args->track, data, len); 210739213Sgibbs 210839213Sgibbs if (error) { 210939213Sgibbs free(data, M_TEMP); 2110168752Sscottl cam_periph_unlock(periph); 211139213Sgibbs break; 211239213Sgibbs } 211339213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 211439213Sgibbs data->what.track_info.track_number = 211539213Sgibbs bcd2bin(data->what.track_info.track_number); 211639213Sgibbs len = min(len, ((data->header.data_len[0] << 8) + 211739213Sgibbs data->header.data_len[1] + 211839213Sgibbs sizeof(struct cd_sub_channel_header))); 2119168752Sscottl cam_periph_unlock(periph); 2120141031Ssobomax if (nocopyout == 0) { 2121141031Ssobomax if (copyout(data, args->data, len) != 0) { 2122141031Ssobomax error = EFAULT; 2123141031Ssobomax } 2124141031Ssobomax } else { 2125141031Ssobomax bcopy(data, args->data, len); 212639213Sgibbs } 212739213Sgibbs free(data, M_TEMP); 212839213Sgibbs } 212939213Sgibbs break; 213039213Sgibbs 213139213Sgibbs case CDIOREADTOCHEADER: 213239213Sgibbs { 213339213Sgibbs struct ioc_toc_header *th; 213439213Sgibbs 2135168752Sscottl th = malloc(sizeof(struct ioc_toc_header), M_TEMP, 2136168752Sscottl M_WAITOK); 2137168752Sscottl 2138168752Sscottl cam_periph_lock(periph); 213939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 214039213Sgibbs ("trying to do CDIOREADTOCHEADER\n")); 214139213Sgibbs 2142111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2143111206Sken sizeof (*th), /*sense_flags*/0); 214439213Sgibbs if (error) { 214539213Sgibbs free(th, M_TEMP); 2146168752Sscottl cam_periph_unlock(periph); 214739213Sgibbs break; 214839213Sgibbs } 214939213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 215039213Sgibbs /* we are going to have to convert the BCD 215139213Sgibbs * encoding on the cd to what is expected 215239213Sgibbs */ 215339213Sgibbs th->starting_track = 215439213Sgibbs bcd2bin(th->starting_track); 215539213Sgibbs th->ending_track = bcd2bin(th->ending_track); 215639213Sgibbs } 215790868Smike th->len = ntohs(th->len); 215839213Sgibbs bcopy(th, addr, sizeof(*th)); 215939213Sgibbs free(th, M_TEMP); 2160168752Sscottl cam_periph_unlock(periph); 216139213Sgibbs } 216239213Sgibbs break; 216339213Sgibbs case CDIOREADTOCENTRYS: 216439213Sgibbs { 2165111206Sken struct cd_tocdata *data; 2166111206Sken struct cd_toc_single *lead; 216739213Sgibbs struct ioc_read_toc_entry *te = 216839213Sgibbs (struct ioc_read_toc_entry *) addr; 216939213Sgibbs struct ioc_toc_header *th; 217039213Sgibbs u_int32_t len, readlen, idx, num; 217139213Sgibbs u_int32_t starting_track = te->starting_track; 217239213Sgibbs 2173168752Sscottl data = malloc(sizeof(*data), M_TEMP, M_WAITOK); 2174168752Sscottl lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK); 2175168752Sscottl 2176168752Sscottl cam_periph_lock(periph); 217739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 217839213Sgibbs ("trying to do CDIOREADTOCENTRYS\n")); 217939213Sgibbs 218039213Sgibbs if (te->data_len < sizeof(struct cd_toc_entry) 218139213Sgibbs || (te->data_len % sizeof(struct cd_toc_entry)) != 0 218239213Sgibbs || (te->address_format != CD_MSF_FORMAT 218339213Sgibbs && te->address_format != CD_LBA_FORMAT)) { 218439213Sgibbs error = EINVAL; 218539213Sgibbs printf("scsi_cd: error in readtocentries, " 218639213Sgibbs "returning EINVAL\n"); 218739213Sgibbs free(data, M_TEMP); 218839213Sgibbs free(lead, M_TEMP); 2189168752Sscottl cam_periph_unlock(periph); 219039213Sgibbs break; 219139213Sgibbs } 219239213Sgibbs 219339213Sgibbs th = &data->header; 2194111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2195111206Sken sizeof (*th), /*sense_flags*/0); 219639213Sgibbs if (error) { 219739213Sgibbs free(data, M_TEMP); 219839213Sgibbs free(lead, M_TEMP); 2199168752Sscottl cam_periph_unlock(periph); 220039213Sgibbs break; 220139213Sgibbs } 220239213Sgibbs 220339213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 220439213Sgibbs /* we are going to have to convert the BCD 220539213Sgibbs * encoding on the cd to what is expected 220639213Sgibbs */ 220739213Sgibbs th->starting_track = 220839213Sgibbs bcd2bin(th->starting_track); 220939213Sgibbs th->ending_track = bcd2bin(th->ending_track); 221039213Sgibbs } 221139213Sgibbs 221239213Sgibbs if (starting_track == 0) 221339213Sgibbs starting_track = th->starting_track; 221439213Sgibbs else if (starting_track == LEADOUT) 221539213Sgibbs starting_track = th->ending_track + 1; 221639213Sgibbs else if (starting_track < th->starting_track || 221739213Sgibbs starting_track > th->ending_track + 1) { 221839213Sgibbs printf("scsi_cd: error in readtocentries, " 221939213Sgibbs "returning EINVAL\n"); 222039213Sgibbs free(data, M_TEMP); 222139213Sgibbs free(lead, M_TEMP); 2222168752Sscottl cam_periph_unlock(periph); 222339213Sgibbs error = EINVAL; 222439213Sgibbs break; 222539213Sgibbs } 222639213Sgibbs 222739213Sgibbs /* calculate reading length without leadout entry */ 222839213Sgibbs readlen = (th->ending_track - starting_track + 1) * 222939213Sgibbs sizeof(struct cd_toc_entry); 223039213Sgibbs 223139213Sgibbs /* and with leadout entry */ 223239213Sgibbs len = readlen + sizeof(struct cd_toc_entry); 223339213Sgibbs if (te->data_len < len) { 223439213Sgibbs len = te->data_len; 223539213Sgibbs if (readlen > len) 223639213Sgibbs readlen = len; 223739213Sgibbs } 223839213Sgibbs if (len > sizeof(data->entries)) { 223939213Sgibbs printf("scsi_cd: error in readtocentries, " 224039213Sgibbs "returning EINVAL\n"); 224139213Sgibbs error = EINVAL; 224239213Sgibbs free(data, M_TEMP); 224339213Sgibbs free(lead, M_TEMP); 2244168752Sscottl cam_periph_unlock(periph); 224539213Sgibbs break; 224639213Sgibbs } 224739213Sgibbs num = len / sizeof(struct cd_toc_entry); 224839213Sgibbs 224939213Sgibbs if (readlen > 0) { 225039213Sgibbs error = cdreadtoc(periph, te->address_format, 225139213Sgibbs starting_track, 2252111206Sken (u_int8_t *)data, 2253111206Sken readlen + sizeof (*th), 2254111206Sken /*sense_flags*/0); 225539213Sgibbs if (error) { 225639213Sgibbs free(data, M_TEMP); 225739213Sgibbs free(lead, M_TEMP); 2258168752Sscottl cam_periph_unlock(periph); 225939213Sgibbs break; 226039213Sgibbs } 226139213Sgibbs } 226239213Sgibbs 226339213Sgibbs /* make leadout entry if needed */ 226439213Sgibbs idx = starting_track + num - 1; 226539213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 226639213Sgibbs th->ending_track = bcd2bin(th->ending_track); 226739213Sgibbs if (idx == th->ending_track + 1) { 226839213Sgibbs error = cdreadtoc(periph, te->address_format, 2269111206Sken LEADOUT, (u_int8_t *)lead, 2270111206Sken sizeof(*lead), 2271111206Sken /*sense_flags*/0); 227239213Sgibbs if (error) { 227339213Sgibbs free(data, M_TEMP); 227439213Sgibbs free(lead, M_TEMP); 2275168752Sscottl cam_periph_unlock(periph); 227639213Sgibbs break; 227739213Sgibbs } 227839213Sgibbs data->entries[idx - starting_track] = 227939213Sgibbs lead->entry; 228039213Sgibbs } 228139213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 228239213Sgibbs for (idx = 0; idx < num - 1; idx++) { 228339213Sgibbs data->entries[idx].track = 228439213Sgibbs bcd2bin(data->entries[idx].track); 228539213Sgibbs } 228639213Sgibbs } 228739213Sgibbs 2288168752Sscottl cam_periph_unlock(periph); 228939213Sgibbs error = copyout(data->entries, te->data, len); 229039213Sgibbs free(data, M_TEMP); 229139213Sgibbs free(lead, M_TEMP); 229239213Sgibbs } 229339213Sgibbs break; 229439213Sgibbs case CDIOREADTOCENTRY: 229539213Sgibbs { 2296111206Sken struct cd_toc_single *data; 229739213Sgibbs struct ioc_read_toc_single_entry *te = 229839213Sgibbs (struct ioc_read_toc_single_entry *) addr; 229939213Sgibbs struct ioc_toc_header *th; 230039213Sgibbs u_int32_t track; 230139213Sgibbs 2302168752Sscottl data = malloc(sizeof(*data), M_TEMP, M_WAITOK); 2303168752Sscottl 2304168752Sscottl cam_periph_lock(periph); 230539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 230639213Sgibbs ("trying to do CDIOREADTOCENTRY\n")); 230739213Sgibbs 230839213Sgibbs if (te->address_format != CD_MSF_FORMAT 230939213Sgibbs && te->address_format != CD_LBA_FORMAT) { 231039213Sgibbs printf("error in readtocentry, " 231139213Sgibbs " returning EINVAL\n"); 231239213Sgibbs free(data, M_TEMP); 231339213Sgibbs error = EINVAL; 2314168752Sscottl cam_periph_unlock(periph); 231539213Sgibbs break; 231639213Sgibbs } 231739213Sgibbs 231839213Sgibbs th = &data->header; 2319111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)th, 2320111206Sken sizeof (*th), /*sense_flags*/0); 232139213Sgibbs if (error) { 232239213Sgibbs free(data, M_TEMP); 2323168752Sscottl cam_periph_unlock(periph); 232439213Sgibbs break; 232539213Sgibbs } 232639213Sgibbs 232739213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) { 232839213Sgibbs /* we are going to have to convert the BCD 232939213Sgibbs * encoding on the cd to what is expected 233039213Sgibbs */ 233139213Sgibbs th->starting_track = 233239213Sgibbs bcd2bin(th->starting_track); 233339213Sgibbs th->ending_track = bcd2bin(th->ending_track); 233439213Sgibbs } 233539213Sgibbs track = te->track; 233639213Sgibbs if (track == 0) 233739213Sgibbs track = th->starting_track; 233839213Sgibbs else if (track == LEADOUT) 233939213Sgibbs /* OK */; 234039213Sgibbs else if (track < th->starting_track || 234139213Sgibbs track > th->ending_track + 1) { 234239213Sgibbs printf("error in readtocentry, " 234339213Sgibbs " returning EINVAL\n"); 234439213Sgibbs free(data, M_TEMP); 234539213Sgibbs error = EINVAL; 2346168752Sscottl cam_periph_unlock(periph); 234739213Sgibbs break; 234839213Sgibbs } 234939213Sgibbs 235039213Sgibbs error = cdreadtoc(periph, te->address_format, track, 2351111206Sken (u_int8_t *)data, sizeof(*data), 2352111206Sken /*sense_flags*/0); 235339213Sgibbs if (error) { 235439213Sgibbs free(data, M_TEMP); 2355168752Sscottl cam_periph_unlock(periph); 235639213Sgibbs break; 235739213Sgibbs } 235839213Sgibbs 235939213Sgibbs if (softc->quirks & CD_Q_BCD_TRACKS) 236039213Sgibbs data->entry.track = bcd2bin(data->entry.track); 236139213Sgibbs bcopy(&data->entry, &te->entry, 236239213Sgibbs sizeof(struct cd_toc_entry)); 236339213Sgibbs free(data, M_TEMP); 2364168752Sscottl cam_periph_unlock(periph); 236539213Sgibbs } 236639213Sgibbs break; 236739213Sgibbs case CDIOCSETPATCH: 236839213Sgibbs { 2369111206Sken struct ioc_patch *arg = (struct ioc_patch *)addr; 2370111206Sken struct cd_mode_params params; 2371111206Sken union cd_pages *page; 237239213Sgibbs 2373168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2374168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2375168752Sscottl M_WAITOK | M_ZERO); 2376168752Sscottl 2377168752Sscottl cam_periph_lock(periph); 237839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 237939213Sgibbs ("trying to do CDIOCSETPATCH\n")); 238039213Sgibbs 2381111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 238239213Sgibbs if (error) { 2383111206Sken free(params.mode_buf, M_TEMP); 2384168752Sscottl cam_periph_unlock(periph); 238539213Sgibbs break; 238639213Sgibbs } 2387111206Sken page = cdgetpage(¶ms); 2388111206Sken 2389111206Sken page->audio.port[LEFT_PORT].channels = 239039213Sgibbs arg->patch[0]; 2391111206Sken page->audio.port[RIGHT_PORT].channels = 239239213Sgibbs arg->patch[1]; 2393111206Sken page->audio.port[2].channels = arg->patch[2]; 2394111206Sken page->audio.port[3].channels = arg->patch[3]; 2395111206Sken error = cdsetmode(periph, ¶ms); 2396111206Sken free(params.mode_buf, M_TEMP); 2397168752Sscottl cam_periph_unlock(periph); 239839213Sgibbs } 239939213Sgibbs break; 240039213Sgibbs case CDIOCGETVOL: 240139213Sgibbs { 240239213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2403111206Sken struct cd_mode_params params; 2404111206Sken union cd_pages *page; 240539213Sgibbs 2406168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2407168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2408168752Sscottl M_WAITOK | M_ZERO); 2409168752Sscottl 2410168752Sscottl cam_periph_lock(periph); 241139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 241239213Sgibbs ("trying to do CDIOCGETVOL\n")); 241339213Sgibbs 2414111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 241539213Sgibbs if (error) { 2416111206Sken free(params.mode_buf, M_TEMP); 2417168752Sscottl cam_periph_unlock(periph); 241839213Sgibbs break; 241939213Sgibbs } 2420111206Sken page = cdgetpage(¶ms); 2421111206Sken 242239213Sgibbs arg->vol[LEFT_PORT] = 2423111206Sken page->audio.port[LEFT_PORT].volume; 242439213Sgibbs arg->vol[RIGHT_PORT] = 2425111206Sken page->audio.port[RIGHT_PORT].volume; 2426111206Sken arg->vol[2] = page->audio.port[2].volume; 2427111206Sken arg->vol[3] = page->audio.port[3].volume; 2428111206Sken free(params.mode_buf, M_TEMP); 2429168752Sscottl cam_periph_unlock(periph); 243039213Sgibbs } 243139213Sgibbs break; 243239213Sgibbs case CDIOCSETVOL: 243339213Sgibbs { 243439213Sgibbs struct ioc_vol *arg = (struct ioc_vol *) addr; 2435111206Sken struct cd_mode_params params; 2436111206Sken union cd_pages *page; 243739213Sgibbs 2438168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2439168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2440168752Sscottl M_WAITOK | M_ZERO); 2441168752Sscottl 2442168752Sscottl cam_periph_lock(periph); 244339213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 244439213Sgibbs ("trying to do CDIOCSETVOL\n")); 244539213Sgibbs 2446111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 244739213Sgibbs if (error) { 2448111206Sken free(params.mode_buf, M_TEMP); 2449168752Sscottl cam_periph_unlock(periph); 245039213Sgibbs break; 245139213Sgibbs } 2452111206Sken page = cdgetpage(¶ms); 2453111206Sken 2454111206Sken page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2455111206Sken page->audio.port[LEFT_PORT].volume = 245639213Sgibbs arg->vol[LEFT_PORT]; 2457111206Sken page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2458111206Sken page->audio.port[RIGHT_PORT].volume = 245939213Sgibbs arg->vol[RIGHT_PORT]; 2460111206Sken page->audio.port[2].volume = arg->vol[2]; 2461111206Sken page->audio.port[3].volume = arg->vol[3]; 2462111206Sken error = cdsetmode(periph, ¶ms); 2463168752Sscottl cam_periph_unlock(periph); 2464111206Sken free(params.mode_buf, M_TEMP); 246539213Sgibbs } 246639213Sgibbs break; 246739213Sgibbs case CDIOCSETMONO: 246839213Sgibbs { 2469111206Sken struct cd_mode_params params; 2470111206Sken union cd_pages *page; 247139213Sgibbs 2472168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2473168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2474168752Sscottl M_WAITOK | M_ZERO); 2475168752Sscottl 2476168752Sscottl cam_periph_lock(periph); 247739213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 247839213Sgibbs ("trying to do CDIOCSETMONO\n")); 247939213Sgibbs 2480111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 248139213Sgibbs if (error) { 2482111206Sken free(params.mode_buf, M_TEMP); 2483168752Sscottl cam_periph_unlock(periph); 248439213Sgibbs break; 248539213Sgibbs } 2486111206Sken page = cdgetpage(¶ms); 2487111206Sken 2488111206Sken page->audio.port[LEFT_PORT].channels = 248939213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2490111206Sken page->audio.port[RIGHT_PORT].channels = 249139213Sgibbs LEFT_CHANNEL | RIGHT_CHANNEL; 2492111206Sken page->audio.port[2].channels = 0; 2493111206Sken page->audio.port[3].channels = 0; 2494111206Sken error = cdsetmode(periph, ¶ms); 2495168752Sscottl cam_periph_unlock(periph); 2496111206Sken free(params.mode_buf, M_TEMP); 249739213Sgibbs } 249839213Sgibbs break; 249939213Sgibbs case CDIOCSETSTEREO: 250039213Sgibbs { 2501111206Sken struct cd_mode_params params; 2502111206Sken union cd_pages *page; 250339213Sgibbs 2504168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2505168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2506168752Sscottl M_WAITOK | M_ZERO); 2507168752Sscottl 2508168752Sscottl cam_periph_lock(periph); 250939213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 251039213Sgibbs ("trying to do CDIOCSETSTEREO\n")); 251139213Sgibbs 2512111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 251339213Sgibbs if (error) { 2514111206Sken free(params.mode_buf, M_TEMP); 2515168752Sscottl cam_periph_unlock(periph); 251639213Sgibbs break; 251739213Sgibbs } 2518111206Sken page = cdgetpage(¶ms); 2519111206Sken 2520111206Sken page->audio.port[LEFT_PORT].channels = 252139213Sgibbs LEFT_CHANNEL; 2522111206Sken page->audio.port[RIGHT_PORT].channels = 252339213Sgibbs RIGHT_CHANNEL; 2524111206Sken page->audio.port[2].channels = 0; 2525111206Sken page->audio.port[3].channels = 0; 2526111206Sken error = cdsetmode(periph, ¶ms); 2527111206Sken free(params.mode_buf, M_TEMP); 2528168752Sscottl cam_periph_unlock(periph); 252939213Sgibbs } 253039213Sgibbs break; 253139213Sgibbs case CDIOCSETMUTE: 253239213Sgibbs { 2533111206Sken struct cd_mode_params params; 2534111206Sken union cd_pages *page; 253539213Sgibbs 2536168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2537168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2538168752Sscottl M_WAITOK | M_ZERO); 2539168752Sscottl 2540168752Sscottl cam_periph_lock(periph); 254139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 254239213Sgibbs ("trying to do CDIOCSETMUTE\n")); 254339213Sgibbs 2544111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 254539213Sgibbs if (error) { 2546111206Sken free(¶ms, M_TEMP); 2547168752Sscottl cam_periph_unlock(periph); 254839213Sgibbs break; 254939213Sgibbs } 2550111206Sken page = cdgetpage(¶ms); 2551111206Sken 2552111206Sken page->audio.port[LEFT_PORT].channels = 0; 2553111206Sken page->audio.port[RIGHT_PORT].channels = 0; 2554111206Sken page->audio.port[2].channels = 0; 2555111206Sken page->audio.port[3].channels = 0; 2556111206Sken error = cdsetmode(periph, ¶ms); 2557111206Sken free(params.mode_buf, M_TEMP); 2558168752Sscottl cam_periph_unlock(periph); 255939213Sgibbs } 256039213Sgibbs break; 256139213Sgibbs case CDIOCSETLEFT: 256239213Sgibbs { 2563111206Sken struct cd_mode_params params; 2564111206Sken union cd_pages *page; 256539213Sgibbs 2566168752Sscottl params.alloc_len = sizeof(union cd_mode_data_6_10); 2567168752Sscottl params.mode_buf = malloc(params.alloc_len, M_TEMP, 2568168752Sscottl M_WAITOK | M_ZERO); 2569168752Sscottl 2570168752Sscottl cam_periph_lock(periph); 257139213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 257239213Sgibbs ("trying to do CDIOCSETLEFT\n")); 257339213Sgibbs 2574111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 257539213Sgibbs if (error) { 2576111206Sken free(params.mode_buf, M_TEMP); 2577168752Sscottl cam_periph_unlock(periph); 257839213Sgibbs break; 257939213Sgibbs } 2580111206Sken page = cdgetpage(¶ms); 2581111206Sken 2582111206Sken page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2583111206Sken page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2584111206Sken page->audio.port[2].channels = 0; 2585111206Sken page->audio.port[3].channels = 0; 2586111206Sken error = cdsetmode(periph, ¶ms); 2587111206Sken free(params.mode_buf, M_TEMP); 2588168752Sscottl cam_periph_unlock(periph); 258939213Sgibbs } 259039213Sgibbs break; 259139213Sgibbs case CDIOCSETRIGHT: 259239213Sgibbs { 2593111206Sken struct cd_mode_params params; 2594111206Sken union cd_pages *page; 259539213Sgibbs 2596111206Sken params.alloc_len = sizeof(union cd_mode_data_6_10); 2597111206Sken params.mode_buf = malloc(params.alloc_len, M_TEMP, 2598111206Sken M_WAITOK | M_ZERO); 2599111206Sken 2600168752Sscottl cam_periph_lock(periph); 2601168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2602168752Sscottl ("trying to do CDIOCSETRIGHT\n")); 2603168752Sscottl 2604111206Sken error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 260539213Sgibbs if (error) { 2606111206Sken free(params.mode_buf, M_TEMP); 2607168752Sscottl cam_periph_unlock(periph); 260839213Sgibbs break; 260939213Sgibbs } 2610111206Sken page = cdgetpage(¶ms); 2611111206Sken 2612111206Sken page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2613111206Sken page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2614111206Sken page->audio.port[2].channels = 0; 2615111206Sken page->audio.port[3].channels = 0; 2616111206Sken error = cdsetmode(periph, ¶ms); 2617111206Sken free(params.mode_buf, M_TEMP); 2618168752Sscottl cam_periph_unlock(periph); 261939213Sgibbs } 262039213Sgibbs break; 262139213Sgibbs case CDIOCRESUME: 2622168752Sscottl cam_periph_lock(periph); 262339213Sgibbs error = cdpause(periph, 1); 2624168752Sscottl cam_periph_unlock(periph); 262539213Sgibbs break; 262639213Sgibbs case CDIOCPAUSE: 2627168752Sscottl cam_periph_lock(periph); 262839213Sgibbs error = cdpause(periph, 0); 2629168752Sscottl cam_periph_unlock(periph); 263039213Sgibbs break; 263139213Sgibbs case CDIOCSTART: 2632168752Sscottl cam_periph_lock(periph); 2633111206Sken error = cdstartunit(periph, 0); 2634168752Sscottl cam_periph_unlock(periph); 263539213Sgibbs break; 2636111206Sken case CDIOCCLOSE: 2637168752Sscottl cam_periph_lock(periph); 2638111206Sken error = cdstartunit(periph, 1); 2639168752Sscottl cam_periph_unlock(periph); 2640111206Sken break; 264139213Sgibbs case CDIOCSTOP: 2642168752Sscottl cam_periph_lock(periph); 264339213Sgibbs error = cdstopunit(periph, 0); 2644168752Sscottl cam_periph_unlock(periph); 264539213Sgibbs break; 264639213Sgibbs case CDIOCEJECT: 2647168752Sscottl cam_periph_lock(periph); 264839213Sgibbs error = cdstopunit(periph, 1); 2649168752Sscottl cam_periph_unlock(periph); 265039213Sgibbs break; 265139213Sgibbs case CDIOCALLOW: 2652168752Sscottl cam_periph_lock(periph); 265339213Sgibbs cdprevent(periph, PR_ALLOW); 2654168752Sscottl cam_periph_unlock(periph); 265539213Sgibbs break; 265639213Sgibbs case CDIOCPREVENT: 2657168752Sscottl cam_periph_lock(periph); 265839213Sgibbs cdprevent(periph, PR_PREVENT); 2659168752Sscottl cam_periph_unlock(periph); 266039213Sgibbs break; 266139213Sgibbs case CDIOCSETDEBUG: 266239213Sgibbs /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 266339213Sgibbs error = ENOTTY; 266439213Sgibbs break; 266539213Sgibbs case CDIOCCLRDEBUG: 266639213Sgibbs /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 266739213Sgibbs error = ENOTTY; 266839213Sgibbs break; 266939213Sgibbs case CDIOCRESET: 267039213Sgibbs /* return (cd_reset(periph)); */ 267139213Sgibbs error = ENOTTY; 267239213Sgibbs break; 2673105421Snjl case CDRIOCREADSPEED: 2674168752Sscottl cam_periph_lock(periph); 2675105421Snjl error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED); 2676168752Sscottl cam_periph_unlock(periph); 2677105421Snjl break; 2678105421Snjl case CDRIOCWRITESPEED: 2679168752Sscottl cam_periph_lock(periph); 2680105421Snjl error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr); 2681168752Sscottl cam_periph_unlock(periph); 2682105421Snjl break; 268360422Sken case DVDIOCSENDKEY: 268460422Sken case DVDIOCREPORTKEY: { 268560422Sken struct dvd_authinfo *authinfo; 268660422Sken 268760422Sken authinfo = (struct dvd_authinfo *)addr; 268860422Sken 2689168752Sscottl cam_periph_lock(periph); 269060422Sken if (cmd == DVDIOCREPORTKEY) 269160422Sken error = cdreportkey(periph, authinfo); 269260422Sken else 269360422Sken error = cdsendkey(periph, authinfo); 2694168752Sscottl cam_periph_unlock(periph); 269560422Sken break; 2696104456Sphk } 269760422Sken case DVDIOCREADSTRUCTURE: { 269860422Sken struct dvd_struct *dvdstruct; 269960422Sken 270060422Sken dvdstruct = (struct dvd_struct *)addr; 270160422Sken 2702168752Sscottl cam_periph_lock(periph); 270360422Sken error = cdreaddvdstructure(periph, dvdstruct); 2704168752Sscottl cam_periph_unlock(periph); 270560422Sken 270660422Sken break; 270760422Sken } 270839213Sgibbs default: 2709168752Sscottl cam_periph_lock(periph); 271039213Sgibbs error = cam_periph_ioctl(periph, cmd, addr, cderror); 2711168752Sscottl cam_periph_unlock(periph); 271239213Sgibbs break; 271339213Sgibbs } 271439213Sgibbs 2715168752Sscottl cam_periph_lock(periph); 2716168752Sscottl cam_periph_unhold(periph); 2717168752Sscottl 271839213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2719104456Sphk if (error && bootverbose) { 2720104456Sphk printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2721104456Sphk } 2722168752Sscottl cam_periph_unlock(periph); 272339213Sgibbs 272439213Sgibbs return (error); 272539213Sgibbs} 272639213Sgibbs 272739213Sgibbsstatic void 272839213Sgibbscdprevent(struct cam_periph *periph, int action) 272939213Sgibbs{ 273039213Sgibbs union ccb *ccb; 273139213Sgibbs struct cd_softc *softc; 273239213Sgibbs int error; 273339213Sgibbs 273439213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 273539213Sgibbs 273639213Sgibbs softc = (struct cd_softc *)periph->softc; 273739213Sgibbs 273839213Sgibbs if (((action == PR_ALLOW) 273939213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 274039213Sgibbs || ((action == PR_PREVENT) 274139213Sgibbs && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 274239213Sgibbs return; 274339213Sgibbs } 274439213Sgibbs 274539213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 274639213Sgibbs 274739213Sgibbs scsi_prevent(&ccb->csio, 274839213Sgibbs /*retries*/ 1, 274939213Sgibbs cddone, 275039213Sgibbs MSG_SIMPLE_Q_TAG, 275139213Sgibbs action, 275239213Sgibbs SSD_FULL_SIZE, 275339213Sgibbs /* timeout */60000); 275439213Sgibbs 275574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 275674840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 275739213Sgibbs 275839213Sgibbs xpt_release_ccb(ccb); 275939213Sgibbs 276039213Sgibbs if (error == 0) { 276139213Sgibbs if (action == PR_ALLOW) 276239213Sgibbs softc->flags &= ~CD_FLAG_DISC_LOCKED; 276339213Sgibbs else 276439213Sgibbs softc->flags |= CD_FLAG_DISC_LOCKED; 276539213Sgibbs } 276639213Sgibbs} 276739213Sgibbs 2768120599Sphk/* 2769120599Sphk * XXX: the disk media and sector size is only really able to change 2770120599Sphk * XXX: while the device is closed. 2771120599Sphk */ 277239213Sgibbsstatic int 2773111206Skencdcheckmedia(struct cam_periph *periph) 277439213Sgibbs{ 277539213Sgibbs struct cd_softc *softc; 2776111206Sken struct ioc_toc_header *toch; 2777111206Sken struct cd_toc_single leadout; 2778111206Sken u_int32_t size, toclen; 2779111206Sken int error, num_entries, cdindex; 2780111206Sken 2781111206Sken softc = (struct cd_softc *)periph->softc; 2782111206Sken 2783111206Sken cdprevent(periph, PR_PREVENT); 2784125975Sphk softc->disk->d_maxsize = DFLTPHYS; 2785134824Sphk softc->disk->d_sectorsize = 2048; 2786125975Sphk softc->disk->d_mediasize = 0; 2787111206Sken 2788111206Sken /* 2789111206Sken * Get the disc size and block size. If we can't get it, we don't 2790111206Sken * have media, most likely. 2791111206Sken */ 2792111206Sken if ((error = cdsize(periph, &size)) != 0) { 2793111206Sken softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 2794111206Sken cdprevent(periph, PR_ALLOW); 2795111206Sken return (error); 2796111206Sken } else 2797111206Sken softc->flags |= CD_FLAG_VALID_MEDIA; 2798111206Sken 2799111206Sken /* 2800111206Sken * Now we check the table of contents. This (currently) is only 2801111206Sken * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do 2802111206Sken * things like present a separate entry in /dev for each track, 2803111206Sken * like that acd(4) driver does. 2804111206Sken */ 2805111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2806111206Sken toch = &softc->toc.header; 2807111206Sken /* 2808111206Sken * We will get errors here for media that doesn't have a table of 2809111206Sken * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP 2810111206Sken * command is presented for a DDCD/CD-R/RW media, where the first TOC 2811111206Sken * has not been recorded (no complete session) and the Format codes 2812111206Sken * 0000b, 0001b, or 0010b are specified, this command shall be rejected 2813111206Sken * with an INVALID FIELD IN CDB. Devices that are not capable of 2814111206Sken * reading an incomplete session on DDC/CD-R/RW media shall report 2815111206Sken * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 2816111206Sken * 2817111206Sken * So this isn't fatal if we can't read the table of contents, it 2818111206Sken * just means that the user won't be able to issue the play tracks 2819111206Sken * ioctl, and likely lots of other stuff won't work either. They 2820111206Sken * need to burn the CD before we can do a whole lot with it. So 2821111206Sken * we don't print anything here if we get an error back. 2822111206Sken */ 2823111206Sken error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch), 2824111206Sken SF_NO_PRINT); 2825111206Sken /* 2826111206Sken * Errors in reading the table of contents aren't fatal, we just 2827111206Sken * won't have a valid table of contents cached. 2828111206Sken */ 2829111206Sken if (error != 0) { 2830111206Sken error = 0; 2831111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2832111206Sken goto bailout; 2833111206Sken } 2834111206Sken 2835111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2836111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2837111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2838111206Sken } 2839111206Sken 2840111206Sken /* Number of TOC entries, plus leadout */ 2841111206Sken num_entries = (toch->ending_track - toch->starting_track) + 2; 2842111206Sken 2843111206Sken if (num_entries <= 0) 2844111206Sken goto bailout; 2845111206Sken 2846111206Sken toclen = num_entries * sizeof(struct cd_toc_entry); 2847111206Sken 2848111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track, 2849111206Sken (u_int8_t *)&softc->toc, toclen + sizeof(*toch), 2850111206Sken SF_NO_PRINT); 2851111206Sken if (error != 0) { 2852111206Sken error = 0; 2853111206Sken bzero(&softc->toc, sizeof(softc->toc)); 2854111206Sken goto bailout; 2855111206Sken } 2856111206Sken 2857111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2858111206Sken toch->starting_track = bcd2bin(toch->starting_track); 2859111206Sken toch->ending_track = bcd2bin(toch->ending_track); 2860111206Sken } 2861111206Sken /* 2862111206Sken * XXX KDM is this necessary? Probably only if the drive doesn't 2863111206Sken * return leadout information with the table of contents. 2864111206Sken */ 2865111206Sken cdindex = toch->starting_track + num_entries -1; 2866111206Sken if (cdindex == toch->ending_track + 1) { 2867111206Sken 2868111206Sken error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT, 2869111206Sken (u_int8_t *)&leadout, sizeof(leadout), 2870111206Sken SF_NO_PRINT); 2871111206Sken if (error != 0) { 2872111206Sken error = 0; 2873111206Sken goto bailout; 2874111206Sken } 2875111206Sken softc->toc.entries[cdindex - toch->starting_track] = 2876111206Sken leadout.entry; 2877111206Sken } 2878111206Sken if (softc->quirks & CD_Q_BCD_TRACKS) { 2879111206Sken for (cdindex = 0; cdindex < num_entries - 1; cdindex++) { 2880111206Sken softc->toc.entries[cdindex].track = 2881111206Sken bcd2bin(softc->toc.entries[cdindex].track); 2882111206Sken } 2883111206Sken } 2884111206Sken 2885111206Sken softc->flags |= CD_FLAG_VALID_TOC; 2886125975Sphk softc->disk->d_maxsize = DFLTPHYS; 2887125975Sphk softc->disk->d_sectorsize = softc->params.blksize; 2888125975Sphk softc->disk->d_mediasize = 2889120599Sphk (off_t)softc->params.blksize * softc->params.disksize; 2890111206Sken 2891111206Skenbailout: 2892111206Sken 2893111206Sken /* 2894111206Sken * We unconditionally (re)set the blocksize each time the 2895111206Sken * CD device is opened. This is because the CD can change, 2896111206Sken * and therefore the blocksize might change. 2897111206Sken * XXX problems here if some slice or partition is still 2898111206Sken * open with the old size? 2899111206Sken */ 2900125975Sphk if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0) 2901125975Sphk softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2902125975Sphk softc->disk->d_devstat->block_size = softc->params.blksize; 2903111206Sken 2904111206Sken return (error); 2905111206Sken} 2906111206Sken 2907111206Skenstatic int 2908111206Skencdsize(struct cam_periph *periph, u_int32_t *size) 2909111206Sken{ 2910111206Sken struct cd_softc *softc; 291139213Sgibbs union ccb *ccb; 291239213Sgibbs struct scsi_read_capacity_data *rcap_buf; 291339213Sgibbs int error; 291439213Sgibbs 291539213Sgibbs CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n")); 291639213Sgibbs 291739213Sgibbs softc = (struct cd_softc *)periph->softc; 291839213Sgibbs 291939213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 292039213Sgibbs 2921168752Sscottl /* XXX Should be M_WAITOK */ 292239213Sgibbs rcap_buf = malloc(sizeof(struct scsi_read_capacity_data), 2923168752Sscottl M_TEMP, M_NOWAIT); 2924168752Sscottl if (rcap_buf == NULL) 2925168752Sscottl return (ENOMEM); 292639213Sgibbs 292739213Sgibbs scsi_read_capacity(&ccb->csio, 292839213Sgibbs /*retries*/ 1, 292939213Sgibbs cddone, 293039213Sgibbs MSG_SIMPLE_Q_TAG, 293139213Sgibbs rcap_buf, 293239213Sgibbs SSD_FULL_SIZE, 293339213Sgibbs /* timeout */20000); 293439213Sgibbs 293574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 293674840Sken /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 293739213Sgibbs 293839213Sgibbs xpt_release_ccb(ccb); 293939213Sgibbs 294039213Sgibbs softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1; 294139213Sgibbs softc->params.blksize = scsi_4btoul(rcap_buf->length); 294260806Sjoerg /* 294360806Sjoerg * SCSI-3 mandates that the reported blocksize shall be 2048. 294460806Sjoerg * Older drives sometimes report funny values, trim it down to 294560806Sjoerg * 2048, or other parts of the kernel will get confused. 294660806Sjoerg * 294760806Sjoerg * XXX we leave drives alone that might report 512 bytes, as 294860806Sjoerg * well as drives reporting more weird sizes like perhaps 4K. 294960806Sjoerg */ 295060806Sjoerg if (softc->params.blksize > 2048 && softc->params.blksize <= 2352) 295160806Sjoerg softc->params.blksize = 2048; 295239213Sgibbs 295339213Sgibbs free(rcap_buf, M_TEMP); 295439213Sgibbs *size = softc->params.disksize; 295539213Sgibbs 295639213Sgibbs return (error); 295739213Sgibbs 295839213Sgibbs} 295939213Sgibbs 296039213Sgibbsstatic int 2961111206Skencd6byteworkaround(union ccb *ccb) 2962111206Sken{ 2963111206Sken u_int8_t *cdb; 2964111206Sken struct cam_periph *periph; 2965111206Sken struct cd_softc *softc; 2966111206Sken struct cd_mode_params *params; 2967111206Sken int frozen, found; 2968111206Sken 2969111206Sken periph = xpt_path_periph(ccb->ccb_h.path); 2970111206Sken softc = (struct cd_softc *)periph->softc; 2971111206Sken 2972111206Sken cdb = ccb->csio.cdb_io.cdb_bytes; 2973111206Sken 2974111206Sken if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2975111206Sken || ((cdb[0] != MODE_SENSE_6) 2976111206Sken && (cdb[0] != MODE_SELECT_6))) 2977111206Sken return (0); 2978111206Sken 2979111206Sken /* 2980111206Sken * Because there is no convenient place to stash the overall 2981111206Sken * cd_mode_params structure pointer, we have to grab it like this. 2982111206Sken * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2983111206Sken * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2984111206Sken * 2985111206Sken * XXX It would be nice if, at some point, we could increase the 2986111206Sken * number of available peripheral private pointers. Both pointers 2987111206Sken * are currently used in most every peripheral driver. 2988111206Sken */ 2989111206Sken found = 0; 2990111206Sken 2991111206Sken STAILQ_FOREACH(params, &softc->mode_queue, links) { 2992111206Sken if (params->mode_buf == ccb->csio.data_ptr) { 2993111206Sken found = 1; 2994111206Sken break; 2995111206Sken } 2996111206Sken } 2997111206Sken 2998111206Sken /* 2999111206Sken * This shouldn't happen. All mode sense and mode select 3000111206Sken * operations in the cd(4) driver MUST go through cdgetmode() and 3001111206Sken * cdsetmode()! 3002111206Sken */ 3003111206Sken if (found == 0) { 3004164906Smjacob xpt_print(periph->path, 3005164906Smjacob "mode buffer not found in mode queue!\n"); 3006111206Sken return (0); 3007111206Sken } 3008111206Sken 3009111206Sken params->cdb_size = 10; 3010111206Sken softc->minimum_command_size = 10; 3011164906Smjacob xpt_print(ccb->ccb_h.path, 3012164906Smjacob "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 3013164906Smjacob (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 3014111206Sken 3015111206Sken if (cdb[0] == MODE_SENSE_6) { 3016111206Sken struct scsi_mode_sense_10 ms10; 3017111206Sken struct scsi_mode_sense_6 *ms6; 3018111206Sken int len; 3019111206Sken 3020111206Sken ms6 = (struct scsi_mode_sense_6 *)cdb; 3021111206Sken 3022111206Sken bzero(&ms10, sizeof(ms10)); 3023111206Sken ms10.opcode = MODE_SENSE_10; 3024111206Sken ms10.byte2 = ms6->byte2; 3025111206Sken ms10.page = ms6->page; 3026111206Sken 3027111206Sken /* 3028111206Sken * 10 byte mode header, block descriptor, 3029111206Sken * sizeof(union cd_pages) 3030111206Sken */ 3031111206Sken len = sizeof(struct cd_mode_data_10); 3032111206Sken ccb->csio.dxfer_len = len; 3033111206Sken 3034111206Sken scsi_ulto2b(len, ms10.length); 3035111206Sken ms10.control = ms6->control; 3036111206Sken bcopy(&ms10, cdb, 10); 3037111206Sken ccb->csio.cdb_len = 10; 3038111206Sken } else { 3039111206Sken struct scsi_mode_select_10 ms10; 3040111206Sken struct scsi_mode_select_6 *ms6; 3041111206Sken struct scsi_mode_header_6 *header6; 3042111206Sken struct scsi_mode_header_10 *header10; 3043111206Sken struct scsi_mode_page_header *page_header; 3044111206Sken int blk_desc_len, page_num, page_size, len; 3045111206Sken 3046111206Sken ms6 = (struct scsi_mode_select_6 *)cdb; 3047111206Sken 3048111206Sken bzero(&ms10, sizeof(ms10)); 3049111206Sken ms10.opcode = MODE_SELECT_10; 3050111206Sken ms10.byte2 = ms6->byte2; 3051111206Sken 3052111206Sken header6 = (struct scsi_mode_header_6 *)params->mode_buf; 3053111206Sken header10 = (struct scsi_mode_header_10 *)params->mode_buf; 3054111206Sken 3055111206Sken page_header = find_mode_page_6(header6); 3056111206Sken page_num = page_header->page_code; 3057111206Sken 3058111206Sken blk_desc_len = header6->blk_desc_len; 3059111206Sken 3060111206Sken page_size = cdgetpagesize(page_num); 3061111206Sken 3062111206Sken if (page_size != (page_header->page_length + 3063111206Sken sizeof(*page_header))) 3064111206Sken page_size = page_header->page_length + 3065111206Sken sizeof(*page_header); 3066111206Sken 3067111206Sken len = sizeof(*header10) + blk_desc_len + page_size; 3068111206Sken 3069111206Sken len = min(params->alloc_len, len); 3070111206Sken 3071111206Sken /* 3072111206Sken * Since the 6 byte parameter header is shorter than the 10 3073111206Sken * byte parameter header, we need to copy the actual mode 3074111206Sken * page data, and the block descriptor, if any, so things wind 3075111206Sken * up in the right place. The regions will overlap, but 3076111206Sken * bcopy() does the right thing. 3077111206Sken */ 3078111206Sken bcopy(params->mode_buf + sizeof(*header6), 3079111206Sken params->mode_buf + sizeof(*header10), 3080111206Sken len - sizeof(*header10)); 3081111206Sken 3082111206Sken /* Make sure these fields are set correctly. */ 3083111206Sken scsi_ulto2b(0, header10->data_length); 3084111206Sken header10->medium_type = 0; 3085111206Sken scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 3086111206Sken 3087111206Sken ccb->csio.dxfer_len = len; 3088111206Sken 3089111206Sken scsi_ulto2b(len, ms10.length); 3090111206Sken ms10.control = ms6->control; 3091111206Sken bcopy(&ms10, cdb, 10); 3092111206Sken ccb->csio.cdb_len = 10; 3093111206Sken } 3094111206Sken 3095111206Sken frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 3096111206Sken ccb->ccb_h.status = CAM_REQUEUE_REQ; 3097111206Sken xpt_action(ccb); 3098111206Sken if (frozen) { 3099111206Sken cam_release_devq(ccb->ccb_h.path, 3100111206Sken /*relsim_flags*/0, 3101111206Sken /*openings*/0, 3102111206Sken /*timeout*/0, 3103111206Sken /*getcount_only*/0); 3104111206Sken } 3105111206Sken 3106111206Sken return (ERESTART); 3107111206Sken} 3108111206Sken 3109111206Skenstatic int 311039213Sgibbscderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 311139213Sgibbs{ 311239213Sgibbs struct cd_softc *softc; 311339213Sgibbs struct cam_periph *periph; 3114111206Sken int error; 311539213Sgibbs 311639213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 311739213Sgibbs softc = (struct cd_softc *)periph->softc; 311839213Sgibbs 3119111206Sken error = 0; 3120111206Sken 312139514Sgibbs /* 3122111206Sken * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 3123111206Sken * CDB comes back with this particular error, try transforming it 3124111206Sken * into the 10 byte version. 3125111206Sken */ 3126111206Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 3127111206Sken error = cd6byteworkaround(ccb); 3128111206Sken } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 3129111206Sken CAM_SCSI_STATUS_ERROR) 3130111206Sken && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 3131111206Sken && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 3132111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 3133111206Sken && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 3134111206Sken int sense_key, error_code, asc, ascq; 3135111206Sken 3136111206Sken scsi_extract_sense(&ccb->csio.sense_data, 3137111206Sken &error_code, &sense_key, &asc, &ascq); 3138111206Sken if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 3139111206Sken error = cd6byteworkaround(ccb); 3140111206Sken } 3141111206Sken 3142111206Sken if (error == ERESTART) 3143111206Sken return (error); 3144111206Sken 3145111206Sken /* 314639514Sgibbs * XXX 314739514Sgibbs * Until we have a better way of doing pack validation, 314839514Sgibbs * don't treat UAs as errors. 314939514Sgibbs */ 315039514Sgibbs sense_flags |= SF_RETRY_UA; 315139213Sgibbs return (cam_periph_error(ccb, cam_flags, sense_flags, 315239213Sgibbs &softc->saved_ccb)); 315339213Sgibbs} 315439213Sgibbs 315539213Sgibbs/* 315639213Sgibbs * Read table of contents 315739213Sgibbs */ 315839213Sgibbsstatic int 315939213Sgibbscdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start, 3160111206Sken u_int8_t *data, u_int32_t len, u_int32_t sense_flags) 316139213Sgibbs{ 316239213Sgibbs struct scsi_read_toc *scsi_cmd; 316339213Sgibbs u_int32_t ntoc; 316439213Sgibbs struct ccb_scsiio *csio; 316539213Sgibbs union ccb *ccb; 316639213Sgibbs int error; 316739213Sgibbs 316839213Sgibbs ntoc = len; 316939213Sgibbs error = 0; 317039213Sgibbs 317139213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 317239213Sgibbs 317339213Sgibbs csio = &ccb->csio; 317439213Sgibbs 317539213Sgibbs cam_fill_csio(csio, 317639213Sgibbs /* retries */ 1, 317739213Sgibbs /* cbfcnp */ cddone, 317839213Sgibbs /* flags */ CAM_DIR_IN, 317939213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 3180111206Sken /* data_ptr */ data, 318139213Sgibbs /* dxfer_len */ len, 318239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 318339213Sgibbs sizeof(struct scsi_read_toc), 318439213Sgibbs /* timeout */ 50000); 318539213Sgibbs 318639213Sgibbs scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 318739213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 318839213Sgibbs 318939213Sgibbs if (mode == CD_MSF_FORMAT) 319039213Sgibbs scsi_cmd->byte2 |= CD_MSF; 319139213Sgibbs scsi_cmd->from_track = start; 319239213Sgibbs /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */ 319339213Sgibbs scsi_cmd->data_len[0] = (ntoc) >> 8; 319439213Sgibbs scsi_cmd->data_len[1] = (ntoc) & 0xff; 319539213Sgibbs 319639213Sgibbs scsi_cmd->op_code = READ_TOC; 319739213Sgibbs 319874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3199111206Sken /*sense_flags*/SF_RETRY_UA | sense_flags); 320039213Sgibbs 320139213Sgibbs xpt_release_ccb(ccb); 320239213Sgibbs 320339213Sgibbs return(error); 320439213Sgibbs} 320539213Sgibbs 320639213Sgibbsstatic int 320739213Sgibbscdreadsubchannel(struct cam_periph *periph, u_int32_t mode, 320839213Sgibbs u_int32_t format, int track, 320939213Sgibbs struct cd_sub_channel_info *data, u_int32_t len) 321039213Sgibbs{ 321139213Sgibbs struct scsi_read_subchannel *scsi_cmd; 321239213Sgibbs struct ccb_scsiio *csio; 321339213Sgibbs union ccb *ccb; 321439213Sgibbs int error; 321539213Sgibbs 321639213Sgibbs error = 0; 321739213Sgibbs 321839213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 321939213Sgibbs 322039213Sgibbs csio = &ccb->csio; 322139213Sgibbs 322239213Sgibbs cam_fill_csio(csio, 322339213Sgibbs /* retries */ 1, 322439213Sgibbs /* cbfcnp */ cddone, 322539213Sgibbs /* flags */ CAM_DIR_IN, 322639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 322739213Sgibbs /* data_ptr */ (u_int8_t *)data, 322839213Sgibbs /* dxfer_len */ len, 322939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 323039213Sgibbs sizeof(struct scsi_read_subchannel), 323139213Sgibbs /* timeout */ 50000); 323239213Sgibbs 323339213Sgibbs scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 323439213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 323539213Sgibbs 323639213Sgibbs scsi_cmd->op_code = READ_SUBCHANNEL; 323739213Sgibbs if (mode == CD_MSF_FORMAT) 323839213Sgibbs scsi_cmd->byte1 |= CD_MSF; 323939213Sgibbs scsi_cmd->byte2 = SRS_SUBQ; 324039213Sgibbs scsi_cmd->subchan_format = format; 324139213Sgibbs scsi_cmd->track = track; 324239213Sgibbs scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len); 324339213Sgibbs scsi_cmd->control = 0; 324439213Sgibbs 324574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 324674840Sken /*sense_flags*/SF_RETRY_UA); 324739213Sgibbs 324839213Sgibbs xpt_release_ccb(ccb); 324939213Sgibbs 325039213Sgibbs return(error); 325139213Sgibbs} 325239213Sgibbs 325339213Sgibbs 3254111206Sken/* 3255111206Sken * All MODE_SENSE requests in the cd(4) driver MUST go through this 3256111206Sken * routine. See comments in cd6byteworkaround() for details. 3257111206Sken */ 325839213Sgibbsstatic int 3259111206Skencdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3260111206Sken u_int32_t page) 326139213Sgibbs{ 3262111206Sken struct ccb_scsiio *csio; 3263111206Sken struct cd_softc *softc; 326439213Sgibbs union ccb *ccb; 3265111206Sken int param_len; 326639213Sgibbs int error; 326739213Sgibbs 3268111206Sken softc = (struct cd_softc *)periph->softc; 3269111206Sken 327039213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 327139213Sgibbs 327239213Sgibbs csio = &ccb->csio; 327339213Sgibbs 3274111206Sken data->cdb_size = softc->minimum_command_size; 3275111206Sken if (data->cdb_size < 10) 3276111206Sken param_len = sizeof(struct cd_mode_data); 3277111206Sken else 3278111206Sken param_len = sizeof(struct cd_mode_data_10); 327939213Sgibbs 3280111206Sken /* Don't say we've got more room than we actually allocated */ 3281111206Sken param_len = min(param_len, data->alloc_len); 328239213Sgibbs 3283111206Sken scsi_mode_sense_len(csio, 3284111206Sken /* retries */ 1, 3285111206Sken /* cbfcnp */ cddone, 3286111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3287111206Sken /* dbd */ 0, 3288111206Sken /* page_code */ SMS_PAGE_CTRL_CURRENT, 3289111206Sken /* page */ page, 3290111206Sken /* param_buf */ data->mode_buf, 3291111206Sken /* param_len */ param_len, 3292111206Sken /* minimum_cmd_size */ softc->minimum_command_size, 3293111206Sken /* sense_len */ SSD_FULL_SIZE, 3294111206Sken /* timeout */ 50000); 329539213Sgibbs 3296111206Sken /* 3297111206Sken * It would be nice not to have to do this, but there's no 3298111206Sken * available pointer in the CCB that would allow us to stuff the 3299111206Sken * mode params structure in there and retrieve it in 3300111206Sken * cd6byteworkaround(), so we can set the cdb size. The cdb size 3301111206Sken * lets the caller know what CDB size we ended up using, so they 3302111206Sken * can find the actual mode page offset. 3303111206Sken */ 3304111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3305111206Sken 330674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 330774840Sken /*sense_flags*/SF_RETRY_UA); 330839213Sgibbs 330939213Sgibbs xpt_release_ccb(ccb); 331039213Sgibbs 3311111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3312111206Sken 3313111206Sken /* 3314111206Sken * This is a bit of belt-and-suspenders checking, but if we run 3315111206Sken * into a situation where the target sends back multiple block 3316111206Sken * descriptors, we might not have enough space in the buffer to 3317111206Sken * see the whole mode page. Better to return an error than 3318111206Sken * potentially access memory beyond our malloced region. 3319111206Sken */ 3320111206Sken if (error == 0) { 3321111206Sken u_int32_t data_len; 3322111206Sken 3323111206Sken if (data->cdb_size == 10) { 3324111206Sken struct scsi_mode_header_10 *hdr10; 3325111206Sken 3326111206Sken hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3327111206Sken data_len = scsi_2btoul(hdr10->data_length); 3328111206Sken data_len += sizeof(hdr10->data_length); 3329111206Sken } else { 3330111206Sken struct scsi_mode_header_6 *hdr6; 3331111206Sken 3332111206Sken hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3333111206Sken data_len = hdr6->data_length; 3334111206Sken data_len += sizeof(hdr6->data_length); 3335111206Sken } 3336111206Sken 3337111206Sken /* 3338111206Sken * Complain if there is more mode data available than we 3339111206Sken * allocated space for. This could potentially happen if 3340111206Sken * we miscalculated the page length for some reason, if the 3341111206Sken * drive returns multiple block descriptors, or if it sets 3342111206Sken * the data length incorrectly. 3343111206Sken */ 3344111206Sken if (data_len > data->alloc_len) { 3345164906Smjacob xpt_print(periph->path, "allocated modepage %d length " 3346164906Smjacob "%d < returned length %d\n", page, data->alloc_len, 3347164906Smjacob data_len); 3348111206Sken error = ENOSPC; 3349111206Sken } 3350111206Sken } 3351111206Sken return (error); 335239213Sgibbs} 335339213Sgibbs 3354111206Sken/* 3355111206Sken * All MODE_SELECT requests in the cd(4) driver MUST go through this 3356111206Sken * routine. See comments in cd6byteworkaround() for details. 3357111206Sken */ 335839213Sgibbsstatic int 3359111206Skencdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 336039213Sgibbs{ 3361111206Sken struct ccb_scsiio *csio; 3362111206Sken struct cd_softc *softc; 336339213Sgibbs union ccb *ccb; 3364111206Sken int cdb_size, param_len; 336539213Sgibbs int error; 336639213Sgibbs 3367111206Sken softc = (struct cd_softc *)periph->softc; 3368111206Sken 336939213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 337039213Sgibbs 337139213Sgibbs csio = &ccb->csio; 337239213Sgibbs 337339213Sgibbs error = 0; 337439213Sgibbs 337539213Sgibbs /* 3376111206Sken * If the data is formatted for the 10 byte version of the mode 3377111206Sken * select parameter list, we need to use the 10 byte CDB. 3378111206Sken * Otherwise, we use whatever the stored minimum command size. 337939213Sgibbs */ 3380111206Sken if (data->cdb_size == 10) 3381111206Sken cdb_size = data->cdb_size; 3382111206Sken else 3383111206Sken cdb_size = softc->minimum_command_size; 338439213Sgibbs 3385111206Sken if (cdb_size >= 10) { 3386111206Sken struct scsi_mode_header_10 *mode_header; 3387111206Sken u_int32_t data_len; 3388111206Sken 3389111206Sken mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3390111206Sken 3391111206Sken data_len = scsi_2btoul(mode_header->data_length); 3392111206Sken 3393111206Sken scsi_ulto2b(0, mode_header->data_length); 3394111206Sken /* 3395111206Sken * SONY drives do not allow a mode select with a medium_type 3396111206Sken * value that has just been returned by a mode sense; use a 3397111206Sken * medium_type of 0 (Default) instead. 3398111206Sken */ 3399111206Sken mode_header->medium_type = 0; 3400111206Sken 3401111206Sken /* 3402111206Sken * Pass back whatever the drive passed to us, plus the size 3403111206Sken * of the data length field. 3404111206Sken */ 3405111206Sken param_len = data_len + sizeof(mode_header->data_length); 3406111206Sken 3407111206Sken } else { 3408111206Sken struct scsi_mode_header_6 *mode_header; 3409111206Sken 3410111206Sken mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3411111206Sken 3412111206Sken param_len = mode_header->data_length + 1; 3413111206Sken 3414111206Sken mode_header->data_length = 0; 3415111206Sken /* 3416111206Sken * SONY drives do not allow a mode select with a medium_type 3417111206Sken * value that has just been returned by a mode sense; use a 3418111206Sken * medium_type of 0 (Default) instead. 3419111206Sken */ 3420111206Sken mode_header->medium_type = 0; 3421111206Sken } 3422111206Sken 3423111206Sken /* Don't say we've got more room than we actually allocated */ 3424111206Sken param_len = min(param_len, data->alloc_len); 3425111206Sken 3426111206Sken scsi_mode_select_len(csio, 3427111206Sken /* retries */ 1, 3428111206Sken /* cbfcnp */ cddone, 3429111206Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 3430111206Sken /* scsi_page_fmt */ 1, 3431111206Sken /* save_pages */ 0, 3432111206Sken /* param_buf */ data->mode_buf, 3433111206Sken /* param_len */ param_len, 3434111206Sken /* minimum_cmd_size */ cdb_size, 3435111206Sken /* sense_len */ SSD_FULL_SIZE, 3436111206Sken /* timeout */ 50000); 3437111206Sken 3438111206Sken /* See comments in cdgetmode() and cd6byteworkaround(). */ 3439111206Sken STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3440111206Sken 344174840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 344274840Sken /*sense_flags*/SF_RETRY_UA); 344339213Sgibbs 344439213Sgibbs xpt_release_ccb(ccb); 344539213Sgibbs 3446111206Sken STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3447111206Sken 3448111206Sken return (error); 344939213Sgibbs} 345039213Sgibbs 345139213Sgibbs 345239213Sgibbsstatic int 345339213Sgibbscdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len) 345439213Sgibbs{ 345539531Sken struct ccb_scsiio *csio; 345639213Sgibbs union ccb *ccb; 345739213Sgibbs int error; 345839531Sken u_int8_t cdb_len; 345939213Sgibbs 346039213Sgibbs error = 0; 346139213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 346239213Sgibbs csio = &ccb->csio; 346339531Sken /* 346439531Sken * Use the smallest possible command to perform the operation. 346539531Sken */ 346639531Sken if ((len & 0xffff0000) == 0) { 346739531Sken /* 346839531Sken * We can fit in a 10 byte cdb. 346939531Sken */ 347039531Sken struct scsi_play_10 *scsi_cmd; 347139213Sgibbs 347239531Sken scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 347339531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 347439531Sken scsi_cmd->op_code = PLAY_10; 347539531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 347639531Sken scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len); 347739531Sken cdb_len = sizeof(*scsi_cmd); 347839531Sken } else { 347939531Sken struct scsi_play_12 *scsi_cmd; 348039213Sgibbs 348139531Sken scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 348239531Sken bzero (scsi_cmd, sizeof(*scsi_cmd)); 348339531Sken scsi_cmd->op_code = PLAY_12; 348439531Sken scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr); 348539531Sken scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len); 348639531Sken cdb_len = sizeof(*scsi_cmd); 348739531Sken } 348839531Sken cam_fill_csio(csio, 348939531Sken /*retries*/2, 349039531Sken cddone, 349139531Sken /*flags*/CAM_DIR_NONE, 349239531Sken MSG_SIMPLE_Q_TAG, 349339531Sken /*dataptr*/NULL, 349439531Sken /*datalen*/0, 349539531Sken /*sense_len*/SSD_FULL_SIZE, 349639531Sken cdb_len, 349739531Sken /*timeout*/50 * 1000); 349839213Sgibbs 349974840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 350074840Sken /*sense_flags*/SF_RETRY_UA); 350139213Sgibbs 350239213Sgibbs xpt_release_ccb(ccb); 350339213Sgibbs 350439213Sgibbs return(error); 350539213Sgibbs} 350639213Sgibbs 350739213Sgibbsstatic int 350839213Sgibbscdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts, 350939213Sgibbs u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf) 351039213Sgibbs{ 351139213Sgibbs struct scsi_play_msf *scsi_cmd; 351239213Sgibbs struct ccb_scsiio *csio; 351339213Sgibbs union ccb *ccb; 351439213Sgibbs int error; 351539213Sgibbs 351639213Sgibbs error = 0; 351739213Sgibbs 351839213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 351939213Sgibbs 352039213Sgibbs csio = &ccb->csio; 352139213Sgibbs 352239213Sgibbs cam_fill_csio(csio, 352339213Sgibbs /* retries */ 1, 352439213Sgibbs /* cbfcnp */ cddone, 352539531Sken /* flags */ CAM_DIR_NONE, 352639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 352739213Sgibbs /* data_ptr */ NULL, 352839213Sgibbs /* dxfer_len */ 0, 352939213Sgibbs /* sense_len */ SSD_FULL_SIZE, 353039213Sgibbs sizeof(struct scsi_play_msf), 353139213Sgibbs /* timeout */ 50000); 353239213Sgibbs 353339213Sgibbs scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 353439213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 353539213Sgibbs 353639213Sgibbs scsi_cmd->op_code = PLAY_MSF; 353739213Sgibbs scsi_cmd->start_m = startm; 353839213Sgibbs scsi_cmd->start_s = starts; 353939213Sgibbs scsi_cmd->start_f = startf; 354039213Sgibbs scsi_cmd->end_m = endm; 354139213Sgibbs scsi_cmd->end_s = ends; 354239213Sgibbs scsi_cmd->end_f = endf; 354339213Sgibbs 354474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 354574840Sken /*sense_flags*/SF_RETRY_UA); 354639213Sgibbs 354739213Sgibbs xpt_release_ccb(ccb); 354839213Sgibbs 354939213Sgibbs return(error); 355039213Sgibbs} 355139213Sgibbs 355239213Sgibbs 355339213Sgibbsstatic int 355439213Sgibbscdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex, 355539213Sgibbs u_int32_t etrack, u_int32_t eindex) 355639213Sgibbs{ 355739213Sgibbs struct scsi_play_track *scsi_cmd; 355839213Sgibbs struct ccb_scsiio *csio; 355939213Sgibbs union ccb *ccb; 356039213Sgibbs int error; 356139213Sgibbs 356239213Sgibbs error = 0; 356339213Sgibbs 356439213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 356539213Sgibbs 356639213Sgibbs csio = &ccb->csio; 356739213Sgibbs 356839213Sgibbs cam_fill_csio(csio, 356939213Sgibbs /* retries */ 1, 357039213Sgibbs /* cbfcnp */ cddone, 357139531Sken /* flags */ CAM_DIR_NONE, 357239213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 357339213Sgibbs /* data_ptr */ NULL, 357439213Sgibbs /* dxfer_len */ 0, 357539213Sgibbs /* sense_len */ SSD_FULL_SIZE, 357639213Sgibbs sizeof(struct scsi_play_track), 357739213Sgibbs /* timeout */ 50000); 357839213Sgibbs 357939213Sgibbs scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 358039213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 358139213Sgibbs 358239213Sgibbs scsi_cmd->op_code = PLAY_TRACK; 358339213Sgibbs scsi_cmd->start_track = strack; 358439213Sgibbs scsi_cmd->start_index = sindex; 358539213Sgibbs scsi_cmd->end_track = etrack; 358639213Sgibbs scsi_cmd->end_index = eindex; 358739213Sgibbs 358874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 358974840Sken /*sense_flags*/SF_RETRY_UA); 359039213Sgibbs 359139213Sgibbs xpt_release_ccb(ccb); 359239213Sgibbs 359339213Sgibbs return(error); 359439213Sgibbs} 359539213Sgibbs 359639213Sgibbsstatic int 359739213Sgibbscdpause(struct cam_periph *periph, u_int32_t go) 359839213Sgibbs{ 359939213Sgibbs struct scsi_pause *scsi_cmd; 360039213Sgibbs struct ccb_scsiio *csio; 360139213Sgibbs union ccb *ccb; 360239213Sgibbs int error; 360339213Sgibbs 360439213Sgibbs error = 0; 360539213Sgibbs 360639213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 360739213Sgibbs 360839213Sgibbs csio = &ccb->csio; 360939213Sgibbs 361039213Sgibbs cam_fill_csio(csio, 361139213Sgibbs /* retries */ 1, 361239213Sgibbs /* cbfcnp */ cddone, 361339531Sken /* flags */ CAM_DIR_NONE, 361439213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 361539213Sgibbs /* data_ptr */ NULL, 361639213Sgibbs /* dxfer_len */ 0, 361739213Sgibbs /* sense_len */ SSD_FULL_SIZE, 361839213Sgibbs sizeof(struct scsi_pause), 361939213Sgibbs /* timeout */ 50000); 362039213Sgibbs 362139213Sgibbs scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 362239213Sgibbs bzero (scsi_cmd, sizeof(*scsi_cmd)); 362339213Sgibbs 362439213Sgibbs scsi_cmd->op_code = PAUSE; 362539213Sgibbs scsi_cmd->resume = go; 362639213Sgibbs 362774840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 362874840Sken /*sense_flags*/SF_RETRY_UA); 362939213Sgibbs 363039213Sgibbs xpt_release_ccb(ccb); 363139213Sgibbs 363239213Sgibbs return(error); 363339213Sgibbs} 363439213Sgibbs 363539213Sgibbsstatic int 3636111206Skencdstartunit(struct cam_periph *periph, int load) 363739213Sgibbs{ 363839213Sgibbs union ccb *ccb; 363939213Sgibbs int error; 364039213Sgibbs 364139213Sgibbs error = 0; 364239213Sgibbs 364339213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 364439213Sgibbs 364539213Sgibbs scsi_start_stop(&ccb->csio, 364639213Sgibbs /* retries */ 1, 364739213Sgibbs /* cbfcnp */ cddone, 364839213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 364939213Sgibbs /* start */ TRUE, 3650111206Sken /* load_eject */ load, 365139213Sgibbs /* immediate */ FALSE, 365239213Sgibbs /* sense_len */ SSD_FULL_SIZE, 365339213Sgibbs /* timeout */ 50000); 365439213Sgibbs 365574840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 365674840Sken /*sense_flags*/SF_RETRY_UA); 365739213Sgibbs 365839213Sgibbs xpt_release_ccb(ccb); 365939213Sgibbs 366039213Sgibbs return(error); 366139213Sgibbs} 366239213Sgibbs 366339213Sgibbsstatic int 366439213Sgibbscdstopunit(struct cam_periph *periph, u_int32_t eject) 366539213Sgibbs{ 366639213Sgibbs union ccb *ccb; 366739213Sgibbs int error; 366839213Sgibbs 366939213Sgibbs error = 0; 367039213Sgibbs 367139213Sgibbs ccb = cdgetccb(periph, /* priority */ 1); 367239213Sgibbs 367339213Sgibbs scsi_start_stop(&ccb->csio, 367439213Sgibbs /* retries */ 1, 367539213Sgibbs /* cbfcnp */ cddone, 367639213Sgibbs /* tag_action */ MSG_SIMPLE_Q_TAG, 367739213Sgibbs /* start */ FALSE, 367839213Sgibbs /* load_eject */ eject, 367939213Sgibbs /* immediate */ FALSE, 368039213Sgibbs /* sense_len */ SSD_FULL_SIZE, 368139213Sgibbs /* timeout */ 50000); 368239213Sgibbs 368374840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 368474840Sken /*sense_flags*/SF_RETRY_UA); 368539213Sgibbs 368639213Sgibbs xpt_release_ccb(ccb); 368739213Sgibbs 368839213Sgibbs return(error); 368939213Sgibbs} 369060422Sken 369160422Skenstatic int 3692105421Snjlcdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed) 3693105421Snjl{ 3694105421Snjl struct scsi_set_speed *scsi_cmd; 3695105421Snjl struct ccb_scsiio *csio; 3696105421Snjl union ccb *ccb; 3697105421Snjl int error; 3698105421Snjl 3699105421Snjl error = 0; 3700105421Snjl ccb = cdgetccb(periph, /* priority */ 1); 3701105421Snjl csio = &ccb->csio; 3702105421Snjl 3703107193Snjl /* Preserve old behavior: units in multiples of CDROM speed */ 3704107193Snjl if (rdspeed < 177) 3705107193Snjl rdspeed *= 177; 3706107193Snjl if (wrspeed < 177) 3707107193Snjl wrspeed *= 177; 3708107193Snjl 3709105421Snjl cam_fill_csio(csio, 3710105421Snjl /* retries */ 1, 3711105421Snjl /* cbfcnp */ cddone, 3712105421Snjl /* flags */ CAM_DIR_NONE, 3713105421Snjl /* tag_action */ MSG_SIMPLE_Q_TAG, 3714105421Snjl /* data_ptr */ NULL, 3715105421Snjl /* dxfer_len */ 0, 3716105421Snjl /* sense_len */ SSD_FULL_SIZE, 3717105421Snjl sizeof(struct scsi_set_speed), 3718105421Snjl /* timeout */ 50000); 3719105421Snjl 3720105421Snjl scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3721105421Snjl bzero(scsi_cmd, sizeof(*scsi_cmd)); 3722105421Snjl 3723105421Snjl scsi_cmd->opcode = SET_CD_SPEED; 3724105421Snjl scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3725105421Snjl scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3726105421Snjl 3727105421Snjl error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3728105421Snjl /*sense_flags*/SF_RETRY_UA); 3729105421Snjl 3730105421Snjl xpt_release_ccb(ccb); 3731105421Snjl 3732105421Snjl return(error); 3733105421Snjl} 3734105421Snjl 3735105421Snjlstatic int 373660422Skencdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 373760422Sken{ 373860422Sken union ccb *ccb; 373960422Sken u_int8_t *databuf; 374060422Sken u_int32_t lba; 374160422Sken int error; 374260422Sken int length; 374360422Sken 374460422Sken error = 0; 374560422Sken databuf = NULL; 374660422Sken lba = 0; 374760422Sken 374860422Sken ccb = cdgetccb(periph, /* priority */ 1); 374960422Sken 375060422Sken switch (authinfo->format) { 375160422Sken case DVD_REPORT_AGID: 375260422Sken length = sizeof(struct scsi_report_key_data_agid); 375360422Sken break; 375460422Sken case DVD_REPORT_CHALLENGE: 375560422Sken length = sizeof(struct scsi_report_key_data_challenge); 375660422Sken break; 375760422Sken case DVD_REPORT_KEY1: 375860422Sken length = sizeof(struct scsi_report_key_data_key1_key2); 375960422Sken break; 376060422Sken case DVD_REPORT_TITLE_KEY: 376160422Sken length = sizeof(struct scsi_report_key_data_title); 376260422Sken /* The lba field is only set for the title key */ 376360422Sken lba = authinfo->lba; 376460422Sken break; 376560422Sken case DVD_REPORT_ASF: 376660422Sken length = sizeof(struct scsi_report_key_data_asf); 376760422Sken break; 376860422Sken case DVD_REPORT_RPC: 376960422Sken length = sizeof(struct scsi_report_key_data_rpc); 377060422Sken break; 377160422Sken case DVD_INVALIDATE_AGID: 377260422Sken length = 0; 377360422Sken break; 377460422Sken default: 377560422Sken error = EINVAL; 377660422Sken goto bailout; 377760422Sken break; /* NOTREACHED */ 377860422Sken } 377960422Sken 378060422Sken if (length != 0) { 3781111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 378260422Sken } else 378360422Sken databuf = NULL; 378460422Sken 378560422Sken 378660422Sken scsi_report_key(&ccb->csio, 378760422Sken /* retries */ 1, 378860422Sken /* cbfcnp */ cddone, 378960422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 379060422Sken /* lba */ lba, 379160422Sken /* agid */ authinfo->agid, 379260422Sken /* key_format */ authinfo->format, 379360422Sken /* data_ptr */ databuf, 379460422Sken /* dxfer_len */ length, 379560422Sken /* sense_len */ SSD_FULL_SIZE, 379660422Sken /* timeout */ 50000); 379760422Sken 379874840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 379974840Sken /*sense_flags*/SF_RETRY_UA); 380060422Sken 380160422Sken if (error != 0) 380260422Sken goto bailout; 380360422Sken 380460422Sken if (ccb->csio.resid != 0) { 3805164906Smjacob xpt_print(periph->path, "warning, residual for report key " 3806164906Smjacob "command is %d\n", ccb->csio.resid); 380760422Sken } 380860422Sken 380960422Sken switch(authinfo->format) { 381060422Sken case DVD_REPORT_AGID: { 381160422Sken struct scsi_report_key_data_agid *agid_data; 381260422Sken 381360422Sken agid_data = (struct scsi_report_key_data_agid *)databuf; 381460422Sken 381560422Sken authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 381660422Sken RKD_AGID_SHIFT; 381760422Sken break; 381860422Sken } 381960422Sken case DVD_REPORT_CHALLENGE: { 382060422Sken struct scsi_report_key_data_challenge *chal_data; 382160422Sken 382260422Sken chal_data = (struct scsi_report_key_data_challenge *)databuf; 382360422Sken 382460422Sken bcopy(chal_data->challenge_key, authinfo->keychal, 382560422Sken min(sizeof(chal_data->challenge_key), 382660422Sken sizeof(authinfo->keychal))); 382760422Sken break; 382860422Sken } 382960422Sken case DVD_REPORT_KEY1: { 383060422Sken struct scsi_report_key_data_key1_key2 *key1_data; 383160422Sken 383260422Sken key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 383360422Sken 383460422Sken bcopy(key1_data->key1, authinfo->keychal, 383560422Sken min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 383660422Sken break; 383760422Sken } 383860422Sken case DVD_REPORT_TITLE_KEY: { 383960422Sken struct scsi_report_key_data_title *title_data; 384060422Sken 384160422Sken title_data = (struct scsi_report_key_data_title *)databuf; 384260422Sken 384360422Sken authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 384460422Sken RKD_TITLE_CPM_SHIFT; 384560422Sken authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 384660422Sken RKD_TITLE_CP_SEC_SHIFT; 384760422Sken authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 384860422Sken RKD_TITLE_CMGS_SHIFT; 384960422Sken bcopy(title_data->title_key, authinfo->keychal, 385060422Sken min(sizeof(title_data->title_key), 385160422Sken sizeof(authinfo->keychal))); 385260422Sken break; 385360422Sken } 385460422Sken case DVD_REPORT_ASF: { 385560422Sken struct scsi_report_key_data_asf *asf_data; 385660422Sken 385760422Sken asf_data = (struct scsi_report_key_data_asf *)databuf; 385860422Sken 385960422Sken authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 386060422Sken break; 386160422Sken } 386260422Sken case DVD_REPORT_RPC: { 386360422Sken struct scsi_report_key_data_rpc *rpc_data; 386460422Sken 386560422Sken rpc_data = (struct scsi_report_key_data_rpc *)databuf; 386660422Sken 386760422Sken authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 386860422Sken RKD_RPC_TYPE_SHIFT; 386960422Sken authinfo->vend_rsts = 387060422Sken (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 387160422Sken RKD_RPC_VENDOR_RESET_SHIFT; 387260422Sken authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 387371752Sken authinfo->region = rpc_data->region_mask; 387471752Sken authinfo->rpc_scheme = rpc_data->rpc_scheme1; 387560422Sken break; 387660422Sken } 387760422Sken case DVD_INVALIDATE_AGID: 387860422Sken break; 387960422Sken default: 388060422Sken /* This should be impossible, since we checked above */ 388160422Sken error = EINVAL; 388260422Sken goto bailout; 388360422Sken break; /* NOTREACHED */ 388460422Sken } 388560422Skenbailout: 388660422Sken if (databuf != NULL) 388760422Sken free(databuf, M_DEVBUF); 388860422Sken 388960422Sken xpt_release_ccb(ccb); 389060422Sken 389160422Sken return(error); 389260422Sken} 389360422Sken 389460422Skenstatic int 389560422Skencdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 389660422Sken{ 389760422Sken union ccb *ccb; 389860422Sken u_int8_t *databuf; 389960422Sken int length; 390060422Sken int error; 390160422Sken 390260422Sken error = 0; 390360422Sken databuf = NULL; 390460422Sken 390560422Sken ccb = cdgetccb(periph, /* priority */ 1); 390660422Sken 390760422Sken switch(authinfo->format) { 390860422Sken case DVD_SEND_CHALLENGE: { 390960422Sken struct scsi_report_key_data_challenge *challenge_data; 391060422Sken 391160422Sken length = sizeof(*challenge_data); 391260422Sken 3913111119Simp challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 391460422Sken 391560422Sken databuf = (u_int8_t *)challenge_data; 391660422Sken 391760422Sken scsi_ulto2b(length - sizeof(challenge_data->data_len), 391860422Sken challenge_data->data_len); 391960422Sken 392060422Sken bcopy(authinfo->keychal, challenge_data->challenge_key, 392160422Sken min(sizeof(authinfo->keychal), 392260422Sken sizeof(challenge_data->challenge_key))); 392360422Sken break; 392460422Sken } 392560422Sken case DVD_SEND_KEY2: { 392660422Sken struct scsi_report_key_data_key1_key2 *key2_data; 392760422Sken 392860422Sken length = sizeof(*key2_data); 392960422Sken 3930111119Simp key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 393160422Sken 393260422Sken databuf = (u_int8_t *)key2_data; 393360422Sken 393460422Sken scsi_ulto2b(length - sizeof(key2_data->data_len), 393560422Sken key2_data->data_len); 393660422Sken 393760422Sken bcopy(authinfo->keychal, key2_data->key1, 393860422Sken min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 393960422Sken 394060422Sken break; 394160422Sken } 394260422Sken case DVD_SEND_RPC: { 394360422Sken struct scsi_send_key_data_rpc *rpc_data; 394460422Sken 394560422Sken length = sizeof(*rpc_data); 394660422Sken 3947111119Simp rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 394860422Sken 394960422Sken databuf = (u_int8_t *)rpc_data; 395060422Sken 395160422Sken scsi_ulto2b(length - sizeof(rpc_data->data_len), 395260422Sken rpc_data->data_len); 395360422Sken 395460422Sken rpc_data->region_code = authinfo->region; 395560422Sken break; 395660422Sken } 395760422Sken default: 395860422Sken error = EINVAL; 395960422Sken goto bailout; 396060422Sken break; /* NOTREACHED */ 396160422Sken } 396260422Sken 396360422Sken scsi_send_key(&ccb->csio, 396460422Sken /* retries */ 1, 396560422Sken /* cbfcnp */ cddone, 396660422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 396760422Sken /* agid */ authinfo->agid, 396860422Sken /* key_format */ authinfo->format, 396960422Sken /* data_ptr */ databuf, 397060422Sken /* dxfer_len */ length, 397160422Sken /* sense_len */ SSD_FULL_SIZE, 397260422Sken /* timeout */ 50000); 397360422Sken 397474840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 397574840Sken /*sense_flags*/SF_RETRY_UA); 397660422Sken 397760422Skenbailout: 397860422Sken 397960422Sken if (databuf != NULL) 398060422Sken free(databuf, M_DEVBUF); 398160422Sken 398260422Sken xpt_release_ccb(ccb); 398360422Sken 398460422Sken return(error); 398560422Sken} 398660422Sken 398760422Skenstatic int 398860422Skencdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 398960422Sken{ 399060422Sken union ccb *ccb; 399160422Sken u_int8_t *databuf; 399260422Sken u_int32_t address; 399360422Sken int error; 399460422Sken int length; 399560422Sken 399660422Sken error = 0; 399760422Sken databuf = NULL; 399860422Sken /* The address is reserved for many of the formats */ 399960422Sken address = 0; 400060422Sken 400160422Sken ccb = cdgetccb(periph, /* priority */ 1); 400260422Sken 400360422Sken switch(dvdstruct->format) { 400460422Sken case DVD_STRUCT_PHYSICAL: 400560422Sken length = sizeof(struct scsi_read_dvd_struct_data_physical); 400660422Sken break; 400760422Sken case DVD_STRUCT_COPYRIGHT: 400860422Sken length = sizeof(struct scsi_read_dvd_struct_data_copyright); 400960422Sken break; 401060422Sken case DVD_STRUCT_DISCKEY: 401160422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 401260422Sken break; 401360422Sken case DVD_STRUCT_BCA: 401460422Sken length = sizeof(struct scsi_read_dvd_struct_data_bca); 401560422Sken break; 401660422Sken case DVD_STRUCT_MANUFACT: 401760422Sken length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 401860422Sken break; 401960422Sken case DVD_STRUCT_CMI: 402060422Sken error = ENODEV; 402160422Sken goto bailout; 402260422Sken#ifdef notyet 402360422Sken length = sizeof(struct scsi_read_dvd_struct_data_copy_manage); 402460422Sken address = dvdstruct->address; 402560422Sken#endif 402660422Sken break; /* NOTREACHED */ 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: 404360422Sken error = ENODEV; 404460422Sken goto bailout; 404560422Sken#ifdef notyet 404660422Sken length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout); 404760422Sken address = dvdstruct->address; 404860422Sken#endif 404960422Sken break; /* NOTREACHED */ 405060422Sken case DVD_STRUCT_RMD_RMA: 405160422Sken error = ENODEV; 405260422Sken goto bailout; 405360422Sken#ifdef notyet 405460422Sken length = sizeof(struct scsi_read_dvd_struct_data_rmd); 405560422Sken address = dvdstruct->address; 405660422Sken#endif 405760422Sken break; /* NOTREACHED */ 405860422Sken case DVD_STRUCT_PRERECORDED: 405960422Sken length = sizeof(struct scsi_read_dvd_struct_data_leadin); 406060422Sken break; 406160422Sken case DVD_STRUCT_UNIQUEID: 406260422Sken length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 406360422Sken break; 406460422Sken case DVD_STRUCT_DCB: 406560422Sken error = ENODEV; 406660422Sken goto bailout; 406760422Sken#ifdef notyet 406860422Sken length = sizeof(struct scsi_read_dvd_struct_data_dcb); 406960422Sken address = dvdstruct->address; 407060422Sken#endif 407160422Sken break; /* NOTREACHED */ 407260422Sken case DVD_STRUCT_LIST: 407360422Sken /* 407460422Sken * This is the maximum allocation length for the READ DVD 407560422Sken * STRUCTURE command. There's nothing in the MMC3 spec 407660422Sken * that indicates a limit in the amount of data that can 407760422Sken * be returned from this call, other than the limits 407860422Sken * imposed by the 2-byte length variables. 407960422Sken */ 408060422Sken length = 65535; 408160422Sken break; 408260422Sken default: 408360422Sken error = EINVAL; 408460422Sken goto bailout; 408560422Sken break; /* NOTREACHED */ 408660422Sken } 408760422Sken 408860422Sken if (length != 0) { 4089111119Simp databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 409060422Sken } else 409160422Sken databuf = NULL; 409260422Sken 409360422Sken scsi_read_dvd_structure(&ccb->csio, 409460422Sken /* retries */ 1, 409560422Sken /* cbfcnp */ cddone, 409660422Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 409760422Sken /* lba */ address, 409860422Sken /* layer_number */ dvdstruct->layer_num, 409960422Sken /* key_format */ dvdstruct->format, 410060422Sken /* agid */ dvdstruct->agid, 410160422Sken /* data_ptr */ databuf, 410260422Sken /* dxfer_len */ length, 410360422Sken /* sense_len */ SSD_FULL_SIZE, 410460422Sken /* timeout */ 50000); 410560422Sken 410674840Sken error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 410774840Sken /*sense_flags*/SF_RETRY_UA); 410860422Sken 410960422Sken if (error != 0) 411060422Sken goto bailout; 411160422Sken 411260422Sken switch(dvdstruct->format) { 411360422Sken case DVD_STRUCT_PHYSICAL: { 411460422Sken struct scsi_read_dvd_struct_data_layer_desc *inlayer; 411560422Sken struct dvd_layer *outlayer; 411660422Sken struct scsi_read_dvd_struct_data_physical *phys_data; 411760422Sken 411860422Sken phys_data = 411960422Sken (struct scsi_read_dvd_struct_data_physical *)databuf; 412060422Sken inlayer = &phys_data->layer_desc; 412160422Sken outlayer = (struct dvd_layer *)&dvdstruct->data; 412260422Sken 412360422Sken dvdstruct->length = sizeof(*inlayer); 412460422Sken 412560422Sken outlayer->book_type = (inlayer->book_type_version & 412660422Sken RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 412760422Sken outlayer->book_version = (inlayer->book_type_version & 412860422Sken RDSD_BOOK_VERSION_MASK); 412960422Sken outlayer->disc_size = (inlayer->disc_size_max_rate & 413060422Sken RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 413160422Sken outlayer->max_rate = (inlayer->disc_size_max_rate & 413260422Sken RDSD_MAX_RATE_MASK); 413360422Sken outlayer->nlayers = (inlayer->layer_info & 413460422Sken RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 413560422Sken outlayer->track_path = (inlayer->layer_info & 413660422Sken RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 413760422Sken outlayer->layer_type = (inlayer->layer_info & 413860422Sken RDSD_LAYER_TYPE_MASK); 413960422Sken outlayer->linear_density = (inlayer->density & 414060422Sken RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 414160422Sken outlayer->track_density = (inlayer->density & 414260422Sken RDSD_TRACK_DENSITY_MASK); 414360422Sken outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 414460422Sken RDSD_BCA_SHIFT; 414560422Sken outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 414660422Sken outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 414760422Sken outlayer->end_sector_l0 = 414860422Sken scsi_3btoul(inlayer->end_sector_layer0); 414960422Sken break; 415060422Sken } 415160422Sken case DVD_STRUCT_COPYRIGHT: { 415260422Sken struct scsi_read_dvd_struct_data_copyright *copy_data; 415360422Sken 415460422Sken copy_data = (struct scsi_read_dvd_struct_data_copyright *) 415560422Sken databuf; 415660422Sken 415760422Sken dvdstruct->cpst = copy_data->cps_type; 415860422Sken dvdstruct->rmi = copy_data->region_info; 415960422Sken dvdstruct->length = 0; 416060422Sken 416160422Sken break; 416260422Sken } 416360422Sken default: 416460422Sken /* 416560422Sken * Tell the user what the overall length is, no matter 416660422Sken * what we can actually fit in the data buffer. 416760422Sken */ 416860422Sken dvdstruct->length = length - ccb->csio.resid - 416960422Sken sizeof(struct scsi_read_dvd_struct_data_header); 417060422Sken 417160422Sken /* 417260422Sken * But only actually copy out the smaller of what we read 417360422Sken * in or what the structure can take. 417460422Sken */ 417560422Sken bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 417660422Sken dvdstruct->data, 417760422Sken min(sizeof(dvdstruct->data), dvdstruct->length)); 417860422Sken break; 417960422Sken } 418060422Skenbailout: 418160422Sken 418260422Sken if (databuf != NULL) 418360422Sken free(databuf, M_DEVBUF); 418460422Sken 418560422Sken xpt_release_ccb(ccb); 418660422Sken 418760422Sken return(error); 418860422Sken} 418960422Sken 419060422Skenvoid 419160422Skenscsi_report_key(struct ccb_scsiio *csio, u_int32_t retries, 419260422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 419360422Sken u_int8_t tag_action, u_int32_t lba, u_int8_t agid, 419460422Sken u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len, 419560422Sken u_int8_t sense_len, u_int32_t timeout) 419660422Sken{ 419760422Sken struct scsi_report_key *scsi_cmd; 419860422Sken 419960422Sken scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 420060422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 420160422Sken scsi_cmd->opcode = REPORT_KEY; 420260422Sken scsi_ulto4b(lba, scsi_cmd->lba); 420360422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 420460422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 420560422Sken (key_format & RK_KF_KEYFORMAT_MASK); 420660422Sken 420760422Sken cam_fill_csio(csio, 420860422Sken retries, 420960422Sken cbfcnp, 421060422Sken /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 421160422Sken tag_action, 421260422Sken /*data_ptr*/ data_ptr, 421360422Sken /*dxfer_len*/ dxfer_len, 421460422Sken sense_len, 421560422Sken sizeof(*scsi_cmd), 421660422Sken timeout); 421760422Sken} 421860422Sken 421960422Skenvoid 422060422Skenscsi_send_key(struct ccb_scsiio *csio, u_int32_t retries, 422160422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 422260422Sken u_int8_t tag_action, u_int8_t agid, u_int8_t key_format, 422360422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 422460422Sken u_int32_t timeout) 422560422Sken{ 422660422Sken struct scsi_send_key *scsi_cmd; 422760422Sken 422860422Sken scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 422960422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 423060422Sken scsi_cmd->opcode = SEND_KEY; 423160422Sken 423260422Sken scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 423360422Sken scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 423460422Sken (key_format & RK_KF_KEYFORMAT_MASK); 423560422Sken 423660422Sken cam_fill_csio(csio, 423760422Sken retries, 423860422Sken cbfcnp, 423960422Sken /*flags*/ CAM_DIR_OUT, 424060422Sken tag_action, 424160422Sken /*data_ptr*/ data_ptr, 424260422Sken /*dxfer_len*/ dxfer_len, 424360422Sken sense_len, 424460422Sken sizeof(*scsi_cmd), 424560422Sken timeout); 424660422Sken} 424760422Sken 424860422Sken 424960422Skenvoid 425060422Skenscsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries, 425160422Sken void (*cbfcnp)(struct cam_periph *, union ccb *), 425260422Sken u_int8_t tag_action, u_int32_t address, 425360422Sken u_int8_t layer_number, u_int8_t format, u_int8_t agid, 425460422Sken u_int8_t *data_ptr, u_int32_t dxfer_len, 425560422Sken u_int8_t sense_len, u_int32_t timeout) 425660422Sken{ 425760422Sken struct scsi_read_dvd_structure *scsi_cmd; 425860422Sken 425960422Sken scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 426060422Sken bzero(scsi_cmd, sizeof(*scsi_cmd)); 426160422Sken scsi_cmd->opcode = READ_DVD_STRUCTURE; 426260422Sken 426360422Sken scsi_ulto4b(address, scsi_cmd->address); 426460422Sken scsi_cmd->layer_number = layer_number; 426560422Sken scsi_cmd->format = format; 426660422Sken scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 426760422Sken /* The AGID is the top two bits of this byte */ 426860422Sken scsi_cmd->agid = agid << 6; 426960422Sken 427060422Sken cam_fill_csio(csio, 427160422Sken retries, 427260422Sken cbfcnp, 427360422Sken /*flags*/ CAM_DIR_IN, 427460422Sken tag_action, 427560422Sken /*data_ptr*/ data_ptr, 427660422Sken /*dxfer_len*/ dxfer_len, 427760422Sken sense_len, 427860422Sken sizeof(*scsi_cmd), 427960422Sken timeout); 428060422Sken} 4281