dc5de891ad0b39b1e16c42ab6b806b3404264e97
[dragonfly.git] / sys / bus / cam / scsi / scsi_ch.c
blobdc5de891ad0b39b1e16c42ab6b806b3404264e97
1 /*
2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
27 * $FreeBSD: src/sys/cam/scsi/scsi_ch.c,v 1.20.2.2 2000/10/31 08:09:49 dwmalone Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_ch.c,v 1.27 2008/05/18 20:30:20 pavalos Exp $
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
46 * are met:
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
69 * SUCH DAMAGE.
72 #include <sys/param.h>
73 #include <sys/queue.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/types.h>
77 #include <sys/malloc.h>
78 #include <sys/fcntl.h>
79 #include <sys/conf.h>
80 #include <sys/buf.h>
81 #include <sys/chio.h>
82 #include <sys/errno.h>
83 #include <sys/devicestat.h>
84 #include <sys/thread2.h>
86 #include "../cam.h"
87 #include "../cam_ccb.h"
88 #include "../cam_extend.h"
89 #include "../cam_periph.h"
90 #include "../cam_xpt_periph.h"
91 #include "../cam_queue.h"
92 #include "../cam_debug.h"
94 #include "scsi_all.h"
95 #include "scsi_message.h"
96 #include "scsi_ch.h"
99 * Timeout definitions for various changer related commands. They may
100 * be too short for some devices (especially the timeout for INITIALIZE
101 * ELEMENT STATUS).
104 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
105 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 100000;
106 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 100000;
107 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 100000;
108 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 10000;
109 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
110 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
112 typedef enum {
113 CH_FLAG_INVALID = 0x001,
114 CH_FLAG_OPEN = 0x002
115 } ch_flags;
117 typedef enum {
118 CH_STATE_PROBE,
119 CH_STATE_NORMAL
120 } ch_state;
122 typedef enum {
123 CH_CCB_PROBE,
124 CH_CCB_WAITING
125 } ch_ccb_types;
127 typedef enum {
128 CH_Q_NONE = 0x00,
129 CH_Q_NO_DBD = 0x01
130 } ch_quirks;
132 #define ccb_state ppriv_field0
133 #define ccb_bio ppriv_ptr1
135 struct scsi_mode_sense_data {
136 struct scsi_mode_header_6 header;
137 struct scsi_mode_blk_desc blk_desc;
138 union {
139 struct page_element_address_assignment ea;
140 struct page_transport_geometry_parameters tg;
141 struct page_device_capabilities cap;
142 } pages;
145 struct ch_softc {
146 ch_flags flags;
147 ch_state state;
148 ch_quirks quirks;
149 union ccb saved_ccb;
150 struct devstat device_stats;
152 int sc_picker; /* current picker */
155 * The following information is obtained from the
156 * element address assignment page.
158 int sc_firsts[CHET_MAX + 1]; /* firsts */
159 int sc_counts[CHET_MAX + 1]; /* counts */
162 * The following mask defines the legal combinations
163 * of elements for the MOVE MEDIUM command.
165 u_int8_t sc_movemask[CHET_MAX + 1];
168 * As above, but for EXCHANGE MEDIUM.
170 u_int8_t sc_exchangemask[CHET_MAX + 1];
173 * Quirks; see below. XXX KDM not implemented yet
175 int sc_settledelay; /* delay for settle */
178 #define CHUNIT(x) (minor((x)))
179 #define CH_CDEV_MAJOR 17
181 static d_open_t chopen;
182 static d_close_t chclose;
183 static d_ioctl_t chioctl;
184 static periph_init_t chinit;
185 static periph_ctor_t chregister;
186 static periph_oninv_t choninvalidate;
187 static periph_dtor_t chcleanup;
188 static periph_start_t chstart;
189 static void chasync(void *callback_arg, u_int32_t code,
190 struct cam_path *path, void *arg);
191 static void chdone(struct cam_periph *periph,
192 union ccb *done_ccb);
193 static int cherror(union ccb *ccb, u_int32_t cam_flags,
194 u_int32_t sense_flags);
195 static int chmove(struct cam_periph *periph,
196 struct changer_move *cm);
197 static int chexchange(struct cam_periph *periph,
198 struct changer_exchange *ce);
199 static int chposition(struct cam_periph *periph,
200 struct changer_position *cp);
201 static int chgetelemstatus(struct cam_periph *periph,
202 struct changer_element_status_request *csr);
203 static int chsetvoltag(struct cam_periph *periph,
204 struct changer_set_voltag_request *csvr);
205 static int chielem(struct cam_periph *periph,
206 unsigned int timeout);
207 static int chgetparams(struct cam_periph *periph);
209 static struct periph_driver chdriver =
211 chinit, "ch",
212 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
215 PERIPHDRIVER_DECLARE(ch, chdriver);
217 static struct dev_ops ch_ops = {
218 { "ch", CH_CDEV_MAJOR, 0 },
219 .d_open = chopen,
220 .d_close = chclose,
221 .d_ioctl = chioctl
224 static struct extend_array *chperiphs;
226 MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
228 static void
229 chinit(void)
231 cam_status status;
234 * Create our extend array for storing the devices we attach to.
236 chperiphs = cam_extend_new();
237 if (chperiphs == NULL) {
238 kprintf("ch: Failed to alloc extend array!\n");
239 return;
243 * Install a global async callback. This callback will
244 * receive async callbacks like "new device found".
246 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
248 if (status != CAM_REQ_CMP) {
249 kprintf("ch: Failed to attach master async callback "
250 "due to status 0x%x!\n", status);
254 static void
255 choninvalidate(struct cam_periph *periph)
257 struct ch_softc *softc;
259 softc = (struct ch_softc *)periph->softc;
262 * De-register any async callbacks.
264 xpt_register_async(0, chasync, periph, periph->path);
266 softc->flags |= CH_FLAG_INVALID;
268 xpt_print(periph->path, "lost device\n");
272 static void
273 chcleanup(struct cam_periph *periph)
275 struct ch_softc *softc;
277 softc = (struct ch_softc *)periph->softc;
279 devstat_remove_entry(&softc->device_stats);
280 cam_extend_release(chperiphs, periph->unit_number);
281 xpt_print(periph->path, "removing device entry\n");
282 dev_ops_remove(&ch_ops, -1, periph->unit_number);
283 kfree(softc, M_DEVBUF);
286 static void
287 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
289 struct cam_periph *periph;
291 periph = (struct cam_periph *)callback_arg;
293 switch(code) {
294 case AC_FOUND_DEVICE:
296 struct ccb_getdev *cgd;
297 cam_status status;
299 cgd = (struct ccb_getdev *)arg;
300 if (cgd == NULL)
301 break;
303 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
304 break;
307 * Allocate a peripheral instance for
308 * this device and start the probe
309 * process.
311 status = cam_periph_alloc(chregister, choninvalidate,
312 chcleanup, chstart, "ch",
313 CAM_PERIPH_BIO, cgd->ccb_h.path,
314 chasync, AC_FOUND_DEVICE, cgd);
316 if (status != CAM_REQ_CMP
317 && status != CAM_REQ_INPROG)
318 kprintf("chasync: Unable to probe new device "
319 "due to status 0x%x\n", status);
321 break;
324 default:
325 cam_periph_async(periph, code, path, arg);
326 break;
330 static cam_status
331 chregister(struct cam_periph *periph, void *arg)
333 struct ch_softc *softc;
334 struct ccb_getdev *cgd;
336 cgd = (struct ccb_getdev *)arg;
337 if (periph == NULL) {
338 kprintf("chregister: periph was NULL!!\n");
339 return(CAM_REQ_CMP_ERR);
342 if (cgd == NULL) {
343 kprintf("chregister: no getdev CCB, can't register device\n");
344 return(CAM_REQ_CMP_ERR);
347 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
348 softc->state = CH_STATE_PROBE;
349 periph->softc = softc;
350 cam_extend_set(chperiphs, periph->unit_number, periph);
351 softc->quirks = CH_Q_NONE;
354 * Changers don't have a blocksize, and obviously don't support
355 * tagged queueing.
357 cam_periph_unlock(periph);
358 devstat_add_entry(&softc->device_stats, "ch",
359 periph->unit_number, 0,
360 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
361 SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
362 DEVSTAT_PRIORITY_OTHER);
364 /* Register the device */
365 dev_ops_add(&ch_ops, -1, periph->unit_number);
366 make_dev(&ch_ops, periph->unit_number, UID_ROOT,
367 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
368 periph->unit_number);
369 cam_periph_lock(periph);
372 * Add an async callback so that we get
373 * notified if this device goes away.
375 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
378 * Lock this periph until we are setup.
379 * This first call can't block
381 cam_periph_hold(periph, 0);
382 xpt_schedule(periph, /*priority*/5);
384 return(CAM_REQ_CMP);
387 static int
388 chopen(struct dev_open_args *ap)
390 cdev_t dev = ap->a_head.a_dev;
391 struct cam_periph *periph;
392 struct ch_softc *softc;
393 int unit, error;
395 unit = CHUNIT(dev);
396 periph = cam_extend_get(chperiphs, unit);
398 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
399 return (ENXIO);
401 softc = (struct ch_softc *)periph->softc;
403 cam_periph_lock(periph);
405 if (softc->flags & CH_FLAG_INVALID) {
406 cam_periph_unlock(periph);
407 cam_periph_release(periph);
408 return(ENXIO);
411 if ((softc->flags & CH_FLAG_OPEN) == 0)
412 softc->flags |= CH_FLAG_OPEN;
413 else
414 cam_periph_release(periph);
416 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
417 cam_periph_unlock(periph);
418 cam_periph_release(periph);
419 return (error);
423 * Load information about this changer device into the softc.
425 if ((error = chgetparams(periph)) != 0) {
426 softc->flags &= ~CH_FLAG_OPEN;
427 cam_periph_unlock(periph);
428 cam_periph_release(periph);
429 return(error);
432 cam_periph_unhold(periph);
433 cam_periph_unlock(periph);
435 return(error);
438 static int
439 chclose(struct dev_close_args *ap)
441 cdev_t dev = ap->a_head.a_dev;
442 struct cam_periph *periph;
443 struct ch_softc *softc;
444 int unit, error;
446 error = 0;
448 unit = CHUNIT(dev);
449 periph = cam_extend_get(chperiphs, unit);
450 if (periph == NULL)
451 return(ENXIO);
453 softc = (struct ch_softc *)periph->softc;
455 cam_periph_lock(periph);
457 softc->flags &= ~CH_FLAG_OPEN;
459 cam_periph_unlock(periph);
460 cam_periph_release(periph);
462 return(0);
465 static void
466 chstart(struct cam_periph *periph, union ccb *start_ccb)
468 struct ch_softc *softc;
470 softc = (struct ch_softc *)periph->softc;
472 switch (softc->state) {
473 case CH_STATE_NORMAL:
475 if (periph->immediate_priority <= periph->pinfo.priority){
476 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
478 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
479 periph_links.sle);
480 periph->immediate_priority = CAM_PRIORITY_NONE;
481 wakeup(&periph->ccb_list);
483 break;
485 case CH_STATE_PROBE:
487 int mode_buffer_len;
488 void *mode_buffer;
491 * Include the block descriptor when calculating the mode
492 * buffer length,
494 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
495 sizeof(struct scsi_mode_blk_desc) +
496 sizeof(struct page_element_address_assignment);
498 mode_buffer = kmalloc(mode_buffer_len, M_SCSICH, M_INTWAIT | M_ZERO);
500 * Get the element address assignment page.
502 scsi_mode_sense(&start_ccb->csio,
503 /* retries */ 1,
504 /* cbfcnp */ chdone,
505 /* tag_action */ MSG_SIMPLE_Q_TAG,
506 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
507 FALSE : TRUE,
508 /* page_code */ SMS_PAGE_CTRL_CURRENT,
509 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
510 /* param_buf */ (u_int8_t *)mode_buffer,
511 /* param_len */ mode_buffer_len,
512 /* sense_len */ SSD_FULL_SIZE,
513 /* timeout */ CH_TIMEOUT_MODE_SENSE);
515 start_ccb->ccb_h.ccb_bio = NULL;
516 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
517 xpt_action(start_ccb);
518 break;
523 static void
524 chdone(struct cam_periph *periph, union ccb *done_ccb)
526 struct ch_softc *softc;
527 struct ccb_scsiio *csio;
529 softc = (struct ch_softc *)periph->softc;
530 csio = &done_ccb->csio;
532 switch(done_ccb->ccb_h.ccb_state) {
533 case CH_CCB_PROBE:
535 struct scsi_mode_header_6 *mode_header;
536 struct page_element_address_assignment *ea;
537 char announce_buf[80];
540 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
542 ea = (struct page_element_address_assignment *)
543 find_mode_page_6(mode_header);
545 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
547 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
548 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
549 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
550 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
551 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
552 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
553 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
554 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
555 softc->sc_picker = softc->sc_firsts[CHET_MT];
557 #define PLURAL(c) (c) == 1 ? "" : "s"
558 ksnprintf(announce_buf, sizeof(announce_buf),
559 "%d slot%s, %d drive%s, "
560 "%d picker%s, %d portal%s",
561 softc->sc_counts[CHET_ST],
562 PLURAL(softc->sc_counts[CHET_ST]),
563 softc->sc_counts[CHET_DT],
564 PLURAL(softc->sc_counts[CHET_DT]),
565 softc->sc_counts[CHET_MT],
566 PLURAL(softc->sc_counts[CHET_MT]),
567 softc->sc_counts[CHET_IE],
568 PLURAL(softc->sc_counts[CHET_IE]));
569 #undef PLURAL
570 } else {
571 int error;
573 error = cherror(done_ccb, CAM_RETRY_SELTO,
574 SF_RETRY_UA | SF_NO_PRINT);
576 * Retry any UNIT ATTENTION type errors. They
577 * are expected at boot.
579 if (error == ERESTART) {
581 * A retry was scheuled, so
582 * just return.
584 return;
585 } else if (error != 0) {
586 int retry_scheduled;
587 struct scsi_mode_sense_6 *sms;
589 sms = (struct scsi_mode_sense_6 *)
590 done_ccb->csio.cdb_io.cdb_bytes;
593 * Check to see if block descriptors were
594 * disabled. Some devices don't like that.
595 * We're taking advantage of the fact that
596 * the first few bytes of the 6 and 10 byte
597 * mode sense commands are the same. If
598 * block descriptors were disabled, enable
599 * them and re-send the command.
601 if (sms->byte2 & SMS_DBD) {
602 sms->byte2 &= ~SMS_DBD;
603 xpt_action(done_ccb);
604 softc->quirks |= CH_Q_NO_DBD;
605 retry_scheduled = 1;
606 } else
607 retry_scheduled = 0;
609 /* Don't wedge this device's queue */
610 cam_release_devq(done_ccb->ccb_h.path,
611 /*relsim_flags*/0,
612 /*reduction*/0,
613 /*timeout*/0,
614 /*getcount_only*/0);
616 if (retry_scheduled)
617 return;
619 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
620 == CAM_SCSI_STATUS_ERROR)
621 scsi_sense_print(&done_ccb->csio);
622 else {
623 xpt_print(periph->path,
624 "got CAM status %#x\n",
625 done_ccb->ccb_h.status);
627 xpt_print(periph->path, "fatal error, failed "
628 "to attach to device\n");
630 cam_periph_invalidate(periph);
632 announce_buf[0] = '\0';
635 if (announce_buf[0] != '\0')
636 xpt_announce_periph(periph, announce_buf);
637 softc->state = CH_STATE_NORMAL;
638 kfree(mode_header, M_SCSICH);
640 * Since our peripheral may be invalidated by an error
641 * above or an external event, we must release our CCB
642 * before releasing the probe lock on the peripheral.
643 * The peripheral will only go away once the last lock
644 * is removed, and we need it around for the CCB release
645 * operation.
647 xpt_release_ccb(done_ccb);
648 cam_periph_unhold(periph);
649 return;
651 case CH_CCB_WAITING:
653 /* Caller will release the CCB */
654 wakeup(&done_ccb->ccb_h.cbfcnp);
655 return;
657 default:
658 break;
660 xpt_release_ccb(done_ccb);
663 static int
664 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
666 struct ch_softc *softc;
667 struct cam_periph *periph;
669 periph = xpt_path_periph(ccb->ccb_h.path);
670 softc = (struct ch_softc *)periph->softc;
672 return (cam_periph_error(ccb, cam_flags, sense_flags,
673 &softc->saved_ccb));
676 static int
677 chioctl(struct dev_ioctl_args *ap)
679 cdev_t dev = ap->a_head.a_dev;
680 caddr_t addr = ap->a_data;
681 int flag = ap->a_fflag;
682 struct cam_periph *periph;
683 struct ch_softc *softc;
684 u_int8_t unit;
685 int error;
687 unit = CHUNIT(dev);
689 periph = cam_extend_get(chperiphs, unit);
690 if (periph == NULL)
691 return(ENXIO);
693 cam_periph_lock(periph);
694 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
696 softc = (struct ch_softc *)periph->softc;
698 error = 0;
700 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
701 ("trying to do ioctl %#lx\n", ap->a_cmd));
704 * If this command can change the device's state, we must
705 * have the device open for writing.
707 switch (ap->a_cmd) {
708 case CHIOGPICKER:
709 case CHIOGPARAMS:
710 case CHIOGSTATUS:
711 break;
713 default:
714 if ((flag & FWRITE) == 0) {
715 cam_periph_unlock(periph);
716 return (EBADF);
720 switch (ap->a_cmd) {
721 case CHIOMOVE:
722 error = chmove(periph, (struct changer_move *)addr);
723 break;
725 case CHIOEXCHANGE:
726 error = chexchange(periph, (struct changer_exchange *)addr);
727 break;
729 case CHIOPOSITION:
730 error = chposition(periph, (struct changer_position *)addr);
731 break;
733 case CHIOGPICKER:
734 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
735 break;
737 case CHIOSPICKER:
739 int new_picker = *(int *)addr;
741 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
742 error = EINVAL;
743 break;
745 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
746 break;
748 case CHIOGPARAMS:
750 struct changer_params *cp = (struct changer_params *)addr;
752 cp->cp_npickers = softc->sc_counts[CHET_MT];
753 cp->cp_nslots = softc->sc_counts[CHET_ST];
754 cp->cp_nportals = softc->sc_counts[CHET_IE];
755 cp->cp_ndrives = softc->sc_counts[CHET_DT];
756 break;
758 case CHIOIELEM:
759 error = chielem(periph, *(unsigned int *)addr);
760 break;
762 case CHIOGSTATUS:
764 error = chgetelemstatus(periph,
765 (struct changer_element_status_request *) addr);
766 break;
769 case CHIOSETVOLTAG:
771 error = chsetvoltag(periph,
772 (struct changer_set_voltag_request *) addr);
773 break;
776 /* Implement prevent/allow? */
778 default:
779 error = cam_periph_ioctl(periph, ap->a_cmd, addr, cherror);
780 break;
783 cam_periph_unlock(periph);
784 return (error);
787 static int
788 chmove(struct cam_periph *periph, struct changer_move *cm)
790 struct ch_softc *softc;
791 u_int16_t fromelem, toelem;
792 union ccb *ccb;
793 int error;
795 error = 0;
796 softc = (struct ch_softc *)periph->softc;
799 * Check arguments.
801 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
802 return (EINVAL);
803 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
804 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
805 return (ENODEV);
808 * Check the request against the changer's capabilities.
810 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
811 return (ENODEV);
814 * Calculate the source and destination elements.
816 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
817 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
819 ccb = cam_periph_getccb(periph, /*priority*/ 1);
821 scsi_move_medium(&ccb->csio,
822 /* retries */ 1,
823 /* cbfcnp */ chdone,
824 /* tag_action */ MSG_SIMPLE_Q_TAG,
825 /* tea */ softc->sc_picker,
826 /* src */ fromelem,
827 /* dst */ toelem,
828 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
829 /* sense_len */ SSD_FULL_SIZE,
830 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
832 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
833 /*sense_flags*/ SF_RETRY_UA,
834 &softc->device_stats);
836 xpt_release_ccb(ccb);
838 return(error);
841 static int
842 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
844 struct ch_softc *softc;
845 u_int16_t src, dst1, dst2;
846 union ccb *ccb;
847 int error;
849 error = 0;
850 softc = (struct ch_softc *)periph->softc;
852 * Check arguments.
854 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
855 (ce->ce_sdsttype > CHET_DT))
856 return (EINVAL);
857 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
858 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
859 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
860 return (ENODEV);
863 * Check the request against the changer's capabilities.
865 if (((softc->sc_exchangemask[ce->ce_srctype] &
866 (1 << ce->ce_fdsttype)) == 0) ||
867 ((softc->sc_exchangemask[ce->ce_fdsttype] &
868 (1 << ce->ce_sdsttype)) == 0))
869 return (ENODEV);
872 * Calculate the source and destination elements.
874 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
875 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
876 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
878 ccb = cam_periph_getccb(periph, /*priority*/ 1);
880 scsi_exchange_medium(&ccb->csio,
881 /* retries */ 1,
882 /* cbfcnp */ chdone,
883 /* tag_action */ MSG_SIMPLE_Q_TAG,
884 /* tea */ softc->sc_picker,
885 /* src */ src,
886 /* dst1 */ dst1,
887 /* dst2 */ dst2,
888 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
889 TRUE : FALSE,
890 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
891 TRUE : FALSE,
892 /* sense_len */ SSD_FULL_SIZE,
893 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
895 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
896 /*sense_flags*/ SF_RETRY_UA,
897 &softc->device_stats);
899 xpt_release_ccb(ccb);
901 return(error);
904 static int
905 chposition(struct cam_periph *periph, struct changer_position *cp)
907 struct ch_softc *softc;
908 u_int16_t dst;
909 union ccb *ccb;
910 int error;
912 error = 0;
913 softc = (struct ch_softc *)periph->softc;
916 * Check arguments.
918 if (cp->cp_type > CHET_DT)
919 return (EINVAL);
920 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
921 return (ENODEV);
924 * Calculate the destination element.
926 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
928 ccb = cam_periph_getccb(periph, /*priority*/ 1);
930 scsi_position_to_element(&ccb->csio,
931 /* retries */ 1,
932 /* cbfcnp */ chdone,
933 /* tag_action */ MSG_SIMPLE_Q_TAG,
934 /* tea */ softc->sc_picker,
935 /* dst */ dst,
936 /* invert */ (cp->cp_flags & CP_INVERT) ?
937 TRUE : FALSE,
938 /* sense_len */ SSD_FULL_SIZE,
939 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
941 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
942 /*sense_flags*/ SF_RETRY_UA,
943 &softc->device_stats);
945 xpt_release_ccb(ccb);
947 return(error);
951 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
952 * to host native byte order in the volume serial number. The volume
953 * label as returned by the changer is transferred to user mode as
954 * nul-terminated string. Volume labels are truncated at the first
955 * space, as suggested by SCSI-2.
957 static void
958 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
960 int i;
961 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
962 char c = voltag->vif[i];
963 if (c && c != ' ')
964 uvoltag->cv_volid[i] = c;
965 else
966 break;
968 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
972 * Copy an an element status descriptor to a user-mode
973 * changer_element_status structure.
976 static void
977 copy_element_status(struct ch_softc *softc,
978 u_int16_t flags,
979 struct read_element_status_descriptor *desc,
980 struct changer_element_status *ces)
982 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
983 u_int16_t et;
985 ces->ces_int_addr = eaddr;
986 /* set up logical address in element status */
987 for (et = CHET_MT; et <= CHET_DT; et++) {
988 if ((softc->sc_firsts[et] <= eaddr)
989 && ((softc->sc_firsts[et] + softc->sc_counts[et])
990 > eaddr)) {
991 ces->ces_addr = eaddr - softc->sc_firsts[et];
992 ces->ces_type = et;
993 break;
997 ces->ces_flags = desc->flags1;
999 ces->ces_sensecode = desc->sense_code;
1000 ces->ces_sensequal = desc->sense_qual;
1002 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1003 ces->ces_flags |= CES_INVERT;
1005 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1007 eaddr = scsi_2btoul(desc->ssea);
1009 /* convert source address to logical format */
1010 for (et = CHET_MT; et <= CHET_DT; et++) {
1011 if ((softc->sc_firsts[et] <= eaddr)
1012 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1013 > eaddr)) {
1014 ces->ces_source_addr =
1015 eaddr - softc->sc_firsts[et];
1016 ces->ces_source_type = et;
1017 ces->ces_flags |= CES_SOURCE_VALID;
1018 break;
1022 if (!(ces->ces_flags & CES_SOURCE_VALID))
1023 kprintf("ch: warning: could not map element source "
1024 "address %ud to a valid element type\n",
1025 eaddr);
1029 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1030 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
1031 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1032 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
1034 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
1035 ces->ces_flags |= CES_SCSIID_VALID;
1036 ces->ces_scsi_id = desc->dt_scsi_addr;
1039 if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
1040 ces->ces_flags |= CES_LUN_VALID;
1041 ces->ces_scsi_lun =
1042 desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
1046 static int
1047 chgetelemstatus(struct cam_periph *periph,
1048 struct changer_element_status_request *cesr)
1050 struct read_element_status_header *st_hdr;
1051 struct read_element_status_page_header *pg_hdr;
1052 struct read_element_status_descriptor *desc;
1053 caddr_t data = NULL;
1054 size_t size, desclen;
1055 int avail, i, error = 0;
1056 struct changer_element_status *user_data = NULL;
1057 struct ch_softc *softc;
1058 union ccb *ccb;
1059 int chet = cesr->cesr_element_type;
1060 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1062 softc = (struct ch_softc *)periph->softc;
1064 /* perform argument checking */
1067 * Perform a range check on the cesr_element_{base,count}
1068 * request argument fields.
1070 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1071 || (cesr->cesr_element_base + cesr->cesr_element_count)
1072 > softc->sc_counts[chet])
1073 return (EINVAL);
1076 * Request one descriptor for the given element type. This
1077 * is used to determine the size of the descriptor so that
1078 * we can allocate enough storage for all of them. We assume
1079 * that the first one can fit into 1k.
1081 cam_periph_unlock(periph);
1082 data = (caddr_t)kmalloc(1024, M_DEVBUF, M_INTWAIT);
1084 cam_periph_lock(periph);
1085 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1087 scsi_read_element_status(&ccb->csio,
1088 /* retries */ 1,
1089 /* cbfcnp */ chdone,
1090 /* tag_action */ MSG_SIMPLE_Q_TAG,
1091 /* voltag */ want_voltags,
1092 /* sea */ softc->sc_firsts[chet],
1093 /* count */ 1,
1094 /* data_ptr */ data,
1095 /* dxfer_len */ 1024,
1096 /* sense_len */ SSD_FULL_SIZE,
1097 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1099 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1100 /*sense_flags*/ SF_RETRY_UA,
1101 &softc->device_stats);
1103 if (error)
1104 goto done;
1105 cam_periph_unlock(periph);
1107 st_hdr = (struct read_element_status_header *)data;
1108 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1109 sizeof(struct read_element_status_header));
1110 desclen = scsi_2btoul(pg_hdr->edl);
1112 size = sizeof(struct read_element_status_header) +
1113 sizeof(struct read_element_status_page_header) +
1114 (desclen * cesr->cesr_element_count);
1117 * Reallocate storage for descriptors and get them from the
1118 * device.
1120 kfree(data, M_DEVBUF);
1121 data = (caddr_t)kmalloc(size, M_DEVBUF, M_INTWAIT);
1123 cam_periph_lock(periph);
1124 scsi_read_element_status(&ccb->csio,
1125 /* retries */ 1,
1126 /* cbfcnp */ chdone,
1127 /* tag_action */ MSG_SIMPLE_Q_TAG,
1128 /* voltag */ want_voltags,
1129 /* sea */ softc->sc_firsts[chet]
1130 + cesr->cesr_element_base,
1131 /* count */ cesr->cesr_element_count,
1132 /* data_ptr */ data,
1133 /* dxfer_len */ size,
1134 /* sense_len */ SSD_FULL_SIZE,
1135 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1137 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1138 /*sense_flags*/ SF_RETRY_UA,
1139 &softc->device_stats);
1141 if (error)
1142 goto done;
1143 cam_periph_unlock(periph);
1146 * Fill in the user status array.
1148 st_hdr = (struct read_element_status_header *)data;
1149 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1150 sizeof(struct read_element_status_header));
1151 avail = scsi_2btoul(st_hdr->count);
1153 if (avail != cesr->cesr_element_count) {
1154 xpt_print(periph->path,
1155 "warning, READ ELEMENT STATUS avail != count\n");
1158 user_data = (struct changer_element_status *)
1159 kmalloc(avail * sizeof(struct changer_element_status),
1160 M_DEVBUF, M_INTWAIT | M_ZERO);
1162 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1163 sizeof(struct read_element_status_header) +
1164 sizeof(struct read_element_status_page_header));
1166 * Set up the individual element status structures
1168 for (i = 0; i < avail; ++i) {
1169 struct changer_element_status *ces = &(user_data[i]);
1171 copy_element_status(softc, pg_hdr->flags, desc, ces);
1173 desc = (struct read_element_status_descriptor *)
1174 ((uintptr_t)desc + desclen);
1177 /* Copy element status structures out to userspace. */
1178 error = copyout(user_data,
1179 cesr->cesr_element_status,
1180 avail * sizeof(struct changer_element_status));
1181 cam_periph_lock(periph);
1183 done:
1184 xpt_release_ccb(ccb);
1186 if (data != NULL)
1187 kfree(data, M_DEVBUF);
1188 if (user_data != NULL)
1189 kfree(user_data, M_DEVBUF);
1191 return (error);
1194 static int
1195 chielem(struct cam_periph *periph,
1196 unsigned int timeout)
1198 union ccb *ccb;
1199 struct ch_softc *softc;
1200 int error;
1202 if (!timeout) {
1203 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1204 } else {
1205 timeout *= 1000;
1208 error = 0;
1209 softc = (struct ch_softc *)periph->softc;
1211 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1213 scsi_initialize_element_status(&ccb->csio,
1214 /* retries */ 1,
1215 /* cbfcnp */ chdone,
1216 /* tag_action */ MSG_SIMPLE_Q_TAG,
1217 /* sense_len */ SSD_FULL_SIZE,
1218 /* timeout */ timeout);
1220 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1221 /*sense_flags*/ SF_RETRY_UA,
1222 &softc->device_stats);
1224 xpt_release_ccb(ccb);
1226 return(error);
1229 static int
1230 chsetvoltag(struct cam_periph *periph,
1231 struct changer_set_voltag_request *csvr)
1233 union ccb *ccb;
1234 struct ch_softc *softc;
1235 u_int16_t ea;
1236 u_int8_t sac;
1237 struct scsi_send_volume_tag_parameters ssvtp;
1238 int error;
1239 int i;
1241 error = 0;
1242 softc = (struct ch_softc *)periph->softc;
1244 bzero(&ssvtp, sizeof(ssvtp));
1245 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1246 ssvtp.vitf[i] = ' ';
1250 * Check arguments.
1252 if (csvr->csvr_type > CHET_DT)
1253 return EINVAL;
1254 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1255 return ENODEV;
1257 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1259 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1260 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1261 case CSVR_MODE_SET:
1262 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1263 break;
1264 case CSVR_MODE_REPLACE:
1265 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1266 break;
1267 case CSVR_MODE_CLEAR:
1268 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1269 break;
1270 default:
1271 error = EINVAL;
1272 goto out;
1274 } else {
1275 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1276 case CSVR_MODE_SET:
1277 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1278 break;
1279 case CSVR_MODE_REPLACE:
1280 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1281 break;
1282 case CSVR_MODE_CLEAR:
1283 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1284 break;
1285 default:
1286 error = EINVAL;
1287 goto out;
1291 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1292 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1293 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1295 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1297 scsi_send_volume_tag(&ccb->csio,
1298 /* retries */ 1,
1299 /* cbfcnp */ chdone,
1300 /* tag_action */ MSG_SIMPLE_Q_TAG,
1301 /* element_address */ ea,
1302 /* send_action_code */ sac,
1303 /* parameters */ &ssvtp,
1304 /* sense_len */ SSD_FULL_SIZE,
1305 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1307 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1308 /*sense_flags*/ SF_RETRY_UA,
1309 &softc->device_stats);
1311 xpt_release_ccb(ccb);
1313 out:
1314 return error;
1317 static int
1318 chgetparams(struct cam_periph *periph)
1320 union ccb *ccb;
1321 struct ch_softc *softc;
1322 void *mode_buffer;
1323 int mode_buffer_len;
1324 struct page_element_address_assignment *ea;
1325 struct page_device_capabilities *cap;
1326 int error, from, dbd;
1327 u_int8_t *moves, *exchanges;
1329 error = 0;
1331 softc = (struct ch_softc *)periph->softc;
1333 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1336 * The scsi_mode_sense_data structure is just a convenience
1337 * structure that allows us to easily calculate the worst-case
1338 * storage size of the mode sense buffer.
1340 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1342 mode_buffer = kmalloc(mode_buffer_len, M_SCSICH, M_INTWAIT | M_ZERO);
1344 if (softc->quirks & CH_Q_NO_DBD)
1345 dbd = FALSE;
1346 else
1347 dbd = TRUE;
1350 * Get the element address assignment page.
1352 scsi_mode_sense(&ccb->csio,
1353 /* retries */ 1,
1354 /* cbfcnp */ chdone,
1355 /* tag_action */ MSG_SIMPLE_Q_TAG,
1356 /* dbd */ dbd,
1357 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1358 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1359 /* param_buf */ (u_int8_t *)mode_buffer,
1360 /* param_len */ mode_buffer_len,
1361 /* sense_len */ SSD_FULL_SIZE,
1362 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1364 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1365 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1366 &softc->device_stats);
1368 if (error) {
1369 if (dbd) {
1370 struct scsi_mode_sense_6 *sms;
1372 sms = (struct scsi_mode_sense_6 *)
1373 ccb->csio.cdb_io.cdb_bytes;
1375 sms->byte2 &= ~SMS_DBD;
1376 error = cam_periph_runccb(ccb, cherror,
1377 /*cam_flags*/ CAM_RETRY_SELTO,
1378 /*sense_flags*/ SF_RETRY_UA,
1379 &softc->device_stats);
1380 } else {
1382 * Since we disabled sense printing above, print
1383 * out the sense here since we got an error.
1385 scsi_sense_print(&ccb->csio);
1388 if (error) {
1389 xpt_print(periph->path,
1390 "chgetparams: error getting element "
1391 "address page\n");
1392 xpt_release_ccb(ccb);
1393 kfree(mode_buffer, M_SCSICH);
1394 return(error);
1398 ea = (struct page_element_address_assignment *)
1399 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1401 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1402 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1403 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1404 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1405 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1406 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1407 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1408 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1410 bzero(mode_buffer, mode_buffer_len);
1413 * Now get the device capabilities page.
1415 scsi_mode_sense(&ccb->csio,
1416 /* retries */ 1,
1417 /* cbfcnp */ chdone,
1418 /* tag_action */ MSG_SIMPLE_Q_TAG,
1419 /* dbd */ dbd,
1420 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1421 /* page */ CH_DEVICE_CAP_PAGE,
1422 /* param_buf */ (u_int8_t *)mode_buffer,
1423 /* param_len */ mode_buffer_len,
1424 /* sense_len */ SSD_FULL_SIZE,
1425 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1427 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1428 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1429 &softc->device_stats);
1431 if (error) {
1432 if (dbd) {
1433 struct scsi_mode_sense_6 *sms;
1435 sms = (struct scsi_mode_sense_6 *)
1436 ccb->csio.cdb_io.cdb_bytes;
1438 sms->byte2 &= ~SMS_DBD;
1439 error = cam_periph_runccb(ccb, cherror,
1440 /*cam_flags*/ CAM_RETRY_SELTO,
1441 /*sense_flags*/ SF_RETRY_UA,
1442 &softc->device_stats);
1443 } else {
1445 * Since we disabled sense printing above, print
1446 * out the sense here since we got an error.
1448 scsi_sense_print(&ccb->csio);
1451 if (error) {
1452 xpt_print(periph->path,
1453 "chgetparams: error getting device "
1454 "capabilities page\n");
1455 xpt_release_ccb(ccb);
1456 kfree(mode_buffer, M_SCSICH);
1457 return(error);
1461 xpt_release_ccb(ccb);
1463 cap = (struct page_device_capabilities *)
1464 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1466 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1467 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1468 moves = cap->move_from;
1469 exchanges = cap->exchange_with;
1470 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1471 softc->sc_movemask[from] = moves[from];
1472 softc->sc_exchangemask[from] = exchanges[from];
1475 kfree(mode_buffer, M_SCSICH);
1477 return(error);
1480 void
1481 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1482 void (*cbfcnp)(struct cam_periph *, union ccb *),
1483 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1484 u_int32_t dst, int invert, u_int8_t sense_len,
1485 u_int32_t timeout)
1487 struct scsi_move_medium *scsi_cmd;
1489 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1490 bzero(scsi_cmd, sizeof(*scsi_cmd));
1492 scsi_cmd->opcode = MOVE_MEDIUM;
1494 scsi_ulto2b(tea, scsi_cmd->tea);
1495 scsi_ulto2b(src, scsi_cmd->src);
1496 scsi_ulto2b(dst, scsi_cmd->dst);
1498 if (invert)
1499 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1501 cam_fill_csio(csio,
1502 retries,
1503 cbfcnp,
1504 /*flags*/ CAM_DIR_NONE,
1505 tag_action,
1506 /*data_ptr*/ NULL,
1507 /*dxfer_len*/ 0,
1508 sense_len,
1509 sizeof(*scsi_cmd),
1510 timeout);
1513 void
1514 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1515 void (*cbfcnp)(struct cam_periph *, union ccb *),
1516 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1517 u_int32_t dst1, u_int32_t dst2, int invert1,
1518 int invert2, u_int8_t sense_len, u_int32_t timeout)
1520 struct scsi_exchange_medium *scsi_cmd;
1522 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1523 bzero(scsi_cmd, sizeof(*scsi_cmd));
1525 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1527 scsi_ulto2b(tea, scsi_cmd->tea);
1528 scsi_ulto2b(src, scsi_cmd->src);
1529 scsi_ulto2b(dst1, scsi_cmd->fdst);
1530 scsi_ulto2b(dst2, scsi_cmd->sdst);
1532 if (invert1)
1533 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1535 if (invert2)
1536 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1538 cam_fill_csio(csio,
1539 retries,
1540 cbfcnp,
1541 /*flags*/ CAM_DIR_NONE,
1542 tag_action,
1543 /*data_ptr*/ NULL,
1544 /*dxfer_len*/ 0,
1545 sense_len,
1546 sizeof(*scsi_cmd),
1547 timeout);
1550 void
1551 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1552 void (*cbfcnp)(struct cam_periph *, union ccb *),
1553 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1554 int invert, u_int8_t sense_len, u_int32_t timeout)
1556 struct scsi_position_to_element *scsi_cmd;
1558 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1559 bzero(scsi_cmd, sizeof(*scsi_cmd));
1561 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1563 scsi_ulto2b(tea, scsi_cmd->tea);
1564 scsi_ulto2b(dst, scsi_cmd->dst);
1566 if (invert)
1567 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1569 cam_fill_csio(csio,
1570 retries,
1571 cbfcnp,
1572 /*flags*/ CAM_DIR_NONE,
1573 tag_action,
1574 /*data_ptr*/ NULL,
1575 /*dxfer_len*/ 0,
1576 sense_len,
1577 sizeof(*scsi_cmd),
1578 timeout);
1581 void
1582 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1583 void (*cbfcnp)(struct cam_periph *, union ccb *),
1584 u_int8_t tag_action, int voltag, u_int32_t sea,
1585 u_int32_t count, u_int8_t *data_ptr,
1586 u_int32_t dxfer_len, u_int8_t sense_len,
1587 u_int32_t timeout)
1589 struct scsi_read_element_status *scsi_cmd;
1591 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1592 bzero(scsi_cmd, sizeof(*scsi_cmd));
1594 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1596 scsi_ulto2b(sea, scsi_cmd->sea);
1597 scsi_ulto2b(count, scsi_cmd->count);
1598 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1600 if (voltag)
1601 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1603 cam_fill_csio(csio,
1604 retries,
1605 cbfcnp,
1606 /*flags*/ CAM_DIR_IN,
1607 tag_action,
1608 data_ptr,
1609 dxfer_len,
1610 sense_len,
1611 sizeof(*scsi_cmd),
1612 timeout);
1615 void
1616 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1617 void (*cbfcnp)(struct cam_periph *, union ccb *),
1618 u_int8_t tag_action, u_int8_t sense_len,
1619 u_int32_t timeout)
1621 struct scsi_initialize_element_status *scsi_cmd;
1623 scsi_cmd = (struct scsi_initialize_element_status *)
1624 &csio->cdb_io.cdb_bytes;
1625 bzero(scsi_cmd, sizeof(*scsi_cmd));
1627 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1629 cam_fill_csio(csio,
1630 retries,
1631 cbfcnp,
1632 /*flags*/ CAM_DIR_NONE,
1633 tag_action,
1634 /* data_ptr */ NULL,
1635 /* dxfer_len */ 0,
1636 sense_len,
1637 sizeof(*scsi_cmd),
1638 timeout);
1641 void
1642 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1643 void (*cbfcnp)(struct cam_periph *, union ccb *),
1644 u_int8_t tag_action,
1645 u_int16_t element_address,
1646 u_int8_t send_action_code,
1647 struct scsi_send_volume_tag_parameters *parameters,
1648 u_int8_t sense_len, u_int32_t timeout)
1650 struct scsi_send_volume_tag *scsi_cmd;
1652 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1653 bzero(scsi_cmd, sizeof(*scsi_cmd));
1655 scsi_cmd->opcode = SEND_VOLUME_TAG;
1656 scsi_ulto2b(element_address, scsi_cmd->ea);
1657 scsi_cmd->sac = send_action_code;
1658 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1660 cam_fill_csio(csio,
1661 retries,
1662 cbfcnp,
1663 /*flags*/ CAM_DIR_OUT,
1664 tag_action,
1665 /* data_ptr */ (u_int8_t *) parameters,
1666 sizeof(*parameters),
1667 sense_len,
1668 sizeof(*scsi_cmd),
1669 timeout);