scsi_cd.c revision 257049
1/*-
2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer,
11 *    without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*-
29 * Portions of this driver taken from the original FreeBSD cd driver.
30 * Written by Julian Elischer (julian@tfs.com)
31 * for TRW Financial Systems for use under the MACH(2.5) operating system.
32 *
33 * TRW Financial Systems, in accordance with their agreement with Carnegie
34 * Mellon University, makes this software available to CMU to distribute
35 * or use in any manner that they see fit as long as this message is kept with
36 * the software. For this reason TFS also grants any other persons or
37 * organisations permission to use or modify this software.
38 *
39 * TFS supplies this software to be publicly redistributed
40 * on the understanding that TFS is not responsible for the correct
41 * functioning of this software in any circumstances.
42 *
43 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
44 *
45 *      from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
46 */
47
48#include <sys/cdefs.h>
49__FBSDID("$FreeBSD: stable/10/sys/cam/scsi/scsi_cd.c 257049 2013-10-24 10:33:31Z mav $");
50
51#include "opt_cd.h"
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/kernel.h>
56#include <sys/bio.h>
57#include <sys/conf.h>
58#include <sys/disk.h>
59#include <sys/malloc.h>
60#include <sys/cdio.h>
61#include <sys/cdrio.h>
62#include <sys/dvdio.h>
63#include <sys/devicestat.h>
64#include <sys/sysctl.h>
65#include <sys/taskqueue.h>
66#include <geom/geom_disk.h>
67
68#include <cam/cam.h>
69#include <cam/cam_ccb.h>
70#include <cam/cam_periph.h>
71#include <cam/cam_xpt_periph.h>
72#include <cam/cam_queue.h>
73#include <cam/cam_sim.h>
74
75#include <cam/scsi/scsi_message.h>
76#include <cam/scsi/scsi_da.h>
77#include <cam/scsi/scsi_cd.h>
78
79#define LEADOUT         0xaa            /* leadout toc entry */
80
81struct cd_params {
82	u_int32_t blksize;
83	u_long    disksize;
84};
85
86typedef enum {
87	CD_Q_NONE		= 0x00,
88	CD_Q_NO_TOUCH		= 0x01,
89	CD_Q_BCD_TRACKS		= 0x02,
90	CD_Q_NO_CHANGER		= 0x04,
91	CD_Q_CHANGER		= 0x08,
92	CD_Q_10_BYTE_ONLY	= 0x10
93} cd_quirks;
94
95#define CD_Q_BIT_STRING		\
96	"\020"			\
97	"\001NO_TOUCH"		\
98	"\002BCD_TRACKS"	\
99	"\003NO_CHANGER"	\
100	"\004CHANGER"		\
101	"\00510_BYTE_ONLY"
102
103typedef enum {
104	CD_FLAG_INVALID		= 0x0001,
105	CD_FLAG_NEW_DISC	= 0x0002,
106	CD_FLAG_DISC_LOCKED	= 0x0004,
107	CD_FLAG_DISC_REMOVABLE	= 0x0008,
108	CD_FLAG_SAW_MEDIA	= 0x0010,
109	CD_FLAG_CHANGER		= 0x0040,
110	CD_FLAG_ACTIVE		= 0x0080,
111	CD_FLAG_SCHED_ON_COMP	= 0x0100,
112	CD_FLAG_RETRY_UA	= 0x0200,
113	CD_FLAG_VALID_MEDIA	= 0x0400,
114	CD_FLAG_VALID_TOC	= 0x0800,
115	CD_FLAG_SCTX_INIT	= 0x1000
116} cd_flags;
117
118typedef enum {
119	CD_CCB_PROBE		= 0x01,
120	CD_CCB_BUFFER_IO	= 0x02,
121	CD_CCB_WAITING		= 0x03,
122	CD_CCB_TUR		= 0x04,
123	CD_CCB_TYPE_MASK	= 0x0F,
124	CD_CCB_RETRY_UA		= 0x10
125} cd_ccb_state;
126
127typedef enum {
128	CHANGER_TIMEOUT_SCHED		= 0x01,
129	CHANGER_SHORT_TMOUT_SCHED	= 0x02,
130	CHANGER_MANUAL_CALL		= 0x04,
131	CHANGER_NEED_TIMEOUT		= 0x08
132} cd_changer_flags;
133
134#define ccb_state ppriv_field0
135#define ccb_bp ppriv_ptr1
136
137struct cd_tocdata {
138	struct ioc_toc_header header;
139	struct cd_toc_entry entries[100];
140};
141
142struct cd_toc_single {
143	struct ioc_toc_header header;
144	struct cd_toc_entry entry;
145};
146
147typedef enum {
148	CD_STATE_PROBE,
149	CD_STATE_NORMAL
150} cd_state;
151
152struct cd_softc {
153	cam_pinfo		pinfo;
154	cd_state		state;
155	volatile cd_flags	flags;
156	struct bio_queue_head	bio_queue;
157	LIST_HEAD(, ccb_hdr)	pending_ccbs;
158	struct cd_params	params;
159	union ccb		saved_ccb;
160	cd_quirks		quirks;
161	STAILQ_ENTRY(cd_softc)	changer_links;
162	struct cdchanger	*changer;
163	int			bufs_left;
164	struct cam_periph	*periph;
165	int			minimum_command_size;
166	int			outstanding_cmds;
167	int			tur;
168	struct task		sysctl_task;
169	struct sysctl_ctx_list	sysctl_ctx;
170	struct sysctl_oid	*sysctl_tree;
171	STAILQ_HEAD(, cd_mode_params)	mode_queue;
172	struct cd_tocdata	toc;
173	struct disk		*disk;
174	struct callout		mediapoll_c;
175};
176
177struct cd_page_sizes {
178	int page;
179	int page_size;
180};
181
182static struct cd_page_sizes cd_page_size_table[] =
183{
184	{ AUDIO_PAGE, sizeof(struct cd_audio_page)}
185};
186
187struct cd_quirk_entry {
188	struct scsi_inquiry_pattern inq_pat;
189	cd_quirks quirks;
190};
191
192/*
193 * The changer quirk entries aren't strictly necessary.  Basically, what
194 * they do is tell cdregister() up front that a device is a changer.
195 * Otherwise, it will figure that fact out once it sees a LUN on the device
196 * that is greater than 0.  If it is known up front that a device is a changer,
197 * all I/O to the device will go through the changer scheduling routines, as
198 * opposed to the "normal" CD code.
199 *
200 * NOTE ON 10_BYTE_ONLY quirks:  Any 10_BYTE_ONLY quirks MUST be because
201 * your device hangs when it gets a 10 byte command.  Adding a quirk just
202 * to get rid of the informative diagnostic message is not acceptable.  All
203 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
204 * referenced in a comment along with the quirk) , and must be approved by
205 * ken@FreeBSD.org.  Any quirks added that don't adhere to this policy may
206 * be removed until the submitter can explain why they are needed.
207 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
208 * when the CAM_NEW_TRAN_CODE work is done.
209 */
210static struct cd_quirk_entry cd_quirk_table[] =
211{
212	{
213		{ T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
214		 /*quirks*/ CD_Q_CHANGER
215	},
216	{
217		{ T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
218		  "*"}, /* quirks */ CD_Q_CHANGER
219	},
220	{
221		{ T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
222		 /* quirks */ CD_Q_CHANGER
223	},
224	{
225		{ T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
226		/* quirks */ CD_Q_BCD_TRACKS
227	}
228};
229
230static	disk_open_t	cdopen;
231static	disk_close_t	cdclose;
232static	disk_ioctl_t	cdioctl;
233static	disk_strategy_t	cdstrategy;
234
235static	periph_init_t	cdinit;
236static	periph_ctor_t	cdregister;
237static	periph_dtor_t	cdcleanup;
238static	periph_start_t	cdstart;
239static	periph_oninv_t	cdoninvalidate;
240static	void		cdasync(void *callback_arg, u_int32_t code,
241				struct cam_path *path, void *arg);
242static	int		cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
243static	void		cdshorttimeout(void *arg);
244static	void		cdschedule(struct cam_periph *periph, int priority);
245static	void		cdrunchangerqueue(void *arg);
246static	void		cdchangerschedule(struct cd_softc *softc);
247static	int		cdrunccb(union ccb *ccb,
248				 int (*error_routine)(union ccb *ccb,
249						      u_int32_t cam_flags,
250						      u_int32_t sense_flags),
251				 u_int32_t cam_flags, u_int32_t sense_flags);
252static	union ccb 	*cdgetccb(struct cam_periph *periph,
253				  u_int32_t priority);
254static	void		cddone(struct cam_periph *periph,
255			       union ccb *start_ccb);
256static	union cd_pages	*cdgetpage(struct cd_mode_params *mode_params);
257static	int		cdgetpagesize(int page_num);
258static	void		cdprevent(struct cam_periph *periph, int action);
259static	int		cdcheckmedia(struct cam_periph *periph);
260static	int		cdsize(struct cam_periph *periph, u_int32_t *size);
261static	int		cd6byteworkaround(union ccb *ccb);
262static	int		cderror(union ccb *ccb, u_int32_t cam_flags,
263				u_int32_t sense_flags);
264static	int		cdreadtoc(struct cam_periph *periph, u_int32_t mode,
265				  u_int32_t start, u_int8_t *data,
266				  u_int32_t len, u_int32_t sense_flags);
267static	int		cdgetmode(struct cam_periph *periph,
268				  struct cd_mode_params *data, u_int32_t page);
269static	int		cdsetmode(struct cam_periph *periph,
270				  struct cd_mode_params *data);
271static	int		cdplay(struct cam_periph *periph, u_int32_t blk,
272			       u_int32_t len);
273static	int		cdreadsubchannel(struct cam_periph *periph,
274					 u_int32_t mode, u_int32_t format,
275					 int track,
276					 struct cd_sub_channel_info *data,
277					 u_int32_t len);
278static	int		cdplaymsf(struct cam_periph *periph, u_int32_t startm,
279				  u_int32_t starts, u_int32_t startf,
280				  u_int32_t endm, u_int32_t ends,
281				  u_int32_t endf);
282static	int		cdplaytracks(struct cam_periph *periph,
283				     u_int32_t strack, u_int32_t sindex,
284				     u_int32_t etrack, u_int32_t eindex);
285static	int		cdpause(struct cam_periph *periph, u_int32_t go);
286static	int		cdstopunit(struct cam_periph *periph, u_int32_t eject);
287static	int		cdstartunit(struct cam_periph *periph, int load);
288static	int		cdsetspeed(struct cam_periph *periph,
289				   u_int32_t rdspeed, u_int32_t wrspeed);
290static	int		cdreportkey(struct cam_periph *periph,
291				    struct dvd_authinfo *authinfo);
292static	int		cdsendkey(struct cam_periph *periph,
293				  struct dvd_authinfo *authinfo);
294static	int		cdreaddvdstructure(struct cam_periph *periph,
295					   struct dvd_struct *dvdstruct);
296static timeout_t	cdmediapoll;
297
298static struct periph_driver cddriver =
299{
300	cdinit, "cd",
301	TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
302};
303
304PERIPHDRIVER_DECLARE(cd, cddriver);
305
306#ifndef	CD_DEFAULT_POLL_PERIOD
307#define	CD_DEFAULT_POLL_PERIOD	3
308#endif
309#ifndef	CD_DEFAULT_RETRY
310#define	CD_DEFAULT_RETRY	4
311#endif
312#ifndef	CD_DEFAULT_TIMEOUT
313#define	CD_DEFAULT_TIMEOUT	30000
314#endif
315#ifndef CHANGER_MIN_BUSY_SECONDS
316#define CHANGER_MIN_BUSY_SECONDS	5
317#endif
318#ifndef CHANGER_MAX_BUSY_SECONDS
319#define CHANGER_MAX_BUSY_SECONDS	15
320#endif
321
322static int cd_poll_period = CD_DEFAULT_POLL_PERIOD;
323static int cd_retry_count = CD_DEFAULT_RETRY;
324static int cd_timeout = CD_DEFAULT_TIMEOUT;
325static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
326static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
327
328static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
329static SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0,
330    "CD Changer");
331SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RW,
332           &cd_poll_period, 0, "Media polling period in seconds");
333TUNABLE_INT("kern.cam.cd.poll_period", &cd_poll_period);
334SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RW,
335           &cd_retry_count, 0, "Normal I/O retry count");
336TUNABLE_INT("kern.cam.cd.retry_count", &cd_retry_count);
337SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RW,
338	   &cd_timeout, 0, "Timeout, in us, for read operations");
339TUNABLE_INT("kern.cam.cd.timeout", &cd_timeout);
340SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
341	   &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
342TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
343SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
344	   &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
345TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
346
347struct cdchanger {
348	path_id_t			 path_id;
349	target_id_t			 target_id;
350	int				 num_devices;
351	struct camq			 devq;
352	struct timeval			 start_time;
353	struct cd_softc			 *cur_device;
354	struct callout			 short_handle;
355	struct callout			 long_handle;
356	volatile cd_changer_flags	 flags;
357	STAILQ_ENTRY(cdchanger)		 changer_links;
358	STAILQ_HEAD(chdevlist, cd_softc) chluns;
359};
360
361static struct mtx changerq_mtx;
362static STAILQ_HEAD(changerlist, cdchanger) changerq;
363static int num_changers;
364
365static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
366
367static void
368cdinit(void)
369{
370	cam_status status;
371
372	mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
373	STAILQ_INIT(&changerq);
374
375	/*
376	 * Install a global async callback.  This callback will
377	 * receive async callbacks like "new device found".
378	 */
379	status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
380
381	if (status != CAM_REQ_CMP) {
382		printf("cd: Failed to attach master async callback "
383		       "due to status 0x%x!\n", status);
384	}
385}
386
387/*
388 * Callback from GEOM, called when it has finished cleaning up its
389 * resources.
390 */
391static void
392cddiskgonecb(struct disk *dp)
393{
394	struct cam_periph *periph;
395
396	periph = (struct cam_periph *)dp->d_drv1;
397	cam_periph_release(periph);
398}
399
400static void
401cdoninvalidate(struct cam_periph *periph)
402{
403	struct cd_softc *softc;
404
405	softc = (struct cd_softc *)periph->softc;
406
407	/*
408	 * De-register any async callbacks.
409	 */
410	xpt_register_async(0, cdasync, periph, periph->path);
411
412	softc->flags |= CD_FLAG_INVALID;
413
414	/*
415	 * Return all queued I/O with ENXIO.
416	 * XXX Handle any transactions queued to the card
417	 *     with XPT_ABORT_CCB.
418	 */
419	bioq_flush(&softc->bio_queue, NULL, ENXIO);
420
421	/*
422	 * If this device is part of a changer, and it was scheduled
423	 * to run, remove it from the run queue since we just nuked
424	 * all of its scheduled I/O.
425	 */
426	if ((softc->flags & CD_FLAG_CHANGER)
427	 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
428		camq_remove(&softc->changer->devq, softc->pinfo.index);
429
430	disk_gone(softc->disk);
431}
432
433static void
434cdcleanup(struct cam_periph *periph)
435{
436	struct cd_softc *softc;
437
438	softc = (struct cd_softc *)periph->softc;
439
440	/*
441	 * In the queued, non-active case, the device in question
442	 * has already been removed from the changer run queue.  Since this
443	 * device is active, we need to de-activate it, and schedule
444	 * another device to run.  (if there is another one to run)
445	 */
446	if ((softc->flags & CD_FLAG_CHANGER)
447	 && (softc->flags & CD_FLAG_ACTIVE)) {
448
449		/*
450		 * The purpose of the short timeout is soley to determine
451		 * whether the current device has finished or not.  Well,
452		 * since we're removing the active device, we know that it
453		 * is finished.  So, get rid of the short timeout.
454		 * Otherwise, if we're in the time period before the short
455		 * timeout fires, and there are no other devices in the
456		 * queue to run, there won't be any other device put in the
457		 * active slot.  i.e., when we call cdrunchangerqueue()
458		 * below, it won't do anything.  Then, when the short
459		 * timeout fires, it'll look at the "current device", which
460		 * we are free below, and possibly panic the kernel on a
461		 * bogus pointer reference.
462		 *
463		 * The long timeout doesn't really matter, since we
464		 * decrement the qfrozen_cnt to indicate that there is
465		 * nothing in the active slot now.  Therefore, there won't
466		 * be any bogus pointer references there.
467		 */
468		if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
469			callout_stop(&softc->changer->short_handle);
470			softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
471		}
472		softc->changer->devq.qfrozen_cnt--;
473		softc->changer->flags |= CHANGER_MANUAL_CALL;
474		cdrunchangerqueue(softc->changer);
475	}
476
477	/*
478	 * If we're removing the last device on the changer, go ahead and
479	 * remove the changer device structure.
480	 */
481	if ((softc->flags & CD_FLAG_CHANGER)
482	 && (--softc->changer->num_devices == 0)) {
483
484		/*
485		 * Theoretically, there shouldn't be any timeouts left, but
486		 * I'm not completely sure that that will be the case.  So,
487		 * it won't hurt to check and see if there are any left.
488		 */
489		if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
490			callout_stop(&softc->changer->long_handle);
491			softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
492		}
493
494		if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
495			callout_stop(&softc->changer->short_handle);
496			softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
497		}
498
499		mtx_lock(&changerq_mtx);
500		STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
501			      changer_links);
502		num_changers--;
503		mtx_unlock(&changerq_mtx);
504		xpt_print(periph->path, "removing changer entry\n");
505		free(softc->changer, M_DEVBUF);
506	}
507	cam_periph_unlock(periph);
508	if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
509	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
510		xpt_print(periph->path, "can't remove sysctl context\n");
511	}
512
513	callout_drain(&softc->mediapoll_c);
514	disk_destroy(softc->disk);
515	free(softc, M_DEVBUF);
516	cam_periph_lock(periph);
517}
518
519static void
520cdasync(void *callback_arg, u_int32_t code,
521	struct cam_path *path, void *arg)
522{
523	struct cam_periph *periph;
524	struct cd_softc *softc;
525
526	periph = (struct cam_periph *)callback_arg;
527	switch (code) {
528	case AC_FOUND_DEVICE:
529	{
530		struct ccb_getdev *cgd;
531		cam_status status;
532
533		cgd = (struct ccb_getdev *)arg;
534		if (cgd == NULL)
535			break;
536
537		if (cgd->protocol != PROTO_SCSI)
538			break;
539
540		if (SID_TYPE(&cgd->inq_data) != T_CDROM
541		    && SID_TYPE(&cgd->inq_data) != T_WORM)
542			break;
543
544		/*
545		 * Allocate a peripheral instance for
546		 * this device and start the probe
547		 * process.
548		 */
549		status = cam_periph_alloc(cdregister, cdoninvalidate,
550					  cdcleanup, cdstart,
551					  "cd", CAM_PERIPH_BIO,
552					  cgd->ccb_h.path, cdasync,
553					  AC_FOUND_DEVICE, cgd);
554
555		if (status != CAM_REQ_CMP
556		 && status != CAM_REQ_INPROG)
557			printf("cdasync: Unable to attach new device "
558			       "due to status 0x%x\n", status);
559
560		break;
561	}
562	case AC_UNIT_ATTENTION:
563	{
564		union ccb *ccb;
565		int error_code, sense_key, asc, ascq;
566
567		softc = (struct cd_softc *)periph->softc;
568		ccb = (union ccb *)arg;
569
570		/*
571		 * Handle all media change UNIT ATTENTIONs except
572		 * our own, as they will be handled by cderror().
573		 */
574		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
575		    scsi_extract_sense_ccb(ccb,
576		     &error_code, &sense_key, &asc, &ascq)) {
577			if (asc == 0x28 && ascq == 0x00)
578				disk_media_changed(softc->disk, M_NOWAIT);
579		}
580		cam_periph_async(periph, code, path, arg);
581		break;
582	}
583	case AC_SCSI_AEN:
584		softc = (struct cd_softc *)periph->softc;
585		if (softc->state == CD_STATE_NORMAL && !softc->tur) {
586			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
587				softc->tur = 1;
588				xpt_schedule(periph, CAM_PRIORITY_NORMAL);
589			}
590		}
591		/* FALLTHROUGH */
592	case AC_SENT_BDR:
593	case AC_BUS_RESET:
594	{
595		struct ccb_hdr *ccbh;
596
597		softc = (struct cd_softc *)periph->softc;
598		/*
599		 * Don't fail on the expected unit attention
600		 * that will occur.
601		 */
602		softc->flags |= CD_FLAG_RETRY_UA;
603		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
604			ccbh->ccb_state |= CD_CCB_RETRY_UA;
605		/* FALLTHROUGH */
606	}
607	default:
608		cam_periph_async(periph, code, path, arg);
609		break;
610	}
611}
612
613static void
614cdsysctlinit(void *context, int pending)
615{
616	struct cam_periph *periph;
617	struct cd_softc *softc;
618	char tmpstr[80], tmpstr2[80];
619
620	periph = (struct cam_periph *)context;
621	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
622		return;
623
624	softc = (struct cd_softc *)periph->softc;
625	snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
626	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
627
628	sysctl_ctx_init(&softc->sysctl_ctx);
629	softc->flags |= CD_FLAG_SCTX_INIT;
630	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
631		SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
632		tmpstr2, CTLFLAG_RD, 0, tmpstr);
633
634	if (softc->sysctl_tree == NULL) {
635		printf("cdsysctlinit: unable to allocate sysctl tree\n");
636		cam_periph_release(periph);
637		return;
638	}
639
640	/*
641	 * Now register the sysctl handler, so the user can the value on
642	 * the fly.
643	 */
644	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
645		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
646		&softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
647		"Minimum CDB size");
648
649	cam_periph_release(periph);
650}
651
652/*
653 * We have a handler function for this so we can check the values when the
654 * user sets them, instead of every time we look at them.
655 */
656static int
657cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
658{
659	int error, value;
660
661	value = *(int *)arg1;
662
663	error = sysctl_handle_int(oidp, &value, 0, req);
664
665	if ((error != 0)
666	 || (req->newptr == NULL))
667		return (error);
668
669	/*
670	 * The only real values we can have here are 6 or 10.  I don't
671	 * really forsee having 12 be an option at any time in the future.
672	 * So if the user sets something less than or equal to 6, we'll set
673	 * it to 6.  If he sets something greater than 6, we'll set it to 10.
674	 *
675	 * I suppose we could just return an error here for the wrong values,
676	 * but I don't think it's necessary to do so, as long as we can
677	 * determine the user's intent without too much trouble.
678	 */
679	if (value < 6)
680		value = 6;
681	else if (value > 6)
682		value = 10;
683
684	*(int *)arg1 = value;
685
686	return (0);
687}
688
689static cam_status
690cdregister(struct cam_periph *periph, void *arg)
691{
692	struct cd_softc *softc;
693	struct ccb_pathinq cpi;
694	struct ccb_getdev *cgd;
695	char tmpstr[80];
696	caddr_t match;
697
698	cgd = (struct ccb_getdev *)arg;
699	if (cgd == NULL) {
700		printf("cdregister: no getdev CCB, can't register device\n");
701		return(CAM_REQ_CMP_ERR);
702	}
703
704	softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,
705	    M_NOWAIT | M_ZERO);
706	if (softc == NULL) {
707		printf("cdregister: Unable to probe new device. "
708		       "Unable to allocate softc\n");
709		return(CAM_REQ_CMP_ERR);
710	}
711
712	LIST_INIT(&softc->pending_ccbs);
713	STAILQ_INIT(&softc->mode_queue);
714	softc->state = CD_STATE_PROBE;
715	bioq_init(&softc->bio_queue);
716	if (SID_IS_REMOVABLE(&cgd->inq_data))
717		softc->flags |= CD_FLAG_DISC_REMOVABLE;
718
719	periph->softc = softc;
720	softc->periph = periph;
721
722	/*
723	 * See if this device has any quirks.
724	 */
725	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
726			       (caddr_t)cd_quirk_table,
727			       sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
728			       sizeof(*cd_quirk_table), scsi_inquiry_match);
729
730	if (match != NULL)
731		softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
732	else
733		softc->quirks = CD_Q_NONE;
734
735	/* Check if the SIM does not want 6 byte commands */
736	bzero(&cpi, sizeof(cpi));
737	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
738	cpi.ccb_h.func_code = XPT_PATH_INQ;
739	xpt_action((union ccb *)&cpi);
740	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
741		softc->quirks |= CD_Q_10_BYTE_ONLY;
742
743	TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
744
745	/* The default is 6 byte commands, unless quirked otherwise */
746	if (softc->quirks & CD_Q_10_BYTE_ONLY)
747		softc->minimum_command_size = 10;
748	else
749		softc->minimum_command_size = 6;
750
751	/*
752	 * Refcount and block open attempts until we are setup
753	 * Can't block
754	 */
755	(void)cam_periph_hold(periph, PRIBIO);
756	cam_periph_unlock(periph);
757	/*
758	 * Load the user's default, if any.
759	 */
760	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
761		 periph->unit_number);
762	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
763
764	/* 6 and 10 are the only permissible values here. */
765	if (softc->minimum_command_size < 6)
766		softc->minimum_command_size = 6;
767	else if (softc->minimum_command_size > 6)
768		softc->minimum_command_size = 10;
769
770	/*
771	 * We need to register the statistics structure for this device,
772	 * but we don't have the blocksize yet for it.  So, we register
773	 * the structure and indicate that we don't have the blocksize
774	 * yet.  Unlike other SCSI peripheral drivers, we explicitly set
775	 * the device type here to be CDROM, rather than just ORing in
776	 * the device type.  This is because this driver can attach to either
777	 * CDROM or WORM devices, and we want this peripheral driver to
778	 * show up in the devstat list as a CD peripheral driver, not a
779	 * WORM peripheral driver.  WORM drives will also have the WORM
780	 * driver attached to them.
781	 */
782	softc->disk = disk_alloc();
783	softc->disk->d_devstat = devstat_new_entry("cd",
784			  periph->unit_number, 0,
785			  DEVSTAT_BS_UNAVAILABLE,
786			  DEVSTAT_TYPE_CDROM |
787			  XPORT_DEVSTAT_TYPE(cpi.transport),
788			  DEVSTAT_PRIORITY_CD);
789	softc->disk->d_open = cdopen;
790	softc->disk->d_close = cdclose;
791	softc->disk->d_strategy = cdstrategy;
792	softc->disk->d_gone = cddiskgonecb;
793	softc->disk->d_ioctl = cdioctl;
794	softc->disk->d_name = "cd";
795	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
796	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
797	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
798	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
799	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
800	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
801	softc->disk->d_unit = periph->unit_number;
802	softc->disk->d_drv1 = periph;
803	if (cpi.maxio == 0)
804		softc->disk->d_maxsize = DFLTPHYS;	/* traditional default */
805	else if (cpi.maxio > MAXPHYS)
806		softc->disk->d_maxsize = MAXPHYS;	/* for safety */
807	else
808		softc->disk->d_maxsize = cpi.maxio;
809	softc->disk->d_flags = 0;
810	softc->disk->d_hba_vendor = cpi.hba_vendor;
811	softc->disk->d_hba_device = cpi.hba_device;
812	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
813	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
814
815	/*
816	 * Acquire a reference to the periph before we register with GEOM.
817	 * We'll release this reference once GEOM calls us back (via
818	 * dadiskgonecb()) telling us that our provider has been freed.
819	 */
820	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
821		xpt_print(periph->path, "%s: lost periph during "
822			  "registration!\n", __func__);
823		cam_periph_lock(periph);
824		return (CAM_REQ_CMP_ERR);
825	}
826
827	disk_create(softc->disk, DISK_VERSION);
828	cam_periph_lock(periph);
829
830	/*
831	 * Add an async callback so that we get
832	 * notified if this device goes away.
833	 */
834	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
835	    AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path);
836
837	/*
838	 * If the target lun is greater than 0, we most likely have a CD
839	 * changer device.  Check the quirk entries as well, though, just
840	 * in case someone has a CD tower with one lun per drive or
841	 * something like that.  Also, if we know up front that a
842	 * particular device is a changer, we can mark it as such starting
843	 * with lun 0, instead of lun 1.  It shouldn't be necessary to have
844	 * a quirk entry to define something as a changer, however.
845	 */
846	if (((cgd->ccb_h.target_lun > 0)
847	  && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
848	 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
849		struct cdchanger *nchanger;
850		struct cam_periph *nperiph;
851		struct cam_path *path;
852		cam_status status;
853		int found;
854
855		/* Set the changer flag in the current device's softc */
856		softc->flags |= CD_FLAG_CHANGER;
857
858		/*
859		 * Now, look around for an existing changer device with the
860		 * same path and target ID as the current device.
861		 */
862		mtx_lock(&changerq_mtx);
863		for (found = 0,
864		     nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
865		     nchanger != NULL;
866		     nchanger = STAILQ_NEXT(nchanger, changer_links)){
867			if ((nchanger->path_id == cgd->ccb_h.path_id)
868			 && (nchanger->target_id == cgd->ccb_h.target_id)) {
869				found = 1;
870				break;
871			}
872		}
873		mtx_unlock(&changerq_mtx);
874
875		/*
876		 * If we found a matching entry, just add this device to
877		 * the list of devices on this changer.
878		 */
879		if (found == 1) {
880			struct chdevlist *chlunhead;
881
882			chlunhead = &nchanger->chluns;
883
884			/*
885			 * XXX KDM look at consolidating this code with the
886			 * code below in a separate function.
887			 */
888
889			/*
890			 * Create a path with lun id 0, and see if we can
891			 * find a matching device
892			 */
893			status = xpt_create_path(&path, /*periph*/ periph,
894						 cgd->ccb_h.path_id,
895						 cgd->ccb_h.target_id, 0);
896
897			if ((status == CAM_REQ_CMP)
898			 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
899				struct cd_softc *nsoftc;
900
901				nsoftc = (struct cd_softc *)nperiph->softc;
902
903				if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
904					nsoftc->flags |= CD_FLAG_CHANGER;
905					nchanger->num_devices++;
906					if (camq_resize(&nchanger->devq,
907					   nchanger->num_devices)!=CAM_REQ_CMP){
908						printf("cdregister: "
909						       "camq_resize "
910						       "failed, changer "
911						       "support may "
912						       "be messed up\n");
913					}
914					nsoftc->changer = nchanger;
915					nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
916
917					STAILQ_INSERT_TAIL(&nchanger->chluns,
918							  nsoftc,changer_links);
919				}
920				xpt_free_path(path);
921			} else if (status == CAM_REQ_CMP)
922				xpt_free_path(path);
923			else {
924				printf("cdregister: unable to allocate path\n"
925				       "cdregister: changer support may be "
926				       "broken\n");
927			}
928
929			nchanger->num_devices++;
930
931			softc->changer = nchanger;
932			softc->pinfo.index = CAM_UNQUEUED_INDEX;
933
934			if (camq_resize(&nchanger->devq,
935			    nchanger->num_devices) != CAM_REQ_CMP) {
936				printf("cdregister: camq_resize "
937				       "failed, changer support may "
938				       "be messed up\n");
939			}
940
941			STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
942		}
943		/*
944		 * In this case, we don't already have an entry for this
945		 * particular changer, so we need to create one, add it to
946		 * the queue, and queue this device on the list for this
947		 * changer.  Before we queue this device, however, we need
948		 * to search for lun id 0 on this target, and add it to the
949		 * queue first, if it exists.  (and if it hasn't already
950		 * been marked as part of the changer.)
951		 */
952		else {
953			nchanger = malloc(sizeof(struct cdchanger),
954				M_DEVBUF, M_NOWAIT | M_ZERO);
955			if (nchanger == NULL) {
956				softc->flags &= ~CD_FLAG_CHANGER;
957				printf("cdregister: unable to malloc "
958				       "changer structure\ncdregister: "
959				       "changer support disabled\n");
960
961				/*
962				 * Yes, gotos can be gross but in this case
963				 * I think it's justified..
964				 */
965				goto cdregisterexit;
966			}
967			if (camq_init(&nchanger->devq, 1) != 0) {
968				softc->flags &= ~CD_FLAG_CHANGER;
969				printf("cdregister: changer support "
970				       "disabled\n");
971				goto cdregisterexit;
972			}
973
974			nchanger->path_id = cgd->ccb_h.path_id;
975			nchanger->target_id = cgd->ccb_h.target_id;
976
977			/* this is superfluous, but it makes things clearer */
978			nchanger->num_devices = 0;
979
980			STAILQ_INIT(&nchanger->chluns);
981
982			callout_init_mtx(&nchanger->long_handle,
983			    periph->sim->mtx, 0);
984			callout_init_mtx(&nchanger->short_handle,
985			    periph->sim->mtx, 0);
986
987			mtx_lock(&changerq_mtx);
988			num_changers++;
989			STAILQ_INSERT_TAIL(&changerq, nchanger,
990					   changer_links);
991			mtx_unlock(&changerq_mtx);
992
993			/*
994			 * Create a path with lun id 0, and see if we can
995			 * find a matching device
996			 */
997			status = xpt_create_path(&path, /*periph*/ periph,
998						 cgd->ccb_h.path_id,
999						 cgd->ccb_h.target_id, 0);
1000
1001			/*
1002			 * If we were able to allocate the path, and if we
1003			 * find a matching device and it isn't already
1004			 * marked as part of a changer, then we add it to
1005			 * the current changer.
1006			 */
1007			if ((status == CAM_REQ_CMP)
1008			 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
1009			 && ((((struct cd_softc *)periph->softc)->flags &
1010			       CD_FLAG_CHANGER) == 0)) {
1011				struct cd_softc *nsoftc;
1012
1013				nsoftc = (struct cd_softc *)nperiph->softc;
1014
1015				nsoftc->flags |= CD_FLAG_CHANGER;
1016				nchanger->num_devices++;
1017				if (camq_resize(&nchanger->devq,
1018				    nchanger->num_devices) != CAM_REQ_CMP) {
1019					printf("cdregister: camq_resize "
1020					       "failed, changer support may "
1021					       "be messed up\n");
1022				}
1023				nsoftc->changer = nchanger;
1024				nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
1025
1026				STAILQ_INSERT_TAIL(&nchanger->chluns,
1027						   nsoftc, changer_links);
1028				xpt_free_path(path);
1029			} else if (status == CAM_REQ_CMP)
1030				xpt_free_path(path);
1031			else {
1032				printf("cdregister: unable to allocate path\n"
1033				       "cdregister: changer support may be "
1034				       "broken\n");
1035			}
1036
1037			softc->changer = nchanger;
1038			softc->pinfo.index = CAM_UNQUEUED_INDEX;
1039			nchanger->num_devices++;
1040			if (camq_resize(&nchanger->devq,
1041			    nchanger->num_devices) != CAM_REQ_CMP) {
1042				printf("cdregister: camq_resize "
1043				       "failed, changer support may "
1044				       "be messed up\n");
1045			}
1046			STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
1047					   changer_links);
1048		}
1049	}
1050
1051	/*
1052	 * Schedule a periodic media polling events.
1053	 */
1054	callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1055	if ((softc->flags & CD_FLAG_DISC_REMOVABLE) &&
1056	    (softc->flags & CD_FLAG_CHANGER) == 0 &&
1057	    (cgd->inq_flags & SID_AEN) == 0 &&
1058	    cd_poll_period != 0)
1059		callout_reset(&softc->mediapoll_c, cd_poll_period * hz,
1060		    cdmediapoll, periph);
1061
1062cdregisterexit:
1063
1064	if ((softc->flags & CD_FLAG_CHANGER) == 0)
1065		xpt_schedule(periph, CAM_PRIORITY_DEV);
1066	else
1067		cdschedule(periph, CAM_PRIORITY_DEV);
1068
1069	return(CAM_REQ_CMP);
1070}
1071
1072static int
1073cdopen(struct disk *dp)
1074{
1075	struct cam_periph *periph;
1076	struct cd_softc *softc;
1077	int error;
1078
1079	periph = (struct cam_periph *)dp->d_drv1;
1080	softc = (struct cd_softc *)periph->softc;
1081
1082	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1083		return(ENXIO);
1084
1085	cam_periph_lock(periph);
1086
1087	if (softc->flags & CD_FLAG_INVALID) {
1088		cam_periph_release_locked(periph);
1089		cam_periph_unlock(periph);
1090		return(ENXIO);
1091	}
1092
1093	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1094		cam_periph_release_locked(periph);
1095		cam_periph_unlock(periph);
1096		return (error);
1097	}
1098
1099	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1100	    ("cdopen\n"));
1101
1102	/*
1103	 * Check for media, and set the appropriate flags.  We don't bail
1104	 * if we don't have media, but then we don't allow anything but the
1105	 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1106	 */
1107	cdcheckmedia(periph);
1108
1109	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1110	cam_periph_unhold(periph);
1111
1112	cam_periph_unlock(periph);
1113
1114	return (0);
1115}
1116
1117static int
1118cdclose(struct disk *dp)
1119{
1120	struct 	cam_periph *periph;
1121	struct	cd_softc *softc;
1122
1123	periph = (struct cam_periph *)dp->d_drv1;
1124	softc = (struct cd_softc *)periph->softc;
1125
1126	cam_periph_lock(periph);
1127	if (cam_periph_hold(periph, PRIBIO) != 0) {
1128		cam_periph_unlock(periph);
1129		cam_periph_release(periph);
1130		return (0);
1131	}
1132
1133	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1134	    ("cdclose\n"));
1135
1136	if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1137		cdprevent(periph, PR_ALLOW);
1138
1139	/*
1140	 * Since we're closing this CD, mark the blocksize as unavailable.
1141	 * It will be marked as available when the CD is opened again.
1142	 */
1143	softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1144
1145	/*
1146	 * We'll check the media and toc again at the next open().
1147	 */
1148	softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
1149
1150	cam_periph_unhold(periph);
1151	cam_periph_release_locked(periph);
1152	cam_periph_unlock(periph);
1153
1154	return (0);
1155}
1156
1157static void
1158cdshorttimeout(void *arg)
1159{
1160	struct cdchanger *changer;
1161
1162	changer = (struct cdchanger *)arg;
1163
1164	/* Always clear the short timeout flag, since that's what we're in */
1165	changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1166
1167	/*
1168	 * Check to see if there is any more pending or outstanding I/O for
1169	 * this device.  If not, move it out of the active slot.
1170	 */
1171	if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1172	 && (changer->cur_device->outstanding_cmds == 0)) {
1173		changer->flags |= CHANGER_MANUAL_CALL;
1174		cdrunchangerqueue(changer);
1175	}
1176}
1177
1178/*
1179 * This is a wrapper for xpt_schedule.  It only applies to changers.
1180 */
1181static void
1182cdschedule(struct cam_periph *periph, int priority)
1183{
1184	struct cd_softc *softc;
1185
1186	softc = (struct cd_softc *)periph->softc;
1187
1188	/*
1189	 * If this device isn't currently queued, and if it isn't
1190	 * the active device, then we queue this device and run the
1191	 * changer queue if there is no timeout scheduled to do it.
1192	 * If this device is the active device, just schedule it
1193	 * to run again.  If this device is queued, there should be
1194	 * a timeout in place already that will make sure it runs.
1195	 */
1196	if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1197	 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1198		/*
1199		 * We don't do anything with the priority here.
1200		 * This is strictly a fifo queue.
1201		 */
1202		softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1203		softc->pinfo.generation = ++softc->changer->devq.generation;
1204		camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1205
1206		/*
1207		 * Since we just put a device in the changer queue,
1208		 * check and see if there is a timeout scheduled for
1209		 * this changer.  If so, let the timeout handle
1210		 * switching this device into the active slot.  If
1211		 * not, manually call the timeout routine to
1212		 * bootstrap things.
1213		 */
1214		if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1215		 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1216		 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1217			softc->changer->flags |= CHANGER_MANUAL_CALL;
1218			cdrunchangerqueue(softc->changer);
1219		}
1220	} else if ((softc->flags & CD_FLAG_ACTIVE)
1221		&& ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1222		xpt_schedule(periph, priority);
1223}
1224
1225static void
1226cdrunchangerqueue(void *arg)
1227{
1228	struct cd_softc *softc;
1229	struct cdchanger *changer;
1230	int called_from_timeout;
1231
1232	changer = (struct cdchanger *)arg;
1233
1234	/*
1235	 * If we have NOT been called from cdstrategy() or cddone(), and
1236	 * instead from a timeout routine, go ahead and clear the
1237	 * timeout flag.
1238	 */
1239	if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1240		changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1241		called_from_timeout = 1;
1242	} else
1243		called_from_timeout = 0;
1244
1245	/* Always clear the manual call flag */
1246	changer->flags &= ~CHANGER_MANUAL_CALL;
1247
1248	/* nothing to do if the queue is empty */
1249	if (changer->devq.entries <= 0) {
1250		return;
1251	}
1252
1253	/*
1254	 * If the changer queue is frozen, that means we have an active
1255	 * device.
1256	 */
1257	if (changer->devq.qfrozen_cnt > 0) {
1258
1259		/*
1260		 * We always need to reset the frozen count and clear the
1261		 * active flag.
1262		 */
1263		changer->devq.qfrozen_cnt--;
1264		changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1265		changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1266
1267		if (changer->cur_device->outstanding_cmds > 0) {
1268			changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1269			changer->cur_device->bufs_left =
1270				changer->cur_device->outstanding_cmds;
1271			if (called_from_timeout) {
1272				callout_reset(&changer->long_handle,
1273			            changer_max_busy_seconds * hz,
1274				    cdrunchangerqueue, changer);
1275				changer->flags |= CHANGER_TIMEOUT_SCHED;
1276			}
1277			return;
1278		}
1279
1280		/*
1281		 * Check to see whether the current device has any I/O left
1282		 * to do.  If so, requeue it at the end of the queue.  If
1283		 * not, there is no need to requeue it.
1284		 */
1285		if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1286
1287			changer->cur_device->pinfo.generation =
1288				++changer->devq.generation;
1289			camq_insert(&changer->devq,
1290				(cam_pinfo *)changer->cur_device);
1291		}
1292	}
1293
1294	softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1295
1296	changer->cur_device = softc;
1297
1298	changer->devq.qfrozen_cnt++;
1299	softc->flags |= CD_FLAG_ACTIVE;
1300
1301	/* Just in case this device is waiting */
1302	wakeup(&softc->changer);
1303	xpt_schedule(softc->periph, CAM_PRIORITY_NORMAL);
1304
1305	/*
1306	 * Get rid of any pending timeouts, and set a flag to schedule new
1307	 * ones so this device gets its full time quantum.
1308	 */
1309	if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1310		callout_stop(&changer->long_handle);
1311		changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1312	}
1313
1314	if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1315		callout_stop(&changer->short_handle);
1316		changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1317	}
1318
1319	/*
1320	 * We need to schedule timeouts, but we only do this after the
1321	 * first transaction has completed.  This eliminates the changer
1322	 * switch time.
1323	 */
1324	changer->flags |= CHANGER_NEED_TIMEOUT;
1325}
1326
1327static void
1328cdchangerschedule(struct cd_softc *softc)
1329{
1330	struct cdchanger *changer;
1331
1332	changer = softc->changer;
1333
1334	/*
1335	 * If this is a changer, and this is the current device,
1336	 * and this device has at least the minimum time quantum to
1337	 * run, see if we can switch it out.
1338	 */
1339	if ((softc->flags & CD_FLAG_ACTIVE)
1340	 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1341	 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1342		/*
1343		 * We try three things here.  The first is that we
1344		 * check to see whether the schedule on completion
1345		 * flag is set.  If it is, we decrement the number
1346		 * of buffers left, and if it's zero, we reschedule.
1347		 * Next, we check to see whether the pending buffer
1348		 * queue is empty and whether there are no
1349		 * outstanding transactions.  If so, we reschedule.
1350		 * Next, we see if the pending buffer queue is empty.
1351		 * If it is, we set the number of buffers left to
1352		 * the current active buffer count and set the
1353		 * schedule on complete flag.
1354		 */
1355		if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1356		 	if (--softc->bufs_left == 0) {
1357				softc->changer->flags |=
1358					CHANGER_MANUAL_CALL;
1359				softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1360				cdrunchangerqueue(softc->changer);
1361			}
1362		} else if ((bioq_first(&softc->bio_queue) == NULL)
1363		        && (softc->outstanding_cmds == 0)) {
1364			softc->changer->flags |= CHANGER_MANUAL_CALL;
1365			cdrunchangerqueue(softc->changer);
1366		}
1367	} else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1368		&& (softc->flags & CD_FLAG_ACTIVE)) {
1369
1370		/*
1371		 * Now that the first transaction to this
1372		 * particular device has completed, we can go ahead
1373		 * and schedule our timeouts.
1374		 */
1375		if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1376			callout_reset(&changer->long_handle,
1377			    changer_max_busy_seconds * hz,
1378			    cdrunchangerqueue, changer);
1379			changer->flags |= CHANGER_TIMEOUT_SCHED;
1380		} else
1381			printf("cdchangerschedule: already have a long"
1382			       " timeout!\n");
1383
1384		if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1385			callout_reset(&changer->short_handle,
1386			    changer_min_busy_seconds * hz,
1387			    cdshorttimeout, changer);
1388			changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1389		} else
1390			printf("cdchangerschedule: already have a short "
1391			       "timeout!\n");
1392
1393		/*
1394		 * We just scheduled timeouts, no need to schedule
1395		 * more.
1396		 */
1397		changer->flags &= ~CHANGER_NEED_TIMEOUT;
1398
1399	}
1400}
1401
1402static int
1403cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1404					      u_int32_t cam_flags,
1405					      u_int32_t sense_flags),
1406	 u_int32_t cam_flags, u_int32_t sense_flags)
1407{
1408	struct cd_softc *softc;
1409	struct cam_periph *periph;
1410	int error;
1411
1412	periph = xpt_path_periph(ccb->ccb_h.path);
1413	softc = (struct cd_softc *)periph->softc;
1414
1415	error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1416				  softc->disk->d_devstat);
1417
1418	if (softc->flags & CD_FLAG_CHANGER)
1419		cdchangerschedule(softc);
1420
1421	return(error);
1422}
1423
1424static union ccb *
1425cdgetccb(struct cam_periph *periph, u_int32_t priority)
1426{
1427	struct cd_softc *softc;
1428
1429	softc = (struct cd_softc *)periph->softc;
1430
1431	if (softc->flags & CD_FLAG_CHANGER) {
1432		/*
1433		 * This should work the first time this device is woken up,
1434		 * but just in case it doesn't, we use a while loop.
1435		 */
1436		while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1437			/*
1438			 * If this changer isn't already queued, queue it up.
1439			 */
1440			if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1441				softc->pinfo.priority = CAM_PRIORITY_NORMAL;
1442				softc->pinfo.generation =
1443					++softc->changer->devq.generation;
1444				camq_insert(&softc->changer->devq,
1445					    (cam_pinfo *)softc);
1446			}
1447			if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1448			 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1449			 && ((softc->changer->flags
1450			      & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1451				softc->changer->flags |= CHANGER_MANUAL_CALL;
1452				cdrunchangerqueue(softc->changer);
1453			} else
1454				cam_periph_sleep(periph, &softc->changer,
1455				    PRIBIO, "cgticb", 0);
1456		}
1457	}
1458	return(cam_periph_getccb(periph, priority));
1459}
1460
1461
1462/*
1463 * Actually translate the requested transfer into one the physical driver
1464 * can understand.  The transfer is described by a buf and will include
1465 * only one physical transfer.
1466 */
1467static void
1468cdstrategy(struct bio *bp)
1469{
1470	struct cam_periph *periph;
1471	struct cd_softc *softc;
1472
1473	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1474	cam_periph_lock(periph);
1475	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1476	    ("cdstrategy(%p)\n", bp));
1477
1478	softc = (struct cd_softc *)periph->softc;
1479
1480	/*
1481	 * If the device has been made invalid, error out
1482	 */
1483	if ((softc->flags & CD_FLAG_INVALID)) {
1484		cam_periph_unlock(periph);
1485		biofinish(bp, NULL, ENXIO);
1486		return;
1487	}
1488
1489        /*
1490	 * If we don't have valid media, look for it before trying to
1491	 * schedule the I/O.
1492	 */
1493	if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1494		int error;
1495
1496		error = cdcheckmedia(periph);
1497		if (error != 0) {
1498			cam_periph_unlock(periph);
1499			biofinish(bp, NULL, error);
1500			return;
1501		}
1502	}
1503
1504	/*
1505	 * Place it in the queue of disk activities for this disk
1506	 */
1507	bioq_disksort(&softc->bio_queue, bp);
1508
1509	/*
1510	 * Schedule ourselves for performing the work.  We do things
1511	 * differently for changers.
1512	 */
1513	if ((softc->flags & CD_FLAG_CHANGER) == 0)
1514		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1515	else
1516		cdschedule(periph, CAM_PRIORITY_NORMAL);
1517
1518	cam_periph_unlock(periph);
1519	return;
1520}
1521
1522static void
1523cdstart(struct cam_periph *periph, union ccb *start_ccb)
1524{
1525	struct cd_softc *softc;
1526	struct bio *bp;
1527	struct ccb_scsiio *csio;
1528	struct scsi_read_capacity_data *rcap;
1529
1530	softc = (struct cd_softc *)periph->softc;
1531
1532	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1533
1534	switch (softc->state) {
1535	case CD_STATE_NORMAL:
1536	{
1537		bp = bioq_first(&softc->bio_queue);
1538		if (periph->immediate_priority <= periph->pinfo.priority) {
1539			start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1540
1541			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1542					  periph_links.sle);
1543			periph->immediate_priority = CAM_PRIORITY_NONE;
1544			wakeup(&periph->ccb_list);
1545		} else if (bp == NULL) {
1546			if (softc->tur) {
1547				softc->tur = 0;
1548				csio = &start_ccb->csio;
1549				scsi_test_unit_ready(csio,
1550				     /*retries*/ cd_retry_count,
1551				     cddone,
1552				     MSG_SIMPLE_Q_TAG,
1553				     SSD_FULL_SIZE,
1554				     cd_timeout);
1555				start_ccb->ccb_h.ccb_bp = NULL;
1556				start_ccb->ccb_h.ccb_state = CD_CCB_TUR;
1557				xpt_action(start_ccb);
1558			} else
1559				xpt_release_ccb(start_ccb);
1560		} else {
1561			if (softc->tur) {
1562				softc->tur = 0;
1563				cam_periph_release_locked(periph);
1564			}
1565			bioq_remove(&softc->bio_queue, bp);
1566
1567			scsi_read_write(&start_ccb->csio,
1568					/*retries*/ cd_retry_count,
1569					/* cbfcnp */ cddone,
1570					MSG_SIMPLE_Q_TAG,
1571					/* read */bp->bio_cmd == BIO_READ ?
1572					SCSI_RW_READ : SCSI_RW_WRITE,
1573					/* byte2 */ 0,
1574					/* minimum_cmd_size */ 10,
1575					/* lba */ bp->bio_offset /
1576					  softc->params.blksize,
1577					bp->bio_bcount / softc->params.blksize,
1578					/* data_ptr */ bp->bio_data,
1579					/* dxfer_len */ bp->bio_bcount,
1580					/* sense_len */ cd_retry_count ?
1581					  SSD_FULL_SIZE : SF_NO_PRINT,
1582					/* timeout */ cd_timeout);
1583			/* Use READ CD command for audio tracks. */
1584			if (softc->params.blksize == 2352) {
1585				start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD;
1586				start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8;
1587				start_ccb->csio.cdb_io.cdb_bytes[10] = 0;
1588				start_ccb->csio.cdb_io.cdb_bytes[11] = 0;
1589				start_ccb->csio.cdb_len = 12;
1590			}
1591			start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1592
1593
1594			LIST_INSERT_HEAD(&softc->pending_ccbs,
1595					 &start_ccb->ccb_h, periph_links.le);
1596			softc->outstanding_cmds++;
1597
1598			/* We expect a unit attention from this device */
1599			if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1600				start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1601				softc->flags &= ~CD_FLAG_RETRY_UA;
1602			}
1603
1604			start_ccb->ccb_h.ccb_bp = bp;
1605			bp = bioq_first(&softc->bio_queue);
1606
1607			xpt_action(start_ccb);
1608		}
1609		if (bp != NULL || softc->tur ||
1610		    periph->immediate_priority != CAM_PRIORITY_NONE) {
1611			/* Have more work to do, so ensure we stay scheduled */
1612			xpt_schedule(periph, min(CAM_PRIORITY_NORMAL,
1613			    periph->immediate_priority));
1614		}
1615		break;
1616	}
1617	case CD_STATE_PROBE:
1618	{
1619
1620		rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1621		    M_SCSICD, M_NOWAIT | M_ZERO);
1622		if (rcap == NULL) {
1623			xpt_print(periph->path,
1624			    "cdstart: Couldn't malloc read_capacity data\n");
1625			/* cd_free_periph??? */
1626			break;
1627		}
1628		csio = &start_ccb->csio;
1629		scsi_read_capacity(csio,
1630				   /*retries*/ cd_retry_count,
1631				   cddone,
1632				   MSG_SIMPLE_Q_TAG,
1633				   rcap,
1634				   SSD_FULL_SIZE,
1635				   /*timeout*/20000);
1636		start_ccb->ccb_h.ccb_bp = NULL;
1637		start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1638		xpt_action(start_ccb);
1639		break;
1640	}
1641	}
1642}
1643
1644static void
1645cddone(struct cam_periph *periph, union ccb *done_ccb)
1646{
1647	struct cd_softc *softc;
1648	struct ccb_scsiio *csio;
1649
1650	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1651
1652	softc = (struct cd_softc *)periph->softc;
1653	csio = &done_ccb->csio;
1654
1655	switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1656	case CD_CCB_BUFFER_IO:
1657	{
1658		struct bio	*bp;
1659		int		error;
1660
1661		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1662		error = 0;
1663
1664		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1665			int sf;
1666
1667			if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1668				sf = SF_RETRY_UA;
1669			else
1670				sf = 0;
1671
1672			error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1673			if (error == ERESTART) {
1674				/*
1675				 * A retry was scheuled, so
1676				 * just return.
1677				 */
1678				return;
1679			}
1680		}
1681
1682		if (error != 0) {
1683			xpt_print(periph->path,
1684			    "cddone: got error %#x back\n", error);
1685			bioq_flush(&softc->bio_queue, NULL, EIO);
1686			bp->bio_resid = bp->bio_bcount;
1687			bp->bio_error = error;
1688			bp->bio_flags |= BIO_ERROR;
1689			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1690				cam_release_devq(done_ccb->ccb_h.path,
1691					 /*relsim_flags*/0,
1692					 /*reduction*/0,
1693					 /*timeout*/0,
1694					 /*getcount_only*/0);
1695
1696		} else {
1697			bp->bio_resid = csio->resid;
1698			bp->bio_error = 0;
1699			if (bp->bio_resid != 0) {
1700				/*
1701				 * Short transfer ???
1702				 * XXX: not sure this is correct for partial
1703				 * transfers at EOM
1704				 */
1705				bp->bio_flags |= BIO_ERROR;
1706			}
1707		}
1708
1709		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1710		softc->outstanding_cmds--;
1711
1712		if (softc->flags & CD_FLAG_CHANGER)
1713			cdchangerschedule(softc);
1714
1715		biofinish(bp, NULL, 0);
1716		break;
1717	}
1718	case CD_CCB_PROBE:
1719	{
1720		struct	   scsi_read_capacity_data *rdcap;
1721		char	   announce_buf[120]; /*
1722					       * Currently (9/30/97) the
1723					       * longest possible announce
1724					       * buffer is 108 bytes, for the
1725					       * first error case below.
1726					       * That is 39 bytes for the
1727					       * basic string, 16 bytes for the
1728					       * biggest sense key (hardware
1729					       * error), 52 bytes for the
1730					       * text of the largest sense
1731					       * qualifier valid for a CDROM,
1732					       * (0x72, 0x03 or 0x04,
1733					       * 0x03), and one byte for the
1734					       * null terminating character.
1735					       * To allow for longer strings,
1736					       * the announce buffer is 120
1737					       * bytes.
1738					       */
1739		struct	   cd_params *cdp;
1740		int error;
1741
1742		cdp = &softc->params;
1743
1744		rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1745
1746		cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1747		cdp->blksize = scsi_4btoul (rdcap->length);
1748
1749		/*
1750		 * Retry any UNIT ATTENTION type errors.  They
1751		 * are expected at boot.
1752		 */
1753		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP ||
1754		    (error = cderror(done_ccb, CAM_RETRY_SELTO,
1755				SF_RETRY_UA | SF_NO_PRINT)) == 0) {
1756
1757			snprintf(announce_buf, sizeof(announce_buf),
1758				"cd present [%lu x %lu byte records]",
1759				cdp->disksize, (u_long)cdp->blksize);
1760
1761		} else {
1762			if (error == ERESTART) {
1763				/*
1764				 * A retry was scheuled, so
1765				 * just return.
1766				 */
1767				return;
1768			} else {
1769				int asc, ascq;
1770				int sense_key, error_code;
1771				int have_sense;
1772				cam_status status;
1773				struct ccb_getdev cgd;
1774
1775				/* Don't wedge this device's queue */
1776				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1777					cam_release_devq(done_ccb->ccb_h.path,
1778						 /*relsim_flags*/0,
1779						 /*reduction*/0,
1780						 /*timeout*/0,
1781						 /*getcount_only*/0);
1782
1783				status = done_ccb->ccb_h.status;
1784
1785				xpt_setup_ccb(&cgd.ccb_h,
1786					      done_ccb->ccb_h.path,
1787					      CAM_PRIORITY_NORMAL);
1788				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1789				xpt_action((union ccb *)&cgd);
1790
1791				if (scsi_extract_sense_ccb(done_ccb,
1792				    &error_code, &sense_key, &asc, &ascq))
1793					have_sense = TRUE;
1794				else
1795					have_sense = FALSE;
1796
1797				/*
1798				 * Attach to anything that claims to be a
1799				 * CDROM or WORM device, as long as it
1800				 * doesn't return a "Logical unit not
1801				 * supported" (0x25) error.
1802				 */
1803				if ((have_sense) && (asc != 0x25)
1804				 && (error_code == SSD_CURRENT_ERROR)) {
1805					const char *sense_key_desc;
1806					const char *asc_desc;
1807
1808					scsi_sense_desc(sense_key, asc, ascq,
1809							&cgd.inq_data,
1810							&sense_key_desc,
1811							&asc_desc);
1812					snprintf(announce_buf,
1813					    sizeof(announce_buf),
1814						"Attempt to query device "
1815						"size failed: %s, %s",
1816						sense_key_desc,
1817						asc_desc);
1818 				} else if ((have_sense == 0)
1819 				      && ((status & CAM_STATUS_MASK) ==
1820 					   CAM_SCSI_STATUS_ERROR)
1821 				      && (csio->scsi_status ==
1822 					  SCSI_STATUS_BUSY)) {
1823 					snprintf(announce_buf,
1824 					    sizeof(announce_buf),
1825 					    "Attempt to query device "
1826 					    "size failed: SCSI Status: %s",
1827					    scsi_status_string(csio));
1828				} else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1829					/*
1830					 * We only print out an error for
1831					 * CDROM type devices.  For WORM
1832					 * devices, we don't print out an
1833					 * error since a few WORM devices
1834					 * don't support CDROM commands.
1835					 * If we have sense information, go
1836					 * ahead and print it out.
1837					 * Otherwise, just say that we
1838					 * couldn't attach.
1839					 */
1840
1841					/*
1842					 * Just print out the error, not
1843					 * the full probe message, when we
1844					 * don't attach.
1845					 */
1846					if (have_sense)
1847						scsi_sense_print(
1848							&done_ccb->csio);
1849					else {
1850						xpt_print(periph->path,
1851						    "got CAM status %#x\n",
1852						    done_ccb->ccb_h.status);
1853					}
1854					xpt_print(periph->path, "fatal error, "
1855					    "failed to attach to device\n");
1856					/*
1857					 * Invalidate this peripheral.
1858					 */
1859					cam_periph_invalidate(periph);
1860
1861					announce_buf[0] = '\0';
1862				} else {
1863
1864					/*
1865					 * Invalidate this peripheral.
1866					 */
1867					cam_periph_invalidate(periph);
1868					announce_buf[0] = '\0';
1869				}
1870			}
1871		}
1872		free(rdcap, M_SCSICD);
1873		if (announce_buf[0] != '\0') {
1874			xpt_announce_periph(periph, announce_buf);
1875			xpt_announce_quirks(periph, softc->quirks,
1876			    CD_Q_BIT_STRING);
1877			if (softc->flags & CD_FLAG_CHANGER)
1878				cdchangerschedule(softc);
1879			/*
1880			 * Create our sysctl variables, now that we know
1881			 * we have successfully attached.
1882			 */
1883			taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1884		}
1885		softc->state = CD_STATE_NORMAL;
1886		/*
1887		 * Since our peripheral may be invalidated by an error
1888		 * above or an external event, we must release our CCB
1889		 * before releasing the probe lock on the peripheral.
1890		 * The peripheral will only go away once the last lock
1891		 * is removed, and we need it around for the CCB release
1892		 * operation.
1893		 */
1894		xpt_release_ccb(done_ccb);
1895		cam_periph_unhold(periph);
1896		return;
1897	}
1898	case CD_CCB_WAITING:
1899	{
1900		/* Caller will release the CCB */
1901		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1902			  ("trying to wakeup ccbwait\n"));
1903
1904		wakeup(&done_ccb->ccb_h.cbfcnp);
1905		return;
1906	}
1907	case CD_CCB_TUR:
1908	{
1909		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1910
1911			if (cderror(done_ccb, CAM_RETRY_SELTO,
1912			    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
1913			    ERESTART)
1914				return;
1915			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1916				cam_release_devq(done_ccb->ccb_h.path,
1917						 /*relsim_flags*/0,
1918						 /*reduction*/0,
1919						 /*timeout*/0,
1920						 /*getcount_only*/0);
1921		}
1922		xpt_release_ccb(done_ccb);
1923		cam_periph_release_locked(periph);
1924		return;
1925	}
1926	default:
1927		break;
1928	}
1929	xpt_release_ccb(done_ccb);
1930}
1931
1932static union cd_pages *
1933cdgetpage(struct cd_mode_params *mode_params)
1934{
1935	union cd_pages *page;
1936
1937	if (mode_params->cdb_size == 10)
1938		page = (union cd_pages *)find_mode_page_10(
1939			(struct scsi_mode_header_10 *)mode_params->mode_buf);
1940	else
1941		page = (union cd_pages *)find_mode_page_6(
1942			(struct scsi_mode_header_6 *)mode_params->mode_buf);
1943
1944	return (page);
1945}
1946
1947static int
1948cdgetpagesize(int page_num)
1949{
1950	int i;
1951
1952	for (i = 0; i < (sizeof(cd_page_size_table)/
1953	     sizeof(cd_page_size_table[0])); i++) {
1954		if (cd_page_size_table[i].page == page_num)
1955			return (cd_page_size_table[i].page_size);
1956	}
1957
1958	return (-1);
1959}
1960
1961static int
1962cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1963{
1964
1965	struct 	cam_periph *periph;
1966	struct	cd_softc *softc;
1967	int	nocopyout, error = 0;
1968
1969	periph = (struct cam_periph *)dp->d_drv1;
1970	cam_periph_lock(periph);
1971
1972	softc = (struct cd_softc *)periph->softc;
1973
1974	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1975	    ("cdioctl(%#lx)\n", cmd));
1976
1977	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1978		cam_periph_unlock(periph);
1979		cam_periph_release(periph);
1980		return (error);
1981	}
1982
1983	/*
1984	 * If we don't have media loaded, check for it.  If still don't
1985	 * have media loaded, we can only do a load or eject.
1986	 *
1987	 * We only care whether media is loaded if this is a cd-specific ioctl
1988	 * (thus the IOCGROUP check below).  Note that this will break if
1989	 * anyone adds any ioctls into the switch statement below that don't
1990	 * have their ioctl group set to 'c'.
1991	 */
1992	if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1993	 && ((cmd != CDIOCCLOSE)
1994	  && (cmd != CDIOCEJECT))
1995	 && (IOCGROUP(cmd) == 'c')) {
1996		error = cdcheckmedia(periph);
1997		if (error != 0) {
1998			cam_periph_unhold(periph);
1999			cam_periph_unlock(periph);
2000			return (error);
2001		}
2002	}
2003	/*
2004	 * Drop the lock here so later mallocs can use WAITOK.  The periph
2005	 * is essentially locked still with the cam_periph_hold call above.
2006	 */
2007	cam_periph_unlock(periph);
2008
2009	nocopyout = 0;
2010	switch (cmd) {
2011
2012	case CDIOCPLAYTRACKS:
2013		{
2014			struct ioc_play_track *args
2015			    = (struct ioc_play_track *) addr;
2016			struct cd_mode_params params;
2017			union cd_pages *page;
2018
2019			params.alloc_len = sizeof(union cd_mode_data_6_10);
2020			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2021						 M_WAITOK | M_ZERO);
2022
2023			cam_periph_lock(periph);
2024			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2025				  ("trying to do CDIOCPLAYTRACKS\n"));
2026
2027			error = cdgetmode(periph, &params, AUDIO_PAGE);
2028			if (error) {
2029				free(params.mode_buf, M_SCSICD);
2030				cam_periph_unlock(periph);
2031				break;
2032			}
2033			page = cdgetpage(&params);
2034
2035			page->audio.flags &= ~CD_PA_SOTC;
2036			page->audio.flags |= CD_PA_IMMED;
2037			error = cdsetmode(periph, &params);
2038			free(params.mode_buf, M_SCSICD);
2039			if (error) {
2040				cam_periph_unlock(periph);
2041				break;
2042			}
2043
2044			/*
2045			 * This was originally implemented with the PLAY
2046			 * AUDIO TRACK INDEX command, but that command was
2047			 * deprecated after SCSI-2.  Most (all?) SCSI CDROM
2048			 * drives support it but ATAPI and ATAPI-derivative
2049			 * drives don't seem to support it.  So we keep a
2050			 * cache of the table of contents and translate
2051			 * track numbers to MSF format.
2052			 */
2053			if (softc->flags & CD_FLAG_VALID_TOC) {
2054				union msf_lba *sentry, *eentry;
2055				int st, et;
2056
2057				if (args->end_track <
2058				    softc->toc.header.ending_track + 1)
2059					args->end_track++;
2060				if (args->end_track >
2061				    softc->toc.header.ending_track + 1)
2062					args->end_track =
2063					    softc->toc.header.ending_track + 1;
2064				st = args->start_track -
2065					softc->toc.header.starting_track;
2066				et = args->end_track -
2067					softc->toc.header.starting_track;
2068				if ((st < 0)
2069				 || (et < 0)
2070			 	 || (st > (softc->toc.header.ending_track -
2071				     softc->toc.header.starting_track))) {
2072					error = EINVAL;
2073					cam_periph_unlock(periph);
2074					break;
2075				}
2076				sentry = &softc->toc.entries[st].addr;
2077				eentry = &softc->toc.entries[et].addr;
2078				error = cdplaymsf(periph,
2079						  sentry->msf.minute,
2080						  sentry->msf.second,
2081						  sentry->msf.frame,
2082						  eentry->msf.minute,
2083						  eentry->msf.second,
2084						  eentry->msf.frame);
2085			} else {
2086				/*
2087				 * If we don't have a valid TOC, try the
2088				 * play track index command.  It is part of
2089				 * the SCSI-2 spec, but was removed in the
2090				 * MMC specs.  ATAPI and ATAPI-derived
2091				 * drives don't support it.
2092				 */
2093				if (softc->quirks & CD_Q_BCD_TRACKS) {
2094					args->start_track =
2095						bin2bcd(args->start_track);
2096					args->end_track =
2097						bin2bcd(args->end_track);
2098				}
2099				error = cdplaytracks(periph,
2100						     args->start_track,
2101						     args->start_index,
2102						     args->end_track,
2103						     args->end_index);
2104			}
2105			cam_periph_unlock(periph);
2106		}
2107		break;
2108	case CDIOCPLAYMSF:
2109		{
2110			struct ioc_play_msf *args
2111				= (struct ioc_play_msf *) addr;
2112			struct cd_mode_params params;
2113			union cd_pages *page;
2114
2115			params.alloc_len = sizeof(union cd_mode_data_6_10);
2116			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2117						 M_WAITOK | M_ZERO);
2118
2119			cam_periph_lock(periph);
2120			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2121				  ("trying to do CDIOCPLAYMSF\n"));
2122
2123			error = cdgetmode(periph, &params, AUDIO_PAGE);
2124			if (error) {
2125				free(params.mode_buf, M_SCSICD);
2126				cam_periph_unlock(periph);
2127				break;
2128			}
2129			page = cdgetpage(&params);
2130
2131			page->audio.flags &= ~CD_PA_SOTC;
2132			page->audio.flags |= CD_PA_IMMED;
2133			error = cdsetmode(periph, &params);
2134			free(params.mode_buf, M_SCSICD);
2135			if (error) {
2136				cam_periph_unlock(periph);
2137				break;
2138			}
2139			error = cdplaymsf(periph,
2140					  args->start_m,
2141					  args->start_s,
2142					  args->start_f,
2143					  args->end_m,
2144					  args->end_s,
2145					  args->end_f);
2146			cam_periph_unlock(periph);
2147		}
2148		break;
2149	case CDIOCPLAYBLOCKS:
2150		{
2151			struct ioc_play_blocks *args
2152				= (struct ioc_play_blocks *) addr;
2153			struct cd_mode_params params;
2154			union cd_pages *page;
2155
2156			params.alloc_len = sizeof(union cd_mode_data_6_10);
2157			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2158						 M_WAITOK | M_ZERO);
2159
2160			cam_periph_lock(periph);
2161			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2162				  ("trying to do CDIOCPLAYBLOCKS\n"));
2163
2164
2165			error = cdgetmode(periph, &params, AUDIO_PAGE);
2166			if (error) {
2167				free(params.mode_buf, M_SCSICD);
2168				cam_periph_unlock(periph);
2169				break;
2170			}
2171			page = cdgetpage(&params);
2172
2173			page->audio.flags &= ~CD_PA_SOTC;
2174			page->audio.flags |= CD_PA_IMMED;
2175			error = cdsetmode(periph, &params);
2176			free(params.mode_buf, M_SCSICD);
2177			if (error) {
2178				cam_periph_unlock(periph);
2179				break;
2180			}
2181			error = cdplay(periph, args->blk, args->len);
2182			cam_periph_unlock(periph);
2183		}
2184		break;
2185	case CDIOCREADSUBCHANNEL_SYSSPACE:
2186		nocopyout = 1;
2187		/* Fallthrough */
2188	case CDIOCREADSUBCHANNEL:
2189		{
2190			struct ioc_read_subchannel *args
2191				= (struct ioc_read_subchannel *) addr;
2192			struct cd_sub_channel_info *data;
2193			u_int32_t len = args->data_len;
2194
2195			data = malloc(sizeof(struct cd_sub_channel_info),
2196				      M_SCSICD, M_WAITOK | M_ZERO);
2197
2198			cam_periph_lock(periph);
2199			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2200				  ("trying to do CDIOCREADSUBCHANNEL\n"));
2201
2202			if ((len > sizeof(struct cd_sub_channel_info)) ||
2203			    (len < sizeof(struct cd_sub_channel_header))) {
2204				printf(
2205					"scsi_cd: cdioctl: "
2206					"cdioreadsubchannel: error, len=%d\n",
2207					len);
2208				error = EINVAL;
2209				free(data, M_SCSICD);
2210				cam_periph_unlock(periph);
2211				break;
2212			}
2213
2214			if (softc->quirks & CD_Q_BCD_TRACKS)
2215				args->track = bin2bcd(args->track);
2216
2217			error = cdreadsubchannel(periph, args->address_format,
2218				args->data_format, args->track, data, len);
2219
2220			if (error) {
2221				free(data, M_SCSICD);
2222				cam_periph_unlock(periph);
2223	 			break;
2224			}
2225			if (softc->quirks & CD_Q_BCD_TRACKS)
2226				data->what.track_info.track_number =
2227				    bcd2bin(data->what.track_info.track_number);
2228			len = min(len, ((data->header.data_len[0] << 8) +
2229				data->header.data_len[1] +
2230				sizeof(struct cd_sub_channel_header)));
2231			cam_periph_unlock(periph);
2232			if (nocopyout == 0) {
2233				if (copyout(data, args->data, len) != 0) {
2234					error = EFAULT;
2235				}
2236			} else {
2237				bcopy(data, args->data, len);
2238			}
2239			free(data, M_SCSICD);
2240		}
2241		break;
2242
2243	case CDIOREADTOCHEADER:
2244		{
2245			struct ioc_toc_header *th;
2246
2247			th = malloc(sizeof(struct ioc_toc_header), M_SCSICD,
2248				    M_WAITOK | M_ZERO);
2249
2250			cam_periph_lock(periph);
2251			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2252				  ("trying to do CDIOREADTOCHEADER\n"));
2253
2254			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2255				          sizeof (*th), /*sense_flags*/SF_NO_PRINT);
2256			if (error) {
2257				free(th, M_SCSICD);
2258				cam_periph_unlock(periph);
2259				break;
2260			}
2261			if (softc->quirks & CD_Q_BCD_TRACKS) {
2262				/* we are going to have to convert the BCD
2263				 * encoding on the cd to what is expected
2264				 */
2265				th->starting_track =
2266					bcd2bin(th->starting_track);
2267				th->ending_track = bcd2bin(th->ending_track);
2268			}
2269			th->len = ntohs(th->len);
2270			bcopy(th, addr, sizeof(*th));
2271			free(th, M_SCSICD);
2272			cam_periph_unlock(periph);
2273		}
2274		break;
2275	case CDIOREADTOCENTRYS:
2276		{
2277			struct cd_tocdata *data;
2278			struct cd_toc_single *lead;
2279			struct ioc_read_toc_entry *te =
2280				(struct ioc_read_toc_entry *) addr;
2281			struct ioc_toc_header *th;
2282			u_int32_t len, readlen, idx, num;
2283			u_int32_t starting_track = te->starting_track;
2284
2285			data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2286			lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO);
2287
2288			cam_periph_lock(periph);
2289			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2290				  ("trying to do CDIOREADTOCENTRYS\n"));
2291
2292			if (te->data_len < sizeof(struct cd_toc_entry)
2293			 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2294			 || (te->address_format != CD_MSF_FORMAT
2295			  && te->address_format != CD_LBA_FORMAT)) {
2296				error = EINVAL;
2297				printf("scsi_cd: error in readtocentries, "
2298				       "returning EINVAL\n");
2299				free(data, M_SCSICD);
2300				free(lead, M_SCSICD);
2301				cam_periph_unlock(periph);
2302				break;
2303			}
2304
2305			th = &data->header;
2306			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2307					  sizeof (*th), /*sense_flags*/0);
2308			if (error) {
2309				free(data, M_SCSICD);
2310				free(lead, M_SCSICD);
2311				cam_periph_unlock(periph);
2312				break;
2313			}
2314
2315			if (softc->quirks & CD_Q_BCD_TRACKS) {
2316				/* we are going to have to convert the BCD
2317				 * encoding on the cd to what is expected
2318				 */
2319				th->starting_track =
2320				    bcd2bin(th->starting_track);
2321				th->ending_track = bcd2bin(th->ending_track);
2322			}
2323
2324			if (starting_track == 0)
2325				starting_track = th->starting_track;
2326			else if (starting_track == LEADOUT)
2327				starting_track = th->ending_track + 1;
2328			else if (starting_track < th->starting_track ||
2329				 starting_track > th->ending_track + 1) {
2330				printf("scsi_cd: error in readtocentries, "
2331				       "returning EINVAL\n");
2332				free(data, M_SCSICD);
2333				free(lead, M_SCSICD);
2334				cam_periph_unlock(periph);
2335				error = EINVAL;
2336				break;
2337			}
2338
2339			/* calculate reading length without leadout entry */
2340			readlen = (th->ending_track - starting_track + 1) *
2341				  sizeof(struct cd_toc_entry);
2342
2343			/* and with leadout entry */
2344			len = readlen + sizeof(struct cd_toc_entry);
2345			if (te->data_len < len) {
2346				len = te->data_len;
2347				if (readlen > len)
2348					readlen = len;
2349			}
2350			if (len > sizeof(data->entries)) {
2351				printf("scsi_cd: error in readtocentries, "
2352				       "returning EINVAL\n");
2353				error = EINVAL;
2354				free(data, M_SCSICD);
2355				free(lead, M_SCSICD);
2356				cam_periph_unlock(periph);
2357				break;
2358			}
2359			num = len / sizeof(struct cd_toc_entry);
2360
2361			if (readlen > 0) {
2362				error = cdreadtoc(periph, te->address_format,
2363						  starting_track,
2364						  (u_int8_t *)data,
2365						  readlen + sizeof (*th),
2366						  /*sense_flags*/0);
2367				if (error) {
2368					free(data, M_SCSICD);
2369					free(lead, M_SCSICD);
2370					cam_periph_unlock(periph);
2371					break;
2372				}
2373			}
2374
2375			/* make leadout entry if needed */
2376			idx = starting_track + num - 1;
2377			if (softc->quirks & CD_Q_BCD_TRACKS)
2378				th->ending_track = bcd2bin(th->ending_track);
2379			if (idx == th->ending_track + 1) {
2380				error = cdreadtoc(periph, te->address_format,
2381						  LEADOUT, (u_int8_t *)lead,
2382						  sizeof(*lead),
2383						  /*sense_flags*/0);
2384				if (error) {
2385					free(data, M_SCSICD);
2386					free(lead, M_SCSICD);
2387					cam_periph_unlock(periph);
2388					break;
2389				}
2390				data->entries[idx - starting_track] =
2391					lead->entry;
2392			}
2393			if (softc->quirks & CD_Q_BCD_TRACKS) {
2394				for (idx = 0; idx < num - 1; idx++) {
2395					data->entries[idx].track =
2396					    bcd2bin(data->entries[idx].track);
2397				}
2398			}
2399
2400			cam_periph_unlock(periph);
2401			error = copyout(data->entries, te->data, len);
2402			free(data, M_SCSICD);
2403			free(lead, M_SCSICD);
2404		}
2405		break;
2406	case CDIOREADTOCENTRY:
2407		{
2408			struct cd_toc_single *data;
2409			struct ioc_read_toc_single_entry *te =
2410				(struct ioc_read_toc_single_entry *) addr;
2411			struct ioc_toc_header *th;
2412			u_int32_t track;
2413
2414			data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO);
2415
2416			cam_periph_lock(periph);
2417			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2418				  ("trying to do CDIOREADTOCENTRY\n"));
2419
2420			if (te->address_format != CD_MSF_FORMAT
2421			    && te->address_format != CD_LBA_FORMAT) {
2422				printf("error in readtocentry, "
2423				       " returning EINVAL\n");
2424				free(data, M_SCSICD);
2425				error = EINVAL;
2426				cam_periph_unlock(periph);
2427				break;
2428			}
2429
2430			th = &data->header;
2431			error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2432					  sizeof (*th), /*sense_flags*/0);
2433			if (error) {
2434				free(data, M_SCSICD);
2435				cam_periph_unlock(periph);
2436				break;
2437			}
2438
2439			if (softc->quirks & CD_Q_BCD_TRACKS) {
2440				/* we are going to have to convert the BCD
2441				 * encoding on the cd to what is expected
2442				 */
2443				th->starting_track =
2444				    bcd2bin(th->starting_track);
2445				th->ending_track = bcd2bin(th->ending_track);
2446			}
2447			track = te->track;
2448			if (track == 0)
2449				track = th->starting_track;
2450			else if (track == LEADOUT)
2451				/* OK */;
2452			else if (track < th->starting_track ||
2453				 track > th->ending_track + 1) {
2454				printf("error in readtocentry, "
2455				       " returning EINVAL\n");
2456				free(data, M_SCSICD);
2457				error = EINVAL;
2458				cam_periph_unlock(periph);
2459				break;
2460			}
2461
2462			error = cdreadtoc(periph, te->address_format, track,
2463					  (u_int8_t *)data, sizeof(*data),
2464					  /*sense_flags*/0);
2465			if (error) {
2466				free(data, M_SCSICD);
2467				cam_periph_unlock(periph);
2468				break;
2469			}
2470
2471			if (softc->quirks & CD_Q_BCD_TRACKS)
2472				data->entry.track = bcd2bin(data->entry.track);
2473			bcopy(&data->entry, &te->entry,
2474			      sizeof(struct cd_toc_entry));
2475			free(data, M_SCSICD);
2476			cam_periph_unlock(periph);
2477		}
2478		break;
2479	case CDIOCSETPATCH:
2480		{
2481			struct ioc_patch *arg = (struct ioc_patch *)addr;
2482			struct cd_mode_params params;
2483			union cd_pages *page;
2484
2485			params.alloc_len = sizeof(union cd_mode_data_6_10);
2486			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2487						 M_WAITOK | M_ZERO);
2488
2489			cam_periph_lock(periph);
2490			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2491				  ("trying to do CDIOCSETPATCH\n"));
2492
2493			error = cdgetmode(periph, &params, AUDIO_PAGE);
2494			if (error) {
2495				free(params.mode_buf, M_SCSICD);
2496				cam_periph_unlock(periph);
2497				break;
2498			}
2499			page = cdgetpage(&params);
2500
2501			page->audio.port[LEFT_PORT].channels =
2502				arg->patch[0];
2503			page->audio.port[RIGHT_PORT].channels =
2504				arg->patch[1];
2505			page->audio.port[2].channels = arg->patch[2];
2506			page->audio.port[3].channels = arg->patch[3];
2507			error = cdsetmode(periph, &params);
2508			free(params.mode_buf, M_SCSICD);
2509			cam_periph_unlock(periph);
2510		}
2511		break;
2512	case CDIOCGETVOL:
2513		{
2514			struct ioc_vol *arg = (struct ioc_vol *) addr;
2515			struct cd_mode_params params;
2516			union cd_pages *page;
2517
2518			params.alloc_len = sizeof(union cd_mode_data_6_10);
2519			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2520						 M_WAITOK | M_ZERO);
2521
2522			cam_periph_lock(periph);
2523			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2524				  ("trying to do CDIOCGETVOL\n"));
2525
2526			error = cdgetmode(periph, &params, AUDIO_PAGE);
2527			if (error) {
2528				free(params.mode_buf, M_SCSICD);
2529				cam_periph_unlock(periph);
2530				break;
2531			}
2532			page = cdgetpage(&params);
2533
2534			arg->vol[LEFT_PORT] =
2535				page->audio.port[LEFT_PORT].volume;
2536			arg->vol[RIGHT_PORT] =
2537				page->audio.port[RIGHT_PORT].volume;
2538			arg->vol[2] = page->audio.port[2].volume;
2539			arg->vol[3] = page->audio.port[3].volume;
2540			free(params.mode_buf, M_SCSICD);
2541			cam_periph_unlock(periph);
2542		}
2543		break;
2544	case CDIOCSETVOL:
2545		{
2546			struct ioc_vol *arg = (struct ioc_vol *) addr;
2547			struct cd_mode_params params;
2548			union cd_pages *page;
2549
2550			params.alloc_len = sizeof(union cd_mode_data_6_10);
2551			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2552						 M_WAITOK | M_ZERO);
2553
2554			cam_periph_lock(periph);
2555			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2556				  ("trying to do CDIOCSETVOL\n"));
2557
2558			error = cdgetmode(periph, &params, AUDIO_PAGE);
2559			if (error) {
2560				free(params.mode_buf, M_SCSICD);
2561				cam_periph_unlock(periph);
2562				break;
2563			}
2564			page = cdgetpage(&params);
2565
2566			page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2567			page->audio.port[LEFT_PORT].volume =
2568				arg->vol[LEFT_PORT];
2569			page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2570			page->audio.port[RIGHT_PORT].volume =
2571				arg->vol[RIGHT_PORT];
2572			page->audio.port[2].volume = arg->vol[2];
2573			page->audio.port[3].volume = arg->vol[3];
2574			error = cdsetmode(periph, &params);
2575			cam_periph_unlock(periph);
2576			free(params.mode_buf, M_SCSICD);
2577		}
2578		break;
2579	case CDIOCSETMONO:
2580		{
2581			struct cd_mode_params params;
2582			union cd_pages *page;
2583
2584			params.alloc_len = sizeof(union cd_mode_data_6_10);
2585			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2586						 M_WAITOK | M_ZERO);
2587
2588			cam_periph_lock(periph);
2589			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2590				  ("trying to do CDIOCSETMONO\n"));
2591
2592			error = cdgetmode(periph, &params, AUDIO_PAGE);
2593			if (error) {
2594				free(params.mode_buf, M_SCSICD);
2595				cam_periph_unlock(periph);
2596				break;
2597			}
2598			page = cdgetpage(&params);
2599
2600			page->audio.port[LEFT_PORT].channels =
2601				LEFT_CHANNEL | RIGHT_CHANNEL;
2602			page->audio.port[RIGHT_PORT].channels =
2603				LEFT_CHANNEL | RIGHT_CHANNEL;
2604			page->audio.port[2].channels = 0;
2605			page->audio.port[3].channels = 0;
2606			error = cdsetmode(periph, &params);
2607			cam_periph_unlock(periph);
2608			free(params.mode_buf, M_SCSICD);
2609		}
2610		break;
2611	case CDIOCSETSTEREO:
2612		{
2613			struct cd_mode_params params;
2614			union cd_pages *page;
2615
2616			params.alloc_len = sizeof(union cd_mode_data_6_10);
2617			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2618						 M_WAITOK | M_ZERO);
2619
2620			cam_periph_lock(periph);
2621			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2622				  ("trying to do CDIOCSETSTEREO\n"));
2623
2624			error = cdgetmode(periph, &params, AUDIO_PAGE);
2625			if (error) {
2626				free(params.mode_buf, M_SCSICD);
2627				cam_periph_unlock(periph);
2628				break;
2629			}
2630			page = cdgetpage(&params);
2631
2632			page->audio.port[LEFT_PORT].channels =
2633				LEFT_CHANNEL;
2634			page->audio.port[RIGHT_PORT].channels =
2635				RIGHT_CHANNEL;
2636			page->audio.port[2].channels = 0;
2637			page->audio.port[3].channels = 0;
2638			error = cdsetmode(periph, &params);
2639			free(params.mode_buf, M_SCSICD);
2640			cam_periph_unlock(periph);
2641		}
2642		break;
2643	case CDIOCSETMUTE:
2644		{
2645			struct cd_mode_params params;
2646			union cd_pages *page;
2647
2648			params.alloc_len = sizeof(union cd_mode_data_6_10);
2649			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2650						 M_WAITOK | M_ZERO);
2651
2652			cam_periph_lock(periph);
2653			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2654				  ("trying to do CDIOCSETMUTE\n"));
2655
2656			error = cdgetmode(periph, &params, AUDIO_PAGE);
2657			if (error) {
2658				free(params.mode_buf, M_SCSICD);
2659				cam_periph_unlock(periph);
2660				break;
2661			}
2662			page = cdgetpage(&params);
2663
2664			page->audio.port[LEFT_PORT].channels = 0;
2665			page->audio.port[RIGHT_PORT].channels = 0;
2666			page->audio.port[2].channels = 0;
2667			page->audio.port[3].channels = 0;
2668			error = cdsetmode(periph, &params);
2669			free(params.mode_buf, M_SCSICD);
2670			cam_periph_unlock(periph);
2671		}
2672		break;
2673	case CDIOCSETLEFT:
2674		{
2675			struct cd_mode_params params;
2676			union cd_pages *page;
2677
2678			params.alloc_len = sizeof(union cd_mode_data_6_10);
2679			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2680						 M_WAITOK | M_ZERO);
2681
2682			cam_periph_lock(periph);
2683			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2684				  ("trying to do CDIOCSETLEFT\n"));
2685
2686			error = cdgetmode(periph, &params, AUDIO_PAGE);
2687			if (error) {
2688				free(params.mode_buf, M_SCSICD);
2689				cam_periph_unlock(periph);
2690				break;
2691			}
2692			page = cdgetpage(&params);
2693
2694			page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2695			page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2696			page->audio.port[2].channels = 0;
2697			page->audio.port[3].channels = 0;
2698			error = cdsetmode(periph, &params);
2699			free(params.mode_buf, M_SCSICD);
2700			cam_periph_unlock(periph);
2701		}
2702		break;
2703	case CDIOCSETRIGHT:
2704		{
2705			struct cd_mode_params params;
2706			union cd_pages *page;
2707
2708			params.alloc_len = sizeof(union cd_mode_data_6_10);
2709			params.mode_buf = malloc(params.alloc_len, M_SCSICD,
2710						 M_WAITOK | M_ZERO);
2711
2712			cam_periph_lock(periph);
2713			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2714				  ("trying to do CDIOCSETRIGHT\n"));
2715
2716			error = cdgetmode(periph, &params, AUDIO_PAGE);
2717			if (error) {
2718				free(params.mode_buf, M_SCSICD);
2719				cam_periph_unlock(periph);
2720				break;
2721			}
2722			page = cdgetpage(&params);
2723
2724			page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2725			page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2726			page->audio.port[2].channels = 0;
2727			page->audio.port[3].channels = 0;
2728			error = cdsetmode(periph, &params);
2729			free(params.mode_buf, M_SCSICD);
2730			cam_periph_unlock(periph);
2731		}
2732		break;
2733	case CDIOCRESUME:
2734		cam_periph_lock(periph);
2735		error = cdpause(periph, 1);
2736		cam_periph_unlock(periph);
2737		break;
2738	case CDIOCPAUSE:
2739		cam_periph_lock(periph);
2740		error = cdpause(periph, 0);
2741		cam_periph_unlock(periph);
2742		break;
2743	case CDIOCSTART:
2744		cam_periph_lock(periph);
2745		error = cdstartunit(periph, 0);
2746		cam_periph_unlock(periph);
2747		break;
2748	case CDIOCCLOSE:
2749		cam_periph_lock(periph);
2750		error = cdstartunit(periph, 1);
2751		cam_periph_unlock(periph);
2752		break;
2753	case CDIOCSTOP:
2754		cam_periph_lock(periph);
2755		error = cdstopunit(periph, 0);
2756		cam_periph_unlock(periph);
2757		break;
2758	case CDIOCEJECT:
2759		cam_periph_lock(periph);
2760		error = cdstopunit(periph, 1);
2761		cam_periph_unlock(periph);
2762		break;
2763	case CDIOCALLOW:
2764		cam_periph_lock(periph);
2765		cdprevent(periph, PR_ALLOW);
2766		cam_periph_unlock(periph);
2767		break;
2768	case CDIOCPREVENT:
2769		cam_periph_lock(periph);
2770		cdprevent(periph, PR_PREVENT);
2771		cam_periph_unlock(periph);
2772		break;
2773	case CDIOCSETDEBUG:
2774		/* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2775		error = ENOTTY;
2776		break;
2777	case CDIOCCLRDEBUG:
2778		/* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2779		error = ENOTTY;
2780		break;
2781	case CDIOCRESET:
2782		/* return (cd_reset(periph)); */
2783		error = ENOTTY;
2784		break;
2785	case CDRIOCREADSPEED:
2786		cam_periph_lock(periph);
2787		error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2788		cam_periph_unlock(periph);
2789		break;
2790	case CDRIOCWRITESPEED:
2791		cam_periph_lock(periph);
2792		error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2793		cam_periph_unlock(periph);
2794		break;
2795	case CDRIOCGETBLOCKSIZE:
2796		*(int *)addr = softc->params.blksize;
2797		break;
2798	case CDRIOCSETBLOCKSIZE:
2799		if (*(int *)addr <= 0) {
2800			error = EINVAL;
2801			break;
2802		}
2803		softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr;
2804		break;
2805	case DVDIOCSENDKEY:
2806	case DVDIOCREPORTKEY: {
2807		struct dvd_authinfo *authinfo;
2808
2809		authinfo = (struct dvd_authinfo *)addr;
2810
2811		if (cmd == DVDIOCREPORTKEY)
2812			error = cdreportkey(periph, authinfo);
2813		else
2814			error = cdsendkey(periph, authinfo);
2815		break;
2816		}
2817	case DVDIOCREADSTRUCTURE: {
2818		struct dvd_struct *dvdstruct;
2819
2820		dvdstruct = (struct dvd_struct *)addr;
2821
2822		error = cdreaddvdstructure(periph, dvdstruct);
2823
2824		break;
2825	}
2826	default:
2827		cam_periph_lock(periph);
2828		error = cam_periph_ioctl(periph, cmd, addr, cderror);
2829		cam_periph_unlock(periph);
2830		break;
2831	}
2832
2833	cam_periph_lock(periph);
2834	cam_periph_unhold(periph);
2835
2836	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2837	if (error && bootverbose) {
2838		printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2839	}
2840	cam_periph_unlock(periph);
2841
2842	return (error);
2843}
2844
2845static void
2846cdprevent(struct cam_periph *periph, int action)
2847{
2848	union	ccb *ccb;
2849	struct	cd_softc *softc;
2850	int	error;
2851
2852	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2853
2854	softc = (struct cd_softc *)periph->softc;
2855
2856	if (((action == PR_ALLOW)
2857	  && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2858	 || ((action == PR_PREVENT)
2859	  && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2860		return;
2861	}
2862
2863	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
2864
2865	scsi_prevent(&ccb->csio,
2866		     /*retries*/ cd_retry_count,
2867		     cddone,
2868		     MSG_SIMPLE_Q_TAG,
2869		     action,
2870		     SSD_FULL_SIZE,
2871		     /* timeout */60000);
2872
2873	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2874			/*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2875
2876	xpt_release_ccb(ccb);
2877
2878	if (error == 0) {
2879		if (action == PR_ALLOW)
2880			softc->flags &= ~CD_FLAG_DISC_LOCKED;
2881		else
2882			softc->flags |= CD_FLAG_DISC_LOCKED;
2883	}
2884}
2885
2886/*
2887 * XXX: the disk media and sector size is only really able to change
2888 * XXX: while the device is closed.
2889 */
2890static int
2891cdcheckmedia(struct cam_periph *periph)
2892{
2893	struct cd_softc *softc;
2894	struct ioc_toc_header *toch;
2895	struct cd_toc_single leadout;
2896	u_int32_t size, toclen;
2897	int error, num_entries, cdindex;
2898
2899	softc = (struct cd_softc *)periph->softc;
2900
2901	cdprevent(periph, PR_PREVENT);
2902	softc->disk->d_sectorsize = 2048;
2903	softc->disk->d_mediasize = 0;
2904
2905	/*
2906	 * Get the disc size and block size.  If we can't get it, we don't
2907	 * have media, most likely.
2908	 */
2909	if ((error = cdsize(periph, &size)) != 0) {
2910		softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2911		cdprevent(periph, PR_ALLOW);
2912		return (error);
2913	} else {
2914		softc->flags |= CD_FLAG_SAW_MEDIA | CD_FLAG_VALID_MEDIA;
2915		softc->disk->d_sectorsize = softc->params.blksize;
2916		softc->disk->d_mediasize =
2917		    (off_t)softc->params.blksize * softc->params.disksize;
2918	}
2919
2920	/*
2921	 * Now we check the table of contents.  This (currently) is only
2922	 * used for the CDIOCPLAYTRACKS ioctl.  It may be used later to do
2923	 * things like present a separate entry in /dev for each track,
2924	 * like that acd(4) driver does.
2925	 */
2926	bzero(&softc->toc, sizeof(softc->toc));
2927	toch = &softc->toc.header;
2928	/*
2929	 * We will get errors here for media that doesn't have a table of
2930	 * contents.  According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2931	 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2932	 * has not been recorded (no complete session) and the Format codes
2933	 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2934	 * with an INVALID FIELD IN CDB.  Devices that are not capable of
2935	 * reading an incomplete session on DDC/CD-R/RW media shall report
2936	 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2937	 *
2938	 * So this isn't fatal if we can't read the table of contents, it
2939	 * just means that the user won't be able to issue the play tracks
2940	 * ioctl, and likely lots of other stuff won't work either.  They
2941	 * need to burn the CD before we can do a whole lot with it.  So
2942	 * we don't print anything here if we get an error back.
2943	 */
2944	error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2945			  SF_NO_PRINT);
2946	/*
2947	 * Errors in reading the table of contents aren't fatal, we just
2948	 * won't have a valid table of contents cached.
2949	 */
2950	if (error != 0) {
2951		error = 0;
2952		bzero(&softc->toc, sizeof(softc->toc));
2953		goto bailout;
2954	}
2955
2956	if (softc->quirks & CD_Q_BCD_TRACKS) {
2957		toch->starting_track = bcd2bin(toch->starting_track);
2958		toch->ending_track = bcd2bin(toch->ending_track);
2959	}
2960
2961	/* Number of TOC entries, plus leadout */
2962	num_entries = (toch->ending_track - toch->starting_track) + 2;
2963
2964	if (num_entries <= 0)
2965		goto bailout;
2966
2967	toclen = num_entries * sizeof(struct cd_toc_entry);
2968
2969	error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2970			  (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2971			  SF_NO_PRINT);
2972	if (error != 0) {
2973		error = 0;
2974		bzero(&softc->toc, sizeof(softc->toc));
2975		goto bailout;
2976	}
2977
2978	if (softc->quirks & CD_Q_BCD_TRACKS) {
2979		toch->starting_track = bcd2bin(toch->starting_track);
2980		toch->ending_track = bcd2bin(toch->ending_track);
2981	}
2982	/*
2983	 * XXX KDM is this necessary?  Probably only if the drive doesn't
2984	 * return leadout information with the table of contents.
2985	 */
2986	cdindex = toch->starting_track + num_entries -1;
2987	if (cdindex == toch->ending_track + 1) {
2988
2989		error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2990				  (u_int8_t *)&leadout, sizeof(leadout),
2991				  SF_NO_PRINT);
2992		if (error != 0) {
2993			error = 0;
2994			goto bailout;
2995		}
2996		softc->toc.entries[cdindex - toch->starting_track] =
2997			leadout.entry;
2998	}
2999	if (softc->quirks & CD_Q_BCD_TRACKS) {
3000		for (cdindex = 0; cdindex < num_entries - 1; cdindex++) {
3001			softc->toc.entries[cdindex].track =
3002				bcd2bin(softc->toc.entries[cdindex].track);
3003		}
3004	}
3005
3006	softc->flags |= CD_FLAG_VALID_TOC;
3007
3008	/* If the first track is audio, correct sector size. */
3009	if ((softc->toc.entries[0].control & 4) == 0) {
3010		softc->disk->d_sectorsize = softc->params.blksize = 2352;
3011		softc->disk->d_mediasize =
3012		    (off_t)softc->params.blksize * softc->params.disksize;
3013	}
3014
3015bailout:
3016
3017	/*
3018	 * We unconditionally (re)set the blocksize each time the
3019	 * CD device is opened.  This is because the CD can change,
3020	 * and therefore the blocksize might change.
3021	 * XXX problems here if some slice or partition is still
3022	 * open with the old size?
3023	 */
3024	if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE) != 0)
3025		softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3026	softc->disk->d_devstat->block_size = softc->params.blksize;
3027
3028	return (error);
3029}
3030
3031static int
3032cdsize(struct cam_periph *periph, u_int32_t *size)
3033{
3034	struct cd_softc *softc;
3035	union ccb *ccb;
3036	struct scsi_read_capacity_data *rcap_buf;
3037	int error;
3038
3039	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3040
3041	softc = (struct cd_softc *)periph->softc;
3042
3043	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3044
3045	/* XXX Should be M_WAITOK */
3046	rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
3047			  M_SCSICD, M_NOWAIT | M_ZERO);
3048	if (rcap_buf == NULL)
3049		return (ENOMEM);
3050
3051	scsi_read_capacity(&ccb->csio,
3052			   /*retries*/ cd_retry_count,
3053			   cddone,
3054			   MSG_SIMPLE_Q_TAG,
3055			   rcap_buf,
3056			   SSD_FULL_SIZE,
3057			   /* timeout */20000);
3058
3059	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3060			 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3061
3062	xpt_release_ccb(ccb);
3063
3064	softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3065	softc->params.blksize  = scsi_4btoul(rcap_buf->length);
3066	/* Make sure we got at least some block size. */
3067	if (error == 0 && softc->params.blksize == 0)
3068		error = EIO;
3069	/*
3070	 * SCSI-3 mandates that the reported blocksize shall be 2048.
3071	 * Older drives sometimes report funny values, trim it down to
3072	 * 2048, or other parts of the kernel will get confused.
3073	 *
3074	 * XXX we leave drives alone that might report 512 bytes, as
3075	 * well as drives reporting more weird sizes like perhaps 4K.
3076	 */
3077	if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3078		softc->params.blksize = 2048;
3079
3080	free(rcap_buf, M_SCSICD);
3081	*size = softc->params.disksize;
3082
3083	return (error);
3084
3085}
3086
3087static int
3088cd6byteworkaround(union ccb *ccb)
3089{
3090	u_int8_t *cdb;
3091	struct cam_periph *periph;
3092	struct cd_softc *softc;
3093	struct cd_mode_params *params;
3094	int frozen, found;
3095
3096	periph = xpt_path_periph(ccb->ccb_h.path);
3097	softc = (struct cd_softc *)periph->softc;
3098
3099	cdb = ccb->csio.cdb_io.cdb_bytes;
3100
3101	if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3102	 || ((cdb[0] != MODE_SENSE_6)
3103	  && (cdb[0] != MODE_SELECT_6)))
3104		return (0);
3105
3106	/*
3107	 * Because there is no convenient place to stash the overall
3108	 * cd_mode_params structure pointer, we have to grab it like this.
3109	 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3110	 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3111	 *
3112	 * XXX It would be nice if, at some point, we could increase the
3113	 * number of available peripheral private pointers.  Both pointers
3114	 * are currently used in most every peripheral driver.
3115	 */
3116	found = 0;
3117
3118	STAILQ_FOREACH(params, &softc->mode_queue, links) {
3119		if (params->mode_buf == ccb->csio.data_ptr) {
3120			found = 1;
3121			break;
3122		}
3123	}
3124
3125	/*
3126	 * This shouldn't happen.  All mode sense and mode select
3127	 * operations in the cd(4) driver MUST go through cdgetmode() and
3128	 * cdsetmode()!
3129	 */
3130	if (found == 0) {
3131		xpt_print(periph->path,
3132		    "mode buffer not found in mode queue!\n");
3133		return (0);
3134	}
3135
3136	params->cdb_size = 10;
3137	softc->minimum_command_size = 10;
3138	xpt_print(ccb->ccb_h.path,
3139	    "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3140	    (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3141
3142	if (cdb[0] == MODE_SENSE_6) {
3143		struct scsi_mode_sense_10 ms10;
3144		struct scsi_mode_sense_6 *ms6;
3145		int len;
3146
3147		ms6 = (struct scsi_mode_sense_6 *)cdb;
3148
3149		bzero(&ms10, sizeof(ms10));
3150 		ms10.opcode = MODE_SENSE_10;
3151 		ms10.byte2 = ms6->byte2;
3152 		ms10.page = ms6->page;
3153
3154		/*
3155		 * 10 byte mode header, block descriptor,
3156		 * sizeof(union cd_pages)
3157		 */
3158		len = sizeof(struct cd_mode_data_10);
3159		ccb->csio.dxfer_len = len;
3160
3161		scsi_ulto2b(len, ms10.length);
3162		ms10.control = ms6->control;
3163		bcopy(&ms10, cdb, 10);
3164		ccb->csio.cdb_len = 10;
3165	} else {
3166		struct scsi_mode_select_10 ms10;
3167		struct scsi_mode_select_6 *ms6;
3168		struct scsi_mode_header_6 *header6;
3169		struct scsi_mode_header_10 *header10;
3170		struct scsi_mode_page_header *page_header;
3171		int blk_desc_len, page_num, page_size, len;
3172
3173		ms6 = (struct scsi_mode_select_6 *)cdb;
3174
3175		bzero(&ms10, sizeof(ms10));
3176		ms10.opcode = MODE_SELECT_10;
3177		ms10.byte2 = ms6->byte2;
3178
3179		header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3180		header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3181
3182		page_header = find_mode_page_6(header6);
3183		page_num = page_header->page_code;
3184
3185		blk_desc_len = header6->blk_desc_len;
3186
3187		page_size = cdgetpagesize(page_num);
3188
3189		if (page_size != (page_header->page_length +
3190		    sizeof(*page_header)))
3191			page_size = page_header->page_length +
3192				sizeof(*page_header);
3193
3194		len = sizeof(*header10) + blk_desc_len + page_size;
3195
3196		len = min(params->alloc_len, len);
3197
3198		/*
3199		 * Since the 6 byte parameter header is shorter than the 10
3200		 * byte parameter header, we need to copy the actual mode
3201		 * page data, and the block descriptor, if any, so things wind
3202		 * up in the right place.  The regions will overlap, but
3203		 * bcopy() does the right thing.
3204		 */
3205		bcopy(params->mode_buf + sizeof(*header6),
3206		      params->mode_buf + sizeof(*header10),
3207		      len - sizeof(*header10));
3208
3209		/* Make sure these fields are set correctly. */
3210		scsi_ulto2b(0, header10->data_length);
3211		header10->medium_type = 0;
3212		scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3213
3214		ccb->csio.dxfer_len = len;
3215
3216		scsi_ulto2b(len, ms10.length);
3217		ms10.control = ms6->control;
3218		bcopy(&ms10, cdb, 10);
3219		ccb->csio.cdb_len = 10;
3220	}
3221
3222	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3223	ccb->ccb_h.status = CAM_REQUEUE_REQ;
3224	xpt_action(ccb);
3225	if (frozen) {
3226		cam_release_devq(ccb->ccb_h.path,
3227				 /*relsim_flags*/0,
3228				 /*openings*/0,
3229				 /*timeout*/0,
3230				 /*getcount_only*/0);
3231	}
3232
3233	return (ERESTART);
3234}
3235
3236static int
3237cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3238{
3239	struct cd_softc *softc;
3240	struct cam_periph *periph;
3241	int error, error_code, sense_key, asc, ascq;
3242
3243	periph = xpt_path_periph(ccb->ccb_h.path);
3244	softc = (struct cd_softc *)periph->softc;
3245
3246	error = 0;
3247
3248	/*
3249	 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3250	 * CDB comes back with this particular error, try transforming it
3251	 * into the 10 byte version.
3252	 */
3253	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3254		error = cd6byteworkaround(ccb);
3255	} else if (scsi_extract_sense_ccb(ccb,
3256	    &error_code, &sense_key, &asc, &ascq)) {
3257		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3258			error = cd6byteworkaround(ccb);
3259		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3260		    asc == 0x28 && ascq == 0x00)
3261			disk_media_changed(softc->disk, M_NOWAIT);
3262		else if (sense_key == SSD_KEY_NOT_READY &&
3263		    asc == 0x3a && (softc->flags & CD_FLAG_SAW_MEDIA)) {
3264			softc->flags &= ~CD_FLAG_SAW_MEDIA;
3265			disk_media_gone(softc->disk, M_NOWAIT);
3266		}
3267	}
3268
3269	if (error == ERESTART)
3270		return (error);
3271
3272	/*
3273	 * XXX
3274	 * Until we have a better way of doing pack validation,
3275	 * don't treat UAs as errors.
3276	 */
3277	sense_flags |= SF_RETRY_UA;
3278	return (cam_periph_error(ccb, cam_flags, sense_flags,
3279				 &softc->saved_ccb));
3280}
3281
3282static void
3283cdmediapoll(void *arg)
3284{
3285	struct cam_periph *periph = arg;
3286	struct cd_softc *softc = periph->softc;
3287
3288	if (softc->flags & CD_FLAG_CHANGER)
3289		return;
3290
3291	if (softc->state == CD_STATE_NORMAL && !softc->tur &&
3292	    softc->outstanding_cmds == 0) {
3293		if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3294			softc->tur = 1;
3295			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
3296		}
3297	}
3298	/* Queue us up again */
3299	if (cd_poll_period != 0)
3300		callout_schedule(&softc->mediapoll_c, cd_poll_period * hz);
3301}
3302
3303/*
3304 * Read table of contents
3305 */
3306static int
3307cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3308	  u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3309{
3310	struct scsi_read_toc *scsi_cmd;
3311	u_int32_t ntoc;
3312        struct ccb_scsiio *csio;
3313	union ccb *ccb;
3314	int error;
3315
3316	ntoc = len;
3317	error = 0;
3318
3319	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3320
3321	csio = &ccb->csio;
3322
3323	cam_fill_csio(csio,
3324		      /* retries */ cd_retry_count,
3325		      /* cbfcnp */ cddone,
3326		      /* flags */ CAM_DIR_IN,
3327		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3328		      /* data_ptr */ data,
3329		      /* dxfer_len */ len,
3330		      /* sense_len */ SSD_FULL_SIZE,
3331		      sizeof(struct scsi_read_toc),
3332 		      /* timeout */ 50000);
3333
3334	scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3335	bzero (scsi_cmd, sizeof(*scsi_cmd));
3336
3337	if (mode == CD_MSF_FORMAT)
3338		scsi_cmd->byte2 |= CD_MSF;
3339	scsi_cmd->from_track = start;
3340	/* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3341	scsi_cmd->data_len[0] = (ntoc) >> 8;
3342	scsi_cmd->data_len[1] = (ntoc) & 0xff;
3343
3344	scsi_cmd->op_code = READ_TOC;
3345
3346	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3347			 /*sense_flags*/SF_RETRY_UA | sense_flags);
3348
3349	xpt_release_ccb(ccb);
3350
3351	return(error);
3352}
3353
3354static int
3355cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3356		 u_int32_t format, int track,
3357		 struct cd_sub_channel_info *data, u_int32_t len)
3358{
3359	struct scsi_read_subchannel *scsi_cmd;
3360        struct ccb_scsiio *csio;
3361	union ccb *ccb;
3362	int error;
3363
3364	error = 0;
3365
3366	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3367
3368	csio = &ccb->csio;
3369
3370	cam_fill_csio(csio,
3371		      /* retries */ cd_retry_count,
3372		      /* cbfcnp */ cddone,
3373		      /* flags */ CAM_DIR_IN,
3374		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3375		      /* data_ptr */ (u_int8_t *)data,
3376		      /* dxfer_len */ len,
3377		      /* sense_len */ SSD_FULL_SIZE,
3378		      sizeof(struct scsi_read_subchannel),
3379 		      /* timeout */ 50000);
3380
3381	scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3382	bzero (scsi_cmd, sizeof(*scsi_cmd));
3383
3384	scsi_cmd->op_code = READ_SUBCHANNEL;
3385	if (mode == CD_MSF_FORMAT)
3386		scsi_cmd->byte1 |= CD_MSF;
3387	scsi_cmd->byte2 = SRS_SUBQ;
3388	scsi_cmd->subchan_format = format;
3389	scsi_cmd->track = track;
3390	scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3391	scsi_cmd->control = 0;
3392
3393	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3394			 /*sense_flags*/SF_RETRY_UA);
3395
3396	xpt_release_ccb(ccb);
3397
3398	return(error);
3399}
3400
3401
3402/*
3403 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3404 * routine.  See comments in cd6byteworkaround() for details.
3405 */
3406static int
3407cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3408	  u_int32_t page)
3409{
3410	struct ccb_scsiio *csio;
3411	struct cd_softc *softc;
3412	union ccb *ccb;
3413	int param_len;
3414	int error;
3415
3416	softc = (struct cd_softc *)periph->softc;
3417
3418	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3419
3420	csio = &ccb->csio;
3421
3422	data->cdb_size = softc->minimum_command_size;
3423	if (data->cdb_size < 10)
3424		param_len = sizeof(struct cd_mode_data);
3425	else
3426		param_len = sizeof(struct cd_mode_data_10);
3427
3428	/* Don't say we've got more room than we actually allocated */
3429	param_len = min(param_len, data->alloc_len);
3430
3431	scsi_mode_sense_len(csio,
3432			    /* retries */ cd_retry_count,
3433			    /* cbfcnp */ cddone,
3434			    /* tag_action */ MSG_SIMPLE_Q_TAG,
3435			    /* dbd */ 0,
3436			    /* page_code */ SMS_PAGE_CTRL_CURRENT,
3437			    /* page */ page,
3438			    /* param_buf */ data->mode_buf,
3439			    /* param_len */ param_len,
3440			    /* minimum_cmd_size */ softc->minimum_command_size,
3441			    /* sense_len */ SSD_FULL_SIZE,
3442			    /* timeout */ 50000);
3443
3444	/*
3445	 * It would be nice not to have to do this, but there's no
3446	 * available pointer in the CCB that would allow us to stuff the
3447	 * mode params structure in there and retrieve it in
3448	 * cd6byteworkaround(), so we can set the cdb size.  The cdb size
3449	 * lets the caller know what CDB size we ended up using, so they
3450	 * can find the actual mode page offset.
3451	 */
3452	STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3453
3454	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3455			 /*sense_flags*/SF_RETRY_UA);
3456
3457	xpt_release_ccb(ccb);
3458
3459	STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3460
3461	/*
3462	 * This is a bit of belt-and-suspenders checking, but if we run
3463	 * into a situation where the target sends back multiple block
3464	 * descriptors, we might not have enough space in the buffer to
3465	 * see the whole mode page.  Better to return an error than
3466	 * potentially access memory beyond our malloced region.
3467	 */
3468	if (error == 0) {
3469		u_int32_t data_len;
3470
3471		if (data->cdb_size == 10) {
3472			struct scsi_mode_header_10 *hdr10;
3473
3474			hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3475			data_len = scsi_2btoul(hdr10->data_length);
3476			data_len += sizeof(hdr10->data_length);
3477		} else {
3478			struct scsi_mode_header_6 *hdr6;
3479
3480			hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3481			data_len = hdr6->data_length;
3482			data_len += sizeof(hdr6->data_length);
3483		}
3484
3485		/*
3486		 * Complain if there is more mode data available than we
3487		 * allocated space for.  This could potentially happen if
3488		 * we miscalculated the page length for some reason, if the
3489		 * drive returns multiple block descriptors, or if it sets
3490		 * the data length incorrectly.
3491		 */
3492		if (data_len > data->alloc_len) {
3493			xpt_print(periph->path, "allocated modepage %d length "
3494			    "%d < returned length %d\n", page, data->alloc_len,
3495			    data_len);
3496			error = ENOSPC;
3497		}
3498	}
3499	return (error);
3500}
3501
3502/*
3503 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3504 * routine.  See comments in cd6byteworkaround() for details.
3505 */
3506static int
3507cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3508{
3509	struct ccb_scsiio *csio;
3510	struct cd_softc *softc;
3511	union ccb *ccb;
3512	int cdb_size, param_len;
3513	int error;
3514
3515	softc = (struct cd_softc *)periph->softc;
3516
3517	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3518
3519	csio = &ccb->csio;
3520
3521	error = 0;
3522
3523	/*
3524	 * If the data is formatted for the 10 byte version of the mode
3525	 * select parameter list, we need to use the 10 byte CDB.
3526	 * Otherwise, we use whatever the stored minimum command size.
3527	 */
3528	if (data->cdb_size == 10)
3529		cdb_size = data->cdb_size;
3530	else
3531		cdb_size = softc->minimum_command_size;
3532
3533	if (cdb_size >= 10) {
3534		struct scsi_mode_header_10 *mode_header;
3535		u_int32_t data_len;
3536
3537		mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3538
3539		data_len = scsi_2btoul(mode_header->data_length);
3540
3541		scsi_ulto2b(0, mode_header->data_length);
3542		/*
3543		 * SONY drives do not allow a mode select with a medium_type
3544		 * value that has just been returned by a mode sense; use a
3545		 * medium_type of 0 (Default) instead.
3546		 */
3547		mode_header->medium_type = 0;
3548
3549		/*
3550		 * Pass back whatever the drive passed to us, plus the size
3551		 * of the data length field.
3552		 */
3553		param_len = data_len + sizeof(mode_header->data_length);
3554
3555	} else {
3556		struct scsi_mode_header_6 *mode_header;
3557
3558		mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3559
3560		param_len = mode_header->data_length + 1;
3561
3562		mode_header->data_length = 0;
3563		/*
3564		 * SONY drives do not allow a mode select with a medium_type
3565		 * value that has just been returned by a mode sense; use a
3566		 * medium_type of 0 (Default) instead.
3567		 */
3568		mode_header->medium_type = 0;
3569	}
3570
3571	/* Don't say we've got more room than we actually allocated */
3572	param_len = min(param_len, data->alloc_len);
3573
3574	scsi_mode_select_len(csio,
3575			     /* retries */ cd_retry_count,
3576			     /* cbfcnp */ cddone,
3577			     /* tag_action */ MSG_SIMPLE_Q_TAG,
3578			     /* scsi_page_fmt */ 1,
3579			     /* save_pages */ 0,
3580			     /* param_buf */ data->mode_buf,
3581			     /* param_len */ param_len,
3582			     /* minimum_cmd_size */ cdb_size,
3583			     /* sense_len */ SSD_FULL_SIZE,
3584			     /* timeout */ 50000);
3585
3586	/* See comments in cdgetmode() and cd6byteworkaround(). */
3587	STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3588
3589	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3590			 /*sense_flags*/SF_RETRY_UA);
3591
3592	xpt_release_ccb(ccb);
3593
3594	STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3595
3596	return (error);
3597}
3598
3599
3600static int
3601cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3602{
3603	struct ccb_scsiio *csio;
3604	union ccb *ccb;
3605	int error;
3606	u_int8_t cdb_len;
3607
3608	error = 0;
3609	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3610	csio = &ccb->csio;
3611	/*
3612	 * Use the smallest possible command to perform the operation.
3613	 */
3614	if ((len & 0xffff0000) == 0) {
3615		/*
3616		 * We can fit in a 10 byte cdb.
3617		 */
3618		struct scsi_play_10 *scsi_cmd;
3619
3620		scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3621		bzero (scsi_cmd, sizeof(*scsi_cmd));
3622		scsi_cmd->op_code = PLAY_10;
3623		scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3624		scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3625		cdb_len = sizeof(*scsi_cmd);
3626	} else  {
3627		struct scsi_play_12 *scsi_cmd;
3628
3629		scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3630		bzero (scsi_cmd, sizeof(*scsi_cmd));
3631		scsi_cmd->op_code = PLAY_12;
3632		scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3633		scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3634		cdb_len = sizeof(*scsi_cmd);
3635	}
3636	cam_fill_csio(csio,
3637		      /*retries*/ cd_retry_count,
3638		      cddone,
3639		      /*flags*/CAM_DIR_NONE,
3640		      MSG_SIMPLE_Q_TAG,
3641		      /*dataptr*/NULL,
3642		      /*datalen*/0,
3643		      /*sense_len*/SSD_FULL_SIZE,
3644		      cdb_len,
3645		      /*timeout*/50 * 1000);
3646
3647	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3648			 /*sense_flags*/SF_RETRY_UA);
3649
3650	xpt_release_ccb(ccb);
3651
3652	return(error);
3653}
3654
3655static int
3656cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3657	  u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3658{
3659	struct scsi_play_msf *scsi_cmd;
3660        struct ccb_scsiio *csio;
3661	union ccb *ccb;
3662	int error;
3663
3664	error = 0;
3665
3666	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3667
3668	csio = &ccb->csio;
3669
3670	cam_fill_csio(csio,
3671		      /* retries */ cd_retry_count,
3672		      /* cbfcnp */ cddone,
3673		      /* flags */ CAM_DIR_NONE,
3674		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3675		      /* data_ptr */ NULL,
3676		      /* dxfer_len */ 0,
3677		      /* sense_len */ SSD_FULL_SIZE,
3678		      sizeof(struct scsi_play_msf),
3679 		      /* timeout */ 50000);
3680
3681	scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3682	bzero (scsi_cmd, sizeof(*scsi_cmd));
3683
3684        scsi_cmd->op_code = PLAY_MSF;
3685        scsi_cmd->start_m = startm;
3686        scsi_cmd->start_s = starts;
3687        scsi_cmd->start_f = startf;
3688        scsi_cmd->end_m = endm;
3689        scsi_cmd->end_s = ends;
3690        scsi_cmd->end_f = endf;
3691
3692	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3693			 /*sense_flags*/SF_RETRY_UA);
3694
3695	xpt_release_ccb(ccb);
3696
3697	return(error);
3698}
3699
3700
3701static int
3702cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3703	     u_int32_t etrack, u_int32_t eindex)
3704{
3705	struct scsi_play_track *scsi_cmd;
3706        struct ccb_scsiio *csio;
3707	union ccb *ccb;
3708	int error;
3709
3710	error = 0;
3711
3712	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3713
3714	csio = &ccb->csio;
3715
3716	cam_fill_csio(csio,
3717		      /* retries */ cd_retry_count,
3718		      /* cbfcnp */ cddone,
3719		      /* flags */ CAM_DIR_NONE,
3720		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3721		      /* data_ptr */ NULL,
3722		      /* dxfer_len */ 0,
3723		      /* sense_len */ SSD_FULL_SIZE,
3724		      sizeof(struct scsi_play_track),
3725 		      /* timeout */ 50000);
3726
3727	scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3728	bzero (scsi_cmd, sizeof(*scsi_cmd));
3729
3730        scsi_cmd->op_code = PLAY_TRACK;
3731        scsi_cmd->start_track = strack;
3732        scsi_cmd->start_index = sindex;
3733        scsi_cmd->end_track = etrack;
3734        scsi_cmd->end_index = eindex;
3735
3736	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3737			 /*sense_flags*/SF_RETRY_UA);
3738
3739	xpt_release_ccb(ccb);
3740
3741	return(error);
3742}
3743
3744static int
3745cdpause(struct cam_periph *periph, u_int32_t go)
3746{
3747	struct scsi_pause *scsi_cmd;
3748        struct ccb_scsiio *csio;
3749	union ccb *ccb;
3750	int error;
3751
3752	error = 0;
3753
3754	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3755
3756	csio = &ccb->csio;
3757
3758	cam_fill_csio(csio,
3759		      /* retries */ cd_retry_count,
3760		      /* cbfcnp */ cddone,
3761		      /* flags */ CAM_DIR_NONE,
3762		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3763		      /* data_ptr */ NULL,
3764		      /* dxfer_len */ 0,
3765		      /* sense_len */ SSD_FULL_SIZE,
3766		      sizeof(struct scsi_pause),
3767 		      /* timeout */ 50000);
3768
3769	scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3770	bzero (scsi_cmd, sizeof(*scsi_cmd));
3771
3772        scsi_cmd->op_code = PAUSE;
3773	scsi_cmd->resume = go;
3774
3775	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3776			 /*sense_flags*/SF_RETRY_UA);
3777
3778	xpt_release_ccb(ccb);
3779
3780	return(error);
3781}
3782
3783static int
3784cdstartunit(struct cam_periph *periph, int load)
3785{
3786	union ccb *ccb;
3787	int error;
3788
3789	error = 0;
3790
3791	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3792
3793	scsi_start_stop(&ccb->csio,
3794			/* retries */ cd_retry_count,
3795			/* cbfcnp */ cddone,
3796			/* tag_action */ MSG_SIMPLE_Q_TAG,
3797			/* start */ TRUE,
3798			/* load_eject */ load,
3799			/* immediate */ FALSE,
3800			/* sense_len */ SSD_FULL_SIZE,
3801			/* timeout */ 50000);
3802
3803	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3804			 /*sense_flags*/SF_RETRY_UA);
3805
3806	xpt_release_ccb(ccb);
3807
3808	return(error);
3809}
3810
3811static int
3812cdstopunit(struct cam_periph *periph, u_int32_t eject)
3813{
3814	union ccb *ccb;
3815	int error;
3816
3817	error = 0;
3818
3819	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3820
3821	scsi_start_stop(&ccb->csio,
3822			/* retries */ cd_retry_count,
3823			/* cbfcnp */ cddone,
3824			/* tag_action */ MSG_SIMPLE_Q_TAG,
3825			/* start */ FALSE,
3826			/* load_eject */ eject,
3827			/* immediate */ FALSE,
3828			/* sense_len */ SSD_FULL_SIZE,
3829			/* timeout */ 50000);
3830
3831	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3832			 /*sense_flags*/SF_RETRY_UA);
3833
3834	xpt_release_ccb(ccb);
3835
3836	return(error);
3837}
3838
3839static int
3840cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3841{
3842	struct scsi_set_speed *scsi_cmd;
3843	struct ccb_scsiio *csio;
3844	union ccb *ccb;
3845	int error;
3846
3847	error = 0;
3848	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3849	csio = &ccb->csio;
3850
3851	/* Preserve old behavior: units in multiples of CDROM speed */
3852	if (rdspeed < 177)
3853		rdspeed *= 177;
3854	if (wrspeed < 177)
3855		wrspeed *= 177;
3856
3857	cam_fill_csio(csio,
3858		      /* retries */ cd_retry_count,
3859		      /* cbfcnp */ cddone,
3860		      /* flags */ CAM_DIR_NONE,
3861		      /* tag_action */ MSG_SIMPLE_Q_TAG,
3862		      /* data_ptr */ NULL,
3863		      /* dxfer_len */ 0,
3864		      /* sense_len */ SSD_FULL_SIZE,
3865		      sizeof(struct scsi_set_speed),
3866 		      /* timeout */ 50000);
3867
3868	scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3869	bzero(scsi_cmd, sizeof(*scsi_cmd));
3870
3871	scsi_cmd->opcode = SET_CD_SPEED;
3872	scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3873	scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3874
3875	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3876			 /*sense_flags*/SF_RETRY_UA);
3877
3878	xpt_release_ccb(ccb);
3879
3880	return(error);
3881}
3882
3883static int
3884cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3885{
3886	union ccb *ccb;
3887	u_int8_t *databuf;
3888	u_int32_t lba;
3889	int error;
3890	int length;
3891
3892	error = 0;
3893	databuf = NULL;
3894	lba = 0;
3895
3896	switch (authinfo->format) {
3897	case DVD_REPORT_AGID:
3898		length = sizeof(struct scsi_report_key_data_agid);
3899		break;
3900	case DVD_REPORT_CHALLENGE:
3901		length = sizeof(struct scsi_report_key_data_challenge);
3902		break;
3903	case DVD_REPORT_KEY1:
3904		length = sizeof(struct scsi_report_key_data_key1_key2);
3905		break;
3906	case DVD_REPORT_TITLE_KEY:
3907		length = sizeof(struct scsi_report_key_data_title);
3908		/* The lba field is only set for the title key */
3909		lba = authinfo->lba;
3910		break;
3911	case DVD_REPORT_ASF:
3912		length = sizeof(struct scsi_report_key_data_asf);
3913		break;
3914	case DVD_REPORT_RPC:
3915		length = sizeof(struct scsi_report_key_data_rpc);
3916		break;
3917	case DVD_INVALIDATE_AGID:
3918		length = 0;
3919		break;
3920	default:
3921		return (EINVAL);
3922	}
3923
3924	if (length != 0) {
3925		databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
3926	} else
3927		databuf = NULL;
3928
3929	cam_periph_lock(periph);
3930	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
3931
3932	scsi_report_key(&ccb->csio,
3933			/* retries */ cd_retry_count,
3934			/* cbfcnp */ cddone,
3935			/* tag_action */ MSG_SIMPLE_Q_TAG,
3936			/* lba */ lba,
3937			/* agid */ authinfo->agid,
3938			/* key_format */ authinfo->format,
3939			/* data_ptr */ databuf,
3940			/* dxfer_len */ length,
3941			/* sense_len */ SSD_FULL_SIZE,
3942			/* timeout */ 50000);
3943
3944	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3945			 /*sense_flags*/SF_RETRY_UA);
3946
3947	if (error != 0)
3948		goto bailout;
3949
3950	if (ccb->csio.resid != 0) {
3951		xpt_print(periph->path, "warning, residual for report key "
3952		    "command is %d\n", ccb->csio.resid);
3953	}
3954
3955	switch(authinfo->format) {
3956	case DVD_REPORT_AGID: {
3957		struct scsi_report_key_data_agid *agid_data;
3958
3959		agid_data = (struct scsi_report_key_data_agid *)databuf;
3960
3961		authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3962			RKD_AGID_SHIFT;
3963		break;
3964	}
3965	case DVD_REPORT_CHALLENGE: {
3966		struct scsi_report_key_data_challenge *chal_data;
3967
3968		chal_data = (struct scsi_report_key_data_challenge *)databuf;
3969
3970		bcopy(chal_data->challenge_key, authinfo->keychal,
3971		      min(sizeof(chal_data->challenge_key),
3972		          sizeof(authinfo->keychal)));
3973		break;
3974	}
3975	case DVD_REPORT_KEY1: {
3976		struct scsi_report_key_data_key1_key2 *key1_data;
3977
3978		key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3979
3980		bcopy(key1_data->key1, authinfo->keychal,
3981		      min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3982		break;
3983	}
3984	case DVD_REPORT_TITLE_KEY: {
3985		struct scsi_report_key_data_title *title_data;
3986
3987		title_data = (struct scsi_report_key_data_title *)databuf;
3988
3989		authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3990			RKD_TITLE_CPM_SHIFT;
3991		authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3992			RKD_TITLE_CP_SEC_SHIFT;
3993		authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3994			RKD_TITLE_CMGS_SHIFT;
3995		bcopy(title_data->title_key, authinfo->keychal,
3996		      min(sizeof(title_data->title_key),
3997			  sizeof(authinfo->keychal)));
3998		break;
3999	}
4000	case DVD_REPORT_ASF: {
4001		struct scsi_report_key_data_asf *asf_data;
4002
4003		asf_data = (struct scsi_report_key_data_asf *)databuf;
4004
4005		authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
4006		break;
4007	}
4008	case DVD_REPORT_RPC: {
4009		struct scsi_report_key_data_rpc *rpc_data;
4010
4011		rpc_data = (struct scsi_report_key_data_rpc *)databuf;
4012
4013		authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
4014			RKD_RPC_TYPE_SHIFT;
4015		authinfo->vend_rsts =
4016			(rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
4017			RKD_RPC_VENDOR_RESET_SHIFT;
4018		authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4019		authinfo->region = rpc_data->region_mask;
4020		authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4021		break;
4022	}
4023	case DVD_INVALIDATE_AGID:
4024		break;
4025	default:
4026		/* This should be impossible, since we checked above */
4027		error = EINVAL;
4028		goto bailout;
4029		break; /* NOTREACHED */
4030	}
4031
4032bailout:
4033	xpt_release_ccb(ccb);
4034	cam_periph_unlock(periph);
4035
4036	if (databuf != NULL)
4037		free(databuf, M_DEVBUF);
4038
4039	return(error);
4040}
4041
4042static int
4043cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4044{
4045	union ccb *ccb;
4046	u_int8_t *databuf;
4047	int length;
4048	int error;
4049
4050	error = 0;
4051	databuf = NULL;
4052
4053	switch(authinfo->format) {
4054	case DVD_SEND_CHALLENGE: {
4055		struct scsi_report_key_data_challenge *challenge_data;
4056
4057		length = sizeof(*challenge_data);
4058
4059		challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4060
4061		databuf = (u_int8_t *)challenge_data;
4062
4063		scsi_ulto2b(length - sizeof(challenge_data->data_len),
4064			    challenge_data->data_len);
4065
4066		bcopy(authinfo->keychal, challenge_data->challenge_key,
4067		      min(sizeof(authinfo->keychal),
4068			  sizeof(challenge_data->challenge_key)));
4069		break;
4070	}
4071	case DVD_SEND_KEY2: {
4072		struct scsi_report_key_data_key1_key2 *key2_data;
4073
4074		length = sizeof(*key2_data);
4075
4076		key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4077
4078		databuf = (u_int8_t *)key2_data;
4079
4080		scsi_ulto2b(length - sizeof(key2_data->data_len),
4081			    key2_data->data_len);
4082
4083		bcopy(authinfo->keychal, key2_data->key1,
4084		      min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4085
4086		break;
4087	}
4088	case DVD_SEND_RPC: {
4089		struct scsi_send_key_data_rpc *rpc_data;
4090
4091		length = sizeof(*rpc_data);
4092
4093		rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4094
4095		databuf = (u_int8_t *)rpc_data;
4096
4097		scsi_ulto2b(length - sizeof(rpc_data->data_len),
4098			    rpc_data->data_len);
4099
4100		rpc_data->region_code = authinfo->region;
4101		break;
4102	}
4103	default:
4104		return (EINVAL);
4105	}
4106
4107	cam_periph_lock(periph);
4108	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4109
4110	scsi_send_key(&ccb->csio,
4111		      /* retries */ cd_retry_count,
4112		      /* cbfcnp */ cddone,
4113		      /* tag_action */ MSG_SIMPLE_Q_TAG,
4114		      /* agid */ authinfo->agid,
4115		      /* key_format */ authinfo->format,
4116		      /* data_ptr */ databuf,
4117		      /* dxfer_len */ length,
4118		      /* sense_len */ SSD_FULL_SIZE,
4119		      /* timeout */ 50000);
4120
4121	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4122			 /*sense_flags*/SF_RETRY_UA);
4123
4124	xpt_release_ccb(ccb);
4125	cam_periph_unlock(periph);
4126
4127	if (databuf != NULL)
4128		free(databuf, M_DEVBUF);
4129
4130	return(error);
4131}
4132
4133static int
4134cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4135{
4136	union ccb *ccb;
4137	u_int8_t *databuf;
4138	u_int32_t address;
4139	int error;
4140	int length;
4141
4142	error = 0;
4143	databuf = NULL;
4144	/* The address is reserved for many of the formats */
4145	address = 0;
4146
4147	switch(dvdstruct->format) {
4148	case DVD_STRUCT_PHYSICAL:
4149		length = sizeof(struct scsi_read_dvd_struct_data_physical);
4150		break;
4151	case DVD_STRUCT_COPYRIGHT:
4152		length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4153		break;
4154	case DVD_STRUCT_DISCKEY:
4155		length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4156		break;
4157	case DVD_STRUCT_BCA:
4158		length = sizeof(struct scsi_read_dvd_struct_data_bca);
4159		break;
4160	case DVD_STRUCT_MANUFACT:
4161		length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4162		break;
4163	case DVD_STRUCT_CMI:
4164		return (ENODEV);
4165	case DVD_STRUCT_PROTDISCID:
4166		length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4167		break;
4168	case DVD_STRUCT_DISCKEYBLOCK:
4169		length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4170		break;
4171	case DVD_STRUCT_DDS:
4172		length = sizeof(struct scsi_read_dvd_struct_data_dds);
4173		break;
4174	case DVD_STRUCT_MEDIUM_STAT:
4175		length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4176		break;
4177	case DVD_STRUCT_SPARE_AREA:
4178		length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4179		break;
4180	case DVD_STRUCT_RMD_LAST:
4181		return (ENODEV);
4182	case DVD_STRUCT_RMD_RMA:
4183		return (ENODEV);
4184	case DVD_STRUCT_PRERECORDED:
4185		length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4186		break;
4187	case DVD_STRUCT_UNIQUEID:
4188		length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4189		break;
4190	case DVD_STRUCT_DCB:
4191		return (ENODEV);
4192	case DVD_STRUCT_LIST:
4193		/*
4194		 * This is the maximum allocation length for the READ DVD
4195		 * STRUCTURE command.  There's nothing in the MMC3 spec
4196		 * that indicates a limit in the amount of data that can
4197		 * be returned from this call, other than the limits
4198		 * imposed by the 2-byte length variables.
4199		 */
4200		length = 65535;
4201		break;
4202	default:
4203		return (EINVAL);
4204	}
4205
4206	if (length != 0) {
4207		databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO);
4208	} else
4209		databuf = NULL;
4210
4211	cam_periph_lock(periph);
4212	ccb = cdgetccb(periph, CAM_PRIORITY_NORMAL);
4213
4214	scsi_read_dvd_structure(&ccb->csio,
4215				/* retries */ cd_retry_count,
4216				/* cbfcnp */ cddone,
4217				/* tag_action */ MSG_SIMPLE_Q_TAG,
4218				/* lba */ address,
4219				/* layer_number */ dvdstruct->layer_num,
4220				/* key_format */ dvdstruct->format,
4221				/* agid */ dvdstruct->agid,
4222				/* data_ptr */ databuf,
4223				/* dxfer_len */ length,
4224				/* sense_len */ SSD_FULL_SIZE,
4225				/* timeout */ 50000);
4226
4227	error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4228			 /*sense_flags*/SF_RETRY_UA);
4229
4230	if (error != 0)
4231		goto bailout;
4232
4233	switch(dvdstruct->format) {
4234	case DVD_STRUCT_PHYSICAL: {
4235		struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4236		struct dvd_layer *outlayer;
4237		struct scsi_read_dvd_struct_data_physical *phys_data;
4238
4239		phys_data =
4240			(struct scsi_read_dvd_struct_data_physical *)databuf;
4241		inlayer = &phys_data->layer_desc;
4242		outlayer = (struct dvd_layer *)&dvdstruct->data;
4243
4244		dvdstruct->length = sizeof(*inlayer);
4245
4246		outlayer->book_type = (inlayer->book_type_version &
4247			RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4248		outlayer->book_version = (inlayer->book_type_version &
4249			RDSD_BOOK_VERSION_MASK);
4250		outlayer->disc_size = (inlayer->disc_size_max_rate &
4251			RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4252		outlayer->max_rate = (inlayer->disc_size_max_rate &
4253			RDSD_MAX_RATE_MASK);
4254		outlayer->nlayers = (inlayer->layer_info &
4255			RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4256		outlayer->track_path = (inlayer->layer_info &
4257			RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4258		outlayer->layer_type = (inlayer->layer_info &
4259			RDSD_LAYER_TYPE_MASK);
4260		outlayer->linear_density = (inlayer->density &
4261			RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4262		outlayer->track_density = (inlayer->density &
4263			RDSD_TRACK_DENSITY_MASK);
4264		outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4265			RDSD_BCA_SHIFT;
4266		outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4267		outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4268		outlayer->end_sector_l0 =
4269			scsi_3btoul(inlayer->end_sector_layer0);
4270		break;
4271	}
4272	case DVD_STRUCT_COPYRIGHT: {
4273		struct scsi_read_dvd_struct_data_copyright *copy_data;
4274
4275		copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4276			databuf;
4277
4278		dvdstruct->cpst = copy_data->cps_type;
4279		dvdstruct->rmi = copy_data->region_info;
4280		dvdstruct->length = 0;
4281
4282		break;
4283	}
4284	default:
4285		/*
4286		 * Tell the user what the overall length is, no matter
4287		 * what we can actually fit in the data buffer.
4288		 */
4289		dvdstruct->length = length - ccb->csio.resid -
4290			sizeof(struct scsi_read_dvd_struct_data_header);
4291
4292		/*
4293		 * But only actually copy out the smaller of what we read
4294		 * in or what the structure can take.
4295		 */
4296		bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4297		      dvdstruct->data,
4298		      min(sizeof(dvdstruct->data), dvdstruct->length));
4299		break;
4300	}
4301
4302bailout:
4303	xpt_release_ccb(ccb);
4304	cam_periph_unlock(periph);
4305
4306	if (databuf != NULL)
4307		free(databuf, M_DEVBUF);
4308
4309	return(error);
4310}
4311
4312void
4313scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4314		void (*cbfcnp)(struct cam_periph *, union ccb *),
4315		u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4316		u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4317		u_int8_t sense_len, u_int32_t timeout)
4318{
4319	struct scsi_report_key *scsi_cmd;
4320
4321	scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4322	bzero(scsi_cmd, sizeof(*scsi_cmd));
4323	scsi_cmd->opcode = REPORT_KEY;
4324	scsi_ulto4b(lba, scsi_cmd->lba);
4325	scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4326	scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4327		(key_format & RK_KF_KEYFORMAT_MASK);
4328
4329	cam_fill_csio(csio,
4330		      retries,
4331		      cbfcnp,
4332		      /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4333		      tag_action,
4334		      /*data_ptr*/ data_ptr,
4335		      /*dxfer_len*/ dxfer_len,
4336		      sense_len,
4337		      sizeof(*scsi_cmd),
4338		      timeout);
4339}
4340
4341void
4342scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4343	      void (*cbfcnp)(struct cam_periph *, union ccb *),
4344	      u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4345	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4346	      u_int32_t timeout)
4347{
4348	struct scsi_send_key *scsi_cmd;
4349
4350	scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4351	bzero(scsi_cmd, sizeof(*scsi_cmd));
4352	scsi_cmd->opcode = SEND_KEY;
4353
4354	scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4355	scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4356		(key_format & RK_KF_KEYFORMAT_MASK);
4357
4358	cam_fill_csio(csio,
4359		      retries,
4360		      cbfcnp,
4361		      /*flags*/ CAM_DIR_OUT,
4362		      tag_action,
4363		      /*data_ptr*/ data_ptr,
4364		      /*dxfer_len*/ dxfer_len,
4365		      sense_len,
4366		      sizeof(*scsi_cmd),
4367		      timeout);
4368}
4369
4370
4371void
4372scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4373			void (*cbfcnp)(struct cam_periph *, union ccb *),
4374			u_int8_t tag_action, u_int32_t address,
4375			u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4376			u_int8_t *data_ptr, u_int32_t dxfer_len,
4377			u_int8_t sense_len, u_int32_t timeout)
4378{
4379	struct scsi_read_dvd_structure *scsi_cmd;
4380
4381	scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4382	bzero(scsi_cmd, sizeof(*scsi_cmd));
4383	scsi_cmd->opcode = READ_DVD_STRUCTURE;
4384
4385	scsi_ulto4b(address, scsi_cmd->address);
4386	scsi_cmd->layer_number = layer_number;
4387	scsi_cmd->format = format;
4388	scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4389	/* The AGID is the top two bits of this byte */
4390	scsi_cmd->agid = agid << 6;
4391
4392	cam_fill_csio(csio,
4393		      retries,
4394		      cbfcnp,
4395		      /*flags*/ CAM_DIR_IN,
4396		      tag_action,
4397		      /*data_ptr*/ data_ptr,
4398		      /*dxfer_len*/ dxfer_len,
4399		      sense_len,
4400		      sizeof(*scsi_cmd),
4401		      timeout);
4402}
4403