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 * Derived from the NetBSD SCSI changer driver.
33 * $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
37 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
38 * All rights reserved.
40 * Partially based on an autochanger driver written by Stefan Grefen
41 * and on an autochanger driver written by the Systems Programming Group
42 * at the University of Utah Computer Science Department.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgements:
54 * This product includes software developed by Jason R. Thorpe
55 * for And Communications, http://www.and.com/
56 * 4. The name of the author may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
64 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
65 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
66 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
67 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
75 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/types.h>
80 #include <sys/malloc.h>
81 #include <sys/fcntl.h>
84 #include <sys/errno.h>
85 #include <sys/devicestat.h>
88 #include <cam/cam_ccb.h>
89 #include <cam/cam_periph.h>
90 #include <cam/cam_xpt_periph.h>
91 #include <cam/cam_debug.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
95 #include <cam/scsi/scsi_ch.h>
98 * Timeout definitions for various changer related commands. They may
99 * be too short for some devices (especially the timeout for INITIALIZE
103 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
104 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 15 * 60 * 1000;
105 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 15 * 60 * 1000;
106 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 15 * 60 * 1000;
107 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 5 * 60 * 1000;
108 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
109 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
112 CH_FLAG_INVALID = 0x001
130 #define CH_Q_BIT_STRING \
135 #define ccb_state ppriv_field0
136 #define ccb_bp ppriv_ptr1
138 struct scsi_mode_sense_data {
139 struct scsi_mode_header_6 header;
140 struct scsi_mode_blk_desc blk_desc;
142 struct page_element_address_assignment ea;
143 struct page_transport_geometry_parameters tg;
144 struct page_device_capabilities cap;
153 struct devstat *device_stats;
157 int sc_picker; /* current picker */
160 * The following information is obtained from the
161 * element address assignment page.
163 int sc_firsts[CHET_MAX + 1]; /* firsts */
164 int sc_counts[CHET_MAX + 1]; /* counts */
167 * The following mask defines the legal combinations
168 * of elements for the MOVE MEDIUM command.
170 u_int8_t sc_movemask[CHET_MAX + 1];
173 * As above, but for EXCHANGE MEDIUM.
175 u_int8_t sc_exchangemask[CHET_MAX + 1];
178 * Quirks; see below. XXX KDM not implemented yet
180 int sc_settledelay; /* delay for settle */
183 static d_open_t chopen;
184 static d_close_t chclose;
185 static d_ioctl_t chioctl;
186 static periph_init_t chinit;
187 static periph_ctor_t chregister;
188 static periph_oninv_t choninvalidate;
189 static periph_dtor_t chcleanup;
190 static periph_start_t chstart;
191 static void chasync(void *callback_arg, u_int32_t code,
192 struct cam_path *path, void *arg);
193 static void chdone(struct cam_periph *periph,
194 union ccb *done_ccb);
195 static int cherror(union ccb *ccb, u_int32_t cam_flags,
196 u_int32_t sense_flags);
197 static int chmove(struct cam_periph *periph,
198 struct changer_move *cm);
199 static int chexchange(struct cam_periph *periph,
200 struct changer_exchange *ce);
201 static int chposition(struct cam_periph *periph,
202 struct changer_position *cp);
203 static int chgetelemstatus(struct cam_periph *periph,
204 int scsi_version, u_long cmd,
205 struct changer_element_status_request *csr);
206 static int chsetvoltag(struct cam_periph *periph,
207 struct changer_set_voltag_request *csvr);
208 static int chielem(struct cam_periph *periph,
209 unsigned int timeout);
210 static int chgetparams(struct cam_periph *periph);
211 static int chscsiversion(struct cam_periph *periph);
213 static struct periph_driver chdriver =
216 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
219 PERIPHDRIVER_DECLARE(ch, chdriver);
221 static struct cdevsw ch_cdevsw = {
222 .d_version = D_VERSION,
223 .d_flags = D_TRACKCLOSE,
230 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
238 * Install a global async callback. This callback will
239 * receive async callbacks like "new device found".
241 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
243 if (status != CAM_REQ_CMP) {
244 printf("ch: Failed to attach master async callback "
245 "due to status 0x%x!\n", status);
250 chdevgonecb(void *arg)
252 struct ch_softc *softc;
253 struct cam_periph *periph;
257 periph = (struct cam_periph *)arg;
258 mtx = cam_periph_mtx(periph);
261 softc = (struct ch_softc *)periph->softc;
262 KASSERT(softc->open_count >= 0, ("Negative open count %d",
266 * When we get this callback, we will get no more close calls from
267 * devfs. So if we have any dangling opens, we need to release the
268 * reference held for that particular context.
270 for (i = 0; i < softc->open_count; i++)
271 cam_periph_release_locked(periph);
273 softc->open_count = 0;
276 * Release the reference held for the device node, it is gone now.
278 cam_periph_release_locked(periph);
281 * We reference the lock directly here, instead of using
282 * cam_periph_unlock(). The reason is that the final call to
283 * cam_periph_release_locked() above could result in the periph
284 * getting freed. If that is the case, dereferencing the periph
285 * with a cam_periph_unlock() call would cause a page fault.
291 choninvalidate(struct cam_periph *periph)
293 struct ch_softc *softc;
295 softc = (struct ch_softc *)periph->softc;
298 * De-register any async callbacks.
300 xpt_register_async(0, chasync, periph, periph->path);
302 softc->flags |= CH_FLAG_INVALID;
305 * Tell devfs this device has gone away, and ask for a callback
306 * when it has cleaned up its state.
308 destroy_dev_sched_cb(softc->dev, chdevgonecb, periph);
312 chcleanup(struct cam_periph *periph)
314 struct ch_softc *softc;
316 softc = (struct ch_softc *)periph->softc;
318 devstat_remove_entry(softc->device_stats);
320 free(softc, M_DEVBUF);
324 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
326 struct cam_periph *periph;
328 periph = (struct cam_periph *)callback_arg;
331 case AC_FOUND_DEVICE:
333 struct ccb_getdev *cgd;
336 cgd = (struct ccb_getdev *)arg;
340 if (cgd->protocol != PROTO_SCSI)
342 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
344 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
348 * Allocate a peripheral instance for
349 * this device and start the probe
352 status = cam_periph_alloc(chregister, choninvalidate,
353 chcleanup, chstart, "ch",
354 CAM_PERIPH_BIO, path,
355 chasync, AC_FOUND_DEVICE, cgd);
357 if (status != CAM_REQ_CMP
358 && status != CAM_REQ_INPROG)
359 printf("chasync: Unable to probe new device "
360 "due to status 0x%x\n", status);
366 cam_periph_async(periph, code, path, arg);
372 chregister(struct cam_periph *periph, void *arg)
374 struct ch_softc *softc;
375 struct ccb_getdev *cgd;
376 struct ccb_pathinq cpi;
377 struct make_dev_args args;
380 cgd = (struct ccb_getdev *)arg;
382 printf("chregister: no getdev CCB, can't register device\n");
383 return(CAM_REQ_CMP_ERR);
386 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
389 printf("chregister: Unable to probe new device. "
390 "Unable to allocate softc\n");
391 return(CAM_REQ_CMP_ERR);
394 bzero(softc, sizeof(*softc));
395 softc->state = CH_STATE_PROBE;
396 periph->softc = softc;
397 softc->quirks = CH_Q_NONE;
400 * The DVCID and CURDATA bits were not introduced until the SMC
401 * spec. If this device claims SCSI-2 or earlier support, then it
402 * very likely does not support these bits.
404 if (cgd->inq_data.version <= SCSI_REV_2)
405 softc->quirks |= CH_Q_NO_DVCID;
407 xpt_path_inq(&cpi, periph->path);
410 * Changers don't have a blocksize, and obviously don't support
413 cam_periph_unlock(periph);
414 softc->device_stats = devstat_new_entry("ch",
415 periph->unit_number, 0,
416 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
417 SID_TYPE(&cgd->inq_data) |
418 XPORT_DEVSTAT_TYPE(cpi.transport),
419 DEVSTAT_PRIORITY_OTHER);
422 * Acquire a reference to the periph before we create the devfs
423 * instance for it. We'll release this reference once the devfs
424 * instance has been freed.
426 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
427 xpt_print(periph->path, "%s: lost periph during "
428 "registration!\n", __func__);
429 cam_periph_lock(periph);
430 return (CAM_REQ_CMP_ERR);
434 /* Register the device */
435 make_dev_args_init(&args);
436 args.mda_devsw = &ch_cdevsw;
437 args.mda_unit = periph->unit_number;
438 args.mda_uid = UID_ROOT;
439 args.mda_gid = GID_OPERATOR;
440 args.mda_mode = 0600;
441 args.mda_si_drv1 = periph;
442 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
443 periph->unit_number);
444 cam_periph_lock(periph);
446 cam_periph_release_locked(periph);
447 return (CAM_REQ_CMP_ERR);
451 * Add an async callback so that we get
452 * notified if this device goes away.
454 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
457 * Lock this periph until we are setup.
458 * This first call can't block
460 (void)cam_periph_hold(periph, PRIBIO);
461 xpt_schedule(periph, CAM_PRIORITY_DEV);
467 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
469 struct cam_periph *periph;
470 struct ch_softc *softc;
473 periph = (struct cam_periph *)dev->si_drv1;
474 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
477 softc = (struct ch_softc *)periph->softc;
479 cam_periph_lock(periph);
481 if (softc->flags & CH_FLAG_INVALID) {
482 cam_periph_release_locked(periph);
483 cam_periph_unlock(periph);
487 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
488 cam_periph_unlock(periph);
489 cam_periph_release(periph);
494 * Load information about this changer device into the softc.
496 if ((error = chgetparams(periph)) != 0) {
497 cam_periph_unhold(periph);
498 cam_periph_release_locked(periph);
499 cam_periph_unlock(periph);
503 cam_periph_unhold(periph);
507 cam_periph_unlock(periph);
513 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
515 struct cam_periph *periph;
516 struct ch_softc *softc;
519 periph = (struct cam_periph *)dev->si_drv1;
520 mtx = cam_periph_mtx(periph);
523 softc = (struct ch_softc *)periph->softc;
526 cam_periph_release_locked(periph);
529 * We reference the lock directly here, instead of using
530 * cam_periph_unlock(). The reason is that the call to
531 * cam_periph_release_locked() above could result in the periph
532 * getting freed. If that is the case, dereferencing the periph
533 * with a cam_periph_unlock() call would cause a page fault.
535 * cam_periph_release() avoids this problem using the same method,
536 * but we're manually acquiring and dropping the lock here to
537 * protect the open count and avoid another lock acquisition and
546 chstart(struct cam_periph *periph, union ccb *start_ccb)
548 struct ch_softc *softc;
550 softc = (struct ch_softc *)periph->softc;
552 switch (softc->state) {
553 case CH_STATE_NORMAL:
555 xpt_release_ccb(start_ccb);
564 * Include the block descriptor when calculating the mode
567 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
568 sizeof(struct scsi_mode_blk_desc) +
569 sizeof(struct page_element_address_assignment);
571 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
573 if (mode_buffer == NULL) {
574 printf("chstart: couldn't malloc mode sense data\n");
577 bzero(mode_buffer, mode_buffer_len);
580 * Get the element address assignment page.
582 scsi_mode_sense(&start_ccb->csio,
585 /* tag_action */ MSG_SIMPLE_Q_TAG,
586 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
588 /* pc */ SMS_PAGE_CTRL_CURRENT,
589 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
590 /* param_buf */ (u_int8_t *)mode_buffer,
591 /* param_len */ mode_buffer_len,
592 /* sense_len */ SSD_FULL_SIZE,
593 /* timeout */ CH_TIMEOUT_MODE_SENSE);
595 start_ccb->ccb_h.ccb_bp = NULL;
596 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
597 xpt_action(start_ccb);
604 chdone(struct cam_periph *periph, union ccb *done_ccb)
606 struct ch_softc *softc;
607 struct ccb_scsiio *csio;
609 softc = (struct ch_softc *)periph->softc;
610 csio = &done_ccb->csio;
612 switch(done_ccb->ccb_h.ccb_state) {
615 struct scsi_mode_header_6 *mode_header;
616 struct page_element_address_assignment *ea;
617 char announce_buf[80];
620 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
622 ea = (struct page_element_address_assignment *)
623 find_mode_page_6(mode_header);
625 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
627 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
628 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
629 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
630 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
631 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
632 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
633 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
634 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
635 softc->sc_picker = softc->sc_firsts[CHET_MT];
637 #define PLURAL(c) (c) == 1 ? "" : "s"
638 snprintf(announce_buf, sizeof(announce_buf),
639 "%d slot%s, %d drive%s, "
640 "%d picker%s, %d portal%s",
641 softc->sc_counts[CHET_ST],
642 PLURAL(softc->sc_counts[CHET_ST]),
643 softc->sc_counts[CHET_DT],
644 PLURAL(softc->sc_counts[CHET_DT]),
645 softc->sc_counts[CHET_MT],
646 PLURAL(softc->sc_counts[CHET_MT]),
647 softc->sc_counts[CHET_IE],
648 PLURAL(softc->sc_counts[CHET_IE]));
650 if (announce_buf[0] != '\0') {
651 xpt_announce_periph(periph, announce_buf);
652 xpt_announce_quirks(periph, softc->quirks,
658 error = cherror(done_ccb, CAM_RETRY_SELTO,
659 SF_RETRY_UA | SF_NO_PRINT);
661 * Retry any UNIT ATTENTION type errors. They
662 * are expected at boot.
664 if (error == ERESTART) {
666 * A retry was scheduled, so
670 } else if (error != 0) {
671 struct scsi_mode_sense_6 *sms;
672 int frozen, retry_scheduled;
674 sms = (struct scsi_mode_sense_6 *)
675 done_ccb->csio.cdb_io.cdb_bytes;
676 frozen = (done_ccb->ccb_h.status &
680 * Check to see if block descriptors were
681 * disabled. Some devices don't like that.
682 * We're taking advantage of the fact that
683 * the first few bytes of the 6 and 10 byte
684 * mode sense commands are the same. If
685 * block descriptors were disabled, enable
686 * them and re-send the command.
688 if ((sms->byte2 & SMS_DBD) != 0 &&
689 (periph->flags & CAM_PERIPH_INVALID) == 0) {
690 sms->byte2 &= ~SMS_DBD;
691 xpt_action(done_ccb);
692 softc->quirks |= CH_Q_NO_DBD;
697 /* Don't wedge this device's queue */
699 cam_release_devq(done_ccb->ccb_h.path,
708 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
709 == CAM_SCSI_STATUS_ERROR)
710 scsi_sense_print(&done_ccb->csio);
712 xpt_print(periph->path,
713 "got CAM status %#x\n",
714 done_ccb->ccb_h.status);
716 xpt_print(periph->path, "fatal error, failed "
717 "to attach to device\n");
719 cam_periph_invalidate(periph);
723 softc->state = CH_STATE_NORMAL;
724 free(mode_header, M_SCSICH);
726 * Since our peripheral may be invalidated by an error
727 * above or an external event, we must release our CCB
728 * before releasing the probe lock on the peripheral.
729 * The peripheral will only go away once the last lock
730 * is removed, and we need it around for the CCB release
733 xpt_release_ccb(done_ccb);
734 cam_periph_unhold(periph);
740 xpt_release_ccb(done_ccb);
744 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
746 struct ch_softc *softc;
747 struct cam_periph *periph;
749 periph = xpt_path_periph(ccb->ccb_h.path);
750 softc = (struct ch_softc *)periph->softc;
752 return (cam_periph_error(ccb, cam_flags, sense_flags));
756 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
758 struct cam_periph *periph;
759 struct ch_softc *softc;
762 periph = (struct cam_periph *)dev->si_drv1;
763 cam_periph_lock(periph);
764 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
766 softc = (struct ch_softc *)periph->softc;
770 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
771 ("trying to do ioctl %#lx\n", cmd));
774 * If this command can change the device's state, we must
775 * have the device open for writing.
785 if ((flag & FWRITE) == 0) {
786 cam_periph_unlock(periph);
793 error = chmove(periph, (struct changer_move *)addr);
797 error = chexchange(periph, (struct changer_exchange *)addr);
801 error = chposition(periph, (struct changer_position *)addr);
805 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
810 int new_picker = *(int *)addr;
812 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
816 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
821 struct changer_params *cp = (struct changer_params *)addr;
823 cp->cp_npickers = softc->sc_counts[CHET_MT];
824 cp->cp_nslots = softc->sc_counts[CHET_ST];
825 cp->cp_nportals = softc->sc_counts[CHET_IE];
826 cp->cp_ndrives = softc->sc_counts[CHET_DT];
830 error = chielem(periph, *(unsigned int *)addr);
835 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
836 (struct changer_element_status_request *)addr);
844 scsi_version = chscsiversion(periph);
845 if (scsi_version >= SCSI_REV_0) {
846 error = chgetelemstatus(periph, scsi_version, cmd,
847 (struct changer_element_status_request *)addr);
849 else { /* unable to determine the SCSI version */
850 cam_periph_unlock(periph);
858 error = chsetvoltag(periph,
859 (struct changer_set_voltag_request *) addr);
863 /* Implement prevent/allow? */
866 error = cam_periph_ioctl(periph, cmd, addr, cherror);
870 cam_periph_unlock(periph);
875 chmove(struct cam_periph *periph, struct changer_move *cm)
877 struct ch_softc *softc;
878 u_int16_t fromelem, toelem;
883 softc = (struct ch_softc *)periph->softc;
888 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
890 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
891 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
895 * Check the request against the changer's capabilities.
897 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
901 * Calculate the source and destination elements.
903 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
904 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
906 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
908 scsi_move_medium(&ccb->csio,
911 /* tag_action */ MSG_SIMPLE_Q_TAG,
912 /* tea */ softc->sc_picker,
915 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
916 /* sense_len */ SSD_FULL_SIZE,
917 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
919 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
920 /*sense_flags*/ SF_RETRY_UA,
921 softc->device_stats);
923 xpt_release_ccb(ccb);
929 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
931 struct ch_softc *softc;
932 u_int16_t src, dst1, dst2;
937 softc = (struct ch_softc *)periph->softc;
941 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
942 (ce->ce_sdsttype > CHET_DT))
944 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
945 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
946 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
950 * Check the request against the changer's capabilities.
952 if (((softc->sc_exchangemask[ce->ce_srctype] &
953 (1 << ce->ce_fdsttype)) == 0) ||
954 ((softc->sc_exchangemask[ce->ce_fdsttype] &
955 (1 << ce->ce_sdsttype)) == 0))
959 * Calculate the source and destination elements.
961 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
962 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
963 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
965 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
967 scsi_exchange_medium(&ccb->csio,
970 /* tag_action */ MSG_SIMPLE_Q_TAG,
971 /* tea */ softc->sc_picker,
975 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
977 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
979 /* sense_len */ SSD_FULL_SIZE,
980 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
982 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
983 /*sense_flags*/ SF_RETRY_UA,
984 softc->device_stats);
986 xpt_release_ccb(ccb);
992 chposition(struct cam_periph *periph, struct changer_position *cp)
994 struct ch_softc *softc;
1000 softc = (struct ch_softc *)periph->softc;
1005 if (cp->cp_type > CHET_DT)
1007 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1011 * Calculate the destination element.
1013 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1015 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1017 scsi_position_to_element(&ccb->csio,
1019 /* cbfcnp */ chdone,
1020 /* tag_action */ MSG_SIMPLE_Q_TAG,
1021 /* tea */ softc->sc_picker,
1023 /* invert */ (cp->cp_flags & CP_INVERT) ?
1025 /* sense_len */ SSD_FULL_SIZE,
1026 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1028 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1029 /*sense_flags*/ SF_RETRY_UA,
1030 softc->device_stats);
1032 xpt_release_ccb(ccb);
1038 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1039 * to host native byte order in the volume serial number. The volume
1040 * label as returned by the changer is transferred to user mode as
1041 * nul-terminated string. Volume labels are truncated at the first
1042 * space, as suggested by SCSI-2.
1045 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1048 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1049 char c = voltag->vif[i];
1051 uvoltag->cv_volid[i] = c;
1055 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1059 * Copy an element status descriptor to a user-mode
1060 * changer_element_status structure.
1063 copy_element_status(struct ch_softc *softc,
1065 struct read_element_status_descriptor *desc,
1066 struct changer_element_status *ces,
1069 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1071 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1072 struct read_element_status_device_id *devid = NULL;
1074 ces->ces_int_addr = eaddr;
1075 /* set up logical address in element status */
1076 for (et = CHET_MT; et <= CHET_DT; et++) {
1077 if ((softc->sc_firsts[et] <= eaddr)
1078 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1080 ces->ces_addr = eaddr - softc->sc_firsts[et];
1086 ces->ces_flags = desc->flags1;
1088 ces->ces_sensecode = desc->sense_code;
1089 ces->ces_sensequal = desc->sense_qual;
1091 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1092 ces->ces_flags |= CES_INVERT;
1094 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1096 eaddr = scsi_2btoul(desc->ssea);
1098 /* convert source address to logical format */
1099 for (et = CHET_MT; et <= CHET_DT; et++) {
1100 if ((softc->sc_firsts[et] <= eaddr)
1101 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1103 ces->ces_source_addr =
1104 eaddr - softc->sc_firsts[et];
1105 ces->ces_source_type = et;
1106 ces->ces_flags |= CES_SOURCE_VALID;
1111 if (!(ces->ces_flags & CES_SOURCE_VALID))
1112 printf("ch: warning: could not map element source "
1113 "address %ud to a valid element type\n",
1118 * pvoltag and avoltag are common between SCSI-2 and later versions
1120 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1121 pvol_tag = &desc->voltag_devid.pvoltag;
1122 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1123 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1124 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1126 * For SCSI-3 and later, element status can carry designator and
1127 * other information.
1129 if (scsi_version >= SCSI_REV_SPC) {
1130 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1131 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1132 devid = &desc->voltag_devid.pvol_and_devid.devid;
1133 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1134 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1135 devid = &desc->voltag_devid.devid;
1136 else /* Have both PVOLTAG and AVOLTAG */
1137 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1141 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1143 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1144 if (devid != NULL) {
1145 if (devid->designator_length > 0) {
1146 bcopy((void *)devid->designator,
1147 (void *)ces->ces_designator,
1148 devid->designator_length);
1149 ces->ces_designator_length = devid->designator_length;
1151 * Make sure we are always NUL terminated. The
1152 * This won't matter for the binary code set,
1153 * since the user will only pay attention to the
1156 ces->ces_designator[devid->designator_length]= '\0';
1158 if (devid->piv_assoc_designator_type &
1159 READ_ELEMENT_STATUS_PIV_SET) {
1160 ces->ces_flags |= CES_PIV;
1161 ces->ces_protocol_id =
1162 READ_ELEMENT_STATUS_PROTOCOL_ID(
1163 devid->prot_code_set);
1166 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1167 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1168 devid->piv_assoc_designator_type);
1169 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1170 devid->piv_assoc_designator_type);
1171 } else if (scsi_version > SCSI_REV_2) {
1172 /* SCSI-SPC and No devid, no designator */
1173 ces->ces_designator_length = 0;
1174 ces->ces_designator[0] = '\0';
1175 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1178 if (scsi_version <= SCSI_REV_2) {
1179 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1180 READ_ELEMENT_STATUS_DT_IDVALID) {
1181 ces->ces_flags |= CES_SCSIID_VALID;
1183 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1186 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1187 READ_ELEMENT_STATUS_DT_LUVALID) {
1188 ces->ces_flags |= CES_LUN_VALID;
1190 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1191 READ_ELEMENT_STATUS_DT_LUNMASK;
1197 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1198 struct changer_element_status_request *cesr)
1200 struct read_element_status_header *st_hdr;
1201 struct read_element_status_page_header *pg_hdr;
1202 struct read_element_status_descriptor *desc;
1203 caddr_t data = NULL;
1204 size_t size, desclen;
1205 int avail, i, error = 0;
1206 int curdata, dvcid, sense_flags;
1207 int try_no_dvcid = 0;
1208 struct changer_element_status *user_data = NULL;
1209 struct ch_softc *softc;
1211 int chet = cesr->cesr_element_type;
1212 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1214 softc = (struct ch_softc *)periph->softc;
1216 /* perform argument checking */
1219 * Perform a range check on the cesr_element_{base,count}
1220 * request argument fields.
1222 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1223 || (cesr->cesr_element_base + cesr->cesr_element_count)
1224 > softc->sc_counts[chet])
1228 * Request one descriptor for the given element type. This
1229 * is used to determine the size of the descriptor so that
1230 * we can allocate enough storage for all of them. We assume
1231 * that the first one can fit into 1k.
1233 cam_periph_unlock(periph);
1234 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1236 cam_periph_lock(periph);
1237 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1239 sense_flags = SF_RETRY_UA;
1240 if (softc->quirks & CH_Q_NO_DVCID) {
1247 * Don't print anything for an Illegal Request, because
1248 * these flags can cause some changers to complain. We'll
1249 * retry without them if we get an error.
1251 sense_flags |= SF_QUIET_IR;
1256 scsi_read_element_status(&ccb->csio,
1258 /* cbfcnp */ chdone,
1259 /* tag_action */ MSG_SIMPLE_Q_TAG,
1260 /* voltag */ want_voltags,
1261 /* sea */ softc->sc_firsts[chet],
1262 /* curdata */ curdata,
1265 /* data_ptr */ data,
1266 /* dxfer_len */ 1024,
1267 /* sense_len */ SSD_FULL_SIZE,
1268 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1270 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1271 /*sense_flags*/ sense_flags,
1272 softc->device_stats);
1275 * An Illegal Request sense key (only used if there is no asc/ascq)
1276 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1277 * curdata are set (we set both or neither), try turning them off
1278 * and see if the command is successful.
1280 if ((error == EINVAL)
1281 && (dvcid || curdata)) {
1285 /* At this point we want to report any Illegal Request */
1286 sense_flags &= ~SF_QUIET_IR;
1292 * In this case, we tried a read element status with dvcid and
1293 * curdata set, and it failed. We retried without those bits, and
1294 * it succeeded. Suggest to the user that he set a quirk, so we
1295 * don't go through the retry process the first time in the future.
1296 * This should only happen on changers that claim SCSI-3 or higher,
1297 * but don't support these bits.
1299 if ((try_no_dvcid != 0)
1301 softc->quirks |= CH_Q_NO_DVCID;
1305 cam_periph_unlock(periph);
1307 st_hdr = (struct read_element_status_header *)data;
1308 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1309 sizeof(struct read_element_status_header));
1310 desclen = scsi_2btoul(pg_hdr->edl);
1312 size = sizeof(struct read_element_status_header) +
1313 sizeof(struct read_element_status_page_header) +
1314 (desclen * cesr->cesr_element_count);
1316 * Reallocate storage for descriptors and get them from the
1319 free(data, M_DEVBUF);
1320 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1322 cam_periph_lock(periph);
1323 scsi_read_element_status(&ccb->csio,
1325 /* cbfcnp */ chdone,
1326 /* tag_action */ MSG_SIMPLE_Q_TAG,
1327 /* voltag */ want_voltags,
1328 /* sea */ softc->sc_firsts[chet]
1329 + cesr->cesr_element_base,
1330 /* curdata */ curdata,
1332 /* count */ cesr->cesr_element_count,
1333 /* data_ptr */ data,
1334 /* dxfer_len */ size,
1335 /* sense_len */ SSD_FULL_SIZE,
1336 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1338 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1339 /*sense_flags*/ SF_RETRY_UA,
1340 softc->device_stats);
1344 cam_periph_unlock(periph);
1347 * Fill in the user status array.
1349 st_hdr = (struct read_element_status_header *)data;
1350 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1351 sizeof(struct read_element_status_header));
1352 avail = scsi_2btoul(st_hdr->count);
1354 if (avail != cesr->cesr_element_count) {
1355 xpt_print(periph->path,
1356 "warning, READ ELEMENT STATUS avail != count\n");
1359 user_data = (struct changer_element_status *)
1360 malloc(avail * sizeof(struct changer_element_status),
1361 M_DEVBUF, M_WAITOK | M_ZERO);
1363 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1364 sizeof(struct read_element_status_header) +
1365 sizeof(struct read_element_status_page_header));
1367 * Set up the individual element status structures
1369 for (i = 0; i < avail; ++i) {
1370 struct changer_element_status *ces;
1373 * In the changer_element_status structure, fields from
1374 * the beginning to the field of ces_scsi_lun are common
1375 * between SCSI-2 and SCSI-3, while all the rest are new
1376 * from SCSI-3. In order to maintain backward compatibility
1377 * of the chio command, the ces pointer, below, is computed
1378 * such that it lines up with the structure boundary
1379 * corresponding to the SCSI version.
1381 ces = cmd == OCHIOGSTATUS ?
1382 (struct changer_element_status *)
1383 ((unsigned char *)user_data + i *
1384 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1387 copy_element_status(softc, pg_hdr->flags, desc,
1390 desc = (struct read_element_status_descriptor *)
1391 ((unsigned char *)desc + desclen);
1394 /* Copy element status structures out to userspace. */
1395 if (cmd == OCHIOGSTATUS)
1396 error = copyout(user_data,
1397 cesr->cesr_element_status,
1398 avail* (offsetof(struct changer_element_status,
1399 ces_scsi_lun) + 1));
1401 error = copyout(user_data,
1402 cesr->cesr_element_status,
1403 avail * sizeof(struct changer_element_status));
1405 cam_periph_lock(periph);
1408 xpt_release_ccb(ccb);
1411 free(data, M_DEVBUF);
1412 if (user_data != NULL)
1413 free(user_data, M_DEVBUF);
1419 chielem(struct cam_periph *periph,
1420 unsigned int timeout)
1423 struct ch_softc *softc;
1427 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1433 softc = (struct ch_softc *)periph->softc;
1435 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1437 scsi_initialize_element_status(&ccb->csio,
1439 /* cbfcnp */ chdone,
1440 /* tag_action */ MSG_SIMPLE_Q_TAG,
1441 /* sense_len */ SSD_FULL_SIZE,
1442 /* timeout */ timeout);
1444 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1445 /*sense_flags*/ SF_RETRY_UA,
1446 softc->device_stats);
1448 xpt_release_ccb(ccb);
1454 chsetvoltag(struct cam_periph *periph,
1455 struct changer_set_voltag_request *csvr)
1458 struct ch_softc *softc;
1461 struct scsi_send_volume_tag_parameters ssvtp;
1466 softc = (struct ch_softc *)periph->softc;
1468 bzero(&ssvtp, sizeof(ssvtp));
1469 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1470 ssvtp.vitf[i] = ' ';
1476 if (csvr->csvr_type > CHET_DT)
1478 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1481 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1483 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1484 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1486 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1488 case CSVR_MODE_REPLACE:
1489 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1491 case CSVR_MODE_CLEAR:
1492 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1499 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1501 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1503 case CSVR_MODE_REPLACE:
1504 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1506 case CSVR_MODE_CLEAR:
1507 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1515 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1516 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1517 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1519 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1521 scsi_send_volume_tag(&ccb->csio,
1523 /* cbfcnp */ chdone,
1524 /* tag_action */ MSG_SIMPLE_Q_TAG,
1525 /* element_address */ ea,
1526 /* send_action_code */ sac,
1527 /* parameters */ &ssvtp,
1528 /* sense_len */ SSD_FULL_SIZE,
1529 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1531 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1532 /*sense_flags*/ SF_RETRY_UA,
1533 softc->device_stats);
1535 xpt_release_ccb(ccb);
1542 chgetparams(struct cam_periph *periph)
1545 struct ch_softc *softc;
1547 int mode_buffer_len;
1548 struct page_element_address_assignment *ea;
1549 struct page_device_capabilities *cap;
1550 int error, from, dbd;
1551 u_int8_t *moves, *exchanges;
1555 softc = (struct ch_softc *)periph->softc;
1557 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1560 * The scsi_mode_sense_data structure is just a convenience
1561 * structure that allows us to easily calculate the worst-case
1562 * storage size of the mode sense buffer.
1564 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1566 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1568 if (mode_buffer == NULL) {
1569 printf("chgetparams: couldn't malloc mode sense data\n");
1570 xpt_release_ccb(ccb);
1574 bzero(mode_buffer, mode_buffer_len);
1576 if (softc->quirks & CH_Q_NO_DBD)
1582 * Get the element address assignment page.
1584 scsi_mode_sense(&ccb->csio,
1586 /* cbfcnp */ chdone,
1587 /* tag_action */ MSG_SIMPLE_Q_TAG,
1589 /* pc */ SMS_PAGE_CTRL_CURRENT,
1590 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1591 /* param_buf */ (u_int8_t *)mode_buffer,
1592 /* param_len */ mode_buffer_len,
1593 /* sense_len */ SSD_FULL_SIZE,
1594 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1596 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1597 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1598 softc->device_stats);
1602 struct scsi_mode_sense_6 *sms;
1604 sms = (struct scsi_mode_sense_6 *)
1605 ccb->csio.cdb_io.cdb_bytes;
1607 sms->byte2 &= ~SMS_DBD;
1608 error = cam_periph_runccb(ccb, cherror,
1609 /*cam_flags*/ CAM_RETRY_SELTO,
1610 /*sense_flags*/ SF_RETRY_UA,
1611 softc->device_stats);
1614 * Since we disabled sense printing above, print
1615 * out the sense here since we got an error.
1617 scsi_sense_print(&ccb->csio);
1621 xpt_print(periph->path,
1622 "chgetparams: error getting element "
1624 xpt_release_ccb(ccb);
1625 free(mode_buffer, M_SCSICH);
1630 ea = (struct page_element_address_assignment *)
1631 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1633 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1634 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1635 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1636 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1637 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1638 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1639 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1640 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1642 bzero(mode_buffer, mode_buffer_len);
1645 * Now get the device capabilities page.
1647 scsi_mode_sense(&ccb->csio,
1649 /* cbfcnp */ chdone,
1650 /* tag_action */ MSG_SIMPLE_Q_TAG,
1652 /* pc */ SMS_PAGE_CTRL_CURRENT,
1653 /* page */ CH_DEVICE_CAP_PAGE,
1654 /* param_buf */ (u_int8_t *)mode_buffer,
1655 /* param_len */ mode_buffer_len,
1656 /* sense_len */ SSD_FULL_SIZE,
1657 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1659 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1660 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1661 softc->device_stats);
1665 struct scsi_mode_sense_6 *sms;
1667 sms = (struct scsi_mode_sense_6 *)
1668 ccb->csio.cdb_io.cdb_bytes;
1670 sms->byte2 &= ~SMS_DBD;
1671 error = cam_periph_runccb(ccb, cherror,
1672 /*cam_flags*/ CAM_RETRY_SELTO,
1673 /*sense_flags*/ SF_RETRY_UA,
1674 softc->device_stats);
1677 * Since we disabled sense printing above, print
1678 * out the sense here since we got an error.
1680 scsi_sense_print(&ccb->csio);
1684 xpt_print(periph->path,
1685 "chgetparams: error getting device "
1686 "capabilities page\n");
1687 xpt_release_ccb(ccb);
1688 free(mode_buffer, M_SCSICH);
1693 xpt_release_ccb(ccb);
1695 cap = (struct page_device_capabilities *)
1696 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1698 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1699 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1700 moves = cap->move_from;
1701 exchanges = cap->exchange_with;
1702 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1703 softc->sc_movemask[from] = moves[from];
1704 softc->sc_exchangemask[from] = exchanges[from];
1707 free(mode_buffer, M_SCSICH);
1713 chscsiversion(struct cam_periph *periph)
1715 struct scsi_inquiry_data *inq_data;
1716 struct ccb_getdev *cgd;
1717 int dev_scsi_version;
1719 cam_periph_assert(periph, MA_OWNED);
1720 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1723 * Get the device information.
1725 xpt_setup_ccb(&cgd->ccb_h,
1727 CAM_PRIORITY_NORMAL);
1728 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1729 xpt_action((union ccb *)cgd);
1731 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1732 xpt_free_ccb((union ccb *)cgd);
1736 inq_data = &cgd->inq_data;
1737 dev_scsi_version = inq_data->version;
1738 xpt_free_ccb((union ccb *)cgd);
1740 return dev_scsi_version;
1744 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1745 void (*cbfcnp)(struct cam_periph *, union ccb *),
1746 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1747 u_int32_t dst, int invert, u_int8_t sense_len,
1750 struct scsi_move_medium *scsi_cmd;
1752 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1753 bzero(scsi_cmd, sizeof(*scsi_cmd));
1755 scsi_cmd->opcode = MOVE_MEDIUM;
1757 scsi_ulto2b(tea, scsi_cmd->tea);
1758 scsi_ulto2b(src, scsi_cmd->src);
1759 scsi_ulto2b(dst, scsi_cmd->dst);
1762 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1767 /*flags*/ CAM_DIR_NONE,
1777 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1778 void (*cbfcnp)(struct cam_periph *, union ccb *),
1779 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1780 u_int32_t dst1, u_int32_t dst2, int invert1,
1781 int invert2, u_int8_t sense_len, u_int32_t timeout)
1783 struct scsi_exchange_medium *scsi_cmd;
1785 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1786 bzero(scsi_cmd, sizeof(*scsi_cmd));
1788 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1790 scsi_ulto2b(tea, scsi_cmd->tea);
1791 scsi_ulto2b(src, scsi_cmd->src);
1792 scsi_ulto2b(dst1, scsi_cmd->fdst);
1793 scsi_ulto2b(dst2, scsi_cmd->sdst);
1796 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1799 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1804 /*flags*/ CAM_DIR_NONE,
1814 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1815 void (*cbfcnp)(struct cam_periph *, union ccb *),
1816 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1817 int invert, u_int8_t sense_len, u_int32_t timeout)
1819 struct scsi_position_to_element *scsi_cmd;
1821 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1822 bzero(scsi_cmd, sizeof(*scsi_cmd));
1824 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1826 scsi_ulto2b(tea, scsi_cmd->tea);
1827 scsi_ulto2b(dst, scsi_cmd->dst);
1830 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1835 /*flags*/ CAM_DIR_NONE,
1845 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1846 void (*cbfcnp)(struct cam_periph *, union ccb *),
1847 u_int8_t tag_action, int voltag, u_int32_t sea,
1848 int curdata, int dvcid,
1849 u_int32_t count, u_int8_t *data_ptr,
1850 u_int32_t dxfer_len, u_int8_t sense_len,
1853 struct scsi_read_element_status *scsi_cmd;
1855 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1856 bzero(scsi_cmd, sizeof(*scsi_cmd));
1858 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1860 scsi_ulto2b(sea, scsi_cmd->sea);
1861 scsi_ulto2b(count, scsi_cmd->count);
1862 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1864 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1866 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1869 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1874 /*flags*/ CAM_DIR_IN,
1884 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1885 void (*cbfcnp)(struct cam_periph *, union ccb *),
1886 u_int8_t tag_action, u_int8_t sense_len,
1889 struct scsi_initialize_element_status *scsi_cmd;
1891 scsi_cmd = (struct scsi_initialize_element_status *)
1892 &csio->cdb_io.cdb_bytes;
1893 bzero(scsi_cmd, sizeof(*scsi_cmd));
1895 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1900 /*flags*/ CAM_DIR_NONE,
1902 /* data_ptr */ NULL,
1910 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1911 void (*cbfcnp)(struct cam_periph *, union ccb *),
1912 u_int8_t tag_action,
1913 u_int16_t element_address,
1914 u_int8_t send_action_code,
1915 struct scsi_send_volume_tag_parameters *parameters,
1916 u_int8_t sense_len, u_int32_t timeout)
1918 struct scsi_send_volume_tag *scsi_cmd;
1920 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1921 bzero(scsi_cmd, sizeof(*scsi_cmd));
1923 scsi_cmd->opcode = SEND_VOLUME_TAG;
1924 scsi_ulto2b(element_address, scsi_cmd->ea);
1925 scsi_cmd->sac = send_action_code;
1926 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1931 /*flags*/ CAM_DIR_OUT,
1933 /* data_ptr */ (u_int8_t *) parameters,
1934 sizeof(*parameters),