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