2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Derived from the NetBSD SCSI changer driver.
31 * $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
35 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
36 * All rights reserved.
38 * Partially based on an autochanger driver written by Stefan Grefen
39 * and on an autochanger driver written by the Systems Programming Group
40 * at the University of Utah Computer Science Department.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgements:
52 * This product includes software developed by Jason R. Thorpe
53 * for And Communications, http://www.and.com/
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
62 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
64 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
73 #include <sys/param.h>
74 #include <sys/queue.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/types.h>
78 #include <sys/malloc.h>
79 #include <sys/fcntl.h>
82 #include <sys/errno.h>
83 #include <sys/devicestat.h>
86 #include <cam/cam_ccb.h>
87 #include <cam/cam_periph.h>
88 #include <cam/cam_xpt_periph.h>
89 #include <cam/cam_debug.h>
91 #include <cam/scsi/scsi_all.h>
92 #include <cam/scsi/scsi_message.h>
93 #include <cam/scsi/scsi_ch.h>
96 * Timeout definitions for various changer related commands. They may
97 * be too short for some devices (especially the timeout for INITIALIZE
101 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
102 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 15 * 60 * 1000;
103 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 15 * 60 * 1000;
104 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 15 * 60 * 1000;
105 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 5 * 60 * 1000;
106 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
107 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
110 CH_FLAG_INVALID = 0x001
129 #define CH_Q_BIT_STRING \
134 #define ccb_state ppriv_field0
135 #define ccb_bp ppriv_ptr1
137 struct scsi_mode_sense_data {
138 struct scsi_mode_header_6 header;
139 struct scsi_mode_blk_desc blk_desc;
141 struct page_element_address_assignment ea;
142 struct page_transport_geometry_parameters tg;
143 struct page_device_capabilities cap;
152 struct devstat *device_stats;
156 int sc_picker; /* current picker */
159 * The following information is obtained from the
160 * element address assignment page.
162 int sc_firsts[CHET_MAX + 1]; /* firsts */
163 int sc_counts[CHET_MAX + 1]; /* counts */
166 * The following mask defines the legal combinations
167 * of elements for the MOVE MEDIUM command.
169 u_int8_t sc_movemask[CHET_MAX + 1];
172 * As above, but for EXCHANGE MEDIUM.
174 u_int8_t sc_exchangemask[CHET_MAX + 1];
177 * Quirks; see below. XXX KDM not implemented yet
179 int sc_settledelay; /* delay for settle */
182 static d_open_t chopen;
183 static d_close_t chclose;
184 static d_ioctl_t chioctl;
185 static periph_init_t chinit;
186 static periph_ctor_t chregister;
187 static periph_oninv_t choninvalidate;
188 static periph_dtor_t chcleanup;
189 static periph_start_t chstart;
190 static void chasync(void *callback_arg, u_int32_t code,
191 struct cam_path *path, void *arg);
192 static void chdone(struct cam_periph *periph,
193 union ccb *done_ccb);
194 static int cherror(union ccb *ccb, u_int32_t cam_flags,
195 u_int32_t sense_flags);
196 static int chmove(struct cam_periph *periph,
197 struct changer_move *cm);
198 static int chexchange(struct cam_periph *periph,
199 struct changer_exchange *ce);
200 static int chposition(struct cam_periph *periph,
201 struct changer_position *cp);
202 static int chgetelemstatus(struct cam_periph *periph,
203 int scsi_version, u_long cmd,
204 struct changer_element_status_request *csr);
205 static int chsetvoltag(struct cam_periph *periph,
206 struct changer_set_voltag_request *csvr);
207 static int chielem(struct cam_periph *periph,
208 unsigned int timeout);
209 static int chgetparams(struct cam_periph *periph);
210 static int chscsiversion(struct cam_periph *periph);
212 static struct periph_driver chdriver =
215 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
218 PERIPHDRIVER_DECLARE(ch, chdriver);
220 static struct cdevsw ch_cdevsw = {
221 .d_version = D_VERSION,
222 .d_flags = D_TRACKCLOSE,
229 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
237 * Install a global async callback. This callback will
238 * receive async callbacks like "new device found".
240 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
242 if (status != CAM_REQ_CMP) {
243 printf("ch: Failed to attach master async callback "
244 "due to status 0x%x!\n", status);
249 chdevgonecb(void *arg)
252 struct ch_softc *softc;
253 struct cam_periph *periph;
256 periph = (struct cam_periph *)arg;
258 softc = (struct ch_softc *)periph->softc;
260 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 SIM 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.
287 mtx_unlock(sim->mtx);
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)
343 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
347 * Allocate a peripheral instance for
348 * this device and start the probe
351 status = cam_periph_alloc(chregister, choninvalidate,
352 chcleanup, chstart, "ch",
353 CAM_PERIPH_BIO, cgd->ccb_h.path,
354 chasync, AC_FOUND_DEVICE, cgd);
356 if (status != CAM_REQ_CMP
357 && status != CAM_REQ_INPROG)
358 printf("chasync: Unable to probe new device "
359 "due to status 0x%x\n", status);
365 cam_periph_async(periph, code, path, arg);
371 chregister(struct cam_periph *periph, void *arg)
373 struct ch_softc *softc;
374 struct ccb_getdev *cgd;
375 struct ccb_pathinq cpi;
377 cgd = (struct ccb_getdev *)arg;
379 printf("chregister: no getdev CCB, can't register device\n");
380 return(CAM_REQ_CMP_ERR);
383 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
386 printf("chregister: Unable to probe new device. "
387 "Unable to allocate softc\n");
388 return(CAM_REQ_CMP_ERR);
391 bzero(softc, sizeof(*softc));
392 softc->state = CH_STATE_PROBE;
393 periph->softc = softc;
394 softc->quirks = CH_Q_NONE;
397 * The DVCID and CURDATA bits were not introduced until the SMC
398 * spec. If this device claims SCSI-2 or earlier support, then it
399 * very likely does not support these bits.
401 if (cgd->inq_data.version <= SCSI_REV_2)
402 softc->quirks |= CH_Q_NO_DVCID;
404 bzero(&cpi, sizeof(cpi));
405 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
406 cpi.ccb_h.func_code = XPT_PATH_INQ;
407 xpt_action((union ccb *)&cpi);
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 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
436 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
437 periph->unit_number);
438 cam_periph_lock(periph);
439 softc->dev->si_drv1 = periph;
442 * Add an async callback so that we get
443 * notified if this device goes away.
445 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
448 * Lock this periph until we are setup.
449 * This first call can't block
451 (void)cam_periph_hold(periph, PRIBIO);
452 xpt_schedule(periph, CAM_PRIORITY_DEV);
458 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
460 struct cam_periph *periph;
461 struct ch_softc *softc;
464 periph = (struct cam_periph *)dev->si_drv1;
465 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
468 softc = (struct ch_softc *)periph->softc;
470 cam_periph_lock(periph);
472 if (softc->flags & CH_FLAG_INVALID) {
473 cam_periph_release_locked(periph);
474 cam_periph_unlock(periph);
478 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
479 cam_periph_unlock(periph);
480 cam_periph_release(periph);
485 * Load information about this changer device into the softc.
487 if ((error = chgetparams(periph)) != 0) {
488 cam_periph_unhold(periph);
489 cam_periph_release_locked(periph);
490 cam_periph_unlock(periph);
494 cam_periph_unhold(periph);
498 cam_periph_unlock(periph);
504 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
507 struct cam_periph *periph;
508 struct ch_softc *softc;
510 periph = (struct cam_periph *)dev->si_drv1;
515 softc = (struct ch_softc *)periph->softc;
521 cam_periph_release_locked(periph);
524 * We reference the SIM lock directly here, instead of using
525 * cam_periph_unlock(). The reason is that the call to
526 * cam_periph_release_locked() above could result in the periph
527 * getting freed. If that is the case, dereferencing the periph
528 * with a cam_periph_unlock() call would cause a page fault.
530 * cam_periph_release() avoids this problem using the same method,
531 * but we're manually acquiring and dropping the lock here to
532 * protect the open count and avoid another lock acquisition and
535 mtx_unlock(sim->mtx);
541 chstart(struct cam_periph *periph, union ccb *start_ccb)
543 struct ch_softc *softc;
545 softc = (struct ch_softc *)periph->softc;
547 switch (softc->state) {
548 case CH_STATE_NORMAL:
550 if (periph->immediate_priority <= periph->pinfo.priority){
551 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
553 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
555 periph->immediate_priority = CAM_PRIORITY_NONE;
556 wakeup(&periph->ccb_list);
566 * Include the block descriptor when calculating the mode
569 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
570 sizeof(struct scsi_mode_blk_desc) +
571 sizeof(struct page_element_address_assignment);
573 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
575 if (mode_buffer == NULL) {
576 printf("chstart: couldn't malloc mode sense data\n");
579 bzero(mode_buffer, mode_buffer_len);
582 * Get the element address assignment page.
584 scsi_mode_sense(&start_ccb->csio,
587 /* tag_action */ MSG_SIMPLE_Q_TAG,
588 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
590 /* page_code */ SMS_PAGE_CTRL_CURRENT,
591 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
592 /* param_buf */ (u_int8_t *)mode_buffer,
593 /* param_len */ mode_buffer_len,
594 /* sense_len */ SSD_FULL_SIZE,
595 /* timeout */ CH_TIMEOUT_MODE_SENSE);
597 start_ccb->ccb_h.ccb_bp = NULL;
598 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
599 xpt_action(start_ccb);
606 chdone(struct cam_periph *periph, union ccb *done_ccb)
608 struct ch_softc *softc;
609 struct ccb_scsiio *csio;
611 softc = (struct ch_softc *)periph->softc;
612 csio = &done_ccb->csio;
614 switch(done_ccb->ccb_h.ccb_state) {
617 struct scsi_mode_header_6 *mode_header;
618 struct page_element_address_assignment *ea;
619 char announce_buf[80];
622 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
624 ea = (struct page_element_address_assignment *)
625 find_mode_page_6(mode_header);
627 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
629 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
630 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
631 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
632 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
633 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
634 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
635 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
636 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
637 softc->sc_picker = softc->sc_firsts[CHET_MT];
639 #define PLURAL(c) (c) == 1 ? "" : "s"
640 snprintf(announce_buf, sizeof(announce_buf),
641 "%d slot%s, %d drive%s, "
642 "%d picker%s, %d portal%s",
643 softc->sc_counts[CHET_ST],
644 PLURAL(softc->sc_counts[CHET_ST]),
645 softc->sc_counts[CHET_DT],
646 PLURAL(softc->sc_counts[CHET_DT]),
647 softc->sc_counts[CHET_MT],
648 PLURAL(softc->sc_counts[CHET_MT]),
649 softc->sc_counts[CHET_IE],
650 PLURAL(softc->sc_counts[CHET_IE]));
652 if (announce_buf[0] != '\0') {
653 xpt_announce_periph(periph, announce_buf);
654 xpt_announce_quirks(periph, softc->quirks,
660 error = cherror(done_ccb, CAM_RETRY_SELTO,
661 SF_RETRY_UA | SF_NO_PRINT);
663 * Retry any UNIT ATTENTION type errors. They
664 * are expected at boot.
666 if (error == ERESTART) {
668 * A retry was scheuled, so
672 } else if (error != 0) {
674 struct scsi_mode_sense_6 *sms;
676 sms = (struct scsi_mode_sense_6 *)
677 done_ccb->csio.cdb_io.cdb_bytes;
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) {
689 sms->byte2 &= ~SMS_DBD;
690 xpt_action(done_ccb);
691 softc->quirks |= CH_Q_NO_DBD;
696 /* Don't wedge this device's queue */
697 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
698 cam_release_devq(done_ccb->ccb_h.path,
707 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
708 == CAM_SCSI_STATUS_ERROR)
709 scsi_sense_print(&done_ccb->csio);
711 xpt_print(periph->path,
712 "got CAM status %#x\n",
713 done_ccb->ccb_h.status);
715 xpt_print(periph->path, "fatal error, failed "
716 "to attach to device\n");
718 cam_periph_invalidate(periph);
722 softc->state = CH_STATE_NORMAL;
723 free(mode_header, M_SCSICH);
725 * Since our peripheral may be invalidated by an error
726 * above or an external event, we must release our CCB
727 * before releasing the probe lock on the peripheral.
728 * The peripheral will only go away once the last lock
729 * is removed, and we need it around for the CCB release
732 xpt_release_ccb(done_ccb);
733 cam_periph_unhold(periph);
738 /* Caller will release the CCB */
739 wakeup(&done_ccb->ccb_h.cbfcnp);
745 xpt_release_ccb(done_ccb);
749 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
751 struct ch_softc *softc;
752 struct cam_periph *periph;
754 periph = xpt_path_periph(ccb->ccb_h.path);
755 softc = (struct ch_softc *)periph->softc;
757 return (cam_periph_error(ccb, cam_flags, sense_flags,
762 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
764 struct cam_periph *periph;
765 struct ch_softc *softc;
768 periph = (struct cam_periph *)dev->si_drv1;
772 cam_periph_lock(periph);
773 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
775 softc = (struct ch_softc *)periph->softc;
779 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
780 ("trying to do ioctl %#lx\n", cmd));
783 * If this command can change the device's state, we must
784 * have the device open for writing.
794 if ((flag & FWRITE) == 0) {
795 cam_periph_unlock(periph);
802 error = chmove(periph, (struct changer_move *)addr);
806 error = chexchange(periph, (struct changer_exchange *)addr);
810 error = chposition(periph, (struct changer_position *)addr);
814 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
819 int new_picker = *(int *)addr;
821 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
825 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
830 struct changer_params *cp = (struct changer_params *)addr;
832 cp->cp_npickers = softc->sc_counts[CHET_MT];
833 cp->cp_nslots = softc->sc_counts[CHET_ST];
834 cp->cp_nportals = softc->sc_counts[CHET_IE];
835 cp->cp_ndrives = softc->sc_counts[CHET_DT];
839 error = chielem(periph, *(unsigned int *)addr);
844 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
845 (struct changer_element_status_request *)addr);
853 scsi_version = chscsiversion(periph);
854 if (scsi_version >= SCSI_REV_0) {
855 error = chgetelemstatus(periph, scsi_version, cmd,
856 (struct changer_element_status_request *)addr);
858 else { /* unable to determine the SCSI version */
859 cam_periph_unlock(periph);
867 error = chsetvoltag(periph,
868 (struct changer_set_voltag_request *) addr);
872 /* Implement prevent/allow? */
875 error = cam_periph_ioctl(periph, cmd, addr, cherror);
879 cam_periph_unlock(periph);
884 chmove(struct cam_periph *periph, struct changer_move *cm)
886 struct ch_softc *softc;
887 u_int16_t fromelem, toelem;
892 softc = (struct ch_softc *)periph->softc;
897 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
899 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
900 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
904 * Check the request against the changer's capabilities.
906 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
910 * Calculate the source and destination elements.
912 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
913 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
915 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
917 scsi_move_medium(&ccb->csio,
920 /* tag_action */ MSG_SIMPLE_Q_TAG,
921 /* tea */ softc->sc_picker,
924 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
925 /* sense_len */ SSD_FULL_SIZE,
926 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
928 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
929 /*sense_flags*/ SF_RETRY_UA,
930 softc->device_stats);
932 xpt_release_ccb(ccb);
938 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
940 struct ch_softc *softc;
941 u_int16_t src, dst1, dst2;
946 softc = (struct ch_softc *)periph->softc;
950 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
951 (ce->ce_sdsttype > CHET_DT))
953 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
954 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
955 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
959 * Check the request against the changer's capabilities.
961 if (((softc->sc_exchangemask[ce->ce_srctype] &
962 (1 << ce->ce_fdsttype)) == 0) ||
963 ((softc->sc_exchangemask[ce->ce_fdsttype] &
964 (1 << ce->ce_sdsttype)) == 0))
968 * Calculate the source and destination elements.
970 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
971 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
972 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
974 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
976 scsi_exchange_medium(&ccb->csio,
979 /* tag_action */ MSG_SIMPLE_Q_TAG,
980 /* tea */ softc->sc_picker,
984 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
986 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
988 /* sense_len */ SSD_FULL_SIZE,
989 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
991 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
992 /*sense_flags*/ SF_RETRY_UA,
993 softc->device_stats);
995 xpt_release_ccb(ccb);
1001 chposition(struct cam_periph *periph, struct changer_position *cp)
1003 struct ch_softc *softc;
1009 softc = (struct ch_softc *)periph->softc;
1014 if (cp->cp_type > CHET_DT)
1016 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1020 * Calculate the destination element.
1022 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1024 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1026 scsi_position_to_element(&ccb->csio,
1028 /* cbfcnp */ chdone,
1029 /* tag_action */ MSG_SIMPLE_Q_TAG,
1030 /* tea */ softc->sc_picker,
1032 /* invert */ (cp->cp_flags & CP_INVERT) ?
1034 /* sense_len */ SSD_FULL_SIZE,
1035 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1037 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1038 /*sense_flags*/ SF_RETRY_UA,
1039 softc->device_stats);
1041 xpt_release_ccb(ccb);
1047 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1048 * to host native byte order in the volume serial number. The volume
1049 * label as returned by the changer is transferred to user mode as
1050 * nul-terminated string. Volume labels are truncated at the first
1051 * space, as suggested by SCSI-2.
1054 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1057 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1058 char c = voltag->vif[i];
1060 uvoltag->cv_volid[i] = c;
1064 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1068 * Copy an element status descriptor to a user-mode
1069 * changer_element_status structure.
1072 copy_element_status(struct ch_softc *softc,
1074 struct read_element_status_descriptor *desc,
1075 struct changer_element_status *ces,
1078 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1080 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1081 struct read_element_status_device_id *devid = NULL;
1083 ces->ces_int_addr = eaddr;
1084 /* set up logical address in element status */
1085 for (et = CHET_MT; et <= CHET_DT; et++) {
1086 if ((softc->sc_firsts[et] <= eaddr)
1087 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1089 ces->ces_addr = eaddr - softc->sc_firsts[et];
1095 ces->ces_flags = desc->flags1;
1097 ces->ces_sensecode = desc->sense_code;
1098 ces->ces_sensequal = desc->sense_qual;
1100 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1101 ces->ces_flags |= CES_INVERT;
1103 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1105 eaddr = scsi_2btoul(desc->ssea);
1107 /* convert source address to logical format */
1108 for (et = CHET_MT; et <= CHET_DT; et++) {
1109 if ((softc->sc_firsts[et] <= eaddr)
1110 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1112 ces->ces_source_addr =
1113 eaddr - softc->sc_firsts[et];
1114 ces->ces_source_type = et;
1115 ces->ces_flags |= CES_SOURCE_VALID;
1120 if (!(ces->ces_flags & CES_SOURCE_VALID))
1121 printf("ch: warning: could not map element source "
1122 "address %ud to a valid element type\n",
1127 * pvoltag and avoltag are common between SCSI-2 and later versions
1129 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1130 pvol_tag = &desc->voltag_devid.pvoltag;
1131 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1132 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1133 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1135 * For SCSI-3 and later, element status can carry designator and
1136 * other information.
1138 if (scsi_version >= SCSI_REV_SPC) {
1139 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1140 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1141 devid = &desc->voltag_devid.pvol_and_devid.devid;
1142 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1143 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1144 devid = &desc->voltag_devid.devid;
1145 else /* Have both PVOLTAG and AVOLTAG */
1146 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1150 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1152 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1153 if (devid != NULL) {
1154 if (devid->designator_length > 0) {
1155 bcopy((void *)devid->designator,
1156 (void *)ces->ces_designator,
1157 devid->designator_length);
1158 ces->ces_designator_length = devid->designator_length;
1160 * Make sure we are always NUL terminated. The
1161 * This won't matter for the binary code set,
1162 * since the user will only pay attention to the
1165 ces->ces_designator[devid->designator_length]= '\0';
1167 if (devid->piv_assoc_designator_type &
1168 READ_ELEMENT_STATUS_PIV_SET) {
1169 ces->ces_flags |= CES_PIV;
1170 ces->ces_protocol_id =
1171 READ_ELEMENT_STATUS_PROTOCOL_ID(
1172 devid->prot_code_set);
1175 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1176 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1177 devid->piv_assoc_designator_type);
1178 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1179 devid->piv_assoc_designator_type);
1180 } else if (scsi_version > SCSI_REV_2) {
1181 /* SCSI-SPC and No devid, no designator */
1182 ces->ces_designator_length = 0;
1183 ces->ces_designator[0] = '\0';
1184 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1187 if (scsi_version <= SCSI_REV_2) {
1188 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1189 READ_ELEMENT_STATUS_DT_IDVALID) {
1190 ces->ces_flags |= CES_SCSIID_VALID;
1192 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1195 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1196 READ_ELEMENT_STATUS_DT_LUVALID) {
1197 ces->ces_flags |= CES_LUN_VALID;
1199 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1200 READ_ELEMENT_STATUS_DT_LUNMASK;
1206 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1207 struct changer_element_status_request *cesr)
1209 struct read_element_status_header *st_hdr;
1210 struct read_element_status_page_header *pg_hdr;
1211 struct read_element_status_descriptor *desc;
1212 caddr_t data = NULL;
1213 size_t size, desclen;
1214 int avail, i, error = 0;
1215 int curdata, dvcid, sense_flags;
1216 int try_no_dvcid = 0;
1217 struct changer_element_status *user_data = NULL;
1218 struct ch_softc *softc;
1220 int chet = cesr->cesr_element_type;
1221 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1223 softc = (struct ch_softc *)periph->softc;
1225 /* perform argument checking */
1228 * Perform a range check on the cesr_element_{base,count}
1229 * request argument fields.
1231 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1232 || (cesr->cesr_element_base + cesr->cesr_element_count)
1233 > softc->sc_counts[chet])
1237 * Request one descriptor for the given element type. This
1238 * is used to determine the size of the descriptor so that
1239 * we can allocate enough storage for all of them. We assume
1240 * that the first one can fit into 1k.
1242 cam_periph_unlock(periph);
1243 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1245 cam_periph_lock(periph);
1246 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1248 sense_flags = SF_RETRY_UA;
1249 if (softc->quirks & CH_Q_NO_DVCID) {
1256 * Don't print anything for an Illegal Request, because
1257 * these flags can cause some changers to complain. We'll
1258 * retry without them if we get an error.
1260 sense_flags |= SF_QUIET_IR;
1265 scsi_read_element_status(&ccb->csio,
1267 /* cbfcnp */ chdone,
1268 /* tag_action */ MSG_SIMPLE_Q_TAG,
1269 /* voltag */ want_voltags,
1270 /* sea */ softc->sc_firsts[chet],
1271 /* curdata */ curdata,
1274 /* data_ptr */ data,
1275 /* dxfer_len */ 1024,
1276 /* sense_len */ SSD_FULL_SIZE,
1277 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1279 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1280 /*sense_flags*/ sense_flags,
1281 softc->device_stats);
1284 * An Illegal Request sense key (only used if there is no asc/ascq)
1285 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1286 * curdata are set (we set both or neither), try turning them off
1287 * and see if the command is successful.
1289 if ((error == EINVAL)
1290 && (dvcid || curdata)) {
1294 /* At this point we want to report any Illegal Request */
1295 sense_flags &= ~SF_QUIET_IR;
1301 * In this case, we tried a read element status with dvcid and
1302 * curdata set, and it failed. We retried without those bits, and
1303 * it succeeded. Suggest to the user that he set a quirk, so we
1304 * don't go through the retry process the first time in the future.
1305 * This should only happen on changers that claim SCSI-3 or higher,
1306 * but don't support these bits.
1308 if ((try_no_dvcid != 0)
1310 softc->quirks |= CH_Q_NO_DVCID;
1314 cam_periph_unlock(periph);
1316 st_hdr = (struct read_element_status_header *)data;
1317 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1318 sizeof(struct read_element_status_header));
1319 desclen = scsi_2btoul(pg_hdr->edl);
1321 size = sizeof(struct read_element_status_header) +
1322 sizeof(struct read_element_status_page_header) +
1323 (desclen * cesr->cesr_element_count);
1325 * Reallocate storage for descriptors and get them from the
1328 free(data, M_DEVBUF);
1329 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1331 cam_periph_lock(periph);
1332 scsi_read_element_status(&ccb->csio,
1334 /* cbfcnp */ chdone,
1335 /* tag_action */ MSG_SIMPLE_Q_TAG,
1336 /* voltag */ want_voltags,
1337 /* sea */ softc->sc_firsts[chet]
1338 + cesr->cesr_element_base,
1339 /* curdata */ curdata,
1341 /* count */ cesr->cesr_element_count,
1342 /* data_ptr */ data,
1343 /* dxfer_len */ size,
1344 /* sense_len */ SSD_FULL_SIZE,
1345 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1347 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1348 /*sense_flags*/ SF_RETRY_UA,
1349 softc->device_stats);
1353 cam_periph_unlock(periph);
1356 * Fill in the user status array.
1358 st_hdr = (struct read_element_status_header *)data;
1359 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1360 sizeof(struct read_element_status_header));
1361 avail = scsi_2btoul(st_hdr->count);
1363 if (avail != cesr->cesr_element_count) {
1364 xpt_print(periph->path,
1365 "warning, READ ELEMENT STATUS avail != count\n");
1368 user_data = (struct changer_element_status *)
1369 malloc(avail * sizeof(struct changer_element_status),
1370 M_DEVBUF, M_WAITOK | M_ZERO);
1372 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1373 sizeof(struct read_element_status_header) +
1374 sizeof(struct read_element_status_page_header));
1376 * Set up the individual element status structures
1378 for (i = 0; i < avail; ++i) {
1379 struct changer_element_status *ces;
1382 * In the changer_element_status structure, fields from
1383 * the beginning to the field of ces_scsi_lun are common
1384 * between SCSI-2 and SCSI-3, while all the rest are new
1385 * from SCSI-3. In order to maintain backward compatibility
1386 * of the chio command, the ces pointer, below, is computed
1387 * such that it lines up with the structure boundary
1388 * corresponding to the SCSI version.
1390 ces = cmd == OCHIOGSTATUS ?
1391 (struct changer_element_status *)
1392 ((unsigned char *)user_data + i *
1393 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1396 copy_element_status(softc, pg_hdr->flags, desc,
1399 desc = (struct read_element_status_descriptor *)
1400 ((unsigned char *)desc + desclen);
1403 /* Copy element status structures out to userspace. */
1404 if (cmd == OCHIOGSTATUS)
1405 error = copyout(user_data,
1406 cesr->cesr_element_status,
1407 avail* (offsetof(struct changer_element_status,
1408 ces_scsi_lun) + 1));
1410 error = copyout(user_data,
1411 cesr->cesr_element_status,
1412 avail * sizeof(struct changer_element_status));
1414 cam_periph_lock(periph);
1417 xpt_release_ccb(ccb);
1420 free(data, M_DEVBUF);
1421 if (user_data != NULL)
1422 free(user_data, M_DEVBUF);
1428 chielem(struct cam_periph *periph,
1429 unsigned int timeout)
1432 struct ch_softc *softc;
1436 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1442 softc = (struct ch_softc *)periph->softc;
1444 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1446 scsi_initialize_element_status(&ccb->csio,
1448 /* cbfcnp */ chdone,
1449 /* tag_action */ MSG_SIMPLE_Q_TAG,
1450 /* sense_len */ SSD_FULL_SIZE,
1451 /* timeout */ timeout);
1453 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1454 /*sense_flags*/ SF_RETRY_UA,
1455 softc->device_stats);
1457 xpt_release_ccb(ccb);
1463 chsetvoltag(struct cam_periph *periph,
1464 struct changer_set_voltag_request *csvr)
1467 struct ch_softc *softc;
1470 struct scsi_send_volume_tag_parameters ssvtp;
1475 softc = (struct ch_softc *)periph->softc;
1477 bzero(&ssvtp, sizeof(ssvtp));
1478 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1479 ssvtp.vitf[i] = ' ';
1485 if (csvr->csvr_type > CHET_DT)
1487 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1490 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1492 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1493 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1495 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1497 case CSVR_MODE_REPLACE:
1498 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1500 case CSVR_MODE_CLEAR:
1501 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1508 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1510 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1512 case CSVR_MODE_REPLACE:
1513 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1515 case CSVR_MODE_CLEAR:
1516 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1524 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1525 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1526 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1528 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1530 scsi_send_volume_tag(&ccb->csio,
1532 /* cbfcnp */ chdone,
1533 /* tag_action */ MSG_SIMPLE_Q_TAG,
1534 /* element_address */ ea,
1535 /* send_action_code */ sac,
1536 /* parameters */ &ssvtp,
1537 /* sense_len */ SSD_FULL_SIZE,
1538 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1540 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1541 /*sense_flags*/ SF_RETRY_UA,
1542 softc->device_stats);
1544 xpt_release_ccb(ccb);
1551 chgetparams(struct cam_periph *periph)
1554 struct ch_softc *softc;
1556 int mode_buffer_len;
1557 struct page_element_address_assignment *ea;
1558 struct page_device_capabilities *cap;
1559 int error, from, dbd;
1560 u_int8_t *moves, *exchanges;
1564 softc = (struct ch_softc *)periph->softc;
1566 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1569 * The scsi_mode_sense_data structure is just a convenience
1570 * structure that allows us to easily calculate the worst-case
1571 * storage size of the mode sense buffer.
1573 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1575 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1577 if (mode_buffer == NULL) {
1578 printf("chgetparams: couldn't malloc mode sense data\n");
1582 bzero(mode_buffer, mode_buffer_len);
1584 if (softc->quirks & CH_Q_NO_DBD)
1590 * Get the element address assignment page.
1592 scsi_mode_sense(&ccb->csio,
1594 /* cbfcnp */ chdone,
1595 /* tag_action */ MSG_SIMPLE_Q_TAG,
1597 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1598 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1599 /* param_buf */ (u_int8_t *)mode_buffer,
1600 /* param_len */ mode_buffer_len,
1601 /* sense_len */ SSD_FULL_SIZE,
1602 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1604 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1605 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1606 softc->device_stats);
1610 struct scsi_mode_sense_6 *sms;
1612 sms = (struct scsi_mode_sense_6 *)
1613 ccb->csio.cdb_io.cdb_bytes;
1615 sms->byte2 &= ~SMS_DBD;
1616 error = cam_periph_runccb(ccb, cherror,
1617 /*cam_flags*/ CAM_RETRY_SELTO,
1618 /*sense_flags*/ SF_RETRY_UA,
1619 softc->device_stats);
1622 * Since we disabled sense printing above, print
1623 * out the sense here since we got an error.
1625 scsi_sense_print(&ccb->csio);
1629 xpt_print(periph->path,
1630 "chgetparams: error getting element "
1632 xpt_release_ccb(ccb);
1633 free(mode_buffer, M_SCSICH);
1638 ea = (struct page_element_address_assignment *)
1639 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1641 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1642 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1643 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1644 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1645 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1646 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1647 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1648 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1650 bzero(mode_buffer, mode_buffer_len);
1653 * Now get the device capabilities page.
1655 scsi_mode_sense(&ccb->csio,
1657 /* cbfcnp */ chdone,
1658 /* tag_action */ MSG_SIMPLE_Q_TAG,
1660 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1661 /* page */ CH_DEVICE_CAP_PAGE,
1662 /* param_buf */ (u_int8_t *)mode_buffer,
1663 /* param_len */ mode_buffer_len,
1664 /* sense_len */ SSD_FULL_SIZE,
1665 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1667 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1668 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1669 softc->device_stats);
1673 struct scsi_mode_sense_6 *sms;
1675 sms = (struct scsi_mode_sense_6 *)
1676 ccb->csio.cdb_io.cdb_bytes;
1678 sms->byte2 &= ~SMS_DBD;
1679 error = cam_periph_runccb(ccb, cherror,
1680 /*cam_flags*/ CAM_RETRY_SELTO,
1681 /*sense_flags*/ SF_RETRY_UA,
1682 softc->device_stats);
1685 * Since we disabled sense printing above, print
1686 * out the sense here since we got an error.
1688 scsi_sense_print(&ccb->csio);
1692 xpt_print(periph->path,
1693 "chgetparams: error getting device "
1694 "capabilities page\n");
1695 xpt_release_ccb(ccb);
1696 free(mode_buffer, M_SCSICH);
1701 xpt_release_ccb(ccb);
1703 cap = (struct page_device_capabilities *)
1704 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1706 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1707 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1708 moves = cap->move_from;
1709 exchanges = cap->exchange_with;
1710 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1711 softc->sc_movemask[from] = moves[from];
1712 softc->sc_exchangemask[from] = exchanges[from];
1715 free(mode_buffer, M_SCSICH);
1721 chscsiversion(struct cam_periph *periph)
1723 struct scsi_inquiry_data *inq_data;
1724 struct ccb_getdev *cgd;
1725 int dev_scsi_version;
1726 struct cam_sim *sim;
1728 sim = xpt_path_sim(periph->path);
1729 mtx_assert(sim->mtx, MA_OWNED);
1730 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1733 * Get the device information.
1735 xpt_setup_ccb(&cgd->ccb_h,
1737 CAM_PRIORITY_NORMAL);
1738 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1739 xpt_action((union ccb *)cgd);
1741 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1742 xpt_free_ccb((union ccb *)cgd);
1746 inq_data = &cgd->inq_data;
1747 dev_scsi_version = inq_data->version;
1748 xpt_free_ccb((union ccb *)cgd);
1750 return dev_scsi_version;
1754 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1755 void (*cbfcnp)(struct cam_periph *, union ccb *),
1756 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1757 u_int32_t dst, int invert, u_int8_t sense_len,
1760 struct scsi_move_medium *scsi_cmd;
1762 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1763 bzero(scsi_cmd, sizeof(*scsi_cmd));
1765 scsi_cmd->opcode = MOVE_MEDIUM;
1767 scsi_ulto2b(tea, scsi_cmd->tea);
1768 scsi_ulto2b(src, scsi_cmd->src);
1769 scsi_ulto2b(dst, scsi_cmd->dst);
1772 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1777 /*flags*/ CAM_DIR_NONE,
1787 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1788 void (*cbfcnp)(struct cam_periph *, union ccb *),
1789 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1790 u_int32_t dst1, u_int32_t dst2, int invert1,
1791 int invert2, u_int8_t sense_len, u_int32_t timeout)
1793 struct scsi_exchange_medium *scsi_cmd;
1795 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1796 bzero(scsi_cmd, sizeof(*scsi_cmd));
1798 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1800 scsi_ulto2b(tea, scsi_cmd->tea);
1801 scsi_ulto2b(src, scsi_cmd->src);
1802 scsi_ulto2b(dst1, scsi_cmd->fdst);
1803 scsi_ulto2b(dst2, scsi_cmd->sdst);
1806 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1809 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1814 /*flags*/ CAM_DIR_NONE,
1824 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1825 void (*cbfcnp)(struct cam_periph *, union ccb *),
1826 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1827 int invert, u_int8_t sense_len, u_int32_t timeout)
1829 struct scsi_position_to_element *scsi_cmd;
1831 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1832 bzero(scsi_cmd, sizeof(*scsi_cmd));
1834 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1836 scsi_ulto2b(tea, scsi_cmd->tea);
1837 scsi_ulto2b(dst, scsi_cmd->dst);
1840 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1845 /*flags*/ CAM_DIR_NONE,
1855 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1856 void (*cbfcnp)(struct cam_periph *, union ccb *),
1857 u_int8_t tag_action, int voltag, u_int32_t sea,
1858 int curdata, int dvcid,
1859 u_int32_t count, u_int8_t *data_ptr,
1860 u_int32_t dxfer_len, u_int8_t sense_len,
1863 struct scsi_read_element_status *scsi_cmd;
1865 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1866 bzero(scsi_cmd, sizeof(*scsi_cmd));
1868 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1870 scsi_ulto2b(sea, scsi_cmd->sea);
1871 scsi_ulto2b(count, scsi_cmd->count);
1872 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1874 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1876 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1879 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1884 /*flags*/ CAM_DIR_IN,
1894 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1895 void (*cbfcnp)(struct cam_periph *, union ccb *),
1896 u_int8_t tag_action, u_int8_t sense_len,
1899 struct scsi_initialize_element_status *scsi_cmd;
1901 scsi_cmd = (struct scsi_initialize_element_status *)
1902 &csio->cdb_io.cdb_bytes;
1903 bzero(scsi_cmd, sizeof(*scsi_cmd));
1905 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1910 /*flags*/ CAM_DIR_NONE,
1912 /* data_ptr */ NULL,
1920 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1921 void (*cbfcnp)(struct cam_periph *, union ccb *),
1922 u_int8_t tag_action,
1923 u_int16_t element_address,
1924 u_int8_t send_action_code,
1925 struct scsi_send_volume_tag_parameters *parameters,
1926 u_int8_t sense_len, u_int32_t timeout)
1928 struct scsi_send_volume_tag *scsi_cmd;
1930 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1931 bzero(scsi_cmd, sizeof(*scsi_cmd));
1933 scsi_cmd->opcode = SEND_VOLUME_TAG;
1934 scsi_ulto2b(element_address, scsi_cmd->ea);
1935 scsi_cmd->sac = send_action_code;
1936 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1941 /*flags*/ CAM_DIR_OUT,
1943 /* data_ptr */ (u_int8_t *) parameters,
1944 sizeof(*parameters),