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 bzero(&cpi, sizeof(cpi));
408 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
409 cpi.ccb_h.func_code = XPT_PATH_INQ;
410 xpt_action((union ccb *)&cpi);
413 * Changers don't have a blocksize, and obviously don't support
416 cam_periph_unlock(periph);
417 softc->device_stats = devstat_new_entry("ch",
418 periph->unit_number, 0,
419 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
420 SID_TYPE(&cgd->inq_data) |
421 XPORT_DEVSTAT_TYPE(cpi.transport),
422 DEVSTAT_PRIORITY_OTHER);
425 * Acquire a reference to the periph before we create the devfs
426 * instance for it. We'll release this reference once the devfs
427 * instance has been freed.
429 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
430 xpt_print(periph->path, "%s: lost periph during "
431 "registration!\n", __func__);
432 cam_periph_lock(periph);
433 return (CAM_REQ_CMP_ERR);
437 /* Register the device */
438 make_dev_args_init(&args);
439 args.mda_devsw = &ch_cdevsw;
440 args.mda_unit = periph->unit_number;
441 args.mda_uid = UID_ROOT;
442 args.mda_gid = GID_OPERATOR;
443 args.mda_mode = 0600;
444 args.mda_si_drv1 = periph;
445 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
446 periph->unit_number);
447 cam_periph_lock(periph);
449 cam_periph_release_locked(periph);
450 return (CAM_REQ_CMP_ERR);
454 * Add an async callback so that we get
455 * notified if this device goes away.
457 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
460 * Lock this periph until we are setup.
461 * This first call can't block
463 (void)cam_periph_hold(periph, PRIBIO);
464 xpt_schedule(periph, CAM_PRIORITY_DEV);
470 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
472 struct cam_periph *periph;
473 struct ch_softc *softc;
476 periph = (struct cam_periph *)dev->si_drv1;
477 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
480 softc = (struct ch_softc *)periph->softc;
482 cam_periph_lock(periph);
484 if (softc->flags & CH_FLAG_INVALID) {
485 cam_periph_release_locked(periph);
486 cam_periph_unlock(periph);
490 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
491 cam_periph_unlock(periph);
492 cam_periph_release(periph);
497 * Load information about this changer device into the softc.
499 if ((error = chgetparams(periph)) != 0) {
500 cam_periph_unhold(periph);
501 cam_periph_release_locked(periph);
502 cam_periph_unlock(periph);
506 cam_periph_unhold(periph);
510 cam_periph_unlock(periph);
516 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
518 struct cam_periph *periph;
519 struct ch_softc *softc;
522 periph = (struct cam_periph *)dev->si_drv1;
523 mtx = cam_periph_mtx(periph);
526 softc = (struct ch_softc *)periph->softc;
529 cam_periph_release_locked(periph);
532 * We reference the lock directly here, instead of using
533 * cam_periph_unlock(). The reason is that the call to
534 * cam_periph_release_locked() above could result in the periph
535 * getting freed. If that is the case, dereferencing the periph
536 * with a cam_periph_unlock() call would cause a page fault.
538 * cam_periph_release() avoids this problem using the same method,
539 * but we're manually acquiring and dropping the lock here to
540 * protect the open count and avoid another lock acquisition and
549 chstart(struct cam_periph *periph, union ccb *start_ccb)
551 struct ch_softc *softc;
553 softc = (struct ch_softc *)periph->softc;
555 switch (softc->state) {
556 case CH_STATE_NORMAL:
558 xpt_release_ccb(start_ccb);
567 * Include the block descriptor when calculating the mode
570 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
571 sizeof(struct scsi_mode_blk_desc) +
572 sizeof(struct page_element_address_assignment);
574 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
576 if (mode_buffer == NULL) {
577 printf("chstart: couldn't malloc mode sense data\n");
580 bzero(mode_buffer, mode_buffer_len);
583 * Get the element address assignment page.
585 scsi_mode_sense(&start_ccb->csio,
588 /* tag_action */ MSG_SIMPLE_Q_TAG,
589 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
591 /* pc */ SMS_PAGE_CTRL_CURRENT,
592 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
593 /* param_buf */ (u_int8_t *)mode_buffer,
594 /* param_len */ mode_buffer_len,
595 /* sense_len */ SSD_FULL_SIZE,
596 /* timeout */ CH_TIMEOUT_MODE_SENSE);
598 start_ccb->ccb_h.ccb_bp = NULL;
599 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
600 xpt_action(start_ccb);
607 chdone(struct cam_periph *periph, union ccb *done_ccb)
609 struct ch_softc *softc;
610 struct ccb_scsiio *csio;
612 softc = (struct ch_softc *)periph->softc;
613 csio = &done_ccb->csio;
615 switch(done_ccb->ccb_h.ccb_state) {
618 struct scsi_mode_header_6 *mode_header;
619 struct page_element_address_assignment *ea;
620 char announce_buf[80];
623 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
625 ea = (struct page_element_address_assignment *)
626 find_mode_page_6(mode_header);
628 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
630 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
631 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
632 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
633 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
634 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
635 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
636 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
637 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
638 softc->sc_picker = softc->sc_firsts[CHET_MT];
640 #define PLURAL(c) (c) == 1 ? "" : "s"
641 snprintf(announce_buf, sizeof(announce_buf),
642 "%d slot%s, %d drive%s, "
643 "%d picker%s, %d portal%s",
644 softc->sc_counts[CHET_ST],
645 PLURAL(softc->sc_counts[CHET_ST]),
646 softc->sc_counts[CHET_DT],
647 PLURAL(softc->sc_counts[CHET_DT]),
648 softc->sc_counts[CHET_MT],
649 PLURAL(softc->sc_counts[CHET_MT]),
650 softc->sc_counts[CHET_IE],
651 PLURAL(softc->sc_counts[CHET_IE]));
653 if (announce_buf[0] != '\0') {
654 xpt_announce_periph(periph, announce_buf);
655 xpt_announce_quirks(periph, softc->quirks,
661 error = cherror(done_ccb, CAM_RETRY_SELTO,
662 SF_RETRY_UA | SF_NO_PRINT);
664 * Retry any UNIT ATTENTION type errors. They
665 * are expected at boot.
667 if (error == ERESTART) {
669 * A retry was scheduled, so
673 } else if (error != 0) {
674 struct scsi_mode_sense_6 *sms;
675 int frozen, retry_scheduled;
677 sms = (struct scsi_mode_sense_6 *)
678 done_ccb->csio.cdb_io.cdb_bytes;
679 frozen = (done_ccb->ccb_h.status &
683 * Check to see if block descriptors were
684 * disabled. Some devices don't like that.
685 * We're taking advantage of the fact that
686 * the first few bytes of the 6 and 10 byte
687 * mode sense commands are the same. If
688 * block descriptors were disabled, enable
689 * them and re-send the command.
691 if ((sms->byte2 & SMS_DBD) != 0 &&
692 (periph->flags & CAM_PERIPH_INVALID) == 0) {
693 sms->byte2 &= ~SMS_DBD;
694 xpt_action(done_ccb);
695 softc->quirks |= CH_Q_NO_DBD;
700 /* Don't wedge this device's queue */
702 cam_release_devq(done_ccb->ccb_h.path,
711 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
712 == CAM_SCSI_STATUS_ERROR)
713 scsi_sense_print(&done_ccb->csio);
715 xpt_print(periph->path,
716 "got CAM status %#x\n",
717 done_ccb->ccb_h.status);
719 xpt_print(periph->path, "fatal error, failed "
720 "to attach to device\n");
722 cam_periph_invalidate(periph);
726 softc->state = CH_STATE_NORMAL;
727 free(mode_header, M_SCSICH);
729 * Since our peripheral may be invalidated by an error
730 * above or an external event, we must release our CCB
731 * before releasing the probe lock on the peripheral.
732 * The peripheral will only go away once the last lock
733 * is removed, and we need it around for the CCB release
736 xpt_release_ccb(done_ccb);
737 cam_periph_unhold(periph);
743 xpt_release_ccb(done_ccb);
747 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
749 struct ch_softc *softc;
750 struct cam_periph *periph;
752 periph = xpt_path_periph(ccb->ccb_h.path);
753 softc = (struct ch_softc *)periph->softc;
755 return (cam_periph_error(ccb, cam_flags, sense_flags,
760 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
762 struct cam_periph *periph;
763 struct ch_softc *softc;
766 periph = (struct cam_periph *)dev->si_drv1;
767 cam_periph_lock(periph);
768 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
770 softc = (struct ch_softc *)periph->softc;
774 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
775 ("trying to do ioctl %#lx\n", cmd));
778 * If this command can change the device's state, we must
779 * have the device open for writing.
789 if ((flag & FWRITE) == 0) {
790 cam_periph_unlock(periph);
797 error = chmove(periph, (struct changer_move *)addr);
801 error = chexchange(periph, (struct changer_exchange *)addr);
805 error = chposition(periph, (struct changer_position *)addr);
809 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
814 int new_picker = *(int *)addr;
816 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
820 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
825 struct changer_params *cp = (struct changer_params *)addr;
827 cp->cp_npickers = softc->sc_counts[CHET_MT];
828 cp->cp_nslots = softc->sc_counts[CHET_ST];
829 cp->cp_nportals = softc->sc_counts[CHET_IE];
830 cp->cp_ndrives = softc->sc_counts[CHET_DT];
834 error = chielem(periph, *(unsigned int *)addr);
839 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
840 (struct changer_element_status_request *)addr);
848 scsi_version = chscsiversion(periph);
849 if (scsi_version >= SCSI_REV_0) {
850 error = chgetelemstatus(periph, scsi_version, cmd,
851 (struct changer_element_status_request *)addr);
853 else { /* unable to determine the SCSI version */
854 cam_periph_unlock(periph);
862 error = chsetvoltag(periph,
863 (struct changer_set_voltag_request *) addr);
867 /* Implement prevent/allow? */
870 error = cam_periph_ioctl(periph, cmd, addr, cherror);
874 cam_periph_unlock(periph);
879 chmove(struct cam_periph *periph, struct changer_move *cm)
881 struct ch_softc *softc;
882 u_int16_t fromelem, toelem;
887 softc = (struct ch_softc *)periph->softc;
892 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
894 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
895 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
899 * Check the request against the changer's capabilities.
901 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
905 * Calculate the source and destination elements.
907 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
908 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
910 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
912 scsi_move_medium(&ccb->csio,
915 /* tag_action */ MSG_SIMPLE_Q_TAG,
916 /* tea */ softc->sc_picker,
919 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
920 /* sense_len */ SSD_FULL_SIZE,
921 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
923 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
924 /*sense_flags*/ SF_RETRY_UA,
925 softc->device_stats);
927 xpt_release_ccb(ccb);
933 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
935 struct ch_softc *softc;
936 u_int16_t src, dst1, dst2;
941 softc = (struct ch_softc *)periph->softc;
945 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
946 (ce->ce_sdsttype > CHET_DT))
948 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
949 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
950 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
954 * Check the request against the changer's capabilities.
956 if (((softc->sc_exchangemask[ce->ce_srctype] &
957 (1 << ce->ce_fdsttype)) == 0) ||
958 ((softc->sc_exchangemask[ce->ce_fdsttype] &
959 (1 << ce->ce_sdsttype)) == 0))
963 * Calculate the source and destination elements.
965 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
966 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
967 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
969 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
971 scsi_exchange_medium(&ccb->csio,
974 /* tag_action */ MSG_SIMPLE_Q_TAG,
975 /* tea */ softc->sc_picker,
979 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
981 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
983 /* sense_len */ SSD_FULL_SIZE,
984 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
986 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
987 /*sense_flags*/ SF_RETRY_UA,
988 softc->device_stats);
990 xpt_release_ccb(ccb);
996 chposition(struct cam_periph *periph, struct changer_position *cp)
998 struct ch_softc *softc;
1004 softc = (struct ch_softc *)periph->softc;
1009 if (cp->cp_type > CHET_DT)
1011 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1015 * Calculate the destination element.
1017 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1019 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1021 scsi_position_to_element(&ccb->csio,
1023 /* cbfcnp */ chdone,
1024 /* tag_action */ MSG_SIMPLE_Q_TAG,
1025 /* tea */ softc->sc_picker,
1027 /* invert */ (cp->cp_flags & CP_INVERT) ?
1029 /* sense_len */ SSD_FULL_SIZE,
1030 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1032 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1033 /*sense_flags*/ SF_RETRY_UA,
1034 softc->device_stats);
1036 xpt_release_ccb(ccb);
1042 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1043 * to host native byte order in the volume serial number. The volume
1044 * label as returned by the changer is transferred to user mode as
1045 * nul-terminated string. Volume labels are truncated at the first
1046 * space, as suggested by SCSI-2.
1049 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1052 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1053 char c = voltag->vif[i];
1055 uvoltag->cv_volid[i] = c;
1059 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1063 * Copy an element status descriptor to a user-mode
1064 * changer_element_status structure.
1067 copy_element_status(struct ch_softc *softc,
1069 struct read_element_status_descriptor *desc,
1070 struct changer_element_status *ces,
1073 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1075 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1076 struct read_element_status_device_id *devid = NULL;
1078 ces->ces_int_addr = eaddr;
1079 /* set up logical address in element status */
1080 for (et = CHET_MT; et <= CHET_DT; et++) {
1081 if ((softc->sc_firsts[et] <= eaddr)
1082 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1084 ces->ces_addr = eaddr - softc->sc_firsts[et];
1090 ces->ces_flags = desc->flags1;
1092 ces->ces_sensecode = desc->sense_code;
1093 ces->ces_sensequal = desc->sense_qual;
1095 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1096 ces->ces_flags |= CES_INVERT;
1098 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1100 eaddr = scsi_2btoul(desc->ssea);
1102 /* convert source address to logical format */
1103 for (et = CHET_MT; et <= CHET_DT; et++) {
1104 if ((softc->sc_firsts[et] <= eaddr)
1105 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1107 ces->ces_source_addr =
1108 eaddr - softc->sc_firsts[et];
1109 ces->ces_source_type = et;
1110 ces->ces_flags |= CES_SOURCE_VALID;
1115 if (!(ces->ces_flags & CES_SOURCE_VALID))
1116 printf("ch: warning: could not map element source "
1117 "address %ud to a valid element type\n",
1122 * pvoltag and avoltag are common between SCSI-2 and later versions
1124 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1125 pvol_tag = &desc->voltag_devid.pvoltag;
1126 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1127 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1128 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1130 * For SCSI-3 and later, element status can carry designator and
1131 * other information.
1133 if (scsi_version >= SCSI_REV_SPC) {
1134 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1135 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1136 devid = &desc->voltag_devid.pvol_and_devid.devid;
1137 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1138 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1139 devid = &desc->voltag_devid.devid;
1140 else /* Have both PVOLTAG and AVOLTAG */
1141 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1145 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1147 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1148 if (devid != NULL) {
1149 if (devid->designator_length > 0) {
1150 bcopy((void *)devid->designator,
1151 (void *)ces->ces_designator,
1152 devid->designator_length);
1153 ces->ces_designator_length = devid->designator_length;
1155 * Make sure we are always NUL terminated. The
1156 * This won't matter for the binary code set,
1157 * since the user will only pay attention to the
1160 ces->ces_designator[devid->designator_length]= '\0';
1162 if (devid->piv_assoc_designator_type &
1163 READ_ELEMENT_STATUS_PIV_SET) {
1164 ces->ces_flags |= CES_PIV;
1165 ces->ces_protocol_id =
1166 READ_ELEMENT_STATUS_PROTOCOL_ID(
1167 devid->prot_code_set);
1170 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1171 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1172 devid->piv_assoc_designator_type);
1173 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1174 devid->piv_assoc_designator_type);
1175 } else if (scsi_version > SCSI_REV_2) {
1176 /* SCSI-SPC and No devid, no designator */
1177 ces->ces_designator_length = 0;
1178 ces->ces_designator[0] = '\0';
1179 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1182 if (scsi_version <= SCSI_REV_2) {
1183 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1184 READ_ELEMENT_STATUS_DT_IDVALID) {
1185 ces->ces_flags |= CES_SCSIID_VALID;
1187 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1190 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1191 READ_ELEMENT_STATUS_DT_LUVALID) {
1192 ces->ces_flags |= CES_LUN_VALID;
1194 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1195 READ_ELEMENT_STATUS_DT_LUNMASK;
1201 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1202 struct changer_element_status_request *cesr)
1204 struct read_element_status_header *st_hdr;
1205 struct read_element_status_page_header *pg_hdr;
1206 struct read_element_status_descriptor *desc;
1207 caddr_t data = NULL;
1208 size_t size, desclen;
1209 int avail, i, error = 0;
1210 int curdata, dvcid, sense_flags;
1211 int try_no_dvcid = 0;
1212 struct changer_element_status *user_data = NULL;
1213 struct ch_softc *softc;
1215 int chet = cesr->cesr_element_type;
1216 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1218 softc = (struct ch_softc *)periph->softc;
1220 /* perform argument checking */
1223 * Perform a range check on the cesr_element_{base,count}
1224 * request argument fields.
1226 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1227 || (cesr->cesr_element_base + cesr->cesr_element_count)
1228 > softc->sc_counts[chet])
1232 * Request one descriptor for the given element type. This
1233 * is used to determine the size of the descriptor so that
1234 * we can allocate enough storage for all of them. We assume
1235 * that the first one can fit into 1k.
1237 cam_periph_unlock(periph);
1238 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1240 cam_periph_lock(periph);
1241 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1243 sense_flags = SF_RETRY_UA;
1244 if (softc->quirks & CH_Q_NO_DVCID) {
1251 * Don't print anything for an Illegal Request, because
1252 * these flags can cause some changers to complain. We'll
1253 * retry without them if we get an error.
1255 sense_flags |= SF_QUIET_IR;
1260 scsi_read_element_status(&ccb->csio,
1262 /* cbfcnp */ chdone,
1263 /* tag_action */ MSG_SIMPLE_Q_TAG,
1264 /* voltag */ want_voltags,
1265 /* sea */ softc->sc_firsts[chet],
1266 /* curdata */ curdata,
1269 /* data_ptr */ data,
1270 /* dxfer_len */ 1024,
1271 /* sense_len */ SSD_FULL_SIZE,
1272 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1274 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1275 /*sense_flags*/ sense_flags,
1276 softc->device_stats);
1279 * An Illegal Request sense key (only used if there is no asc/ascq)
1280 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1281 * curdata are set (we set both or neither), try turning them off
1282 * and see if the command is successful.
1284 if ((error == EINVAL)
1285 && (dvcid || curdata)) {
1289 /* At this point we want to report any Illegal Request */
1290 sense_flags &= ~SF_QUIET_IR;
1296 * In this case, we tried a read element status with dvcid and
1297 * curdata set, and it failed. We retried without those bits, and
1298 * it succeeded. Suggest to the user that he set a quirk, so we
1299 * don't go through the retry process the first time in the future.
1300 * This should only happen on changers that claim SCSI-3 or higher,
1301 * but don't support these bits.
1303 if ((try_no_dvcid != 0)
1305 softc->quirks |= CH_Q_NO_DVCID;
1309 cam_periph_unlock(periph);
1311 st_hdr = (struct read_element_status_header *)data;
1312 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1313 sizeof(struct read_element_status_header));
1314 desclen = scsi_2btoul(pg_hdr->edl);
1316 size = sizeof(struct read_element_status_header) +
1317 sizeof(struct read_element_status_page_header) +
1318 (desclen * cesr->cesr_element_count);
1320 * Reallocate storage for descriptors and get them from the
1323 free(data, M_DEVBUF);
1324 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1326 cam_periph_lock(periph);
1327 scsi_read_element_status(&ccb->csio,
1329 /* cbfcnp */ chdone,
1330 /* tag_action */ MSG_SIMPLE_Q_TAG,
1331 /* voltag */ want_voltags,
1332 /* sea */ softc->sc_firsts[chet]
1333 + cesr->cesr_element_base,
1334 /* curdata */ curdata,
1336 /* count */ cesr->cesr_element_count,
1337 /* data_ptr */ data,
1338 /* dxfer_len */ size,
1339 /* sense_len */ SSD_FULL_SIZE,
1340 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1342 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1343 /*sense_flags*/ SF_RETRY_UA,
1344 softc->device_stats);
1348 cam_periph_unlock(periph);
1351 * Fill in the user status array.
1353 st_hdr = (struct read_element_status_header *)data;
1354 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1355 sizeof(struct read_element_status_header));
1356 avail = scsi_2btoul(st_hdr->count);
1358 if (avail != cesr->cesr_element_count) {
1359 xpt_print(periph->path,
1360 "warning, READ ELEMENT STATUS avail != count\n");
1363 user_data = (struct changer_element_status *)
1364 malloc(avail * sizeof(struct changer_element_status),
1365 M_DEVBUF, M_WAITOK | M_ZERO);
1367 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1368 sizeof(struct read_element_status_header) +
1369 sizeof(struct read_element_status_page_header));
1371 * Set up the individual element status structures
1373 for (i = 0; i < avail; ++i) {
1374 struct changer_element_status *ces;
1377 * In the changer_element_status structure, fields from
1378 * the beginning to the field of ces_scsi_lun are common
1379 * between SCSI-2 and SCSI-3, while all the rest are new
1380 * from SCSI-3. In order to maintain backward compatibility
1381 * of the chio command, the ces pointer, below, is computed
1382 * such that it lines up with the structure boundary
1383 * corresponding to the SCSI version.
1385 ces = cmd == OCHIOGSTATUS ?
1386 (struct changer_element_status *)
1387 ((unsigned char *)user_data + i *
1388 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1391 copy_element_status(softc, pg_hdr->flags, desc,
1394 desc = (struct read_element_status_descriptor *)
1395 ((unsigned char *)desc + desclen);
1398 /* Copy element status structures out to userspace. */
1399 if (cmd == OCHIOGSTATUS)
1400 error = copyout(user_data,
1401 cesr->cesr_element_status,
1402 avail* (offsetof(struct changer_element_status,
1403 ces_scsi_lun) + 1));
1405 error = copyout(user_data,
1406 cesr->cesr_element_status,
1407 avail * sizeof(struct changer_element_status));
1409 cam_periph_lock(periph);
1412 xpt_release_ccb(ccb);
1415 free(data, M_DEVBUF);
1416 if (user_data != NULL)
1417 free(user_data, M_DEVBUF);
1423 chielem(struct cam_periph *periph,
1424 unsigned int timeout)
1427 struct ch_softc *softc;
1431 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1437 softc = (struct ch_softc *)periph->softc;
1439 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1441 scsi_initialize_element_status(&ccb->csio,
1443 /* cbfcnp */ chdone,
1444 /* tag_action */ MSG_SIMPLE_Q_TAG,
1445 /* sense_len */ SSD_FULL_SIZE,
1446 /* timeout */ timeout);
1448 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1449 /*sense_flags*/ SF_RETRY_UA,
1450 softc->device_stats);
1452 xpt_release_ccb(ccb);
1458 chsetvoltag(struct cam_periph *periph,
1459 struct changer_set_voltag_request *csvr)
1462 struct ch_softc *softc;
1465 struct scsi_send_volume_tag_parameters ssvtp;
1470 softc = (struct ch_softc *)periph->softc;
1472 bzero(&ssvtp, sizeof(ssvtp));
1473 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1474 ssvtp.vitf[i] = ' ';
1480 if (csvr->csvr_type > CHET_DT)
1482 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1485 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1487 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1488 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1490 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1492 case CSVR_MODE_REPLACE:
1493 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1495 case CSVR_MODE_CLEAR:
1496 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1503 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1505 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1507 case CSVR_MODE_REPLACE:
1508 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1510 case CSVR_MODE_CLEAR:
1511 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1519 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1520 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1521 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1523 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1525 scsi_send_volume_tag(&ccb->csio,
1527 /* cbfcnp */ chdone,
1528 /* tag_action */ MSG_SIMPLE_Q_TAG,
1529 /* element_address */ ea,
1530 /* send_action_code */ sac,
1531 /* parameters */ &ssvtp,
1532 /* sense_len */ SSD_FULL_SIZE,
1533 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1535 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1536 /*sense_flags*/ SF_RETRY_UA,
1537 softc->device_stats);
1539 xpt_release_ccb(ccb);
1546 chgetparams(struct cam_periph *periph)
1549 struct ch_softc *softc;
1551 int mode_buffer_len;
1552 struct page_element_address_assignment *ea;
1553 struct page_device_capabilities *cap;
1554 int error, from, dbd;
1555 u_int8_t *moves, *exchanges;
1559 softc = (struct ch_softc *)periph->softc;
1561 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1564 * The scsi_mode_sense_data structure is just a convenience
1565 * structure that allows us to easily calculate the worst-case
1566 * storage size of the mode sense buffer.
1568 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1570 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1572 if (mode_buffer == NULL) {
1573 printf("chgetparams: couldn't malloc mode sense data\n");
1574 xpt_release_ccb(ccb);
1578 bzero(mode_buffer, mode_buffer_len);
1580 if (softc->quirks & CH_Q_NO_DBD)
1586 * Get the element address assignment page.
1588 scsi_mode_sense(&ccb->csio,
1590 /* cbfcnp */ chdone,
1591 /* tag_action */ MSG_SIMPLE_Q_TAG,
1593 /* pc */ SMS_PAGE_CTRL_CURRENT,
1594 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1595 /* param_buf */ (u_int8_t *)mode_buffer,
1596 /* param_len */ mode_buffer_len,
1597 /* sense_len */ SSD_FULL_SIZE,
1598 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1600 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1601 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1602 softc->device_stats);
1606 struct scsi_mode_sense_6 *sms;
1608 sms = (struct scsi_mode_sense_6 *)
1609 ccb->csio.cdb_io.cdb_bytes;
1611 sms->byte2 &= ~SMS_DBD;
1612 error = cam_periph_runccb(ccb, cherror,
1613 /*cam_flags*/ CAM_RETRY_SELTO,
1614 /*sense_flags*/ SF_RETRY_UA,
1615 softc->device_stats);
1618 * Since we disabled sense printing above, print
1619 * out the sense here since we got an error.
1621 scsi_sense_print(&ccb->csio);
1625 xpt_print(periph->path,
1626 "chgetparams: error getting element "
1628 xpt_release_ccb(ccb);
1629 free(mode_buffer, M_SCSICH);
1634 ea = (struct page_element_address_assignment *)
1635 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1637 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1638 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1639 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1640 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1641 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1642 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1643 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1644 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1646 bzero(mode_buffer, mode_buffer_len);
1649 * Now get the device capabilities page.
1651 scsi_mode_sense(&ccb->csio,
1653 /* cbfcnp */ chdone,
1654 /* tag_action */ MSG_SIMPLE_Q_TAG,
1656 /* pc */ SMS_PAGE_CTRL_CURRENT,
1657 /* page */ CH_DEVICE_CAP_PAGE,
1658 /* param_buf */ (u_int8_t *)mode_buffer,
1659 /* param_len */ mode_buffer_len,
1660 /* sense_len */ SSD_FULL_SIZE,
1661 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1663 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1664 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1665 softc->device_stats);
1669 struct scsi_mode_sense_6 *sms;
1671 sms = (struct scsi_mode_sense_6 *)
1672 ccb->csio.cdb_io.cdb_bytes;
1674 sms->byte2 &= ~SMS_DBD;
1675 error = cam_periph_runccb(ccb, cherror,
1676 /*cam_flags*/ CAM_RETRY_SELTO,
1677 /*sense_flags*/ SF_RETRY_UA,
1678 softc->device_stats);
1681 * Since we disabled sense printing above, print
1682 * out the sense here since we got an error.
1684 scsi_sense_print(&ccb->csio);
1688 xpt_print(periph->path,
1689 "chgetparams: error getting device "
1690 "capabilities page\n");
1691 xpt_release_ccb(ccb);
1692 free(mode_buffer, M_SCSICH);
1697 xpt_release_ccb(ccb);
1699 cap = (struct page_device_capabilities *)
1700 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1702 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1703 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1704 moves = cap->move_from;
1705 exchanges = cap->exchange_with;
1706 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1707 softc->sc_movemask[from] = moves[from];
1708 softc->sc_exchangemask[from] = exchanges[from];
1711 free(mode_buffer, M_SCSICH);
1717 chscsiversion(struct cam_periph *periph)
1719 struct scsi_inquiry_data *inq_data;
1720 struct ccb_getdev *cgd;
1721 int dev_scsi_version;
1723 cam_periph_assert(periph, MA_OWNED);
1724 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1727 * Get the device information.
1729 xpt_setup_ccb(&cgd->ccb_h,
1731 CAM_PRIORITY_NORMAL);
1732 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1733 xpt_action((union ccb *)cgd);
1735 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1736 xpt_free_ccb((union ccb *)cgd);
1740 inq_data = &cgd->inq_data;
1741 dev_scsi_version = inq_data->version;
1742 xpt_free_ccb((union ccb *)cgd);
1744 return dev_scsi_version;
1748 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1749 void (*cbfcnp)(struct cam_periph *, union ccb *),
1750 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1751 u_int32_t dst, int invert, u_int8_t sense_len,
1754 struct scsi_move_medium *scsi_cmd;
1756 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1757 bzero(scsi_cmd, sizeof(*scsi_cmd));
1759 scsi_cmd->opcode = MOVE_MEDIUM;
1761 scsi_ulto2b(tea, scsi_cmd->tea);
1762 scsi_ulto2b(src, scsi_cmd->src);
1763 scsi_ulto2b(dst, scsi_cmd->dst);
1766 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1771 /*flags*/ CAM_DIR_NONE,
1781 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1782 void (*cbfcnp)(struct cam_periph *, union ccb *),
1783 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1784 u_int32_t dst1, u_int32_t dst2, int invert1,
1785 int invert2, u_int8_t sense_len, u_int32_t timeout)
1787 struct scsi_exchange_medium *scsi_cmd;
1789 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1790 bzero(scsi_cmd, sizeof(*scsi_cmd));
1792 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1794 scsi_ulto2b(tea, scsi_cmd->tea);
1795 scsi_ulto2b(src, scsi_cmd->src);
1796 scsi_ulto2b(dst1, scsi_cmd->fdst);
1797 scsi_ulto2b(dst2, scsi_cmd->sdst);
1800 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1803 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1808 /*flags*/ CAM_DIR_NONE,
1818 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1819 void (*cbfcnp)(struct cam_periph *, union ccb *),
1820 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1821 int invert, u_int8_t sense_len, u_int32_t timeout)
1823 struct scsi_position_to_element *scsi_cmd;
1825 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1826 bzero(scsi_cmd, sizeof(*scsi_cmd));
1828 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1830 scsi_ulto2b(tea, scsi_cmd->tea);
1831 scsi_ulto2b(dst, scsi_cmd->dst);
1834 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1839 /*flags*/ CAM_DIR_NONE,
1849 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1850 void (*cbfcnp)(struct cam_periph *, union ccb *),
1851 u_int8_t tag_action, int voltag, u_int32_t sea,
1852 int curdata, int dvcid,
1853 u_int32_t count, u_int8_t *data_ptr,
1854 u_int32_t dxfer_len, u_int8_t sense_len,
1857 struct scsi_read_element_status *scsi_cmd;
1859 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1860 bzero(scsi_cmd, sizeof(*scsi_cmd));
1862 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1864 scsi_ulto2b(sea, scsi_cmd->sea);
1865 scsi_ulto2b(count, scsi_cmd->count);
1866 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1868 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1870 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1873 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1878 /*flags*/ CAM_DIR_IN,
1888 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1889 void (*cbfcnp)(struct cam_periph *, union ccb *),
1890 u_int8_t tag_action, u_int8_t sense_len,
1893 struct scsi_initialize_element_status *scsi_cmd;
1895 scsi_cmd = (struct scsi_initialize_element_status *)
1896 &csio->cdb_io.cdb_bytes;
1897 bzero(scsi_cmd, sizeof(*scsi_cmd));
1899 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1904 /*flags*/ CAM_DIR_NONE,
1906 /* data_ptr */ NULL,
1914 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1915 void (*cbfcnp)(struct cam_periph *, union ccb *),
1916 u_int8_t tag_action,
1917 u_int16_t element_address,
1918 u_int8_t send_action_code,
1919 struct scsi_send_volume_tag_parameters *parameters,
1920 u_int8_t sense_len, u_int32_t timeout)
1922 struct scsi_send_volume_tag *scsi_cmd;
1924 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1925 bzero(scsi_cmd, sizeof(*scsi_cmd));
1927 scsi_cmd->opcode = SEND_VOLUME_TAG;
1928 scsi_ulto2b(element_address, scsi_cmd->ea);
1929 scsi_cmd->sac = send_action_code;
1930 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1935 /*flags*/ CAM_DIR_OUT,
1937 /* data_ptr */ (u_int8_t *) parameters,
1938 sizeof(*parameters),