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