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