Fix multiple bugs in CAM related devices which go away unexpectedly. This
[dragonfly.git] / sys / bus / cam / scsi / scsi_cd.c
blobabd2463641a0f62d9d03dd418d052dc1d6fd7823
1 /*
2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2003 Kenneth D. Merry.
4 * All rights reserved.
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.
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.
27 * $FreeBSD: src/sys/cam/scsi/scsi_cd.c,v 1.31.2.16 2003/10/21 22:26:11 thomas Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_cd.c,v 1.44 2008/07/18 00:07:23 dillon Exp $
31 * Portions of this driver taken from the original FreeBSD cd driver.
32 * Written by Julian Elischer (julian@tfs.com)
33 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 * TRW Financial Systems, in accordance with their agreement with Carnegie
36 * Mellon University, makes this software available to CMU to distribute
37 * or use in any manner that they see fit as long as this message is kept with
38 * the software. For this reason TFS also grants any other persons or
39 * organisations permission to use or modify this software.
41 * TFS supplies this software to be publicly redistributed
42 * on the understanding that TFS is not responsible for the correct
43 * functioning of this software in any circumstances.
45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $
50 #include "opt_cd.h"
52 #include <sys/param.h>
53 #include <sys/bootmaj.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/buf.h>
57 #include <sys/conf.h>
58 #include <sys/disk.h>
59 #include <sys/dtype.h>
60 #include <sys/malloc.h>
61 #include <sys/cdio.h>
62 #include <sys/cdrio.h>
63 #include <sys/dvdio.h>
64 #include <sys/devicestat.h>
65 #include <sys/sysctl.h>
66 #include <sys/taskqueue.h>
67 #include <sys/proc.h>
68 #include <sys/buf2.h>
69 #include <sys/thread2.h>
71 #include "../cam.h"
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
76 #include "../cam_queue.h"
77 #include "../cam_sim.h"
79 #include "scsi_message.h"
80 #include "scsi_da.h"
81 #include "scsi_cd.h"
83 #define LEADOUT 0xaa /* leadout toc entry */
85 struct cd_params {
86 u_int32_t blksize;
87 u_long disksize;
90 typedef enum {
91 CD_Q_NONE = 0x00,
92 CD_Q_NO_TOUCH = 0x01,
93 CD_Q_BCD_TRACKS = 0x02,
94 CD_Q_NO_CHANGER = 0x04,
95 CD_Q_CHANGER = 0x08,
96 CD_Q_10_BYTE_ONLY = 0x10
97 } cd_quirks;
99 typedef enum {
100 CD_FLAG_INVALID = 0x0001,
101 CD_FLAG_NEW_DISC = 0x0002,
102 CD_FLAG_DISC_LOCKED = 0x0004,
103 CD_FLAG_DISC_REMOVABLE = 0x0008,
104 CD_FLAG_TAGGED_QUEUING = 0x0010,
105 CD_FLAG_CHANGER = 0x0040,
106 CD_FLAG_ACTIVE = 0x0080,
107 CD_FLAG_SCHED_ON_COMP = 0x0100,
108 CD_FLAG_RETRY_UA = 0x0200,
109 CD_FLAG_VALID_MEDIA = 0x0400,
110 CD_FLAG_VALID_TOC = 0x0800,
111 CD_FLAG_SCTX_INIT = 0x1000,
112 CD_FLAG_OPEN = 0x2000
113 } cd_flags;
115 typedef enum {
116 CD_CCB_PROBE = 0x01,
117 CD_CCB_BUFFER_IO = 0x02,
118 CD_CCB_WAITING = 0x03,
119 CD_CCB_TYPE_MASK = 0x0F,
120 CD_CCB_RETRY_UA = 0x10
121 } cd_ccb_state;
123 typedef enum {
124 CHANGER_TIMEOUT_SCHED = 0x01,
125 CHANGER_SHORT_TMOUT_SCHED = 0x02,
126 CHANGER_MANUAL_CALL = 0x04,
127 CHANGER_NEED_TIMEOUT = 0x08
128 } cd_changer_flags;
130 #define ccb_state ppriv_field0
131 #define ccb_bio ppriv_ptr1
133 struct cd_tocdata {
134 struct ioc_toc_header header;
135 struct cd_toc_entry entries[100];
138 struct cd_toc_single {
139 struct ioc_toc_header header;
140 struct cd_toc_entry entry;
143 typedef enum {
144 CD_STATE_PROBE,
145 CD_STATE_NORMAL
146 } cd_state;
148 struct cd_softc {
149 cam_pinfo pinfo;
150 cd_state state;
151 volatile cd_flags flags;
152 struct bio_queue_head bio_queue;
153 LIST_HEAD(, ccb_hdr) pending_ccbs;
154 struct cd_params params;
155 struct disk disk;
156 union ccb saved_ccb;
157 cd_quirks quirks;
158 struct devstat device_stats;
159 STAILQ_ENTRY(cd_softc) changer_links;
160 struct cdchanger *changer;
161 int bufs_left;
162 struct cam_periph *periph;
163 int minimum_command_size;
164 int outstanding_cmds;
165 struct task sysctl_task;
166 struct sysctl_ctx_list sysctl_ctx;
167 struct sysctl_oid *sysctl_tree;
168 STAILQ_HEAD(, cd_mode_params) mode_queue;
169 struct cd_tocdata toc;
172 struct cd_page_sizes {
173 int page;
174 int page_size;
177 static struct cd_page_sizes cd_page_size_table[] =
179 { AUDIO_PAGE, sizeof(struct cd_audio_page)}
182 struct cd_quirk_entry {
183 struct scsi_inquiry_pattern inq_pat;
184 cd_quirks quirks;
188 * The changer quirk entries aren't strictly necessary. Basically, what
189 * they do is tell cdregister() up front that a device is a changer.
190 * Otherwise, it will figure that fact out once it sees a LUN on the device
191 * that is greater than 0. If it is known up front that a device is a changer,
192 * all I/O to the device will go through the changer scheduling routines, as
193 * opposed to the "normal" CD code.
195 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because
196 * your device hangs when it gets a 10 byte command. Adding a quirk just
197 * to get rid of the informative diagnostic message is not acceptable. All
198 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be
199 * referenced in a comment along with the quirk) , and must be approved by
200 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may
201 * be removed until the submitter can explain why they are needed.
202 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary)
203 * when the CAM_NEW_TRAN_CODE work is done.
205 static struct cd_quirk_entry cd_quirk_table[] =
208 { T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
209 /*quirks*/ CD_Q_CHANGER
212 { T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
213 "*"}, /* quirks */ CD_Q_CHANGER
216 { T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
217 /* quirks */ CD_Q_CHANGER
220 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
221 /* quirks */ CD_Q_BCD_TRACKS
225 static d_open_t cdopen;
226 static d_close_t cdclose;
227 static d_ioctl_t cdioctl;
228 static d_strategy_t cdstrategy;
230 static periph_init_t cdinit;
231 static periph_ctor_t cdregister;
232 static periph_dtor_t cdcleanup;
233 static periph_start_t cdstart;
234 static periph_oninv_t cdoninvalidate;
235 static void cdasync(void *callback_arg, u_int32_t code,
236 struct cam_path *path, void *arg);
237 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
238 static void cdshorttimeout(void *arg);
239 static void cdschedule(struct cam_periph *periph, int priority);
240 static void cdrunchangerqueue(void *arg);
241 static void cdchangerschedule(struct cd_softc *softc);
242 static int cdrunccb(union ccb *ccb,
243 int (*error_routine)(union ccb *ccb,
244 u_int32_t cam_flags,
245 u_int32_t sense_flags),
246 u_int32_t cam_flags, u_int32_t sense_flags);
247 static union ccb *cdgetccb(struct cam_periph *periph,
248 u_int32_t priority);
249 static void cddone(struct cam_periph *periph,
250 union ccb *start_ccb);
251 static int cderror(union ccb *ccb, u_int32_t cam_flags,
252 u_int32_t sense_flags);
253 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
254 static int cdgetpagesize(int page_num);
255 static void cdprevent(struct cam_periph *periph, int action);
256 static int cdcheckmedia(struct cam_periph *periph);
257 static int cdsize(struct cam_periph *periph, u_int32_t *size);
258 static int cd6byteworkaround(union ccb *ccb);
259 static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
260 u_int32_t start, u_int8_t *data,
261 u_int32_t len, u_int32_t sense_flags);
262 static int cdgetmode(struct cam_periph *periph,
263 struct cd_mode_params *data, u_int32_t page);
264 static int cdsetmode(struct cam_periph *periph,
265 struct cd_mode_params *data);
266 static int cdplay(struct cam_periph *periph, u_int32_t blk,
267 u_int32_t len);
268 static int cdreadsubchannel(struct cam_periph *periph,
269 u_int32_t mode, u_int32_t format,
270 int track,
271 struct cd_sub_channel_info *data,
272 u_int32_t len);
273 static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
274 u_int32_t starts, u_int32_t startf,
275 u_int32_t endm, u_int32_t ends,
276 u_int32_t endf);
277 static int cdplaytracks(struct cam_periph *periph,
278 u_int32_t strack, u_int32_t sindex,
279 u_int32_t etrack, u_int32_t eindex);
280 static int cdpause(struct cam_periph *periph, u_int32_t go);
281 static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
282 static int cdstartunit(struct cam_periph *periph, int load);
283 static int cdsetspeed(struct cam_periph *periph,
284 u_int32_t rdspeed, u_int32_t wrspeed);
285 static int cdreportkey(struct cam_periph *periph,
286 struct dvd_authinfo *authinfo);
287 static int cdsendkey(struct cam_periph *periph,
288 struct dvd_authinfo *authinfo);
289 static int cdreaddvdstructure(struct cam_periph *periph,
290 struct dvd_struct *dvdstruct);
292 static struct periph_driver cddriver =
294 cdinit, "cd",
295 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
298 PERIPHDRIVER_DECLARE(cd, cddriver);
300 static struct dev_ops cd_ops = {
301 { "cd", SCSICD_CDEV_MAJOR, D_DISK },
302 .d_open = cdopen,
303 .d_close = cdclose,
304 .d_read = physread,
305 .d_write = physwrite,
306 .d_ioctl = cdioctl,
307 .d_strategy = cdstrategy
310 static struct extend_array *cdperiphs;
312 #ifndef CHANGER_MIN_BUSY_SECONDS
313 #define CHANGER_MIN_BUSY_SECONDS 5
314 #endif
315 #ifndef CHANGER_MAX_BUSY_SECONDS
316 #define CHANGER_MAX_BUSY_SECONDS 15
317 #endif
319 static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
320 static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
322 SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
323 SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
324 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
325 &changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
326 TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
327 SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
328 &changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
329 TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
331 struct cdchanger {
332 path_id_t path_id;
333 target_id_t target_id;
334 int num_devices;
335 struct camq devq;
336 struct timeval start_time;
337 struct cd_softc *cur_device;
338 struct callout short_handle;
339 struct callout long_handle;
340 volatile cd_changer_flags flags;
341 STAILQ_ENTRY(cdchanger) changer_links;
342 STAILQ_HEAD(chdevlist, cd_softc) chluns;
345 static struct lock changerq_lock;
346 static STAILQ_HEAD(changerlist, cdchanger) changerq;
347 static int num_changers;
349 MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
351 static void
352 cdinit(void)
354 cam_status status;
356 lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
357 STAILQ_INIT(&changerq);
360 * Create our extend array for storing the devices we attach to.
362 cdperiphs = cam_extend_new();
363 if (cdperiphs == NULL) {
364 kprintf("cd: Failed to alloc extend array!\n");
365 return;
369 * Install a global async callback. This callback will
370 * receive async callbacks like "new device found".
372 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
374 if (status != CAM_REQ_CMP) {
375 kprintf("cd: Failed to attach master async callback "
376 "due to status 0x%x!\n", status);
380 static void
381 cdoninvalidate(struct cam_periph *periph)
383 struct cd_softc *softc;
384 struct buf *q_bp;
385 struct bio *q_bio;
387 softc = (struct cd_softc *)periph->softc;
390 * De-register any async callbacks.
392 xpt_register_async(0, cdasync, periph, periph->path);
394 softc->flags |= CD_FLAG_INVALID;
397 * Return all queued I/O with ENXIO.
398 * XXX Handle any transactions queued to the card
399 * with XPT_ABORT_CCB.
401 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
402 bioq_remove(&softc->bio_queue, q_bio);
403 q_bp = q_bio->bio_buf;
404 q_bp->b_resid = q_bp->b_bcount;
405 q_bp->b_error = ENXIO;
406 q_bp->b_flags |= B_ERROR;
407 biodone(q_bio);
411 * If this device is part of a changer, and it was scheduled
412 * to run, remove it from the run queue since we just nuked
413 * all of its scheduled I/O.
415 if ((softc->flags & CD_FLAG_CHANGER)
416 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
417 camq_remove(&softc->changer->devq, softc->pinfo.index);
419 xpt_print(periph->path, "lost device\n");
422 static void
423 cdcleanup(struct cam_periph *periph)
425 struct cd_softc *softc;
427 softc = (struct cd_softc *)periph->softc;
429 xpt_print(periph->path, "removing device entry\n");
431 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
432 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
433 xpt_print(periph->path, "can't remove sysctl context\n");
437 * In the queued, non-active case, the device in question
438 * has already been removed from the changer run queue. Since this
439 * device is active, we need to de-activate it, and schedule
440 * another device to run. (if there is another one to run)
442 if ((softc->flags & CD_FLAG_CHANGER)
443 && (softc->flags & CD_FLAG_ACTIVE)) {
446 * The purpose of the short timeout is soley to determine
447 * whether the current device has finished or not. Well,
448 * since we're removing the active device, we know that it
449 * is finished. So, get rid of the short timeout.
450 * Otherwise, if we're in the time period before the short
451 * timeout fires, and there are no other devices in the
452 * queue to run, there won't be any other device put in the
453 * active slot. i.e., when we call cdrunchangerqueue()
454 * below, it won't do anything. Then, when the short
455 * timeout fires, it'll look at the "current device", which
456 * we are free below, and possibly panic the kernel on a
457 * bogus pointer reference.
459 * The long timeout doesn't really matter, since we
460 * decrement the qfrozen_cnt to indicate that there is
461 * nothing in the active slot now. Therefore, there won't
462 * be any bogus pointer references there.
464 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
465 callout_stop(&softc->changer->short_handle);
466 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
468 softc->changer->devq.qfrozen_cnt--;
469 softc->changer->flags |= CHANGER_MANUAL_CALL;
470 cdrunchangerqueue(softc->changer);
474 * If we're removing the last device on the changer, go ahead and
475 * remove the changer device structure.
477 if ((softc->flags & CD_FLAG_CHANGER)
478 && (--softc->changer->num_devices == 0)) {
481 * Theoretically, there shouldn't be any timeouts left, but
482 * I'm not completely sure that that will be the case. So,
483 * it won't hurt to check and see if there are any left.
485 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
486 callout_stop(&softc->changer->long_handle);
487 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
490 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
491 callout_stop(&softc->changer->short_handle);
492 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
495 lockmgr(&changerq_lock, LK_EXCLUSIVE);
496 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
497 changer_links);
498 num_changers--;
499 lockmgr(&changerq_lock, LK_RELEASE);
500 xpt_print(periph->path, "removing changer entry\n");
501 kfree(softc->changer, M_DEVBUF);
503 devstat_remove_entry(&softc->device_stats);
504 cam_extend_release(cdperiphs, periph->unit_number);
505 if (softc->disk.d_rawdev) {
506 cam_periph_unlock(periph);
507 disk_destroy(&softc->disk);
508 cam_periph_lock(periph);
510 kfree(softc, M_DEVBUF);
513 static void
514 cdasync(void *callback_arg, u_int32_t code,
515 struct cam_path *path, void *arg)
517 struct cam_periph *periph;
519 periph = (struct cam_periph *)callback_arg;
520 switch (code) {
521 case AC_FOUND_DEVICE:
523 struct ccb_getdev *cgd;
524 cam_status status;
526 cgd = (struct ccb_getdev *)arg;
527 if (cgd == NULL)
528 break;
530 if (SID_TYPE(&cgd->inq_data) != T_CDROM
531 && SID_TYPE(&cgd->inq_data) != T_WORM)
532 break;
535 * Allocate a peripheral instance for
536 * this device and start the probe
537 * process.
539 status = cam_periph_alloc(cdregister, cdoninvalidate,
540 cdcleanup, cdstart,
541 "cd", CAM_PERIPH_BIO,
542 cgd->ccb_h.path, cdasync,
543 AC_FOUND_DEVICE, cgd);
545 if (status != CAM_REQ_CMP
546 && status != CAM_REQ_INPROG)
547 kprintf("cdasync: Unable to attach new device "
548 "due to status 0x%x\n", status);
550 break;
552 case AC_SENT_BDR:
553 case AC_BUS_RESET:
555 struct cd_softc *softc;
556 struct ccb_hdr *ccbh;
558 softc = (struct cd_softc *)periph->softc;
560 * Don't fail on the expected unit attention
561 * that will occur.
563 softc->flags |= CD_FLAG_RETRY_UA;
564 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
565 ccbh->ccb_state |= CD_CCB_RETRY_UA;
566 /* FALLTHROUGH */
568 default:
569 cam_periph_async(periph, code, path, arg);
570 break;
574 static void
575 cdsysctlinit(void *context, int pending)
577 struct cam_periph *periph;
578 struct cd_softc *softc;
579 char tmpstr[80], tmpstr2[80];
581 periph = (struct cam_periph *)context;
582 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
583 return;
585 softc = (struct cd_softc *)periph->softc;
586 ksnprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
587 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
589 sysctl_ctx_init(&softc->sysctl_ctx);
590 softc->flags |= CD_FLAG_SCTX_INIT;
591 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
592 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
593 tmpstr2, CTLFLAG_RD, 0, tmpstr);
595 if (softc->sysctl_tree == NULL) {
596 kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
597 cam_periph_release(periph);
598 return;
602 * Now register the sysctl handler, so the user can the value on
603 * the fly.
605 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
606 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
607 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
608 "Minimum CDB size");
610 cam_periph_release(periph);
614 * We have a handler function for this so we can check the values when the
615 * user sets them, instead of every time we look at them.
617 static int
618 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
620 int error, value;
622 value = *(int *)arg1;
624 error = sysctl_handle_int(oidp, &value, 0, req);
626 if ((error != 0) || (req->newptr == NULL))
627 return (error);
630 * The only real values we can have here are 6 or 10. I don't
631 * really forsee having 12 be an option at any time in the future.
632 * So if the user sets something less than or equal to 6, we'll set
633 * it to 6. If he sets something greater than 6, we'll set it to 10.
635 * I suppose we could just return an error here for the wrong values,
636 * but I don't think it's necessary to do so, as long as we can
637 * determine the user's intent without too much trouble.
639 if (value < 6)
640 value = 6;
641 else if (value > 6)
642 value = 10;
644 *(int *)arg1 = value;
646 return (0);
650 static cam_status
651 cdregister(struct cam_periph *periph, void *arg)
653 struct cd_softc *softc;
654 struct ccb_pathinq cpi;
655 struct ccb_getdev *cgd;
656 char tmpstr[80];
657 caddr_t match;
659 cgd = (struct ccb_getdev *)arg;
660 if (periph == NULL) {
661 kprintf("cdregister: periph was NULL!!\n");
662 return(CAM_REQ_CMP_ERR);
664 if (cgd == NULL) {
665 kprintf("cdregister: no getdev CCB, can't register device\n");
666 return(CAM_REQ_CMP_ERR);
669 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
670 LIST_INIT(&softc->pending_ccbs);
671 STAILQ_INIT(&softc->mode_queue);
672 softc->state = CD_STATE_PROBE;
673 bioq_init(&softc->bio_queue);
674 if (SID_IS_REMOVABLE(&cgd->inq_data))
675 softc->flags |= CD_FLAG_DISC_REMOVABLE;
676 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
677 softc->flags |= CD_FLAG_TAGGED_QUEUING;
679 periph->softc = softc;
680 softc->periph = periph;
682 cam_extend_set(cdperiphs, periph->unit_number, periph);
685 * See if this device has any quirks.
687 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
688 (caddr_t)cd_quirk_table,
689 sizeof(cd_quirk_table)/sizeof(*cd_quirk_table),
690 sizeof(*cd_quirk_table), scsi_inquiry_match);
692 if (match != NULL)
693 softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
694 else
695 softc->quirks = CD_Q_NONE;
697 /* Check if the SIM does not want 6 byte commands */
698 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
699 cpi.ccb_h.func_code = XPT_PATH_INQ;
700 xpt_action((union ccb *)&cpi);
701 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
702 softc->quirks |= CD_Q_10_BYTE_ONLY;
704 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
706 /* The default is 6 byte commands, unless quirked otherwise */
707 if (softc->quirks & CD_Q_10_BYTE_ONLY)
708 softc->minimum_command_size = 10;
709 else
710 softc->minimum_command_size = 6;
713 * Load the user's default, if any.
715 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
716 periph->unit_number);
717 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
719 /* 6 and 10 are the only permissible values here. */
720 if (softc->minimum_command_size < 6)
721 softc->minimum_command_size = 6;
722 else if (softc->minimum_command_size > 6)
723 softc->minimum_command_size = 10;
726 * We need to register the statistics structure for this device,
727 * but we don't have the blocksize yet for it. So, we register
728 * the structure and indicate that we don't have the blocksize
729 * yet. Unlike other SCSI peripheral drivers, we explicitly set
730 * the device type here to be CDROM, rather than just ORing in
731 * the device type. This is because this driver can attach to either
732 * CDROM or WORM devices, and we want this peripheral driver to
733 * show up in the devstat list as a CD peripheral driver, not a
734 * WORM peripheral driver. WORM drives will also have the WORM
735 * driver attached to them.
737 cam_periph_unlock(periph);
738 devstat_add_entry(&softc->device_stats, "cd",
739 periph->unit_number, 0,
740 DEVSTAT_BS_UNAVAILABLE,
741 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
742 DEVSTAT_PRIORITY_CD);
743 disk_create(periph->unit_number, &softc->disk, &cd_ops);
744 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
745 softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
746 DSO_COMPATPARTA;
747 cam_periph_lock(periph);
750 * Add an async callback so that we get
751 * notified if this device goes away.
753 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
754 cdasync, periph, periph->path);
757 * If the target lun is greater than 0, we most likely have a CD
758 * changer device. Check the quirk entries as well, though, just
759 * in case someone has a CD tower with one lun per drive or
760 * something like that. Also, if we know up front that a
761 * particular device is a changer, we can mark it as such starting
762 * with lun 0, instead of lun 1. It shouldn't be necessary to have
763 * a quirk entry to define something as a changer, however.
765 if (((cgd->ccb_h.target_lun > 0)
766 && ((softc->quirks & CD_Q_NO_CHANGER) == 0))
767 || ((softc->quirks & CD_Q_CHANGER) != 0)) {
768 struct cdchanger *nchanger;
769 struct cam_periph *nperiph;
770 struct cam_path *path;
771 cam_status status;
772 int found;
774 /* Set the changer flag in the current device's softc */
775 softc->flags |= CD_FLAG_CHANGER;
778 * Now, look around for an existing changer device with the
779 * same path and target ID as the current device.
781 lockmgr(&changerq_lock, LK_EXCLUSIVE);
782 for (found = 0,
783 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
784 nchanger != NULL;
785 nchanger = STAILQ_NEXT(nchanger, changer_links)){
786 if ((nchanger->path_id == cgd->ccb_h.path_id)
787 && (nchanger->target_id == cgd->ccb_h.target_id)) {
788 found = 1;
789 break;
792 lockmgr(&changerq_lock, LK_RELEASE);
795 * If we found a matching entry, just add this device to
796 * the list of devices on this changer.
798 if (found == 1) {
799 struct chdevlist *chlunhead;
801 chlunhead = &nchanger->chluns;
804 * XXX KDM look at consolidating this code with the
805 * code below in a separate function.
809 * Create a path with lun id 0, and see if we can
810 * find a matching device
812 status = xpt_create_path(&path, /*periph*/ periph,
813 cgd->ccb_h.path_id,
814 cgd->ccb_h.target_id, 0);
816 if ((status == CAM_REQ_CMP)
817 && ((nperiph = cam_periph_find(path, "cd")) != NULL)){
818 struct cd_softc *nsoftc;
820 nsoftc = (struct cd_softc *)nperiph->softc;
822 if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
823 nsoftc->flags |= CD_FLAG_CHANGER;
824 nchanger->num_devices++;
825 if (camq_resize(&nchanger->devq,
826 nchanger->num_devices)!=CAM_REQ_CMP){
827 kprintf("cdregister: "
828 "camq_resize "
829 "failed, changer "
830 "support may "
831 "be messed up\n");
833 nsoftc->changer = nchanger;
834 nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
836 STAILQ_INSERT_TAIL(&nchanger->chluns,
837 nsoftc,changer_links);
839 xpt_free_path(path);
840 } else if (status == CAM_REQ_CMP)
841 xpt_free_path(path);
842 else {
843 kprintf("cdregister: unable to allocate path\n"
844 "cdregister: changer support may be "
845 "broken\n");
848 nchanger->num_devices++;
850 softc->changer = nchanger;
851 softc->pinfo.index = CAM_UNQUEUED_INDEX;
853 if (camq_resize(&nchanger->devq,
854 nchanger->num_devices) != CAM_REQ_CMP) {
855 kprintf("cdregister: camq_resize "
856 "failed, changer support may "
857 "be messed up\n");
860 STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
863 * In this case, we don't already have an entry for this
864 * particular changer, so we need to create one, add it to
865 * the queue, and queue this device on the list for this
866 * changer. Before we queue this device, however, we need
867 * to search for lun id 0 on this target, and add it to the
868 * queue first, if it exists. (and if it hasn't already
869 * been marked as part of the changer.)
871 else {
872 nchanger = kmalloc(sizeof(struct cdchanger),
873 M_DEVBUF, M_INTWAIT | M_ZERO);
874 callout_init(&nchanger->short_handle);
875 callout_init(&nchanger->long_handle);
876 if (camq_init(&nchanger->devq, 1) != 0) {
877 softc->flags &= ~CD_FLAG_CHANGER;
878 kprintf("cdregister: changer support "
879 "disabled\n");
880 goto cdregisterexit;
883 nchanger->path_id = cgd->ccb_h.path_id;
884 nchanger->target_id = cgd->ccb_h.target_id;
886 /* this is superfluous, but it makes things clearer */
887 nchanger->num_devices = 0;
889 STAILQ_INIT(&nchanger->chluns);
891 callout_init(&nchanger->long_handle);
892 callout_init(&nchanger->short_handle);
894 lockmgr(&changerq_lock, LK_EXCLUSIVE);
895 num_changers++;
896 STAILQ_INSERT_TAIL(&changerq, nchanger,
897 changer_links);
898 lockmgr(&changerq_lock, LK_RELEASE);
901 * Create a path with lun id 0, and see if we can
902 * find a matching device
904 status = xpt_create_path(&path, /*periph*/ periph,
905 cgd->ccb_h.path_id,
906 cgd->ccb_h.target_id, 0);
909 * If we were able to allocate the path, and if we
910 * find a matching device and it isn't already
911 * marked as part of a changer, then we add it to
912 * the current changer.
914 if ((status == CAM_REQ_CMP)
915 && ((nperiph = cam_periph_find(path, "cd")) != NULL)
916 && ((((struct cd_softc *)periph->softc)->flags &
917 CD_FLAG_CHANGER) == 0)) {
918 struct cd_softc *nsoftc;
920 nsoftc = (struct cd_softc *)nperiph->softc;
922 nsoftc->flags |= CD_FLAG_CHANGER;
923 nchanger->num_devices++;
924 if (camq_resize(&nchanger->devq,
925 nchanger->num_devices) != CAM_REQ_CMP) {
926 kprintf("cdregister: camq_resize "
927 "failed, changer support may "
928 "be messed up\n");
930 nsoftc->changer = nchanger;
931 nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
933 STAILQ_INSERT_TAIL(&nchanger->chluns,
934 nsoftc, changer_links);
935 xpt_free_path(path);
936 } else if (status == CAM_REQ_CMP)
937 xpt_free_path(path);
938 else {
939 kprintf("cdregister: unable to allocate path\n"
940 "cdregister: changer support may be "
941 "broken\n");
944 softc->changer = nchanger;
945 softc->pinfo.index = CAM_UNQUEUED_INDEX;
946 nchanger->num_devices++;
947 if (camq_resize(&nchanger->devq,
948 nchanger->num_devices) != CAM_REQ_CMP) {
949 kprintf("cdregister: camq_resize "
950 "failed, changer support may "
951 "be messed up\n");
953 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
954 changer_links);
958 cdregisterexit:
961 * Refcount and block open attempts until we are setup
962 * Can't block
964 cam_periph_hold(periph, 0);
966 if ((softc->flags & CD_FLAG_CHANGER) == 0)
967 xpt_schedule(periph, /*priority*/5);
968 else
969 cdschedule(periph, /*priority*/ 5);
971 return(CAM_REQ_CMP);
974 static int
975 cdopen(struct dev_open_args *ap)
977 cdev_t dev = ap->a_head.a_dev;
978 struct cam_periph *periph;
979 struct cd_softc *softc;
980 int unit, error;
982 unit = dkunit(dev);
983 periph = cam_extend_get(cdperiphs, unit);
985 if (periph == NULL)
986 return (ENXIO);
988 softc = (struct cd_softc *)periph->softc;
990 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
991 return(ENXIO);
993 cam_periph_lock(periph);
995 if (softc->flags & CD_FLAG_INVALID) {
996 cam_periph_unlock(periph);
997 cam_periph_release(periph);
998 return(ENXIO);
1001 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1002 cam_periph_unlock(periph);
1003 cam_periph_release(periph);
1004 return (error);
1007 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1008 if (softc->flags & CD_FLAG_OPEN)
1009 cam_periph_release(periph);
1010 else
1011 softc->flags |= CD_FLAG_OPEN;
1014 * Check for media, and set the appropriate flags. We don't bail
1015 * if we don't have media, but then we don't allow anything but the
1016 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1018 * XXX KDM for now, we do fail the open if we don't have media. We
1019 * can change this once we've figured out how to make the slice
1020 * code work well with media changing underneath it.
1022 error = cdcheckmedia(periph);
1024 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1025 cam_periph_unhold(periph, 1);
1026 /* stays acquired */
1028 return (0);
1031 static int
1032 cdclose(struct dev_close_args *ap)
1034 cdev_t dev = ap->a_head.a_dev;
1035 struct cam_periph *periph;
1036 struct cd_softc *softc;
1037 struct disk_info *info;
1038 int unit;
1040 unit = dkunit(dev);
1041 periph = cam_extend_get(cdperiphs, unit);
1042 if (periph == NULL)
1043 return (ENXIO);
1045 softc = (struct cd_softc *)periph->softc;
1047 cam_periph_lock(periph);
1048 cam_periph_hold(periph, 0);
1050 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1051 cdprevent(periph, PR_ALLOW);
1054 * Unconditionally set the dsopen() flags back to their default
1055 * state.
1057 info = &softc->disk.d_info;
1058 info->d_dsflags &= ~DSO_NOLABELS;
1059 info->d_dsflags |= DSO_COMPATLABEL;
1062 * Since we're closing this CD, mark the blocksize as unavailable.
1063 * It will be marked as available when the CD is opened again.
1065 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1068 * We'll check the media and toc again at the next open().
1070 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1072 cam_periph_unhold(periph, 1);
1073 cam_periph_release(periph);
1075 return (0);
1078 static void
1079 cdshorttimeout(void *arg)
1081 struct cdchanger *changer;
1083 changer = (struct cdchanger *)arg;
1085 /* Always clear the short timeout flag, since that's what we're in */
1086 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1089 * Check to see if there is any more pending or outstanding I/O for
1090 * this device. If not, move it out of the active slot.
1092 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1093 && (changer->cur_device->outstanding_cmds == 0)) {
1094 changer->flags |= CHANGER_MANUAL_CALL;
1095 cdrunchangerqueue(changer);
1100 * This is a wrapper for xpt_schedule. It only applies to changers.
1102 static void
1103 cdschedule(struct cam_periph *periph, int priority)
1105 struct cd_softc *softc;
1107 softc = (struct cd_softc *)periph->softc;
1110 * If this device isn't currently queued, and if it isn't
1111 * the active device, then we queue this device and run the
1112 * changer queue if there is no timeout scheduled to do it.
1113 * If this device is the active device, just schedule it
1114 * to run again. If this device is queued, there should be
1115 * a timeout in place already that will make sure it runs.
1117 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1118 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1120 * We don't do anything with the priority here.
1121 * This is strictly a fifo queue.
1123 softc->pinfo.priority = 1;
1124 softc->pinfo.generation = ++softc->changer->devq.generation;
1125 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1128 * Since we just put a device in the changer queue,
1129 * check and see if there is a timeout scheduled for
1130 * this changer. If so, let the timeout handle
1131 * switching this device into the active slot. If
1132 * not, manually call the timeout routine to
1133 * bootstrap things.
1135 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1136 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1137 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1138 softc->changer->flags |= CHANGER_MANUAL_CALL;
1139 cdrunchangerqueue(softc->changer);
1141 } else if ((softc->flags & CD_FLAG_ACTIVE)
1142 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1143 xpt_schedule(periph, priority);
1147 static void
1148 cdrunchangerqueue(void *arg)
1150 struct cd_softc *softc;
1151 struct cdchanger *changer;
1152 int called_from_timeout;
1154 changer = (struct cdchanger *)arg;
1157 * If we have NOT been called from cdstrategy() or cddone(), and
1158 * instead from a timeout routine, go ahead and clear the
1159 * timeout flag.
1161 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1162 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1163 called_from_timeout = 1;
1164 } else
1165 called_from_timeout = 0;
1167 /* Always clear the manual call flag */
1168 changer->flags &= ~CHANGER_MANUAL_CALL;
1170 /* nothing to do if the queue is empty */
1171 if (changer->devq.entries <= 0) {
1172 return;
1176 * If the changer queue is frozen, that means we have an active
1177 * device.
1179 if (changer->devq.qfrozen_cnt > 0) {
1182 * We always need to reset the frozen count and clear the
1183 * active flag.
1185 changer->devq.qfrozen_cnt--;
1186 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1187 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1189 if (changer->cur_device->outstanding_cmds > 0) {
1190 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1191 changer->cur_device->bufs_left =
1192 changer->cur_device->outstanding_cmds;
1193 if (called_from_timeout) {
1194 callout_reset(&changer->long_handle,
1195 changer_max_busy_seconds * hz,
1196 cdrunchangerqueue, changer);
1197 changer->flags |= CHANGER_TIMEOUT_SCHED;
1199 return;
1203 * Check to see whether the current device has any I/O left
1204 * to do. If so, requeue it at the end of the queue. If
1205 * not, there is no need to requeue it.
1207 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1209 changer->cur_device->pinfo.generation =
1210 ++changer->devq.generation;
1211 camq_insert(&changer->devq,
1212 (cam_pinfo *)changer->cur_device);
1216 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1218 changer->cur_device = softc;
1220 changer->devq.qfrozen_cnt++;
1221 softc->flags |= CD_FLAG_ACTIVE;
1223 /* Just in case this device is waiting */
1224 wakeup(&softc->changer);
1225 xpt_schedule(softc->periph, /*priority*/ 1);
1228 * Get rid of any pending timeouts, and set a flag to schedule new
1229 * ones so this device gets its full time quantum.
1231 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1232 callout_stop(&changer->long_handle);
1233 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1236 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1237 callout_stop(&changer->short_handle);
1238 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1242 * We need to schedule timeouts, but we only do this after the
1243 * first transaction has completed. This eliminates the changer
1244 * switch time.
1246 changer->flags |= CHANGER_NEED_TIMEOUT;
1249 static void
1250 cdchangerschedule(struct cd_softc *softc)
1252 struct cdchanger *changer;
1254 changer = softc->changer;
1257 * If this is a changer, and this is the current device,
1258 * and this device has at least the minimum time quantum to
1259 * run, see if we can switch it out.
1261 if ((softc->flags & CD_FLAG_ACTIVE)
1262 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1263 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1265 * We try three things here. The first is that we
1266 * check to see whether the schedule on completion
1267 * flag is set. If it is, we decrement the number
1268 * of buffers left, and if it's zero, we reschedule.
1269 * Next, we check to see whether the pending buffer
1270 * queue is empty and whether there are no
1271 * outstanding transactions. If so, we reschedule.
1272 * Next, we see if the pending buffer queue is empty.
1273 * If it is, we set the number of buffers left to
1274 * the current active buffer count and set the
1275 * schedule on complete flag.
1277 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1278 if (--softc->bufs_left == 0) {
1279 softc->changer->flags |=
1280 CHANGER_MANUAL_CALL;
1281 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1282 cdrunchangerqueue(softc->changer);
1284 } else if ((bioq_first(&softc->bio_queue) == NULL)
1285 && (softc->outstanding_cmds == 0)) {
1286 softc->changer->flags |= CHANGER_MANUAL_CALL;
1287 cdrunchangerqueue(softc->changer);
1289 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1290 && (softc->flags & CD_FLAG_ACTIVE)) {
1293 * Now that the first transaction to this
1294 * particular device has completed, we can go ahead
1295 * and schedule our timeouts.
1297 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1298 callout_reset(&changer->long_handle,
1299 changer_max_busy_seconds * hz,
1300 cdrunchangerqueue, changer);
1301 changer->flags |= CHANGER_TIMEOUT_SCHED;
1302 } else
1303 kprintf("cdchangerschedule: already have a long"
1304 " timeout!\n");
1306 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1307 callout_reset(&changer->short_handle,
1308 changer_min_busy_seconds * hz,
1309 cdshorttimeout, changer);
1310 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1311 } else
1312 kprintf("cdchangerschedule: already have a short "
1313 "timeout!\n");
1316 * We just scheduled timeouts, no need to schedule
1317 * more.
1319 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1324 static int
1325 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1326 u_int32_t cam_flags,
1327 u_int32_t sense_flags),
1328 u_int32_t cam_flags, u_int32_t sense_flags)
1330 struct cd_softc *softc;
1331 struct cam_periph *periph;
1332 int error;
1334 periph = xpt_path_periph(ccb->ccb_h.path);
1335 softc = (struct cd_softc *)periph->softc;
1337 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1338 &softc->device_stats);
1340 if (softc->flags & CD_FLAG_CHANGER)
1341 cdchangerschedule(softc);
1343 return(error);
1346 static union ccb *
1347 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1349 struct cd_softc *softc;
1351 softc = (struct cd_softc *)periph->softc;
1353 if (softc->flags & CD_FLAG_CHANGER) {
1355 * This should work the first time this device is woken up,
1356 * but just in case it doesn't, we use a while loop.
1358 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1360 * If this changer isn't already queued, queue it up.
1362 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1363 softc->pinfo.priority = 1;
1364 softc->pinfo.generation =
1365 ++softc->changer->devq.generation;
1366 camq_insert(&softc->changer->devq,
1367 (cam_pinfo *)softc);
1369 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1370 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1371 && ((softc->changer->flags
1372 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1373 softc->changer->flags |= CHANGER_MANUAL_CALL;
1374 cdrunchangerqueue(softc->changer);
1375 } else
1376 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1377 periph->sim->lock);
1380 return(cam_periph_getccb(periph, priority));
1384 * Actually translate the requested transfer into one the physical driver
1385 * can understand. The transfer is described by a buf and will include
1386 * only one physical transfer.
1388 static int
1389 cdstrategy(struct dev_strategy_args *ap)
1391 cdev_t dev = ap->a_head.a_dev;
1392 struct bio *bio = ap->a_bio;
1393 struct buf *bp = bio->bio_buf;
1394 struct cam_periph *periph;
1395 struct cd_softc *softc;
1396 u_int unit;
1398 unit = dkunit(dev);
1399 periph = cam_extend_get(cdperiphs, unit);
1400 if (periph == NULL) {
1401 bp->b_error = ENXIO;
1402 goto bad;
1405 cam_periph_lock(periph);
1406 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1408 softc = (struct cd_softc *)periph->softc;
1411 * If the device has been made invalid, error out
1413 if ((softc->flags & CD_FLAG_INVALID)) {
1414 cam_periph_unlock(periph);
1415 bp->b_error = ENXIO;
1416 goto bad;
1420 * If we don't have valid media, look for it before trying to
1421 * schedule the I/O.
1423 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1424 int error;
1426 error = cdcheckmedia(periph);
1427 if (error != 0) {
1428 cam_periph_unlock(periph);
1429 bp->b_error = error;
1430 goto bad;
1435 * Place it in the queue of disk activities for this disk
1437 bioqdisksort(&softc->bio_queue, bio);
1440 * Schedule ourselves for performing the work. We do things
1441 * differently for changers.
1443 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1444 xpt_schedule(periph, /* XXX priority */1);
1445 else
1446 cdschedule(periph, /* priority */ 1);
1448 cam_periph_unlock(periph);
1449 return(0);
1450 bad:
1451 bp->b_flags |= B_ERROR;
1453 * Correctly set the buf to indicate a completed xfer
1455 bp->b_resid = bp->b_bcount;
1456 biodone(bio);
1457 return(0);
1460 static void
1461 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1463 struct cd_softc *softc;
1464 struct bio *bio;
1465 struct buf *bp;
1466 struct ccb_scsiio *csio;
1467 struct scsi_read_capacity_data *rcap;
1469 softc = (struct cd_softc *)periph->softc;
1471 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1473 switch (softc->state) {
1474 case CD_STATE_NORMAL:
1476 bio = bioq_first(&softc->bio_queue);
1477 if (periph->immediate_priority <= periph->pinfo.priority) {
1478 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1480 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1481 periph_links.sle);
1482 periph->immediate_priority = CAM_PRIORITY_NONE;
1483 wakeup(&periph->ccb_list);
1484 } else if (bio == NULL) {
1485 xpt_release_ccb(start_ccb);
1486 } else {
1487 bp = bio->bio_buf;
1488 bioq_remove(&softc->bio_queue, bio);
1490 devstat_start_transaction(&softc->device_stats);
1492 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1494 scsi_read_write(&start_ccb->csio,
1495 /*retries*/4,
1496 /* cbfcnp */ cddone,
1497 (bp->b_flags & B_ORDERED) != 0 ?
1498 MSG_ORDERED_Q_TAG :
1499 MSG_SIMPLE_Q_TAG,
1500 /* read */(bp->b_cmd == BUF_CMD_READ),
1501 /* byte2 */ 0,
1502 /* minimum_cmd_size */ 10,
1503 /* lba */
1504 bio->bio_offset / softc->params.blksize,
1505 bp->b_bcount / softc->params.blksize,
1506 /* data_ptr */ bp->b_data,
1507 /* dxfer_len */ bp->b_bcount,
1508 /* sense_len */ SSD_FULL_SIZE,
1509 /* timeout */ 30000);
1510 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1513 LIST_INSERT_HEAD(&softc->pending_ccbs,
1514 &start_ccb->ccb_h, periph_links.le);
1516 softc->outstanding_cmds++;
1517 /* We expect a unit attention from this device */
1518 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1519 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1520 softc->flags &= ~CD_FLAG_RETRY_UA;
1523 start_ccb->ccb_h.ccb_bio = bio;
1524 bio = bioq_first(&softc->bio_queue);
1526 xpt_action(start_ccb);
1528 if (bio != NULL) {
1529 /* Have more work to do, so ensure we stay scheduled */
1530 xpt_schedule(periph, /* XXX priority */1);
1532 break;
1534 case CD_STATE_PROBE:
1537 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1538 csio = &start_ccb->csio;
1539 scsi_read_capacity(csio,
1540 /*retries*/1,
1541 cddone,
1542 MSG_SIMPLE_Q_TAG,
1543 rcap,
1544 SSD_FULL_SIZE,
1545 /*timeout*/20000);
1546 start_ccb->ccb_h.ccb_bio = NULL;
1547 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1548 xpt_action(start_ccb);
1549 break;
1554 static void
1555 cddone(struct cam_periph *periph, union ccb *done_ccb)
1557 struct cd_softc *softc;
1558 struct ccb_scsiio *csio;
1560 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1562 softc = (struct cd_softc *)periph->softc;
1563 csio = &done_ccb->csio;
1565 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1566 case CD_CCB_BUFFER_IO:
1568 struct buf *bp;
1569 struct bio *bio;
1570 int error;
1572 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1573 bp = bio->bio_buf;
1574 error = 0;
1576 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1577 int sf;
1579 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1580 sf = SF_RETRY_UA;
1581 else
1582 sf = 0;
1584 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1585 if (error == ERESTART) {
1587 * A retry was scheuled, so
1588 * just return.
1590 return;
1594 if (error != 0) {
1595 struct bio *q_bio;
1596 struct buf *q_bp;
1598 xpt_print(periph->path,
1599 "cddone: got error %#x back\n", error);
1600 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1601 bioq_remove(&softc->bio_queue, q_bio);
1602 q_bp = q_bio->bio_buf;
1603 q_bp->b_resid = q_bp->b_bcount;
1604 q_bp->b_error = EIO;
1605 q_bp->b_flags |= B_ERROR;
1606 biodone(q_bio);
1608 bp->b_resid = bp->b_bcount;
1609 bp->b_error = error;
1610 bp->b_flags |= B_ERROR;
1611 cam_release_devq(done_ccb->ccb_h.path,
1612 /*relsim_flags*/0,
1613 /*reduction*/0,
1614 /*timeout*/0,
1615 /*getcount_only*/0);
1617 } else {
1618 bp->b_resid = csio->resid;
1619 bp->b_error = 0;
1620 if (bp->b_resid != 0) {
1621 /* Short transfer ??? */
1622 bp->b_flags |= B_ERROR;
1626 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1627 softc->outstanding_cmds--;
1629 if (softc->flags & CD_FLAG_CHANGER)
1630 cdchangerschedule(softc);
1632 devstat_end_transaction_buf(&softc->device_stats, bp);
1633 biodone(bio);
1634 break;
1636 case CD_CCB_PROBE:
1638 struct scsi_read_capacity_data *rdcap;
1639 char announce_buf[120]; /*
1640 * Currently (9/30/97) the
1641 * longest possible announce
1642 * buffer is 108 bytes, for the
1643 * first error case below.
1644 * That is 39 bytes for the
1645 * basic string, 16 bytes for the
1646 * biggest sense key (hardware
1647 * error), 52 bytes for the
1648 * text of the largest sense
1649 * qualifier valid for a CDROM,
1650 * (0x72, 0x03 or 0x04,
1651 * 0x03), and one byte for the
1652 * null terminating character.
1653 * To allow for longer strings,
1654 * the announce buffer is 120
1655 * bytes.
1657 struct cd_params *cdp;
1659 cdp = &softc->params;
1661 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1663 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1664 cdp->blksize = scsi_4btoul (rdcap->length);
1666 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1668 ksnprintf(announce_buf, sizeof(announce_buf),
1669 "cd present [%lu x %lu byte records]",
1670 cdp->disksize, (u_long)cdp->blksize);
1672 } else {
1673 int error;
1675 * Retry any UNIT ATTENTION type errors. They
1676 * are expected at boot.
1678 error = cderror(done_ccb, CAM_RETRY_SELTO,
1679 SF_RETRY_UA | SF_NO_PRINT);
1680 if (error == ERESTART) {
1682 * A retry was scheuled, so
1683 * just return.
1685 return;
1686 } else if (error != 0) {
1688 struct scsi_sense_data *sense;
1689 int asc, ascq;
1690 int sense_key, error_code;
1691 int have_sense;
1692 cam_status status;
1693 struct ccb_getdev cgd;
1695 /* Don't wedge this device's queue */
1696 cam_release_devq(done_ccb->ccb_h.path,
1697 /*relsim_flags*/0,
1698 /*reduction*/0,
1699 /*timeout*/0,
1700 /*getcount_only*/0);
1702 status = done_ccb->ccb_h.status;
1704 xpt_setup_ccb(&cgd.ccb_h,
1705 done_ccb->ccb_h.path,
1706 /* priority */ 1);
1707 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1708 xpt_action((union ccb *)&cgd);
1710 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1711 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1712 || ((status & CAM_AUTOSNS_VALID) == 0))
1713 have_sense = FALSE;
1714 else
1715 have_sense = TRUE;
1717 if (have_sense) {
1718 sense = &csio->sense_data;
1719 scsi_extract_sense(sense, &error_code,
1720 &sense_key,
1721 &asc, &ascq);
1724 * Attach to anything that claims to be a
1725 * CDROM or WORM device, as long as it
1726 * doesn't return a "Logical unit not
1727 * supported" (0x25) error.
1729 if ((have_sense) && (asc != 0x25)
1730 && (error_code == SSD_CURRENT_ERROR)) {
1731 const char *sense_key_desc;
1732 const char *asc_desc;
1734 scsi_sense_desc(sense_key, asc, ascq,
1735 &cgd.inq_data,
1736 &sense_key_desc,
1737 &asc_desc);
1738 ksnprintf(announce_buf,
1739 sizeof(announce_buf),
1740 "Attempt to query device "
1741 "size failed: %s, %s",
1742 sense_key_desc,
1743 asc_desc);
1744 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1746 * We only print out an error for
1747 * CDROM type devices. For WORM
1748 * devices, we don't print out an
1749 * error since a few WORM devices
1750 * don't support CDROM commands.
1751 * If we have sense information, go
1752 * ahead and print it out.
1753 * Otherwise, just say that we
1754 * couldn't attach.
1758 * Just print out the error, not
1759 * the full probe message, when we
1760 * don't attach.
1762 if (have_sense)
1763 scsi_sense_print(
1764 &done_ccb->csio);
1765 else {
1766 xpt_print(periph->path,
1767 "got CAM status %#x\n",
1768 done_ccb->ccb_h.status);
1770 xpt_print(periph->path, "fatal error, "
1771 "failed to attach to device\n");
1773 * Invalidate this peripheral.
1775 cam_periph_invalidate(periph);
1777 announce_buf[0] = '\0';
1778 } else {
1781 * Invalidate this peripheral.
1783 cam_periph_invalidate(periph);
1784 announce_buf[0] = '\0';
1788 kfree(rdcap, M_SCSICD);
1789 if (announce_buf[0] != '\0') {
1790 xpt_announce_periph(periph, announce_buf);
1791 if (softc->flags & CD_FLAG_CHANGER)
1792 cdchangerschedule(softc);
1794 * Create our sysctl variables, now that we know
1795 * we have successfully attached.
1797 taskqueue_enqueue(taskqueue_thread[mycpuid],
1798 &softc->sysctl_task);
1800 softc->state = CD_STATE_NORMAL;
1802 * Since our peripheral may be invalidated by an error
1803 * above or an external event, we must release our CCB
1804 * before releasing the probe lock on the peripheral.
1805 * The peripheral will only go away once the last lock
1806 * is removed, and we need it around for the CCB release
1807 * operation.
1809 xpt_release_ccb(done_ccb);
1810 cam_periph_unhold(periph, 0);
1811 return;
1813 case CD_CCB_WAITING:
1815 /* Caller will release the CCB */
1816 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1817 ("trying to wakeup ccbwait\n"));
1819 wakeup(&done_ccb->ccb_h.cbfcnp);
1820 return;
1822 default:
1823 break;
1825 xpt_release_ccb(done_ccb);
1828 static union cd_pages *
1829 cdgetpage(struct cd_mode_params *mode_params)
1831 union cd_pages *page;
1833 if (mode_params->cdb_size == 10)
1834 page = (union cd_pages *)find_mode_page_10(
1835 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1836 else
1837 page = (union cd_pages *)find_mode_page_6(
1838 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1840 return (page);
1843 static int
1844 cdgetpagesize(int page_num)
1846 int i;
1848 for (i = 0; i < (sizeof(cd_page_size_table)/
1849 sizeof(cd_page_size_table[0])); i++) {
1850 if (cd_page_size_table[i].page == page_num)
1851 return (cd_page_size_table[i].page_size);
1853 return (-1);
1856 static int
1857 cdioctl(struct dev_ioctl_args *ap)
1859 cdev_t dev = ap->a_head.a_dev;
1860 caddr_t addr = ap->a_data;
1861 struct cam_periph *periph;
1862 struct cd_softc *softc;
1863 int unit, error = 0;
1865 unit = dkunit(dev);
1867 periph = cam_extend_get(cdperiphs, unit);
1868 if (periph == NULL)
1869 return(ENXIO);
1871 cam_periph_lock(periph);
1872 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1874 softc = (struct cd_softc *)periph->softc;
1876 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1877 ("trying to do ioctl %#lx\n", ap->a_cmd));
1879 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1880 cam_periph_unlock(periph);
1881 cam_periph_release(periph);
1882 return (error);
1886 * If we don't have media loaded, check for it. If still don't
1887 * have media loaded, we can only do a load or eject.
1889 * We only care whether media is loaded if this is a cd-specific ioctl
1890 * (thus the IOCGROUP check below). Note that this will break if
1891 * anyone adds any ioctls into the switch statement below that don't
1892 * have their ioctl group set to 'c'.
1894 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1895 && ((ap->a_cmd != CDIOCCLOSE)
1896 && (ap->a_cmd != CDIOCEJECT))
1897 && (IOCGROUP(ap->a_cmd) == 'c')) {
1898 error = cdcheckmedia(periph);
1899 if (error != 0) {
1900 cam_periph_unhold(periph, 1);
1901 return (error);
1905 * Drop the lock here so later mallocs can use WAITOK. The periph
1906 * is essentially locked still with the cam_periph_hold call above.
1908 cam_periph_unlock(periph);
1910 switch (ap->a_cmd) {
1912 case CDIOCPLAYTRACKS:
1914 struct ioc_play_track *args
1915 = (struct ioc_play_track *)addr;
1916 struct cd_mode_params params;
1917 union cd_pages *page;
1919 params.alloc_len = sizeof(union cd_mode_data_6_10);
1920 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1921 M_WAITOK | M_ZERO);
1923 cam_periph_lock(periph);
1924 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1925 ("trying to do CDIOCPLAYTRACKS\n"));
1927 error = cdgetmode(periph, &params, AUDIO_PAGE);
1928 if (error) {
1929 kfree(params.mode_buf, M_SCSICD);
1930 cam_periph_unlock(periph);
1931 break;
1933 page = cdgetpage(&params);
1935 page->audio.flags &= ~CD_PA_SOTC;
1936 page->audio.flags |= CD_PA_IMMED;
1937 error = cdsetmode(periph, &params);
1938 kfree(params.mode_buf, M_SCSICD);
1939 if (error) {
1940 cam_periph_unlock(periph);
1941 break;
1945 * This was originally implemented with the PLAY
1946 * AUDIO TRACK INDEX command, but that command was
1947 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1948 * drives support it but ATAPI and ATAPI-derivative
1949 * drives don't seem to support it. So we keep a
1950 * cache of the table of contents and translate
1951 * track numbers to MSF format.
1953 if (softc->flags & CD_FLAG_VALID_TOC) {
1954 union msf_lba *sentry, *eentry;
1955 int st, et;
1957 if (args->end_track <
1958 softc->toc.header.ending_track + 1)
1959 args->end_track++;
1960 if (args->end_track >
1961 softc->toc.header.ending_track + 1)
1962 args->end_track =
1963 softc->toc.header.ending_track + 1;
1964 st = args->start_track -
1965 softc->toc.header.starting_track;
1966 et = args->end_track -
1967 softc->toc.header.starting_track;
1968 if ((st < 0)
1969 || (et < 0)
1970 || (st > (softc->toc.header.ending_track -
1971 softc->toc.header.starting_track))) {
1972 error = EINVAL;
1973 break;
1975 sentry = &softc->toc.entries[st].addr;
1976 eentry = &softc->toc.entries[et].addr;
1977 error = cdplaymsf(periph,
1978 sentry->msf.minute,
1979 sentry->msf.second,
1980 sentry->msf.frame,
1981 eentry->msf.minute,
1982 eentry->msf.second,
1983 eentry->msf.frame);
1984 } else {
1986 * If we don't have a valid TOC, try the
1987 * play track index command. It is part of
1988 * the SCSI-2 spec, but was removed in the
1989 * MMC specs. ATAPI and ATAPI-derived
1990 * drives don't support it.
1992 if (softc->quirks & CD_Q_BCD_TRACKS) {
1993 args->start_track =
1994 bin2bcd(args->start_track);
1995 args->end_track =
1996 bin2bcd(args->end_track);
1998 error = cdplaytracks(periph,
1999 args->start_track,
2000 args->start_index,
2001 args->end_track,
2002 args->end_index);
2004 cam_periph_unlock(periph);
2006 break;
2007 case CDIOCPLAYMSF:
2009 struct ioc_play_msf *args
2010 = (struct ioc_play_msf *) addr;
2011 struct cd_mode_params params;
2012 union cd_pages *page;
2014 params.alloc_len = sizeof(union cd_mode_data_6_10);
2015 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2016 M_WAITOK | M_ZERO);
2018 cam_periph_lock(periph);
2019 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2020 ("trying to do CDIOCPLAYMSF\n"));
2022 error = cdgetmode(periph, &params, AUDIO_PAGE);
2023 if (error) {
2024 kfree(params.mode_buf, M_SCSICD);
2025 cam_periph_unlock(periph);
2026 break;
2028 page = cdgetpage(&params);
2030 page->audio.flags &= ~CD_PA_SOTC;
2031 page->audio.flags |= CD_PA_IMMED;
2032 error = cdsetmode(periph, &params);
2033 kfree(params.mode_buf, M_SCSICD);
2034 if (error) {
2035 cam_periph_unlock(periph);
2036 break;
2038 error = cdplaymsf(periph,
2039 args->start_m,
2040 args->start_s,
2041 args->start_f,
2042 args->end_m,
2043 args->end_s,
2044 args->end_f);
2045 cam_periph_unlock(periph);
2047 break;
2048 case CDIOCPLAYBLOCKS:
2050 struct ioc_play_blocks *args
2051 = (struct ioc_play_blocks *) addr;
2052 struct cd_mode_params params;
2053 union cd_pages *page;
2055 params.alloc_len = sizeof(union cd_mode_data_6_10);
2056 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2057 M_WAITOK | M_ZERO);
2058 cam_periph_lock(periph);
2059 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2060 ("trying to do CDIOCPLAYBLOCKS\n"));
2063 error = cdgetmode(periph, &params, AUDIO_PAGE);
2064 if (error) {
2065 kfree(params.mode_buf, M_SCSICD);
2066 cam_periph_unlock(periph);
2067 break;
2069 page = cdgetpage(&params);
2071 page->audio.flags &= ~CD_PA_SOTC;
2072 page->audio.flags |= CD_PA_IMMED;
2073 error = cdsetmode(periph, &params);
2074 kfree(params.mode_buf, M_SCSICD);
2075 if (error) {
2076 cam_periph_unlock(periph);
2077 break;
2079 error = cdplay(periph, args->blk, args->len);
2080 cam_periph_unlock(periph);
2082 break;
2083 case CDIOCREADSUBCHANNEL:
2085 struct ioc_read_subchannel *args
2086 = (struct ioc_read_subchannel *) addr;
2087 struct cd_sub_channel_info *data;
2088 u_int32_t len = args->data_len;
2090 data = kmalloc(sizeof(struct cd_sub_channel_info),
2091 M_SCSICD, M_WAITOK);
2093 cam_periph_lock(periph);
2094 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2095 ("trying to do CDIOCREADSUBCHANNEL\n"));
2097 if ((len > sizeof(struct cd_sub_channel_info)) ||
2098 (len < sizeof(struct cd_sub_channel_header))) {
2099 kprintf(
2100 "scsi_cd: cdioctl: "
2101 "cdioreadsubchannel: error, len=%d\n",
2102 len);
2103 error = EINVAL;
2104 kfree(data, M_SCSICD);
2105 cam_periph_unlock(periph);
2106 break;
2109 if (softc->quirks & CD_Q_BCD_TRACKS)
2110 args->track = bin2bcd(args->track);
2112 error = cdreadsubchannel(periph, args->address_format,
2113 args->data_format, args->track, data, len);
2115 if (error) {
2116 kfree(data, M_SCSICD);
2117 cam_periph_unlock(periph);
2118 break;
2120 if (softc->quirks & CD_Q_BCD_TRACKS)
2121 data->what.track_info.track_number =
2122 bcd2bin(data->what.track_info.track_number);
2123 len = min(len, ((data->header.data_len[0] << 8) +
2124 data->header.data_len[1] +
2125 sizeof(struct cd_sub_channel_header)));
2126 cam_periph_unlock(periph);
2127 if (copyout(data, args->data, len) != 0) {
2128 error = EFAULT;
2130 kfree(data, M_SCSICD);
2132 break;
2134 case CDIOREADTOCHEADER:
2136 struct ioc_toc_header *th;
2138 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2139 M_WAITOK);
2141 cam_periph_lock(periph);
2142 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2143 ("trying to do CDIOREADTOCHEADER\n"));
2145 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2146 sizeof (*th), /*sense_flags*/0);
2147 if (error) {
2148 kfree(th, M_SCSICD);
2149 cam_periph_unlock(periph);
2150 break;
2152 if (softc->quirks & CD_Q_BCD_TRACKS) {
2153 /* we are going to have to convert the BCD
2154 * encoding on the cd to what is expected
2156 th->starting_track =
2157 bcd2bin(th->starting_track);
2158 th->ending_track = bcd2bin(th->ending_track);
2160 th->len = ntohs(th->len);
2161 bcopy(th, addr, sizeof(*th));
2162 kfree(th, M_SCSICD);
2163 cam_periph_unlock(periph);
2165 break;
2166 case CDIOREADTOCENTRYS:
2168 struct cd_tocdata *data;
2169 struct cd_toc_single *lead;
2170 struct ioc_read_toc_entry *te =
2171 (struct ioc_read_toc_entry *) addr;
2172 struct ioc_toc_header *th;
2173 u_int32_t len, readlen, idx, num;
2174 u_int32_t starting_track = te->starting_track;
2176 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2177 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2179 cam_periph_lock(periph);
2180 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2181 ("trying to do CDIOREADTOCENTRYS\n"));
2183 if (te->data_len < sizeof(struct cd_toc_entry)
2184 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2185 || (te->address_format != CD_MSF_FORMAT
2186 && te->address_format != CD_LBA_FORMAT)) {
2187 error = EINVAL;
2188 kprintf("scsi_cd: error in readtocentries, "
2189 "returning EINVAL\n");
2190 kfree(data, M_SCSICD);
2191 kfree(lead, M_SCSICD);
2192 cam_periph_unlock(periph);
2193 break;
2196 th = &data->header;
2197 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2198 sizeof (*th), /*sense_flags*/0);
2199 if (error) {
2200 kfree(data, M_SCSICD);
2201 kfree(lead, M_SCSICD);
2202 cam_periph_unlock(periph);
2203 break;
2206 if (softc->quirks & CD_Q_BCD_TRACKS) {
2207 /* we are going to have to convert the BCD
2208 * encoding on the cd to what is expected
2210 th->starting_track =
2211 bcd2bin(th->starting_track);
2212 th->ending_track = bcd2bin(th->ending_track);
2215 if (starting_track == 0)
2216 starting_track = th->starting_track;
2217 else if (starting_track == LEADOUT)
2218 starting_track = th->ending_track + 1;
2219 else if (starting_track < th->starting_track ||
2220 starting_track > th->ending_track + 1) {
2221 kprintf("scsi_cd: error in readtocentries, "
2222 "returning EINVAL\n");
2223 kfree(data, M_SCSICD);
2224 kfree(lead, M_SCSICD);
2225 cam_periph_unlock(periph);
2226 error = EINVAL;
2227 break;
2230 /* calculate reading length without leadout entry */
2231 readlen = (th->ending_track - starting_track + 1) *
2232 sizeof(struct cd_toc_entry);
2234 /* and with leadout entry */
2235 len = readlen + sizeof(struct cd_toc_entry);
2236 if (te->data_len < len) {
2237 len = te->data_len;
2238 if (readlen > len)
2239 readlen = len;
2241 if (len > sizeof(data->entries)) {
2242 kprintf("scsi_cd: error in readtocentries, "
2243 "returning EINVAL\n");
2244 error = EINVAL;
2245 kfree(data, M_SCSICD);
2246 kfree(lead, M_SCSICD);
2247 cam_periph_unlock(periph);
2248 break;
2250 num = len / sizeof(struct cd_toc_entry);
2252 if (readlen > 0) {
2253 error = cdreadtoc(periph, te->address_format,
2254 starting_track,
2255 (u_int8_t *)data,
2256 readlen + sizeof (*th),
2257 /*sense_flags*/0);
2258 if (error) {
2259 kfree(data, M_SCSICD);
2260 kfree(lead, M_SCSICD);
2261 cam_periph_unlock(periph);
2262 break;
2266 /* make leadout entry if needed */
2267 idx = starting_track + num - 1;
2268 if (softc->quirks & CD_Q_BCD_TRACKS)
2269 th->ending_track = bcd2bin(th->ending_track);
2270 if (idx == th->ending_track + 1) {
2271 error = cdreadtoc(periph, te->address_format,
2272 LEADOUT, (u_int8_t *)lead,
2273 sizeof(*lead),
2274 /*sense_flags*/0);
2275 if (error) {
2276 kfree(data, M_SCSICD);
2277 kfree(lead, M_SCSICD);
2278 cam_periph_unlock(periph);
2279 break;
2281 data->entries[idx - starting_track] =
2282 lead->entry;
2284 if (softc->quirks & CD_Q_BCD_TRACKS) {
2285 for (idx = 0; idx < num - 1; idx++) {
2286 data->entries[idx].track =
2287 bcd2bin(data->entries[idx].track);
2291 cam_periph_unlock(periph);
2292 error = copyout(data->entries, te->data, len);
2293 kfree(data, M_SCSICD);
2294 kfree(lead, M_SCSICD);
2296 break;
2297 case CDIOREADTOCENTRY:
2299 struct cd_toc_single *data;
2300 struct ioc_read_toc_single_entry *te =
2301 (struct ioc_read_toc_single_entry *) addr;
2302 struct ioc_toc_header *th;
2303 u_int32_t track;
2305 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2307 cam_periph_lock(periph);
2308 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2309 ("trying to do CDIOREADTOCENTRY\n"));
2311 if (te->address_format != CD_MSF_FORMAT
2312 && te->address_format != CD_LBA_FORMAT) {
2313 kprintf("error in readtocentry, "
2314 " returning EINVAL\n");
2315 kfree(data, M_SCSICD);
2316 error = EINVAL;
2317 cam_periph_unlock(periph);
2318 break;
2321 th = &data->header;
2322 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2323 sizeof (*th), /*sense_flags*/0);
2324 if (error) {
2325 kfree(data, M_SCSICD);
2326 cam_periph_unlock(periph);
2327 break;
2330 if (softc->quirks & CD_Q_BCD_TRACKS) {
2331 /* we are going to have to convert the BCD
2332 * encoding on the cd to what is expected
2334 th->starting_track =
2335 bcd2bin(th->starting_track);
2336 th->ending_track = bcd2bin(th->ending_track);
2338 track = te->track;
2339 if (track == 0)
2340 track = th->starting_track;
2341 else if (track == LEADOUT)
2342 /* OK */;
2343 else if (track < th->starting_track ||
2344 track > th->ending_track + 1) {
2345 kprintf("error in readtocentry, "
2346 " returning EINVAL\n");
2347 kfree(data, M_SCSICD);
2348 error = EINVAL;
2349 cam_periph_unlock(periph);
2350 break;
2353 error = cdreadtoc(periph, te->address_format, track,
2354 (u_int8_t *)data, sizeof(*data),
2355 /*sense_flags*/0);
2356 if (error) {
2357 kfree(data, M_SCSICD);
2358 cam_periph_unlock(periph);
2359 break;
2362 if (softc->quirks & CD_Q_BCD_TRACKS)
2363 data->entry.track = bcd2bin(data->entry.track);
2364 bcopy(&data->entry, &te->entry,
2365 sizeof(struct cd_toc_entry));
2366 kfree(data, M_SCSICD);
2367 cam_periph_unlock(periph);
2369 break;
2370 case CDIOCSETPATCH:
2372 struct ioc_patch *arg = (struct ioc_patch *)addr;
2373 struct cd_mode_params params;
2374 union cd_pages *page;
2376 params.alloc_len = sizeof(union cd_mode_data_6_10);
2377 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2378 M_WAITOK | M_ZERO);
2380 cam_periph_lock(periph);
2381 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2382 ("trying to do CDIOCSETPATCH\n"));
2384 error = cdgetmode(periph, &params, AUDIO_PAGE);
2385 if (error) {
2386 kfree(params.mode_buf, M_SCSICD);
2387 cam_periph_unlock(periph);
2388 break;
2390 page = cdgetpage(&params);
2392 page->audio.port[LEFT_PORT].channels =
2393 arg->patch[0];
2394 page->audio.port[RIGHT_PORT].channels =
2395 arg->patch[1];
2396 page->audio.port[2].channels = arg->patch[2];
2397 page->audio.port[3].channels = arg->patch[3];
2398 error = cdsetmode(periph, &params);
2399 kfree(params.mode_buf, M_SCSICD);
2400 cam_periph_unlock(periph);
2402 break;
2403 case CDIOCGETVOL:
2405 struct ioc_vol *arg = (struct ioc_vol *) addr;
2406 struct cd_mode_params params;
2407 union cd_pages *page;
2409 params.alloc_len = sizeof(union cd_mode_data_6_10);
2410 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2411 M_WAITOK | M_ZERO);
2413 cam_periph_lock(periph);
2414 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2415 ("trying to do CDIOCGETVOL\n"));
2417 error = cdgetmode(periph, &params, AUDIO_PAGE);
2418 if (error) {
2419 kfree(params.mode_buf, M_SCSICD);
2420 cam_periph_unlock(periph);
2421 break;
2423 page = cdgetpage(&params);
2425 arg->vol[LEFT_PORT] =
2426 page->audio.port[LEFT_PORT].volume;
2427 arg->vol[RIGHT_PORT] =
2428 page->audio.port[RIGHT_PORT].volume;
2429 arg->vol[2] = page->audio.port[2].volume;
2430 arg->vol[3] = page->audio.port[3].volume;
2431 kfree(params.mode_buf, M_SCSICD);
2432 cam_periph_unlock(periph);
2434 break;
2435 case CDIOCSETVOL:
2437 struct ioc_vol *arg = (struct ioc_vol *) addr;
2438 struct cd_mode_params params;
2439 union cd_pages *page;
2441 params.alloc_len = sizeof(union cd_mode_data_6_10);
2442 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2443 M_WAITOK | M_ZERO);
2445 cam_periph_lock(periph);
2446 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2447 ("trying to do CDIOCSETVOL\n"));
2449 error = cdgetmode(periph, &params, AUDIO_PAGE);
2450 if (error) {
2451 kfree(params.mode_buf, M_SCSICD);
2452 cam_periph_unlock(periph);
2453 break;
2455 page = cdgetpage(&params);
2457 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2458 page->audio.port[LEFT_PORT].volume =
2459 arg->vol[LEFT_PORT];
2460 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2461 page->audio.port[RIGHT_PORT].volume =
2462 arg->vol[RIGHT_PORT];
2463 page->audio.port[2].volume = arg->vol[2];
2464 page->audio.port[3].volume = arg->vol[3];
2465 error = cdsetmode(periph, &params);
2466 cam_periph_unlock(periph);
2467 kfree(params.mode_buf, M_SCSICD);
2469 break;
2470 case CDIOCSETMONO:
2472 struct cd_mode_params params;
2473 union cd_pages *page;
2475 params.alloc_len = sizeof(union cd_mode_data_6_10);
2476 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2477 M_WAITOK | M_ZERO);
2479 cam_periph_lock(periph);
2480 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2481 ("trying to do CDIOCSETMONO\n"));
2483 error = cdgetmode(periph, &params, AUDIO_PAGE);
2484 if (error) {
2485 kfree(params.mode_buf, M_SCSICD);
2486 cam_periph_unlock(periph);
2487 break;
2489 page = cdgetpage(&params);
2491 page->audio.port[LEFT_PORT].channels =
2492 LEFT_CHANNEL | RIGHT_CHANNEL;
2493 page->audio.port[RIGHT_PORT].channels =
2494 LEFT_CHANNEL | RIGHT_CHANNEL;
2495 page->audio.port[2].channels = 0;
2496 page->audio.port[3].channels = 0;
2497 error = cdsetmode(periph, &params);
2498 cam_periph_unlock(periph);
2499 kfree(params.mode_buf, M_SCSICD);
2501 break;
2502 case CDIOCSETSTEREO:
2504 struct cd_mode_params params;
2505 union cd_pages *page;
2507 params.alloc_len = sizeof(union cd_mode_data_6_10);
2508 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2509 M_WAITOK | M_ZERO);
2511 cam_periph_lock(periph);
2512 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2513 ("trying to do CDIOCSETSTEREO\n"));
2515 error = cdgetmode(periph, &params, AUDIO_PAGE);
2516 if (error) {
2517 kfree(params.mode_buf, M_SCSICD);
2518 cam_periph_unlock(periph);
2519 break;
2521 page = cdgetpage(&params);
2523 page->audio.port[LEFT_PORT].channels =
2524 LEFT_CHANNEL;
2525 page->audio.port[RIGHT_PORT].channels =
2526 RIGHT_CHANNEL;
2527 page->audio.port[2].channels = 0;
2528 page->audio.port[3].channels = 0;
2529 error = cdsetmode(periph, &params);
2530 kfree(params.mode_buf, M_SCSICD);
2531 cam_periph_unlock(periph);
2533 break;
2534 case CDIOCSETMUTE:
2536 struct cd_mode_params params;
2537 union cd_pages *page;
2539 params.alloc_len = sizeof(union cd_mode_data_6_10);
2540 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2541 M_WAITOK | M_ZERO);
2543 cam_periph_lock(periph);
2544 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2545 ("trying to do CDIOCSETMUTE\n"));
2547 error = cdgetmode(periph, &params, AUDIO_PAGE);
2548 if (error) {
2549 kfree(&params, M_SCSICD);
2550 cam_periph_unlock(periph);
2551 break;
2553 page = cdgetpage(&params);
2555 page->audio.port[LEFT_PORT].channels = 0;
2556 page->audio.port[RIGHT_PORT].channels = 0;
2557 page->audio.port[2].channels = 0;
2558 page->audio.port[3].channels = 0;
2559 error = cdsetmode(periph, &params);
2560 kfree(params.mode_buf, M_SCSICD);
2561 cam_periph_unlock(periph);
2563 break;
2564 case CDIOCSETLEFT:
2566 struct cd_mode_params params;
2567 union cd_pages *page;
2569 params.alloc_len = sizeof(union cd_mode_data_6_10);
2570 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2571 M_WAITOK | M_ZERO);
2573 cam_periph_lock(periph);
2574 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2575 ("trying to do CDIOCSETLEFT\n"));
2577 error = cdgetmode(periph, &params, AUDIO_PAGE);
2578 if (error) {
2579 kfree(params.mode_buf, M_SCSICD);
2580 cam_periph_unlock(periph);
2581 break;
2583 page = cdgetpage(&params);
2585 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2586 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2587 page->audio.port[2].channels = 0;
2588 page->audio.port[3].channels = 0;
2589 error = cdsetmode(periph, &params);
2590 kfree(params.mode_buf, M_SCSICD);
2591 cam_periph_unlock(periph);
2593 break;
2594 case CDIOCSETRIGHT:
2596 struct cd_mode_params params;
2597 union cd_pages *page;
2599 params.alloc_len = sizeof(union cd_mode_data_6_10);
2600 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2601 M_WAITOK | M_ZERO);
2603 cam_periph_lock(periph);
2604 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2605 ("trying to do CDIOCSETRIGHT\n"));
2607 error = cdgetmode(periph, &params, AUDIO_PAGE);
2608 if (error) {
2609 kfree(params.mode_buf, M_SCSICD);
2610 cam_periph_unlock(periph);
2611 break;
2613 page = cdgetpage(&params);
2615 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2616 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2617 page->audio.port[2].channels = 0;
2618 page->audio.port[3].channels = 0;
2619 error = cdsetmode(periph, &params);
2620 kfree(params.mode_buf, M_SCSICD);
2621 cam_periph_unlock(periph);
2623 break;
2624 case CDIOCRESUME:
2625 cam_periph_lock(periph);
2626 error = cdpause(periph, 1);
2627 cam_periph_unlock(periph);
2628 break;
2629 case CDIOCPAUSE:
2630 cam_periph_lock(periph);
2631 error = cdpause(periph, 0);
2632 cam_periph_unlock(periph);
2633 break;
2634 case CDIOCSTART:
2635 cam_periph_lock(periph);
2636 error = cdstartunit(periph, 0);
2637 cam_periph_unlock(periph);
2638 break;
2639 case CDIOCCLOSE:
2640 cam_periph_lock(periph);
2641 error = cdstartunit(periph, 1);
2642 cam_periph_unlock(periph);
2643 break;
2644 case CDIOCSTOP:
2645 cam_periph_lock(periph);
2646 error = cdstopunit(periph, 0);
2647 cam_periph_unlock(periph);
2648 break;
2649 case CDIOCEJECT:
2650 cam_periph_lock(periph);
2651 error = cdstopunit(periph, 1);
2652 cam_periph_unlock(periph);
2653 break;
2654 case CDIOCALLOW:
2655 cam_periph_lock(periph);
2656 cdprevent(periph, PR_ALLOW);
2657 cam_periph_unlock(periph);
2658 break;
2659 case CDIOCPREVENT:
2660 cam_periph_lock(periph);
2661 cdprevent(periph, PR_PREVENT);
2662 cam_periph_unlock(periph);
2663 break;
2664 case CDIOCSETDEBUG:
2665 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2666 error = ENOTTY;
2667 break;
2668 case CDIOCCLRDEBUG:
2669 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2670 error = ENOTTY;
2671 break;
2672 case CDIOCRESET:
2673 /* return (cd_reset(periph)); */
2674 error = ENOTTY;
2675 break;
2676 case CDRIOCREADSPEED:
2677 cam_periph_lock(periph);
2678 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2679 cam_periph_unlock(periph);
2680 break;
2681 case CDRIOCWRITESPEED:
2682 cam_periph_lock(periph);
2683 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2684 cam_periph_unlock(periph);
2685 break;
2686 case DVDIOCSENDKEY:
2687 case DVDIOCREPORTKEY: {
2688 struct dvd_authinfo *authinfo;
2690 authinfo = (struct dvd_authinfo *)addr;
2692 cam_periph_lock(periph);
2693 if (ap->a_cmd == DVDIOCREPORTKEY)
2694 error = cdreportkey(periph, authinfo);
2695 else
2696 error = cdsendkey(periph, authinfo);
2697 cam_periph_unlock(periph);
2698 break;
2700 case DVDIOCREADSTRUCTURE: {
2701 struct dvd_struct *dvdstruct;
2703 dvdstruct = (struct dvd_struct *)addr;
2705 cam_periph_lock(periph);
2706 error = cdreaddvdstructure(periph, dvdstruct);
2707 cam_periph_unlock(periph);
2709 break;
2711 default:
2712 cam_periph_lock(periph);
2713 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2714 cam_periph_unlock(periph);
2715 break;
2718 cam_periph_lock(periph);
2719 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2720 cam_periph_unhold(periph, 1);
2722 return (error);
2725 static void
2726 cdprevent(struct cam_periph *periph, int action)
2728 union ccb *ccb;
2729 struct cd_softc *softc;
2730 int error;
2732 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2734 softc = (struct cd_softc *)periph->softc;
2736 if (((action == PR_ALLOW)
2737 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2738 || ((action == PR_PREVENT)
2739 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2740 return;
2743 ccb = cdgetccb(periph, /* priority */ 1);
2745 scsi_prevent(&ccb->csio,
2746 /*retries*/ 1,
2747 cddone,
2748 MSG_SIMPLE_Q_TAG,
2749 action,
2750 SSD_FULL_SIZE,
2751 /* timeout */60000);
2753 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2754 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2756 xpt_release_ccb(ccb);
2758 if (error == 0) {
2759 if (action == PR_ALLOW)
2760 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2761 else
2762 softc->flags |= CD_FLAG_DISC_LOCKED;
2767 cdcheckmedia(struct cam_periph *periph)
2769 struct cd_softc *softc;
2770 struct ioc_toc_header *toch;
2771 struct cd_toc_single leadout;
2772 struct ccb_getdev cgd;
2773 u_int32_t size, toclen;
2774 int error, num_entries, cdindex;
2775 int first_track_audio;
2776 struct disk_info info;
2778 softc = (struct cd_softc *)periph->softc;
2780 first_track_audio = -1;
2781 error = 0;
2783 cdprevent(periph, PR_PREVENT);
2786 * Set up disk info fields and tell the disk subsystem to reset
2787 * its internal copy of the label.
2789 bzero(&info, sizeof(info));
2790 info.d_type = DTYPE_SCSI;
2791 info.d_dsflags &= ~DSO_COMPATLABEL;
2792 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2795 * Grab the inquiry data to get the vendor and product names.
2796 * Put them in the typename and packname for the label.
2798 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2799 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2800 xpt_action((union ccb *)&cgd);
2802 #if 0
2803 strncpy(label->d_typename, cgd.inq_data.vendor,
2804 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2805 strncpy(label->d_packname, cgd.inq_data.product,
2806 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2807 #endif
2809 * Clear the valid media and TOC flags until we've verified that we
2810 * have both.
2812 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2815 * Get the disc size and block size. If we can't get it, we don't
2816 * have media, most likely.
2818 if ((error = cdsize(periph, &size)) != 0) {
2820 * Set a bogus sector size, so the slice code won't try to
2821 * divide by 0 and panic the kernel.
2823 info.d_media_blksize = 2048;
2824 disk_setdiskinfo(&softc->disk, &info);
2827 * Tell devstat(9) we don't have a blocksize.
2829 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2831 cdprevent(periph, PR_ALLOW);
2833 return (error);
2834 } else {
2835 info.d_media_blksize = softc->params.blksize;
2836 info.d_media_blocks = softc->params.disksize;
2837 disk_setdiskinfo(&softc->disk, &info);
2840 * We unconditionally (re)set the blocksize each time the
2841 * CD device is opened. This is because the CD can change,
2842 * and therefore the blocksize might change.
2843 * XXX problems here if some slice or partition is still
2844 * open with the old size?
2846 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2847 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2848 softc->device_stats.block_size = softc->params.blksize;
2850 softc->flags |= CD_FLAG_VALID_MEDIA;
2854 * Now we check the table of contents. This (currently) is only
2855 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2856 * things like present a separate entry in /dev for each track,
2857 * like that acd(4) driver does.
2859 bzero(&softc->toc, sizeof(softc->toc));
2860 toch = &softc->toc.header;
2863 * We will get errors here for media that doesn't have a table of
2864 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2865 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2866 * has not been recorded (no complete session) and the Format codes
2867 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2868 * with an INVALID FIELD IN CDB. Devices that are not capable of
2869 * reading an incomplete session on DDC/CD-R/RW media shall report
2870 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2872 * So this isn't fatal if we can't read the table of contents, it
2873 * just means that the user won't be able to issue the play tracks
2874 * ioctl, and likely lots of other stuff won't work either. They
2875 * need to burn the CD before we can do a whole lot with it. So
2876 * we don't print anything here if we get an error back.
2878 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2879 SF_NO_PRINT);
2881 * Errors in reading the table of contents aren't fatal, we just
2882 * won't have a valid table of contents cached.
2884 if (error != 0) {
2885 error = 0;
2886 bzero(&softc->toc, sizeof(softc->toc));
2887 goto bailout;
2890 if (softc->quirks & CD_Q_BCD_TRACKS) {
2891 toch->starting_track = bcd2bin(toch->starting_track);
2892 toch->ending_track = bcd2bin(toch->ending_track);
2895 /* Number of TOC entries, plus leadout */
2896 num_entries = (toch->ending_track - toch->starting_track) + 2;
2898 if (num_entries <= 0)
2899 goto bailout;
2901 toclen = num_entries * sizeof(struct cd_toc_entry);
2903 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2904 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2905 SF_NO_PRINT);
2906 if (error != 0) {
2907 error = 0;
2908 bzero(&softc->toc, sizeof(softc->toc));
2909 goto bailout;
2912 if (softc->quirks & CD_Q_BCD_TRACKS) {
2913 toch->starting_track = bcd2bin(toch->starting_track);
2914 toch->ending_track = bcd2bin(toch->ending_track);
2916 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2919 * XXX KDM is this necessary? Probably only if the drive doesn't
2920 * return leadout information with the table of contents.
2922 cdindex = toch->starting_track + num_entries -1;
2923 if (cdindex == toch->ending_track + 1) {
2925 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2926 (u_int8_t *)&leadout, sizeof(leadout),
2927 SF_NO_PRINT);
2928 if (error != 0) {
2929 error = 0;
2930 goto bailout;
2932 softc->toc.entries[cdindex - toch->starting_track] =
2933 leadout.entry;
2935 if (softc->quirks & CD_Q_BCD_TRACKS) {
2936 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2937 softc->toc.entries[cdindex].track =
2938 bcd2bin(softc->toc.entries[cdindex].track);
2943 * Run through the TOC entries, find the first entry and determine
2944 * whether it is an audio or data track.
2946 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
2947 if (softc->toc.entries[cdindex].track == toch->starting_track) {
2948 if (softc->toc.entries[cdindex].control & 0x04)
2949 first_track_audio = 0;
2950 else
2951 first_track_audio = 1;
2952 break;
2957 * If first_track_audio is non-zero, we either have an error (e.g.
2958 * couldn't find the starting track) or the first track is an audio
2959 * track. If first_track_audio is 0, the first track is a data
2960 * track that could have a disklabel. Attempt to read the
2961 * disklabel off the media, just in case the user put one there.
2963 if (first_track_audio == 0) {
2964 info.d_dsflags |= DSO_COMPATLABEL;
2965 info.d_dsflags &= ~DSO_NOLABELS;
2966 disk_setdiskinfo(&softc->disk, &info);
2968 softc->flags |= CD_FLAG_VALID_TOC;
2970 bailout:
2971 return (error);
2974 static int
2975 cdsize(struct cam_periph *periph, u_int32_t *size)
2977 struct cd_softc *softc;
2978 union ccb *ccb;
2979 struct scsi_read_capacity_data *rcap_buf;
2980 int error;
2982 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2984 softc = (struct cd_softc *)periph->softc;
2986 ccb = cdgetccb(periph, /* priority */ 1);
2988 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
2989 M_SCSICD, M_INTWAIT | M_ZERO);
2991 scsi_read_capacity(&ccb->csio,
2992 /*retries*/ 1,
2993 cddone,
2994 MSG_SIMPLE_Q_TAG,
2995 rcap_buf,
2996 SSD_FULL_SIZE,
2997 /* timeout */20000);
2999 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3000 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3002 xpt_release_ccb(ccb);
3004 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3005 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3007 * SCSI-3 mandates that the reported blocksize shall be 2048.
3008 * Older drives sometimes report funny values, trim it down to
3009 * 2048, or other parts of the kernel will get confused.
3011 * XXX we leave drives alone that might report 512 bytes, as
3012 * well as drives reporting more weird sizes like perhaps 4K.
3014 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3015 softc->params.blksize = 2048;
3017 kfree(rcap_buf, M_SCSICD);
3018 *size = softc->params.disksize;
3020 return (error);
3024 static int
3025 cd6byteworkaround(union ccb *ccb)
3027 u_int8_t *cdb;
3028 struct cam_periph *periph;
3029 struct cd_softc *softc;
3030 struct cd_mode_params *params;
3031 int frozen, found;
3033 periph = xpt_path_periph(ccb->ccb_h.path);
3034 softc = (struct cd_softc *)periph->softc;
3036 cdb = ccb->csio.cdb_io.cdb_bytes;
3038 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3039 || ((cdb[0] != MODE_SENSE_6)
3040 && (cdb[0] != MODE_SELECT_6)))
3041 return (0);
3044 * Because there is no convenient place to stash the overall
3045 * cd_mode_params structure pointer, we have to grab it like this.
3046 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3047 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3049 * XXX It would be nice if, at some point, we could increase the
3050 * number of available peripheral private pointers. Both pointers
3051 * are currently used in most every peripheral driver.
3053 found = 0;
3055 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3056 if (params->mode_buf == ccb->csio.data_ptr) {
3057 found = 1;
3058 break;
3063 * This shouldn't happen. All mode sense and mode select
3064 * operations in the cd(4) driver MUST go through cdgetmode() and
3065 * cdsetmode()!
3067 if (found == 0) {
3068 xpt_print(periph->path,
3069 "mode buffer not found in mode queue!\n");
3070 return (0);
3073 params->cdb_size = 10;
3074 softc->minimum_command_size = 10;
3075 xpt_print(ccb->ccb_h.path,
3076 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3077 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3079 if (cdb[0] == MODE_SENSE_6) {
3080 struct scsi_mode_sense_10 ms10;
3081 struct scsi_mode_sense_6 *ms6;
3082 int len;
3084 ms6 = (struct scsi_mode_sense_6 *)cdb;
3086 bzero(&ms10, sizeof(ms10));
3087 ms10.opcode = MODE_SENSE_10;
3088 ms10.byte2 = ms6->byte2;
3089 ms10.page = ms6->page;
3092 * 10 byte mode header, block descriptor,
3093 * sizeof(union cd_pages)
3095 len = sizeof(struct cd_mode_data_10);
3096 ccb->csio.dxfer_len = len;
3098 scsi_ulto2b(len, ms10.length);
3099 ms10.control = ms6->control;
3100 bcopy(&ms10, cdb, 10);
3101 ccb->csio.cdb_len = 10;
3102 } else {
3103 struct scsi_mode_select_10 ms10;
3104 struct scsi_mode_select_6 *ms6;
3105 struct scsi_mode_header_6 *header6;
3106 struct scsi_mode_header_10 *header10;
3107 struct scsi_mode_page_header *page_header;
3108 int blk_desc_len, page_num, page_size, len;
3110 ms6 = (struct scsi_mode_select_6 *)cdb;
3112 bzero(&ms10, sizeof(ms10));
3113 ms10.opcode = MODE_SELECT_10;
3114 ms10.byte2 = ms6->byte2;
3116 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3117 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3119 page_header = find_mode_page_6(header6);
3120 page_num = page_header->page_code;
3122 blk_desc_len = header6->blk_desc_len;
3124 page_size = cdgetpagesize(page_num);
3126 if (page_size != (page_header->page_length +
3127 sizeof(*page_header)))
3128 page_size = page_header->page_length +
3129 sizeof(*page_header);
3131 len = sizeof(*header10) + blk_desc_len + page_size;
3133 len = min(params->alloc_len, len);
3136 * Since the 6 byte parameter header is shorter than the 10
3137 * byte parameter header, we need to copy the actual mode
3138 * page data, and the block descriptor, if any, so things wind
3139 * up in the right place. The regions will overlap, but
3140 * bcopy() does the right thing.
3142 bcopy(params->mode_buf + sizeof(*header6),
3143 params->mode_buf + sizeof(*header10),
3144 len - sizeof(*header10));
3146 /* Make sure these fields are set correctly. */
3147 scsi_ulto2b(0, header10->data_length);
3148 header10->medium_type = 0;
3149 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3151 ccb->csio.dxfer_len = len;
3153 scsi_ulto2b(len, ms10.length);
3154 ms10.control = ms6->control;
3155 bcopy(&ms10, cdb, 10);
3156 ccb->csio.cdb_len = 10;
3159 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3160 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3161 xpt_action(ccb);
3162 if (frozen) {
3163 cam_release_devq(ccb->ccb_h.path,
3164 /*relsim_flags*/0,
3165 /*openings*/0,
3166 /*timeout*/0,
3167 /*getcount_only*/0);
3170 return (ERESTART);
3173 static int
3174 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3176 struct cd_softc *softc;
3177 struct cam_periph *periph;
3178 int error;
3180 periph = xpt_path_periph(ccb->ccb_h.path);
3181 softc = (struct cd_softc *)periph->softc;
3183 error = 0;
3186 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3187 * CDB comes back with this particular error, try transforming it
3188 * into the 10 byte version.
3190 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3191 error = cd6byteworkaround(ccb);
3192 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3193 CAM_SCSI_STATUS_ERROR)
3194 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3195 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3196 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3197 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3198 int sense_key, error_code, asc, ascq;
3200 scsi_extract_sense(&ccb->csio.sense_data,
3201 &error_code, &sense_key, &asc, &ascq);
3202 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3203 error = cd6byteworkaround(ccb);
3206 if (error == ERESTART)
3207 return (error);
3210 * XXX
3211 * Until we have a better way of doing pack validation,
3212 * don't treat UAs as errors.
3214 sense_flags |= SF_RETRY_UA;
3215 return (cam_periph_error(ccb, cam_flags, sense_flags,
3216 &softc->saved_ccb));
3220 * Read table of contents
3222 static int
3223 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3224 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3226 struct scsi_read_toc *scsi_cmd;
3227 u_int32_t ntoc;
3228 struct ccb_scsiio *csio;
3229 union ccb *ccb;
3230 int error;
3232 ntoc = len;
3233 error = 0;
3235 ccb = cdgetccb(periph, /* priority */ 1);
3237 csio = &ccb->csio;
3239 cam_fill_csio(csio,
3240 /* retries */ 1,
3241 /* cbfcnp */ cddone,
3242 /* flags */ CAM_DIR_IN,
3243 /* tag_action */ MSG_SIMPLE_Q_TAG,
3244 /* data_ptr */ data,
3245 /* dxfer_len */ len,
3246 /* sense_len */ SSD_FULL_SIZE,
3247 sizeof(struct scsi_read_toc),
3248 /* timeout */ 50000);
3250 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3251 bzero (scsi_cmd, sizeof(*scsi_cmd));
3253 if (mode == CD_MSF_FORMAT)
3254 scsi_cmd->byte2 |= CD_MSF;
3255 scsi_cmd->from_track = start;
3256 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3257 scsi_cmd->data_len[0] = (ntoc) >> 8;
3258 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3260 scsi_cmd->op_code = READ_TOC;
3262 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3263 /*sense_flags*/SF_RETRY_UA | sense_flags);
3265 xpt_release_ccb(ccb);
3267 return(error);
3270 static int
3271 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3272 u_int32_t format, int track,
3273 struct cd_sub_channel_info *data, u_int32_t len)
3275 struct scsi_read_subchannel *scsi_cmd;
3276 struct ccb_scsiio *csio;
3277 union ccb *ccb;
3278 int error;
3280 error = 0;
3282 ccb = cdgetccb(periph, /* priority */ 1);
3284 csio = &ccb->csio;
3286 cam_fill_csio(csio,
3287 /* retries */ 1,
3288 /* cbfcnp */ cddone,
3289 /* flags */ CAM_DIR_IN,
3290 /* tag_action */ MSG_SIMPLE_Q_TAG,
3291 /* data_ptr */ (u_int8_t *)data,
3292 /* dxfer_len */ len,
3293 /* sense_len */ SSD_FULL_SIZE,
3294 sizeof(struct scsi_read_subchannel),
3295 /* timeout */ 50000);
3297 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3298 bzero (scsi_cmd, sizeof(*scsi_cmd));
3300 scsi_cmd->op_code = READ_SUBCHANNEL;
3301 if (mode == CD_MSF_FORMAT)
3302 scsi_cmd->byte1 |= CD_MSF;
3303 scsi_cmd->byte2 = SRS_SUBQ;
3304 scsi_cmd->subchan_format = format;
3305 scsi_cmd->track = track;
3306 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3307 scsi_cmd->control = 0;
3309 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3310 /*sense_flags*/SF_RETRY_UA);
3312 xpt_release_ccb(ccb);
3314 return(error);
3318 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3319 * routine. See comments in cd6byteworkaround() for details.
3321 static int
3322 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3323 u_int32_t page)
3325 struct ccb_scsiio *csio;
3326 struct cd_softc *softc;
3327 union ccb *ccb;
3328 int param_len;
3329 int error;
3331 softc = (struct cd_softc *)periph->softc;
3333 ccb = cdgetccb(periph, /* priority */ 1);
3335 csio = &ccb->csio;
3337 data->cdb_size = softc->minimum_command_size;
3338 if (data->cdb_size < 10)
3339 param_len = sizeof(struct cd_mode_data);
3340 else
3341 param_len = sizeof(struct cd_mode_data_10);
3343 /* Don't say we've got more room than we actually allocated */
3344 param_len = min(param_len, data->alloc_len);
3346 scsi_mode_sense_len(csio,
3347 /* retries */ 1,
3348 /* cbfcnp */ cddone,
3349 /* tag_action */ MSG_SIMPLE_Q_TAG,
3350 /* dbd */ 0,
3351 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3352 /* page */ page,
3353 /* param_buf */ data->mode_buf,
3354 /* param_len */ param_len,
3355 /* minimum_cmd_size */ softc->minimum_command_size,
3356 /* sense_len */ SSD_FULL_SIZE,
3357 /* timeout */ 50000);
3360 * It would be nice not to have to do this, but there's no
3361 * available pointer in the CCB that would allow us to stuff the
3362 * mode params structure in there and retrieve it in
3363 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3364 * lets the caller know what CDB size we ended up using, so they
3365 * can find the actual mode page offset.
3367 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3369 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3370 /*sense_flags*/SF_RETRY_UA);
3372 xpt_release_ccb(ccb);
3374 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3377 * This is a bit of belt-and-suspenders checking, but if we run
3378 * into a situation where the target sends back multiple block
3379 * descriptors, we might not have enough space in the buffer to
3380 * see the whole mode page. Better to return an error than
3381 * potentially access memory beyond our malloced region.
3383 if (error == 0) {
3384 u_int32_t data_len;
3386 if (data->cdb_size == 10) {
3387 struct scsi_mode_header_10 *hdr10;
3389 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3390 data_len = scsi_2btoul(hdr10->data_length);
3391 data_len += sizeof(hdr10->data_length);
3392 } else {
3393 struct scsi_mode_header_6 *hdr6;
3395 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3396 data_len = hdr6->data_length;
3397 data_len += sizeof(hdr6->data_length);
3401 * Complain if there is more mode data available than we
3402 * allocated space for. This could potentially happen if
3403 * we miscalculated the page length for some reason, if the
3404 * drive returns multiple block descriptors, or if it sets
3405 * the data length incorrectly.
3407 if (data_len > data->alloc_len) {
3408 xpt_print(periph->path, "allocated modepage %d length "
3409 "%d < returned length %d\n", page, data->alloc_len,
3410 data_len);
3411 error = ENOSPC;
3414 return (error);
3418 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3419 * routine. See comments in cd6byteworkaround() for details.
3421 static int
3422 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3424 struct ccb_scsiio *csio;
3425 struct cd_softc *softc;
3426 union ccb *ccb;
3427 int cdb_size, param_len;
3428 int error;
3430 softc = (struct cd_softc *)periph->softc;
3432 ccb = cdgetccb(periph, /* priority */ 1);
3434 csio = &ccb->csio;
3436 error = 0;
3439 * If the data is formatted for the 10 byte version of the mode
3440 * select parameter list, we need to use the 10 byte CDB.
3441 * Otherwise, we use whatever the stored minimum command size.
3443 if (data->cdb_size == 10)
3444 cdb_size = data->cdb_size;
3445 else
3446 cdb_size = softc->minimum_command_size;
3448 if (cdb_size >= 10) {
3449 struct scsi_mode_header_10 *mode_header;
3450 u_int32_t data_len;
3452 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3454 data_len = scsi_2btoul(mode_header->data_length);
3456 scsi_ulto2b(0, mode_header->data_length);
3458 * SONY drives do not allow a mode select with a medium_type
3459 * value that has just been returned by a mode sense; use a
3460 * medium_type of 0 (Default) instead.
3462 mode_header->medium_type = 0;
3465 * Pass back whatever the drive passed to us, plus the size
3466 * of the data length field.
3468 param_len = data_len + sizeof(mode_header->data_length);
3470 } else {
3471 struct scsi_mode_header_6 *mode_header;
3473 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3475 param_len = mode_header->data_length + 1;
3477 mode_header->data_length = 0;
3479 * SONY drives do not allow a mode select with a medium_type
3480 * value that has just been returned by a mode sense; use a
3481 * medium_type of 0 (Default) instead.
3483 mode_header->medium_type = 0;
3486 /* Don't say we've got more room than we actually allocated */
3487 param_len = min(param_len, data->alloc_len);
3489 scsi_mode_select_len(csio,
3490 /* retries */ 1,
3491 /* cbfcnp */ cddone,
3492 /* tag_action */ MSG_SIMPLE_Q_TAG,
3493 /* scsi_page_fmt */ 1,
3494 /* save_pages */ 0,
3495 /* param_buf */ data->mode_buf,
3496 /* param_len */ param_len,
3497 /* minimum_cmd_size */ cdb_size,
3498 /* sense_len */ SSD_FULL_SIZE,
3499 /* timeout */ 50000);
3501 /* See comments in cdgetmode() and cd6byteworkaround(). */
3502 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3504 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3505 /*sense_flags*/SF_RETRY_UA);
3507 xpt_release_ccb(ccb);
3509 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3511 return (error);
3516 static int
3517 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3519 struct ccb_scsiio *csio;
3520 union ccb *ccb;
3521 int error;
3522 u_int8_t cdb_len;
3524 error = 0;
3525 ccb = cdgetccb(periph, /* priority */ 1);
3526 csio = &ccb->csio;
3528 * Use the smallest possible command to perform the operation.
3530 if ((len & 0xffff0000) == 0) {
3532 * We can fit in a 10 byte cdb.
3534 struct scsi_play_10 *scsi_cmd;
3536 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3537 bzero (scsi_cmd, sizeof(*scsi_cmd));
3538 scsi_cmd->op_code = PLAY_10;
3539 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3540 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3541 cdb_len = sizeof(*scsi_cmd);
3542 } else {
3543 struct scsi_play_12 *scsi_cmd;
3545 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3546 bzero (scsi_cmd, sizeof(*scsi_cmd));
3547 scsi_cmd->op_code = PLAY_12;
3548 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3549 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3550 cdb_len = sizeof(*scsi_cmd);
3552 cam_fill_csio(csio,
3553 /*retries*/2,
3554 cddone,
3555 /*flags*/CAM_DIR_NONE,
3556 MSG_SIMPLE_Q_TAG,
3557 /*dataptr*/NULL,
3558 /*datalen*/0,
3559 /*sense_len*/SSD_FULL_SIZE,
3560 cdb_len,
3561 /*timeout*/50 * 1000);
3563 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3564 /*sense_flags*/SF_RETRY_UA);
3566 xpt_release_ccb(ccb);
3568 return(error);
3571 static int
3572 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3573 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3575 struct scsi_play_msf *scsi_cmd;
3576 struct ccb_scsiio *csio;
3577 union ccb *ccb;
3578 int error;
3580 error = 0;
3582 ccb = cdgetccb(periph, /* priority */ 1);
3584 csio = &ccb->csio;
3586 cam_fill_csio(csio,
3587 /* retries */ 1,
3588 /* cbfcnp */ cddone,
3589 /* flags */ CAM_DIR_NONE,
3590 /* tag_action */ MSG_SIMPLE_Q_TAG,
3591 /* data_ptr */ NULL,
3592 /* dxfer_len */ 0,
3593 /* sense_len */ SSD_FULL_SIZE,
3594 sizeof(struct scsi_play_msf),
3595 /* timeout */ 50000);
3597 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3598 bzero (scsi_cmd, sizeof(*scsi_cmd));
3600 scsi_cmd->op_code = PLAY_MSF;
3601 scsi_cmd->start_m = startm;
3602 scsi_cmd->start_s = starts;
3603 scsi_cmd->start_f = startf;
3604 scsi_cmd->end_m = endm;
3605 scsi_cmd->end_s = ends;
3606 scsi_cmd->end_f = endf;
3608 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3609 /*sense_flags*/SF_RETRY_UA);
3611 xpt_release_ccb(ccb);
3613 return(error);
3617 static int
3618 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3619 u_int32_t etrack, u_int32_t eindex)
3621 struct scsi_play_track *scsi_cmd;
3622 struct ccb_scsiio *csio;
3623 union ccb *ccb;
3624 int error;
3626 error = 0;
3628 ccb = cdgetccb(periph, /* priority */ 1);
3630 csio = &ccb->csio;
3632 cam_fill_csio(csio,
3633 /* retries */ 1,
3634 /* cbfcnp */ cddone,
3635 /* flags */ CAM_DIR_NONE,
3636 /* tag_action */ MSG_SIMPLE_Q_TAG,
3637 /* data_ptr */ NULL,
3638 /* dxfer_len */ 0,
3639 /* sense_len */ SSD_FULL_SIZE,
3640 sizeof(struct scsi_play_track),
3641 /* timeout */ 50000);
3643 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3644 bzero (scsi_cmd, sizeof(*scsi_cmd));
3646 scsi_cmd->op_code = PLAY_TRACK;
3647 scsi_cmd->start_track = strack;
3648 scsi_cmd->start_index = sindex;
3649 scsi_cmd->end_track = etrack;
3650 scsi_cmd->end_index = eindex;
3652 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3653 /*sense_flags*/SF_RETRY_UA);
3655 xpt_release_ccb(ccb);
3657 return(error);
3660 static int
3661 cdpause(struct cam_periph *periph, u_int32_t go)
3663 struct scsi_pause *scsi_cmd;
3664 struct ccb_scsiio *csio;
3665 union ccb *ccb;
3666 int error;
3668 error = 0;
3670 ccb = cdgetccb(periph, /* priority */ 1);
3672 csio = &ccb->csio;
3674 cam_fill_csio(csio,
3675 /* retries */ 1,
3676 /* cbfcnp */ cddone,
3677 /* flags */ CAM_DIR_NONE,
3678 /* tag_action */ MSG_SIMPLE_Q_TAG,
3679 /* data_ptr */ NULL,
3680 /* dxfer_len */ 0,
3681 /* sense_len */ SSD_FULL_SIZE,
3682 sizeof(struct scsi_pause),
3683 /* timeout */ 50000);
3685 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3686 bzero (scsi_cmd, sizeof(*scsi_cmd));
3688 scsi_cmd->op_code = PAUSE;
3689 scsi_cmd->resume = go;
3691 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3692 /*sense_flags*/SF_RETRY_UA);
3694 xpt_release_ccb(ccb);
3696 return(error);
3699 static int
3700 cdstartunit(struct cam_periph *periph, int load)
3702 union ccb *ccb;
3703 int error;
3705 error = 0;
3707 ccb = cdgetccb(periph, /* priority */ 1);
3709 scsi_start_stop(&ccb->csio,
3710 /* retries */ 1,
3711 /* cbfcnp */ cddone,
3712 /* tag_action */ MSG_SIMPLE_Q_TAG,
3713 /* start */ TRUE,
3714 /* load_eject */ load,
3715 /* immediate */ FALSE,
3716 /* sense_len */ SSD_FULL_SIZE,
3717 /* timeout */ 50000);
3719 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3720 /*sense_flags*/SF_RETRY_UA);
3722 xpt_release_ccb(ccb);
3724 return(error);
3727 static int
3728 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3730 union ccb *ccb;
3731 int error;
3733 error = 0;
3735 ccb = cdgetccb(periph, /* priority */ 1);
3737 scsi_start_stop(&ccb->csio,
3738 /* retries */ 1,
3739 /* cbfcnp */ cddone,
3740 /* tag_action */ MSG_SIMPLE_Q_TAG,
3741 /* start */ FALSE,
3742 /* load_eject */ eject,
3743 /* immediate */ FALSE,
3744 /* sense_len */ SSD_FULL_SIZE,
3745 /* timeout */ 50000);
3747 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3748 /*sense_flags*/SF_RETRY_UA);
3750 xpt_release_ccb(ccb);
3752 return(error);
3755 static int
3756 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3758 struct scsi_set_speed *scsi_cmd;
3759 struct ccb_scsiio *csio;
3760 union ccb *ccb;
3761 int error;
3763 error = 0;
3764 ccb = cdgetccb(periph, /* priority */ 1);
3765 csio = &ccb->csio;
3767 /* Preserve old behavior: units in multiples of CDROM speed */
3768 if (rdspeed < 177)
3769 rdspeed *= 177;
3770 if (wrspeed < 177)
3771 wrspeed *= 177;
3773 cam_fill_csio(csio,
3774 /* retries */ 1,
3775 /* cbfcnp */ cddone,
3776 /* flags */ CAM_DIR_NONE,
3777 /* tag_action */ MSG_SIMPLE_Q_TAG,
3778 /* data_ptr */ NULL,
3779 /* dxfer_len */ 0,
3780 /* sense_len */ SSD_FULL_SIZE,
3781 sizeof(struct scsi_set_speed),
3782 /* timeout */ 50000);
3784 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3785 bzero(scsi_cmd, sizeof(*scsi_cmd));
3787 scsi_cmd->opcode = SET_CD_SPEED;
3788 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3789 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3791 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3792 /*sense_flags*/SF_RETRY_UA);
3794 xpt_release_ccb(ccb);
3796 return(error);
3799 static int
3800 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3802 union ccb *ccb;
3803 u_int8_t *databuf;
3804 u_int32_t lba;
3805 int error;
3806 int length;
3808 error = 0;
3809 databuf = NULL;
3810 lba = 0;
3812 ccb = cdgetccb(periph, /* priority */ 1);
3814 switch (authinfo->format) {
3815 case DVD_REPORT_AGID:
3816 length = sizeof(struct scsi_report_key_data_agid);
3817 break;
3818 case DVD_REPORT_CHALLENGE:
3819 length = sizeof(struct scsi_report_key_data_challenge);
3820 break;
3821 case DVD_REPORT_KEY1:
3822 length = sizeof(struct scsi_report_key_data_key1_key2);
3823 break;
3824 case DVD_REPORT_TITLE_KEY:
3825 length = sizeof(struct scsi_report_key_data_title);
3826 /* The lba field is only set for the title key */
3827 lba = authinfo->lba;
3828 break;
3829 case DVD_REPORT_ASF:
3830 length = sizeof(struct scsi_report_key_data_asf);
3831 break;
3832 case DVD_REPORT_RPC:
3833 length = sizeof(struct scsi_report_key_data_rpc);
3834 break;
3835 case DVD_INVALIDATE_AGID:
3836 length = 0;
3837 break;
3838 default:
3839 error = EINVAL;
3840 goto bailout;
3841 break; /* NOTREACHED */
3844 if (length != 0) {
3845 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3846 } else {
3847 databuf = NULL;
3851 scsi_report_key(&ccb->csio,
3852 /* retries */ 1,
3853 /* cbfcnp */ cddone,
3854 /* tag_action */ MSG_SIMPLE_Q_TAG,
3855 /* lba */ lba,
3856 /* agid */ authinfo->agid,
3857 /* key_format */ authinfo->format,
3858 /* data_ptr */ databuf,
3859 /* dxfer_len */ length,
3860 /* sense_len */ SSD_FULL_SIZE,
3861 /* timeout */ 50000);
3863 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3864 /*sense_flags*/SF_RETRY_UA);
3866 if (error != 0)
3867 goto bailout;
3869 if (ccb->csio.resid != 0) {
3870 xpt_print(periph->path, "warning, residual for report key "
3871 "command is %d\n", ccb->csio.resid);
3874 switch(authinfo->format) {
3875 case DVD_REPORT_AGID: {
3876 struct scsi_report_key_data_agid *agid_data;
3878 agid_data = (struct scsi_report_key_data_agid *)databuf;
3880 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3881 RKD_AGID_SHIFT;
3882 break;
3884 case DVD_REPORT_CHALLENGE: {
3885 struct scsi_report_key_data_challenge *chal_data;
3887 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3889 bcopy(chal_data->challenge_key, authinfo->keychal,
3890 min(sizeof(chal_data->challenge_key),
3891 sizeof(authinfo->keychal)));
3892 break;
3894 case DVD_REPORT_KEY1: {
3895 struct scsi_report_key_data_key1_key2 *key1_data;
3897 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3899 bcopy(key1_data->key1, authinfo->keychal,
3900 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3901 break;
3903 case DVD_REPORT_TITLE_KEY: {
3904 struct scsi_report_key_data_title *title_data;
3906 title_data = (struct scsi_report_key_data_title *)databuf;
3908 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3909 RKD_TITLE_CPM_SHIFT;
3910 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3911 RKD_TITLE_CP_SEC_SHIFT;
3912 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3913 RKD_TITLE_CMGS_SHIFT;
3914 bcopy(title_data->title_key, authinfo->keychal,
3915 min(sizeof(title_data->title_key),
3916 sizeof(authinfo->keychal)));
3917 break;
3919 case DVD_REPORT_ASF: {
3920 struct scsi_report_key_data_asf *asf_data;
3922 asf_data = (struct scsi_report_key_data_asf *)databuf;
3924 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3925 break;
3927 case DVD_REPORT_RPC: {
3928 struct scsi_report_key_data_rpc *rpc_data;
3930 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3932 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3933 RKD_RPC_TYPE_SHIFT;
3934 authinfo->vend_rsts =
3935 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
3936 RKD_RPC_VENDOR_RESET_SHIFT;
3937 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
3938 authinfo->region = rpc_data->region_mask;
3939 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
3940 break;
3942 case DVD_INVALIDATE_AGID:
3943 break;
3944 default:
3945 /* This should be impossible, since we checked above */
3946 error = EINVAL;
3947 goto bailout;
3948 break; /* NOTREACHED */
3950 bailout:
3951 if (databuf != NULL)
3952 kfree(databuf, M_DEVBUF);
3954 xpt_release_ccb(ccb);
3956 return(error);
3959 static int
3960 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3962 union ccb *ccb;
3963 u_int8_t *databuf;
3964 int length;
3965 int error;
3967 error = 0;
3968 databuf = NULL;
3970 ccb = cdgetccb(periph, /* priority */ 1);
3972 switch(authinfo->format) {
3973 case DVD_SEND_CHALLENGE: {
3974 struct scsi_report_key_data_challenge *challenge_data;
3976 length = sizeof(*challenge_data);
3978 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3980 databuf = (u_int8_t *)challenge_data;
3982 scsi_ulto2b(length - sizeof(challenge_data->data_len),
3983 challenge_data->data_len);
3985 bcopy(authinfo->keychal, challenge_data->challenge_key,
3986 min(sizeof(authinfo->keychal),
3987 sizeof(challenge_data->challenge_key)));
3988 break;
3990 case DVD_SEND_KEY2: {
3991 struct scsi_report_key_data_key1_key2 *key2_data;
3993 length = sizeof(*key2_data);
3995 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3997 databuf = (u_int8_t *)key2_data;
3999 scsi_ulto2b(length - sizeof(key2_data->data_len),
4000 key2_data->data_len);
4002 bcopy(authinfo->keychal, key2_data->key1,
4003 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4005 break;
4007 case DVD_SEND_RPC: {
4008 struct scsi_send_key_data_rpc *rpc_data;
4010 length = sizeof(*rpc_data);
4012 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4014 databuf = (u_int8_t *)rpc_data;
4016 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4017 rpc_data->data_len);
4019 rpc_data->region_code = authinfo->region;
4020 break;
4022 default:
4023 error = EINVAL;
4024 goto bailout;
4025 break; /* NOTREACHED */
4028 scsi_send_key(&ccb->csio,
4029 /* retries */ 1,
4030 /* cbfcnp */ cddone,
4031 /* tag_action */ MSG_SIMPLE_Q_TAG,
4032 /* agid */ authinfo->agid,
4033 /* key_format */ authinfo->format,
4034 /* data_ptr */ databuf,
4035 /* dxfer_len */ length,
4036 /* sense_len */ SSD_FULL_SIZE,
4037 /* timeout */ 50000);
4039 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4040 /*sense_flags*/SF_RETRY_UA);
4042 bailout:
4044 if (databuf != NULL)
4045 kfree(databuf, M_DEVBUF);
4047 xpt_release_ccb(ccb);
4049 return(error);
4052 static int
4053 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4055 union ccb *ccb;
4056 u_int8_t *databuf;
4057 u_int32_t address;
4058 int error;
4059 int length;
4061 error = 0;
4062 databuf = NULL;
4063 /* The address is reserved for many of the formats */
4064 address = 0;
4066 ccb = cdgetccb(periph, /* priority */ 1);
4068 switch(dvdstruct->format) {
4069 case DVD_STRUCT_PHYSICAL:
4070 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4071 break;
4072 case DVD_STRUCT_COPYRIGHT:
4073 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4074 break;
4075 case DVD_STRUCT_DISCKEY:
4076 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4077 break;
4078 case DVD_STRUCT_BCA:
4079 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4080 break;
4081 case DVD_STRUCT_MANUFACT:
4082 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4083 break;
4084 case DVD_STRUCT_CMI:
4085 error = ENODEV;
4086 goto bailout;
4087 #ifdef notyet
4088 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4089 address = dvdstruct->address;
4090 #endif
4091 break; /* NOTREACHED */
4092 case DVD_STRUCT_PROTDISCID:
4093 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4094 break;
4095 case DVD_STRUCT_DISCKEYBLOCK:
4096 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4097 break;
4098 case DVD_STRUCT_DDS:
4099 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4100 break;
4101 case DVD_STRUCT_MEDIUM_STAT:
4102 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4103 break;
4104 case DVD_STRUCT_SPARE_AREA:
4105 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4106 break;
4107 case DVD_STRUCT_RMD_LAST:
4108 error = ENODEV;
4109 goto bailout;
4110 #ifdef notyet
4111 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4112 address = dvdstruct->address;
4113 #endif
4114 break; /* NOTREACHED */
4115 case DVD_STRUCT_RMD_RMA:
4116 error = ENODEV;
4117 goto bailout;
4118 #ifdef notyet
4119 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4120 address = dvdstruct->address;
4121 #endif
4122 break; /* NOTREACHED */
4123 case DVD_STRUCT_PRERECORDED:
4124 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4125 break;
4126 case DVD_STRUCT_UNIQUEID:
4127 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4128 break;
4129 case DVD_STRUCT_DCB:
4130 error = ENODEV;
4131 goto bailout;
4132 #ifdef notyet
4133 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4134 address = dvdstruct->address;
4135 #endif
4136 break; /* NOTREACHED */
4137 case DVD_STRUCT_LIST:
4139 * This is the maximum allocation length for the READ DVD
4140 * STRUCTURE command. There's nothing in the MMC3 spec
4141 * that indicates a limit in the amount of data that can
4142 * be returned from this call, other than the limits
4143 * imposed by the 2-byte length variables.
4145 length = 65535;
4146 break;
4147 default:
4148 error = EINVAL;
4149 goto bailout;
4150 break; /* NOTREACHED */
4153 if (length != 0) {
4154 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4155 } else {
4156 databuf = NULL;
4159 scsi_read_dvd_structure(&ccb->csio,
4160 /* retries */ 1,
4161 /* cbfcnp */ cddone,
4162 /* tag_action */ MSG_SIMPLE_Q_TAG,
4163 /* lba */ address,
4164 /* layer_number */ dvdstruct->layer_num,
4165 /* key_format */ dvdstruct->format,
4166 /* agid */ dvdstruct->agid,
4167 /* data_ptr */ databuf,
4168 /* dxfer_len */ length,
4169 /* sense_len */ SSD_FULL_SIZE,
4170 /* timeout */ 50000);
4172 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4173 /*sense_flags*/SF_RETRY_UA);
4175 if (error != 0)
4176 goto bailout;
4178 switch(dvdstruct->format) {
4179 case DVD_STRUCT_PHYSICAL: {
4180 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4181 struct dvd_layer *outlayer;
4182 struct scsi_read_dvd_struct_data_physical *phys_data;
4184 phys_data =
4185 (struct scsi_read_dvd_struct_data_physical *)databuf;
4186 inlayer = &phys_data->layer_desc;
4187 outlayer = (struct dvd_layer *)&dvdstruct->data;
4189 dvdstruct->length = sizeof(*inlayer);
4191 outlayer->book_type = (inlayer->book_type_version &
4192 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4193 outlayer->book_version = (inlayer->book_type_version &
4194 RDSD_BOOK_VERSION_MASK);
4195 outlayer->disc_size = (inlayer->disc_size_max_rate &
4196 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4197 outlayer->max_rate = (inlayer->disc_size_max_rate &
4198 RDSD_MAX_RATE_MASK);
4199 outlayer->nlayers = (inlayer->layer_info &
4200 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4201 outlayer->track_path = (inlayer->layer_info &
4202 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4203 outlayer->layer_type = (inlayer->layer_info &
4204 RDSD_LAYER_TYPE_MASK);
4205 outlayer->linear_density = (inlayer->density &
4206 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4207 outlayer->track_density = (inlayer->density &
4208 RDSD_TRACK_DENSITY_MASK);
4209 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4210 RDSD_BCA_SHIFT;
4211 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4212 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4213 outlayer->end_sector_l0 =
4214 scsi_3btoul(inlayer->end_sector_layer0);
4215 break;
4217 case DVD_STRUCT_COPYRIGHT: {
4218 struct scsi_read_dvd_struct_data_copyright *copy_data;
4220 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4221 databuf;
4223 dvdstruct->cpst = copy_data->cps_type;
4224 dvdstruct->rmi = copy_data->region_info;
4225 dvdstruct->length = 0;
4227 break;
4229 default:
4231 * Tell the user what the overall length is, no matter
4232 * what we can actually fit in the data buffer.
4234 dvdstruct->length = length - ccb->csio.resid -
4235 sizeof(struct scsi_read_dvd_struct_data_header);
4238 * But only actually copy out the smaller of what we read
4239 * in or what the structure can take.
4241 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4242 dvdstruct->data,
4243 min(sizeof(dvdstruct->data), dvdstruct->length));
4244 break;
4246 bailout:
4248 if (databuf != NULL)
4249 kfree(databuf, M_DEVBUF);
4251 xpt_release_ccb(ccb);
4253 return(error);
4256 void
4257 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4258 void (*cbfcnp)(struct cam_periph *, union ccb *),
4259 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4260 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4261 u_int8_t sense_len, u_int32_t timeout)
4263 struct scsi_report_key *scsi_cmd;
4265 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4266 bzero(scsi_cmd, sizeof(*scsi_cmd));
4267 scsi_cmd->opcode = REPORT_KEY;
4268 scsi_ulto4b(lba, scsi_cmd->lba);
4269 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4270 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4271 (key_format & RK_KF_KEYFORMAT_MASK);
4273 cam_fill_csio(csio,
4274 retries,
4275 cbfcnp,
4276 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4277 tag_action,
4278 /*data_ptr*/ data_ptr,
4279 /*dxfer_len*/ dxfer_len,
4280 sense_len,
4281 sizeof(*scsi_cmd),
4282 timeout);
4285 void
4286 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4287 void (*cbfcnp)(struct cam_periph *, union ccb *),
4288 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4289 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4290 u_int32_t timeout)
4292 struct scsi_send_key *scsi_cmd;
4294 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4295 bzero(scsi_cmd, sizeof(*scsi_cmd));
4296 scsi_cmd->opcode = SEND_KEY;
4298 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4299 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4300 (key_format & RK_KF_KEYFORMAT_MASK);
4302 cam_fill_csio(csio,
4303 retries,
4304 cbfcnp,
4305 /*flags*/ CAM_DIR_OUT,
4306 tag_action,
4307 /*data_ptr*/ data_ptr,
4308 /*dxfer_len*/ dxfer_len,
4309 sense_len,
4310 sizeof(*scsi_cmd),
4311 timeout);
4315 void
4316 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4317 void (*cbfcnp)(struct cam_periph *, union ccb *),
4318 u_int8_t tag_action, u_int32_t address,
4319 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4320 u_int8_t *data_ptr, u_int32_t dxfer_len,
4321 u_int8_t sense_len, u_int32_t timeout)
4323 struct scsi_read_dvd_structure *scsi_cmd;
4325 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4326 bzero(scsi_cmd, sizeof(*scsi_cmd));
4327 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4329 scsi_ulto4b(address, scsi_cmd->address);
4330 scsi_cmd->layer_number = layer_number;
4331 scsi_cmd->format = format;
4332 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4333 /* The AGID is the top two bits of this byte */
4334 scsi_cmd->agid = agid << 6;
4336 cam_fill_csio(csio,
4337 retries,
4338 cbfcnp,
4339 /*flags*/ CAM_DIR_IN,
4340 tag_action,
4341 /*data_ptr*/ data_ptr,
4342 /*dxfer_len*/ dxfer_len,
4343 sense_len,
4344 sizeof(*scsi_cmd),
4345 timeout);