2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-4-Clause)
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
32 * All rights reserved.
34 * Partially based on an autochanger driver written by Stefan Grefen
35 * and on an autochanger driver written by the Systems Programming Group
36 * at the University of Utah Computer Science Department.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgements:
48 * This product includes software developed by Jason R. Thorpe
49 * for And Communications, http://www.and.com/
50 * 4. The name of the author may not be used to endorse or promote products
51 * derived from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
58 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
59 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
60 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * $NetBSD: ch.c,v 1.34 1998/08/31 22:28:06 cgd Exp $
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include <sys/param.h>
72 #include <sys/queue.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/types.h>
76 #include <sys/malloc.h>
77 #include <sys/fcntl.h>
80 #include <sys/errno.h>
81 #include <sys/devicestat.h>
84 #include <cam/cam_ccb.h>
85 #include <cam/cam_periph.h>
86 #include <cam/cam_xpt_periph.h>
87 #include <cam/cam_debug.h>
89 #include <cam/scsi/scsi_all.h>
90 #include <cam/scsi/scsi_message.h>
91 #include <cam/scsi/scsi_ch.h>
94 * Timeout definitions for various changer related commands. They may
95 * be too short for some devices (especially the timeout for INITIALIZE
99 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
100 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 15 * 60 * 1000;
101 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 15 * 60 * 1000;
102 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 15 * 60 * 1000;
103 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 5 * 60 * 1000;
104 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
105 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
108 CH_FLAG_INVALID = 0x001
126 #define CH_Q_BIT_STRING \
131 #define ccb_state ppriv_field0
132 #define ccb_bp ppriv_ptr1
134 struct scsi_mode_sense_data {
135 struct scsi_mode_header_6 header;
136 struct scsi_mode_blk_desc blk_desc;
138 struct page_element_address_assignment ea;
139 struct page_transport_geometry_parameters tg;
140 struct page_device_capabilities cap;
149 struct devstat *device_stats;
153 int sc_picker; /* current picker */
156 * The following information is obtained from the
157 * element address assignment page.
159 int sc_firsts[CHET_MAX + 1]; /* firsts */
160 int sc_counts[CHET_MAX + 1]; /* counts */
163 * The following mask defines the legal combinations
164 * of elements for the MOVE MEDIUM command.
166 u_int8_t sc_movemask[CHET_MAX + 1];
169 * As above, but for EXCHANGE MEDIUM.
171 u_int8_t sc_exchangemask[CHET_MAX + 1];
174 * Quirks; see below. XXX KDM not implemented yet
176 int sc_settledelay; /* delay for settle */
179 static d_open_t chopen;
180 static d_close_t chclose;
181 static d_ioctl_t chioctl;
182 static periph_init_t chinit;
183 static periph_ctor_t chregister;
184 static periph_oninv_t choninvalidate;
185 static periph_dtor_t chcleanup;
186 static periph_start_t chstart;
187 static void chasync(void *callback_arg, u_int32_t code,
188 struct cam_path *path, void *arg);
189 static void chdone(struct cam_periph *periph,
190 union ccb *done_ccb);
191 static int cherror(union ccb *ccb, u_int32_t cam_flags,
192 u_int32_t sense_flags);
193 static int chmove(struct cam_periph *periph,
194 struct changer_move *cm);
195 static int chexchange(struct cam_periph *periph,
196 struct changer_exchange *ce);
197 static int chposition(struct cam_periph *periph,
198 struct changer_position *cp);
199 static int chgetelemstatus(struct cam_periph *periph,
200 int scsi_version, u_long cmd,
201 struct changer_element_status_request *csr);
202 static int chsetvoltag(struct cam_periph *periph,
203 struct changer_set_voltag_request *csvr);
204 static int chielem(struct cam_periph *periph,
205 unsigned int timeout);
206 static int chgetparams(struct cam_periph *periph);
207 static int chscsiversion(struct cam_periph *periph);
209 static struct periph_driver chdriver =
212 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
215 PERIPHDRIVER_DECLARE(ch, chdriver);
217 static struct cdevsw ch_cdevsw = {
218 .d_version = D_VERSION,
219 .d_flags = D_TRACKCLOSE,
226 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
234 * Install a global async callback. This callback will
235 * receive async callbacks like "new device found".
237 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
239 if (status != CAM_REQ_CMP) {
240 printf("ch: Failed to attach master async callback "
241 "due to status 0x%x!\n", status);
246 chdevgonecb(void *arg)
248 struct ch_softc *softc;
249 struct cam_periph *periph;
253 periph = (struct cam_periph *)arg;
254 mtx = cam_periph_mtx(periph);
257 softc = (struct ch_softc *)periph->softc;
258 KASSERT(softc->open_count >= 0, ("Negative open count %d",
262 * When we get this callback, we will get no more close calls from
263 * devfs. So if we have any dangling opens, we need to release the
264 * reference held for that particular context.
266 for (i = 0; i < softc->open_count; i++)
267 cam_periph_release_locked(periph);
269 softc->open_count = 0;
272 * Release the reference held for the device node, it is gone now.
274 cam_periph_release_locked(periph);
277 * We reference the lock directly here, instead of using
278 * cam_periph_unlock(). The reason is that the final call to
279 * cam_periph_release_locked() above could result in the periph
280 * getting freed. If that is the case, dereferencing the periph
281 * with a cam_periph_unlock() call would cause a page fault.
287 choninvalidate(struct cam_periph *periph)
289 struct ch_softc *softc;
291 softc = (struct ch_softc *)periph->softc;
294 * De-register any async callbacks.
296 xpt_register_async(0, chasync, periph, periph->path);
298 softc->flags |= CH_FLAG_INVALID;
301 * Tell devfs this device has gone away, and ask for a callback
302 * when it has cleaned up its state.
304 destroy_dev_sched_cb(softc->dev, chdevgonecb, periph);
308 chcleanup(struct cam_periph *periph)
310 struct ch_softc *softc;
312 softc = (struct ch_softc *)periph->softc;
314 devstat_remove_entry(softc->device_stats);
316 free(softc, M_DEVBUF);
320 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
322 struct cam_periph *periph;
324 periph = (struct cam_periph *)callback_arg;
327 case AC_FOUND_DEVICE:
329 struct ccb_getdev *cgd;
332 cgd = (struct ccb_getdev *)arg;
336 if (cgd->protocol != PROTO_SCSI)
338 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
340 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
344 * Allocate a peripheral instance for
345 * this device and start the probe
348 status = cam_periph_alloc(chregister, choninvalidate,
349 chcleanup, chstart, "ch",
350 CAM_PERIPH_BIO, path,
351 chasync, AC_FOUND_DEVICE, cgd);
353 if (status != CAM_REQ_CMP
354 && status != CAM_REQ_INPROG)
355 printf("chasync: Unable to probe new device "
356 "due to status 0x%x\n", status);
362 cam_periph_async(periph, code, path, arg);
368 chregister(struct cam_periph *periph, void *arg)
370 struct ch_softc *softc;
371 struct ccb_getdev *cgd;
372 struct ccb_pathinq cpi;
373 struct make_dev_args args;
376 cgd = (struct ccb_getdev *)arg;
378 printf("chregister: no getdev CCB, can't register device\n");
379 return(CAM_REQ_CMP_ERR);
382 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
385 printf("chregister: Unable to probe new device. "
386 "Unable to allocate softc\n");
387 return(CAM_REQ_CMP_ERR);
390 bzero(softc, sizeof(*softc));
391 softc->state = CH_STATE_PROBE;
392 periph->softc = softc;
393 softc->quirks = CH_Q_NONE;
396 * The DVCID and CURDATA bits were not introduced until the SMC
397 * spec. If this device claims SCSI-2 or earlier support, then it
398 * very likely does not support these bits.
400 if (cgd->inq_data.version <= SCSI_REV_2)
401 softc->quirks |= CH_Q_NO_DVCID;
403 xpt_path_inq(&cpi, periph->path);
406 * Changers don't have a blocksize, and obviously don't support
409 cam_periph_unlock(periph);
410 softc->device_stats = devstat_new_entry("ch",
411 periph->unit_number, 0,
412 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
413 SID_TYPE(&cgd->inq_data) |
414 XPORT_DEVSTAT_TYPE(cpi.transport),
415 DEVSTAT_PRIORITY_OTHER);
418 * Acquire a reference to the periph before we create the devfs
419 * instance for it. We'll release this reference once the devfs
420 * instance has been freed.
422 if (cam_periph_acquire(periph) != 0) {
423 xpt_print(periph->path, "%s: lost periph during "
424 "registration!\n", __func__);
425 cam_periph_lock(periph);
426 return (CAM_REQ_CMP_ERR);
430 /* Register the device */
431 make_dev_args_init(&args);
432 args.mda_devsw = &ch_cdevsw;
433 args.mda_unit = periph->unit_number;
434 args.mda_uid = UID_ROOT;
435 args.mda_gid = GID_OPERATOR;
436 args.mda_mode = 0600;
437 args.mda_si_drv1 = periph;
438 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
439 periph->unit_number);
440 cam_periph_lock(periph);
442 cam_periph_release_locked(periph);
443 return (CAM_REQ_CMP_ERR);
447 * Add an async callback so that we get
448 * notified if this device goes away.
450 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
453 * Lock this periph until we are setup.
454 * This first call can't block
456 (void)cam_periph_hold(periph, PRIBIO);
457 xpt_schedule(periph, CAM_PRIORITY_DEV);
463 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
465 struct cam_periph *periph;
466 struct ch_softc *softc;
469 periph = (struct cam_periph *)dev->si_drv1;
470 if (cam_periph_acquire(periph) != 0)
473 softc = (struct ch_softc *)periph->softc;
475 cam_periph_lock(periph);
477 if (softc->flags & CH_FLAG_INVALID) {
478 cam_periph_release_locked(periph);
479 cam_periph_unlock(periph);
483 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
484 cam_periph_unlock(periph);
485 cam_periph_release(periph);
490 * Load information about this changer device into the softc.
492 if ((error = chgetparams(periph)) != 0) {
493 cam_periph_unhold(periph);
494 cam_periph_release_locked(periph);
495 cam_periph_unlock(periph);
499 cam_periph_unhold(periph);
503 cam_periph_unlock(periph);
509 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
511 struct cam_periph *periph;
512 struct ch_softc *softc;
515 periph = (struct cam_periph *)dev->si_drv1;
516 mtx = cam_periph_mtx(periph);
519 softc = (struct ch_softc *)periph->softc;
522 cam_periph_release_locked(periph);
525 * We reference the lock directly here, instead of using
526 * cam_periph_unlock(). The reason is that the call to
527 * cam_periph_release_locked() above could result in the periph
528 * getting freed. If that is the case, dereferencing the periph
529 * with a cam_periph_unlock() call would cause a page fault.
531 * cam_periph_release() avoids this problem using the same method,
532 * but we're manually acquiring and dropping the lock here to
533 * protect the open count and avoid another lock acquisition and
542 chstart(struct cam_periph *periph, union ccb *start_ccb)
544 struct ch_softc *softc;
546 softc = (struct ch_softc *)periph->softc;
548 switch (softc->state) {
549 case CH_STATE_NORMAL:
551 xpt_release_ccb(start_ccb);
560 * Include the block descriptor when calculating the mode
563 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
564 sizeof(struct scsi_mode_blk_desc) +
565 sizeof(struct page_element_address_assignment);
567 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
569 if (mode_buffer == NULL) {
570 printf("chstart: couldn't malloc mode sense data\n");
573 bzero(mode_buffer, mode_buffer_len);
576 * Get the element address assignment page.
578 scsi_mode_sense(&start_ccb->csio,
581 /* tag_action */ MSG_SIMPLE_Q_TAG,
582 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
584 /* pc */ SMS_PAGE_CTRL_CURRENT,
585 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
586 /* param_buf */ (u_int8_t *)mode_buffer,
587 /* param_len */ mode_buffer_len,
588 /* sense_len */ SSD_FULL_SIZE,
589 /* timeout */ CH_TIMEOUT_MODE_SENSE);
591 start_ccb->ccb_h.ccb_bp = NULL;
592 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
593 xpt_action(start_ccb);
600 chdone(struct cam_periph *periph, union ccb *done_ccb)
602 struct ch_softc *softc;
603 struct ccb_scsiio *csio;
605 softc = (struct ch_softc *)periph->softc;
606 csio = &done_ccb->csio;
608 switch(done_ccb->ccb_h.ccb_state) {
611 struct scsi_mode_header_6 *mode_header;
612 struct page_element_address_assignment *ea;
613 char announce_buf[80];
616 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
618 ea = (struct page_element_address_assignment *)
619 find_mode_page_6(mode_header);
621 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
623 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
624 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
625 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
626 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
627 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
628 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
629 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
630 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
631 softc->sc_picker = softc->sc_firsts[CHET_MT];
633 #define PLURAL(c) (c) == 1 ? "" : "s"
634 snprintf(announce_buf, sizeof(announce_buf),
635 "%d slot%s, %d drive%s, "
636 "%d picker%s, %d portal%s",
637 softc->sc_counts[CHET_ST],
638 PLURAL(softc->sc_counts[CHET_ST]),
639 softc->sc_counts[CHET_DT],
640 PLURAL(softc->sc_counts[CHET_DT]),
641 softc->sc_counts[CHET_MT],
642 PLURAL(softc->sc_counts[CHET_MT]),
643 softc->sc_counts[CHET_IE],
644 PLURAL(softc->sc_counts[CHET_IE]));
646 if (announce_buf[0] != '\0') {
647 xpt_announce_periph(periph, announce_buf);
648 xpt_announce_quirks(periph, softc->quirks,
654 error = cherror(done_ccb, CAM_RETRY_SELTO,
655 SF_RETRY_UA | SF_NO_PRINT);
657 * Retry any UNIT ATTENTION type errors. They
658 * are expected at boot.
660 if (error == ERESTART) {
662 * A retry was scheduled, so
666 } else if (error != 0) {
667 struct scsi_mode_sense_6 *sms;
668 int frozen, retry_scheduled;
670 sms = (struct scsi_mode_sense_6 *)
671 done_ccb->csio.cdb_io.cdb_bytes;
672 frozen = (done_ccb->ccb_h.status &
676 * Check to see if block descriptors were
677 * disabled. Some devices don't like that.
678 * We're taking advantage of the fact that
679 * the first few bytes of the 6 and 10 byte
680 * mode sense commands are the same. If
681 * block descriptors were disabled, enable
682 * them and re-send the command.
684 if ((sms->byte2 & SMS_DBD) != 0 &&
685 (periph->flags & CAM_PERIPH_INVALID) == 0) {
686 sms->byte2 &= ~SMS_DBD;
687 xpt_action(done_ccb);
688 softc->quirks |= CH_Q_NO_DBD;
693 /* Don't wedge this device's queue */
695 cam_release_devq(done_ccb->ccb_h.path,
704 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
705 == CAM_SCSI_STATUS_ERROR)
706 scsi_sense_print(&done_ccb->csio);
708 xpt_print(periph->path,
709 "got CAM status %#x\n",
710 done_ccb->ccb_h.status);
712 xpt_print(periph->path, "fatal error, failed "
713 "to attach to device\n");
715 cam_periph_invalidate(periph);
719 softc->state = CH_STATE_NORMAL;
720 free(mode_header, M_SCSICH);
722 * Since our peripheral may be invalidated by an error
723 * above or an external event, we must release our CCB
724 * before releasing the probe lock on the peripheral.
725 * The peripheral will only go away once the last lock
726 * is removed, and we need it around for the CCB release
729 xpt_release_ccb(done_ccb);
730 cam_periph_unhold(periph);
736 xpt_release_ccb(done_ccb);
740 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
742 struct ch_softc *softc;
743 struct cam_periph *periph;
745 periph = xpt_path_periph(ccb->ccb_h.path);
746 softc = (struct ch_softc *)periph->softc;
748 return (cam_periph_error(ccb, cam_flags, sense_flags));
752 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
754 struct cam_periph *periph;
755 struct ch_softc *softc;
758 periph = (struct cam_periph *)dev->si_drv1;
759 cam_periph_lock(periph);
760 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
762 softc = (struct ch_softc *)periph->softc;
766 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
767 ("trying to do ioctl %#lx\n", cmd));
770 * If this command can change the device's state, we must
771 * have the device open for writing.
781 if ((flag & FWRITE) == 0) {
782 cam_periph_unlock(periph);
789 error = chmove(periph, (struct changer_move *)addr);
793 error = chexchange(periph, (struct changer_exchange *)addr);
797 error = chposition(periph, (struct changer_position *)addr);
801 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
806 int new_picker = *(int *)addr;
808 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
812 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
817 struct changer_params *cp = (struct changer_params *)addr;
819 cp->cp_npickers = softc->sc_counts[CHET_MT];
820 cp->cp_nslots = softc->sc_counts[CHET_ST];
821 cp->cp_nportals = softc->sc_counts[CHET_IE];
822 cp->cp_ndrives = softc->sc_counts[CHET_DT];
826 error = chielem(periph, *(unsigned int *)addr);
831 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
832 (struct changer_element_status_request *)addr);
840 scsi_version = chscsiversion(periph);
841 if (scsi_version >= SCSI_REV_0) {
842 error = chgetelemstatus(periph, scsi_version, cmd,
843 (struct changer_element_status_request *)addr);
845 else { /* unable to determine the SCSI version */
846 cam_periph_unlock(periph);
854 error = chsetvoltag(periph,
855 (struct changer_set_voltag_request *) addr);
859 /* Implement prevent/allow? */
862 error = cam_periph_ioctl(periph, cmd, addr, cherror);
866 cam_periph_unlock(periph);
871 chmove(struct cam_periph *periph, struct changer_move *cm)
873 struct ch_softc *softc;
874 u_int16_t fromelem, toelem;
879 softc = (struct ch_softc *)periph->softc;
884 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
886 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
887 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
891 * Check the request against the changer's capabilities.
893 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
897 * Calculate the source and destination elements.
899 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
900 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
902 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
904 scsi_move_medium(&ccb->csio,
907 /* tag_action */ MSG_SIMPLE_Q_TAG,
908 /* tea */ softc->sc_picker,
911 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
912 /* sense_len */ SSD_FULL_SIZE,
913 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
915 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
916 /*sense_flags*/ SF_RETRY_UA,
917 softc->device_stats);
919 xpt_release_ccb(ccb);
925 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
927 struct ch_softc *softc;
928 u_int16_t src, dst1, dst2;
933 softc = (struct ch_softc *)periph->softc;
937 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
938 (ce->ce_sdsttype > CHET_DT))
940 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
941 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
942 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
946 * Check the request against the changer's capabilities.
948 if (((softc->sc_exchangemask[ce->ce_srctype] &
949 (1 << ce->ce_fdsttype)) == 0) ||
950 ((softc->sc_exchangemask[ce->ce_fdsttype] &
951 (1 << ce->ce_sdsttype)) == 0))
955 * Calculate the source and destination elements.
957 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
958 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
959 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
961 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
963 scsi_exchange_medium(&ccb->csio,
966 /* tag_action */ MSG_SIMPLE_Q_TAG,
967 /* tea */ softc->sc_picker,
971 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
973 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
975 /* sense_len */ SSD_FULL_SIZE,
976 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
978 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
979 /*sense_flags*/ SF_RETRY_UA,
980 softc->device_stats);
982 xpt_release_ccb(ccb);
988 chposition(struct cam_periph *periph, struct changer_position *cp)
990 struct ch_softc *softc;
996 softc = (struct ch_softc *)periph->softc;
1001 if (cp->cp_type > CHET_DT)
1003 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1007 * Calculate the destination element.
1009 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1011 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1013 scsi_position_to_element(&ccb->csio,
1015 /* cbfcnp */ chdone,
1016 /* tag_action */ MSG_SIMPLE_Q_TAG,
1017 /* tea */ softc->sc_picker,
1019 /* invert */ (cp->cp_flags & CP_INVERT) ?
1021 /* sense_len */ SSD_FULL_SIZE,
1022 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1024 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1025 /*sense_flags*/ SF_RETRY_UA,
1026 softc->device_stats);
1028 xpt_release_ccb(ccb);
1034 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1035 * to host native byte order in the volume serial number. The volume
1036 * label as returned by the changer is transferred to user mode as
1037 * nul-terminated string. Volume labels are truncated at the first
1038 * space, as suggested by SCSI-2.
1041 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1044 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1045 char c = voltag->vif[i];
1047 uvoltag->cv_volid[i] = c;
1051 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1055 * Copy an element status descriptor to a user-mode
1056 * changer_element_status structure.
1059 copy_element_status(struct ch_softc *softc,
1061 struct read_element_status_descriptor *desc,
1062 struct changer_element_status *ces,
1065 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1067 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1068 struct read_element_status_device_id *devid = NULL;
1070 ces->ces_int_addr = eaddr;
1071 /* set up logical address in element status */
1072 for (et = CHET_MT; et <= CHET_DT; et++) {
1073 if ((softc->sc_firsts[et] <= eaddr)
1074 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1076 ces->ces_addr = eaddr - softc->sc_firsts[et];
1082 ces->ces_flags = desc->flags1;
1084 ces->ces_sensecode = desc->sense_code;
1085 ces->ces_sensequal = desc->sense_qual;
1087 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1088 ces->ces_flags |= CES_INVERT;
1090 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1092 eaddr = scsi_2btoul(desc->ssea);
1094 /* convert source address to logical format */
1095 for (et = CHET_MT; et <= CHET_DT; et++) {
1096 if ((softc->sc_firsts[et] <= eaddr)
1097 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1099 ces->ces_source_addr =
1100 eaddr - softc->sc_firsts[et];
1101 ces->ces_source_type = et;
1102 ces->ces_flags |= CES_SOURCE_VALID;
1107 if (!(ces->ces_flags & CES_SOURCE_VALID))
1108 printf("ch: warning: could not map element source "
1109 "address %ud to a valid element type\n",
1114 * pvoltag and avoltag are common between SCSI-2 and later versions
1116 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1117 pvol_tag = &desc->voltag_devid.pvoltag;
1118 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1119 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1120 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1122 * For SCSI-3 and later, element status can carry designator and
1123 * other information.
1125 if (scsi_version >= SCSI_REV_SPC) {
1126 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1127 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1128 devid = &desc->voltag_devid.pvol_and_devid.devid;
1129 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1130 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1131 devid = &desc->voltag_devid.devid;
1132 else /* Have both PVOLTAG and AVOLTAG */
1133 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1137 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1139 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1140 if (devid != NULL) {
1141 if (devid->designator_length > 0) {
1142 bcopy((void *)devid->designator,
1143 (void *)ces->ces_designator,
1144 devid->designator_length);
1145 ces->ces_designator_length = devid->designator_length;
1147 * Make sure we are always NUL terminated. The
1148 * This won't matter for the binary code set,
1149 * since the user will only pay attention to the
1152 ces->ces_designator[devid->designator_length]= '\0';
1154 if (devid->piv_assoc_designator_type &
1155 READ_ELEMENT_STATUS_PIV_SET) {
1156 ces->ces_flags |= CES_PIV;
1157 ces->ces_protocol_id =
1158 READ_ELEMENT_STATUS_PROTOCOL_ID(
1159 devid->prot_code_set);
1162 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1163 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1164 devid->piv_assoc_designator_type);
1165 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1166 devid->piv_assoc_designator_type);
1167 } else if (scsi_version > SCSI_REV_2) {
1168 /* SCSI-SPC and No devid, no designator */
1169 ces->ces_designator_length = 0;
1170 ces->ces_designator[0] = '\0';
1171 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1174 if (scsi_version <= SCSI_REV_2) {
1175 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1176 READ_ELEMENT_STATUS_DT_IDVALID) {
1177 ces->ces_flags |= CES_SCSIID_VALID;
1179 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1182 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1183 READ_ELEMENT_STATUS_DT_LUVALID) {
1184 ces->ces_flags |= CES_LUN_VALID;
1186 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1187 READ_ELEMENT_STATUS_DT_LUNMASK;
1193 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1194 struct changer_element_status_request *cesr)
1196 struct read_element_status_header *st_hdr;
1197 struct read_element_status_page_header *pg_hdr;
1198 struct read_element_status_descriptor *desc;
1199 caddr_t data = NULL;
1200 size_t size, desclen;
1202 int curdata, dvcid, sense_flags;
1203 int try_no_dvcid = 0;
1204 struct changer_element_status *user_data = NULL;
1205 struct ch_softc *softc;
1207 int chet = cesr->cesr_element_type;
1209 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1211 softc = (struct ch_softc *)periph->softc;
1213 /* perform argument checking */
1216 * Perform a range check on the cesr_element_{base,count}
1217 * request argument fields.
1219 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1220 || (cesr->cesr_element_base + cesr->cesr_element_count)
1221 > softc->sc_counts[chet])
1225 * Request one descriptor for the given element type. This
1226 * is used to determine the size of the descriptor so that
1227 * we can allocate enough storage for all of them. We assume
1228 * that the first one can fit into 1k.
1230 cam_periph_unlock(periph);
1231 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1233 cam_periph_lock(periph);
1234 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1236 sense_flags = SF_RETRY_UA;
1237 if (softc->quirks & CH_Q_NO_DVCID) {
1244 * Don't print anything for an Illegal Request, because
1245 * these flags can cause some changers to complain. We'll
1246 * retry without them if we get an error.
1248 sense_flags |= SF_QUIET_IR;
1253 scsi_read_element_status(&ccb->csio,
1255 /* cbfcnp */ chdone,
1256 /* tag_action */ MSG_SIMPLE_Q_TAG,
1257 /* voltag */ want_voltags,
1258 /* sea */ softc->sc_firsts[chet],
1259 /* curdata */ curdata,
1262 /* data_ptr */ data,
1263 /* dxfer_len */ 1024,
1264 /* sense_len */ SSD_FULL_SIZE,
1265 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1267 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1268 /*sense_flags*/ sense_flags,
1269 softc->device_stats);
1272 * An Illegal Request sense key (only used if there is no asc/ascq)
1273 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1274 * curdata are set (we set both or neither), try turning them off
1275 * and see if the command is successful.
1277 if ((error == EINVAL)
1278 && (dvcid || curdata)) {
1282 /* At this point we want to report any Illegal Request */
1283 sense_flags &= ~SF_QUIET_IR;
1289 * In this case, we tried a read element status with dvcid and
1290 * curdata set, and it failed. We retried without those bits, and
1291 * it succeeded. Suggest to the user that he set a quirk, so we
1292 * don't go through the retry process the first time in the future.
1293 * This should only happen on changers that claim SCSI-3 or higher,
1294 * but don't support these bits.
1296 if ((try_no_dvcid != 0)
1298 softc->quirks |= CH_Q_NO_DVCID;
1302 cam_periph_unlock(periph);
1304 st_hdr = (struct read_element_status_header *)data;
1305 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1306 sizeof(struct read_element_status_header));
1307 desclen = scsi_2btoul(pg_hdr->edl);
1309 size = sizeof(struct read_element_status_header) +
1310 sizeof(struct read_element_status_page_header) +
1311 (desclen * cesr->cesr_element_count);
1313 * Reallocate storage for descriptors and get them from the
1316 free(data, M_DEVBUF);
1317 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1319 cam_periph_lock(periph);
1320 scsi_read_element_status(&ccb->csio,
1322 /* cbfcnp */ chdone,
1323 /* tag_action */ MSG_SIMPLE_Q_TAG,
1324 /* voltag */ want_voltags,
1325 /* sea */ softc->sc_firsts[chet]
1326 + cesr->cesr_element_base,
1327 /* curdata */ curdata,
1329 /* count */ cesr->cesr_element_count,
1330 /* data_ptr */ data,
1331 /* dxfer_len */ size,
1332 /* sense_len */ SSD_FULL_SIZE,
1333 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1335 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1336 /*sense_flags*/ SF_RETRY_UA,
1337 softc->device_stats);
1341 cam_periph_unlock(periph);
1344 * Fill in the user status array.
1346 st_hdr = (struct read_element_status_header *)data;
1347 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1348 sizeof(struct read_element_status_header));
1349 avail = scsi_2btoul(st_hdr->count);
1351 if (avail != cesr->cesr_element_count) {
1352 xpt_print(periph->path,
1353 "warning, READ ELEMENT STATUS avail != count\n");
1356 user_data = (struct changer_element_status *)
1357 malloc(avail * sizeof(struct changer_element_status),
1358 M_DEVBUF, M_WAITOK | M_ZERO);
1360 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1361 sizeof(struct read_element_status_header) +
1362 sizeof(struct read_element_status_page_header));
1364 * Set up the individual element status structures
1366 for (i = 0; i < avail; ++i) {
1367 struct changer_element_status *ces;
1370 * In the changer_element_status structure, fields from
1371 * the beginning to the field of ces_scsi_lun are common
1372 * between SCSI-2 and SCSI-3, while all the rest are new
1373 * from SCSI-3. In order to maintain backward compatibility
1374 * of the chio command, the ces pointer, below, is computed
1375 * such that it lines up with the structure boundary
1376 * corresponding to the SCSI version.
1378 ces = cmd == OCHIOGSTATUS ?
1379 (struct changer_element_status *)
1380 ((unsigned char *)user_data + i *
1381 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1384 copy_element_status(softc, pg_hdr->flags, desc,
1387 desc = (struct read_element_status_descriptor *)
1388 ((unsigned char *)desc + desclen);
1391 /* Copy element status structures out to userspace. */
1392 if (cmd == OCHIOGSTATUS)
1393 error = copyout(user_data,
1394 cesr->cesr_element_status,
1395 avail* (offsetof(struct changer_element_status,
1396 ces_scsi_lun) + 1));
1398 error = copyout(user_data,
1399 cesr->cesr_element_status,
1400 avail * sizeof(struct changer_element_status));
1402 cam_periph_lock(periph);
1405 xpt_release_ccb(ccb);
1408 free(data, M_DEVBUF);
1409 if (user_data != NULL)
1410 free(user_data, M_DEVBUF);
1416 chielem(struct cam_periph *periph,
1417 unsigned int timeout)
1420 struct ch_softc *softc;
1424 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1430 softc = (struct ch_softc *)periph->softc;
1432 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1434 scsi_initialize_element_status(&ccb->csio,
1436 /* cbfcnp */ chdone,
1437 /* tag_action */ MSG_SIMPLE_Q_TAG,
1438 /* sense_len */ SSD_FULL_SIZE,
1439 /* timeout */ timeout);
1441 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1442 /*sense_flags*/ SF_RETRY_UA,
1443 softc->device_stats);
1445 xpt_release_ccb(ccb);
1451 chsetvoltag(struct cam_periph *periph,
1452 struct changer_set_voltag_request *csvr)
1455 struct ch_softc *softc;
1458 struct scsi_send_volume_tag_parameters ssvtp;
1463 softc = (struct ch_softc *)periph->softc;
1465 bzero(&ssvtp, sizeof(ssvtp));
1466 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1467 ssvtp.vitf[i] = ' ';
1473 if (csvr->csvr_type > CHET_DT)
1475 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1478 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1480 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1481 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1483 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1485 case CSVR_MODE_REPLACE:
1486 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1488 case CSVR_MODE_CLEAR:
1489 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1496 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1498 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1500 case CSVR_MODE_REPLACE:
1501 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1503 case CSVR_MODE_CLEAR:
1504 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1512 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1513 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1514 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1516 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1518 scsi_send_volume_tag(&ccb->csio,
1520 /* cbfcnp */ chdone,
1521 /* tag_action */ MSG_SIMPLE_Q_TAG,
1522 /* element_address */ ea,
1523 /* send_action_code */ sac,
1524 /* parameters */ &ssvtp,
1525 /* sense_len */ SSD_FULL_SIZE,
1526 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1528 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1529 /*sense_flags*/ SF_RETRY_UA,
1530 softc->device_stats);
1532 xpt_release_ccb(ccb);
1539 chgetparams(struct cam_periph *periph)
1542 struct ch_softc *softc;
1544 int mode_buffer_len;
1545 struct page_element_address_assignment *ea;
1546 struct page_device_capabilities *cap;
1547 int error, from, dbd;
1548 u_int8_t *moves, *exchanges;
1552 softc = (struct ch_softc *)periph->softc;
1554 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1557 * The scsi_mode_sense_data structure is just a convenience
1558 * structure that allows us to easily calculate the worst-case
1559 * storage size of the mode sense buffer.
1561 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1563 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1565 if (mode_buffer == NULL) {
1566 printf("chgetparams: couldn't malloc mode sense data\n");
1567 xpt_release_ccb(ccb);
1571 bzero(mode_buffer, mode_buffer_len);
1573 if (softc->quirks & CH_Q_NO_DBD)
1579 * Get the element address assignment page.
1581 scsi_mode_sense(&ccb->csio,
1583 /* cbfcnp */ chdone,
1584 /* tag_action */ MSG_SIMPLE_Q_TAG,
1586 /* pc */ SMS_PAGE_CTRL_CURRENT,
1587 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1588 /* param_buf */ (u_int8_t *)mode_buffer,
1589 /* param_len */ mode_buffer_len,
1590 /* sense_len */ SSD_FULL_SIZE,
1591 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1593 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1594 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1595 softc->device_stats);
1599 struct scsi_mode_sense_6 *sms;
1601 sms = (struct scsi_mode_sense_6 *)
1602 ccb->csio.cdb_io.cdb_bytes;
1604 sms->byte2 &= ~SMS_DBD;
1605 error = cam_periph_runccb(ccb, cherror,
1606 /*cam_flags*/ CAM_RETRY_SELTO,
1607 /*sense_flags*/ SF_RETRY_UA,
1608 softc->device_stats);
1611 * Since we disabled sense printing above, print
1612 * out the sense here since we got an error.
1614 scsi_sense_print(&ccb->csio);
1618 xpt_print(periph->path,
1619 "chgetparams: error getting element "
1621 xpt_release_ccb(ccb);
1622 free(mode_buffer, M_SCSICH);
1627 ea = (struct page_element_address_assignment *)
1628 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1630 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1631 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1632 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1633 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1634 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1635 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1636 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1637 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1639 bzero(mode_buffer, mode_buffer_len);
1642 * Now get the device capabilities page.
1644 scsi_mode_sense(&ccb->csio,
1646 /* cbfcnp */ chdone,
1647 /* tag_action */ MSG_SIMPLE_Q_TAG,
1649 /* pc */ SMS_PAGE_CTRL_CURRENT,
1650 /* page */ CH_DEVICE_CAP_PAGE,
1651 /* param_buf */ (u_int8_t *)mode_buffer,
1652 /* param_len */ mode_buffer_len,
1653 /* sense_len */ SSD_FULL_SIZE,
1654 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1656 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1657 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1658 softc->device_stats);
1662 struct scsi_mode_sense_6 *sms;
1664 sms = (struct scsi_mode_sense_6 *)
1665 ccb->csio.cdb_io.cdb_bytes;
1667 sms->byte2 &= ~SMS_DBD;
1668 error = cam_periph_runccb(ccb, cherror,
1669 /*cam_flags*/ CAM_RETRY_SELTO,
1670 /*sense_flags*/ SF_RETRY_UA,
1671 softc->device_stats);
1674 * Since we disabled sense printing above, print
1675 * out the sense here since we got an error.
1677 scsi_sense_print(&ccb->csio);
1681 xpt_print(periph->path,
1682 "chgetparams: error getting device "
1683 "capabilities page\n");
1684 xpt_release_ccb(ccb);
1685 free(mode_buffer, M_SCSICH);
1690 xpt_release_ccb(ccb);
1692 cap = (struct page_device_capabilities *)
1693 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1695 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1696 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1697 moves = cap->move_from;
1698 exchanges = cap->exchange_with;
1699 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1700 softc->sc_movemask[from] = moves[from];
1701 softc->sc_exchangemask[from] = exchanges[from];
1704 free(mode_buffer, M_SCSICH);
1710 chscsiversion(struct cam_periph *periph)
1712 struct scsi_inquiry_data *inq_data;
1713 struct ccb_getdev *cgd;
1714 int dev_scsi_version;
1716 cam_periph_assert(periph, MA_OWNED);
1717 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1720 * Get the device information.
1722 xpt_setup_ccb(&cgd->ccb_h,
1724 CAM_PRIORITY_NORMAL);
1725 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1726 xpt_action((union ccb *)cgd);
1728 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1729 xpt_free_ccb((union ccb *)cgd);
1733 inq_data = &cgd->inq_data;
1734 dev_scsi_version = inq_data->version;
1735 xpt_free_ccb((union ccb *)cgd);
1737 return dev_scsi_version;
1741 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1742 void (*cbfcnp)(struct cam_periph *, union ccb *),
1743 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1744 u_int32_t dst, int invert, u_int8_t sense_len,
1747 struct scsi_move_medium *scsi_cmd;
1749 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1750 bzero(scsi_cmd, sizeof(*scsi_cmd));
1752 scsi_cmd->opcode = MOVE_MEDIUM;
1754 scsi_ulto2b(tea, scsi_cmd->tea);
1755 scsi_ulto2b(src, scsi_cmd->src);
1756 scsi_ulto2b(dst, scsi_cmd->dst);
1759 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1764 /*flags*/ CAM_DIR_NONE,
1774 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1775 void (*cbfcnp)(struct cam_periph *, union ccb *),
1776 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1777 u_int32_t dst1, u_int32_t dst2, int invert1,
1778 int invert2, u_int8_t sense_len, u_int32_t timeout)
1780 struct scsi_exchange_medium *scsi_cmd;
1782 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1783 bzero(scsi_cmd, sizeof(*scsi_cmd));
1785 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1787 scsi_ulto2b(tea, scsi_cmd->tea);
1788 scsi_ulto2b(src, scsi_cmd->src);
1789 scsi_ulto2b(dst1, scsi_cmd->fdst);
1790 scsi_ulto2b(dst2, scsi_cmd->sdst);
1793 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1796 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1801 /*flags*/ CAM_DIR_NONE,
1811 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1812 void (*cbfcnp)(struct cam_periph *, union ccb *),
1813 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1814 int invert, u_int8_t sense_len, u_int32_t timeout)
1816 struct scsi_position_to_element *scsi_cmd;
1818 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1819 bzero(scsi_cmd, sizeof(*scsi_cmd));
1821 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1823 scsi_ulto2b(tea, scsi_cmd->tea);
1824 scsi_ulto2b(dst, scsi_cmd->dst);
1827 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1832 /*flags*/ CAM_DIR_NONE,
1842 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1843 void (*cbfcnp)(struct cam_periph *, union ccb *),
1844 u_int8_t tag_action, int voltag, u_int32_t sea,
1845 int curdata, int dvcid,
1846 u_int32_t count, u_int8_t *data_ptr,
1847 u_int32_t dxfer_len, u_int8_t sense_len,
1850 struct scsi_read_element_status *scsi_cmd;
1852 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1853 bzero(scsi_cmd, sizeof(*scsi_cmd));
1855 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1857 scsi_ulto2b(sea, scsi_cmd->sea);
1858 scsi_ulto2b(count, scsi_cmd->count);
1859 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1861 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1863 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1866 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1871 /*flags*/ CAM_DIR_IN,
1881 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1882 void (*cbfcnp)(struct cam_periph *, union ccb *),
1883 u_int8_t tag_action, u_int8_t sense_len,
1886 struct scsi_initialize_element_status *scsi_cmd;
1888 scsi_cmd = (struct scsi_initialize_element_status *)
1889 &csio->cdb_io.cdb_bytes;
1890 bzero(scsi_cmd, sizeof(*scsi_cmd));
1892 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1897 /*flags*/ CAM_DIR_NONE,
1899 /* data_ptr */ NULL,
1907 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1908 void (*cbfcnp)(struct cam_periph *, union ccb *),
1909 u_int8_t tag_action,
1910 u_int16_t element_address,
1911 u_int8_t send_action_code,
1912 struct scsi_send_volume_tag_parameters *parameters,
1913 u_int8_t sense_len, u_int32_t timeout)
1915 struct scsi_send_volume_tag *scsi_cmd;
1917 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1918 bzero(scsi_cmd, sizeof(*scsi_cmd));
1920 scsi_cmd->opcode = SEND_VOLUME_TAG;
1921 scsi_ulto2b(element_address, scsi_cmd->ea);
1922 scsi_cmd->sac = send_action_code;
1923 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1928 /*flags*/ CAM_DIR_OUT,
1930 /* data_ptr */ (u_int8_t *) parameters,
1931 sizeof(*parameters),