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