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