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