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:
--- 32 unchanged lines hidden (view full) ---
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 168752 2007-04-15 08:49:19Z scottl $");
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>
--- 7 unchanged lines hidden (view full) ---
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#include <cam/cam_sim.h>
74
75#include <cam/scsi/scsi_message.h>
76#include <cam/scsi/scsi_da.h>
77#include <cam/scsi/scsi_cd.h>
78
79#define LEADOUT 0xaa /* leadout toc entry */
80
81struct cd_params {
--- 17 unchanged lines hidden (view full) ---
99 CD_FLAG_DISC_REMOVABLE = 0x0008,
100 CD_FLAG_TAGGED_QUEUING = 0x0010,
101 CD_FLAG_CHANGER = 0x0040,
102 CD_FLAG_ACTIVE = 0x0080,
103 CD_FLAG_SCHED_ON_COMP = 0x0100,
104 CD_FLAG_RETRY_UA = 0x0200,
105 CD_FLAG_VALID_MEDIA = 0x0400,
106 CD_FLAG_VALID_TOC = 0x0800,
107 CD_FLAG_SCTX_INIT = 0x1000,
108 CD_FLAG_OPEN = 0x2000
109} cd_flags;
110
111typedef enum {
112 CD_CCB_PROBE = 0x01,
113 CD_CCB_BUFFER_IO = 0x02,
114 CD_CCB_WAITING = 0x03,
115 CD_CCB_TYPE_MASK = 0x0F,
116 CD_CCB_RETRY_UA = 0x10
--- 170 unchanged lines hidden (view full) ---
287static struct periph_driver cddriver =
288{
289 cdinit, "cd",
290 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
291};
292
293PERIPHDRIVER_DECLARE(cd, cddriver);
294
295#ifndef CHANGER_MIN_BUSY_SECONDS
296#define CHANGER_MIN_BUSY_SECONDS 5
297#endif
298#ifndef CHANGER_MAX_BUSY_SECONDS
299#define CHANGER_MAX_BUSY_SECONDS 15
300#endif
301
302static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
--- 10 unchanged lines hidden (view full) ---
313
314struct cdchanger {
315 path_id_t path_id;
316 target_id_t target_id;
317 int num_devices;
318 struct camq devq;
319 struct timeval start_time;
320 struct cd_softc *cur_device;
321 struct callout short_handle;
322 struct callout long_handle;
323 volatile cd_changer_flags flags;
324 STAILQ_ENTRY(cdchanger) changer_links;
325 STAILQ_HEAD(chdevlist, cd_softc) chluns;
326};
327
328static struct mtx changerq_mtx;
329static STAILQ_HEAD(changerlist, cdchanger) changerq;
330static int num_changers;
331
332static void
333cdinit(void)
334{
335 cam_status status;
336 struct cam_path *path;
337
338 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
339 STAILQ_INIT(&changerq);
340
341 /*
342 * Install a global async callback. This callback will
343 * receive async callbacks like "new device found".
344 */
345 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
346 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
347
348 if (status == CAM_REQ_CMP) {
--- 13 unchanged lines hidden (view full) ---
362 printf("cd: Failed to attach master async callback "
363 "due to status 0x%x!\n", status);
364 }
365}
366
367static void
368cdoninvalidate(struct cam_periph *periph)
369{
370 struct cd_softc *softc;
371 struct ccb_setasync csa;
372
373 softc = (struct cd_softc *)periph->softc;
374
375 /*
376 * De-register any async callbacks.
377 */
378 xpt_setup_ccb(&csa.ccb_h, periph->path,
379 /* priority */ 5);
380 csa.ccb_h.func_code = XPT_SASYNC_CB;
381 csa.event_enable = 0;
382 csa.callback = cdasync;
383 csa.callback_arg = periph;
384 xpt_action((union ccb *)&csa);
385
386 softc->flags |= CD_FLAG_INVALID;
387
388 /*
389 * Return all queued I/O with ENXIO.
390 * XXX Handle any transactions queued to the card
391 * with XPT_ABORT_CCB.
392 */
393 bioq_flush(&softc->bio_queue, NULL, ENXIO);
394
395 /*
396 * If this device is part of a changer, and it was scheduled
397 * to run, remove it from the run queue since we just nuked
398 * all of its scheduled I/O.
399 */
400 if ((softc->flags & CD_FLAG_CHANGER)
401 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
402 camq_remove(&softc->changer->devq, softc->pinfo.index);
403
404 disk_gone(softc->disk);
405 xpt_print(periph->path, "lost device\n");
406}
407
408static void
409cdcleanup(struct cam_periph *periph)
410{
411 struct cd_softc *softc;
412
413 softc = (struct cd_softc *)periph->softc;
414
415 xpt_print(periph->path, "removing device entry\n");
416
417 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
418 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
419 xpt_print(periph->path, "can't remove sysctl context\n");
420 }
421
422 /*
423 * In the queued, non-active case, the device in question
424 * has already been removed from the changer run queue. Since this
425 * device is active, we need to de-activate it, and schedule
426 * another device to run. (if there is another one to run)
427 */
428 if ((softc->flags & CD_FLAG_CHANGER)
429 && (softc->flags & CD_FLAG_ACTIVE)) {
--- 13 unchanged lines hidden (view full) ---
443 * bogus pointer reference.
444 *
445 * The long timeout doesn't really matter, since we
446 * decrement the qfrozen_cnt to indicate that there is
447 * nothing in the active slot now. Therefore, there won't
448 * be any bogus pointer references there.
449 */
450 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
451 callout_stop(&softc->changer->short_handle);
452 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
453 }
454 softc->changer->devq.qfrozen_cnt--;
455 softc->changer->flags |= CHANGER_MANUAL_CALL;
456 cdrunchangerqueue(softc->changer);
457 }
458
459 /*
--- 4 unchanged lines hidden (view full) ---
464 && (--softc->changer->num_devices == 0)) {
465
466 /*
467 * Theoretically, there shouldn't be any timeouts left, but
468 * I'm not completely sure that that will be the case. So,
469 * it won't hurt to check and see if there are any left.
470 */
471 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
472 callout_stop(&softc->changer->long_handle);
473 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
474 }
475
476 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
477 callout_stop(&softc->changer->short_handle);
478 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
479 }
480
481 mtx_lock(&changerq_mtx);
482 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
483 changer_links);
484 num_changers--;
485 mtx_unlock(&changerq_mtx);
486 xpt_print(periph->path, "removing changer entry\n");
487 free(softc->changer, M_DEVBUF);
488 }
489 disk_destroy(softc->disk);
490 free(softc, M_DEVBUF);
491}
492
493static void
494cdasync(void *callback_arg, u_int32_t code,
495 struct cam_path *path, void *arg)
496{
497 struct cam_periph *periph;
498
--- 30 unchanged lines hidden (view full) ---
529
530 break;
531 }
532 case AC_SENT_BDR:
533 case AC_BUS_RESET:
534 {
535 struct cd_softc *softc;
536 struct ccb_hdr *ccbh;
537
538 softc = (struct cd_softc *)periph->softc;
539 /*
540 * Don't fail on the expected unit attention
541 * that will occur.
542 */
543 softc->flags |= CD_FLAG_RETRY_UA;
544 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
545 ccbh->ccb_state |= CD_CCB_RETRY_UA;
546 /* FALLTHROUGH */
547 }
548 default:
549 cam_periph_async(periph, code, path, arg);
550 break;
551 }
552}
553
554static void
555cdsysctlinit(void *context, int pending)
556{
557 struct cam_periph *periph;
558 struct cd_softc *softc;
559 char tmpstr[80], tmpstr2[80];
560
561 periph = (struct cam_periph *)context;
562 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
563 return;
564
565 softc = (struct cd_softc *)periph->softc;
566 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
567 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
568
569 mtx_lock(&Giant);
570
571 sysctl_ctx_init(&softc->sysctl_ctx);
572 softc->flags |= CD_FLAG_SCTX_INIT;
573 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
574 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
575 tmpstr2, CTLFLAG_RD, 0, tmpstr);
576
577 if (softc->sysctl_tree == NULL) {
578 printf("cdsysctlinit: unable to allocate sysctl tree\n");
579 mtx_unlock(&Giant);
580 cam_periph_release(periph);
581 return;
582 }
583
584 /*
585 * Now register the sysctl handler, so the user can the value on
586 * the fly.
587 */
588 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
589 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
590 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
591 "Minimum CDB size");
592
593 mtx_unlock(&Giant);
594 cam_periph_release(periph);
595}
596
597/*
598 * We have a handler function for this so we can check the values when the
599 * user sets them, instead of every time we look at them.
600 */
601static int
602cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
--- 117 unchanged lines hidden (view full) ---
720 * yet. Unlike other SCSI peripheral drivers, we explicitly set
721 * the device type here to be CDROM, rather than just ORing in
722 * the device type. This is because this driver can attach to either
723 * CDROM or WORM devices, and we want this peripheral driver to
724 * show up in the devstat list as a CD peripheral driver, not a
725 * WORM peripheral driver. WORM drives will also have the WORM
726 * driver attached to them.
727 */
728 cam_periph_unlock(periph);
729 softc->disk = disk_alloc();
730 softc->disk->d_devstat = devstat_new_entry("cd",
731 periph->unit_number, 0,
732 DEVSTAT_BS_UNAVAILABLE,
733 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
734 DEVSTAT_PRIORITY_CD);
735 softc->disk->d_open = cdopen;
736 softc->disk->d_close = cdclose;
737 softc->disk->d_strategy = cdstrategy;
738 softc->disk->d_ioctl = cdioctl;
739 softc->disk->d_name = "cd";
740 softc->disk->d_unit = periph->unit_number;
741 softc->disk->d_drv1 = periph;
742 softc->disk->d_flags = 0;
743 disk_create(softc->disk, DISK_VERSION);
744 cam_periph_lock(periph);
745
746 /*
747 * Add an async callback so that we get
748 * notified if this device goes away.
749 */
750 xpt_setup_ccb(&csa.ccb_h, periph->path,
751 /* priority */ 5);
752 csa.ccb_h.func_code = XPT_SASYNC_CB;
--- 18 unchanged lines hidden (view full) ---
771 struct cam_periph *nperiph;
772 struct cam_path *path;
773 cam_status status;
774 int found;
775
776 /* Set the changer flag in the current device's softc */
777 softc->flags |= CD_FLAG_CHANGER;
778
779 /*
780 * Now, look around for an existing changer device with the
781 * same path and target ID as the current device.
782 */
783 mtx_lock(&changerq_mtx);
784 for (found = 0,
785 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
786 nchanger != NULL;
787 nchanger = STAILQ_NEXT(nchanger, changer_links)){
788 if ((nchanger->path_id == cgd->ccb_h.path_id)
789 && (nchanger->target_id == cgd->ccb_h.target_id)) {
790 found = 1;
791 break;
792 }
793 }
794 mtx_unlock(&changerq_mtx);
795
796 /*
797 * If we found a matching entry, just add this device to
798 * the list of devices on this changer.
799 */
800 if (found == 1) {
801 struct chdevlist *chlunhead;
802
--- 89 unchanged lines hidden (view full) ---
892
893 if (camq_init(&nchanger->devq, 1) != 0) {
894 softc->flags &= ~CD_FLAG_CHANGER;
895 printf("cdregister: changer support "
896 "disabled\n");
897 goto cdregisterexit;
898 }
899
900 nchanger->path_id = cgd->ccb_h.path_id;
901 nchanger->target_id = cgd->ccb_h.target_id;
902
903 /* this is superfluous, but it makes things clearer */
904 nchanger->num_devices = 0;
905
906 STAILQ_INIT(&nchanger->chluns);
907
908 callout_init_mtx(&nchanger->long_handle,
909 periph->sim->mtx, 0);
910 callout_init_mtx(&nchanger->short_handle,
911 periph->sim->mtx, 0);
912
913 mtx_lock(&changerq_mtx);
914 num_changers++;
915 STAILQ_INSERT_TAIL(&changerq, nchanger,
916 changer_links);
917 mtx_unlock(&changerq_mtx);
918
919 /*
920 * Create a path with lun id 0, and see if we can
921 * find a matching device
922 */
923 status = xpt_create_path(&path, /*periph*/ periph,
924 cgd->ccb_h.path_id,
925 cgd->ccb_h.target_id, 0);
--- 45 unchanged lines hidden (view full) ---
971 }
972 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
973 changer_links);
974 }
975 }
976
977cdregisterexit:
978
979 /*
980 * Refcount and block open attempts until we are setup
981 * Can't block
982 */
983 (void)cam_periph_hold(periph, PRIBIO);
984
985 if ((softc->flags & CD_FLAG_CHANGER) == 0)
986 xpt_schedule(periph, /*priority*/5);
987 else
988 cdschedule(periph, /*priority*/ 5);
989
990 return(CAM_REQ_CMP);
991}
992
993static int
994cdopen(struct disk *dp)
995{
996 struct cam_periph *periph;
997 struct cd_softc *softc;
998 int error;
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 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1007 return(ENXIO);
1008
1009 cam_periph_lock(periph);
1010
1011 if (softc->flags & CD_FLAG_INVALID) {
1012 cam_periph_unlock(periph);
1013 cam_periph_release(periph);
1014 return(ENXIO);
1015 }
1016
1017 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1018 cam_periph_unlock(periph);
1019 cam_periph_release(periph);
1020 return (error);
1021 }
1022
1023 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1024 if (softc->flags & CD_FLAG_OPEN)
1025 cam_periph_release(periph);
1026 else
1027 softc->flags |= CD_FLAG_OPEN;
1028
1029 /*
1030 * Check for media, and set the appropriate flags. We don't bail
1031 * if we don't have media, but then we don't allow anything but the
1032 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1033 */
1034 cdcheckmedia(periph);
1035
1036 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1037 cam_periph_unhold(periph);
1038 cam_periph_unlock(periph);
1039
1040 return (0);
1041}
1042
1043static int
1044cdclose(struct disk *dp)
1045{
1046 struct cam_periph *periph;
1047 struct cd_softc *softc;
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 cam_periph_lock(periph);
1056 cam_periph_hold(periph, PRIBIO);
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 */
1065 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
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|CD_FLAG_OPEN);
1071
1072 cam_periph_unhold(periph);
1073 cam_periph_unlock(periph);
1074 cam_periph_release(periph);
1075
1076 return (0);
1077}
1078
1079static void
1080cdshorttimeout(void *arg)
1081{
1082 struct cdchanger *changer;
1083
1084 changer = (struct cdchanger *)arg;
1085
1086 /* Always clear the short timeout flag, since that's what we're in */
1087 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1088
1089 /*
1090 * Check to see if there is any more pending or outstanding I/O for
1091 * this device. If not, move it out of the active slot.
1092 */
1093 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1094 && (changer->cur_device->outstanding_cmds == 0)) {
1095 changer->flags |= CHANGER_MANUAL_CALL;
1096 cdrunchangerqueue(changer);
1097 }
1098}
1099
1100/*
1101 * This is a wrapper for xpt_schedule. It only applies to changers.
1102 */
1103static void
1104cdschedule(struct cam_periph *periph, int priority)
1105{
1106 struct cd_softc *softc;
1107
1108 softc = (struct cd_softc *)periph->softc;
1109
1110 /*
1111 * If this device isn't currently queued, and if it isn't
1112 * the active device, then we queue this device and run the
1113 * changer queue if there is no timeout scheduled to do it.
1114 * If this device is the active device, just schedule it
1115 * to run again. If this device is queued, there should be
--- 21 unchanged lines hidden (view full) ---
1137 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1138 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1139 softc->changer->flags |= CHANGER_MANUAL_CALL;
1140 cdrunchangerqueue(softc->changer);
1141 }
1142 } else if ((softc->flags & CD_FLAG_ACTIVE)
1143 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1144 xpt_schedule(periph, priority);
1145}
1146
1147static void
1148cdrunchangerqueue(void *arg)
1149{
1150 struct cd_softc *softc;
1151 struct cdchanger *changer;
1152 int called_from_timeout;
1153
1154 changer = (struct cdchanger *)arg;
1155
1156 /*
1157 * If we have NOT been called from cdstrategy() or cddone(), and
1158 * instead from a timeout routine, go ahead and clear the
1159 * timeout flag.
1160 */
1161 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1162 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1163 called_from_timeout = 1;
1164 } else
1165 called_from_timeout = 0;
1166
1167 /* Always clear the manual call flag */
1168 changer->flags &= ~CHANGER_MANUAL_CALL;
1169
1170 /* nothing to do if the queue is empty */
1171 if (changer->devq.entries <= 0) {
1172 return;
1173 }
1174
1175 /*
1176 * If the changer queue is frozen, that means we have an active
1177 * device.
1178 */
1179 if (changer->devq.qfrozen_cnt > 0) {
1180
1181 /*
1182 * We always need to reset the frozen count and clear the
1183 * active flag.
1184 */
1185 changer->devq.qfrozen_cnt--;
1186 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1187 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1188
1189 if (changer->cur_device->outstanding_cmds > 0) {
1190 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1191 changer->cur_device->bufs_left =
1192 changer->cur_device->outstanding_cmds;
1193 if (called_from_timeout) {
1194 callout_reset(&changer->long_handle,
1195 changer_max_busy_seconds * hz,
1196 cdrunchangerqueue, changer);
1197 changer->flags |= CHANGER_TIMEOUT_SCHED;
1198 }
1199 return;
1200 }
1201
1202 /*
1203 * Check to see whether the current device has any I/O left
1204 * to do. If so, requeue it at the end of the queue. If
1205 * not, there is no need to requeue it.
1206 */
1207 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1208
1209 changer->cur_device->pinfo.generation =
1210 ++changer->devq.generation;
--- 13 unchanged lines hidden (view full) ---
1224 wakeup(&softc->changer);
1225 xpt_schedule(softc->periph, /*priority*/ 1);
1226
1227 /*
1228 * Get rid of any pending timeouts, and set a flag to schedule new
1229 * ones so this device gets its full time quantum.
1230 */
1231 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1232 callout_stop(&changer->long_handle);
1233 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1234 }
1235
1236 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1237 callout_stop(&changer->short_handle);
1238 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1239 }
1240
1241 /*
1242 * We need to schedule timeouts, but we only do this after the
1243 * first transaction has completed. This eliminates the changer
1244 * switch time.
1245 */
1246 changer->flags |= CHANGER_NEED_TIMEOUT;
1247}
1248
1249static void
1250cdchangerschedule(struct cd_softc *softc)
1251{
1252 struct cdchanger *changer;
1253
1254 changer = softc->changer;
1255
1256 /*
1257 * If this is a changer, and this is the current device,
1258 * and this device has at least the minimum time quantum to
1259 * run, see if we can switch it out.
1260 */
1261 if ((softc->flags & CD_FLAG_ACTIVE)
--- 28 unchanged lines hidden (view full) ---
1290 && (softc->flags & CD_FLAG_ACTIVE)) {
1291
1292 /*
1293 * Now that the first transaction to this
1294 * particular device has completed, we can go ahead
1295 * and schedule our timeouts.
1296 */
1297 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1298 callout_reset(&changer->long_handle,
1299 changer_max_busy_seconds * hz,
1300 cdrunchangerqueue, changer);
1301 changer->flags |= CHANGER_TIMEOUT_SCHED;
1302 } else
1303 printf("cdchangerschedule: already have a long"
1304 " timeout!\n");
1305
1306 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1307 callout_reset(&changer->short_handle,
1308 changer_min_busy_seconds * hz,
1309 cdshorttimeout, changer);
1310 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1311 } else
1312 printf("cdchangerschedule: already have a short "
1313 "timeout!\n");
1314
1315 /*
1316 * We just scheduled timeouts, no need to schedule
1317 * more.
1318 */
1319 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1320
1321 }
1322}
1323
1324static int
1325cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1326 u_int32_t cam_flags,
1327 u_int32_t sense_flags),
1328 u_int32_t cam_flags, u_int32_t sense_flags)
1329{
--- 12 unchanged lines hidden (view full) ---
1342
1343 return(error);
1344}
1345
1346static union ccb *
1347cdgetccb(struct cam_periph *periph, u_int32_t priority)
1348{
1349 struct cd_softc *softc;
1350
1351 softc = (struct cd_softc *)periph->softc;
1352
1353 if (softc->flags & CD_FLAG_CHANGER) {
1354 /*
1355 * This should work the first time this device is woken up,
1356 * but just in case it doesn't, we use a while loop.
1357 */
1358 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1359 /*
1360 * If this changer isn't already queued, queue it up.
1361 */
--- 6 unchanged lines hidden (view full) ---
1368 }
1369 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1370 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1371 && ((softc->changer->flags
1372 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1373 softc->changer->flags |= CHANGER_MANUAL_CALL;
1374 cdrunchangerqueue(softc->changer);
1375 } else
1376 msleep(&softc->changer, periph->sim->mtx,
1377 PRIBIO, "cgticb", 0);
1378 }
1379 }
1380 return(cam_periph_getccb(periph, priority));
1381}
1382
1383
1384/*
1385 * Actually translate the requested transfer into one the physical driver
1386 * can understand. The transfer is described by a buf and will include
1387 * only one physical transfer.
1388 */
1389static void
1390cdstrategy(struct bio *bp)
1391{
1392 struct cam_periph *periph;
1393 struct cd_softc *softc;
1394
1395 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1396 if (periph == NULL) {
1397 biofinish(bp, NULL, ENXIO);
1398 return;
1399 }
1400
1401 cam_periph_lock(periph);
1402 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1403
1404 softc = (struct cd_softc *)periph->softc;
1405
1406 /*
1407 * If the device has been made invalid, error out
1408 */
1409 if ((softc->flags & CD_FLAG_INVALID)) {
1410 cam_periph_unlock(periph);
1411 biofinish(bp, NULL, ENXIO);
1412 return;
1413 }
1414
1415 /*
1416 * If we don't have valid media, look for it before trying to
1417 * schedule the I/O.
1418 */
1419 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1420 int error;
1421
1422 error = cdcheckmedia(periph);
1423 if (error != 0) {
1424 cam_periph_unlock(periph);
1425 biofinish(bp, NULL, error);
1426 return;
1427 }
1428 }
1429
1430 /*
1431 * Place it in the queue of disk activities for this disk
1432 */
1433 bioq_disksort(&softc->bio_queue, bp);
1434
1435 /*
1436 * Schedule ourselves for performing the work. We do things
1437 * differently for changers.
1438 */
1439 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1440 xpt_schedule(periph, /* XXX priority */1);
1441 else
1442 cdschedule(periph, /* priority */ 1);
1443
1444 cam_periph_unlock(periph);
1445 return;
1446}
1447
1448static void
1449cdstart(struct cam_periph *periph, union ccb *start_ccb)
1450{
1451 struct cd_softc *softc;
1452 struct bio *bp;
1453 struct ccb_scsiio *csio;
1454 struct scsi_read_capacity_data *rcap;
1455
1456 softc = (struct cd_softc *)periph->softc;
1457
1458 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1459
1460 switch (softc->state) {
1461 case CD_STATE_NORMAL:
1462 {
1463 bp = bioq_first(&softc->bio_queue);
1464 if (periph->immediate_priority <= periph->pinfo.priority) {
1465 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1466
1467 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1468 periph_links.sle);
1469 periph->immediate_priority = CAM_PRIORITY_NONE;
1470 wakeup(&periph->ccb_list);
1471 } else if (bp == NULL) {
1472 xpt_release_ccb(start_ccb);
1473 } else {
1474 bioq_remove(&softc->bio_queue, bp);
1475
1476 devstat_start_transaction_bio(softc->disk->d_devstat, bp);
1477
1478 scsi_read_write(&start_ccb->csio,
1479 /*retries*/4,
--- 7 unchanged lines hidden (view full) ---
1487 bp->bio_bcount / softc->params.blksize,
1488 /* data_ptr */ bp->bio_data,
1489 /* dxfer_len */ bp->bio_bcount,
1490 /* sense_len */ SSD_FULL_SIZE,
1491 /* timeout */ 30000);
1492 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1493
1494
1495 LIST_INSERT_HEAD(&softc->pending_ccbs,
1496 &start_ccb->ccb_h, periph_links.le);
1497 softc->outstanding_cmds++;
1498
1499 /* We expect a unit attention from this device */
1500 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1501 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1502 softc->flags &= ~CD_FLAG_RETRY_UA;
1503 }
1504
1505 start_ccb->ccb_h.ccb_bp = bp;
1506 bp = bioq_first(&softc->bio_queue);
1507
1508 xpt_action(start_ccb);
1509 }
1510 if (bp != NULL) {
1511 /* Have more work to do, so ensure we stay scheduled */
1512 xpt_schedule(periph, /* XXX priority */1);
1513 }
1514 break;
--- 37 unchanged lines hidden (view full) ---
1552 softc = (struct cd_softc *)periph->softc;
1553 csio = &done_ccb->csio;
1554
1555 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1556 case CD_CCB_BUFFER_IO:
1557 {
1558 struct bio *bp;
1559 int error;
1560
1561 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1562 error = 0;
1563
1564 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1565 int sf;
1566
1567 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
--- 7 unchanged lines hidden (view full) ---
1575 * A retry was scheuled, so
1576 * just return.
1577 */
1578 return;
1579 }
1580 }
1581
1582 if (error != 0) {
1583 xpt_print(periph->path,
1584 "cddone: got error %#x back\n", error);
1585 bioq_flush(&softc->bio_queue, NULL, EIO);
1586 bp->bio_resid = bp->bio_bcount;
1587 bp->bio_error = error;
1588 bp->bio_flags |= BIO_ERROR;
1589 cam_release_devq(done_ccb->ccb_h.path,
1590 /*relsim_flags*/0,
1591 /*reduction*/0,
1592 /*timeout*/0,
1593 /*getcount_only*/0);
--- 6 unchanged lines hidden (view full) ---
1600 * Short transfer ???
1601 * XXX: not sure this is correct for partial
1602 * transfers at EOM
1603 */
1604 bp->bio_flags |= BIO_ERROR;
1605 }
1606 }
1607
1608 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1609 softc->outstanding_cmds--;
1610
1611 if (softc->flags & CD_FLAG_CHANGER)
1612 cdchangerschedule(softc);
1613
1614 biofinish(bp, NULL, 0);
1615 break;
1616 }
1617 case CD_CCB_PROBE:
--- 174 unchanged lines hidden (view full) ---
1792 * Since our peripheral may be invalidated by an error
1793 * above or an external event, we must release our CCB
1794 * before releasing the probe lock on the peripheral.
1795 * The peripheral will only go away once the last lock
1796 * is removed, and we need it around for the CCB release
1797 * operation.
1798 */
1799 xpt_release_ccb(done_ccb);
1800 cam_periph_unhold(periph);
1801 return;
1802 }
1803 case CD_CCB_WAITING:
1804 {
1805 /* Caller will release the CCB */
1806 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1807 ("trying to wakeup ccbwait\n"));
1808
--- 36 unchanged lines hidden (view full) ---
1845}
1846
1847static int
1848cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1849{
1850
1851 struct cam_periph *periph;
1852 struct cd_softc *softc;
1853 int nocopyout, error = 0;
1854
1855 periph = (struct cam_periph *)dp->d_drv1;
1856 if (periph == NULL)
1857 return(ENXIO);
1858
1859 cam_periph_lock(periph);
1860 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1861
1862 softc = (struct cd_softc *)periph->softc;
1863
1864 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1865 ("trying to do ioctl %#lx\n", cmd));
1866
1867 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1868 cam_periph_unlock(periph);
1869 cam_periph_release(periph);
1870 return (error);
1871 }
1872
1873 /*
1874 * If we don't have media loaded, check for it. If still don't
1875 * have media loaded, we can only do a load or eject.
1876 *
1877 * We only care whether media is loaded if this is a cd-specific ioctl
1878 * (thus the IOCGROUP check below). Note that this will break if
1879 * anyone adds any ioctls into the switch statement below that don't
1880 * have their ioctl group set to 'c'.
1881 */
1882 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1883 && ((cmd != CDIOCCLOSE)
1884 && (cmd != CDIOCEJECT))
1885 && (IOCGROUP(cmd) == 'c')) {
1886 error = cdcheckmedia(periph);
1887 }
1888 /*
1889 * Drop the lock here so later mallocs can use WAITOK. The periph
1890 * is essentially locked still with the cam_periph_hold call above.
1891 */
1892 cam_periph_unlock(periph);
1893 if (error != 0)
1894 return (error);
1895
1896 nocopyout = 0;
1897 switch (cmd) {
1898
1899 case CDIOCPLAYTRACKS:
1900 {
1901 struct ioc_play_track *args
1902 = (struct ioc_play_track *) addr;
1903 struct cd_mode_params params;
1904 union cd_pages *page;
1905
1906 params.alloc_len = sizeof(union cd_mode_data_6_10);
1907 params.mode_buf = malloc(params.alloc_len, M_TEMP,
1908 M_WAITOK | M_ZERO);
1909
1910 cam_periph_lock(periph);
1911 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1912 ("trying to do CDIOCPLAYTRACKS\n"));
1913
1914 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1915 if (error) {
1916 free(params.mode_buf, M_TEMP);
1917 cam_periph_unlock(periph);
1918 break;
1919 }
1920 page = cdgetpage(¶ms);
1921
1922 page->audio.flags &= ~CD_PA_SOTC;
1923 page->audio.flags |= CD_PA_IMMED;
1924 error = cdsetmode(periph, ¶ms);
1925 free(params.mode_buf, M_TEMP);
1926 if (error) {
1927 cam_periph_unlock(periph);
1928 break;
1929 }
1930
1931 /*
1932 * This was originally implemented with the PLAY
1933 * AUDIO TRACK INDEX command, but that command was
1934 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1935 * drives support it but ATAPI and ATAPI-derivative
1936 * drives don't seem to support it. So we keep a
1937 * cache of the table of contents and translate
--- 45 unchanged lines hidden (view full) ---
1983 bin2bcd(args->end_track);
1984 }
1985 error = cdplaytracks(periph,
1986 args->start_track,
1987 args->start_index,
1988 args->end_track,
1989 args->end_index);
1990 }
1991 cam_periph_unlock(periph);
1992 }
1993 break;
1994 case CDIOCPLAYMSF:
1995 {
1996 struct ioc_play_msf *args
1997 = (struct ioc_play_msf *) addr;
1998 struct cd_mode_params params;
1999 union cd_pages *page;
2000
2001 params.alloc_len = sizeof(union cd_mode_data_6_10);
2002 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2003 M_WAITOK | M_ZERO);
2004
2005 cam_periph_lock(periph);
2006 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2007 ("trying to do CDIOCPLAYMSF\n"));
2008
2009 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2010 if (error) {
2011 free(params.mode_buf, M_TEMP);
2012 cam_periph_unlock(periph);
2013 break;
2014 }
2015 page = cdgetpage(¶ms);
2016
2017 page->audio.flags &= ~CD_PA_SOTC;
2018 page->audio.flags |= CD_PA_IMMED;
2019 error = cdsetmode(periph, ¶ms);
2020 free(params.mode_buf, M_TEMP);
2021 if (error) {
2022 cam_periph_unlock(periph);
2023 break;
2024 }
2025 error = cdplaymsf(periph,
2026 args->start_m,
2027 args->start_s,
2028 args->start_f,
2029 args->end_m,
2030 args->end_s,
2031 args->end_f);
2032 cam_periph_unlock(periph);
2033 }
2034 break;
2035 case CDIOCPLAYBLOCKS:
2036 {
2037 struct ioc_play_blocks *args
2038 = (struct ioc_play_blocks *) 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_periph_lock(periph);
2047 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2048 ("trying to do CDIOCPLAYBLOCKS\n"));
2049
2050
2051 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2052 if (error) {
2053 free(params.mode_buf, M_TEMP);
2054 cam_periph_unlock(periph);
2055 break;
2056 }
2057 page = cdgetpage(¶ms);
2058
2059 page->audio.flags &= ~CD_PA_SOTC;
2060 page->audio.flags |= CD_PA_IMMED;
2061 error = cdsetmode(periph, ¶ms);
2062 free(params.mode_buf, M_TEMP);
2063 if (error) {
2064 cam_periph_unlock(periph);
2065 break;
2066 }
2067 error = cdplay(periph, args->blk, args->len);
2068 cam_periph_unlock(periph);
2069 }
2070 break;
2071 case CDIOCREADSUBCHANNEL_SYSSPACE:
2072 nocopyout = 1;
2073 /* Fallthrough */
2074 case CDIOCREADSUBCHANNEL:
2075 {
2076 struct ioc_read_subchannel *args
2077 = (struct ioc_read_subchannel *) addr;
2078 struct cd_sub_channel_info *data;
2079 u_int32_t len = args->data_len;
2080
2081 data = malloc(sizeof(struct cd_sub_channel_info),
2082 M_TEMP, M_WAITOK);
2083
2084 cam_periph_lock(periph);
2085 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2086 ("trying to do CDIOCREADSUBCHANNEL\n"));
2087
2088 if ((len > sizeof(struct cd_sub_channel_info)) ||
2089 (len < sizeof(struct cd_sub_channel_header))) {
2090 printf(
2091 "scsi_cd: cdioctl: "
2092 "cdioreadsubchannel: error, len=%d\n",
2093 len);
2094 error = EINVAL;
2095 free(data, M_TEMP);
2096 cam_periph_unlock(periph);
2097 break;
2098 }
2099
2100 if (softc->quirks & CD_Q_BCD_TRACKS)
2101 args->track = bin2bcd(args->track);
2102
2103 error = cdreadsubchannel(periph, args->address_format,
2104 args->data_format, args->track, data, len);
2105
2106 if (error) {
2107 free(data, M_TEMP);
2108 cam_periph_unlock(periph);
2109 break;
2110 }
2111 if (softc->quirks & CD_Q_BCD_TRACKS)
2112 data->what.track_info.track_number =
2113 bcd2bin(data->what.track_info.track_number);
2114 len = min(len, ((data->header.data_len[0] << 8) +
2115 data->header.data_len[1] +
2116 sizeof(struct cd_sub_channel_header)));
2117 cam_periph_unlock(periph);
2118 if (nocopyout == 0) {
2119 if (copyout(data, args->data, len) != 0) {
2120 error = EFAULT;
2121 }
2122 } else {
2123 bcopy(data, args->data, len);
2124 }
2125 free(data, M_TEMP);
2126 }
2127 break;
2128
2129 case CDIOREADTOCHEADER:
2130 {
2131 struct ioc_toc_header *th;
2132
2133 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
2134 M_WAITOK);
2135
2136 cam_periph_lock(periph);
2137 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2138 ("trying to do CDIOREADTOCHEADER\n"));
2139
2140 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2141 sizeof (*th), /*sense_flags*/0);
2142 if (error) {
2143 free(th, M_TEMP);
2144 cam_periph_unlock(periph);
2145 break;
2146 }
2147 if (softc->quirks & CD_Q_BCD_TRACKS) {
2148 /* we are going to have to convert the BCD
2149 * encoding on the cd to what is expected
2150 */
2151 th->starting_track =
2152 bcd2bin(th->starting_track);
2153 th->ending_track = bcd2bin(th->ending_track);
2154 }
2155 th->len = ntohs(th->len);
2156 bcopy(th, addr, sizeof(*th));
2157 free(th, M_TEMP);
2158 cam_periph_unlock(periph);
2159 }
2160 break;
2161 case CDIOREADTOCENTRYS:
2162 {
2163 struct cd_tocdata *data;
2164 struct cd_toc_single *lead;
2165 struct ioc_read_toc_entry *te =
2166 (struct ioc_read_toc_entry *) addr;
2167 struct ioc_toc_header *th;
2168 u_int32_t len, readlen, idx, num;
2169 u_int32_t starting_track = te->starting_track;
2170
2171 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2172 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK);
2173
2174 cam_periph_lock(periph);
2175 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2176 ("trying to do CDIOREADTOCENTRYS\n"));
2177
2178 if (te->data_len < sizeof(struct cd_toc_entry)
2179 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2180 || (te->address_format != CD_MSF_FORMAT
2181 && te->address_format != CD_LBA_FORMAT)) {
2182 error = EINVAL;
2183 printf("scsi_cd: error in readtocentries, "
2184 "returning EINVAL\n");
2185 free(data, M_TEMP);
2186 free(lead, M_TEMP);
2187 cam_periph_unlock(periph);
2188 break;
2189 }
2190
2191 th = &data->header;
2192 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2193 sizeof (*th), /*sense_flags*/0);
2194 if (error) {
2195 free(data, M_TEMP);
2196 free(lead, M_TEMP);
2197 cam_periph_unlock(periph);
2198 break;
2199 }
2200
2201 if (softc->quirks & CD_Q_BCD_TRACKS) {
2202 /* we are going to have to convert the BCD
2203 * encoding on the cd to what is expected
2204 */
2205 th->starting_track =
--- 6 unchanged lines hidden (view full) ---
2212 else if (starting_track == LEADOUT)
2213 starting_track = th->ending_track + 1;
2214 else if (starting_track < th->starting_track ||
2215 starting_track > th->ending_track + 1) {
2216 printf("scsi_cd: error in readtocentries, "
2217 "returning EINVAL\n");
2218 free(data, M_TEMP);
2219 free(lead, M_TEMP);
2220 cam_periph_unlock(periph);
2221 error = EINVAL;
2222 break;
2223 }
2224
2225 /* calculate reading length without leadout entry */
2226 readlen = (th->ending_track - starting_track + 1) *
2227 sizeof(struct cd_toc_entry);
2228
--- 5 unchanged lines hidden (view full) ---
2234 readlen = len;
2235 }
2236 if (len > sizeof(data->entries)) {
2237 printf("scsi_cd: error in readtocentries, "
2238 "returning EINVAL\n");
2239 error = EINVAL;
2240 free(data, M_TEMP);
2241 free(lead, M_TEMP);
2242 cam_periph_unlock(periph);
2243 break;
2244 }
2245 num = len / sizeof(struct cd_toc_entry);
2246
2247 if (readlen > 0) {
2248 error = cdreadtoc(periph, te->address_format,
2249 starting_track,
2250 (u_int8_t *)data,
2251 readlen + sizeof (*th),
2252 /*sense_flags*/0);
2253 if (error) {
2254 free(data, M_TEMP);
2255 free(lead, M_TEMP);
2256 cam_periph_unlock(periph);
2257 break;
2258 }
2259 }
2260
2261 /* make leadout entry if needed */
2262 idx = starting_track + num - 1;
2263 if (softc->quirks & CD_Q_BCD_TRACKS)
2264 th->ending_track = bcd2bin(th->ending_track);
2265 if (idx == th->ending_track + 1) {
2266 error = cdreadtoc(periph, te->address_format,
2267 LEADOUT, (u_int8_t *)lead,
2268 sizeof(*lead),
2269 /*sense_flags*/0);
2270 if (error) {
2271 free(data, M_TEMP);
2272 free(lead, M_TEMP);
2273 cam_periph_unlock(periph);
2274 break;
2275 }
2276 data->entries[idx - starting_track] =
2277 lead->entry;
2278 }
2279 if (softc->quirks & CD_Q_BCD_TRACKS) {
2280 for (idx = 0; idx < num - 1; idx++) {
2281 data->entries[idx].track =
2282 bcd2bin(data->entries[idx].track);
2283 }
2284 }
2285
2286 cam_periph_unlock(periph);
2287 error = copyout(data->entries, te->data, len);
2288 free(data, M_TEMP);
2289 free(lead, M_TEMP);
2290 }
2291 break;
2292 case CDIOREADTOCENTRY:
2293 {
2294 struct cd_toc_single *data;
2295 struct ioc_read_toc_single_entry *te =
2296 (struct ioc_read_toc_single_entry *) addr;
2297 struct ioc_toc_header *th;
2298 u_int32_t track;
2299
2300 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2301
2302 cam_periph_lock(periph);
2303 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2304 ("trying to do CDIOREADTOCENTRY\n"));
2305
2306 if (te->address_format != CD_MSF_FORMAT
2307 && te->address_format != CD_LBA_FORMAT) {
2308 printf("error in readtocentry, "
2309 " returning EINVAL\n");
2310 free(data, M_TEMP);
2311 error = EINVAL;
2312 cam_periph_unlock(periph);
2313 break;
2314 }
2315
2316 th = &data->header;
2317 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2318 sizeof (*th), /*sense_flags*/0);
2319 if (error) {
2320 free(data, M_TEMP);
2321 cam_periph_unlock(periph);
2322 break;
2323 }
2324
2325 if (softc->quirks & CD_Q_BCD_TRACKS) {
2326 /* we are going to have to convert the BCD
2327 * encoding on the cd to what is expected
2328 */
2329 th->starting_track =
--- 6 unchanged lines hidden (view full) ---
2336 else if (track == LEADOUT)
2337 /* OK */;
2338 else if (track < th->starting_track ||
2339 track > th->ending_track + 1) {
2340 printf("error in readtocentry, "
2341 " returning EINVAL\n");
2342 free(data, M_TEMP);
2343 error = EINVAL;
2344 cam_periph_unlock(periph);
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 cam_periph_unlock(periph);
2354 break;
2355 }
2356
2357 if (softc->quirks & CD_Q_BCD_TRACKS)
2358 data->entry.track = bcd2bin(data->entry.track);
2359 bcopy(&data->entry, &te->entry,
2360 sizeof(struct cd_toc_entry));
2361 free(data, M_TEMP);
2362 cam_periph_unlock(periph);
2363 }
2364 break;
2365 case CDIOCSETPATCH:
2366 {
2367 struct ioc_patch *arg = (struct ioc_patch *)addr;
2368 struct cd_mode_params params;
2369 union cd_pages *page;
2370
2371 params.alloc_len = sizeof(union cd_mode_data_6_10);
2372 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2373 M_WAITOK | M_ZERO);
2374
2375 cam_periph_lock(periph);
2376 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2377 ("trying to do CDIOCSETPATCH\n"));
2378
2379 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2380 if (error) {
2381 free(params.mode_buf, M_TEMP);
2382 cam_periph_unlock(periph);
2383 break;
2384 }
2385 page = cdgetpage(¶ms);
2386
2387 page->audio.port[LEFT_PORT].channels =
2388 arg->patch[0];
2389 page->audio.port[RIGHT_PORT].channels =
2390 arg->patch[1];
2391 page->audio.port[2].channels = arg->patch[2];
2392 page->audio.port[3].channels = arg->patch[3];
2393 error = cdsetmode(periph, ¶ms);
2394 free(params.mode_buf, M_TEMP);
2395 cam_periph_unlock(periph);
2396 }
2397 break;
2398 case CDIOCGETVOL:
2399 {
2400 struct ioc_vol *arg = (struct ioc_vol *) addr;
2401 struct cd_mode_params params;
2402 union cd_pages *page;
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
2408 cam_periph_lock(periph);
2409 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2410 ("trying to do CDIOCGETVOL\n"));
2411
2412 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2413 if (error) {
2414 free(params.mode_buf, M_TEMP);
2415 cam_periph_unlock(periph);
2416 break;
2417 }
2418 page = cdgetpage(¶ms);
2419
2420 arg->vol[LEFT_PORT] =
2421 page->audio.port[LEFT_PORT].volume;
2422 arg->vol[RIGHT_PORT] =
2423 page->audio.port[RIGHT_PORT].volume;
2424 arg->vol[2] = page->audio.port[2].volume;
2425 arg->vol[3] = page->audio.port[3].volume;
2426 free(params.mode_buf, M_TEMP);
2427 cam_periph_unlock(periph);
2428 }
2429 break;
2430 case CDIOCSETVOL:
2431 {
2432 struct ioc_vol *arg = (struct ioc_vol *) addr;
2433 struct cd_mode_params params;
2434 union cd_pages *page;
2435
2436 params.alloc_len = sizeof(union cd_mode_data_6_10);
2437 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2438 M_WAITOK | M_ZERO);
2439
2440 cam_periph_lock(periph);
2441 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2442 ("trying to do CDIOCSETVOL\n"));
2443
2444 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2445 if (error) {
2446 free(params.mode_buf, M_TEMP);
2447 cam_periph_unlock(periph);
2448 break;
2449 }
2450 page = cdgetpage(¶ms);
2451
2452 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2453 page->audio.port[LEFT_PORT].volume =
2454 arg->vol[LEFT_PORT];
2455 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2456 page->audio.port[RIGHT_PORT].volume =
2457 arg->vol[RIGHT_PORT];
2458 page->audio.port[2].volume = arg->vol[2];
2459 page->audio.port[3].volume = arg->vol[3];
2460 error = cdsetmode(periph, ¶ms);
2461 cam_periph_unlock(periph);
2462 free(params.mode_buf, M_TEMP);
2463 }
2464 break;
2465 case CDIOCSETMONO:
2466 {
2467 struct cd_mode_params params;
2468 union cd_pages *page;
2469
2470 params.alloc_len = sizeof(union cd_mode_data_6_10);
2471 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2472 M_WAITOK | M_ZERO);
2473
2474 cam_periph_lock(periph);
2475 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2476 ("trying to do CDIOCSETMONO\n"));
2477
2478 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2479 if (error) {
2480 free(params.mode_buf, M_TEMP);
2481 cam_periph_unlock(periph);
2482 break;
2483 }
2484 page = cdgetpage(¶ms);
2485
2486 page->audio.port[LEFT_PORT].channels =
2487 LEFT_CHANNEL | RIGHT_CHANNEL;
2488 page->audio.port[RIGHT_PORT].channels =
2489 LEFT_CHANNEL | RIGHT_CHANNEL;
2490 page->audio.port[2].channels = 0;
2491 page->audio.port[3].channels = 0;
2492 error = cdsetmode(periph, ¶ms);
2493 cam_periph_unlock(periph);
2494 free(params.mode_buf, M_TEMP);
2495 }
2496 break;
2497 case CDIOCSETSTEREO:
2498 {
2499 struct cd_mode_params params;
2500 union cd_pages *page;
2501
2502 params.alloc_len = sizeof(union cd_mode_data_6_10);
2503 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2504 M_WAITOK | M_ZERO);
2505
2506 cam_periph_lock(periph);
2507 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2508 ("trying to do CDIOCSETSTEREO\n"));
2509
2510 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2511 if (error) {
2512 free(params.mode_buf, M_TEMP);
2513 cam_periph_unlock(periph);
2514 break;
2515 }
2516 page = cdgetpage(¶ms);
2517
2518 page->audio.port[LEFT_PORT].channels =
2519 LEFT_CHANNEL;
2520 page->audio.port[RIGHT_PORT].channels =
2521 RIGHT_CHANNEL;
2522 page->audio.port[2].channels = 0;
2523 page->audio.port[3].channels = 0;
2524 error = cdsetmode(periph, ¶ms);
2525 free(params.mode_buf, M_TEMP);
2526 cam_periph_unlock(periph);
2527 }
2528 break;
2529 case CDIOCSETMUTE:
2530 {
2531 struct cd_mode_params params;
2532 union cd_pages *page;
2533
2534 params.alloc_len = sizeof(union cd_mode_data_6_10);
2535 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2536 M_WAITOK | M_ZERO);
2537
2538 cam_periph_lock(periph);
2539 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2540 ("trying to do CDIOCSETMUTE\n"));
2541
2542 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2543 if (error) {
2544 free(¶ms, M_TEMP);
2545 cam_periph_unlock(periph);
2546 break;
2547 }
2548 page = cdgetpage(¶ms);
2549
2550 page->audio.port[LEFT_PORT].channels = 0;
2551 page->audio.port[RIGHT_PORT].channels = 0;
2552 page->audio.port[2].channels = 0;
2553 page->audio.port[3].channels = 0;
2554 error = cdsetmode(periph, ¶ms);
2555 free(params.mode_buf, M_TEMP);
2556 cam_periph_unlock(periph);
2557 }
2558 break;
2559 case CDIOCSETLEFT:
2560 {
2561 struct cd_mode_params params;
2562 union cd_pages *page;
2563
2564 params.alloc_len = sizeof(union cd_mode_data_6_10);
2565 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2566 M_WAITOK | M_ZERO);
2567
2568 cam_periph_lock(periph);
2569 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2570 ("trying to do CDIOCSETLEFT\n"));
2571
2572 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2573 if (error) {
2574 free(params.mode_buf, M_TEMP);
2575 cam_periph_unlock(periph);
2576 break;
2577 }
2578 page = cdgetpage(¶ms);
2579
2580 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2581 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2582 page->audio.port[2].channels = 0;
2583 page->audio.port[3].channels = 0;
2584 error = cdsetmode(periph, ¶ms);
2585 free(params.mode_buf, M_TEMP);
2586 cam_periph_unlock(periph);
2587 }
2588 break;
2589 case CDIOCSETRIGHT:
2590 {
2591 struct cd_mode_params params;
2592 union cd_pages *page;
2593
2594 params.alloc_len = sizeof(union cd_mode_data_6_10);
2595 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2596 M_WAITOK | M_ZERO);
2597
2598 cam_periph_lock(periph);
2599 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2600 ("trying to do CDIOCSETRIGHT\n"));
2601
2602 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2603 if (error) {
2604 free(params.mode_buf, M_TEMP);
2605 cam_periph_unlock(periph);
2606 break;
2607 }
2608 page = cdgetpage(¶ms);
2609
2610 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2611 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2612 page->audio.port[2].channels = 0;
2613 page->audio.port[3].channels = 0;
2614 error = cdsetmode(periph, ¶ms);
2615 free(params.mode_buf, M_TEMP);
2616 cam_periph_unlock(periph);
2617 }
2618 break;
2619 case CDIOCRESUME:
2620 cam_periph_lock(periph);
2621 error = cdpause(periph, 1);
2622 cam_periph_unlock(periph);
2623 break;
2624 case CDIOCPAUSE:
2625 cam_periph_lock(periph);
2626 error = cdpause(periph, 0);
2627 cam_periph_unlock(periph);
2628 break;
2629 case CDIOCSTART:
2630 cam_periph_lock(periph);
2631 error = cdstartunit(periph, 0);
2632 cam_periph_unlock(periph);
2633 break;
2634 case CDIOCCLOSE:
2635 cam_periph_lock(periph);
2636 error = cdstartunit(periph, 1);
2637 cam_periph_unlock(periph);
2638 break;
2639 case CDIOCSTOP:
2640 cam_periph_lock(periph);
2641 error = cdstopunit(periph, 0);
2642 cam_periph_unlock(periph);
2643 break;
2644 case CDIOCEJECT:
2645 cam_periph_lock(periph);
2646 error = cdstopunit(periph, 1);
2647 cam_periph_unlock(periph);
2648 break;
2649 case CDIOCALLOW:
2650 cam_periph_lock(periph);
2651 cdprevent(periph, PR_ALLOW);
2652 cam_periph_unlock(periph);
2653 break;
2654 case CDIOCPREVENT:
2655 cam_periph_lock(periph);
2656 cdprevent(periph, PR_PREVENT);
2657 cam_periph_unlock(periph);
2658 break;
2659 case CDIOCSETDEBUG:
2660 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2661 error = ENOTTY;
2662 break;
2663 case CDIOCCLRDEBUG:
2664 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2665 error = ENOTTY;
2666 break;
2667 case CDIOCRESET:
2668 /* return (cd_reset(periph)); */
2669 error = ENOTTY;
2670 break;
2671 case CDRIOCREADSPEED:
2672 cam_periph_lock(periph);
2673 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2674 cam_periph_unlock(periph);
2675 break;
2676 case CDRIOCWRITESPEED:
2677 cam_periph_lock(periph);
2678 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2679 cam_periph_unlock(periph);
2680 break;
2681 case DVDIOCSENDKEY:
2682 case DVDIOCREPORTKEY: {
2683 struct dvd_authinfo *authinfo;
2684
2685 authinfo = (struct dvd_authinfo *)addr;
2686
2687 cam_periph_lock(periph);
2688 if (cmd == DVDIOCREPORTKEY)
2689 error = cdreportkey(periph, authinfo);
2690 else
2691 error = cdsendkey(periph, authinfo);
2692 cam_periph_unlock(periph);
2693 break;
2694 }
2695 case DVDIOCREADSTRUCTURE: {
2696 struct dvd_struct *dvdstruct;
2697
2698 dvdstruct = (struct dvd_struct *)addr;
2699
2700 cam_periph_lock(periph);
2701 error = cdreaddvdstructure(periph, dvdstruct);
2702 cam_periph_unlock(periph);
2703
2704 break;
2705 }
2706 default:
2707 cam_periph_lock(periph);
2708 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2709 cam_periph_unlock(periph);
2710 break;
2711 }
2712
2713 cam_periph_lock(periph);
2714 cam_periph_unhold(periph);
2715
2716 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2717 if (error && bootverbose) {
2718 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2719 }
2720 cam_periph_unlock(periph);
2721
2722 return (error);
2723}
2724
2725static void
2726cdprevent(struct cam_periph *periph, int action)
2727{
2728 union ccb *ccb;
--- 182 unchanged lines hidden (view full) ---
2911 int error;
2912
2913 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2914
2915 softc = (struct cd_softc *)periph->softc;
2916
2917 ccb = cdgetccb(periph, /* priority */ 1);
2918
2919 /* XXX Should be M_WAITOK */
2920 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2921 M_TEMP, M_NOWAIT);
2922 if (rcap_buf == NULL)
2923 return (ENOMEM);
2924
2925 scsi_read_capacity(&ccb->csio,
2926 /*retries*/ 1,
2927 cddone,
2928 MSG_SIMPLE_Q_TAG,
2929 rcap_buf,
2930 SSD_FULL_SIZE,
2931 /* timeout */20000);
--- 1347 unchanged lines hidden ---
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:
--- 32 unchanged lines hidden (view full) ---
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 168752 2007-04-15 08:49:19Z scottl $");
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>
--- 7 unchanged lines hidden (view full) ---
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#include <cam/cam_sim.h>
74
75#include <cam/scsi/scsi_message.h>
76#include <cam/scsi/scsi_da.h>
77#include <cam/scsi/scsi_cd.h>
78
79#define LEADOUT 0xaa /* leadout toc entry */
80
81struct cd_params {
--- 17 unchanged lines hidden (view full) ---
99 CD_FLAG_DISC_REMOVABLE = 0x0008,
100 CD_FLAG_TAGGED_QUEUING = 0x0010,
101 CD_FLAG_CHANGER = 0x0040,
102 CD_FLAG_ACTIVE = 0x0080,
103 CD_FLAG_SCHED_ON_COMP = 0x0100,
104 CD_FLAG_RETRY_UA = 0x0200,
105 CD_FLAG_VALID_MEDIA = 0x0400,
106 CD_FLAG_VALID_TOC = 0x0800,
107 CD_FLAG_SCTX_INIT = 0x1000,
108 CD_FLAG_OPEN = 0x2000
109} cd_flags;
110
111typedef enum {
112 CD_CCB_PROBE = 0x01,
113 CD_CCB_BUFFER_IO = 0x02,
114 CD_CCB_WAITING = 0x03,
115 CD_CCB_TYPE_MASK = 0x0F,
116 CD_CCB_RETRY_UA = 0x10
--- 170 unchanged lines hidden (view full) ---
287static struct periph_driver cddriver =
288{
289 cdinit, "cd",
290 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0
291};
292
293PERIPHDRIVER_DECLARE(cd, cddriver);
294
295#ifndef CHANGER_MIN_BUSY_SECONDS
296#define CHANGER_MIN_BUSY_SECONDS 5
297#endif
298#ifndef CHANGER_MAX_BUSY_SECONDS
299#define CHANGER_MAX_BUSY_SECONDS 15
300#endif
301
302static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
--- 10 unchanged lines hidden (view full) ---
313
314struct cdchanger {
315 path_id_t path_id;
316 target_id_t target_id;
317 int num_devices;
318 struct camq devq;
319 struct timeval start_time;
320 struct cd_softc *cur_device;
321 struct callout short_handle;
322 struct callout long_handle;
323 volatile cd_changer_flags flags;
324 STAILQ_ENTRY(cdchanger) changer_links;
325 STAILQ_HEAD(chdevlist, cd_softc) chluns;
326};
327
328static struct mtx changerq_mtx;
329static STAILQ_HEAD(changerlist, cdchanger) changerq;
330static int num_changers;
331
332static void
333cdinit(void)
334{
335 cam_status status;
336 struct cam_path *path;
337
338 mtx_init(&changerq_mtx, "cdchangerq", "SCSI CD Changer List", MTX_DEF);
339 STAILQ_INIT(&changerq);
340
341 /*
342 * Install a global async callback. This callback will
343 * receive async callbacks like "new device found".
344 */
345 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
346 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
347
348 if (status == CAM_REQ_CMP) {
--- 13 unchanged lines hidden (view full) ---
362 printf("cd: Failed to attach master async callback "
363 "due to status 0x%x!\n", status);
364 }
365}
366
367static void
368cdoninvalidate(struct cam_periph *periph)
369{
370 struct cd_softc *softc;
371 struct ccb_setasync csa;
372
373 softc = (struct cd_softc *)periph->softc;
374
375 /*
376 * De-register any async callbacks.
377 */
378 xpt_setup_ccb(&csa.ccb_h, periph->path,
379 /* priority */ 5);
380 csa.ccb_h.func_code = XPT_SASYNC_CB;
381 csa.event_enable = 0;
382 csa.callback = cdasync;
383 csa.callback_arg = periph;
384 xpt_action((union ccb *)&csa);
385
386 softc->flags |= CD_FLAG_INVALID;
387
388 /*
389 * Return all queued I/O with ENXIO.
390 * XXX Handle any transactions queued to the card
391 * with XPT_ABORT_CCB.
392 */
393 bioq_flush(&softc->bio_queue, NULL, ENXIO);
394
395 /*
396 * If this device is part of a changer, and it was scheduled
397 * to run, remove it from the run queue since we just nuked
398 * all of its scheduled I/O.
399 */
400 if ((softc->flags & CD_FLAG_CHANGER)
401 && (softc->pinfo.index != CAM_UNQUEUED_INDEX))
402 camq_remove(&softc->changer->devq, softc->pinfo.index);
403
404 disk_gone(softc->disk);
405 xpt_print(periph->path, "lost device\n");
406}
407
408static void
409cdcleanup(struct cam_periph *periph)
410{
411 struct cd_softc *softc;
412
413 softc = (struct cd_softc *)periph->softc;
414
415 xpt_print(periph->path, "removing device entry\n");
416
417 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
418 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
419 xpt_print(periph->path, "can't remove sysctl context\n");
420 }
421
422 /*
423 * In the queued, non-active case, the device in question
424 * has already been removed from the changer run queue. Since this
425 * device is active, we need to de-activate it, and schedule
426 * another device to run. (if there is another one to run)
427 */
428 if ((softc->flags & CD_FLAG_CHANGER)
429 && (softc->flags & CD_FLAG_ACTIVE)) {
--- 13 unchanged lines hidden (view full) ---
443 * bogus pointer reference.
444 *
445 * The long timeout doesn't really matter, since we
446 * decrement the qfrozen_cnt to indicate that there is
447 * nothing in the active slot now. Therefore, there won't
448 * be any bogus pointer references there.
449 */
450 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
451 callout_stop(&softc->changer->short_handle);
452 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
453 }
454 softc->changer->devq.qfrozen_cnt--;
455 softc->changer->flags |= CHANGER_MANUAL_CALL;
456 cdrunchangerqueue(softc->changer);
457 }
458
459 /*
--- 4 unchanged lines hidden (view full) ---
464 && (--softc->changer->num_devices == 0)) {
465
466 /*
467 * Theoretically, there shouldn't be any timeouts left, but
468 * I'm not completely sure that that will be the case. So,
469 * it won't hurt to check and see if there are any left.
470 */
471 if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
472 callout_stop(&softc->changer->long_handle);
473 softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
474 }
475
476 if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
477 callout_stop(&softc->changer->short_handle);
478 softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
479 }
480
481 mtx_lock(&changerq_mtx);
482 STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
483 changer_links);
484 num_changers--;
485 mtx_unlock(&changerq_mtx);
486 xpt_print(periph->path, "removing changer entry\n");
487 free(softc->changer, M_DEVBUF);
488 }
489 disk_destroy(softc->disk);
490 free(softc, M_DEVBUF);
491}
492
493static void
494cdasync(void *callback_arg, u_int32_t code,
495 struct cam_path *path, void *arg)
496{
497 struct cam_periph *periph;
498
--- 30 unchanged lines hidden (view full) ---
529
530 break;
531 }
532 case AC_SENT_BDR:
533 case AC_BUS_RESET:
534 {
535 struct cd_softc *softc;
536 struct ccb_hdr *ccbh;
537
538 softc = (struct cd_softc *)periph->softc;
539 /*
540 * Don't fail on the expected unit attention
541 * that will occur.
542 */
543 softc->flags |= CD_FLAG_RETRY_UA;
544 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
545 ccbh->ccb_state |= CD_CCB_RETRY_UA;
546 /* FALLTHROUGH */
547 }
548 default:
549 cam_periph_async(periph, code, path, arg);
550 break;
551 }
552}
553
554static void
555cdsysctlinit(void *context, int pending)
556{
557 struct cam_periph *periph;
558 struct cd_softc *softc;
559 char tmpstr[80], tmpstr2[80];
560
561 periph = (struct cam_periph *)context;
562 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
563 return;
564
565 softc = (struct cd_softc *)periph->softc;
566 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number);
567 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
568
569 mtx_lock(&Giant);
570
571 sysctl_ctx_init(&softc->sysctl_ctx);
572 softc->flags |= CD_FLAG_SCTX_INIT;
573 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
574 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
575 tmpstr2, CTLFLAG_RD, 0, tmpstr);
576
577 if (softc->sysctl_tree == NULL) {
578 printf("cdsysctlinit: unable to allocate sysctl tree\n");
579 mtx_unlock(&Giant);
580 cam_periph_release(periph);
581 return;
582 }
583
584 /*
585 * Now register the sysctl handler, so the user can the value on
586 * the fly.
587 */
588 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
589 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
590 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
591 "Minimum CDB size");
592
593 mtx_unlock(&Giant);
594 cam_periph_release(periph);
595}
596
597/*
598 * We have a handler function for this so we can check the values when the
599 * user sets them, instead of every time we look at them.
600 */
601static int
602cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
--- 117 unchanged lines hidden (view full) ---
720 * yet. Unlike other SCSI peripheral drivers, we explicitly set
721 * the device type here to be CDROM, rather than just ORing in
722 * the device type. This is because this driver can attach to either
723 * CDROM or WORM devices, and we want this peripheral driver to
724 * show up in the devstat list as a CD peripheral driver, not a
725 * WORM peripheral driver. WORM drives will also have the WORM
726 * driver attached to them.
727 */
728 cam_periph_unlock(periph);
729 softc->disk = disk_alloc();
730 softc->disk->d_devstat = devstat_new_entry("cd",
731 periph->unit_number, 0,
732 DEVSTAT_BS_UNAVAILABLE,
733 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
734 DEVSTAT_PRIORITY_CD);
735 softc->disk->d_open = cdopen;
736 softc->disk->d_close = cdclose;
737 softc->disk->d_strategy = cdstrategy;
738 softc->disk->d_ioctl = cdioctl;
739 softc->disk->d_name = "cd";
740 softc->disk->d_unit = periph->unit_number;
741 softc->disk->d_drv1 = periph;
742 softc->disk->d_flags = 0;
743 disk_create(softc->disk, DISK_VERSION);
744 cam_periph_lock(periph);
745
746 /*
747 * Add an async callback so that we get
748 * notified if this device goes away.
749 */
750 xpt_setup_ccb(&csa.ccb_h, periph->path,
751 /* priority */ 5);
752 csa.ccb_h.func_code = XPT_SASYNC_CB;
--- 18 unchanged lines hidden (view full) ---
771 struct cam_periph *nperiph;
772 struct cam_path *path;
773 cam_status status;
774 int found;
775
776 /* Set the changer flag in the current device's softc */
777 softc->flags |= CD_FLAG_CHANGER;
778
779 /*
780 * Now, look around for an existing changer device with the
781 * same path and target ID as the current device.
782 */
783 mtx_lock(&changerq_mtx);
784 for (found = 0,
785 nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
786 nchanger != NULL;
787 nchanger = STAILQ_NEXT(nchanger, changer_links)){
788 if ((nchanger->path_id == cgd->ccb_h.path_id)
789 && (nchanger->target_id == cgd->ccb_h.target_id)) {
790 found = 1;
791 break;
792 }
793 }
794 mtx_unlock(&changerq_mtx);
795
796 /*
797 * If we found a matching entry, just add this device to
798 * the list of devices on this changer.
799 */
800 if (found == 1) {
801 struct chdevlist *chlunhead;
802
--- 89 unchanged lines hidden (view full) ---
892
893 if (camq_init(&nchanger->devq, 1) != 0) {
894 softc->flags &= ~CD_FLAG_CHANGER;
895 printf("cdregister: changer support "
896 "disabled\n");
897 goto cdregisterexit;
898 }
899
900 nchanger->path_id = cgd->ccb_h.path_id;
901 nchanger->target_id = cgd->ccb_h.target_id;
902
903 /* this is superfluous, but it makes things clearer */
904 nchanger->num_devices = 0;
905
906 STAILQ_INIT(&nchanger->chluns);
907
908 callout_init_mtx(&nchanger->long_handle,
909 periph->sim->mtx, 0);
910 callout_init_mtx(&nchanger->short_handle,
911 periph->sim->mtx, 0);
912
913 mtx_lock(&changerq_mtx);
914 num_changers++;
915 STAILQ_INSERT_TAIL(&changerq, nchanger,
916 changer_links);
917 mtx_unlock(&changerq_mtx);
918
919 /*
920 * Create a path with lun id 0, and see if we can
921 * find a matching device
922 */
923 status = xpt_create_path(&path, /*periph*/ periph,
924 cgd->ccb_h.path_id,
925 cgd->ccb_h.target_id, 0);
--- 45 unchanged lines hidden (view full) ---
971 }
972 STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
973 changer_links);
974 }
975 }
976
977cdregisterexit:
978
979 /*
980 * Refcount and block open attempts until we are setup
981 * Can't block
982 */
983 (void)cam_periph_hold(periph, PRIBIO);
984
985 if ((softc->flags & CD_FLAG_CHANGER) == 0)
986 xpt_schedule(periph, /*priority*/5);
987 else
988 cdschedule(periph, /*priority*/ 5);
989
990 return(CAM_REQ_CMP);
991}
992
993static int
994cdopen(struct disk *dp)
995{
996 struct cam_periph *periph;
997 struct cd_softc *softc;
998 int error;
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 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1007 return(ENXIO);
1008
1009 cam_periph_lock(periph);
1010
1011 if (softc->flags & CD_FLAG_INVALID) {
1012 cam_periph_unlock(periph);
1013 cam_periph_release(periph);
1014 return(ENXIO);
1015 }
1016
1017 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1018 cam_periph_unlock(periph);
1019 cam_periph_release(periph);
1020 return (error);
1021 }
1022
1023 /* Closes aren't symmetrical with opens, so fix up the refcounting. */
1024 if (softc->flags & CD_FLAG_OPEN)
1025 cam_periph_release(periph);
1026 else
1027 softc->flags |= CD_FLAG_OPEN;
1028
1029 /*
1030 * Check for media, and set the appropriate flags. We don't bail
1031 * if we don't have media, but then we don't allow anything but the
1032 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media.
1033 */
1034 cdcheckmedia(periph);
1035
1036 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
1037 cam_periph_unhold(periph);
1038 cam_periph_unlock(periph);
1039
1040 return (0);
1041}
1042
1043static int
1044cdclose(struct disk *dp)
1045{
1046 struct cam_periph *periph;
1047 struct cd_softc *softc;
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 cam_periph_lock(periph);
1056 cam_periph_hold(periph, PRIBIO);
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 */
1065 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
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|CD_FLAG_OPEN);
1071
1072 cam_periph_unhold(periph);
1073 cam_periph_unlock(periph);
1074 cam_periph_release(periph);
1075
1076 return (0);
1077}
1078
1079static void
1080cdshorttimeout(void *arg)
1081{
1082 struct cdchanger *changer;
1083
1084 changer = (struct cdchanger *)arg;
1085
1086 /* Always clear the short timeout flag, since that's what we're in */
1087 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1088
1089 /*
1090 * Check to see if there is any more pending or outstanding I/O for
1091 * this device. If not, move it out of the active slot.
1092 */
1093 if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
1094 && (changer->cur_device->outstanding_cmds == 0)) {
1095 changer->flags |= CHANGER_MANUAL_CALL;
1096 cdrunchangerqueue(changer);
1097 }
1098}
1099
1100/*
1101 * This is a wrapper for xpt_schedule. It only applies to changers.
1102 */
1103static void
1104cdschedule(struct cam_periph *periph, int priority)
1105{
1106 struct cd_softc *softc;
1107
1108 softc = (struct cd_softc *)periph->softc;
1109
1110 /*
1111 * If this device isn't currently queued, and if it isn't
1112 * the active device, then we queue this device and run the
1113 * changer queue if there is no timeout scheduled to do it.
1114 * If this device is the active device, just schedule it
1115 * to run again. If this device is queued, there should be
--- 21 unchanged lines hidden (view full) ---
1137 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1138 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
1139 softc->changer->flags |= CHANGER_MANUAL_CALL;
1140 cdrunchangerqueue(softc->changer);
1141 }
1142 } else if ((softc->flags & CD_FLAG_ACTIVE)
1143 && ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))
1144 xpt_schedule(periph, priority);
1145}
1146
1147static void
1148cdrunchangerqueue(void *arg)
1149{
1150 struct cd_softc *softc;
1151 struct cdchanger *changer;
1152 int called_from_timeout;
1153
1154 changer = (struct cdchanger *)arg;
1155
1156 /*
1157 * If we have NOT been called from cdstrategy() or cddone(), and
1158 * instead from a timeout routine, go ahead and clear the
1159 * timeout flag.
1160 */
1161 if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
1162 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1163 called_from_timeout = 1;
1164 } else
1165 called_from_timeout = 0;
1166
1167 /* Always clear the manual call flag */
1168 changer->flags &= ~CHANGER_MANUAL_CALL;
1169
1170 /* nothing to do if the queue is empty */
1171 if (changer->devq.entries <= 0) {
1172 return;
1173 }
1174
1175 /*
1176 * If the changer queue is frozen, that means we have an active
1177 * device.
1178 */
1179 if (changer->devq.qfrozen_cnt > 0) {
1180
1181 /*
1182 * We always need to reset the frozen count and clear the
1183 * active flag.
1184 */
1185 changer->devq.qfrozen_cnt--;
1186 changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
1187 changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
1188
1189 if (changer->cur_device->outstanding_cmds > 0) {
1190 changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
1191 changer->cur_device->bufs_left =
1192 changer->cur_device->outstanding_cmds;
1193 if (called_from_timeout) {
1194 callout_reset(&changer->long_handle,
1195 changer_max_busy_seconds * hz,
1196 cdrunchangerqueue, changer);
1197 changer->flags |= CHANGER_TIMEOUT_SCHED;
1198 }
1199 return;
1200 }
1201
1202 /*
1203 * Check to see whether the current device has any I/O left
1204 * to do. If so, requeue it at the end of the queue. If
1205 * not, there is no need to requeue it.
1206 */
1207 if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
1208
1209 changer->cur_device->pinfo.generation =
1210 ++changer->devq.generation;
--- 13 unchanged lines hidden (view full) ---
1224 wakeup(&softc->changer);
1225 xpt_schedule(softc->periph, /*priority*/ 1);
1226
1227 /*
1228 * Get rid of any pending timeouts, and set a flag to schedule new
1229 * ones so this device gets its full time quantum.
1230 */
1231 if (changer->flags & CHANGER_TIMEOUT_SCHED) {
1232 callout_stop(&changer->long_handle);
1233 changer->flags &= ~CHANGER_TIMEOUT_SCHED;
1234 }
1235
1236 if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
1237 callout_stop(&changer->short_handle);
1238 changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
1239 }
1240
1241 /*
1242 * We need to schedule timeouts, but we only do this after the
1243 * first transaction has completed. This eliminates the changer
1244 * switch time.
1245 */
1246 changer->flags |= CHANGER_NEED_TIMEOUT;
1247}
1248
1249static void
1250cdchangerschedule(struct cd_softc *softc)
1251{
1252 struct cdchanger *changer;
1253
1254 changer = softc->changer;
1255
1256 /*
1257 * If this is a changer, and this is the current device,
1258 * and this device has at least the minimum time quantum to
1259 * run, see if we can switch it out.
1260 */
1261 if ((softc->flags & CD_FLAG_ACTIVE)
--- 28 unchanged lines hidden (view full) ---
1290 && (softc->flags & CD_FLAG_ACTIVE)) {
1291
1292 /*
1293 * Now that the first transaction to this
1294 * particular device has completed, we can go ahead
1295 * and schedule our timeouts.
1296 */
1297 if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
1298 callout_reset(&changer->long_handle,
1299 changer_max_busy_seconds * hz,
1300 cdrunchangerqueue, changer);
1301 changer->flags |= CHANGER_TIMEOUT_SCHED;
1302 } else
1303 printf("cdchangerschedule: already have a long"
1304 " timeout!\n");
1305
1306 if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
1307 callout_reset(&changer->short_handle,
1308 changer_min_busy_seconds * hz,
1309 cdshorttimeout, changer);
1310 changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
1311 } else
1312 printf("cdchangerschedule: already have a short "
1313 "timeout!\n");
1314
1315 /*
1316 * We just scheduled timeouts, no need to schedule
1317 * more.
1318 */
1319 changer->flags &= ~CHANGER_NEED_TIMEOUT;
1320
1321 }
1322}
1323
1324static int
1325cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
1326 u_int32_t cam_flags,
1327 u_int32_t sense_flags),
1328 u_int32_t cam_flags, u_int32_t sense_flags)
1329{
--- 12 unchanged lines hidden (view full) ---
1342
1343 return(error);
1344}
1345
1346static union ccb *
1347cdgetccb(struct cam_periph *periph, u_int32_t priority)
1348{
1349 struct cd_softc *softc;
1350
1351 softc = (struct cd_softc *)periph->softc;
1352
1353 if (softc->flags & CD_FLAG_CHANGER) {
1354 /*
1355 * This should work the first time this device is woken up,
1356 * but just in case it doesn't, we use a while loop.
1357 */
1358 while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
1359 /*
1360 * If this changer isn't already queued, queue it up.
1361 */
--- 6 unchanged lines hidden (view full) ---
1368 }
1369 if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
1370 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
1371 && ((softc->changer->flags
1372 & CHANGER_SHORT_TMOUT_SCHED)==0)) {
1373 softc->changer->flags |= CHANGER_MANUAL_CALL;
1374 cdrunchangerqueue(softc->changer);
1375 } else
1376 msleep(&softc->changer, periph->sim->mtx,
1377 PRIBIO, "cgticb", 0);
1378 }
1379 }
1380 return(cam_periph_getccb(periph, priority));
1381}
1382
1383
1384/*
1385 * Actually translate the requested transfer into one the physical driver
1386 * can understand. The transfer is described by a buf and will include
1387 * only one physical transfer.
1388 */
1389static void
1390cdstrategy(struct bio *bp)
1391{
1392 struct cam_periph *periph;
1393 struct cd_softc *softc;
1394
1395 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1396 if (periph == NULL) {
1397 biofinish(bp, NULL, ENXIO);
1398 return;
1399 }
1400
1401 cam_periph_lock(periph);
1402 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
1403
1404 softc = (struct cd_softc *)periph->softc;
1405
1406 /*
1407 * If the device has been made invalid, error out
1408 */
1409 if ((softc->flags & CD_FLAG_INVALID)) {
1410 cam_periph_unlock(periph);
1411 biofinish(bp, NULL, ENXIO);
1412 return;
1413 }
1414
1415 /*
1416 * If we don't have valid media, look for it before trying to
1417 * schedule the I/O.
1418 */
1419 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
1420 int error;
1421
1422 error = cdcheckmedia(periph);
1423 if (error != 0) {
1424 cam_periph_unlock(periph);
1425 biofinish(bp, NULL, error);
1426 return;
1427 }
1428 }
1429
1430 /*
1431 * Place it in the queue of disk activities for this disk
1432 */
1433 bioq_disksort(&softc->bio_queue, bp);
1434
1435 /*
1436 * Schedule ourselves for performing the work. We do things
1437 * differently for changers.
1438 */
1439 if ((softc->flags & CD_FLAG_CHANGER) == 0)
1440 xpt_schedule(periph, /* XXX priority */1);
1441 else
1442 cdschedule(periph, /* priority */ 1);
1443
1444 cam_periph_unlock(periph);
1445 return;
1446}
1447
1448static void
1449cdstart(struct cam_periph *periph, union ccb *start_ccb)
1450{
1451 struct cd_softc *softc;
1452 struct bio *bp;
1453 struct ccb_scsiio *csio;
1454 struct scsi_read_capacity_data *rcap;
1455
1456 softc = (struct cd_softc *)periph->softc;
1457
1458 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
1459
1460 switch (softc->state) {
1461 case CD_STATE_NORMAL:
1462 {
1463 bp = bioq_first(&softc->bio_queue);
1464 if (periph->immediate_priority <= periph->pinfo.priority) {
1465 start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
1466
1467 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1468 periph_links.sle);
1469 periph->immediate_priority = CAM_PRIORITY_NONE;
1470 wakeup(&periph->ccb_list);
1471 } else if (bp == NULL) {
1472 xpt_release_ccb(start_ccb);
1473 } else {
1474 bioq_remove(&softc->bio_queue, bp);
1475
1476 devstat_start_transaction_bio(softc->disk->d_devstat, bp);
1477
1478 scsi_read_write(&start_ccb->csio,
1479 /*retries*/4,
--- 7 unchanged lines hidden (view full) ---
1487 bp->bio_bcount / softc->params.blksize,
1488 /* data_ptr */ bp->bio_data,
1489 /* dxfer_len */ bp->bio_bcount,
1490 /* sense_len */ SSD_FULL_SIZE,
1491 /* timeout */ 30000);
1492 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
1493
1494
1495 LIST_INSERT_HEAD(&softc->pending_ccbs,
1496 &start_ccb->ccb_h, periph_links.le);
1497 softc->outstanding_cmds++;
1498
1499 /* We expect a unit attention from this device */
1500 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
1501 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
1502 softc->flags &= ~CD_FLAG_RETRY_UA;
1503 }
1504
1505 start_ccb->ccb_h.ccb_bp = bp;
1506 bp = bioq_first(&softc->bio_queue);
1507
1508 xpt_action(start_ccb);
1509 }
1510 if (bp != NULL) {
1511 /* Have more work to do, so ensure we stay scheduled */
1512 xpt_schedule(periph, /* XXX priority */1);
1513 }
1514 break;
--- 37 unchanged lines hidden (view full) ---
1552 softc = (struct cd_softc *)periph->softc;
1553 csio = &done_ccb->csio;
1554
1555 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
1556 case CD_CCB_BUFFER_IO:
1557 {
1558 struct bio *bp;
1559 int error;
1560
1561 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1562 error = 0;
1563
1564 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1565 int sf;
1566
1567 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
--- 7 unchanged lines hidden (view full) ---
1575 * A retry was scheuled, so
1576 * just return.
1577 */
1578 return;
1579 }
1580 }
1581
1582 if (error != 0) {
1583 xpt_print(periph->path,
1584 "cddone: got error %#x back\n", error);
1585 bioq_flush(&softc->bio_queue, NULL, EIO);
1586 bp->bio_resid = bp->bio_bcount;
1587 bp->bio_error = error;
1588 bp->bio_flags |= BIO_ERROR;
1589 cam_release_devq(done_ccb->ccb_h.path,
1590 /*relsim_flags*/0,
1591 /*reduction*/0,
1592 /*timeout*/0,
1593 /*getcount_only*/0);
--- 6 unchanged lines hidden (view full) ---
1600 * Short transfer ???
1601 * XXX: not sure this is correct for partial
1602 * transfers at EOM
1603 */
1604 bp->bio_flags |= BIO_ERROR;
1605 }
1606 }
1607
1608 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1609 softc->outstanding_cmds--;
1610
1611 if (softc->flags & CD_FLAG_CHANGER)
1612 cdchangerschedule(softc);
1613
1614 biofinish(bp, NULL, 0);
1615 break;
1616 }
1617 case CD_CCB_PROBE:
--- 174 unchanged lines hidden (view full) ---
1792 * Since our peripheral may be invalidated by an error
1793 * above or an external event, we must release our CCB
1794 * before releasing the probe lock on the peripheral.
1795 * The peripheral will only go away once the last lock
1796 * is removed, and we need it around for the CCB release
1797 * operation.
1798 */
1799 xpt_release_ccb(done_ccb);
1800 cam_periph_unhold(periph);
1801 return;
1802 }
1803 case CD_CCB_WAITING:
1804 {
1805 /* Caller will release the CCB */
1806 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1807 ("trying to wakeup ccbwait\n"));
1808
--- 36 unchanged lines hidden (view full) ---
1845}
1846
1847static int
1848cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1849{
1850
1851 struct cam_periph *periph;
1852 struct cd_softc *softc;
1853 int nocopyout, error = 0;
1854
1855 periph = (struct cam_periph *)dp->d_drv1;
1856 if (periph == NULL)
1857 return(ENXIO);
1858
1859 cam_periph_lock(periph);
1860 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
1861
1862 softc = (struct cd_softc *)periph->softc;
1863
1864 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1865 ("trying to do ioctl %#lx\n", cmd));
1866
1867 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
1868 cam_periph_unlock(periph);
1869 cam_periph_release(periph);
1870 return (error);
1871 }
1872
1873 /*
1874 * If we don't have media loaded, check for it. If still don't
1875 * have media loaded, we can only do a load or eject.
1876 *
1877 * We only care whether media is loaded if this is a cd-specific ioctl
1878 * (thus the IOCGROUP check below). Note that this will break if
1879 * anyone adds any ioctls into the switch statement below that don't
1880 * have their ioctl group set to 'c'.
1881 */
1882 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
1883 && ((cmd != CDIOCCLOSE)
1884 && (cmd != CDIOCEJECT))
1885 && (IOCGROUP(cmd) == 'c')) {
1886 error = cdcheckmedia(periph);
1887 }
1888 /*
1889 * Drop the lock here so later mallocs can use WAITOK. The periph
1890 * is essentially locked still with the cam_periph_hold call above.
1891 */
1892 cam_periph_unlock(periph);
1893 if (error != 0)
1894 return (error);
1895
1896 nocopyout = 0;
1897 switch (cmd) {
1898
1899 case CDIOCPLAYTRACKS:
1900 {
1901 struct ioc_play_track *args
1902 = (struct ioc_play_track *) addr;
1903 struct cd_mode_params params;
1904 union cd_pages *page;
1905
1906 params.alloc_len = sizeof(union cd_mode_data_6_10);
1907 params.mode_buf = malloc(params.alloc_len, M_TEMP,
1908 M_WAITOK | M_ZERO);
1909
1910 cam_periph_lock(periph);
1911 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1912 ("trying to do CDIOCPLAYTRACKS\n"));
1913
1914 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
1915 if (error) {
1916 free(params.mode_buf, M_TEMP);
1917 cam_periph_unlock(periph);
1918 break;
1919 }
1920 page = cdgetpage(¶ms);
1921
1922 page->audio.flags &= ~CD_PA_SOTC;
1923 page->audio.flags |= CD_PA_IMMED;
1924 error = cdsetmode(periph, ¶ms);
1925 free(params.mode_buf, M_TEMP);
1926 if (error) {
1927 cam_periph_unlock(periph);
1928 break;
1929 }
1930
1931 /*
1932 * This was originally implemented with the PLAY
1933 * AUDIO TRACK INDEX command, but that command was
1934 * deprecated after SCSI-2. Most (all?) SCSI CDROM
1935 * drives support it but ATAPI and ATAPI-derivative
1936 * drives don't seem to support it. So we keep a
1937 * cache of the table of contents and translate
--- 45 unchanged lines hidden (view full) ---
1983 bin2bcd(args->end_track);
1984 }
1985 error = cdplaytracks(periph,
1986 args->start_track,
1987 args->start_index,
1988 args->end_track,
1989 args->end_index);
1990 }
1991 cam_periph_unlock(periph);
1992 }
1993 break;
1994 case CDIOCPLAYMSF:
1995 {
1996 struct ioc_play_msf *args
1997 = (struct ioc_play_msf *) addr;
1998 struct cd_mode_params params;
1999 union cd_pages *page;
2000
2001 params.alloc_len = sizeof(union cd_mode_data_6_10);
2002 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2003 M_WAITOK | M_ZERO);
2004
2005 cam_periph_lock(periph);
2006 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2007 ("trying to do CDIOCPLAYMSF\n"));
2008
2009 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2010 if (error) {
2011 free(params.mode_buf, M_TEMP);
2012 cam_periph_unlock(periph);
2013 break;
2014 }
2015 page = cdgetpage(¶ms);
2016
2017 page->audio.flags &= ~CD_PA_SOTC;
2018 page->audio.flags |= CD_PA_IMMED;
2019 error = cdsetmode(periph, ¶ms);
2020 free(params.mode_buf, M_TEMP);
2021 if (error) {
2022 cam_periph_unlock(periph);
2023 break;
2024 }
2025 error = cdplaymsf(periph,
2026 args->start_m,
2027 args->start_s,
2028 args->start_f,
2029 args->end_m,
2030 args->end_s,
2031 args->end_f);
2032 cam_periph_unlock(periph);
2033 }
2034 break;
2035 case CDIOCPLAYBLOCKS:
2036 {
2037 struct ioc_play_blocks *args
2038 = (struct ioc_play_blocks *) 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_periph_lock(periph);
2047 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2048 ("trying to do CDIOCPLAYBLOCKS\n"));
2049
2050
2051 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2052 if (error) {
2053 free(params.mode_buf, M_TEMP);
2054 cam_periph_unlock(periph);
2055 break;
2056 }
2057 page = cdgetpage(¶ms);
2058
2059 page->audio.flags &= ~CD_PA_SOTC;
2060 page->audio.flags |= CD_PA_IMMED;
2061 error = cdsetmode(periph, ¶ms);
2062 free(params.mode_buf, M_TEMP);
2063 if (error) {
2064 cam_periph_unlock(periph);
2065 break;
2066 }
2067 error = cdplay(periph, args->blk, args->len);
2068 cam_periph_unlock(periph);
2069 }
2070 break;
2071 case CDIOCREADSUBCHANNEL_SYSSPACE:
2072 nocopyout = 1;
2073 /* Fallthrough */
2074 case CDIOCREADSUBCHANNEL:
2075 {
2076 struct ioc_read_subchannel *args
2077 = (struct ioc_read_subchannel *) addr;
2078 struct cd_sub_channel_info *data;
2079 u_int32_t len = args->data_len;
2080
2081 data = malloc(sizeof(struct cd_sub_channel_info),
2082 M_TEMP, M_WAITOK);
2083
2084 cam_periph_lock(periph);
2085 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2086 ("trying to do CDIOCREADSUBCHANNEL\n"));
2087
2088 if ((len > sizeof(struct cd_sub_channel_info)) ||
2089 (len < sizeof(struct cd_sub_channel_header))) {
2090 printf(
2091 "scsi_cd: cdioctl: "
2092 "cdioreadsubchannel: error, len=%d\n",
2093 len);
2094 error = EINVAL;
2095 free(data, M_TEMP);
2096 cam_periph_unlock(periph);
2097 break;
2098 }
2099
2100 if (softc->quirks & CD_Q_BCD_TRACKS)
2101 args->track = bin2bcd(args->track);
2102
2103 error = cdreadsubchannel(periph, args->address_format,
2104 args->data_format, args->track, data, len);
2105
2106 if (error) {
2107 free(data, M_TEMP);
2108 cam_periph_unlock(periph);
2109 break;
2110 }
2111 if (softc->quirks & CD_Q_BCD_TRACKS)
2112 data->what.track_info.track_number =
2113 bcd2bin(data->what.track_info.track_number);
2114 len = min(len, ((data->header.data_len[0] << 8) +
2115 data->header.data_len[1] +
2116 sizeof(struct cd_sub_channel_header)));
2117 cam_periph_unlock(periph);
2118 if (nocopyout == 0) {
2119 if (copyout(data, args->data, len) != 0) {
2120 error = EFAULT;
2121 }
2122 } else {
2123 bcopy(data, args->data, len);
2124 }
2125 free(data, M_TEMP);
2126 }
2127 break;
2128
2129 case CDIOREADTOCHEADER:
2130 {
2131 struct ioc_toc_header *th;
2132
2133 th = malloc(sizeof(struct ioc_toc_header), M_TEMP,
2134 M_WAITOK);
2135
2136 cam_periph_lock(periph);
2137 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2138 ("trying to do CDIOREADTOCHEADER\n"));
2139
2140 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2141 sizeof (*th), /*sense_flags*/0);
2142 if (error) {
2143 free(th, M_TEMP);
2144 cam_periph_unlock(periph);
2145 break;
2146 }
2147 if (softc->quirks & CD_Q_BCD_TRACKS) {
2148 /* we are going to have to convert the BCD
2149 * encoding on the cd to what is expected
2150 */
2151 th->starting_track =
2152 bcd2bin(th->starting_track);
2153 th->ending_track = bcd2bin(th->ending_track);
2154 }
2155 th->len = ntohs(th->len);
2156 bcopy(th, addr, sizeof(*th));
2157 free(th, M_TEMP);
2158 cam_periph_unlock(periph);
2159 }
2160 break;
2161 case CDIOREADTOCENTRYS:
2162 {
2163 struct cd_tocdata *data;
2164 struct cd_toc_single *lead;
2165 struct ioc_read_toc_entry *te =
2166 (struct ioc_read_toc_entry *) addr;
2167 struct ioc_toc_header *th;
2168 u_int32_t len, readlen, idx, num;
2169 u_int32_t starting_track = te->starting_track;
2170
2171 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2172 lead = malloc(sizeof(*lead), M_TEMP, M_WAITOK);
2173
2174 cam_periph_lock(periph);
2175 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2176 ("trying to do CDIOREADTOCENTRYS\n"));
2177
2178 if (te->data_len < sizeof(struct cd_toc_entry)
2179 || (te->data_len % sizeof(struct cd_toc_entry)) != 0
2180 || (te->address_format != CD_MSF_FORMAT
2181 && te->address_format != CD_LBA_FORMAT)) {
2182 error = EINVAL;
2183 printf("scsi_cd: error in readtocentries, "
2184 "returning EINVAL\n");
2185 free(data, M_TEMP);
2186 free(lead, M_TEMP);
2187 cam_periph_unlock(periph);
2188 break;
2189 }
2190
2191 th = &data->header;
2192 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2193 sizeof (*th), /*sense_flags*/0);
2194 if (error) {
2195 free(data, M_TEMP);
2196 free(lead, M_TEMP);
2197 cam_periph_unlock(periph);
2198 break;
2199 }
2200
2201 if (softc->quirks & CD_Q_BCD_TRACKS) {
2202 /* we are going to have to convert the BCD
2203 * encoding on the cd to what is expected
2204 */
2205 th->starting_track =
--- 6 unchanged lines hidden (view full) ---
2212 else if (starting_track == LEADOUT)
2213 starting_track = th->ending_track + 1;
2214 else if (starting_track < th->starting_track ||
2215 starting_track > th->ending_track + 1) {
2216 printf("scsi_cd: error in readtocentries, "
2217 "returning EINVAL\n");
2218 free(data, M_TEMP);
2219 free(lead, M_TEMP);
2220 cam_periph_unlock(periph);
2221 error = EINVAL;
2222 break;
2223 }
2224
2225 /* calculate reading length without leadout entry */
2226 readlen = (th->ending_track - starting_track + 1) *
2227 sizeof(struct cd_toc_entry);
2228
--- 5 unchanged lines hidden (view full) ---
2234 readlen = len;
2235 }
2236 if (len > sizeof(data->entries)) {
2237 printf("scsi_cd: error in readtocentries, "
2238 "returning EINVAL\n");
2239 error = EINVAL;
2240 free(data, M_TEMP);
2241 free(lead, M_TEMP);
2242 cam_periph_unlock(periph);
2243 break;
2244 }
2245 num = len / sizeof(struct cd_toc_entry);
2246
2247 if (readlen > 0) {
2248 error = cdreadtoc(periph, te->address_format,
2249 starting_track,
2250 (u_int8_t *)data,
2251 readlen + sizeof (*th),
2252 /*sense_flags*/0);
2253 if (error) {
2254 free(data, M_TEMP);
2255 free(lead, M_TEMP);
2256 cam_periph_unlock(periph);
2257 break;
2258 }
2259 }
2260
2261 /* make leadout entry if needed */
2262 idx = starting_track + num - 1;
2263 if (softc->quirks & CD_Q_BCD_TRACKS)
2264 th->ending_track = bcd2bin(th->ending_track);
2265 if (idx == th->ending_track + 1) {
2266 error = cdreadtoc(periph, te->address_format,
2267 LEADOUT, (u_int8_t *)lead,
2268 sizeof(*lead),
2269 /*sense_flags*/0);
2270 if (error) {
2271 free(data, M_TEMP);
2272 free(lead, M_TEMP);
2273 cam_periph_unlock(periph);
2274 break;
2275 }
2276 data->entries[idx - starting_track] =
2277 lead->entry;
2278 }
2279 if (softc->quirks & CD_Q_BCD_TRACKS) {
2280 for (idx = 0; idx < num - 1; idx++) {
2281 data->entries[idx].track =
2282 bcd2bin(data->entries[idx].track);
2283 }
2284 }
2285
2286 cam_periph_unlock(periph);
2287 error = copyout(data->entries, te->data, len);
2288 free(data, M_TEMP);
2289 free(lead, M_TEMP);
2290 }
2291 break;
2292 case CDIOREADTOCENTRY:
2293 {
2294 struct cd_toc_single *data;
2295 struct ioc_read_toc_single_entry *te =
2296 (struct ioc_read_toc_single_entry *) addr;
2297 struct ioc_toc_header *th;
2298 u_int32_t track;
2299
2300 data = malloc(sizeof(*data), M_TEMP, M_WAITOK);
2301
2302 cam_periph_lock(periph);
2303 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2304 ("trying to do CDIOREADTOCENTRY\n"));
2305
2306 if (te->address_format != CD_MSF_FORMAT
2307 && te->address_format != CD_LBA_FORMAT) {
2308 printf("error in readtocentry, "
2309 " returning EINVAL\n");
2310 free(data, M_TEMP);
2311 error = EINVAL;
2312 cam_periph_unlock(periph);
2313 break;
2314 }
2315
2316 th = &data->header;
2317 error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
2318 sizeof (*th), /*sense_flags*/0);
2319 if (error) {
2320 free(data, M_TEMP);
2321 cam_periph_unlock(periph);
2322 break;
2323 }
2324
2325 if (softc->quirks & CD_Q_BCD_TRACKS) {
2326 /* we are going to have to convert the BCD
2327 * encoding on the cd to what is expected
2328 */
2329 th->starting_track =
--- 6 unchanged lines hidden (view full) ---
2336 else if (track == LEADOUT)
2337 /* OK */;
2338 else if (track < th->starting_track ||
2339 track > th->ending_track + 1) {
2340 printf("error in readtocentry, "
2341 " returning EINVAL\n");
2342 free(data, M_TEMP);
2343 error = EINVAL;
2344 cam_periph_unlock(periph);
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 cam_periph_unlock(periph);
2354 break;
2355 }
2356
2357 if (softc->quirks & CD_Q_BCD_TRACKS)
2358 data->entry.track = bcd2bin(data->entry.track);
2359 bcopy(&data->entry, &te->entry,
2360 sizeof(struct cd_toc_entry));
2361 free(data, M_TEMP);
2362 cam_periph_unlock(periph);
2363 }
2364 break;
2365 case CDIOCSETPATCH:
2366 {
2367 struct ioc_patch *arg = (struct ioc_patch *)addr;
2368 struct cd_mode_params params;
2369 union cd_pages *page;
2370
2371 params.alloc_len = sizeof(union cd_mode_data_6_10);
2372 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2373 M_WAITOK | M_ZERO);
2374
2375 cam_periph_lock(periph);
2376 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2377 ("trying to do CDIOCSETPATCH\n"));
2378
2379 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2380 if (error) {
2381 free(params.mode_buf, M_TEMP);
2382 cam_periph_unlock(periph);
2383 break;
2384 }
2385 page = cdgetpage(¶ms);
2386
2387 page->audio.port[LEFT_PORT].channels =
2388 arg->patch[0];
2389 page->audio.port[RIGHT_PORT].channels =
2390 arg->patch[1];
2391 page->audio.port[2].channels = arg->patch[2];
2392 page->audio.port[3].channels = arg->patch[3];
2393 error = cdsetmode(periph, ¶ms);
2394 free(params.mode_buf, M_TEMP);
2395 cam_periph_unlock(periph);
2396 }
2397 break;
2398 case CDIOCGETVOL:
2399 {
2400 struct ioc_vol *arg = (struct ioc_vol *) addr;
2401 struct cd_mode_params params;
2402 union cd_pages *page;
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
2408 cam_periph_lock(periph);
2409 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2410 ("trying to do CDIOCGETVOL\n"));
2411
2412 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2413 if (error) {
2414 free(params.mode_buf, M_TEMP);
2415 cam_periph_unlock(periph);
2416 break;
2417 }
2418 page = cdgetpage(¶ms);
2419
2420 arg->vol[LEFT_PORT] =
2421 page->audio.port[LEFT_PORT].volume;
2422 arg->vol[RIGHT_PORT] =
2423 page->audio.port[RIGHT_PORT].volume;
2424 arg->vol[2] = page->audio.port[2].volume;
2425 arg->vol[3] = page->audio.port[3].volume;
2426 free(params.mode_buf, M_TEMP);
2427 cam_periph_unlock(periph);
2428 }
2429 break;
2430 case CDIOCSETVOL:
2431 {
2432 struct ioc_vol *arg = (struct ioc_vol *) addr;
2433 struct cd_mode_params params;
2434 union cd_pages *page;
2435
2436 params.alloc_len = sizeof(union cd_mode_data_6_10);
2437 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2438 M_WAITOK | M_ZERO);
2439
2440 cam_periph_lock(periph);
2441 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2442 ("trying to do CDIOCSETVOL\n"));
2443
2444 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2445 if (error) {
2446 free(params.mode_buf, M_TEMP);
2447 cam_periph_unlock(periph);
2448 break;
2449 }
2450 page = cdgetpage(¶ms);
2451
2452 page->audio.port[LEFT_PORT].channels = CHANNEL_0;
2453 page->audio.port[LEFT_PORT].volume =
2454 arg->vol[LEFT_PORT];
2455 page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
2456 page->audio.port[RIGHT_PORT].volume =
2457 arg->vol[RIGHT_PORT];
2458 page->audio.port[2].volume = arg->vol[2];
2459 page->audio.port[3].volume = arg->vol[3];
2460 error = cdsetmode(periph, ¶ms);
2461 cam_periph_unlock(periph);
2462 free(params.mode_buf, M_TEMP);
2463 }
2464 break;
2465 case CDIOCSETMONO:
2466 {
2467 struct cd_mode_params params;
2468 union cd_pages *page;
2469
2470 params.alloc_len = sizeof(union cd_mode_data_6_10);
2471 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2472 M_WAITOK | M_ZERO);
2473
2474 cam_periph_lock(periph);
2475 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2476 ("trying to do CDIOCSETMONO\n"));
2477
2478 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2479 if (error) {
2480 free(params.mode_buf, M_TEMP);
2481 cam_periph_unlock(periph);
2482 break;
2483 }
2484 page = cdgetpage(¶ms);
2485
2486 page->audio.port[LEFT_PORT].channels =
2487 LEFT_CHANNEL | RIGHT_CHANNEL;
2488 page->audio.port[RIGHT_PORT].channels =
2489 LEFT_CHANNEL | RIGHT_CHANNEL;
2490 page->audio.port[2].channels = 0;
2491 page->audio.port[3].channels = 0;
2492 error = cdsetmode(periph, ¶ms);
2493 cam_periph_unlock(periph);
2494 free(params.mode_buf, M_TEMP);
2495 }
2496 break;
2497 case CDIOCSETSTEREO:
2498 {
2499 struct cd_mode_params params;
2500 union cd_pages *page;
2501
2502 params.alloc_len = sizeof(union cd_mode_data_6_10);
2503 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2504 M_WAITOK | M_ZERO);
2505
2506 cam_periph_lock(periph);
2507 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2508 ("trying to do CDIOCSETSTEREO\n"));
2509
2510 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2511 if (error) {
2512 free(params.mode_buf, M_TEMP);
2513 cam_periph_unlock(periph);
2514 break;
2515 }
2516 page = cdgetpage(¶ms);
2517
2518 page->audio.port[LEFT_PORT].channels =
2519 LEFT_CHANNEL;
2520 page->audio.port[RIGHT_PORT].channels =
2521 RIGHT_CHANNEL;
2522 page->audio.port[2].channels = 0;
2523 page->audio.port[3].channels = 0;
2524 error = cdsetmode(periph, ¶ms);
2525 free(params.mode_buf, M_TEMP);
2526 cam_periph_unlock(periph);
2527 }
2528 break;
2529 case CDIOCSETMUTE:
2530 {
2531 struct cd_mode_params params;
2532 union cd_pages *page;
2533
2534 params.alloc_len = sizeof(union cd_mode_data_6_10);
2535 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2536 M_WAITOK | M_ZERO);
2537
2538 cam_periph_lock(periph);
2539 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2540 ("trying to do CDIOCSETMUTE\n"));
2541
2542 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2543 if (error) {
2544 free(¶ms, M_TEMP);
2545 cam_periph_unlock(periph);
2546 break;
2547 }
2548 page = cdgetpage(¶ms);
2549
2550 page->audio.port[LEFT_PORT].channels = 0;
2551 page->audio.port[RIGHT_PORT].channels = 0;
2552 page->audio.port[2].channels = 0;
2553 page->audio.port[3].channels = 0;
2554 error = cdsetmode(periph, ¶ms);
2555 free(params.mode_buf, M_TEMP);
2556 cam_periph_unlock(periph);
2557 }
2558 break;
2559 case CDIOCSETLEFT:
2560 {
2561 struct cd_mode_params params;
2562 union cd_pages *page;
2563
2564 params.alloc_len = sizeof(union cd_mode_data_6_10);
2565 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2566 M_WAITOK | M_ZERO);
2567
2568 cam_periph_lock(periph);
2569 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2570 ("trying to do CDIOCSETLEFT\n"));
2571
2572 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2573 if (error) {
2574 free(params.mode_buf, M_TEMP);
2575 cam_periph_unlock(periph);
2576 break;
2577 }
2578 page = cdgetpage(¶ms);
2579
2580 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
2581 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
2582 page->audio.port[2].channels = 0;
2583 page->audio.port[3].channels = 0;
2584 error = cdsetmode(periph, ¶ms);
2585 free(params.mode_buf, M_TEMP);
2586 cam_periph_unlock(periph);
2587 }
2588 break;
2589 case CDIOCSETRIGHT:
2590 {
2591 struct cd_mode_params params;
2592 union cd_pages *page;
2593
2594 params.alloc_len = sizeof(union cd_mode_data_6_10);
2595 params.mode_buf = malloc(params.alloc_len, M_TEMP,
2596 M_WAITOK | M_ZERO);
2597
2598 cam_periph_lock(periph);
2599 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2600 ("trying to do CDIOCSETRIGHT\n"));
2601
2602 error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
2603 if (error) {
2604 free(params.mode_buf, M_TEMP);
2605 cam_periph_unlock(periph);
2606 break;
2607 }
2608 page = cdgetpage(¶ms);
2609
2610 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
2611 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
2612 page->audio.port[2].channels = 0;
2613 page->audio.port[3].channels = 0;
2614 error = cdsetmode(periph, ¶ms);
2615 free(params.mode_buf, M_TEMP);
2616 cam_periph_unlock(periph);
2617 }
2618 break;
2619 case CDIOCRESUME:
2620 cam_periph_lock(periph);
2621 error = cdpause(periph, 1);
2622 cam_periph_unlock(periph);
2623 break;
2624 case CDIOCPAUSE:
2625 cam_periph_lock(periph);
2626 error = cdpause(periph, 0);
2627 cam_periph_unlock(periph);
2628 break;
2629 case CDIOCSTART:
2630 cam_periph_lock(periph);
2631 error = cdstartunit(periph, 0);
2632 cam_periph_unlock(periph);
2633 break;
2634 case CDIOCCLOSE:
2635 cam_periph_lock(periph);
2636 error = cdstartunit(periph, 1);
2637 cam_periph_unlock(periph);
2638 break;
2639 case CDIOCSTOP:
2640 cam_periph_lock(periph);
2641 error = cdstopunit(periph, 0);
2642 cam_periph_unlock(periph);
2643 break;
2644 case CDIOCEJECT:
2645 cam_periph_lock(periph);
2646 error = cdstopunit(periph, 1);
2647 cam_periph_unlock(periph);
2648 break;
2649 case CDIOCALLOW:
2650 cam_periph_lock(periph);
2651 cdprevent(periph, PR_ALLOW);
2652 cam_periph_unlock(periph);
2653 break;
2654 case CDIOCPREVENT:
2655 cam_periph_lock(periph);
2656 cdprevent(periph, PR_PREVENT);
2657 cam_periph_unlock(periph);
2658 break;
2659 case CDIOCSETDEBUG:
2660 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */
2661 error = ENOTTY;
2662 break;
2663 case CDIOCCLRDEBUG:
2664 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */
2665 error = ENOTTY;
2666 break;
2667 case CDIOCRESET:
2668 /* return (cd_reset(periph)); */
2669 error = ENOTTY;
2670 break;
2671 case CDRIOCREADSPEED:
2672 cam_periph_lock(periph);
2673 error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
2674 cam_periph_unlock(periph);
2675 break;
2676 case CDRIOCWRITESPEED:
2677 cam_periph_lock(periph);
2678 error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
2679 cam_periph_unlock(periph);
2680 break;
2681 case DVDIOCSENDKEY:
2682 case DVDIOCREPORTKEY: {
2683 struct dvd_authinfo *authinfo;
2684
2685 authinfo = (struct dvd_authinfo *)addr;
2686
2687 cam_periph_lock(periph);
2688 if (cmd == DVDIOCREPORTKEY)
2689 error = cdreportkey(periph, authinfo);
2690 else
2691 error = cdsendkey(periph, authinfo);
2692 cam_periph_unlock(periph);
2693 break;
2694 }
2695 case DVDIOCREADSTRUCTURE: {
2696 struct dvd_struct *dvdstruct;
2697
2698 dvdstruct = (struct dvd_struct *)addr;
2699
2700 cam_periph_lock(periph);
2701 error = cdreaddvdstructure(periph, dvdstruct);
2702 cam_periph_unlock(periph);
2703
2704 break;
2705 }
2706 default:
2707 cam_periph_lock(periph);
2708 error = cam_periph_ioctl(periph, cmd, addr, cderror);
2709 cam_periph_unlock(periph);
2710 break;
2711 }
2712
2713 cam_periph_lock(periph);
2714 cam_periph_unhold(periph);
2715
2716 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
2717 if (error && bootverbose) {
2718 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error);
2719 }
2720 cam_periph_unlock(periph);
2721
2722 return (error);
2723}
2724
2725static void
2726cdprevent(struct cam_periph *periph, int action)
2727{
2728 union ccb *ccb;
--- 182 unchanged lines hidden (view full) ---
2911 int error;
2912
2913 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
2914
2915 softc = (struct cd_softc *)periph->softc;
2916
2917 ccb = cdgetccb(periph, /* priority */ 1);
2918
2919 /* XXX Should be M_WAITOK */
2920 rcap_buf = malloc(sizeof(struct scsi_read_capacity_data),
2921 M_TEMP, M_NOWAIT);
2922 if (rcap_buf == NULL)
2923 return (ENOMEM);
2924
2925 scsi_read_capacity(&ccb->csio,
2926 /*retries*/ 1,
2927 cddone,
2928 MSG_SIMPLE_Q_TAG,
2929 rcap_buf,
2930 SSD_FULL_SIZE,
2931 /* timeout */20000);
--- 1347 unchanged lines hidden ---