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);
361 cam_periph_async(periph, code, path, arg);
367 chregister(struct cam_periph *periph, void *arg)
369 struct ch_softc *softc;
370 struct ccb_getdev *cgd;
371 struct ccb_pathinq cpi;
372 struct make_dev_args args;
375 cgd = (struct ccb_getdev *)arg;
377 printf("chregister: no getdev CCB, can't register device\n");
378 return(CAM_REQ_CMP_ERR);
381 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
384 printf("chregister: Unable to probe new device. "
385 "Unable to allocate softc\n");
386 return(CAM_REQ_CMP_ERR);
389 bzero(softc, sizeof(*softc));
390 softc->state = CH_STATE_PROBE;
391 periph->softc = softc;
392 softc->quirks = CH_Q_NONE;
395 * The DVCID and CURDATA bits were not introduced until the SMC
396 * spec. If this device claims SCSI-2 or earlier support, then it
397 * very likely does not support these bits.
399 if (cgd->inq_data.version <= SCSI_REV_2)
400 softc->quirks |= CH_Q_NO_DVCID;
402 xpt_path_inq(&cpi, periph->path);
405 * Changers don't have a blocksize, and obviously don't support
408 cam_periph_unlock(periph);
409 softc->device_stats = devstat_new_entry("ch",
410 periph->unit_number, 0,
411 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
412 SID_TYPE(&cgd->inq_data) |
413 XPORT_DEVSTAT_TYPE(cpi.transport),
414 DEVSTAT_PRIORITY_OTHER);
417 * Acquire a reference to the periph before we create the devfs
418 * instance for it. We'll release this reference once the devfs
419 * instance has been freed.
421 if (cam_periph_acquire(periph) != 0) {
422 xpt_print(periph->path, "%s: lost periph during "
423 "registration!\n", __func__);
424 cam_periph_lock(periph);
425 return (CAM_REQ_CMP_ERR);
428 /* Register the device */
429 make_dev_args_init(&args);
430 args.mda_devsw = &ch_cdevsw;
431 args.mda_unit = periph->unit_number;
432 args.mda_uid = UID_ROOT;
433 args.mda_gid = GID_OPERATOR;
434 args.mda_mode = 0600;
435 args.mda_si_drv1 = periph;
436 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
437 periph->unit_number);
438 cam_periph_lock(periph);
440 cam_periph_release_locked(periph);
441 return (CAM_REQ_CMP_ERR);
445 * Add an async callback so that we get
446 * notified if this device goes away.
448 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
451 * Lock this periph until we are setup.
452 * This first call can't block
454 (void)cam_periph_hold(periph, PRIBIO);
455 xpt_schedule(periph, CAM_PRIORITY_DEV);
461 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
463 struct cam_periph *periph;
464 struct ch_softc *softc;
467 periph = (struct cam_periph *)dev->si_drv1;
468 if (cam_periph_acquire(periph) != 0)
471 softc = (struct ch_softc *)periph->softc;
473 cam_periph_lock(periph);
475 if (softc->flags & CH_FLAG_INVALID) {
476 cam_periph_release_locked(periph);
477 cam_periph_unlock(periph);
481 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
482 cam_periph_unlock(periph);
483 cam_periph_release(periph);
488 * Load information about this changer device into the softc.
490 if ((error = chgetparams(periph)) != 0) {
491 cam_periph_unhold(periph);
492 cam_periph_release_locked(periph);
493 cam_periph_unlock(periph);
497 cam_periph_unhold(periph);
501 cam_periph_unlock(periph);
507 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
509 struct cam_periph *periph;
510 struct ch_softc *softc;
513 periph = (struct cam_periph *)dev->si_drv1;
514 mtx = cam_periph_mtx(periph);
517 softc = (struct ch_softc *)periph->softc;
520 cam_periph_release_locked(periph);
523 * We reference the lock directly here, instead of using
524 * cam_periph_unlock(). The reason is that the call to
525 * cam_periph_release_locked() above could result in the periph
526 * getting freed. If that is the case, dereferencing the periph
527 * with a cam_periph_unlock() call would cause a page fault.
529 * cam_periph_release() avoids this problem using the same method,
530 * but we're manually acquiring and dropping the lock here to
531 * protect the open count and avoid another lock acquisition and
540 chstart(struct cam_periph *periph, union ccb *start_ccb)
542 struct ch_softc *softc;
544 softc = (struct ch_softc *)periph->softc;
546 switch (softc->state) {
547 case CH_STATE_NORMAL:
549 xpt_release_ccb(start_ccb);
558 * Include the block descriptor when calculating the mode
561 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
562 sizeof(struct scsi_mode_blk_desc) +
563 sizeof(struct page_element_address_assignment);
565 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
567 if (mode_buffer == NULL) {
568 printf("chstart: couldn't malloc mode sense data\n");
571 bzero(mode_buffer, mode_buffer_len);
574 * Get the element address assignment page.
576 scsi_mode_sense(&start_ccb->csio,
579 /* tag_action */ MSG_SIMPLE_Q_TAG,
580 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
582 /* pc */ SMS_PAGE_CTRL_CURRENT,
583 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
584 /* param_buf */ (u_int8_t *)mode_buffer,
585 /* param_len */ mode_buffer_len,
586 /* sense_len */ SSD_FULL_SIZE,
587 /* timeout */ CH_TIMEOUT_MODE_SENSE);
589 start_ccb->ccb_h.ccb_bp = NULL;
590 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
591 xpt_action(start_ccb);
598 chdone(struct cam_periph *periph, union ccb *done_ccb)
600 struct ch_softc *softc;
601 struct ccb_scsiio *csio;
603 softc = (struct ch_softc *)periph->softc;
604 csio = &done_ccb->csio;
606 switch(done_ccb->ccb_h.ccb_state) {
609 struct scsi_mode_header_6 *mode_header;
610 struct page_element_address_assignment *ea;
611 char announce_buf[80];
613 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
615 ea = (struct page_element_address_assignment *)
616 find_mode_page_6(mode_header);
618 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
620 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
621 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
622 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
623 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
624 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
625 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
626 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
627 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
628 softc->sc_picker = softc->sc_firsts[CHET_MT];
630 #define PLURAL(c) (c) == 1 ? "" : "s"
631 snprintf(announce_buf, sizeof(announce_buf),
632 "%d slot%s, %d drive%s, "
633 "%d picker%s, %d portal%s",
634 softc->sc_counts[CHET_ST],
635 PLURAL(softc->sc_counts[CHET_ST]),
636 softc->sc_counts[CHET_DT],
637 PLURAL(softc->sc_counts[CHET_DT]),
638 softc->sc_counts[CHET_MT],
639 PLURAL(softc->sc_counts[CHET_MT]),
640 softc->sc_counts[CHET_IE],
641 PLURAL(softc->sc_counts[CHET_IE]));
643 if (announce_buf[0] != '\0') {
644 xpt_announce_periph(periph, announce_buf);
645 xpt_announce_quirks(periph, softc->quirks,
651 error = cherror(done_ccb, CAM_RETRY_SELTO,
652 SF_RETRY_UA | SF_NO_PRINT);
654 * Retry any UNIT ATTENTION type errors. They
655 * are expected at boot.
657 if (error == ERESTART) {
659 * A retry was scheduled, so
663 } else if (error != 0) {
664 struct scsi_mode_sense_6 *sms;
665 int frozen, retry_scheduled;
667 sms = (struct scsi_mode_sense_6 *)
668 done_ccb->csio.cdb_io.cdb_bytes;
669 frozen = (done_ccb->ccb_h.status &
673 * Check to see if block descriptors were
674 * disabled. Some devices don't like that.
675 * We're taking advantage of the fact that
676 * the first few bytes of the 6 and 10 byte
677 * mode sense commands are the same. If
678 * block descriptors were disabled, enable
679 * them and re-send the command.
681 if ((sms->byte2 & SMS_DBD) != 0 &&
682 (periph->flags & CAM_PERIPH_INVALID) == 0) {
683 sms->byte2 &= ~SMS_DBD;
684 xpt_action(done_ccb);
685 softc->quirks |= CH_Q_NO_DBD;
690 /* Don't wedge this device's queue */
692 cam_release_devq(done_ccb->ccb_h.path,
701 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
702 == CAM_SCSI_STATUS_ERROR)
703 scsi_sense_print(&done_ccb->csio);
705 xpt_print(periph->path,
706 "got CAM status %#x\n",
707 done_ccb->ccb_h.status);
709 xpt_print(periph->path, "fatal error, failed "
710 "to attach to device\n");
712 cam_periph_invalidate(periph);
715 softc->state = CH_STATE_NORMAL;
716 free(mode_header, M_SCSICH);
718 * Since our peripheral may be invalidated by an error
719 * above or an external event, we must release our CCB
720 * before releasing the probe lock on the peripheral.
721 * The peripheral will only go away once the last lock
722 * is removed, and we need it around for the CCB release
725 xpt_release_ccb(done_ccb);
726 cam_periph_unhold(periph);
732 xpt_release_ccb(done_ccb);
736 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
738 struct ch_softc *softc;
739 struct cam_periph *periph;
741 periph = xpt_path_periph(ccb->ccb_h.path);
742 softc = (struct ch_softc *)periph->softc;
744 return (cam_periph_error(ccb, cam_flags, sense_flags));
748 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
750 struct cam_periph *periph;
751 struct ch_softc *softc;
754 periph = (struct cam_periph *)dev->si_drv1;
755 cam_periph_lock(periph);
756 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
758 softc = (struct ch_softc *)periph->softc;
762 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
763 ("trying to do ioctl %#lx\n", cmd));
766 * If this command can change the device's state, we must
767 * have the device open for writing.
777 if ((flag & FWRITE) == 0) {
778 cam_periph_unlock(periph);
785 error = chmove(periph, (struct changer_move *)addr);
789 error = chexchange(periph, (struct changer_exchange *)addr);
793 error = chposition(periph, (struct changer_position *)addr);
797 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
802 int new_picker = *(int *)addr;
804 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
808 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
813 struct changer_params *cp = (struct changer_params *)addr;
815 cp->cp_npickers = softc->sc_counts[CHET_MT];
816 cp->cp_nslots = softc->sc_counts[CHET_ST];
817 cp->cp_nportals = softc->sc_counts[CHET_IE];
818 cp->cp_ndrives = softc->sc_counts[CHET_DT];
822 error = chielem(periph, *(unsigned int *)addr);
827 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
828 (struct changer_element_status_request *)addr);
836 scsi_version = chscsiversion(periph);
837 if (scsi_version >= SCSI_REV_0) {
838 error = chgetelemstatus(periph, scsi_version, cmd,
839 (struct changer_element_status_request *)addr);
841 else { /* unable to determine the SCSI version */
842 cam_periph_unlock(periph);
850 error = chsetvoltag(periph,
851 (struct changer_set_voltag_request *) addr);
855 /* Implement prevent/allow? */
858 error = cam_periph_ioctl(periph, cmd, addr, cherror);
862 cam_periph_unlock(periph);
867 chmove(struct cam_periph *periph, struct changer_move *cm)
869 struct ch_softc *softc;
870 u_int16_t fromelem, toelem;
875 softc = (struct ch_softc *)periph->softc;
880 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
882 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
883 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
887 * Check the request against the changer's capabilities.
889 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
893 * Calculate the source and destination elements.
895 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
896 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
898 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
900 scsi_move_medium(&ccb->csio,
903 /* tag_action */ MSG_SIMPLE_Q_TAG,
904 /* tea */ softc->sc_picker,
907 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
908 /* sense_len */ SSD_FULL_SIZE,
909 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
911 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
912 /*sense_flags*/ SF_RETRY_UA,
913 softc->device_stats);
915 xpt_release_ccb(ccb);
921 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
923 struct ch_softc *softc;
924 u_int16_t src, dst1, dst2;
929 softc = (struct ch_softc *)periph->softc;
933 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
934 (ce->ce_sdsttype > CHET_DT))
936 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
937 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
938 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
942 * Check the request against the changer's capabilities.
944 if (((softc->sc_exchangemask[ce->ce_srctype] &
945 (1 << ce->ce_fdsttype)) == 0) ||
946 ((softc->sc_exchangemask[ce->ce_fdsttype] &
947 (1 << ce->ce_sdsttype)) == 0))
951 * Calculate the source and destination elements.
953 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
954 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
955 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
957 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
959 scsi_exchange_medium(&ccb->csio,
962 /* tag_action */ MSG_SIMPLE_Q_TAG,
963 /* tea */ softc->sc_picker,
967 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
969 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
971 /* sense_len */ SSD_FULL_SIZE,
972 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
974 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
975 /*sense_flags*/ SF_RETRY_UA,
976 softc->device_stats);
978 xpt_release_ccb(ccb);
984 chposition(struct cam_periph *periph, struct changer_position *cp)
986 struct ch_softc *softc;
992 softc = (struct ch_softc *)periph->softc;
997 if (cp->cp_type > CHET_DT)
999 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1003 * Calculate the destination element.
1005 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1007 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1009 scsi_position_to_element(&ccb->csio,
1011 /* cbfcnp */ chdone,
1012 /* tag_action */ MSG_SIMPLE_Q_TAG,
1013 /* tea */ softc->sc_picker,
1015 /* invert */ (cp->cp_flags & CP_INVERT) ?
1017 /* sense_len */ SSD_FULL_SIZE,
1018 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1020 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1021 /*sense_flags*/ SF_RETRY_UA,
1022 softc->device_stats);
1024 xpt_release_ccb(ccb);
1030 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1031 * to host native byte order in the volume serial number. The volume
1032 * label as returned by the changer is transferred to user mode as
1033 * nul-terminated string. Volume labels are truncated at the first
1034 * space, as suggested by SCSI-2.
1037 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1040 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1041 char c = voltag->vif[i];
1043 uvoltag->cv_volid[i] = c;
1047 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1051 * Copy an element status descriptor to a user-mode
1052 * changer_element_status structure.
1055 copy_element_status(struct ch_softc *softc,
1057 struct read_element_status_descriptor *desc,
1058 struct changer_element_status *ces,
1061 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1063 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1064 struct read_element_status_device_id *devid = NULL;
1066 ces->ces_int_addr = eaddr;
1067 /* set up logical address in element status */
1068 for (et = CHET_MT; et <= CHET_DT; et++) {
1069 if ((softc->sc_firsts[et] <= eaddr)
1070 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1072 ces->ces_addr = eaddr - softc->sc_firsts[et];
1078 ces->ces_flags = desc->flags1;
1080 ces->ces_sensecode = desc->sense_code;
1081 ces->ces_sensequal = desc->sense_qual;
1083 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1084 ces->ces_flags |= CES_INVERT;
1086 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1087 eaddr = scsi_2btoul(desc->ssea);
1089 /* convert source address to logical format */
1090 for (et = CHET_MT; et <= CHET_DT; et++) {
1091 if ((softc->sc_firsts[et] <= eaddr)
1092 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1094 ces->ces_source_addr =
1095 eaddr - softc->sc_firsts[et];
1096 ces->ces_source_type = et;
1097 ces->ces_flags |= CES_SOURCE_VALID;
1102 if (!(ces->ces_flags & CES_SOURCE_VALID))
1103 printf("ch: warning: could not map element source "
1104 "address %ud to a valid element type\n",
1109 * pvoltag and avoltag are common between SCSI-2 and later versions
1111 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1112 pvol_tag = &desc->voltag_devid.pvoltag;
1113 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1114 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1115 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1117 * For SCSI-3 and later, element status can carry designator and
1118 * other information.
1120 if (scsi_version >= SCSI_REV_SPC) {
1121 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1122 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1123 devid = &desc->voltag_devid.pvol_and_devid.devid;
1124 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1125 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1126 devid = &desc->voltag_devid.devid;
1127 else /* Have both PVOLTAG and AVOLTAG */
1128 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1132 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1134 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1135 if (devid != NULL) {
1136 if (devid->designator_length > 0) {
1137 bcopy((void *)devid->designator,
1138 (void *)ces->ces_designator,
1139 devid->designator_length);
1140 ces->ces_designator_length = devid->designator_length;
1142 * Make sure we are always NUL terminated. The
1143 * This won't matter for the binary code set,
1144 * since the user will only pay attention to the
1147 ces->ces_designator[devid->designator_length]= '\0';
1149 if (devid->piv_assoc_designator_type &
1150 READ_ELEMENT_STATUS_PIV_SET) {
1151 ces->ces_flags |= CES_PIV;
1152 ces->ces_protocol_id =
1153 READ_ELEMENT_STATUS_PROTOCOL_ID(
1154 devid->prot_code_set);
1157 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1158 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1159 devid->piv_assoc_designator_type);
1160 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1161 devid->piv_assoc_designator_type);
1162 } else if (scsi_version > SCSI_REV_2) {
1163 /* SCSI-SPC and No devid, no designator */
1164 ces->ces_designator_length = 0;
1165 ces->ces_designator[0] = '\0';
1166 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1169 if (scsi_version <= SCSI_REV_2) {
1170 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1171 READ_ELEMENT_STATUS_DT_IDVALID) {
1172 ces->ces_flags |= CES_SCSIID_VALID;
1174 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1177 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1178 READ_ELEMENT_STATUS_DT_LUVALID) {
1179 ces->ces_flags |= CES_LUN_VALID;
1181 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1182 READ_ELEMENT_STATUS_DT_LUNMASK;
1188 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1189 struct changer_element_status_request *cesr)
1191 struct read_element_status_header *st_hdr;
1192 struct read_element_status_page_header *pg_hdr;
1193 struct read_element_status_descriptor *desc;
1194 caddr_t data = NULL;
1195 size_t size, desclen;
1197 int curdata, dvcid, sense_flags;
1198 int try_no_dvcid = 0;
1199 struct changer_element_status *user_data = NULL;
1200 struct ch_softc *softc;
1202 int chet = cesr->cesr_element_type;
1204 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1206 softc = (struct ch_softc *)periph->softc;
1208 /* perform argument checking */
1211 * Perform a range check on the cesr_element_{base,count}
1212 * request argument fields.
1214 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1215 || (cesr->cesr_element_base + cesr->cesr_element_count)
1216 > softc->sc_counts[chet])
1220 * Request one descriptor for the given element type. This
1221 * is used to determine the size of the descriptor so that
1222 * we can allocate enough storage for all of them. We assume
1223 * that the first one can fit into 1k.
1225 cam_periph_unlock(periph);
1226 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1228 cam_periph_lock(periph);
1229 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1231 sense_flags = SF_RETRY_UA;
1232 if (softc->quirks & CH_Q_NO_DVCID) {
1239 * Don't print anything for an Illegal Request, because
1240 * these flags can cause some changers to complain. We'll
1241 * retry without them if we get an error.
1243 sense_flags |= SF_QUIET_IR;
1248 scsi_read_element_status(&ccb->csio,
1250 /* cbfcnp */ chdone,
1251 /* tag_action */ MSG_SIMPLE_Q_TAG,
1252 /* voltag */ want_voltags,
1253 /* sea */ softc->sc_firsts[chet],
1254 /* curdata */ curdata,
1257 /* data_ptr */ data,
1258 /* dxfer_len */ 1024,
1259 /* sense_len */ SSD_FULL_SIZE,
1260 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1262 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1263 /*sense_flags*/ sense_flags,
1264 softc->device_stats);
1267 * An Illegal Request sense key (only used if there is no asc/ascq)
1268 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1269 * curdata are set (we set both or neither), try turning them off
1270 * and see if the command is successful.
1272 if ((error == EINVAL)
1273 && (dvcid || curdata)) {
1277 /* At this point we want to report any Illegal Request */
1278 sense_flags &= ~SF_QUIET_IR;
1284 * In this case, we tried a read element status with dvcid and
1285 * curdata set, and it failed. We retried without those bits, and
1286 * it succeeded. Suggest to the user that he set a quirk, so we
1287 * don't go through the retry process the first time in the future.
1288 * This should only happen on changers that claim SCSI-3 or higher,
1289 * but don't support these bits.
1291 if ((try_no_dvcid != 0)
1293 softc->quirks |= CH_Q_NO_DVCID;
1297 cam_periph_unlock(periph);
1299 st_hdr = (struct read_element_status_header *)data;
1300 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1301 sizeof(struct read_element_status_header));
1302 desclen = scsi_2btoul(pg_hdr->edl);
1304 size = sizeof(struct read_element_status_header) +
1305 sizeof(struct read_element_status_page_header) +
1306 (desclen * cesr->cesr_element_count);
1308 * Reallocate storage for descriptors and get them from the
1311 free(data, M_DEVBUF);
1312 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1314 cam_periph_lock(periph);
1315 scsi_read_element_status(&ccb->csio,
1317 /* cbfcnp */ chdone,
1318 /* tag_action */ MSG_SIMPLE_Q_TAG,
1319 /* voltag */ want_voltags,
1320 /* sea */ softc->sc_firsts[chet]
1321 + cesr->cesr_element_base,
1322 /* curdata */ curdata,
1324 /* count */ cesr->cesr_element_count,
1325 /* data_ptr */ data,
1326 /* dxfer_len */ size,
1327 /* sense_len */ SSD_FULL_SIZE,
1328 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1330 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1331 /*sense_flags*/ SF_RETRY_UA,
1332 softc->device_stats);
1336 cam_periph_unlock(periph);
1339 * Fill in the user status array.
1341 st_hdr = (struct read_element_status_header *)data;
1342 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1343 sizeof(struct read_element_status_header));
1344 avail = scsi_2btoul(st_hdr->count);
1346 if (avail != cesr->cesr_element_count) {
1347 xpt_print(periph->path,
1348 "warning, READ ELEMENT STATUS avail != count\n");
1351 user_data = (struct changer_element_status *)
1352 malloc(avail * sizeof(struct changer_element_status),
1353 M_DEVBUF, M_WAITOK | M_ZERO);
1355 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1356 sizeof(struct read_element_status_header) +
1357 sizeof(struct read_element_status_page_header));
1359 * Set up the individual element status structures
1361 for (i = 0; i < avail; ++i) {
1362 struct changer_element_status *ces;
1365 * In the changer_element_status structure, fields from
1366 * the beginning to the field of ces_scsi_lun are common
1367 * between SCSI-2 and SCSI-3, while all the rest are new
1368 * from SCSI-3. In order to maintain backward compatibility
1369 * of the chio command, the ces pointer, below, is computed
1370 * such that it lines up with the structure boundary
1371 * corresponding to the SCSI version.
1373 ces = cmd == OCHIOGSTATUS ?
1374 (struct changer_element_status *)
1375 ((unsigned char *)user_data + i *
1376 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1379 copy_element_status(softc, pg_hdr->flags, desc,
1382 desc = (struct read_element_status_descriptor *)
1383 ((unsigned char *)desc + desclen);
1386 /* Copy element status structures out to userspace. */
1387 if (cmd == OCHIOGSTATUS)
1388 error = copyout(user_data,
1389 cesr->cesr_element_status,
1390 avail* (offsetof(struct changer_element_status,
1391 ces_scsi_lun) + 1));
1393 error = copyout(user_data,
1394 cesr->cesr_element_status,
1395 avail * sizeof(struct changer_element_status));
1397 cam_periph_lock(periph);
1400 xpt_release_ccb(ccb);
1403 free(data, M_DEVBUF);
1404 if (user_data != NULL)
1405 free(user_data, M_DEVBUF);
1411 chielem(struct cam_periph *periph,
1412 unsigned int timeout)
1415 struct ch_softc *softc;
1419 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1425 softc = (struct ch_softc *)periph->softc;
1427 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1429 scsi_initialize_element_status(&ccb->csio,
1431 /* cbfcnp */ chdone,
1432 /* tag_action */ MSG_SIMPLE_Q_TAG,
1433 /* sense_len */ SSD_FULL_SIZE,
1434 /* timeout */ timeout);
1436 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1437 /*sense_flags*/ SF_RETRY_UA,
1438 softc->device_stats);
1440 xpt_release_ccb(ccb);
1446 chsetvoltag(struct cam_periph *periph,
1447 struct changer_set_voltag_request *csvr)
1450 struct ch_softc *softc;
1453 struct scsi_send_volume_tag_parameters ssvtp;
1458 softc = (struct ch_softc *)periph->softc;
1460 bzero(&ssvtp, sizeof(ssvtp));
1461 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1462 ssvtp.vitf[i] = ' ';
1468 if (csvr->csvr_type > CHET_DT)
1470 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1473 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1475 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1476 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1478 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1480 case CSVR_MODE_REPLACE:
1481 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1483 case CSVR_MODE_CLEAR:
1484 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1491 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1493 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1495 case CSVR_MODE_REPLACE:
1496 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1498 case CSVR_MODE_CLEAR:
1499 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1507 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1508 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1509 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1511 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1513 scsi_send_volume_tag(&ccb->csio,
1515 /* cbfcnp */ chdone,
1516 /* tag_action */ MSG_SIMPLE_Q_TAG,
1517 /* element_address */ ea,
1518 /* send_action_code */ sac,
1519 /* parameters */ &ssvtp,
1520 /* sense_len */ SSD_FULL_SIZE,
1521 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1523 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1524 /*sense_flags*/ SF_RETRY_UA,
1525 softc->device_stats);
1527 xpt_release_ccb(ccb);
1534 chgetparams(struct cam_periph *periph)
1537 struct ch_softc *softc;
1539 int mode_buffer_len;
1540 struct page_element_address_assignment *ea;
1541 struct page_device_capabilities *cap;
1542 int error, from, dbd;
1543 u_int8_t *moves, *exchanges;
1547 softc = (struct ch_softc *)periph->softc;
1549 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1552 * The scsi_mode_sense_data structure is just a convenience
1553 * structure that allows us to easily calculate the worst-case
1554 * storage size of the mode sense buffer.
1556 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1558 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1560 if (mode_buffer == NULL) {
1561 printf("chgetparams: couldn't malloc mode sense data\n");
1562 xpt_release_ccb(ccb);
1566 bzero(mode_buffer, mode_buffer_len);
1568 if (softc->quirks & CH_Q_NO_DBD)
1574 * Get the element address assignment page.
1576 scsi_mode_sense(&ccb->csio,
1578 /* cbfcnp */ chdone,
1579 /* tag_action */ MSG_SIMPLE_Q_TAG,
1581 /* pc */ SMS_PAGE_CTRL_CURRENT,
1582 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1583 /* param_buf */ (u_int8_t *)mode_buffer,
1584 /* param_len */ mode_buffer_len,
1585 /* sense_len */ SSD_FULL_SIZE,
1586 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1588 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1589 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1590 softc->device_stats);
1594 struct scsi_mode_sense_6 *sms;
1596 sms = (struct scsi_mode_sense_6 *)
1597 ccb->csio.cdb_io.cdb_bytes;
1599 sms->byte2 &= ~SMS_DBD;
1600 error = cam_periph_runccb(ccb, cherror,
1601 /*cam_flags*/ CAM_RETRY_SELTO,
1602 /*sense_flags*/ SF_RETRY_UA,
1603 softc->device_stats);
1606 * Since we disabled sense printing above, print
1607 * out the sense here since we got an error.
1609 scsi_sense_print(&ccb->csio);
1613 xpt_print(periph->path,
1614 "chgetparams: error getting element "
1616 xpt_release_ccb(ccb);
1617 free(mode_buffer, M_SCSICH);
1622 ea = (struct page_element_address_assignment *)
1623 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1625 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1626 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1627 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1628 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1629 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1630 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1631 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1632 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1634 bzero(mode_buffer, mode_buffer_len);
1637 * Now get the device capabilities page.
1639 scsi_mode_sense(&ccb->csio,
1641 /* cbfcnp */ chdone,
1642 /* tag_action */ MSG_SIMPLE_Q_TAG,
1644 /* pc */ SMS_PAGE_CTRL_CURRENT,
1645 /* page */ CH_DEVICE_CAP_PAGE,
1646 /* param_buf */ (u_int8_t *)mode_buffer,
1647 /* param_len */ mode_buffer_len,
1648 /* sense_len */ SSD_FULL_SIZE,
1649 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1651 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1652 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1653 softc->device_stats);
1657 struct scsi_mode_sense_6 *sms;
1659 sms = (struct scsi_mode_sense_6 *)
1660 ccb->csio.cdb_io.cdb_bytes;
1662 sms->byte2 &= ~SMS_DBD;
1663 error = cam_periph_runccb(ccb, cherror,
1664 /*cam_flags*/ CAM_RETRY_SELTO,
1665 /*sense_flags*/ SF_RETRY_UA,
1666 softc->device_stats);
1669 * Since we disabled sense printing above, print
1670 * out the sense here since we got an error.
1672 scsi_sense_print(&ccb->csio);
1676 xpt_print(periph->path,
1677 "chgetparams: error getting device "
1678 "capabilities page\n");
1679 xpt_release_ccb(ccb);
1680 free(mode_buffer, M_SCSICH);
1685 xpt_release_ccb(ccb);
1687 cap = (struct page_device_capabilities *)
1688 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1690 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1691 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1692 moves = cap->move_from;
1693 exchanges = cap->exchange_with;
1694 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1695 softc->sc_movemask[from] = moves[from];
1696 softc->sc_exchangemask[from] = exchanges[from];
1699 free(mode_buffer, M_SCSICH);
1705 chscsiversion(struct cam_periph *periph)
1707 struct scsi_inquiry_data *inq_data;
1708 struct ccb_getdev *cgd;
1709 int dev_scsi_version;
1711 cam_periph_assert(periph, MA_OWNED);
1712 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1715 * Get the device information.
1717 xpt_setup_ccb(&cgd->ccb_h,
1719 CAM_PRIORITY_NORMAL);
1720 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1721 xpt_action((union ccb *)cgd);
1723 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1724 xpt_free_ccb((union ccb *)cgd);
1728 inq_data = &cgd->inq_data;
1729 dev_scsi_version = inq_data->version;
1730 xpt_free_ccb((union ccb *)cgd);
1732 return dev_scsi_version;
1736 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1737 void (*cbfcnp)(struct cam_periph *, union ccb *),
1738 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1739 u_int32_t dst, int invert, u_int8_t sense_len,
1742 struct scsi_move_medium *scsi_cmd;
1744 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1745 bzero(scsi_cmd, sizeof(*scsi_cmd));
1747 scsi_cmd->opcode = MOVE_MEDIUM;
1749 scsi_ulto2b(tea, scsi_cmd->tea);
1750 scsi_ulto2b(src, scsi_cmd->src);
1751 scsi_ulto2b(dst, scsi_cmd->dst);
1754 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1759 /*flags*/ CAM_DIR_NONE,
1769 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1770 void (*cbfcnp)(struct cam_periph *, union ccb *),
1771 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1772 u_int32_t dst1, u_int32_t dst2, int invert1,
1773 int invert2, u_int8_t sense_len, u_int32_t timeout)
1775 struct scsi_exchange_medium *scsi_cmd;
1777 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1778 bzero(scsi_cmd, sizeof(*scsi_cmd));
1780 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1782 scsi_ulto2b(tea, scsi_cmd->tea);
1783 scsi_ulto2b(src, scsi_cmd->src);
1784 scsi_ulto2b(dst1, scsi_cmd->fdst);
1785 scsi_ulto2b(dst2, scsi_cmd->sdst);
1788 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1791 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1796 /*flags*/ CAM_DIR_NONE,
1806 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1807 void (*cbfcnp)(struct cam_periph *, union ccb *),
1808 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1809 int invert, u_int8_t sense_len, u_int32_t timeout)
1811 struct scsi_position_to_element *scsi_cmd;
1813 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1814 bzero(scsi_cmd, sizeof(*scsi_cmd));
1816 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1818 scsi_ulto2b(tea, scsi_cmd->tea);
1819 scsi_ulto2b(dst, scsi_cmd->dst);
1822 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1827 /*flags*/ CAM_DIR_NONE,
1837 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1838 void (*cbfcnp)(struct cam_periph *, union ccb *),
1839 u_int8_t tag_action, int voltag, u_int32_t sea,
1840 int curdata, int dvcid,
1841 u_int32_t count, u_int8_t *data_ptr,
1842 u_int32_t dxfer_len, u_int8_t sense_len,
1845 struct scsi_read_element_status *scsi_cmd;
1847 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1848 bzero(scsi_cmd, sizeof(*scsi_cmd));
1850 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1852 scsi_ulto2b(sea, scsi_cmd->sea);
1853 scsi_ulto2b(count, scsi_cmd->count);
1854 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1856 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1858 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1861 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1866 /*flags*/ CAM_DIR_IN,
1876 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1877 void (*cbfcnp)(struct cam_periph *, union ccb *),
1878 u_int8_t tag_action, u_int8_t sense_len,
1881 struct scsi_initialize_element_status *scsi_cmd;
1883 scsi_cmd = (struct scsi_initialize_element_status *)
1884 &csio->cdb_io.cdb_bytes;
1885 bzero(scsi_cmd, sizeof(*scsi_cmd));
1887 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1892 /*flags*/ CAM_DIR_NONE,
1894 /* data_ptr */ NULL,
1902 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1903 void (*cbfcnp)(struct cam_periph *, union ccb *),
1904 u_int8_t tag_action,
1905 u_int16_t element_address,
1906 u_int8_t send_action_code,
1907 struct scsi_send_volume_tag_parameters *parameters,
1908 u_int8_t sense_len, u_int32_t timeout)
1910 struct scsi_send_volume_tag *scsi_cmd;
1912 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1913 bzero(scsi_cmd, sizeof(*scsi_cmd));
1915 scsi_cmd->opcode = SEND_VOLUME_TAG;
1916 scsi_ulto2b(element_address, scsi_cmd->ea);
1917 scsi_cmd->sac = send_action_code;
1918 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1923 /*flags*/ CAM_DIR_OUT,
1925 /* data_ptr */ (u_int8_t *) parameters,
1926 sizeof(*parameters),