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) != CAM_REQ_CMP) {
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) != CAM_REQ_CMP)
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;
1201 int avail, i, error = 0;
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;
1208 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1210 softc = (struct ch_softc *)periph->softc;
1212 /* perform argument checking */
1215 * Perform a range check on the cesr_element_{base,count}
1216 * request argument fields.
1218 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1219 || (cesr->cesr_element_base + cesr->cesr_element_count)
1220 > softc->sc_counts[chet])
1224 * Request one descriptor for the given element type. This
1225 * is used to determine the size of the descriptor so that
1226 * we can allocate enough storage for all of them. We assume
1227 * that the first one can fit into 1k.
1229 cam_periph_unlock(periph);
1230 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1232 cam_periph_lock(periph);
1233 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1235 sense_flags = SF_RETRY_UA;
1236 if (softc->quirks & CH_Q_NO_DVCID) {
1243 * Don't print anything for an Illegal Request, because
1244 * these flags can cause some changers to complain. We'll
1245 * retry without them if we get an error.
1247 sense_flags |= SF_QUIET_IR;
1252 scsi_read_element_status(&ccb->csio,
1254 /* cbfcnp */ chdone,
1255 /* tag_action */ MSG_SIMPLE_Q_TAG,
1256 /* voltag */ want_voltags,
1257 /* sea */ softc->sc_firsts[chet],
1258 /* curdata */ curdata,
1261 /* data_ptr */ data,
1262 /* dxfer_len */ 1024,
1263 /* sense_len */ SSD_FULL_SIZE,
1264 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1266 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1267 /*sense_flags*/ sense_flags,
1268 softc->device_stats);
1271 * An Illegal Request sense key (only used if there is no asc/ascq)
1272 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1273 * curdata are set (we set both or neither), try turning them off
1274 * and see if the command is successful.
1276 if ((error == EINVAL)
1277 && (dvcid || curdata)) {
1281 /* At this point we want to report any Illegal Request */
1282 sense_flags &= ~SF_QUIET_IR;
1288 * In this case, we tried a read element status with dvcid and
1289 * curdata set, and it failed. We retried without those bits, and
1290 * it succeeded. Suggest to the user that he set a quirk, so we
1291 * don't go through the retry process the first time in the future.
1292 * This should only happen on changers that claim SCSI-3 or higher,
1293 * but don't support these bits.
1295 if ((try_no_dvcid != 0)
1297 softc->quirks |= CH_Q_NO_DVCID;
1301 cam_periph_unlock(periph);
1303 st_hdr = (struct read_element_status_header *)data;
1304 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1305 sizeof(struct read_element_status_header));
1306 desclen = scsi_2btoul(pg_hdr->edl);
1308 size = sizeof(struct read_element_status_header) +
1309 sizeof(struct read_element_status_page_header) +
1310 (desclen * cesr->cesr_element_count);
1312 * Reallocate storage for descriptors and get them from the
1315 free(data, M_DEVBUF);
1316 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1318 cam_periph_lock(periph);
1319 scsi_read_element_status(&ccb->csio,
1321 /* cbfcnp */ chdone,
1322 /* tag_action */ MSG_SIMPLE_Q_TAG,
1323 /* voltag */ want_voltags,
1324 /* sea */ softc->sc_firsts[chet]
1325 + cesr->cesr_element_base,
1326 /* curdata */ curdata,
1328 /* count */ cesr->cesr_element_count,
1329 /* data_ptr */ data,
1330 /* dxfer_len */ size,
1331 /* sense_len */ SSD_FULL_SIZE,
1332 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1334 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1335 /*sense_flags*/ SF_RETRY_UA,
1336 softc->device_stats);
1340 cam_periph_unlock(periph);
1343 * Fill in the user status array.
1345 st_hdr = (struct read_element_status_header *)data;
1346 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1347 sizeof(struct read_element_status_header));
1348 avail = scsi_2btoul(st_hdr->count);
1350 if (avail != cesr->cesr_element_count) {
1351 xpt_print(periph->path,
1352 "warning, READ ELEMENT STATUS avail != count\n");
1355 user_data = (struct changer_element_status *)
1356 malloc(avail * sizeof(struct changer_element_status),
1357 M_DEVBUF, M_WAITOK | M_ZERO);
1359 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1360 sizeof(struct read_element_status_header) +
1361 sizeof(struct read_element_status_page_header));
1363 * Set up the individual element status structures
1365 for (i = 0; i < avail; ++i) {
1366 struct changer_element_status *ces;
1369 * In the changer_element_status structure, fields from
1370 * the beginning to the field of ces_scsi_lun are common
1371 * between SCSI-2 and SCSI-3, while all the rest are new
1372 * from SCSI-3. In order to maintain backward compatibility
1373 * of the chio command, the ces pointer, below, is computed
1374 * such that it lines up with the structure boundary
1375 * corresponding to the SCSI version.
1377 ces = cmd == OCHIOGSTATUS ?
1378 (struct changer_element_status *)
1379 ((unsigned char *)user_data + i *
1380 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1383 copy_element_status(softc, pg_hdr->flags, desc,
1386 desc = (struct read_element_status_descriptor *)
1387 ((unsigned char *)desc + desclen);
1390 /* Copy element status structures out to userspace. */
1391 if (cmd == OCHIOGSTATUS)
1392 error = copyout(user_data,
1393 cesr->cesr_element_status,
1394 avail* (offsetof(struct changer_element_status,
1395 ces_scsi_lun) + 1));
1397 error = copyout(user_data,
1398 cesr->cesr_element_status,
1399 avail * sizeof(struct changer_element_status));
1401 cam_periph_lock(periph);
1404 xpt_release_ccb(ccb);
1407 free(data, M_DEVBUF);
1408 if (user_data != NULL)
1409 free(user_data, M_DEVBUF);
1415 chielem(struct cam_periph *periph,
1416 unsigned int timeout)
1419 struct ch_softc *softc;
1423 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1429 softc = (struct ch_softc *)periph->softc;
1431 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1433 scsi_initialize_element_status(&ccb->csio,
1435 /* cbfcnp */ chdone,
1436 /* tag_action */ MSG_SIMPLE_Q_TAG,
1437 /* sense_len */ SSD_FULL_SIZE,
1438 /* timeout */ timeout);
1440 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1441 /*sense_flags*/ SF_RETRY_UA,
1442 softc->device_stats);
1444 xpt_release_ccb(ccb);
1450 chsetvoltag(struct cam_periph *periph,
1451 struct changer_set_voltag_request *csvr)
1454 struct ch_softc *softc;
1457 struct scsi_send_volume_tag_parameters ssvtp;
1462 softc = (struct ch_softc *)periph->softc;
1464 bzero(&ssvtp, sizeof(ssvtp));
1465 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1466 ssvtp.vitf[i] = ' ';
1472 if (csvr->csvr_type > CHET_DT)
1474 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1477 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1479 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1480 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1482 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1484 case CSVR_MODE_REPLACE:
1485 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1487 case CSVR_MODE_CLEAR:
1488 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1495 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1497 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1499 case CSVR_MODE_REPLACE:
1500 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1502 case CSVR_MODE_CLEAR:
1503 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1511 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1512 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1513 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1515 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1517 scsi_send_volume_tag(&ccb->csio,
1519 /* cbfcnp */ chdone,
1520 /* tag_action */ MSG_SIMPLE_Q_TAG,
1521 /* element_address */ ea,
1522 /* send_action_code */ sac,
1523 /* parameters */ &ssvtp,
1524 /* sense_len */ SSD_FULL_SIZE,
1525 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1527 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1528 /*sense_flags*/ SF_RETRY_UA,
1529 softc->device_stats);
1531 xpt_release_ccb(ccb);
1538 chgetparams(struct cam_periph *periph)
1541 struct ch_softc *softc;
1543 int mode_buffer_len;
1544 struct page_element_address_assignment *ea;
1545 struct page_device_capabilities *cap;
1546 int error, from, dbd;
1547 u_int8_t *moves, *exchanges;
1551 softc = (struct ch_softc *)periph->softc;
1553 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1556 * The scsi_mode_sense_data structure is just a convenience
1557 * structure that allows us to easily calculate the worst-case
1558 * storage size of the mode sense buffer.
1560 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1562 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1564 if (mode_buffer == NULL) {
1565 printf("chgetparams: couldn't malloc mode sense data\n");
1566 xpt_release_ccb(ccb);
1570 bzero(mode_buffer, mode_buffer_len);
1572 if (softc->quirks & CH_Q_NO_DBD)
1578 * Get the element address assignment page.
1580 scsi_mode_sense(&ccb->csio,
1582 /* cbfcnp */ chdone,
1583 /* tag_action */ MSG_SIMPLE_Q_TAG,
1585 /* pc */ SMS_PAGE_CTRL_CURRENT,
1586 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1587 /* param_buf */ (u_int8_t *)mode_buffer,
1588 /* param_len */ mode_buffer_len,
1589 /* sense_len */ SSD_FULL_SIZE,
1590 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1592 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1593 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1594 softc->device_stats);
1598 struct scsi_mode_sense_6 *sms;
1600 sms = (struct scsi_mode_sense_6 *)
1601 ccb->csio.cdb_io.cdb_bytes;
1603 sms->byte2 &= ~SMS_DBD;
1604 error = cam_periph_runccb(ccb, cherror,
1605 /*cam_flags*/ CAM_RETRY_SELTO,
1606 /*sense_flags*/ SF_RETRY_UA,
1607 softc->device_stats);
1610 * Since we disabled sense printing above, print
1611 * out the sense here since we got an error.
1613 scsi_sense_print(&ccb->csio);
1617 xpt_print(periph->path,
1618 "chgetparams: error getting element "
1620 xpt_release_ccb(ccb);
1621 free(mode_buffer, M_SCSICH);
1626 ea = (struct page_element_address_assignment *)
1627 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1629 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1630 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1631 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1632 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1633 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1634 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1635 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1636 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1638 bzero(mode_buffer, mode_buffer_len);
1641 * Now get the device capabilities page.
1643 scsi_mode_sense(&ccb->csio,
1645 /* cbfcnp */ chdone,
1646 /* tag_action */ MSG_SIMPLE_Q_TAG,
1648 /* pc */ SMS_PAGE_CTRL_CURRENT,
1649 /* page */ CH_DEVICE_CAP_PAGE,
1650 /* param_buf */ (u_int8_t *)mode_buffer,
1651 /* param_len */ mode_buffer_len,
1652 /* sense_len */ SSD_FULL_SIZE,
1653 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1655 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1656 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1657 softc->device_stats);
1661 struct scsi_mode_sense_6 *sms;
1663 sms = (struct scsi_mode_sense_6 *)
1664 ccb->csio.cdb_io.cdb_bytes;
1666 sms->byte2 &= ~SMS_DBD;
1667 error = cam_periph_runccb(ccb, cherror,
1668 /*cam_flags*/ CAM_RETRY_SELTO,
1669 /*sense_flags*/ SF_RETRY_UA,
1670 softc->device_stats);
1673 * Since we disabled sense printing above, print
1674 * out the sense here since we got an error.
1676 scsi_sense_print(&ccb->csio);
1680 xpt_print(periph->path,
1681 "chgetparams: error getting device "
1682 "capabilities page\n");
1683 xpt_release_ccb(ccb);
1684 free(mode_buffer, M_SCSICH);
1689 xpt_release_ccb(ccb);
1691 cap = (struct page_device_capabilities *)
1692 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1694 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1695 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1696 moves = cap->move_from;
1697 exchanges = cap->exchange_with;
1698 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1699 softc->sc_movemask[from] = moves[from];
1700 softc->sc_exchangemask[from] = exchanges[from];
1703 free(mode_buffer, M_SCSICH);
1709 chscsiversion(struct cam_periph *periph)
1711 struct scsi_inquiry_data *inq_data;
1712 struct ccb_getdev *cgd;
1713 int dev_scsi_version;
1715 cam_periph_assert(periph, MA_OWNED);
1716 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1719 * Get the device information.
1721 xpt_setup_ccb(&cgd->ccb_h,
1723 CAM_PRIORITY_NORMAL);
1724 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1725 xpt_action((union ccb *)cgd);
1727 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1728 xpt_free_ccb((union ccb *)cgd);
1732 inq_data = &cgd->inq_data;
1733 dev_scsi_version = inq_data->version;
1734 xpt_free_ccb((union ccb *)cgd);
1736 return dev_scsi_version;
1740 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1741 void (*cbfcnp)(struct cam_periph *, union ccb *),
1742 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1743 u_int32_t dst, int invert, u_int8_t sense_len,
1746 struct scsi_move_medium *scsi_cmd;
1748 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1749 bzero(scsi_cmd, sizeof(*scsi_cmd));
1751 scsi_cmd->opcode = MOVE_MEDIUM;
1753 scsi_ulto2b(tea, scsi_cmd->tea);
1754 scsi_ulto2b(src, scsi_cmd->src);
1755 scsi_ulto2b(dst, scsi_cmd->dst);
1758 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1763 /*flags*/ CAM_DIR_NONE,
1773 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1774 void (*cbfcnp)(struct cam_periph *, union ccb *),
1775 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1776 u_int32_t dst1, u_int32_t dst2, int invert1,
1777 int invert2, u_int8_t sense_len, u_int32_t timeout)
1779 struct scsi_exchange_medium *scsi_cmd;
1781 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1782 bzero(scsi_cmd, sizeof(*scsi_cmd));
1784 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1786 scsi_ulto2b(tea, scsi_cmd->tea);
1787 scsi_ulto2b(src, scsi_cmd->src);
1788 scsi_ulto2b(dst1, scsi_cmd->fdst);
1789 scsi_ulto2b(dst2, scsi_cmd->sdst);
1792 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1795 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1800 /*flags*/ CAM_DIR_NONE,
1810 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1811 void (*cbfcnp)(struct cam_periph *, union ccb *),
1812 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1813 int invert, u_int8_t sense_len, u_int32_t timeout)
1815 struct scsi_position_to_element *scsi_cmd;
1817 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1818 bzero(scsi_cmd, sizeof(*scsi_cmd));
1820 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1822 scsi_ulto2b(tea, scsi_cmd->tea);
1823 scsi_ulto2b(dst, scsi_cmd->dst);
1826 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1831 /*flags*/ CAM_DIR_NONE,
1841 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1842 void (*cbfcnp)(struct cam_periph *, union ccb *),
1843 u_int8_t tag_action, int voltag, u_int32_t sea,
1844 int curdata, int dvcid,
1845 u_int32_t count, u_int8_t *data_ptr,
1846 u_int32_t dxfer_len, u_int8_t sense_len,
1849 struct scsi_read_element_status *scsi_cmd;
1851 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1852 bzero(scsi_cmd, sizeof(*scsi_cmd));
1854 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1856 scsi_ulto2b(sea, scsi_cmd->sea);
1857 scsi_ulto2b(count, scsi_cmd->count);
1858 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1860 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1862 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1865 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1870 /*flags*/ CAM_DIR_IN,
1880 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1881 void (*cbfcnp)(struct cam_periph *, union ccb *),
1882 u_int8_t tag_action, u_int8_t sense_len,
1885 struct scsi_initialize_element_status *scsi_cmd;
1887 scsi_cmd = (struct scsi_initialize_element_status *)
1888 &csio->cdb_io.cdb_bytes;
1889 bzero(scsi_cmd, sizeof(*scsi_cmd));
1891 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1896 /*flags*/ CAM_DIR_NONE,
1898 /* data_ptr */ NULL,
1906 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1907 void (*cbfcnp)(struct cam_periph *, union ccb *),
1908 u_int8_t tag_action,
1909 u_int16_t element_address,
1910 u_int8_t send_action_code,
1911 struct scsi_send_volume_tag_parameters *parameters,
1912 u_int8_t sense_len, u_int32_t timeout)
1914 struct scsi_send_volume_tag *scsi_cmd;
1916 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1917 bzero(scsi_cmd, sizeof(*scsi_cmd));
1919 scsi_cmd->opcode = SEND_VOLUME_TAG;
1920 scsi_ulto2b(element_address, scsi_cmd->ea);
1921 scsi_cmd->sac = send_action_code;
1922 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1927 /*flags*/ CAM_DIR_OUT,
1929 /* data_ptr */ (u_int8_t *) parameters,
1930 sizeof(*parameters),