scsi_cd.c revision 169605
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 169605 2007-05-16 16:54:23Z 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
332169562SscottlMALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
333169562Sscottl
334104880Sphkstatic void
33539213Sgibbscdinit(void)
33639213Sgibbs{
33739213Sgibbs	cam_status status;
33839213Sgibbs
339168752Sscottl	mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
340168752Sscottl	STAILQ_INIT(&changerq);
341168752Sscottl
34239213Sgibbs	/*
34339213Sgibbs	 * Install a global async callback.  This callback will
34439213Sgibbs	 * receive async callbacks like "new device found".
34539213Sgibbs	 */
346169605Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
34739213Sgibbs
34839213Sgibbs	if (status != CAM_REQ_CMP) {
34939213Sgibbs		printf("cd: Failed to attach master async callback "
35039213Sgibbs		       "due to status 0x%x!\n", status);
35139213Sgibbs	}
35239213Sgibbs}
35339213Sgibbs
35439213Sgibbsstatic void
35540603Skencdoninvalidate(struct cam_periph *periph)
35640603Sken{
35740603Sken	struct cd_softc *softc;
35840603Sken
35940603Sken	softc = (struct cd_softc *)periph->softc;
36040603Sken
36140603Sken	/*
36240603Sken	 * De-register any async callbacks.
36340603Sken	 */
364169605Sscottl	xpt_register_async(0, cdasync, periph, periph->path);
36540603Sken
36640603Sken	softc->flags |= CD_FLAG_INVALID;
36740603Sken
36840603Sken	/*
36940603Sken	 * Return all queued I/O with ENXIO.
37040603Sken	 * XXX Handle any transactions queued to the card
37140603Sken	 *     with XPT_ABORT_CCB.
37240603Sken	 */
373112946Sphk	bioq_flush(&softc->bio_queue, NULL, ENXIO);
37440603Sken
37540603Sken	/*
37640603Sken	 * If this device is part of a changer, and it was scheduled
37740603Sken	 * to run, remove it from the run queue since we just nuked
37840603Sken	 * all of its scheduled I/O.
37940603Sken	 */
38040603Sken	if ((softc->flags & CD_FLAG_CHANGER)
38140603Sken	 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
38240603Sken		camq_remove(&softc->changer->devq, softc->pinfo.index);
38340603Sken
384152565Sjdp	disk_gone(softc->disk);
385164906Smjacob	xpt_print(periph->path, "lost device\n");
38640603Sken}
38740603Sken
38840603Skenstatic void
38939213Sgibbscdcleanup(struct cam_periph *periph)
39039213Sgibbs{
39139213Sgibbs	struct cd_softc *softc;
39239213Sgibbs
39339213Sgibbs	softc = (struct cd_softc *)periph->softc;
39439213Sgibbs
395164906Smjacob	xpt_print(periph->path, "removing device entry\n");
39640603Sken
397120884Sthomas	if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
398120884Sthomas	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
399164906Smjacob		xpt_print(periph->path, "can't remove sysctl context\n");
400112668Sken	}
401112668Sken
40239213Sgibbs	/*
40339213Sgibbs	 * In the queued, non-active case, the device in question
40439213Sgibbs	 * has already been removed from the changer run queue.  Since this
40539213Sgibbs	 * device is active, we need to de-activate it, and schedule
40639213Sgibbs	 * another device to run.  (if there is another one to run)
40739213Sgibbs	 */
40839213Sgibbs	if ((softc->flags & CD_FLAG_CHANGER)
40939213Sgibbs	 && (softc->flags & CD_FLAG_ACTIVE)) {
41039213Sgibbs
41139213Sgibbs		/*
41239213Sgibbs		 * The purpose of the short timeout is soley to determine
41339213Sgibbs		 * whether the current device has finished or not.  Well,
41439213Sgibbs		 * since we're removing the active device, we know that it
41539213Sgibbs		 * is finished.  So, get rid of the short timeout.
41639213Sgibbs		 * Otherwise, if we're in the time period before the short
41739213Sgibbs		 * timeout fires, and there are no other devices in the
41839213Sgibbs		 * queue to run, there won't be any other device put in the
41939213Sgibbs		 * active slot.  i.e., when we call cdrunchangerqueue()
42039213Sgibbs		 * below, it won't do anything.  Then, when the short
42139213Sgibbs		 * timeout fires, it'll look at the "current device", which
42239213Sgibbs		 * we are free below, and possibly panic the kernel on a
42339213Sgibbs		 * bogus pointer reference.
42439213Sgibbs		 *
42539213Sgibbs		 * The long timeout doesn't really matter, since we
42639213Sgibbs		 * decrement the qfrozen_cnt to indicate that there is
42739213Sgibbs		 * nothing in the active slot now.  Therefore, there won't
42839213Sgibbs		 * be any bogus pointer references there.
42939213Sgibbs		 */
43039213Sgibbs		if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
431168752Sscottl			callout_stop(&softc->changer->short_handle);
43239213Sgibbs			softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
43339213Sgibbs		}
43439213Sgibbs		softc->changer->devq.qfrozen_cnt--;
43539213Sgibbs		softc->changer->flags |= CHANGER_MANUAL_CALL;
43639213Sgibbs		cdrunchangerqueue(softc->changer);
43739213Sgibbs	}
43839213Sgibbs
43939213Sgibbs	/*
44039213Sgibbs	 * If we're removing the last device on the changer, go ahead and
44139213Sgibbs	 * remove the changer device structure.
44239213Sgibbs	 */
44339213Sgibbs	if ((softc->flags & CD_FLAG_CHANGER)
44439213Sgibbs	 && (--softc->changer->num_devices == 0)) {
44539213Sgibbs
44639213Sgibbs		/*
44739213Sgibbs		 * Theoretically, there shouldn't be any timeouts left, but
44839213Sgibbs		 * I'm not completely sure that that will be the case.  So,
44939213Sgibbs		 * it won't hurt to check and see if there are any left.
45039213Sgibbs		 */
45139213Sgibbs		if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
452168752Sscottl			callout_stop(&softc->changer->long_handle);
45339213Sgibbs			softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
45439213Sgibbs		}
45539213Sgibbs
45639213Sgibbs		if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
457168752Sscottl			callout_stop(&softc->changer->short_handle);
45839213Sgibbs			softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
45939213Sgibbs		}
46039213Sgibbs
461168752Sscottl		mtx_lock(&changerq_mtx);
46260938Sjake		STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
46339213Sgibbs			      changer_links);
464168752Sscottl		num_changers--;
465168752Sscottl		mtx_unlock(&changerq_mtx);
466164906Smjacob		xpt_print(periph->path, "removing changer entry\n");
46739213Sgibbs		free(softc->changer, M_DEVBUF);
46839213Sgibbs	}
469168786Sscottl	cam_periph_unlock(periph);
470125975Sphk	disk_destroy(softc->disk);
471168786Sscottl	cam_periph_lock(periph);
47240603Sken	free(softc, M_DEVBUF);
47339213Sgibbs}
47439213Sgibbs
47539213Sgibbsstatic void
47639213Sgibbscdasync(void *callback_arg, u_int32_t code,
47739213Sgibbs	struct cam_path *path, void *arg)
47839213Sgibbs{
47939213Sgibbs	struct cam_periph *periph;
48039213Sgibbs
48139213Sgibbs	periph = (struct cam_periph *)callback_arg;
48239213Sgibbs	switch (code) {
48339213Sgibbs	case AC_FOUND_DEVICE:
48439213Sgibbs	{
48539213Sgibbs		struct ccb_getdev *cgd;
48639213Sgibbs		cam_status status;
48739213Sgibbs
48839213Sgibbs		cgd = (struct ccb_getdev *)arg;
48979177Smjacob		if (cgd == NULL)
49079177Smjacob			break;
49139213Sgibbs
49256148Smjacob		if (SID_TYPE(&cgd->inq_data) != T_CDROM
49356148Smjacob		    && SID_TYPE(&cgd->inq_data) != T_WORM)
49439213Sgibbs			break;
49539213Sgibbs
49639213Sgibbs		/*
49739213Sgibbs		 * Allocate a peripheral instance for
49839213Sgibbs		 * this device and start the probe
49939213Sgibbs		 * process.
50039213Sgibbs		 */
50140603Sken		status = cam_periph_alloc(cdregister, cdoninvalidate,
50240603Sken					  cdcleanup, cdstart,
50340603Sken					  "cd", CAM_PERIPH_BIO,
50440603Sken					  cgd->ccb_h.path, cdasync,
50540603Sken					  AC_FOUND_DEVICE, cgd);
50639213Sgibbs
50739213Sgibbs		if (status != CAM_REQ_CMP
50839213Sgibbs		 && status != CAM_REQ_INPROG)
50939213Sgibbs			printf("cdasync: Unable to attach new device "
51039213Sgibbs			       "due to status 0x%x\n", status);
51139213Sgibbs
51239213Sgibbs		break;
51339213Sgibbs	}
51439213Sgibbs	case AC_SENT_BDR:
51539213Sgibbs	case AC_BUS_RESET:
51639213Sgibbs	{
51739213Sgibbs		struct cd_softc *softc;
51839213Sgibbs		struct ccb_hdr *ccbh;
51939213Sgibbs
52039213Sgibbs		softc = (struct cd_softc *)periph->softc;
52139213Sgibbs		/*
52239213Sgibbs		 * Don't fail on the expected unit attention
52339213Sgibbs		 * that will occur.
52439213Sgibbs		 */
52539213Sgibbs		softc->flags |= CD_FLAG_RETRY_UA;
52671999Sphk		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
52739213Sgibbs			ccbh->ccb_state |= CD_CCB_RETRY_UA;
52847413Sgibbs		/* FALLTHROUGH */
52939213Sgibbs	}
53039213Sgibbs	default:
53147413Sgibbs		cam_periph_async(periph, code, path, arg);
53239213Sgibbs		break;
53339213Sgibbs	}
53439213Sgibbs}
53539213Sgibbs
536119708Skenstatic void
537119708Skencdsysctlinit(void *context, int pending)
538119708Sken{
539119708Sken	struct cam_periph *periph;
540119708Sken	struct cd_softc *softc;
541119708Sken	char tmpstr[80], tmpstr2[80];
542119708Sken
543119708Sken	periph = (struct cam_periph *)context;
544168752Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
545168752Sscottl		return;
546168752Sscottl
547119708Sken	softc = (struct cd_softc *)periph->softc;
548119708Sken	snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
549119708Sken	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
550119708Sken
551119708Sken	mtx_lock(&Giant);
552119708Sken
553119708Sken	sysctl_ctx_init(&softc->sysctl_ctx);
554120884Sthomas	softc->flags |= CD_FLAG_SCTX_INIT;
555119708Sken	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
556119708Sken		SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
557119708Sken		tmpstr2, CTLFLAG_RD, 0, tmpstr);
558119708Sken
559119708Sken	if (softc->sysctl_tree == NULL) {
560119708Sken		printf("cdsysctlinit: unable to allocate sysctl tree\n");
561160345Sdelphij		mtx_unlock(&Giant);
562168752Sscottl		cam_periph_release(periph);
563119708Sken		return;
564119708Sken	}
565119708Sken
566119708Sken	/*
567119708Sken	 * Now register the sysctl handler, so the user can the value on
568119708Sken	 * the fly.
569119708Sken	 */
570119708Sken	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
571119708Sken		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
572119708Sken		&softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
573119708Sken		"Minimum CDB size");
574119708Sken
575119708Sken	mtx_unlock(&Giant);
576168752Sscottl	cam_periph_release(periph);
577119708Sken}
578119708Sken
579111206Sken/*
580111206Sken * We have a handler function for this so we can check the values when the
581111206Sken * user sets them, instead of every time we look at them.
582111206Sken */
583111206Skenstatic int
584111206Skencdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
585111206Sken{
586111206Sken	int error, value;
587111206Sken
588111206Sken	value = *(int *)arg1;
589111206Sken
590111206Sken	error = sysctl_handle_int(oidp, &value, 0, req);
591111206Sken
592111206Sken	if ((error != 0)
593111206Sken	 || (req->newptr == NULL))
594111206Sken		return (error);
595111206Sken
596111206Sken	/*
597111206Sken	 * The only real values we can have here are 6 or 10.  I don't
598111206Sken	 * really forsee having 12 be an option at any time in the future.
599111206Sken	 * So if the user sets something less than or equal to 6, we'll set
600111206Sken	 * it to 6.  If he sets something greater than 6, we'll set it to 10.
601111206Sken	 *
602111206Sken	 * I suppose we could just return an error here for the wrong values,
603111206Sken	 * but I don't think it's necessary to do so, as long as we can
604111206Sken	 * determine the user's intent without too much trouble.
605111206Sken	 */
606111206Sken	if (value < 6)
607111206Sken		value = 6;
608111206Sken	else if (value > 6)
609111206Sken		value = 10;
610111206Sken
611111206Sken	*(int *)arg1 = value;
612111206Sken
613111206Sken	return (0);
614111206Sken}
615111206Sken
61639213Sgibbsstatic cam_status
61739213Sgibbscdregister(struct cam_periph *periph, void *arg)
61839213Sgibbs{
61939213Sgibbs	struct cd_softc *softc;
620118105Snjl	struct ccb_pathinq cpi;
62139213Sgibbs	struct ccb_getdev *cgd;
622119708Sken	char tmpstr[80];
62339213Sgibbs	caddr_t match;
62439213Sgibbs
62539213Sgibbs	cgd = (struct ccb_getdev *)arg;
62639213Sgibbs	if (periph == NULL) {
62739213Sgibbs		printf("cdregister: periph was NULL!!\n");
62839213Sgibbs		return(CAM_REQ_CMP_ERR);
62939213Sgibbs	}
63039213Sgibbs	if (cgd == NULL) {
63139213Sgibbs		printf("cdregister: no getdev CCB, can't register device\n");
63239213Sgibbs		return(CAM_REQ_CMP_ERR);
63339213Sgibbs	}
63439213Sgibbs
63539213Sgibbs	softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
63639213Sgibbs
63739213Sgibbs	if (softc == NULL) {
63839213Sgibbs		printf("cdregister: Unable to probe new device. "
63939213Sgibbs		       "Unable to allocate softc\n");
64039213Sgibbs		return(CAM_REQ_CMP_ERR);
64139213Sgibbs	}
64239213Sgibbs
64339213Sgibbs	bzero(softc, sizeof(*softc));
64439213Sgibbs	LIST_INIT(&softc->pending_ccbs);
645111206Sken	STAILQ_INIT(&softc->mode_queue);
64639213Sgibbs	softc->state = CD_STATE_PROBE;
64759249Sphk	bioq_init(&softc->bio_queue);
64839213Sgibbs	if (SID_IS_REMOVABLE(&cgd->inq_data))
64939213Sgibbs		softc->flags |= CD_FLAG_DISC_REMOVABLE;
65039213Sgibbs	if ((cgd->inq_data.flags & SID_CmdQue) != 0)
65139213Sgibbs		softc->flags |= CD_FLAG_TAGGED_QUEUING;
65239213Sgibbs
65339213Sgibbs	periph->softc = softc;
65439213Sgibbs	softc->periph = periph;
65539213Sgibbs
65639213Sgibbs	/*
65739213Sgibbs	 * See if this device has any quirks.
65839213Sgibbs	 */
65939213Sgibbs	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
66039213Sgibbs			       (caddr_t)cd_quirk_table,
66139213Sgibbs			       sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
66239213Sgibbs			       sizeof(*cd_quirk_table), scsi_inquiry_match);
66339213Sgibbs
66439213Sgibbs	if (match != NULL)
66539213Sgibbs		softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
66639213Sgibbs	else
66739213Sgibbs		softc->quirks = CD_Q_NONE;
66839213Sgibbs
669118105Snjl	/* Check if the SIM does not want 6 byte commands */
670118105Snjl	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
671118105Snjl	cpi.ccb_h.func_code = XPT_PATH_INQ;
672118105Snjl	xpt_action((union ccb *)&cpi);
673118105Snjl	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
674118105Snjl		softc->quirks |= CD_Q_10_BYTE_ONLY;
675118105Snjl
676119708Sken	TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
677111206Sken
678111206Sken	/* The default is 6 byte commands, unless quirked otherwise */
679111206Sken	if (softc->quirks & CD_Q_10_BYTE_ONLY)
680111206Sken		softc->minimum_command_size = 10;
681111206Sken	else
682111206Sken		softc->minimum_command_size = 6;
683111206Sken
68439213Sgibbs	/*
685111206Sken	 * Load the user's default, if any.
686111206Sken	 */
687111206Sken	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
688111206Sken		 periph->unit_number);
689111206Sken	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
690111206Sken
691111206Sken	/* 6 and 10 are the only permissible values here. */
692111206Sken	if (softc->minimum_command_size < 6)
693111206Sken		softc->minimum_command_size = 6;
694111206Sken	else if (softc->minimum_command_size > 6)
695111206Sken		softc->minimum_command_size = 10;
696111206Sken
697111206Sken	/*
69839213Sgibbs	 * We need to register the statistics structure for this device,
69939213Sgibbs	 * but we don't have the blocksize yet for it.  So, we register
70039213Sgibbs	 * the structure and indicate that we don't have the blocksize
70139213Sgibbs	 * yet.  Unlike other SCSI peripheral drivers, we explicitly set
70239213Sgibbs	 * the device type here to be CDROM, rather than just ORing in
70356148Smjacob	 * the device type.  This is because this driver can attach to either
70439213Sgibbs	 * CDROM or WORM devices, and we want this peripheral driver to
70539213Sgibbs	 * show up in the devstat list as a CD peripheral driver, not a
70639213Sgibbs	 * WORM peripheral driver.  WORM drives will also have the WORM
70739213Sgibbs	 * driver attached to them.
70839213Sgibbs	 */
709168752Sscottl	cam_periph_unlock(periph);
710125975Sphk	softc->disk = disk_alloc();
711125975Sphk	softc->disk->d_devstat = devstat_new_entry("cd",
71239213Sgibbs			  periph->unit_number, 0,
71339213Sgibbs	  		  DEVSTAT_BS_UNAVAILABLE,
71443819Sken			  DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
71543819Sken			  DEVSTAT_PRIORITY_CD);
716125975Sphk	softc->disk->d_open = cdopen;
717125975Sphk	softc->disk->d_close = cdclose;
718125975Sphk	softc->disk->d_strategy = cdstrategy;
719125975Sphk	softc->disk->d_ioctl = cdioctl;
720125975Sphk	softc->disk->d_name = "cd";
721125975Sphk	softc->disk->d_unit = periph->unit_number;
722125975Sphk	softc->disk->d_drv1 = periph;
723168752Sscottl	softc->disk->d_flags = 0;
724125975Sphk	disk_create(softc->disk, DISK_VERSION);
725168752Sscottl	cam_periph_lock(periph);
72639213Sgibbs
72739213Sgibbs	/*
72839213Sgibbs	 * Add an async callback so that we get
72939213Sgibbs	 * notified if this device goes away.
73039213Sgibbs	 */
731169605Sscottl	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
732169605Sscottl			   cdasync, periph, periph->path);
73339213Sgibbs
73439213Sgibbs	/*
73539213Sgibbs	 * If the target lun is greater than 0, we most likely have a CD
73639213Sgibbs	 * changer device.  Check the quirk entries as well, though, just
73739213Sgibbs	 * in case someone has a CD tower with one lun per drive or
73839213Sgibbs	 * something like that.  Also, if we know up front that a
73939213Sgibbs	 * particular device is a changer, we can mark it as such starting
74039213Sgibbs	 * with lun 0, instead of lun 1.  It shouldn't be necessary to have
74139213Sgibbs	 * a quirk entry to define something as a changer, however.
74239213Sgibbs	 */
74339213Sgibbs	if (((cgd->ccb_h.target_lun > 0)
74439213Sgibbs	  && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
74539213Sgibbs	 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
74639213Sgibbs		struct cdchanger *nchanger;
74739213Sgibbs		struct cam_periph *nperiph;
74839213Sgibbs		struct cam_path *path;
74939213Sgibbs		cam_status status;
75039213Sgibbs		int found;
75139213Sgibbs
75239213Sgibbs		/* Set the changer flag in the current device's softc */
75339213Sgibbs		softc->flags |= CD_FLAG_CHANGER;
75439213Sgibbs
75539213Sgibbs		/*
75639213Sgibbs		 * Now, look around for an existing changer device with the
75739213Sgibbs		 * same path and target ID as the current device.
75839213Sgibbs		 */
759168752Sscottl		mtx_lock(&changerq_mtx);
76039213Sgibbs		for (found = 0,
76139213Sgibbs		     nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
76239213Sgibbs		     nchanger != NULL;
76339213Sgibbs		     nchanger = STAILQ_NEXT(nchanger, changer_links)){
76439213Sgibbs			if ((nchanger->path_id == cgd->ccb_h.path_id)
76539213Sgibbs			 && (nchanger->target_id == cgd->ccb_h.target_id)) {
76639213Sgibbs				found = 1;
76739213Sgibbs				break;
76839213Sgibbs			}
76939213Sgibbs		}
770168752Sscottl		mtx_unlock(&changerq_mtx);
77139213Sgibbs
77239213Sgibbs		/*
77339213Sgibbs		 * If we found a matching entry, just add this device to
77439213Sgibbs		 * the list of devices on this changer.
77539213Sgibbs		 */
77639213Sgibbs		if (found == 1) {
77739213Sgibbs			struct chdevlist *chlunhead;
77839213Sgibbs
77939213Sgibbs			chlunhead = &nchanger->chluns;
78039213Sgibbs
78139213Sgibbs			/*
78239213Sgibbs			 * XXX KDM look at consolidating this code with the
78339213Sgibbs			 * code below in a separate function.
78439213Sgibbs			 */
78539213Sgibbs
78639213Sgibbs			/*
78739213Sgibbs			 * Create a path with lun id 0, and see if we can
78839213Sgibbs			 * find a matching device
78939213Sgibbs			 */
79039213Sgibbs			status = xpt_create_path(&path, /*periph*/ periph,
79139213Sgibbs						 cgd->ccb_h.path_id,
79239213Sgibbs						 cgd->ccb_h.target_id, 0);
79339213Sgibbs
79439213Sgibbs			if ((status == CAM_REQ_CMP)
79539213Sgibbs			 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
79639213Sgibbs				struct cd_softc *nsoftc;
79739213Sgibbs
79839213Sgibbs				nsoftc = (struct cd_softc *)nperiph->softc;
79939213Sgibbs
80039213Sgibbs				if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
80139213Sgibbs					nsoftc->flags |= CD_FLAG_CHANGER;
80239213Sgibbs					nchanger->num_devices++;
80339213Sgibbs					if (camq_resize(&nchanger->devq,
80439213Sgibbs					   nchanger->num_devices)!=CAM_REQ_CMP){
80539213Sgibbs						printf("cdregister: "
80639213Sgibbs						       "camq_resize "
80739213Sgibbs						       "failed, changer "
80839213Sgibbs						       "support may "
80939213Sgibbs						       "be messed up\n");
81039213Sgibbs					}
81139213Sgibbs					nsoftc->changer = nchanger;
81239213Sgibbs					nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
81339213Sgibbs
81439213Sgibbs					STAILQ_INSERT_TAIL(&nchanger->chluns,
81539213Sgibbs							  nsoftc,changer_links);
81639213Sgibbs				}
81767928Sken				xpt_free_path(path);
81839213Sgibbs			} else if (status == CAM_REQ_CMP)
81939213Sgibbs				xpt_free_path(path);
82039213Sgibbs			else {
82139213Sgibbs				printf("cdregister: unable to allocate path\n"
82239213Sgibbs				       "cdregister: changer support may be "
82339213Sgibbs				       "broken\n");
82439213Sgibbs			}
82539213Sgibbs
82639213Sgibbs			nchanger->num_devices++;
82739213Sgibbs
82839213Sgibbs			softc->changer = nchanger;
82939213Sgibbs			softc->pinfo.index = CAM_UNQUEUED_INDEX;
83039213Sgibbs
83139213Sgibbs			if (camq_resize(&nchanger->devq,
83239213Sgibbs			    nchanger->num_devices) != CAM_REQ_CMP) {
83339213Sgibbs				printf("cdregister: camq_resize "
83439213Sgibbs				       "failed, changer support may "
83539213Sgibbs				       "be messed up\n");
83639213Sgibbs			}
83739213Sgibbs
83839213Sgibbs			STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
83939213Sgibbs		}
84039213Sgibbs		/*
84139213Sgibbs		 * In this case, we don't already have an entry for this
84239213Sgibbs		 * particular changer, so we need to create one, add it to
84339213Sgibbs		 * the queue, and queue this device on the list for this
84439213Sgibbs		 * changer.  Before we queue this device, however, we need
84539213Sgibbs		 * to search for lun id 0 on this target, and add it to the
84639213Sgibbs		 * queue first, if it exists.  (and if it hasn't already
84739213Sgibbs		 * been marked as part of the changer.)
84839213Sgibbs		 */
84939213Sgibbs		else {
85039213Sgibbs			nchanger = malloc(sizeof(struct cdchanger),
85139213Sgibbs				M_DEVBUF, M_NOWAIT);
85239213Sgibbs
85339213Sgibbs			if (nchanger == NULL) {
85439213Sgibbs				softc->flags &= ~CD_FLAG_CHANGER;
85539213Sgibbs				printf("cdregister: unable to malloc "
85639213Sgibbs				       "changer structure\ncdregister: "
85739213Sgibbs				       "changer support disabled\n");
85839213Sgibbs
85939213Sgibbs				/*
86039213Sgibbs				 * Yes, gotos can be gross but in this case
86139213Sgibbs				 * I think it's justified..
86239213Sgibbs				 */
86339213Sgibbs				goto cdregisterexit;
86439213Sgibbs			}
86539213Sgibbs
86639213Sgibbs			/* zero the structure */
86739213Sgibbs			bzero(nchanger, sizeof(struct cdchanger));
86839213Sgibbs
86939213Sgibbs			if (camq_init(&nchanger->devq, 1) != 0) {
87039213Sgibbs				softc->flags &= ~CD_FLAG_CHANGER;
87139213Sgibbs				printf("cdregister: changer support "
87239213Sgibbs				       "disabled\n");
87339213Sgibbs				goto cdregisterexit;
87439213Sgibbs			}
87539213Sgibbs
87639213Sgibbs			nchanger->path_id = cgd->ccb_h.path_id;
87739213Sgibbs			nchanger->target_id = cgd->ccb_h.target_id;
87839213Sgibbs
87939213Sgibbs			/* this is superfluous, but it makes things clearer */
88039213Sgibbs			nchanger->num_devices = 0;
88139213Sgibbs
88239213Sgibbs			STAILQ_INIT(&nchanger->chluns);
88339213Sgibbs
884168752Sscottl			callout_init_mtx(&nchanger->long_handle,
885168752Sscottl			    periph->sim->mtx, 0);
886168752Sscottl			callout_init_mtx(&nchanger->short_handle,
887168752Sscottl			    periph->sim->mtx, 0);
888168752Sscottl
889168752Sscottl			mtx_lock(&changerq_mtx);
890168752Sscottl			num_changers++;
89139213Sgibbs			STAILQ_INSERT_TAIL(&changerq, nchanger,
89239213Sgibbs					   changer_links);
893168752Sscottl			mtx_unlock(&changerq_mtx);
89439213Sgibbs
89539213Sgibbs			/*
89639213Sgibbs			 * Create a path with lun id 0, and see if we can
89739213Sgibbs			 * find a matching device
89839213Sgibbs			 */
89939213Sgibbs			status = xpt_create_path(&path, /*periph*/ periph,
90039213Sgibbs						 cgd->ccb_h.path_id,
90139213Sgibbs						 cgd->ccb_h.target_id, 0);
90239213Sgibbs
90339213Sgibbs			/*
90439213Sgibbs			 * If we were able to allocate the path, and if we
90539213Sgibbs			 * find a matching device and it isn't already
90639213Sgibbs			 * marked as part of a changer, then we add it to
90739213Sgibbs			 * the current changer.
90839213Sgibbs			 */
90939213Sgibbs			if ((status == CAM_REQ_CMP)
91039213Sgibbs			 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
91139213Sgibbs			 && ((((struct cd_softc *)periph->softc)->flags &
91239213Sgibbs			       CD_FLAG_CHANGER) == 0)) {
91339213Sgibbs				struct cd_softc *nsoftc;
91439213Sgibbs
91539213Sgibbs				nsoftc = (struct cd_softc *)nperiph->softc;
91639213Sgibbs
91739213Sgibbs				nsoftc->flags |= CD_FLAG_CHANGER;
91839213Sgibbs				nchanger->num_devices++;
91939213Sgibbs				if (camq_resize(&nchanger->devq,
92039213Sgibbs				    nchanger->num_devices) != CAM_REQ_CMP) {
92139213Sgibbs					printf("cdregister: camq_resize "
92239213Sgibbs					       "failed, changer support may "
92339213Sgibbs					       "be messed up\n");
92439213Sgibbs				}
92539213Sgibbs				nsoftc->changer = nchanger;
92639213Sgibbs				nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
92739213Sgibbs
92839213Sgibbs				STAILQ_INSERT_TAIL(&nchanger->chluns,
92939213Sgibbs						   nsoftc, changer_links);
93067928Sken				xpt_free_path(path);
93139213Sgibbs			} else if (status == CAM_REQ_CMP)
93239213Sgibbs				xpt_free_path(path);
93339213Sgibbs			else {
93439213Sgibbs				printf("cdregister: unable to allocate path\n"
93539213Sgibbs				       "cdregister: changer support may be "
93639213Sgibbs				       "broken\n");
93739213Sgibbs			}
93839213Sgibbs
93939213Sgibbs			softc->changer = nchanger;
94039213Sgibbs			softc->pinfo.index = CAM_UNQUEUED_INDEX;
94139213Sgibbs			nchanger->num_devices++;
94239213Sgibbs			if (camq_resize(&nchanger->devq,
94339213Sgibbs			    nchanger->num_devices) != CAM_REQ_CMP) {
94439213Sgibbs				printf("cdregister: camq_resize "
94539213Sgibbs				       "failed, changer support may "
94639213Sgibbs				       "be messed up\n");
94739213Sgibbs			}
94839213Sgibbs			STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
94939213Sgibbs					   changer_links);
95039213Sgibbs		}
95139213Sgibbs	}
95239213Sgibbs
95339213Sgibbscdregisterexit:
95439213Sgibbs
955168752Sscottl	/*
956168752Sscottl	 * Refcount and block open attempts until we are setup
957168752Sscottl	 * Can't block
958168752Sscottl	 */
959168752Sscottl	(void)cam_periph_hold(periph, PRIBIO);
96039213Sgibbs
96139213Sgibbs	if ((softc->flags & CD_FLAG_CHANGER) == 0)
96239213Sgibbs		xpt_schedule(periph, /*priority*/5);
96339213Sgibbs	else
96439213Sgibbs		cdschedule(periph, /*priority*/ 5);
96539213Sgibbs
96639213Sgibbs	return(CAM_REQ_CMP);
96739213Sgibbs}
96839213Sgibbs
96939213Sgibbsstatic int
970120599Sphkcdopen(struct disk *dp)
97139213Sgibbs{
97239213Sgibbs	struct cam_periph *periph;
97339213Sgibbs	struct cd_softc *softc;
974101940Snjl	int error;
97539213Sgibbs
976120599Sphk	periph = (struct cam_periph *)dp->d_drv1;
97739213Sgibbs	if (periph == NULL)
97839213Sgibbs		return (ENXIO);
97939213Sgibbs
98039213Sgibbs	softc = (struct cd_softc *)periph->softc;
98139213Sgibbs
982168752Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
983168752Sscottl		return(ENXIO);
984168752Sscottl
985168752Sscottl	cam_periph_lock(periph);
986168752Sscottl
98740603Sken	if (softc->flags & CD_FLAG_INVALID) {
988168752Sscottl		cam_periph_unlock(periph);
989168752Sscottl		cam_periph_release(periph);
99039213Sgibbs		return(ENXIO);
99140603Sken	}
99239213Sgibbs
993168752Sscottl	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
994168752Sscottl		cam_periph_unlock(periph);
995168752Sscottl		cam_periph_release(periph);
99639213Sgibbs		return (error);
99741297Sken	}
99839213Sgibbs
999168752Sscottl	/* Closes aren't symmetrical with opens, so fix up the refcounting. */
1000168752Sscottl	if (softc->flags & CD_FLAG_OPEN)
1001168752Sscottl		cam_periph_release(periph);
1002168752Sscottl	else
1003168752Sscottl		softc->flags |= CD_FLAG_OPEN;
100441297Sken
100540020Sken	/*
1006111206Sken	 * Check for media, and set the appropriate flags.  We don't bail
1007111206Sken	 * if we don't have media, but then we don't allow anything but the
1008111206Sken	 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
100951836Sphk	 */
1010111206Sken	cdcheckmedia(periph);
101140020Sken
1012168752Sscottl	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1013168752Sscottl	cam_periph_unhold(periph);
101439213Sgibbs	cam_periph_unlock(periph);
101539213Sgibbs
1016168752Sscottl	return (0);
101739213Sgibbs}
101839213Sgibbs
101939213Sgibbsstatic int
1020120599Sphkcdclose(struct disk *dp)
102139213Sgibbs{
102239213Sgibbs	struct 	cam_periph *periph;
102339213Sgibbs	struct	cd_softc *softc;
102439213Sgibbs
1025120599Sphk	periph = (struct cam_periph *)dp->d_drv1;
102639213Sgibbs	if (periph == NULL)
102739213Sgibbs		return (ENXIO);
102839213Sgibbs
102939213Sgibbs	softc = (struct cd_softc *)periph->softc;
103039213Sgibbs
1031168752Sscottl	cam_periph_lock(periph);
1032168752Sscottl	cam_periph_hold(periph, PRIBIO);
103339213Sgibbs
103439213Sgibbs	if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
103539213Sgibbs		cdprevent(periph, PR_ALLOW);
103639213Sgibbs
103739213Sgibbs	/*
103839213Sgibbs	 * Since we're closing this CD, mark the blocksize as unavailable.
1039111206Sken	 * It will be marked as available when the CD is opened again.
104039213Sgibbs	 */
1041125975Sphk	softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
104239213Sgibbs
1043111206Sken	/*
1044111206Sken	 * We'll check the media and toc again at the next open().
1045111206Sken	 */
1046168752Sscottl	softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1047111206Sken
1048168752Sscottl	cam_periph_unhold(periph);
104939213Sgibbs	cam_periph_unlock(periph);
105039213Sgibbs	cam_periph_release(periph);
105139213Sgibbs
105239213Sgibbs	return (0);
105339213Sgibbs}
105439213Sgibbs
105539213Sgibbsstatic void
105639213Sgibbscdshorttimeout(void *arg)
105739213Sgibbs{
105839213Sgibbs	struct cdchanger *changer;
105939213Sgibbs
106039213Sgibbs	changer = (struct cdchanger *)arg;
106139213Sgibbs
106239213Sgibbs	/* Always clear the short timeout flag, since that's what we're in */
106339213Sgibbs	changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
106439213Sgibbs
106539213Sgibbs	/*
106639213Sgibbs	 * Check to see if there is any more pending or outstanding I/O for
106739213Sgibbs	 * this device.  If not, move it out of the active slot.
106839213Sgibbs	 */
106959249Sphk	if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1070112262Sphk	 && (changer->cur_device->outstanding_cmds == 0)) {
107139213Sgibbs		changer->flags |= CHANGER_MANUAL_CALL;
107239213Sgibbs		cdrunchangerqueue(changer);
107339213Sgibbs	}
107439213Sgibbs}
107539213Sgibbs
107639213Sgibbs/*
107739213Sgibbs * This is a wrapper for xpt_schedule.  It only applies to changers.
107839213Sgibbs */
107939213Sgibbsstatic void
108039213Sgibbscdschedule(struct cam_periph *periph, int priority)
108139213Sgibbs{
108239213Sgibbs	struct cd_softc *softc;
108339213Sgibbs
108439213Sgibbs	softc = (struct cd_softc *)periph->softc;
108539213Sgibbs
108639213Sgibbs	/*
108739213Sgibbs	 * If this device isn't currently queued, and if it isn't
108839213Sgibbs	 * the active device, then we queue this device and run the
108939213Sgibbs	 * changer queue if there is no timeout scheduled to do it.
109039213Sgibbs	 * If this device is the active device, just schedule it
109139213Sgibbs	 * to run again.  If this device is queued, there should be
109239213Sgibbs	 * a timeout in place already that will make sure it runs.
109339213Sgibbs	 */
109439213Sgibbs	if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
109539213Sgibbs	 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
109639213Sgibbs		/*
109739213Sgibbs		 * We don't do anything with the priority here.
109839213Sgibbs		 * This is strictly a fifo queue.
109939213Sgibbs		 */
110039213Sgibbs		softc->pinfo.priority = 1;
110145442Sgibbs		softc->pinfo.generation = ++softc->changer->devq.generation;
110239213Sgibbs		camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
110339213Sgibbs
110439213Sgibbs		/*
110539213Sgibbs		 * Since we just put a device in the changer queue,
110639213Sgibbs		 * check and see if there is a timeout scheduled for
110739213Sgibbs		 * this changer.  If so, let the timeout handle
110839213Sgibbs		 * switching this device into the active slot.  If
110939213Sgibbs		 * not, manually call the timeout routine to
111039213Sgibbs		 * bootstrap things.
111139213Sgibbs		 */
111239213Sgibbs		if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
111346581Sken		 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
111446581Sken		 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
111539213Sgibbs			softc->changer->flags |= CHANGER_MANUAL_CALL;
111639213Sgibbs			cdrunchangerqueue(softc->changer);
111739213Sgibbs		}
111839213Sgibbs	} else if ((softc->flags & CD_FLAG_ACTIVE)
111939213Sgibbs		&& ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
112039213Sgibbs		xpt_schedule(periph, priority);
112139213Sgibbs}
112239213Sgibbs
112339213Sgibbsstatic void
112439213Sgibbscdrunchangerqueue(void *arg)
112539213Sgibbs{
112639213Sgibbs	struct cd_softc *softc;
112739213Sgibbs	struct cdchanger *changer;
112839213Sgibbs	int called_from_timeout;
112939213Sgibbs
113039213Sgibbs	changer = (struct cdchanger *)arg;
113139213Sgibbs
113239213Sgibbs	/*
113339213Sgibbs	 * If we have NOT been called from cdstrategy() or cddone(), and
113439213Sgibbs	 * instead from a timeout routine, go ahead and clear the
113539213Sgibbs	 * timeout flag.
113639213Sgibbs	 */
113739213Sgibbs	if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
113839213Sgibbs		changer->flags &= ~CHANGER_TIMEOUT_SCHED;
113939213Sgibbs		called_from_timeout = 1;
114039213Sgibbs	} else
114139213Sgibbs		called_from_timeout = 0;
114239213Sgibbs
114339213Sgibbs	/* Always clear the manual call flag */
114439213Sgibbs	changer->flags &= ~CHANGER_MANUAL_CALL;
114539213Sgibbs
114639213Sgibbs	/* nothing to do if the queue is empty */
114739213Sgibbs	if (changer->devq.entries <= 0) {
114839213Sgibbs		return;
114939213Sgibbs	}
115039213Sgibbs
115139213Sgibbs	/*
115239213Sgibbs	 * If the changer queue is frozen, that means we have an active
115339213Sgibbs	 * device.
115439213Sgibbs	 */
115539213Sgibbs	if (changer->devq.qfrozen_cnt > 0) {
115639213Sgibbs
1157168752Sscottl		/*
1158168752Sscottl		 * We always need to reset the frozen count and clear the
1159168752Sscottl		 * active flag.
1160168752Sscottl		 */
1161168752Sscottl		changer->devq.qfrozen_cnt--;
1162168752Sscottl		changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1163168752Sscottl		changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1164168752Sscottl
1165112262Sphk		if (changer->cur_device->outstanding_cmds > 0) {
116639213Sgibbs			changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
116739213Sgibbs			changer->cur_device->bufs_left =
1168112262Sphk				changer->cur_device->outstanding_cmds;
116939213Sgibbs			if (called_from_timeout) {
1170168752Sscottl				callout_reset(&changer->long_handle,
1171168752Sscottl			            changer_max_busy_seconds * hz,
1172168752Sscottl				    cdrunchangerqueue, changer);
117339213Sgibbs				changer->flags |= CHANGER_TIMEOUT_SCHED;
117439213Sgibbs			}
117539213Sgibbs			return;
117639213Sgibbs		}
117739213Sgibbs
117839213Sgibbs		/*
117939213Sgibbs		 * Check to see whether the current device has any I/O left
118039213Sgibbs		 * to do.  If so, requeue it at the end of the queue.  If
118139213Sgibbs		 * not, there is no need to requeue it.
118239213Sgibbs		 */
118359249Sphk		if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
118439213Sgibbs
118539213Sgibbs			changer->cur_device->pinfo.generation =
118645442Sgibbs				++changer->devq.generation;
118739213Sgibbs			camq_insert(&changer->devq,
118839213Sgibbs				(cam_pinfo *)changer->cur_device);
118939213Sgibbs		}
119039213Sgibbs	}
119139213Sgibbs
119245845Sgibbs	softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
119339213Sgibbs
119439213Sgibbs	changer->cur_device = softc;
119539213Sgibbs
119639213Sgibbs	changer->devq.qfrozen_cnt++;
119739213Sgibbs	softc->flags |= CD_FLAG_ACTIVE;
119839213Sgibbs
119939213Sgibbs	/* Just in case this device is waiting */
120039213Sgibbs	wakeup(&softc->changer);
120139213Sgibbs	xpt_schedule(softc->periph, /*priority*/ 1);
120239213Sgibbs
120339213Sgibbs	/*
120439213Sgibbs	 * Get rid of any pending timeouts, and set a flag to schedule new
120539213Sgibbs	 * ones so this device gets its full time quantum.
120639213Sgibbs	 */
120739213Sgibbs	if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1208168752Sscottl		callout_stop(&changer->long_handle);
120939213Sgibbs		changer->flags &= ~CHANGER_TIMEOUT_SCHED;
121039213Sgibbs	}
121139213Sgibbs
121239213Sgibbs	if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1213168752Sscottl		callout_stop(&changer->short_handle);
121439213Sgibbs		changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
121539213Sgibbs	}
121639213Sgibbs
121739213Sgibbs	/*
121839213Sgibbs	 * We need to schedule timeouts, but we only do this after the
121939213Sgibbs	 * first transaction has completed.  This eliminates the changer
122039213Sgibbs	 * switch time.
122139213Sgibbs	 */
122239213Sgibbs	changer->flags |= CHANGER_NEED_TIMEOUT;
122339213Sgibbs}
122439213Sgibbs
122539213Sgibbsstatic void
122639213Sgibbscdchangerschedule(struct cd_softc *softc)
122739213Sgibbs{
122839213Sgibbs	struct cdchanger *changer;
122939213Sgibbs
123039213Sgibbs	changer = softc->changer;
123139213Sgibbs
123239213Sgibbs	/*
123339213Sgibbs	 * If this is a changer, and this is the current device,
123439213Sgibbs	 * and this device has at least the minimum time quantum to
123539213Sgibbs	 * run, see if we can switch it out.
123639213Sgibbs	 */
123739213Sgibbs	if ((softc->flags & CD_FLAG_ACTIVE)
123839213Sgibbs	 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
123939213Sgibbs	 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
124039213Sgibbs		/*
124139213Sgibbs		 * We try three things here.  The first is that we
124239213Sgibbs		 * check to see whether the schedule on completion
124339213Sgibbs		 * flag is set.  If it is, we decrement the number
124439213Sgibbs		 * of buffers left, and if it's zero, we reschedule.
124539213Sgibbs		 * Next, we check to see whether the pending buffer
124639213Sgibbs		 * queue is empty and whether there are no
124739213Sgibbs		 * outstanding transactions.  If so, we reschedule.
124839213Sgibbs		 * Next, we see if the pending buffer queue is empty.
124939213Sgibbs		 * If it is, we set the number of buffers left to
125039213Sgibbs		 * the current active buffer count and set the
125139213Sgibbs		 * schedule on complete flag.
125239213Sgibbs		 */
125339213Sgibbs		if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
125439213Sgibbs		 	if (--softc->bufs_left == 0) {
125539213Sgibbs				softc->changer->flags |=
125639213Sgibbs					CHANGER_MANUAL_CALL;
125739213Sgibbs				softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
125839213Sgibbs				cdrunchangerqueue(softc->changer);
125939213Sgibbs			}
126059249Sphk		} else if ((bioq_first(&softc->bio_queue) == NULL)
1261112262Sphk		        && (softc->outstanding_cmds == 0)) {
126239213Sgibbs			softc->changer->flags |= CHANGER_MANUAL_CALL;
126339213Sgibbs			cdrunchangerqueue(softc->changer);
126439213Sgibbs		}
126539213Sgibbs	} else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
126639213Sgibbs		&& (softc->flags & CD_FLAG_ACTIVE)) {
126739213Sgibbs
126839213Sgibbs		/*
126939213Sgibbs		 * Now that the first transaction to this
127039213Sgibbs		 * particular device has completed, we can go ahead
127139213Sgibbs		 * and schedule our timeouts.
127239213Sgibbs		 */
127339213Sgibbs		if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1274168752Sscottl			callout_reset(&changer->long_handle,
1275168752Sscottl			    changer_max_busy_seconds * hz,
1276168752Sscottl			    cdrunchangerqueue, changer);
127739213Sgibbs			changer->flags |= CHANGER_TIMEOUT_SCHED;
127839213Sgibbs		} else
127939213Sgibbs			printf("cdchangerschedule: already have a long"
128039213Sgibbs			       " timeout!\n");
128139213Sgibbs
128239213Sgibbs		if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1283168752Sscottl			callout_reset(&changer->short_handle,
1284168752Sscottl			    changer_min_busy_seconds * hz,
1285168752Sscottl			    cdshorttimeout, changer);
128639213Sgibbs			changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
128739213Sgibbs		} else
128839213Sgibbs			printf("cdchangerschedule: already have a short "
128939213Sgibbs			       "timeout!\n");
129039213Sgibbs
129139213Sgibbs		/*
129239213Sgibbs		 * We just scheduled timeouts, no need to schedule
129339213Sgibbs		 * more.
129439213Sgibbs		 */
129539213Sgibbs		changer->flags &= ~CHANGER_NEED_TIMEOUT;
129639213Sgibbs
129739213Sgibbs	}
129839213Sgibbs}
129939213Sgibbs
130039213Sgibbsstatic int
130139213Sgibbscdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
130239213Sgibbs					      u_int32_t cam_flags,
130339213Sgibbs					      u_int32_t sense_flags),
130439213Sgibbs	 u_int32_t cam_flags, u_int32_t sense_flags)
130539213Sgibbs{
130639213Sgibbs	struct cd_softc *softc;
130739213Sgibbs	struct cam_periph *periph;
130839213Sgibbs	int error;
130939213Sgibbs
131039213Sgibbs	periph = xpt_path_periph(ccb->ccb_h.path);
131139213Sgibbs	softc = (struct cd_softc *)periph->softc;
131239213Sgibbs
131339213Sgibbs	error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1314125975Sphk				  softc->disk->d_devstat);
131539213Sgibbs
131639213Sgibbs	if (softc->flags & CD_FLAG_CHANGER)
131739213Sgibbs		cdchangerschedule(softc);
131839213Sgibbs
131939213Sgibbs	return(error);
132039213Sgibbs}
132139213Sgibbs
132242017Seivindstatic union ccb *
132339213Sgibbscdgetccb(struct cam_periph *periph, u_int32_t priority)
132439213Sgibbs{
132539213Sgibbs	struct cd_softc *softc;
132639213Sgibbs
132739213Sgibbs	softc = (struct cd_softc *)periph->softc;
132839213Sgibbs
132939213Sgibbs	if (softc->flags & CD_FLAG_CHANGER) {
133039213Sgibbs		/*
133139213Sgibbs		 * This should work the first time this device is woken up,
133239213Sgibbs		 * but just in case it doesn't, we use a while loop.
133339213Sgibbs		 */
133446581Sken		while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
133539213Sgibbs			/*
133639213Sgibbs			 * If this changer isn't already queued, queue it up.
133739213Sgibbs			 */
133839213Sgibbs			if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
133939213Sgibbs				softc->pinfo.priority = 1;
134039213Sgibbs				softc->pinfo.generation =
134145442Sgibbs					++softc->changer->devq.generation;
134239213Sgibbs				camq_insert(&softc->changer->devq,
134339213Sgibbs					    (cam_pinfo *)softc);
134439213Sgibbs			}
134546581Sken			if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
134646581Sken			 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
134746581Sken			 && ((softc->changer->flags
134846581Sken			      & CHANGER_SHORT_TMOUT_SCHED)==0)) {
134939213Sgibbs				softc->changer->flags |= CHANGER_MANUAL_CALL;
135039213Sgibbs				cdrunchangerqueue(softc->changer);
135139213Sgibbs			} else
1352168752Sscottl				msleep(&softc->changer, periph->sim->mtx,
1353168752Sscottl				    PRIBIO, "cgticb", 0);
135439213Sgibbs		}
135539213Sgibbs	}
135639213Sgibbs	return(cam_periph_getccb(periph, priority));
135739213Sgibbs}
135839213Sgibbs
135939213Sgibbs
136039213Sgibbs/*
136139213Sgibbs * Actually translate the requested transfer into one the physical driver
136239213Sgibbs * can understand.  The transfer is described by a buf and will include
136339213Sgibbs * only one physical transfer.
136439213Sgibbs */
136539213Sgibbsstatic void
136659249Sphkcdstrategy(struct bio *bp)
136739213Sgibbs{
136839213Sgibbs	struct cam_periph *periph;
136939213Sgibbs	struct cd_softc *softc;
137039213Sgibbs
1371120599Sphk	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
137239213Sgibbs	if (periph == NULL) {
137376362Sphk		biofinish(bp, NULL, ENXIO);
137476362Sphk		return;
137539213Sgibbs	}
137639213Sgibbs
1377168752Sscottl	cam_periph_lock(periph);
137839213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
137939213Sgibbs
138039213Sgibbs	softc = (struct cd_softc *)periph->softc;
138139213Sgibbs
138239213Sgibbs	/*
138339213Sgibbs	 * If the device has been made invalid, error out
138439213Sgibbs	 */
138539213Sgibbs	if ((softc->flags & CD_FLAG_INVALID)) {
1386168752Sscottl		cam_periph_unlock(periph);
138776362Sphk		biofinish(bp, NULL, ENXIO);
138876362Sphk		return;
138939213Sgibbs	}
139039213Sgibbs
1391111206Sken        /*
1392111206Sken	 * If we don't have valid media, look for it before trying to
1393111206Sken	 * schedule the I/O.
1394111206Sken	 */
1395111206Sken	if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1396111206Sken		int error;
1397111206Sken
1398111206Sken		error = cdcheckmedia(periph);
1399111206Sken		if (error != 0) {
1400168752Sscottl			cam_periph_unlock(periph);
1401111206Sken			biofinish(bp, NULL, error);
1402111206Sken			return;
1403111206Sken		}
1404111206Sken	}
1405111206Sken
140639213Sgibbs	/*
140739213Sgibbs	 * Place it in the queue of disk activities for this disk
140839213Sgibbs	 */
1409112946Sphk	bioq_disksort(&softc->bio_queue, bp);
141039213Sgibbs
141139213Sgibbs	/*
141239213Sgibbs	 * Schedule ourselves for performing the work.  We do things
141339213Sgibbs	 * differently for changers.
141439213Sgibbs	 */
141539213Sgibbs	if ((softc->flags & CD_FLAG_CHANGER) == 0)
141639213Sgibbs		xpt_schedule(periph, /* XXX priority */1);
141739213Sgibbs	else
141839213Sgibbs		cdschedule(periph, /* priority */ 1);
141939213Sgibbs
1420168752Sscottl	cam_periph_unlock(periph);
142139213Sgibbs	return;
142239213Sgibbs}
142339213Sgibbs
142439213Sgibbsstatic void
142539213Sgibbscdstart(struct cam_periph *periph, union ccb *start_ccb)
142639213Sgibbs{
142739213Sgibbs	struct cd_softc *softc;
142859249Sphk	struct bio *bp;
142939213Sgibbs	struct ccb_scsiio *csio;
143039213Sgibbs	struct scsi_read_capacity_data *rcap;
143139213Sgibbs
143239213Sgibbs	softc = (struct cd_softc *)periph->softc;
143339213Sgibbs
143439213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
143539213Sgibbs
143639213Sgibbs	switch (softc->state) {
143739213Sgibbs	case CD_STATE_NORMAL:
143839213Sgibbs	{
143959249Sphk		bp = bioq_first(&softc->bio_queue);
144039213Sgibbs		if (periph->immediate_priority <= periph->pinfo.priority) {
144139213Sgibbs			start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
144239213Sgibbs
144339213Sgibbs			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
144439213Sgibbs					  periph_links.sle);
144539213Sgibbs			periph->immediate_priority = CAM_PRIORITY_NONE;
144639213Sgibbs			wakeup(&periph->ccb_list);
144739213Sgibbs		} else if (bp == NULL) {
144839213Sgibbs			xpt_release_ccb(start_ccb);
144939213Sgibbs		} else {
145059249Sphk			bioq_remove(&softc->bio_queue, bp);
145139213Sgibbs
1452125975Sphk			devstat_start_transaction_bio(softc->disk->d_devstat, bp);
145339213Sgibbs
145439213Sgibbs			scsi_read_write(&start_ccb->csio,
145539213Sgibbs					/*retries*/4,
145639213Sgibbs					/* cbfcnp */ cddone,
145791062Sphk					MSG_SIMPLE_Q_TAG,
145859249Sphk					/* read */bp->bio_cmd == BIO_READ,
145939213Sgibbs					/* byte2 */ 0,
146039213Sgibbs					/* minimum_cmd_size */ 10,
1461121209Sphk					/* lba */ bp->bio_offset /
1462121209Sphk					  softc->params.blksize,
146359249Sphk					bp->bio_bcount / softc->params.blksize,
146459249Sphk					/* data_ptr */ bp->bio_data,
146559249Sphk					/* dxfer_len */ bp->bio_bcount,
146639213Sgibbs					/* sense_len */ SSD_FULL_SIZE,
146739213Sgibbs					/* timeout */ 30000);
146839213Sgibbs			start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
146939213Sgibbs
147039213Sgibbs
147139213Sgibbs			LIST_INSERT_HEAD(&softc->pending_ccbs,
147239213Sgibbs					 &start_ccb->ccb_h, periph_links.le);
1473112262Sphk			softc->outstanding_cmds++;
147439213Sgibbs
147539213Sgibbs			/* We expect a unit attention from this device */
147639213Sgibbs			if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
147739213Sgibbs				start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
147839213Sgibbs				softc->flags &= ~CD_FLAG_RETRY_UA;
147939213Sgibbs			}
148039213Sgibbs
148139213Sgibbs			start_ccb->ccb_h.ccb_bp = bp;
148259249Sphk			bp = bioq_first(&softc->bio_queue);
148339213Sgibbs
148439213Sgibbs			xpt_action(start_ccb);
148539213Sgibbs		}
148639213Sgibbs		if (bp != NULL) {
148739213Sgibbs			/* Have more work to do, so ensure we stay scheduled */
148839213Sgibbs			xpt_schedule(periph, /* XXX priority */1);
148939213Sgibbs		}
149039213Sgibbs		break;
149139213Sgibbs	}
149239213Sgibbs	case CD_STATE_PROBE:
149339213Sgibbs	{
149439213Sgibbs
149539213Sgibbs		rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1496169562Sscottl								M_SCSICD,
149739213Sgibbs								M_NOWAIT);
149839213Sgibbs		if (rcap == NULL) {
1499164906Smjacob			xpt_print(periph->path,
1500164906Smjacob			    "cdstart: Couldn't malloc read_capacity data\n");
150139213Sgibbs			/* cd_free_periph??? */
150239213Sgibbs			break;
150339213Sgibbs		}
150439213Sgibbs		csio = &start_ccb->csio;
150539213Sgibbs		scsi_read_capacity(csio,
150639213Sgibbs				   /*retries*/1,
150739213Sgibbs				   cddone,
150839213Sgibbs				   MSG_SIMPLE_Q_TAG,
150939213Sgibbs				   rcap,
151039213Sgibbs				   SSD_FULL_SIZE,
151139213Sgibbs				   /*timeout*/20000);
151239213Sgibbs		start_ccb->ccb_h.ccb_bp = NULL;
151339213Sgibbs		start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
151439213Sgibbs		xpt_action(start_ccb);
151539213Sgibbs		break;
151639213Sgibbs	}
151739213Sgibbs	}
151839213Sgibbs}
151939213Sgibbs
152039213Sgibbsstatic void
152139213Sgibbscddone(struct cam_periph *periph, union ccb *done_ccb)
152239213Sgibbs{
152339213Sgibbs	struct cd_softc *softc;
152439213Sgibbs	struct ccb_scsiio *csio;
152539213Sgibbs
152639213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
152739213Sgibbs
152839213Sgibbs	softc = (struct cd_softc *)periph->softc;
152939213Sgibbs	csio = &done_ccb->csio;
153039213Sgibbs
153139213Sgibbs	switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
153239213Sgibbs	case CD_CCB_BUFFER_IO:
153339213Sgibbs	{
153459249Sphk		struct bio	*bp;
153539213Sgibbs		int		error;
153639213Sgibbs
153759249Sphk		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
153839213Sgibbs		error = 0;
153939213Sgibbs
154039213Sgibbs		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
154139213Sgibbs			int sf;
154239213Sgibbs
154339213Sgibbs			if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
154439213Sgibbs				sf = SF_RETRY_UA;
154539213Sgibbs			else
154639213Sgibbs				sf = 0;
154746747Sken
154874840Sken			error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
154974840Sken			if (error == ERESTART) {
155039213Sgibbs				/*
155139213Sgibbs				 * A retry was scheuled, so
155239213Sgibbs				 * just return.
155339213Sgibbs				 */
155439213Sgibbs				return;
155539213Sgibbs			}
155639213Sgibbs		}
155739213Sgibbs
155839213Sgibbs		if (error != 0) {
1559164906Smjacob			xpt_print(periph->path,
1560164906Smjacob			    "cddone: got error %#x back\n", error);
1561112946Sphk			bioq_flush(&softc->bio_queue, NULL, EIO);
156259249Sphk			bp->bio_resid = bp->bio_bcount;
156359249Sphk			bp->bio_error = error;
156459249Sphk			bp->bio_flags |= BIO_ERROR;
156539213Sgibbs			cam_release_devq(done_ccb->ccb_h.path,
156639213Sgibbs					 /*relsim_flags*/0,
156739213Sgibbs					 /*reduction*/0,
156839213Sgibbs					 /*timeout*/0,
156939213Sgibbs					 /*getcount_only*/0);
157039213Sgibbs
157139213Sgibbs		} else {
157259249Sphk			bp->bio_resid = csio->resid;
157359249Sphk			bp->bio_error = 0;
157459249Sphk			if (bp->bio_resid != 0) {
1575104456Sphk				/*
1576104456Sphk				 * Short transfer ???
1577104456Sphk				 * XXX: not sure this is correct for partial
1578104456Sphk				 * transfers at EOM
1579104456Sphk				 */
158059249Sphk				bp->bio_flags |= BIO_ERROR;
158139213Sgibbs			}
158239213Sgibbs		}
158339213Sgibbs
158439213Sgibbs		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1585112262Sphk		softc->outstanding_cmds--;
158639213Sgibbs
158739213Sgibbs		if (softc->flags & CD_FLAG_CHANGER)
158839213Sgibbs			cdchangerschedule(softc);
158939213Sgibbs
1590157806Smaxim		biofinish(bp, NULL, 0);
159139213Sgibbs		break;
159239213Sgibbs	}
159339213Sgibbs	case CD_CCB_PROBE:
159439213Sgibbs	{
159539213Sgibbs		struct	   scsi_read_capacity_data *rdcap;
159639213Sgibbs		char	   announce_buf[120]; /*
159739213Sgibbs					       * Currently (9/30/97) the
159839213Sgibbs					       * longest possible announce
159939213Sgibbs					       * buffer is 108 bytes, for the
160039213Sgibbs					       * first error case below.
160139213Sgibbs					       * That is 39 bytes for the
160239213Sgibbs					       * basic string, 16 bytes for the
160339213Sgibbs					       * biggest sense key (hardware
160439213Sgibbs					       * error), 52 bytes for the
160539213Sgibbs					       * text of the largest sense
160639213Sgibbs					       * qualifier valid for a CDROM,
160739213Sgibbs					       * (0x72, 0x03 or 0x04,
160839213Sgibbs					       * 0x03), and one byte for the
160939213Sgibbs					       * null terminating character.
161039213Sgibbs					       * To allow for longer strings,
161139213Sgibbs					       * the announce buffer is 120
161239213Sgibbs					       * bytes.
161339213Sgibbs					       */
161439213Sgibbs		struct	   cd_params *cdp;
161539213Sgibbs
161639213Sgibbs		cdp = &softc->params;
161739213Sgibbs
161839213Sgibbs		rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
161939213Sgibbs
162039213Sgibbs		cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
162139213Sgibbs		cdp->blksize = scsi_4btoul (rdcap->length);
162239213Sgibbs
162339213Sgibbs		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
162439213Sgibbs
162541514Sarchie			snprintf(announce_buf, sizeof(announce_buf),
162640020Sken				"cd present [%lu x %lu byte records]",
162740020Sken				cdp->disksize, (u_long)cdp->blksize);
162839213Sgibbs
162939213Sgibbs		} else {
163039213Sgibbs			int	error;
163139213Sgibbs			/*
163239213Sgibbs			 * Retry any UNIT ATTENTION type errors.  They
163339213Sgibbs			 * are expected at boot.
163439213Sgibbs			 */
163574840Sken			error = cderror(done_ccb, CAM_RETRY_SELTO,
163674840Sken					SF_RETRY_UA | SF_NO_PRINT);
163739213Sgibbs			if (error == ERESTART) {
163839213Sgibbs				/*
163939213Sgibbs				 * A retry was scheuled, so
164039213Sgibbs				 * just return.
164139213Sgibbs				 */
164239213Sgibbs				return;
164339213Sgibbs			} else if (error != 0) {
164439213Sgibbs
164539213Sgibbs				struct scsi_sense_data *sense;
164639213Sgibbs				int asc, ascq;
164739213Sgibbs				int sense_key, error_code;
164839213Sgibbs				int have_sense;
164939213Sgibbs				cam_status status;
165039213Sgibbs				struct ccb_getdev cgd;
165139213Sgibbs
165239213Sgibbs				/* Don't wedge this device's queue */
165339213Sgibbs				cam_release_devq(done_ccb->ccb_h.path,
165439213Sgibbs						 /*relsim_flags*/0,
165539213Sgibbs						 /*reduction*/0,
165639213Sgibbs						 /*timeout*/0,
165739213Sgibbs						 /*getcount_only*/0);
165839213Sgibbs
165939213Sgibbs				status = done_ccb->ccb_h.status;
166039213Sgibbs
166139213Sgibbs				xpt_setup_ccb(&cgd.ccb_h,
166239213Sgibbs					      done_ccb->ccb_h.path,
166339213Sgibbs					      /* priority */ 1);
166439213Sgibbs				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
166539213Sgibbs				xpt_action((union ccb *)&cgd);
166639213Sgibbs
166739213Sgibbs				if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
166839213Sgibbs				 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
166939213Sgibbs				 || ((status & CAM_AUTOSNS_VALID) == 0))
167039213Sgibbs					have_sense = FALSE;
167139213Sgibbs				else
167239213Sgibbs					have_sense = TRUE;
167339213Sgibbs
167439213Sgibbs				if (have_sense) {
167539213Sgibbs					sense = &csio->sense_data;
167639213Sgibbs					scsi_extract_sense(sense, &error_code,
167739213Sgibbs							   &sense_key,
167839213Sgibbs							   &asc, &ascq);
167939213Sgibbs				}
168039213Sgibbs				/*
168146581Sken				 * Attach to anything that claims to be a
168246581Sken				 * CDROM or WORM device, as long as it
168346581Sken				 * doesn't return a "Logical unit not
168446581Sken				 * supported" (0x25) error.
168539213Sgibbs				 */
168646581Sken				if ((have_sense) && (asc != 0x25)
168774840Sken				 && (error_code == SSD_CURRENT_ERROR)) {
168874840Sken					const char *sense_key_desc;
168974840Sken					const char *asc_desc;
169074840Sken
169174840Sken					scsi_sense_desc(sense_key, asc, ascq,
169274840Sken							&cgd.inq_data,
169374840Sken							&sense_key_desc,
169474840Sken							&asc_desc);
169541514Sarchie					snprintf(announce_buf,
169641514Sarchie					    sizeof(announce_buf),
169739213Sgibbs						"Attempt to query device "
169839213Sgibbs						"size failed: %s, %s",
169974840Sken						sense_key_desc,
170074840Sken						asc_desc);
170182850Sken 				} else if ((have_sense == 0)
170282850Sken 				      && ((status & CAM_STATUS_MASK) ==
170382850Sken 					   CAM_SCSI_STATUS_ERROR)
170482850Sken 				      && (csio->scsi_status ==
170582850Sken 					  SCSI_STATUS_BUSY)) {
170682850Sken 					snprintf(announce_buf,
170782850Sken 					    sizeof(announce_buf),
170882850Sken 					    "Attempt to query device "
170982850Sken 					    "size failed: SCSI Status: %s",
171082850Sken					    scsi_status_string(csio));
171174840Sken				} else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
171239213Sgibbs					/*
171339213Sgibbs					 * We only print out an error for
171439213Sgibbs					 * CDROM type devices.  For WORM
171539213Sgibbs					 * devices, we don't print out an
171639213Sgibbs					 * error since a few WORM devices
171739213Sgibbs					 * don't support CDROM commands.
171839213Sgibbs					 * If we have sense information, go
171939213Sgibbs					 * ahead and print it out.
172039213Sgibbs					 * Otherwise, just say that we
172139213Sgibbs					 * couldn't attach.
172239213Sgibbs					 */
172339213Sgibbs
172439213Sgibbs					/*
172539213Sgibbs					 * Just print out the error, not
172639213Sgibbs					 * the full probe message, when we
172739213Sgibbs					 * don't attach.
172839213Sgibbs					 */
172940020Sken					if (have_sense)
173040020Sken						scsi_sense_print(
173140020Sken							&done_ccb->csio);
173240020Sken					else {
1733164906Smjacob						xpt_print(periph->path,
1734164906Smjacob						    "got CAM status %#x\n",
1735164906Smjacob						    done_ccb->ccb_h.status);
173640020Sken					}
1737164906Smjacob					xpt_print(periph->path, "fatal error, "
1738164906Smjacob					    "failed to attach to device\n");
173939213Sgibbs					/*
174040603Sken					 * Invalidate this peripheral.
174139213Sgibbs					 */
174239213Sgibbs					cam_periph_invalidate(periph);
174340020Sken
174440020Sken					announce_buf[0] = '\0';
174539213Sgibbs				} else {
174640603Sken
174739213Sgibbs					/*
174840603Sken					 * Invalidate this peripheral.
174939213Sgibbs					 */
175039213Sgibbs					cam_periph_invalidate(periph);
175140020Sken					announce_buf[0] = '\0';
175239213Sgibbs				}
175339213Sgibbs			}
175439213Sgibbs		}
1755169562Sscottl		free(rdcap, M_SCSICD);
175642378Smjacob		if (announce_buf[0] != '\0') {
175739213Sgibbs			xpt_announce_periph(periph, announce_buf);
175842378Smjacob			if (softc->flags & CD_FLAG_CHANGER)
175942378Smjacob				cdchangerschedule(softc);
1760119708Sken			/*
1761119708Sken			 * Create our sysctl variables, now that we know
1762119708Sken			 * we have successfully attached.
1763119708Sken			 */
1764119708Sken			taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
176542378Smjacob		}
176640020Sken		softc->state = CD_STATE_NORMAL;
176742378Smjacob		/*
176842378Smjacob		 * Since our peripheral may be invalidated by an error
176942378Smjacob		 * above or an external event, we must release our CCB
177042378Smjacob		 * before releasing the probe lock on the peripheral.
177142378Smjacob		 * The peripheral will only go away once the last lock
177242378Smjacob		 * is removed, and we need it around for the CCB release
177342378Smjacob		 * operation.
177442378Smjacob		 */
177542378Smjacob		xpt_release_ccb(done_ccb);
1776168752Sscottl		cam_periph_unhold(periph);
177742378Smjacob		return;
177839213Sgibbs	}
177939213Sgibbs	case CD_CCB_WAITING:
178039213Sgibbs	{
178139213Sgibbs		/* Caller will release the CCB */
178239213Sgibbs		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
178339213Sgibbs			  ("trying to wakeup ccbwait\n"));
178439213Sgibbs
178539213Sgibbs		wakeup(&done_ccb->ccb_h.cbfcnp);
178639213Sgibbs		return;
178739213Sgibbs	}
178842378Smjacob	default:
178942378Smjacob		break;
179039213Sgibbs	}
179139213Sgibbs	xpt_release_ccb(done_ccb);
179239213Sgibbs}
179339213Sgibbs
1794111206Skenstatic union cd_pages *
1795111206Skencdgetpage(struct cd_mode_params *mode_params)
1796111206Sken{
1797111206Sken	union cd_pages *page;
1798111206Sken
1799111206Sken	if (mode_params->cdb_size == 10)
1800111206Sken		page = (union cd_pages *)find_mode_page_10(
1801111206Sken			(struct scsi_mode_header_10 *)mode_params->mode_buf);
1802111206Sken	else
1803111206Sken		page = (union cd_pages *)find_mode_page_6(
1804111206Sken			(struct scsi_mode_header_6 *)mode_params->mode_buf);
1805111206Sken
1806111206Sken	return (page);
1807111206Sken}
1808111206Sken
180939213Sgibbsstatic int
1810111206Skencdgetpagesize(int page_num)
1811111206Sken{
1812111206Sken	int i;
1813111206Sken
1814111206Sken	for (i = 0; i < (sizeof(cd_page_size_table)/
1815111206Sken	     sizeof(cd_page_size_table[0])); i++) {
1816111206Sken		if (cd_page_size_table[i].page == page_num)
1817111206Sken			return (cd_page_size_table[i].page_size);
1818111206Sken	}
1819111206Sken
1820111206Sken	return (-1);
1821111206Sken}
1822111206Sken
1823111206Skenstatic int
1824120599Sphkcdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
182539213Sgibbs{
182639213Sgibbs
182739213Sgibbs	struct 	cam_periph *periph;
182839213Sgibbs	struct	cd_softc *softc;
1829168752Sscottl	int	nocopyout, error = 0;
183039213Sgibbs
1831120599Sphk	periph = (struct cam_periph *)dp->d_drv1;
183239213Sgibbs	if (periph == NULL)
183339213Sgibbs		return(ENXIO);
183439213Sgibbs
1835168752Sscottl	cam_periph_lock(periph);
183639213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
183739213Sgibbs
183839213Sgibbs	softc = (struct cd_softc *)periph->softc;
183939213Sgibbs
184039213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
184140020Sken		  ("trying to do ioctl %#lx\n", cmd));
184239213Sgibbs
1843168752Sscottl	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1844168752Sscottl		cam_periph_unlock(periph);
1845168752Sscottl		cam_periph_release(periph);
1846168752Sscottl		return (error);
1847168752Sscottl	}
184839213Sgibbs
1849111206Sken	/*
1850111206Sken	 * If we don't have media loaded, check for it.  If still don't
1851111206Sken	 * have media loaded, we can only do a load or eject.
1852144135Sken	 *
1853144135Sken	 * We only care whether media is loaded if this is a cd-specific ioctl
1854144135Sken	 * (thus the IOCGROUP check below).  Note that this will break if
1855144135Sken	 * anyone adds any ioctls into the switch statement below that don't
1856144135Sken	 * have their ioctl group set to 'c'.
1857111206Sken	 */
1858111206Sken	if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1859111206Sken	 && ((cmd != CDIOCCLOSE)
1860144135Sken	  && (cmd != CDIOCEJECT))
1861144135Sken	 && (IOCGROUP(cmd) == 'c')) {
1862111206Sken		error = cdcheckmedia(periph);
1863111206Sken	}
1864168752Sscottl	/*
1865168752Sscottl	 * Drop the lock here so later mallocs can use WAITOK.  The periph
1866168752Sscottl	 * is essentially locked still with the cam_periph_hold call above.
1867168752Sscottl	 */
1868168752Sscottl	cam_periph_unlock(periph);
1869168752Sscottl	if (error != 0)
1870168752Sscottl		return (error);
187140020Sken
1872141031Ssobomax	nocopyout = 0;
187339213Sgibbs	switch (cmd) {
187439213Sgibbs
187539213Sgibbs	case CDIOCPLAYTRACKS:
187639213Sgibbs		{
187739213Sgibbs			struct ioc_play_track *args
187839213Sgibbs			    = (struct ioc_play_track *) addr;
1879111206Sken			struct cd_mode_params params;
1880111206Sken			union cd_pages *page;
188139213Sgibbs
1882111206Sken			params.alloc_len = sizeof(union cd_mode_data_6_10);
1883169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1884111206Sken						 M_WAITOK | M_ZERO);
188539213Sgibbs
1886168752Sscottl			cam_periph_lock(periph);
188739213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
188839213Sgibbs				  ("trying to do CDIOCPLAYTRACKS\n"));
188939213Sgibbs
1890111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
189139213Sgibbs			if (error) {
1892169562Sscottl				free(params.mode_buf, M_SCSICD);
1893168752Sscottl				cam_periph_unlock(periph);
189439213Sgibbs				break;
189539213Sgibbs			}
1896111206Sken			page = cdgetpage(&params);
1897111206Sken
1898111206Sken			page->audio.flags &= ~CD_PA_SOTC;
1899111206Sken			page->audio.flags |= CD_PA_IMMED;
1900111206Sken			error = cdsetmode(periph, &params);
1901169562Sscottl			free(params.mode_buf, M_SCSICD);
1902168752Sscottl			if (error) {
1903168752Sscottl				cam_periph_unlock(periph);
190439213Sgibbs				break;
1905168752Sscottl			}
1906111206Sken
1907111206Sken			/*
1908111206Sken			 * This was originally implemented with the PLAY
1909111206Sken			 * AUDIO TRACK INDEX command, but that command was
1910111206Sken			 * deprecated after SCSI-2.  Most (all?) SCSI CDROM
1911111206Sken			 * drives support it but ATAPI and ATAPI-derivative
1912111206Sken			 * drives don't seem to support it.  So we keep a
1913111206Sken			 * cache of the table of contents and translate
1914111206Sken			 * track numbers to MSF format.
1915111206Sken			 */
1916111206Sken			if (softc->flags & CD_FLAG_VALID_TOC) {
1917111206Sken				union msf_lba *sentry, *eentry;
1918111206Sken				int st, et;
1919111206Sken
1920111206Sken				if (args->end_track <
1921111206Sken				    softc->toc.header.ending_track + 1)
1922111206Sken					args->end_track++;
1923111206Sken				if (args->end_track >
1924111206Sken				    softc->toc.header.ending_track + 1)
1925111206Sken					args->end_track =
1926111206Sken					    softc->toc.header.ending_track + 1;
1927111206Sken				st = args->start_track -
1928111206Sken					softc->toc.header.starting_track;
1929111206Sken				et = args->end_track -
1930111206Sken					softc->toc.header.starting_track;
1931111206Sken				if ((st < 0)
1932111206Sken				 || (et < 0)
1933111206Sken			 	 || (st > (softc->toc.header.ending_track -
1934111206Sken				     softc->toc.header.starting_track))) {
1935111206Sken					error = EINVAL;
1936111206Sken					break;
1937111206Sken				}
1938111206Sken				sentry = &softc->toc.entries[st].addr;
1939111206Sken				eentry = &softc->toc.entries[et].addr;
1940111206Sken				error = cdplaymsf(periph,
1941111206Sken						  sentry->msf.minute,
1942111206Sken						  sentry->msf.second,
1943111206Sken						  sentry->msf.frame,
1944111206Sken						  eentry->msf.minute,
1945111206Sken						  eentry->msf.second,
1946111206Sken						  eentry->msf.frame);
1947111206Sken			} else {
1948111206Sken				/*
1949111206Sken				 * If we don't have a valid TOC, try the
1950111206Sken				 * play track index command.  It is part of
1951111206Sken				 * the SCSI-2 spec, but was removed in the
1952111206Sken				 * MMC specs.  ATAPI and ATAPI-derived
1953111206Sken				 * drives don't support it.
1954111206Sken				 */
1955111206Sken				if (softc->quirks & CD_Q_BCD_TRACKS) {
1956111206Sken					args->start_track =
1957111206Sken						bin2bcd(args->start_track);
1958111206Sken					args->end_track =
1959111206Sken						bin2bcd(args->end_track);
1960111206Sken				}
1961111206Sken				error = cdplaytracks(periph,
1962111206Sken						     args->start_track,
1963111206Sken						     args->start_index,
1964111206Sken						     args->end_track,
1965111206Sken						     args->end_index);
196639213Sgibbs			}
1967168752Sscottl			cam_periph_unlock(periph);
196839213Sgibbs		}
196939213Sgibbs		break;
197039213Sgibbs	case CDIOCPLAYMSF:
197139213Sgibbs		{
197239213Sgibbs			struct ioc_play_msf *args
197339213Sgibbs				= (struct ioc_play_msf *) addr;
1974111206Sken			struct cd_mode_params params;
1975111206Sken			union cd_pages *page;
197639213Sgibbs
1977111206Sken			params.alloc_len = sizeof(union cd_mode_data_6_10);
1978169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
1979111206Sken						 M_WAITOK | M_ZERO);
198039213Sgibbs
1981168752Sscottl			cam_periph_lock(periph);
198239213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
198339213Sgibbs				  ("trying to do CDIOCPLAYMSF\n"));
198439213Sgibbs
1985111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
198639213Sgibbs			if (error) {
1987169562Sscottl				free(params.mode_buf, M_SCSICD);
1988168752Sscottl				cam_periph_unlock(periph);
198939213Sgibbs				break;
199039213Sgibbs			}
1991111206Sken			page = cdgetpage(&params);
1992111206Sken
1993111206Sken			page->audio.flags &= ~CD_PA_SOTC;
1994111206Sken			page->audio.flags |= CD_PA_IMMED;
1995111206Sken			error = cdsetmode(periph, &params);
1996169562Sscottl			free(params.mode_buf, M_SCSICD);
1997168752Sscottl			if (error) {
1998168752Sscottl				cam_periph_unlock(periph);
199939213Sgibbs				break;
2000168752Sscottl			}
200139213Sgibbs			error = cdplaymsf(periph,
200239213Sgibbs					  args->start_m,
200339213Sgibbs					  args->start_s,
200439213Sgibbs					  args->start_f,
200539213Sgibbs					  args->end_m,
200639213Sgibbs					  args->end_s,
200739213Sgibbs					  args->end_f);
2008168752Sscottl			cam_periph_unlock(periph);
200939213Sgibbs		}
201039213Sgibbs		break;
201139213Sgibbs	case CDIOCPLAYBLOCKS:
201239213Sgibbs		{
201339213Sgibbs			struct ioc_play_blocks *args
201439213Sgibbs				= (struct ioc_play_blocks *) addr;
2015111206Sken			struct cd_mode_params params;
2016111206Sken			union cd_pages *page;
201739213Sgibbs
2018111206Sken			params.alloc_len = sizeof(union cd_mode_data_6_10);
2019169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2020111206Sken						 M_WAITOK | M_ZERO);
202139213Sgibbs
2022168752Sscottl			cam_periph_lock(periph);
2023168752Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2024168752Sscottl				  ("trying to do CDIOCPLAYBLOCKS\n"));
2025168752Sscottl
2026168752Sscottl
2027111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
202839213Sgibbs			if (error) {
2029169562Sscottl				free(params.mode_buf, M_SCSICD);
2030168752Sscottl				cam_periph_unlock(periph);
203139213Sgibbs				break;
203239213Sgibbs			}
2033111206Sken			page = cdgetpage(&params);
2034111206Sken
2035111206Sken			page->audio.flags &= ~CD_PA_SOTC;
2036111206Sken			page->audio.flags |= CD_PA_IMMED;
2037111206Sken			error = cdsetmode(periph, &params);
2038169562Sscottl			free(params.mode_buf, M_SCSICD);
2039168752Sscottl			if (error) {
2040168752Sscottl				cam_periph_unlock(periph);
204139213Sgibbs				break;
2042168752Sscottl			}
204339213Sgibbs			error = cdplay(periph, args->blk, args->len);
2044168752Sscottl			cam_periph_unlock(periph);
204539213Sgibbs		}
204639213Sgibbs		break;
2047141031Ssobomax	case CDIOCREADSUBCHANNEL_SYSSPACE:
2048141031Ssobomax		nocopyout = 1;
2049141031Ssobomax		/* Fallthrough */
205039213Sgibbs	case CDIOCREADSUBCHANNEL:
205139213Sgibbs		{
205239213Sgibbs			struct ioc_read_subchannel *args
205339213Sgibbs				= (struct ioc_read_subchannel *) addr;
205439213Sgibbs			struct cd_sub_channel_info *data;
205539213Sgibbs			u_int32_t len = args->data_len;
205639213Sgibbs
2057168752Sscottl			data = malloc(sizeof(struct cd_sub_channel_info),
2058169562Sscottl				      M_SCSICD, M_WAITOK);
2059168752Sscottl
2060168752Sscottl			cam_periph_lock(periph);
206139213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
206239213Sgibbs				  ("trying to do CDIOCREADSUBCHANNEL\n"));
206339213Sgibbs
206439213Sgibbs			if ((len > sizeof(struct cd_sub_channel_info)) ||
206539213Sgibbs			    (len < sizeof(struct cd_sub_channel_header))) {
206639213Sgibbs				printf(
206739213Sgibbs					"scsi_cd: cdioctl: "
206839213Sgibbs					"cdioreadsubchannel: error, len=%d\n",
206939213Sgibbs					len);
207039213Sgibbs				error = EINVAL;
2071169562Sscottl				free(data, M_SCSICD);
2072168752Sscottl				cam_periph_unlock(periph);
207339213Sgibbs				break;
207439213Sgibbs			}
207539213Sgibbs
207639213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS)
207739213Sgibbs				args->track = bin2bcd(args->track);
207839213Sgibbs
207939213Sgibbs			error = cdreadsubchannel(periph, args->address_format,
208039213Sgibbs				args->data_format, args->track, data, len);
208139213Sgibbs
208239213Sgibbs			if (error) {
2083169562Sscottl				free(data, M_SCSICD);
2084168752Sscottl				cam_periph_unlock(periph);
208539213Sgibbs	 			break;
208639213Sgibbs			}
208739213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS)
208839213Sgibbs				data->what.track_info.track_number =
208939213Sgibbs				    bcd2bin(data->what.track_info.track_number);
209039213Sgibbs			len = min(len, ((data->header.data_len[0] << 8) +
209139213Sgibbs				data->header.data_len[1] +
209239213Sgibbs				sizeof(struct cd_sub_channel_header)));
2093168752Sscottl			cam_periph_unlock(periph);
2094141031Ssobomax			if (nocopyout == 0) {
2095141031Ssobomax				if (copyout(data, args->data, len) != 0) {
2096141031Ssobomax					error = EFAULT;
2097141031Ssobomax				}
2098141031Ssobomax			} else {
2099141031Ssobomax				bcopy(data, args->data, len);
210039213Sgibbs			}
2101169562Sscottl			free(data, M_SCSICD);
210239213Sgibbs		}
210339213Sgibbs		break;
210439213Sgibbs
210539213Sgibbs	case CDIOREADTOCHEADER:
210639213Sgibbs		{
210739213Sgibbs			struct ioc_toc_header *th;
210839213Sgibbs
2109169562Sscottl			th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2110168752Sscottl				    M_WAITOK);
2111168752Sscottl
2112168752Sscottl			cam_periph_lock(periph);
211339213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
211439213Sgibbs				  ("trying to do CDIOREADTOCHEADER\n"));
211539213Sgibbs
2116111206Sken			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2117111206Sken				          sizeof (*th), /*sense_flags*/0);
211839213Sgibbs			if (error) {
2119169562Sscottl				free(th, M_SCSICD);
2120168752Sscottl				cam_periph_unlock(periph);
212139213Sgibbs				break;
212239213Sgibbs			}
212339213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS) {
212439213Sgibbs				/* we are going to have to convert the BCD
212539213Sgibbs				 * encoding on the cd to what is expected
212639213Sgibbs				 */
212739213Sgibbs				th->starting_track =
212839213Sgibbs					bcd2bin(th->starting_track);
212939213Sgibbs				th->ending_track = bcd2bin(th->ending_track);
213039213Sgibbs			}
213190868Smike			th->len = ntohs(th->len);
213239213Sgibbs			bcopy(th, addr, sizeof(*th));
2133169562Sscottl			free(th, M_SCSICD);
2134168752Sscottl			cam_periph_unlock(periph);
213539213Sgibbs		}
213639213Sgibbs		break;
213739213Sgibbs	case CDIOREADTOCENTRYS:
213839213Sgibbs		{
2139111206Sken			struct cd_tocdata *data;
2140111206Sken			struct cd_toc_single *lead;
214139213Sgibbs			struct ioc_read_toc_entry *te =
214239213Sgibbs				(struct ioc_read_toc_entry *) addr;
214339213Sgibbs			struct ioc_toc_header *th;
214439213Sgibbs			u_int32_t len, readlen, idx, num;
214539213Sgibbs			u_int32_t starting_track = te->starting_track;
214639213Sgibbs
2147169562Sscottl			data = malloc(sizeof(*data), M_SCSICD, M_WAITOK);
2148169562Sscottl			lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2149168752Sscottl
2150168752Sscottl			cam_periph_lock(periph);
215139213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
215239213Sgibbs				  ("trying to do CDIOREADTOCENTRYS\n"));
215339213Sgibbs
215439213Sgibbs			if (te->data_len < sizeof(struct cd_toc_entry)
215539213Sgibbs			 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
215639213Sgibbs			 || (te->address_format != CD_MSF_FORMAT
215739213Sgibbs			  && te->address_format != CD_LBA_FORMAT)) {
215839213Sgibbs				error = EINVAL;
215939213Sgibbs				printf("scsi_cd: error in readtocentries, "
216039213Sgibbs				       "returning EINVAL\n");
2161169562Sscottl				free(data, M_SCSICD);
2162169562Sscottl				free(lead, M_SCSICD);
2163168752Sscottl				cam_periph_unlock(periph);
216439213Sgibbs				break;
216539213Sgibbs			}
216639213Sgibbs
216739213Sgibbs			th = &data->header;
2168111206Sken			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2169111206Sken					  sizeof (*th), /*sense_flags*/0);
217039213Sgibbs			if (error) {
2171169562Sscottl				free(data, M_SCSICD);
2172169562Sscottl				free(lead, M_SCSICD);
2173168752Sscottl				cam_periph_unlock(periph);
217439213Sgibbs				break;
217539213Sgibbs			}
217639213Sgibbs
217739213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS) {
217839213Sgibbs				/* we are going to have to convert the BCD
217939213Sgibbs				 * encoding on the cd to what is expected
218039213Sgibbs				 */
218139213Sgibbs				th->starting_track =
218239213Sgibbs				    bcd2bin(th->starting_track);
218339213Sgibbs				th->ending_track = bcd2bin(th->ending_track);
218439213Sgibbs			}
218539213Sgibbs
218639213Sgibbs			if (starting_track == 0)
218739213Sgibbs				starting_track = th->starting_track;
218839213Sgibbs			else if (starting_track == LEADOUT)
218939213Sgibbs				starting_track = th->ending_track + 1;
219039213Sgibbs			else if (starting_track < th->starting_track ||
219139213Sgibbs				 starting_track > th->ending_track + 1) {
219239213Sgibbs				printf("scsi_cd: error in readtocentries, "
219339213Sgibbs				       "returning EINVAL\n");
2194169562Sscottl				free(data, M_SCSICD);
2195169562Sscottl				free(lead, M_SCSICD);
2196168752Sscottl				cam_periph_unlock(periph);
219739213Sgibbs				error = EINVAL;
219839213Sgibbs				break;
219939213Sgibbs			}
220039213Sgibbs
220139213Sgibbs			/* calculate reading length without leadout entry */
220239213Sgibbs			readlen = (th->ending_track - starting_track + 1) *
220339213Sgibbs				  sizeof(struct cd_toc_entry);
220439213Sgibbs
220539213Sgibbs			/* and with leadout entry */
220639213Sgibbs			len = readlen + sizeof(struct cd_toc_entry);
220739213Sgibbs			if (te->data_len < len) {
220839213Sgibbs				len = te->data_len;
220939213Sgibbs				if (readlen > len)
221039213Sgibbs					readlen = len;
221139213Sgibbs			}
221239213Sgibbs			if (len > sizeof(data->entries)) {
221339213Sgibbs				printf("scsi_cd: error in readtocentries, "
221439213Sgibbs				       "returning EINVAL\n");
221539213Sgibbs				error = EINVAL;
2216169562Sscottl				free(data, M_SCSICD);
2217169562Sscottl				free(lead, M_SCSICD);
2218168752Sscottl				cam_periph_unlock(periph);
221939213Sgibbs				break;
222039213Sgibbs			}
222139213Sgibbs			num = len / sizeof(struct cd_toc_entry);
222239213Sgibbs
222339213Sgibbs			if (readlen > 0) {
222439213Sgibbs				error = cdreadtoc(periph, te->address_format,
222539213Sgibbs						  starting_track,
2226111206Sken						  (u_int8_t *)data,
2227111206Sken						  readlen + sizeof (*th),
2228111206Sken						  /*sense_flags*/0);
222939213Sgibbs				if (error) {
2230169562Sscottl					free(data, M_SCSICD);
2231169562Sscottl					free(lead, M_SCSICD);
2232168752Sscottl					cam_periph_unlock(periph);
223339213Sgibbs					break;
223439213Sgibbs				}
223539213Sgibbs			}
223639213Sgibbs
223739213Sgibbs			/* make leadout entry if needed */
223839213Sgibbs			idx = starting_track + num - 1;
223939213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS)
224039213Sgibbs				th->ending_track = bcd2bin(th->ending_track);
224139213Sgibbs			if (idx == th->ending_track + 1) {
224239213Sgibbs				error = cdreadtoc(periph, te->address_format,
2243111206Sken						  LEADOUT, (u_int8_t *)lead,
2244111206Sken						  sizeof(*lead),
2245111206Sken						  /*sense_flags*/0);
224639213Sgibbs				if (error) {
2247169562Sscottl					free(data, M_SCSICD);
2248169562Sscottl					free(lead, M_SCSICD);
2249168752Sscottl					cam_periph_unlock(periph);
225039213Sgibbs					break;
225139213Sgibbs				}
225239213Sgibbs				data->entries[idx - starting_track] =
225339213Sgibbs					lead->entry;
225439213Sgibbs			}
225539213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS) {
225639213Sgibbs				for (idx = 0; idx < num - 1; idx++) {
225739213Sgibbs					data->entries[idx].track =
225839213Sgibbs					    bcd2bin(data->entries[idx].track);
225939213Sgibbs				}
226039213Sgibbs			}
226139213Sgibbs
2262168752Sscottl			cam_periph_unlock(periph);
226339213Sgibbs			error = copyout(data->entries, te->data, len);
2264169562Sscottl			free(data, M_SCSICD);
2265169562Sscottl			free(lead, M_SCSICD);
226639213Sgibbs		}
226739213Sgibbs		break;
226839213Sgibbs	case CDIOREADTOCENTRY:
226939213Sgibbs		{
2270111206Sken			struct cd_toc_single *data;
227139213Sgibbs			struct ioc_read_toc_single_entry *te =
227239213Sgibbs				(struct ioc_read_toc_single_entry *) addr;
227339213Sgibbs			struct ioc_toc_header *th;
227439213Sgibbs			u_int32_t track;
227539213Sgibbs
2276169562Sscottl			data = malloc(sizeof(*data), M_SCSICD, M_WAITOK);
2277168752Sscottl
2278168752Sscottl			cam_periph_lock(periph);
227939213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
228039213Sgibbs				  ("trying to do CDIOREADTOCENTRY\n"));
228139213Sgibbs
228239213Sgibbs			if (te->address_format != CD_MSF_FORMAT
228339213Sgibbs			    && te->address_format != CD_LBA_FORMAT) {
228439213Sgibbs				printf("error in readtocentry, "
228539213Sgibbs				       " returning EINVAL\n");
2286169562Sscottl				free(data, M_SCSICD);
228739213Sgibbs				error = EINVAL;
2288168752Sscottl				cam_periph_unlock(periph);
228939213Sgibbs				break;
229039213Sgibbs			}
229139213Sgibbs
229239213Sgibbs			th = &data->header;
2293111206Sken			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2294111206Sken					  sizeof (*th), /*sense_flags*/0);
229539213Sgibbs			if (error) {
2296169562Sscottl				free(data, M_SCSICD);
2297168752Sscottl				cam_periph_unlock(periph);
229839213Sgibbs				break;
229939213Sgibbs			}
230039213Sgibbs
230139213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS) {
230239213Sgibbs				/* we are going to have to convert the BCD
230339213Sgibbs				 * encoding on the cd to what is expected
230439213Sgibbs				 */
230539213Sgibbs				th->starting_track =
230639213Sgibbs				    bcd2bin(th->starting_track);
230739213Sgibbs				th->ending_track = bcd2bin(th->ending_track);
230839213Sgibbs			}
230939213Sgibbs			track = te->track;
231039213Sgibbs			if (track == 0)
231139213Sgibbs				track = th->starting_track;
231239213Sgibbs			else if (track == LEADOUT)
231339213Sgibbs				/* OK */;
231439213Sgibbs			else if (track < th->starting_track ||
231539213Sgibbs				 track > th->ending_track + 1) {
231639213Sgibbs				printf("error in readtocentry, "
231739213Sgibbs				       " returning EINVAL\n");
2318169562Sscottl				free(data, M_SCSICD);
231939213Sgibbs				error = EINVAL;
2320168752Sscottl				cam_periph_unlock(periph);
232139213Sgibbs				break;
232239213Sgibbs			}
232339213Sgibbs
232439213Sgibbs			error = cdreadtoc(periph, te->address_format, track,
2325111206Sken					  (u_int8_t *)data, sizeof(*data),
2326111206Sken					  /*sense_flags*/0);
232739213Sgibbs			if (error) {
2328169562Sscottl				free(data, M_SCSICD);
2329168752Sscottl				cam_periph_unlock(periph);
233039213Sgibbs				break;
233139213Sgibbs			}
233239213Sgibbs
233339213Sgibbs			if (softc->quirks & CD_Q_BCD_TRACKS)
233439213Sgibbs				data->entry.track = bcd2bin(data->entry.track);
233539213Sgibbs			bcopy(&data->entry, &te->entry,
233639213Sgibbs			      sizeof(struct cd_toc_entry));
2337169562Sscottl			free(data, M_SCSICD);
2338168752Sscottl			cam_periph_unlock(periph);
233939213Sgibbs		}
234039213Sgibbs		break;
234139213Sgibbs	case CDIOCSETPATCH:
234239213Sgibbs		{
2343111206Sken			struct ioc_patch *arg = (struct ioc_patch *)addr;
2344111206Sken			struct cd_mode_params params;
2345111206Sken			union cd_pages *page;
234639213Sgibbs
2347168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2348169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2349168752Sscottl						 M_WAITOK | M_ZERO);
2350168752Sscottl
2351168752Sscottl			cam_periph_lock(periph);
235239213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
235339213Sgibbs				  ("trying to do CDIOCSETPATCH\n"));
235439213Sgibbs
2355111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
235639213Sgibbs			if (error) {
2357169562Sscottl				free(params.mode_buf, M_SCSICD);
2358168752Sscottl				cam_periph_unlock(periph);
235939213Sgibbs				break;
236039213Sgibbs			}
2361111206Sken			page = cdgetpage(&params);
2362111206Sken
2363111206Sken			page->audio.port[LEFT_PORT].channels =
236439213Sgibbs				arg->patch[0];
2365111206Sken			page->audio.port[RIGHT_PORT].channels =
236639213Sgibbs				arg->patch[1];
2367111206Sken			page->audio.port[2].channels = arg->patch[2];
2368111206Sken			page->audio.port[3].channels = arg->patch[3];
2369111206Sken			error = cdsetmode(periph, &params);
2370169562Sscottl			free(params.mode_buf, M_SCSICD);
2371168752Sscottl			cam_periph_unlock(periph);
237239213Sgibbs		}
237339213Sgibbs		break;
237439213Sgibbs	case CDIOCGETVOL:
237539213Sgibbs		{
237639213Sgibbs			struct ioc_vol *arg = (struct ioc_vol *) addr;
2377111206Sken			struct cd_mode_params params;
2378111206Sken			union cd_pages *page;
237939213Sgibbs
2380168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2381169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2382168752Sscottl						 M_WAITOK | M_ZERO);
2383168752Sscottl
2384168752Sscottl			cam_periph_lock(periph);
238539213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
238639213Sgibbs				  ("trying to do CDIOCGETVOL\n"));
238739213Sgibbs
2388111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
238939213Sgibbs			if (error) {
2390169562Sscottl				free(params.mode_buf, M_SCSICD);
2391168752Sscottl				cam_periph_unlock(periph);
239239213Sgibbs				break;
239339213Sgibbs			}
2394111206Sken			page = cdgetpage(&params);
2395111206Sken
239639213Sgibbs			arg->vol[LEFT_PORT] =
2397111206Sken				page->audio.port[LEFT_PORT].volume;
239839213Sgibbs			arg->vol[RIGHT_PORT] =
2399111206Sken				page->audio.port[RIGHT_PORT].volume;
2400111206Sken			arg->vol[2] = page->audio.port[2].volume;
2401111206Sken			arg->vol[3] = page->audio.port[3].volume;
2402169562Sscottl			free(params.mode_buf, M_SCSICD);
2403168752Sscottl			cam_periph_unlock(periph);
240439213Sgibbs		}
240539213Sgibbs		break;
240639213Sgibbs	case CDIOCSETVOL:
240739213Sgibbs		{
240839213Sgibbs			struct ioc_vol *arg = (struct ioc_vol *) addr;
2409111206Sken			struct cd_mode_params params;
2410111206Sken			union cd_pages *page;
241139213Sgibbs
2412168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2413169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2414168752Sscottl						 M_WAITOK | M_ZERO);
2415168752Sscottl
2416168752Sscottl			cam_periph_lock(periph);
241739213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
241839213Sgibbs				  ("trying to do CDIOCSETVOL\n"));
241939213Sgibbs
2420111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
242139213Sgibbs			if (error) {
2422169562Sscottl				free(params.mode_buf, M_SCSICD);
2423168752Sscottl				cam_periph_unlock(periph);
242439213Sgibbs				break;
242539213Sgibbs			}
2426111206Sken			page = cdgetpage(&params);
2427111206Sken
2428111206Sken			page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2429111206Sken			page->audio.port[LEFT_PORT].volume =
243039213Sgibbs				arg->vol[LEFT_PORT];
2431111206Sken			page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2432111206Sken			page->audio.port[RIGHT_PORT].volume =
243339213Sgibbs				arg->vol[RIGHT_PORT];
2434111206Sken			page->audio.port[2].volume = arg->vol[2];
2435111206Sken			page->audio.port[3].volume = arg->vol[3];
2436111206Sken			error = cdsetmode(periph, &params);
2437168752Sscottl			cam_periph_unlock(periph);
2438169562Sscottl			free(params.mode_buf, M_SCSICD);
243939213Sgibbs		}
244039213Sgibbs		break;
244139213Sgibbs	case CDIOCSETMONO:
244239213Sgibbs		{
2443111206Sken			struct cd_mode_params params;
2444111206Sken			union cd_pages *page;
244539213Sgibbs
2446168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2447169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2448168752Sscottl						 M_WAITOK | M_ZERO);
2449168752Sscottl
2450168752Sscottl			cam_periph_lock(periph);
245139213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
245239213Sgibbs				  ("trying to do CDIOCSETMONO\n"));
245339213Sgibbs
2454111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
245539213Sgibbs			if (error) {
2456169562Sscottl				free(params.mode_buf, M_SCSICD);
2457168752Sscottl				cam_periph_unlock(periph);
245839213Sgibbs				break;
245939213Sgibbs			}
2460111206Sken			page = cdgetpage(&params);
2461111206Sken
2462111206Sken			page->audio.port[LEFT_PORT].channels =
246339213Sgibbs				LEFT_CHANNEL | RIGHT_CHANNEL;
2464111206Sken			page->audio.port[RIGHT_PORT].channels =
246539213Sgibbs				LEFT_CHANNEL | RIGHT_CHANNEL;
2466111206Sken			page->audio.port[2].channels = 0;
2467111206Sken			page->audio.port[3].channels = 0;
2468111206Sken			error = cdsetmode(periph, &params);
2469168752Sscottl			cam_periph_unlock(periph);
2470169562Sscottl			free(params.mode_buf, M_SCSICD);
247139213Sgibbs		}
247239213Sgibbs		break;
247339213Sgibbs	case CDIOCSETSTEREO:
247439213Sgibbs		{
2475111206Sken			struct cd_mode_params params;
2476111206Sken			union cd_pages *page;
247739213Sgibbs
2478168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2479169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2480168752Sscottl						 M_WAITOK | M_ZERO);
2481168752Sscottl
2482168752Sscottl			cam_periph_lock(periph);
248339213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
248439213Sgibbs				  ("trying to do CDIOCSETSTEREO\n"));
248539213Sgibbs
2486111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
248739213Sgibbs			if (error) {
2488169562Sscottl				free(params.mode_buf, M_SCSICD);
2489168752Sscottl				cam_periph_unlock(periph);
249039213Sgibbs				break;
249139213Sgibbs			}
2492111206Sken			page = cdgetpage(&params);
2493111206Sken
2494111206Sken			page->audio.port[LEFT_PORT].channels =
249539213Sgibbs				LEFT_CHANNEL;
2496111206Sken			page->audio.port[RIGHT_PORT].channels =
249739213Sgibbs				RIGHT_CHANNEL;
2498111206Sken			page->audio.port[2].channels = 0;
2499111206Sken			page->audio.port[3].channels = 0;
2500111206Sken			error = cdsetmode(periph, &params);
2501169562Sscottl			free(params.mode_buf, M_SCSICD);
2502168752Sscottl			cam_periph_unlock(periph);
250339213Sgibbs		}
250439213Sgibbs		break;
250539213Sgibbs	case CDIOCSETMUTE:
250639213Sgibbs		{
2507111206Sken			struct cd_mode_params params;
2508111206Sken			union cd_pages *page;
250939213Sgibbs
2510168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2511169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2512168752Sscottl						 M_WAITOK | M_ZERO);
2513168752Sscottl
2514168752Sscottl			cam_periph_lock(periph);
251539213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
251639213Sgibbs				  ("trying to do CDIOCSETMUTE\n"));
251739213Sgibbs
2518111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
251939213Sgibbs			if (error) {
2520169562Sscottl				free(&params, M_SCSICD);
2521168752Sscottl				cam_periph_unlock(periph);
252239213Sgibbs				break;
252339213Sgibbs			}
2524111206Sken			page = cdgetpage(&params);
2525111206Sken
2526111206Sken			page->audio.port[LEFT_PORT].channels = 0;
2527111206Sken			page->audio.port[RIGHT_PORT].channels = 0;
2528111206Sken			page->audio.port[2].channels = 0;
2529111206Sken			page->audio.port[3].channels = 0;
2530111206Sken			error = cdsetmode(periph, &params);
2531169562Sscottl			free(params.mode_buf, M_SCSICD);
2532168752Sscottl			cam_periph_unlock(periph);
253339213Sgibbs		}
253439213Sgibbs		break;
253539213Sgibbs	case CDIOCSETLEFT:
253639213Sgibbs		{
2537111206Sken			struct cd_mode_params params;
2538111206Sken			union cd_pages *page;
253939213Sgibbs
2540168752Sscottl			params.alloc_len = sizeof(union cd_mode_data_6_10);
2541169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2542168752Sscottl						 M_WAITOK | M_ZERO);
2543168752Sscottl
2544168752Sscottl			cam_periph_lock(periph);
254539213Sgibbs			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
254639213Sgibbs				  ("trying to do CDIOCSETLEFT\n"));
254739213Sgibbs
2548111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
254939213Sgibbs			if (error) {
2550169562Sscottl				free(params.mode_buf, M_SCSICD);
2551168752Sscottl				cam_periph_unlock(periph);
255239213Sgibbs				break;
255339213Sgibbs			}
2554111206Sken			page = cdgetpage(&params);
2555111206Sken
2556111206Sken			page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2557111206Sken			page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2558111206Sken			page->audio.port[2].channels = 0;
2559111206Sken			page->audio.port[3].channels = 0;
2560111206Sken			error = cdsetmode(periph, &params);
2561169562Sscottl			free(params.mode_buf, M_SCSICD);
2562168752Sscottl			cam_periph_unlock(periph);
256339213Sgibbs		}
256439213Sgibbs		break;
256539213Sgibbs	case CDIOCSETRIGHT:
256639213Sgibbs		{
2567111206Sken			struct cd_mode_params params;
2568111206Sken			union cd_pages *page;
256939213Sgibbs
2570111206Sken			params.alloc_len = sizeof(union cd_mode_data_6_10);
2571169562Sscottl			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2572111206Sken						 M_WAITOK | M_ZERO);
2573111206Sken
2574168752Sscottl			cam_periph_lock(periph);
2575168752Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2576168752Sscottl				  ("trying to do CDIOCSETRIGHT\n"));
2577168752Sscottl
2578111206Sken			error = cdgetmode(periph, &params, AUDIO_PAGE);
257939213Sgibbs			if (error) {
2580169562Sscottl				free(params.mode_buf, M_SCSICD);
2581168752Sscottl				cam_periph_unlock(periph);
258239213Sgibbs				break;
258339213Sgibbs			}
2584111206Sken			page = cdgetpage(&params);
2585111206Sken
2586111206Sken			page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2587111206Sken			page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2588111206Sken			page->audio.port[2].channels = 0;
2589111206Sken			page->audio.port[3].channels = 0;
2590111206Sken			error = cdsetmode(periph, &params);
2591169562Sscottl			free(params.mode_buf, M_SCSICD);
2592168752Sscottl			cam_periph_unlock(periph);
259339213Sgibbs		}
259439213Sgibbs		break;
259539213Sgibbs	case CDIOCRESUME:
2596168752Sscottl		cam_periph_lock(periph);
259739213Sgibbs		error = cdpause(periph, 1);
2598168752Sscottl		cam_periph_unlock(periph);
259939213Sgibbs		break;
260039213Sgibbs	case CDIOCPAUSE:
2601168752Sscottl		cam_periph_lock(periph);
260239213Sgibbs		error = cdpause(periph, 0);
2603168752Sscottl		cam_periph_unlock(periph);
260439213Sgibbs		break;
260539213Sgibbs	case CDIOCSTART:
2606168752Sscottl		cam_periph_lock(periph);
2607111206Sken		error = cdstartunit(periph, 0);
2608168752Sscottl		cam_periph_unlock(periph);
260939213Sgibbs		break;
2610111206Sken	case CDIOCCLOSE:
2611168752Sscottl		cam_periph_lock(periph);
2612111206Sken		error = cdstartunit(periph, 1);
2613168752Sscottl		cam_periph_unlock(periph);
2614111206Sken		break;
261539213Sgibbs	case CDIOCSTOP:
2616168752Sscottl		cam_periph_lock(periph);
261739213Sgibbs		error = cdstopunit(periph, 0);
2618168752Sscottl		cam_periph_unlock(periph);
261939213Sgibbs		break;
262039213Sgibbs	case CDIOCEJECT:
2621168752Sscottl		cam_periph_lock(periph);
262239213Sgibbs		error = cdstopunit(periph, 1);
2623168752Sscottl		cam_periph_unlock(periph);
262439213Sgibbs		break;
262539213Sgibbs	case CDIOCALLOW:
2626168752Sscottl		cam_periph_lock(periph);
262739213Sgibbs		cdprevent(periph, PR_ALLOW);
2628168752Sscottl		cam_periph_unlock(periph);
262939213Sgibbs		break;
263039213Sgibbs	case CDIOCPREVENT:
2631168752Sscottl		cam_periph_lock(periph);
263239213Sgibbs		cdprevent(periph, PR_PREVENT);
2633168752Sscottl		cam_periph_unlock(periph);
263439213Sgibbs		break;
263539213Sgibbs	case CDIOCSETDEBUG:
263639213Sgibbs		/* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
263739213Sgibbs		error = ENOTTY;
263839213Sgibbs		break;
263939213Sgibbs	case CDIOCCLRDEBUG:
264039213Sgibbs		/* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
264139213Sgibbs		error = ENOTTY;
264239213Sgibbs		break;
264339213Sgibbs	case CDIOCRESET:
264439213Sgibbs		/* return (cd_reset(periph)); */
264539213Sgibbs		error = ENOTTY;
264639213Sgibbs		break;
2647105421Snjl	case CDRIOCREADSPEED:
2648168752Sscottl		cam_periph_lock(periph);
2649105421Snjl		error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2650168752Sscottl		cam_periph_unlock(periph);
2651105421Snjl		break;
2652105421Snjl	case CDRIOCWRITESPEED:
2653168752Sscottl		cam_periph_lock(periph);
2654105421Snjl		error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2655168752Sscottl		cam_periph_unlock(periph);
2656105421Snjl		break;
265760422Sken	case DVDIOCSENDKEY:
265860422Sken	case DVDIOCREPORTKEY: {
265960422Sken		struct dvd_authinfo *authinfo;
266060422Sken
266160422Sken		authinfo = (struct dvd_authinfo *)addr;
266260422Sken
2663168752Sscottl		cam_periph_lock(periph);
266460422Sken		if (cmd == DVDIOCREPORTKEY)
266560422Sken			error = cdreportkey(periph, authinfo);
266660422Sken		else
266760422Sken			error = cdsendkey(periph, authinfo);
2668168752Sscottl		cam_periph_unlock(periph);
266960422Sken		break;
2670104456Sphk		}
267160422Sken	case DVDIOCREADSTRUCTURE: {
267260422Sken		struct dvd_struct *dvdstruct;
267360422Sken
267460422Sken		dvdstruct = (struct dvd_struct *)addr;
267560422Sken
2676168752Sscottl		cam_periph_lock(periph);
267760422Sken		error = cdreaddvdstructure(periph, dvdstruct);
2678168752Sscottl		cam_periph_unlock(periph);
267960422Sken
268060422Sken		break;
268160422Sken	}
268239213Sgibbs	default:
2683168752Sscottl		cam_periph_lock(periph);
268439213Sgibbs		error = cam_periph_ioctl(periph, cmd, addr, cderror);
2685168752Sscottl		cam_periph_unlock(periph);
268639213Sgibbs		break;
268739213Sgibbs	}
268839213Sgibbs
2689168752Sscottl	cam_periph_lock(periph);
2690168752Sscottl	cam_periph_unhold(periph);
2691168752Sscottl
269239213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2693104456Sphk	if (error && bootverbose) {
2694104456Sphk		printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2695104456Sphk	}
2696168752Sscottl	cam_periph_unlock(periph);
269739213Sgibbs
269839213Sgibbs	return (error);
269939213Sgibbs}
270039213Sgibbs
270139213Sgibbsstatic void
270239213Sgibbscdprevent(struct cam_periph *periph, int action)
270339213Sgibbs{
270439213Sgibbs	union	ccb *ccb;
270539213Sgibbs	struct	cd_softc *softc;
270639213Sgibbs	int	error;
270739213Sgibbs
270839213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
270939213Sgibbs
271039213Sgibbs	softc = (struct cd_softc *)periph->softc;
271139213Sgibbs
271239213Sgibbs	if (((action == PR_ALLOW)
271339213Sgibbs	  && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
271439213Sgibbs	 || ((action == PR_PREVENT)
271539213Sgibbs	  && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
271639213Sgibbs		return;
271739213Sgibbs	}
271839213Sgibbs
271939213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
272039213Sgibbs
272139213Sgibbs	scsi_prevent(&ccb->csio,
272239213Sgibbs		     /*retries*/ 1,
272339213Sgibbs		     cddone,
272439213Sgibbs		     MSG_SIMPLE_Q_TAG,
272539213Sgibbs		     action,
272639213Sgibbs		     SSD_FULL_SIZE,
272739213Sgibbs		     /* timeout */60000);
272839213Sgibbs
272974840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
273074840Sken			/*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
273139213Sgibbs
273239213Sgibbs	xpt_release_ccb(ccb);
273339213Sgibbs
273439213Sgibbs	if (error == 0) {
273539213Sgibbs		if (action == PR_ALLOW)
273639213Sgibbs			softc->flags &= ~CD_FLAG_DISC_LOCKED;
273739213Sgibbs		else
273839213Sgibbs			softc->flags |= CD_FLAG_DISC_LOCKED;
273939213Sgibbs	}
274039213Sgibbs}
274139213Sgibbs
2742120599Sphk/*
2743120599Sphk * XXX: the disk media and sector size is only really able to change
2744120599Sphk * XXX: while the device is closed.
2745120599Sphk */
274639213Sgibbsstatic int
2747111206Skencdcheckmedia(struct cam_periph *periph)
274839213Sgibbs{
274939213Sgibbs	struct cd_softc *softc;
2750111206Sken	struct ioc_toc_header *toch;
2751111206Sken	struct cd_toc_single leadout;
2752111206Sken	u_int32_t size, toclen;
2753111206Sken	int error, num_entries, cdindex;
2754111206Sken
2755111206Sken	softc = (struct cd_softc *)periph->softc;
2756111206Sken
2757111206Sken	cdprevent(periph, PR_PREVENT);
2758125975Sphk	softc->disk->d_maxsize = DFLTPHYS;
2759134824Sphk	softc->disk->d_sectorsize = 2048;
2760125975Sphk	softc->disk->d_mediasize = 0;
2761111206Sken
2762111206Sken	/*
2763111206Sken	 * Get the disc size and block size.  If we can't get it, we don't
2764111206Sken	 * have media, most likely.
2765111206Sken	 */
2766111206Sken	if ((error = cdsize(periph, &size)) != 0) {
2767111206Sken		softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2768111206Sken		cdprevent(periph, PR_ALLOW);
2769111206Sken		return (error);
2770111206Sken	} else
2771111206Sken		softc->flags |= CD_FLAG_VALID_MEDIA;
2772111206Sken
2773111206Sken	/*
2774111206Sken	 * Now we check the table of contents.  This (currently) is only
2775111206Sken	 * used for the CDIOCPLAYTRACKS ioctl.  It may be used later to do
2776111206Sken	 * things like present a separate entry in /dev for each track,
2777111206Sken	 * like that acd(4) driver does.
2778111206Sken	 */
2779111206Sken	bzero(&softc->toc, sizeof(softc->toc));
2780111206Sken	toch = &softc->toc.header;
2781111206Sken	/*
2782111206Sken	 * We will get errors here for media that doesn't have a table of
2783111206Sken	 * contents.  According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2784111206Sken	 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2785111206Sken	 * has not been recorded (no complete session) and the Format codes
2786111206Sken	 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2787111206Sken	 * with an INVALID FIELD IN CDB.  Devices that are not capable of
2788111206Sken	 * reading an incomplete session on DDC/CD-R/RW media shall report
2789111206Sken	 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2790111206Sken	 *
2791111206Sken	 * So this isn't fatal if we can't read the table of contents, it
2792111206Sken	 * just means that the user won't be able to issue the play tracks
2793111206Sken	 * ioctl, and likely lots of other stuff won't work either.  They
2794111206Sken	 * need to burn the CD before we can do a whole lot with it.  So
2795111206Sken	 * we don't print anything here if we get an error back.
2796111206Sken	 */
2797111206Sken	error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2798111206Sken			  SF_NO_PRINT);
2799111206Sken	/*
2800111206Sken	 * Errors in reading the table of contents aren't fatal, we just
2801111206Sken	 * won't have a valid table of contents cached.
2802111206Sken	 */
2803111206Sken	if (error != 0) {
2804111206Sken		error = 0;
2805111206Sken		bzero(&softc->toc, sizeof(softc->toc));
2806111206Sken		goto bailout;
2807111206Sken	}
2808111206Sken
2809111206Sken	if (softc->quirks & CD_Q_BCD_TRACKS) {
2810111206Sken		toch->starting_track = bcd2bin(toch->starting_track);
2811111206Sken		toch->ending_track = bcd2bin(toch->ending_track);
2812111206Sken	}
2813111206Sken
2814111206Sken	/* Number of TOC entries, plus leadout */
2815111206Sken	num_entries = (toch->ending_track - toch->starting_track) + 2;
2816111206Sken
2817111206Sken	if (num_entries <= 0)
2818111206Sken		goto bailout;
2819111206Sken
2820111206Sken	toclen = num_entries * sizeof(struct cd_toc_entry);
2821111206Sken
2822111206Sken	error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2823111206Sken			  (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2824111206Sken			  SF_NO_PRINT);
2825111206Sken	if (error != 0) {
2826111206Sken		error = 0;
2827111206Sken		bzero(&softc->toc, sizeof(softc->toc));
2828111206Sken		goto bailout;
2829111206Sken	}
2830111206Sken
2831111206Sken	if (softc->quirks & CD_Q_BCD_TRACKS) {
2832111206Sken		toch->starting_track = bcd2bin(toch->starting_track);
2833111206Sken		toch->ending_track = bcd2bin(toch->ending_track);
2834111206Sken	}
2835111206Sken	/*
2836111206Sken	 * XXX KDM is this necessary?  Probably only if the drive doesn't
2837111206Sken	 * return leadout information with the table of contents.
2838111206Sken	 */
2839111206Sken	cdindex = toch->starting_track + num_entries -1;
2840111206Sken	if (cdindex == toch->ending_track + 1) {
2841111206Sken
2842111206Sken		error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2843111206Sken				  (u_int8_t *)&leadout, sizeof(leadout),
2844111206Sken				  SF_NO_PRINT);
2845111206Sken		if (error != 0) {
2846111206Sken			error = 0;
2847111206Sken			goto bailout;
2848111206Sken		}
2849111206Sken		softc->toc.entries[cdindex - toch->starting_track] =
2850111206Sken			leadout.entry;
2851111206Sken	}
2852111206Sken	if (softc->quirks & CD_Q_BCD_TRACKS) {
2853111206Sken		for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
2854111206Sken			softc->toc.entries[cdindex].track =
2855111206Sken				bcd2bin(softc->toc.entries[cdindex].track);
2856111206Sken		}
2857111206Sken	}
2858111206Sken
2859111206Sken	softc->flags |= CD_FLAG_VALID_TOC;
2860125975Sphk	softc->disk->d_maxsize = DFLTPHYS;
2861125975Sphk	softc->disk->d_sectorsize = softc->params.blksize;
2862125975Sphk	softc->disk->d_mediasize =
2863120599Sphk	    (off_t)softc->params.blksize * softc->params.disksize;
2864111206Sken
2865111206Skenbailout:
2866111206Sken
2867111206Sken	/*
2868111206Sken	 * We unconditionally (re)set the blocksize each time the
2869111206Sken	 * CD device is opened.  This is because the CD can change,
2870111206Sken	 * and therefore the blocksize might change.
2871111206Sken	 * XXX problems here if some slice or partition is still
2872111206Sken	 * open with the old size?
2873111206Sken	 */
2874125975Sphk	if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2875125975Sphk		softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2876125975Sphk	softc->disk->d_devstat->block_size = softc->params.blksize;
2877111206Sken
2878111206Sken	return (error);
2879111206Sken}
2880111206Sken
2881111206Skenstatic int
2882111206Skencdsize(struct cam_periph *periph, u_int32_t *size)
2883111206Sken{
2884111206Sken	struct cd_softc *softc;
288539213Sgibbs	union ccb *ccb;
288639213Sgibbs	struct scsi_read_capacity_data *rcap_buf;
288739213Sgibbs	int error;
288839213Sgibbs
288939213Sgibbs	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
289039213Sgibbs
289139213Sgibbs	softc = (struct cd_softc *)periph->softc;
289239213Sgibbs
289339213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
289439213Sgibbs
2895168752Sscottl	/* XXX Should be M_WAITOK */
289639213Sgibbs	rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2897169562Sscottl			  M_SCSICD, M_NOWAIT);
2898168752Sscottl	if (rcap_buf == NULL)
2899168752Sscottl		return (ENOMEM);
290039213Sgibbs
290139213Sgibbs	scsi_read_capacity(&ccb->csio,
290239213Sgibbs			   /*retries*/ 1,
290339213Sgibbs			   cddone,
290439213Sgibbs			   MSG_SIMPLE_Q_TAG,
290539213Sgibbs			   rcap_buf,
290639213Sgibbs			   SSD_FULL_SIZE,
290739213Sgibbs			   /* timeout */20000);
290839213Sgibbs
290974840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
291074840Sken			 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
291139213Sgibbs
291239213Sgibbs	xpt_release_ccb(ccb);
291339213Sgibbs
291439213Sgibbs	softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
291539213Sgibbs	softc->params.blksize  = scsi_4btoul(rcap_buf->length);
291660806Sjoerg	/*
291760806Sjoerg	 * SCSI-3 mandates that the reported blocksize shall be 2048.
291860806Sjoerg	 * Older drives sometimes report funny values, trim it down to
291960806Sjoerg	 * 2048, or other parts of the kernel will get confused.
292060806Sjoerg	 *
292160806Sjoerg	 * XXX we leave drives alone that might report 512 bytes, as
292260806Sjoerg	 * well as drives reporting more weird sizes like perhaps 4K.
292360806Sjoerg	 */
292460806Sjoerg	if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
292560806Sjoerg		softc->params.blksize = 2048;
292639213Sgibbs
2927169562Sscottl	free(rcap_buf, M_SCSICD);
292839213Sgibbs	*size = softc->params.disksize;
292939213Sgibbs
293039213Sgibbs	return (error);
293139213Sgibbs
293239213Sgibbs}
293339213Sgibbs
293439213Sgibbsstatic int
2935111206Skencd6byteworkaround(union ccb *ccb)
2936111206Sken{
2937111206Sken	u_int8_t *cdb;
2938111206Sken	struct cam_periph *periph;
2939111206Sken	struct cd_softc *softc;
2940111206Sken	struct cd_mode_params *params;
2941111206Sken	int frozen, found;
2942111206Sken
2943111206Sken	periph = xpt_path_periph(ccb->ccb_h.path);
2944111206Sken	softc = (struct cd_softc *)periph->softc;
2945111206Sken
2946111206Sken	cdb = ccb->csio.cdb_io.cdb_bytes;
2947111206Sken
2948111206Sken	if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
2949111206Sken	 || ((cdb[0] != MODE_SENSE_6)
2950111206Sken	  && (cdb[0] != MODE_SELECT_6)))
2951111206Sken		return (0);
2952111206Sken
2953111206Sken	/*
2954111206Sken	 * Because there is no convenient place to stash the overall
2955111206Sken	 * cd_mode_params structure pointer, we have to grab it like this.
2956111206Sken	 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
2957111206Sken	 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
2958111206Sken	 *
2959111206Sken	 * XXX It would be nice if, at some point, we could increase the
2960111206Sken	 * number of available peripheral private pointers.  Both pointers
2961111206Sken	 * are currently used in most every peripheral driver.
2962111206Sken	 */
2963111206Sken	found = 0;
2964111206Sken
2965111206Sken	STAILQ_FOREACH(params, &softc->mode_queue, links) {
2966111206Sken		if (params->mode_buf == ccb->csio.data_ptr) {
2967111206Sken			found = 1;
2968111206Sken			break;
2969111206Sken		}
2970111206Sken	}
2971111206Sken
2972111206Sken	/*
2973111206Sken	 * This shouldn't happen.  All mode sense and mode select
2974111206Sken	 * operations in the cd(4) driver MUST go through cdgetmode() and
2975111206Sken	 * cdsetmode()!
2976111206Sken	 */
2977111206Sken	if (found == 0) {
2978164906Smjacob		xpt_print(periph->path,
2979164906Smjacob		    "mode buffer not found in mode queue!\n");
2980111206Sken		return (0);
2981111206Sken	}
2982111206Sken
2983111206Sken	params->cdb_size = 10;
2984111206Sken	softc->minimum_command_size = 10;
2985164906Smjacob	xpt_print(ccb->ccb_h.path,
2986164906Smjacob	    "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
2987164906Smjacob	    (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
2988111206Sken
2989111206Sken	if (cdb[0] == MODE_SENSE_6) {
2990111206Sken		struct scsi_mode_sense_10 ms10;
2991111206Sken		struct scsi_mode_sense_6 *ms6;
2992111206Sken		int len;
2993111206Sken
2994111206Sken		ms6 = (struct scsi_mode_sense_6 *)cdb;
2995111206Sken
2996111206Sken		bzero(&ms10, sizeof(ms10));
2997111206Sken 		ms10.opcode = MODE_SENSE_10;
2998111206Sken 		ms10.byte2 = ms6->byte2;
2999111206Sken 		ms10.page = ms6->page;
3000111206Sken
3001111206Sken		/*
3002111206Sken		 * 10 byte mode header, block descriptor,
3003111206Sken		 * sizeof(union cd_pages)
3004111206Sken		 */
3005111206Sken		len = sizeof(struct cd_mode_data_10);
3006111206Sken		ccb->csio.dxfer_len = len;
3007111206Sken
3008111206Sken		scsi_ulto2b(len, ms10.length);
3009111206Sken		ms10.control = ms6->control;
3010111206Sken		bcopy(&ms10, cdb, 10);
3011111206Sken		ccb->csio.cdb_len = 10;
3012111206Sken	} else {
3013111206Sken		struct scsi_mode_select_10 ms10;
3014111206Sken		struct scsi_mode_select_6 *ms6;
3015111206Sken		struct scsi_mode_header_6 *header6;
3016111206Sken		struct scsi_mode_header_10 *header10;
3017111206Sken		struct scsi_mode_page_header *page_header;
3018111206Sken		int blk_desc_len, page_num, page_size, len;
3019111206Sken
3020111206Sken		ms6 = (struct scsi_mode_select_6 *)cdb;
3021111206Sken
3022111206Sken		bzero(&ms10, sizeof(ms10));
3023111206Sken		ms10.opcode = MODE_SELECT_10;
3024111206Sken		ms10.byte2 = ms6->byte2;
3025111206Sken
3026111206Sken		header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3027111206Sken		header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3028111206Sken
3029111206Sken		page_header = find_mode_page_6(header6);
3030111206Sken		page_num = page_header->page_code;
3031111206Sken
3032111206Sken		blk_desc_len = header6->blk_desc_len;
3033111206Sken
3034111206Sken		page_size = cdgetpagesize(page_num);
3035111206Sken
3036111206Sken		if (page_size != (page_header->page_length +
3037111206Sken		    sizeof(*page_header)))
3038111206Sken			page_size = page_header->page_length +
3039111206Sken				sizeof(*page_header);
3040111206Sken
3041111206Sken		len = sizeof(*header10) + blk_desc_len + page_size;
3042111206Sken
3043111206Sken		len = min(params->alloc_len, len);
3044111206Sken
3045111206Sken		/*
3046111206Sken		 * Since the 6 byte parameter header is shorter than the 10
3047111206Sken		 * byte parameter header, we need to copy the actual mode
3048111206Sken		 * page data, and the block descriptor, if any, so things wind
3049111206Sken		 * up in the right place.  The regions will overlap, but
3050111206Sken		 * bcopy() does the right thing.
3051111206Sken		 */
3052111206Sken		bcopy(params->mode_buf + sizeof(*header6),
3053111206Sken		      params->mode_buf + sizeof(*header10),
3054111206Sken		      len - sizeof(*header10));
3055111206Sken
3056111206Sken		/* Make sure these fields are set correctly. */
3057111206Sken		scsi_ulto2b(0, header10->data_length);
3058111206Sken		header10->medium_type = 0;
3059111206Sken		scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3060111206Sken
3061111206Sken		ccb->csio.dxfer_len = len;
3062111206Sken
3063111206Sken		scsi_ulto2b(len, ms10.length);
3064111206Sken		ms10.control = ms6->control;
3065111206Sken		bcopy(&ms10, cdb, 10);
3066111206Sken		ccb->csio.cdb_len = 10;
3067111206Sken	}
3068111206Sken
3069111206Sken	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3070111206Sken	ccb->ccb_h.status = CAM_REQUEUE_REQ;
3071111206Sken	xpt_action(ccb);
3072111206Sken	if (frozen) {
3073111206Sken		cam_release_devq(ccb->ccb_h.path,
3074111206Sken				 /*relsim_flags*/0,
3075111206Sken				 /*openings*/0,
3076111206Sken				 /*timeout*/0,
3077111206Sken				 /*getcount_only*/0);
3078111206Sken	}
3079111206Sken
3080111206Sken	return (ERESTART);
3081111206Sken}
3082111206Sken
3083111206Skenstatic int
308439213Sgibbscderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
308539213Sgibbs{
308639213Sgibbs	struct cd_softc *softc;
308739213Sgibbs	struct cam_periph *periph;
3088111206Sken	int error;
308939213Sgibbs
309039213Sgibbs	periph = xpt_path_periph(ccb->ccb_h.path);
309139213Sgibbs	softc = (struct cd_softc *)periph->softc;
309239213Sgibbs
3093111206Sken	error = 0;
3094111206Sken
309539514Sgibbs	/*
3096111206Sken	 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3097111206Sken	 * CDB comes back with this particular error, try transforming it
3098111206Sken	 * into the 10 byte version.
3099111206Sken	 */
3100111206Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3101111206Sken		error = cd6byteworkaround(ccb);
3102111206Sken	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3103111206Sken		     CAM_SCSI_STATUS_ERROR)
3104111206Sken	 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3105111206Sken	 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3106111206Sken	 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3107111206Sken	 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3108111206Sken		int sense_key, error_code, asc, ascq;
3109111206Sken
3110111206Sken 		scsi_extract_sense(&ccb->csio.sense_data,
3111111206Sken				   &error_code, &sense_key, &asc, &ascq);
3112111206Sken		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3113111206Sken 			error = cd6byteworkaround(ccb);
3114111206Sken	}
3115111206Sken
3116111206Sken	if (error == ERESTART)
3117111206Sken		return (error);
3118111206Sken
3119111206Sken	/*
312039514Sgibbs	 * XXX
312139514Sgibbs	 * Until we have a better way of doing pack validation,
312239514Sgibbs	 * don't treat UAs as errors.
312339514Sgibbs	 */
312439514Sgibbs	sense_flags |= SF_RETRY_UA;
312539213Sgibbs	return (cam_periph_error(ccb, cam_flags, sense_flags,
312639213Sgibbs				 &softc->saved_ccb));
312739213Sgibbs}
312839213Sgibbs
312939213Sgibbs/*
313039213Sgibbs * Read table of contents
313139213Sgibbs */
313239213Sgibbsstatic int
313339213Sgibbscdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3134111206Sken	  u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
313539213Sgibbs{
313639213Sgibbs	struct scsi_read_toc *scsi_cmd;
313739213Sgibbs	u_int32_t ntoc;
313839213Sgibbs        struct ccb_scsiio *csio;
313939213Sgibbs	union ccb *ccb;
314039213Sgibbs	int error;
314139213Sgibbs
314239213Sgibbs	ntoc = len;
314339213Sgibbs	error = 0;
314439213Sgibbs
314539213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
314639213Sgibbs
314739213Sgibbs	csio = &ccb->csio;
314839213Sgibbs
314939213Sgibbs	cam_fill_csio(csio,
315039213Sgibbs		      /* retries */ 1,
315139213Sgibbs		      /* cbfcnp */ cddone,
315239213Sgibbs		      /* flags */ CAM_DIR_IN,
315339213Sgibbs		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3154111206Sken		      /* data_ptr */ data,
315539213Sgibbs		      /* dxfer_len */ len,
315639213Sgibbs		      /* sense_len */ SSD_FULL_SIZE,
315739213Sgibbs		      sizeof(struct scsi_read_toc),
315839213Sgibbs 		      /* timeout */ 50000);
315939213Sgibbs
316039213Sgibbs	scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
316139213Sgibbs	bzero (scsi_cmd, sizeof(*scsi_cmd));
316239213Sgibbs
316339213Sgibbs	if (mode == CD_MSF_FORMAT)
316439213Sgibbs		scsi_cmd->byte2 |= CD_MSF;
316539213Sgibbs	scsi_cmd->from_track = start;
316639213Sgibbs	/* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
316739213Sgibbs	scsi_cmd->data_len[0] = (ntoc) >> 8;
316839213Sgibbs	scsi_cmd->data_len[1] = (ntoc) & 0xff;
316939213Sgibbs
317039213Sgibbs	scsi_cmd->op_code = READ_TOC;
317139213Sgibbs
317274840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3173111206Sken			 /*sense_flags*/SF_RETRY_UA | sense_flags);
317439213Sgibbs
317539213Sgibbs	xpt_release_ccb(ccb);
317639213Sgibbs
317739213Sgibbs	return(error);
317839213Sgibbs}
317939213Sgibbs
318039213Sgibbsstatic int
318139213Sgibbscdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
318239213Sgibbs		 u_int32_t format, int track,
318339213Sgibbs		 struct cd_sub_channel_info *data, u_int32_t len)
318439213Sgibbs{
318539213Sgibbs	struct scsi_read_subchannel *scsi_cmd;
318639213Sgibbs        struct ccb_scsiio *csio;
318739213Sgibbs	union ccb *ccb;
318839213Sgibbs	int error;
318939213Sgibbs
319039213Sgibbs	error = 0;
319139213Sgibbs
319239213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
319339213Sgibbs
319439213Sgibbs	csio = &ccb->csio;
319539213Sgibbs
319639213Sgibbs	cam_fill_csio(csio,
319739213Sgibbs		      /* retries */ 1,
319839213Sgibbs		      /* cbfcnp */ cddone,
319939213Sgibbs		      /* flags */ CAM_DIR_IN,
320039213Sgibbs		      /* tag_action */ MSG_SIMPLE_Q_TAG,
320139213Sgibbs		      /* data_ptr */ (u_int8_t *)data,
320239213Sgibbs		      /* dxfer_len */ len,
320339213Sgibbs		      /* sense_len */ SSD_FULL_SIZE,
320439213Sgibbs		      sizeof(struct scsi_read_subchannel),
320539213Sgibbs 		      /* timeout */ 50000);
320639213Sgibbs
320739213Sgibbs	scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
320839213Sgibbs	bzero (scsi_cmd, sizeof(*scsi_cmd));
320939213Sgibbs
321039213Sgibbs	scsi_cmd->op_code = READ_SUBCHANNEL;
321139213Sgibbs	if (mode == CD_MSF_FORMAT)
321239213Sgibbs		scsi_cmd->byte1 |= CD_MSF;
321339213Sgibbs	scsi_cmd->byte2 = SRS_SUBQ;
321439213Sgibbs	scsi_cmd->subchan_format = format;
321539213Sgibbs	scsi_cmd->track = track;
321639213Sgibbs	scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
321739213Sgibbs	scsi_cmd->control = 0;
321839213Sgibbs
321974840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
322074840Sken			 /*sense_flags*/SF_RETRY_UA);
322139213Sgibbs
322239213Sgibbs	xpt_release_ccb(ccb);
322339213Sgibbs
322439213Sgibbs	return(error);
322539213Sgibbs}
322639213Sgibbs
322739213Sgibbs
3228111206Sken/*
3229111206Sken * All MODE_SENSE requests in the cd(4) driver MUST go through this
3230111206Sken * routine.  See comments in cd6byteworkaround() for details.
3231111206Sken */
323239213Sgibbsstatic int
3233111206Skencdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3234111206Sken	  u_int32_t page)
323539213Sgibbs{
3236111206Sken	struct ccb_scsiio *csio;
3237111206Sken	struct cd_softc *softc;
323839213Sgibbs	union ccb *ccb;
3239111206Sken	int param_len;
324039213Sgibbs	int error;
324139213Sgibbs
3242111206Sken	softc = (struct cd_softc *)periph->softc;
3243111206Sken
324439213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
324539213Sgibbs
324639213Sgibbs	csio = &ccb->csio;
324739213Sgibbs
3248111206Sken	data->cdb_size = softc->minimum_command_size;
3249111206Sken	if (data->cdb_size < 10)
3250111206Sken		param_len = sizeof(struct cd_mode_data);
3251111206Sken	else
3252111206Sken		param_len = sizeof(struct cd_mode_data_10);
325339213Sgibbs
3254111206Sken	/* Don't say we've got more room than we actually allocated */
3255111206Sken	param_len = min(param_len, data->alloc_len);
325639213Sgibbs
3257111206Sken	scsi_mode_sense_len(csio,
3258111206Sken			    /* retries */ 1,
3259111206Sken			    /* cbfcnp */ cddone,
3260111206Sken			    /* tag_action */ MSG_SIMPLE_Q_TAG,
3261111206Sken			    /* dbd */ 0,
3262111206Sken			    /* page_code */ SMS_PAGE_CTRL_CURRENT,
3263111206Sken			    /* page */ page,
3264111206Sken			    /* param_buf */ data->mode_buf,
3265111206Sken			    /* param_len */ param_len,
3266111206Sken			    /* minimum_cmd_size */ softc->minimum_command_size,
3267111206Sken			    /* sense_len */ SSD_FULL_SIZE,
3268111206Sken			    /* timeout */ 50000);
326939213Sgibbs
3270111206Sken	/*
3271111206Sken	 * It would be nice not to have to do this, but there's no
3272111206Sken	 * available pointer in the CCB that would allow us to stuff the
3273111206Sken	 * mode params structure in there and retrieve it in
3274111206Sken	 * cd6byteworkaround(), so we can set the cdb size.  The cdb size
3275111206Sken	 * lets the caller know what CDB size we ended up using, so they
3276111206Sken	 * can find the actual mode page offset.
3277111206Sken	 */
3278111206Sken	STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3279111206Sken
328074840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
328174840Sken			 /*sense_flags*/SF_RETRY_UA);
328239213Sgibbs
328339213Sgibbs	xpt_release_ccb(ccb);
328439213Sgibbs
3285111206Sken	STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3286111206Sken
3287111206Sken	/*
3288111206Sken	 * This is a bit of belt-and-suspenders checking, but if we run
3289111206Sken	 * into a situation where the target sends back multiple block
3290111206Sken	 * descriptors, we might not have enough space in the buffer to
3291111206Sken	 * see the whole mode page.  Better to return an error than
3292111206Sken	 * potentially access memory beyond our malloced region.
3293111206Sken	 */
3294111206Sken	if (error == 0) {
3295111206Sken		u_int32_t data_len;
3296111206Sken
3297111206Sken		if (data->cdb_size == 10) {
3298111206Sken			struct scsi_mode_header_10 *hdr10;
3299111206Sken
3300111206Sken			hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3301111206Sken			data_len = scsi_2btoul(hdr10->data_length);
3302111206Sken			data_len += sizeof(hdr10->data_length);
3303111206Sken		} else {
3304111206Sken			struct scsi_mode_header_6 *hdr6;
3305111206Sken
3306111206Sken			hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3307111206Sken			data_len = hdr6->data_length;
3308111206Sken			data_len += sizeof(hdr6->data_length);
3309111206Sken		}
3310111206Sken
3311111206Sken		/*
3312111206Sken		 * Complain if there is more mode data available than we
3313111206Sken		 * allocated space for.  This could potentially happen if
3314111206Sken		 * we miscalculated the page length for some reason, if the
3315111206Sken		 * drive returns multiple block descriptors, or if it sets
3316111206Sken		 * the data length incorrectly.
3317111206Sken		 */
3318111206Sken		if (data_len > data->alloc_len) {
3319164906Smjacob			xpt_print(periph->path, "allocated modepage %d length "
3320164906Smjacob			    "%d < returned length %d\n", page, data->alloc_len,
3321164906Smjacob			    data_len);
3322111206Sken			error = ENOSPC;
3323111206Sken		}
3324111206Sken	}
3325111206Sken	return (error);
332639213Sgibbs}
332739213Sgibbs
3328111206Sken/*
3329111206Sken * All MODE_SELECT requests in the cd(4) driver MUST go through this
3330111206Sken * routine.  See comments in cd6byteworkaround() for details.
3331111206Sken */
333239213Sgibbsstatic int
3333111206Skencdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
333439213Sgibbs{
3335111206Sken	struct ccb_scsiio *csio;
3336111206Sken	struct cd_softc *softc;
333739213Sgibbs	union ccb *ccb;
3338111206Sken	int cdb_size, param_len;
333939213Sgibbs	int error;
334039213Sgibbs
3341111206Sken	softc = (struct cd_softc *)periph->softc;
3342111206Sken
334339213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
334439213Sgibbs
334539213Sgibbs	csio = &ccb->csio;
334639213Sgibbs
334739213Sgibbs	error = 0;
334839213Sgibbs
334939213Sgibbs	/*
3350111206Sken	 * If the data is formatted for the 10 byte version of the mode
3351111206Sken	 * select parameter list, we need to use the 10 byte CDB.
3352111206Sken	 * Otherwise, we use whatever the stored minimum command size.
335339213Sgibbs	 */
3354111206Sken	if (data->cdb_size == 10)
3355111206Sken		cdb_size = data->cdb_size;
3356111206Sken	else
3357111206Sken		cdb_size = softc->minimum_command_size;
335839213Sgibbs
3359111206Sken	if (cdb_size >= 10) {
3360111206Sken		struct scsi_mode_header_10 *mode_header;
3361111206Sken		u_int32_t data_len;
3362111206Sken
3363111206Sken		mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3364111206Sken
3365111206Sken		data_len = scsi_2btoul(mode_header->data_length);
3366111206Sken
3367111206Sken		scsi_ulto2b(0, mode_header->data_length);
3368111206Sken		/*
3369111206Sken		 * SONY drives do not allow a mode select with a medium_type
3370111206Sken		 * value that has just been returned by a mode sense; use a
3371111206Sken		 * medium_type of 0 (Default) instead.
3372111206Sken		 */
3373111206Sken		mode_header->medium_type = 0;
3374111206Sken
3375111206Sken		/*
3376111206Sken		 * Pass back whatever the drive passed to us, plus the size
3377111206Sken		 * of the data length field.
3378111206Sken		 */
3379111206Sken		param_len = data_len + sizeof(mode_header->data_length);
3380111206Sken
3381111206Sken	} else {
3382111206Sken		struct scsi_mode_header_6 *mode_header;
3383111206Sken
3384111206Sken		mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3385111206Sken
3386111206Sken		param_len = mode_header->data_length + 1;
3387111206Sken
3388111206Sken		mode_header->data_length = 0;
3389111206Sken		/*
3390111206Sken		 * SONY drives do not allow a mode select with a medium_type
3391111206Sken		 * value that has just been returned by a mode sense; use a
3392111206Sken		 * medium_type of 0 (Default) instead.
3393111206Sken		 */
3394111206Sken		mode_header->medium_type = 0;
3395111206Sken	}
3396111206Sken
3397111206Sken	/* Don't say we've got more room than we actually allocated */
3398111206Sken	param_len = min(param_len, data->alloc_len);
3399111206Sken
3400111206Sken	scsi_mode_select_len(csio,
3401111206Sken			     /* retries */ 1,
3402111206Sken			     /* cbfcnp */ cddone,
3403111206Sken			     /* tag_action */ MSG_SIMPLE_Q_TAG,
3404111206Sken			     /* scsi_page_fmt */ 1,
3405111206Sken			     /* save_pages */ 0,
3406111206Sken			     /* param_buf */ data->mode_buf,
3407111206Sken			     /* param_len */ param_len,
3408111206Sken			     /* minimum_cmd_size */ cdb_size,
3409111206Sken			     /* sense_len */ SSD_FULL_SIZE,
3410111206Sken			     /* timeout */ 50000);
3411111206Sken
3412111206Sken	/* See comments in cdgetmode() and cd6byteworkaround(). */
3413111206Sken	STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3414111206Sken
341574840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
341674840Sken			 /*sense_flags*/SF_RETRY_UA);
341739213Sgibbs
341839213Sgibbs	xpt_release_ccb(ccb);
341939213Sgibbs
3420111206Sken	STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3421111206Sken
3422111206Sken	return (error);
342339213Sgibbs}
342439213Sgibbs
342539213Sgibbs
342639213Sgibbsstatic int
342739213Sgibbscdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
342839213Sgibbs{
342939531Sken	struct ccb_scsiio *csio;
343039213Sgibbs	union ccb *ccb;
343139213Sgibbs	int error;
343239531Sken	u_int8_t cdb_len;
343339213Sgibbs
343439213Sgibbs	error = 0;
343539213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
343639213Sgibbs	csio = &ccb->csio;
343739531Sken	/*
343839531Sken	 * Use the smallest possible command to perform the operation.
343939531Sken	 */
344039531Sken	if ((len & 0xffff0000) == 0) {
344139531Sken		/*
344239531Sken		 * We can fit in a 10 byte cdb.
344339531Sken		 */
344439531Sken		struct scsi_play_10 *scsi_cmd;
344539213Sgibbs
344639531Sken		scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
344739531Sken		bzero (scsi_cmd, sizeof(*scsi_cmd));
344839531Sken		scsi_cmd->op_code = PLAY_10;
344939531Sken		scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
345039531Sken		scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
345139531Sken		cdb_len = sizeof(*scsi_cmd);
345239531Sken	} else  {
345339531Sken		struct scsi_play_12 *scsi_cmd;
345439213Sgibbs
345539531Sken		scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
345639531Sken		bzero (scsi_cmd, sizeof(*scsi_cmd));
345739531Sken		scsi_cmd->op_code = PLAY_12;
345839531Sken		scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
345939531Sken		scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
346039531Sken		cdb_len = sizeof(*scsi_cmd);
346139531Sken	}
346239531Sken	cam_fill_csio(csio,
346339531Sken		      /*retries*/2,
346439531Sken		      cddone,
346539531Sken		      /*flags*/CAM_DIR_NONE,
346639531Sken		      MSG_SIMPLE_Q_TAG,
346739531Sken		      /*dataptr*/NULL,
346839531Sken		      /*datalen*/0,
346939531Sken		      /*sense_len*/SSD_FULL_SIZE,
347039531Sken		      cdb_len,
347139531Sken		      /*timeout*/50 * 1000);
347239213Sgibbs
347374840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
347474840Sken			 /*sense_flags*/SF_RETRY_UA);
347539213Sgibbs
347639213Sgibbs	xpt_release_ccb(ccb);
347739213Sgibbs
347839213Sgibbs	return(error);
347939213Sgibbs}
348039213Sgibbs
348139213Sgibbsstatic int
348239213Sgibbscdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
348339213Sgibbs	  u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
348439213Sgibbs{
348539213Sgibbs	struct scsi_play_msf *scsi_cmd;
348639213Sgibbs        struct ccb_scsiio *csio;
348739213Sgibbs	union ccb *ccb;
348839213Sgibbs	int error;
348939213Sgibbs
349039213Sgibbs	error = 0;
349139213Sgibbs
349239213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
349339213Sgibbs
349439213Sgibbs	csio = &ccb->csio;
349539213Sgibbs
349639213Sgibbs	cam_fill_csio(csio,
349739213Sgibbs		      /* retries */ 1,
349839213Sgibbs		      /* cbfcnp */ cddone,
349939531Sken		      /* flags */ CAM_DIR_NONE,
350039213Sgibbs		      /* tag_action */ MSG_SIMPLE_Q_TAG,
350139213Sgibbs		      /* data_ptr */ NULL,
350239213Sgibbs		      /* dxfer_len */ 0,
350339213Sgibbs		      /* sense_len */ SSD_FULL_SIZE,
350439213Sgibbs		      sizeof(struct scsi_play_msf),
350539213Sgibbs 		      /* timeout */ 50000);
350639213Sgibbs
350739213Sgibbs	scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
350839213Sgibbs	bzero (scsi_cmd, sizeof(*scsi_cmd));
350939213Sgibbs
351039213Sgibbs        scsi_cmd->op_code = PLAY_MSF;
351139213Sgibbs        scsi_cmd->start_m = startm;
351239213Sgibbs        scsi_cmd->start_s = starts;
351339213Sgibbs        scsi_cmd->start_f = startf;
351439213Sgibbs        scsi_cmd->end_m = endm;
351539213Sgibbs        scsi_cmd->end_s = ends;
351639213Sgibbs        scsi_cmd->end_f = endf;
351739213Sgibbs
351874840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
351974840Sken			 /*sense_flags*/SF_RETRY_UA);
352039213Sgibbs
352139213Sgibbs	xpt_release_ccb(ccb);
352239213Sgibbs
352339213Sgibbs	return(error);
352439213Sgibbs}
352539213Sgibbs
352639213Sgibbs
352739213Sgibbsstatic int
352839213Sgibbscdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
352939213Sgibbs	     u_int32_t etrack, u_int32_t eindex)
353039213Sgibbs{
353139213Sgibbs	struct scsi_play_track *scsi_cmd;
353239213Sgibbs        struct ccb_scsiio *csio;
353339213Sgibbs	union ccb *ccb;
353439213Sgibbs	int error;
353539213Sgibbs
353639213Sgibbs	error = 0;
353739213Sgibbs
353839213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
353939213Sgibbs
354039213Sgibbs	csio = &ccb->csio;
354139213Sgibbs
354239213Sgibbs	cam_fill_csio(csio,
354339213Sgibbs		      /* retries */ 1,
354439213Sgibbs		      /* cbfcnp */ cddone,
354539531Sken		      /* flags */ CAM_DIR_NONE,
354639213Sgibbs		      /* tag_action */ MSG_SIMPLE_Q_TAG,
354739213Sgibbs		      /* data_ptr */ NULL,
354839213Sgibbs		      /* dxfer_len */ 0,
354939213Sgibbs		      /* sense_len */ SSD_FULL_SIZE,
355039213Sgibbs		      sizeof(struct scsi_play_track),
355139213Sgibbs 		      /* timeout */ 50000);
355239213Sgibbs
355339213Sgibbs	scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
355439213Sgibbs	bzero (scsi_cmd, sizeof(*scsi_cmd));
355539213Sgibbs
355639213Sgibbs        scsi_cmd->op_code = PLAY_TRACK;
355739213Sgibbs        scsi_cmd->start_track = strack;
355839213Sgibbs        scsi_cmd->start_index = sindex;
355939213Sgibbs        scsi_cmd->end_track = etrack;
356039213Sgibbs        scsi_cmd->end_index = eindex;
356139213Sgibbs
356274840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
356374840Sken			 /*sense_flags*/SF_RETRY_UA);
356439213Sgibbs
356539213Sgibbs	xpt_release_ccb(ccb);
356639213Sgibbs
356739213Sgibbs	return(error);
356839213Sgibbs}
356939213Sgibbs
357039213Sgibbsstatic int
357139213Sgibbscdpause(struct cam_periph *periph, u_int32_t go)
357239213Sgibbs{
357339213Sgibbs	struct scsi_pause *scsi_cmd;
357439213Sgibbs        struct ccb_scsiio *csio;
357539213Sgibbs	union ccb *ccb;
357639213Sgibbs	int error;
357739213Sgibbs
357839213Sgibbs	error = 0;
357939213Sgibbs
358039213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
358139213Sgibbs
358239213Sgibbs	csio = &ccb->csio;
358339213Sgibbs
358439213Sgibbs	cam_fill_csio(csio,
358539213Sgibbs		      /* retries */ 1,
358639213Sgibbs		      /* cbfcnp */ cddone,
358739531Sken		      /* flags */ CAM_DIR_NONE,
358839213Sgibbs		      /* tag_action */ MSG_SIMPLE_Q_TAG,
358939213Sgibbs		      /* data_ptr */ NULL,
359039213Sgibbs		      /* dxfer_len */ 0,
359139213Sgibbs		      /* sense_len */ SSD_FULL_SIZE,
359239213Sgibbs		      sizeof(struct scsi_pause),
359339213Sgibbs 		      /* timeout */ 50000);
359439213Sgibbs
359539213Sgibbs	scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
359639213Sgibbs	bzero (scsi_cmd, sizeof(*scsi_cmd));
359739213Sgibbs
359839213Sgibbs        scsi_cmd->op_code = PAUSE;
359939213Sgibbs	scsi_cmd->resume = go;
360039213Sgibbs
360174840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
360274840Sken			 /*sense_flags*/SF_RETRY_UA);
360339213Sgibbs
360439213Sgibbs	xpt_release_ccb(ccb);
360539213Sgibbs
360639213Sgibbs	return(error);
360739213Sgibbs}
360839213Sgibbs
360939213Sgibbsstatic int
3610111206Skencdstartunit(struct cam_periph *periph, int load)
361139213Sgibbs{
361239213Sgibbs	union ccb *ccb;
361339213Sgibbs	int error;
361439213Sgibbs
361539213Sgibbs	error = 0;
361639213Sgibbs
361739213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
361839213Sgibbs
361939213Sgibbs	scsi_start_stop(&ccb->csio,
362039213Sgibbs			/* retries */ 1,
362139213Sgibbs			/* cbfcnp */ cddone,
362239213Sgibbs			/* tag_action */ MSG_SIMPLE_Q_TAG,
362339213Sgibbs			/* start */ TRUE,
3624111206Sken			/* load_eject */ load,
362539213Sgibbs			/* immediate */ FALSE,
362639213Sgibbs			/* sense_len */ SSD_FULL_SIZE,
362739213Sgibbs			/* timeout */ 50000);
362839213Sgibbs
362974840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
363074840Sken			 /*sense_flags*/SF_RETRY_UA);
363139213Sgibbs
363239213Sgibbs	xpt_release_ccb(ccb);
363339213Sgibbs
363439213Sgibbs	return(error);
363539213Sgibbs}
363639213Sgibbs
363739213Sgibbsstatic int
363839213Sgibbscdstopunit(struct cam_periph *periph, u_int32_t eject)
363939213Sgibbs{
364039213Sgibbs	union ccb *ccb;
364139213Sgibbs	int error;
364239213Sgibbs
364339213Sgibbs	error = 0;
364439213Sgibbs
364539213Sgibbs	ccb = cdgetccb(periph, /* priority */ 1);
364639213Sgibbs
364739213Sgibbs	scsi_start_stop(&ccb->csio,
364839213Sgibbs			/* retries */ 1,
364939213Sgibbs			/* cbfcnp */ cddone,
365039213Sgibbs			/* tag_action */ MSG_SIMPLE_Q_TAG,
365139213Sgibbs			/* start */ FALSE,
365239213Sgibbs			/* load_eject */ eject,
365339213Sgibbs			/* immediate */ FALSE,
365439213Sgibbs			/* sense_len */ SSD_FULL_SIZE,
365539213Sgibbs			/* timeout */ 50000);
365639213Sgibbs
365774840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
365874840Sken			 /*sense_flags*/SF_RETRY_UA);
365939213Sgibbs
366039213Sgibbs	xpt_release_ccb(ccb);
366139213Sgibbs
366239213Sgibbs	return(error);
366339213Sgibbs}
366460422Sken
366560422Skenstatic int
3666105421Snjlcdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3667105421Snjl{
3668105421Snjl	struct scsi_set_speed *scsi_cmd;
3669105421Snjl	struct ccb_scsiio *csio;
3670105421Snjl	union ccb *ccb;
3671105421Snjl	int error;
3672105421Snjl
3673105421Snjl	error = 0;
3674105421Snjl	ccb = cdgetccb(periph, /* priority */ 1);
3675105421Snjl	csio = &ccb->csio;
3676105421Snjl
3677107193Snjl	/* Preserve old behavior: units in multiples of CDROM speed */
3678107193Snjl	if (rdspeed < 177)
3679107193Snjl		rdspeed *= 177;
3680107193Snjl	if (wrspeed < 177)
3681107193Snjl		wrspeed *= 177;
3682107193Snjl
3683105421Snjl	cam_fill_csio(csio,
3684105421Snjl		      /* retries */ 1,
3685105421Snjl		      /* cbfcnp */ cddone,
3686105421Snjl		      /* flags */ CAM_DIR_NONE,
3687105421Snjl		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3688105421Snjl		      /* data_ptr */ NULL,
3689105421Snjl		      /* dxfer_len */ 0,
3690105421Snjl		      /* sense_len */ SSD_FULL_SIZE,
3691105421Snjl		      sizeof(struct scsi_set_speed),
3692105421Snjl 		      /* timeout */ 50000);
3693105421Snjl
3694105421Snjl	scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3695105421Snjl	bzero(scsi_cmd, sizeof(*scsi_cmd));
3696105421Snjl
3697105421Snjl	scsi_cmd->opcode = SET_CD_SPEED;
3698105421Snjl	scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3699105421Snjl	scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3700105421Snjl
3701105421Snjl	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3702105421Snjl			 /*sense_flags*/SF_RETRY_UA);
3703105421Snjl
3704105421Snjl	xpt_release_ccb(ccb);
3705105421Snjl
3706105421Snjl	return(error);
3707105421Snjl}
3708105421Snjl
3709105421Snjlstatic int
371060422Skencdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
371160422Sken{
371260422Sken	union ccb *ccb;
371360422Sken	u_int8_t *databuf;
371460422Sken	u_int32_t lba;
371560422Sken	int error;
371660422Sken	int length;
371760422Sken
371860422Sken	error = 0;
371960422Sken	databuf = NULL;
372060422Sken	lba = 0;
372160422Sken
372260422Sken	ccb = cdgetccb(periph, /* priority */ 1);
372360422Sken
372460422Sken	switch (authinfo->format) {
372560422Sken	case DVD_REPORT_AGID:
372660422Sken		length = sizeof(struct scsi_report_key_data_agid);
372760422Sken		break;
372860422Sken	case DVD_REPORT_CHALLENGE:
372960422Sken		length = sizeof(struct scsi_report_key_data_challenge);
373060422Sken		break;
373160422Sken	case DVD_REPORT_KEY1:
373260422Sken		length = sizeof(struct scsi_report_key_data_key1_key2);
373360422Sken		break;
373460422Sken	case DVD_REPORT_TITLE_KEY:
373560422Sken		length = sizeof(struct scsi_report_key_data_title);
373660422Sken		/* The lba field is only set for the title key */
373760422Sken		lba = authinfo->lba;
373860422Sken		break;
373960422Sken	case DVD_REPORT_ASF:
374060422Sken		length = sizeof(struct scsi_report_key_data_asf);
374160422Sken		break;
374260422Sken	case DVD_REPORT_RPC:
374360422Sken		length = sizeof(struct scsi_report_key_data_rpc);
374460422Sken		break;
374560422Sken	case DVD_INVALIDATE_AGID:
374660422Sken		length = 0;
374760422Sken		break;
374860422Sken	default:
374960422Sken		error = EINVAL;
375060422Sken		goto bailout;
375160422Sken		break; /* NOTREACHED */
375260422Sken	}
375360422Sken
375460422Sken	if (length != 0) {
3755111119Simp		databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
375660422Sken	} else
375760422Sken		databuf = NULL;
375860422Sken
375960422Sken
376060422Sken	scsi_report_key(&ccb->csio,
376160422Sken			/* retries */ 1,
376260422Sken			/* cbfcnp */ cddone,
376360422Sken			/* tag_action */ MSG_SIMPLE_Q_TAG,
376460422Sken			/* lba */ lba,
376560422Sken			/* agid */ authinfo->agid,
376660422Sken			/* key_format */ authinfo->format,
376760422Sken			/* data_ptr */ databuf,
376860422Sken			/* dxfer_len */ length,
376960422Sken			/* sense_len */ SSD_FULL_SIZE,
377060422Sken			/* timeout */ 50000);
377160422Sken
377274840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
377374840Sken			 /*sense_flags*/SF_RETRY_UA);
377460422Sken
377560422Sken	if (error != 0)
377660422Sken		goto bailout;
377760422Sken
377860422Sken	if (ccb->csio.resid != 0) {
3779164906Smjacob		xpt_print(periph->path, "warning, residual for report key "
3780164906Smjacob		    "command is %d\n", ccb->csio.resid);
378160422Sken	}
378260422Sken
378360422Sken	switch(authinfo->format) {
378460422Sken	case DVD_REPORT_AGID: {
378560422Sken		struct scsi_report_key_data_agid *agid_data;
378660422Sken
378760422Sken		agid_data = (struct scsi_report_key_data_agid *)databuf;
378860422Sken
378960422Sken		authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
379060422Sken			RKD_AGID_SHIFT;
379160422Sken		break;
379260422Sken	}
379360422Sken	case DVD_REPORT_CHALLENGE: {
379460422Sken		struct scsi_report_key_data_challenge *chal_data;
379560422Sken
379660422Sken		chal_data = (struct scsi_report_key_data_challenge *)databuf;
379760422Sken
379860422Sken		bcopy(chal_data->challenge_key, authinfo->keychal,
379960422Sken		      min(sizeof(chal_data->challenge_key),
380060422Sken		          sizeof(authinfo->keychal)));
380160422Sken		break;
380260422Sken	}
380360422Sken	case DVD_REPORT_KEY1: {
380460422Sken		struct scsi_report_key_data_key1_key2 *key1_data;
380560422Sken
380660422Sken		key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
380760422Sken
380860422Sken		bcopy(key1_data->key1, authinfo->keychal,
380960422Sken		      min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
381060422Sken		break;
381160422Sken	}
381260422Sken	case DVD_REPORT_TITLE_KEY: {
381360422Sken		struct scsi_report_key_data_title *title_data;
381460422Sken
381560422Sken		title_data = (struct scsi_report_key_data_title *)databuf;
381660422Sken
381760422Sken		authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
381860422Sken			RKD_TITLE_CPM_SHIFT;
381960422Sken		authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
382060422Sken			RKD_TITLE_CP_SEC_SHIFT;
382160422Sken		authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
382260422Sken			RKD_TITLE_CMGS_SHIFT;
382360422Sken		bcopy(title_data->title_key, authinfo->keychal,
382460422Sken		      min(sizeof(title_data->title_key),
382560422Sken			  sizeof(authinfo->keychal)));
382660422Sken		break;
382760422Sken	}
382860422Sken	case DVD_REPORT_ASF: {
382960422Sken		struct scsi_report_key_data_asf *asf_data;
383060422Sken
383160422Sken		asf_data = (struct scsi_report_key_data_asf *)databuf;
383260422Sken
383360422Sken		authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
383460422Sken		break;
383560422Sken	}
383660422Sken	case DVD_REPORT_RPC: {
383760422Sken		struct scsi_report_key_data_rpc *rpc_data;
383860422Sken
383960422Sken		rpc_data = (struct scsi_report_key_data_rpc *)databuf;
384060422Sken
384160422Sken		authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
384260422Sken			RKD_RPC_TYPE_SHIFT;
384360422Sken		authinfo->vend_rsts =
384460422Sken			(rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
384560422Sken			RKD_RPC_VENDOR_RESET_SHIFT;
384660422Sken		authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
384771752Sken		authinfo->region = rpc_data->region_mask;
384871752Sken		authinfo->rpc_scheme = rpc_data->rpc_scheme1;
384960422Sken		break;
385060422Sken	}
385160422Sken	case DVD_INVALIDATE_AGID:
385260422Sken		break;
385360422Sken	default:
385460422Sken		/* This should be impossible, since we checked above */
385560422Sken		error = EINVAL;
385660422Sken		goto bailout;
385760422Sken		break; /* NOTREACHED */
385860422Sken	}
385960422Skenbailout:
386060422Sken	if (databuf != NULL)
386160422Sken		free(databuf, M_DEVBUF);
386260422Sken
386360422Sken	xpt_release_ccb(ccb);
386460422Sken
386560422Sken	return(error);
386660422Sken}
386760422Sken
386860422Skenstatic int
386960422Skencdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
387060422Sken{
387160422Sken	union ccb *ccb;
387260422Sken	u_int8_t *databuf;
387360422Sken	int length;
387460422Sken	int error;
387560422Sken
387660422Sken	error = 0;
387760422Sken	databuf = NULL;
387860422Sken
387960422Sken	ccb = cdgetccb(periph, /* priority */ 1);
388060422Sken
388160422Sken	switch(authinfo->format) {
388260422Sken	case DVD_SEND_CHALLENGE: {
388360422Sken		struct scsi_report_key_data_challenge *challenge_data;
388460422Sken
388560422Sken		length = sizeof(*challenge_data);
388660422Sken
3887111119Simp		challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
388860422Sken
388960422Sken		databuf = (u_int8_t *)challenge_data;
389060422Sken
389160422Sken		scsi_ulto2b(length - sizeof(challenge_data->data_len),
389260422Sken			    challenge_data->data_len);
389360422Sken
389460422Sken		bcopy(authinfo->keychal, challenge_data->challenge_key,
389560422Sken		      min(sizeof(authinfo->keychal),
389660422Sken			  sizeof(challenge_data->challenge_key)));
389760422Sken		break;
389860422Sken	}
389960422Sken	case DVD_SEND_KEY2: {
390060422Sken		struct scsi_report_key_data_key1_key2 *key2_data;
390160422Sken
390260422Sken		length = sizeof(*key2_data);
390360422Sken
3904111119Simp		key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
390560422Sken
390660422Sken		databuf = (u_int8_t *)key2_data;
390760422Sken
390860422Sken		scsi_ulto2b(length - sizeof(key2_data->data_len),
390960422Sken			    key2_data->data_len);
391060422Sken
391160422Sken		bcopy(authinfo->keychal, key2_data->key1,
391260422Sken		      min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
391360422Sken
391460422Sken		break;
391560422Sken	}
391660422Sken	case DVD_SEND_RPC: {
391760422Sken		struct scsi_send_key_data_rpc *rpc_data;
391860422Sken
391960422Sken		length = sizeof(*rpc_data);
392060422Sken
3921111119Simp		rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
392260422Sken
392360422Sken		databuf = (u_int8_t *)rpc_data;
392460422Sken
392560422Sken		scsi_ulto2b(length - sizeof(rpc_data->data_len),
392660422Sken			    rpc_data->data_len);
392760422Sken
392860422Sken		rpc_data->region_code = authinfo->region;
392960422Sken		break;
393060422Sken	}
393160422Sken	default:
393260422Sken		error = EINVAL;
393360422Sken		goto bailout;
393460422Sken		break; /* NOTREACHED */
393560422Sken	}
393660422Sken
393760422Sken	scsi_send_key(&ccb->csio,
393860422Sken		      /* retries */ 1,
393960422Sken		      /* cbfcnp */ cddone,
394060422Sken		      /* tag_action */ MSG_SIMPLE_Q_TAG,
394160422Sken		      /* agid */ authinfo->agid,
394260422Sken		      /* key_format */ authinfo->format,
394360422Sken		      /* data_ptr */ databuf,
394460422Sken		      /* dxfer_len */ length,
394560422Sken		      /* sense_len */ SSD_FULL_SIZE,
394660422Sken		      /* timeout */ 50000);
394760422Sken
394874840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
394974840Sken			 /*sense_flags*/SF_RETRY_UA);
395060422Sken
395160422Skenbailout:
395260422Sken
395360422Sken	if (databuf != NULL)
395460422Sken		free(databuf, M_DEVBUF);
395560422Sken
395660422Sken	xpt_release_ccb(ccb);
395760422Sken
395860422Sken	return(error);
395960422Sken}
396060422Sken
396160422Skenstatic int
396260422Skencdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
396360422Sken{
396460422Sken	union ccb *ccb;
396560422Sken	u_int8_t *databuf;
396660422Sken	u_int32_t address;
396760422Sken	int error;
396860422Sken	int length;
396960422Sken
397060422Sken	error = 0;
397160422Sken	databuf = NULL;
397260422Sken	/* The address is reserved for many of the formats */
397360422Sken	address = 0;
397460422Sken
397560422Sken	ccb = cdgetccb(periph, /* priority */ 1);
397660422Sken
397760422Sken	switch(dvdstruct->format) {
397860422Sken	case DVD_STRUCT_PHYSICAL:
397960422Sken		length = sizeof(struct scsi_read_dvd_struct_data_physical);
398060422Sken		break;
398160422Sken	case DVD_STRUCT_COPYRIGHT:
398260422Sken		length = sizeof(struct scsi_read_dvd_struct_data_copyright);
398360422Sken		break;
398460422Sken	case DVD_STRUCT_DISCKEY:
398560422Sken		length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
398660422Sken		break;
398760422Sken	case DVD_STRUCT_BCA:
398860422Sken		length = sizeof(struct scsi_read_dvd_struct_data_bca);
398960422Sken		break;
399060422Sken	case DVD_STRUCT_MANUFACT:
399160422Sken		length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
399260422Sken		break;
399360422Sken	case DVD_STRUCT_CMI:
399460422Sken		error = ENODEV;
399560422Sken		goto bailout;
399660422Sken#ifdef notyet
399760422Sken		length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
399860422Sken		address = dvdstruct->address;
399960422Sken#endif
400060422Sken		break; /* NOTREACHED */
400160422Sken	case DVD_STRUCT_PROTDISCID:
400260422Sken		length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
400360422Sken		break;
400460422Sken	case DVD_STRUCT_DISCKEYBLOCK:
400560422Sken		length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
400660422Sken		break;
400760422Sken	case DVD_STRUCT_DDS:
400860422Sken		length = sizeof(struct scsi_read_dvd_struct_data_dds);
400960422Sken		break;
401060422Sken	case DVD_STRUCT_MEDIUM_STAT:
401160422Sken		length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
401260422Sken		break;
401360422Sken	case DVD_STRUCT_SPARE_AREA:
401460422Sken		length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
401560422Sken		break;
401660422Sken	case DVD_STRUCT_RMD_LAST:
401760422Sken		error = ENODEV;
401860422Sken		goto bailout;
401960422Sken#ifdef notyet
402060422Sken		length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
402160422Sken		address = dvdstruct->address;
402260422Sken#endif
402360422Sken		break; /* NOTREACHED */
402460422Sken	case DVD_STRUCT_RMD_RMA:
402560422Sken		error = ENODEV;
402660422Sken		goto bailout;
402760422Sken#ifdef notyet
402860422Sken		length = sizeof(struct scsi_read_dvd_struct_data_rmd);
402960422Sken		address = dvdstruct->address;
403060422Sken#endif
403160422Sken		break; /* NOTREACHED */
403260422Sken	case DVD_STRUCT_PRERECORDED:
403360422Sken		length = sizeof(struct scsi_read_dvd_struct_data_leadin);
403460422Sken		break;
403560422Sken	case DVD_STRUCT_UNIQUEID:
403660422Sken		length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
403760422Sken		break;
403860422Sken	case DVD_STRUCT_DCB:
403960422Sken		error = ENODEV;
404060422Sken		goto bailout;
404160422Sken#ifdef notyet
404260422Sken		length = sizeof(struct scsi_read_dvd_struct_data_dcb);
404360422Sken		address = dvdstruct->address;
404460422Sken#endif
404560422Sken		break; /* NOTREACHED */
404660422Sken	case DVD_STRUCT_LIST:
404760422Sken		/*
404860422Sken		 * This is the maximum allocation length for the READ DVD
404960422Sken		 * STRUCTURE command.  There's nothing in the MMC3 spec
405060422Sken		 * that indicates a limit in the amount of data that can
405160422Sken		 * be returned from this call, other than the limits
405260422Sken		 * imposed by the 2-byte length variables.
405360422Sken		 */
405460422Sken		length = 65535;
405560422Sken		break;
405660422Sken	default:
405760422Sken		error = EINVAL;
405860422Sken		goto bailout;
405960422Sken		break; /* NOTREACHED */
406060422Sken	}
406160422Sken
406260422Sken	if (length != 0) {
4063111119Simp		databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
406460422Sken	} else
406560422Sken		databuf = NULL;
406660422Sken
406760422Sken	scsi_read_dvd_structure(&ccb->csio,
406860422Sken				/* retries */ 1,
406960422Sken				/* cbfcnp */ cddone,
407060422Sken				/* tag_action */ MSG_SIMPLE_Q_TAG,
407160422Sken				/* lba */ address,
407260422Sken				/* layer_number */ dvdstruct->layer_num,
407360422Sken				/* key_format */ dvdstruct->format,
407460422Sken				/* agid */ dvdstruct->agid,
407560422Sken				/* data_ptr */ databuf,
407660422Sken				/* dxfer_len */ length,
407760422Sken				/* sense_len */ SSD_FULL_SIZE,
407860422Sken				/* timeout */ 50000);
407960422Sken
408074840Sken	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
408174840Sken			 /*sense_flags*/SF_RETRY_UA);
408260422Sken
408360422Sken	if (error != 0)
408460422Sken		goto bailout;
408560422Sken
408660422Sken	switch(dvdstruct->format) {
408760422Sken	case DVD_STRUCT_PHYSICAL: {
408860422Sken		struct scsi_read_dvd_struct_data_layer_desc *inlayer;
408960422Sken		struct dvd_layer *outlayer;
409060422Sken		struct scsi_read_dvd_struct_data_physical *phys_data;
409160422Sken
409260422Sken		phys_data =
409360422Sken			(struct scsi_read_dvd_struct_data_physical *)databuf;
409460422Sken		inlayer = &phys_data->layer_desc;
409560422Sken		outlayer = (struct dvd_layer *)&dvdstruct->data;
409660422Sken
409760422Sken		dvdstruct->length = sizeof(*inlayer);
409860422Sken
409960422Sken		outlayer->book_type = (inlayer->book_type_version &
410060422Sken			RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
410160422Sken		outlayer->book_version = (inlayer->book_type_version &
410260422Sken			RDSD_BOOK_VERSION_MASK);
410360422Sken		outlayer->disc_size = (inlayer->disc_size_max_rate &
410460422Sken			RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
410560422Sken		outlayer->max_rate = (inlayer->disc_size_max_rate &
410660422Sken			RDSD_MAX_RATE_MASK);
410760422Sken		outlayer->nlayers = (inlayer->layer_info &
410860422Sken			RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
410960422Sken		outlayer->track_path = (inlayer->layer_info &
411060422Sken			RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
411160422Sken		outlayer->layer_type = (inlayer->layer_info &
411260422Sken			RDSD_LAYER_TYPE_MASK);
411360422Sken		outlayer->linear_density = (inlayer->density &
411460422Sken			RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
411560422Sken		outlayer->track_density = (inlayer->density &
411660422Sken			RDSD_TRACK_DENSITY_MASK);
411760422Sken		outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
411860422Sken			RDSD_BCA_SHIFT;
411960422Sken		outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
412060422Sken		outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
412160422Sken		outlayer->end_sector_l0 =
412260422Sken			scsi_3btoul(inlayer->end_sector_layer0);
412360422Sken		break;
412460422Sken	}
412560422Sken	case DVD_STRUCT_COPYRIGHT: {
412660422Sken		struct scsi_read_dvd_struct_data_copyright *copy_data;
412760422Sken
412860422Sken		copy_data = (struct scsi_read_dvd_struct_data_copyright *)
412960422Sken			databuf;
413060422Sken
413160422Sken		dvdstruct->cpst = copy_data->cps_type;
413260422Sken		dvdstruct->rmi = copy_data->region_info;
413360422Sken		dvdstruct->length = 0;
413460422Sken
413560422Sken		break;
413660422Sken	}
413760422Sken	default:
413860422Sken		/*
413960422Sken		 * Tell the user what the overall length is, no matter
414060422Sken		 * what we can actually fit in the data buffer.
414160422Sken		 */
414260422Sken		dvdstruct->length = length - ccb->csio.resid -
414360422Sken			sizeof(struct scsi_read_dvd_struct_data_header);
414460422Sken
414560422Sken		/*
414660422Sken		 * But only actually copy out the smaller of what we read
414760422Sken		 * in or what the structure can take.
414860422Sken		 */
414960422Sken		bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
415060422Sken		      dvdstruct->data,
415160422Sken		      min(sizeof(dvdstruct->data), dvdstruct->length));
415260422Sken		break;
415360422Sken	}
415460422Skenbailout:
415560422Sken
415660422Sken	if (databuf != NULL)
415760422Sken		free(databuf, M_DEVBUF);
415860422Sken
415960422Sken	xpt_release_ccb(ccb);
416060422Sken
416160422Sken	return(error);
416260422Sken}
416360422Sken
416460422Skenvoid
416560422Skenscsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
416660422Sken		void (*cbfcnp)(struct cam_periph *, union ccb *),
416760422Sken		u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
416860422Sken		u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
416960422Sken		u_int8_t sense_len, u_int32_t timeout)
417060422Sken{
417160422Sken	struct scsi_report_key *scsi_cmd;
417260422Sken
417360422Sken	scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
417460422Sken	bzero(scsi_cmd, sizeof(*scsi_cmd));
417560422Sken	scsi_cmd->opcode = REPORT_KEY;
417660422Sken	scsi_ulto4b(lba, scsi_cmd->lba);
417760422Sken	scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
417860422Sken	scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
417960422Sken		(key_format & RK_KF_KEYFORMAT_MASK);
418060422Sken
418160422Sken	cam_fill_csio(csio,
418260422Sken		      retries,
418360422Sken		      cbfcnp,
418460422Sken		      /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
418560422Sken		      tag_action,
418660422Sken		      /*data_ptr*/ data_ptr,
418760422Sken		      /*dxfer_len*/ dxfer_len,
418860422Sken		      sense_len,
418960422Sken		      sizeof(*scsi_cmd),
419060422Sken		      timeout);
419160422Sken}
419260422Sken
419360422Skenvoid
419460422Skenscsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
419560422Sken	      void (*cbfcnp)(struct cam_periph *, union ccb *),
419660422Sken	      u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
419760422Sken	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
419860422Sken	      u_int32_t timeout)
419960422Sken{
420060422Sken	struct scsi_send_key *scsi_cmd;
420160422Sken
420260422Sken	scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
420360422Sken	bzero(scsi_cmd, sizeof(*scsi_cmd));
420460422Sken	scsi_cmd->opcode = SEND_KEY;
420560422Sken
420660422Sken	scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
420760422Sken	scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
420860422Sken		(key_format & RK_KF_KEYFORMAT_MASK);
420960422Sken
421060422Sken	cam_fill_csio(csio,
421160422Sken		      retries,
421260422Sken		      cbfcnp,
421360422Sken		      /*flags*/ CAM_DIR_OUT,
421460422Sken		      tag_action,
421560422Sken		      /*data_ptr*/ data_ptr,
421660422Sken		      /*dxfer_len*/ dxfer_len,
421760422Sken		      sense_len,
421860422Sken		      sizeof(*scsi_cmd),
421960422Sken		      timeout);
422060422Sken}
422160422Sken
422260422Sken
422360422Skenvoid
422460422Skenscsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
422560422Sken			void (*cbfcnp)(struct cam_periph *, union ccb *),
422660422Sken			u_int8_t tag_action, u_int32_t address,
422760422Sken			u_int8_t layer_number, u_int8_t format, u_int8_t agid,
422860422Sken			u_int8_t *data_ptr, u_int32_t dxfer_len,
422960422Sken			u_int8_t sense_len, u_int32_t timeout)
423060422Sken{
423160422Sken	struct scsi_read_dvd_structure *scsi_cmd;
423260422Sken
423360422Sken	scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
423460422Sken	bzero(scsi_cmd, sizeof(*scsi_cmd));
423560422Sken	scsi_cmd->opcode = READ_DVD_STRUCTURE;
423660422Sken
423760422Sken	scsi_ulto4b(address, scsi_cmd->address);
423860422Sken	scsi_cmd->layer_number = layer_number;
423960422Sken	scsi_cmd->format = format;
424060422Sken	scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
424160422Sken	/* The AGID is the top two bits of this byte */
424260422Sken	scsi_cmd->agid = agid << 6;
424360422Sken
424460422Sken	cam_fill_csio(csio,
424560422Sken		      retries,
424660422Sken		      cbfcnp,
424760422Sken		      /*flags*/ CAM_DIR_IN,
424860422Sken		      tag_action,
424960422Sken		      /*data_ptr*/ data_ptr,
425060422Sken		      /*dxfer_len*/ dxfer_len,
425160422Sken		      sense_len,
425260422Sken		      sizeof(*scsi_cmd),
425360422Sken		      timeout);
425460422Sken}
4255