]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_ch.c
Define xpt_path_inq.
[FreeBSD/FreeBSD.git] / sys / cam / scsi / scsi_ch.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * Derived from the NetBSD SCSI changer driver.
32  *
33  *      $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
34  *
35  */
36 /*-
37  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
38  * All rights reserved.
39  *
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.
43  *
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.
58  *
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.
70  */
71
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
74
75 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/types.h>
80 #include <sys/malloc.h>
81 #include <sys/fcntl.h>
82 #include <sys/conf.h>
83 #include <sys/chio.h>
84 #include <sys/errno.h>
85 #include <sys/devicestat.h>
86
87 #include <cam/cam.h>
88 #include <cam/cam_ccb.h>
89 #include <cam/cam_periph.h>
90 #include <cam/cam_xpt_periph.h>
91 #include <cam/cam_debug.h>
92
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
95 #include <cam/scsi/scsi_ch.h>
96
97 /*
98  * Timeout definitions for various changer related commands.  They may
99  * be too short for some devices (especially the timeout for INITIALIZE
100  * ELEMENT STATUS).
101  */
102
103 static const u_int32_t  CH_TIMEOUT_MODE_SENSE                = 6000;
104 static const u_int32_t  CH_TIMEOUT_MOVE_MEDIUM               = 15 * 60 * 1000;
105 static const u_int32_t  CH_TIMEOUT_EXCHANGE_MEDIUM           = 15 * 60 * 1000;
106 static const u_int32_t  CH_TIMEOUT_POSITION_TO_ELEMENT       = 15 * 60 * 1000;
107 static const u_int32_t  CH_TIMEOUT_READ_ELEMENT_STATUS       = 5 * 60 * 1000;
108 static const u_int32_t  CH_TIMEOUT_SEND_VOLTAG               = 10000;
109 static const u_int32_t  CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
110
111 typedef enum {
112         CH_FLAG_INVALID         = 0x001
113 } ch_flags;
114
115 typedef enum {
116         CH_STATE_PROBE,
117         CH_STATE_NORMAL
118 } ch_state;
119
120 typedef enum {
121         CH_CCB_PROBE
122 } ch_ccb_types;
123
124 typedef enum {
125         CH_Q_NONE       = 0x00,
126         CH_Q_NO_DBD     = 0x01,
127         CH_Q_NO_DVCID   = 0x02
128 } ch_quirks;
129
130 #define CH_Q_BIT_STRING \
131         "\020"          \
132         "\001NO_DBD"    \
133         "\002NO_DVCID"
134
135 #define ccb_state       ppriv_field0
136 #define ccb_bp          ppriv_ptr1
137
138 struct scsi_mode_sense_data {
139         struct scsi_mode_header_6 header;
140         struct scsi_mode_blk_desc blk_desc;
141         union {
142                 struct page_element_address_assignment ea;
143                 struct page_transport_geometry_parameters tg;
144                 struct page_device_capabilities cap;
145         } pages;
146 };
147
148 struct ch_softc {
149         ch_flags        flags;
150         ch_state        state;
151         ch_quirks       quirks;
152         union ccb       saved_ccb;
153         struct devstat  *device_stats;
154         struct cdev     *dev;
155         int             open_count;
156
157         int             sc_picker;      /* current picker */
158
159         /*
160          * The following information is obtained from the
161          * element address assignment page.
162          */
163         int             sc_firsts[CHET_MAX + 1];        /* firsts */
164         int             sc_counts[CHET_MAX + 1];        /* counts */
165
166         /*
167          * The following mask defines the legal combinations
168          * of elements for the MOVE MEDIUM command.
169          */
170         u_int8_t        sc_movemask[CHET_MAX + 1];
171
172         /*
173          * As above, but for EXCHANGE MEDIUM.
174          */
175         u_int8_t        sc_exchangemask[CHET_MAX + 1];
176
177         /*
178          * Quirks; see below.  XXX KDM not implemented yet
179          */
180         int             sc_settledelay; /* delay for settle */
181 };
182
183 static  d_open_t        chopen;
184 static  d_close_t       chclose;
185 static  d_ioctl_t       chioctl;
186 static  periph_init_t   chinit;
187 static  periph_ctor_t   chregister;
188 static  periph_oninv_t  choninvalidate;
189 static  periph_dtor_t   chcleanup;
190 static  periph_start_t  chstart;
191 static  void            chasync(void *callback_arg, u_int32_t code,
192                                 struct cam_path *path, void *arg);
193 static  void            chdone(struct cam_periph *periph,
194                                union ccb *done_ccb);
195 static  int             cherror(union ccb *ccb, u_int32_t cam_flags,
196                                 u_int32_t sense_flags);
197 static  int             chmove(struct cam_periph *periph,
198                                struct changer_move *cm);
199 static  int             chexchange(struct cam_periph *periph,
200                                    struct changer_exchange *ce);
201 static  int             chposition(struct cam_periph *periph,
202                                    struct changer_position *cp);
203 static  int             chgetelemstatus(struct cam_periph *periph,
204                                 int scsi_version, u_long cmd,
205                                 struct changer_element_status_request *csr);
206 static  int             chsetvoltag(struct cam_periph *periph,
207                                     struct changer_set_voltag_request *csvr);
208 static  int             chielem(struct cam_periph *periph, 
209                                 unsigned int timeout);
210 static  int             chgetparams(struct cam_periph *periph);
211 static  int             chscsiversion(struct cam_periph *periph);
212
213 static struct periph_driver chdriver =
214 {
215         chinit, "ch",
216         TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
217 };
218
219 PERIPHDRIVER_DECLARE(ch, chdriver);
220
221 static struct cdevsw ch_cdevsw = {
222         .d_version =    D_VERSION,
223         .d_flags =      D_TRACKCLOSE,
224         .d_open =       chopen,
225         .d_close =      chclose,
226         .d_ioctl =      chioctl,
227         .d_name =       "ch",
228 };
229
230 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
231
232 static void
233 chinit(void)
234 {
235         cam_status status;
236
237         /*
238          * Install a global async callback.  This callback will
239          * receive async callbacks like "new device found".
240          */
241         status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
242
243         if (status != CAM_REQ_CMP) {
244                 printf("ch: Failed to attach master async callback "
245                        "due to status 0x%x!\n", status);
246         }
247 }
248
249 static void
250 chdevgonecb(void *arg)
251 {
252         struct ch_softc   *softc;
253         struct cam_periph *periph;
254         struct mtx *mtx;
255         int i;
256
257         periph = (struct cam_periph *)arg;
258         mtx = cam_periph_mtx(periph);
259         mtx_lock(mtx);
260
261         softc = (struct ch_softc *)periph->softc;
262         KASSERT(softc->open_count >= 0, ("Negative open count %d",
263                 softc->open_count));
264
265         /*
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.
269          */
270         for (i = 0; i < softc->open_count; i++)
271                 cam_periph_release_locked(periph);
272
273         softc->open_count = 0;
274
275         /*
276          * Release the reference held for the device node, it is gone now.
277          */
278         cam_periph_release_locked(periph);
279
280         /*
281          * We reference the lock directly here, instead of using
282          * cam_periph_unlock().  The reason is that the final call to
283          * cam_periph_release_locked() above could result in the periph
284          * getting freed.  If that is the case, dereferencing the periph
285          * with a cam_periph_unlock() call would cause a page fault.
286          */
287         mtx_unlock(mtx);
288 }
289
290 static void
291 choninvalidate(struct cam_periph *periph)
292 {
293         struct ch_softc *softc;
294
295         softc = (struct ch_softc *)periph->softc;
296
297         /*
298          * De-register any async callbacks.
299          */
300         xpt_register_async(0, chasync, periph, periph->path);
301
302         softc->flags |= CH_FLAG_INVALID;
303
304         /*
305          * Tell devfs this device has gone away, and ask for a callback
306          * when it has cleaned up its state.
307          */
308         destroy_dev_sched_cb(softc->dev, chdevgonecb, periph);
309 }
310
311 static void
312 chcleanup(struct cam_periph *periph)
313 {
314         struct ch_softc *softc;
315
316         softc = (struct ch_softc *)periph->softc;
317
318         devstat_remove_entry(softc->device_stats);
319
320         free(softc, M_DEVBUF);
321 }
322
323 static void
324 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
325 {
326         struct cam_periph *periph;
327
328         periph = (struct cam_periph *)callback_arg;
329
330         switch(code) {
331         case AC_FOUND_DEVICE:
332         {
333                 struct ccb_getdev *cgd;
334                 cam_status status;
335
336                 cgd = (struct ccb_getdev *)arg;
337                 if (cgd == NULL)
338                         break;
339
340                 if (cgd->protocol != PROTO_SCSI)
341                         break;
342                 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
343                         break;
344                 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
345                         break;
346
347                 /*
348                  * Allocate a peripheral instance for
349                  * this device and start the probe
350                  * process.
351                  */
352                 status = cam_periph_alloc(chregister, choninvalidate,
353                                           chcleanup, chstart, "ch",
354                                           CAM_PERIPH_BIO, path,
355                                           chasync, AC_FOUND_DEVICE, cgd);
356
357                 if (status != CAM_REQ_CMP
358                  && status != CAM_REQ_INPROG)
359                         printf("chasync: Unable to probe new device "
360                                "due to status 0x%x\n", status);
361
362                 break;
363
364         }
365         default:
366                 cam_periph_async(periph, code, path, arg);
367                 break;
368         }
369 }
370
371 static cam_status
372 chregister(struct cam_periph *periph, void *arg)
373 {
374         struct ch_softc *softc;
375         struct ccb_getdev *cgd;
376         struct ccb_pathinq cpi;
377         struct make_dev_args args;
378         int error;
379
380         cgd = (struct ccb_getdev *)arg;
381         if (cgd == NULL) {
382                 printf("chregister: no getdev CCB, can't register device\n");
383                 return(CAM_REQ_CMP_ERR);
384         }
385
386         softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
387
388         if (softc == NULL) {
389                 printf("chregister: Unable to probe new device. "
390                        "Unable to allocate softc\n");                           
391                 return(CAM_REQ_CMP_ERR);
392         }
393
394         bzero(softc, sizeof(*softc));
395         softc->state = CH_STATE_PROBE;
396         periph->softc = softc;
397         softc->quirks = CH_Q_NONE;
398
399         /*
400          * The DVCID and CURDATA bits were not introduced until the SMC
401          * spec.  If this device claims SCSI-2 or earlier support, then it
402          * very likely does not support these bits.
403          */
404         if (cgd->inq_data.version <= SCSI_REV_2)
405                 softc->quirks |= CH_Q_NO_DVCID;
406
407         xpt_path_inq(&cpi, periph->path);
408
409         /*
410          * Changers don't have a blocksize, and obviously don't support
411          * tagged queueing.
412          */
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);
420
421         /*
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.
425          */
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);
431         }
432
433
434         /* Register the device */
435         make_dev_args_init(&args);
436         args.mda_devsw = &ch_cdevsw;
437         args.mda_unit = periph->unit_number;
438         args.mda_uid = UID_ROOT;
439         args.mda_gid = GID_OPERATOR;
440         args.mda_mode = 0600;
441         args.mda_si_drv1 = periph;
442         error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
443             periph->unit_number);
444         cam_periph_lock(periph);
445         if (error != 0) {
446                 cam_periph_release_locked(periph);
447                 return (CAM_REQ_CMP_ERR);
448         }
449
450         /*
451          * Add an async callback so that we get
452          * notified if this device goes away.
453          */
454         xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
455
456         /*
457          * Lock this periph until we are setup.
458          * This first call can't block
459          */
460         (void)cam_periph_hold(periph, PRIBIO);
461         xpt_schedule(periph, CAM_PRIORITY_DEV);
462
463         return(CAM_REQ_CMP);
464 }
465
466 static int
467 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
468 {
469         struct cam_periph *periph;
470         struct ch_softc *softc;
471         int error;
472
473         periph = (struct cam_periph *)dev->si_drv1;
474         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
475                 return (ENXIO);
476
477         softc = (struct ch_softc *)periph->softc;
478
479         cam_periph_lock(periph);
480         
481         if (softc->flags & CH_FLAG_INVALID) {
482                 cam_periph_release_locked(periph);
483                 cam_periph_unlock(periph);
484                 return(ENXIO);
485         }
486
487         if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
488                 cam_periph_unlock(periph);
489                 cam_periph_release(periph);
490                 return (error);
491         }
492
493         /*
494          * Load information about this changer device into the softc.
495          */
496         if ((error = chgetparams(periph)) != 0) {
497                 cam_periph_unhold(periph);
498                 cam_periph_release_locked(periph);
499                 cam_periph_unlock(periph);
500                 return(error);
501         }
502
503         cam_periph_unhold(periph);
504
505         softc->open_count++;
506
507         cam_periph_unlock(periph);
508
509         return(error);
510 }
511
512 static int
513 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
514 {
515         struct  cam_periph *periph;
516         struct  ch_softc *softc;
517         struct mtx *mtx;
518
519         periph = (struct cam_periph *)dev->si_drv1;
520         mtx = cam_periph_mtx(periph);
521         mtx_lock(mtx);
522
523         softc = (struct ch_softc *)periph->softc;
524         softc->open_count--;
525
526         cam_periph_release_locked(periph);
527
528         /*
529          * We reference the lock directly here, instead of using
530          * cam_periph_unlock().  The reason is that the call to
531          * cam_periph_release_locked() above could result in the periph
532          * getting freed.  If that is the case, dereferencing the periph
533          * with a cam_periph_unlock() call would cause a page fault.
534          *
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
538          * release.
539          */
540         mtx_unlock(mtx);
541
542         return(0);
543 }
544
545 static void
546 chstart(struct cam_periph *periph, union ccb *start_ccb)
547 {
548         struct ch_softc *softc;
549
550         softc = (struct ch_softc *)periph->softc;
551
552         switch (softc->state) {
553         case CH_STATE_NORMAL:
554         {
555                 xpt_release_ccb(start_ccb);
556                 break;
557         }
558         case CH_STATE_PROBE:
559         {
560                 int mode_buffer_len;
561                 void *mode_buffer;
562
563                 /*
564                  * Include the block descriptor when calculating the mode
565                  * buffer length,
566                  */
567                 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
568                                   sizeof(struct scsi_mode_blk_desc) +
569                                  sizeof(struct page_element_address_assignment);
570
571                 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
572
573                 if (mode_buffer == NULL) {
574                         printf("chstart: couldn't malloc mode sense data\n");
575                         break;
576                 }
577                 bzero(mode_buffer, mode_buffer_len);
578
579                 /*
580                  * Get the element address assignment page.
581                  */
582                 scsi_mode_sense(&start_ccb->csio,
583                                 /* retries */ 1,
584                                 /* cbfcnp */ chdone,
585                                 /* tag_action */ MSG_SIMPLE_Q_TAG,
586                                 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
587                                         FALSE : TRUE,
588                                 /* pc */ SMS_PAGE_CTRL_CURRENT,
589                                 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
590                                 /* param_buf */ (u_int8_t *)mode_buffer,
591                                 /* param_len */ mode_buffer_len,
592                                 /* sense_len */ SSD_FULL_SIZE,
593                                 /* timeout */ CH_TIMEOUT_MODE_SENSE);
594
595                 start_ccb->ccb_h.ccb_bp = NULL;
596                 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
597                 xpt_action(start_ccb);
598                 break;
599         }
600         }
601 }
602
603 static void
604 chdone(struct cam_periph *periph, union ccb *done_ccb)
605 {
606         struct ch_softc *softc;
607         struct ccb_scsiio *csio;
608
609         softc = (struct ch_softc *)periph->softc;
610         csio = &done_ccb->csio;
611
612         switch(done_ccb->ccb_h.ccb_state) {
613         case CH_CCB_PROBE:
614         {
615                 struct scsi_mode_header_6 *mode_header;
616                 struct page_element_address_assignment *ea;
617                 char announce_buf[80];
618
619
620                 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
621
622                 ea = (struct page_element_address_assignment *)
623                         find_mode_page_6(mode_header);
624
625                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
626                         
627                         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
628                         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
629                         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
630                         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
631                         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
632                         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
633                         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
634                         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
635                         softc->sc_picker = softc->sc_firsts[CHET_MT];
636
637 #define PLURAL(c)       (c) == 1 ? "" : "s"
638                         snprintf(announce_buf, sizeof(announce_buf),
639                                 "%d slot%s, %d drive%s, "
640                                 "%d picker%s, %d portal%s",
641                                 softc->sc_counts[CHET_ST],
642                                 PLURAL(softc->sc_counts[CHET_ST]),
643                                 softc->sc_counts[CHET_DT],
644                                 PLURAL(softc->sc_counts[CHET_DT]),
645                                 softc->sc_counts[CHET_MT],
646                                 PLURAL(softc->sc_counts[CHET_MT]),
647                                 softc->sc_counts[CHET_IE],
648                                 PLURAL(softc->sc_counts[CHET_IE]));
649 #undef PLURAL
650                         if (announce_buf[0] != '\0') {
651                                 xpt_announce_periph(periph, announce_buf);
652                                 xpt_announce_quirks(periph, softc->quirks,
653                                     CH_Q_BIT_STRING);
654                         }
655                 } else {
656                         int error;
657
658                         error = cherror(done_ccb, CAM_RETRY_SELTO,
659                                         SF_RETRY_UA | SF_NO_PRINT);
660                         /*
661                          * Retry any UNIT ATTENTION type errors.  They
662                          * are expected at boot.
663                          */
664                         if (error == ERESTART) {
665                                 /*
666                                  * A retry was scheduled, so
667                                  * just return.
668                                  */
669                                 return;
670                         } else if (error != 0) {
671                                 struct scsi_mode_sense_6 *sms;
672                                 int frozen, retry_scheduled;
673
674                                 sms = (struct scsi_mode_sense_6 *)
675                                         done_ccb->csio.cdb_io.cdb_bytes;
676                                 frozen = (done_ccb->ccb_h.status &
677                                     CAM_DEV_QFRZN) != 0;
678
679                                 /*
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.
687                                  */
688                                 if ((sms->byte2 & SMS_DBD) != 0 &&
689                                     (periph->flags & CAM_PERIPH_INVALID) == 0) {
690                                         sms->byte2 &= ~SMS_DBD;
691                                         xpt_action(done_ccb);
692                                         softc->quirks |= CH_Q_NO_DBD;
693                                         retry_scheduled = 1;
694                                 } else
695                                         retry_scheduled = 0;
696
697                                 /* Don't wedge this device's queue */
698                                 if (frozen)
699                                         cam_release_devq(done_ccb->ccb_h.path,
700                                                  /*relsim_flags*/0,
701                                                  /*reduction*/0,
702                                                  /*timeout*/0,
703                                                  /*getcount_only*/0);
704
705                                 if (retry_scheduled)
706                                         return;
707
708                                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
709                                     == CAM_SCSI_STATUS_ERROR) 
710                                         scsi_sense_print(&done_ccb->csio);
711                                 else {
712                                         xpt_print(periph->path,
713                                             "got CAM status %#x\n",
714                                             done_ccb->ccb_h.status);
715                                 }
716                                 xpt_print(periph->path, "fatal error, failed "
717                                     "to attach to device\n");
718
719                                 cam_periph_invalidate(periph);
720
721                         }
722                 }
723                 softc->state = CH_STATE_NORMAL;
724                 free(mode_header, M_SCSICH);
725                 /*
726                  * Since our peripheral may be invalidated by an error
727                  * above or an external event, we must release our CCB
728                  * before releasing the probe lock on the peripheral.
729                  * The peripheral will only go away once the last lock
730                  * is removed, and we need it around for the CCB release
731                  * operation.
732                  */
733                 xpt_release_ccb(done_ccb);
734                 cam_periph_unhold(periph);
735                 return;
736         }
737         default:
738                 break;
739         }
740         xpt_release_ccb(done_ccb);
741 }
742
743 static int
744 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
745 {
746         struct ch_softc *softc;
747         struct cam_periph *periph;
748
749         periph = xpt_path_periph(ccb->ccb_h.path);
750         softc = (struct ch_softc *)periph->softc;
751
752         return (cam_periph_error(ccb, cam_flags, sense_flags));
753 }
754
755 static int
756 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
757 {
758         struct cam_periph *periph;
759         struct ch_softc *softc;
760         int error;
761
762         periph = (struct cam_periph *)dev->si_drv1;
763         cam_periph_lock(periph);
764         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
765
766         softc = (struct ch_softc *)periph->softc;
767
768         error = 0;
769
770         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
771                   ("trying to do ioctl %#lx\n", cmd));
772
773         /*
774          * If this command can change the device's state, we must
775          * have the device open for writing.
776          */
777         switch (cmd) {
778         case CHIOGPICKER:
779         case CHIOGPARAMS:
780         case OCHIOGSTATUS:
781         case CHIOGSTATUS:
782                 break;
783
784         default:
785                 if ((flag & FWRITE) == 0) {
786                         cam_periph_unlock(periph);
787                         return (EBADF);
788                 }
789         }
790
791         switch (cmd) {
792         case CHIOMOVE:
793                 error = chmove(periph, (struct changer_move *)addr);
794                 break;
795
796         case CHIOEXCHANGE:
797                 error = chexchange(periph, (struct changer_exchange *)addr);
798                 break;
799
800         case CHIOPOSITION:
801                 error = chposition(periph, (struct changer_position *)addr);
802                 break;
803
804         case CHIOGPICKER:
805                 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
806                 break;
807
808         case CHIOSPICKER:
809         {
810                 int new_picker = *(int *)addr;
811
812                 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
813                         error = EINVAL;
814                         break;
815                 }
816                 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
817                 break;
818         }
819         case CHIOGPARAMS:
820         {
821                 struct changer_params *cp = (struct changer_params *)addr;
822
823                 cp->cp_npickers = softc->sc_counts[CHET_MT];
824                 cp->cp_nslots = softc->sc_counts[CHET_ST];
825                 cp->cp_nportals = softc->sc_counts[CHET_IE];
826                 cp->cp_ndrives = softc->sc_counts[CHET_DT];
827                 break;
828         }
829         case CHIOIELEM:
830                 error = chielem(periph, *(unsigned int *)addr);
831                 break;
832
833         case OCHIOGSTATUS:
834         {
835                 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
836                     (struct changer_element_status_request *)addr);
837                 break;
838         }
839
840         case CHIOGSTATUS:
841         {
842                 int scsi_version;
843
844                 scsi_version = chscsiversion(periph);
845                 if (scsi_version >= SCSI_REV_0) {
846                         error = chgetelemstatus(periph, scsi_version, cmd,
847                             (struct changer_element_status_request *)addr);
848                 }
849                 else { /* unable to determine the SCSI version */
850                         cam_periph_unlock(periph);
851                         return (ENXIO);
852                 }
853                 break;
854         }
855
856         case CHIOSETVOLTAG:
857         {
858                 error = chsetvoltag(periph,
859                                     (struct changer_set_voltag_request *) addr);
860                 break;
861         }
862
863         /* Implement prevent/allow? */
864
865         default:
866                 error = cam_periph_ioctl(periph, cmd, addr, cherror);
867                 break;
868         }
869
870         cam_periph_unlock(periph);
871         return (error);
872 }
873
874 static int
875 chmove(struct cam_periph *periph, struct changer_move *cm)
876 {
877         struct ch_softc *softc;
878         u_int16_t fromelem, toelem;
879         union ccb *ccb;
880         int error;
881
882         error = 0;
883         softc = (struct ch_softc *)periph->softc;
884
885         /*
886          * Check arguments.
887          */
888         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
889                 return (EINVAL);
890         if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
891             (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
892                 return (ENODEV);
893
894         /*
895          * Check the request against the changer's capabilities.
896          */
897         if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
898                 return (ENODEV);
899
900         /*
901          * Calculate the source and destination elements.
902          */
903         fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
904         toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
905
906         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
907
908         scsi_move_medium(&ccb->csio,
909                          /* retries */ 1,
910                          /* cbfcnp */ chdone,
911                          /* tag_action */ MSG_SIMPLE_Q_TAG,
912                          /* tea */ softc->sc_picker,
913                          /* src */ fromelem,
914                          /* dst */ toelem,
915                          /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
916                          /* sense_len */ SSD_FULL_SIZE,
917                          /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
918
919         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
920                                   /*sense_flags*/ SF_RETRY_UA,
921                                   softc->device_stats);
922
923         xpt_release_ccb(ccb);
924
925         return(error);
926 }
927
928 static int
929 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
930 {
931         struct ch_softc *softc;
932         u_int16_t src, dst1, dst2;
933         union ccb *ccb;
934         int error;
935
936         error = 0;
937         softc = (struct ch_softc *)periph->softc;
938         /*
939          * Check arguments.
940          */
941         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
942             (ce->ce_sdsttype > CHET_DT))
943                 return (EINVAL);
944         if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
945             (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
946             (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
947                 return (ENODEV);
948
949         /*
950          * Check the request against the changer's capabilities.
951          */
952         if (((softc->sc_exchangemask[ce->ce_srctype] &
953              (1 << ce->ce_fdsttype)) == 0) ||
954             ((softc->sc_exchangemask[ce->ce_fdsttype] &
955              (1 << ce->ce_sdsttype)) == 0))
956                 return (ENODEV);
957
958         /*
959          * Calculate the source and destination elements.
960          */
961         src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
962         dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
963         dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
964
965         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
966
967         scsi_exchange_medium(&ccb->csio,
968                              /* retries */ 1,
969                              /* cbfcnp */ chdone,
970                              /* tag_action */ MSG_SIMPLE_Q_TAG,
971                              /* tea */ softc->sc_picker,
972                              /* src */ src,
973                              /* dst1 */ dst1,
974                              /* dst2 */ dst2,
975                              /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
976                                            TRUE : FALSE,
977                              /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
978                                            TRUE : FALSE,
979                              /* sense_len */ SSD_FULL_SIZE,
980                              /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
981
982         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
983                                   /*sense_flags*/ SF_RETRY_UA,
984                                   softc->device_stats);
985
986         xpt_release_ccb(ccb);
987
988         return(error);
989 }
990
991 static int
992 chposition(struct cam_periph *periph, struct changer_position *cp)
993 {
994         struct ch_softc *softc;
995         u_int16_t dst;
996         union ccb *ccb;
997         int error;
998
999         error = 0;
1000         softc = (struct ch_softc *)periph->softc;
1001
1002         /*
1003          * Check arguments.
1004          */
1005         if (cp->cp_type > CHET_DT)
1006                 return (EINVAL);
1007         if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1008                 return (ENODEV);
1009
1010         /*
1011          * Calculate the destination element.
1012          */
1013         dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1014
1015         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1016
1017         scsi_position_to_element(&ccb->csio,
1018                                  /* retries */ 1,
1019                                  /* cbfcnp */ chdone,
1020                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1021                                  /* tea */ softc->sc_picker,
1022                                  /* dst */ dst,
1023                                  /* invert */ (cp->cp_flags & CP_INVERT) ?
1024                                               TRUE : FALSE,
1025                                  /* sense_len */ SSD_FULL_SIZE,
1026                                  /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1027
1028         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1029                                   /*sense_flags*/ SF_RETRY_UA,
1030                                   softc->device_stats);
1031
1032         xpt_release_ccb(ccb);
1033
1034         return(error);
1035 }
1036
1037 /*
1038  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1039  * to host native byte order in the volume serial number.  The volume
1040  * label as returned by the changer is transferred to user mode as
1041  * nul-terminated string.  Volume labels are truncated at the first
1042  * space, as suggested by SCSI-2.
1043  */
1044 static  void
1045 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1046 {
1047         int i;
1048         for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1049                 char c = voltag->vif[i];
1050                 if (c && c != ' ')
1051                         uvoltag->cv_volid[i] = c;
1052                 else
1053                         break;
1054         }
1055         uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1056 }
1057
1058 /*
1059  * Copy an element status descriptor to a user-mode
1060  * changer_element_status structure.
1061  */
1062 static void
1063 copy_element_status(struct ch_softc *softc,
1064                     u_int16_t flags,
1065                     struct read_element_status_descriptor *desc,
1066                     struct changer_element_status *ces,
1067                     int scsi_version)
1068 {
1069         u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1070         u_int16_t et;
1071         struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1072         struct read_element_status_device_id *devid = NULL;
1073
1074         ces->ces_int_addr = eaddr;
1075         /* set up logical address in element status */
1076         for (et = CHET_MT; et <= CHET_DT; et++) {
1077                 if ((softc->sc_firsts[et] <= eaddr)
1078                     && ((softc->sc_firsts[et] + softc->sc_counts[et])
1079                         > eaddr)) {
1080                         ces->ces_addr = eaddr - softc->sc_firsts[et];
1081                         ces->ces_type = et;
1082                         break;
1083                 }
1084         }
1085
1086         ces->ces_flags = desc->flags1;
1087
1088         ces->ces_sensecode = desc->sense_code;
1089         ces->ces_sensequal = desc->sense_qual;
1090
1091         if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1092                 ces->ces_flags |= CES_INVERT;
1093
1094         if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1095
1096                 eaddr = scsi_2btoul(desc->ssea);
1097
1098                 /* convert source address to logical format */
1099                 for (et = CHET_MT; et <= CHET_DT; et++) {
1100                         if ((softc->sc_firsts[et] <= eaddr)
1101                             && ((softc->sc_firsts[et] + softc->sc_counts[et])
1102                                 > eaddr)) {
1103                                 ces->ces_source_addr =
1104                                         eaddr - softc->sc_firsts[et];
1105                                 ces->ces_source_type = et;
1106                                 ces->ces_flags |= CES_SOURCE_VALID;
1107                                 break;
1108                         }
1109                 }
1110
1111                 if (!(ces->ces_flags & CES_SOURCE_VALID))
1112                         printf("ch: warning: could not map element source "
1113                                "address %ud to a valid element type\n",
1114                                eaddr);
1115         }
1116
1117         /*
1118          * pvoltag and avoltag are common between SCSI-2 and later versions
1119          */
1120         if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1121                 pvol_tag = &desc->voltag_devid.pvoltag;
1122         if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1123                 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1124                     &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1125         /*
1126          * For SCSI-3 and later, element status can carry designator and
1127          * other information.
1128          */
1129         if (scsi_version >= SCSI_REV_SPC) {
1130                 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1131                     (flags & READ_ELEMENT_STATUS_AVOLTAG))
1132                         devid = &desc->voltag_devid.pvol_and_devid.devid;
1133                 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1134                          !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1135                         devid = &desc->voltag_devid.devid;
1136                 else /* Have both PVOLTAG and AVOLTAG */
1137                         devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1138         }
1139
1140         if (pvol_tag)
1141                 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1142         if (avol_tag)
1143                 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1144         if (devid != NULL) {
1145                 if (devid->designator_length > 0) {
1146                         bcopy((void *)devid->designator,
1147                               (void *)ces->ces_designator,
1148                               devid->designator_length);
1149                         ces->ces_designator_length = devid->designator_length;
1150                         /*
1151                          * Make sure we are always NUL terminated.  The
1152                          * This won't matter for the binary code set,
1153                          * since the user will only pay attention to the
1154                          * length field.
1155                          */
1156                         ces->ces_designator[devid->designator_length]= '\0';
1157                 }
1158                 if (devid->piv_assoc_designator_type &
1159                     READ_ELEMENT_STATUS_PIV_SET) {
1160                         ces->ces_flags |= CES_PIV;
1161                         ces->ces_protocol_id =
1162                             READ_ELEMENT_STATUS_PROTOCOL_ID(
1163                             devid->prot_code_set);
1164                 }
1165                 ces->ces_code_set =
1166                     READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1167                 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1168                     devid->piv_assoc_designator_type);
1169                 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1170                     devid->piv_assoc_designator_type);
1171         } else if (scsi_version > SCSI_REV_2) {
1172                 /* SCSI-SPC and No devid, no designator */
1173                 ces->ces_designator_length = 0;
1174                 ces->ces_designator[0] = '\0';
1175                 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1176         }
1177
1178         if (scsi_version <= SCSI_REV_2) {
1179                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1180                     READ_ELEMENT_STATUS_DT_IDVALID) {
1181                         ces->ces_flags |= CES_SCSIID_VALID;
1182                         ces->ces_scsi_id =
1183                             desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1184                 }
1185
1186                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1187                     READ_ELEMENT_STATUS_DT_LUVALID) {
1188                         ces->ces_flags |= CES_LUN_VALID;
1189                         ces->ces_scsi_lun =
1190                             desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1191                             READ_ELEMENT_STATUS_DT_LUNMASK;
1192                 }
1193         }
1194 }
1195
1196 static int
1197 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1198                 struct changer_element_status_request *cesr)
1199 {
1200         struct read_element_status_header *st_hdr;
1201         struct read_element_status_page_header *pg_hdr;
1202         struct read_element_status_descriptor *desc;
1203         caddr_t data = NULL;
1204         size_t size, desclen;
1205         int avail, i, error = 0;
1206         int curdata, dvcid, sense_flags;
1207         int try_no_dvcid = 0;
1208         struct changer_element_status *user_data = NULL;
1209         struct ch_softc *softc;
1210         union ccb *ccb;
1211         int chet = cesr->cesr_element_type;
1212         int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1213
1214         softc = (struct ch_softc *)periph->softc;
1215
1216         /* perform argument checking */
1217
1218         /*
1219          * Perform a range check on the cesr_element_{base,count}
1220          * request argument fields.
1221          */
1222         if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1223             || (cesr->cesr_element_base + cesr->cesr_element_count)
1224                 > softc->sc_counts[chet])
1225                 return (EINVAL);
1226
1227         /*
1228          * Request one descriptor for the given element type.  This
1229          * is used to determine the size of the descriptor so that
1230          * we can allocate enough storage for all of them.  We assume
1231          * that the first one can fit into 1k.
1232          */
1233         cam_periph_unlock(periph);
1234         data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1235
1236         cam_periph_lock(periph);
1237         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1238
1239         sense_flags = SF_RETRY_UA;
1240         if (softc->quirks & CH_Q_NO_DVCID) {
1241                 dvcid = 0;
1242                 curdata = 0;
1243         } else {
1244                 dvcid = 1;
1245                 curdata = 1;
1246                 /*
1247                  * Don't print anything for an Illegal Request, because
1248                  * these flags can cause some changers to complain.  We'll
1249                  * retry without them if we get an error.
1250                  */
1251                 sense_flags |= SF_QUIET_IR;
1252         }
1253
1254 retry_einval:
1255
1256         scsi_read_element_status(&ccb->csio,
1257                                  /* retries */ 1,
1258                                  /* cbfcnp */ chdone,
1259                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1260                                  /* voltag */ want_voltags,
1261                                  /* sea */ softc->sc_firsts[chet],
1262                                  /* curdata */ curdata,
1263                                  /* dvcid */ dvcid,
1264                                  /* count */ 1,
1265                                  /* data_ptr */ data,
1266                                  /* dxfer_len */ 1024,
1267                                  /* sense_len */ SSD_FULL_SIZE,
1268                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1269
1270         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1271                                   /*sense_flags*/ sense_flags,
1272                                   softc->device_stats);
1273
1274         /*
1275          * An Illegal Request sense key (only used if there is no asc/ascq)
1276          * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL.  If dvcid or
1277          * curdata are set (we set both or neither), try turning them off
1278          * and see if the command is successful.
1279          */
1280         if ((error == EINVAL)
1281          && (dvcid || curdata))  {
1282                 dvcid = 0;
1283                 curdata = 0;
1284                 error = 0;
1285                 /* At this point we want to report any Illegal Request */
1286                 sense_flags &= ~SF_QUIET_IR;
1287                 try_no_dvcid = 1;
1288                 goto retry_einval;
1289         }
1290
1291         /*
1292          * In this case, we tried a read element status with dvcid and
1293          * curdata set, and it failed.  We retried without those bits, and
1294          * it succeeded.  Suggest to the user that he set a quirk, so we
1295          * don't go through the retry process the first time in the future.
1296          * This should only happen on changers that claim SCSI-3 or higher,
1297          * but don't support these bits.
1298          */
1299         if ((try_no_dvcid != 0)
1300          && (error == 0))
1301                 softc->quirks |= CH_Q_NO_DVCID;
1302
1303         if (error)
1304                 goto done;
1305         cam_periph_unlock(periph);
1306
1307         st_hdr = (struct read_element_status_header *)data;
1308         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1309                   sizeof(struct read_element_status_header));
1310         desclen = scsi_2btoul(pg_hdr->edl);
1311
1312         size = sizeof(struct read_element_status_header) +
1313                sizeof(struct read_element_status_page_header) +
1314                (desclen * cesr->cesr_element_count);
1315         /*
1316          * Reallocate storage for descriptors and get them from the
1317          * device.
1318          */
1319         free(data, M_DEVBUF);
1320         data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1321
1322         cam_periph_lock(periph);
1323         scsi_read_element_status(&ccb->csio,
1324                                  /* retries */ 1,
1325                                  /* cbfcnp */ chdone,
1326                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1327                                  /* voltag */ want_voltags,
1328                                  /* sea */ softc->sc_firsts[chet]
1329                                  + cesr->cesr_element_base,
1330                                  /* curdata */ curdata,
1331                                  /* dvcid */ dvcid,
1332                                  /* count */ cesr->cesr_element_count,
1333                                  /* data_ptr */ data,
1334                                  /* dxfer_len */ size,
1335                                  /* sense_len */ SSD_FULL_SIZE,
1336                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1337
1338         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1339                                   /*sense_flags*/ SF_RETRY_UA,
1340                                   softc->device_stats);
1341
1342         if (error)
1343                 goto done;
1344         cam_periph_unlock(periph);
1345
1346         /*
1347          * Fill in the user status array.
1348          */
1349         st_hdr = (struct read_element_status_header *)data;
1350         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1351                   sizeof(struct read_element_status_header));
1352         avail = scsi_2btoul(st_hdr->count);
1353
1354         if (avail != cesr->cesr_element_count) {
1355                 xpt_print(periph->path,
1356                     "warning, READ ELEMENT STATUS avail != count\n");
1357         }
1358
1359         user_data = (struct changer_element_status *)
1360                 malloc(avail * sizeof(struct changer_element_status),
1361                        M_DEVBUF, M_WAITOK | M_ZERO);
1362
1363         desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1364                 sizeof(struct read_element_status_header) +
1365                 sizeof(struct read_element_status_page_header));
1366         /*
1367          * Set up the individual element status structures
1368          */
1369         for (i = 0; i < avail; ++i) {
1370                 struct changer_element_status *ces;
1371
1372                 /*
1373                  * In the changer_element_status structure, fields from
1374                  * the beginning to the field of ces_scsi_lun are common
1375                  * between SCSI-2 and SCSI-3, while all the rest are new
1376                  * from SCSI-3. In order to maintain backward compatibility
1377                  * of the chio command, the ces pointer, below, is computed
1378                  * such that it lines up with the structure boundary
1379                  * corresponding to the SCSI version.
1380                  */
1381                 ces = cmd == OCHIOGSTATUS ?
1382                     (struct changer_element_status *)
1383                     ((unsigned char *)user_data + i *
1384                      (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1385                     &user_data[i];
1386
1387                 copy_element_status(softc, pg_hdr->flags, desc,
1388                                     ces, scsi_version);
1389
1390                 desc = (struct read_element_status_descriptor *)
1391                        ((unsigned char *)desc + desclen);
1392         }
1393
1394         /* Copy element status structures out to userspace. */
1395         if (cmd == OCHIOGSTATUS)
1396                 error = copyout(user_data,
1397                                 cesr->cesr_element_status,
1398                                 avail* (offsetof(struct changer_element_status,
1399                                 ces_scsi_lun) + 1));
1400         else
1401                 error = copyout(user_data,
1402                                 cesr->cesr_element_status,
1403                                 avail * sizeof(struct changer_element_status));
1404
1405         cam_periph_lock(periph);
1406
1407  done:
1408         xpt_release_ccb(ccb);
1409
1410         if (data != NULL)
1411                 free(data, M_DEVBUF);
1412         if (user_data != NULL)
1413                 free(user_data, M_DEVBUF);
1414
1415         return (error);
1416 }
1417
1418 static int
1419 chielem(struct cam_periph *periph,
1420         unsigned int timeout)
1421 {
1422         union ccb *ccb;
1423         struct ch_softc *softc;
1424         int error;
1425
1426         if (!timeout) {
1427                 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1428         } else {
1429                 timeout *= 1000;
1430         }
1431
1432         error = 0;
1433         softc = (struct ch_softc *)periph->softc;
1434
1435         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1436
1437         scsi_initialize_element_status(&ccb->csio,
1438                                       /* retries */ 1,
1439                                       /* cbfcnp */ chdone,
1440                                       /* tag_action */ MSG_SIMPLE_Q_TAG,
1441                                       /* sense_len */ SSD_FULL_SIZE,
1442                                       /* timeout */ timeout);
1443
1444         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1445                                   /*sense_flags*/ SF_RETRY_UA,
1446                                   softc->device_stats);
1447
1448         xpt_release_ccb(ccb);
1449
1450         return(error);
1451 }
1452
1453 static int
1454 chsetvoltag(struct cam_periph *periph,
1455             struct changer_set_voltag_request *csvr)
1456 {
1457         union ccb *ccb;
1458         struct ch_softc *softc;
1459         u_int16_t ea;
1460         u_int8_t sac;
1461         struct scsi_send_volume_tag_parameters ssvtp;
1462         int error;
1463         int i;
1464
1465         error = 0;
1466         softc = (struct ch_softc *)periph->softc;
1467
1468         bzero(&ssvtp, sizeof(ssvtp));
1469         for (i=0; i<sizeof(ssvtp.vitf); i++) {
1470                 ssvtp.vitf[i] = ' ';
1471         }
1472
1473         /*
1474          * Check arguments.
1475          */
1476         if (csvr->csvr_type > CHET_DT)
1477                 return EINVAL;
1478         if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1479                 return ENODEV;
1480
1481         ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1482
1483         if (csvr->csvr_flags & CSVR_ALTERNATE) {
1484                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1485                 case CSVR_MODE_SET:
1486                         sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1487                         break;
1488                 case CSVR_MODE_REPLACE:
1489                         sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1490                         break;
1491                 case CSVR_MODE_CLEAR:
1492                         sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1493                         break;
1494                 default:
1495                         error = EINVAL;
1496                         goto out;
1497                 }
1498         } else {
1499                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1500                 case CSVR_MODE_SET:
1501                         sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1502                         break;
1503                 case CSVR_MODE_REPLACE:
1504                         sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1505                         break;
1506                 case CSVR_MODE_CLEAR:
1507                         sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1508                         break;
1509                 default:
1510                         error = EINVAL;
1511                         goto out;
1512                 }
1513         }
1514
1515         memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1516                min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1517         scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1518
1519         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1520
1521         scsi_send_volume_tag(&ccb->csio,
1522                              /* retries */ 1,
1523                              /* cbfcnp */ chdone,
1524                              /* tag_action */ MSG_SIMPLE_Q_TAG,
1525                              /* element_address */ ea,
1526                              /* send_action_code */ sac,
1527                              /* parameters */ &ssvtp,
1528                              /* sense_len */ SSD_FULL_SIZE,
1529                              /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1530         
1531         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1532                                   /*sense_flags*/ SF_RETRY_UA,
1533                                   softc->device_stats);
1534
1535         xpt_release_ccb(ccb);
1536
1537  out:
1538         return error;
1539 }
1540
1541 static int
1542 chgetparams(struct cam_periph *periph)
1543 {
1544         union ccb *ccb;
1545         struct ch_softc *softc;
1546         void *mode_buffer;
1547         int mode_buffer_len;
1548         struct page_element_address_assignment *ea;
1549         struct page_device_capabilities *cap;
1550         int error, from, dbd;
1551         u_int8_t *moves, *exchanges;
1552
1553         error = 0;
1554
1555         softc = (struct ch_softc *)periph->softc;
1556
1557         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1558
1559         /*
1560          * The scsi_mode_sense_data structure is just a convenience
1561          * structure that allows us to easily calculate the worst-case
1562          * storage size of the mode sense buffer.
1563          */
1564         mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1565
1566         mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1567
1568         if (mode_buffer == NULL) {
1569                 printf("chgetparams: couldn't malloc mode sense data\n");
1570                 xpt_release_ccb(ccb);
1571                 return(ENOSPC);
1572         }
1573
1574         bzero(mode_buffer, mode_buffer_len);
1575
1576         if (softc->quirks & CH_Q_NO_DBD)
1577                 dbd = FALSE;
1578         else
1579                 dbd = TRUE;
1580
1581         /*
1582          * Get the element address assignment page.
1583          */
1584         scsi_mode_sense(&ccb->csio,
1585                         /* retries */ 1,
1586                         /* cbfcnp */ chdone,
1587                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1588                         /* dbd */ dbd,
1589                         /* pc */ SMS_PAGE_CTRL_CURRENT,
1590                         /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1591                         /* param_buf */ (u_int8_t *)mode_buffer,
1592                         /* param_len */ mode_buffer_len,
1593                         /* sense_len */ SSD_FULL_SIZE,
1594                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1595
1596         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1597                                   /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1598                                   softc->device_stats);
1599
1600         if (error) {
1601                 if (dbd) {
1602                         struct scsi_mode_sense_6 *sms;
1603
1604                         sms = (struct scsi_mode_sense_6 *)
1605                                 ccb->csio.cdb_io.cdb_bytes;
1606
1607                         sms->byte2 &= ~SMS_DBD;
1608                         error = cam_periph_runccb(ccb, cherror,
1609                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1610                                                   /*sense_flags*/ SF_RETRY_UA,
1611                                                   softc->device_stats);
1612                 } else {
1613                         /*
1614                          * Since we disabled sense printing above, print
1615                          * out the sense here since we got an error.
1616                          */
1617                         scsi_sense_print(&ccb->csio);
1618                 }
1619
1620                 if (error) {
1621                         xpt_print(periph->path,
1622                             "chgetparams: error getting element "
1623                             "address page\n");
1624                         xpt_release_ccb(ccb);
1625                         free(mode_buffer, M_SCSICH);
1626                         return(error);
1627                 }
1628         }
1629
1630         ea = (struct page_element_address_assignment *)
1631                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1632
1633         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1634         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1635         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1636         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1637         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1638         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1639         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1640         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1641
1642         bzero(mode_buffer, mode_buffer_len);
1643
1644         /*
1645          * Now get the device capabilities page.
1646          */
1647         scsi_mode_sense(&ccb->csio,
1648                         /* retries */ 1,
1649                         /* cbfcnp */ chdone,
1650                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1651                         /* dbd */ dbd,
1652                         /* pc */ SMS_PAGE_CTRL_CURRENT,
1653                         /* page */ CH_DEVICE_CAP_PAGE,
1654                         /* param_buf */ (u_int8_t *)mode_buffer,
1655                         /* param_len */ mode_buffer_len,
1656                         /* sense_len */ SSD_FULL_SIZE,
1657                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1658         
1659         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1660                                   /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1661                                   softc->device_stats);
1662
1663         if (error) {
1664                 if (dbd) {
1665                         struct scsi_mode_sense_6 *sms;
1666
1667                         sms = (struct scsi_mode_sense_6 *)
1668                                 ccb->csio.cdb_io.cdb_bytes;
1669
1670                         sms->byte2 &= ~SMS_DBD;
1671                         error = cam_periph_runccb(ccb, cherror,
1672                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1673                                                   /*sense_flags*/ SF_RETRY_UA,
1674                                                   softc->device_stats);
1675                 } else {
1676                         /*
1677                          * Since we disabled sense printing above, print
1678                          * out the sense here since we got an error.
1679                          */
1680                         scsi_sense_print(&ccb->csio);
1681                 }
1682
1683                 if (error) {
1684                         xpt_print(periph->path,
1685                             "chgetparams: error getting device "
1686                             "capabilities page\n");
1687                         xpt_release_ccb(ccb);
1688                         free(mode_buffer, M_SCSICH);
1689                         return(error);
1690                 }
1691         }
1692
1693         xpt_release_ccb(ccb);
1694
1695         cap = (struct page_device_capabilities *)
1696                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1697
1698         bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1699         bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1700         moves = cap->move_from;
1701         exchanges = cap->exchange_with;
1702         for (from = CHET_MT; from <= CHET_MAX; ++from) {
1703                 softc->sc_movemask[from] = moves[from];
1704                 softc->sc_exchangemask[from] = exchanges[from];
1705         }
1706
1707         free(mode_buffer, M_SCSICH);
1708
1709         return(error);
1710 }
1711
1712 static int
1713 chscsiversion(struct cam_periph *periph)
1714 {
1715         struct scsi_inquiry_data *inq_data;
1716         struct ccb_getdev *cgd;
1717         int dev_scsi_version;
1718
1719         cam_periph_assert(periph, MA_OWNED);
1720         if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1721                 return (-1);
1722         /*
1723          * Get the device information.
1724          */
1725         xpt_setup_ccb(&cgd->ccb_h,
1726                       periph->path,
1727                       CAM_PRIORITY_NORMAL);
1728         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1729         xpt_action((union ccb *)cgd);
1730
1731         if (cgd->ccb_h.status != CAM_REQ_CMP) {
1732                 xpt_free_ccb((union ccb *)cgd);
1733                 return -1;
1734         }
1735
1736         inq_data = &cgd->inq_data;
1737         dev_scsi_version = inq_data->version;
1738         xpt_free_ccb((union ccb *)cgd);
1739
1740         return dev_scsi_version;
1741 }
1742
1743 void
1744 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1745                  void (*cbfcnp)(struct cam_periph *, union ccb *),
1746                  u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1747                  u_int32_t dst, int invert, u_int8_t sense_len,
1748                  u_int32_t timeout)
1749 {
1750         struct scsi_move_medium *scsi_cmd;
1751
1752         scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1753         bzero(scsi_cmd, sizeof(*scsi_cmd));
1754
1755         scsi_cmd->opcode = MOVE_MEDIUM;
1756
1757         scsi_ulto2b(tea, scsi_cmd->tea);
1758         scsi_ulto2b(src, scsi_cmd->src);
1759         scsi_ulto2b(dst, scsi_cmd->dst);
1760
1761         if (invert)
1762                 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1763
1764         cam_fill_csio(csio,
1765                       retries,
1766                       cbfcnp,
1767                       /*flags*/ CAM_DIR_NONE,
1768                       tag_action,
1769                       /*data_ptr*/ NULL,
1770                       /*dxfer_len*/ 0,
1771                       sense_len,
1772                       sizeof(*scsi_cmd),
1773                       timeout);
1774 }
1775
1776 void
1777 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1778                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1779                      u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1780                      u_int32_t dst1, u_int32_t dst2, int invert1,
1781                      int invert2, u_int8_t sense_len, u_int32_t timeout)
1782 {
1783         struct scsi_exchange_medium *scsi_cmd;
1784
1785         scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1786         bzero(scsi_cmd, sizeof(*scsi_cmd));
1787
1788         scsi_cmd->opcode = EXCHANGE_MEDIUM;
1789
1790         scsi_ulto2b(tea, scsi_cmd->tea);
1791         scsi_ulto2b(src, scsi_cmd->src);
1792         scsi_ulto2b(dst1, scsi_cmd->fdst);
1793         scsi_ulto2b(dst2, scsi_cmd->sdst);
1794
1795         if (invert1)
1796                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1797
1798         if (invert2)
1799                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1800
1801         cam_fill_csio(csio,
1802                       retries,
1803                       cbfcnp,
1804                       /*flags*/ CAM_DIR_NONE,
1805                       tag_action,
1806                       /*data_ptr*/ NULL,
1807                       /*dxfer_len*/ 0,
1808                       sense_len,
1809                       sizeof(*scsi_cmd),
1810                       timeout);
1811 }
1812
1813 void
1814 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1815                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1816                          u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1817                          int invert, u_int8_t sense_len, u_int32_t timeout)
1818 {
1819         struct scsi_position_to_element *scsi_cmd;
1820
1821         scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1822         bzero(scsi_cmd, sizeof(*scsi_cmd));
1823
1824         scsi_cmd->opcode = POSITION_TO_ELEMENT;
1825
1826         scsi_ulto2b(tea, scsi_cmd->tea);
1827         scsi_ulto2b(dst, scsi_cmd->dst);
1828
1829         if (invert)
1830                 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1831
1832         cam_fill_csio(csio,
1833                       retries,
1834                       cbfcnp,
1835                       /*flags*/ CAM_DIR_NONE,
1836                       tag_action,
1837                       /*data_ptr*/ NULL,
1838                       /*dxfer_len*/ 0,
1839                       sense_len,
1840                       sizeof(*scsi_cmd),
1841                       timeout);
1842 }
1843
1844 void
1845 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1846                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1847                          u_int8_t tag_action, int voltag, u_int32_t sea,
1848                          int curdata, int dvcid,
1849                          u_int32_t count, u_int8_t *data_ptr,
1850                          u_int32_t dxfer_len, u_int8_t sense_len,
1851                          u_int32_t timeout)
1852 {
1853         struct scsi_read_element_status *scsi_cmd;
1854
1855         scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1856         bzero(scsi_cmd, sizeof(*scsi_cmd));
1857
1858         scsi_cmd->opcode = READ_ELEMENT_STATUS;
1859
1860         scsi_ulto2b(sea, scsi_cmd->sea);
1861         scsi_ulto2b(count, scsi_cmd->count);
1862         scsi_ulto3b(dxfer_len, scsi_cmd->len);
1863         if (dvcid)
1864                 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1865         if (curdata)
1866                 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1867
1868         if (voltag)
1869                 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1870
1871         cam_fill_csio(csio,
1872                       retries,
1873                       cbfcnp,
1874                       /*flags*/ CAM_DIR_IN,
1875                       tag_action,
1876                       data_ptr,
1877                       dxfer_len,
1878                       sense_len,
1879                       sizeof(*scsi_cmd),
1880                       timeout);
1881 }
1882
1883 void
1884 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1885                                void (*cbfcnp)(struct cam_periph *, union ccb *),
1886                                u_int8_t tag_action, u_int8_t sense_len,
1887                                u_int32_t timeout)
1888 {
1889         struct scsi_initialize_element_status *scsi_cmd;
1890
1891         scsi_cmd = (struct scsi_initialize_element_status *)
1892                     &csio->cdb_io.cdb_bytes;
1893         bzero(scsi_cmd, sizeof(*scsi_cmd));
1894
1895         scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1896
1897         cam_fill_csio(csio,
1898                       retries,
1899                       cbfcnp,
1900                       /*flags*/ CAM_DIR_NONE,
1901                       tag_action,
1902                       /* data_ptr */ NULL,
1903                       /* dxfer_len */ 0,
1904                       sense_len,
1905                       sizeof(*scsi_cmd),
1906                       timeout);
1907 }
1908
1909 void
1910 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1911                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1912                      u_int8_t tag_action, 
1913                      u_int16_t element_address,
1914                      u_int8_t send_action_code,
1915                      struct scsi_send_volume_tag_parameters *parameters,
1916                      u_int8_t sense_len, u_int32_t timeout)
1917 {
1918         struct scsi_send_volume_tag *scsi_cmd;
1919
1920         scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1921         bzero(scsi_cmd, sizeof(*scsi_cmd));
1922
1923         scsi_cmd->opcode = SEND_VOLUME_TAG;
1924         scsi_ulto2b(element_address, scsi_cmd->ea);
1925         scsi_cmd->sac = send_action_code;
1926         scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1927
1928         cam_fill_csio(csio,
1929                       retries,
1930                       cbfcnp,
1931                       /*flags*/ CAM_DIR_OUT,
1932                       tag_action,
1933                       /* data_ptr */ (u_int8_t *) parameters,
1934                       sizeof(*parameters),
1935                       sense_len,
1936                       sizeof(*scsi_cmd),
1937                       timeout);
1938 }