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