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