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