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