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