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