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