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