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