Sync CAM with FreeBSD using lockmgr locks instead of mutexes.
[dragonfly.git] / sys / bus / cam / scsi / scsi_cd.c
blob5eac85076d17d6c75598c67c706f8e84404c72ed
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.42 2008/05/18 20:30:19 pavalos 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 if (error == 0)
1025 softc->flags |= CD_FLAG_OPEN;
1027 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1028 cam_periph_unhold(periph);
1029 cam_periph_unlock(periph);
1031 return (0);
1034 static int
1035 cdclose(struct dev_close_args *ap)
1037 cdev_t dev = ap->a_head.a_dev;
1038 struct cam_periph *periph;
1039 struct cd_softc *softc;
1040 struct disk_info *info;
1041 int unit;
1043 unit = dkunit(dev);
1044 periph = cam_extend_get(cdperiphs, unit);
1045 if (periph == NULL)
1046 return (ENXIO);
1048 softc = (struct cd_softc *)periph->softc;
1050 cam_periph_lock(periph);
1051 cam_periph_hold(periph, 0);
1053 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
1054 cdprevent(periph, PR_ALLOW);
1057 * Unconditionally set the dsopen() flags back to their default
1058 * state.
1060 info = &softc->disk.d_info;
1061 info->d_dsflags &= ~DSO_NOLABELS;
1062 info->d_dsflags |= DSO_COMPATLABEL;
1065 * Since we're closing this CD, mark the blocksize as unavailable.
1066 * It will be marked as available when the CD is opened again.
1068 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
1071 * We'll check the media and toc again at the next open().
1073 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
1075 cam_periph_unhold(periph);
1076 cam_periph_unlock(periph);
1077 cam_periph_release(periph);
1079 return (0);
1082 static void
1083 cdshorttimeout(void *arg)
1085 struct cdchanger *changer;
1087 changer = (struct cdchanger *)arg;
1089 /* Always clear the short timeout flag, since that's what we're in */
1090 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1093 * Check to see if there is any more pending or outstanding I/O for
1094 * this device. If not, move it out of the active slot.
1096 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1097 && (changer->cur_device->outstanding_cmds == 0)) {
1098 changer->flags |= CHANGER_MANUAL_CALL;
1099 cdrunchangerqueue(changer);
1104 * This is a wrapper for xpt_schedule. It only applies to changers.
1106 static void
1107 cdschedule(struct cam_periph *periph, int priority)
1109 struct cd_softc *softc;
1111 softc = (struct cd_softc *)periph->softc;
1114 * If this device isn't currently queued, and if it isn't
1115 * the active device, then we queue this device and run the
1116 * changer queue if there is no timeout scheduled to do it.
1117 * If this device is the active device, just schedule it
1118 * to run again. If this device is queued, there should be
1119 * a timeout in place already that will make sure it runs.
1121 if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
1122 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
1124 * We don't do anything with the priority here.
1125 * This is strictly a fifo queue.
1127 softc->pinfo.priority = 1;
1128 softc->pinfo.generation = ++softc->changer->devq.generation;
1129 camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
1132 * Since we just put a device in the changer queue,
1133 * check and see if there is a timeout scheduled for
1134 * this changer. If so, let the timeout handle
1135 * switching this device into the active slot. If
1136 * not, manually call the timeout routine to
1137 * bootstrap things.
1139 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1140 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1141 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1142 softc->changer->flags |= CHANGER_MANUAL_CALL;
1143 cdrunchangerqueue(softc->changer);
1145 } else if ((softc->flags & CD_FLAG_ACTIVE)
1146 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
1147 xpt_schedule(periph, priority);
1151 static void
1152 cdrunchangerqueue(void *arg)
1154 struct cd_softc *softc;
1155 struct cdchanger *changer;
1156 int called_from_timeout;
1158 changer = (struct cdchanger *)arg;
1161 * If we have NOT been called from cdstrategy() or cddone(), and
1162 * instead from a timeout routine, go ahead and clear the
1163 * timeout flag.
1165 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1166 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1167 called_from_timeout = 1;
1168 } else
1169 called_from_timeout = 0;
1171 /* Always clear the manual call flag */
1172 changer->flags &= ~CHANGER_MANUAL_CALL;
1174 /* nothing to do if the queue is empty */
1175 if (changer->devq.entries <= 0) {
1176 return;
1180 * If the changer queue is frozen, that means we have an active
1181 * device.
1183 if (changer->devq.qfrozen_cnt > 0) {
1186 * We always need to reset the frozen count and clear the
1187 * active flag.
1189 changer->devq.qfrozen_cnt--;
1190 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1191 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1193 if (changer->cur_device->outstanding_cmds > 0) {
1194 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1195 changer->cur_device->bufs_left =
1196 changer->cur_device->outstanding_cmds;
1197 if (called_from_timeout) {
1198 callout_reset(&changer->long_handle,
1199 changer_max_busy_seconds * hz,
1200 cdrunchangerqueue, changer);
1201 changer->flags |= CHANGER_TIMEOUT_SCHED;
1203 return;
1207 * Check to see whether the current device has any I/O left
1208 * to do. If so, requeue it at the end of the queue. If
1209 * not, there is no need to requeue it.
1211 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1213 changer->cur_device->pinfo.generation =
1214 ++changer->devq.generation;
1215 camq_insert(&changer->devq,
1216 (cam_pinfo *)changer->cur_device);
1220 softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
1222 changer->cur_device = softc;
1224 changer->devq.qfrozen_cnt++;
1225 softc->flags |= CD_FLAG_ACTIVE;
1227 /* Just in case this device is waiting */
1228 wakeup(&softc->changer);
1229 xpt_schedule(softc->periph, /*priority*/ 1);
1232 * Get rid of any pending timeouts, and set a flag to schedule new
1233 * ones so this device gets its full time quantum.
1235 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1236 callout_stop(&changer->long_handle);
1237 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1240 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1241 callout_stop(&changer->short_handle);
1242 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1246 * We need to schedule timeouts, but we only do this after the
1247 * first transaction has completed. This eliminates the changer
1248 * switch time.
1250 changer->flags |= CHANGER_NEED_TIMEOUT;
1253 static void
1254 cdchangerschedule(struct cd_softc *softc)
1256 struct cdchanger *changer;
1258 changer = softc->changer;
1261 * If this is a changer, and this is the current device,
1262 * and this device has at least the minimum time quantum to
1263 * run, see if we can switch it out.
1265 if ((softc->flags & CD_FLAG_ACTIVE)
1266 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
1267 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
1269 * We try three things here. The first is that we
1270 * check to see whether the schedule on completion
1271 * flag is set. If it is, we decrement the number
1272 * of buffers left, and if it's zero, we reschedule.
1273 * Next, we check to see whether the pending buffer
1274 * queue is empty and whether there are no
1275 * outstanding transactions. If so, we reschedule.
1276 * Next, we see if the pending buffer queue is empty.
1277 * If it is, we set the number of buffers left to
1278 * the current active buffer count and set the
1279 * schedule on complete flag.
1281 if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
1282 if (--softc->bufs_left == 0) {
1283 softc->changer->flags |=
1284 CHANGER_MANUAL_CALL;
1285 softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
1286 cdrunchangerqueue(softc->changer);
1288 } else if ((bioq_first(&softc->bio_queue) == NULL)
1289 && (softc->outstanding_cmds == 0)) {
1290 softc->changer->flags |= CHANGER_MANUAL_CALL;
1291 cdrunchangerqueue(softc->changer);
1293 } else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
1294 && (softc->flags & CD_FLAG_ACTIVE)) {
1297 * Now that the first transaction to this
1298 * particular device has completed, we can go ahead
1299 * and schedule our timeouts.
1301 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1302 callout_reset(&changer->long_handle,
1303 changer_max_busy_seconds * hz,
1304 cdrunchangerqueue, changer);
1305 changer->flags |= CHANGER_TIMEOUT_SCHED;
1306 } else
1307 kprintf("cdchangerschedule: already have a long"
1308 " timeout!\n");
1310 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1311 callout_reset(&changer->short_handle,
1312 changer_min_busy_seconds * hz,
1313 cdshorttimeout, changer);
1314 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1315 } else
1316 kprintf("cdchangerschedule: already have a short "
1317 "timeout!\n");
1320 * We just scheduled timeouts, no need to schedule
1321 * more.
1323 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1328 static int
1329 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1330 u_int32_t cam_flags,
1331 u_int32_t sense_flags),
1332 u_int32_t cam_flags, u_int32_t sense_flags)
1334 struct cd_softc *softc;
1335 struct cam_periph *periph;
1336 int error;
1338 periph = xpt_path_periph(ccb->ccb_h.path);
1339 softc = (struct cd_softc *)periph->softc;
1341 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
1342 &softc->device_stats);
1344 if (softc->flags & CD_FLAG_CHANGER)
1345 cdchangerschedule(softc);
1347 return(error);
1350 static union ccb *
1351 cdgetccb(struct cam_periph *periph, u_int32_t priority)
1353 struct cd_softc *softc;
1355 softc = (struct cd_softc *)periph->softc;
1357 if (softc->flags & CD_FLAG_CHANGER) {
1359 * This should work the first time this device is woken up,
1360 * but just in case it doesn't, we use a while loop.
1362 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1364 * If this changer isn't already queued, queue it up.
1366 if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
1367 softc->pinfo.priority = 1;
1368 softc->pinfo.generation =
1369 ++softc->changer->devq.generation;
1370 camq_insert(&softc->changer->devq,
1371 (cam_pinfo *)softc);
1373 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1374 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1375 && ((softc->changer->flags
1376 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1377 softc->changer->flags |= CHANGER_MANUAL_CALL;
1378 cdrunchangerqueue(softc->changer);
1379 } else
1380 sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
1381 periph->sim->lock);
1384 return(cam_periph_getccb(periph, priority));
1388 * Actually translate the requested transfer into one the physical driver
1389 * can understand. The transfer is described by a buf and will include
1390 * only one physical transfer.
1392 static int
1393 cdstrategy(struct dev_strategy_args *ap)
1395 cdev_t dev = ap->a_head.a_dev;
1396 struct bio *bio = ap->a_bio;
1397 struct buf *bp = bio->bio_buf;
1398 struct cam_periph *periph;
1399 struct cd_softc *softc;
1400 u_int unit;
1402 unit = dkunit(dev);
1403 periph = cam_extend_get(cdperiphs, unit);
1404 if (periph == NULL) {
1405 bp->b_error = ENXIO;
1406 goto bad;
1409 cam_periph_lock(periph);
1410 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1412 softc = (struct cd_softc *)periph->softc;
1415 * If the device has been made invalid, error out
1417 if ((softc->flags & CD_FLAG_INVALID)) {
1418 cam_periph_unlock(periph);
1419 bp->b_error = ENXIO;
1420 goto bad;
1424 * If we don't have valid media, look for it before trying to
1425 * schedule the I/O.
1427 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1428 int error;
1430 error = cdcheckmedia(periph);
1431 if (error != 0) {
1432 cam_periph_unlock(periph);
1433 bp->b_error = error;
1434 goto bad;
1439 * Place it in the queue of disk activities for this disk
1441 bioqdisksort(&softc->bio_queue, bio);
1444 * Schedule ourselves for performing the work. We do things
1445 * differently for changers.
1447 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1448 xpt_schedule(periph, /* XXX priority */1);
1449 else
1450 cdschedule(periph, /* priority */ 1);
1452 cam_periph_unlock(periph);
1453 return(0);
1454 bad:
1455 bp->b_flags |= B_ERROR;
1457 * Correctly set the buf to indicate a completed xfer
1459 bp->b_resid = bp->b_bcount;
1460 biodone(bio);
1461 return(0);
1464 static void
1465 cdstart(struct cam_periph *periph, union ccb *start_ccb)
1467 struct cd_softc *softc;
1468 struct bio *bio;
1469 struct buf *bp;
1470 struct ccb_scsiio *csio;
1471 struct scsi_read_capacity_data *rcap;
1473 softc = (struct cd_softc *)periph->softc;
1475 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1477 switch (softc->state) {
1478 case CD_STATE_NORMAL:
1480 bio = bioq_first(&softc->bio_queue);
1481 if (periph->immediate_priority <= periph->pinfo.priority) {
1482 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1484 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1485 periph_links.sle);
1486 periph->immediate_priority = CAM_PRIORITY_NONE;
1487 wakeup(&periph->ccb_list);
1488 } else if (bio == NULL) {
1489 xpt_release_ccb(start_ccb);
1490 } else {
1491 bp = bio->bio_buf;
1492 bioq_remove(&softc->bio_queue, bio);
1494 devstat_start_transaction(&softc->device_stats);
1496 KKASSERT(bio->bio_offset % softc->params.blksize == 0);
1498 scsi_read_write(&start_ccb->csio,
1499 /*retries*/4,
1500 /* cbfcnp */ cddone,
1501 (bp->b_flags & B_ORDERED) != 0 ?
1502 MSG_ORDERED_Q_TAG :
1503 MSG_SIMPLE_Q_TAG,
1504 /* read */(bp->b_cmd == BUF_CMD_READ),
1505 /* byte2 */ 0,
1506 /* minimum_cmd_size */ 10,
1507 /* lba */
1508 bio->bio_offset / softc->params.blksize,
1509 bp->b_bcount / softc->params.blksize,
1510 /* data_ptr */ bp->b_data,
1511 /* dxfer_len */ bp->b_bcount,
1512 /* sense_len */ SSD_FULL_SIZE,
1513 /* timeout */ 30000);
1514 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1517 LIST_INSERT_HEAD(&softc->pending_ccbs,
1518 &start_ccb->ccb_h, periph_links.le);
1520 softc->outstanding_cmds++;
1521 /* We expect a unit attention from this device */
1522 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1523 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1524 softc->flags &= ~CD_FLAG_RETRY_UA;
1527 start_ccb->ccb_h.ccb_bio = bio;
1528 bio = bioq_first(&softc->bio_queue);
1530 xpt_action(start_ccb);
1532 if (bio != NULL) {
1533 /* Have more work to do, so ensure we stay scheduled */
1534 xpt_schedule(periph, /* XXX priority */1);
1536 break;
1538 case CD_STATE_PROBE:
1541 rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
1542 csio = &start_ccb->csio;
1543 scsi_read_capacity(csio,
1544 /*retries*/1,
1545 cddone,
1546 MSG_SIMPLE_Q_TAG,
1547 rcap,
1548 SSD_FULL_SIZE,
1549 /*timeout*/20000);
1550 start_ccb->ccb_h.ccb_bio = NULL;
1551 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
1552 xpt_action(start_ccb);
1553 break;
1558 static void
1559 cddone(struct cam_periph *periph, union ccb *done_ccb)
1561 struct cd_softc *softc;
1562 struct ccb_scsiio *csio;
1564 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
1566 softc = (struct cd_softc *)periph->softc;
1567 csio = &done_ccb->csio;
1569 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1570 case CD_CCB_BUFFER_IO:
1572 struct buf *bp;
1573 struct bio *bio;
1574 int error;
1576 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1577 bp = bio->bio_buf;
1578 error = 0;
1580 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1581 int sf;
1583 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
1584 sf = SF_RETRY_UA;
1585 else
1586 sf = 0;
1588 error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
1589 if (error == ERESTART) {
1591 * A retry was scheuled, so
1592 * just return.
1594 return;
1598 if (error != 0) {
1599 struct bio *q_bio;
1600 struct buf *q_bp;
1602 xpt_print(periph->path,
1603 "cddone: got error %#x back\n", error);
1604 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1605 bioq_remove(&softc->bio_queue, q_bio);
1606 q_bp = q_bio->bio_buf;
1607 q_bp->b_resid = q_bp->b_bcount;
1608 q_bp->b_error = EIO;
1609 q_bp->b_flags |= B_ERROR;
1610 biodone(q_bio);
1612 bp->b_resid = bp->b_bcount;
1613 bp->b_error = error;
1614 bp->b_flags |= B_ERROR;
1615 cam_release_devq(done_ccb->ccb_h.path,
1616 /*relsim_flags*/0,
1617 /*reduction*/0,
1618 /*timeout*/0,
1619 /*getcount_only*/0);
1621 } else {
1622 bp->b_resid = csio->resid;
1623 bp->b_error = 0;
1624 if (bp->b_resid != 0) {
1625 /* Short transfer ??? */
1626 bp->b_flags |= B_ERROR;
1630 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1631 softc->outstanding_cmds--;
1633 if (softc->flags & CD_FLAG_CHANGER)
1634 cdchangerschedule(softc);
1636 devstat_end_transaction_buf(&softc->device_stats, bp);
1637 biodone(bio);
1638 break;
1640 case CD_CCB_PROBE:
1642 struct scsi_read_capacity_data *rdcap;
1643 char announce_buf[120]; /*
1644 * Currently (9/30/97) the
1645 * longest possible announce
1646 * buffer is 108 bytes, for the
1647 * first error case below.
1648 * That is 39 bytes for the
1649 * basic string, 16 bytes for the
1650 * biggest sense key (hardware
1651 * error), 52 bytes for the
1652 * text of the largest sense
1653 * qualifier valid for a CDROM,
1654 * (0x72, 0x03 or 0x04,
1655 * 0x03), and one byte for the
1656 * null terminating character.
1657 * To allow for longer strings,
1658 * the announce buffer is 120
1659 * bytes.
1661 struct cd_params *cdp;
1663 cdp = &softc->params;
1665 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1667 cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
1668 cdp->blksize = scsi_4btoul (rdcap->length);
1670 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1672 ksnprintf(announce_buf, sizeof(announce_buf),
1673 "cd present [%lu x %lu byte records]",
1674 cdp->disksize, (u_long)cdp->blksize);
1676 } else {
1677 int error;
1679 * Retry any UNIT ATTENTION type errors. They
1680 * are expected at boot.
1682 error = cderror(done_ccb, CAM_RETRY_SELTO,
1683 SF_RETRY_UA | SF_NO_PRINT);
1684 if (error == ERESTART) {
1686 * A retry was scheuled, so
1687 * just return.
1689 return;
1690 } else if (error != 0) {
1692 struct scsi_sense_data *sense;
1693 int asc, ascq;
1694 int sense_key, error_code;
1695 int have_sense;
1696 cam_status status;
1697 struct ccb_getdev cgd;
1699 /* Don't wedge this device's queue */
1700 cam_release_devq(done_ccb->ccb_h.path,
1701 /*relsim_flags*/0,
1702 /*reduction*/0,
1703 /*timeout*/0,
1704 /*getcount_only*/0);
1706 status = done_ccb->ccb_h.status;
1708 xpt_setup_ccb(&cgd.ccb_h,
1709 done_ccb->ccb_h.path,
1710 /* priority */ 1);
1711 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1712 xpt_action((union ccb *)&cgd);
1714 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1715 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1716 || ((status & CAM_AUTOSNS_VALID) == 0))
1717 have_sense = FALSE;
1718 else
1719 have_sense = TRUE;
1721 if (have_sense) {
1722 sense = &csio->sense_data;
1723 scsi_extract_sense(sense, &error_code,
1724 &sense_key,
1725 &asc, &ascq);
1728 * Attach to anything that claims to be a
1729 * CDROM or WORM device, as long as it
1730 * doesn't return a "Logical unit not
1731 * supported" (0x25) error.
1733 if ((have_sense) && (asc != 0x25)
1734 && (error_code == SSD_CURRENT_ERROR)) {
1735 const char *sense_key_desc;
1736 const char *asc_desc;
1738 scsi_sense_desc(sense_key, asc, ascq,
1739 &cgd.inq_data,
1740 &sense_key_desc,
1741 &asc_desc);
1742 ksnprintf(announce_buf,
1743 sizeof(announce_buf),
1744 "Attempt to query device "
1745 "size failed: %s, %s",
1746 sense_key_desc,
1747 asc_desc);
1748 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) {
1750 * We only print out an error for
1751 * CDROM type devices. For WORM
1752 * devices, we don't print out an
1753 * error since a few WORM devices
1754 * don't support CDROM commands.
1755 * If we have sense information, go
1756 * ahead and print it out.
1757 * Otherwise, just say that we
1758 * couldn't attach.
1762 * Just print out the error, not
1763 * the full probe message, when we
1764 * don't attach.
1766 if (have_sense)
1767 scsi_sense_print(
1768 &done_ccb->csio);
1769 else {
1770 xpt_print(periph->path,
1771 "got CAM status %#x\n",
1772 done_ccb->ccb_h.status);
1774 xpt_print(periph->path, "fatal error, "
1775 "failed to attach to device\n");
1777 * Invalidate this peripheral.
1779 cam_periph_invalidate(periph);
1781 announce_buf[0] = '\0';
1782 } else {
1785 * Invalidate this peripheral.
1787 cam_periph_invalidate(periph);
1788 announce_buf[0] = '\0';
1792 kfree(rdcap, M_SCSICD);
1793 if (announce_buf[0] != '\0') {
1794 xpt_announce_periph(periph, announce_buf);
1795 if (softc->flags & CD_FLAG_CHANGER)
1796 cdchangerschedule(softc);
1798 * Create our sysctl variables, now that we know
1799 * we have successfully attached.
1801 taskqueue_enqueue(taskqueue_thread[mycpuid],
1802 &softc->sysctl_task);
1804 softc->state = CD_STATE_NORMAL;
1806 * Since our peripheral may be invalidated by an error
1807 * above or an external event, we must release our CCB
1808 * before releasing the probe lock on the peripheral.
1809 * The peripheral will only go away once the last lock
1810 * is removed, and we need it around for the CCB release
1811 * operation.
1813 xpt_release_ccb(done_ccb);
1814 cam_periph_unhold(periph);
1815 return;
1817 case CD_CCB_WAITING:
1819 /* Caller will release the CCB */
1820 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1821 ("trying to wakeup ccbwait\n"));
1823 wakeup(&done_ccb->ccb_h.cbfcnp);
1824 return;
1826 default:
1827 break;
1829 xpt_release_ccb(done_ccb);
1832 static union cd_pages *
1833 cdgetpage(struct cd_mode_params *mode_params)
1835 union cd_pages *page;
1837 if (mode_params->cdb_size == 10)
1838 page = (union cd_pages *)find_mode_page_10(
1839 (struct scsi_mode_header_10 *)mode_params->mode_buf);
1840 else
1841 page = (union cd_pages *)find_mode_page_6(
1842 (struct scsi_mode_header_6 *)mode_params->mode_buf);
1844 return (page);
1847 static int
1848 cdgetpagesize(int page_num)
1850 int i;
1852 for (i = 0; i < (sizeof(cd_page_size_table)/
1853 sizeof(cd_page_size_table[0])); i++) {
1854 if (cd_page_size_table[i].page == page_num)
1855 return (cd_page_size_table[i].page_size);
1857 return (-1);
1860 static int
1861 cdioctl(struct dev_ioctl_args *ap)
1863 cdev_t dev = ap->a_head.a_dev;
1864 caddr_t addr = ap->a_data;
1865 struct cam_periph *periph;
1866 struct cd_softc *softc;
1867 int unit, error = 0;
1869 unit = dkunit(dev);
1871 periph = cam_extend_get(cdperiphs, unit);
1872 if (periph == NULL)
1873 return(ENXIO);
1875 cam_periph_lock(periph);
1876 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1878 softc = (struct cd_softc *)periph->softc;
1880 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1881 ("trying to do ioctl %#lx\n", ap->a_cmd));
1883 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
1884 cam_periph_unlock(periph);
1885 cam_periph_release(periph);
1886 return (error);
1890 * If we don't have media loaded, check for it. If still don't
1891 * have media loaded, we can only do a load or eject.
1893 * We only care whether media is loaded if this is a cd-specific ioctl
1894 * (thus the IOCGROUP check below). Note that this will break if
1895 * anyone adds any ioctls into the switch statement below that don't
1896 * have their ioctl group set to 'c'.
1898 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1899 && ((ap->a_cmd != CDIOCCLOSE)
1900 && (ap->a_cmd != CDIOCEJECT))
1901 && (IOCGROUP(ap->a_cmd) == 'c')) {
1902 error = cdcheckmedia(periph);
1903 if (error != 0) {
1904 cam_periph_unhold(periph);
1905 cam_periph_unlock(periph);
1906 return (error);
1910 * Drop the lock here so later mallocs can use WAITOK. The periph
1911 * is essentially locked still with the cam_periph_hold call above.
1913 cam_periph_unlock(periph);
1915 switch (ap->a_cmd) {
1917 case CDIOCPLAYTRACKS:
1919 struct ioc_play_track *args
1920 = (struct ioc_play_track *)addr;
1921 struct cd_mode_params params;
1922 union cd_pages *page;
1924 params.alloc_len = sizeof(union cd_mode_data_6_10);
1925 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
1926 M_WAITOK | M_ZERO);
1928 cam_periph_lock(periph);
1929 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1930 ("trying to do CDIOCPLAYTRACKS\n"));
1932 error = cdgetmode(periph, &params, AUDIO_PAGE);
1933 if (error) {
1934 kfree(params.mode_buf, M_SCSICD);
1935 cam_periph_unlock(periph);
1936 break;
1938 page = cdgetpage(&params);
1940 page->audio.flags &= ~CD_PA_SOTC;
1941 page->audio.flags |= CD_PA_IMMED;
1942 error = cdsetmode(periph, &params);
1943 kfree(params.mode_buf, M_SCSICD);
1944 if (error) {
1945 cam_periph_unlock(periph);
1946 break;
1950 * This was originally implemented with the PLAY
1951 * AUDIO TRACK INDEX command, but that command was
1952 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1953 * drives support it but ATAPI and ATAPI-derivative
1954 * drives don't seem to support it. So we keep a
1955 * cache of the table of contents and translate
1956 * track numbers to MSF format.
1958 if (softc->flags & CD_FLAG_VALID_TOC) {
1959 union msf_lba *sentry, *eentry;
1960 int st, et;
1962 if (args->end_track <
1963 softc->toc.header.ending_track + 1)
1964 args->end_track++;
1965 if (args->end_track >
1966 softc->toc.header.ending_track + 1)
1967 args->end_track =
1968 softc->toc.header.ending_track + 1;
1969 st = args->start_track -
1970 softc->toc.header.starting_track;
1971 et = args->end_track -
1972 softc->toc.header.starting_track;
1973 if ((st < 0)
1974 || (et < 0)
1975 || (st > (softc->toc.header.ending_track -
1976 softc->toc.header.starting_track))) {
1977 error = EINVAL;
1978 break;
1980 sentry = &softc->toc.entries[st].addr;
1981 eentry = &softc->toc.entries[et].addr;
1982 error = cdplaymsf(periph,
1983 sentry->msf.minute,
1984 sentry->msf.second,
1985 sentry->msf.frame,
1986 eentry->msf.minute,
1987 eentry->msf.second,
1988 eentry->msf.frame);
1989 } else {
1991 * If we don't have a valid TOC, try the
1992 * play track index command. It is part of
1993 * the SCSI-2 spec, but was removed in the
1994 * MMC specs. ATAPI and ATAPI-derived
1995 * drives don't support it.
1997 if (softc->quirks & CD_Q_BCD_TRACKS) {
1998 args->start_track =
1999 bin2bcd(args->start_track);
2000 args->end_track =
2001 bin2bcd(args->end_track);
2003 error = cdplaytracks(periph,
2004 args->start_track,
2005 args->start_index,
2006 args->end_track,
2007 args->end_index);
2009 cam_periph_unlock(periph);
2011 break;
2012 case CDIOCPLAYMSF:
2014 struct ioc_play_msf *args
2015 = (struct ioc_play_msf *) addr;
2016 struct cd_mode_params params;
2017 union cd_pages *page;
2019 params.alloc_len = sizeof(union cd_mode_data_6_10);
2020 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2021 M_WAITOK | M_ZERO);
2023 cam_periph_lock(periph);
2024 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2025 ("trying to do CDIOCPLAYMSF\n"));
2027 error = cdgetmode(periph, &params, AUDIO_PAGE);
2028 if (error) {
2029 kfree(params.mode_buf, M_SCSICD);
2030 cam_periph_unlock(periph);
2031 break;
2033 page = cdgetpage(&params);
2035 page->audio.flags &= ~CD_PA_SOTC;
2036 page->audio.flags |= CD_PA_IMMED;
2037 error = cdsetmode(periph, &params);
2038 kfree(params.mode_buf, M_SCSICD);
2039 if (error) {
2040 cam_periph_unlock(periph);
2041 break;
2043 error = cdplaymsf(periph,
2044 args->start_m,
2045 args->start_s,
2046 args->start_f,
2047 args->end_m,
2048 args->end_s,
2049 args->end_f);
2050 cam_periph_unlock(periph);
2052 break;
2053 case CDIOCPLAYBLOCKS:
2055 struct ioc_play_blocks *args
2056 = (struct ioc_play_blocks *) addr;
2057 struct cd_mode_params params;
2058 union cd_pages *page;
2060 params.alloc_len = sizeof(union cd_mode_data_6_10);
2061 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2062 M_WAITOK | M_ZERO);
2063 cam_periph_lock(periph);
2064 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2065 ("trying to do CDIOCPLAYBLOCKS\n"));
2068 error = cdgetmode(periph, &params, AUDIO_PAGE);
2069 if (error) {
2070 kfree(params.mode_buf, M_SCSICD);
2071 cam_periph_unlock(periph);
2072 break;
2074 page = cdgetpage(&params);
2076 page->audio.flags &= ~CD_PA_SOTC;
2077 page->audio.flags |= CD_PA_IMMED;
2078 error = cdsetmode(periph, &params);
2079 kfree(params.mode_buf, M_SCSICD);
2080 if (error) {
2081 cam_periph_unlock(periph);
2082 break;
2084 error = cdplay(periph, args->blk, args->len);
2085 cam_periph_unlock(periph);
2087 break;
2088 case CDIOCREADSUBCHANNEL:
2090 struct ioc_read_subchannel *args
2091 = (struct ioc_read_subchannel *) addr;
2092 struct cd_sub_channel_info *data;
2093 u_int32_t len = args->data_len;
2095 data = kmalloc(sizeof(struct cd_sub_channel_info),
2096 M_SCSICD, M_WAITOK);
2098 cam_periph_lock(periph);
2099 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2100 ("trying to do CDIOCREADSUBCHANNEL\n"));
2102 if ((len > sizeof(struct cd_sub_channel_info)) ||
2103 (len < sizeof(struct cd_sub_channel_header))) {
2104 kprintf(
2105 "scsi_cd: cdioctl: "
2106 "cdioreadsubchannel: error, len=%d\n",
2107 len);
2108 error = EINVAL;
2109 kfree(data, M_SCSICD);
2110 cam_periph_unlock(periph);
2111 break;
2114 if (softc->quirks & CD_Q_BCD_TRACKS)
2115 args->track = bin2bcd(args->track);
2117 error = cdreadsubchannel(periph, args->address_format,
2118 args->data_format, args->track, data, len);
2120 if (error) {
2121 kfree(data, M_SCSICD);
2122 cam_periph_unlock(periph);
2123 break;
2125 if (softc->quirks & CD_Q_BCD_TRACKS)
2126 data->what.track_info.track_number =
2127 bcd2bin(data->what.track_info.track_number);
2128 len = min(len, ((data->header.data_len[0] << 8) +
2129 data->header.data_len[1] +
2130 sizeof(struct cd_sub_channel_header)));
2131 cam_periph_unlock(periph);
2132 if (copyout(data, args->data, len) != 0) {
2133 error = EFAULT;
2135 kfree(data, M_SCSICD);
2137 break;
2139 case CDIOREADTOCHEADER:
2141 struct ioc_toc_header *th;
2143 th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
2144 M_WAITOK);
2146 cam_periph_lock(periph);
2147 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2148 ("trying to do CDIOREADTOCHEADER\n"));
2150 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2151 sizeof (*th), /*sense_flags*/0);
2152 if (error) {
2153 kfree(th, M_SCSICD);
2154 cam_periph_unlock(periph);
2155 break;
2157 if (softc->quirks & CD_Q_BCD_TRACKS) {
2158 /* we are going to have to convert the BCD
2159 * encoding on the cd to what is expected
2161 th->starting_track =
2162 bcd2bin(th->starting_track);
2163 th->ending_track = bcd2bin(th->ending_track);
2165 th->len = ntohs(th->len);
2166 bcopy(th, addr, sizeof(*th));
2167 kfree(th, M_SCSICD);
2168 cam_periph_unlock(periph);
2170 break;
2171 case CDIOREADTOCENTRYS:
2173 struct cd_tocdata *data;
2174 struct cd_toc_single *lead;
2175 struct ioc_read_toc_entry *te =
2176 (struct ioc_read_toc_entry *) addr;
2177 struct ioc_toc_header *th;
2178 u_int32_t len, readlen, idx, num;
2179 u_int32_t starting_track = te->starting_track;
2181 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2182 lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
2184 cam_periph_lock(periph);
2185 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2186 ("trying to do CDIOREADTOCENTRYS\n"));
2188 if (te->data_len < sizeof(struct cd_toc_entry)
2189 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2190 || (te->address_format != CD_MSF_FORMAT
2191 && te->address_format != CD_LBA_FORMAT)) {
2192 error = EINVAL;
2193 kprintf("scsi_cd: error in readtocentries, "
2194 "returning EINVAL\n");
2195 kfree(data, M_SCSICD);
2196 kfree(lead, M_SCSICD);
2197 cam_periph_unlock(periph);
2198 break;
2201 th = &data->header;
2202 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2203 sizeof (*th), /*sense_flags*/0);
2204 if (error) {
2205 kfree(data, M_SCSICD);
2206 kfree(lead, M_SCSICD);
2207 cam_periph_unlock(periph);
2208 break;
2211 if (softc->quirks & CD_Q_BCD_TRACKS) {
2212 /* we are going to have to convert the BCD
2213 * encoding on the cd to what is expected
2215 th->starting_track =
2216 bcd2bin(th->starting_track);
2217 th->ending_track = bcd2bin(th->ending_track);
2220 if (starting_track == 0)
2221 starting_track = th->starting_track;
2222 else if (starting_track == LEADOUT)
2223 starting_track = th->ending_track + 1;
2224 else if (starting_track < th->starting_track ||
2225 starting_track > th->ending_track + 1) {
2226 kprintf("scsi_cd: error in readtocentries, "
2227 "returning EINVAL\n");
2228 kfree(data, M_SCSICD);
2229 kfree(lead, M_SCSICD);
2230 cam_periph_unlock(periph);
2231 error = EINVAL;
2232 break;
2235 /* calculate reading length without leadout entry */
2236 readlen = (th->ending_track - starting_track + 1) *
2237 sizeof(struct cd_toc_entry);
2239 /* and with leadout entry */
2240 len = readlen + sizeof(struct cd_toc_entry);
2241 if (te->data_len < len) {
2242 len = te->data_len;
2243 if (readlen > len)
2244 readlen = len;
2246 if (len > sizeof(data->entries)) {
2247 kprintf("scsi_cd: error in readtocentries, "
2248 "returning EINVAL\n");
2249 error = EINVAL;
2250 kfree(data, M_SCSICD);
2251 kfree(lead, M_SCSICD);
2252 cam_periph_unlock(periph);
2253 break;
2255 num = len / sizeof(struct cd_toc_entry);
2257 if (readlen > 0) {
2258 error = cdreadtoc(periph, te->address_format,
2259 starting_track,
2260 (u_int8_t *)data,
2261 readlen + sizeof (*th),
2262 /*sense_flags*/0);
2263 if (error) {
2264 kfree(data, M_SCSICD);
2265 kfree(lead, M_SCSICD);
2266 cam_periph_unlock(periph);
2267 break;
2271 /* make leadout entry if needed */
2272 idx = starting_track + num - 1;
2273 if (softc->quirks & CD_Q_BCD_TRACKS)
2274 th->ending_track = bcd2bin(th->ending_track);
2275 if (idx == th->ending_track + 1) {
2276 error = cdreadtoc(periph, te->address_format,
2277 LEADOUT, (u_int8_t *)lead,
2278 sizeof(*lead),
2279 /*sense_flags*/0);
2280 if (error) {
2281 kfree(data, M_SCSICD);
2282 kfree(lead, M_SCSICD);
2283 cam_periph_unlock(periph);
2284 break;
2286 data->entries[idx - starting_track] =
2287 lead->entry;
2289 if (softc->quirks & CD_Q_BCD_TRACKS) {
2290 for (idx = 0; idx < num - 1; idx++) {
2291 data->entries[idx].track =
2292 bcd2bin(data->entries[idx].track);
2296 cam_periph_unlock(periph);
2297 error = copyout(data->entries, te->data, len);
2298 kfree(data, M_SCSICD);
2299 kfree(lead, M_SCSICD);
2301 break;
2302 case CDIOREADTOCENTRY:
2304 struct cd_toc_single *data;
2305 struct ioc_read_toc_single_entry *te =
2306 (struct ioc_read_toc_single_entry *) addr;
2307 struct ioc_toc_header *th;
2308 u_int32_t track;
2310 data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
2312 cam_periph_lock(periph);
2313 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2314 ("trying to do CDIOREADTOCENTRY\n"));
2316 if (te->address_format != CD_MSF_FORMAT
2317 && te->address_format != CD_LBA_FORMAT) {
2318 kprintf("error in readtocentry, "
2319 " returning EINVAL\n");
2320 kfree(data, M_SCSICD);
2321 error = EINVAL;
2322 cam_periph_unlock(periph);
2323 break;
2326 th = &data->header;
2327 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2328 sizeof (*th), /*sense_flags*/0);
2329 if (error) {
2330 kfree(data, M_SCSICD);
2331 cam_periph_unlock(periph);
2332 break;
2335 if (softc->quirks & CD_Q_BCD_TRACKS) {
2336 /* we are going to have to convert the BCD
2337 * encoding on the cd to what is expected
2339 th->starting_track =
2340 bcd2bin(th->starting_track);
2341 th->ending_track = bcd2bin(th->ending_track);
2343 track = te->track;
2344 if (track == 0)
2345 track = th->starting_track;
2346 else if (track == LEADOUT)
2347 /* OK */;
2348 else if (track < th->starting_track ||
2349 track > th->ending_track + 1) {
2350 kprintf("error in readtocentry, "
2351 " returning EINVAL\n");
2352 kfree(data, M_SCSICD);
2353 error = EINVAL;
2354 cam_periph_unlock(periph);
2355 break;
2358 error = cdreadtoc(periph, te->address_format, track,
2359 (u_int8_t *)data, sizeof(*data),
2360 /*sense_flags*/0);
2361 if (error) {
2362 kfree(data, M_SCSICD);
2363 cam_periph_unlock(periph);
2364 break;
2367 if (softc->quirks & CD_Q_BCD_TRACKS)
2368 data->entry.track = bcd2bin(data->entry.track);
2369 bcopy(&data->entry, &te->entry,
2370 sizeof(struct cd_toc_entry));
2371 kfree(data, M_SCSICD);
2372 cam_periph_unlock(periph);
2374 break;
2375 case CDIOCSETPATCH:
2377 struct ioc_patch *arg = (struct ioc_patch *)addr;
2378 struct cd_mode_params params;
2379 union cd_pages *page;
2381 params.alloc_len = sizeof(union cd_mode_data_6_10);
2382 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2383 M_WAITOK | M_ZERO);
2385 cam_periph_lock(periph);
2386 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2387 ("trying to do CDIOCSETPATCH\n"));
2389 error = cdgetmode(periph, &params, AUDIO_PAGE);
2390 if (error) {
2391 kfree(params.mode_buf, M_SCSICD);
2392 cam_periph_unlock(periph);
2393 break;
2395 page = cdgetpage(&params);
2397 page->audio.port[LEFT_PORT].channels =
2398 arg->patch[0];
2399 page->audio.port[RIGHT_PORT].channels =
2400 arg->patch[1];
2401 page->audio.port[2].channels = arg->patch[2];
2402 page->audio.port[3].channels = arg->patch[3];
2403 error = cdsetmode(periph, &params);
2404 kfree(params.mode_buf, M_SCSICD);
2405 cam_periph_unlock(periph);
2407 break;
2408 case CDIOCGETVOL:
2410 struct ioc_vol *arg = (struct ioc_vol *) addr;
2411 struct cd_mode_params params;
2412 union cd_pages *page;
2414 params.alloc_len = sizeof(union cd_mode_data_6_10);
2415 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2416 M_WAITOK | M_ZERO);
2418 cam_periph_lock(periph);
2419 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2420 ("trying to do CDIOCGETVOL\n"));
2422 error = cdgetmode(periph, &params, AUDIO_PAGE);
2423 if (error) {
2424 kfree(params.mode_buf, M_SCSICD);
2425 cam_periph_unlock(periph);
2426 break;
2428 page = cdgetpage(&params);
2430 arg->vol[LEFT_PORT] =
2431 page->audio.port[LEFT_PORT].volume;
2432 arg->vol[RIGHT_PORT] =
2433 page->audio.port[RIGHT_PORT].volume;
2434 arg->vol[2] = page->audio.port[2].volume;
2435 arg->vol[3] = page->audio.port[3].volume;
2436 kfree(params.mode_buf, M_SCSICD);
2437 cam_periph_unlock(periph);
2439 break;
2440 case CDIOCSETVOL:
2442 struct ioc_vol *arg = (struct ioc_vol *) addr;
2443 struct cd_mode_params params;
2444 union cd_pages *page;
2446 params.alloc_len = sizeof(union cd_mode_data_6_10);
2447 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2448 M_WAITOK | M_ZERO);
2450 cam_periph_lock(periph);
2451 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2452 ("trying to do CDIOCSETVOL\n"));
2454 error = cdgetmode(periph, &params, AUDIO_PAGE);
2455 if (error) {
2456 kfree(params.mode_buf, M_SCSICD);
2457 cam_periph_unlock(periph);
2458 break;
2460 page = cdgetpage(&params);
2462 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2463 page->audio.port[LEFT_PORT].volume =
2464 arg->vol[LEFT_PORT];
2465 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2466 page->audio.port[RIGHT_PORT].volume =
2467 arg->vol[RIGHT_PORT];
2468 page->audio.port[2].volume = arg->vol[2];
2469 page->audio.port[3].volume = arg->vol[3];
2470 error = cdsetmode(periph, &params);
2471 cam_periph_unlock(periph);
2472 kfree(params.mode_buf, M_SCSICD);
2474 break;
2475 case CDIOCSETMONO:
2477 struct cd_mode_params params;
2478 union cd_pages *page;
2480 params.alloc_len = sizeof(union cd_mode_data_6_10);
2481 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2482 M_WAITOK | M_ZERO);
2484 cam_periph_lock(periph);
2485 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2486 ("trying to do CDIOCSETMONO\n"));
2488 error = cdgetmode(periph, &params, AUDIO_PAGE);
2489 if (error) {
2490 kfree(params.mode_buf, M_SCSICD);
2491 cam_periph_unlock(periph);
2492 break;
2494 page = cdgetpage(&params);
2496 page->audio.port[LEFT_PORT].channels =
2497 LEFT_CHANNEL | RIGHT_CHANNEL;
2498 page->audio.port[RIGHT_PORT].channels =
2499 LEFT_CHANNEL | RIGHT_CHANNEL;
2500 page->audio.port[2].channels = 0;
2501 page->audio.port[3].channels = 0;
2502 error = cdsetmode(periph, &params);
2503 cam_periph_unlock(periph);
2504 kfree(params.mode_buf, M_SCSICD);
2506 break;
2507 case CDIOCSETSTEREO:
2509 struct cd_mode_params params;
2510 union cd_pages *page;
2512 params.alloc_len = sizeof(union cd_mode_data_6_10);
2513 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2514 M_WAITOK | M_ZERO);
2516 cam_periph_lock(periph);
2517 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2518 ("trying to do CDIOCSETSTEREO\n"));
2520 error = cdgetmode(periph, &params, AUDIO_PAGE);
2521 if (error) {
2522 kfree(params.mode_buf, M_SCSICD);
2523 cam_periph_unlock(periph);
2524 break;
2526 page = cdgetpage(&params);
2528 page->audio.port[LEFT_PORT].channels =
2529 LEFT_CHANNEL;
2530 page->audio.port[RIGHT_PORT].channels =
2531 RIGHT_CHANNEL;
2532 page->audio.port[2].channels = 0;
2533 page->audio.port[3].channels = 0;
2534 error = cdsetmode(periph, &params);
2535 kfree(params.mode_buf, M_SCSICD);
2536 cam_periph_unlock(periph);
2538 break;
2539 case CDIOCSETMUTE:
2541 struct cd_mode_params params;
2542 union cd_pages *page;
2544 params.alloc_len = sizeof(union cd_mode_data_6_10);
2545 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2546 M_WAITOK | M_ZERO);
2548 cam_periph_lock(periph);
2549 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2550 ("trying to do CDIOCSETMUTE\n"));
2552 error = cdgetmode(periph, &params, AUDIO_PAGE);
2553 if (error) {
2554 kfree(&params, M_SCSICD);
2555 cam_periph_unlock(periph);
2556 break;
2558 page = cdgetpage(&params);
2560 page->audio.port[LEFT_PORT].channels = 0;
2561 page->audio.port[RIGHT_PORT].channels = 0;
2562 page->audio.port[2].channels = 0;
2563 page->audio.port[3].channels = 0;
2564 error = cdsetmode(periph, &params);
2565 kfree(params.mode_buf, M_SCSICD);
2566 cam_periph_unlock(periph);
2568 break;
2569 case CDIOCSETLEFT:
2571 struct cd_mode_params params;
2572 union cd_pages *page;
2574 params.alloc_len = sizeof(union cd_mode_data_6_10);
2575 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2576 M_WAITOK | M_ZERO);
2578 cam_periph_lock(periph);
2579 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2580 ("trying to do CDIOCSETLEFT\n"));
2582 error = cdgetmode(periph, &params, AUDIO_PAGE);
2583 if (error) {
2584 kfree(params.mode_buf, M_SCSICD);
2585 cam_periph_unlock(periph);
2586 break;
2588 page = cdgetpage(&params);
2590 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2591 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2592 page->audio.port[2].channels = 0;
2593 page->audio.port[3].channels = 0;
2594 error = cdsetmode(periph, &params);
2595 kfree(params.mode_buf, M_SCSICD);
2596 cam_periph_unlock(periph);
2598 break;
2599 case CDIOCSETRIGHT:
2601 struct cd_mode_params params;
2602 union cd_pages *page;
2604 params.alloc_len = sizeof(union cd_mode_data_6_10);
2605 params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
2606 M_WAITOK | M_ZERO);
2608 cam_periph_lock(periph);
2609 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2610 ("trying to do CDIOCSETRIGHT\n"));
2612 error = cdgetmode(periph, &params, AUDIO_PAGE);
2613 if (error) {
2614 kfree(params.mode_buf, M_SCSICD);
2615 cam_periph_unlock(periph);
2616 break;
2618 page = cdgetpage(&params);
2620 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2621 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2622 page->audio.port[2].channels = 0;
2623 page->audio.port[3].channels = 0;
2624 error = cdsetmode(periph, &params);
2625 kfree(params.mode_buf, M_SCSICD);
2626 cam_periph_unlock(periph);
2628 break;
2629 case CDIOCRESUME:
2630 cam_periph_lock(periph);
2631 error = cdpause(periph, 1);
2632 cam_periph_unlock(periph);
2633 break;
2634 case CDIOCPAUSE:
2635 cam_periph_lock(periph);
2636 error = cdpause(periph, 0);
2637 cam_periph_unlock(periph);
2638 break;
2639 case CDIOCSTART:
2640 cam_periph_lock(periph);
2641 error = cdstartunit(periph, 0);
2642 cam_periph_unlock(periph);
2643 break;
2644 case CDIOCCLOSE:
2645 cam_periph_lock(periph);
2646 error = cdstartunit(periph, 1);
2647 cam_periph_unlock(periph);
2648 break;
2649 case CDIOCSTOP:
2650 cam_periph_lock(periph);
2651 error = cdstopunit(periph, 0);
2652 cam_periph_unlock(periph);
2653 break;
2654 case CDIOCEJECT:
2655 cam_periph_lock(periph);
2656 error = cdstopunit(periph, 1);
2657 cam_periph_unlock(periph);
2658 break;
2659 case CDIOCALLOW:
2660 cam_periph_lock(periph);
2661 cdprevent(periph, PR_ALLOW);
2662 cam_periph_unlock(periph);
2663 break;
2664 case CDIOCPREVENT:
2665 cam_periph_lock(periph);
2666 cdprevent(periph, PR_PREVENT);
2667 cam_periph_unlock(periph);
2668 break;
2669 case CDIOCSETDEBUG:
2670 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2671 error = ENOTTY;
2672 break;
2673 case CDIOCCLRDEBUG:
2674 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2675 error = ENOTTY;
2676 break;
2677 case CDIOCRESET:
2678 /* return (cd_reset(periph)); */
2679 error = ENOTTY;
2680 break;
2681 case CDRIOCREADSPEED:
2682 cam_periph_lock(periph);
2683 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2684 cam_periph_unlock(periph);
2685 break;
2686 case CDRIOCWRITESPEED:
2687 cam_periph_lock(periph);
2688 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2689 cam_periph_unlock(periph);
2690 break;
2691 case DVDIOCSENDKEY:
2692 case DVDIOCREPORTKEY: {
2693 struct dvd_authinfo *authinfo;
2695 authinfo = (struct dvd_authinfo *)addr;
2697 cam_periph_lock(periph);
2698 if (ap->a_cmd == DVDIOCREPORTKEY)
2699 error = cdreportkey(periph, authinfo);
2700 else
2701 error = cdsendkey(periph, authinfo);
2702 cam_periph_unlock(periph);
2703 break;
2705 case DVDIOCREADSTRUCTURE: {
2706 struct dvd_struct *dvdstruct;
2708 dvdstruct = (struct dvd_struct *)addr;
2710 cam_periph_lock(periph);
2711 error = cdreaddvdstructure(periph, dvdstruct);
2712 cam_periph_unlock(periph);
2714 break;
2716 default:
2717 cam_periph_lock(periph);
2718 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
2719 cam_periph_unlock(periph);
2720 break;
2723 cam_periph_lock(periph);
2724 cam_periph_unhold(periph);
2726 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2728 cam_periph_unlock(periph);
2730 return (error);
2733 static void
2734 cdprevent(struct cam_periph *periph, int action)
2736 union ccb *ccb;
2737 struct cd_softc *softc;
2738 int error;
2740 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
2742 softc = (struct cd_softc *)periph->softc;
2744 if (((action == PR_ALLOW)
2745 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
2746 || ((action == PR_PREVENT)
2747 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
2748 return;
2751 ccb = cdgetccb(periph, /* priority */ 1);
2753 scsi_prevent(&ccb->csio,
2754 /*retries*/ 1,
2755 cddone,
2756 MSG_SIMPLE_Q_TAG,
2757 action,
2758 SSD_FULL_SIZE,
2759 /* timeout */60000);
2761 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
2762 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
2764 xpt_release_ccb(ccb);
2766 if (error == 0) {
2767 if (action == PR_ALLOW)
2768 softc->flags &= ~CD_FLAG_DISC_LOCKED;
2769 else
2770 softc->flags |= CD_FLAG_DISC_LOCKED;
2775 cdcheckmedia(struct cam_periph *periph)
2777 struct cd_softc *softc;
2778 struct ioc_toc_header *toch;
2779 struct cd_toc_single leadout;
2780 struct ccb_getdev cgd;
2781 u_int32_t size, toclen;
2782 int error, num_entries, cdindex;
2783 int first_track_audio;
2784 struct disk_info info;
2786 softc = (struct cd_softc *)periph->softc;
2788 first_track_audio = -1;
2789 error = 0;
2791 cdprevent(periph, PR_PREVENT);
2794 * Set up disk info fields and tell the disk subsystem to reset
2795 * its internal copy of the label.
2797 bzero(&info, sizeof(info));
2798 info.d_type = DTYPE_SCSI;
2799 info.d_dsflags &= ~DSO_COMPATLABEL;
2800 info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
2803 * Grab the inquiry data to get the vendor and product names.
2804 * Put them in the typename and packname for the label.
2806 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
2807 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2808 xpt_action((union ccb *)&cgd);
2810 #if 0
2811 strncpy(label->d_typename, cgd.inq_data.vendor,
2812 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
2813 strncpy(label->d_packname, cgd.inq_data.product,
2814 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
2815 #endif
2817 * Clear the valid media and TOC flags until we've verified that we
2818 * have both.
2820 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
2823 * Get the disc size and block size. If we can't get it, we don't
2824 * have media, most likely.
2826 if ((error = cdsize(periph, &size)) != 0) {
2828 * Set a bogus sector size, so the slice code won't try to
2829 * divide by 0 and panic the kernel.
2831 info.d_media_blksize = 2048;
2832 disk_setdiskinfo(&softc->disk, &info);
2835 * XXX KDM is this a good idea? Seems to cause more
2836 * problems.
2838 if (softc->flags & CD_FLAG_OPEN) {
2839 int force;
2841 force = 1;
2844 * We don't bother checking the return value here,
2845 * since we already have an error...
2847 dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2848 /*data*/(caddr_t)&force, /*flags*/ 0,
2849 &softc->disk.d_slice, &softc->disk.d_info);
2853 * Tell devstat(9) we don't have a blocksize.
2855 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
2857 cdprevent(periph, PR_ALLOW);
2859 return (error);
2860 } else {
2861 info.d_media_blksize = softc->params.blksize;
2862 info.d_media_blocks = softc->params.disksize;
2863 disk_setdiskinfo(&softc->disk, &info);
2866 * Force a re-sync of slice information, like the blocksize,
2867 * now that we know it. It isn't pretty...but according to
2868 * Bruce Evans, this is probably the best way to do it in
2869 * -stable. We only do this if we're already open, and
2870 * therefore dsopen() has already run. If CD_FLAG_OPEN
2871 * isn't set, this isn't necessary.
2873 if (softc->flags & CD_FLAG_OPEN) {
2874 int force;
2876 force = 1;
2878 error = dsioctl(softc->disk.d_cdev, DIOCSYNCSLICEINFO,
2879 /*data*/(caddr_t)&force, /*flags*/ 0,
2880 &softc->disk.d_slice,
2881 &softc->disk.d_info);
2882 if (error != 0) {
2884 * Set a bogus sector size, so the slice code
2885 * won't try to divide by 0 and panic the
2886 * kernel.
2888 info.d_media_blksize = 2048;
2889 info.d_media_blocks = 0;
2890 info.d_media_size = 0;
2891 disk_setdiskinfo(&softc->disk, &info);
2894 * Tell devstat(9) we don't have a blocksize.
2896 softc->device_stats.flags |=
2897 DEVSTAT_BS_UNAVAILABLE;
2899 cdprevent(periph, PR_ALLOW);
2904 * We unconditionally (re)set the blocksize each time the
2905 * CD device is opened. This is because the CD can change,
2906 * and therefore the blocksize might change.
2907 * XXX problems here if some slice or partition is still
2908 * open with the old size?
2910 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
2911 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
2912 softc->device_stats.block_size = softc->params.blksize;
2914 softc->flags |= CD_FLAG_VALID_MEDIA;
2918 * Now we check the table of contents. This (currently) is only
2919 * used for the CDIOCPLAYTRACKS ioctl. It may be used later to do
2920 * things like present a separate entry in /dev for each track,
2921 * like that acd(4) driver does.
2923 bzero(&softc->toc, sizeof(softc->toc));
2924 toch = &softc->toc.header;
2927 * We will get errors here for media that doesn't have a table of
2928 * contents. According to the MMC-3 spec: "When a Read TOC/PMA/ATIP
2929 * command is presented for a DDCD/CD-R/RW media, where the first TOC
2930 * has not been recorded (no complete session) and the Format codes
2931 * 0000b, 0001b, or 0010b are specified, this command shall be rejected
2932 * with an INVALID FIELD IN CDB. Devices that are not capable of
2933 * reading an incomplete session on DDC/CD-R/RW media shall report
2934 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT."
2936 * So this isn't fatal if we can't read the table of contents, it
2937 * just means that the user won't be able to issue the play tracks
2938 * ioctl, and likely lots of other stuff won't work either. They
2939 * need to burn the CD before we can do a whole lot with it. So
2940 * we don't print anything here if we get an error back.
2942 error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
2943 SF_NO_PRINT);
2945 * Errors in reading the table of contents aren't fatal, we just
2946 * won't have a valid table of contents cached.
2948 if (error != 0) {
2949 error = 0;
2950 bzero(&softc->toc, sizeof(softc->toc));
2951 goto bailout;
2954 if (softc->quirks & CD_Q_BCD_TRACKS) {
2955 toch->starting_track = bcd2bin(toch->starting_track);
2956 toch->ending_track = bcd2bin(toch->ending_track);
2959 /* Number of TOC entries, plus leadout */
2960 num_entries = (toch->ending_track - toch->starting_track) + 2;
2962 if (num_entries <= 0)
2963 goto bailout;
2965 toclen = num_entries * sizeof(struct cd_toc_entry);
2967 error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
2968 (u_int8_t *)&softc->toc, toclen + sizeof(*toch),
2969 SF_NO_PRINT);
2970 if (error != 0) {
2971 error = 0;
2972 bzero(&softc->toc, sizeof(softc->toc));
2973 goto bailout;
2976 if (softc->quirks & CD_Q_BCD_TRACKS) {
2977 toch->starting_track = bcd2bin(toch->starting_track);
2978 toch->ending_track = bcd2bin(toch->ending_track);
2980 toch->len = scsi_2btoul((uint8_t *)&toch->len);
2983 * XXX KDM is this necessary? Probably only if the drive doesn't
2984 * return leadout information with the table of contents.
2986 cdindex = toch->starting_track + num_entries -1;
2987 if (cdindex == toch->ending_track + 1) {
2989 error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
2990 (u_int8_t *)&leadout, sizeof(leadout),
2991 SF_NO_PRINT);
2992 if (error != 0) {
2993 error = 0;
2994 goto bailout;
2996 softc->toc.entries[cdindex - toch->starting_track] =
2997 leadout.entry;
2999 if (softc->quirks & CD_Q_BCD_TRACKS) {
3000 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3001 softc->toc.entries[cdindex].track =
3002 bcd2bin(softc->toc.entries[cdindex].track);
3007 * Run through the TOC entries, find the first entry and determine
3008 * whether it is an audio or data track.
3010 for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
3011 if (softc->toc.entries[cdindex].track == toch->starting_track) {
3012 if (softc->toc.entries[cdindex].control & 0x04)
3013 first_track_audio = 0;
3014 else
3015 first_track_audio = 1;
3016 break;
3021 * If first_track_audio is non-zero, we either have an error (e.g.
3022 * couldn't find the starting track) or the first track is an audio
3023 * track. If first_track_audio is 0, the first track is a data
3024 * track that could have a disklabel. Attempt to read the
3025 * disklabel off the media, just in case the user put one there.
3027 if (first_track_audio == 0) {
3028 info.d_dsflags |= DSO_COMPATLABEL;
3029 info.d_dsflags &= ~DSO_NOLABELS;
3030 disk_setdiskinfo(&softc->disk, &info);
3032 softc->flags |= CD_FLAG_VALID_TOC;
3034 bailout:
3035 return (error);
3038 static int
3039 cdsize(struct cam_periph *periph, u_int32_t *size)
3041 struct cd_softc *softc;
3042 union ccb *ccb;
3043 struct scsi_read_capacity_data *rcap_buf;
3044 int error;
3046 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
3048 softc = (struct cd_softc *)periph->softc;
3050 ccb = cdgetccb(periph, /* priority */ 1);
3052 rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
3053 M_SCSICD, M_INTWAIT | M_ZERO);
3055 scsi_read_capacity(&ccb->csio,
3056 /*retries*/ 1,
3057 cddone,
3058 MSG_SIMPLE_Q_TAG,
3059 rcap_buf,
3060 SSD_FULL_SIZE,
3061 /* timeout */20000);
3063 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3064 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT);
3066 xpt_release_ccb(ccb);
3068 softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
3069 softc->params.blksize = scsi_4btoul(rcap_buf->length);
3071 * SCSI-3 mandates that the reported blocksize shall be 2048.
3072 * Older drives sometimes report funny values, trim it down to
3073 * 2048, or other parts of the kernel will get confused.
3075 * XXX we leave drives alone that might report 512 bytes, as
3076 * well as drives reporting more weird sizes like perhaps 4K.
3078 if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
3079 softc->params.blksize = 2048;
3081 kfree(rcap_buf, M_SCSICD);
3082 *size = softc->params.disksize;
3084 return (error);
3088 static int
3089 cd6byteworkaround(union ccb *ccb)
3091 u_int8_t *cdb;
3092 struct cam_periph *periph;
3093 struct cd_softc *softc;
3094 struct cd_mode_params *params;
3095 int frozen, found;
3097 periph = xpt_path_periph(ccb->ccb_h.path);
3098 softc = (struct cd_softc *)periph->softc;
3100 cdb = ccb->csio.cdb_io.cdb_bytes;
3102 if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
3103 || ((cdb[0] != MODE_SENSE_6)
3104 && (cdb[0] != MODE_SELECT_6)))
3105 return (0);
3108 * Because there is no convenient place to stash the overall
3109 * cd_mode_params structure pointer, we have to grab it like this.
3110 * This means that ALL MODE_SENSE and MODE_SELECT requests in the
3111 * cd(4) driver MUST go through cdgetmode() and cdsetmode()!
3113 * XXX It would be nice if, at some point, we could increase the
3114 * number of available peripheral private pointers. Both pointers
3115 * are currently used in most every peripheral driver.
3117 found = 0;
3119 STAILQ_FOREACH(params, &softc->mode_queue, links) {
3120 if (params->mode_buf == ccb->csio.data_ptr) {
3121 found = 1;
3122 break;
3127 * This shouldn't happen. All mode sense and mode select
3128 * operations in the cd(4) driver MUST go through cdgetmode() and
3129 * cdsetmode()!
3131 if (found == 0) {
3132 xpt_print(periph->path,
3133 "mode buffer not found in mode queue!\n");
3134 return (0);
3137 params->cdb_size = 10;
3138 softc->minimum_command_size = 10;
3139 xpt_print(ccb->ccb_h.path,
3140 "%s(6) failed, increasing minimum CDB size to 10 bytes\n",
3141 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
3143 if (cdb[0] == MODE_SENSE_6) {
3144 struct scsi_mode_sense_10 ms10;
3145 struct scsi_mode_sense_6 *ms6;
3146 int len;
3148 ms6 = (struct scsi_mode_sense_6 *)cdb;
3150 bzero(&ms10, sizeof(ms10));
3151 ms10.opcode = MODE_SENSE_10;
3152 ms10.byte2 = ms6->byte2;
3153 ms10.page = ms6->page;
3156 * 10 byte mode header, block descriptor,
3157 * sizeof(union cd_pages)
3159 len = sizeof(struct cd_mode_data_10);
3160 ccb->csio.dxfer_len = len;
3162 scsi_ulto2b(len, ms10.length);
3163 ms10.control = ms6->control;
3164 bcopy(&ms10, cdb, 10);
3165 ccb->csio.cdb_len = 10;
3166 } else {
3167 struct scsi_mode_select_10 ms10;
3168 struct scsi_mode_select_6 *ms6;
3169 struct scsi_mode_header_6 *header6;
3170 struct scsi_mode_header_10 *header10;
3171 struct scsi_mode_page_header *page_header;
3172 int blk_desc_len, page_num, page_size, len;
3174 ms6 = (struct scsi_mode_select_6 *)cdb;
3176 bzero(&ms10, sizeof(ms10));
3177 ms10.opcode = MODE_SELECT_10;
3178 ms10.byte2 = ms6->byte2;
3180 header6 = (struct scsi_mode_header_6 *)params->mode_buf;
3181 header10 = (struct scsi_mode_header_10 *)params->mode_buf;
3183 page_header = find_mode_page_6(header6);
3184 page_num = page_header->page_code;
3186 blk_desc_len = header6->blk_desc_len;
3188 page_size = cdgetpagesize(page_num);
3190 if (page_size != (page_header->page_length +
3191 sizeof(*page_header)))
3192 page_size = page_header->page_length +
3193 sizeof(*page_header);
3195 len = sizeof(*header10) + blk_desc_len + page_size;
3197 len = min(params->alloc_len, len);
3200 * Since the 6 byte parameter header is shorter than the 10
3201 * byte parameter header, we need to copy the actual mode
3202 * page data, and the block descriptor, if any, so things wind
3203 * up in the right place. The regions will overlap, but
3204 * bcopy() does the right thing.
3206 bcopy(params->mode_buf + sizeof(*header6),
3207 params->mode_buf + sizeof(*header10),
3208 len - sizeof(*header10));
3210 /* Make sure these fields are set correctly. */
3211 scsi_ulto2b(0, header10->data_length);
3212 header10->medium_type = 0;
3213 scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
3215 ccb->csio.dxfer_len = len;
3217 scsi_ulto2b(len, ms10.length);
3218 ms10.control = ms6->control;
3219 bcopy(&ms10, cdb, 10);
3220 ccb->csio.cdb_len = 10;
3223 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3224 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3225 xpt_action(ccb);
3226 if (frozen) {
3227 cam_release_devq(ccb->ccb_h.path,
3228 /*relsim_flags*/0,
3229 /*openings*/0,
3230 /*timeout*/0,
3231 /*getcount_only*/0);
3234 return (ERESTART);
3237 static int
3238 cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3240 struct cd_softc *softc;
3241 struct cam_periph *periph;
3242 int error;
3244 periph = xpt_path_periph(ccb->ccb_h.path);
3245 softc = (struct cd_softc *)periph->softc;
3247 error = 0;
3250 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte
3251 * CDB comes back with this particular error, try transforming it
3252 * into the 10 byte version.
3254 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3255 error = cd6byteworkaround(ccb);
3256 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3257 CAM_SCSI_STATUS_ERROR)
3258 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
3259 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
3260 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
3261 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
3262 int sense_key, error_code, asc, ascq;
3264 scsi_extract_sense(&ccb->csio.sense_data,
3265 &error_code, &sense_key, &asc, &ascq);
3266 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3267 error = cd6byteworkaround(ccb);
3270 if (error == ERESTART)
3271 return (error);
3274 * XXX
3275 * Until we have a better way of doing pack validation,
3276 * don't treat UAs as errors.
3278 sense_flags |= SF_RETRY_UA;
3279 return (cam_periph_error(ccb, cam_flags, sense_flags,
3280 &softc->saved_ccb));
3284 * Read table of contents
3286 static int
3287 cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
3288 u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
3290 struct scsi_read_toc *scsi_cmd;
3291 u_int32_t ntoc;
3292 struct ccb_scsiio *csio;
3293 union ccb *ccb;
3294 int error;
3296 ntoc = len;
3297 error = 0;
3299 ccb = cdgetccb(periph, /* priority */ 1);
3301 csio = &ccb->csio;
3303 cam_fill_csio(csio,
3304 /* retries */ 1,
3305 /* cbfcnp */ cddone,
3306 /* flags */ CAM_DIR_IN,
3307 /* tag_action */ MSG_SIMPLE_Q_TAG,
3308 /* data_ptr */ data,
3309 /* dxfer_len */ len,
3310 /* sense_len */ SSD_FULL_SIZE,
3311 sizeof(struct scsi_read_toc),
3312 /* timeout */ 50000);
3314 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
3315 bzero (scsi_cmd, sizeof(*scsi_cmd));
3317 if (mode == CD_MSF_FORMAT)
3318 scsi_cmd->byte2 |= CD_MSF;
3319 scsi_cmd->from_track = start;
3320 /* scsi_ulto2b(ntoc, (u_int8_t *)scsi_cmd->data_len); */
3321 scsi_cmd->data_len[0] = (ntoc) >> 8;
3322 scsi_cmd->data_len[1] = (ntoc) & 0xff;
3324 scsi_cmd->op_code = READ_TOC;
3326 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3327 /*sense_flags*/SF_RETRY_UA | sense_flags);
3329 xpt_release_ccb(ccb);
3331 return(error);
3334 static int
3335 cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
3336 u_int32_t format, int track,
3337 struct cd_sub_channel_info *data, u_int32_t len)
3339 struct scsi_read_subchannel *scsi_cmd;
3340 struct ccb_scsiio *csio;
3341 union ccb *ccb;
3342 int error;
3344 error = 0;
3346 ccb = cdgetccb(periph, /* priority */ 1);
3348 csio = &ccb->csio;
3350 cam_fill_csio(csio,
3351 /* retries */ 1,
3352 /* cbfcnp */ cddone,
3353 /* flags */ CAM_DIR_IN,
3354 /* tag_action */ MSG_SIMPLE_Q_TAG,
3355 /* data_ptr */ (u_int8_t *)data,
3356 /* dxfer_len */ len,
3357 /* sense_len */ SSD_FULL_SIZE,
3358 sizeof(struct scsi_read_subchannel),
3359 /* timeout */ 50000);
3361 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
3362 bzero (scsi_cmd, sizeof(*scsi_cmd));
3364 scsi_cmd->op_code = READ_SUBCHANNEL;
3365 if (mode == CD_MSF_FORMAT)
3366 scsi_cmd->byte1 |= CD_MSF;
3367 scsi_cmd->byte2 = SRS_SUBQ;
3368 scsi_cmd->subchan_format = format;
3369 scsi_cmd->track = track;
3370 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
3371 scsi_cmd->control = 0;
3373 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3374 /*sense_flags*/SF_RETRY_UA);
3376 xpt_release_ccb(ccb);
3378 return(error);
3382 * All MODE_SENSE requests in the cd(4) driver MUST go through this
3383 * routine. See comments in cd6byteworkaround() for details.
3385 static int
3386 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
3387 u_int32_t page)
3389 struct ccb_scsiio *csio;
3390 struct cd_softc *softc;
3391 union ccb *ccb;
3392 int param_len;
3393 int error;
3395 softc = (struct cd_softc *)periph->softc;
3397 ccb = cdgetccb(periph, /* priority */ 1);
3399 csio = &ccb->csio;
3401 data->cdb_size = softc->minimum_command_size;
3402 if (data->cdb_size < 10)
3403 param_len = sizeof(struct cd_mode_data);
3404 else
3405 param_len = sizeof(struct cd_mode_data_10);
3407 /* Don't say we've got more room than we actually allocated */
3408 param_len = min(param_len, data->alloc_len);
3410 scsi_mode_sense_len(csio,
3411 /* retries */ 1,
3412 /* cbfcnp */ cddone,
3413 /* tag_action */ MSG_SIMPLE_Q_TAG,
3414 /* dbd */ 0,
3415 /* page_code */ SMS_PAGE_CTRL_CURRENT,
3416 /* page */ page,
3417 /* param_buf */ data->mode_buf,
3418 /* param_len */ param_len,
3419 /* minimum_cmd_size */ softc->minimum_command_size,
3420 /* sense_len */ SSD_FULL_SIZE,
3421 /* timeout */ 50000);
3424 * It would be nice not to have to do this, but there's no
3425 * available pointer in the CCB that would allow us to stuff the
3426 * mode params structure in there and retrieve it in
3427 * cd6byteworkaround(), so we can set the cdb size. The cdb size
3428 * lets the caller know what CDB size we ended up using, so they
3429 * can find the actual mode page offset.
3431 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3433 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3434 /*sense_flags*/SF_RETRY_UA);
3436 xpt_release_ccb(ccb);
3438 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3441 * This is a bit of belt-and-suspenders checking, but if we run
3442 * into a situation where the target sends back multiple block
3443 * descriptors, we might not have enough space in the buffer to
3444 * see the whole mode page. Better to return an error than
3445 * potentially access memory beyond our malloced region.
3447 if (error == 0) {
3448 u_int32_t data_len;
3450 if (data->cdb_size == 10) {
3451 struct scsi_mode_header_10 *hdr10;
3453 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
3454 data_len = scsi_2btoul(hdr10->data_length);
3455 data_len += sizeof(hdr10->data_length);
3456 } else {
3457 struct scsi_mode_header_6 *hdr6;
3459 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
3460 data_len = hdr6->data_length;
3461 data_len += sizeof(hdr6->data_length);
3465 * Complain if there is more mode data available than we
3466 * allocated space for. This could potentially happen if
3467 * we miscalculated the page length for some reason, if the
3468 * drive returns multiple block descriptors, or if it sets
3469 * the data length incorrectly.
3471 if (data_len > data->alloc_len) {
3472 xpt_print(periph->path, "allocated modepage %d length "
3473 "%d < returned length %d\n", page, data->alloc_len,
3474 data_len);
3475 error = ENOSPC;
3478 return (error);
3482 * All MODE_SELECT requests in the cd(4) driver MUST go through this
3483 * routine. See comments in cd6byteworkaround() for details.
3485 static int
3486 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
3488 struct ccb_scsiio *csio;
3489 struct cd_softc *softc;
3490 union ccb *ccb;
3491 int cdb_size, param_len;
3492 int error;
3494 softc = (struct cd_softc *)periph->softc;
3496 ccb = cdgetccb(periph, /* priority */ 1);
3498 csio = &ccb->csio;
3500 error = 0;
3503 * If the data is formatted for the 10 byte version of the mode
3504 * select parameter list, we need to use the 10 byte CDB.
3505 * Otherwise, we use whatever the stored minimum command size.
3507 if (data->cdb_size == 10)
3508 cdb_size = data->cdb_size;
3509 else
3510 cdb_size = softc->minimum_command_size;
3512 if (cdb_size >= 10) {
3513 struct scsi_mode_header_10 *mode_header;
3514 u_int32_t data_len;
3516 mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
3518 data_len = scsi_2btoul(mode_header->data_length);
3520 scsi_ulto2b(0, mode_header->data_length);
3522 * SONY drives do not allow a mode select with a medium_type
3523 * value that has just been returned by a mode sense; use a
3524 * medium_type of 0 (Default) instead.
3526 mode_header->medium_type = 0;
3529 * Pass back whatever the drive passed to us, plus the size
3530 * of the data length field.
3532 param_len = data_len + sizeof(mode_header->data_length);
3534 } else {
3535 struct scsi_mode_header_6 *mode_header;
3537 mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
3539 param_len = mode_header->data_length + 1;
3541 mode_header->data_length = 0;
3543 * SONY drives do not allow a mode select with a medium_type
3544 * value that has just been returned by a mode sense; use a
3545 * medium_type of 0 (Default) instead.
3547 mode_header->medium_type = 0;
3550 /* Don't say we've got more room than we actually allocated */
3551 param_len = min(param_len, data->alloc_len);
3553 scsi_mode_select_len(csio,
3554 /* retries */ 1,
3555 /* cbfcnp */ cddone,
3556 /* tag_action */ MSG_SIMPLE_Q_TAG,
3557 /* scsi_page_fmt */ 1,
3558 /* save_pages */ 0,
3559 /* param_buf */ data->mode_buf,
3560 /* param_len */ param_len,
3561 /* minimum_cmd_size */ cdb_size,
3562 /* sense_len */ SSD_FULL_SIZE,
3563 /* timeout */ 50000);
3565 /* See comments in cdgetmode() and cd6byteworkaround(). */
3566 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
3568 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3569 /*sense_flags*/SF_RETRY_UA);
3571 xpt_release_ccb(ccb);
3573 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
3575 return (error);
3580 static int
3581 cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
3583 struct ccb_scsiio *csio;
3584 union ccb *ccb;
3585 int error;
3586 u_int8_t cdb_len;
3588 error = 0;
3589 ccb = cdgetccb(periph, /* priority */ 1);
3590 csio = &ccb->csio;
3592 * Use the smallest possible command to perform the operation.
3594 if ((len & 0xffff0000) == 0) {
3596 * We can fit in a 10 byte cdb.
3598 struct scsi_play_10 *scsi_cmd;
3600 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
3601 bzero (scsi_cmd, sizeof(*scsi_cmd));
3602 scsi_cmd->op_code = PLAY_10;
3603 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3604 scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
3605 cdb_len = sizeof(*scsi_cmd);
3606 } else {
3607 struct scsi_play_12 *scsi_cmd;
3609 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
3610 bzero (scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->op_code = PLAY_12;
3612 scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
3613 scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
3614 cdb_len = sizeof(*scsi_cmd);
3616 cam_fill_csio(csio,
3617 /*retries*/2,
3618 cddone,
3619 /*flags*/CAM_DIR_NONE,
3620 MSG_SIMPLE_Q_TAG,
3621 /*dataptr*/NULL,
3622 /*datalen*/0,
3623 /*sense_len*/SSD_FULL_SIZE,
3624 cdb_len,
3625 /*timeout*/50 * 1000);
3627 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3628 /*sense_flags*/SF_RETRY_UA);
3630 xpt_release_ccb(ccb);
3632 return(error);
3635 static int
3636 cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
3637 u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
3639 struct scsi_play_msf *scsi_cmd;
3640 struct ccb_scsiio *csio;
3641 union ccb *ccb;
3642 int error;
3644 error = 0;
3646 ccb = cdgetccb(periph, /* priority */ 1);
3648 csio = &ccb->csio;
3650 cam_fill_csio(csio,
3651 /* retries */ 1,
3652 /* cbfcnp */ cddone,
3653 /* flags */ CAM_DIR_NONE,
3654 /* tag_action */ MSG_SIMPLE_Q_TAG,
3655 /* data_ptr */ NULL,
3656 /* dxfer_len */ 0,
3657 /* sense_len */ SSD_FULL_SIZE,
3658 sizeof(struct scsi_play_msf),
3659 /* timeout */ 50000);
3661 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
3662 bzero (scsi_cmd, sizeof(*scsi_cmd));
3664 scsi_cmd->op_code = PLAY_MSF;
3665 scsi_cmd->start_m = startm;
3666 scsi_cmd->start_s = starts;
3667 scsi_cmd->start_f = startf;
3668 scsi_cmd->end_m = endm;
3669 scsi_cmd->end_s = ends;
3670 scsi_cmd->end_f = endf;
3672 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3673 /*sense_flags*/SF_RETRY_UA);
3675 xpt_release_ccb(ccb);
3677 return(error);
3681 static int
3682 cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
3683 u_int32_t etrack, u_int32_t eindex)
3685 struct scsi_play_track *scsi_cmd;
3686 struct ccb_scsiio *csio;
3687 union ccb *ccb;
3688 int error;
3690 error = 0;
3692 ccb = cdgetccb(periph, /* priority */ 1);
3694 csio = &ccb->csio;
3696 cam_fill_csio(csio,
3697 /* retries */ 1,
3698 /* cbfcnp */ cddone,
3699 /* flags */ CAM_DIR_NONE,
3700 /* tag_action */ MSG_SIMPLE_Q_TAG,
3701 /* data_ptr */ NULL,
3702 /* dxfer_len */ 0,
3703 /* sense_len */ SSD_FULL_SIZE,
3704 sizeof(struct scsi_play_track),
3705 /* timeout */ 50000);
3707 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
3708 bzero (scsi_cmd, sizeof(*scsi_cmd));
3710 scsi_cmd->op_code = PLAY_TRACK;
3711 scsi_cmd->start_track = strack;
3712 scsi_cmd->start_index = sindex;
3713 scsi_cmd->end_track = etrack;
3714 scsi_cmd->end_index = eindex;
3716 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3717 /*sense_flags*/SF_RETRY_UA);
3719 xpt_release_ccb(ccb);
3721 return(error);
3724 static int
3725 cdpause(struct cam_periph *periph, u_int32_t go)
3727 struct scsi_pause *scsi_cmd;
3728 struct ccb_scsiio *csio;
3729 union ccb *ccb;
3730 int error;
3732 error = 0;
3734 ccb = cdgetccb(periph, /* priority */ 1);
3736 csio = &ccb->csio;
3738 cam_fill_csio(csio,
3739 /* retries */ 1,
3740 /* cbfcnp */ cddone,
3741 /* flags */ CAM_DIR_NONE,
3742 /* tag_action */ MSG_SIMPLE_Q_TAG,
3743 /* data_ptr */ NULL,
3744 /* dxfer_len */ 0,
3745 /* sense_len */ SSD_FULL_SIZE,
3746 sizeof(struct scsi_pause),
3747 /* timeout */ 50000);
3749 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
3750 bzero (scsi_cmd, sizeof(*scsi_cmd));
3752 scsi_cmd->op_code = PAUSE;
3753 scsi_cmd->resume = go;
3755 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3756 /*sense_flags*/SF_RETRY_UA);
3758 xpt_release_ccb(ccb);
3760 return(error);
3763 static int
3764 cdstartunit(struct cam_periph *periph, int load)
3766 union ccb *ccb;
3767 int error;
3769 error = 0;
3771 ccb = cdgetccb(periph, /* priority */ 1);
3773 scsi_start_stop(&ccb->csio,
3774 /* retries */ 1,
3775 /* cbfcnp */ cddone,
3776 /* tag_action */ MSG_SIMPLE_Q_TAG,
3777 /* start */ TRUE,
3778 /* load_eject */ load,
3779 /* immediate */ FALSE,
3780 /* sense_len */ SSD_FULL_SIZE,
3781 /* timeout */ 50000);
3783 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3784 /*sense_flags*/SF_RETRY_UA);
3786 xpt_release_ccb(ccb);
3788 return(error);
3791 static int
3792 cdstopunit(struct cam_periph *periph, u_int32_t eject)
3794 union ccb *ccb;
3795 int error;
3797 error = 0;
3799 ccb = cdgetccb(periph, /* priority */ 1);
3801 scsi_start_stop(&ccb->csio,
3802 /* retries */ 1,
3803 /* cbfcnp */ cddone,
3804 /* tag_action */ MSG_SIMPLE_Q_TAG,
3805 /* start */ FALSE,
3806 /* load_eject */ eject,
3807 /* immediate */ FALSE,
3808 /* sense_len */ SSD_FULL_SIZE,
3809 /* timeout */ 50000);
3811 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3812 /*sense_flags*/SF_RETRY_UA);
3814 xpt_release_ccb(ccb);
3816 return(error);
3819 static int
3820 cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
3822 struct scsi_set_speed *scsi_cmd;
3823 struct ccb_scsiio *csio;
3824 union ccb *ccb;
3825 int error;
3827 error = 0;
3828 ccb = cdgetccb(periph, /* priority */ 1);
3829 csio = &ccb->csio;
3831 /* Preserve old behavior: units in multiples of CDROM speed */
3832 if (rdspeed < 177)
3833 rdspeed *= 177;
3834 if (wrspeed < 177)
3835 wrspeed *= 177;
3837 cam_fill_csio(csio,
3838 /* retries */ 1,
3839 /* cbfcnp */ cddone,
3840 /* flags */ CAM_DIR_NONE,
3841 /* tag_action */ MSG_SIMPLE_Q_TAG,
3842 /* data_ptr */ NULL,
3843 /* dxfer_len */ 0,
3844 /* sense_len */ SSD_FULL_SIZE,
3845 sizeof(struct scsi_set_speed),
3846 /* timeout */ 50000);
3848 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
3849 bzero(scsi_cmd, sizeof(*scsi_cmd));
3851 scsi_cmd->opcode = SET_CD_SPEED;
3852 scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
3853 scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
3855 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3856 /*sense_flags*/SF_RETRY_UA);
3858 xpt_release_ccb(ccb);
3860 return(error);
3863 static int
3864 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
3866 union ccb *ccb;
3867 u_int8_t *databuf;
3868 u_int32_t lba;
3869 int error;
3870 int length;
3872 error = 0;
3873 databuf = NULL;
3874 lba = 0;
3876 ccb = cdgetccb(periph, /* priority */ 1);
3878 switch (authinfo->format) {
3879 case DVD_REPORT_AGID:
3880 length = sizeof(struct scsi_report_key_data_agid);
3881 break;
3882 case DVD_REPORT_CHALLENGE:
3883 length = sizeof(struct scsi_report_key_data_challenge);
3884 break;
3885 case DVD_REPORT_KEY1:
3886 length = sizeof(struct scsi_report_key_data_key1_key2);
3887 break;
3888 case DVD_REPORT_TITLE_KEY:
3889 length = sizeof(struct scsi_report_key_data_title);
3890 /* The lba field is only set for the title key */
3891 lba = authinfo->lba;
3892 break;
3893 case DVD_REPORT_ASF:
3894 length = sizeof(struct scsi_report_key_data_asf);
3895 break;
3896 case DVD_REPORT_RPC:
3897 length = sizeof(struct scsi_report_key_data_rpc);
3898 break;
3899 case DVD_INVALIDATE_AGID:
3900 length = 0;
3901 break;
3902 default:
3903 error = EINVAL;
3904 goto bailout;
3905 break; /* NOTREACHED */
3908 if (length != 0) {
3909 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
3910 } else {
3911 databuf = NULL;
3915 scsi_report_key(&ccb->csio,
3916 /* retries */ 1,
3917 /* cbfcnp */ cddone,
3918 /* tag_action */ MSG_SIMPLE_Q_TAG,
3919 /* lba */ lba,
3920 /* agid */ authinfo->agid,
3921 /* key_format */ authinfo->format,
3922 /* data_ptr */ databuf,
3923 /* dxfer_len */ length,
3924 /* sense_len */ SSD_FULL_SIZE,
3925 /* timeout */ 50000);
3927 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
3928 /*sense_flags*/SF_RETRY_UA);
3930 if (error != 0)
3931 goto bailout;
3933 if (ccb->csio.resid != 0) {
3934 xpt_print(periph->path, "warning, residual for report key "
3935 "command is %d\n", ccb->csio.resid);
3938 switch(authinfo->format) {
3939 case DVD_REPORT_AGID: {
3940 struct scsi_report_key_data_agid *agid_data;
3942 agid_data = (struct scsi_report_key_data_agid *)databuf;
3944 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
3945 RKD_AGID_SHIFT;
3946 break;
3948 case DVD_REPORT_CHALLENGE: {
3949 struct scsi_report_key_data_challenge *chal_data;
3951 chal_data = (struct scsi_report_key_data_challenge *)databuf;
3953 bcopy(chal_data->challenge_key, authinfo->keychal,
3954 min(sizeof(chal_data->challenge_key),
3955 sizeof(authinfo->keychal)));
3956 break;
3958 case DVD_REPORT_KEY1: {
3959 struct scsi_report_key_data_key1_key2 *key1_data;
3961 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
3963 bcopy(key1_data->key1, authinfo->keychal,
3964 min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
3965 break;
3967 case DVD_REPORT_TITLE_KEY: {
3968 struct scsi_report_key_data_title *title_data;
3970 title_data = (struct scsi_report_key_data_title *)databuf;
3972 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
3973 RKD_TITLE_CPM_SHIFT;
3974 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
3975 RKD_TITLE_CP_SEC_SHIFT;
3976 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
3977 RKD_TITLE_CMGS_SHIFT;
3978 bcopy(title_data->title_key, authinfo->keychal,
3979 min(sizeof(title_data->title_key),
3980 sizeof(authinfo->keychal)));
3981 break;
3983 case DVD_REPORT_ASF: {
3984 struct scsi_report_key_data_asf *asf_data;
3986 asf_data = (struct scsi_report_key_data_asf *)databuf;
3988 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
3989 break;
3991 case DVD_REPORT_RPC: {
3992 struct scsi_report_key_data_rpc *rpc_data;
3994 rpc_data = (struct scsi_report_key_data_rpc *)databuf;
3996 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
3997 RKD_RPC_TYPE_SHIFT;
3998 authinfo->vend_rsts =
3999 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
4000 RKD_RPC_VENDOR_RESET_SHIFT;
4001 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
4002 authinfo->region = rpc_data->region_mask;
4003 authinfo->rpc_scheme = rpc_data->rpc_scheme1;
4004 break;
4006 case DVD_INVALIDATE_AGID:
4007 break;
4008 default:
4009 /* This should be impossible, since we checked above */
4010 error = EINVAL;
4011 goto bailout;
4012 break; /* NOTREACHED */
4014 bailout:
4015 if (databuf != NULL)
4016 kfree(databuf, M_DEVBUF);
4018 xpt_release_ccb(ccb);
4020 return(error);
4023 static int
4024 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
4026 union ccb *ccb;
4027 u_int8_t *databuf;
4028 int length;
4029 int error;
4031 error = 0;
4032 databuf = NULL;
4034 ccb = cdgetccb(periph, /* priority */ 1);
4036 switch(authinfo->format) {
4037 case DVD_SEND_CHALLENGE: {
4038 struct scsi_report_key_data_challenge *challenge_data;
4040 length = sizeof(*challenge_data);
4042 challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4044 databuf = (u_int8_t *)challenge_data;
4046 scsi_ulto2b(length - sizeof(challenge_data->data_len),
4047 challenge_data->data_len);
4049 bcopy(authinfo->keychal, challenge_data->challenge_key,
4050 min(sizeof(authinfo->keychal),
4051 sizeof(challenge_data->challenge_key)));
4052 break;
4054 case DVD_SEND_KEY2: {
4055 struct scsi_report_key_data_key1_key2 *key2_data;
4057 length = sizeof(*key2_data);
4059 key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4061 databuf = (u_int8_t *)key2_data;
4063 scsi_ulto2b(length - sizeof(key2_data->data_len),
4064 key2_data->data_len);
4066 bcopy(authinfo->keychal, key2_data->key1,
4067 min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
4069 break;
4071 case DVD_SEND_RPC: {
4072 struct scsi_send_key_data_rpc *rpc_data;
4074 length = sizeof(*rpc_data);
4076 rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4078 databuf = (u_int8_t *)rpc_data;
4080 scsi_ulto2b(length - sizeof(rpc_data->data_len),
4081 rpc_data->data_len);
4083 rpc_data->region_code = authinfo->region;
4084 break;
4086 default:
4087 error = EINVAL;
4088 goto bailout;
4089 break; /* NOTREACHED */
4092 scsi_send_key(&ccb->csio,
4093 /* retries */ 1,
4094 /* cbfcnp */ cddone,
4095 /* tag_action */ MSG_SIMPLE_Q_TAG,
4096 /* agid */ authinfo->agid,
4097 /* key_format */ authinfo->format,
4098 /* data_ptr */ databuf,
4099 /* dxfer_len */ length,
4100 /* sense_len */ SSD_FULL_SIZE,
4101 /* timeout */ 50000);
4103 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4104 /*sense_flags*/SF_RETRY_UA);
4106 bailout:
4108 if (databuf != NULL)
4109 kfree(databuf, M_DEVBUF);
4111 xpt_release_ccb(ccb);
4113 return(error);
4116 static int
4117 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
4119 union ccb *ccb;
4120 u_int8_t *databuf;
4121 u_int32_t address;
4122 int error;
4123 int length;
4125 error = 0;
4126 databuf = NULL;
4127 /* The address is reserved for many of the formats */
4128 address = 0;
4130 ccb = cdgetccb(periph, /* priority */ 1);
4132 switch(dvdstruct->format) {
4133 case DVD_STRUCT_PHYSICAL:
4134 length = sizeof(struct scsi_read_dvd_struct_data_physical);
4135 break;
4136 case DVD_STRUCT_COPYRIGHT:
4137 length = sizeof(struct scsi_read_dvd_struct_data_copyright);
4138 break;
4139 case DVD_STRUCT_DISCKEY:
4140 length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
4141 break;
4142 case DVD_STRUCT_BCA:
4143 length = sizeof(struct scsi_read_dvd_struct_data_bca);
4144 break;
4145 case DVD_STRUCT_MANUFACT:
4146 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
4147 break;
4148 case DVD_STRUCT_CMI:
4149 error = ENODEV;
4150 goto bailout;
4151 #ifdef notyet
4152 length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
4153 address = dvdstruct->address;
4154 #endif
4155 break; /* NOTREACHED */
4156 case DVD_STRUCT_PROTDISCID:
4157 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
4158 break;
4159 case DVD_STRUCT_DISCKEYBLOCK:
4160 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
4161 break;
4162 case DVD_STRUCT_DDS:
4163 length = sizeof(struct scsi_read_dvd_struct_data_dds);
4164 break;
4165 case DVD_STRUCT_MEDIUM_STAT:
4166 length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
4167 break;
4168 case DVD_STRUCT_SPARE_AREA:
4169 length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
4170 break;
4171 case DVD_STRUCT_RMD_LAST:
4172 error = ENODEV;
4173 goto bailout;
4174 #ifdef notyet
4175 length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
4176 address = dvdstruct->address;
4177 #endif
4178 break; /* NOTREACHED */
4179 case DVD_STRUCT_RMD_RMA:
4180 error = ENODEV;
4181 goto bailout;
4182 #ifdef notyet
4183 length = sizeof(struct scsi_read_dvd_struct_data_rmd);
4184 address = dvdstruct->address;
4185 #endif
4186 break; /* NOTREACHED */
4187 case DVD_STRUCT_PRERECORDED:
4188 length = sizeof(struct scsi_read_dvd_struct_data_leadin);
4189 break;
4190 case DVD_STRUCT_UNIQUEID:
4191 length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
4192 break;
4193 case DVD_STRUCT_DCB:
4194 error = ENODEV;
4195 goto bailout;
4196 #ifdef notyet
4197 length = sizeof(struct scsi_read_dvd_struct_data_dcb);
4198 address = dvdstruct->address;
4199 #endif
4200 break; /* NOTREACHED */
4201 case DVD_STRUCT_LIST:
4203 * This is the maximum allocation length for the READ DVD
4204 * STRUCTURE command. There's nothing in the MMC3 spec
4205 * that indicates a limit in the amount of data that can
4206 * be returned from this call, other than the limits
4207 * imposed by the 2-byte length variables.
4209 length = 65535;
4210 break;
4211 default:
4212 error = EINVAL;
4213 goto bailout;
4214 break; /* NOTREACHED */
4217 if (length != 0) {
4218 databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
4219 } else {
4220 databuf = NULL;
4223 scsi_read_dvd_structure(&ccb->csio,
4224 /* retries */ 1,
4225 /* cbfcnp */ cddone,
4226 /* tag_action */ MSG_SIMPLE_Q_TAG,
4227 /* lba */ address,
4228 /* layer_number */ dvdstruct->layer_num,
4229 /* key_format */ dvdstruct->format,
4230 /* agid */ dvdstruct->agid,
4231 /* data_ptr */ databuf,
4232 /* dxfer_len */ length,
4233 /* sense_len */ SSD_FULL_SIZE,
4234 /* timeout */ 50000);
4236 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO,
4237 /*sense_flags*/SF_RETRY_UA);
4239 if (error != 0)
4240 goto bailout;
4242 switch(dvdstruct->format) {
4243 case DVD_STRUCT_PHYSICAL: {
4244 struct scsi_read_dvd_struct_data_layer_desc *inlayer;
4245 struct dvd_layer *outlayer;
4246 struct scsi_read_dvd_struct_data_physical *phys_data;
4248 phys_data =
4249 (struct scsi_read_dvd_struct_data_physical *)databuf;
4250 inlayer = &phys_data->layer_desc;
4251 outlayer = (struct dvd_layer *)&dvdstruct->data;
4253 dvdstruct->length = sizeof(*inlayer);
4255 outlayer->book_type = (inlayer->book_type_version &
4256 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
4257 outlayer->book_version = (inlayer->book_type_version &
4258 RDSD_BOOK_VERSION_MASK);
4259 outlayer->disc_size = (inlayer->disc_size_max_rate &
4260 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
4261 outlayer->max_rate = (inlayer->disc_size_max_rate &
4262 RDSD_MAX_RATE_MASK);
4263 outlayer->nlayers = (inlayer->layer_info &
4264 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
4265 outlayer->track_path = (inlayer->layer_info &
4266 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
4267 outlayer->layer_type = (inlayer->layer_info &
4268 RDSD_LAYER_TYPE_MASK);
4269 outlayer->linear_density = (inlayer->density &
4270 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
4271 outlayer->track_density = (inlayer->density &
4272 RDSD_TRACK_DENSITY_MASK);
4273 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
4274 RDSD_BCA_SHIFT;
4275 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
4276 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
4277 outlayer->end_sector_l0 =
4278 scsi_3btoul(inlayer->end_sector_layer0);
4279 break;
4281 case DVD_STRUCT_COPYRIGHT: {
4282 struct scsi_read_dvd_struct_data_copyright *copy_data;
4284 copy_data = (struct scsi_read_dvd_struct_data_copyright *)
4285 databuf;
4287 dvdstruct->cpst = copy_data->cps_type;
4288 dvdstruct->rmi = copy_data->region_info;
4289 dvdstruct->length = 0;
4291 break;
4293 default:
4295 * Tell the user what the overall length is, no matter
4296 * what we can actually fit in the data buffer.
4298 dvdstruct->length = length - ccb->csio.resid -
4299 sizeof(struct scsi_read_dvd_struct_data_header);
4302 * But only actually copy out the smaller of what we read
4303 * in or what the structure can take.
4305 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
4306 dvdstruct->data,
4307 min(sizeof(dvdstruct->data), dvdstruct->length));
4308 break;
4310 bailout:
4312 if (databuf != NULL)
4313 kfree(databuf, M_DEVBUF);
4315 xpt_release_ccb(ccb);
4317 return(error);
4320 void
4321 scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
4322 void (*cbfcnp)(struct cam_periph *, union ccb *),
4323 u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
4324 u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
4325 u_int8_t sense_len, u_int32_t timeout)
4327 struct scsi_report_key *scsi_cmd;
4329 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
4330 bzero(scsi_cmd, sizeof(*scsi_cmd));
4331 scsi_cmd->opcode = REPORT_KEY;
4332 scsi_ulto4b(lba, scsi_cmd->lba);
4333 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4334 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4335 (key_format & RK_KF_KEYFORMAT_MASK);
4337 cam_fill_csio(csio,
4338 retries,
4339 cbfcnp,
4340 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
4341 tag_action,
4342 /*data_ptr*/ data_ptr,
4343 /*dxfer_len*/ dxfer_len,
4344 sense_len,
4345 sizeof(*scsi_cmd),
4346 timeout);
4349 void
4350 scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
4351 void (*cbfcnp)(struct cam_periph *, union ccb *),
4352 u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
4353 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4354 u_int32_t timeout)
4356 struct scsi_send_key *scsi_cmd;
4358 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
4359 bzero(scsi_cmd, sizeof(*scsi_cmd));
4360 scsi_cmd->opcode = SEND_KEY;
4362 scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
4363 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
4364 (key_format & RK_KF_KEYFORMAT_MASK);
4366 cam_fill_csio(csio,
4367 retries,
4368 cbfcnp,
4369 /*flags*/ CAM_DIR_OUT,
4370 tag_action,
4371 /*data_ptr*/ data_ptr,
4372 /*dxfer_len*/ dxfer_len,
4373 sense_len,
4374 sizeof(*scsi_cmd),
4375 timeout);
4379 void
4380 scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
4381 void (*cbfcnp)(struct cam_periph *, union ccb *),
4382 u_int8_t tag_action, u_int32_t address,
4383 u_int8_t layer_number, u_int8_t format, u_int8_t agid,
4384 u_int8_t *data_ptr, u_int32_t dxfer_len,
4385 u_int8_t sense_len, u_int32_t timeout)
4387 struct scsi_read_dvd_structure *scsi_cmd;
4389 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
4390 bzero(scsi_cmd, sizeof(*scsi_cmd));
4391 scsi_cmd->opcode = READ_DVD_STRUCTURE;
4393 scsi_ulto4b(address, scsi_cmd->address);
4394 scsi_cmd->layer_number = layer_number;
4395 scsi_cmd->format = format;
4396 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
4397 /* The AGID is the top two bits of this byte */
4398 scsi_cmd->agid = agid << 6;
4400 cam_fill_csio(csio,
4401 retries,
4402 cbfcnp,
4403 /*flags*/ CAM_DIR_IN,
4404 tag_action,
4405 /*data_ptr*/ data_ptr,
4406 /*dxfer_len*/ dxfer_len,
4407 sense_len,
4408 sizeof(*scsi_cmd),
4409 timeout);