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