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);
310 xpt_print(periph->path, "lost device\n");
315 chcleanup(struct cam_periph *periph)
317 struct ch_softc *softc;
319 softc = (struct ch_softc *)periph->softc;
321 xpt_print(periph->path, "removing device entry\n");
323 devstat_remove_entry(softc->device_stats);
325 free(softc, M_DEVBUF);
329 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
331 struct cam_periph *periph;
333 periph = (struct cam_periph *)callback_arg;
336 case AC_FOUND_DEVICE:
338 struct ccb_getdev *cgd;
341 cgd = (struct ccb_getdev *)arg;
345 if (cgd->protocol != PROTO_SCSI)
348 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
352 * Allocate a peripheral instance for
353 * this device and start the probe
356 status = cam_periph_alloc(chregister, choninvalidate,
357 chcleanup, chstart, "ch",
358 CAM_PERIPH_BIO, cgd->ccb_h.path,
359 chasync, AC_FOUND_DEVICE, cgd);
361 if (status != CAM_REQ_CMP
362 && status != CAM_REQ_INPROG)
363 printf("chasync: Unable to probe new device "
364 "due to status 0x%x\n", status);
370 cam_periph_async(periph, code, path, arg);
376 chregister(struct cam_periph *periph, void *arg)
378 struct ch_softc *softc;
379 struct ccb_getdev *cgd;
380 struct ccb_pathinq cpi;
382 cgd = (struct ccb_getdev *)arg;
384 printf("chregister: no getdev CCB, can't register device\n");
385 return(CAM_REQ_CMP_ERR);
388 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
391 printf("chregister: Unable to probe new device. "
392 "Unable to allocate softc\n");
393 return(CAM_REQ_CMP_ERR);
396 bzero(softc, sizeof(*softc));
397 softc->state = CH_STATE_PROBE;
398 periph->softc = softc;
399 softc->quirks = CH_Q_NONE;
402 * The DVCID and CURDATA bits were not introduced until the SMC
403 * spec. If this device claims SCSI-2 or earlier support, then it
404 * very likely does not support these bits.
406 if (cgd->inq_data.version <= SCSI_REV_2)
407 softc->quirks |= CH_Q_NO_DVCID;
409 bzero(&cpi, sizeof(cpi));
410 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
411 cpi.ccb_h.func_code = XPT_PATH_INQ;
412 xpt_action((union ccb *)&cpi);
415 * Changers don't have a blocksize, and obviously don't support
418 cam_periph_unlock(periph);
419 softc->device_stats = devstat_new_entry("ch",
420 periph->unit_number, 0,
421 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
422 SID_TYPE(&cgd->inq_data) |
423 XPORT_DEVSTAT_TYPE(cpi.transport),
424 DEVSTAT_PRIORITY_OTHER);
427 * Acquire a reference to the periph before we create the devfs
428 * instance for it. We'll release this reference once the devfs
429 * instance has been freed.
431 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
432 xpt_print(periph->path, "%s: lost periph during "
433 "registration!\n", __func__);
434 cam_periph_lock(periph);
435 return (CAM_REQ_CMP_ERR);
439 /* Register the device */
440 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
441 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
442 periph->unit_number);
443 cam_periph_lock(periph);
444 softc->dev->si_drv1 = periph;
447 * Add an async callback so that we get
448 * notified if this device goes away.
450 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
453 * Lock this periph until we are setup.
454 * This first call can't block
456 (void)cam_periph_hold(periph, PRIBIO);
457 xpt_schedule(periph, CAM_PRIORITY_DEV);
463 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
465 struct cam_periph *periph;
466 struct ch_softc *softc;
469 periph = (struct cam_periph *)dev->si_drv1;
470 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
473 softc = (struct ch_softc *)periph->softc;
475 cam_periph_lock(periph);
477 if (softc->flags & CH_FLAG_INVALID) {
478 cam_periph_release_locked(periph);
479 cam_periph_unlock(periph);
483 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
484 cam_periph_unlock(periph);
485 cam_periph_release(periph);
490 * Load information about this changer device into the softc.
492 if ((error = chgetparams(periph)) != 0) {
493 cam_periph_unhold(periph);
494 cam_periph_release_locked(periph);
495 cam_periph_unlock(periph);
499 cam_periph_unhold(periph);
503 cam_periph_unlock(periph);
509 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
512 struct cam_periph *periph;
513 struct ch_softc *softc;
515 periph = (struct cam_periph *)dev->si_drv1;
520 softc = (struct ch_softc *)periph->softc;
526 cam_periph_release_locked(periph);
529 * We reference the SIM 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
540 mtx_unlock(sim->mtx);
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 if (periph->immediate_priority <= periph->pinfo.priority){
556 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
558 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
560 periph->immediate_priority = CAM_PRIORITY_NONE;
561 wakeup(&periph->ccb_list);
571 * Include the block descriptor when calculating the mode
574 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
575 sizeof(struct scsi_mode_blk_desc) +
576 sizeof(struct page_element_address_assignment);
578 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
580 if (mode_buffer == NULL) {
581 printf("chstart: couldn't malloc mode sense data\n");
584 bzero(mode_buffer, mode_buffer_len);
587 * Get the element address assignment page.
589 scsi_mode_sense(&start_ccb->csio,
592 /* tag_action */ MSG_SIMPLE_Q_TAG,
593 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
595 /* page_code */ SMS_PAGE_CTRL_CURRENT,
596 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
597 /* param_buf */ (u_int8_t *)mode_buffer,
598 /* param_len */ mode_buffer_len,
599 /* sense_len */ SSD_FULL_SIZE,
600 /* timeout */ CH_TIMEOUT_MODE_SENSE);
602 start_ccb->ccb_h.ccb_bp = NULL;
603 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
604 xpt_action(start_ccb);
611 chdone(struct cam_periph *periph, union ccb *done_ccb)
613 struct ch_softc *softc;
614 struct ccb_scsiio *csio;
616 softc = (struct ch_softc *)periph->softc;
617 csio = &done_ccb->csio;
619 switch(done_ccb->ccb_h.ccb_state) {
622 struct scsi_mode_header_6 *mode_header;
623 struct page_element_address_assignment *ea;
624 char announce_buf[80];
627 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
629 ea = (struct page_element_address_assignment *)
630 find_mode_page_6(mode_header);
632 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
634 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
635 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
636 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
637 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
638 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
639 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
640 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
641 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
642 softc->sc_picker = softc->sc_firsts[CHET_MT];
644 #define PLURAL(c) (c) == 1 ? "" : "s"
645 snprintf(announce_buf, sizeof(announce_buf),
646 "%d slot%s, %d drive%s, "
647 "%d picker%s, %d portal%s",
648 softc->sc_counts[CHET_ST],
649 PLURAL(softc->sc_counts[CHET_ST]),
650 softc->sc_counts[CHET_DT],
651 PLURAL(softc->sc_counts[CHET_DT]),
652 softc->sc_counts[CHET_MT],
653 PLURAL(softc->sc_counts[CHET_MT]),
654 softc->sc_counts[CHET_IE],
655 PLURAL(softc->sc_counts[CHET_IE]));
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);
720 announce_buf[0] = '\0';
723 if (announce_buf[0] != '\0') {
724 xpt_announce_periph(periph, announce_buf);
725 xpt_announce_quirks(periph, softc->quirks,
728 softc->state = CH_STATE_NORMAL;
729 free(mode_header, M_SCSICH);
731 * Since our peripheral may be invalidated by an error
732 * above or an external event, we must release our CCB
733 * before releasing the probe lock on the peripheral.
734 * The peripheral will only go away once the last lock
735 * is removed, and we need it around for the CCB release
738 xpt_release_ccb(done_ccb);
739 cam_periph_unhold(periph);
744 /* Caller will release the CCB */
745 wakeup(&done_ccb->ccb_h.cbfcnp);
751 xpt_release_ccb(done_ccb);
755 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
757 struct ch_softc *softc;
758 struct cam_periph *periph;
760 periph = xpt_path_periph(ccb->ccb_h.path);
761 softc = (struct ch_softc *)periph->softc;
763 return (cam_periph_error(ccb, cam_flags, sense_flags,
768 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
770 struct cam_periph *periph;
771 struct ch_softc *softc;
774 periph = (struct cam_periph *)dev->si_drv1;
778 cam_periph_lock(periph);
779 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
781 softc = (struct ch_softc *)periph->softc;
785 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
786 ("trying to do ioctl %#lx\n", cmd));
789 * If this command can change the device's state, we must
790 * have the device open for writing.
800 if ((flag & FWRITE) == 0) {
801 cam_periph_unlock(periph);
808 error = chmove(periph, (struct changer_move *)addr);
812 error = chexchange(periph, (struct changer_exchange *)addr);
816 error = chposition(periph, (struct changer_position *)addr);
820 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
825 int new_picker = *(int *)addr;
827 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
831 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
836 struct changer_params *cp = (struct changer_params *)addr;
838 cp->cp_npickers = softc->sc_counts[CHET_MT];
839 cp->cp_nslots = softc->sc_counts[CHET_ST];
840 cp->cp_nportals = softc->sc_counts[CHET_IE];
841 cp->cp_ndrives = softc->sc_counts[CHET_DT];
845 error = chielem(periph, *(unsigned int *)addr);
850 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
851 (struct changer_element_status_request *)addr);
859 scsi_version = chscsiversion(periph);
860 if (scsi_version >= SCSI_REV_0) {
861 error = chgetelemstatus(periph, scsi_version, cmd,
862 (struct changer_element_status_request *)addr);
864 else { /* unable to determine the SCSI version */
865 cam_periph_unlock(periph);
873 error = chsetvoltag(periph,
874 (struct changer_set_voltag_request *) addr);
878 /* Implement prevent/allow? */
881 error = cam_periph_ioctl(periph, cmd, addr, cherror);
885 cam_periph_unlock(periph);
890 chmove(struct cam_periph *periph, struct changer_move *cm)
892 struct ch_softc *softc;
893 u_int16_t fromelem, toelem;
898 softc = (struct ch_softc *)periph->softc;
903 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
905 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
906 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
910 * Check the request against the changer's capabilities.
912 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
916 * Calculate the source and destination elements.
918 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
919 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
921 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
923 scsi_move_medium(&ccb->csio,
926 /* tag_action */ MSG_SIMPLE_Q_TAG,
927 /* tea */ softc->sc_picker,
930 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
931 /* sense_len */ SSD_FULL_SIZE,
932 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
934 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
935 /*sense_flags*/ SF_RETRY_UA,
936 softc->device_stats);
938 xpt_release_ccb(ccb);
944 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
946 struct ch_softc *softc;
947 u_int16_t src, dst1, dst2;
952 softc = (struct ch_softc *)periph->softc;
956 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
957 (ce->ce_sdsttype > CHET_DT))
959 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
960 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
961 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
965 * Check the request against the changer's capabilities.
967 if (((softc->sc_exchangemask[ce->ce_srctype] &
968 (1 << ce->ce_fdsttype)) == 0) ||
969 ((softc->sc_exchangemask[ce->ce_fdsttype] &
970 (1 << ce->ce_sdsttype)) == 0))
974 * Calculate the source and destination elements.
976 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
977 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
978 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
980 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
982 scsi_exchange_medium(&ccb->csio,
985 /* tag_action */ MSG_SIMPLE_Q_TAG,
986 /* tea */ softc->sc_picker,
990 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
992 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
994 /* sense_len */ SSD_FULL_SIZE,
995 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
997 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
998 /*sense_flags*/ SF_RETRY_UA,
999 softc->device_stats);
1001 xpt_release_ccb(ccb);
1007 chposition(struct cam_periph *periph, struct changer_position *cp)
1009 struct ch_softc *softc;
1015 softc = (struct ch_softc *)periph->softc;
1020 if (cp->cp_type > CHET_DT)
1022 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1026 * Calculate the destination element.
1028 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1030 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1032 scsi_position_to_element(&ccb->csio,
1034 /* cbfcnp */ chdone,
1035 /* tag_action */ MSG_SIMPLE_Q_TAG,
1036 /* tea */ softc->sc_picker,
1038 /* invert */ (cp->cp_flags & CP_INVERT) ?
1040 /* sense_len */ SSD_FULL_SIZE,
1041 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1043 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1044 /*sense_flags*/ SF_RETRY_UA,
1045 softc->device_stats);
1047 xpt_release_ccb(ccb);
1053 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1054 * to host native byte order in the volume serial number. The volume
1055 * label as returned by the changer is transferred to user mode as
1056 * nul-terminated string. Volume labels are truncated at the first
1057 * space, as suggested by SCSI-2.
1060 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1063 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1064 char c = voltag->vif[i];
1066 uvoltag->cv_volid[i] = c;
1070 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1074 * Copy an element status descriptor to a user-mode
1075 * changer_element_status structure.
1078 copy_element_status(struct ch_softc *softc,
1080 struct read_element_status_descriptor *desc,
1081 struct changer_element_status *ces,
1084 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1086 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1087 struct read_element_status_device_id *devid = NULL;
1089 ces->ces_int_addr = eaddr;
1090 /* set up logical address in element status */
1091 for (et = CHET_MT; et <= CHET_DT; et++) {
1092 if ((softc->sc_firsts[et] <= eaddr)
1093 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1095 ces->ces_addr = eaddr - softc->sc_firsts[et];
1101 ces->ces_flags = desc->flags1;
1103 ces->ces_sensecode = desc->sense_code;
1104 ces->ces_sensequal = desc->sense_qual;
1106 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1107 ces->ces_flags |= CES_INVERT;
1109 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1111 eaddr = scsi_2btoul(desc->ssea);
1113 /* convert source address to logical format */
1114 for (et = CHET_MT; et <= CHET_DT; et++) {
1115 if ((softc->sc_firsts[et] <= eaddr)
1116 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1118 ces->ces_source_addr =
1119 eaddr - softc->sc_firsts[et];
1120 ces->ces_source_type = et;
1121 ces->ces_flags |= CES_SOURCE_VALID;
1126 if (!(ces->ces_flags & CES_SOURCE_VALID))
1127 printf("ch: warning: could not map element source "
1128 "address %ud to a valid element type\n",
1133 * pvoltag and avoltag are common between SCSI-2 and later versions
1135 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1136 pvol_tag = &desc->voltag_devid.pvoltag;
1137 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1138 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1139 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1141 * For SCSI-3 and later, element status can carry designator and
1142 * other information.
1144 if (scsi_version >= SCSI_REV_SPC) {
1145 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1146 (flags & READ_ELEMENT_STATUS_AVOLTAG))
1147 devid = &desc->voltag_devid.pvol_and_devid.devid;
1148 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1149 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1150 devid = &desc->voltag_devid.devid;
1151 else /* Have both PVOLTAG and AVOLTAG */
1152 devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1156 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1158 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1159 if (devid != NULL) {
1160 if (devid->designator_length > 0) {
1161 bcopy((void *)devid->designator,
1162 (void *)ces->ces_designator,
1163 devid->designator_length);
1164 ces->ces_designator_length = devid->designator_length;
1166 * Make sure we are always NUL terminated. The
1167 * This won't matter for the binary code set,
1168 * since the user will only pay attention to the
1171 ces->ces_designator[devid->designator_length]= '\0';
1173 if (devid->piv_assoc_designator_type &
1174 READ_ELEMENT_STATUS_PIV_SET) {
1175 ces->ces_flags |= CES_PIV;
1176 ces->ces_protocol_id =
1177 READ_ELEMENT_STATUS_PROTOCOL_ID(
1178 devid->prot_code_set);
1181 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1182 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1183 devid->piv_assoc_designator_type);
1184 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1185 devid->piv_assoc_designator_type);
1186 } else if (scsi_version > SCSI_REV_2) {
1187 /* SCSI-SPC and No devid, no designator */
1188 ces->ces_designator_length = 0;
1189 ces->ces_designator[0] = '\0';
1190 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1193 if (scsi_version <= SCSI_REV_2) {
1194 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1195 READ_ELEMENT_STATUS_DT_IDVALID) {
1196 ces->ces_flags |= CES_SCSIID_VALID;
1198 desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1201 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1202 READ_ELEMENT_STATUS_DT_LUVALID) {
1203 ces->ces_flags |= CES_LUN_VALID;
1205 desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1206 READ_ELEMENT_STATUS_DT_LUNMASK;
1212 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1213 struct changer_element_status_request *cesr)
1215 struct read_element_status_header *st_hdr;
1216 struct read_element_status_page_header *pg_hdr;
1217 struct read_element_status_descriptor *desc;
1218 caddr_t data = NULL;
1219 size_t size, desclen;
1220 int avail, i, error = 0;
1221 int curdata, dvcid, sense_flags;
1222 int try_no_dvcid = 0;
1223 struct changer_element_status *user_data = NULL;
1224 struct ch_softc *softc;
1226 int chet = cesr->cesr_element_type;
1227 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1229 softc = (struct ch_softc *)periph->softc;
1231 /* perform argument checking */
1234 * Perform a range check on the cesr_element_{base,count}
1235 * request argument fields.
1237 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1238 || (cesr->cesr_element_base + cesr->cesr_element_count)
1239 > softc->sc_counts[chet])
1243 * Request one descriptor for the given element type. This
1244 * is used to determine the size of the descriptor so that
1245 * we can allocate enough storage for all of them. We assume
1246 * that the first one can fit into 1k.
1248 cam_periph_unlock(periph);
1249 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1251 cam_periph_lock(periph);
1252 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1254 sense_flags = SF_RETRY_UA;
1255 if (softc->quirks & CH_Q_NO_DVCID) {
1262 * Don't print anything for an Illegal Request, because
1263 * these flags can cause some changers to complain. We'll
1264 * retry without them if we get an error.
1266 sense_flags |= SF_QUIET_IR;
1271 scsi_read_element_status(&ccb->csio,
1273 /* cbfcnp */ chdone,
1274 /* tag_action */ MSG_SIMPLE_Q_TAG,
1275 /* voltag */ want_voltags,
1276 /* sea */ softc->sc_firsts[chet],
1277 /* curdata */ curdata,
1280 /* data_ptr */ data,
1281 /* dxfer_len */ 1024,
1282 /* sense_len */ SSD_FULL_SIZE,
1283 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1285 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1286 /*sense_flags*/ sense_flags,
1287 softc->device_stats);
1290 * An Illegal Request sense key (only used if there is no asc/ascq)
1291 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or
1292 * curdata are set (we set both or neither), try turning them off
1293 * and see if the command is successful.
1295 if ((error == EINVAL)
1296 && (dvcid || curdata)) {
1300 /* At this point we want to report any Illegal Request */
1301 sense_flags &= ~SF_QUIET_IR;
1307 * In this case, we tried a read element status with dvcid and
1308 * curdata set, and it failed. We retried without those bits, and
1309 * it succeeded. Suggest to the user that he set a quirk, so we
1310 * don't go through the retry process the first time in the future.
1311 * This should only happen on changers that claim SCSI-3 or higher,
1312 * but don't support these bits.
1314 if ((try_no_dvcid != 0)
1316 softc->quirks |= CH_Q_NO_DVCID;
1320 cam_periph_unlock(periph);
1322 st_hdr = (struct read_element_status_header *)data;
1323 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1324 sizeof(struct read_element_status_header));
1325 desclen = scsi_2btoul(pg_hdr->edl);
1327 size = sizeof(struct read_element_status_header) +
1328 sizeof(struct read_element_status_page_header) +
1329 (desclen * cesr->cesr_element_count);
1331 * Reallocate storage for descriptors and get them from the
1334 free(data, M_DEVBUF);
1335 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1337 cam_periph_lock(periph);
1338 scsi_read_element_status(&ccb->csio,
1340 /* cbfcnp */ chdone,
1341 /* tag_action */ MSG_SIMPLE_Q_TAG,
1342 /* voltag */ want_voltags,
1343 /* sea */ softc->sc_firsts[chet]
1344 + cesr->cesr_element_base,
1345 /* curdata */ curdata,
1347 /* count */ cesr->cesr_element_count,
1348 /* data_ptr */ data,
1349 /* dxfer_len */ size,
1350 /* sense_len */ SSD_FULL_SIZE,
1351 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1353 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1354 /*sense_flags*/ SF_RETRY_UA,
1355 softc->device_stats);
1359 cam_periph_unlock(periph);
1362 * Fill in the user status array.
1364 st_hdr = (struct read_element_status_header *)data;
1365 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1366 sizeof(struct read_element_status_header));
1367 avail = scsi_2btoul(st_hdr->count);
1369 if (avail != cesr->cesr_element_count) {
1370 xpt_print(periph->path,
1371 "warning, READ ELEMENT STATUS avail != count\n");
1374 user_data = (struct changer_element_status *)
1375 malloc(avail * sizeof(struct changer_element_status),
1376 M_DEVBUF, M_WAITOK | M_ZERO);
1378 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1379 sizeof(struct read_element_status_header) +
1380 sizeof(struct read_element_status_page_header));
1382 * Set up the individual element status structures
1384 for (i = 0; i < avail; ++i) {
1385 struct changer_element_status *ces;
1388 * In the changer_element_status structure, fields from
1389 * the beginning to the field of ces_scsi_lun are common
1390 * between SCSI-2 and SCSI-3, while all the rest are new
1391 * from SCSI-3. In order to maintain backward compatibility
1392 * of the chio command, the ces pointer, below, is computed
1393 * such that it lines up with the structure boundary
1394 * corresponding to the SCSI version.
1396 ces = cmd == OCHIOGSTATUS ?
1397 (struct changer_element_status *)
1398 ((unsigned char *)user_data + i *
1399 (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1402 copy_element_status(softc, pg_hdr->flags, desc,
1405 desc = (struct read_element_status_descriptor *)
1406 ((unsigned char *)desc + desclen);
1409 /* Copy element status structures out to userspace. */
1410 if (cmd == OCHIOGSTATUS)
1411 error = copyout(user_data,
1412 cesr->cesr_element_status,
1413 avail* (offsetof(struct changer_element_status,
1414 ces_scsi_lun) + 1));
1416 error = copyout(user_data,
1417 cesr->cesr_element_status,
1418 avail * sizeof(struct changer_element_status));
1420 cam_periph_lock(periph);
1423 xpt_release_ccb(ccb);
1426 free(data, M_DEVBUF);
1427 if (user_data != NULL)
1428 free(user_data, M_DEVBUF);
1434 chielem(struct cam_periph *periph,
1435 unsigned int timeout)
1438 struct ch_softc *softc;
1442 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1448 softc = (struct ch_softc *)periph->softc;
1450 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1452 scsi_initialize_element_status(&ccb->csio,
1454 /* cbfcnp */ chdone,
1455 /* tag_action */ MSG_SIMPLE_Q_TAG,
1456 /* sense_len */ SSD_FULL_SIZE,
1457 /* timeout */ timeout);
1459 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1460 /*sense_flags*/ SF_RETRY_UA,
1461 softc->device_stats);
1463 xpt_release_ccb(ccb);
1469 chsetvoltag(struct cam_periph *periph,
1470 struct changer_set_voltag_request *csvr)
1473 struct ch_softc *softc;
1476 struct scsi_send_volume_tag_parameters ssvtp;
1481 softc = (struct ch_softc *)periph->softc;
1483 bzero(&ssvtp, sizeof(ssvtp));
1484 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1485 ssvtp.vitf[i] = ' ';
1491 if (csvr->csvr_type > CHET_DT)
1493 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1496 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1498 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1499 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1501 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1503 case CSVR_MODE_REPLACE:
1504 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1506 case CSVR_MODE_CLEAR:
1507 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1514 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1516 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1518 case CSVR_MODE_REPLACE:
1519 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1521 case CSVR_MODE_CLEAR:
1522 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1530 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1531 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1532 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1534 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1536 scsi_send_volume_tag(&ccb->csio,
1538 /* cbfcnp */ chdone,
1539 /* tag_action */ MSG_SIMPLE_Q_TAG,
1540 /* element_address */ ea,
1541 /* send_action_code */ sac,
1542 /* parameters */ &ssvtp,
1543 /* sense_len */ SSD_FULL_SIZE,
1544 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1546 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1547 /*sense_flags*/ SF_RETRY_UA,
1548 softc->device_stats);
1550 xpt_release_ccb(ccb);
1557 chgetparams(struct cam_periph *periph)
1560 struct ch_softc *softc;
1562 int mode_buffer_len;
1563 struct page_element_address_assignment *ea;
1564 struct page_device_capabilities *cap;
1565 int error, from, dbd;
1566 u_int8_t *moves, *exchanges;
1570 softc = (struct ch_softc *)periph->softc;
1572 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1575 * The scsi_mode_sense_data structure is just a convenience
1576 * structure that allows us to easily calculate the worst-case
1577 * storage size of the mode sense buffer.
1579 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1581 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1583 if (mode_buffer == NULL) {
1584 printf("chgetparams: couldn't malloc mode sense data\n");
1588 bzero(mode_buffer, mode_buffer_len);
1590 if (softc->quirks & CH_Q_NO_DBD)
1596 * Get the element address assignment page.
1598 scsi_mode_sense(&ccb->csio,
1600 /* cbfcnp */ chdone,
1601 /* tag_action */ MSG_SIMPLE_Q_TAG,
1603 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1604 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1605 /* param_buf */ (u_int8_t *)mode_buffer,
1606 /* param_len */ mode_buffer_len,
1607 /* sense_len */ SSD_FULL_SIZE,
1608 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1610 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1611 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1612 softc->device_stats);
1616 struct scsi_mode_sense_6 *sms;
1618 sms = (struct scsi_mode_sense_6 *)
1619 ccb->csio.cdb_io.cdb_bytes;
1621 sms->byte2 &= ~SMS_DBD;
1622 error = cam_periph_runccb(ccb, cherror,
1623 /*cam_flags*/ CAM_RETRY_SELTO,
1624 /*sense_flags*/ SF_RETRY_UA,
1625 softc->device_stats);
1628 * Since we disabled sense printing above, print
1629 * out the sense here since we got an error.
1631 scsi_sense_print(&ccb->csio);
1635 xpt_print(periph->path,
1636 "chgetparams: error getting element "
1638 xpt_release_ccb(ccb);
1639 free(mode_buffer, M_SCSICH);
1644 ea = (struct page_element_address_assignment *)
1645 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1647 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1648 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1649 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1650 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1651 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1652 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1653 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1654 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1656 bzero(mode_buffer, mode_buffer_len);
1659 * Now get the device capabilities page.
1661 scsi_mode_sense(&ccb->csio,
1663 /* cbfcnp */ chdone,
1664 /* tag_action */ MSG_SIMPLE_Q_TAG,
1666 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1667 /* page */ CH_DEVICE_CAP_PAGE,
1668 /* param_buf */ (u_int8_t *)mode_buffer,
1669 /* param_len */ mode_buffer_len,
1670 /* sense_len */ SSD_FULL_SIZE,
1671 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1673 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1674 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1675 softc->device_stats);
1679 struct scsi_mode_sense_6 *sms;
1681 sms = (struct scsi_mode_sense_6 *)
1682 ccb->csio.cdb_io.cdb_bytes;
1684 sms->byte2 &= ~SMS_DBD;
1685 error = cam_periph_runccb(ccb, cherror,
1686 /*cam_flags*/ CAM_RETRY_SELTO,
1687 /*sense_flags*/ SF_RETRY_UA,
1688 softc->device_stats);
1691 * Since we disabled sense printing above, print
1692 * out the sense here since we got an error.
1694 scsi_sense_print(&ccb->csio);
1698 xpt_print(periph->path,
1699 "chgetparams: error getting device "
1700 "capabilities page\n");
1701 xpt_release_ccb(ccb);
1702 free(mode_buffer, M_SCSICH);
1707 xpt_release_ccb(ccb);
1709 cap = (struct page_device_capabilities *)
1710 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1712 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1713 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1714 moves = cap->move_from;
1715 exchanges = cap->exchange_with;
1716 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1717 softc->sc_movemask[from] = moves[from];
1718 softc->sc_exchangemask[from] = exchanges[from];
1721 free(mode_buffer, M_SCSICH);
1727 chscsiversion(struct cam_periph *periph)
1729 struct scsi_inquiry_data *inq_data;
1730 struct ccb_getdev *cgd;
1731 int dev_scsi_version;
1732 struct cam_sim *sim;
1734 sim = xpt_path_sim(periph->path);
1735 mtx_assert(sim->mtx, MA_OWNED);
1736 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1739 * Get the device information.
1741 xpt_setup_ccb(&cgd->ccb_h,
1743 CAM_PRIORITY_NORMAL);
1744 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1745 xpt_action((union ccb *)cgd);
1747 if (cgd->ccb_h.status != CAM_REQ_CMP) {
1748 xpt_free_ccb((union ccb *)cgd);
1752 inq_data = &cgd->inq_data;
1753 dev_scsi_version = inq_data->version;
1754 xpt_free_ccb((union ccb *)cgd);
1756 return dev_scsi_version;
1760 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1761 void (*cbfcnp)(struct cam_periph *, union ccb *),
1762 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1763 u_int32_t dst, int invert, u_int8_t sense_len,
1766 struct scsi_move_medium *scsi_cmd;
1768 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1769 bzero(scsi_cmd, sizeof(*scsi_cmd));
1771 scsi_cmd->opcode = MOVE_MEDIUM;
1773 scsi_ulto2b(tea, scsi_cmd->tea);
1774 scsi_ulto2b(src, scsi_cmd->src);
1775 scsi_ulto2b(dst, scsi_cmd->dst);
1778 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1783 /*flags*/ CAM_DIR_NONE,
1793 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1794 void (*cbfcnp)(struct cam_periph *, union ccb *),
1795 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1796 u_int32_t dst1, u_int32_t dst2, int invert1,
1797 int invert2, u_int8_t sense_len, u_int32_t timeout)
1799 struct scsi_exchange_medium *scsi_cmd;
1801 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1802 bzero(scsi_cmd, sizeof(*scsi_cmd));
1804 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1806 scsi_ulto2b(tea, scsi_cmd->tea);
1807 scsi_ulto2b(src, scsi_cmd->src);
1808 scsi_ulto2b(dst1, scsi_cmd->fdst);
1809 scsi_ulto2b(dst2, scsi_cmd->sdst);
1812 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1815 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1820 /*flags*/ CAM_DIR_NONE,
1830 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1831 void (*cbfcnp)(struct cam_periph *, union ccb *),
1832 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1833 int invert, u_int8_t sense_len, u_int32_t timeout)
1835 struct scsi_position_to_element *scsi_cmd;
1837 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1838 bzero(scsi_cmd, sizeof(*scsi_cmd));
1840 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1842 scsi_ulto2b(tea, scsi_cmd->tea);
1843 scsi_ulto2b(dst, scsi_cmd->dst);
1846 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1851 /*flags*/ CAM_DIR_NONE,
1861 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1862 void (*cbfcnp)(struct cam_periph *, union ccb *),
1863 u_int8_t tag_action, int voltag, u_int32_t sea,
1864 int curdata, int dvcid,
1865 u_int32_t count, u_int8_t *data_ptr,
1866 u_int32_t dxfer_len, u_int8_t sense_len,
1869 struct scsi_read_element_status *scsi_cmd;
1871 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1872 bzero(scsi_cmd, sizeof(*scsi_cmd));
1874 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1876 scsi_ulto2b(sea, scsi_cmd->sea);
1877 scsi_ulto2b(count, scsi_cmd->count);
1878 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1880 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1882 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1885 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1890 /*flags*/ CAM_DIR_IN,
1900 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1901 void (*cbfcnp)(struct cam_periph *, union ccb *),
1902 u_int8_t tag_action, u_int8_t sense_len,
1905 struct scsi_initialize_element_status *scsi_cmd;
1907 scsi_cmd = (struct scsi_initialize_element_status *)
1908 &csio->cdb_io.cdb_bytes;
1909 bzero(scsi_cmd, sizeof(*scsi_cmd));
1911 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1916 /*flags*/ CAM_DIR_NONE,
1918 /* data_ptr */ NULL,
1926 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1927 void (*cbfcnp)(struct cam_periph *, union ccb *),
1928 u_int8_t tag_action,
1929 u_int16_t element_address,
1930 u_int8_t send_action_code,
1931 struct scsi_send_volume_tag_parameters *parameters,
1932 u_int8_t sense_len, u_int32_t timeout)
1934 struct scsi_send_volume_tag *scsi_cmd;
1936 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1937 bzero(scsi_cmd, sizeof(*scsi_cmd));
1939 scsi_cmd->opcode = SEND_VOLUME_TAG;
1940 scsi_ulto2b(element_address, scsi_cmd->ea);
1941 scsi_cmd->sac = send_action_code;
1942 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1947 /*flags*/ CAM_DIR_OUT,
1949 /* data_ptr */ (u_int8_t *) parameters,
1950 sizeof(*parameters),